forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera-manager.js
247 lines (213 loc) · 8.12 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
import * as THREE from 'three';
import {getRenderer, camera} from './renderer.js';
import * as notifications from './notifications.js';
import metaversefile from 'metaversefile';
import physicsManager from './physics-manager.js';
const localVector = new THREE.Vector3();
const cameraOffset = new THREE.Vector3();
let cameraOffsetTargetZ = cameraOffset.z;
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
const lastCameraQuaternion = new THREE.Quaternion();
let lastCameraZ = 0;
let lastCameraValidZ = 0;
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) {
const localPlayer = metaversefile.useLocalPlayer();
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) {
const localPlayer = metaversefile.useLocalPlayer();
rayDirection.subVectors(localPlayer.position, camera.position).normalize();
rayMatrix.lookAt(rayDirection,rayVectorZero,rayVectorUp);
rayQuaternion.setFromRotationMatrix(rayMatrix);
rayOriginArray[arrayIndex].copy(originPoint);
rayDirectionArray[arrayIndex].copy(rayQuaternion);
}
class CameraManager extends EventTarget {
constructor() {
super();
}
wasActivated() {
return wasActivated;
}
focusCamera(position) {
camera.lookAt(position);
camera.updateMatrixWorld();
}
async requestPointerLock() {
for (const options of [
{
unadjustedMovement: true,
},
undefined
]) {
try {
await new Promise((accept, reject) => {
if (!document.pointerLockElement) {
const _pointerlockchange = e => {
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);
};
const renderer = getRenderer();
renderer.domElement.requestPointerLock(options);
} else {
accept();
}
});
break;
} catch (err) {
console.warn(err);
continue;
}
}
}
getMode() {
const f = -cameraOffset.z;
if (f < 0.5) {
return 'firstperson';
} else {
return 'isometric';
}
}
getCameraOffset() {
return cameraOffset;
}
handleWheelEvent(e) {
e.preventDefault();
cameraOffsetTargetZ = Math.min(cameraOffsetTargetZ - e.deltaY * 0.01, 0);
}
update(timeDiff) {
const localPlayer = metaversefile.useLocalPlayer();
const startMode = this.getMode();
let newVal = cameraOffsetTargetZ;
let hasIntersection = false;
// Camera - Top left
initCameraRayParams(0,rayStartPos.set(-1, 1, ( camera.near + camera.far ) / ( camera.near - camera.far )).unproject( camera ));
// Camera - Bottom left
initCameraRayParams(1,rayStartPos.set(-1, -1, ( camera.near + camera.far ) / ( camera.near - camera.far )).unproject( camera ));
// Camera - Top right
initCameraRayParams(2,rayStartPos.set(1, 1, ( camera.near + camera.far ) / ( camera.near - camera.far )).unproject( camera ));
// Camera - Bottom right
initCameraRayParams(3,rayStartPos.set(1, -1, ( camera.near + camera.far ) / ( camera.near - camera.far )).unproject( camera ));
// Player postition - offset to left
rayStartPos.copy(localPlayer.position);
rayOffsetPoint.set(-1, 0, 0);
rayOffsetPoint.applyQuaternion(camera.quaternion);
rayOffsetPoint.normalize();
rayStartPos.add(rayOffsetPoint);
initOffsetRayParams(4,rayStartPos);
// Player postition - offset to right
rayStartPos.copy(localPlayer.position);
rayOffsetPoint.set(1, 0, 0);
rayOffsetPoint.applyQuaternion(camera.quaternion);
rayOffsetPoint.normalize();
rayStartPos.add(rayOffsetPoint);
initOffsetRayParams(5,rayStartPos);
let collisionArray = physicsManager.raycastArray(rayOriginArray, rayDirectionArray, 6);
// Check collision from player to camera corners
for(let i=0;i<4;i++) {
if ((collisionArray.hit[i] === 1) && (collisionArray.distance[i] <= -1 * newVal)) {
if (newVal < (-1 * (collisionArray.distance[i]-0.15))) {
newVal = (-1 * (collisionArray.distance[i]-0.15));
hasIntersection = true;
//console.log(i + " " + collisionArray.distance[i]+ " " + collisionArray.hit[i]);
}
}
}
// Check collision from player pos and small offset to left and righ - to camera center
let offsetCollisionCount = 0;
for(let i=4;i<6;i++) {
if ((collisionArray.hit[i] === 1) && (collisionArray.distance[i] <= (-1 * cameraOffsetTargetZ))) {
offsetCollisionCount++;
}
}
// Discard collision with small objects
if (hasIntersection && (offsetCollisionCount === 0))
{
hasIntersection = false;
newVal = cameraOffsetTargetZ;
}
// Remove jitter when there is no movement
if (lastCameraQuaternion.equals(camera.quaternion) && lastCameraZ === cameraOffsetTargetZ) {
if (lastCameraValidZ < newVal) {
lastCameraValidZ = newVal;
}
if (newVal < lastCameraValidZ)
newVal = lastCameraValidZ;
}
else {
lastCameraQuaternion.copy(camera.quaternion);
lastCameraZ = cameraOffsetTargetZ;
lastCameraValidZ = cameraOffsetTargetZ;
}
// Slow zoom out if there is no intersection
cameraOffsetZ = lerpNum(cameraOffsetZ,newVal, 0.2);
// Fast zoom in to the point of intersection
if (hasIntersection) {
cameraOffsetZ = newVal;
}
const zDiff = Math.abs(cameraOffset.z - cameraOffsetZ);
if (zDiff === 0) {
// nothing
} else {
camera.position.add(localVector.copy(cameraOffset).applyQuaternion(camera.quaternion));
cameraOffset.z = cameraOffsetZ;
camera.position.sub(localVector.copy(cameraOffset).applyQuaternion(camera.quaternion));
camera.updateMatrixWorld();
}
const endMode = this.getMode();
if (endMode !== startMode) {
this.dispatchEvent(new MessageEvent('modechange', {
data: {
mode: endMode,
},
}));
}
}
};
const cameraManager = new CameraManager();
export default cameraManager;