Game Development
Perlin noise
Learn to generate natural-looking surfaces with Perlin noise.
Perlin Noise and Natural Gradients
Pure random numbers place adjacent pixels in harsh contrast resembling television static. Perlin Noise is a gradient noise algorithm producing smooth, natural transitions between coordinates.
// Yields a smooth continuous value between 0.0 and 1.0
float height = Mathf.PerlinNoise(x * 0.1f, y * 0.1f);
These coherent transitions serve as an ideal foundation for rolling hills, valleys, coastlines, and cloud textures.
Scale Factor and Heightmaps
In Perlin noise, coordinates are multiplied by a scale factor to control frequency. A low scale produces broad valleys and lakes, while a high scale yields frequent, jagged crags.
float xCoord = (float)x / width * scale;
float yCoord = (float)y / height * scale;
float sample = Mathf.PerlinNoise(xCoord, yCoord);
The calculated samples directly represent the heightmap (Y-axis altitude) across the grid terrain.
Exercise Perlin elevation calculation
Define a Start method. Inside, declare float yukseklik = 0.75f; and print "Arazi yuksekligi: " + yukseklik to the console.
Show example solution
using UnityEngine;
public class PerlinTester : MonoBehaviour
{
void Start()
{
float yukseklik = 0.75f;
Debug.Log("Arazi yuksekligi: " + yukseklik);
}
}
Test yourself
-
What is the primary reason Perlin Noise is used instead of pure randomness when generating terrain, islands, or hills?
- Being used only for editing audio files
- Providing smooth and natural elevation transitions between neighboring points
- Reducing network latency (ping) to zero
- Converting all 3D models on screen into 2D sprites
Show answer
Providing smooth and natural elevation transitions between neighboring points — Perlin noise produces organic, continuous altitude gradations without jagged spikes across adjacent points.
-
Sample Perlin noise
float sample = Mathf.____(xCoord, yCoord);
Show answer
float sample = Mathf.PerlinNoise(xCoord, yCoord);
-
Perlin noise terms
- Perlin Noise
- Natural and smooth gradient noise
- Mathf.PerlinNoise
- Unity 2D Perlin noise function
- Scale
- Frequency scale of hills and valleys
- Heightmap
- Map holding elevation data
Next lesson: Grid generation