Stylized Grass Shader
6.Using grass bending
The shader is paired with a system to flatten and/or bend the grass through meshes, trails or particle effects. This can be previewed in the “BendingDemo” scene.
1. Renderer component
Create an empty GameObject and go to Add Component -> Stylized Grass -> Stylized Grass Renderer.
With the component selected, you’ll notice a white box around the scene-view camera. This represents the area in which bending is processed.
Ideally, you’d leave the “Follow Camera” option enabled so bending is always processed around what’s currently visible on screen. This functionality also works with a split-screen set up, in affect it will align the render area to whichever camera is currently rendering something.
If disabled, the render area (shown as a white box in the scene view) can follow any given Transform through the “Follow target” field. This can also be set at runtime through the static
StylizedGrassRenderer.SetFollowTarget(Transform) function from other scripts.
2. Grass Benders
Bending can be performed through 3D shapes as represented by meshes, trails or particle effects. Their shape and position is translated to the shader on the GPU, so no actual physics calculations are being used. This is done through the “Grass Bender” component. It can be added to these types of renderers
- Mesh Renderer
- Trail Renderer
- Line Renderer
- Particle System
You can easily add a Grass Bender by opening the context menu on any Mesh Filter, Trail Renderer or Particle System component.
After which, a Grass Bender component will be added to the object
Move it around in scene-view, and you’ll see it taking effect on the grass placed in the scene.
You can create meshes, or trail renders as a child of your player object so it’ll follow it. It is recommended to create a sphere at the feet of the player, so grass always bends where it stands. Next to using a trail so the grass bends behind it when it moves. In any case, you can disable the renderer so the actual mesh isn’t visible to the camera, it’ll still be visible to the bending renderer.
In the Prefabs/Benders folder you can find several pre-configured prefabs for this purpose.
- For a explosion particle effect, create a sub-particle system using a sphere and roughly the same lifetime. When it fires, the grass will appear to bend away by the force of the explosion
- For ragdolls, create capsules for all the limbs and torso. When they are thrown to the ground, the grass will appear to be flattened by the body.
Particle effects must use 3D meshes instead of flat billboards. To do so, go to the Particle System’s “Renderer” section and set the ‘Render Mode’ to ‘Mesh’, next assign a mesh, such as a sphere. You can disable the Renderer component, so it’s invisible to the game camera.
With Unity’s TrailRenderer component. Trails retract at the same speed as they extend. Fast movement will cause the grass to also spring back rapidly. If a trail is not moving, its lifetime also doesn’t decrease.
The “BendTrailParticles” prefab is an alternative example of using a particle system to leave a trail of bent grass.