displacement mapping

21
Displacement mapping Szécsi László

Upload: eve-vinson

Post on 03-Jan-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Displacement mapping. Sz écsi László. Let öltés. rock . obj rkd.jpg rbump.jpg rnormal.jpg. lua: Normal mappelt modell. O:IndexedMesh(_, {name='rock', file='rock. obj '}) O:Material(_, {name='normalMappedRock', technique='displacement', pass='normalMapped'}, function(_) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Displacement mapping

Displacement mapping

Szécsi László

Page 2: Displacement mapping

Letöltés

rock.obj

rkd.jpg

rbump.jpg

rnormal.jpg

Page 3: Displacement mapping

lua: Normal mappelt modellO:IndexedMesh(_, {name='rock', file='rock.obj'})

O:Material(_, {name='normalMappedRock', technique='displacement', pass='normalMapped'}, function(_)

O:setSrv(_, {effectVariable='kdTexture', file='rkd.jpg'})

O:setSrv(_, {effectVariable='bumpTexture', file='rbumb.jpg'})

O:setSrv(_, {effectVariable='normalTexture', file='rnormal.jpg'})

end )

O:MultiMesh(_, {name='normalMappedRock'}, function(_)

O:FlipMesh(_, {}, function(_)

O:ShadedMesh(_, {mien='basic',

indexedMesh='rock', material='normalMapped'})

end )

end )

Page 4: Displacement mapping

lua: entitás

O:StaticEntity(_, {name='rock0', multiMesh='normalMappedRock', position = { x=0, y=100, z=0} } )

Page 5: Displacement mapping

main.fx

#include <envmapped.fx>

#include <billboard.fx>

#include "displacement.fx"

Page 6: Displacement mapping

displacement.fxtechnique11 displacement

{

pass normalMapped

{

SetVertexShader ( CompileShader( vs_5_0, vsTrafo() ) );

SetGeometryShader( NULL );

SetRasterizerState( defaultRasterizer );

SetPixelShader( CompileShader( ps_5_0,

psNormalMapped() ) );

SetDepthStencilState( defaultCompositor, 0 );

SetBlendState( defaultBlender, float4( 0.0f, 0.0f,

0.0f, 0.0f ), 0xFFFFFFFF );

}

}

Page 7: Displacement mapping

textúra shader resourceok

Texture2D normalTexture;

Texture2D bumpTexture;

Page 8: Displacement mapping

pixel shader: tangent framefloat4 psNormalMapped(VsosTrafo input) : SV_TARGET {

float3 normal = normalize(input.normal);

float2 dtdx = ddx(input.tex);

float2 dtdy = ddy(input.tex);

float3 dpdx = ddx(input.worldPos);

float3 dpdy = ddy(input.worldPos);

float3 sTangent = (dpdx * dtdy.y - dpdy * dtdx.y);

float3 sBinormal = (dpdy * dtdx.x - dpdx * dtdy.x) ;

float3 N = normalize(cross(sBinormal, sTangent));

float3 B = normalize(cross(input.normal, sTangent));

float3 T = normalize(cross(B, input.normal));

return abs(N.xyzz);

}

Page 9: Displacement mapping

normálok

Page 10: Displacement mapping

pixel shader: normal map

float3 tangentNormal = normalize(normalTexture.Sample(linearSampler, input.tex).xzy -float3(0.5, 0.5, 0.5));

float3 worldNormal = B * tangentNormal.z + normal * tangentNormal.y + T * tangentNormal.x;

return abs(worldNormal.y) * kdTexture.Sample(linearSampler, input.tex);

}

Page 11: Displacement mapping

Normal mapped

Page 12: Displacement mapping

.luaO:Material(_, {name='parallaxMapped', technique='displacement',

pass='parallaxMapped'}, function(_)

O:setSrv(_, {effectVariable='kdTexture', file='rkd.jpg'})

O:setSrv(_, {effectVariable='bumpTexture', file='rbump.jpg'})

O:setSrv(_, {effectVariable='normalTexture', file='rnormal.jpg'})

end )

O:MultiMesh(_, {name='parallaxMappedRock'}, function(_)

O:FlipMesh(_, {}, function(_)

O:ShadedMesh(_, {mien='basic',

indexedMesh='rock', material='parallaxMapped'})

end )

end )

O:StaticEntity(_, {name='rock1', multiMesh='parallaxMappedRock', position = { x=50, y=100, z=0} } )

Page 13: Displacement mapping

