/Unity-Grass-Instancer

Unity C# script and shader for GPU instanced grass

Primary LanguageC#MIT LicenseMIT

Unity Grass Instancer

Contains a C# script and a shader for GPU instanced grass (or any other mesh)

Made with Unity 2020.3.4f1 and HDRP 10.4.0

Alt text

Video showcase: https://www.youtube.com/watch?v=pJSKUQJqBUs

Resources

How to use

  • Drag the script and the shader into your project
  • Create an empty transform and attach the script
  • Adjust the settings
  • Add a spriterenderer
    • with this simple trick we can get some frustum culling for free and don't have to implement it ourselves. Not the best solution but it works alright.
  • Create a material with the shader (or your own shader)

Example settings

Alt text

Properties explained

  • Draw Gizmos - Will draw a bounding box inside OnDrawGizmos() for debugging
  • Batch Size - The size you want your batches to be. Unity's max is 1023
  • Instances - The amount of grass blades or other meshes
  • Range - Range of the bounding box where rays for mesh positions will be tested
  • Scale min/max - You can scale your meshes randomly between a min and max value
  • Steepness - Limit the steepness your meshes can be spawned on
  • Rotate to ground normal - Rotates the mesh towards the ground
  • Random Y axis rotation - Rotates the mesh randomly on the Y axis between a negative and positive value
  • Max Y rotation - Max random Y rotation
  • Recieve shadows - Depends on your material
  • Terrain/Ground layer - You can define layers where your meshes can be spawned
  • Material - The material you want your meshes to have
  • Meshes array
    • Set the mesh and shadow option for each LOD individually

Behind the scenes

The script will create a box where it shoots down raycasts to detect possible mesh positions Alt text

I got the best results with overlapping volumes Alt text Alt text

Known issues

The grass does not always bend in the correct wind direction inside the shader Alt text

My approach on grass rendering isn't that optimized. You might want to have a look at Acerola's channel, he has also uploaded his code to Github: https://www.youtube.com/c/Acerola_t

Material config

Be sure to check GPU instancing on your material or it can't be instanced

Alt text

Shader info

For those of you who don't want to use shadergraph, here are some screenshots of how the shader works. With this you can basically rebuild it with plain code.

Fragment shader Alt text

Vertex shader part 1 Alt text

Vertex shader part 2 Alt text