Generate planet textures using the Fault Formation algorithm
Perhaps someone remembers the wonderful old-school space game Star Control 2. At one time, I was struck by a huge star map with uncharted planets that were to be explored against the backdrop of an unfolding global catastrophe. Since the authors published the source codes, the game has been ported under the new name The Ur-Quan Masters to most modern platforms.
Rummaging around in the source, I discovered a simple algorithm that generates the textures of the planets, and wrote a program in Python that allows you to generate similar textures.
- We form a height map using the Fault Formation algorithm ( “fault formation” )
- Color the height map using reference RGB colors and the gradient between them
Formation of heights map
- Create a matrix of heights of the same dimension as the generated texture of the planet. North is above, south is from below, and the horizontal of the matrix is a circular scan of the planet along the parallel. The value of the heights can only vary from 0 to 255, so it’s convenient to display them in shades of gray
- Fill the matrix with a base elevation , for example, 128:
- We generate two random disjoint lines from north to south. These lines “form a fault” - they divide the planet's surface into two parts, one of which we raise to a fixed constant ( elevation delta ), and the other we lower to the same constant. For clarity, let the constant be 10:
- We repeat the previous paragraph a specified number of times ( iterations num ).
In the figures below: 10 iterations, 100 iterations, 1000 iterations:
Coloring a height map with RGB colors
- Divide the entire range of heights (from 0 to 255) into N more or less equal parts with N + 1 reference points at the boundaries of these parts
- Set the RGB color for each reference point in the range.
- We calculate the RGB color for all the intermediate points of the range, linearly interpolating the color components between the anchor points. In other words, fill the heights with a gradient between the reference colors
- We generate the texture of the planet, replacing each cell of the DEM with the RGB color calculated in the previous paragraph
Here are some possible options for coloring the same height map:
To run the program, you need to install Python 2.7 and several libraries for it: NumPy , PIL and PyOpenGL.
The program can be downloaded from the git repository: https://github.com/barabanus/starcontrol
The program consists of two independent scripts: planet.py (generates a texture), and space.py (draws a rotating planet with a superimposed texture).
Some management features:
- To change the reference color, left-click on the square with the color. In MacOS, in the pop-up dialog, it is possible to sample color from the screen
- To create a new reference color, left-click on the gradient
- To remove the reference color, right-click on the square with the color
- To save the texture, click on it with the left mouse button. This runs a script that draws a spinning planet with this texture.
The program was created on MacOS, tested on WinXP. If you find and manage to correct a mistake in the code or decorate the render, write and suggest changes.