Thursday, April 28, 2011

How blending works, the BlendState object

Blending is the process of combing color data. We already saw how to combine textures with multitexturing, but what about combing the colors of different objects? Blending will allow us to combine the colors of multiple objects in many ways, this type of techniques will allow us to draw translucent objects or combine multiple pixel shaders over the same object.

Backreferences:
The BlendState object allows us to control how object colors are combined. When we draw we will sometimes have overlapping objects, we can consider that the default is that the new object will always replace the old object (remember that the graphics card by default only draws objects that are closer than the object already on screen, this can be changed in the DepthStencilState).

The blending equation is defined as:

    result = (source * sourceBlendFactor) blendFunction (destination * destinationBlendFactor)

This is how 2 pixels mix, this is the same equation for both color channels blending and alpha channel blending.

In the above equation source represents the color/alpha that is now being drawn over the the old color/alpha, while the destination is the color/alpha already existent in the buffer. The 2 factors they are multiplied with are used to determine how much of each color/alpha is going to make it into the result. The blenFunction represents the way these 2 colors/alphas are combined, you can add, subtract, invert subtract (subtract in inverted order), use the minimum or use the maxim value.

The BlendState object has properties for all of these and more:
  • AlphaBlendFunction sets the blendFunction used for blending the alpha channel
  • AlphaDestinationBlend is the destinationBlendFactor used for blending the alpha channel
  • AlphaSourceBlend  is the sourceBlendFactor used for blending the alpha channel
  • ColorBlendFunction is the blendFunction used for blending the color channels
  • ColorDestinationBlend  is the destinationBlendFactor used for blending the color channels
  • ColorSourceBlend  is the sourceBlendFactor used for blending the color channels
  • BlendFactor sets a custom blendFactor that we can use
  • ColorWriteChannels(1/2/3) sets what channels we want to write to.
From this list most elements are enumerations: Alpha/ColorBlendFunction are of type BlendFunction, all 4 blend factors (from the equation) are of type Blend, BlendFactor is a color and the ColorWriteChannels are of type ColorWriteChannels.

No comments:

Post a Comment