Is there a way to assign mesh.vertices[i] directly

I actually simulate a deformable surface (using mass-spring model as this used to simulate clothes for instance) and draw the mesh every frame.

To draw it i follow what’s explain on this link (Unity - Scripting API: Mesh) at section ‘3. Continously changing the mesh triangles and vertices’.

However, the framerate decrease a lot, and when profiling I aware it was the garbage collector (gc.collect() call) in my drawing method which takes the most of the time by frame.

Is there a way to directly modify mesh.vertices = new Vector3(…) for instance instead to pass an array which is going to be copy in the mesh and so garbage collected after ?
Thanks a lot

– are you asking, can you modify JUST ONE of the verts? no, you can only write the whole .vertices at the same time

I don’t understand the graphics pipeline, but I believe the reason is possibly because it writes the whole thing to the graphics chips at once

– yes you should certainly keep your own array (“yourArray”)

and modify it as you wish.

then simply write that same array to the vertices (.vertices = yourArray)

– never GET the vertices like this … some other array of yours == mmm.vertices

never do that. it makes a copy each time.

(OK you could do it once when you launch, but generally don’t it in play.)

it’s a bit of a gotchya in Unity, it only mentions it subtly in the doco. But you have to be super-aware of the few functions that do make a copy each time. It only took me like 5 years to figure that out :slight_smile: