Instanced Rendering
Hyperscape uses GPU instancing to render thousands of resources (trees, rocks, ores, herbs) with minimal draw calls.Overview
Instead of creating individual meshes for each resource, the system usesInstancedMesh to render all instances of the same model in a single draw call.
Performance improvement:
- 1000 trees (5 unique models) = 5 draw calls (was 1000)
- 1000 rocks (3 unique models) = 3 draw calls (was 1000)
- Total reduction: ~250x fewer draw calls
How It Works
Traditional Rendering
Instanced Rendering
Components
GLBTreeInstancer
Manages instanced rendering for tree resources. Features:- Separate
InstancedMeshper LOD level - Distance-based LOD switching
- Dissolve materials for respawn animations
- Highlight mesh pooling
- Depleted model support (stumps)
GLBResourceInstancer
Manages instanced rendering for rocks, ores, and herbs. Features:- Same as GLBTreeInstancer
- Optimized for smaller resources
- Invisible collision proxies for raycasting
LOD System
LOD Levels
Each resource has 3 LOD levels:| LOD | Distance | Triangles | Use Case |
|---|---|---|---|
| LOD0 | 0-20m | 100% | Close-up detail |
| LOD1 | 20-40m | ~50% | Medium distance |
| LOD2 | 40-80m | ~25% | Far distance |
LOD Switching
LOD switches based on camera distance with hysteresis to prevent flickering:Depletion System
Depleted Models
When a resource is depleted, it shows a depleted model: Trees:- Depleted model:
oak_stump.glb - Depleted scale: 80% of original
- Depleted model:
granite_depleted.glb - Depleted scale: 60% of original
Depletion Flow
- Player harvests resource
- Visual strategy marks instance as depleted
- Instancer switches to depleted pool
- Resource respawns after timer
- Instancer switches back to normal pool
Hover Highlights
Highlight Mesh
When player hovers over a resource, a highlight mesh is shown:- Highlight mesh preloaded from LOD0
- Shared across all instances
- Positioned at hovered instance
- Removed when hover ends
Collision Detection
Raycasting
Instanced meshes don’t support per-instance raycasting. The system uses invisible collision proxies:Performance Metrics
Draw Call Reduction
Test scene (1000 resources):- Before: 1000 draw calls
- After: 8 draw calls
- Reduction: 99.2%
Memory Usage
Per-instance overhead:- Before: ~500KB (full mesh clone)
- After: ~64 bytes (matrix + state)
- Reduction: 99.99%
Frame Rate
Test scene (5000 resources):- Before: 15 FPS
- After: 60 FPS
- Improvement: 4x
Best Practices
When to Use Instancing
Good candidates:- Resources with many instances (>10)
- Static or rarely-moving objects
- Objects with same model/material
- Unique objects (players, NPCs)
- Objects with per-instance materials
- Objects with complex animations
Performance Tips
- Group by model: One instancer per unique model
- Limit max instances: Set realistic
maxInstances - Use LODs: Configure appropriate LOD distances
- Batch updates: Update multiple instances before
needsUpdate - Reuse instancers: Share across resource types