Mesh generation out of array

I have an array with 65x65 points that looks like this:

Index (00, 0): 139
Index (00, 1): 150
Index (00, 2): 120
Index (00, 3): 80
Index (00, 4): 130
Index (00, 5): 133
...
Index (64,64): 153

I want to use this array to generate a quad-like mesh, with 65 different heights (it’s going to represent terreinheights).

I’m fairly new to this side of Unity, and am not sure how to proceed. As far as I found right now I could do this by translating each point to be a vertice, looking something like this:

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

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

for (int i = 0; i < 64; i++){
vertices[i] = arrayValues[i];
}

mesh.vertices = vertices;

Unity documentation also declares the triangles, but I have a very hard time understanding how I know which triangle should be assigned what.

Any directions or advice? Thanks!

Well, first of all, you don’t need 65 vertices, but 65x65. And then the loop should run 65 times, not 64. Each triangle simply consists of three integers, which are the indices of the vertices used in the triangle.

int[][] arrayValues = new int[65][65];
Vector3[] vertices = new Vector3[65 * 65];
int index = 0;
for (int x = 0;x < 65;x++)
{
  for (int z = 0;z < 65;z++)
  {
     vertices[index++] = new Vector3(x, arrayValues[x][z], z); // Some scaling might be needed
  }
}
int[] triangles = new int[64 * 64 * 2 * 3]; // 64x64 quads, 2 triangles per quad, 3 vertices per triangle
index = 0;
for (int x = 0;x < 64;x++)
{
  for (int z = 0;z < 64;z++)
  {
    // Triangle 1 of quad
    triangles[index++] = x + z * 65;
    triangles[index++] = x + 1 + z * 65; // Swap with line below to change triangle front face
    triangles[index++] = x + 1 + (z + 1) * 65;
    // Triangle 2 of quad
    triangles[index++] = x + z * 65;
    triangles[index++] = x + 1 + (z + 1) * 65; // Swap with line below to change triangle front face
    triangles[index++] = x + (z + 1) * 65;
  }
}
mesh.vertices = vertices;
mesh.triangles = triangles;

Thanks @jvo3dc . You’re correct on the 65 points part, didn’t proofread my logic here, heh.
Also, thanks a lot for the code example. It provides me a good starting point, especially for the triangle part :)!

One more thing… May I assume you meant new int[65,65] on the first line?

Yes, that’s fine. Old java habits :wink:

No worries, thanks to you I got everything working correctly now! :slight_smile:

Hi @jvo3dc , sorry to bother you again, but I’m hoping you can help me out here seeing your knowledge of mesh generation.

Using your code I managed to generate a tile that has heightvalues from my terrain tiles integrated, but I’m stuck on a new issue here. So, what I’m doing is this:

  1. Grab the Quad Primitive meshfilter of the tile.
  2. Create a new mesh with your code.
  3. Replace the mesh of the quad with the new mesh (this way the material stays on).

This results in the new mesh being visualized with the correct height values (but too big and on a different position). However, the material acts very weirdly. It looks like this: Imgur: The magic of the Internet . The original tiles look like this: Imgur: The magic of the Internet

This is the relevant code: string[] arrayValuesString = new string[65 * 65]; int[,] arrayValues = new - Pastebin.com
This is an example of the material being placed on the tile: http://134.221.20.240:3456/HR_LUFO/17/67533/43620.png

How could I warp the material with the changed mesh?

Thank you!

There is no problem with the material, but you should set the uv for each vertex.

Vector3[] vertices = new Vector3[65 * 65];
Vector2[] uv = new Vector2[65 * 65];
int index = 0;
for (int x = 0;x < 65;x++)
{
  for (int z = 0;z < 65;z++)
  {
    vertices[index] = new Vector3(x, arrayValues[x][z], z); // Some scaling might be needed
    uv[index++] = new Vector2(x / 64.0f, z / 64.0f);
  }
}
// Generate triangles in between like before
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
1 Like

Whoah, I was thinking way too difficult. Thanks a lot!