Table of Contents

Class TreeInstanceGenerator

Namespace
Map.Rendering
Assembly
MapAssembly.dll

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
object
TreeInstanceGenerator

Constructors

TreeInstanceGenerator(ComputeShader, int, int, int, bool)

public TreeInstanceGenerator(ComputeShader shader, int mapWidth, int mapHeight, int maxTrees = 100000, bool logProgress = true)

Parameters

shader ComputeShader
mapWidth int
mapHeight int
maxTrees int
logProgress bool

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

terrainTypeTexture Texture2D

R8 texture with terrain type indices

heightmapTexture Texture2D

R8 texture with heightmap data

treeTerrainTypes uint[]

Which terrain types spawn trees (GAME layer policy)

treeDensity float

Trees per 100 world units squared

treeScale float

Base tree scale

treeScaleVariation float

Random scale variation (0-1)

mapWorldWidth float

Map width in world space units

mapWorldHeight float

Map height in world space units

GetTreeCountBuffer()

Get the tree count buffer for indirect args

public ComputeBuffer GetTreeCountBuffer()

Returns

ComputeBuffer

GetTreeMatrixBuffer()

Get the tree matrix buffer for rendering (DrawMeshInstancedIndirect)

public ComputeBuffer GetTreeMatrixBuffer()

Returns

ComputeBuffer

Release()

Release GPU buffers

public void Release()