Adding Grass
Learn the tips and tricks how to create an animated grass, swaying and moving as if with the wind. Your grass will also realistically vary in color across the field, so that it does not look unnatural like a freshly-painted lawn. More important, you will keep the framerate high when rendering all these polygons. No matter how big your grass field should be (the whole terrain? no problem!), the grass appears only around the camera. Basically, you need to tweak only two parameters to achieve optimum performance (if you can't wait, these are Density and Step).
As not to prolong this any further, here is a first tip: the grass looks better upon a hilly terrain. As for planes, low and non-dense grass would be a reasonable choice. Go ahead and be armed with knowledge!
Required Textures
The textures needed to set up the grass are the following:
- Diffuse texture. It should contain 4 slots in a row and any number of such rows (here, for example, it is two rows). In our case, it will be colorless to use spatial coloring.
- Spatial noise texture (RGB). It will cover the whole terrain and allows to color the grass differently in all locations. This texture is generated based on the terrain's diffuse texture colors. However, it does not have the same size as the terrain. For example, for the 8193 × 8193 terrain, 2048 × 2048 texture is enough.
- Mask texture (RGBA8) that determines spreading of the grass across the terrain. This texture is generated based on the terrain's height map. For example, for the 8193 × 8193 terrain, the reasonable size could be 1024 × 1024.
Each channel of the mask (R, G, B and A) control if the grass from the 1st, 2nd, 3rd and 4th columns (respectively) is rendered or not. The grass will grow only across areas with a channel color value (here, the grass from all columns will be rendered where white color is):This texture is not included into the SDK, so you have to create it yourself.
Required Settings
You may need to increase the precision of the depth buffer. Otherwise, flickering of the grass in the distance may occur.
Press T to go to the Tools settings -> Camera tab. Increase the Near clipping up to 0.3.
Step 1. Add Grass
- On the editor panel, click Add an object button, and then Grass in the rollout. Drop the grass somewhere in the world:
- Press N to open Nodes settings. Rename the grass, if necessary.
- First of all, go to the Surfaces tab. Here, we will define the area of visibility for our future grass field. All the grass blades cannot be rendered all at once, especially, if the field is huge. Instead, they are going to be rendered in a small area around the camera.
Set the Maximum Visible distance to 4 units. Within this distance from the camera all grass blades are visible.
- Set the Maximum Fade distance to 90 units. Over this distance the grass will gradually fade out according to the alpha channel value of its diffuse texture.
- Now we can safely set the real size of the grass field.
Go to the Grass tab and set Size X (along the X axis) and Size Y (along the Y axis) to repeat the size of our 8193 × 8193 terrain. - For now, increase Density up to 1.1, so that the grass is generated.
We see a corner of our grass field appear and fade out up to the set distance (94 units). - Make the grass a child of the terrain, so it can grow upon it repeating its relief: press ALT and drag the grass node to the terrain one.
- Check Intersection option to enable intersection with a parent node. Now the grass grows upon the terrain, but our field is obviously not positioned properly:
- Go to the Node tab and reset coordinates of the occluder to 0 relative its parent. Its position becomes the synchronized with the terrain:
Step 2. Set Up Grass Color
It's time to create material for the grass before tweaking its spread any further.
- Press M to open Materials settings. For further actions, you should already have a material library associated with the world (see how to add a new material library, steps 1-5).
- Select a basic (and therefore unchangeable) grass_base material in the list and click Inherit the material.
- Choose the world-associated material library where to save a new material and specify its name.
- Click Assign material to apply it to the currently selected node — the grass, in our case.
- Let's adjust the material for our grass. Go to the Textures tab and load the diffuse texture.
You may want to manually edit the alpha channel of the mipmaps, because on automatically generated mipmaps thin blades may disappear incorrectly or unpleasantly.
- Select a path to the diffuse texture. You can use the following texture form the SDK:
<Unigine SDK>\data\demos\heaven\textures\environment\grass_regular_d.dds The diffuse texture is now loaded, but the number of vertical columns is wrong: - Go to Nodes settings -> Grass tab. Set the proper number of horizontal rows in diffuse texture in Num textures field. For example, we only have 1. As you can see, our diffuse texture is colorless:
- Go back to the Materials settings -> States tab. Check Color noise option. It will allow to color our grass blades differently across the whole field.
- Go to the Textures tab and load the Spatial noise texture.
- Select path to the color noise texture. You can use the following texture form the SDK:
<Unigine SDK>\data\demos\heaven\textures\environment\grass_irregularity_d.dds The grass coloring appears to be off: - Go to the Parameters tab. Double-click on the Noise Transform field. (Or you can modify the expression right in this field).
- Our color noise texture should cover the entire terrain. Set the scale by X and Y axes to 1. You can see patches of dark green, light green and yellow:
- The colors are too vivid to match the terrain ones. Decrease the Scale down to 0.35 to dim the color noise color.
- The blades are quite thin, so let's make them a little bit more transparent to light when it shines from behind.
Increase the Translucency up to 0.6. The grass will become more transparent and lighter: - Increase the Diffuse to 1.1 to brighten diffuse coloring of the grass.
- Go to the Common tab and disable shadows from the grass:
- Uncheck the Cast world shadow box. After that, the grass will not cast shadows from world lights.
- Uncheck also the Cast shadow box to disable shadows from other types of light sources: omni and projected ones.
Step 2. Set Up Grass Density and Spread
First of all, let's set up the density of the grass.
- Go to the Nodes settings -> Grass tab and increase the Step up to 15. It make the cells which are used to render the grass field larger, but not so large as to hit the performance when it is created. The grass is regenerated. The density also changed, because it is defined for one cell. As we made the cell bigger, it the grass turned out to be slightly more sparse.
- Increase the Density of the grass in a cell up to 2.5 to add more grass blades on the field.
- It's time to tweak the spread of the grass. Click to load a Mask image that will specify the areas where the grass grows.
- Select path to your mask texture, if any. The grass no longer grows on mountains peaks and stony areas:
- Decrease the Threshold down to 0.3 to lower the mask intensity. This will increase the area that the grass covers:
- Usually the grass does not grow on the steep hillsides. Increase the Angle up to 0.7 to make steep areas grassless. Now the spread of the grass is more realistic:
Step 4. Set Up Grass Shape
- When changing from bare areas to grassy ones, our grass starts to grow as small and thinned out. But on the stones it does not look too good: Increase the Min height up to 1.2. In areas with low density (according to the mask) the grass will be higher. You can set different height values for each of four columns in the diffuse texture, or leave it the same for all.
- Increase the Max height up to 1.2. of the grass blades up to 1.5 units. In areas with high density (according to the mask) the grass becomes higher. We'll use the same height for all four columns in the diffuse texture as well.
- We have set the mean value of the grass height. It is an average height that is randomized. As a result, higher and lower grass blades are randomly planted. However, the difference between the highest and the lowest is pretty big.
Decrease Spread for Max height and Max height down to 0.1 units. It controls how blades can possibly vary in height. In our case, it will make the grass on the field more uniform in height.
- Check the Variation box, so that the grass blades from diffuse texture are flipped horizontally at random.
- From above the grass seems too flat: Go to the Materials settings -> Parameters tab. Increase the Slope up to 0.7 to make it more billboard-like.
- Grass blades are too thin and sometimes appear in dashed lines: Increase the Alpha up to 2 to make the blades look thicker and the grass denser.
Step 5. Set Up Grass Animation
- For now, set the Stem noise to 0 to make all the grass blades move uniformly. It will help to learn how to adjust the animation.
- Decrease the Stem offset down to 0.1 to make the grass sway and stretch horizontally to a lesser degree.
- Decrease the Stem radius down to 0.1 so that the grass does not bend down to the ground.
- Increase the Stem scale up to 2 to make the grass move two times faster.
- At last, increase the Stem noise up to 2 to make the blades movement more random.