[Part 3] Building mountains: Creating the landscape
On the second part of this series, we learnt how we could create polygons that resemble mountains. Now, on the third part of this series we will tweak our algorithm to start generating random mountains
Building the first version of our terrain
The previous algorithm we had it would create basic mountains:
What we are looking for now is to reuse that algorithm to create a more smooth terrain like shape:
Stacking the mountains
Now that we have created our basic shape of a terrain we can stack them together one above the other:
You may have noticed that if you click “Create terrain” enough times, the red mountain (the closer to you) might be tall enough that it will cover the one behind it. To solve that issue we will need to introduce the concept of a maximum height for the mountains.
Introducing the concept of flatness
The terrain we have built so far have extremely highs and lows. In order to solve that, we can introduce the new concept of chance of a part of the terrain to be flat.
Putting everything together to create the scene
Now that we have added multiple mountains to our scene, we can add a third mountain to give the impression of depth to our scene:
We adjust the colors of the mountains and add an opacity to replicate our depth of view:
Now it’s a matter of adjusting our gradient, adding a few clouds and opacity:
Performance considerations
All of this runs in the browser, so performance depends on how much work we make it do. Generating the terrain is fast as long as we keep the number of points small. We do not need very detailed shapes to make it look good. The sense of depth comes from stacking a few layers, not from adding more detail.
SVG itself is fine, but updating too many things in the DOM is not. We should only update the shape data, not the whole component. Simple animations like opacity or small movements are cheap. Three or four mountain layers are enough.