Reference

IntroductionThis page presents a collection of expressions that can be used with source code for surface and displacement shaders. The expressions use the values of the global variables s and t, both of which by default are in the range 0 to 1, to calculate values in other ranges.


Being able to conveniently convert one range of
values into another range is very useful when, for example, the effect of
a surface or displacement shader needs to be gradually modified towards
the edges of a surface. 


Mapping 0  1 to 1  0  1
Deriving an expression that will convert values in one range to their
associated values in another range can be visualized as a process of
'moving' and 'stretching' the initial range, say 0 to 1, along a
fixed number line.
For example, the diagram below shows the original range, 0 to 1, being mapped to the range 1 to 0 to 1.
If the original range of values was represened by say the s
texture coordinate, new values could be calculated using the following
expression.


The effect of using newValue to scale the displacement of a wave is shown below.

Mapping 0  1 to 0  1  0The diagram below shows the original range, 0 to 1, being mapped to the range 0 to 1 to 0.
The equivalent expression is,


The effect of using newValue, calculated with both 's' and 't', to scale the displacement of a turbulance shader is shown below.

Mapping a Subrange to 0  1  0The diagrams show a subrange a to c being mapped to the range 0 to 1 to 0. It is assumed b is the midpoint of ac
The equivalent expression is,


The effect of using this expression to calculate a displacement in both 's' and 't' is shown below.

Using the Sine FunctionThe sine function is part of the maths library that is built into the shading language. The function returns a value that is the ratio of the length of a rotating line divided by its vertical height. The end of the rotating line sweeps out the circumference of a circle. If the line is one unit long the circumference of the circle is 2 x PI or 6.283185 units in length.
In the diagram shown above the vertical height
of the rotating line, itself 1 unit in length, can never be greater
than +1 and never be less than 1. Consequently for any rotation
of the line, measured by the distance its end has moved along the
circumference of the circle, the sine function returns values in the
range +1 to 1. The unit of measurement used to express the rotation
is the radian.


The effect of using waveHeight in a displacement shader is shown below.
To avoid negative numbers it is often necessary to keep waveHeight
in the range 0 to 1. The following expression accomplishes this,


It is also useful to be able to shift the sine wave horizonally so
that the wave begins and ends at a 'trough', a 'peak' or anywhere
in between. The following expression introduces control over the
phase of the wave.
Often an arbitary number of cycles of the sine wave is required, the
following expression includes a variable that controls the number of
waves,
The effect of using this expression in a displacement shader is shown below.


Occasionally it is necessary to modify the sine wave so that it produces
only 'bumps'. Here is the expression,

Using the Mod Function
The mod function divides one number by another but returns only the
remainder. For example,
The mod function is very useful when creating repeating patterns
on a surface. In the diagram above, 4 subranges have been created,
however, the function can be used to make any number of repeats.
To generate 8 repeated patterns, say in the 's' direction, the following
expression would be applied.

© 2002 Malcolm Kesson. All rights reserved.