Game Development Reference
7.3.4 Infinite Terrain
Infinite terrain or endless terrain is a form of procedurally generated
landscapes. It uses a mathematical equation and the player's current
position to create the landscape as the player moves. Small parts of the
map are created as needed based on the player's visible distance. Any
map outside the visible range is not generated and therefore not a load
on computer memory.
To create undulation of a terrain, a mathematical formula is used for
determining the height based on the x and z positions on the terrain
for which there will always be a y value no matter the x and z values.
For example, the sine or cosine functions can produce values for infinite
values of x and/or z and the result used as the y value.
The beauty of using a formula is that the result of y for any x and z values is
always going to be the same, thus assuring us that if we return to a previous
location on the map that was destroyed after we left but recreated on our
return, the height remains the same. In addition, the terrain can be infinitely
large, generated entirely by the computer. This means a human artist is not
required to constantly churn out more and more of the landscape.
Terrain textures, trees, rocks, and other model objects are used over
and over again.
Perlin Noise makes for a good choice in infinite terrain generation
applications as it produces a random yet smooth undulating surface.
On the Web
An Infinite Terrain Generator
Developing an infinite terrain generator is a tricky business and as such
is beyond the scope of this topic. However, a generous Unity forum
member going by the name of Quick Fingers posted a package for one
on the Unity Web site. For this topic, the code has been modified to
use Perlin Noise and places trees and different textures on the terrain
at varying heights. You can download the project from the Web site as
Chapter Seven/InfiniteTerrain.zip .
On opening the Infinite Terrain scene, you will find a Terrain Generator
object in the Hierarchy. Select this to modify the terrain's parameters
in the Inspector. You will notice that different terrain textures are
used for different altitudes. When rendered, the contrast between
the textures is evident. They are not blended in the same way as the
Unity paint on textures in the Unity Terrain Editor. This is because they
are placed on the mesh live as the game is playing and the mesh is
generated. While a blending algorithm could blend the textures to
make them fit together better, it would slow down the generation
of the terrain.