Unity为立方体生成网格

时间:2018-08-28 01:21:14

标签: unity3d

对不起, 最近,我尝试制作一款类似于Minecraft的游戏。

并且在地形的生成过程中遇到了一些问题。

我已经尝试更改一些坐标,没有运气。

for (int x = 0; x < Chunk.size.x; x++)
    {
        for (int y = 0; y < Chunk.size.y; y++)
        {
            for (int z = 0; z < Chunk.size.z; z++)
            {
                // Check if 0 (transparent), skip
                if (faces[index] == 0)
                {
                    index++;
                    continue;
                }

                // Generate face toward North
                if ((faces[index] & (byte)Direction.North) != 0 )
                {
                    // Points on face toward the North
                    vertices[vertexIndex] = new Vector3(x + position.x, y + position.y, z + position.z + 1);
                    vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y, z + position.z + 1);
                    vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);
                    vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z + 1);

                    // First triangle
                    triangles[trianglesIndex] = vertexIndex + 1;
                    triangles[trianglesIndex + 1] = vertexIndex + 2;
                    triangles[trianglesIndex + 2] = vertexIndex;
                    // Second triangle
                    triangles[trianglesIndex + 3] = vertexIndex + 1;
                    triangles[trianglesIndex + 4] = vertexIndex + 3;
                    triangles[trianglesIndex + 5] = vertexIndex + 2;

                    // Increments
                    vertexIndex += 4;
                    trianglesIndex += 6;
                }

                // Generate face toward East
                if ((faces[index] & (byte)Direction.East) != 0 )
                {
                    // Points on face toward the East
                    vertices[vertexIndex] = new Vector3(x + position.x + 1, y + position.y, z + position.z);
                    vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y, z + position.z + 1);
                    vertices[vertexIndex + 2] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z);
                    vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z + 1);
                    // First triangle
                    triangles[trianglesIndex] = vertexIndex;
                    triangles[trianglesIndex + 1] = vertexIndex + 2;
                    triangles[trianglesIndex + 2] = vertexIndex + 1;
                    // Second triangle
                    triangles[trianglesIndex + 3] = vertexIndex + 2;
                    triangles[trianglesIndex + 4] = vertexIndex + 3;
                    triangles[trianglesIndex + 5] = vertexIndex + 1;

                    // Increments
                    vertexIndex += 4;
                    trianglesIndex += 6;
                }

                // Generate face toward South
                if ((faces[index] & (byte)Direction.South) != 0 )
                {
                    // Points on face toward the South
                    vertices[vertexIndex] = new Vector3(x + position.x, y + position.y, z + position.z);
                    vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y , z + position.z);
                    vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z);
                    vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z);
                    // First triangle
                    triangles[trianglesIndex] = vertexIndex;
                    triangles[trianglesIndex + 1] = vertexIndex + 2;
                    triangles[trianglesIndex + 2] = vertexIndex + 1;
                    // Second triangle
                    triangles[trianglesIndex + 3] = vertexIndex + 2;
                    triangles[trianglesIndex + 4] = vertexIndex + 3;
                    triangles[trianglesIndex + 5] = vertexIndex + 1;

                    // Increments
                    vertexIndex += 4;
                    trianglesIndex += 6;
                }

                // Generate face toward West
                if ((faces[index] & (byte)Direction.West) != 0 )
                {
                    // Points on face toward the West
                    vertices[vertexIndex] = new Vector3(x + position.x, y + position.y, z + position.z);
                    vertices[vertexIndex + 1] = new Vector3(x + position.x, y + position.y, z + position.z);
                    vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);
                    vertices[vertexIndex + 3] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);
                    // First triangle
                    triangles[trianglesIndex] = vertexIndex + 1;
                    triangles[trianglesIndex + 1] = vertexIndex + 2;
                    triangles[trianglesIndex + 2] = vertexIndex;
                    // Second triangle
                    triangles[trianglesIndex + 3] = vertexIndex + 1;
                    triangles[trianglesIndex + 4] = vertexIndex + 3;
                    triangles[trianglesIndex + 5] = vertexIndex + 2;

                    // Increments
                    vertexIndex += 4;
                    trianglesIndex += 6;
                } 

                // Generate face toward Up
                if ((faces[index] & (byte)Direction.Up) != 0 )
                {
                    // Points on face toward the Up
                    vertices[vertexIndex] = new Vector3(x + position.x, y + position.y + 1, z + position.z);
                    vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z);
                    vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);
                    vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z + 1);
                    // First triangle
                    triangles[trianglesIndex] = vertexIndex;
                    triangles[trianglesIndex + 1] = vertexIndex + 2;
                    triangles[trianglesIndex + 2] = vertexIndex + 1;
                    // Second triangle
                    triangles[trianglesIndex + 3] = vertexIndex + 2;
                    triangles[trianglesIndex + 4] = vertexIndex + 3;
                    triangles[trianglesIndex + 5] = vertexIndex + 1;

                    // Increments
                    vertexIndex += 4;
                    trianglesIndex += 6;
                } 

                // Generate face toward Down
                if ((faces[index] & (byte)Direction.Down) != 0 )
                {
                    // Points on face toward the Down
                    vertices[vertexIndex] = new Vector3(x + position.x, y + position.y, z + position.z);
                    vertices[vertexIndex + 1] = new Vector3(x + position.x, y + position.y, z + position.z + 1);
                    vertices[vertexIndex + 2] = new Vector3(x + position.x + 1, y + position.y, z + position.z);
                    vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y, z + position.z + 1);
                    // First triangle
                    triangles[trianglesIndex] = vertexIndex;
                    triangles[trianglesIndex + 1] = vertexIndex + 2;
                    triangles[trianglesIndex + 2] = vertexIndex + 1;
                    // Second triangle
                    triangles[trianglesIndex + 3] = vertexIndex + 2;
                    triangles[trianglesIndex + 4] = vertexIndex + 3;
                    triangles[trianglesIndex + 5] = vertexIndex + 1;

                    // Increments
                    vertexIndex += 4;
                    trianglesIndex += 6;
                }
                index++;
            }
        }
    }

我有这个, enter image description here

我根本无法获得顶部网格,是坐标错误还是其他问题?

当我注释掉Perlin Noise时,可以像这样正确显示。

enter image description here

谢谢您的帮助。

0 个答案:

没有答案