- Home /
Is it faster to access an Vector3 array than to access a mesh's vertices?
I'm having some real slowdown in an app when it accesses a mesh's vertex positions. (It has to do this sometimes many times in one frame.) I want to try adding a separate array of Vector3's which will mimic the mesh.vertices array - and then use the separate array for reference, only accessing the mesh vertices when changing them.
Am I barking up the wrong tree here, or might this help my performance? I'm asking if there's any extra overhead in checking an entry in a mesh.vertices array compared with checking an identical array which is separate from the mesh.
Answer by robinking · Jun 02, 2011 at 04:30 PM
I've checked and using a stored separate array gives the much needed speed boost. For comparison I also tried Eric's method above, but because of the structure of my code, the function I call several times per frame copies mesh.vertices into a new Vector3 array and accesses that instead - so I still got the very bad slowdown I need to avoid.
Having a stored identical array (i.e. declaring it at top of file), and accessing that instead, was ~180X faster! Obviously it can only be used when referencing the vertex positions, not changing them, but that's all I need for my current purposes.
Answer by Eric5h5 · Jun 02, 2011 at 04:10 PM
Yes, you should do
var vertices = GetComponent.<MeshFilter>().mesh.vertices;
// do stuff with vertices
GetComponent.<MeshFilter>().mesh.vertices = vertices;
Thanks Eric. However, the function I have which I tried that in is accessed many times per frame, so doing it strictly like you state means copying the mesh.vertices array many times per second, so still resulted in the slowdown. For clarity, I've put a separate answer as the key to it is permanently storing the copied array. I still gave you a thumbs up though!
Sorry for not being more explicit; I meant that you should do the GetComponent once, and use that array from then on, only assigning it back when needed. It wasn't intended that it should all be in one function, though I didn't make that clear.
Ah - good to have outside confirmation! Thanks again :)
Your answer
Follow this Question
Related Questions
Re-meshing Static Objects as one mesh? 1 Answer
new Mesh() works outside of array, but not in array. Huh? 3 Answers
Does using the same Mesh variable for multiple Mesh Filters take up more memory? 1 Answer
Would combining meshes of 2D hexagons help my performace? 0 Answers
Best practices for a giant moving environement/enemy 0 Answers