Here is the shader:
// Volume Fog
// By CuCuMBeR
//--------------------------------
float4x4 World : World;
float4x4 WorldViewProject : WorldViewProjection;
float3 EyePosition : CameraPosition;
float3 LightPosition = (0,-1,0);
float4 ambient = {0.25, 0.25, 0.25, 1.0};
float4 diffuse = {1.0, 1.0, 1.0, 1.0};
float4 specularColor = {0.2, 0.2, 0.2, 1.0};
float shininess = 40;
float fogNear = 10;
float fogFar = 100;
float fogAltitudeScale = 10;
float fogThinning = 100;
float4 fogColor = {0.5, 0.5, 0.5, 1.0};
texture BaseTX
<
string Name="";
>;
sampler TextureSampler = sampler_state
{
Texture = <BaseTX>;
};
struct VS_INPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
float3 Normal : NORMAL0;
};
struct VS_OUTPUT
{
float4 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 WorldPos : TEXCOORD2;
};
struct PS_INPUT
{
float4 Position : TEXCOORD4;
float2 Texcoord : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 WorldPos : TEXCOORD2;
};
float4 blinn2(
float3 N,
float3 L,
float3 V,
uniform float4 diffuseColor,
uniform float4 specularColor,
uniform float shininess)
{
float3 H = normalize(V+L);
float4 lighting = lit(dot(L,N), dot(H,N), shininess);
return diffuseColor*lighting.y + specularColor*lighting.z;
}
VS_OUTPUT Transform(VS_INPUT Input)
{
VS_OUTPUT Output;
Output.WorldPos = mul(Input.Position, World);
Output.Position = mul(Input.Position, WorldViewProject);
Output.Texcoord = Input.Texcoord;
Output.Normal = mul(Input.Normal, World);
return Output;
}
float4 Texture(PS_INPUT Input) : COLOR0
{
float4 colorMap = tex2D(TextureSampler, Input.Texcoord.xy) * 1.5;
float3 N = normalize(Input.Normal);
float3 V = normalize(EyePosition - Input.WorldPos);
float3 L = normalize(LightPosition - Input.WorldPos);
float4 C = ambient*colorMap;
C += blinn2(N, L, V, colorMap * diffuse, specularColor * colorMap.a, shininess);
float d = length(Input.WorldPos - EyePosition);
float l = saturate((d - fogNear) / (fogFar - fogNear) / clamp(Input.Position.y / fogAltitudeScale + 1, 1, fogThinning));
return lerp(C, fogColor, l);
};
technique TransformTexture
{
pass P0
{
VertexShader = compile vs_1_1 Transform();
PixelShader = compile ps_2_0 Texture();
}
}
Attached is the sample project file with everything you need.