-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathorbital-camera.ts
More file actions
337 lines (276 loc) · 11.8 KB
/
orbital-camera.ts
File metadata and controls
337 lines (276 loc) · 11.8 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import {Component, Object3D} from '@wonderlandengine/api';
import {property} from '@wonderlandengine/api/decorators.js';
import {deg2rad, rad2deg} from './utils/utils.js';
import {quat, vec3} from 'gl-matrix';
const preventDefault = (e: Event) => {
e.preventDefault();
};
const tempVec = [0, 0, 0];
const tempquat = quat.create();
const tempquat2 = quat.create();
const tempVec3 = vec3.create();
/**
* OrbitalCamera component allows the user to orbit around a target point, which
* is the position of the object itself. It rotates at the specified distance.
*
* @remarks
* The component works using mouse or touch. Therefor it does not work in VR.
*/
export class OrbitalCamera extends Component {
static TypeName = 'orbital-camera';
@property.int()
mouseButtonIndex = 0;
@property.float(5)
radial = 5;
@property.float()
minElevation = 0.0;
@property.float(89.99)
maxElevation = 89.99;
@property.float()
minZoom = 0.01;
@property.float(10.0)
maxZoom = 10.0;
@property.float(0.5)
xSensitivity = 0.5;
@property.float(0.5)
ySensitivity = 0.5;
@property.float(0.02)
zoomSensitivity = 0.02;
@property.float(0.9)
damping = 0.9;
@property.object()
target: Object3D | null = null;
private _mouseDown: boolean = false;
private _origin = vec3.create();
private _originCache = vec3.create();
private _azimuth = 0;
private _polar = 45;
private _initialPinchDistance: number = 0;
private _touchStartX: number = 0;
private _touchStartY: number = 0;
private _azimuthSpeed: number = 0;
private _polarSpeed: number = 0;
init() {
if (this.target) {
this.target.getPositionWorld(this._origin);
} else {
this.object.getPositionWorld(this._origin);
}
}
start(): void {
this._polar = Math.min(this.maxElevation, Math.max(this.minElevation, this._polar));
this._updateCamera();
}
onActivate(): void {
const canvas = this.engine.canvas;
if (this.mouseButtonIndex === 2) {
canvas.addEventListener('contextmenu', preventDefault, {passive: false});
}
canvas.addEventListener('mousedown', this._onMouseDown);
canvas.addEventListener('wheel', this._onMouseScroll, {passive: false});
canvas.addEventListener('touchstart', this._onTouchStart, {passive: false});
canvas.addEventListener('touchmove', this._onTouchMove, {passive: false});
canvas.addEventListener('touchend', this._onTouchEnd);
}
onDeactivate(): void {
const canvas = this.engine.canvas;
if (this.mouseButtonIndex === 2) {
canvas.removeEventListener('contextmenu', preventDefault);
}
canvas.removeEventListener('mousemove', this._onMouseMove);
canvas.removeEventListener('mousedown', this._onMouseDown);
canvas.removeEventListener('wheel', this._onMouseScroll);
canvas.removeEventListener('touchstart', this._onTouchStart);
canvas.removeEventListener('touchmove', this._onTouchMove);
canvas.removeEventListener('touchend', this._onTouchEnd);
/* Reset state to make sure nothing gets stuck */
this._mouseDown = false;
this._initialPinchDistance = 0;
this._touchStartX = 0;
this._touchStartY = 0;
this._azimuthSpeed = 0;
this._polarSpeed = 0;
}
update(): void {
// Update the orbit center to the target's position if one is assigned.
if (this.target) {
this.target.getPositionWorld(this._origin);
}
/* Always apply damping, because there's no event for stop moving */
this._azimuthSpeed *= this.damping;
this._polarSpeed *= this.damping;
/* Stop completely if the speed is very low to avoid endless tiny movements */
if (Math.abs(this._azimuthSpeed) < 0.01) this._azimuthSpeed = 0;
if (Math.abs(this._polarSpeed) < 0.01) this._polarSpeed = 0;
/* Apply the speed to the camera angles */
this._azimuth += this._azimuthSpeed;
this._polar += this._polarSpeed;
/* Clamp the polar angle */
this._polar = Math.min(this.maxElevation, Math.max(this.minElevation, this._polar));
/* Update the camera if there's any speed */
if (
this._azimuthSpeed !== 0 ||
this._polarSpeed !== 0 ||
vec3.equals(this._origin, this._originCache) === false
) {
this._updateCamera();
vec3.copy(this._originCache, this._origin);
}
}
/**
* Get the closest position to the given position on the orbit sphere of the camera.
* This can be used to get a position and rotation to transition to.
*
* You pass this a position object. The method calculates the closest positition and updates the position parameter.
* It also sets the rotation parameter to reflect the rotate the camera will have when it is on the orbit sphere,
* pointing towards the center.
* @param position the position to get the closest position to
* @param rotation the rotation to get the closest position to
*/
getClosestPosition(position: vec3, rotation: quat) {
/* It's a bit hacky, but the easiest way to get the rotation of the camera is just briefly
change the rotation to look at the center and then get the rotation. */
this.object.getRotationWorld(tempquat);
this.object.lookAt(this._origin);
this.object.getRotationWorld(tempquat2);
if (quat.dot(tempquat, tempquat2) < 0) {
quat.scale(tempquat2, tempquat2, -1); /* Negate to ensure shortest path */
}
this.object.setRotationWorld(tempquat);
/* Calculate the direction from the center of orbit to the current camera position */
const directionToCamera = vec3.create();
vec3.subtract(directionToCamera, position, this._origin as vec3);
vec3.normalize(directionToCamera, directionToCamera);
/* Scale this direction by the radius of your orbital sphere to get the nearest point on the sphere */
const nearestPointOnSphere = vec3.create();
vec3.scale(nearestPointOnSphere, directionToCamera, this.radial);
vec3.add(nearestPointOnSphere, nearestPointOnSphere, this._origin as vec3);
vec3.copy(position, nearestPointOnSphere);
quat.copy(rotation, tempquat2);
}
/**
* Set the camera position based on the given position and calculate the rotation.
* @param cameraPosition the position to set the camera to
*/
setPosition(cameraPosition: vec3) {
const centerOfOrbit = this._origin as vec3;
/* Compute the direction vector */
vec3.subtract(tempVec3, cameraPosition, centerOfOrbit);
vec3.normalize(tempVec3, tempVec3);
/* Compute the azimuth angle (in radians) */
const azimuth = Math.atan2(tempVec3[0], tempVec3[2]);
/* Compute the polar angle (in radians) */
const polar = Math.acos(tempVec3[1]);
const azimuthDeg = rad2deg(azimuth);
/* Polar is inverted to match the orbital camera */
const polarDeg = 90 - rad2deg(polar);
this._azimuth = azimuthDeg;
this._polar = polarDeg;
}
/**
* Update the camera position based on the current azimuth,
* polar and radial values
*/
private _updateCamera() {
const azimuthInRadians = deg2rad(this._azimuth);
const polarInRadians = deg2rad(this._polar);
tempVec[0] = this.radial * Math.sin(azimuthInRadians) * Math.cos(polarInRadians);
tempVec[1] = this.radial * Math.sin(polarInRadians);
tempVec[2] = this.radial * Math.cos(azimuthInRadians) * Math.cos(polarInRadians);
this.object.setPositionWorld(tempVec);
this.object.translateWorld(this._origin);
this.object.lookAt(this._origin);
}
/* Mouse Event Handlers */
private _onMouseDown = (e: MouseEvent) => {
window.addEventListener('mouseup', this._onMouseUp);
window.addEventListener('mousemove', this._onMouseMove);
if (e.button === this.mouseButtonIndex) {
this._mouseDown = true;
document.body.style.cursor = 'grabbing';
if (e.button === 1) {
e.preventDefault(); /* to prevent scrolling */
return false;
}
}
};
private _onMouseUp = (e: MouseEvent) => {
window.removeEventListener('mouseup', this._onMouseUp);
window.removeEventListener('mousemove', this._onMouseMove);
if (e.button === this.mouseButtonIndex) {
this._mouseDown = false;
document.body.style.cursor = 'initial';
}
};
private _onMouseMove = (e: MouseEvent) => {
if (this.active && this._mouseDown) {
if (this.active && this._mouseDown) {
this._azimuthSpeed = -(e.movementX * this.xSensitivity);
this._polarSpeed = e.movementY * this.ySensitivity;
}
}
};
private _onMouseScroll = (e: WheelEvent) => {
e.preventDefault(); /* to prevent scrolling */
this.radial *= 1 - e.deltaY * this.zoomSensitivity * -0.001;
this.radial = Math.min(this.maxZoom, Math.max(this.minZoom, this.radial));
this._updateCamera();
};
/* Touch event handlers */
private _onTouchStart = (e: TouchEvent) => {
if (e.touches.length === 1) {
/* to prevent scrolling and allow us to track touch movement */
e.preventDefault();
this._touchStartX = e.touches[0].clientX;
this._touchStartY = e.touches[0].clientY;
this._mouseDown = true; /* Treat touch like mouse down */
} else if (e.touches.length === 2) {
/* Calculate initial pinch distance */
this._initialPinchDistance = this._getDistanceBetweenTouches(e.touches);
e.preventDefault(); /* Prevent default pinch actions */
}
};
private _onTouchMove = (e: TouchEvent) => {
if (!this.active || !this._mouseDown) {
return;
}
e.preventDefault(); /* to prevent moving the page */
if (e.touches.length === 1) {
const deltaX = e.touches[0].clientX - this._touchStartX;
const deltaY = e.touches[0].clientY - this._touchStartY;
this._azimuthSpeed = -(deltaX * this.xSensitivity);
this._polarSpeed = deltaY * this.ySensitivity;
this._touchStartX = e.touches[0].clientX;
this._touchStartY = e.touches[0].clientY;
} else if (e.touches.length === 2) {
/* Handle pinch zoom */
const currentPinchDistance = this._getDistanceBetweenTouches(e.touches);
const pinchScale = this._initialPinchDistance / currentPinchDistance;
this.radial *= pinchScale;
this.radial = Math.min(this.maxZoom, Math.max(this.minZoom, this.radial));
this._updateCamera();
/* Update initial pinch distance for next move */
this._initialPinchDistance = currentPinchDistance;
}
};
private _onTouchEnd = (e: TouchEvent) => {
if (e.touches.length < 2) {
this._mouseDown = false; /* Treat touch end like mouse up */
}
if (e.touches.length === 1) {
/* Prepare for possible single touch movement */
this._touchStartX = e.touches[0].clientX;
this._touchStartY = e.touches[0].clientY;
}
};
/**
* Helper function to calculate the distance between two touch points
* @param touches list of touch points
* @returns distance between the two touch points
*/
private _getDistanceBetweenTouches(touches: TouchList): number {
const dx = touches[0].clientX - touches[1].clientX;
const dy = touches[0].clientY - touches[1].clientY;
return Math.sqrt(dx * dx + dy * dy);
}
}