0
\$\begingroup\$

I recently posted a question about how to technically represent a generated terrain similar to the game kingdoms and castles.

I got some great advice and decided to create a 1x1 cube at each coordinate since I will have very small maps, for now 50x50.

I wrote some code to generate terrain, this is super basic atm and simply instantiates either grass or water tiles depending on the noise value at the coordinate:

public static class Noise
{

    public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, float scale)
    {
        float[,] noiseMap = new float[mapWidth, mapHeight];

        if (scale <= 0)
            scale = .0001f;

        for (int y = 0; y < mapHeight; y++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                float sampleX = x / scale;
                float sampleY = y / scale;

                float perlinValue = Mathf.PerlinNoise(sampleX, sampleY);
                noiseMap[x, y] = perlinValue;
            }
        }

        return noiseMap;
    }
}

public class MapGenerator : MonoBehaviour
{
    public int mapWidth;
    public int mapHeight;
    public float noiseScale;

    [Space]
    public GameObject grassTile, waterTile;

    public void GenerateMap()
    {
        float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, noiseScale);

        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                if (noiseMap[x,y] >= .3)
                    Instantiate(grassTile, new Vector3(x, 0, y), Quaternion.identity);
                else
                    Instantiate(waterTile, new Vector3(x, -1, y), Quaternion.identity);
            }
        }
    }
}

It generate something like this:

Generated result, with random speckles of water

Now this obviously does not look like a realistic landscape. My question is, how do I decide which values should represent water, and which should represent grass, in order to get a landscape that makes sense?

What I would like my landmass to look like something that has lakes and maybe rivers, instead of these random puddles you see in the image above, more something like this:

Example of what I want to generate, with a river and a medium-sized lake

EDIT:

Setting a higher scale value lead to this:

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Searching for river generation on this same site turns up quite [a few good results], notably this answer has some good methods listed

Perlin noise is a good choice for terrain which is a continuous space, but for more discrete shapes like a river you may be better served by a different generation technique like a random walker. You could give the random walker some rules to get a more river-like 2D shape, then overlay that onto your map.

Alternatively since you have already generated some noise based terrain, you could modify the noise slightly so you get more low valleys and then add water to any area below a certain Y value, so the low points would all become rivers/lakes.

Terrain generation has many different possible approaches so experiment a bit and see which method is good for you. For more ideas check out this very thorough Unity terrain generation playlist from Sebastian Lague. He also has a video on hydraulic erosion to create even more natural and less noise-like terrain, but this is likely beyond what you are looking for.

\$\endgroup\$
  • 1
    \$\begingroup\$ I found that toying around with scale was the answer to my question, see edit.(I have already seen all the stuff you linked :) \$\endgroup\$ – Green_qaue Nov 23 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.