HLSL Fog Effect

Started by
0 comments, last by Medo Mex 10 years, 9 months ago

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;

Advertisement

Resolved.

I had to multiply "cameraPosition" with the projection matrix, I also had to multiply the texture sampler with fogFactor inside the fog equation.

This topic is closed to new replies.

Advertisement