fading effect

Started by
9 comments, last by albinopapa 8 years, 1 month ago

When I enter area a large number of objects will appear. I want to make the transition smooth, is it possible?

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.

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.

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...

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);

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

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

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

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 ;

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.

This topic is closed to new replies.

Advertisement