Sign in to follow this  
Medo Mex

HLSL Fog Effect

Recommended Posts

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 =;
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;


Share this post

Link to post
Share on other sites



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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this