parallax

pass parallaxMapped

{

SetVertexShader ( CompileShader( vs_5_0, vsTrafo() ) );

SetGeometryShader( NULL );

SetRasterizerState( defaultRasterizer );

SetPixelShader( CompileShader( ps_5_0, psParallaxMapped() ) );

SetDepthStencilState( defaultCompositor, 0 );

SetBlendState( defaultBlender, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );

}

Page 14: Displacement mapping

parallax – ez jön be a normalosba…

float bump_height = 0.1;

float bump = bumpTexture.Sample(linearSampler, input.tex);

float3 viewDir = normalize(eyePos - input.worldPos);

viewDir = float3(dot(viewDir, T), dot(viewDir, B), dot(viewDir, normal));

float2 texOffset = bump_height * viewDir.xy

// / viewDir.z

* bump;

input.tex += texOffset;

Page 15: Displacement mapping

parallax – ez marad a végén

float3 tangentNormal = normalize(normalTexture.Sample(linearSampler, input.tex).xzy -float3(0.5, 0.5, 0.5));

float3 worldNormal = B * tangentNormal.z + normal * tangentNormal.y + T * tangentNormal.x;

return abs(worldNormal.y) * kdTexture.Sample(linearSampler, input.tex);

Page 16: Displacement mapping

.luaO:Material(_, {name='reliefMapped', technique='displacement',

pass='reliefMapped'}, function(_)

O:setSrv(_, {effectVariable='kdTexture', file='rkd.jpg'})

O:setSrv(_, {effectVariable='bumpTexture', file='rbump.jpg'})

O:setSrv(_, {effectVariable='normalTexture', file='rnormal.jpg'})

end )

O:MultiMesh(_, {name='reliefMappedRock'}, function(_)

O:FlipMesh(_, {}, function(_)

O:ShadedMesh(_, {mien='basic',

indexedMesh='rock', material='reliefMapped'})

end )

end )

O:StaticEntity(_, {name='rock2', multiMesh='reliefMappedRock', position = { x=100, y=100, z=0} } )

Page 17: Displacement mapping

Binary reliefpass reliefMapped

{

SetVertexShader ( CompileShader( vs_5_0, vsTrafo() ) );

SetGeometryShader( NULL );

SetRasterizerState( defaultRasterizer );

SetPixelShader( CompileShader( ps_5_0, psReliefMapped() ) );

SetDepthStencilState( defaultCompositor, 0 );

SetBlendState( defaultBlender, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );

}

Page 18: Displacement mapping

ez ugyanazfloat4 psReliefMapped(VsosTrafo input) : SV_TARGET {

float3 normal = normalize(input.normal);

float2 dtdx = ddx(input.tex);

float2 dtdy = ddy(input.tex);

float3 dpdx = ddx(input.worldPos);

float3 dpdy = ddy(input.worldPos);

float3 sTangent = (dpdx * dtdy.y - dpdy * dtdx.y);

float3 sBinormal = (dpdy * dtdx.x - dpdx * dtdy.x) ;

float3 N = normalize(cross(sBinormal, sTangent));

float3 B = normalize(cross(input.normal, sTangent));

float3 T = normalize(cross(B, input.normal));

float bump_height = 0.2;

float bump = bumpTexture.Sample(linearSampler, input.tex);

float3 viewDir = normalize(eyePos - input.worldPos);

viewDir = float3(dot(viewDir, T), dot(viewDir, B), dot(viewDir, normal));

Page 19: Displacement mapping

psReliefMapped folyt.float3 sRange = - viewDir * bump_height / viewDir.z *

0.5;float3 sPos = float3(input.tex, 0) - sRange;

for( int i=0; i<6; i++ ){

float bump = bumpTexture.Sample(linearSampler, sPos.xy);sRange *= 0.5;if (sPos.z > bump * bump_height) // If outside

sPos += sRange; // Move forwardelse

sPos -= sRange; // Move backward}sPos -= 4 * sRange;input.tex = sPos.xy;

Page 20: Displacement mapping

psBinaryRelief folyt.float3 tangentNormal = normalize(tex2D(normalMapSampler, input.tex).xzy -float3(0.5, 0.5, 0.5));

float3 worldNormal = B * tangentNormal.z + normal * tangentNormal.y + T * tangentNormal.x;

return abs(worldNormal.y) * tex2D(kdMapSampler, input.tex); }

Page 21: Displacement mapping

Relief mapping