Reader small image

You're reading from  Unity Cookbook - Fifth Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805123026
Edition5th Edition
Languages
Tools
Right arrow
Authors (3):
Shaun Ferns
Shaun Ferns
author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

Sinéad Murphy
Sinéad Murphy
author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy

View More author details
Right arrow

Creating 3D Environments with Terrains

3D games need 3D objects in their scenes! In this chapter, we will explore the powerful Unity terrain tools to create landscapes with a range of characteristics. Through texture and height painting, and the addition of trees and details such as grass and plants, complete environments can be modeled in a Unity scene. We’ll also explore how to use the new visually realistic water modeling features available for High Definition Render Pipeline (HDRP) projects.

In this chapter, we will cover the following recipes:

  • Creating and texture-painting terrains
  • Unity terrain tools and samples for powerful height-painting
  • Adding terrain holes
  • Adding trees and vegetation
  • Realistic water features for HDRP projects

Creating and texture-painting terrains

In this recipe, we will introduce terrains by creating, sizing, positioning, and texture-painting (with an image) a flat terrain. Later recipes will enhance this terrain with heights, trees, and other details.

Figure 6.1: Our flat terrain texture painted with sand and grass around a cube

Getting ready

You’ll need at least two different images to texture-paint the terrain in this recipe. In the folder 06_01, we have provided two free terrain textures (SandAlbedo and GrassHillAlbedo .PSD Photoshop asset files) from the legacy Unity sample assets.

How to do it...

To create a terrain, follow these steps:

  1. Create a new 3D project, and import the two terrain textures.
  2. Add a cube to the scene by choosing the menu: GameObject | 3D Object | Cube. In the Inspector panel, set its Position to (0, 0, 0).
  3. Add a terrain to the scene by choosing the menu: GameObject | 3D Object | Terrain. In the Inspector...

Unity terrain tools and samples for powerful height-painting

Using a combination of textures and terrains with variations in height enables sophisticated and realistic physical environments to be created. In this recipe, we’ll build on the previous texture-painted terrain by varying the height to create an interesting landscape in our scene. To make things a bit easier, we’ll import the terrain samples, which provide a range of useful textures and height brushes for common terrain features.

Figure 6.6: We’ll create a circle hill, bordered by mountains, painted with sand and grass, with a canyon entrance

Getting ready

This recipe builds on the previous one, so make a copy of that and work on this copy.

How to do it...

To use Unity terrain tools and samples for powerful height-painting, follow these steps:

  1. Open your copy of the Unity project from the previous recipe.
  2. Open the Asset Store, ensure you are logged in to your...

Adding terrain holes

A limitation of terrains is that they are a single mesh, with points raised or lowered. Sometimes, we want more complex, concave geometry for effects such as caves, tunnels, or just simple holes in the terrain. In this recipe, we’ll build on the previous one to add a terrain hole that leads to another piece of terrain.

Getting ready

This recipe builds on the previous one, so make a copy of that and work from it.

How to do it...

To paint and make use of a hole in a terrain, follow these steps:

  1. Open your copy of the Unity project from the previous recipe.
  2. Add a spotlight that shines on where the cube is sitting by choosing menu: GameObject. In the Inspector panel, set its Position to (0, 0.5, 0), Range to 50, Spot Angle to 125, and Intensity to 2. This will allow this light to light up a bonus level that we’ll create beneath our terrain.
  3. Delete the cube from the scene – in a full game, this cube might be...

Adding trees and vegetation

As well as textures and heights, Unity provides tools to make it easy to “paint” trees and vegetation models onto a terrain. While, theoretically, you could just create tens, hundreds, or thousands of GameObjects from a tree or grass 3D model, animating them using a grass shader, it’s much easier and faster to use the tree and vegetation detail painting tools for terrains. In this recipe, we’ll work with two models; one is a model for a heather bush (since, for some reason, Unity does not provide any tree models with the Terrain Sample Assets), and the second model is of a kind of grass.

Figure 6.19: The flat entrance to our circle hill detail, painted with grass and plants

Getting ready

This recipe builds on the previous one, so make a copy of that and work with this copy.

How to do it…

To add trees and vegetation, follow these steps:

  1. Open your copy of the Unity project from the previous...

Realistic water features for HDRP projects

Some photorealistic and real-time animated visual effects are only possible when working with HDRP projects. Recent versions of the Unity Editor have added some fantastic features for animated, realistic water effects, which we’ll explore with this HDRP project recipe.

Figure 6.23: Our swimming pool scene with real-time reflected sunlight

How to do it...

To create realistic water features for HDRP projects, perform the following steps:

  1. Create a new 3D (HDRP) project.
  2. Open the OutdoorsScene scene.
  3. Open the Project Settings panel by choosing the menu Edit | Project Settings. Select the Quality | HDRP tab, and locate the Water section under Rendering in the settings. Check the Enable box and wait a few seconds. You have now enabled water rendering for the HDRP quality settings.

Figure 6.24: Enabling water rendering in the Quality | HDRP section of Project Settings

  1. Next,...

Further reading

This section contains some sources that provide more information about the topics that were covered in this chapter.

Every game needs textures and materials. Here are some sources of free textures that are suitable for many games:

A great collection of free-to-use (Creative Commons-licensed) materials can be found at AmbientCG: https://ambientcg.com/.

To learn more about Unity terrains, take a look at the following links (although a couple are a few years old, they’re still very useful):

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Cookbook - Fifth Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781805123026
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy