I’m looking for a way to convert a heightmap I have to a mesh, or more specifically, a plane with the heightmap deforming it.
Is there any easy way to do this? (terrains are not currently an option)
I’m looking for a way to convert a heightmap I have to a mesh, or more specifically, a plane with the heightmap deforming it.
Is there any easy way to do this? (terrains are not currently an option)
You would have to split up your heightmap into chunks as the maximum size of a mesh is 65000 verts. It is fairly straightforward to do it though with GetPixel and assigning the Y value of the vertex to the grayscale value. Here’s an example of one of the chunks:
Texture2D hMap = Resources.Load("Heightmap") as Texture2D;
List<Vector3> verts = new List<Vector3>();
List<int> tris = new List<int>();
//Bottom left section of the map, other sections are similar
for(int i = 0; i < 250; i++)
{
for(int j = 0; j < 250; j++)
{
//Add each new vertex in the plane
verts.Add(new Vector3(i, hMap.GetPixel(i,j).grayscale * 100, j));
//Skip if a new square on the plane hasn't been formed
if (i == 0 || j == 0) continue;
//Adds the index of the three vertices in order to make up each of the two tris
tris.Add(250 * i + j); //Top right
tris.Add(250 * i + j - 1); //Bottom right
tris.Add(250 * (i - 1) + j - 1); //Bottom left - First triangle
tris.Add(250 * (i - 1) + j - 1); //Bottom left
tris.Add(250 * (i - 1) + j); //Top left
tris.Add(250 * i + j); //Top right - Second triangle
}
}
Vector2[] uvs = new Vector2[verts.Count];
for (var i = 0; i < uvs.Length; i++) //Give UV coords X,Z world coords
uvs <em>= new Vector2(verts_.x, verts*.z);*_</em>
GameObject plane = new GameObject(“ProcPlane”); //Create GO and add necessary components
plane.AddComponent();
plane.AddComponent();
Mesh procMesh = new Mesh();
procMesh.vertices = verts.ToArray(); //Assign verts, uvs, and tris to the mesh
procMesh.uv = uvs;
procMesh.triangles = tris.ToArray();
procMesh.RecalculateNormals(); //Determines which way the triangles are facing
plane.GetComponent().mesh = procMesh; //Assign Mesh object to MeshFilter