forked from apprentice3d/ForgeViewerExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turnTableExt.js
79 lines (60 loc) · 2.42 KB
/
turnTableExt.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
///////////////////////////////////////////////////////////////////////////////
// TurnTable extension illustrating camera rotation around the model
// by Denis Grigor, November 2018
//
///////////////////////////////////////////////////////////////////////////////
class TurnTableExtension extends Autodesk.Viewing.Extension {
constructor(viewer, options) {
super(viewer, options);
this.viewer = viewer;
this.customize = this.customize.bind(this);
}
load() {
console.log('TurnTableExtension is loaded!');
this.viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT,
this.customize);
return true;
}
unload() {
console.log('TurnTableExtension is now unloaded!');
return true;
}
customize() {
this.viewer.removeEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT,
this.customize);
//Start coding here ...
let viewer = this.viewer;
viewer.hide(370);
let turnTableToolbarButton = new Autodesk.Viewing.UI.Button('turnTableButton');
turnTableToolbarButton.addClass('toolbarCameraRotation');
turnTableToolbarButton.setToolTip('Start/Stop Camera rotation');
// SubToolbar
this.subToolbar = new Autodesk.Viewing.UI.ControlGroup('CameraRotateToolbar');
this.subToolbar.addControl(turnTableToolbarButton);
this.viewer.toolbar.addControl(this.subToolbar);
let started = false;
let rotateCamera = () => {
if (started) {
requestAnimationFrame(rotateCamera);
}
const nav = viewer.navigation;
const up = nav.getCameraUpVector();
const axis = new THREE.Vector3(0, 0, 1);
const speed = 10.0 * Math.PI / 180;
const matrix = new THREE.Matrix4().makeRotationAxis(axis, speed * 0.1);
let pos = nav.getPosition();
pos.applyMatrix4(matrix);
up.applyMatrix4(matrix);
nav.setView(pos, new THREE.Vector3(0, 0, 0));
nav.setCameraUpVector(up);
var viewState = viewer.getState();
// viewer.restoreState(viewState);
};
turnTableToolbarButton.onClick = function (e) {
started = !started;
if (started) rotateCamera()
};
}
}
Autodesk.Viewing.theExtensionManager.registerExtension('TurnTable',
TurnTableExtension);