Skip to main content
GameDev.net gamedev.net
🔒 Locked

Fading a texture

Started by Jamiernmd Jan 24, 2007 at 10:05 AM 13 replies 1.2k views
Original Post
Jamiernmd
Jamiernmd
Hi all, I have several textured quads which I want to fade out over a period of time, i.e make the texture slightly less visible each frame. At the moment I'm making the diffuse colour of each quad slightly closer to black each frame but it looks kind of funny. Any ideas? cheers
MARS_999
MARS_999
Look into alpha blending
Jamiernmd
Jamiernmd
Ok, well I have alpha blending enabled so several particles within the the same space are blended together to create a brighter colour. For this I have:

g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

I believe that by changing this to:

g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ZERO );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

will fade out the particles completely. My question is how do I fade the particle gradually over several frames. I've tried changing D3DBLEND_ONE to a float but it doesn't work. Any suggestions?

cheers
cobru
cobru
for the duration of the fadeout:
{
set particle alpha color to particle alpha-1

g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

draw particle
present

}

basically, instead of changing the diffuse colour slightly closer to black each frame (like you do now), you change the diffuse alpha slightly closer to 'transparent' and setup your render state so that it takes alpha into consideration.

hope that helps.
Q: How many programmers does it take to write a nice piece of software?A: MORE.
Jamiernmd
Jamiernmd
Oh ok, I must be doing something wrong because that doesn't work for me. When I switch the source and destination to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA respectively, the blending doesn't work properly. It is not rendering the black areas of the particle which is good, but its not combining the colour of all the particles in the same world space, as it does for D3DBLEND_ONE for source and destination. Also changing the diffuse alpha doesn't make any difference at the moment regardless of the source or destination.
Evil Steve
Evil Steve
Quote:
Original post by Jamiernmd
Oh ok, I must be doing something wrong because that doesn't work for me. When I switch the source and destination to D3DBLEND_SRCALPHA and D3DBLEND_INVSRCALPHA respectively, the blending doesn't work properly. It is not rendering the black areas of the particle which is good, but its not combining the colour of all the particles in the same world space, as it does for D3DBLEND_ONE for source and destination. Also changing the diffuse alpha doesn't make any difference at the moment regardless of the source or destination.
When you're using srcalpha / invsrcalpha, you need to depth sort your particles, so the particles furthest from the camera are rendered first. You don't need to do this with one / one or one / zero because the current colour value doesn't matter, it's just added to.
Jamiernmd
Jamiernmd
I was afraid of that. Is there any way of fading out the particles using just D3DBLEND_ONE for source and destination. I'd prefer it if I didn't have to do a depth sort, it's already rendering too slow.

Thanks
Evil Steve
Evil Steve
Quote:
Original post by Jamiernmd
I was afraid of that. Is there any way of fading out the particles using just D3DBLEND_ONE for source and destination. I'd prefer it if I didn't have to do a depth sort, it's already rendering too slow.

Thanks

Have you tried D3DBLEND_ONE and D3DBLEND_SRCALPHA? That might work fairly well, but I haven't tested it.
cobru
cobru
Quote:
Original post by Jamiernmd
I was afraid of that. Is there any way of fading out the particles using just D3DBLEND_ONE for source and destination. I'd prefer it if I didn't have to do a depth sort, it's already rendering too slow.

Thanks


I'm afraid without depth sorting it would look "kind of funny" whatever method you try...

Blending = mix together the thing that is currently drawn with whatever was drawn before it, so you have to draw them in the right order.

How many particles do you have ?
Q: How many programmers does it take to write a nice piece of software?A: MORE.
Jamiernmd
Jamiernmd
D3DBLEND_ONE and D3DBLEND_SRCALPHA doesn't work very well i'm afraid.

There are several hundred particles on screen at once and in some cases a couple of thousand. I also have a trail of particles following each particle to create a motion blur. New emitors are being created constantly so having to do a depth sort each frame would virtually kill the framerate.
Jamiernmd
Jamiernmd
I guess I'm gonna have to do a depth sort then. What sorting algorithm is the quickest at sorting a vector?
cobru
cobru
Hmm... maybe quicksort is fast enough for your case. But, you can also have a look here and choose one which seems better...

Also, a good idea would be to put your particles in 'bins'. For example, each bin defines a range of possible Z-values, first bin for particles with z between 0 and 10, second bin for particles with z between 10 and 20 and so on, then when you update each particle you also calculate in which bin it belongs, then you draw your bins in the right order and you just need to sort the particles inside the bin (which is a lot less particles), not the whole array of particles.

Just a personal tought ;)

Edit: just found this link and also this link, maybe they're helpfull (premultiplied alpha)

[Edited by - cobru on January 25, 2007 10:42:21 AM]
Q: How many programmers does it take to write a nice piece of software?A: MORE.
Kalidor
Kalidor
Quote:
Original post by Jamiernmd
D3DBLEND_ONE and D3DBLEND_SRCALPHA doesn't work very well i'm afraid.

There are several hundred particles on screen at once and in some cases a couple of thousand. I also have a trail of particles following each particle to create a motion blur. New emitors are being created constantly so having to do a depth sort each frame would virtually kill the framerate.
D3DBLEND_SRCALPHA should be the source blend factor and D3DBLEND_ONE should be the destination blend factor. This will give you
c<sub>f</sub> = c<sub>s</sub>α<sub>s</sub> + c<sub>d</sub>
or the source color modulated by the source alpha added to the destination color, which I think is what you want. And since it's still additive you shouldn't need to sort your particles.
Jamiernmd
Jamiernmd
Thanks for the link Cobru, although most of it went straight over my head, lol. Kalidor i've tried D3DBLEND_SRCALPHA = source, D3DBLEND_ONE = destination and it works fine, and your right i've still got the additive blending which is what I want. I was a bit confused by the next bit though. How do I access the source/destination colour/alpha? I take it that it's not the diffuse colour/alpha I specified for the quad?

Thanks
Evil Steve
Evil Steve
Quote:
Original post by Jamiernmd
How do I access the source/destination colour/alpha? I take it that it's not the diffuse colour/alpha I specified for the quad?
Source colour/alpha is the colour/alpha value at the current texel on the particle you're about to render. It comes from the texture colour/alpha modulated with the diffuse colour/alpha (Assuming standard texture ops, you can change this just as you can change the blend operation).
The destination colour/alpha is the value currently on the backbuffer. Since most of the time you won't have a destination alpha (most backbuffers won't have an alpha channel), you tend not to use destination alpha as much as you use inverse source alpha (255 - source alpha).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.