Jump to content
  • Advertisement
Sign in to follow this  
all_names_taken

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!