Generate a plane and apply an heightmap at runtime?

So I know this is a loaded question, but is it possible to create a plane and apply a randomly generated heightmap at runtime to make terrain? I’ve done a lot of searching and nothing really helps. Also, would it be possible to make the plane triangulated and low poly like in this video?

Generate a plane:

yes you can generate any kind of mesh at runtime. Take a look at the Mesh Class

You generate a mesh by:

  • creating array of Vector3 representing positions of vertices. Assign it to the Mesh.vertices

  • then create an array of integers that will represent your faces. In graphics all the faces are triangles (even though some 3D editors couple them into quads). The int array must contain the indexes of vertices. 0,1 and 2 in that array will be a face connecting the corresponding vertices. 3,4 and 5 will be the second face, etc. So the length of the array should be a multiple of 3. Also note that the vertices should be ordered clockwise to correctly generate the face normal. E.g: if 0-1-2 face has its normal pointing up than 0-2-1 face will have a downward normal. This is necessary to correctly let shaders reflect light and other stuff.

  • After you’re done call mesh.RecalculateBounds, otherwise the camera will occlude it incorrectly (objects outside the view spectrum are not rendered. camera decides if the object is in view spectrum by using the mesh bounds). This command sets the bounding box of the object. Bounding box is the smallest box in which the object fits in (calculated in local space).


Apply heightmap at runtime:

Take a shader with heightmap and assign to your mesh.