Last Updated March 5, 2013
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.
(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 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 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;
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;
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.
© Ken Power 1996-2016