Three.js Integration
Full end-to-end VPS integration with Three.js using ThreeAdapter
Installation
npm install @multisetai/vps threeComplete Working Example
import * as THREE from 'three';
import { MultisetClient, XRSessionManager } from '@multisetai/vps/core';
import { ThreeAdapter } from '@multisetai/vps/three';
// --- Scene setup ---
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = null; // transparent so the camera feed shows through
const camera = new THREE.PerspectiveCamera(
70,
window.innerWidth / window.innerHeight,
0.01,
100
);
scene.add(camera);
scene.add(new THREE.AmbientLight(0xffffff, 1));
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// --- Multiset client ---
const client = new MultisetClient({
clientId: import.meta.env.VITE_MULTISET_CLIENT_ID,
clientSecret: import.meta.env.VITE_MULTISET_CLIENT_SECRET,
mapType: 'map',
code: import.meta.env.VITE_MAP_CODE,
});
await client.authorize();
// --- XR session ---
const session = new XRSessionManager(
renderer.getContext() as WebGL2RenderingContext,
{
client,
autoLocalize: true,
onLocalizationFailure: (reason) => console.warn('Localization failed:', reason),
onError: (error) => console.error('Session error:', error),
}
);
// --- Three.js adapter ---
const adapter = new ThreeAdapter({
session,
renderer,
scene,
camera,
showMesh: true, // download and display the map mesh after localization
showGizmo: true, // show transform gizmo at map origin
onLocalizationSuccess: (result, worldFromMap) => {
// worldFromMap is a THREE.Matrix4 that converts map-local to world coordinates.
// Use it to place objects at specific map-local positions.
const box = new THREE.Mesh(
new THREE.BoxGeometry(0.3, 0.3, 0.3),
new THREE.MeshStandardMaterial({ color: 0x0099ff })
);
// Place the box at map origin
box.applyMatrix4(worldFromMap);
scene.add(box);
console.log('Confidence:', result.localizeData.confidence);
},
});
// --- Start ---
// initialize() starts the preview loop and mounts the AR button
adapter.initialize();Placing Content at Map Coordinates
MapSet Mode
Manual Localization (No Auto-Localize)
Custom AR Button
Frame-Level Access
Cleanup
Last updated
Was this helpful?

