Unity генерирует сетку для куба - PullRequest
0 голосов
/ 28 августа 2018

Извините, недавно я попробовал сделать игру похожую на 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, он может отображаться правильно, как это.

enter image description here

Спасибо за любую помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...