RSL
|
IntroductionThis tutorial provides an introduction to the writing of class-based shaders. The notes were prepared using prman 13.5.2. The primary source of information on this topic is the Pixar document, DOCS/prman_technical_rendering/AppNotes/ShaderObjects.html
That document refers to enhancements to the shading language that enable shaders to be
written in an object oriented style of programming (OOP). Because the term " Basic Code - Traditional Shaders
The combined effect of the shaders in listings 1 and 2 are shown below. The displacement
shader,
|
|
Listings 1 and 2 (hills.sl, snow.sl) |
|||||
|
|||||
|
The rib file used to render figure 1 is shown in listing 3. It should be noted the surface shader,
despite changes to the value of the " |
|
Listing 3 (snowOnHills.rib)
|
Basic Code - Class Based Shader
Listing 4 gives the first "cut" of a class-based shader that mimics the behavior
of |
|
Listing 4 (snowOnHills.sl)
Assigning the Shader Object in a Rib FileIf the implementation of snowOnHills contained only a displacement method it would be obvious that a rib file that referenced the shader (object) should do so as follows, Displacement "snowOnHills" "Km" -0.1 "Kf" 8 However, snowOnHills has both displacement and surface shading capabilities, so it is less obvious how it should be referenced in a rib file. As shown in figures 2 and 3 using it as a Displacment shader or a Surface shader yields very different results. |
|
Using the shader object as a Displacement shader ie. Displacement "snowOnHills" Surface "plastic" |
Using the shader object as a Surface shader ie. Surface "snowOnHills" |
|
Specularity is absent from figure 3 because the surface method does not perform a specular lighting calculation. The shader can be adapted to take advantage of (re-lighting) rendering efficiencies that might possibly be introduced in future versions of Pixar's software. This is the subject of the next section. Factored Lighting
Listing 5 replaces the public void prelighting (output color Ci, Oi)
public void lighting (output color Ci, Oi)
public void postlighting(output color Ci, Oi)
Apart from the declaration of TransformBegin
Surface "snowOnHills"
Attribute "bound" "displacement" [0.1]
Color 0.341 0.266 0.184
Sphere 1 -1 1 360
TransformEnd
Listing 5 (factored lighting)
|
Using Co ShadersFor the purposes of illustrating the use of co shaders, the version of snowOnHills in this section does not use factored lighting. Although a "co shader" is part of the shading pipeline it is not a shader as such - at least not in the sense that it can be assigned and used, by itself, to shade an object. Instead, it implements one or more methods that can be called upon to perform calculations on behalf of a class-based shader (ie. shader object). For example, listing 6 provides the code for a co shader that "returns" white only for micro-polygons of a bump that are facing upward. Listing 6 (Co shader)
A co shader friendly version of snowOnHills is shown next. Listing 7
Using a co-shader in a rib file is relatively straightforward. For example, RmanTools can write the appropriate rib statement - figure 4.
As shown below, Cutter inserts a comment that names of the public method(s) implemented
by the co-shader. It also provides a generic name (" TransformBegin
# Public method: getColor()
Shader "hillColor" "locale_name"
"foo" 1.0
Surface "snowOnHills" "co_shader" ["locale_name"]
Attribute "bound" "displacement" [0.1]
Color 0.341 0.266 0.184
Sphere 1 -1 1 360
TransformEnd
The effect of the co-shader can be seen in figure 5.
|
Why Use Co-Shaders?In the context of the relatively simple code for snowOnHills, it makes very little sense to use a co-shader for the calculation of the surface color. However, that is true only because the code has been deliberately kept simple for the sake of the tutorial. A reason for using a co-shader is its "plug-and-playness". For example, without making any changes to the snowOnHills shader, a different effect can be achieved merely by substituting another co-shader. The main point to note is that snowOnHills expects to call a co-shader that implements a public method with this signature, public void getColor(normal; float, float; output color) Therefore, any co-shader that has a public method of the "form" expected by snowOnHills can be deployed. Listing 8 gives the code for a different co-shader that can be used by snowOnHills. Listing 8 (drift.sl)
The rib that produced figure 6 was edited as follows, TransformBegin
# Public method: getColor()
Shader "drift" "locale_name" "snowDrift" [1 1 0]
Surface "snowOnHills" "co_shader" ["locale_name"]
Attribute "bound" "displacement" [0.1]
Color 0.341 0.266 0.184
Sphere 1 -1 1 360
TransformEnd
|
© 2002- Malcolm Kesson. All rights reserved.