Skip to main content

Overview

The GLBTreeBatchedInstancer module provides BatchedMesh-based rendering for multi-variant trees with minimal draw calls.
Source: packages/shared/src/systems/shared/world/GLBTreeBatchedInstancer.tsAdded in commits 82a5365 and 6c14c8e (March 12, 2026).

Initialization

initGLBTreeBatchedInstancer

Initializes the tree instancing system. Parameters:
  • scene - Three.js scene for adding BatchedMesh instances
  • world - World instance for material setup
Example:

destroyGLBTreeBatchedInstancer

Destroys all tree instances and cleans up resources. Example:

Instance Management

addInstance

Adds a tree instance to the rendering system. Parameters:
  • treeType - Tree type identifier (e.g., “oak”, “birch”)
  • variantPaths - Array of model paths for variants (e.g., [“oak_1.glb”, “oak_2.glb”])
  • variantIndex - Index of variant to use (0-based)
  • entityId - Unique entity ID
  • position - World position (THREE.Vector3)
  • rotation - Y-axis rotation in radians
  • scale - Uniform scale multiplier
  • depletedModelPath - Optional path to depleted model (stump)
  • depletedScale - Optional scale for depleted model
Returns: Promise<boolean> - true if successful, false if failed Example:

removeInstance

Removes a tree instance from the rendering system. Parameters:
  • entityId - Entity ID of tree to remove
Example:

State Management

setDepleted

Sets the depleted state of a tree (shows stump or normal tree). Parameters:
  • entityId - Entity ID of tree
  • depleted - true to show stump, false to show normal tree
Example:
Behavior:
  • Removes instance from current LOD pool
  • Adds instance to depleted pool (or normal pool)
  • Uses depletedScale for stump size
  • Preserves position and rotation

setHighlight

Highlights a tree instance (1.15x color multiplier). Parameters:
  • entityId - Entity ID of tree
  • on - true to highlight, false to remove highlight
Example:
Behavior:
  • Only one tree can be highlighted at a time
  • Automatically removes previous highlight when highlighting new tree
  • Highlight persists across LOD transitions

clearHighlight

Clears all tree highlights. Example:

Utility Functions

hasInstance

Checks if an entity has a tree instance. Parameters:
  • entityId - Entity ID to check
Returns: true if instance exists, false otherwise Example:

hasDepleted

Checks if a tree has a depleted model available. Parameters:
  • entityId - Entity ID to check
Returns: true if depleted model available, false otherwise Example:

getModelDimensions

Gets the model dimensions for a tree instance. Parameters:
  • entityId - Entity ID to query
Returns: Object with height and radius in meters, or null if not found Example:

Update Loop

updateGLBTreeBatchedInstancer

Updates LOD levels and material uniforms. Call once per frame. Example:
Behavior:
  • Switches LOD levels based on camera distance
  • Updates dissolve uniforms (camera pos, player pos, sun direction, wind)
  • Preserves highlight state across LOD transitions
  • Uses hysteresis (0.81x) to prevent flickering

Constants

MAX_INSTANCES

Maximum number of instances per BatchedMesh. Usage:
  • Limit instance count per tree type to 512
  • Split into multiple tree types if needed
  • Prevents BatchedMesh overflow

Type Definitions

TreeSlot

Internal slot tracking for tree instances.

BatchedLODPool

Pool of BatchedMesh instances for a specific LOD level.

TreeTypePool

Pool of all LOD levels for a specific tree type.

Error Handling

Model Load Failures

Common Causes:
  • Model file not found
  • Invalid model format
  • Texture loading failure
  • Out of memory

Variant Matching Failures

Causes:
  • Variants have different numbers of meshes
  • Material slot order differs between variants
  • Texture dimensions don’t match
Solution: Ensure all variants have identical mesh structure.

Best Practices

Ensure all variants have materials in the same order (bark first, leaves second). This allows texture fingerprinting to work correctly.
Always provide a depleted model (stump) for harvestable trees. This improves visual feedback when trees are chopped.
Provide LOD1 and LOD2 models for better performance at distance. Use inferLOD1Path() and inferLOD2Path() naming convention.
Keep instance count below MAX_INSTANCES (512) per tree type. Split into multiple tree types if needed.
Call updateGLBTreeBatchedInstancer() once per frame to update LOD levels and material uniforms.

Terrain LOD API

TerrainQuadTree and TerrainQuadNode API

Biome API

BiomeType enum and tree configuration

Resource System

Resource spawning and gathering

GPU Materials

TSL-based materials with dissolve effects