To GG:
Here is your shader with the fog code added:
// Green Gandalf's Blended Normal Mapping Shader
//
// designed for use with Advanced Terrain objects
//
// contains two techniques: with and without bump mapping
//
// Created 7 March 2007, edited 11 August 2008.
//
// Bumpmapping using blended texture and normal maps with a single directional
// light source, plus ambient lighting.
matrix wvp : WorldViewProjection;
matrix mw : World;
matrix winv: WorldInverse;
vector EyePosition : EyePosition;
float4 lightDir = {0.707107f, -0.707107f, 0.0f, 1.0f};
float4 ambiColor = {0.2f, 0.2f, 0.2f, 1.0f};
float4 lightColor = {1.0f, 1.0f, 1.0f, 1.0f};
float2 UVTiling = {4, 4};
float vertScale = 0.01; // reciprocal of maximum height of terrain
float contrast = 1.0; // higher values increase the contrast between textures
float4 fogColor = { 1.0f , 1.0f, 1.0f, 1.0f}; //Added
float fogDistance = 1000.0; //Added
texture detailMap1 < string ResourceName = ""; >;
texture detailMap2 < string ResourceName = ""; >;
texture detailMap3 < string ResourceName = ""; >;
texture detailMap4 < string ResourceName = ""; >;
texture normalMap1 < string ResourceName = ""; >;
texture normalMap2 < string ResourceName = ""; >;
texture normalMap3 < string ResourceName = ""; >;
texture normalMap4 < string ResourceName = ""; >;
sampler2D detail1 = sampler_state
{ texture = <detailMap1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D detail2 = sampler_state
{ texture = <detailMap2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D detail3 = sampler_state
{ texture = <detailMap3>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D detail4 = sampler_state
{ texture = <detailMap4>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D nMap1 = sampler_state
{ texture = <normalMap1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D nMap2 = sampler_state
{ texture = <normalMap2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D nMap3 = sampler_state
{ texture = <normalMap3>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D nMap4 = sampler_state
{ texture = <normalMap4>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
struct VS_INPUT
{ float4 Pos : POSITION;
float3 Tangent : TANGENT;
float3 Normal : NORMAL;
float2 Tex : TEXCOORD0;
};
struct VS_OUTPUT
{ float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float3 Light : TEXCOORD1;
float4 Blender : TEXCOORD2;
float Fog : FOG; //Added
};
struct VS_OUTPUT2
{ float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float Diffuse : TEXCOORD1;
float4 Blender : TEXCOORD2;
float Fog : FOG; //Added
};
VS_OUTPUT GGBlendBumpVShader(VS_INPUT In, VS_OUTPUT Out)
{ float4 tempPos = mul(In.Pos, mw);
Out.Pos = mul(In.Pos, wvp);
Out.Tex = In.Tex * UVTiling;
//smooth tangents/binormals - trick copied from Dark Shader samples
float3 normal = normalize(In.Normal);
float3 binormal = normalize(cross(normal, normalize(In. Tangent)));
float3 tangent = normalize(cross(binormal, normal));
float3x3 TSM = {tangent, binormal, normal};
TSM = transpose(TSM); // tangent space matrix
float3 temp = -mul(lightDir.xyz, winv);
Out.Light = normalize(mul(temp,TSM));
float2 hn = float2 (tempPos.y * vertScale, normal.y);
hn = saturate(0.5.xx + contrast * (hn - 0.5.xx));
Out.Blender = float4 (hn.x * hn.y, hn.x * (1 - hn.y),
(1 - hn.x) * hn.y, (1 - hn.x) * (1 - hn.y));
// e.g. blends = grass, rock, sand, gravel
Out.Fog = 1.0f - (length(In.Pos - EyePosition) / fogDistance); //Added
return Out;
}
VS_OUTPUT2 GGBlendVShader(VS_INPUT In, VS_OUTPUT2 Out)
{ float4 tempPos = mul(In.Pos, mw);
Out.Pos = mul(In.Pos, wvp);
Out.Tex = In.Tex * UVTiling;
Out.Diffuse = saturate(dot(normalize(In.Normal), -normalize(lightDir.xyz)));
float2 hn = float2 (tempPos.y * vertScale, In.Normal.y);
hn = saturate(0.5.xx + contrast * (hn - 0.5.xx));
Out.Blender = float4 (hn.x * hn.y, hn.x * (1 - hn.y),
(1 - hn.x) * hn.y, (1 - hn.x) * (1 - hn.y));
// e.g. blends = grass, rock, sand, gravel
Out.Fog = 1.0f - (length(In.Pos - EyePosition) / fogDistance); //Added
return Out;
}
struct PS_INPUT
{ float2 Tex : TEXCOORD0;
float3 Light : TEXCOORD1;
float4 Blender : TEXCOORD2;
};
struct PS_INPUT2
{ float2 Tex : TEXCOORD0;
float Diffuse : TEXCOORD1;
float4 Blender : TEXCOORD2;
};
struct PS_OUTPUT
{ float4 col : COLOR;
};
PS_OUTPUT GGBlendBumpPShader(PS_INPUT In, PS_OUTPUT Out)
{ float3 tempLightDir = In.Light;
float4 base1 = tex2D(detail1, In.Tex) * In.Blender.x;
float4 base2 = tex2D(detail2, In.Tex) * In.Blender.y;
float4 base3 = tex2D(detail3, In.Tex) * In.Blender.z;
float4 base4 = tex2D(detail4, In.Tex) * In.Blender.w;
float diffuse1 = saturate(dot(2 * tex2D(nMap1, In.Tex).xyz - 1.0, tempLightDir));
float diffuse2 = saturate(dot(2 * tex2D(nMap2, In.Tex).xyz - 1.0, tempLightDir));
float diffuse3 = saturate(dot(2 * tex2D(nMap3, In.Tex).xyz - 1.0, tempLightDir));
float diffuse4 = saturate(dot(2 * tex2D(nMap4, In.Tex).xyz - 1.0, tempLightDir));
float4 base = base1 + base2 + base3 + base4;
float4 diffuse = base1 * diffuse1;
diffuse += base2 * diffuse2;
diffuse += base3 * diffuse3;
diffuse += base4 * diffuse4;
Out.col = base * ambiColor + diffuse * lightColor;
return Out;
}
PS_OUTPUT GGBlendPShader(PS_INPUT2 In, PS_OUTPUT Out)
{ float4 base1 = tex2D(detail1, In.Tex) * In.Blender.x;
float4 base2 = tex2D(detail2, In.Tex) * In.Blender.y;
float4 base3 = tex2D(detail3, In.Tex) * In.Blender.z;
float4 base4 = tex2D(detail4, In.Tex) * In.Blender.w;
float4 base = base1 + base2 + base3 + base4;
Out.col = base * (ambiColor + In.Diffuse * lightColor);
return Out;
}
technique t0 // use this for blending only
{ pass p0
{
FOGENABLE = (3); //Added
FOGCOLOR = (fogColor); //Added
VertexShader = compile vs_2_0 GGBlendVShader();
PixelShader = compile ps_2_0 GGBlendPShader();
}
}
technique t1 // use this for blending and bump mapping
{ pass p0
{
FOGENABLE = (3); //Added
FOGCOLOR = (fogColor); //Added
VertexShader = compile vs_2_0 GGBlendBumpVShader();
PixelShader = compile ps_2_0 GGBlendBumpPShader();
}
}
THX