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)

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.

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.