Procedural Terrain Generation

By Saif Jouda | Feb. 7 2024

Introduction

Procedural terrain generation is a powerful technique used to create vast and realistic landscapes dynamically. In Unity, developers can leverage various algorithms and methods to generate terrains that provide a diverse and immersive experience for players. This article explores one of the many ways procedural terrain generation is created, and will focus on essential concepts and techniques.

Interactable

Section 1: Vertices and Meshes

In procedural terrain generation, vertices play a crucial role in defining the shape and structure of the terrain mesh. Vertices are points in 3D space that, when connected, form the triangles that make up the terrain's surface. The strategic placement and manipulation of these vertices are key to achieving realistic and visually appealing terrains.

1.1 Poisson Disk Sampling

Poisson Disk Sampling is an algorithm used to distribute vertices evenly across the x and z position. By ensuring a minimum distance between points, Poisson Disk Sampling prevents clustering and results in a more natural-looking mesh. This technique is particularly effective in creating diverse landscapes with well-distributed features.

Poisson Disk Sampling Example
Figure 1.1: Random vs Poisson Disk Sampling

1.2 Perlin Noise

Perlin Noise is a gradient noise function that is widely used in procedural terrain generation to set the vertices y position. By applying Perlin Noise to the terrain's vertices, developers can create natural-looking features like mountains, valleys, and other complex landscapes. The smooth, continuous variations produced by Perlin Noise contribute to the overall realism of the generated terrain.

Poisson Disk Sampling Example
Figure 1.2: Perlin Noise

Section 2: Triangulation for Mesh Construction

Once vertices are strategically placed, the next step in procedural terrain generation involves the construction of a mesh. Triangulation is a crucial process where these vertices are connected to form triangles, creating the mesh structure of the terrain. Efficient triangulation algorithms ensure that the resulting mesh is well-defined, providing a foundation for realistic and visually appealing terrains.

2.1 Delaunay Triangulation

Delaunay Triangulation, also known as a Delone triangulation, is a widely-used algorithm that connects vertices in a way that maximizes triangle quality. The key criterion is ensuring that no vertex lies inside the circumcircle of any triangle in the mesh, producing well-shaped triangles. This method is highly effective in procedural terrain generation, creating smooth and aesthetically pleasing landscapes.

How Delaunay Triangulation Works

Delaunay Triangulation operates by iteratively adding vertices to the mesh while maintaining the Delaunay criterion. The algorithm dynamically adjusts the connections between vertices, creating a triangulated mesh that conforms to the input point distribution. This process is fundamental to generating realistic terrains with a natural flow.

Delaunay Triangulation Example
Figure 2.1: Delaunay Triangulation Example With Circumcircles Showing

Section 3: Coloring the Terrain

The final step is to color the terrain. This step adds visual appeal and realism to the generated landscape. One effective way to color the terrain is by using a gradient based on the average height of the vertices.

3.1 Height Driven Gradient

The terrain can be colored using a gradient that represents different elevations. By first calculating the average height of all the vertices, each triangle can be colored based on its position to the average height. This technique creates a smooth transition of colors across the terrain, simulating natural variations in elevation.