TUTORIAL: The Miracle of Blending
Today we’re going to talk a little bit about various methods of blending and compositing in Toolbag, and how you can use them. There will be a bit of theory and a bit of practice. The hope is that every artist will get something out of this run-through, even those experienced with blending in other 3D applications.
BLENDING & MODES
A realtime 3D renderer like Toolbag renders one triangle at a time. While a given triangle’s pixels are drawing, it has the option to replace pixels that have already been rendered behind it, or to modify them. This combining of front/back pixels is what is known as blending.
The important thing to note here are that there are only two inputs to the process – the “front” color, or the color of the pixel being rendered, and the “back” color, or the color already on the screen from whatever rendering occurred previously. These two inputs can be combined in a variety of different methods, and these methods constitute the different blend modes in Toolbag. Depending on the blend mode, a variety of different visual impressions can be achieved.
The most common is “Alpha” blending. This uses a fourth color channel in the texture to indicate the proportions of “front” and “back” pixels that should be mixed. Essentially it means that if your alpha value for a given pixel is 25%, you will get a blended result that is 25% front and 75% back. This blending mode is commonly used to give a sense of transparency, of some light coming through the triangle from behind. In Toolbag, the alpha channel used with blending comes from the diffuse texture of the material.
Some other common blend modes include “Add” – where the front triangle’s color simply adds on top of the back, brightening it. This is useful for objects that emit light but also do not prevent light from passing through from behind (such as fire). “Multiply” (often mathematically inaccurately referred to as “Subtractive”) blending takes the two colors and modulates them together, darkening them. This could be used for something like tinted glass, for example. There are a wide variety of methods possible – only a few are exposed in Toolbag (see the “Blend Mode” material setting).
Toolbag’s Blend Modes (put your math pants on)
| Equation | Explanation | |
|---|---|---|
| None | Front | No blending, front simply replaces back pixel. |
| Add | Front + Back | Front and back colors add, brightening the result. |
| Multiply | Front * Back | Front and back colors modulate, darkening each other. |
| Multiply Inv | (1 – Front) * Back | Back color is modulated with the inverse of the front color. |
| Alpha | Alpha * Front + (1 – Alpha) * Back | Alpha sets the fractions of front and back color present. |
| Add Alpha | Alpha * Front + Back | Takes a fraction of the front based on the alpha channel, and adds it to the back as in the “Add” mode. |
| Premultiplied Alpha | Front + (1 – Alpha) * Back | Assumes the texture has already been multiplied by alpha in the image editor, and proceeds otherwise identically to “Alpha” mode. Has certain content and quality advantages beyond the scope of this tutorial. |
ORDER MATTERS!
More often than not blending does not work properly, this author is sorry to say. The problem is ordering.
Suppose you have two blended triangles lined up in a row, so that they appear on top of each other from the camera’s view. The blending process is run for each pixel of each triangle, but since triangles are rendered one at a time, then by the time we’re rendering our 2nd triangle the first is already entirely drawn. So the second triangle blends with the result of the first triangle blended with the background color. Things chain in this manner for any number of triangles that overlap.
This all works fine as long as the triangles are drawn back to front. But when they aren’t, things get quickly out of whack, and give the appearance of triangles in the back drawing “on top of” triangles in the front – that’s because as far as blending is concerned, that’s literally what’s happening! A renderer can start to correct for this by drawing blended objects in back to front order, but that only partially solves the problem – sometimes objects or triangles intersect, and then no reordering can fix it. Toolbag makes an attempt at drawing your blended meshes in the proper order, but sometimes it can be difficult to determine which order might be best, and you may see odd results from time to time.
What this means for you as an artist is that you should understand the blending order problem, and strive to create content that does not cause this issue, when possible. If you’re crafting hair or something else with a complicated blended structure, it can be a very difficult problem to get around. There are fortunately a few other compositing tools at our disposal besides blending.
ALPHA TESTING
Compared to blending, alpha testing is brain dead simple. It works by cancelling the rendering of a pixel altogether if the alpha value of the texture is less than a user-specified threshold. The result is a kind of “scissor cut-out” type of effect – a hard edge is left on the boundary where the alpha value dipped below the threshold. These hard edges are desirable in cases where an artist wants sharp detail without using triangles.
Alpha blending has no draw order problems, so artists don’t need to worry about the order in which alpha tested geometry is drawn. It can in fact be used in conjunction with alpha blending. Its main drawback is that it produces harsh, sometimes jagged edges, and has none of the subtlety or transparency-like effect that alpha blending can provide. Combined with proper antialiasing, it can provide acceptable results for plant life, cloth, and other surfaces with hard edges.
To use it in Toolbag, adjust the “Alpha Threshold” material setting. If you set the threshold to 0.5, then pixels with alpha of less than 50% will be clipped away. Setting an alpha threshold below zero will disable alpha testing.
ALPHA to COVERAGE
Alpha to Coverage (A2C) is an odd bird. It utilizes sub-pixel dithering, assisted by the anti-aliasing hardware of your graphics card, to provide something approximating alpha blending in appearance. It works by placing your pixel’s alpha value into the antialiasing coverage mask (hence the term, “alpha to coverage”). If you turn it on, you’ll see a gradient smoother than alpha testing, but courser than true blending:
So if A2C doesn’t look as good as blending, why use it? Because it is order independent! It allows your geometry to be drawn in any order it likes, even intersecting geometry. This works because it is not really blending at all, but a way of allowing multiple render pixels to crowd together in a single screen pixel, and then get merged right at the last moment before the image goes up on your monitor (if you care to know, this is also how hardware antialiasing is done).
A2C from a content standpoint is equivalent to alpha blending – the artist provides an alpha channel in the diffuse texture in the same way. To enable alpha to coverage in toolbag, check the “Alpha to Coverage” box in the material settings. Note that it is possible to use alpha blending in conjunction with alpha to coverage, but it is not necessary or even recommended.
REFRACTION
Refraction is similar in concept to blending, in that it interacts with pixels behind the object being rendered, but it is a wholly different technique. Refraction is not merely transparency but is the actual bending of light passing through an object, the most common everyday examples of which are water and glass. Toolbag exposes refraction in the form of the “ComplexRefraction” material channel model. It offers a variety of settings related to the strength of refraction as well as diffuse and specular settings.
Refractive objects in Toolbag allow light to pass through them and become distorted, and, like any other material type, they can *also* use blending, alpha testing, and alpha to coverage. And it often makes sense to do so. Mind blown yet? Here’s an example.
Suppose you want to create a partially broken window. The window glass should have cracks and warps and should refract light that passes through. But the glass has a hole in the middle, or some shards missing, and these areas of your model shouldn’t be refracting light at all. An artist in the know would create an alpha channel in the diffuse map, and use that in combination with alpha testing (or another suitable compositing option). The result is a nice, refractive window pane with an alpha tested hole properly busted in the middle.
Refraction has the notable limitation in Toolbag that one refractive surface is never visible through another – only one “layer” of refraction is supported.
SCREENSHOT BLENDING
So, suppose you have your artwork rendering beautifully in Toolbag, and now you’re ready to save out a shot of your work. Maybe you’d like to composite a set of renders, or composite your model in front of another 2D image?
Blending to the rescue again! When Toolbag takes a screenshot, it will save an alpha channel in your resulting TGA file (HDR screenshots do not currently contain an alpha channel). Simply open this in your image editor of choice, and use this alpha channel as an opacity or layer mask. And voila! You can blend you renders, complete with soft silhouettes, on top of other images to your heart’s content!

For more blending, alpha testing, and A2C info, check out…
http://www.myinkblog.com/an-explanation-of-photoshop-blend-modes/


