首页 > 解决方案 > 网格具有重叠的面/三角形

问题描述

块面渲染错误

放一些内容,我开发了一个块生成系统,可以随机生成块到 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++;
                    }

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

标签: unity3dgraphics3drendering

解决方案


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

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


推荐阅读