Try it out!
Scan or click the QR code to launch a new CodePen instance. Try clicking on or dragging the central sphere to see synchronized 3D interactions across all users!This tutorial assumes you’ve completed the Simple Animation tutorial as it follows the same architectural pattern extended into 3D.
Architecture Overview
The app follows the same Model-View pattern as Simple Animation:- Root MyModel: Manages BallModel collection and central sphere state
- Root MyView: Creates BallView instances and handles Three.js integration
- BallModel: Calculates 3D positions and handles collisions
- BallView: Creates 3D visual objects and responds to position updates
What You’ll Learn
External Library Integration
Import and initialize Three.js for 3D rendering
3D Event Handling
Convert 2D pointer events to 3D object interactions
Raycasting
Detect 3D object intersections from 2D clicks
Render Loop Integration
Hook Three.js rendering into Multisynq’s update cycle
External Library Integration
Import Methods
You can integrate Three.js using standard web development approaches:CodePen Configuration
For CodePen projects, add external libraries in the JavaScript settings:
Three.js Scene Initialization
Basic Scene Setup
Scene Components
Scene Object
Scene Object
Container for all 3D objects, lights, and cameras
Lighting
Lighting
Ambient light for overall illumination + point light for shadows
Camera
Camera
Perspective camera positioned to view the bouncing balls
Renderer
Renderer
WebGL renderer that draws the scene to a canvas element
3D Event Handling
Pointer Event Processing
Convert 2D pointer events to 3D object interactions using raycasting:Drag Movement with Throttling
Event Throttling Strategy
Time Throttling
Limit events to 20 per second (50ms minimum interval)
Distance Throttling
Ignore movements smaller than 0.01 units
Network Optimization
Reduces bandwidth usage without affecting user experience
Touch-Friendly
Particularly important for mobile devices
Click vs. Drag Detection
Custom Event Handling
Adding Custom Properties
Add custom properties to Three.js objects usingq_ prefix to avoid conflicts:
Click Event Handling
Reset Functionality
Models must handle events to ensure synchronization across all users. Even if a view could handle events directly, involving the model ensures all session instances receive the same events.
Drag Event Handling
Position Processing
Data Serialization
Render Loop Integration
Controlled Rendering
Instead of usingrequestAnimationFrame directly, integrate with Multisynq’s update cycle:
MyView Integration
Benefits of Controlled Rendering
Synchronization
Rendering happens at exact moments when state changes
Performance
Avoids unnecessary renders when nothing changes
Determinism
Consistent rendering timing across all users
Integration
Seamless integration with Multisynq’s lifecycle
3D Object Management
Creating 3D Objects
Dynamic Object Updates
Performance Considerations
Optimization Techniques
Event Throttling
Event Throttling
Limit pointer events to 20 per second to reduce network load
Geometry Reuse
Geometry Reuse
Create geometry once and reuse for multiple objects
Efficient Updates
Efficient Updates
Only update 3D objects when model state actually changes
LOD (Level of Detail)
LOD (Level of Detail)
Use simpler geometry for distant objects
Memory Management
Advanced Integration Patterns
Custom Object Properties
Multi-Library Integration
Troubleshooting
Common Issues
Serialization Errors
Always convert Three.js objects to plain arrays before publishing
Event Conflicts
Use
q_ prefix for custom properties to avoid naming conflictsPerformance Issues
Implement proper event throttling and geometry reuse
Synchronization Problems
Ensure all state changes go through the Model layer
Debug Tips
Next Steps
Multiblaster Game
See 3D techniques in a complete multiplayer game
Data API
Learn advanced data management and sharing techniques
Best Practices Summary
Library Integration
Use standard import methods and initialize in view constructor
Event Handling
Implement proper throttling and convert coordinates carefully
State Management
Keep 3D objects in sync with Multisynq model state
Performance
Optimize rendering, reuse geometry, and manage memory properly