【问题标题】:Unity perlin noise having repeating patterns具有重复模式的统一柏林噪声
【发布时间】:2020-04-20 08:57:51
【问题描述】:

我使用 Unity 中的 Perlin Noise 制作了一个 Noise 类,如下所示:

public static float[,] GetNoise(Vector2Int initialOffset, float scale, float persistance, float lacunarity, int octaves)
    {
        float[,] noiseMap = new float[Chunk.width, Chunk.height];
        float maxHeight = 0;
        float minHeight = 0;

        for (int y = 0; y < Chunk.height; y++)
        {
            for (int x = 0; x < Chunk.width; x++)
            {
                float amplitude = 1;
                float frequency = 1;
                float noiseHeight = 0;

                for (int oc = 0; oc < octaves; oc++)
                {
                    float coordX = (x + initialOffset.x) / scale * frequency;
                    float coordY = (y + initialOffset.y) / scale * frequency;

                    float perlin = Mathf.PerlinNoise(coordX, coordY) * 2 - 1;
                    noiseHeight += perlin * amplitude;

                    amplitude *= persistance;
                    frequency *= lacunarity;
                }

                if (noiseHeight < minHeight)
                {
                    minHeight = noiseHeight;
                }
                if (noiseHeight > maxHeight)
                {
                    maxHeight = noiseHeight;
                }
                noiseMap[x, y] = noiseHeight;
            }
        }

        for (int y = 0; y < Chunk.height; y++)
        {
            for (int x = 0; x < Chunk.width; x++)
            {
                noiseMap[x, y] = Mathf.InverseLerp(minHeight, maxHeight, noiseMap[x, y]);
            }
        }

        return noiseMap;
    }

但是,这段代码给了我这样的重复模式:

我做错了什么?还是没有办法摆脱这些模式?

【问题讨论】:

  • 有人能解释一下为什么不喜欢这个问题吗?有什么问题吗?
  • 您是否尝试在后缀中添加噪音以增加更多随机性?看到这个:forum.unity.com/threads/perlin-noise-repeated-values.248132/…
  • 您是否对 x 和 y 偏移使用不同的值?
  • @Jake 是的,偏移量是瓦片和原点(瓦片 0、0)之间的距离,我计算出每个块的大小乘以块的位置
  • @Kale_Surfer_Dude 我正在使用八度音阶,这算吗?我使用的设置是 0.5 表示持久性,2 表示空隙,4 表示八度音阶和 12 的比例

标签: unity3d perlin-noise


【解决方案1】:

我得到它的工作,不是很好,但工作。我所做的方法是为块中的每个图块生成高度图,然后我做了一些随机放置图块,同时考虑了高度图。像这样的:

if (heightMap[x, y] < 0.3 && Random.value < 0.5)
    // Add tile

这样我得到了这个结果: 编辑: 对 Perlin Noise 进行了更多研究,我发现它出于某种原因不喜欢负坐标,所以我这样做了,希望这对某人有所帮助!

【讨论】:

    【解决方案2】:

    所以 .. 像这样修复了负坐标:

    //account for negatives (ex. -1 % 256 = -1, needs to loop around to 255)
    if (noiseOffset.x < 0)
        noiseOffset = new Vector2(noiseOffset.x + noiseRange.x, noiseOffset.y);
    if (noiseOffset.y < 0)
        noiseOffset = new Vector2(noiseOffset.x, noiseOffset.y + noiseRange.y);
    

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2012-01-29
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多