1

I'm experimenting with Perlin Noise in C#. I managed to create a working 2D map generator. But now I want to make my noise tileable. I want that the right edge of the map fits in the left edge of the map.

I'vw been searching for this on the internet and I have tried different things but I could not get it to work. Is there someone who knows a formula for this?

Here is my code:

public float[,] GenerateNoise(float[,] map, int xsize ,int ysize ,float scale, int seed,
                              int octaves, float persistance,float lacunarity)
{
    Random rnd = new Random(seed);

    float offsetX = rnd.Next(0, 100000);
    float offsetY = rnd.Next(0, 100000);

    if (scale <= 0)
    {
        scale = 0.0001f;
    }
    float minNoiseHeight = float.MinValue;
    float maxNoiseHeight = float.MaxValue;

    float[,] fallmap;
    //fallmap = FalloffGenerator.GenerateFalloffMap(xsize);
    Perlin Noise = new Perlin();

    //perlin noise
    for (int y = 0; y <= ysize - 1; y++)
    {
        for (int x = 0; x <= xsize - 1; x++)
        {
            float amplitude = 1f;
            float frequency = 1f;
            float noiseHeight = 0;


            for (int i = 0; i < octaves; i++)
            {
                float sampleX = (xsize - x) / scale * frequency + offsetX;
                float sampleY = (ysize - y) / scale * frequency + offsetY;

                float PerlinValue = (float)Noise.perlin(sampleX, sampleY, 0) * 2 - 1;
                map[x, y] = PerlinValue;
                noiseHeight += PerlinValue * amplitude;
                amplitude *= persistance;
                frequency *= lacunarity;

                // map[x, y] = map[x, y] - fallmap[x, y];
            }

            if(noiseHeight > maxNoiseHeight)
            {
                maxNoiseHeight = noiseHeight; 
            }else if (noiseHeight < minNoiseHeight)
            {
                minNoiseHeight = noiseHeight;
            }
                map[x, y] = noiseHeight;
            //map[x, y] = map[x, y] - fallmap[x, y] ;

        }
    }

    return map;
 }
TaW
  • 53,122
  • 8
  • 69
  • 111
  • See also: [Wrapping 2D perlin noise](https://stackoverflow.com/questions/5587846/wrapping-2d-perlin-noise) – Corak Apr 22 '18 at 12:11
  • i already found that one i converted it to c# but it didnt work every point was the same value – yente paternotte Apr 22 '18 at 12:15
  • 1
    _I could not get it to work._ We need a much more clearer description of what the problem s. An image would also help.. – TaW Apr 22 '18 at 12:35
  • i used different things i found online if they didn't work i reverted my code back to normal so i don't have all those things i tried anymore – yente paternotte Apr 22 '18 at 12:39
  • so i tried @Corak sugestion again and i now get a different result. the code doesnt get past the if (x % size != 0 || y % size != 0) – yente paternotte Apr 22 '18 at 13:15

0 Answers0