# Procedural mesh speed questions

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 <em>+= normals _* Mathf.Sin(Time.time);_</em>
``````

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?
Cheers…

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.