How add fog with pixel shader (HLSL) XNA?
- by Mehdi Bugnard
I started to make a small game in XNA . And recently i tried to add a "fog" on "pixel shader HLSL" with the class Effect from XNA .
I search online about some tutorial and found many sample. But nothing want work on my game :-( .
Before i already add a "fog" effect in my game and everything work, because i used the class "BasicEffect" but with the class "Effect" and HLSL, it's really more complicated.
If somebody have an idea, it's will be wonderfull.
Thanks again.
Here is my code HLSL, i use.
// Both techniques share this same pixel shader.
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
//return tex2D(Sampler, input.TextureCoordinate) * input.Color;
float d = length(input.TextureCoordinate - cameraPos);
float l = saturate((d-fogNear)/(fogFar-fogNear));
float fogFactory = clamp((d - fogNear) / (fogFar - fogNear), 0, 1) * l;
return tex2D(Sampler, input.TextureCoordinate) * lerp(input.Color, fogColor, fogFactory);
}
Here is the screenShot
With effect
Without effect