I'm generating some simple procedural trails for my game, and performance is an issue when many trails are on screen at once. I was wondering what is the fastest way to generate and update geometry, which will be changing per-frame? The positions and topology may both change.
The Unity docs and pro-geo examples follow this pattern:
function Update ()
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3 = mesh.vertices;
var normals : Vector3 = mesh.normals;
for (var i = 0; i < vertices.Length; i++)
vertices[i] += normals[i] * Mathf.Sin(Time.time);
mesh.vertices = vertices;
Is this the most efficient way of updating things? For example, is it actually making a copy of the arrays at the beginning and the end, or is it just a pointer? If it's just a pointer, why is the last assignment needed?
Also, the number of triangles in my mesh changes per frame, and I have two options for this: 1) Clear the mesh and allocate new arrays each time depending on number of triangles - this seems very inefficient. 2) Allocate just once for the maximum number of possible tris (I know this), and make the ones I'm not using degenerate - but "degen hiding" triangles seem very inefficient..is there an easy way to tell the Mesh class to only read n-many triangles even though the buffer is larger?
Answer by Eric5h5
May 13, 2012 at 08:43 PM
It's making a copy, it's not a pointer. The most efficient way is not to copy vertices/triangles from the mesh to local arrays, but just keep the arrays (as global variables) and upload them to the mesh after making changes. For changing mesh sizes, you can just make unused vertices degenerate rather than making new arrays. It's not particularly inefficient, since the graphics card skips over them anyway, though they do still have to be uploaded.
OK awesome. I just tried this (keeping my own buffer) and gained about 20 fps! Killleeerrr thanks man.
I think it's pretty good now, and I should move on to other stuff..but definitely dealing with the extra GPU upload would be the next thing to try..since my # of tris don't actually change that often, and they're usually not at the max, so this would save a lot.
Unity has no way of specifying a subset of triangles, so the only thing you can do is make a new array. If the number doesn't change often, then maybe the overhead for making new arrays (and clearing the mesh) is less than constantly uploading more than you need.
how do you 'upload them to the mesh' ? you mean copy them directly into the array returned by mesh.vertices with something like Array.Copy() ?
"Upload them to the mesh" = "`mesh.vertices = vertices`".
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
6 People are following this question.
make normal maps during runtime?
Generating terrain radially
I keep getting this error!
Collision mesh won't form until mesh is moved?
How to get a specific map with procedural generation?