## Perlin noise

### Posted on November 20, 2014

My last project has been the World generator. This project uses perlin noise algorithm to generate a world. Perlin noise is a wide used algorithm in procedural generation (Minecraft uses it) so I'm going to explain how this is made.

Many people have used random number generators to generate textures. Random number generators certainly have their uses, but at times their output can be too harsh to appear natural. Perlin noise is a function which has a very wide range of uses, more than I can think of, but basically anywhere where you need something to look natural in origin.

If you look at many things in nature, you will notice that they are fractal. They have various levels of detail. A common example is the outline of a mountain range. It contains large variations in height (the mountains), medium variations (hills), small variations (boulders), tiny variations (stones)... you could go on. Look at almost anything: the distribution of patchy grass on a field, waves in the sea, the movements of an ant, the movement of branches of a tree, patterns in marble, winds. All these phenomena exhibit the same pattern of large and small variations. The Perlin Noise function recreates this by simply adding up noisy functions at a range of different scales.

A noise function is essentially a seeded random number generator. It takes an integer as a parameter, and returns a random number based on that parameter. If you pass it the same parameter twice, it produces the same number twice. It is very important that it behaves in this way, otherwise the Perlin function will simply produce nonsense. By smoothly interpolating between the values, we can define a continuous function that takes a non-integer as a parameter.

Now, if you take lots of such smooth functions, with various frequencies and amplitudes (called octaves), you can add them all together to create a nice noisy function. This is the Perlin Noise Function.

You can, of course, do the same in 2 dimensions, interpolating the 2D noise function. Then, a height map is generated, and you can color it depending on the height.

On this image, the same height map is rendered 4 different ways. The top left render paints it with a blue-cyan-yellow-green-grey-white gradient, and appears to be an earth drawing. Top right render uses a colour spectrum, similar to heat maps. The left bottom one renders it as a level curve. The right bottom one uses a threshold, and the result is similar to clouds. There are lots of possibilites rendering the map, for example, using a sine function over the height:

At last, you can use as many octaves as you want. The more octaves you use, the sharper the height map will be:

Hope you like this explanation, you can get more info about perlin noise at http://freespace.virgin.net/hugo.elias/models/m_perlin.htm, the main reference I've used. Now, you can try it yourself!