Overview
TheGLBTreeBatchedInstancer provides BatchedMesh-based rendering for multi-variant trees with minimal draw calls. It supports:
- Multiple model variants per tree type (e.g., oak_1.glb, oak_2.glb, oak_3.glb)
- One BatchedMesh per material slot per LOD (minimal draw calls)
- Texture fingerprinting for automatic material slot matching across variants
- LOD switching (LOD0/LOD1/LOD2) based on camera distance
- Depleted states (stumps after chopping)
- Highlight support for interaction feedback
Added in commits 82a5365 and 6c14c8e (March 12, 2026). Optimized in commit 6c14c8e with deterministic fingerprinting.
Architecture
BatchedMesh Pooling
One BatchedMesh per material slot per LOD level:- Without instancing: 3 variants × 2 materials × 3 LODs = 18 draw calls
- With BatchedMesh: 2 materials × 3 LODs = 6 draw calls
- Reduction: 67% fewer draw calls
API Reference
Initialization
Adding Instances
Removing Instances
Depleted State
Highlighting
Utility Functions
Texture Fingerprinting
The instancer uses texture fingerprinting to match material slots across variants.How It Works
-
Extract fingerprint from first variant’s materials:
-
Match subsequent variants to reference fingerprints:
-
Register geometries in correct material slot order:
Deterministic Fallback
Problem: Random fingerprints could cause silent variant matching failures. Solution (Commit 6c14c8e):- Use monotonic counter
_fingerprintId++instead of random values - Ensures consistent fingerprints across runs
- Prevents silent matching failures
LOD System
LOD Distances
LOD Switching
Hysteresis (0.81x multiplier) prevents flickering:- Prevents rapid LOD switching at boundaries
- Smoother visual transitions
- Reduces GPU state changes
LOD Transition
When LOD changes:- Remove instance from old BatchedMesh
- Preserve highlight state
- Add instance to new BatchedMesh
- Restore highlight if needed
Material System
Dissolve Materials
Trees useTreeDissolveMaterial with TSL shaders:
Uniform Updates
Materials are updated every frame with environment data:Performance Characteristics
Memory Usage
Per Tree Type:- LOD0: 2 BatchedMesh × ~1MB = 2MB
- LOD1: 2 BatchedMesh × ~500KB = 1MB
- LOD2: 2 BatchedMesh × ~250KB = 500KB
- Total: ~3.5MB per tree type
- Matrix: 64 bytes
- Color: 12 bytes
- Total: ~76 bytes per instance
GPU Usage
Draw Calls:- 2 materials × 3 LODs = 6 draw calls per tree type
- Typical world: 5 tree types = 30 draw calls total
- vs 1000+ draw calls without instancing
- LOD0: ~2000 vertices per tree
- LOD1: ~1000 vertices per tree
- LOD2: ~500 vertices per tree
- Shared across all instances (no duplication)
Troubleshooting
Variant Matching Failures
Symptom: Console warnings about part count mismatch. Cause: Variants have different numbers of material slots. Solution: Ensure all variants have the same mesh structure:- Same number of meshes
- Same material slot order
- Same texture dimensions
Missing Depleted Models
Symptom: Trees don’t show stumps when chopped. Cause:depletedModelPath not provided or model failed to load.
Solution:
Highlight Not Working
Symptom: Trees don’t highlight on hover. Cause: Highlight mesh not returned by visual strategy. Solution:Best Practices
Use consistent material slots
Use consistent material slots
Ensure all variants have materials in the same order (bark first, leaves second). This allows texture fingerprinting to work correctly.
Provide depleted models
Provide depleted models
Always provide a depleted model (stump) for harvestable trees. This improves visual feedback when trees are chopped.
Use LOD models
Use LOD models
Provide LOD1 and LOD2 models for better performance at distance. Use
inferLOD1Path() and inferLOD2Path() naming convention.Enable texture repeat
Enable texture repeat
Call
enableTextureRepeat() on materials to prevent texture stretching on large instances.Limit instance count
Limit instance count
Keep instance count below
MAX_INSTANCES (512) per tree type. Split into multiple tree types if needed.Related Systems
Terrain LOD
Hierarchical quadtree LOD for infinite terrain
Biome System
Biome-specific tree distribution and placement
Resource System
Resource spawning and gathering mechanics
GPU Materials
TSL-based materials with dissolve effects