-
Notifications
You must be signed in to change notification settings - Fork 207
/
camera-manager.js
746 lines (658 loc) · 26.1 KB
/
camera-manager.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
import * as THREE from 'three';
import {getRenderer, camera, scene} from './renderer.js';
// import * as notifications from './notifications.js';
import physicsManager from './physics-manager.js';
import {shakeAnimationSpeed} from './constants.js';
import Simplex from './simplex-noise.js';
import {playersManager} from './players-manager.js';
// import alea from './alea.js';
// import * as sounds from './sounds.js';
import {minFov, maxFov, midFov} from './constants.js';
// import { updateRaycasterFromMouseEvent } from './util.js';
import easing from './easing.js';
const cubicBezier = easing(0, 1, 0, 1);
const cubicBezier2 = easing(0.5, 0, 0.5, 1);
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localVector3 = new THREE.Vector3();
const localVector4 = new THREE.Vector3();
const localVector5 = new THREE.Vector3();
const localVector6 = new THREE.Vector3();
const localVector7 = new THREE.Vector3();
const localVector8 = new THREE.Vector3();
const localVector9 = new THREE.Vector3();
const localVector10 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localQuaternion2 = new THREE.Quaternion();
const localQuaternion3 = new THREE.Quaternion();
const localQuaternion4 = new THREE.Quaternion();
const localEuler = new THREE.Euler();
const localMatrix = new THREE.Matrix4();
/*
Anon: "Hey man, can I get your autograph?"
Drake: "Depends. What's it worth to you?"
Anon: "Your first born child"
Drake: "No thanks. I don't think your child would be worth very much."
*/
const zeroVector = new THREE.Vector3(0, 0, 0);
const upVector = new THREE.Vector3(0, 1, 0);
const cameraOffsetDefault = 0.65;
const maxFocusTime = 300;
const cameraOffset = new THREE.Vector3();
let cameraOffsetTargetZ = cameraOffset.z;
let cameraOffsetLimitZ = Infinity;
const physicsScene = physicsManager.getScene();
// let cameraOffsetZ = cameraOffset.z;
const rayVectorZero = new THREE.Vector3(0,0,0);
// const rayVectorUp = new THREE.Vector3(0,1,0);
// const rayStartPos = new THREE.Vector3(0,0,0);
// const rayDirection = new THREE.Vector3(0,0,0);
// const rayOffsetPoint = new THREE.Vector3(0,0,0);
// const rayMatrix = new THREE.Matrix4();
// const rayQuaternion = new THREE.Quaternion();
// const rayOriginArray = [new THREE.Vector3(0,0,0),new THREE.Vector3(0,0,0),new THREE.Vector3(0,0,0),new THREE.Vector3(0,0,0),new THREE.Vector3(0,0,0),new THREE.Vector3(0,0,0)]; // 6 elements
// const rayDirectionArray = [new THREE.Quaternion(),new THREE.Quaternion(),new THREE.Quaternion(),new THREE.Quaternion(),new THREE.Quaternion(),new THREE.Quaternion()]; // 6 elements
/* function getNormal(u, v) {
return localPlane.setFromCoplanarPoints(zeroVector, u, v).normal;
} */
/* function signedAngleTo(u, v) {
// Get the signed angle between u and v, in the range [-pi, pi]
const angle = u.angleTo(v);
console.log('signed angle to', angle, u.dot(v));
return (u.dot(v) >= 0 ? 1 : -1) * angle;
} */
/* function signedAngleTo(a, b, v) {
const s = v.crossVectors(a, b).length();
// s = length(cross_product(a, b))
const c = a.dot(b);
const angle = Math.atan2(s, c);
console.log('get signed angle', s, c, angle);
return angle;
} */
const getSideOfY = (() => {
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localPlane = new THREE.Plane();
function getSideOfY(a, b) {
localQuaternion.setFromRotationMatrix(
localMatrix.lookAt(
zeroVector,
a,
upVector
)
);
const rightVector = localVector.set(1, 0, 0).applyQuaternion(localQuaternion);
localPlane.setFromNormalAndCoplanarPoint(rightVector, a);
const distance = localPlane.distanceToPoint(b, localVector2);
return distance >= 0 ? 1 : -1;
}
return getSideOfY;
})();
// const lastCameraQuaternion = new THREE.Quaternion();
// let lastCameraZ = 0;
// let lastCameraValidZ = 0;
const seed = 'camera';
const shakeNoise = new Simplex(seed);
class Shake extends THREE.Object3D {
constructor(intensity, startTime, radius, decay) {
super();
this.intensity = intensity;
this.startTime = startTime;
this.radius = radius;
this.decay = decay;
}
}
/* function lerpNum(value1, value2, amount) {
amount = amount < 0 ? 0 : amount;
amount = amount > 1 ? 1 : amount;
return value1 + (value2 - value1) * amount;
}
// Raycast from player to camera corner
function initCameraRayParams(arrayIndex,originPoint) {
rayDirection.subVectors(localPlayer.position, originPoint).normalize();
rayMatrix.lookAt(rayDirection,rayVectorZero,rayVectorUp);
rayQuaternion.setFromRotationMatrix(rayMatrix);
// Slightly move ray start position towards camera (to avoid hair,hat)
rayStartPos.copy(localPlayer.position);
rayStartPos.add( rayDirection.multiplyScalar(0.1) );
rayOriginArray[arrayIndex].copy(rayStartPos);
rayDirectionArray[arrayIndex].copy(rayQuaternion);
}
// Raycast from player postition with small offset
function initOffsetRayParams(arrayIndex,originPoint) {
rayDirection.subVectors(localPlayer.position, camera.position).normalize();
rayMatrix.lookAt(rayDirection,rayVectorZero,rayVectorUp);
rayQuaternion.setFromRotationMatrix(rayMatrix);
rayOriginArray[arrayIndex].copy(originPoint);
rayDirectionArray[arrayIndex].copy(rayQuaternion);
} */
/* const redMesh = (() => {
const geometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshBasicMaterial({color: 0xff0000});
const mesh = new THREE.Mesh(geometry, material);
// mesh.visible = false;
return mesh;
})();
scene.add(redMesh);
const blueMesh = (() => {
const geometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshBasicMaterial({color: 0x0000ff});
const mesh = new THREE.Mesh(geometry, material);
// mesh.visible = false;
return mesh;
})();
scene.add(blueMesh); */
class CameraManager extends EventTarget {
constructor() {
super();
this.pointerLockElement = null;
// this.pointerLockEpoch = 0;
this.shakes = [];
this.focus = false;
this.lastFocusChangeTime = 0; // XXX this needs to be removed
this.fovFactor = 0;
this.lastNonzeroDirectionVector = new THREE.Vector3(0, 0, -1);
this.targetType = 'dynamic';
this.target = null;
this.target2 = null;
this.lastTarget = null;
this.targetPosition = new THREE.Vector3(0, 0, 0);
this.targetQuaternion = new THREE.Quaternion();
this.targetFov = camera.fov;
this.sourcePosition = new THREE.Vector3();
this.sourceQuaternion = new THREE.Quaternion();
this.sourceFov = camera.fov;
this.lerpStartTime = 0;
this.lastTimestamp = 0;
this.cinematicScript = null;
this.cinematicScriptStartTime = -1;
document.addEventListener('pointerlockchange', e => {
let pointerLockElement = document.pointerLockElement;
const renderer = getRenderer();
if (pointerLockElement !== null && pointerLockElement !== renderer.domElement) {
pointerLockElement = null;
}
this.pointerLockElement = pointerLockElement;
this.dispatchEvent(new MessageEvent('pointerlockchange', {
data: {
pointerLockElement,
},
}));
});
}
focusCamera(position) {
camera.lookAt(position);
camera.updateMatrixWorld();
}
async requestPointerLock() {
// const localPointerLockEpoch = ++this.pointerLockEpoch;
for (const options of [
{
unadjustedMovement: true,
},
undefined
]) {
try {
await new Promise((accept, reject) => {
const renderer = getRenderer();
if (document.pointerLockElement !== renderer.domElement) {
const _pointerlockchange = e => {
// if (localPointerLockEpoch === this.pointerLockEpoch) {
accept();
// }
_cleanup();
};
document.addEventListener('pointerlockchange', _pointerlockchange);
const _pointerlockerror = err => {
reject(err);
_cleanup();
/* notifications.addNotification(`\
<i class="icon fa fa-mouse-pointer"></i>
<div class=wrap>
<div class=label>Whoa there!</div>
<div class=text>
Hold up champ! The browser wants you to slow down.
</div>
<div class=close-button>✕</div>
</div>
`, {
timeout: 3000,
}); */
};
document.addEventListener('pointerlockerror', _pointerlockerror);
const _cleanup = () => {
document.removeEventListener('pointerlockchange', _pointerlockchange);
document.removeEventListener('pointerlockerror', _pointerlockerror);
};
renderer.domElement.requestPointerLock(options)
.catch(_pointerlockerror);
} else {
accept();
}
});
break;
} catch (err) {
console.warn(err);
continue;
}
}
}
exitPointerLock() {
document.exitPointerLock();
}
getMode() {
if (this.target || this.cinematicScript) {
return 'isometric';
} else {
return cameraOffset.z > -0.5 ? 'firstperson' : 'isometric';
}
}
getCameraOffset() {
return cameraOffset;
}
handleMouseMove(e) {
const {movementX, movementY} = e;
camera.position.add(localVector.copy(this.getCameraOffset()).applyQuaternion(camera.quaternion));
camera.rotation.y -= movementX * Math.PI * 2 * 0.0005;
camera.rotation.x -= movementY * Math.PI * 2 * 0.0005;
camera.rotation.x = Math.min(Math.max(camera.rotation.x, -Math.PI * 0.35), Math.PI / 2);
camera.quaternion.setFromEuler(camera.rotation);
camera.position.sub(localVector.copy(this.getCameraOffset()).applyQuaternion(camera.quaternion));
camera.updateMatrixWorld();
if (!this.target) {
this.targetQuaternion.copy(camera.quaternion);
}
}
handleWheelEvent(e) {
if (!this.target) {
cameraOffsetTargetZ = Math.min(cameraOffset.z - e.deltaY * 0.01, 0);
}
}
addShake(position, intensity, radius, decay) {
const startTime = performance.now();
const shake = new Shake(intensity, startTime, radius, decay);
shake.position.copy(position);
this.shakes.push(shake);
return shake;
}
flushShakes() {
if (this.shakes.length > 0) {
const now = performance.now();
this.shakes = this.shakes.filter(shake => now < shake.startTime + shake.decay);
}
}
getShakeFactor() {
let result = 0;
if (this.shakes.length > 0) {
const now = performance.now();
for (const shake of this.shakes) {
const distanceFactor = Math.min(Math.max((shake.radius - shake.position.distanceTo(camera.position))/shake.radius, 0), 1);
const timeFactor = Math.min(Math.max(1 - (now - shake.startTime) / shake.decay, 0), 1);
// console.log('get shake factor', shake.intensity * distanceFactor * timeFactor, shake.intensity, distanceFactor, timeFactor);
result += shake.intensity * distanceFactor * timeFactor;
}
}
return result;
}
setFocus(focus) {
if (focus !== this.focus) {
this.focus = focus;
// this.lastFocusChangeTime = performance.now();
this.dispatchEvent(new MessageEvent('focuschange', {
data: {
focus,
},
}));
}
}
setDynamicTarget(target = null, target2 = null) {
this.targetType = 'dynamic';
this.target = target;
this.target2 = target2;
// console.log('set dynamic target', this.target, this.target2, new Error().stack);
if (this.target) {
const _setCameraToDynamicTarget = () => {
this.target.matrixWorld.decompose(localVector, localQuaternion, localVector2);
if (this.target2) {
this.target2.matrixWorld.decompose(localVector3, localQuaternion2, localVector4);
const faceDirection = localVector5.set(0, 0, 1).applyQuaternion(localQuaternion);
const lookQuaternion = localQuaternion3.setFromRotationMatrix(
localMatrix.lookAt(
localVector,
localVector3,
upVector,
)
);
const lookDirection = localVector6.set(0, 0, -1).applyQuaternion(lookQuaternion);
const sideOfY = getSideOfY(faceDirection, lookDirection);
const face = faceDirection.dot(lookDirection) >= 0 ? 1 : -1;
const dollyPosition = localVector7.copy(localVector)
.add(localVector3)
.multiplyScalar(0.5);
dollyPosition.add(
localVector8.set(sideOfY * -0.3, 0, 0).applyQuaternion(lookQuaternion)
);
const lookToDollyVector = localVector9.copy(dollyPosition).sub(localVector).normalize();
this.targetPosition.copy(localVector)
.add(lookToDollyVector);
this.targetQuaternion.setFromRotationMatrix(
localMatrix.lookAt(
lookToDollyVector,
zeroVector,
upVector
)
);
if (face < 0) {
this.targetPosition.add(localVector10.set(0, 0, -0.8).applyQuaternion(this.targetQuaternion));
this.targetQuaternion.multiply(localQuaternion4.setFromAxisAngle(upVector, Math.PI));
this.targetPosition.add(localVector10.set(0, 0, 0.8).applyQuaternion(this.targetQuaternion));
} else if (!this.lastTarget) {
this.targetPosition.add(localVector10.set(0, 0, -cameraOffsetDefault).applyQuaternion(this.targetQuaternion));
this.targetQuaternion.multiply(localQuaternion4.setFromAxisAngle(upVector, sideOfY * -Math.PI * 0.87));
this.targetPosition.add(localVector10.set(0, 0, cameraOffsetDefault).applyQuaternion(this.targetQuaternion));
}
} else {
this.targetPosition.copy(localVector)
.add(localVector2.set(0, 0, 1).applyQuaternion(localQuaternion));
this.targetQuaternion.copy(localQuaternion);
}
this.sourceFov = camera.fov;
this.targetFov = minFov;
this.sourcePosition.copy(camera.position);
this.sourceQuaternion.copy(camera.quaternion);
const timestamp = performance.now();
this.lerpStartTime = timestamp;
this.lastTimestamp = timestamp;
// cameraOffsetZ = -cameraOffsetDefault;
cameraOffset.z = -cameraOffsetDefault;
};
_setCameraToDynamicTarget();
} else {
this.setCameraToNullTarget();
}
}
setStaticTarget(target = null, target2 = null) {
this.targetType = 'static';
this.target = target;
this.target2 = target2;
// console.log('set static target', this.target, this.target2, new Error().stack);
if (this.target) {
const _setCameraToStaticTarget = () => {
// this.target.matrixWorld.decompose(localVector, localQuaternion, localVector2);
cameraOffsetTargetZ = -1;
cameraOffset.z = cameraOffsetTargetZ;
const localPlayer = playersManager.getLocalPlayer();
const targetPosition = localVector.copy(localPlayer.position)
.add(localVector2.set(0, 0, -cameraOffsetTargetZ).applyQuaternion(localPlayer.quaternion));
const targetQuaternion = localPlayer.quaternion;
// camera.position.lerp(targetPosition, 0.2);
// camera.quaternion.slerp(targetQuaternion, 0.2);
this.sourcePosition.copy(camera.position);
this.sourceQuaternion.copy(camera.quaternion);
this.sourceFov = camera.fov;
this.targetPosition.copy(targetPosition);
this.targetQuaternion.copy(targetQuaternion);
this.targetFov = midFov;
const timestamp = performance.now();
this.lerpStartTime = timestamp;
this.lastTimestamp = timestamp;
// cameraOffsetZ = -cameraOffsetDefault;
// cameraOffset.z = -cameraOffsetDefault;
};
_setCameraToStaticTarget();
} else {
this.setCameraToNullTarget();
}
}
setCameraToNullTarget() {
this.sourcePosition.copy(camera.position);
this.sourceQuaternion.copy(camera.quaternion);
this.sourceFov = camera.fov;
// this.targetPosition.copy(camera.position);
// this.targetQuaternion.copy(camera.quaternion);
this.targetFov = minFov;
const timestamp = performance.now();
this.lerpStartTime = timestamp;
this.lastTimestamp = timestamp;
}
startCinematicScript(cinematicScript) {
this.cinematicScript = cinematicScript;
this.cinematicScriptStartTime = performance.now();
}
updatePost(timestamp, timeDiff) {
const renderer = getRenderer();
const session = renderer.xr.getSession();
const localPlayer = playersManager.getLocalPlayer();
if (this.target) {
const _setLerpDelta = (position, quaternion) => {
const lerpTime = 2000;
const lastTimeFactor = Math.min(Math.max(cubicBezier((this.lastTimestamp - this.lerpStartTime) / lerpTime), 0), 1);
const currentTimeFactor = Math.min(Math.max(cubicBezier((timestamp - this.lerpStartTime) / lerpTime), 0), 1);
if (lastTimeFactor !== currentTimeFactor) {
{
const lastLerp = localVector.copy(this.sourcePosition).lerp(this.targetPosition, lastTimeFactor);
const currentLerp = localVector2.copy(this.sourcePosition).lerp(this.targetPosition, currentTimeFactor);
position.add(currentLerp).sub(lastLerp);
}
{
const lastLerp = localQuaternion.copy(this.sourceQuaternion).slerp(this.targetQuaternion, lastTimeFactor);
const currentLerp = localQuaternion2.copy(this.sourceQuaternion).slerp(this.targetQuaternion, currentTimeFactor);
quaternion.premultiply(lastLerp.invert()).premultiply(currentLerp);
}
}
this.lastTimestamp = timestamp;
};
_setLerpDelta(camera.position, camera.quaternion);
camera.updateMatrixWorld();
} else if (this.cinematicScript) {
const timeDiff = timestamp - this.cinematicScriptStartTime;
// find the line in the script that we are currently on
let currentDuration = 0;
const currentLineIndex = (() => {
let i;
for (i = 0; i < this.cinematicScript.length; i++) {
const currentLine = this.cinematicScript[i];
// const nextLine = this.cinematicScript[i + 1];
if (currentDuration + currentLine.duration > timeDiff) {
// return currentLine;
break;
} else {
currentDuration += currentLine.duration;
}
// const lineDuration = this.cinematicScript[i].duration;
// currentDuration += lineDuration;
}
return i < this.cinematicScript.length ? i : -1;
})();
if (currentLineIndex !== -1) {
// calculate how far into the line we are, in 0..1
const currentLine = this.cinematicScript[currentLineIndex];
const {type} = currentLine;
switch (type) {
case 'set': {
camera.position.copy(currentLine.position);
camera.quaternion.copy(currentLine.quaternion);
camera.updateMatrixWorld();
break;
}
case 'move': {
let factor = Math.min(Math.max((timeDiff - currentDuration) / currentLine.duration, 0), 1);
if (factor < 1) {
factor = cubicBezier2(factor);
const previousLine = this.cinematicScript[currentLineIndex - 1];
camera.position.copy(previousLine.position).lerp(currentLine.position, factor);
camera.quaternion.copy(previousLine.quaternion).slerp(currentLine.quaternion, factor);
camera.updateMatrixWorld();
// console.log('previous line', previousLine, camera.position.toArray().join(','), camera.quaternion.toArray().join(','), factor);
/* if (isNaN(camera.position.x)) {
debugger;
} */
} else {
this.cinematicScript = null;
}
break;
}
default: {
throw new Error('unknown cinematic script line type: ' + type);
}
}
} else {
// console.log('no line', timeDiff, this.cinematicScript.slice());
this.cinematicScript = null;
}
} else {
const _bumpCamera = () => {
const direction = localVector.set(0, 0, 1)
.applyQuaternion(camera.quaternion);
const backOffset = 1;
// const cameraBackThickness = 0.5;
const sweepDistance = Math.max(-cameraOffsetTargetZ, 0);
// console.log('offset', cameraOffsetTargetZ);
cameraOffsetLimitZ = -Infinity;
if (sweepDistance > 0) {
const halfExtents = localVector2.set(0.5, 0.5, 0.1);
const maxHits = 1;
const result = physicsScene.sweepBox(
localVector3.copy(localPlayer.position)
.add(localVector4.copy(direction).multiplyScalar(backOffset)),
camera.quaternion,
halfExtents,
direction,
sweepDistance,
maxHits,
);
if (result.length > 0) {
const distance = result[0].distance;
cameraOffsetLimitZ = distance < 0.5 ? 0 : -distance;
}
}
};
_bumpCamera();
const _lerpCameraOffset = () => {
const lerpFactor = 0.15;
let cameraOffsetZ = Math.max(cameraOffsetTargetZ, cameraOffsetLimitZ);
if (cameraOffsetZ > -0.5) {
cameraOffsetZ = 0;
}
cameraOffset.z = cameraOffset.z * (1-lerpFactor) + cameraOffsetZ*lerpFactor;
};
_lerpCameraOffset();
const _setFreeCamera = () => {
const avatarCameraOffset = session ? rayVectorZero : this.getCameraOffset();
const avatarHeight = localPlayer.avatar ? localPlayer.avatar.height : 0;
const crouchOffset = avatarHeight * (1 - localPlayer.getCrouchFactor()) * 0.5;
switch (this.getMode()) {
case 'firstperson': {
if (localPlayer.avatar) {
const boneNeck = localPlayer.avatar.foundModelBones['Neck'];
const boneEyeL = localPlayer.avatar.foundModelBones['Eye_L'];
const boneEyeR = localPlayer.avatar.foundModelBones['Eye_R'];
const boneHead = localPlayer.avatar.foundModelBones['Head'];
boneNeck.quaternion.setFromEuler(localEuler.set(Math.min(camera.rotation.x * -0.5, 0.6), 0, 0, 'XYZ'));
boneNeck.updateMatrixWorld();
if (boneEyeL && boneEyeR) {
boneEyeL.matrixWorld.decompose(localVector2, localQuaternion, localVector4);
boneEyeR.matrixWorld.decompose(localVector3, localQuaternion, localVector4);
localVector4.copy(localVector2.add(localVector3).multiplyScalar(0.5));
} else {
boneHead.matrixWorld.decompose(localVector2, localQuaternion, localVector4);
localVector2.add(localVector3.set(0, 0, 0.1).applyQuaternion(localQuaternion));
localVector4.copy(localVector2);
}
} else {
localVector4.copy(localPlayer.position);
}
this.targetPosition.copy(localVector4)
.sub(localVector2.copy(avatarCameraOffset).applyQuaternion(this.targetQuaternion));
break;
}
case 'isometric': {
this.targetPosition.copy(localPlayer.position)
.sub(
localVector2.copy(avatarCameraOffset)
.applyQuaternion(this.targetQuaternion)
);
break;
}
default: {
throw new Error('invalid camera mode: ' + cameraMode);
}
}
const factor = Math.min((timestamp - this.lerpStartTime) / maxFocusTime, 1);
this.targetPosition.y -= crouchOffset;
camera.position.copy(this.sourcePosition)
.lerp(this.targetPosition, factor);
localEuler.setFromQuaternion(this.targetQuaternion, 'YXZ');
localEuler.z = 0;
camera.quaternion.copy(this.sourceQuaternion)
.slerp(localQuaternion.setFromEuler(localEuler), factor);
};
_setFreeCamera();
};
const _setCameraFov = () => {
if (!renderer.xr.getSession()) {
let newFov;
const focusTime = Math.min((timestamp - this.lerpStartTime) / maxFocusTime, 1);
if (focusTime < 1) {
this.fovFactor = 0;
const a = this.sourceFov;
const b = this.targetFov;
newFov = a * (1 - focusTime) + focusTime * b;
} else if (this.focus) {
this.fovFactor = 0;
newFov = midFov;
} else {
const fovInTime = 3;
const fovOutTime = 0.3;
const narutoRun = localPlayer.getAction('narutoRun');
if (narutoRun) {
if (this.lastNonzeroDirectionVector.z < 0) {
this.fovFactor += timeDiff / 1000 / fovInTime;
} else {
this.fovFactor -= timeDiff / 1000 / fovInTime;
}
} else {
this.fovFactor -= timeDiff / 1000 / fovOutTime;
}
this.fovFactor = Math.min(Math.max(this.fovFactor, 0), 1);
newFov = minFov + Math.pow(this.fovFactor, 0.75) * (maxFov - minFov);
}
if (newFov !== camera.fov) {
camera.fov = newFov;
camera.updateProjectionMatrix();
this.dispatchEvent(new MessageEvent('fovchange', {
data: {
fov: newFov,
},
}));
}
}
};
_setCameraFov();
const _shakeCamera = () => {
this.flushShakes();
const shakeFactor = this.getShakeFactor();
if (shakeFactor > 0) {
const baseTime = timestamp/1000 * shakeAnimationSpeed;
const timeOffset = 1000;
const ndc = f => (-0.5 + f) * 2;
let index = 0;
const randomValue = () => ndc(shakeNoise.noise1D(baseTime + timeOffset * index++));
localVector.set(
randomValue(),
randomValue(),
randomValue()
)
.normalize()
.multiplyScalar(shakeFactor * randomValue());
camera.position.add(localVector);
}
};
_shakeCamera();
camera.updateMatrixWorld();
this.lastTarget = this.target;
}
};
const cameraManager = new CameraManager();
export default cameraManager;