How to implement fog in a hlsl shader?

Posted By: Mare

How to implement fog in a hlsl shader? - 06/27/08 10:35

I have made some simple shaders for use in my game, actualy I modified those shaders from the "Introduction to shader programming" by Taco Cohen, and now I added some fog to my level and I noticed that no model that has a shader attached is affected by the fog. The question is simple: How do I implement the fog in the shader?

Thank you
Posted By: BoH_Havoc

Re: How to implement fog in a hlsl shader? - 06/27/08 11:23

in your vertex shader output struct declare this

float Fog : FOG;

and in the vertex shader itself do something like this:

float3 PosWorld = mul(Pos, matWorld);
float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
Out.Fog = ofog;

you also have to declare vecViewPos and vecFog of course so add this at the top of your fx file
float4 vecViewPos;
float4 vecFog;

that should be it.

You could also try to set
FogEnable = False;
in your technique. That might work for some shaders.
© 2024 lite-C Forums