10 months ago, I created and uploaded my first terrain terrain generator. For a first attempt,
it was fine, but there were still features that felt like they were “missing”. Recently,
I decided to reopen the project and started another terrain generator from scratch. I
decided to start from scratch so that I could better structure the program and leave room
for more features. Before I explain the program in depth, though, here are some of
the outputs.
If you watched the video about version one, you can probably see some of the differences
already. Most visibly, more trees, flowers, and other details were added. However, the
most important changes to the program were the ones to how it was structured.
To start, the program now has separate functions for generating and rendering the terrain.
In addition to making the program more logical, it also allows for horizontal camera movement,
which I couldn’t implement in the last terrain generator. It also makes the program more
efficient by setting a maximum generation distance instead of generating terrain as
long as the program is running.
The actual generation was also streamlined in a few other ways, like adding extensions,
which I'll explain later. The rendering was also streamlined by rendering only what’s
visible on the screen.
Most importantly though, version two introduced parameters, as opposed to hard-coding values
into the program. This allowed for biomes: objects that contain values that dictate various
aspects of the terrain. Essentially, biome objects store values that, when passed as
parameters in the generator and render, create specific terrain. Biomes change the height
variability, flatness, details and their frequency, and the colors of the grass and dirt.
Finally, the resolution of the terrain is no longer hard-coded into the program, as
it was in version 1. That being said, the default resolution is also smaller than version
one’s, giving it less of a blocky appearance.
Though it might seem like the program is entirely new, it still uses a heightmap to store the
terrain and a single, factor-based noise algorithm to generate the heightmap. Though the program
is structured differently, the actual height map generation and rendering is essentially
the same as version one.
In addition, the terrain generator can only generate one biome at a time.
First, in the setup block, the program initializes the images and biome objects.
The next step is the terrain generation, which also happens in the setup block. This process
happens stack by stack until the program reaches the specified generation distance.
First, the program will get the height of the previous stack from the height array.
Then, based off of the height factor, it will add a random number to the previous height
to get the current height.
Next, the program will add the new current height to the array multiple times; the number
of times is determined by the extensionsMax variable. This “extends” the terrain height,
and makes the terrain generation more efficient by reducing the number of values that have
to be generated.
Finally, the details will be generated by a random number generator, whose range is
in turn determined by the detail frequency value in the biome object. These values will
be added to an array.
Once this process is finished, the program will begin to render the previously generated
terrain. This process is also stack by stack, but only renders what is visible on the screen.
First, the terrain is shifted based on the camera offset value. Next, like in the previous
generator, the renderer draws the grass and dirt using the rect function, with the color,
height, and width being defined by the biome and the resolution. Finally, the render checks
the details array and draws an image if necessary.
Though version 2 of the terrain generator produces similar-looking terrain, the program’s
new structure makes the generation and rendering more efficient. It also makes the terrain
more customizable, which allows for custom biomes.
Personally, I think this version is more useful as a stepping stone than an actual terrain
generator. Though the program has the structure and components of a full-ish terrain generator,
the visuals that it produces aren’t particularly exciting. I believe that the next step is
to add some artistic flair to the terrain by adding some more parameters, and adding
tile support for images. It could also benefit from a UI to control the generation and rendering
settings. These are all features I plan to implement in version 3.