forked from Kitware/vtk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (131 loc) · 5.89 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import 'vtk.js/Sources/favicon';
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import 'vtk.js/Sources/Rendering/Profiles/Geometry';
import macro from 'vtk.js/Sources/macro';
import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkConeSource from 'vtk.js/Sources/Filters/Sources/ConeSource';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkInteractorStyleManipulator from 'vtk.js/Sources/Interaction/Style/InteractorStyleManipulator';
import vtkMouseCameraTrackballMultiRotateManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballMultiRotateManipulator';
import vtkMouseCameraTrackballPanManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballPanManipulator';
import vtkMouseCameraTrackballRollManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballRollManipulator';
import vtkMouseCameraTrackballRotateManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballRotateManipulator';
import vtkMouseCameraTrackballZoomManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballZoomManipulator';
import vtkMouseCameraTrackballZoomToMouseManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballZoomToMouseManipulator';
import vtkGestureCameraManipulator from 'vtk.js/Sources/Interaction/Manipulators/GestureCameraManipulator';
import controlPanel from './controller.html';
const { vtkDebugMacro } = macro;
// ----------------------------------------------------------------------------
// Standard rendering code setup
// ----------------------------------------------------------------------------
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------
const interactorStyle = vtkInteractorStyleManipulator.newInstance();
fullScreenRenderer.getInteractor().setInteractorStyle(interactorStyle);
// ----------------------------------------------------------------------------
// Source, actor and mapper
// ----------------------------------------------------------------------------
const coneSource = vtkConeSource.newInstance({ height: 1.0 });
const mapper = vtkMapper.newInstance();
mapper.setInputConnection(coneSource.getOutputPort());
const actor = vtkActor.newInstance();
actor.setMapper(mapper);
renderer.addActor(actor);
renderer.resetCamera();
renderWindow.render();
// -----------------------------------------------------------
// UI control handling
// -----------------------------------------------------------
fullScreenRenderer.addController(controlPanel);
const uiComponents = {};
const selectMap = {
leftButton: { button: 1 },
middleButton: { button: 2 },
rightButton: { button: 3 },
shiftLeftButton: { button: 1, shift: true },
shiftMiddleButton: { button: 2, shift: true },
shiftRightButton: { button: 3, shift: true },
controlLeftButton: { button: 1, control: true },
controlMiddleButton: { button: 2, control: true },
controlRightButton: { button: 3, control: true },
altLeftButton: { button: 1, alt: true },
altMiddleButton: { button: 2, alt: true },
altRightButton: { button: 3, alt: true },
scrollMiddleButton: { scrollEnabled: true, dragEnabled: false },
shiftScrollMiddleButton: {
scrollEnabled: true,
dragEnabled: false,
shift: true,
},
controlScrollMiddleButton: {
scrollEnabled: true,
dragEnabled: false,
control: true,
},
altScrollMiddleButton: {
scrollEnabled: true,
dragEnabled: false,
alt: true,
},
};
const manipulatorFactory = {
None: null,
Pan: vtkMouseCameraTrackballPanManipulator,
Zoom: vtkMouseCameraTrackballZoomManipulator,
Roll: vtkMouseCameraTrackballRollManipulator,
Rotate: vtkMouseCameraTrackballRotateManipulator,
MultiRotate: vtkMouseCameraTrackballMultiRotateManipulator,
ZoomToMouse: vtkMouseCameraTrackballZoomToMouseManipulator,
};
function reassignManipulators() {
interactorStyle.removeAllMouseManipulators();
Object.keys(uiComponents).forEach((keyName) => {
const klass = manipulatorFactory[uiComponents[keyName].manipName];
if (klass) {
const manipulator = klass.newInstance();
manipulator.setButton(selectMap[keyName].button);
manipulator.setShift(!!selectMap[keyName].shift);
manipulator.setControl(!!selectMap[keyName].control);
manipulator.setAlt(!!selectMap[keyName].alt);
if (selectMap[keyName].scrollEnabled !== undefined) {
manipulator.setScrollEnabled(selectMap[keyName].scrollEnabled);
}
if (selectMap[keyName].dragEnabled !== undefined) {
manipulator.setDragEnabled(selectMap[keyName].dragEnabled);
}
interactorStyle.addMouseManipulator(manipulator);
}
});
// Always add gesture
interactorStyle.addGestureManipulator(
vtkGestureCameraManipulator.newInstance()
);
}
Object.keys(selectMap).forEach((name) => {
const elt = document.querySelector(`.${name}`);
elt.addEventListener('change', (e) => {
vtkDebugMacro(`Changing action of ${name} to ${e.target.value}`);
uiComponents[name].manipName = e.target.value;
reassignManipulators();
});
uiComponents[name] = {
elt,
manipName: elt.value,
};
});
// Populate with initial manipulators
reassignManipulators();
// -----------------------------------------------------------
// Make some variables global so that you can inspect and
// modify objects in your browser's developer console:
// -----------------------------------------------------------
global.source = coneSource;
global.mapper = mapper;
global.actor = actor;
global.renderer = renderer;
global.renderWindow = renderWindow;