Writing Displacement Shaderscontents
related pages |
|
DISPLACEMENT SHADER ALGORITHM
Displacement shaders alter the smoothness of a surface, however, unlike
bump mapping which mimics the appearance of bumpiness by reorientating
surface normals, displacement shading genuinly effects the geometry of a
surface. In the case of Pixars prman renderer, each object in a 3D scene
is sub-divided into a fine mesh of micro-polygons after which, if
a displacement shader has been assigned to an object, each micro-polygon
is "pushed" or "pulled" in a direction that is parallel to the original
surface normal of the micro-polygon.
The following algorithm lists the four basic steps that a displacement shader must follow in order to set the new location(P) and orientation(N) of the point on the surface being shaded.
This page provides some sample code that will enable you to experiment with displacement shading techniques that use,
|
|||||||||||||
|
DISPLACEMENT SHADERS & GLOBAL VARIABLES
The following table lists the global variables accessible to a displacement
shader. [List of global variables available to
surface shaders].
|
|
A BASIC SHADER
The experiments on displacement shading in this section are based the following "template" code. If you are using Cutter to edit your SL files you will see the syntax colored as shown below.
As each shader is introduced new or modified code will be shown
underlined.
|
|
USING TEXTURE COORDINATES
Although each part of a 3D object has an x, y and z coordinate, the surface
of the object is two dimensional - in the same way that an atlas of the world
is a 'flattened' 2D version of the curved surface of the planet. Locations on
the surface of a globe or atlas are uniquely specified by their coordinates
in latitude and longitude. In 3D computer graphics the position of a point
on the surface of an object(not just a sphere) can be specified using its
s and t texture coordinates.
When a shader is called(instanced) the global variables s and t store the specific texture coordinates of the surface point being shaded. The values of s and t can be used for a variety of purposes. For example, the following shader makes the displacement only when t is greater than 0.5.
Modify the Displacement statement in the RIB file ie. Displacement "tstep"
tstep Shader |
|
IMAGE EMBOSSING
The function texture() enables a texture file to be read. The function
can return either a float, a color or a point value. In the example shown
below because its return value is assigned to a floating point variable
the function is effectively reading the texture file as a series of
grayscale values.
As an experiment change the data type of 'hump' from a float to a point.
Displacement "tex1" "texname" ["your_image.tx"]
tex1 Shader |
|
NOISE
For information about the use of the noise() function refer to,
Because the input value "P" references an xyz location on the surface
of an object the noise
function will generate 3D noise values. The "bumpiness" of the surface
of an object to which this shader is applied depends on where in a
3D scene the object is located. The xyz coordinates of "P" are measured
from the center of the camera - in other words "P" is defined in the
camera coordinate system. Therefore, if the camera is moved the bumps
will appear to "crawl" across the surface of the object!
Displacement "noise"
noise Shader |
|
TURBULANCE
If noise() is used several times but with P multiplied by an
ever increasing value the function will effectively generate higher
and higher frequency noise. If the results of repeated calls to noise() are
added together very interesting patterns can be created.
Try using different values for those shown below in red. For example, reduce the increment for 'totalFrequency' to 1.1. crinckle = 4; for(i = 0; i < 2; i = i + 1) { hump = hump + abs(0.5 - noise(crinckle * totalFrequency * P))/totalFrequency; totalFrequency *= 1.2; } Modify the Displacement statement in the RIB file ie. Displacement "turb"
turb Shader |
|
MAKING WAVES
The following source code shows 'hump' being set to the value returned by the sine function. The function will generate one cycle of a sine wave when its parameter varies from 0 to 2PI(ie. 6.283185). Since 's' will be in the range 0 to 1 by factoring in 'numwaves' this shader can create any number of waves in the 's' direction.
Several examples of the way the sine function can be used can be found at
Tutorial: Expressions and
Expressions Quick Reference
swave Shader |
|
MAKING RIPPLES
The following diagram and source code show a sine wave being calculated according to the distance from a point on a surface, specified by 'a' and 'b'.
Modify the Displacement statement in the RIB file ie. Displacement "ripple"
ripple Shader The second code sample allows the specification of the centers of two ripples. While it is possible to add more centers its clearly going to become increasingly tedious - suppose we require 50 separate ripple centers!
tworipple Shader |
© 2002-4 Malcolm Kesson. All rights reserved.