Skip to main content
This tutorial demonstrates how to integrate powerful third-party libraries like Three.js with Multisynq for 3D rendering. You’ll build a 3D bouncing ball simulation where balls bounce off invisible walls and an interactive central sphere that can be dragged and clicked.

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: CodePen Settings

Three.js Scene Initialization

Basic Scene Setup

Scene Components

Container for all 3D objects, lights, and cameras
Ambient light for overall illumination + point light for shadows
Perspective camera positioned to view the bouncing balls
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 using q_ 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

Convert Three.js objects to plain JavaScript arrays before publishing as events. Multisynq doesn’t know how to serialize external library objects like THREE.Vector3.

Render Loop Integration

Controlled Rendering

Instead of using requestAnimationFrame 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

Limit pointer events to 20 per second to reduce network load
Create geometry once and reuse for multiple objects
Only update 3D objects when model state actually changes
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 conflicts

Performance 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
This tutorial demonstrates how to create sophisticated 3D collaborative experiences by combining Multisynq’s synchronization capabilities with Three.js’s powerful 3D rendering. The same patterns can be applied to other 3D libraries and more complex applications.