Saturday, November 16, 2013

Introduction to Perlin Noise [ Theory ]

In this blog post I will try my best to describe you what Perlin Noise is and why is it so popular. I will walk you through the very basics of Perlin Noise and help you grasp some basic concepts on it.

fig: Two-dimensional slice through 3D Perlin noise
Source: Wikipedia

Why use Random Number?


Random Numbers are used to introduce unpredictability. Unpredictability is generally used to define nature. Most of our daily tasks has random patterns. Even the tasks we do, the things we see and sounds we hear. It has been quite difficult to handle this randomness of nature and this difficulty was quite prominent in Computer Graphics. Generating nature like texture for items such as woods, clouds, metals, stones etc was difficult. Using a texture image would cost memory as it would require us to use many such texture for a single item. So, use of random number to generate texture could be solution.


Is it enough?



Right:Generated By Random No. Generator
Left: Perlin Noise

So, Random numbers could certainly be used to introduce Unpredictability, but often their uncontrollable output can look unnatural. As Ken Perlin said in one of his presentations, 
"Noise appears random, but isn't really. If it were really random, then you'd get a different result every time you call it. Instead, it's "pseudo-random" - it gives the appearance of randomness."
 He clearly states that a Random Number Generator won't be enough to define noise. He says noise is not random but he defines it as a "pseudo-random" pattern. He also says,
"Noise is band-limited - almost all of its energy (when looked at as a signal) is concentrated in a small part of the frequency spectrum."
So, it pretty much explains why a simple Random Number Generator, that we are familiar with,  wont be enough to define Noise.


The "Noise Function"


A noise function is a special random number generating function which takes an integer parameter, and returns a random number based on the input parameter. It is a Rn -> R mapping function when n defines the number of input parameters(dimensions). If you pass the same parameter twice, it returns the same output.

Nature of Noise Function(1D).



Noise function takes some input parameter, lets say 1D parameter X and retruns a random number, lets say between (0 and 1). We can't just caculate values for every possible points in a given range. So, we can say its discrete. But we make it smooth using various interpolation techniques.


Creating Perlin Noise Function


Now, that you have a noise function with a particular Amplitude and Frequency [1/(wavelength)]. We can create a Perlin Noise function. If you take lots of such smooth functions, with varying frequencies and amplitudes, you can add them all together to create a Perlin Noise Function.

Choosing Frequency and Amplitude


We know that Perlin Noise Function is the sum of different smooth Noise Function with various frequencies and amplitudes. The choise of these frequecies and amplitude define the Nature of Noise. So, Mandelbrot coined the term "Persistence" help define these parameters. 

Persistence


Persistence basically defines the value of amplitude for each successive noise function to be added. It is defined by the formula below:


Frequency = 2 ^ i
Amplitude = Persistence ^ i


Where i is the ith noise function to be added

So, for Persistence = 1/4, we have the following values of frequency and amplitude:

Frequency:1234
Amplitude:11/41/161/64

Octaves


Each sucesssively added Noise function is an Octave. They are called octaves because one Noise function differs from its sucessive other by 2*frequency.


Interpolating and Smoothing


Many of us might be quite familiar with interpolation, to those who are not, its just a process of predicting projection of values. To create a continious noise function various Interpolation is used. The most commonly used technique is the Cubic Interpolation technique.

Psuedo Code for obtaining Cubic Interpolation Values:

For a value X between V2  and V3 from the figure:

function CubicInterpolate(v1, v2, v3, v4,x)
P = (v4 - v3) - (v1 - v2)
Q = (v1 - v2) - P
R = v3 - v1
S = v2
return Px3 + Qx2 + Rx + S
end of function

After Interplolation, we can also smoothen the output of the noise function to make it more realistic or natural by trimming of the unwanted sharpness or edges. Smoothing works by accounting neighbouring values to supress surges and picks.


That's All


So, there you go. These are the very basics of Perlin Noise.
For more infomation you can check out these useful links:

Hope this post was useful. I will soon try to publish the code/implementation of Perlin Noise.



0 comments:

Post a Comment