Skip to main content

Overview

The GLBTreeBatchedInstancer 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:
Draw Call Reduction:
  • 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

  1. Extract fingerprint from first variant’s materials:
  2. Match subsequent variants to reference fingerprints:
  3. 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:
Benefits:
  • Prevents rapid LOD switching at boundaries
  • Smoother visual transitions
  • Reduces GPU state changes

LOD Transition

When LOD changes:
  1. Remove instance from old BatchedMesh
  2. Preserve highlight state
  3. Add instance to new BatchedMesh
  4. Restore highlight if needed

Material System

Dissolve Materials

Trees use TreeDissolveMaterial 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
Per Instance:
  • 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
Vertex Count:
  • 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

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.
Call enableTextureRepeat() on materials to prevent texture stretching on large instances.
Keep instance count below MAX_INSTANCES (512) per tree type. Split into multiple tree types if needed.

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