I'm trying to implement fog effect using HLSL, the following code doesn't work correctly:
Pixel Shader:
float fogStart = 1;
float fogEnd = 100;
float3 cameraPosition = IN.worldPos.xyz;
cameraPosition = mul(cameraPosition, View);
float fogFactor = saturate((fogEnd - cameraPosition.z) / (fogEnd - fogStart));
float4 fogColor = float4(1.0f, 0.0f, 0.0f, 1.0f);
// Calculate the final color using the fog effect equation.
float4 fog = fogFactor + (1.0 - fogFactor) * fogColor;
return tex2D( textureSamp, IN.TexCoord) * fog;