speed up mesh creation/modification

Hi all,

Can anyone give me any tips oin how to speed up mesh generation/modification?

Ive split my creation/modification up into 2 main functions
A- generate and fil the appropriate arrays (vertetxes,triangles,normals)
B- update the mesh object with these arrays

Im keeping a handle to the mesh object and trying to do as little as i can in the update.
However is still really slow in comparison with my actual functions to fill the arrays.
Is there anythig i can do to speed up the process? I dont really want lags every frame the mesh is altered.

Oh and btw, the number of triangles can change from frame to frame.

Thanks

                m_pMesh.Clear();
		m_pMesh.vertices = m_Vertices;
		m_pMesh.triangles = m_TriangleIndexes;
		m_pMesh.normals = m_Normals;
		
		//make collider
		MeshCollider pMeshCollider = gameObject.GetComponent<MeshCollider>();
		pMeshCollider.sharedMesh =null;
		pMeshCollider.sharedMesh =m_pMesh;
  1. Don’t clear the mesh
  2. Don’t use mesh colliders if you update it all the time, they take a massive amount of time when they update. also depending on what you want to do it might be totally useless as the mesh must not deform in and out or collisions will no longer be detected as other colliders never penetrate, they are just in or out.

It will speed things up a bit if you only assign the triangles on frames when they change. Also, are you getting the previous mesh values, modifying them and then writing them back? If so, use your own arrays to store the mesh data between frames. If you do things like

myVerts = mesh.vertices;

…then it will pull in the data from graphics memory at a considerable time penalty. Maybe you can post the whole of your mesh generation code in case there are any other possible optimisations?

Hi guys,

thanks for the tips,

I dont think its worth posting my whole generation code as its actually split across number of functions…
However I should say that the arrays are all my own, created on start and then maintained, refilled without any reference to the mesh object.
So i dont pull them off and modify them, i reassign from my own array each time i rebuild (and i dont remake the array every time, i just refill it).

Because the number of triangles each time can be different i keep a count of how many there are in each change and resize the arrays

//resize arrays to correct length of vertices that we have produced
System.Array.Resize(ref m_Vertices,m_TotalVertexes);
System.Array.Resize(ref m_TriangleIndexes,m_TotalVertexes);
System.Array.Resize(ref m_Normals,m_TotalVertexes);

I was assuming this would be faster than having to produce my triangles, then make a new array of correct length and then copy into that and finally assign to the mesh

  1. Don’t clear the mesh
    Oh right, The docs seemed to say i should, especially if the number of trianlges is changing

  2. Don’t use mesh colliders if you update it all the time, they take a massive amount of time when they update. also depending on what you want to do it might be totally useless as the mesh must not deform in and out or collisions will no longer be detected as other colliders never penetrate, they are just in or ou

Yeah i know colliders are slow, but since the mesh is terrain it kinda needs to be up to date in terms of colliders. Actually I disabled the collider reassign and it didnt actually make that much difference :frowning: