Alpha Blending, Part 3

Ok, in this post I'll talk about additive alpha blending.  This time for sure, I promise.  In part 2 I talked about disabling depth write, and how that could give okay-ish results.  It'll come in handy with additive blending, so if you haven't read that post yet, you should.


Additive Blending:

In part 1 I talked about the blend function that is used to combine two colors.

FinalColor = (SourceColor * SourceBlend) + (DestinationColor * DestinationBlend)

For normal alpha blending, SourceBlend is set to SourceAlpha, and DestinationBlend is InverseSourceAlpha. If we leave SourceBlend as SourceAlpha, but change DestinationBlend to be Blend.One, we get additive alpha blending.

FinalColor = (SourceColor * SourceBlend) + (DestinationColor * DestinationBlend)
FinalColor = (SourceColor * SourceAlpha) + (DestinationColor * One)
FinalColor = (SourceColor * SourceAlpha) + DestinationColor

So here's the cool part: with additive blending, the order objects are drawn doesn't matter.  Addition is commutative: no matter what colors you have, the order you add them together doesn't matter, you're always going to get the same result.  Imagine we have three objects that we're going to draw with additive blending.  Their colors are .5, .3, .2, and the alpha for all of them is 1.  No matter what order we draw the three, the result will be the same: .5 + .3 + .2 == .2 + .3 + .5 == .3 + .2 + .5.

Think about what happens when we combine that with the depth write disabled technique from part 2.  If you remember, drawing with depth write disabled solved our problem of transparent objects obscuring each other.  However, the order objects were drawn was still important.  With additive blending though, the order isn't important. So, if we draw objects with additive blending and depth write disabled, we don't have to sort them.

Cool! So there's a blending technique that doesn't require us to sort by depth.  It's easy to set up, too; just a couple of render states.  This is incredibly useful, and can save your engine a lot of complexity and make it faster too.  Some games actually use additive blending exclusively, and don't use traditional blending at all.

It's a little harder to work with from an artistic point of view, though. In the example above, notice how even though the alpha was 1, the objects weren't completely "opaque."  For example, if you draw a green object with 1 alpha using additive blending, your result isn't going to get a solid green object; it's going to be whatever was in the back buffer before you drew your green object, plus 1.0 green.  It usually takes a little while to get used to this style of blending, and to figure out how to make art that looks good with it.  It's invaluable though, so I'd definitely recommend spending the time.  Paint .Net lets you blend layers using additive blending, so you can start to get a feel how your art will look. (I'd be surprised if Photoshop didn't let you do the same thing, but I don't have it installed so I can't check.)



So, we've seen how to set up additive blending, but what's the best way to use it?  Additive blending is ideally suited to making explosions and clouds, among other things.  Let's see how we can use additive blending to make an explosion using a particle system.

(I just heard a huge racket over my shoulder, and looked over and saw a perfect example on Michael's TV.  He's playing Roboblitz, and there's explosions everywhere.  Apparently he's pretty bad at this game.)

A particle system is just a whole bunch of sprites (or other objects) drawn together to create a special effect.  You can use particle systems to make lots of things, but they're typically used for fire, explosions, and smoke.  I might write a detailed post about how to set up a particle system in the future, but for now I'm going to keep it simple. 

Let's just assume we have a particle system that draws ten sprites, and moves them a little bit every frame.  The sprites will use this texture: (it's not very good, I'm not much of an artist):

We'll draw ten of these babies with depth write disabled and additive blending, and all of a sudden we've got a pretty decent explosion.  Since depth write is disabled, the particles won't obscure one another, and since they're drawn with additive blending, the order they're drawn doesn't matter.







What next?

I hope this post was useful. It seems a little shorter than the rest of them, which is unfortunate, because I use additive blending more than any other blend function.  I'm going to stop here, though, because I'm a little hesitant to dive into the deep end of this stuff without knowing what the community would like to see next.  Should I keep going with this additive blending thing, and show a sample particle system?  Should I write more about game structure?  There have been some questions on the forums about cameras, so unless I get any other suggestions, I think my next post is going to be a quick and dirty first person shooter camera.