Read Vertices in a specific order?
I need to read the vertices of a mesh in order from left to right, then backwards to forwards, but the order the vertices are read in are incredibly awkward. They seem to read clockwise around the edges then spiraling in towards the center. Sorry if it's hard to understand what i'm asking for, i've included a picture of the results i'm currently getting so hopefully that can help. My current code is listed below.
//There is a "public int intensity" at the header to adjust the intensity (obviously) of the generation
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int i = 0;
int step = 0;
float lastY = 0;
while (i < vertices.Length)
{
vertices[i] += new Vector3(vertices[i].x, Random.Range(lastY + -1,lastY + 1) * intensity, vertices[i].z);
lastY = vertices[i].y;
i++;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
transform.gameObject.AddComponent<MeshCollider>();
Answer by JonHUnity · Oct 25, 2016 at 12:38 PM
The order of the vertices in a Mesh are determined by the order they were assigned when created. Unless explicitly documented by your importer or by the code which generated your mesh, there are no guarantees about the order of the vertices in the array.
Your picture looks like your mesh is a procedurally generated flat plane. To accomplish this with a specific order to the vertices, I recommend generating the mesh yourself. For example, to create a grid-based mesh, you can do something like the following:
public class CreateMesh : MonoBehaviour {
const int width = 100;
const int height = 100;
// Generate the mesh with a well-known vertex order
void Start () {
Mesh mesh = gameObject.AddComponent<MeshFilter>().mesh;
gameObject.AddComponent<MeshRenderer>();
mesh.vertices = GenerateVertices(width, height);
mesh.triangles = GenerateTriangles(width, height);
}
private int[] GenerateTriangles(int width, int height)
{
//generate two triangles per vertex except the last column and last row
int[] triangles = new int[(width - 1) * (height - 1) * 6];
for (int y = 0; y < height - 1; y++ )
{
for (int x = 0; x < width - 1; x++ )
{
triangles[(y * (width - 1) + x) * 6 ] = y * width + x;
triangles[(y * (width - 1) + x) * 6 + 1] = y * width + x + 1;
triangles[(y * (width - 1) + x) * 6 + 2] = y * width + x + 1 + width;
triangles[(y * (width - 1) + x) * 6 + 3] = y * width + x;
triangles[(y * (width - 1) + x) * 6 + 4] = y * width + x + 1 + width;
triangles[(y * (width - 1) + x) * 6 + 5] = y * width + x + width;
}
}
return triangles;
}
Vector3[] GenerateVertices(int width, int height)
{
Vector3[] vertices = new Vector3[width * height];
for (int y = 0; y < height; y++ )
{
for (int x = 0; x < width; x++ )
{
vertices[y * width + x] = new Vector3(x / (float)width, y / (float) height);
}
}
return vertices;
}
void Update () {
Mesh mesh = this.gameObject.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
SetVertex(vertices, 10, 12, .55f);//set the z component of the vertex at x=10, y=12 to .55
mesh.vertices = vertices;
}
private void SetVertex(Vector3[] verts, int x, int y, float z)
{
verts[width * y + x].z = z;
}
}
Got it working near perfect, just a couple of questions. There seems to be a max vertices of 65000, is there anyway to get around that? (I am going to be optimizing the mesh after the terrain generation, I just need it for higher resolution heightmaps) Also, there seems to be a weird spike at a specific vertex and i'm not sure what happened in the code that is causing it (see picture below). Lastly, is there any way to make the plain bigger, currently it makes a very small 1x1 plane, but I was hoping for something more like 256x256 or higher.
Thanks!
There is a limit of 65,534 vertices. If you want more, you will have to create multiple meshes and line them up so there is no gap. This will involve moving this script to another gameobject that would ins$$anonymous$$d create more gameobjects, one for each mesh.
As for the size, you can either set the scale on the transform or multiply the x,y coordinates of the Vector3s in GenerateVertices by the size you want.
The spike you see is just my example call to SetVertex(vertices, 10, 12, .55f); in Update(). You can remove it.
Your answer
Follow this Question
Related Questions
Mesh generation triangles issue 0 Answers
Edit SkinnedMesh with vertices 0 Answers
Removing Tris/Verts/Polygons from an merged mesh? (C#) 0 Answers
Shared or Non-shared Vertices for mesh generation? 0 Answers
Collisions = Get colliding vertices 0 Answers