【问题标题】:Mesh has overlapping faces/triangles网格具有重叠的面/三角形
【发布时间】:2021-10-18 03:20:06
【问题描述】:

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

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

【问题讨论】:

  • 我会尝试缩小问题范围,似乎是渲染问题,与网格生成或元素位置无关。在这种情况下,所有发布的代码都不相关。我会检查问题是否重现了根据 cemera 的 z 渲染的材料,例如统一的默认材料具有不同的颜色,以便您可以区分立方体。如果在这种情况下没有出现问题,您需要检查您的材质/着色器及其与混合有关的行为

标签: unity3d graphics 3d rendering


【解决方案1】:

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

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

【讨论】:

  • 不过都是同一个网格!正如你所说,我确实发现它似乎是从顶点如何生成的 x、y、z 循环中发生的。如果我将 for 循环翻转为递减,则渲染问题会发生在网格的另一侧。我更新了帖子。无论如何,我几乎可以让它“重新计算”吗?谢谢。
  • 根据您的观察,我假设网格中的三角形根本没有排序,而是按照您创建它们的顺序呈现。如果是这种情况,您可以在相机移动时重新计算它(这听起来非常糟糕)。您还可以将每个立方体放入一个子网格中,这也应该解决您的排序问题。 (但我不确定一个网格中的子网格数量是否有限制)这个答案的一些解决方案也可能对您有所帮助:answers.unity.com/questions/609021/…
【解决方案2】:

正如@Ardor 所说,

切换项目的渲染模式可能也会有所帮助,但不会真正解决网格起源的问题,只需修复效果即可。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多