Cg Programming/Unity/Order-Independent Transparency

This tutorial covers order-independent blending.

It continues the discussion in and solves some problems of standard transparency. If you haven't read that tutorial, you should read it first.

Order-Independent Blending
As noted in, the result of blending often (in particular for standard alpha blending) depends on the order in which triangles are rendered and therefore results in rendering artifacts if the triangles are not sorted from back to front (which they usually aren't). The term “order-independent transparency” describes various techniques to avoid this problem. One of these techniques is order-independent blending, i.e. the use of a blend equation that does not depend on the order in which triangles are rasterized. There are two basic possibilities: additive blending and multiplicative blending.

Additive Blending
The standard example for additive blending are double exposures as in the images in this section: colors are added such that it is impossible (or at least very hard) to say in which order the photos were taken. Additive blending can be characterized in terms of the blend equation introduced in :

where  is the output of the fragment shader, and   is the color that is already in the framebuffer while   and   are determined by a line in Unity's ShaderLab syntax:

{code for } {code for  }

For additive blending, the code for  has to be   and the code for   must not depend on the pixel color in the framebuffer; i.e., it can be ,  ,  ,  , or.

An example is:

Multiplicative Blending
An example for multiplicative blending in photography is the use of multiple uniform grey filters: the order in which the filters are put onto a camera doesn't matter for the resulting attenuation of the image. In terms of the rasterization of triangles, the image corresponds to the contents of the framebuffer before the triangles are rasterized, while the filters correspond to the triangles.

When specifying multiplicative blending in Unity with the line

{code for } {code for  }

the code for  has to be   and the code for   must depend on the fragment color; i.e., it can be ,  ,  , or. A typical example for attenuating the background with the opacity specified by the alpha component of fragments would use  for the code for  :

Complete Shader Code
Finally, it makes good sense to combine multiplicative blending for the attenuation of the background and additive blending for the addition of colors of the triangles in one shader by combining the two passes that were presented above. This can be considered an approximation to alpha blending for small opacities, i.e. small values of alpha, if one ignores attenuation of colors of the triangle mesh by itself.

Note that the order of the two passes is important: first the background is attenuated and then colors are added.

Summary
Congratulations, you have reached the end of this tutorial. We have looked at:
 * What order-independent transparency and order-independent blending is.
 * What the two most important kinds of order-independent blending are (additive and multiplicative).
 * How to implement additive and multiplicative blending.
 * How to combine two passes for additive and multiplicative blending for an order-independent approximation to alpha blending.