Spaces:
Running
Running
Robot Instancing Optimization
This implementation optimizes the rendering of multiple robots using Threlte's instancing capabilities for improved performance.
Key Features
1. Smart Instancing by Robot Type
- Robots are grouped by their URDF type (URL) for optimal instancing
- Single robots render with full detail
- Multiple robots of the same type use instanced rendering
2. Geometry-Specific Instancing
- Box Geometries: Instanced with
T.boxGeometry - Cylinder Geometries: Instanced with
T.cylinderGeometry - Mesh Geometries: Instanced with simplified
T.sphereGeometryfor performance
3. Hybrid Rendering Strategy
- First robot of each type: Full detailed rendering with all URDF components
- Additional robots: Simplified instanced representation
- Maintains visual quality while optimizing performance
4. Performance Benefits
- Reduces draw calls when rendering multiple robots
- Optimizes GPU memory usage through instancing
- Scales better with increasing robot count
- Maintains interactivity for detailed robots
Implementation Details
State Management
// Robots grouped by URDF type for optimal batching
let robotsByType: Record<string, Array<{
id: string;
position: [number, number, number];
robotState: RobotState
}>> = $state({});
Instancing Logic
- Single Robot: Full
UrdfLinkrendering with all details - Multiple Robots:
- Geometry analysis and grouping
- Instanced rendering for primitive shapes
- Simplified representations for complex meshes
Automatic Demonstration
- Spawns additional robots after 2 seconds to showcase instancing
- Shows performance difference between single and multiple robot rendering
Usage
Simply use the Robot.svelte component with a urdfConfig. The component automatically:
- Detects when multiple robots of the same type exist
- Switches to instanced rendering for optimal performance
- Maintains full detail for single robots
<Robot {urdfConfig} />
Performance Impact
- Before: O(n) draw calls for n robots
- After: O(1) draw calls per geometry type regardless of robot count
- Memory: Shared geometry instances reduce GPU memory usage
- Scalability: Linear performance improvement with robot count
This optimization is particularly beneficial for:
- Robot swarms
- Multi-robot simulations
- Arena scenarios with many similar robots
- Performance-critical real-time applications