DOCS

How to use a Three.js Camera Controller?

3dverse has a built-in Camera controller implemented server-side which is configurable through the Livelink SDK:

For more control over client side interaction, you can de-activate the default Camera controller and use a Three.js camera controller.

This example uses the https://github.com/yomotsu/camera-controls open-source library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as THREE from 'three';
import CameraControls from 'camera-controls';
export async function setupCameraController(canvas: HTMLCanvasElement) {
CameraControls.install({ THREE: THREE });
// create THREE camera
const viewport = SDK3DVerse.engineAPI.cameraAPI.getViewports()[0];
const { fovy, aspectRatio, nearPlane, farPlane } = viewport.getProjection();
const camera = new THREE.PerspectiveCamera(fovy, aspectRatio, nearPlane, farPlane || 100000);
const clock = new THREE.Clock();
// create camera controls
const cameraControls = new CameraControls(camera, canvas);
// set the orbit point to the center of the scene
const { min, max } = await SDK3DVerse.webAPI.getSceneAABB();
const center = [
(min[0] + max[0]) / 2,
(min[1] + max[1]) / 2,
(min[2] + max[2]) / 2,
];
cameraControls.setOrbitPoint(center[0], center[1], center[2]);
// on cameraControls update, we'll update the 3dverse camera
function onCameraUpdate() {
const cameraPosition = cameraControls.camera.position.toArray();
const cameraOrientation = new THREE.Quaternion();
cameraControls.camera.getWorldQuaternion(cameraOrientation);
const cameraOrientationArray = cameraOrientation.toArray();
viewport.setLocalTransform({
position: cameraPosition,
orientation: cameraOrientationArray,
});
};
// listen to camera update events
cameraControls.addEventListener('update', onCameraUpdate);
// call onCameraUpdate to set the initial camera position
onCameraUpdate();
// animate the camera
(function anim() {
const delta = clock.getDelta();
cameraControls.update(delta);
requestAnimationFrame(anim);
})();
};

Deactivate the default controller and setup three.js controller

1
2
3
4
5
6
7
8
9
10
11
12
const initApp = async () => {
await SDK3DVerse.joinOrStartSession({
userToken: PUBLIC_TOKEN,
sceneUUID: SCENE_UUID,
canvas: document.getElementById('display-canvas'),
viewportProperties: {
defaultControllerType: SDK3DVerse.controller_type.none,
},
});
setupCameraController(canvasRef.current!);
};
Previous
Detect when an entity is clicked on