Leafshaper Docs
View all packages

GPU rendering

GPU shaders

Convert shaders automatically, set presets, add the three-line HLSL setup or use Shader Graph, and use Foliage Lit.

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

Instances drawn with the Custom Shader Integration sample's hand-written shader

The Leafshaper GPU Renderer draws with indirect instancing. A shader can draw GPU instances when each pass it uses has the procedural instancing setup from LeafshaperGpuInstancing.hlsl. That setup replaces the object matrices and the LOD fade of the instance being drawn, so the shader's own transforms, lighting and cross-fade keep working.

Leafshaper's own shaders, Leafshaper/Tree, Leafshaper/Tree Impostor, Leafshaper/Foliage Lit and Leafshaper/Foliage Impostor, already have the setup and draw as they are.

Automatic conversion

With Convert Shaders Automatically on (the default, in the renderer's ••• menu or the Shader Setup window), the first time a material with an unknown shader is drawn:

  1. Its shader is copied to Assets/Leafshaper Generated/GPU Rendering/Converted Shaders and renamed Leafshaper/Converted/<original name>.

- Text shaders (.shader): every pass the renderer draws gains the instancing pragma, the procedural setup option and the include. Meta, 2D, picking and ray tracing passes are left alone. Relative includes are made absolute. Surface shaders gain addshadow so their shadows follow the instances. - Shader Graphs: the Leafshaper GPU Setup Custom Function node is inserted between the graph's vertex position and the Vertex Position block, and Support LOD CrossFade is turned on for every target.

  1. A persistent copy of the material is created in Assets/Leafshaper Generated/GPU Rendering/Converted Materials so players include the right shader variants.
  2. Both are recorded in the material library, and every renderer refreshes.

Until the conversion finishes (a moment later in the editor) the material draws with Foliage Lit. Editing the source material copies its values into the converted copy. Saving the source shader converts it again.

Open Window > Leafshaper > Shader Setup to see every shader your GPU instances use, how it is drawn, and to convert, reconvert or forget each one.

Presets

Presets decide how a family of shaders is handled and which texture names the impostor baker reads.

PresetShadersHandling
LeafshaperLeafshaper/*Native
URP Lit familyLit, Simple Lit, Complex Lit, Baked Lit, UnlitConverted
URP SpeedTreeUniversal Render Pipeline/Nature/SpeedTree*Converted (see limitations)
HDRP Lit familyHDRP/Lit, HDRP/LitTessellation, HDRP/UnlitConverted
HDRP SpeedTreeHDRP/Nature/SpeedTree*Converted (see limitations)
The Vegetation EngineBOXOPHOBIC/The Vegetation Engine/*Converted
Nature ShadersNature Shaders/*Converted, replacing their own procedural setup
NatureManufactureNatureManufacture*, Shader Graphs/NM_*Converted
Built-in Standard and LegacyStandard, Legacy Shaders/*, Mobile/*, Unlit/*Foliage Lit fallback
Built-in NatureNature/*, terrain detail shadersFoliage Lit fallback

Any other shader with source in the project is converted. To override a family, create Assets > Create > Leafshaper > Shader Preset, set its name patterns (a trailing * matches the start of a name), its handling and its texture names. Project presets win over the built-in list.

Adding the setup to your own shader

Add these three lines to every pass the renderer draws (forward, shadow caster, depth, depth normals, motion vectors, G-buffer), after the pass's own includes. Just before ENDHLSL or ENDCG is a safe place:

hlsl
#pragma multi_compile_instancing
#pragma instancing_options procedural:leafshaperGpuSetup
#include "Packages/net.kiwistudios.trees/ShaderLibrary/LeafshaperGpuInstancing.hlsl"

Also add the tag "LeafshaperGpu" = "Instanced" to the SubShader's Tags, so the renderer draws the shader as it is instead of converting it.

Two more SubShader tags are optional:

TagEffect
"LeafshaperGpuCrossFade" = "Instanced"The shader dithers its own cross-fade from leafshaperGpuFade. The renderer draws your instanced material as it is, without a copy that turns on LOD_FADE_CROSSFADE, so edits to the material show at once.
"LeafshaperGpuImpostor" = "True"The shader draws baked impostor cards. A level of detail drawn only with such materials counts as an impostor, and its card does not widen the culling bounds of a prefab that also has mesh levels.

The pass must call UNITY_SETUP_INSTANCE_ID in its vertex function, as Unity's own shaders do. For dithered LOD cross-fade, declare #pragma multi_compile _ LOD_FADE_CROSSFADE and apply Unity's LODFadeCrossFade (URP) or your own dither in the fragment function: the setup writes unity_LODFade with Unity's sign convention.

Inside the procedural variant, LEAFSHAPER_GPU_INSTANCED is 1 and these values describe the current instance:

ValueMeaning
leafshaperGpuObjectToWorld, leafshaperGpuWorldToObjectThe instance matrices.
leafshaperGpuFadeSigned cross-fade: positive for the outgoing level, negative for the incoming one.
leafshaperGpuTintA per instance value from 0 to 1 (terrain tree colour variation, or the tint you pass when adding instances).

A function that reads these values has to come after the include. Place the three lines above it, but still after the pass's own includes, as the Custom Shader Integration sample does.

Shaders that keep their own object matrices, for example to work in absolute world space in every pipeline, can #define LEAFSHAPER_GPU_CUSTOM_MATRICES before the include. The setup then leaves unity_ObjectToWorld, unity_WorldToObject and unity_LODFade alone, and the shader reads the values above.

Amplify Shader Editor

Add the three lines under Additional Directives in the graph's output node. Converting an Amplify shader as text works too, but saving it from Amplify again removes the conversion.

Shader Graph

Add two Custom Function nodes:

  1. Type: File. Source: Packages/net.kiwistudios.trees/ShaderLibrary/LeafshaperGpuShaderGraph.hlsl. Name: LeafshaperGpuSetup. Inputs: Position (Vector 3). Outputs: Out (Vector 3) and Tint (Float).
  2. Type: String. Name: LeafshaperGpuInstancing. Input In and output Out (both Vector 3). Body:

``hlsl #pragma instancing_options procedural:leafshaperGpuSetup Out = In; ``

Unity ignores the procedural option when it comes from an included file, so it needs its own string node.

Connect a Position node in Object space (or your own vertex offset) to Position, the first node's Out to the second node's In, and the second node's Out to the Vertex Position block.

Turn on Support LOD CrossFade in the Graph Settings for cross-fades. Tint is available in the vertex stage.

The Shader Setup window can also convert a Shader Graph for you: it writes a converted copy with the Leafshaper GPU Setup node and leaves your original untouched.

Pipeline shader sources

The URP and HDRP parts of Leafshaper's shaders and of the Custom Shader Integration sample sit in their own shader sources with the .leafshapershader extension, such as InstancedTint.Universal.leafshapershader. Leafshaper builds each one into a shader when its pipeline is installed, and into an empty stand-in otherwise. Use the same layout for your own shaders with separate URP and HDRP parts; see Render pipelines.

Foliage Lit

Leafshaper/Foliage Lit is the renderer's own shader for leaves, grass and anything converted to a fallback. It works in URP, HDRP and the Built-in Render Pipeline with main texture, colour, cutout, normal map, smoothness, healthy and dry colours mixed by instance tint, translucency, an up-facing normal blend for grass, ground occlusion, and wind driven by the renderer's Wind Zone. Leafshaper/Foliage Impostor draws impostors baked for any prefab; see Impostors.

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