// // Globals // texture Top; texture Slope; texture Bottom; //texture Tex; vector Ambient; vector Sun_Direction; float4x4 g_mWorldViewProjection; float4x4 g_mWorldView; float FarPlaneMinusNearPlane; // Distance of the far plane minus distance of the near plane. // // Sampler // sampler TopTex = sampler_state { Texture = (Top); MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; }; sampler SlopeTex = sampler_state { Texture = (Slope); MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; }; sampler BottomTex = sampler_state { Texture = (Bottom); MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; }; // // Structures // struct VS_OUTPUT { float4 pos : POSITION; float2 coord1 : TEXCOORD0; float3 Normal : TEXCOORD1; vector values : COLOR0; }; struct VS_OUTPUT_RAY { float4 pos : POSITION; float2 coord1 : TEXCOORD0; float3 Normal : TEXCOORD1; float3 pos2 : TEXCOORD2; vector values : COLOR0; }; struct VS_OUTPUT_DEPTH { float4 HPosition : POSITION; float4 texCoord : TEXCOORD0; }; struct PS_OUTPUT { float4 diffuse : COLOR0; }; // // VertexShader Main // VS_OUTPUT vs_main(float4 inPos: POSITION, float3 inNormal: NORMAL, float2 inCoord1: TEXCOORD0, vector inValues : COLOR0) { VS_OUTPUT Out; Out.pos = mul(inPos, g_mWorldViewProjection ); Out.coord1 = inCoord1; Out.Normal = inNormal; Out.values = inValues; return Out; } VS_OUTPUT_RAY vs_main_ray(float4 inPos: POSITION, float3 inNormal: NORMAL, float2 inCoord1: TEXCOORD0, vector inValues : COLOR0) { VS_OUTPUT_RAY Out; Out.pos = mul(inPos, g_mWorldViewProjection ); Out.coord1 = inCoord1; Out.Normal = inNormal; Out.values = inValues; Out.pos2 = mul(inPos, g_mWorldView ); return Out; } VS_OUTPUT_DEPTH vs_main_depth(float4 position : POSITION, float4 texCoord1 : TEXCOORD0) { VS_OUTPUT_DEPTH OUT; float4 vertexpos = float4(position.xyz, 1.0); float4 eyespace = mul(vertexpos, g_mWorldViewProjection); OUT.HPosition = eyespace; float tempDepth = saturate(eyespace.z/FarPlaneMinusNearPlane); texCoord1.a = tempDepth; OUT.texCoord = texCoord1; return OUT; } //per pixel lighting function float4 Light_PointDiffuse(vector LightDir, float3 Normal) { // Compute suface/light angle based attenuation defined as dot(N,L) float AngleAttn = clamp(0, 1, dot(Normal, LightDir.xyz) ); return AngleAttn; } // // PixelShader Main // PS_OUTPUT ps_main(float2 coord1 : TEXCOORD0, float3 Normal : TEXCOORD1, vector values : COLOR0) { // zero out members of output PS_OUTPUT output = (PS_OUTPUT)0; // sample appropriate textures vector t = tex2D(TopTex, coord1); vector s = tex2D(SlopeTex, coord1); vector b = tex2D(BottomTex, coord1); // combine texel colors vector c = t*values.x + s*values.y + b*values.z; // do per-pixel-lighting output.diffuse = c * Light_PointDiffuse(Sun_Direction,Normal) + 0.1; //return result return output; } PS_OUTPUT ps_main_ray(float2 coord1 : TEXCOORD0, float3 Normal : TEXCOORD1, float3 pos2 : TEXCOORD2, vector values : COLOR0) { // zero out members of output PS_OUTPUT output = (PS_OUTPUT)0; // sample appropriate textures vector t = tex2D(TopTex, coord1); vector s = tex2D(SlopeTex, coord1); vector b = tex2D(BottomTex, coord1); // combine texel colors vector c = t*values.x + s*values.y + b*values.z; // do per-pixel-lighting output.diffuse = c * Light_PointDiffuse(Sun_Direction,Normal) + 0.1; // The distance output.diffuse.a = (float) ( length( pos2.xyz ) ); //return result return output; } PS_OUTPUT ps_main_depth(VS_OUTPUT_DEPTH IN) { PS_OUTPUT OUT; float depth = IN.texCoord.a; OUT.diffuse = float4(depth, depth, depth, depth); return OUT; } // // Effect // technique TerrainShader { pass P0 { // // Set Misc render states. vertexshader = compile vs_2_0 vs_main(); pixelshader = compile ps_2_0 ps_main(); fvf = XYZ | Normal | Tex1; Lighting = false; NormalizeNormals = false; SpecularEnable = false; } } technique TerrainShaderRay { pass P0 { // // Set Misc render states. vertexshader = compile vs_2_0 vs_main_ray(); pixelshader = compile ps_2_0 ps_main_ray(); fvf = XYZ | Normal | Tex1; Lighting = false; NormalizeNormals = false; SpecularEnable = false; } } technique TerrainShaderDepth { pass P0 { // // Set Misc render states. vertexshader = compile vs_2_0 vs_main_depth(); pixelshader = compile ps_2_0 ps_main_depth(); fvf = XYZ | Normal | Tex1; Lighting = false; NormalizeNormals = false; SpecularEnable = false; } }