Sunday, November 3, 2013

Tiling a Sphere With Mostly Hexagons

I saw this thing where a person generates planets using a grid that looks like a bunch of hexagons:

Hexagon-based grids are popular for things like strategy games, because adjacent tiles are separated by a constant distance. I used the math from vraid's code as the basis for a demo of this kind of tiling system. This video shows sphere tiled with RGB colors based on each tile's vector location on the unit sphere, and a 2D detail view for navigating around a local region of the grid:

The grid starts as a dodecahedron (made of twelve pentagons), and each vertex is squashed into a hexagon to produce a soccer ball shape. That process is repeated to create each higher resolution grid. This video demonstrates the process, sampling from ETOPO1 data to color each tile:
One really cool thing is that you can define these grids lazily, with hex locations determined for only a small region of the sphere at a given resolution.

You can imagine a game where an entire planet's climate is simulated with 500km tiles, the weather for a single continent is simulated with 10km tiles, battles take place on a grid of 10m tiles, and hand-to-hand combat on a grid of 1m tiles. (The memory cost to store even a single byte of data for each tile of a planet-sized sphere at this resolution would be prohibitive.)

This video shows a grid lazily subdivided to achieve this kind of resolution, using a noise function to color the tiles:
As mentioned above, the grid begins as a grid of pentagons. At each iteration of the vertex-squashing process, the previous grid's faces are carried over to the new one with the same number of sides and centered at the same location on the unit sphere; while each of the previous grid's vertices becomes a hexagonal face of the new grid.

As a consequence, the twelve initial pentagons can be found in the grid for each resolution. Trying to render these in a 2D local view can be challenging as they necessarily break up the hex grid. This video shows one option (other people who have talked about these kinds of grids, again in a game context, have proposed putting mountains or seas or some other inaccessible feature around each of the pentagon locations, to avoid having to deal with this entirely):
Code for my demo program is at Also vraid appears to be rewriting their code in Racket, which is interesting.