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

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

#### Explosions:

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.

Great series of articles.

About the requested feedback: I like the three options but if I had to choose, I’d prefer the sample particle system, then the game structure, and finally the FPS camera.

About the article, one question: given that the draw order is not an issue with additive alpha blending, in which scenarios is it better to use "normal" alpha blending?

Best regards.

2. etayrien says:

"In which scenarios is it better to use "normal" alpha blending?"

Most of what I learned about alpha blending is from working on a shooter, which tend to be very full of explosions and flashy things and not much else.  My team and I were able to use additive blending almost exclusively.  ðŸ™‚

There were always a few transparent GUI elements that we could do normal alpha blending with: we knew right where they were so it was no trouble always drawing them last.  Doing normal blending instead of additive blending for those also gave the artist more intuitive control over the final output, which was useful.

Ok, I see. Thanks.

4. GloryFish says:

Agreed, excellent series of articles. I’m looking forward to more "fundamentals" type articles that I don’t have to translate from MDX or older C++ tutorials. Keeping it in the XNA context really helps.

I would love to see an FPS camera tutorial. It might be good to also include some information on other camera types too.

Also, something that I know I would love to see is some info on moving objects around in 3D. For those of us that have been doing 2D it can be hard to make the jump to a 3D world where you can’t necessarily just do X = X + 1 to move an object. It can be tough to go from textbook-like definitions of matrix math to practical in-game applications.

I also would like to see a sample particle system. Especially with a focus on performance and extensibility.

Thanks for these great articles. ðŸ™‚

5. Kelly White says:

So today while I was riding the MAX to and from work I was reading some great articles from the Cornflower