RenderMan Displacement Shader
From K-3D
Global Variables
The following table lists the set of global variables that can be used by displacement shaders.
Name | Type | Storage | Description |
P | point | varying | Displaced surface position (output) |
N | normal | varying | Displaced surface shading normal (output) |
P | point | varying | Surface position |
dPdu | vector | varying | Change in surface position along u |
dPdv | vector | varying | Change in surface position along v |
N | normal | varying | Surface shading normal |
Ng | normal | varying | Surface geometric normal |
I | vector | varying | Direction of ray stricking a surface point (from the camera) |
E | point | uniform | Position of the camera |
u,v | float | varying | Surface parameters |
du,dv | float | varying | Change in surface parameters |
s,t | float | varying | Surface texture coordinates |
ncomps | float | uniform | Number of color components |
time | float | uniform | Current shutter time |
dtime | float | uniform | The amount of time covered by this shading sample. |
dPdtime | vector | varying | How the surface position P is changing per unit time, as described by motion blur in the scene. |
Geometry of Shading
Displacement shaders alter the smoothness of a surface. Unlike bump mapping which mimics the appearance of bumpiness by reorientating surface normals, RenderMan's displacement shading technique genuinly effects the geometry of a surface. Generally a displacement shader 'pushes' or 'pulls' each point on a surface parallel to the surface normal, after which it recalculates the normal so that any surface shader that has been 'attached' to the geometry can assign the appropriate lighting effects.
N ^ _________|_________ N ____ / / \/ _______/ \______
The displacement shader environment is very similar to a surface shader, except that it only has access to the geometric surface parameters. It computes a new P and/or a new N. In rendering implementations that do not support the Displacement capability, modifications to P will not actually move the surface (change the hidden surface elimination calculation); however, modifications to N will still occur correctly.
Examples
The examples below are taken from RManNotes' Writing RenderMan Shaders.
You can download the source files used to generate pictures below, modified to work with Aqsis, here: k3d.sourceforge.net/wiki-data/aqsis_rmannotes_displacement.tar.gz .
The RIB file uses a RenderMan sphere because it has automatic mapping coordinates. If you try with a polygonal primitive or any other mesh, you'll get unexpected results: K-3D doesn't automatically set texture coordinates for them.
#include "rmannotes.sl" displacement disp1_1(float freq = 5, Km = 0.01) { float surface_mag, layer_mag; float ss, tt; float fuzz = 0.05; /* background layer */ surface_mag = 0; /* rotate & repeat layers */ rotate2d(s, t, radians(45), .5, .5, ss, tt); ss = repeat(ss, freq); tt = repeat(tt, freq); /* layer 1 */ layer_mag = pulse(0.35, 0.65, fuzz, ss); surface_mag += layer_mag; /* layer 2 */ layer_mag = pulse(0.35, 0.65, fuzz, tt); surface_mag += layer_mag; /* surface_mag = max(surface_mag, layer_mag); */ /* disp1_2 */ /* surface_mag = max(surface_mag, layer_mag) + layer_mag; */ /* disp1_3 */ /* displace */ P += Km * surface_mag * normalize(N); N = calculatenormal(P); }