# Most efficient mesh generation out of an array like this

I have an array that contains 65x65 points, which I want to transform into a mesh. It looks like this:

``````Index (00, 0): 139
Index (00, 1): 159
Index (00, 2): 119
Index (00, 3): 129
Index (00, 4): 130
......
Index (64, 64): 139
``````

00,0 is the topleft corner, 64,64 is the bottomright corner. So, it has to be a quad-like objects, with 65 points of height terraforming the quad.

What would be an efficient way to tackle this?
Thanks.

Edit:
I’m currently thinking something like this, but am not sure how to proceed, especially with declaring the triangles.

``````GameObject tile = GameObject.Find("Tile");
MeshFilter mf = tile.GetComponent<MeshFilter>();
mesh = new Mesh();
mf.mesh = mesh;

int[] arrayValues = new int;
Vector3[] vertices = new Vector3;

for (int i = 0; i < 4225; i++){
vertices _= arrayValues*;*_
``````

}

mesh.vertices = vertices;

Well, if you have heightmap samples you could simply use Unity’s Terrain engine. However it expects the height values to be float values in the range 0 - 1

Though if you want to create a mesh you just do something like this:

``````Vector3[] vertices = new Vector3[65*65];
for (int i = 0; i < vertices.Length; i++)
{
vertices _= new Vector3(i %65, arrayValues*, i / 65);*_
``````

}
mesh.vertices = vertices;
Note the `i%65` and `i/65` are basically the x and z values of your grid. You may want to scale your arrayValues as “139” would be huge in usual world units.
Once you have created and assigned your vertices you have to actually define the triangles between the vertices by creating a triangle list:
int[] triangles = new int; // note we only have 6464 quads between 6565 vertices
int n = 0;
for(int y = 0; y < 64; y++)
{
int yOff = y * 65;
for(int x = 0; x <64; x++)
{
triangles[n++] = yOff + x;
triangles[n++] = yOff + x + 65;
triangles[n++] = yOff + x + 65 + 1;

triangles[n++] = yOff + x;
triangles[n++] = yOff + x + 65 + 1;
triangles[n++] = yOff + x + 1;
}
}
mesh.triangles = triangles;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
Note that the resulting mesh won’t have any UV texture coordinates. If you want to texture map the mesh you have to create UV coordinates as well
Vector2[] uvs = new Vector2[vertices.Length];

for(int i = 0; i < uvs.Length; i++)
{
uvs = new Vector2(i%65, i/65) / 64f;
}
mesh.uv = uvs;