Search code examples
c#noiseperlin-noise

How do I make FastNoise Lite work properly?


I'm making a game that will use terrain generation, so I am using the C# version of the FastNoise Lite library. However, it doesn't seem to be working as I want it to.

I have the following code which generates the TileMap:

public void Generate(int mapSeed, Vector2 mapSize)
        {
            noise = new FastNoiseLite(mapSeed);
            noise.SetNoiseType(FastNoiseLite.NoiseType.Perlin);
            
            noise.SetFrequency(0.01f);
            noise.SetFractalLacunarity(2f);
            noise.SetFractalGain(0.5f);

            arrMap = new Tile[(int)mapSize.Y, (int)mapSize.X];

            for (int y = 0; y < mapSize.Y; y++)
                for (int x = 0; x < mapSize.X; x++)
                {
                    if (noise.GetNoise(x, y) <= 0.04)
                        arrMap[y, x] = new Water(new Vector2(x, y), gridSize, textures[0]);
                    else if (noise.GetNoise(x, y) <= 0.08)
                        arrMap[y, x] = new Sand(new Vector2(x, y), gridSize, textures[1]);
                    else
                        arrMap[y, x] = new Grass(new Vector2(x, y), gridSize, textures[2]);
                }            
        }

All this does is create a 2D array with some different tiles (like water or grass).

Then when I create it and draw it, I get the following result:

Image

However, I want to have something similar to:

This

The problem is that: when I change the lacunarity or the gain, nothing happens. Frequency just changes how many "patches" of grass there are. I suppose this is a problem with the library and/or the fact I don't know how to use it properly, as I've done this in python before but never got this bug.

How do I fix this?


Solution

  • All I needed to do was add a fractal type:

    noise = new FastNoiseLite(mapSeed);
    noise.SetNoiseType(FastNoiseLite.NoiseType.Perlin);
    
    noise.SetFrequency(0.01f);
    noise.SetFractalLacunarity(2f);
    noise.SetFractalGain(0.5f);
    
    // this setting
    noise.SetFractalType(FastNoiseLite.FractalType.FBm);
    

    FBm seems to be the one that best works for the terrain generation I want