[英]Getting weird results from terrain generation sys using 2D Perlin Noise
我正在嘗試在 Unity 中創建一個地形生成系統,類似於 Minecraft 的,但使用 Unity 的 Perlin Noise function(所以只有 2D 噪聲)。
所以我有一個vector2int
的塊,它的 vector2int 是 position(就像,如果 x & z = 0,那么里面的塊在世界坐標中是從 0 到 16)。
這就是我試圖生成一個塊的高度 map 的方式:
public void generate(float scale) {
GameObject root = new GameObject("Root");
// this.z & this.x are the chunk coordinates, size is 16
for(int z = this.z * size; z < (this.z + size); ++z) {
for (int x = this.x * size; x < (this.x + size); ++x) {
float[] coord = new float[2] { (float)x / size * scale,
(float)z / size * scale };
Debug.LogFormat("<color='blue'>Perlin coords |</color> x: {0}; y: {1}", coord[0], coord[1]);
float value = Mathf.PerlinNoise(coord[0], coord[1]);
// temporary
GameObject Cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
Cube.transform.position = new Vector3(x, value, z);
Cube.transform.parent = root.transform;
}
}
return;
}
結果……不好。 你自己看:
我能做些什么?
它看起來不錯,看起來只是在 y 變換上變形。
float value = Mathf.PerlinNoise(coord[0], coord[1]);
這會給你帶來問題,我不確定 coord[0] 和 coord[1] 是什么,但 Mathf.PerlinNoise 會返回 coord[0] 和 coord[1] 之間的隨機浮點數,所以隨機浮點數永遠不會能夠生產對齊良好的瓷磚。
最好做類似的事情
int numTilesHigh = Random.Range(0,15);
for (int i = 0; i < numTilesHigh; i++) {
GameObject Cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
Cube.transform.position = new Vector3(x, <cube height> * i, z);
Cube.transform.parent = root.transform;
}
ps 我有點喜歡你的屏幕截圖,不是以我的世界的方式,但它看起來很酷:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.