Thoughts
and ramblings.

Planet Rendering Pt. 2

24 Sep 2017 - Tokyo/San Francisco | 2039 words

A long-awaited continuation of my previous post, in this one we will go into the exciting intricacies of:

• Applying vertex coloring to give our planet a little more personality,
• Rendering a new type of sphere, the icosahedron, and
• Projecting a terrain map onto the icosahedron.

You can view the code used in this post here.

Vertex Coloring

Adding a little color to our planet will turn it from the boring blue orb we’ve seen previously to the more realistic, colorful planet seen below!

To accomplish this task, we first divide the terrain height map into different regions and assign a color based on the height of that particular vertex as we create the sphere. This only requires a tiny refactoring of our current code due to how Three.js handles vertex colors. Choosing offsets is a bit of trial and error depending on how large we choose the displacement factor in terrain generation. In this particular case, since our displacement factor is pretty tiny (0.1), our offsets reflect that.

Coloring is as simple as finding a cutoff for the terrain type we're trying to represent. In this case, we're splitting the terrain into ocean, sand, grassland, and mountains.

Secondly, we modify how we create Face3 objects, using the _color function above to assign a color upon creation.

Now that we have the vertex colorization accomplished, let’s take a stab at fixing the distortion in the UV sphere geometry through the use of another sphere geometry – the icosahedron.

Icosahedron

The icosahedron is a polyhedron with 20 faces where each face retains its form no matter where it appears on the sphere, unlike the UV sphere.

Due this handy property, we can use the icosahedron as a starting point for a more realistic looking planetoid. Through subdividing the faces in increasing levels of detail, the icosahedron becomes a smooth, non-distorted sphere as seen below.

The algorithm to generate the sphere is pretty straightforward. The icosahedron starts off with 20 faces created using a set of 12 initial vertices.

All we need to generate the initial vertices is a single calculation of the golden ratio ($$\tau$$) and then applying that around the sphere.

Afterwards, we’ll setup the initial faces and based on the level of detail we want, divide the initial faces into smaller and smaller triangles.

And voila! One complication with the icosahedron geometry is the mapping of the generated terrain heights to the vertices. Since the icosahedron geometry is not a grid like the UV sphere, we’ll have to estimate where a particular vertex would be in the grid.

Ideally, we’d update our terrain generation code to work nicely with the icosahedron geometry, but this saves us a little bit of time and makes it so we can reuse the same terrain generation code with different geometries.

2D terrain map on a 3D surface

Projecting a 2D terrain map onto the 3D surface will require a little trigonometry. As we’re creating the vertices for the planet, we’ll convert the (x, y, z) coordinate into a (x, y) coordinate that maps onto the terrain. This technique is actually very similar to how you would generate texture coordinates to apply a texture to the icosahedron geometry.

Lets take a look at the updated getHeight function in the icosahedron planet generator below:

Here we use the inverse trigonometry functions atan2 and asin to convert the vertex coordinates into the x, y coordinates we can use to reference a height map.

Combined with the vertex coloring functionality developed earlier, we now have the following, more colorful looking planetoid.

Further tweaking of the terrain generation and blending of the vertex colors would fix some of the artifacts you’ll probably see in the planet below, such as the water/grass terrain stretching up into the mountains and hard cutoffs rather than smooth transitions between terrain types.

You can view the code used in this post here.