• Advertisement
Sign in to follow this  

HLSL Fog Effect

This topic is 1676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement