Professional Documents
Culture Documents
Welcome To The Course: Create and Manage Huge Worlds
Welcome To The Course: Create and Manage Huge Worlds
Introduction
When making large worlds for your game, performance is something you’ll always need to keep in
mind. Throughout this course, we’ll go over a number of methods to increase performance for large
worlds, these involve:
Chunk system for our terrain, only showing chunks near the player. This increases
performance greatly, as rendering the entire world at once can be quite taxing.
Level of detail allows us to lower the quality of a model, the further away it gets.
Fog is used in many games (much more noticeable in older ones), to hide where the
rendering of the world cuts off.
For this project, we’re going to be needing a terrain to test out the chunk system with. The
program L3DT is what we’re going to use. This is a free program which can generate terrains with
various different properties and options. Download L3DT from http://www.bundysoft.com/L3DT.
Download the program, install it, then open it up. To create our terrain, we can click on the new
terrain icon.
Make sure you have Designable map selected, then click on the Next button.
Keep clicking next until you get to the Calculation queue screen. Here, we want to select all of the
options.
Keep clicking next and then the map will begin to generate. It will take around 30 seconds to a
minute or so. When this is done, we can see our finished terrain. There are two of these maps we
need. The first is the Texture map. This is the texture which will be applied to the terrain.
Then we’ll need the Heightfield. This is what we’ll apply to our terrain in Unity to automatically
generate the mesh. It will the whiteness of each pixel. The whiter the pixel is, the higher that point
on the terrain will be.
Let’s start by exporting the Texture map. Right click on the tab and select Export layer.
Make sure you have a new (or existing) Unity project and create a new folder inside the asset folder
called Terrain. Here’s were we want to set the File name, calling the texture TerrainTexture.
In the next lesson, we’ll be hopping in Unity to setup our terrain object.
Unity Terrain
Inside of Unity, let’s right click in the Hierarchy and select 3D Object > Terrain. This will create a new
terrain object.
Select the terrain and click on the Terrain Settings icon. We need to change a few things here.
Down in the Mesh Resolution tab, we want to change the terrain width and length to 1024. This is
because the size of our terrain is 1024×1024 pixels.
To import our heightmap, we can click on the Import Raw… button. Find the RAW heightmap and
open that up.
In the Import Heightmap window, we just want to change the Y size to 200. Then click import.
To add a terrain texture, we can switch over to the brush tab, then in the drop-down, select Paint
Texture. Click on the Edit Terrain Layers… button and choose Create Layer… Here, we want to
find the terrain texture.
You’ll notice though that the texture is repeated 1024 times in the width and length. To fix this, we
can select the new layer and change the Size to 1024, 1024.
One problem though, is that the texture isn’t orientated the right way. To fix this, we can re-import
our heightmap, this time enabling Flip Vertically.
Player Controller
To make our player controller, let’s start by creating a new capsule object called Player.
Now you should be able to press play and walk around your terrain. In the next lesson, we’ll be
splitting our terrain into chunks.
To split the terrain, let’s open up the split terrain window (Dvornik > Terrain > Split Terrain).
Select the terrain, then click on the Split terrain button. This will split the terrain into 4 separate
chunks. You’ll notice that the terrain’s texture has been shifted and didn’t carry over with the split.
This is because all the tiles are still using the same texture and need to be offset. The bottom left tile
is correct, but with the others, we need to offset them.
Let’s now split each of the 4 terrain chunks, so we have 16 in total. Disable the original terrain
objects. Delete the original terrain layer.
Select the bottom left chunk and add a new terrain layer like we done in the last lesson.
For the Offset, we don’t need to change that since we’re working with the first tile.
Now we need to do this for all the other chunks. Creating a new terrain layer with a size of 1024 and
an offset depending on the tile position. An easy way to do this would be to look at the name of the
chunk. Terrain 2 3 for example, would have an offset of 512, 768. Multiply the first number by 256
for the x offset and the second number for the y offset.
In the next lesson, we’ll be setting up the script to toggle the chunks depending on the player
distance.
ChunkHider Script
Create a new C# script called ChunkHider and attach it to our player object. Opening up the script,
we can begin with our variables.
In the Start function, we’re going to start by finding all of our chunks and adding them to
the chunks array. Then, we’ll call start calling the CheckChunks function (making it next)
every checkRate seconds. This is done using the InvokeRepeating method.
void Start ()
{
// get all of the chunks
chunks = FindObjectsOfType<Terrain>();
CheckChunks is a function which we’ll use to check the distance of each chunk from the player. If
it’s past the visibleDistance, then it will be disabled, otherwise enabled.
void CheckChunks ()
{
Vector3 playerPos = transform.position;
playerPos.y = 0;
In the next lesson, we’ll be setting up a system to hide objects that are in certain terrain chunks.
ObjectHider Script
Create a new C# script called ObjectHider and attach it to all of the terrain chunks. This script will
enable/disable objects inside that chunk when it gets enabled/disabled.
Before we begin, we need to create a new tag which we will apply to objects we want to be able to
hide. Click on the Layers drop-down and select Edit Layers… Here, we want to add a new layer
called ChunkObject.
Now select all of the objects you want to be able to hide and add the ChunkObject tag.
Then in the Awake function, we want to get all of the hidable objects and set our bounds.
void Awake ()
{
// get the hidable objects
hidableObjects = GameObject.FindGameObjectsWithTag("ChunkObject");
n.terrainData.heightmapLength);
Vector3 pos = transform.position + new Vector3(dimensions.x / 2, 0, dimensions.z
/ 2);
Unity has a few built in functions that can tell us when the chunk the script is on is being enabled or
disabled. When this happens we want to enable/disable the objects in the chunk.
void OnEnable ()
{
foreach(GameObject obj in hidableObjects)
{
if(obj != null && bounds.Contains(obj.transform.position))
obj.SetActive(true);
}
}
void OnDisable ()
{
foreach(GameObject obj in hidableObjects)
{
if(obj != null && bounds.Contains(obj.transform.position))
obj.SetActive(false);
}
}
Level of Detail
Level of detail (LoD) reduces the detail of a model, the further away the camera is. This increases
performance, especially in huge worlds.
In Unity, we can select a terrain tile and go to the Paint Trees tab. Here, we want to click on
the Edit Trees… button and select Add Tree. Here, just drag in a tree prefab and click Add.
Now we can paint trees on our terrain and even if we click outside of the selected chunk, it
automatically carries over to the next one.
If you zoom in and out you can see the models changing in detail, with the furthest one away being a
2D billboard.
Fog
Many games (especially older ones) use fog to hide the max distance that the world is being
rendered at. It can also be used for both setting the mood of the game and to create the illusion of
the world being larger than it really is. To create fog, we need to go to the Lighting window (Window
> Rendering > Lighting Settings).
You can tweak this however you wish, making the fog denser, a different color, etc.
We used L3DT to create a detailed and realistic looking terrain heightmap and texture.
Then, it was imported and applied inside of Unity.
The chunk was then split into 16 parts and we made a script to toggle them depending on
the distance from the player.
Level of detail, allowed us to lower the quality of models the further away they are,
increasing performance.
Finally, we looked at fog which hid the render distance of the world and can even make it feel
larger.