Blending in XNA

Blend state controls how color and alpha values are blended when combining rendered data with existing render target data.

The blend state class, BlendState, contains state that controls how colors are blended. Each time you render, the pixel data you generate (call it source data) is stored in a render target. The render target might be empty or it might already contain data (call it destination data). Blending occurs each time you combine source and destination data.

Build-in Blend States

Opaque blending

(default)Opaque blending generally ignores the destination pixel. When opaque blending is used the following formula is used

\[\textrm{pixelColour}= src_{\alpha}.src\_pixel\]

If a texture has alpha data, the pixels with low alpha values will appear dark.

  
	//rendering a sprite 
	spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque);

	// rendering primitives
	GraphicsDevice.BlendState =  BlendState.Opaque;

	 
        

Additive blending

Additive blending can be used with or without alpha channel. When additive blending is used the following formula is used

\[\textrm{pixelColour}= src_{\alpha}.src\_pixel+dest\_pixel\]

or, if there is no alpha channel;

\[\textrm{pixelColour}= src\_pixel+dest\_pixel\]

Because additive blending adds two whole pixels, the result is usually brighter than either the destination or the source.

  
	//rendering a sprite 
	spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Additive);

	// rendering primitives
	GraphicsDevice.BlendState = BlendState.Additive;
        

Alpha blending

Alpha blending generally uses the alpha channel. This is the mode to use for basic transparency effects. When alpha blending is used the following formula is used

\[\textrm{pixelColour}= src\_pixel+(1-src_{\alpha}).dest\_pixel\]

This form of blending assumes the source pixels have already been "pre-multiplied" by their alpha values (this happens by default when XNA imports textures)

  
	//rendering a sprite 
	spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

	// rendering with a basic effect
	GraphicsDevice.BlendState = BlendState.AlphaBlend;
	

	 
        

Non-premultiplied blending

This is the mode to use for basic transparency effects for non-premultiplied data. The following formula is used

\[\textrm{pixelColour}= src_{\alpha}.src\_pixel+(1-src_{\alpha}).dest\_pixel\]

Because alpha blending is a convex sum of the two pixels, the result is usually about as bright as the average of the destination or the source.

  
	//rendering a sprite 
	spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.NonPremultiplied);

	// rendering with a basic effect
	GraphicsDevice.BlendState = BlendState.NonPremultiplied;
	

	 
        
 
Two example textures. Left has alpha channel, right has no alpha information.
Top row is the non-alpha texture, bottom row is the texture with alpha information. 1st column is additive blending, 2nd column is alpha blending, 3rd column is alphablending with 50% alpha and 4th column is opaque blending.

Custom Blendstates

If we need to use more complex blending, we can construct our own. The components of the blending formula are;

\[(\mathrm{srcpxlCol} * \mathbf{sourceBlendFactor})\quad \mathbf{blendFunction}\quad (\textrm{dstpixCol} * \mathbf{destinationBlendFactor})\]

Each of the bold components of the above equation can be modified in a custom blendstate. See the code below;

 

For more info on custom belnding,see this MSDN link. This page documents the various allowable settings for custom blend states.