UnityEngineGrass Compute Shader2022-10

Procedural Grass System with Compute Shaders in Unity
Compute ShaderHLSLBuilt-in RPCgUnity
View on GitHub

Implementation

  • Building a procedural mesh for the Grass Blades, setting vertices, normals and indices.
  • Placing randomly generated grass blades given an extent and a density.
  • Raycasting over the terrain to place grass following the uneven surface.
  • Sharing a compute buffer with the grass blades data between the compute shader and the material.
  • Using GPU Instancing to render all the grass blades.
  • Parametrizing the wind direction, wind velocity and wind force.
  • Parametrizing colors for young and old grass.
  • Using DrawMeshInstancedIndirect to draw procedurally all the meshes using GPU Instancing.
  • Perlin noise to calculate the amount of displacement each grass blade needs to have to simulate wind.
  • Using Translation and Rotation Matrices to position the vertices of the mesh in World Space and Clip Space.
  • Displacing the vertices of the grass blade proportionally to the UV Y coordinate, to make the base stay still.
  • Configuring the grass blades to cast shadows and receive shadows.