Stylized Grass Shader Documentation

Stylized Grass Shader

Stylized Grass Shader
Version: 1.4.2
Review

4.Placing grass #

There are several methods to populate a world with grass in Unity, this section outlines them and their (dis)advantages.

This asset is a grass shader, hence 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 native 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. The Unity manual has a section that covers this native functionality.

Unity 2021.2+

The terrain system now supports using custom meshes/material for grass. Except LODs aren’t supported.

Because of this, you’re required to create a grass prefab that just uses a single Mesh Renderer. To do so, open the grass prefab of your choice, and drag the “_LOD0” object into the Project window to create a new prefab.

In the Prefabs/TerrainFlowers & TerrainGrass you’ll find prefabs that have been altered in this way. Only these can be used with the terrain! (see Unity manual)

Unity 2021.1 and older

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 custom grass using the terrain tools.

The Unity vegetation system won’t deliver high performance when grass (as trees) is placed in large amounts. Using this method, grass has to be placed sparingly, regardless if using this grass shader or not.

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 “AI Navigation” system instead doesn’t have this limitation, it effectively offers an improved way of baking navmeshes (as volumes, no longer tied to a single scene).

Pros:

Cons:

  • No support for LOD
  • Not optimized for dense grass or large amounts (inherent to the internal tech)
  • Compatibility with GI is questionable (grass instances aren’t actual renderer objects, so virtually don’t exist to other systems)
  • (Unity 2022.1 and older) Grass can’t cast shadows
  • (Unity 2022.1 and older) Grass can’t align to terrain surface and will stick out on hills/slopes.
  • (Unity 2022.1 and older) No options for random rotation.

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, camera culling overhead, and loading times. It’s not realistic to have an environment with 50k GameObjects and expect to hit all of your performance targets. So the amount of grass you can place is limited by how much performance legroom your project already has in these areas.

The shader uses GPU Instancing or the SRP Batcher, 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 and Lightmapping
  • LOD Crossfading (animated)

Cons:

  • Has to be manually placed or painted
  • Requires CPU resources for culling and batching the individual Mesh Renderers (per camera)

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)
  • Supports LODs and crossfading
  • Renders large amounts of grass at relatively fast speed
  • Material changes are directly visible
  • Aligns grass to terrain surface

Cons:

  • Only renders on terrains

Vegetation Studio + Pro

This asset has been deprecated, and can no longer be licensed.

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-accelerated 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
  • LOD Crossfading (distance based)

GPU Instancer

As the name imply, GPU Instancer is a system is a rendering framework that makes use of Unity’s instanced rendering API. It supports both the conversion of GameObjects and terrain vegetation data.

The grass shader is compatible with its Instanced Indirect rendering, which can be enabled at the bottom of the grass material UI.

[No pro’s/con’s are known, as I personally have no production experience using this tool]

    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, which is inherent to the technique. I recommend to use the Horizon Based Ambient Occlusion asset for far better results.
    Yes No Suggest edit
    Last updated on June 26, 2023
    51 of 51 users found this section helpful
    Suggest Edit