Shader Networks and Layered Shaders

SiTex Graphics Air version 11 (press release) introduces the ability to use multiple shaders to compute the shading for surfaces, displacements, imagers, and environments.

This new capability provides a simple solution to several common shading tasks that would otherwise require complex custom shaders:

  • Decals:  apply an arbitrary number of texture maps to an object’s color and feed the result into any surface shader.
  • Extra output variables:  add an arbitrary number of additional shaders to an object to produce extra output values without affecting the normal shading of the object.
  • Assign different shaders to specific regions:  simple shader compositing allows existing surface shaders to be selectively applied to sections of a surface.
  • Replicate application shader networks:  create arbitrary shader networks by connecting pre-compiled components.

Further, the implementation of networked and layered shaders does not require existing shaders to be re-written to take advantage of these new features.

This article describes how to use multiple shaders to address each of these tasks.  Sample shaders and rib files are included in the AIR distribution in:

$AIRHOME/examples/layers

Simple Color Layers

Here’s a simple RIB fragment that assigns multiple surface shaders to an object:

Surface "ColorMap" "string ColorMapName" "grid.tx"
Surface "+VMarble"

Prepending the shader name with “+” tells Air to append the shader to the list of shaders assigned to the object. The shaders are executed in the order in which they are assigned. Subsequent shaders inherit the current shading state (including the values of global variables) from shaders previously executed. Here’s a ColorMap shader that uses a texture map to set the object’s color value:

generic ColorMap(
  string ColorMapName = "";
  float SetColor = 1;
  output varying color __Color = 0;
)
{
  if (ColorMapName!="") {
    __Color = color texture(ColorMapName,s,t);
    if (SetColor!=0) Cs = __Color;
  }
}

In the RIB fragment given above, the VMarble shader will use the color map result as one of the input colors for the marble pattern. No modification of the VMarble shader is required.

The new generic shader type is compatible with any other shader type.  Generic shaders can be used to construct re-usable components for layered and networked shaders.

Sample scene: colormap.rib

Decals

The layers example directory includes a LayerDecal shader that applies a masked color to a surface. Simple sequential shader assignment allows an arbitrary number of decals to be applied to a surface without constructing a complex custom surface shader:

Surface "LayerDecal"
  "ColorMapName" "sitex.tx"
  "OriginXY" [.3 .67]
  "SizeXY" [-.3 -.3]
  "DecalColor" [1 1 1]
Surface "+LayerDecal"
  "ColorMapName" "sitex.tx"
  "OriginXY" [.25 .6]
  "SizeXY" [-.25 -0.25]
  "DecalColor" [1 0 0]
Surface "+plastic"

Sample scene: decals.rib

Extra Passes

Another task made easy with multiple shaders is adding extra output values to a rendering pass.   Air 11 includes a new generic shader that adds an occlusion output value to any surface (stored in a __occlusion output variable).   Sample usage:

 Display "extrapass.tif" "framebuffer" "rgba,color __occlusion"
...
Surface "VMarble"
Surface "+genOcclusion" "setshaderoutput" 0

Sample scene: extrapass.rib

Shader Compositing

Air’s multi-shader support includes an option to composite one shader on top of another. Here’s an example that adds a plastic decal to a metallic surface:

Surface "VMetal"
Surface "*VPlastic" "OpacityMapName" "sitex.tx" "ColorMapName" "square.tx"

The * before the shader name tells Air to blend the output color from the VPlastic shader with the output color of the VMetal shader using the output opacity from the VPlastic shader. Any output variables shared by the two shaders will also be composited using the output opacity.

Sample scene: compshaders1.rib

The above example shows how to composite a shader with controls for its output opacity. For surface shaders without opacity controls, an auxiliary shader can be used to control the opacity. Here is a simple generic shader that sets the global input opacity value:

generic OpacityMap(
  string OpacityMapName = "";
  output varying color __Opacity = 1;
)
{
  if (OpacityMapName!="") {
    __Opacity = color texture(OpacityMapName,s,t,"fill",-1);
    if (comp(__Opacity,1)==-1) __Opacity=comp(__Opacity,0);
    Os = __Opacity;
  }
}

This shader allows any surface shader that modulates its output color by the input opacity to be composited over another shader.  Most surface shaders modulate their output color in this way.

Here is how the OpacityMap shader might be used to add a marble decal to a 2D grid:

Surface "VGrid2D"
Surface "+OpacityMap" "OpacityMapName" "sitex.tx"
Surface "*VMarble"

To optimize the evaluation of composited shaders, Air will skip any shader layer whose input opacity value is 0.

Sample scene: compshaders2.rib

Shader Networks

Multiple shaders can be used to re-construct a general shader network by defining connections among the shaders assigned to an object. Connections are defined by including additional parameters in the shader declaration. First, each shader declaration is assigned a name using:

“string layername” “componentname

Naturally layername does not need to be an actual parameter of the shader.

Each connection is defined with an additional string parameter:

“string connect:toparametername” “fromlayer:fromparametername

The connection transfers the value of fromparametername in layer fromlayer to the target parameter in the current shader.  The source parameter must be an output variable of the source shader or a global shader variable.  The target parameter is normally one of the shader’s input parameters, but it can also be the global input color (Cs) or input opacity (Os).  The source and target parameters must have the same type.

Sample usage:

Surface "ColorMap"
  "string ColorMapName" "grid.tx"
  "float SetColor" [0]
  "layername" "gridlayer"
Surface "+VMarble"
  "connect:VeinColor" "gridlayer:__Color"

In this example, the marble vein color is taken from the ColorMap shader’s __Color output variable.

Applications

The Air Stream plug-in for Maya uses the new shader network capability in Air 11 to translate Maya’s shader networks for rendering with Air.  This approach also easily allows:

  • Air shader parameters to be driven by a Maya shading network
  • Air shaders and Maya shaders to be mixed in a single network
  • Air custom shading language functions to be incorporated in a Maya shading network using generic shaders
This entry was posted in Shaders and tagged , . Bookmark the permalink.