Multipass effects are created by connecting multiple drawing nodes together. The blending is controlled by the draw shaders’ blending modes both in OpenGL (OGL Draw) or in DirectX (DX Draw) shaders. Although the effects described in this section use OpenGL examples, the concept applies also to DirectX shaders.
Blending with Realtime Shaders
Creating many realtime shader effects requires more than one drawing pass because each pass is adding something to the overall effect. The blending modes that you specify in each Draw shader create transparency, and thus determine how the background layers show through the foreground layers.
How Blending Works
Blending works by adding the incoming pixel (source) and the frame buffer pixel (destination). For example, if you have a pixel that had the following RGBA value (0.7, 0.7, 0.5, 1.0) and the frame buffer already contains the following RGBA value (0.5, 0.2, 0.3, 1.0), then the resulting pixel will be (1.2, 0.9, 0.8,2.0). Values are clamped to 1.0 which means the final pixel will be (1.0, 0.9, 0.8, 1.0).
It is possible to multiply the values of the source and destination pixel by a specific term. These terms are available in the Draw shader’s property editor (ONE, ZERO, ONE_MINUS_SOURCE _ALPHA, and so on). So if, for example, the source is multiplied by ONE and the destination is multiplied by ZERO, then the output will include the source pixel only.
The blending equation is as follows:
clamp ( (source * source multiplier) + (destination * destination multiplier))
So if you have the following incoming pixel:
(1.0, 0.0, 0.0, 0.8)
and the following value in the frame buffer:
(0.2, 0.0, 1.0, 1.0)
then the equation would be as follows:
R = clamp ((1.0 * 0.8) + (0.2 * 0.2)) = 0.84 G = clamp ((0.0 * 0.8) + (0.2 * 0.0)) = 0.0 B = clamp ((0.0 * 0.8) + (0.2 * 1.0)) = 0.2 A = clamp ((0.8 * 0.8) + (0.2 * 1.0)) = 0.84
When applied to every pixel in a texture, this equation determines what the blended output looks like.
Sources and Destinations in the Render Tree
When you look at a realtime shader tree, remember that you can blend passes together at each Draw node. The node closest to the Draw node is the “source”, while the one farthest from the Draw node is the “destination”.
This may sound a bit confusing but remember that the destination pixels are already in the frame buffer, while the source pixels are new pixels that the Draw shader is blending with the frame buffer pixels.
Once a Draw shader renders a pass, its output pixels are put in the frame buffer and it becomes the destination. The next closest Draw node to the material node becomes the source.
Blending Example: Basic Transparency
The render tree shown below is a good example of how the source, the destination, and their multipliers work when you’re using realtime shaders.
The output of the render tree—applied to a grid in this case—is the blue texture shown below made transparent in an area defined by its alpha channel.
In this single-pass tree, the OGLShade shader is the “destination” and the OGLT2D shader is the “source”. When the OGLDraw shader performs its blending calculations, the source pixels are multiplied by their alpha channel value (SRC_ALPHA) and the destination pixels are multiplied but one minus the source pixels’ alpha channel values. The source and destination values are then added together.
The net effect is that the source’s alpha channel is removed from both the source and the destination, leaving the area transparent like this:
If the realtime shader transparency is not displaying properly in RealTime Shaders display mode, you may have to use two passes to see the effect properly.
Duplicate the nodes that you used to create the transparency in the first place. The Draw shader in the first set of nodes should output to the first node in the second set.
Now activates Front culling in the draw node farthest from the material node and Back culling in the one nearest the material node. This ensures proper triangle drawing order and displays the transparency correctly.
SOFTIMAGE|XSI v.6.01