Jason Mitchell

Setting Alpha Value for SpriteBatch.Draw in XNA 4

A few weeks ago I spent some time upgrading my MicroStar Particle System from XNA 3.1 to XNA 4.  For the most part this process went smoothly; there were quite a few changes to be made all over the place.  However once I had cleared up all the build errors and ran the sample project I saw that my code for fading out particles when drawing them via my SpriteBatch instance was obviously not working. 

I immediately set about trying to hunt down the offending piece of code but came up blank; there was nothing that I had changed that should have killed that functionality.  After spending some time rummaging through the source code I felt sure that it was caused by some breaking change in XNA between versions 3.1 and 4.0.  As it turns out, this assumption was correct.

In XNA 3.1 all that was needed to be done to fade a texture out in a SpriteBatch draw call was to enable alpha blending (now enabled by default in XNA 4.0) and set the “A” property of your Color object appropriately like so:

Color myColor = Color.White;
myColor.A = 50;
spriteBatch.Draw(texture, position, sourceRect, myColor, ...... );

Now it seems that changing the alpha value on a Color instance to be used with SpriteBatch doesn’t make a difference.  What we now must do in XNA 4.0 is to define the alpha separately to the colour as a float with a value between 0 and 1.  Then the selected colour must then be multiplied by this float to set the transparency:

spriteBatch.Draw(texture, position, sourceRect, Color.White * 0.5f, .......);

This line of code will draw the texture at half transparency.

At first I found this really confusing and I longed for the old approach from XNA 3.1 but over time I’ve come to prefer the new XNA 4.0 approach that requires me to define my alpha separately to the colour.  This also neatly resolves some conflicts between colour modifiers in my particle system (they could have been resolved anyway but it wouldn’t have been as nice).

No related posts.


Categorised as: XNA


6 Comments

  1. Ned says:

    Ahhhh, that’s what changed. I was wondering why I was having such wonky problems. I like this. Seems much cleaner somehow.

  2. Jason says:

    Yeah I like it better this way now.

  3. Mika says:

    Color alpha channel is the obvious and most convenient place for storing the opaqueness factor. This new approach wastes the Color.A field, requires storing the alpha separately and breaks a lot of existing code.

  4. Jason says:

    I agree that the alpha property of the Color object is the most obvious place for it and it most certainly breaks a lot of code.

    I’m not sure that I would agree that Color.A field is wasted though. It still has its uses; for example in per pixel collision detection if you want to check that a pixel isn’t transparent I would assume that the alpha value would be set here. Similarly for something like a multi-textured terrain shader your code might make use of all 4 colour channels.

    It’s important to remember that Color is not used just for rendering sprites.

  5. matt says:

    Thank you for this post, helped me out.

  6. saboor says:

    useful description

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">