Perlin Noise:我如何制作一个可拼图的地图

时间:2018-04-22 10:53:27

标签: c# .net perlin-noise

我在C#中试验Perlin Noise。我设法创建了一个有效的2D地图生成器。 但现在我想让我的噪音可以拼凑。我希望地图的右边缘适合地图的左边缘。

我在互联网上一直在寻找这个,我尝试了不同的东西,但是我无法让它发挥作用。是否有人知道这个公式?

这是我的代码:

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;
 }

0 个答案:

没有答案