Skip to content

Noise Generation Guide

Peter Robinson edited this page Jun 22, 2022 · 1 revision

The NoiseGenerator object is available starting in version 4.0 ea2 which is currently only available in the development branch.

Introduction

The NoiseGenerator object uses the Perlin Noise Algorithm to create pseudorandom values between 0 and 1. So to be clear, it has nothing to do with sound (at least not directly). It simply creates random values that are the same every time for a given seed. There's a million ways to use these values in a game, but the most common is terrain generation. As such, there's a simple terrain generation example toy called the Noise Toy in the Toy Box. Using the NoiseGenerator is straightforward, but it can be helpful to know what's happening under the hood. Search for articles about Perlin Noise if you want to gain a deeper understanding.

How to Use the NoiseGenerator

After creating a NoiseGenerator, the first thing you'll need to do is set the seed. Again, this is a pseudorandom number generator, which is means that if you give it the same seed you'll get the same numbers.

%generator = new NoiseGenerator();
%generator.setSeed(12345);

Once the seed is set, you can start pulling out values right away. To do this, you'll need two distinct floating point values, such as an x and y position.

%value = %generator.getNoise(%x, %y);

The most important thing to get the results you want is to play with the scale. The Perlin Noise Algorithm creates random values that smoothly transition in a gradient. So if use the value to draw a picture and it's too smooth then you could multiply x and y by a factor greater than 1 to "zoom out" the random values. Or you could use a value less than 1 and greater than zero to "zoom in" if you can't see any gradient at all.

%zoomOut = 2;
%value = %generator.getNoise(%x * %zoomOut, %y * %zoomOut);

%zoomIn = 0.5;
%value = %generator.getNoise(%x * %zoomIn, %y * %zoomIn);

The Noise Toy does something like this to make a convincing map.

Increasing Complexity with Octaves

Although the getNoise() function might produce the results you want, you can make more convincing noise by layering the noise with increasingly smaller iterations of the noise. These are fittingly called octaves. You could do this in script if you want, but there's also a function on the NoiseGenerator that does this for you, like so.

%value = %generator.getComplexNoise(%x, %y, %octave, %persistence);

The octave value should be an integer between 1 and 8 (although passing a 1 would give the exact same result as getNoise()). For each octave, a smaller layer of noise will be layered over the resulting noise, producing a more convincing random value. The persistence determines how much to decrease the noise with each additional pass. Persistence is a value between 0.05 and 0.95. If you're having trouble visualizing the results, try the Noise Toy which lets you play directly with these values. Keep in mind that higher octaves means more work for the engine, so complex noise is best used for things like generating terrain.

Now go make some noise!

Clone this wiki locally