I
I
i__egor2021-09-08 07:23:32
Unity
i__egor, 2021-09-08 07:23:32

Why does unity mesh generation work so weird?

I'm trying to generate a plane with a uniform grid. For example 1024x1024 squares. But the code below generates normally only 256x and less, after 256 I tried 512, etc. For some reason, the generation breaks and the grid is generated incorrectly with stripes? maybe there are limitations (the number of vertices is about a million, it's not a lot after all)? Maybe there are other settings that I don't know about? tried another code that does the same but in a different way, all the same problem

vertices = new Vector3[(xSize + 1) * (ySize + 1)];
            Vector2[] uvs = new Vector2[vertices.Length];
            Vector4[] tangents = new Vector4[vertices.Length];
            Vector4 tangent = new Vector4(1f, 0f, 0f, -1f);
            for (int i = 0, y = 0; y <= ySize; y++)
            {
                for (int x = 0; x <= xSize; x++, i++)
                {
                    vertices[i] = new Vector3(x, y);
                    uvs[i] = new Vector2((float)x / xSize, (float)y / ySize);
                    tangents[i] = tangent;
                }
            }          

            mesh.vertices = vertices;
            mesh.uv = uvs;
            mesh.tangents = tangents;

            int[] triangles = new int[xSize * ySize * 6];
            int ti = 0, vi = 0;
            for (int y = 0; y < ySize; y++, vi++)
            {
                for (int x = 0; x < xSize; x++, ti += 6, vi++)
                {
                    triangles[ti] = vi;
                    triangles[ti + 1] = triangles[ti + 4] = vi + xSize + 1;
                    triangles[ti + 2] = triangles[ti + 3] = vi + 1;
                    triangles[ti + 5] = vi + xSize + 2;
                }
            }
            
            mesh.triangles = triangles;      
            mesh.RecalculateNormals();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Sokolov, 2021-09-08
@i__egor

And where does such confidence come from that a million vertices is not a lot? This is a criminal lot and there is no situation where a solid mesh with such a mesh density would be needed. But if you still want to, the created mesh needs to switch this parameter:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question