Creating Terrains with Unity

Terrains in Unity

Terrains in Unity

In this article, I will introduce the reader to Terrains in Unity. I will describe the different terrain features that Unity offers and we will build a simple terrain that can be used in your game.

Introduction

In this article, I will explain how to use the Terrain Engine in Unity.

I assume the reader has a basic understanding of Unity including creating a new Unity project and, asset management, and the basic GameObject-Component model. If not, then I would refer the reader to my previous article which provides an introduction to Unity here: http://3dgep.com/?p=3246.

Creating a Terrain

To create a new Terrain in your scene, select Terrain > Create Terrain from the main menu.

Unity - Create Terrain

Unity – Create Terrain

This will add a new Terrain Asset in the Project view and add a GameObject in the scene with a Terrain (Script) component and a Terrain Collider component that references the new Terrain Asset.

Unity - New Terrain

Unity – New Terrain

Terrain Resolution

If you want to change the properties of the currently selected terrain, you can choose Terrain > Set Resolution from the main menu.

Unity - Terrain Resolution

Unity – Terrain Resolution

The Set Heightmap Resolution dialog box exposes the following properties:

  • Terrain Width: Allows you to specify the width (X-axis) of the terrain in world units.
  • Terrain Height: Allows you to specify the maximum height (Y-axis) of the terrain in world units.
  • Terrain Length: Allows you to specify the length (Z-axis) of the terrain in world units.
  • Heightmap Resolution: The resolution of the terrain heightmap. The heightmap is always a square texture that has this value for both the width and height of the texture regardless of the size of the terrain.
  • Detail Resolution: The resolution of the detail map that controls the grass and detail meshes. For best performance, you should keep this value as small as possible while still maintaining the desired amount of detail.
  • Detail Resolution Per Patch: The detail texture is split into patches according to this parameter where each patch combines all of the detail (grass and detail meshes) into a single piece of geometry and a single texture. For example a Detail Resolution map of 1024×1024 will be split into 128×128 detail patches if Detail Resolution Per Patch is set to 8. Larger values for this parameter will produce less detail patches and thus less draw class to the GPU if a lot of terrain is visible. Smaller values will produce more patches which could potentially produce more draw calls (depending on how much of the terrain is visible). If you have relatively flat terrain, I would suggest to set this value higher (64, or 128 for example) but if you have a mountainous terrain, I would suggest you keep this value smaller (8 or 16 for example).
  • Control Texture Resolution: The resolution of the spat map that is used to layer the different textures that are painted onto the Terrain.
  • Base Texture Resolution: The base texture is used in place of the splat map when the viewer is sufficiently far away.

Terrain Heightmap

The Terrain Heightmap is a 2-dimensional texture that is used to encode the height of each point on the terrain. The height values of the terrain are usually encoded in 8, or 16-bit floating point values in the range 0 to 1. A value of 0 in the heightmap indicates the lowest part of the terrain and a value of 1 in the heightmap indicates the highest part of the terrain.

Terrain Heightmap

Terrain Heightmap

