[我正在尝试在Unity中制作类似于Minecraft的地形生成系统,但使用Unity的Perlin噪波功能(因此仅2D噪波。
所以我有一个16x16x16的块,其位置为vector2int
(例如,如果x&z = 0,则其中的块在世界坐标中从0到16)。
这就是我试图生成块的高度图的方式:
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,我有点喜欢您的屏幕截图,虽然不是我的世界,但看起来确实很酷:-)