Game Engine Programming 2 Week 7 Module 2
Game Engine Programming 2 Week 7 Module 2
WEEK 2:
Procedural Generation: 2D Perlin Noise
Perlin Noise in 2D: In the previous lecture we introduced the concept of Perlin noise, a structured random
function, in a one-dimensional setting. In this lecture we show how to generalize this concept to a two-
dimensional setting. Such two-dimensional noise functions can be used for generating pseudo-random terrains
and two-dimensional pseudo-random textures.
The general approach is the same as in the one-dimensional case:
• Generate a finite sample of random values
1
1 1
2·
2
noise(2t
0
1
· noise(4t)
4
1
4
0
1
0
Fig. 50: Dampened noise functions and the Perlin noise function (with persistence p = 1/2).
0 0
0 1 n n
0 1
(a) (b) (c)
Noise from Random Gradients: Before explaining the concept of gradients, let’s recall some basics from
differential calculus. Given a continuous function f (x) of a single variable x, we know that the derivative of the
function df /dx yields the tangent slope at the point (x, f (x)) on the function. If we instead consider a function
f (x, y) of two variables, we can visualize the function values (x, y, f (x, y)) as defining the height of a point on a
two-dimensional terrain. If f is smooth, then each point of the terrain can be associated with tangent plane. The
“slope” of the tangent plane passing through such a point is defined by the partial derivatives of the function,
namely ∂f/∂x and ∂f/∂y. The vector (∂f/∂x, ∂f/∂y) is a vector in the (x, y)-plane that points in the
direction of steepest ascent for the function f . This vector
changes from point to point, depending on f . It is called the gradient of f , and is often denoted ∇f .
Perlin’sapproachto producinganoisy 2-dimensionalterrain involvescomputingarandom 2-dimensional gradient
vector at each vertex of the grid with the eventual aim that the smoothed noise function have this gradient value.
Since these vectors are random, the resulting noisy terrain will appear to behave very differently from one
vertex of the grid to the next. At one vertex the terrain may be sloping up to the northeast, and at a
neighboring vertex it may be slopping to south-southwest. The random variations in slope result in a very
complex terrain. But how do we define a smooth function that has this behavior? In the one dimensional case we
used cosing interpolation. Let’s consider how to generalize this to a two-dimensional setting.
Consider a single square of the grid, with corners (x0, y0), (x1, y0), (x1, y1), (x0, y1). Let g[0,0], g[1,0],
g[1,1], and g[0,1] denote the corresponding randomly generated 2-dimensional gradient vectors (see Fig.
51(c)). Now, for each point (x, y) in the interior of this grid square, we need to blend the effects
of the gradients at the corners. To do this, for each corner we will compute a vector from the corner to the point
(x, y). In particular, define
Fading: The problem with these scalar displacement values is that they are affected by all the corners of the square,
and in fact, as we get farther from the associated corner point the displacement gets larger. We want the gradient
effect to apply close to the vertex, and then have it drop off quickly as we get closer to another vertex. That is,
we want the gradient effect of this vertex to fade as we get farther from the vertex. To do this, Perlin defines the
following fade function. This is a function of t that will start at 0 when t = 0 (no fading) and will approach 1
when t = 1 (full fading). Perlin originally
settled on a cubic function to do this, ϕ(t) = 3t2 −2t3. (Notice that this has the desired properties, and further
its derivative is zero at t = 0 and t = 1, so it will smoothly interpolate with neighboring squares.) Later, Perlin
observed that this function has nonzero second derivatives at 0 and 1, and so he settled on the following
improved fade function:
1
The fade function
Because we want the effects to fade as a function of both x and y, we define the joint fade function to be the
product of the fade functions along x and y:
Ψ(x, y) = ψ(x)ψ(y).
The final noise value at the point (x, y), arises by taking the weighted average of gradient displacements, where
each displacement is weighted according to the fade function.
We need to apply the joint fade function differently for each vertex. For example, consider the fading
for the displacement δ[1,0] of the lower right corner vertex. We want the influence of this vertex to increase as x
approaches 1, which will be achieved by using a weight of ψ(x). Similarly, we want the influence of this vertex
to increase as y approaches 0, which will be achieved by using a weight of ψ(1 −y). Therefore, to achieve
both of these effects, we will use the joint weight function Ψ(x, 1− y).
By applying this reasoning to the other corner vertices, we obtain the following 2-dimensional noise
function.
Σk
perlin(x, y) = pi · noise(2i · x, 2i · y).
i=0
(As before, recall that the value 2i can be replaced by some parameter Ai, where A > 1.) This applies to
each square individually. We need to perform the usual “modding” to generalize this to any square of the
grid. (An example of the final result is shown in Fig. 53(a) and a terrain resulting from applying this is shown
in Fig. 53(b). Note that the terrain does not look as realistic as the terragen from Fig. 51(a). There are
other processes, such as erosion and geological forces that need to be modeled to achieve highly realistic
terrains.)
noise(t)
Fig. 53: (a) Two-dimensional Perlin noise and (b) a terrain generated by Perlin noise.
Source Code: While the mathematical concepts that we have discussed are quite involved, it is remarkable that
Perlin noise has a very simple implementation. The entire implementation can be obtained from Perlin’s web
page, and is shown nearly in its entirety in the code block below.
If you have taken a course in formal language theory, the concept of an L-system is very similar to the concept
of a context-free grammar. We start with an alphabet, which is a finite set of characters, called symbols or
variables. There is one special symbol, called the start symbol (or axiom in L-system terminology). In L-
systems, symbols are categorized in two types. First, variables are symbols that can be replaced with other
symbols. Second, constants are symbols that are fixed and cannot be replaced. Finally, there is a finite set of
production rules. Each production replaces a single variable with a string (or zero or more) symbols (which
may be variables or constants). Such a rule is expressed in the following form:
(variable) → (string).
To get a better grasp on this, let us consider a simple example, developed by Lindenmayer himself to describe
the growth of algae.
variables : {A, B}
constants : ∅ (none)
start : A
rules : A → AB; B→A
An L-system works as follows. Starting with the start symbol, we repeatedly replace each variable with a
compatible rule. In this case, each occurrence of A is mapped to AB and each occurrence of B is mapped to A.
This is repeated for some number of levels. (Note that this is major difference between L-systems and context-
free grammars. In context-free grammars, any one rule is applied. In L-systems, all the applicable rules are
applied in parallel. The above grammar produces the following sequence of strings (for the first 7 levels of
application):
n=0:A
n = 1 : AB
n = 2 : ABA
n = 3 : ABAAB
n = 4 : ABAABABA
n = 5 : ABAABABAABAAB
n = 6 : ABAABABAABAABABAABABA
n = 7 : ABAABABAABAABABAABABAABAABABAABAAB
As an aside, if you count the number of symbols in each of the strings, you’ll observe the sequence
(1, 2, 3, 5, 8, 13, 21, .). . . Is this sequence familiar? This is the famous Fibonacci sequence, which has been
observed to arise in the growth patterns of many organisms.
Using L-Systems to Generate Shapes: So what does this have to do with shape generation? The idea is to let
each symbol represent some sort of drawing command (e.g., draw a line segment, turn at a specified angle, etc.)
This is sometimes referred to as turtle geometry (I guess this is because each command is thought of as being
relayed to a turtle who carries out the drawing process).
Consider the following L-system:
variables : {0, 1}
constants : {[ , ]}
start : 0
rules : 1 → 11; 0→1[0]0
If we carry out the first few stages of the expansion, we have
n=0:0
n=1:1[0]0
n = 2 : 11 [ 1 [ 0 ] 0 ] 1 [ 0 ] 0
n = 3 : 1111 [ 11 [ 1 [ 0 ] 0 ] 1 [ 0 ] 0 ] 11 [ 1 [ 0 ] 0 ] 1 [ 0 ] 0
There is nothing particularly pictorial about this, but now let’s assign some drawing instructions. Let us
assume that we associate a push-down stack with the drawing process. Define
• “0”: Draw a unit length line segment with a leaf on the end and increase
• “1”: Draw a unit length line segment
• “[”: push the current position/scale and angle on the stack turn CCW 45 ◦, and scale by 1/2
• “]”: pop the current position/scale and angle off the stack, turn CW 45 ◦, and scale by 1/2 (An
L-system that uses [ and ] is sometimes referred to as an L-system with brackets.)
Now, if we use the above directions as the basis for generating a turtle-geometry drawing, we obtain the
drawing shown in Fig. 55.
Of course, this is far from being a realistic tree, but it is not hard to enhance this basic system with more
parameters and some randomness in order to generate something that looks quite like a tree.
Randomization and Stochastic L-Systems: As described, L-systems generate objects that are too reg- ular
to model natural objects. However, it is an easy matter to add randomization to the process.
The first way of introducing randomness is to randomize the graphical/geometric operations. For ex- ample,
rather than mapping terminal symbols into fixed actions (e.g., draw a unit-length line segment), we could add
some variation (e.g., draw a line segment whose length is a random value between 0.9 and 1.1). Examples
include variations in drawing lengths, variations in branching angles, and variations in thickness and/or texture
( see Fig. 54.)
While the above modifications alter the geometric properties of the generated objects, the underlying structure
is still the same. We can modify L-systems to generate random structures by associating each production
rule with a probability, and apply the rules randomly according to these probabilities. For example consider the
following two rules:
a −→[0.4] a[ b]
a −→[0.6] b[ a] b
0 1[0]0 11[1[0]0]1[0]0
1111[11[1[0]0]1[0]0]11[1[0]0]1[0]0
0 0 0
0 0 0 1 0
0 0 1
0 0 1 0 1 0
1 1
1
1
1
1
0 1
1
1
n=0 n=1 n=2 n=3
Fig. 55: A simple tree-like structure generated by an L-system. (Note that the figure is not drawn exactly to scale.)
With each branching, the scale factor decreases by 1/2.
The interpretation is that the first rule is to by applied 40% of the time and the second rule 60% of the time.