- Home /
Does mesh.triangles also return a copy of the triangle array?
So mesh.vertices returns a copy of the vertex array, so if you are accessing it more than a couple of times you should really store it in a local array to avoid excessive garbage being created. So if I have some code like this:
for(int i = 0; i < mesh.triangles.length; i+=3){
Vector3 v0Vec = mesh.vertices[mesh.triangles[i]];
Vector3 v1Vec = mesh.vertices[mesh.triangles[i + 1]];
Vector3 v2Vec = mesh.vertices[mesh.triangles[i + 2]];
...
}
Then I should really convert it to something like this:
Vector3[] verts = mesh.vertices;
for(int i = 0; i < mesh.triangles.length; i+=3){
Vector3 v0Vec = verts[mesh.triangles[i]];
Vector3 v1Vec = verts[mesh.triangles[i + 1]];
Vector3 v2Vec = verts[mesh.triangles[i + 2]];
...
}
But does mesh.triangles do the same thing? Should my code really be more like this?:
Vector3[] verts = mesh.vertices;
int[] tris = mesh.triangles;
for(int i = 0; i < tris.length; i+=3){
Vector3 v0Vec = verts[tris[i]];
Vector3 v1Vec = verts[tris[i + 1]];
Vector3 v2Vec = verts[tris[i + 2]];
...
}
Thanks for your help.
Answer by Bunny83 · Jun 19, 2019 at 07:55 PM
Yes, it does return a copy as well. Keep in mind that the Untiy engine core is written in native C++. All the built-in component data is stored on the native side. You can't get direct access to native code memory within a managed environment without using unsafe code. Since Unity uses multiple threads in it's core it can not ensure thread safety if they actually provide you direct access to internal memory structures.
If you need to do this more often you may want to use a cached List and use GetIncides instead. The same is true for GetVertices. If you just clear the lists and not recreating them all the time there will be no memory allocated for the subsequental calls.
I'm confused by the usage of GetVertices. So I make List vertList = new List(mesh.vertexCount); mesh.GetVertices(vertList); Then I have all of the vertices in a list? Do I need to reassign the vertex list to mesh.vertices when I'm done with it?
If you want to set the vertices back you would use SetVertices. Note that the usage would be more like this:
private List<Vector3> vertices = new List<Vector3>();
void Some$$anonymous$$ethod()
{
vertices.Clear();
mesh.GetVertices(vertices);
// [ ... ]
mesh.SetVertices(vertices);
}
Of course the capacity of the list would still occupy the memory, but that's the point when reusing the list is to avoid constant creation and collection of memory. GetVertices will automatically resize the List if necessary. Though resizing is only required the first time it's used or when you add more vertices at the [...]
Your answer
Follow this Question
Related Questions
How to find connected mesh triangles? 2 Answers
Null Reference Exception Assigning Vertices to Mesh 0 Answers
Mesh triangles and vertices, searching for adjacent triangle 1 Answer
Lighting up all triangles between two points in a mesh? 1 Answer
How to return from shader the indexes of triangles/vertices that was actually rendered? 1 Answer