240 likes | 396 Views
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(_)
E N D
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(_) 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 )
lua: entitás • O:StaticEntity(_, {name='rock0', multiMesh='normalMappedRock', position = { x=0, y=100, z=0} } )
main.fx • #include <envmapped.fx> • #include <billboard.fx> • #include "displacement.fx"
displacement.fx technique11 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 ); } }
textúra shader resourceok • Texture2D normalTexture; • Texture2D bumpTexture;
pixel shader: tangent frame float4 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); }
MÁSIK MÓDSZER I structIaosTangent { float4 pos: POSITION; float3 normal : NORMAL; float3 tangent : TANGENT; float3 binormal : BINORMAL; float2 tex : TEXCOORD; }; structVsosTangent { float4 pos : SV_POSITION; float4 worldPos : WORLDPOS; float3 normal : NORMAL; float3 tangent : TANGENT; float3 binormal : BINORMAL; float2 tex : TEXCOORD; };
MÁSIK MÓDSZER II VsosTangentvsTangent(IaosTangent input) { VsosTangent output = (VsosTangent)0; output.pos = mul(input.pos, modelViewProjMatrix); output.worldPos = mul(input.pos, modelMatrix); output.normal = mul(modelMatrixInverse, float4(input.normal.xyz, 0.0)); output.tangent = mul(modelMatrixInverse, float4(input.tangent.xyz, 0.0)); output.binormal = mul(modelMatrixInverse, float4(input.binormal.xyz, 0.0)); output.tex = input.tex; return output; }
MÁSIK MÓDSZER III float4 psNormalMapped(VsosTangent input) : SV_TARGET { float3 T= normalize(input.tangent.xyz); float3 B= -normalize(input.binormal.xyz); float3 N = normalize(input.normal.xyz);
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); }
.lua O: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} } )
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 ); }
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;
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);
.lua O: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} } )
Binary relief pass 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 ); }
ez ugyanaz float4 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));
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 forward else sPos -= sRange; // Move backward } sPos -= 4 * sRange; input.tex = sPos.xy;
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);}