I’m using Marching Squares to create a dynamic mesh that is (potentially) updated every frame. For assigning the Mesh Verts and Tris I’ve tried a few different routes and ran into various problems. The issue stems from the fact that the number of verts and tris will change as the Marching Squares data changes.
Method A: create generic Lists that hold the vert and tri info and then assign them with mesh.vertices = vertices.ToArray();
obviously this is no good since ToArray() will generate garbage;
Method B: create arrays large enough to hold the max number of tris and verts. This way I can just do
mesh.vertices = vertices;
mesh.triangles = triangles;
the problem here is that my mesh will potentially have a ton more verts and tris than it actually needs which is not ideal
Method C: I also tried using Method B and making a copy of only the tri/vert indexes that are actually populated
copiedTriangles = new int[triCount];
for (int i = 0; i < triCount; i++)
{
copiedTriangles[i] = triangles[i];
}
mesh.triangles = copiedTriangles;
but now I get allocations again since I’m creating a new array every time.
Is there a good solution to this problem or will I just have to live with having a bunch of wasted space in all of my meshes?