Class TreeInstanceGenerator
GPU-driven tree instance generation using compute shaders.
Architecture:
- Reads terrain type texture to determine tree placement
- Generates tree positions/rotations/scales procedurally on GPU
- Outputs StructuredBuffer for DrawMeshInstancedIndirect
- Deterministic (same terrain = same trees)
Performance:
- Runs once at map load (or when terrain changes)
- GPU parallel processing (8x8 thread groups)
- Zero CPU overhead after generation
Usage:
- Call GenerateTrees() to populate buffers
- Use GetTreeMatrixBuffer() for indirect rendering
public class TreeInstanceGenerator
- Inheritance
-
objectTreeInstanceGenerator
Constructors
TreeInstanceGenerator(ComputeShader, int, int, int, bool)
public TreeInstanceGenerator(ComputeShader shader, int mapWidth, int mapHeight, int maxTrees = 100000, bool logProgress = true)
Parameters
shaderComputeShadermapWidthintmapHeightintmaxTreesintlogProgressbool
Methods
GenerateTrees(Texture2D, Texture2D, uint[], float, float, float, float, float)
Generate tree instances based on terrain types. Runs compute shader to populate tree matrix buffer.
public void GenerateTrees(Texture2D terrainTypeTexture, Texture2D heightmapTexture, uint[] treeTerrainTypes, float treeDensity = 10, float treeScale = 1, float treeScaleVariation = 0.3, float mapWorldWidth = 0, float mapWorldHeight = 0)
Parameters
terrainTypeTextureTexture2DR8 texture with terrain type indices
heightmapTextureTexture2DR8 texture with heightmap data
treeTerrainTypesuint[]Which terrain types spawn trees (GAME layer policy)
treeDensityfloatTrees per 100 world units squared
treeScalefloatBase tree scale
treeScaleVariationfloatRandom scale variation (0-1)
mapWorldWidthfloatMap width in world space units
mapWorldHeightfloatMap height in world space units
GetTreeCountBuffer()
Get the tree count buffer for indirect args
public ComputeBuffer GetTreeCountBuffer()
Returns
GetTreeMatrixBuffer()
Get the tree matrix buffer for rendering (DrawMeshInstancedIndirect)
public ComputeBuffer GetTreeMatrixBuffer()
Returns
Release()
Release GPU buffers
public void Release()