Jump to content
  • Advertisement
Sign in to follow this  
widmowyfox

fading effect

This topic is 843 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

Advertisement

I believe that is how games use to use fog, to hide the far clipping plane and objects 'popping' into the scene.  

 

You could define a cut-off depth that is less than your clipping depth, and interpolate between the cut-off and the clipping depth.  

 

The Z value of your pixel shader input struct or SV_Position position variable, should be normalized between 0.0f - 1.0f.  

 

Something like:

 

float cut_off_depth = 0.9f;
float alpha = (p_in.position.z - cut_off_depth) / (1.0f - cut_off_depth);
alpha = saturate(alpha);
Then just do some alpha blending.
 

 

Edited to correct alpha calculation for linear interpolation.

Edit2: a little cleaner without if block

 

PS, I believe to do alpha blending though, you are going to need a copy of the back buffer or previously rendered frame, just using the alpha value as I have it will only darken the image, but not blend in with the background.

Edited by albinopapa

Share this post


Link to post
Share on other sites

If you're using a pixel shader then you don't need to do any blending either, just get the final color of the pixel and blend (or lerp, or get creative with) it with your fog color by the alpha value that you get from the sample code that albinopapa supplied.  If you want to get fancier than that, look into volumetric effects or atmospheric scattering as well, as basic linear fog is really just the first step in shrouding your objects in mystery.

Share this post


Link to post
Share on other sites

I seem to be confused about something, in some tests I've run using the code I provided, even when object is like 10 units from the camera, the Z value is something like 0.995, while near the far end of the frustum at nearly 1000 units away, it's like 0.99998...

Share this post


Link to post
Share on other sites

This code seems to work pretty good.

 

In my case, the objects faded from full alpha to black from near the camera to far end of frustum.

float cut_off_depth = 0.999f;
float alpha = (p_in.position.z - cut_off_depth) / (1.0f - cut_off_depth);
alpha = 1.0f - saturate(alpha);

Share this post


Link to post
Share on other sites

I seem to be confused about something, in some tests I've run using the code I provided, even when object is like 10 units from the camera, the Z value is something like 0.995, while near the far end of the frustum at nearly 1000 units away, it's like 0.99998...

 

 

Yeah, it will be non-linear. It has to do perspective-correct rasterization:

 

http://www.gamedev.net/topic/539378-why-non-linear-depth-buffer/#entry4483438

Share this post


Link to post
Share on other sites

I have something like this 

if( gFogEnabled )
	{
		float fogLerp = saturate( (distToEye - gFogStart) / gFogRange ); 

		
		litColor = lerp(litColor, gFogColor, fogLerp);
	}

But objects only changing color by distance value(its still visible, and it doesnt look good when they dissapear or appear), I meant about transparency-to be less and less visible as it moves away.

to be less and less visible as it moves away
Edited by widmowyfox

Share this post


Link to post
Share on other sites

What are your fogStart and fogRange values?

 

Your code and my code is pretty similar, difference being I use the z value of the pixel instead of an eye position.

 

As far as invisible, that isn't really a thing.  Transparency is basically saying that the item is there, but you only need to draw what's behind it right? So, you interpolate a value between the front item and the background.  Which is what you are doing.  If the fogLerp is 0.0f, then it shows all of the litPixel and if fogLerp is 1.0f then it shows all of fogColor, or at least that's how it's suppose to work.  

 

I suspect the start and/or range values need to be tweeked

Share this post


Link to post
Share on other sites

I think that we still do not understand each other . I do not want to use short-range fog because it does not look good, I have small amount on far horizon, but objects appear close coz I want to optimize their number. I want to make objects more and more clear with the approach . From nothing to full color. Do you think this will work?

litColor.a = gMaterial.Diffuse.a * texColor.a-distToEye/500 ;
Edited by widmowyfox

Share this post


Link to post
Share on other sites

Something like that will work (though I'd assume you'd want a region close to you where they are all opaque throughout).

 

But if you're using alpha blending, you'll need to make sure that you're sorting all your objects by distance to the camera, and drawing back to front. This could become a performance issue on the GPU (overdraw, blending) or CPU (sorting) depending on how many objects you have and how big they are.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!