网格有重叠 faces/triangles

Mesh has overlapping faces/triangles

为了放一些内容,我开发了一个块生成系统,可以随机生成块到1个网格中进行优化。 (类似于 Minecraft。)所有的顶点、三角形、UV 等都是从头开始制作的。如 GIF 中所示,根据您的观察方式,面孔会彼此剪裁。这里发生的可能的问题是什么?生成工作原理的基本概念:

for (int x = 0; x < 10; x++) {     //For each block in chunk being loaded on x axis,
        for (int y = 0; y < 10; y++) {     //For each block in chunk being loaded on y axis,
            for (int z = 0; z < 10; z++) {     //For each block in chunk being loaded on z axis,

                if (exists[x, y, z] == true) {

                    Vector2[] normalUVs = {
                        new Vector2(0, 0), new Vector2(.1f, 0), new Vector2(.1f, .1f), new Vector2(0, .1f)
                    };

                    normalUVs[0] = new Vector2(normalUVs[0].x + (texPosX[x, y, z] ), normalUVs[0].y + (texPosY[x, y, z] ));
                    normalUVs[1] = new Vector2(normalUVs[1].x + (texPosX[x, y, z] ), normalUVs[1].y + (texPosY[x, y, z] ));
                    normalUVs[2] = new Vector2(normalUVs[2].x + (texPosX[x, y, z] ), normalUVs[2].y + (texPosY[x, y, z] ));
                    normalUVs[3] = new Vector2(normalUVs[3].x + (texPosX[x, y, z] ), normalUVs[3].y + (texPosY[x, y, z] ));


                    Vector2[] flippedUVs = {
                        new Vector2(.1f, .1f), new Vector2(0, .1f), new Vector2(0, 0), new Vector2(.1f, 0)
                    };

                    flippedUVs[0] = new Vector2(flippedUVs[0].x + (texPosX[x, y, z] ), flippedUVs[0].y + (texPosY[x, y, z] ));
                    flippedUVs[1] = new Vector2(flippedUVs[1].x + (texPosX[x, y, z] ), flippedUVs[1].y + (texPosY[x, y, z] ));
                    flippedUVs[2] = new Vector2(flippedUVs[2].x + (texPosX[x, y, z] ), flippedUVs[2].y + (texPosY[x, y, z] ));
                    flippedUVs[3] = new Vector2(flippedUVs[3].x + (texPosX[x, y, z] ), flippedUVs[3].y + (texPosY[x, y, z] ));

                    Vector2[] heightUVs = {
                        new Vector2(0, 0), new Vector2(0, .1f), new Vector2(.1f, .1f), new Vector2(.1f, 0)
                    };

                    heightUVs[0] = new Vector2(heightUVs[0].x + (texPosX[x, y, z] ), heightUVs[0].y + (texPosY[x, y, z] ));
                    heightUVs[1] = new Vector2(heightUVs[1].x + (texPosX[x, y, z] ), heightUVs[1].y + (texPosY[x, y, z] ));
                    heightUVs[2] = new Vector2(heightUVs[2].x + (texPosX[x, y, z] ), heightUVs[2].y + (texPosY[x, y, z] ));
                    heightUVs[3] = new Vector2(heightUVs[3].x + (texPosX[x, y, z] ), heightUVs[3].y + (texPosY[x, y, z] ));

                    List<Vector3> visibleVerts = new List<Vector3>();     //Used as a temp array
                    List<int> visibleTris = new List<int>();     //Used as a temp array
                    List<Vector2> visibleUVs = new List<Vector2>();     //Used as a temp array

如何将正面添加到顶点、UV 和三角形数组的示例:

if ((z == 0 && exists[x, y, z] == true) || (z > 0 && exists[x, y, z - 1] == false)) {     //If there's not a block in front of this one or if it's on the edge of the chunk,

                        faces = new Vector3[] {
                        new Vector3 (0 + x, 0 + y, 0 + z),   
                        new Vector3 (0 + x, 1 + y, 0 + z),      
                        new Vector3 (1 + x, 1 + y, 0 + z),    
                        new Vector3 (1 + x, 0 + y, 0 + z)    
                        };

                        tris = new int[] {
                            0 + (faceCount * 4),     //0
                            1 + (faceCount * 4),     //1
                            2 + (faceCount * 4),     //2
                            0 + (faceCount * 4),     //0
                            2 + (faceCount * 4),     //2
                            3 + (faceCount * 4)     //3
                        };

                        visibleVerts.AddRange(faces);
                        visibleTris.AddRange(tris);
                        visibleUVs.AddRange(normalUVs);
                        faceCount++;
                    }

[更新]问题似乎出现在这里。如果我减少顶点生成方式的这些循环,则渲染问题会出现在网格的另一侧。

我很确定您的问题是渲染管线中对象的排序。看起来您的网格都具有相同的来源,管道使用该来源对它们进行排序。您最好使用原点位于立方体中心的网格,然后将它附加到的游戏对象移动到相应的世界位置。然后排序应该再次正常工作。

注意:据我所知,这只发生在两个渲染管道之一 (forward/deferred) 中,我很确定它在前向管道中,但不是 100%。因此,切换项目的渲染模式可能也会有所帮助,但不会真正解决网格起源的问题,只需修复效果即可。

正如@Ardor 所说,

Switching the render mode of your project would probably also help, but would nit really fix the problem of your meshes origins, just fix the effect.

我的渲染模式是TRANSPARENT。然后我将材质切换为渲染模式 OPAQUE。这解决了这个问题。不过,如果我想要透明 blocks/meshes,这个问题会再次提出。