- Home /
Mesh vertices array order
I want to make simple mesh deformer. What I'm doing is iterating vertex array of a mesh on which my script is added and modifying vertexes coordinate depending on specific condition. But, for performance issue, I want to know the order of vertex, which is how vertex is stored in vertices[] in what order.
My guess is that there is no order. Because, as I know, each vertex has index which is totally dependent on model maker. It means, if model maker just put vertexes on any order, vertices[] order is also almost random. Is my guess true?
If it is true.. I think it's not possible to optimize my deformer algorithm. Simply, I want to access vertexes in order of increasing y or decreasing y. (local y)
Answer by Thomas-Mountainborn · Jul 08, 2015 at 10:48 AM
You're right, vertex indices are not linked in any way to their position.
You could sort your vertices once, at the start of the script, and have an array that stores the indices of the vertices in a sorted manner. You can then always access the sorted vertices without having to sort them each frame.
Alternatively, depending on what it is exactly you're doing, you could consider writing a vertex shader to deform the object instead of deforming it by script.
Thanks for the answer. I have one more question then.
As I know, there is triangle information array(int[]) which saves vertex indexes for mesh triangles. Question is that if I sort vertex array, this triangle information will be messed up(maybe UV as well).
Is it O$$anonymous$$ to sort vertex array? If not, I think I have to make int array to save sorted index of vertex ins$$anonymous$$d of directly changing vertex array.
Unless you reconstruct the triangle index array and sort all the other arrays as well (normals, texcoords, colors, etc.), your vertex positions will no longer match up properly with the rest of the vertex information. This is fine if you don't care about the other information (i.e. the shader that you use with the mesh doesn't use per-vertex normals, texcoords, colors, etc.), but otherwise, you need to sort these with the vertices.
The simplest way to do this is to create a data structure that holds the vertex position, normal, texcoord, colors, etc. for each vertex, then sort a list of that data structure based off of the position. Then using the sorted list, you can reconstruct all of the arrays that the mesh needs.
You will still have to build the triangle index array. To do this, you will have to get the vertex position in the original vertex array using the old index from the triangle array. Then search for the vertex position in the sorted list, which will be the new triangle index.
Right, I didn't mean that you sort the vertices array and then write it back to the mesh, because that will indeed mess everything up. I meant that you should leave mesh.vertices as it is, and create your own separate int array that holds the indices of vertices in mesh.vertices in a sorted manner. You then go mesh.vertices[sortedVertices[i]].
Answer by Darkgaze · Jun 08, 2020 at 10:29 AM
Just FYI, if you are using Terrains instead of a Mesh, and you are getting the TerrainData vertex data. Vertices are stored first Zs and then X, not the other way around. So the index is X * resolutionZ + Z
In case it is important for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220611230312im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast on mesh deformed by shader 0 Answers
Simple mesh deformation : Mesh.vertices or vertex shader with displacement texture ? 1 Answer
Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers