Welcome to another “Is Jono obsessed with procedural generation?”.
This week I read a great article on HackerNews. This prompted me to create a procedural mountain generator based on Cellular Automata (CA), an algorithm in which a collection of cells change state over time based on their neighbouring cells.
In CA a group of cells is iterated on over again and again, each time the cell’s state is determined by its neighbouring cells.
I’m not going to go into great detail here because it has already been done much better than I would have here (chapter 7.2 is particularly relevant to what I am doing here).
But after reading that and seeing some awesome pictures like the one below (Rule 30 of the elementary CA), I decided to give it a crack.
After I creating the elementary cellular automata like the above I added randomness by intermittently swapping between the basic rules.
Add some colour and…
Next I wanted to make it a bit more interesting. Trees are randomly placed anywhere there was solid ground (i.e. the black in the above picture). They appear randomly with a higher chance of spawning the further down the mountain. An example with new colours is below…
You also might have spotted the cave at the top, they have a chance of spawning wherever there is a flat piece of ground that extends for at least 5 pixels. They are also a lot less common than trees.
You might also note that in the above picture there are multiple mountains, I changed the cellular automata rules slightly, so that if the above 3 cells are totally empty then there is a small chance that the cell will be filled. A single filled cell triggers each line below to expand on it creating another mountain.
Next on my list to spruce things up was waterfalls…
To make the waterfalls it was easier to do a second pass on the whole scene, this is because the scene is generated from top to bottom and I would need to keep track of the waterfall the whole way down. So instead I just loop through looking for nice flat pieces of rock face and draw the whole waterfall. I also dither the waterfall left and right when passing down over parts of the mountain that weren’t entirely flat.
Lastly I added some fog just by throwing a layer of white over the top, making sure that the opacity increases as it goes down. I also randomly changed the colour of a bunch to give the rock face a nice speckled look.
Done and just in time for r/proceduralgeneration November challenge.
Feel free to have a play with the generator here.