-
Notifications
You must be signed in to change notification settings - Fork 4
/
RotationManager.ts
498 lines (327 loc) · 8.96 KB
/
RotationManager.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import { BaseEvent, EventDispatcher, Matrix4, Quaternion, Spherical, Vector3 } from "three";
import { ControlMode } from "../core/ControlMode.js";
import { MILLISECONDS_TO_SECONDS } from "../core/time.js";
import { Updatable } from "../core/Updatable.js";
import { ScalarDamper } from "../math/ScalarDamper.js";
import { Settings } from "../settings/Settings.js";
import { ManagerEventMap } from "./ManagerEventMap.js";
const TWO_PI = 2 * Math.PI;
const u = /* @__PURE__ */ new Vector3();
const v = /* @__PURE__ */ new Vector3();
const m = /* @__PURE__ */ new Matrix4();
/**
* A rotation manager.
*
* @group Managers
*/
export class RotationManager extends EventDispatcher<ManagerEventMap> implements Updatable {
/**
* Triggers when the position or quaternion is changed.
*
* @event
*/
static readonly EVENT_UPDATE = "update";
/**
* @see {@link position}
*/
private _position: Vector3;
/**
* @see {@link quaternion}
*/
private _quaternion: Quaternion;
/**
* @see {@link target}
*/
private _target: Vector3;
/**
* The settings.
*/
private settings: Settings;
/**
* The current spherical coordinates.
*/
private spherical0: Spherical;
/**
* The spherical target coordinates.
*/
private spherical1: Spherical;
/**
* Scalar dampers.
*/
private scalarDampers: ScalarDamper[];
/**
* A timestamp.
*/
private timestamp: number;
/**
* A reusable update event.
*/
private updateEvent: BaseEvent<"update">;
/**
* Constructs a new rotation manager.
*
* @param position - The position.
* @param quaternion - The quaternion.
* @param target - The target.
* @param settings - The settings.
*/
constructor(
position: Vector3,
quaternion: Quaternion,
target: Vector3,
settings: Settings
) {
super();
this._position = position;
this._quaternion = quaternion;
this._target = target;
this.settings = settings;
this.spherical0 = new Spherical();
this.spherical1 = new Spherical();
this.timestamp = 0;
this.updateEvent = { type: RotationManager.EVENT_UPDATE };
this.scalarDampers = [
new ScalarDamper(),
new ScalarDamper(),
new ScalarDamper()
];
}
/**
* The position.
*/
get position(): Vector3 {
return this._position;
}
set position(value: Vector3) {
this._position = value;
}
/**
* The quaternion.
*/
get quaternion(): Quaternion {
return this._quaternion;
}
set quaternion(value: Quaternion) {
this._quaternion = value;
}
/**
* The target.
*/
get target(): Vector3 {
return this._target;
}
set target(value: Vector3) {
this._target = value;
}
/**
* The current radius.
*/
get radius(): number {
return this.spherical0.radius;
}
/**
* Resets the current velocity.
*/
resetVelocity(): void {
// Stop moving.
this.spherical1.copy(this.spherical0);
for(const scalarDamper of this.scalarDampers) {
scalarDamper.resetVelocity();
}
}
/**
* Restricts the spherical angles.
*
* @return This manager.
*/
private restrictAngles(): this {
const s = this.spherical1;
const rotation = this.settings.rotation;
const thetaMin = rotation.minAzimuthalAngle;
const thetaMax = rotation.maxAzimuthalAngle;
const phiMin = rotation.minPolarAngle;
const phiMax = rotation.maxPolarAngle;
s.theta = Math.min(Math.max(s.theta, thetaMin), thetaMax);
s.phi = Math.min(Math.max(s.phi, phiMin), phiMax);
if(s.phi === 0.0 || s.phi === Math.PI) {
s.makeSafe();
}
return this;
}
/**
* Restricts the spherical radius.
*
* @return This manager.
*/
private restrictRadius(): this {
const s = this.spherical1;
const zoom = this.settings.zoom;
const min = zoom.minDistance;
const max = zoom.maxDistance;
s.radius = Math.min(Math.max(s.radius, min), max);
return this;
}
/**
* Restricts the spherical system.
*
* @return This manager.
*/
restrictSpherical(): this {
return this.restrictRadius().restrictAngles();
}
/**
* Updates the spherical coordinates based on the position and target.
*
* @return This manager.
*/
updateSpherical(): this {
if(this.settings.general.mode === ControlMode.THIRD_PERSON) {
const pivotOffset = this.settings.rotation.pivotOffset;
v.subVectors(u.subVectors(this.position, pivotOffset), this.target);
this.spherical1.setFromVector3(v);
} else {
this.spherical1.setFromVector3(this.target);
}
this.restrictSpherical();
this.spherical0.copy(this.spherical1);
return this;
}
/**
* Updates the position based on the spherical coordinates.
*
* @return This manager.
*/
updatePosition(): this {
if(this.settings.general.mode === ControlMode.THIRD_PERSON) {
// Construct the position using spherical0 and the target.
const pivotOffset = this.settings.rotation.pivotOffset;
this.position.setFromSpherical(this.spherical0).add(this.target).add(pivotOffset);
}
return this;
}
/**
* Updates the quaternion.
*
* @return This manager.
*/
updateQuaternion(): this {
const settings = this.settings;
const rotation = settings.rotation;
const target = this.target;
const up = u.copy(rotation.up);
const phi = this.spherical0.phi % TWO_PI;
if((phi < 0.0 && phi > -Math.PI) || (phi > Math.PI && phi < TWO_PI)) {
// The current orientation is upside down.
up.negate();
}
if(settings.general.mode === ControlMode.THIRD_PERSON) {
m.lookAt(v.subVectors(this.position, target), rotation.pivotOffset, up);
} else {
m.lookAt(v.set(0, 0, 0), target.setFromSpherical(this.spherical0), up);
}
this.quaternion.setFromRotationMatrix(m);
this.dispatchEvent(this.updateEvent);
return this;
}
/**
* Adjusts the spherical system.
*
* @param theta - The angle to add to theta in radians.
* @param phi - The angle to add to phi in radians.
* @return This manager.
*/
adjustSpherical(theta: number, phi: number): this {
const s = this.spherical1;
const settings = this.settings;
const rotation = settings.rotation;
const invertedY = rotation.invertedY;
const orbit = (settings.general.mode === ControlMode.THIRD_PERSON);
const orbitXorInvertedY = ((orbit || invertedY) && !(orbit && invertedY));
s.theta = rotation.invertedX ? s.theta + theta : s.theta - theta;
s.phi = orbitXorInvertedY ? s.phi - phi : s.phi + phi;
return this.restrictAngles();
}
/**
* Zooms in or out. Only applies in third person mode.
*
* @param sign - The zoom sign. Possible values are [-1, 0, 1].
* @return This manager.
*/
zoom(sign: number): this {
const s = this.spherical1;
const settings = this.settings;
const zoom = settings.zoom;
if(zoom.enabled && settings.general.mode === ControlMode.THIRD_PERSON) {
const amount = sign * zoom.sensitivity;
s.radius = zoom.inverted ? s.radius - amount : s.radius + amount;
this.restrictRadius();
}
return this;
}
/**
* Looks at the given point.
*
* @param point - The target point.
* @return This manager.
*/
lookAt(point: Vector3): this {
if(this.settings.general.mode === ControlMode.THIRD_PERSON) {
this.target.copy(point).sub(this.settings.rotation.pivotOffset);
} else {
this.target.subVectors(point, this.position).normalize();
}
return this;
}
/**
* Returns the current view direction.
*
* @param view - A vector to store the direction in.
* @return The normalized view direction.
*/
getViewDirection(view: Vector3): Vector3 {
const settings = this.settings;
const orbit = (settings.general.mode === ControlMode.THIRD_PERSON);
view.setFromSpherical(this.spherical0).normalize();
return orbit ? view.negate() : view;
}
update(timestamp: number): void {
const s0 = this.spherical0;
const s1 = this.spherical1;
const equal = (s0.radius === s1.radius && s0.theta === s1.theta && s0.phi === s1.phi);
if(!equal) {
const settings = this.settings;
const scalarDampers = this.scalarDampers;
const elapsed = (timestamp - this.timestamp) * MILLISECONDS_TO_SECONDS;
if(settings.rotation.damping > 0.0) {
const damping = settings.rotation.damping;
const omega = ScalarDamper.calculateOmega(damping);
const exp = ScalarDamper.calculateExp(omega, elapsed);
s0.theta = scalarDampers[0].interpolate(s0.theta, s1.theta, damping, omega, exp, elapsed);
s0.phi = scalarDampers[1].interpolate(s0.phi, s1.phi, damping, omega, exp, elapsed);
} else {
s0.theta = s1.theta;
s0.phi = s1.phi;
}
if(settings.zoom.damping > 0.0) {
const damping = settings.zoom.damping;
const omega = ScalarDamper.calculateOmega(damping);
const exp = ScalarDamper.calculateExp(omega, elapsed);
s0.radius = scalarDampers[2].interpolate(s0.radius, s1.radius, damping, omega, exp, elapsed);
} else {
s0.radius = s1.radius;
}
this.updatePosition().updateQuaternion();
} else {
// Prevent overflow.
if(Math.abs(s0.theta) >= TWO_PI) {
s0.theta %= TWO_PI;
s1.theta %= TWO_PI;
}
if(Math.abs(s0.phi) >= TWO_PI) {
s0.phi %= TWO_PI;
s1.phi %= TWO_PI;
}
}
this.timestamp = timestamp;
}
}