Is it normal to send an array of vertices that are not completely filled to the mesh?

I am trying to generate a mesh on the fly and I do not know how many vertices may be needed. Now I use a List for this, but I’m thinking of rewriting it into arrays. It should be faster.

This code works as expected but I don’t know if he has any disadvantages.

var v = new Vector3[64000];
v[0] = new Vector3(1, 1, 0);
v[1] = new Vector3(1, 0, 0);
v[2] = new Vector3(0, 0, 0);

var t = new int[64002];
t[0] = 0;
t[1] = 1;
t[2] = 2;

GetComponent<MeshFilter>().mesh = new Mesh
{
    vertices = v,
    triangles = t
};

Stay with List and use SetVertices(), SetNormals() etc. methods from the Mesh class.
They will not allocate garbage whenever you change the mesh, which has far more impact than the marginal performance increase you will get from using an Array.


As for having parts of the Mesh ‘undefined’:

  1. Initialize the list with the max capacity you will require at some point. This will get rid of any further memory allocations right from the beginning.
  2. Fill the complete list with dummy values (e.g. all vertices at Vector3.zero, TriangleIndices just with 0, …)
  3. Change the List content at any time and pass it to the appropriate Mesh.SetXXX() method.
  4. Profit :smiley:

This is as fast and memory friendly as you will get without using jobs/multithreading.