简体   繁体   中英

direct3d fade effect leaves artifacts

I have a Direct3D program that draws with trails by instead of clearing every frame, it draws a black square over the screen with alpha blending. After rendering, It goes:

renderstate.alphablendenable = true;
renderstate.blendoperation = add; 
renderstate.sourceblend = zero; 
renderstate.destinationblend = invblendfactor; 
renderstate.blendfactor = rgb(8,8,8);

then it renders the squares (please forgive the pseudo code). this works nicely except that it doesn't completely clear the screen. it leaves permanent trails, I can't figure out why. Proper blending should after enough frames fade it completely to black, but this leaves gray trails. anyone know why or a better fade method in Direct3D?

There are plenty of ways you can approach this depending on how the rest of your code is structured. For example you could render everything to a texture and render that with declining alpha over the top of a black background instead of the other way around.

If you don't mind using shaders, one quick and relatively efficient way would be to apply a pixel shader to whatever it is you're 'fading'.

A shader equivalent of what you're doing at the moment (rendering low-alpha black over the top) would be this:

float4 FadeEffectShader(PixelInputType input) : SV_Target
{
   float4 color;    

    color = shaderTexture.Sample(SampleType, input.tex);
    color = color * 0.01;

    return color;
}

You end up with colours fading out along an exponential curve which, for the purpose of this example, will never equal zero and you'll be left with a nice graceful fadeout until almost-black where the artifacts occur. What you actually want is more like:

float4 FadeEffectShader(PixelInputType input) : SV_Target
{
    float4 color;   

    color = shaderTexture.Sample(SampleType, input.tex);
    color = color * 0.01;
    if(color.r<0.01) color.r = 0;
    if(color.g<0.01) color.g = 0;
    if(color.b<0.01) color.b = 0;

    return color;
}

The clamping of values below 0.01 will ensure they fade to black instead of to close-but-not-close-enough-to-black.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM