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

This topic is 4606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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.

##### Share on other sites
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

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013710
×