CodeBlanks Logo Icon CodeBlanks

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.

Practice Perlin elevation calculation

Define a Start method. Inside, declare float yukseklik = 0.75f; and print "Arazi yuksekligi: " + yukseklik to the console.

Starter code

Player.cs
using UnityEngine;

public class PerlinTester : MonoBehaviour
{
    // Start icinde yukseklik tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class PerlinTester : MonoBehaviour
{
    void Start()
    {
        float yukseklik = 0.75f;
        Debug.Log("Arazi yuksekligi: " + yukseklik);
    }
}

Expected output

Arazi yuksekligi: 0.75

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 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 Correct answer
  • Reducing network latency (ping) to zero
  • Converting all 3D models on screen into 2D sprites

Explanation: Perlin noise produces organic, continuous altitude gradations without jagged spikes across adjacent points.

2 Sample Perlin noise

Question
float sample = Mathf.____(xCoord, yCoord);
Answer
float sample = Mathf.PerlinNoise(xCoord, yCoord);

Explanation: The 'Mathf.PerlinNoise' method is used in Unity to compute a 2D Perlin noise value.

3 Perlin noise terms

Term Meaning
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

All lessons in Advanced