DEV Community

Dillon Barrass
Dillon Barrass

Posted on

A Procedural Terrain Adventure in Unity pt.1

This is a little blog I'm writing to keep track of my development progress, along with a sort of tutorial for others. I find that if I can explain a concept to other people, I understand it better myself. I am by no means an expert in any of the topics I will talk about, this is just me learning as I go. Some things may be wrong or inefficient, and hopefully I will discover these eventually.

Since completing a university project that involved Unity's terrain system, I got quite interested in various ways of procedurally generating terrain. For context, I'm using Unity and will try to reference sources of information where appropriate. So let's get started.

Noise

Noise is a pattern of values that seem somewhat random but remain smooth and continuous. When generating terrain, we need a way to determine the heights of each point on the landscape. To achieve this, we can generate noise as a simulation of elevation values. There are many different noise algorithms available, but here we'll just use perlin noise, one of the most popular and widely-used approaches.

An image of perlin noise


An image of perlin noise

In this image, the brightness corresponds to height. Black represents a height of 0.0 and white represents a height of 1.0. However, this doesn't create very interesting terrain on its own. To make our heightmap more visually compelling and realistic, we can introduce octaves.

Octaves

Octaves are multiple layers of noise at different scales, all combined together. Each layer progressively adds finer detail, giving the heightmap much more complexity and realism.

A GIF of perlin noise moving from 1 to 8 octaves


A GIF of perlin noise moving from 1 to 8 octaves

Notice how each additional octave introduces finer detail. With just one octave the terrain looks smooth and simple. By eight octaves we have a much more natural, complex landscape with detail at multiple scales. However, there are diminshing returns; after a certain point (here around 5 octaves) additional octaves add very little noticeable detail while still impacting performance. Finding the right balance between visual quality and computational cost is an important consideration we'll experiment more with later.

Persistence and Lacunarity

The behaviour of these octaves is primarily controlled by two important parameters: persistence and lacunarity.

Persistence controlls how much each successive octave contributes to the final noise.

  • High persistence means that later octaves remain strong in their contribution, leading to more detailed, rough and chaotic terrain.
  • Low persistence means that later octaves contribute less to the final result, producing smoother, gentler terrain.

Here's a comaprison of low versus high persistence:

Noise with a persistence of 0.3 and lacunarity of 2
Noise with a persistence of 0.3 and lacunarity of 2

Noise with a persistence of 0.7 and lacunarity of 2
Noise with a persistence of 0.7 and lacunarity of 2

Lacunarity controls how much the frequency (how "zoomed in" the noise is) increases between each octave. A lacunarity of 2.0 means each octave doubles in frequency. Higher lacunarity creates bigger jumps in detail size between octaves, while lower lacunarity values create more similar sized features across octaves.

Noise with a persistence of 0.5, lacunarity increasing from 1 to 4


Noise with a persistence of 0.5, lacunarity increasing from 1 to 4

We now have a good baseline for creating our own terrain. Now to enhance your imagination a bit, we can create a colourmap from this heightmap, assinging different colours to different height bands, giving us a better idea of what the landscape will look like.

Colour map


Colour map with water, grass, rock and snow

In the image heights from 0 - 0.4 become water), from 0.4 - 0.55 become grass, from 0.55 - 0.65 become rock, and from 0.65 - 1.0 become snow.

Top comments (0)