shown in other instead.
The outdoor locations and bucolic landscapes frequently beg for the high-quality grass effect. It should organically blend into the scene, realistically sway in the wind, and present convincing illusion of natural variation with fine details, while not monopolizing the GPU.
- An Adding grass tutorial
- A grass_base material that is applied to grass
- An ObjectGrass class to edit grass via UnigineScript
- A set of samples located in the data/samples/objects/ directory:
Grass Blades Quads
A typical field of grass can easily have a few hundred thousand blades, every each of which is dissimilar to the others. Of course, reproducing such diversion is an impossible artistic challenge. The grass is drawn as a group of blades, that is rendered as quad. And as all this natural variation cannot be strictly set, it should be simulated by introducing the probabilistic factor. It grants the uniqueness and diversity of the grass characteristics by specifying the controlled degree of deviation of the resulting parameter.
From the point of view of realisation, some grass-related parameters are not specified by one value, but by two:
- Mean value (i.e. Scale, Aspect, Height, Offset) defines the average value. With its help the designer can set a setting milestones to control the parameter.
- Spread value defines the range for possible variation of the parameter. The higher the value, the more diverse the final result will be.
Spread value is optional: if set to 0, it will not influence the simulation process and only the mean value will be uniformly used.
After these values are specified, the following formula is used to calculate the final result for the parameter:
Result = Mean + Random * Spread
where Random is a random value in range from -1 to 1. This means, for each of the grass quad on the field the parameter will differ to the desired extent (see example with height below).
The height determines the height of the grass blades quad. It is set for each of the diffuse texture slots separately. If only the mean value is set, the height of all the grass blades quads throughout the field will be uniform, if the spread value is specified, it will be diverse.
To render the intended grass quad, besides the height, it should also have the width set. But in order to preserve the proportions more easily, the latter is not specified directly, but rather using the aspect parameter. So the final width is calculated by multiplying the height by the aspect.
Aspect is set for each of the diffuse texture slots separately. Like the former, this parameter can be randomly varied if spread value set in addition to mean one. For example, setting spread to 0, yields the following result:
- If mean value is set to 1, the grass will be rendered in square proportions, with height equal width.
- If mean value is set to 2, the width of the grass quad will be twice longer than its height.
Number of Texture Slots
On one single field, it may be necessary to simulate grass that had grown in good and bad conditions, shooting young grass blades and withered straw. The grass diffuse texture can have 4 x n slots, each containing a separate grass cluster, plant or a flower.
Num textures parameter allows to indicate how many grass clusters are lined up vertically in the diffuse texture. After specifying the number, each grass cluster is rendered as separate on the field.
To present additional diversity to the grass and avoid total uniformity of all the blades in the field (especially needed if it is represented as billboards), variation option can be enabled. On the randomly chosen quads, it flips the texture horizontally, automatically giving two variants available for rendering varied blades.
Size of the Grass Field
The grass can shoot as a few separate blades, or form a see of green verdure receding into horizon. The size of the field determines the extent of rectangular area, which will be covered with grass:
- Size X specifies the length of the grass area along the X axis.
- Size Y specifies the length of the grass area along the Y axis.
Step for Cell Division
However visually attractive the above mentioned grass expanse might be, rendering it as whole threatens severe performance penalty. To allow rendering at interactive frame rates, the field is broken down to cells forming a grid. Each cell is estimated, whether it contributes to the grass field or stays grassless. The grassless cells are disregarded, while for the cells with grass all the required computations are done. Depending on the density, the blades are planted in the cell with random coordinates, imitating the natural vegetation sprouting. The cells are rendered one by one, starting from the camera and further forward.
To get the resulting number of cells, size of the field (both along X and Y) is divided by the step. It brings the following:
- The higher the step value, the bigger the cells the field is divided into. But there exists a limit how many grass quads can be planted in each cell. So after reaching this limit, the number of grass quads can no longer be increased, though the density parameter specifies it.
- The smaller the step is, the smaller the cells the field is divided into. While the density value stays unchangeable (for each separate cell), the whole field becomes planted with grass much more densely.
Usually the Step value is set between 10 to 25.
Controlling the step for dividing field into cells allows to change the field size, while not influencing the set density for planting the grass blades. On the other hand, that is why if we change the step and consequently the number of cells, while not altering the density value, the number of grass quads rendered will be different.
Image Mask for the Grass Field
To simulate the convincingly looking pasture that naturally blends with the rest of the virtual environment, the grass field can take an arbitrary outline gradually shaping and growing on the ground. Furthermore, the grass can grow chaotically and irregularly forming clumps of green and grassless patches, just like in the nature. Making this effect possible, the mask is mapped on the terrain and determines the density distribution that will meet the requirement of the created landscape.
The mask is an RGBA8 texture. Each channel of it controls the distribution of the grass per vertical column of grass diffuse texture (there can be up to four columns in the diffuse texture).
- Red channel specifies the areas of growth for the 1st texture column. If there are several grass clusters in a vertical column, they will be randomly spread across the masked area.
- Green — for the 2nd texture column.
- Blue - for the 3rd texture column.
- Alpha - for the 4th texture column.
In case R8, RG8 or RGB8 texture is used as a mask, only the first, the first two or the first three columns of diffuse texture will be displayed.
For every channel separately, areas where the grass should not grow have 0 channel value (no color or alpha data). Non-zero channel value creates a grassy area: the higher the value, the denser the grass grows.
Threshold parameter controls starting from what density (according to the mask), the grass should grow. This means, that if in some particular place the threshold value is higher than the color or alpha value of the mask, it is rendered grassless.
- With the minimum value of 0, the grass is planted on the whole area, that is specified as available by the mask.
- With higher threshold value, the grass will grow only in the areas marked by the mask as dense. The areas of sparse distribution stay bare.
- With the maximum value of 1, there will be no grass at all.
Minimum and Maximum Mask Values
As a solution for advanced objects seeding and memory consumption optimization not only a whole mask, but any required part of it can be applied to the object. It means that different multiple objects can share the same mask, but use different levels. You need to set the color density range (from 0 to 255) and that part of mask, which contains the specified density range, will be applied to the object.
Vector masking allows not to depend on the mask texture resolution and create roads, rivers, etc. with extremely high precision. A mesh for masking should be a simple planar mesh. A mesh-based mask can be set for grass.
The Inverse flag toggles if the grass grows inside or outside the mesh contour.
Additional Shadow Radius
Radius is the distance to draw additional grass blades outside the view frustum. This option allows eliminating popping of shadows at the edges of the screen when the camera is turning.
- The shadow radius to set depends on the grass size and the angle of the sun (which determines how long the shadows from it would be).
- Set the minimum possible value, as this parameter increases rendering load (check RTriangles counter in the performance profiler).
Density of Growth
To achieve the sufficient richness of grass field, density parameter can be tweaked. It determines how many grass quads are to be rendered per square unit.
- The higher the value, the more grass blades are present and the more closely they are positioned. But it should be kept in mind, that the final result also depends on the step of division and the number of cells it defines.
Moving closer to the field edge or bare patches, the grass is not only thined out, but also stunts its growth. Scale allows to control, how quickly the grass blades decrease in size when passing from the highly dense areas to the grassless ones. This parameter is specified by the mean value and additional spread one, if variation is required.
- The smaller the resulting parameter value is, the more area the gradual decrease in grass size takes. On the field edge there stay only small and low blades.
- The higher the resulting parameter value is, the more abruptly the field begins. On the field edge the blades are almost of the same size, as height parameter specifies.
- With the minimum parameter value of 0, there will be no grass.
Intersection with Ground
By reproducing the hilly landscape, it is essential that the grass follows the relief it grows on. For the grass to be rendered upon the ground, the following is necessary:
- Make the grass a child of the terrain or any arbitrary mesh (static or dynamic one). If there are some nodes in the hierarchy between a terrain or mesh node and grass, intersections will still be checked.
- Check Intersection box.
The angle parameter allows to control grass distribution depending on the ground slope angle. This parameter simulates the natural tendency of the grass to strike root in more flat places.
- The minimum value of 0 means the grass grows everywhere on the ground.
- The bigger the value is, the flatter the place should be for the grass to grow. So, as the value is increased, steep slopes turn out to be bare at first, and then also the low-sloped places become grassless.
- The maximum value of 1 means the grass grows only on the strictly horizontal ground.
Sometimes to avoid visual artifacts, it is necessary to slightly raise the grass field above the ground. This parameter specifies the offset in units up the surface normal, thus accurately repeating its relief. It is set by mean value (only positive) and additional spread value (positive or negative), if variation is required.