float4x4 World; ///< World matrix for the current object float4x4 WorldIT; ///< World matrix IT (inverse transposed) to transform surface normals of the current object float4x4 WorldView; float4x4 WorldViewIT; float4x4 WorldViewProj; float3 referencePos; float3 eyePos; float3 F0; // Freshnel factor float N0; // Refraction coefficient texture colorMap; sampler colorSampler = sampler_state { MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = POINT; Texture = ; AddressU = WRAP; AddressV = WRAP; }; texture envCube; sampler envCubeSampler = sampler_state { MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = POINT; Texture = ; AddressU = WRAP; AddressV = WRAP; }; //------------------------------------------------------------------------- // Technique: Simple textured object //------------------------------------------------------------------------- struct Textured_VS_OUT { float4 hPosition :POSITION0; float2 texCoord :TEXCOORD0; }; Textured_VS_OUT texturedVS(float4 position : POSITION0, float2 texCoord : TEXCOORD0) { Textured_VS_OUT OUT; OUT.hPosition = mul(position, WorldViewProj); OUT.texCoord = texCoord; return OUT; } float4 texturedPS(Textured_VS_OUT IN) : COLOR0 { return tex2D(colorSampler, IN.texCoord); } technique Textured { pass p0 { VertexShader = compile vs_2_0 texturedVS(); PixelShader = compile ps_2_0 texturedPS(); } } //------------------------------------------------------------------------- // Technique: Classical environment mapping //------------------------------------------------------------------------- struct EnvMapped_VS_OUT { float4 hPosition : POSITION0; float2 texCoord : TEXCOORD0; float3 wNormal : TEXCOORD1; //world normal float3 cmPos : TEXCOORD2; //cubemap position float3 V : TEXCOORD3; //view direction in world space }; EnvMapped_VS_OUT EnvMappedVS(float4 position : POSITION0, float2 texCoord : TEXCOORD0, float3 normal : NORMAL) { EnvMapped_VS_OUT OUT; OUT.hPosition = mul(position, WorldViewProj); OUT.wNormal = mul(normal, WorldIT); OUT.cmPos = mul(position, World).xyz; OUT.V = OUT.cmPos - eyePos; OUT.cmPos -= referencePos; OUT.texCoord = texCoord; return OUT; } float4 EnvMappedPS(EnvMapped_VS_OUT IN) : COLOR0 { float3 N = normalize(IN.wNormal); float3 V = normalize(IN.V); float3 R; float3 I = 1; float3 F = F0 + pow(1-dot(N, -V), 5) * (1 - F0); if (N0 <= 0) // reflective material { R = reflect(V, N); I *= F; // Fresnel reflection } else //refractive material { R = refract(V, N, N0); if (dot(R, R) == 0) // no refraction direction exits R = reflect(V, N); // total reflection else I *= (1 - F); // Fresnel refraction } return texCUBE(envCubeSampler, R) * float4(I,1); } technique EnvMapped { pass p0 { VertexShader = compile vs_2_0 EnvMappedVS(); PixelShader = compile ps_2_0 EnvMappedPS(); } } //------------------------------------------------------------------------- // Technique: write color and distance from camera //------------------------------------------------------------------------- struct ColorDist_VS_OUT { float4 hPosition : POSITION0; float2 texCoord : TEXCOORD0; float3 cPos : TEXCOORD1; //view space position }; ColorDist_VS_OUT ColorDistVS(float4 position : POSITION0, float2 texCoord : TEXCOORD0) { ColorDist_VS_OUT OUT; OUT.hPosition = mul(position, WorldViewProj); OUT.cPos = mul(position, WorldView).xyz; OUT.texCoord = texCoord; return OUT; } float4 ColorDistPS(ColorDist_VS_OUT IN) : COLOR0 { float4 color = tex2D(colorSampler, IN.texCoord); color.a = length(IN.cPos); return color; } technique ColorDistance { pass p0 { VertexShader = compile vs_2_0 ColorDistVS(); PixelShader = compile ps_2_0 ColorDistPS(); } } //------------------------------------------------------------------------- // Technique: write normal and distance from camera //------------------------------------------------------------------------- struct NormalDist_VS_OUT { float4 hPosition : POSITION0; float2 texCoord : TEXCOORD0; float3 wNormal : TEXCOORD1; //world normal position float3 cPos : TEXCOORD2; //view space position }; NormalDist_VS_OUT NormalDistVS(float4 position : POSITION0, float2 texCoord : TEXCOORD0, float3 normal : NORMAL) { NormalDist_VS_OUT OUT; OUT.hPosition = mul(position, WorldViewProj); OUT.wNormal = mul(normal, WorldIT).xyz; OUT.cPos = mul(position, WorldView).xyz; OUT.texCoord = texCoord; return OUT; } float4 NormalDistPS(NormalDist_VS_OUT IN) : COLOR0 { float4 color; color.rgb = normalize(IN.wNormal); color.a = length(IN.cPos); return color; } technique NormalDistance { pass p0 { VertexShader = compile vs_2_0 NormalDistVS(); PixelShader = compile ps_2_0 NormalDistPS(); } }