Leafshaper Docs
View all packages

GPU rendering

GPU rendering

Draw forests of millions of trees on the GPU automatically or with a Forest Renderer, and draw grass, rocks and other prefabs next to them.

Leafshaper 26.12.0Unity 6000.0+URP · HDRP · Built-in

The Forest Showcase ridge, with the forest culled and drawn on the GPU

Leafshaper includes a GPU renderer that culls every tree, plant and prop on the GPU and draws each mesh level with one indirect call, so forests of up to millions of trees cost the CPU about the same as a few hundred. It draws your Leafshaper trees with no setup, and it also draws terrain grass and details, rocks and any other prefab, converts your vegetation shaders and bakes impostors.

The renderer works in URP, HDRP and the Built-in Render Pipeline, on devices with compute shaders and structured buffers in vertex shaders. Without them, or with GPU forest rendering turned off, every tree draws through Unity's LOD groups and terrain trees instead; see Large forests.

This page covers Leafshaper trees. For everything else the renderer draws:

Automatic drawing

Leafshaper draws these trees on the GPU in the Editor, in Play Mode and in player builds:

  • Terrain trees. A terrain whose tree prototypes are all baked Leafshaper prefabs gets a hidden Forest Renderer. The terrain keeps its tree colliders and stops drawing the trees itself. Painting, erasing and moving trees follow on their own. A terrain that also holds other trees stays with Unity, so nothing on it disappears; add a GPU Terrain Source to draw it on the GPU as well.
  • Trees placed by the Forest Scatterer and its brushes. Terrain trees draw as above. Trees placed as scene objects go under a parent with a Leafshaper Foliage Group.
  • Trees under a Leafshaper Foliage Group. The group's Leafshaper trees draw on the GPU, and follow it when you move, add, remove, turn on or turn off a tree. Their own renderers stay in the scene, untouched. Its inspector shows which renderer draws them.

Trees you dropped into a scene before are left as they are. Choose GameObject > Leafshaper > Move Scene Trees to the GPU Renderer to add a group to each set of loose Leafshaper trees, with Undo.

Edit > Project Settings > Leafshaper > GPU Forest Rendering has the Use GPU forest rendering setting, on by default. It shows the active renderer, how many terrains, groups and trees the GPU draws, and has the move button. Turn it off to give every tree back to Unity's LOD groups and terrain trees, for example to compare; a player built with it off does the same.

Nothing about this is saved in your scenes. The hidden renderers are never saved, a terrain's tree distance is written as it was, and tree renderers stay switched on. With the setting off, or on a device without compute shaders, the same scenes draw through Unity's terrain trees and LOD groups, and the Leafshaper Foliage Group does nothing.

Forest Renderer

For trees from code, or to change culling, LOD and shadow settings, add a renderer yourself with GameObject > Leafshaper > Forest Renderer. A Forest Renderer you add takes over its terrain from the automatic one.

The Leafshaper Forest Renderer keeps its own settings, terrain import and scripting API, and owns a Leafshaper GPU Renderer on a hidden object that is never saved with the scene. Foliage returns that renderer, for its debug views, per prefab statistics and settings. To draw rocks, grass or prefabs from other packages next to your trees, add a Leafshaper GPU Renderer of your own, as below.

The Forest Showcase sample plants 150,000 trees this way and grows to a million. With GPU forest rendering off, Unity draws 3,000 of them.

How it works

For every Game and Scene view camera, a compute shader visits each instance and:

  1. Culls it against the view frustum and Max Distance.
  2. Picks a level of detail from its screen size using the prefab's LOD Group thresholds, your quality level's LOD bias and LOD Bias, with a dithered Cross Fade Width between levels.
  3. Optionally culls it against a farthest-depth pyramid built from the previous frame's depth (Occlusion Culling, URP and the Built-in Render Pipeline). The test uses each tree's oriented box, skips boxes that were partly outside the previous view, widens with camera movement, and switches itself off for a frame when the camera jumps.
  4. Culls shadow casters separately, along the direction of Shadow Light or the scene's sun, within Shadow Distance, using a level of detail Shadow Lod Offset steps coarser.

Each mesh level and material then draws with one indirect call, so the CPU cost does not grow with the number of trees. The forest draws on the layer of the object that holds the renderer; cameras whose culling mask leaves that layer out draw no trees and skip the culling.