The original terrain heightmap can be generated in an external tool such as Terragen (http://www.planetside.co.uk/) or using plug-ins purchased on the Asset Store directly in Unity. You can also create your terrain heigtmap directly in Unity using the available heightmap tools.

Unity - Terrain Tool

Unity – Terrain Tool

The first three buttons on the Terrain Toolbar allow you to adjust the height of the terrain using the following tools:

  • Raise/Lower Height: The Raise/Lower tool is used to adjust the height of the terrain using a variety of brushes.
  • Paint Height: The Paint Height tool allows you to flatten areas of the terrain to a specific height.
  • Smooth Height: The smooth height tool allows you to smooth rough edges of the terrain.

Raise/Lower Height

As the name suggests, the Raise/Lower Height tool allows you to raise and lower the height of the terrain. The terrain can be raised to the maximum (1.0) height and it can be lowered to the minimum height (0.0). You cannot lower the terrain below the lowest possible value.

Unity - Raise/Lower Height

Unity – Raise/Lower Height

Clicking on the terrain will raise the terrain incrementally. Holding Shift while clicking will cause the terrain to lower incrementally. Keeping the mouse button held down while moving the mouse will allow you to raise and lower the terrain continuously.

Unity - Raise/Lower Terrain Tool

Unity – Raise/Lower Terrain Tool

The Raise/Lower Height brush has the following properties:

  • Brush Size: This slider allows you to select the size of the brush in terrain heightmap resolution units. The minimum value being 1 terrain heightmap pixel and the maximum value being 100 terrain heightmap pixels.
  • Brush Opacity: The brush opacity determines the intensity of the change that occurs. This slider property can have a minimum value of 0 which will cause no change to occur and a maximum value of 100 which will cause the terrain to be raised/lowered a full increment.

Paint Height

The Paint Height terrain tool will allow you to flatten certain areas of the terrain to a specific height.

Unity - Paint Height Terrain Tool

Unity – Paint Height Terrain Tool

This is useful for creating plateaus and shelves on your terrain.

The Paint Height terrain tool has the following properties:

  • Brush Size: This slider allows you to select the radius of the brush in terrain heightmap resolution units. The minimum value being 1 terrain heightmap pixel and the maximum value being 100 terrain heightmap pixels.
  • Brush Opacity: The brush opacity determines the intensity of the change that occurs. This slider property can have a minimum value of 0 which will cause no change to occur and a maximum value of 100 which will cause the terrain to be adjusted a full increment.
  • Height: This value determines the desired height to set the terrain to. This value is measured in world-units. The minimum value is 0 and the maximum value is the value of the Terrain Height parameter specified in the Terrain Heigtmap Resolution dialog box. You can adjust this slider value manually, or you can Shift-Click on a part of the terrain to set this property to the height of the terrain at the clicked point.

Smooth Height

After adjusting the height of the terrain several times using the Raise/Lower and Paint Height tools, the terrain can become quite jagged. The Smooth Height terrain tool can be used to smooth-out these apparent jaggies to create a more natural, smooth terrain. This is equivalent to the erosion that occurs naturally after thousands of years of exposure to weather conditions.

The image below shows an example of jaggies that occur after manipulating the terrain.

Unity - Terrain Jaggies

Unity – Terrain Jaggies

After smoothing:

Unity - Smooth Terrain

Unity – Smooth Terrain

The Smooth Height terrain tool has the following properties:

  • Brush Size: This slider allows you to select the radius of the brush in terrain heightmap resolution units. The minimum value being 1 terrain heightmap pixel and the maximum value being 100 terrain heightmap pixels.
  • Brush Opacity: The brush opacity determines the intensity of the change that occurs. This slider property can have a minimum value of 0 which will cause no change to occur and a maximum value of 100 which will cause the terrain to apply the maximum amount of smoothing.

Import/Export Heightmap

After you have created your prefect heightmap, you may want to export it for use in another project or to be used in an external program to be fine-tuned. Or, you may want to import a heightmap that was created with an external tool such as Bryce, Terragen, or Photoshop.

To export your heightmap from Unity, select Terrain > Export Heightmap – Raw… from the main menu.

Unity - Export Heightmap

Unity – Export Heightmap

The Export Heightmap dialog box should appear.

Terrain - Export Heightmap Dialog

Terrain – Export Heightmap Dialog

In most cases, the default settings should be sufficient. Click the “Export” button to export your heightmap to a Raw file.

Because the Raw file format does not encode the pixel format or the dimensions of the texture in the file itself, it is generally a good idea to save the file with this information in the filename. For example, a 16-bit heigtmap texture of size 257×257 pixels should be saved with the name:

terrain_257x257_16-bit.raw

This way, if you want to use this texture in another program, you already know the size of the texture and the pixel format.

You can then use your texture in a program such as Bryce, Terragen, or Photoshop to fine-tune the heightmap.

For example, if you open a raw file in Photoshop, you will get the following dialog box:

Photoshop - Raw File Options

Photoshop – Raw File Options

If you specify the correct settings according to how you exported the Raw file in Unity, you should see the heightmap of your terrain in Photoshop.

Phtoshop - Edit Raw File

Phtoshop – Edit Raw File

After you make the necessary adjustments to the terrain in Photoshop you can import the modified terrain file into Unity using the Terrain > Import Heightmap – RAW… command from the main menu in Unity.

Terrain Textures

Textures can be painted onto the terrain using the Paint Texture tool.

Unity - Paint Texture

Unity – Paint Texture

Before you can paint any textures to the terrain, you must first add at least one texture to the terrain. To get started quickly, Unity provides suitable terrain assets in the Standard Packages that comes with Unity. To import the Terrain Assets, select Assets > Import Package > Terrain Assets from the main menu.

Unity - Import Terrain Assets

Unity – Import Terrain Assets

Click the Import button to import these assets into your current project.

With the Paint Texture tool selected in the Inspector click the Edit Textures… button and select Add Texture from the popup that appears.

Unity - Add Terrain Texture

Unity – Add Terrain Texture

The Add Terrain Texture dialog box should appear.

Unity - Add Texture

Unity – Add Texture

The Add Terrain Texture dialog has the following properties.

  • Splat: The texture to apply to this slot in the terrain.
  • Tile Size: How large each tile of the texture is applied to the terrain. Larger tiles means the texture will have less tiles across the terrain in each direction. Smaller tiles will mean the texture will be repeated more times across the terrain in each direction.
  • Tile Offset: Adjusting this parameter will shift the texture in the terrain. This is useful if you want to blend multiple layers with the same texture to break the repetition that occurs when using a single texture.

Click the target button on the Splat parameter and select a texture to apply to the terrain.

Unity - Select Texture

Unity – Select Texture

After you apply the texture to the terrain, you should see the entire terrain covered in that texture.

Unity - Textured Terrain

Unity – Textured Terrain

The Paint Texture terrain tool has the following properties:

  • Textures: The Textures property shows the different textures that are assigned to the various texture slots of the terrain. You can have a maximum of 255 textures assigned to the terrain.
  • Brush Size: The radius of the Paint Texture brush measured in pixels of the Control Texture resolution.
  • Opacity: The intensity that the selected texture will be applied to the terrain. This parameter is measured as a percentage from 0 to 100.
  • Target Strength: The Target Strength parameter can be used to cap the intensity of the currently selected texture. This value is measured as a ratio of the amount of texture that will replace the texture under it. If you don’t want to apply more than 50% opacity of the selected texture to the terrain, then you should set this value to 0.5.

Before you can blend textures on the terrain, you must have at least 2 textures in the terrain’s texture list. Add a few more textures to your terrain and use the Paint Texture tool to fancy-up your terrain. The currently selected texture will have a blue outline in the Inspector.

Unity - Textured Terrain (2)

Unity – Textured Terrain (2)

As far as I know, there is no options or settings to automatically paint the textures of the terrain based on the height and slope of the terrain. Most likely there is a (paid) plug-in that will do that for you automatically.

Now that we have some interesting textures applied to the terrain, it’s time to add some detail.

Trees

Trees can be painted onto the terrain using the Place Tree tool in the inspector.

Unity - Place Trees Tool

Unity – Place Trees Tool

Just like the Paint Texture tool, the Place Tree tool requires at least one tree to be added to the terrain.

Click the Edit Trees… button and select Add Tree from the pop-up menu that appears.

Unity - Add Tree Dialog

Unity – Add Tree Dialog

The Add Tree dialog provides the following properties:

  • Tree: The tree model to use for this slot in the terrain’s tree list.
  • Bend Factor: The the amount of bending that will be applied to the trees when they are effected by wind.

Select the target icon next to the Tree property and select a tree from the dialog that appears.

Unity - Select Tree

Unity – Select Tree

Click the Add button to add that tree to the tree list for the terrain.

Unity - Tree Inspector

Unity – Tree Inspector

The Place Trees tool has the following properties:

  • Trees: The list of trees assigned to the different slots of the terrain.
  • Brush Size: The radius of the brush in world unit.
  • Tree Density: How many trees will be placed when the terrain is painted.
  • Color Variation: The amount of random shading that will be applied to the trees when they are painted.
  • Tree Height: The amount of scaling to apply to the painted trees. A value of 100 will paint the trees with a scale of 1 and a value of 200 will double the height of the trees.
    Unity - Tree Heights

    Unity – Tree Heights

    The image shows the trees on the left are painted with a height of 50, the trees in the middle are painted with a height of 100 and the trees on the right are painted with a height of 200.

    • Variation: The amount of random variation to apply to the tree heights.
  • Tree Width: The amount of scaling to apply to the width and depth of the tree.
    • Variation: The amount of random variation to apply to width of the tree.

Mass Place Trees

You can have Unity automatically place trees for you on the terrain by using the Mass Place Trees option in the Terrain menu.

Unity - Mass Place Trees

Unity – Mass Place Trees

Select the number of trees that you would like to place and press the “Place” button to randomly place that number of trees on your terrain. Doing this will replace any trees that you have already placed on the terrain.

Unity - Mass Place Trees (2)

Unity – Mass Place Trees (2)

Unity will place the trees on relatively flat areas of your terrain.

Creating Custom Trees

Unity provides the Tree Creator tool that allows you to create your own custom trees directly in the Unity editor. To create a custom tree in Unity, create a new Tree GameObject by selecting GameObject > Create Other > Tree from the main menu.

Unity - Tree Creator

Unity – Tree Creator

You can also import the Tree Creator package from the Standard Assets that ship with Unity (select Assets > Import Package > Tree Creator from the main menu). This package gives you access to some textures that can be used to create your own trees in Unity. The image above shows the Big Tree tree asset that has been created with the Tree Creator tool.

To learn more about how to use the Tree Creator tool to build custom trees in Unity, please refer to the online documentation on the Unity website:
http://docs.unity3d.com/Documentation/Components/class-Tree.html

Grass

To add grass to the terrain, select the Paint Details tool in the Inspector.

Unity - Paint Details Tool

Unity – Paint Details Tool

Similar to the Paint Texture and the Place Trees tools, the Paint Details tool needs to have some detail objects to be defined.

Click the Edit Details… button and select Add Grass Texture in the pop-up menu that appears.

Unity - Add Grass Texture

Unity – Add Grass Texture

The Add Grass Texture dialog has the following properties:

  • Detail Texture: The texture to be used for the grass.
  • Min Width: Minimum width of each grass section in world units.
  • Max Width: Maximum width of each grass section in world units.
  • Min Height: Minimum height of each grass section in world units.
  • Max Height: Maximum height of each grass section in world units.
  • Noise Spread: The size of noise-generated clusters of grass. Lower numbers mean less noise.
  • Healthy Color: The color of healthy grass, prominent in the center of Noise Spread clusters.
  • Dry Color: Color of dry grass, prominent on the outer edges of Noise Spread clusters.
  • Billboard: If checked, this grass will always be rotated to face the current Camera.

Click the target icon next to the Detail Texture property and select a grass texture in the Select Texture2D dialog that appears.

Unity - Select Grass Texture

Unity – Select Grass Texture

The Terrain Assets in the Standard Packages contains a couple grass textures that can be used on your terrain. Add the grass textures to your terrain and paint some areas with grass.

Unity - Grass Detail Textures

Unity – Grass Detail Textures

Detail Meshes

Similar to grass, detail meshes are added to the terrain using the Paint Details tool.

Unity - Paint Details Tool

Unity – Paint Details Tool

To add a detail mesh to the terrain, click the Edit Details… button in the inspector and select Add Detail Mesh from the pop-up menu that appears.

Unity - Add Detail Mesh

Unity – Add Detail Mesh

The Add Detail Mesh dialog has the following properties:

  • Detail: The mesh to be used for the detail.
  • Noise Spread: The size of noise-generated clusters of the Detail. Lower numbers mean less noise.
  • Random Width: Limit for the amount of width variance between all detail objects.
  • Random Height: Limit for the amount of height variance between all detail objects.
  • Healthy Color: Color of healthy detail objects, prominent in the center of Noise Spread clusters.
  • Dry Color: Color of dry detail objects, prominent on the outer edges of Noise Spread clusters.
  • Render Mode: Select whether this type of detail object will be lit using Grass lighting or normal Vertex lighting. Detail objects like rocks should use VertexLit. Detail mesh that should be effected by wind (such as bushes) should be set to Grass.

The Terrain Assets in the Standard Assets package does not include any meshes that can be used as detail meshes for the terrain, but you can download the Terrain Assets package that is provided on the Unity website contains some bushes and rocks that can be used to add some detail to your terrain. You can download the Terrain Assets package here:
http://unity3d.com/support/resources/assets/terrain-assets

Add some detail meshes to your terrain.

Unity - Detail Meshes

Unity – Detail Meshes

If you make changes to detail meshes outside of Unity, the meshes are not automatically updated in your terrain. To tell unity to apply the changes you made to a detail mesh, select Terrain > Refresh Tree and Detail Prototypes from the main menu.

Terrain Settings

The last button on the Terrain toolbar is the Terrain Settings.

Unity - Terrain Setttings

Unity – Terrain Setttings

The properties of the Terrain Settings are split into three categories; Base Terrain, Tree & Detail Objects, and Wind Settings.

Base Terrain

The Base Terrain settings affect the terrain mesh itself.

  • Pixel Error: This property controls the amount of allowable errors in the rendering of the Terrain geometry. Parts of the terrain that are far away from the viewer use less vertices to render. As the viewer approaches the terrain, the terrain will refine to a more detailed mesh. This is a rendering optimization. The Pixel Error controls how much error can occur then the terrain is far away. Higher values can result in better performance but more error.
    Unity - Pixel Error

    Unity – Pixel Error

  • Base Map Dist.: The distance that Terrain textures will be displayed in high-resolution. After this distance, a low-resolution composite texture will be displayed.
  • Cast Shadows: Should the terrain cast shadows?

Tree & Detail Objects

The Tree & Detail Objects settings effect the rendering of the Trees, detail grass, and detail meshes.

  • Draw: Used to toggle the rendering of trees, grass, and detail meshes.
  • Detail Distance: The distance from the camera that details will stop being rendered. The higher this value is, the farther away details will be visible. Raising this value could have a negative impact on rendering performance.
  • Tree Distance: The distance from the camera that trees will stop being rendered. The higher this value is, the farther away trees will be visible.
  • Billboard Start: The distance from the camera that trees will start appearing as Billboards instead of Meshes.
  • Fade Length: The total distance delta that trees will use to transition from Billboard orientation to Mesh orientation.
  • Max Mesh Trees: The maximum number of trees that will be drawn as Meshes.

Wind Settings

The Wind Settings control how wind is simulated over the trees and grass on the terrain.

  • Speed: The speed that wind blows through grass. Setting this value to 0 will top the wind from blowing, but the grass may appear bent in some areas.
  • Size: The areas of grass that are affected by wind all at once.
  • Bending: The amount that grass will bend due to wind. Setting this value to 0 will prevent the grass from bending. Setting this value too high may cause the grass to bend too much and will produce unnatural results.
  • Grass Tint: As the wind blows over the grass, at tint can be applied to the grass and detail meshes. This gives the wind effect a bit more realism but this color should be subtle otherwise it will look unnatural.

Lightmapping

For better performance, terrains can be lightmapped just like any other static object in the scene. For terrains to be considered for lightmpping, they must be marked as Lightmap Static (by default, this should already be the case for terrains).

Lightmapping was covered in a previous article titled Rendering and Special Effects in Unity.

Conclusion

Unity provides a very powerful terrain engine with a lot of features such as grass, trees, and detail meshes. But even with all of these features, it is very difficult to get exactly the result you want. You must take care not to create too much detail near where the player can walk because colliders for detail meshes need to be created by hand.

But with enough skill and effort, it is possible to create very appealing terrains in a short time.

References

Unity Terrain Engine Guide (http://docs.unity3d.com/Documentation/Components/script-Terrain.html)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>