Question

I struggled for some time to add a fog effect in my xna games. I work with a custom shader effect in a file (. Fx). The "PixelShaderFunction" works without error. But the problem is that all my land is colored the same way. I think the problem come from the calculation of the distance between the camera and the model.

float distance = length(input.TextureCoordinate - cameraPos);

Here is my complete code with "PixelShaderFunction"

// Both techniques share this same pixel shader.
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
      float distance = length(input.TextureCoordinate - cameraPos);
      float l = saturate((distance-fogNear)/(fogFar-fogNear));
      return tex2D(Sampler, input.TextureCoordinate) * lerp(input.Color, fogColor, l);
}
Était-ce utile?

La solution

If your input.TextureCoordinate really represents texture coordinates for sampler, than the way you trying to calculate distance is wrong.

You can change body of your PixelShaderFunction as follows:

float distance = distance(cameraPos, input.Position3D);
float l = saturate((distance-fogNear)/(fogFar-fogNear));
return lerp(tex2D(Sampler, input.TextureCoordinate), fogColor, l);

Add the following to your VertexShaderOutput declaration:

float4 Position3D : TEXCOORD1;

In your Vertex Shader populate Position3D with the position of the vertex multiplied on world matrix:

output.Position3D = mul(input.pos, matWorld);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top