Procedural Terrain Generation (Parts)

This reminds me of trying to combine voxelized terrain into convex surfaces to be made into a navigation mesh, but in 3D. To summarize it, finding the optimal way to combine parts to minimize the number of parts used could be reduced to a NP-hard optimization problem. The best method is probably to use an approximate solution in linear time, like mentioned above.

I’ll start by assuming that the map generated is a height map with special labels for the type of cell. For every cell at random, perform a check to see if there are any cells adjacent (so perform a randomized stencil operation) that satisfy the following properties:

  1. They are relatively the same height (+/- a tolerance)
  2. They are the same label/type of cell
  3. Are not a part of a previously made group (to be explained later)

If so, group them. Continue in that direction, checking if the next cell can be merged into the group. if it reaches a cell of another group, stop. The next action will guarentee that any encountered group cannot be merged. Next, we will try to expand this group any of the other three possible directions. When expanding orthogonal to a direction already expanded in, a row of cells will need to be checked for the above properties instead of a single cell at a time. If so, the row is added to the group and a plane of grouped parts is formed. Repeat this until every cell belongs to a group.

This will perform a linear amount of checks up to a maximum of 4 * the number of cells. To obtain a slightly better map you could attempt to resize groups. You could also decide to allow smaller cells to form groups under cells with more height. To do this, you would need to keep track of multiple groups per a cell, with the start/stop heights for each group range. This would allow the water as pictured above to be a single part, and the whole map in roughly two hundred or so parts. This is much more complicated, so I’d only try to implement it only if performance remains an issue after the simpler compression this builds on. It still wont form an optimal map, but it will be much, much better.

4 Likes