forked from WonderlandEngine/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbital-camera.ts
255 lines (204 loc) · 8.38 KB
/
orbital-camera.ts
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
import {Component} from '@wonderlandengine/api';
import {property} from '@wonderlandengine/api/decorators.js';
import {deg2rad} from './utils/utils.js';
const preventDefault = (e: Event) => {
e.preventDefault();
};
const tempVec = [0, 0, 0];
/**
* 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;
private _mouseDown: boolean = false;
private _origin = [0, 0, 0];
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;
start(): void {
this.object.getPositionWorld(this._origin);
this._updateCamera();
}
onActivate(): void {
const canvas = this.engine.canvas;
canvas.addEventListener('mousemove', this._onMouseMove);
if (this.mouseButtonIndex === 2) {
canvas.addEventListener('contextmenu', preventDefault, {passive: false});
}
canvas.addEventListener('mousedown', this._onMouseDown);
canvas.addEventListener('mouseup', this._onMouseUp);
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;
canvas.removeEventListener('mousemove', this._onMouseMove);
if (this.mouseButtonIndex === 2) {
canvas.removeEventListener('contextmenu', preventDefault);
}
canvas.removeEventListener('mousedown', this._onMouseDown);
canvas.removeEventListener('mouseup', this._onMouseUp);
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 {
if (!this._mouseDown) {
/** Apply deceleration only when the user is not actively dragging */
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) {
this._updateCamera();
}
}
/**
* 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) => {
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) => {
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);
}
}