Previously, we discussed how to create a terrain object– short, that is, in comparison to the time it’d take us to develop our own terrain system from scratch. The landscape created so far, as well as the workflow used to create it, together offer an insight into the awesome potential of Unity as a game engine as well as, more generally, a time-saving tool.
The problem with our terrain at this stage is not its shape, form or texturing but rather its sparseness, its lack of set-dressing or props. The terrain is desolate: it has none of the hallmarks of a real-life terrain – no trees or grass or vegetation, no flourishing verdant life of any kind. It’s time for us now to fix that issue by adding trees and grass; and we’ll also add a first-person controller to the project so we can explore the terrain as a game character would be able to do. Consider the following steps.
1. Let’s add some trees to the level. The terrain package imported earlier in this chapter already contains a palm tree mesh that we can add to the terrain. This can be found in the Project panel under the folder Standard Assets > Trees Ambient-Occlusion > Palm. one way to add the palm tree would simply be dragging and dropping the tree mesh from the Project panel into the viewport and positioning it onto the terrain surface, just as we would with any other ‘regular’ mesh file. However, this approach would soon become problematic for at least two reasons. First, terrains typically feature many trees, and duplicating a tree mesh for each tree on the terrain would quickly become tedious work. Second, the terrain elevates and has contours, and we want each tree on the terrain to be positioned so that it matches the flow and elevation of the terrain: again, we could manually position each tree using the Transformation tools, but this too would be tedious work. The Terrain tools therefore offer a solution to this problem: they allow us to use the brush workflow again to paint the meshes onto the surface of the terrain. This means we can paint multiple meshes in one brush stroke and have all of those meshes conform to the elevation. To access these tools, select the Terrain mesh in the viewport, and press the Place Trees button from the object Inspector – it appears on the right-hand side of the Paint Texture button; see Figure 4.7 for more details.
2. Painting trees onto the terrain works much like painting textures. We must first add a set of tree brushes to the tree palette. once added, we select them from the palette and use the brush tool to paint them onto the terrain. To add the palm tree as a brush to the palette, click the Edit Trees button from the object Inspector and then choose the Add Tree option from the context menu. The Add Tree dialog appears in response. Click inside the Tree field and select the Palm Tree mesh from the Gameobject browser to select the palm as the mesh to be added. Then click the Add button and the selected palm tree object is added as a brush to the tree palette.
3. Now let’s paint the palm onto the terrain. Select the palm brush from the palette and the associated brush settings for the brush appear beneath in the object Inspector. The Brush Size option controls the radius of the brush used to paint the tree meshes, and the Tree Density setting controls how tightly packed together each tree mesh will be when painted onto the terrain using the brush. Notice also the three subsequent options for variation: color variation, height variation and width variation. These settings can be tweaked to define a margin or range of possible values for a tree so that, when the trees are painted, each tree painted by the brush will be given a unique and random height, width and colour selected from within that range. This is to add variation and deviance to the appearance of the trees, reducing the ‘sameness’ look that is often entailed by duplicated meshes. A value of 0 for any of the variation options will ensure that all trees share the same value for that setting – it specifies no variation. For this project I have set the brush size to 40, the tree density to 50, the colour variation to 0.8, the height variation to 25, and the width variation to 0 to ensure all trees have the same width. once set, paint the trees onto the terrain as though the brush tool were painting textures – click to stamp the trees onto the terrain. Notice how the meshes are automatically positioned to rest on the terrain surface according to its levels of elevation and also the variation in detail among the trees as specified by the variation settings. Continue adding trees to the level. Consider Figure 4.8.
NOTE: At this stage, why not add a first-person controller to the scene to explore the terrain close up and in first-person mode? The instructions for doing this can be found in Chapter 1, where a first-person controller was added to our scene for exploring a room from first-person perspective. Testing the scene with the first-person controller is the point at which the true vastness of the Terrain mesh becomes apparent. Be careful therefore in future terrain projects to scale your terrain appropriately for your needs. If your game will be a first-person game, then I recommend adding a first-person controller before creating the terrain to act as a point of reference for sizing the Terrain mesh.
4. The next step involves adding grass to the terrain, continuing with the brush-and-painting technique that we have now grown used to for adding details to the terrain. Details such as blades of grass, leaves, bushes, shrubs, flowers and other terrain minutiae are typically added as billboard objects rather than as standard meshes for performance reasons. The billboard is a special rectangular mesh (a quad) that is textured with an image (such as a blade of grass) and then configured so as to a rotate to directly face the camera wherever the camera is looking. Billboards are therefore a computationally inexpensive way to present numerous tiny but important details to the user, and they enhance the realism of the scene when used properly. To add grass to the scene as billboards, select the Terrain mesh in the viewport and click the Paint Details button from the object Inspector – the penultimate button in the Terrain component. Then click the Edit Details button and select Add Grass Texture from the context menu. See Figure 4.9.
5. The Add Grass Texture dialog displays and allows us to select a grass texture from the project assets. This texture will be applied to the billboard objects that we will paint onto the terrain using the standard brush workflow that we employed before. This time we will not be painting a mesh of our choosing – such as a tree mesh – but billboard or quad objects textured with a grass texture. Click in the Detail Texture field and select a grass texture from among the assets included in the Terrain Package; specifically Grass2 (located inside Standard Assets > Terrain Assets > Terrain Grass). See that the dialog also contains settings for adding variation to the appearance of the texture as applied to each billboard mesh. The Min and Max settings for width and height control the range of possible values, out of which one will be randomly selected for the width and height of each painted quad or billboard. Further, the Healthy Color and Dry Color settings specify colours that are multiplied with the grass texture on a per billboard basis to colorize each billboard differently, again adding variation across billboards. Accept the default settings of this dialog and click the Add button to add the grass texture as a billboard brush to the Details palette in the object Inspector.
6. Select the Grass Texture brush from the palette and begin painting grass into the viewport. on doing so, you may see one of two results: you may see grass being painted onto the terrain (if the viewport camera is close to the terrain surface), or you may see no immediate effect at all (if the viewport camera is zoomed out and away from the terrain). You should find that the visibility of the painted grass changes from visible to non-visible depending on the distance between terrain and camera. This feature is active as a performance enhancer to prevent the camera rendering unnecessary distant detail, such as grass and flowers. The distance itself can be controlled by the Detail Distance setting on the final tab or mode of the Terrain component in the object Inspector, Terrain Settings. By default the Detail Distance slider is set to 80 world units, meaning that as the camera moves more than 80 units away from the terrain, the grass becomes hidden in the viewport (note – this does not mean that the grass will be invisible to the player during the game!). The visibility of the grass to the player also depends on the player’s distance from the grass. ensure the grass can be seen in the viewport, either by moving the camera closer to the terrain surface or increasing the Detail Distance slider in the Terrain Settings. Then continue the painting process.
7. The density of the grass within the radius of the brush is controlled by the brush opacity settings: increase opacity to densely pack the grass billboards together and decrease opacity to thin-out or reduce the density. Settle on a density that works for your scene; here, I have chosen 0.15. Continue adding grass to produce a scene resembling Figure 4.10. Take the scene for a test run using the firstperson controller, and see how the grass is automatically configured to sway and bend with the wind.
Excerpt from Unity 4 Fundamentals by Alan Thorn © 2014 Taylor & Francis Group. All Rights Reserved.