我正在开发程序生成的地图/游戏。我在 3D 数组中有 20x20x20 点的 3D 块。我正在使用行进立方体算法来生成块。我尝试通过添加和减去值来操纵密度,但同时尝试将相邻块边缘与块 im 修改同步。
这是我很难使用的代码,但它不起作用:
`for (int i = 0; i < 9; i++)
{
Chunk nChunk = neighbours[i].GetComponent<Chunk>();
if (i == 4) continue; //skip self
for (int z = 0; z < 20; z += 19)
{
for (int x = 0; x < 20; x += 19)
{
for (int zz = 0; zz < 20; zz += 19)
{
for (int xx = 0; xx < 20; xx += 19)
{
for (int y = 0; y < 20; y++)
{
if (Points[xx, y, zz].wPosition == nChunk.Points[x, y, z].wPosition)
nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
}
}
}
}
}
}`
我想检查边缘而不是所有点以节省性能。y 协调高度。
这就是我所说的:
//mouse click
if (Input.GetMouseButtonDown(0))
{
Ray raym = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitm;
if (Physics.Raycast(raym, out hitm))
{
if (hitm.transform.GetComponent<Chunk>())
{
Chunk cScript = hitm.transform.GetComponent<Chunk>();
for (int z = 0; z < 20; z++)
{
for (int y = 1; y < 19; y++)
{
for (int x = 0; x < 20; x++)
{
if (PointInsideSphere(cScript.Points[x, y, z].wPosition, hitm.point, 1f))
cScript.Points[x, y, z].density -= 0.1f;
}
}
}
cScript.SyncNeighbours();
}
}
}
这样它工作得很好,但我只想检查边缘。
for (int i = 0; i < 9; i++)
{
Chunk nChunk = neighbours[i].GetComponent<Chunk>();
if (i == 4) continue; //skip self
for (int z = 0; z < 20; z ++)
{
for (int x = 0; x < 20; x ++)
{
for (int zz = 0; zz < 20; zz ++)
{
for (int xx = 0; xx < 20; xx ++)
{
for (int y = 0; y < 20; y++)
{
if (Points[xx, y, zz].wPosition.x == nChunk.Points[x, y, z].wPosition.x && Points[xx, y, zz].wPosition.y == nChunk.Points[x, y, z].wPosition.y && Points[xx, y, zz].wPosition.z == nChunk.Points[x, y, z].wPosition.z)
nChunk.Points[x, y, z].density = Points[xx, y, zz].density;
}
}
}
}
}
}
for (int j = 0; j < 9; j++)
{
neighbours[j].GetComponent<Chunk>().UpdateChunk();
}