Theory behind per-pixel distance fog with shaders...

Started by
1 comment, last by dave 17 years, 11 months ago
How does the pixel shader find out how far away a pixel is supposed to be? Let's say this is the pixel shader that I want should smoothly make color change over to 0x9999CC in the distance:

float4 PixelShader( float2 tc : TEXCOORD0 ) : COLOR0
{
    float4 Color;

    Color = tex2D( g_samSrcColor, tc.xy);
    return Color;
}
I can access color in screen coordinates by tc.x, tc.y and tc.xy, but how do I access distance/depth buffer value or whatever I should use to calculate distance fog? You can assume linear distance fog, I think it's trivial for me to change to exponential etc. once I get the hang of how to do the basics. Thanks!
Advertisement
I have a suggestion, or just something that might spark an idea,
i never messed with that stuff before but i'm sure i will someday:

Try and lock the Z buffer and retrieve the color from the pixel in it that
you want to calculate the distance for, then (perhaps you can) convert the dword to a float to retrieve the distance? I've never tried this before but i'm sure you could do something like that.
In the vertex shader find the distance between the vertex and the camera and pass it through to the pixel shader as a parameter. This is for pervertex fog.

The slower, not so noticably different but more accurate method is to pass the position through to the vertex shader and work out the distance in there, for per pixel fog.

Hope that helps,

Dave

This topic is closed to new replies.

Advertisement