Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Create and Manage Huge Worlds

Introduction

Welcome to the Course

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.

Now let’s get started on the project!

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Generating our Terrain

Creating our Terrain

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Generating our Terrain

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Generating our Terrain

Let’s start by exporting the Texture map. Right click on the tab and select Export layer.

Set File format to PNG

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.

We’ll also export the Heightfield.

Set File format to RAW


Set File name to the terrain folder, calling the file TerrainHeightmap

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Generating our Terrain

In the next lesson, we’ll be hopping in Unity to setup our terrain object.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Setting Up our Terrain in Unity

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Setting Up our Terrain in Unity

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Setting Up our Terrain in Unity

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.

Add the FPS component


Add the Rigidbody component
Enable Freeze Rotation on all axis’

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Setting Up our Terrain in Unity

With our camera, drag that in as a child of the player.

Set the Position to 0, 1, 0


Drag the camera into the Camera property of the FPS component

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Splitting our Terrain

Splitting the Terrain

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Splitting our Terrain

Select the bottom left chunk and add a new terrain layer like we done in the last lesson.

Set the Size to 1024, 1024

For the Offset, we don’t need to change that since we’re working with the first tile.

Select the Terrain 0 1 chunk and do the same thing as above.

Set the Offset to 0, 256

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Splitting our Terrain

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Enabling and Disabling the Chunks

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.

private Terrain[] chunks;

public float visibleDistance;


public int chunkSize;
public float checkRate;

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>();

// check the chunks every 'checkRate' seconds


InvokeRepeating("CheckChunks", 0.0f, checkRate);
}

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;

// loop through each chunk


foreach(Terrain chunk in chunks)
{
// get the center pos of the chunk
Vector3 chunkCenterPos = chunk.transform.position + new Vector3(chunkSize / 2
, 0, chunkSize / 2);

// check the distance


if(Vector3.Distance(playerPos, chunkCenterPos) > visibleDistance)
chunk.gameObject.SetActive(false);
else
chunk.gameObject.SetActive(true);
}
}

Back in the editor, let’s set the chunk hider properties.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Enabling and Disabling the Chunks

Visible Distance: 500


Chunk Size: 256
Check Rate: 0.5

In the next lesson, we’ll be setting up a system to hide objects that are in certain terrain chunks.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Hiding Objects

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.

In the script, let’s start by setting up our variables.

private Bounds bounds;


private GameObject[] hidableObjects;

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");

// get the bounds of the terrain


Terrain terrain = GetComponent<Terrain>();

Vector3 dimensions = new Vector3(terrain.terrainData.heightmapWidth, 1000, terrai

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Hiding Objects

n.terrainData.heightmapLength);
Vector3 pos = transform.position + new Vector3(dimensions.x / 2, 0, dimensions.z
/ 2);

bounds = new Bounds(pos, dimensions);


}

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);
}
}

Back in the editor, we can press play and try it out.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Level of Detail

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Level of Detail

If you zoom in and out you can see the models changing in detail, with the furthest one away being a
2D billboard.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Fog

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).

Scroll down to the bottom of the window and enable Fog.

Set the Color to green


Set the Density to 0.04

Now let’s select the camera and…

Set the Clear Flags to Solid Color


Set the Background color to the same as the fog

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com


Create and Manage Huge Worlds
Fog

You can tweak this however you wish, making the fog denser, a different color, etc.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)


Create and Manage Huge Worlds
Conclusion

Congratulations on Finishing the Course

Let’s have a look at what we made:

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.

© 2019 Zenva Pty Ltd. All rights reserved. https://academy.zenva.com

Powered by TCPDF (www.tcpdf.org)

You might also like