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 Vite 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/room @superviz/collaboration @superviz/autodesk-viewer-plugin uuid
- @superviz/room: Core package for creating and managing SuperViz rooms.
- @superviz/collaboration: Contains collaboration components like comments.
- @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_FORGE_CLIENT_ID=YOUR_AUTODESK_CLIENT_ID3VITE_FORGE_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. Import Required Packages
Open src/App.tsx
and add the necessary imports for our application:
1import { createRoom, Room } from "@superviz/room";2import { v4 as generateId } from "uuid";34import { useCallback, useEffect, useRef } from "react";5import { Presence3D, AutodeskPin } from "@superviz/autodesk-viewer-plugin";6import { Comments } from "@superviz/collaboration";78// SuperViz developer token ::9const DEVELOPER_TOKEN = import.meta.env.VITE_SUPERVIZ_API_KEY;10const CLIENT_ID = import.meta.env.VITE_FORGE_CLIENT_ID as string11const CLIENT_SECRET = import.meta.env.VITE_FORGE_CLIENT_SECRET as string12const DOCUMENT_ID = `urn:${btoa("urn:adsk.objects:os.object:e8d17563-1a4e-4471-bd72-a0a7e8d719bc/fileifc.ifc")}`
Explanation:
- createRoom, Room: From SuperViz room package for creating and managing rooms.
- Presence3D, AutodeskPin: From the Autodesk viewer plugin to add presence and pinning capabilities.
- Comments: From the collaboration package to enable contextual comments.
- Environment Variables: We're accessing our SuperViz and Forge credentials using environment variables.
- DOCUMENT_ID: The URN for the Autodesk model we want to load.
2. Create the App Component
Set up the main App
component with necessary refs:
1const App = () => {2const autodeskViewer = useRef<Autodesk.Viewing.GuiViewer3D | null>(null)3const roomRef = useRef<Room | null>(null);
Explanation:
- autodeskViewer: A ref to store the Autodesk Viewer instance.
- roomRef: A ref to store the SuperViz room instance.
3. Initialize SuperViz
Create a function to initialize the SuperViz environment and integrate presence and comments:
1// Initialize ::2const initializeSuperViz = useCallback(async () => {3try {4const room = await createRoom({5developerToken: DEVELOPER_TOKEN,6roomId: "ROOM_ID",7participant: {8id: generateId(),9name: "Name " + Math.floor(Math.random() * 10),10},11group: {12id: "GROUP_ID",13name: "GROUP_NAME",14},15});1617// Store the room instance in the ref18roomRef.current = room;1920const presence = new Presence3D(autodeskViewer.current!,{21isAvatarsEnabled: false22})23room.addComponent(presence)2425const pinAdapter = new AutodeskPin(autodeskViewer.current!)26const comments = new Comments(pinAdapter, {27buttonLocation: 'top-right',28})29room.addComponent(comments)3031} catch (error) {32console.error("Error initializing SuperViz Room:", error);33}34}, []);
Explanation:
- createRoom: Creates a new SuperViz room with participant and group information.
- roomRef.current: Stores the room instance for later access.
- Presence3D: Adds 3D presence to the Autodesk viewer with avatars disabled.
- AutodeskPin & Comments: Enables participants to place pins and leave comments on specific parts of the 3D model.
4. Handle Document Loading
Create functions to handle document loading success and failure:
1const onDocumentLoadSuccess = useCallback(async (document: Autodesk.Viewing.Document) => {2const viewable = document.getRoot().getDefaultGeometry();34if(!viewable) return56try {7await autodeskViewer.current!.loadDocumentNode(document, viewable, {8applyScaling: 'meters'9})1011await initializeSuperViz()12} catch (error) {13console.log('Document loaded failed', error)14}1516}, [initializeSuperViz])1718const onDocumentLoadFailure = () => {19console.log('Document loaded failed')20}
Explanation:
- onDocumentLoadSuccess: When the document loads successfully, we get the default geometry and load it into the viewer, then initialize SuperViz.
- onDocumentLoadFailure: Handles any errors that occur during document loading.
5. Initialize Autodesk Viewer
Create a function to initialize the Autodesk Viewer with authentication and configuration:
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(`${CLIENT_ID}:${CLIENT_SECRET}`)}`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 = viewer37window.Autodesk.Viewing.Document.load(DOCUMENT_ID, onDocumentLoadSuccess, onDocumentLoadFailure);38})3940}, [onDocumentLoadSuccess])
Explanation:
- Authentication: Fetches an access token from Autodesk's authentication API using our client credentials.
- Initializing the Viewer: Sets up the Autodesk Viewer with the access token and configures it with optimized settings.
- Loading the Document: Loads the specified model and calls the appropriate success or failure handlers.
6. Initialize on Component Mount
Use an effect hook to initialize the Autodesk Viewer when the component mounts:
1useEffect(() => {2initializeAutodesk();3}, [initializeAutodesk]);
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);11};1213export default App;
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 Application Structure
Here's a quick overview of how the application components work together:
- Room Creation and Management
- The application creates a SuperViz room using the createRoom function.
- It stores the room instance in a ref for later access.
- Autodesk Viewer Integration
- The application authenticates with Autodesk's API and initializes the 3D viewer.
- It loads the specified model and configures the viewer for optimal performance.
- SuperViz Components
- Presence3D: Shows where participants are looking in the 3D model, with avatars disabled for a cleaner interface.
- AutodeskPin & Comments: Enables participants to place pins and add comments to specific locations in the 3D model.
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 used the @superviz/room package to create a room, the @superviz/collaboration package for comments, and the @superviz/autodesk-viewer-plugin for presence and pinning capabilities. This setup enables multiple users to collaborate seamlessly in a shared 3D model, seeing each other's positions and adding contextual comments.
Feel free to explore the full code and further examples in the GitHub repository for more details.