Stylized Grass Shader Documentation

Stylized Grass Shader

Stylized Grass Shader
Author: jonathan
Version: 1.1.0
Dated: 31 Aug 2019

4.Placing grass #

There are several methods to populate a world with grass in Unity, this section outlines them and their (dis)advantages. The package does not contain any tools to achieve this, as it focused on providing a grass shader. Such tools and systems should be considered entirely separate, since they’re often quite extensive (and should be).

While the shader has been written to be as efficient as possible, the method used for rendering grass meshes will greatly determine how and where performance will be affected.

In the Assets/Stylized Grass Shader/Prefabs folder you’ll find several pre-made prefabs, ready to use with any of the techniques described below.

Unity terrain vegetation system

This is arguably the most accessible method, and does not require any third-party tools. However, it is important to understand that the grass/detail object system does not support meshes with custom shaders. To get around this, add a grass prefab as a “tree” instead. This way you can paint grass using the terrain tools.

If you’re using navmeshes then this method is not advised, since trees placed through the terrain system are automatically marked as navmesh obstacles. Meaning that navmesh agents won’t able to navigate through grass (placed as trees). Though, using Unity’s “Navmesh Components” system gets around this issue entirely.

The Unity vegetation system hasn’t been updated in a nearly a decade, and is no longer up to modern standards. It won’t deliver high performance when grass is placed in large amounts. For example, 20.000 grass instances on a 1x1km terrain in a test project took up ~25ms of CPU/GPU time, meaning a steady 60 fps framerate was no longer possible. Using this system, grass has to be placed sparingly.

There is currently a bug in the URP that causes shadows flicker when using either the Distance Fading or Perspective Correction feature, in conjunction with the terrain vegetation system. A bug report has already been filed. If this affects you, please place a vote, which will bump up its priority: https://issuetracker.unity3d.com/product/unity/issues/guid/1215610

Pros:

  • No need for 3rd party tools

Cons:

  • Not optimized for dense grass
  • Grass can’t align to terrain surface and will stick out on hills/slopes.
  • Used by tools like MapMagic and Gaia.
  • Cannot be used with navmeshes
  • No options for random scale, rotation or density
  • Changes to materials won’t apply unless the “tree” prefab is refreshed

Placing grass as GameObjects

By dragging a grass prefab into the scene you’ll have full control over it, just like any other object. It goes without saying that placing a piece grass one by one is not going to be fun. For this reason you can use any prefab painting tool, such as PolyBrush.

Note that populating a scene with a large amount of individual GameObjects will increase CPU load, scene file size and loading times. So the amount of grass you can place is limited by how much performance legroom your project already has in this area. The shader uses GPU Instancing, so drawcalls in any case will fortunately be limited.

Pros:

  • Prefab painters usually apply random scale/rotations.
  • Easy to manage to like any other GameObject
  • Material changes are directly visible
  • Meshes can be used with Occlusion Culling

Cons:

  • Requires a third-party painting tool (free or paid)
  • Has to be manually placed or painted
  • Requires CPU resources for maintaining the individual GameObjects

Nature Renderer

Nature Renderer uses the terrain’s tree and details data, but renders everything using its own system. It allows you to use the terrain vegetation system as you would normally, but get all the modern rendering methods required for high amounts of vegetation (GPU instancing, cell-based and frustrum culling). As a result, this would work with other systems that spawn grass on terrain such as Gaia or Map Magic.

Pros:

  • No change in workflow (grass prefabs can be used with the terrain detail system)
  • Renders large amounts of grass at relatively fast speed
  • Material changes are directly visible
  • Aligns grass to terrain surface
  • Stylized Grass supports its wind controller and “Procedural Instancing” system.

Cons:

  • Requires an additional purchase

Vegetation Studio + Pro

This would be the silver bullet. Vegetation Studio is a system that utilizes GPU Instancing to draw identical meshes in only a few draw calls. It also performs frustum culling, to only render what’s visible in the camera field. It does not use any GameObjects, the grass is purely stored in memory. Additionally, it provides a powerful rule-based system for spawning grass.

The grass shader is compatible with its Instanced Indirect rendering, which enables more than 1023 items to render 1 draw call, next to GPU-powered culling.

Pros:

  • Renders large amounts of grass at relatively fast speed
  • Procedural rule-based placement of grass (in addition granular manual control)
  • Fast control over density and scale/rotation/alignment
  • Support for placement on meshes
  • Integrated material controller when selecting a grass item

Cons:

  • Requires an additional purchase

Tips and tricks

  • Individual grass objects should be rotated randomly on the Y axis between 0-180º. This greatly reduces visual repetition.
  • Scaling the objects randomly between 80-120% is also advised for the same reason.
  • Align the grass to the surface it is placed on. This will help with translating lighting, especially on hills.
  • Disabling shadow casting for the grass will greatly improve performance, consider going in that direction as a style choice.

Note: uNature is not compatible, as this only works with their own shaders.

If you’re the developer of a grass rendering/placement system and would like to validate compatibility, please get in touch!

Yes No Suggest edit
Last updated on September 10, 2020
7 of 7 users found this section helpful
Suggest Edit