190 likes | 294 Views
Displacement mapping. Sz écsi László. Let öltés. big.x rkd.jpg rbump.jpg rnormal.jpg. Normal map .xml. <Mesh name="rock" xFileName="media\big.x" autoShadedMesh="false" /> <ShadedMesh name="rockNormal" mesh="rock" > <Role name="basic"> <Material technique="normalMapped">
E N D
Displacement mapping Szécsi László
Letöltés • big.x • rkd.jpg • rbump.jpg • rnormal.jpg
Normal map .xml • <Mesh name="rock" xFileName="media\\big.x" • autoShadedMesh="false" /> • <ShadedMesh name="rockNormal" mesh="rock" > • <Role name="basic"> • <Material technique="normalMapped"> • <Texture name="kdMap" file="rkd.jpg"/> • <Texture name="bumpMap" • file="rbump.jpg"/> • <Texture name="normalMap" • file="rnormal.jpg"/> • </Material> • </Role> • </ShadedMesh>
XML • <PhysicsEntity name="rockNormal" shadedMesh="rockNormal" physicsModel="ground" position.x="-100" position.z="-50"/>
displacement.fx • technique normalMapped • { • pass ExamplePass • { • VertexShader = compile vs_2_0 vsTrafo(); • PixelShader = compile ps_3_0 psNormalMapped(); • } • }
samplerek • texture normalMap; • sampler2D normalMapSampler = sampler_state{ • texture = < normalMap >; • MinFilter = LINEAR; • MagFilter = LINEAR; • MipFilter = LINEAR; • AddressU = Wrap; • AddressV = Wrap; • }; • texture bumpMap; • sampler2D bumpMapSampler = sampler_state{ • texture = < bumpMap >; • MinFilter = LINEAR; • MagFilter = LINEAR; • MipFilter = LINEAR; • AddressU = Wrap; • AddressV = Wrap; • };
pixel shader: tangent frame • float4 psNormalMapped(TrafoOutput input) : COLOR0{ • 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); }
pixel shader: normal map • … • 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); • }
.xml • <ShadedMesh name="rockParallax" mesh="rock" > • <Role name="basic"> • <Material technique="parallax"> • <Texture name="kdMap" file="rkd.jpg"/> • <Texture name="bumpMap" • file="rbump.jpg"/> • <Texture name="normalMap" • file="rnormal.jpg"/> • </Material> • </Role> • </ShadedMesh>
XML • <PhysicsEntity name="rockParallax" shadedMesh="rockParallax" physicsModel="ground" position.x="0" position.z="-50"/>
parallax • technique parallax • { • pass ExamplePass • { • VertexShader = compile vs_2_0 vsTrafo(); • PixelShader = compile ps_3_0 psParallax(); • } • }
parallax • … • float bump_height = 0.1; • float bump = tex2D(bumpSampler, input.tex); • float3 viewDir = normalize(eyePosition - input.worldPos); • viewDir = float3(dot(viewDir, T), dot(viewDir, B), dot(viewDir, normal)); • float2 texOffset = 0.09 * viewDir.xy • // / viewDir.z • * bump; • input.tex += texOffset;
parallax • 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);
.xml • <ShadedMesh name="rock" mesh="rock" > • <Role name="basic"> • <Material technique="binaryRelief"> • <Texture name="kdMap" file="rkd.jpg"/> • <Texture name="bumpMap" • file="rbump.jpg"/> • <Texture name="normalMap" • file="rnormal.jpg"/> • </Material> • </Role> • </ShadedMesh>
XML • <PhysicsEntity name="rock" shadedMesh="rock" physicsModel="ground" position.x="100" position.z="-50"/>
Binary relief • technique binaryRelief • { • pass ExamplePass • { • VertexShader = compile vs_2_0 vsTrafo(); • PixelShader = compile ps_3_0 psBinaryRelief(); • } • }
ez ugyanaz • float4 psBinaryRelief(TrafoOutput input) : COLOR0 • { • 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); • float k = dtdx.x * dtdy.y - dtdx.y * dtdy.x; • float3 sTangent = (dpdx * dtdy.y - dpdy * dtdx.y); • float3 sBinormal = (dpdy * dtdx.x - dpdx * dtdy.x) ; • float3 B = normalize(cross(input.normal, sTangent)); • float3 T = normalize(cross(B, input.normal)); • float3 viewDir = normalize(eyePosition - input.worldPos); • viewDir = float3(dot(viewDir, T), dot(viewDir, B), dot(viewDir, normal));
psBinaryRelief folyt. • float bump_height = 0.1; • 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 = tex2D(bumpMapSampler, 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)); • tangentNormal += dot(tangentNormal.xzy, viewDir) * viewDir; • tangentNormal = normalize(tangentNormal); • float3 worldNormal = B * tangentNormal.z + normal * tangentNormal.y + T * tangentNormal.x; • return abs(worldNormal.y) * tex2D(kdMapSampler, input.tex);}