首页 > 解决方案 > Mathf.PerlinNoise 仅在每个给定位置返回浮点数 0.4652731

问题描述

我正在尝试编写基于 2d tilemap 的世界生成脚本,但 Mathf.PerlinNoise 命令仅返回 0.4652731!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class WorldGeneration : MonoBehaviour
{

    public Tilemap Tilemap;
    public Tile GrassTile;

    public int width;
    public int height;

    public float threshold;

    void Start()
    {
        Tilemap.ClearAllTiles();

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Debug.Log(Mathf.PerlinNoise(x, y));
                
                if (Mathf.PerlinNoise(x, y) >= threshold)
                {
                    Tilemap.SetTile(new Vector3Int(x, y, 0), GrassTile);
                }
            }
        }

    }

}

执行脚本后完全没有错误!为什么 Mathf.PerlinNoise 命令只返回相同的数字?

标签: c#unity3d

解决方案


AfaikMathf.PerlinNoise生成大小为 1 * 1 的模式。不确定它是否甚至不重复。

我怀疑在您的情况下会发生什么是您传递了整数,因此它始终返回相同/相似的值。

尝试将其缩小到从01喜欢的分数,例如

Debug.Log(Mathf.PerlinNoise(x / width, y / height));

参见例如这篇有完全相同问题的帖子

他们都打印了相同的数字:0.4652731

function Start () 
{
     print(Mathf.PerlinNoise(0, 0));
     print(Mathf.PerlinNoise(2, 0));
     print(Mathf.PerlinNoise(0, 5));
     print(Mathf.PerlinNoise(10, 3));
     print(Mathf.PerlinNoise(2, 0));
     print(Mathf.PerlinNoise(1, 1));
}

推荐阅读