c# - 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 命令只返回相同的数字?
解决方案
AfaikMathf.PerlinNoise
生成大小为 1 * 1 的模式。不确定它是否甚至不重复。
我怀疑在您的情况下会发生什么是您传递了整数,因此它始终返回相同/相似的值。
尝试将其缩小到从0
到1
喜欢的分数,例如
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)); }
推荐阅读
- node.js - Flutter + FCM:java.lang.IllegalArgumentException:不支持的值:Bundle[mParcelledData.dataSize=100]
- c# - 为什么我可以从计算机堆栈c#的末尾访问数据
- python - 为什么这个 django url 不在 Mac 上运行?
- r - 使用 openxlsx 包的工作表链接问题
- node.js - 无法从猫鼬模型中获得文档“查找”承诺
- web - 如何使用思维导图导航创建网站
- android - 使用 firebase 数据库在回收站视图中使用 admob 原生广告
- javascript - 如何删除“显示密码”按钮?
- java - 如何从 pom.xml 的属性文件中读取属性
- node.js - jimp.loadFont 函数使用元音字符使非英语(泰语)字体变得丑陋