In this tutorial, we will guide you through integrating real-time collaboration features into a Three.js application using SuperViz. Real-time collaboration allows multiple users to interact within a 3D environment simultaneously, making it ideal for applications such as design, games or interactive education.
We'll demonstrate how to set up a Three.js scene and integrate SuperViz to enable features like real-time presence, contextual comments, and collaborative navigation within the 3D space. By the end of this tutorial, you'll have a fully functional Three.js application with real-time collaboration capabilities that you can extend and customize to meet your specific needs.
Let's get started!
Prerequisite
To follow this tutorial, you will need a SuperViz account and a developer token. If you already have an account and a developer token, you can move on to the next step.
Create an account
To create an account, go to https://dashboard.superviz.com/register and create an account using either Google or an email/password. It's important to note that when using an email/password, you will receive a confirmation link that you'll need to click to verify your account.
Retrieving a Developer Token
To use the SDK, you’ll need to provide a developer token, as this token is essential for associating SDK requests with your account. You can retrieve both development and production SuperViz tokens from the dashboard..
Copy and save the developer token, as you will need it in the next steps of this tutorial.
Step 1: Set Up Your React Application
To begin, you'll need to set up a new React project where we will integrate SuperViz and Three.js for real-time collaboration.
1. Create a New React Project
First, create a new React application using Vite with TypeScript.
1npm create vite@latest three-js -- --template react-ts2cd three-js
Vite is a modern build tool that provides a faster and more efficient development experience compared to Create React App. It also supports TypeScript out of the box.
2. Install Required Libraries
Next, install the necessary libraries for our project:
1npm install @superviz/sdk three @superviz/threejs-plugin uuid
- @superviz/sdk: For integrating real-time collaboration features.
- three: The core library for creating 3D graphics in the browser.
- @superviz/threejs-plugin: A plugin for adding SuperViz features like presence and comments to a Three.js scene.
- uuid: A library for generating unique identifiers, useful for creating unique participant IDs.
3. Configure tailwind
In this tutorial, we'll use the Tailwind css framework. First, install the tailwind package.
1npm install -D tailwindcss postcss autoprefixer2npx tailwindcss init -p
We then need to configure the template path. Open tailwind.config.js
in the root of the project and insert the following code.
1/** @type {import('tailwindcss').Config} */2export default {3content: [4"./index.html",5"./src/**/*.{js,ts,jsx,tsx}",6],7theme: {8extend: {},9},10plugins: [],11}
Then we need to add the tailwind directives to the global CSS file. (src/index.css)
1@tailwind base;2@tailwind components;3@tailwind utilities;
4. Set Up Environment Variables
Create a .env
file in your project root and add your SuperViz developer key. This key will be used to authenticate your application with SuperViz services.
1VITE_SUPERVIZ_API_KEY=YOUR_SUPERVIZ_DEVELOPER_KEY
Step 2: Implement the Main Application
In this step, we'll implement the main application logic to initialize SuperViz and integrate it with a Three.js scene for real-time collaboration.
1. Implement the App Component
Open src/App.tsx
and set up the main application component using SuperViz to manage the collaborative 3D environment.
1import { v4 as generateId } from 'uuid';2import { useCallback, useEffect } from "react";3import SuperVizRoom, { Comments } from '@superviz/sdk';4import * as THREE from 'three';5import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment.js";6import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";7import { GLTF, GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";8import { Presence3D, ThreeJsPin } from '@superviz/threejs-plugin';
Explanation:
- Imports: Import necessary components from React, SuperViz, Three.js, and plugins for managing state, rendering the 3D scene, and integrating SuperViz features.
2. Define Constants
Define constants for the API key, room ID, and participant ID.
1const apiKey = import.meta.env.VITE_SUPERVIZ_API_KEY as string;23const ROOM_ID = 'threejs-tutorial';4const PLAYER_ID = generateId();
Explanation:
- apiKey: Retrieves the SuperViz API key from environment variables.
- ROOM_ID & PLAYER_ID: Defines the room ID for the SuperViz session and generates a unique player ID for each participant.
3. Create the App Component
Set up the main App
component and initialize the Three.js scene along with SuperViz.
1export default function App() {2useEffect(() => {3initializeThreeJs();4}, []);
Explanation:
- useEffect: Calls the
initializeThreeJs
function once when the component mounts to set up the Three.js scene and SuperViz.
4. Initialize SuperViz
Create a function to initialize SuperViz and integrate presence and comments features into the Three.js scene.
1const initializeSuperViz = useCallback(async (scene: THREE.Scene, camera: THREE.Camera, renderer: THREE.Renderer) => {2const superviz = await SuperVizRoom(apiKey, {3roomId: ROOM_ID,4participant: {5id: PLAYER_ID,6name: 'player-name',7},8group: {9id: 'threejs-tutorial',10name: 'threejs-tutorial',11}12});1314const presence = new Presence3D(scene, camera, camera);15const pinAdapter = new ThreeJsPin(scene, renderer, camera);16const comments = new Comments(pinAdapter, {17buttonLocation: 'top-right',18});1920superviz.addComponent(presence);21superviz.addComponent(comments);22}, [])
Explanation:
- initializeSuperViz: Initializes SuperViz with the Three.js scene, adding real-time presence and contextual comments features.
- Presence3D: Displays real-time presence information, showing where each participant is interacting within the 3D space.
- ThreeJsPin & Comments: Enables users to pin comments to specific locations in the 3D model, enhancing collaborative review sessions.
5. Initialize Three.js
Create a function to set up the Three.js scene, including the camera, lighting, controls, and loading a 3D model.
1const initializeThreeJs = useCallback(async () => {2const canvas = document.getElementById('showcase') as HTMLCanvasElement;3const width = canvas.clientWidth;4const height = canvas.clientHeight;56const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });7renderer.setSize(width, height);89const pmremGenerator = new THREE.PMREMGenerator(renderer);10const scene = new THREE.Scene();11scene.background = new THREE.Color(0xb6b7b8);12scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture;1314const camera = new THREE.PerspectiveCamera(60, width / height, 0.1, 300);15camera.position.set(2, 0, 2);1617const controls = new OrbitControls(camera, renderer.domElement);18controls.target.set(0, 0.5, 0);19controls.update();20controls.enablePan = false;21controls.enableDamping = true;2223const loader = new GLTFLoader();24loader.load(25"https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/GlamVelvetSofa/glTF-Binary/GlamVelvetSofa.glb",26(model: GLTF) => {27scene.add(model.scene);28initializeSuperViz(scene, camera, renderer);29},30undefined,31(e: unknown) => {32console.error(e);33}34);3536const animate = () => {37requestAnimationFrame(animate);38controls.update();39renderer.render(scene, camera);40};4142animate();43}, [initializeSuperViz]);
Explanation:
- initializeThreeJs: Sets up the Three.js scene, loads a 3D model, and then initializes SuperViz for real-time collaboration.
- GLTFLoader: Loads a 3D model (in this case, a sofa) into the scene.
- animate: Continuously updates the scene, ensuring smooth interaction and rendering.
Step 3: Render the 3D Scene
Finally, return the JSX structure for rendering the Three.js scene and interacting with it in real-time.
1return (2<div className='w-full h-full bg-gray-200 flex items-center justify-center flex-col'>3<header className='w-full p-5 bg-purple-400 flex items-center justify-between'>4<h1 className='text-white text-2xl font-bold'>SuperViz Three.JS</h1>5</header>6<main className='w-full h-full flex items-center justify-center relative'>7<canvas id='showcase' className='w-full h-full' />8</main>9</div>10);
Explanation:
- Viewer Container: The
showcase
canvas is where the Three.js scene will be rendered, filling the entire screen. This is where users will interact with the 3D space and collaborate in real-time.
Step 4: Understanding the Project Structure
Here's a quick overview of how the project structure supports real-time collaboration in a Three.js application:
App.tsx
:- Initializes SuperViz and sets up real-time collaboration features, including presence and comments.
- Handles the Three.js scene setup and loading of 3D models.
- 3D Scene: Renders a basic Three.js scene with interactive objects. Supports real-time interaction and updates using SuperViz.
- Real-Time Communication: Manages real-time communication between participants using SuperViz.
Step 5: Running the Application
1. Start the React Application
To run your application, use the following command in your project directory:
1npm run dev
This command will start the development server and open your application in the default web browser. You can interact with the 3D scene and see real-time presence and comments from other participants.
2. Test the Application
- Real-Time Collaboration: Open the application in multiple browser windows or tabs to simulate multiple participants and verify that presence and comments are synchronized in real-time.
- Collaborative Interaction: Test the responsiveness of the application by interacting with the 3D scene and observing how it updates for all participants.
Summary
In this tutorial, we built a Three.js application with real-time collaboration features using SuperViz. We configured a React application to handle real-time interaction within a 3D scene, enabling multiple users to collaborate seamlessly. This setup can be extended and customized to fit various scenarios where real-time collaboration in a 3D environment is essential.
Feel free to explore the full code and further examples in the GitHub repository for more details.