Procedural Terrain Generation – Noise Layers – Part 3

I previous post we generated a plane, and we added noise to it. This time we are going to add layers to the noise so we will have more detail in the terrain.

If you haven’t seen previous posts, I would recommend doing so, as the content of this post is based on them.

Without further ado, let’s get into it! ?‍?

The first thing that we need to do is to move our noise parameters from generator class to the separate class.

using UnityEngine;

/// <summary>
/// Noise layer.
/// </summary>
[System.Serializable]
public class NoiseLayer
{
    // Multiplier for noise.
    [SerializeField]
    private float noisePower = 1;

    // Noise offset.
    [SerializeField]
    private Vector2 noiseOffset;

    // Noise scale.
    [SerializeField]
    private float noiseScale = 1;

    /// <summary>
    /// Evalate value for X and Y coords.
    /// </summary>
    /// <returns>Returns value from noise.</returns>
    /// <param name="x">The x coordinate (0.0-1.0).</param>
    /// <param name="y">The y coordinate (0.0-1.0).</param>
    public float Evalate(float x, float y)
    {
        // Adding elevation from perlin noise.
        float noiseXCoord = noiseOffset.x + x * noiseScale;
        float noiseYCoord = noiseOffset.y + y * noiseScale;
        return (Mathf.PerlinNoise(noiseXCoord, noiseYCoord) - 0.5f) * noisePower;
    }
}

Now, our next step will be to use our newly created class in the generator!

// code – changes in generation

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Script that generates a plane with width and depth.
/// </summary>
public class PlaneGenerator : MonoBehaviour
{
    ...

    [Header("Noise")]
    // Noise layers.
    [SerializeField]
    private List<NoiseLayer> noiseLayers = new List<NoiseLayer>();

    ...

    /// <summary>
    /// Method which generates plane.
    /// </summary>
    private void GeneratePlane()
    {

        ...

        int i = 0;
        for (int d = 0; d <= depth; d++)
        {
            for (int w = 0; w <= width; w++)
            {
                // Setting vertice position.
                vertices[i] = new Vector3(w, 0, d) - new Vector3(width / 2f, 0, depth / 2f);

                // Calculating elevation from noise layers.
                float elevation = 0;
                for (int l = 0; l < noiseLayers.Count; l++)
                {
                    elevation += noiseLayers[l].Evalate(w / (float)width, d / (float)depth);
                }

                // Adding elevation from perlin noise.
                vertices[i].y = elevation;

                i++;
            }
        }

        ...

    }

    ...

}

These changes allow us to control parameters on each layer and add more detail to the terrain and make it more interesting.

Array of noise layers.

At this point, there is just one last thing to do. Test it! ?

Editing noise layers.

Now, this is looking much better than the last one! ?

If you want to get notified on future content, sign up for the newsletter!

As always, the whole project is available at my public repository. ?

And I hope to see you next time! ?

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x