In this tutorial, we will guide you through adding real-time presence features to an Autodesk APS Viewer application using SuperViz. Real-time presence allows multiple users to interact with a 3D model collaboratively, seeing each other's actions and comments in real time. This feature is particularly useful for design review sessions, collaborative planning, and any scenario where real-time feedback on 3D models is essential.
We'll demonstrate how to set up an Autodesk APS Viewer with SuperViz, enabling participants to share their presence in a 3D space, leave comments, and interact with the model collaboratively. This setup will allow multiple participants to join a session, see each other's positions and interactions within the 3D model, and add contextual comments. 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 Autodesk Viewer.
1. Create a New React Project
First, create a new React application using Create React App with TypeScript.
1npm create vite@latest presence-autodesk-app -- --template react-ts2cd presence-autodesk-app
2. Install Required Libraries
Next, install the necessary libraries for our project:
1npm install @superviz/sdk @superviz/autodesk-viewer-plugin uuid
- @superviz/sdk: SDK for integrating real-time collaboration features, including presence.
- @superviz/autodesk-viewer-plugin: Plugin for adding SuperViz presence features to an Autodesk Viewer application.
- 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, Autodesk client ID, and client secret. These credentials will be used to authenticate your application with SuperViz and Autodesk services.
1VITE_SUPERVIZ_API_KEY=YOUR_SUPERVIZ_DEVELOPER_KEY2VITE_AUTODESK_CLIENT_ID=YOUR_AUTODESK_CLIENT_ID3VITE_AUTODESK_CLIENT_SECRET=YOUR_AUTODESK_CLIENT_SECRET
Step 2: Implement the Main Application
In this step, we'll implement the main application logic to initialize SuperViz and Autodesk Viewer, and handle real-time presence and comments.
1. Implement the App Component
Open src/App.tsx
and set up the main application component using SuperViz to manage the collaborative environment.
1import { v4 as generateId } from 'uuid';2import { useCallback, useEffect, useRef } from "react";3import SuperVizRoom, { Comments } from '@superviz/sdk';4import { Presence3D, AutodeskPin } from '@superviz/autodesk-viewer-plugin';
Explanation:
- Imports: Import necessary components from React, SuperViz SDK, and Autodesk Viewer Plugin for managing state, initializing SuperViz, and enabling real-time presence and comments.
2. Define Constants
Define constants for the API key, Autodesk credentials, and room ID.
1const apiKey = import.meta.env.VITE_SUPERVIZ_API_KEY as string;2const clientId = import.meta.env.VITE_AUTODESK_CLIENT_ID as string;3const clientSecret = import.meta.env.VITE_AUTODESK_CLIENT_SECRET as string;4const documentId = `urn:${btoa("urn:adsk.objects:os.object:e8d17563-1a4e-4471-bd72-a0a7e8d719bc/fileifc.ifc")}`;56const ROOM_ID = 'presence-autodesk';7const PLAYER_ID = generateId();
Explanation:
- apiKey: Retrieves the SuperViz API key from environment variables.
- clientId & clientSecret: Autodesk credentials needed to authenticate and access Autodesk services.
- documentId: Encodes and sets the document ID for the 3D model to be loaded in the Autodesk Viewer.
- ROOM_ID & PLAYER_ID: Defines the room ID for the SuperViz session and generates a unique player ID.
3. Create the App Component
Set up the main App
component and initialize references for the Autodesk Viewer.
1export default function App() {2const autodeskViewer = useRef<Autodesk.Viewing.GuiViewer3D | null>(null);34useEffect(() => {5initializeAutodesk();6}, []);
Explanation:
- autodeskViewer: A ref to store the Autodesk Viewer instance.
- useEffect: Calls the
initializeAutodesk
function once when the component mounts, setting up the Autodesk Viewer.
4. Initialize SuperViz
Create a function to initialize the SuperViz environment and integrate presence and comments.
1const initializeSuperViz = useCallback(async () => {2const superviz = await SuperVizRoom(apiKey, {3roomId: ROOM_ID,4participant: {5id: PLAYER_ID,6name: 'player-name',7},8group: {9id: 'presence-autodesk',10name: 'presence-autodesk',11}12});1314const presence = new Presence3D(autodeskViewer.current!);15superviz.addComponent(presence);1617const pinAdapter = new AutodeskPin(autodeskViewer.current!);18const comments = new Comments(pinAdapter, {19buttonLocation: 'top-right',20});21superviz.addComponent(comments);22}, []);
Explanation:
- initializeSuperViz: An asynchronous function that initializes the SuperViz room, adds the presence and commenting components to the Autodesk Viewer.
- Presence3D: Manages real-time presence in the 3D model, showing where each participant is looking or interacting.
- AutodeskPin & Comments: Allows participants to pin comments to specific locations in the 3D model, facilitating collaborative review sessions.
5. Handle Document Loading Success
Create a function to handle the successful loading of the Autodesk document.
1const onDocumentLoadSuccess = useCallback(async (document: Autodesk.Viewing.Document) => {2const viewable = document.getRoot().getDefaultGeometry();34if(!viewable) return;56try {7await autodeskViewer.current!.loadDocumentNode(document, viewable, {8applyScaling: 'meters'9});1011await initializeSuperViz();12} catch (error) {13console.log('Document loaded failed', error);14}1516}, [initializeSuperViz]);
Explanation:
- onDocumentLoadSuccess: Handles the successful loading of the 3D model, loading the document into the viewer and initializing SuperViz.
- initializeSuperViz: Called after the document is successfully loaded to set up presence and comments.
6. Handle Document Loading Failure
Create a function to handle document loading failures.
1const onDocumentLoadFailure = () => {2console.log('Document loaded failed');3};
Explanation:
- onDocumentLoadFailure: Logs an error if the document fails to load into the Autodesk Viewer.
7. Initialize Autodesk Viewer
Create a function to initialize the Autodesk Viewer with the necessary credentials and configurations.
1const initializeAutodesk = useCallback(async () => {2const viewerElement = document.getElementById('viewer')!;34const response = await fetch('https://developer.api.autodesk.com/authentication/v2/token', {5method: 'POST',6headers: {7'Content-Type': 'application/x-www-form-urlencoded',8'Authorization': `Basic ${btoa(`${clientId}:${clientSecret}`)}`9},10body: new URLSearchParams({11grant_type: 'client_credentials',12scope: 'data:read bucket:read',13}).toString()14});1516const data = await response.json();1718const options = {19env: 'AutodeskProduction2',20api: 'streamingV2',21accessToken: data.access_token,22};2324window.Autodesk.Viewing.Initializer(options, async () => {25const viewer = new window.Autodesk.Viewing.GuiViewer3D(viewerElement);26await viewer.start();2728viewer.setTheme("dark-theme");29viewer.setQualityLevel(false, false);30viewer.setGhosting(false);31viewer.setGroundShadow(false);32viewer.setGroundReflection(false);33viewer.setOptimizeNavigation(true);34viewer.setProgressiveRendering(true);3536autodeskViewer.current = viewer;37window.Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);38});3940}, [onDocumentLoadSuccess]);
Explanation:
- initializeAutodesk: Fetches an access token from Autodesk, initializes the Autodesk Viewer, and loads the specified document. If successful, it sets up the Autodesk Viewer with optimized settings for the session.
Step 3: Render the Application
Finally, return the JSX structure for rendering the Autodesk Viewer and the SuperViz-powered interface.
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 Presence Autodesk</h1>5</header>6<main className='w-full h-full flex items-center justify-center relative'>7<div id='viewer' className='w-full h-full overflow-hidden absolute top-0 left-0 w-full! h-full! z-0'></div>8</main>9</div>10);
Explanation:
- Viewer Container: The
viewer
div is where the Autodesk Viewer will be rendered, filling the entire screen. This is where users will interact with the 3D model and collaborate in real time.
Step 4: Understanding the Project Structure
Here's a quick overview of how the project structure supports real-time presence and commenting in an Autodesk Viewer application:
App.tsx
- Initializes the SuperViz environment.
- Sets up the Autodesk Viewer with real-time presence and commenting features.
- Handles the loading of 3D models and integration of collaborative tools.
- Autodesk Viewer
- Renders the 3D model, allowing users to navigate, inspect, and collaborate in a shared virtual space.
- SuperViz Components
- Presence3D: Displays real-time presence information, showing where each participant is looking or interacting in the 3D model.
- AutodeskPin & Comments: Enables users to add comments to specific locations in the 3D model, enhancing collaborative review sessions.
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 model and see real-time presence and comments from other participants.
2. Test the Application
- Real-Time Presence: Open the application in multiple browser windows or tabs to simulate multiple participants and verify that presence information is updated in real-time.
- Collaborative Interaction: Test the commenting feature by adding comments to the 3D model and observing how they appear for other participants.
Summary
In this tutorial, we integrated real-time presence and commenting features into an Autodesk Viewer application using SuperViz. We configured a React application to handle 3D model viewing, enabling multiple users to collaborate seamlessly in a shared virtual space. This setup can be extended and customized to fit various scenarios where real-time collaboration on 3D models is required.
Feel free to explore the full code and further examples in the GitHub repository for more details.