如何在 Perlin 噪声上实现 Offset?

How to implement Offset on Perlin noise?

我需要一点帮助, 我有这个 Perlin 噪声函数,但我不知道如何正确创建偏移量。

我正在使用它来创建无限地形生成,当我使用这个脚本时,各个块的噪声值不能正确地组合在一起。他们创造了洞。

有办法解决这个问题吗?

  public  float[,] GenerateNoise(int chunkSize, int octaves, string seed, float noiseScale, float persistence, float lacunarity, Vector2 offset)
    {
        
        if (noiseScale <= 0)
        {
            noiseScale = 0.0001f;
        }


        float halfWidth = chunkSize / 2f;
        float halfHeight = chunkSize / 2f;

        float[,] noiseMap = new float[chunkSize, chunkSize];
        System.Random rand = new System.Random(seed.GetHashCode());

        //Octaves offset
        Vector2[] octavesOffset = new Vector2[octaves];
        for (int i = 0; i < octaves; i++)
        {
            float offset_X = rand.Next(-100000, 100000) + offset.x;
            float offset_Y = rand.Next(-100000, 100000) + offset.y;
            octavesOffset[i] = new Vector2(offset_X / chunkSize , offset_Y / chunkSize);
        }

        for (int x = 0; x < chunkSize; x++)
        {
            for (int y = 0; y < chunkSize; y++)
            {

                float amplitude = 1;
                float frequency = 1;
                float noiseHeight = 0;
                float superpositionCompensation = 0;

                
                for (int i = 0; i < octaves; i++)
                {


                    float sampleX = (x - halfWidth) / noiseScale * frequency + octavesOffset[i].x * frequency;
                    float sampleY = (y - halfHeight) / noiseScale * frequency + octavesOffset[i].y * frequency;

                    float noiseValue = Mathf.PerlinNoise(sampleX, sampleY);
                    noiseHeight += noiseValue * amplitude;
                    noiseHeight -= superpositionCompensation;

                    amplitude *= persistence;
                    frequency *= lacunarity;
                    superpositionCompensation = amplitude / 2;

                }

                noiseMap[x, y] = Mathf.Clamp01(noiseHeight); 
                
            }
        }

        return noiseMap;
    }

其实很简单,就是把chunk的x,y坐标加到Mathf.PerlinNoise上。以你的代码为例,你可以:

  1. 将 chunkPosition 作为参数传递给它:

    public  float[,] GenerateNoise(Vector2 chunkPos, int chunkSize, int octaves, string seed, float noiseScale, float persistence, float lacunarity, Vector2 offset)
    
  2. 将其添加到 Mathf.PerlinNoise 调用:

    float noiseValue = Mathf.PerlinNoise(sampleX + chunkPos.x, sampleY + chunkPos.y);
    

然后确保为每个块生成一个合适的 chunkPos,其中 chunkPos 可以是它的 transform.position 或任何你拥有的坐标。 就是这样。