Skip to main content

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 uses InstancedMesh 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 InstancedMesh per LOD level
  • Distance-based LOD switching
  • Dissolve materials for respawn animations
  • Highlight mesh pooling
  • Depleted model support (stumps)
Example:

GLBResourceInstancer

Manages instanced rendering for rocks, ores, and herbs. Features:
  • Same as GLBTreeInstancer
  • Optimized for smaller resources
  • Invisible collision proxies for raycasting
Example:

LOD System

LOD Levels

Each resource has 3 LOD levels:

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
Rocks:
  • Depleted model: granite_depleted.glb
  • Depleted scale: 60% of original

Depletion Flow

  1. Player harvests resource
  2. Visual strategy marks instance as depleted
  3. Instancer switches to depleted pool
  4. Resource respawns after timer
  5. Instancer switches back to normal pool

Hover Highlights

Highlight Mesh

When player hovers over a resource, a highlight mesh is shown:
Implementation:
  • 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
Poor candidates:
  • Unique objects (players, NPCs)
  • Objects with per-instance materials
  • Objects with complex animations

Performance Tips

  1. Group by model: One instancer per unique model
  2. Limit max instances: Set realistic maxInstances
  3. Use LODs: Configure appropriate LOD distances
  4. Batch updates: Update multiple instances before needsUpdate
  5. Reuse instancers: Share across resource types

Debugging

Enable Debug Logging

Visual Debugging

Performance Profiling