Procedural mesh performance questions

What is the most performant way to modify mesh information between the two methods I present below? (I’m using Unity iPhone, so every tiny bit can make a big difference.)

First, I’m creating a procedural mesh that has different parts that move independently constantly. It’s basically a way to implement 2D sprites while only using a single draw call. So each part of the mesh is “tied” (sort of) to a separate GameObject, so ideally, I’d like to have each GameObject modify its own part of the mesh as it needs to. So, given that, should I:

A) Duplicate all mesh info and keep a locally copy in a manager class, allowing client objects to modify the portion of the mesh they “own”, then just re-assign the vertex buffer to the mesh object each time there’s a change. Sort of like so:

Vector3[] vertsCopy; // Copy of vertex data to be kept locally
...
mesh.vertices = vertsCopy; // Done each frame, or when there's a change

or

B) Do as the documentation suggests and retrieve the vertex buffer from the mesh object first, then expose that array to the client object to modify, then assign it back once the client object is done making changes. Like so:

Vector3[] tempVerts = mesh.vertices;
... // pass tempVerts to a client object for changes
mesh.vertices = tempVerts;

And in approach A, does Clear() need to be called? Or is mesh.vertices actually just a reference to the original buffer, meaning it doesn’t even need to be re-assigned?

The first approach should be much faster. Does not need to read the array back each time.

The mesh arrays are by-value, i.e. they are not references into the actual mesh data.

By value, eh? In that case, it would still be copying all the data each frame in the first case. Is it adviseable (or even possible) to simply modify the values in the array attached to the mesh object itself (values like vertex positions, UV values, etc, but not changing the array dimensions)? Or is my first method pretty much the best that can be safely done?

Yes, the first case there is still a copy each frame (or whenever there is a change). In the second case, there is two copies (I’m assuming the code you wrote would be done each frame).

Right now it’s not possible to access mesh data by-reference.

Thanks so much for your help! I’ve had good results on the iPhone using particles (small though, to keep fillrate consumption down), so I was wondering, what is the method used in updating vertex data in Unity’s particle system? I figure that is going to be pretty similar to what I’m trying to accomplish with my SpriteManager.

You just access each particle and position it?
Unity particle system is pretty much unique there, I don’t know of any engine that grants you particle access actually.

This makes the unity particle system is, at least out of my sight, a great batched quad renderer with superb “automatisms” that make it behave like a particle system :slight_smile:

What I mean is, internally speaking, does the Unity particle system keep a local copy of vertices, does it extract the mesh data each frame, or something completely different (like does it have privileged direct access to the mesh)? Because whatever it is doing works well.