The following functions are available for use in the creation of procedural textures. These are based on the code provided in the following book:
Texturing and Modeling A Procedural Approach by Kenton Musgrave, Darwyn Peachey, Ken Perlin, Steven Worley (Cambridge, MA: Academic Press, Inc., 1994). ISBN: 0-12-228760-6.
Consult this book for additional insight into the use of these functions.
boxstep()
- Returns 0 if x
is less
than a
, a linear interpolation between 0 and 1 if
x
is greater than or equal to a
and less
than or equal to b
, and 1 if x
is greater
than b
. This function is a smoother version of
step()
using a linear ramp. The function
boxstep()
is defined like this:
float boxstep(float a, float b, float x) {
return clamp((x-a)/(b-a),0.0f, 1.0f);
}
The boxstep() function.
For comparison, note that step()
returns values of
0 when x
is less than a
and 1 when
x
is greater than or equal to a
. This
function is not part of the SDK but step()
is defined
like this:
float step(float a, float x) {
return (float)(x >= a);
}
The step() function.
Parameters:
float a -
The limit for the x
value
where the function will return 0.float b -
The limit for the x
value
where the function will return 1.float x -
A floating point
value.
smoothstep()
- This function is similar to
step()
, but instead of a sharp transition from 0 to 1
at a specified threshold, it makes a gradual transition from 0 to 1
beginning at threshold a and ending at threshold b. To do this,
this function uses a cubic function whose slope is 0 at a and b and
whose value is 0 at a and 1 at b. This function thus provides a
still smoother version of step()
using a cubic spline.
The smoothstep()
function is used (instead of
step()
) in many procedural textures because sharp
transitions often result in artifacts.
The smoothstep() function.
Parameters:
float a
- The limit for the x
value
where the function will return 0.float b
- The limit for the x
value
where the function will return 1.float x
- A floating point
value.
clamp()
- returns a
when
x
is less than a
, the value of
x
when x
is between a
and
b
, and the value b
when x
is
greater than b
. The function clamp()
is
defined as follows:
float clamp( float x, float a, float b) {
return (x < a ? a : (x > b ? b : x));
}
The clamp() function.
Parameters:
float x
- A floating point value.float a
- A floating point value.float b
- A floating point
value.
threshold()
- Returns 0 if x
is less
than a
, 1 if x
is greater than
b
, otherwise it returns x
.
bias()
- This function performs a mapping across
the unit interval [0, 1] where the result is within the unit
interval. That is, if b
is within the interval 0 to 1,
the result of bias()
is within the interval 0 to 1.
This function is defined such that bias(a, 0.5)=a
. The
function looks like:
float bias(float a, float b) {
return (float)pow(a, log(b) / log(0.5f));
}
The bias() function.
gain()
- This function performs a mapping across
the unit interval [0, 1] where the result is within the unit
interval. That is, if b
is within the interval 0 to 1,
the result of gain()
is within the interval 0 to 1.
This function is defined such that gain(a, 0.5)=a
.
Above and below 0.5, this function consists of two scaled down
bias()
curves forming an S-shaped curve. The function
looks like:
float gain(float a, float b)
{
float p = (float)log(1.0f - b) /
(float)log(0.5f);
if (a < .001f)
return 0.f;
else if (a > .999f)
return 1.0f;
if (a < 0.5f)
return (float)pow(2 * a, p) / 2;
else
return 1.0f - (float)pow(2.0 * (1. - a), (double)p) /
2;
}
The gain() function.
The following are noise functions over 1, 2, 3, 4 and dimensions. They return values in the range [-1,1].
noise1()
- An approximation of white noise blurred
to dampen frequencies beyond some value.noise2()
- An approximation of white noise blurred
to dampen frequencies beyond some value in two dimensions.noise3()
- A noise function in three dimensions
implemented by a pseudo-random tricubic spline. This function is an
approximation of white noise blurred to dampen frequencies beyond
some value.noise4()
- This function is an approximation of
white noise blurred to dampen frequencies beyond some value in
three dimensions, and time.noise3ds()
- A noise function in three dimensions
implemented by a pseudo-random tricubic spline. This is the same as
noise3()
scaled up by factor of 1.65 and clamped to
-1,+1. The macro NOISE can be used to map the value returned from
the noise3DS()
function into interval [0,1].turbulence()
- This turbulence function is a
simple fractal generating loop built on top of the noise function.
It is used to make marble, clouds, explosions, etc. It returns a
value in the range [0, 1].Perm()
- Takes the low 9 bits of the an input
value and returns a number in that range (0-512) .fBm1()
- A fractional Brownian motion fractal (or
fBm for short) that returns a floating point value. This version of
the fBm is said to be "homogeneous" (the same everywhere) and
"isotropic" (the same in all directions). There are versions of
this function that accept an input floating point, Point2, and Point3 value.spline()
- a one-dimensional
Catmull-Rom interpolating spline through a set of knot values. The
spline()
function Used to map a number into another
number. The parameter of the spline is a floating point value. If
x
is 0, the result is the second knot value. If
x
is 1, the result is the final knot value. For values
between 0 and 1, the value interpolates smoothly between the values
of the knots from the second knot to the second to last knot. The
first and last knot values determine the derivatives of the spline
at the endpoint.
The spline () function.
spline()
function for mapping colors.floor()
. It returns a floating-point value
representing the largest integer that is less than or equal to
x
.x - (float)FLOOR(x)
.x
if it is less than
y
; otherwise it returns y
.AComp() - Performs an alpha-composite of one
color (ctop)
on top of another (cbot)
,
assuming pre-multiplied alpha.
CellFunction()
. It has additional parameter for
iterations
and lacunariy
.RandFromCellID()
- Returns a random
number in the range 0.0 to 1.0 based on a cell identifier.