unity3d 为什么程序栅格网格不能对大于256x256的栅格正确进行三角剖分?

8wtpewkr  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(162)

我一直在用菱形正方形算法制作程序地形高度图,用下面的三角测量方法制作网格:

public Map GenerateMap()
        {
            Mesh mapMesh = new();

            vertices = new Vector3[(Resolution + 1) * (Resolution + 1)];
            Vector2[] uv1 = new Vector2[vertices.Length];
            Vector2[] uv2 = new Vector2[vertices.Length];
            Vector2[] uv3 = new Vector2[vertices.Length];
            DiamondSquare diamondSquare = new(Resolution, Roughness, Seed, HeightLevels);
            float[,] heightFloatMap = diamondSquare.DoDiamondSquare();
            tex = new Texture2D(Resolution, Resolution);

            for (int y = 0, i = 0; y <= Resolution; y++)
            {
                for (int x = 0; x <= Resolution; x++, i++)
                {
                    //float height = heightMap.GetPixel(x,y).r;
                    float height = heightFloatMap[x, y];
                    vertices[i] = new Vector3(x * CellSize.x, height * CellSize.y, y * CellSize.z);
                    tex.SetPixel(x, y, new Color(height, height, height, 1));
                    if (height == 0)
                        uv1[i] = new Vector2(vertices[i].x, vertices[i].z);
                    else if (height < 0.4)
                        uv2[i] = new Vector2(vertices[i].x, vertices[i].z);
                    else if (height < 0.4)
                        uv3[i] = new Vector2(vertices[i].x, vertices[i].z);
                }
            }
            mapMesh.vertices = vertices;
            mapMesh.uv = uv1;
            mapMesh.uv2 = uv2;

            int[] triangles = new int[Resolution * Resolution * 6];
            Cell[,] cellMap = new Cell[Resolution / 4, Resolution / 4];
            for (int ti = 0, vi = 0, y = 0; y < Resolution; y++, vi++)
            {
                for (int x = 0; x < Resolution; x++, ti += 6, vi++)
                {
                    triangles[ti] = vi;
                    triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                    triangles[ti + 4] = triangles[ti + 1] = vi + Resolution + 1;
                    triangles[ti + 5] = vi + Resolution + 2;

                    Vector3[] cellVerts = new Vector3[]
                        {
                            vertices[vi], vertices[vi + 1], vertices[vi + Resolution + 1], vertices[vi + Resolution + 2]
                        };
                    Cell cell = new(new Vector2Int(x, y), cellVerts, CalculateCellGeometry(cellVerts));
                    cellMap[x / 4, y / 4] = cell;
                }
            }
            mapMesh.triangles = triangles;

            mapMesh.RecalculateNormals();
            mapMesh.RecalculateTangents();
            mapMesh.RecalculateBounds();

            Map map = new(mapMesh, cellMap, heightFloatMap, vertices);
            return map;
        }
    }

这适用于网格大小为16x16、32x32...... 256x256的情况,但当我在512x512或更高的情况下尝试时会出现问题
256x256像素
Mesh is perfect
512x512像素
It successfully triangulates up until the rows starting y=128
On the underside of the terrain there are these bars
我已经绘制了512x512和以上分辨率生成的顶点,他们都是好的,所以我99%肯定它的三角测量。
我是新的程序网格,我被这个问题难住了,任何帮助将不胜感激。

sczxawaw

sczxawaw1#

事实证明,这不是三角剖分,因为我的网格被设置为使用16位索引缓冲区,顶点限制正在达到。
我加了这一行

mapMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;

这个问题已经解决了。这是我的一个恼人的疏忽,但这是学习过程的一部分!

相关问题