Height maps and procedurally generated terrain topology

This was one of the most instructive exercises I had to do back in 2012. After learning theory about the 3D graphics pipeline and how data was structured in memory, we were asked to read a height map and create a textured terrain out of it.

Some configuration was needed like min and max height of the terrain, in order to interpret full black or full white accordingly, and also the separation between vertices. In this case the size of the terrain was defined by the resolution of the height map and the separation between vertices parameter.

I created a vertex for each texel in the heightmap, calculated it's UV coordinates, and later assembled the triangles that formed the mesh, accounting for winding order because of face culling.

This task was just awesome, being able to operate with vertex and index buffers directly was like a new superpower to me, and it was some really useful knowledge in the years to come.

There are so many improvements that come to mind now that I look at it, like setting a terrain width and height in world units and calculating the vertex separation in the Z and X axis separately. Maybe adding the option to increase the terrain resolution and adding intermediate vertices for each texel in the height map. Or maybe just creating the terrain first, setting the UVs, and then just sample the height map in those position using bilinear filtering, to set each vertex height. So many options, but this was a true achievement for me at that point.

Some scenarios where I used this knowledge:

  • Built a procedurally generated slot machine. Each slot column was a cylinder with configurable number of faces, and face size. The images in each face were extracted from a NGUI sprite sheet, I was able to extract the coordinates from each sprite and use them to set the UV coordinates in each vertex for each face. Now that I say this, I remember I had to duplicate the vertices in each corner for each image, even if they were in the same position, so that I could have separate UV coords in sequential slot faces. This was REALLY awesome and powerful. Good times.
  • Made a scrape to win feature, where I would edit vertex alpha in order to add transparency in the covering texture.
  • Reveal hidden door when shot with laser. Inverse of the previous feature lol!

Comments

Popular posts from this blog

Semantic versioned Unity package template

My beginnings!

Hogan's Alley VR adaptation