Understanding Systems
A system is a React component that:- Subscribes to the
dirtyNodesset from the scene state - Uses
useFrame()to run logic every frame - Processes nodes that need updates
- Accesses Three.js objects via the scene registry
- Clears the dirty flag when processing is complete
Systems are already running for core node types (walls, items, slabs, etc.). You typically create custom systems for:
- Custom node types
- Advanced automation
- Performance-critical calculations
- Integration with external services
The System Pattern
Here’s the anatomy of a system using the real ItemSystem as an example:Creating a Custom System
1
Define your system component
Create a React component that will house your system logic:
2
Subscribe to dirty nodes
Access the dirty nodes set to know which nodes need processing:
3
Access Three.js objects via registry
The scene registry maps node IDs to their Three.js representations:
If the mesh doesn’t exist yet, keep the node dirty so it’s processed in the next frame.
4
Implement your logic
Add your custom behavior. Here’s an example that animates items:
5
Clear the dirty flag
When processing is complete, clear the dirty flag:
6
Register your system
Add your system component to the scene:
Real-World Example: Wall Mitering System
The WallSystem is a sophisticated example that demonstrates advanced system patterns:useFrame Priority
The second argument touseFrame() controls execution order:
- Priority 2: ItemSystem (positioning)
- Priority 4: WallSystem (geometry)
- Priority 5: Custom systems (default)
Performance Best Practices
Early Exit for Empty Dirty Set
Batch Processing
Group related operations to minimize redundant calculations:Deferred Mesh Access
Scene Registry API
The scene registry provides fast lookups:Example: Auto-Rotation System
Here’s a complete example of a system that auto-rotates items tagged withautoRotate:
Next Steps
Event Handling
Respond to user interactions in your systems
Spatial Queries
Use spatial queries in your system logic