Stylized Grass Shader Documentation

Stylized Grass Shader

Stylized Grass Shader
Author: jonathan
Version: 1.1.9
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. Such tools and systems should be considered entirely separate, since they’re often quite complex in their own right. The reality today is that Unity does not have the proper tools needed for AAA vegetation rendering.

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 any event, I strongly recommend to learn and use the Profiler and Frame Debugger tools to get an insight into how rendering is performed in your project. This is the only correct way to gauge where rendering optimization is needed.

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. It’s designed to use the built-in grass shader. To get around this, add a grass prefab as a “tree” instead. This way you can paint grass using the terrain tools.

Pre-emptive note: It seems that starting from Unity 2021.2.0, the terrain will finally support custom meshes/shaders for the terrain grass system.

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 instead doesn’t have this limitation.

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. Using this system, grass has to be placed sparingly, regardless if using this grass shader or not.

In URP 7.5.1 and older, there’s a bug in that causes shadows flicker when using either the Distance Fading or Perspective Correction feature, in conjunction with the terrain vegetation system. This has meanwhile been fixed in URP 8.1.0+

Pros:

Cons:

  • Not optimized for dense grass or large amounts
  • Grass can’t align to terrain surface and will stick out on hills/slopes.
  • 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 an option. 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, culling overhead, and loading times. So the amount of grass you can place is limited by how much performance legroom your project already has in these area. The shader uses GPU Instancing, so draw calls in any case will fortunately be limited: one for depth, lighting and one per shadow cascade.

This method is definitely not recommended for large terrains, spanning open fields of dense grass.

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:

  • 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, Map Magic or Vegetation Spawner.

The grass shader is compatible with its “Procedural Instancing” technology. See the third party integrations page for more details.

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:

  • Only renders on terrains

Vegetation Studio + Pro

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 and rendered through a custom render loop. 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 in 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:

  • LOD crossfading will not work (though available in the latest v1.4 Pro beta version)

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 reduce draw calls, consider going in that direction as a style choice. Contact/Raytraced Shadows are an excellent alternative, but currently aren’t available in URP.
  • Unity’s SSAO will always look blurry on small objects such as grass. I recommend to use Horizon Based Ambient Occlusion asset for far better results.
Yes No Suggest edit
Last updated on July 7, 2021
24 of 24 users found this section helpful
Suggest Edit