Levels of detail cross-fade when the prefab's LOD Group uses Fade Mode > Cross Fade, as Leafshaper's baked prefabs do. A group set to None switches levels without a fade. A large batch of new instances uploads over a few frames instead of in one.

Draw a terrain's trees

Assign Source Terrain on a Forest Renderer. The renderer reads the terrain's tree instances whose prototypes are baked Leafshaper prefabs and sets the terrain's tree distance to zero while Hide Terrain Trees is on, so the terrain keeps its tree colliders but stops drawing the trees. Painting trees in the Editor re-imports them. Turning the renderer off restores the terrain's tree distance.

Add trees from code

csharp
var _handle = forest.AddInstance(prefab, position, rotation, widthScale, heightScale);
forest.SetInstanceTransform(_handle, position, rotation, widthScale, heightScale);
forest.SetInstanceVisible(_handle, false);
forest.RemoveInstance(_handle);

A prefab must be baked by Leafshaper, or use Leafshaper/Tree and Leafshaper/Tree Impostor materials. Its level-of-detail renderers may sit on child objects. The Forest Renderer supports up to 4,000,000 instances. InstanceCount, VisibleInstanceCount, ShadowCasterCount and DrawCallCount report what it drew last frame. For any other prefab, use the Leafshaper GPU Renderer's own API; see Scripting API.

Draw grass, rocks and other prefabs

The Leafshaper GPU Renderer draws more than Leafshaper trees.

  • Terrains. Choose GameObject > Leafshaper > Draw Scene Terrains on the GPU. It creates a Leafshaper GPU Renderer if the open scenes have none, links it to the first Wind Zone it finds, and adds a GPU Terrain Source to every terrain that has none. While a terrain source is enabled, the terrain's own Tree Distance and Detail Distance are set to zero so Unity stops drawing them. Disabling or removing the source restores both values.
  • Placed prefabs. Select prefab instances in the Hierarchy (rocks, bushes, hand placed trees) and choose GameObject > Leafshaper > GPU Prefab Source From Selection. Their placements are stored in a GPU Prefab Source and their mesh renderers and LOD groups are turned off, so the GPU draws them while their colliders and scripts keep working. Show Captured Objects in the source's inspector turns them back on.
  • A renderer on its own. GameObject > Leafshaper > GPU Renderer adds a Leafshaper GPU Renderer to the scene, or selects the one it already has.

See Sources for every source setting.

What happens to your shaders

The first time a material is drawn, its shader is checked:

ShaderWhat the renderer does
Leafshaper shadersDraws it as it is.
A .shader file or Shader Graph in your project or a packageCopies it to Assets/Leafshaper Generated/GPU Rendering/Converted Shaders with the instancing setup added, and draws a copy of the material with it.
Unity's built-in shaders (Standard, Legacy, Nature)Draws with Leafshaper/Foliage Lit, copying the main texture, colour, cutout and normal map.

Conversions are recorded in a material library at Assets/Leafshaper Generated/GPU Rendering/Resources/LeafshaperGpuLibrary.asset, which players load at runtime. See Shaders.

Check the result

Select the Leafshaper GPU Renderer. The header shows its state, and the numbers below it show instances, visible instances, draw calls and GPU memory. The Prototypes tab lists every prefab it draws with its levels of detail and any problems. The Debug tab colours instances by level of detail, by prototype or by cross-fade.

Notes

  • Compute shaders and structured buffers in vertex shaders are required. On devices without them, Leafshaper trees draw through Unity's LOD groups and terrain trees, and a Leafshaper GPU Renderer you added reports this and draws nothing.
  • Trees drawn this way use the light probe at the forest's bounds, plus Adaptive Probe Volumes per pixel in URP and HDRP.
  • In URP the renderer requests the camera depth itself; you do not need to enable Depth Texture on the pipeline asset.
  • HDRP keeps frustum, distance and shadow culling. Depth occlusion runs in URP and the Built-in Render Pipeline.
  • While two levels cross-fade, the main view draws both with complementary dithering. The shadow map dithers the same pair, and casters dissolve over the last tenth of Shadow Distance.

Still stuck?

Bring the recipe with you.

Include the Leafshaper, Unity and render-pipeline versions, the operating system and graphics API, the first Console error, the preset and seed, the GPU renderer's header state for forest or shader problems, and the steps that reproduce the problem.

Email supportJoin Discord

Leafshaper 26.12.0

Search documentation