Render Distance Fade

Started by
3 comments, last by Erik Rufelt 14 years, 2 months ago
Hi Everyone, Does anyone have any advice on how to make more distant object fade away. I've been using the near/far view-plane setting in the D3DXMatrixPerspectiveFovLH(); function but this is is a sudden clip. Is there a way to set a gradiant on that?? Or is it possible to use the fog effect with an alpha channel or something? Any Advice would be great! Many Thanks David
Advertisement
Are you using shaders or the Fixed Function Pipeline?

In fixed function you might want to check out this reference: http://msdn.microsoft.com/en-us/library/ee418582%28VS.85%29.aspx

"Fog" Is basically a way to fade things out in the distance to a certain color. This way you won't get that sudden pop / clip.

If you're using shaders / programmable pipeline, you'd have to implement the same functionality in your shader, which is basically taking the depth of the vertex or pixel and multiplying it by the fog color, and multiplying the final color by that.

You can derive depth by dividing Position.z / Position.w after the vertex is transformed to screen space, or divide the Z by the ZFar to get a more linear result.
Thanks for your quick response.
I am using a fixed Pipeline.
Is it not possible for the fog not to fade to a colour but to fade to nothing? So that a background image would show through it and not be washed out.
So it would kinda work like an opacity map.

Thanks
David
As far as I can tell, the alpha component of the RGBA fog color in the fixed function approach is ignored. I think your only choice would be to use a pixel shader with alpha blending enabled and decrement the alpha component based on the depth.
Quote:Original post by thekiwimaddog
Or is it possible to use the fog effect with an alpha channel or something?


It's possible to achieve with multi-texturing. You can set up the first texture as a 1D alpha-texture, fading to transparent at the end. Then you use automatic texture coordinate generation to set the texture coordinate based on Z-distance from the camera, and set up multi-texturing and blending to multiply the alpha of this texture with the alpha in the next texture (the real one that should be visible).

So what you need to do is enable the fade texture like pDevice->SetTexture(0, pFadeTexture), then set your real texture to stage 1, with pDevice->SetTexture(1, pTexture). Then you enable alpha-blending, and set the texture stage states, something like:
pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);pDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);pDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);pDevice->SetTransform(D3DTS_TEXTURE0, &matTexture);pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);pDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_BLENDCURRENTALPHA);


I tried it out and it seems to work fine. Post back if it's a method that fits with your game, and I can post an example or more details if needed. The states can probably be tweaked to achieve different effects depending on your needs. Not sure how easy it is to make it work if you already use multi-texturing to blend different stages together.

EDIT: Actually it probably won't work exactly like that as there will be multiple blending if there is overdraw. To work with that, disable blending and use a back-buffer with an alpha channel, so that the distance-based alpha is written to the back-buffer. Then after drawing is complete, draw the background on top, blending with destination alpha.

[Edited by - Erik Rufelt on February 8, 2010 12:35:19 AM]

This topic is closed to new replies.

Advertisement