How add fog with pixel shader (HLSL) XNA?

Posted by Mehdi Bugnard on Game Development See other posts from Game Development or by Mehdi Bugnard
Published on 2014-08-20T16:00:11Z Indexed on 2014/08/20 16:37 UTC
Read the original article Hit count: 467

Filed under:
|
|

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 enter image description here Without effect enter image description here

© Game Development or respective owner

Related posts about XNA

Related posts about hlsl