-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
367 lines (326 loc) · 16.5 KB
/
index.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
/* global AFRAME, THREE */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* Cursor Teleport component for A-Frame.
*/
AFRAME.registerComponent('cursor-teleport', {
schema: {
enabled: { type: 'boolean', default: true },
cameraHead: { type: 'selector', default: '' },
cameraRig: { type: 'selector', default: '' },
collisionEntities: { type: 'string', default: '' },
cursorColor: { type: 'color', default: '#4d93fd' },
cursorType: { type: 'string', default: 'cylinder', oneOf: ['ring', 'cylinder'] },
defaultPlaneSize: { type: 'number', default: 100 },
ignoreEntities: { type: 'string', default: '' },
landingMaxAngle: { default: 45, min: 0, max: 360 },
landingNormal: { type: 'vec3', default: { x: 0, y: 1, z: 0 } },
transitionSpeed: { type: 'number', default: 0.0006 }
},
init() {
// platform detect
this.mobile = AFRAME.utils.device.isMobile() || AFRAME.utils.device.isMobileDeviceRequestingDesktopSite();
// main app
const sceneEl = this.el.sceneEl;
this.canvas = sceneEl.renderer.domElement;
// camera
this.data.cameraHead.object3D.traverse((child) => {
if (child instanceof THREE.Camera) {
this.cam = child; // This is the PerspectiveCamera
}
});
this.camForRotation = this.data.cameraHead.object3D; // This is the Group, parent of the PerspectiveCamera
this.camRig = this.data.cameraRig.object3D;
// collision
this.rayCaster = new THREE.Raycaster();
this.collisionObjectNormalMatrix = new THREE.Matrix3();
this.collisionWorldNormal = new THREE.Vector3();
this.referenceNormal = new THREE.Vector3();
this.rayCastObjects = [];
// Update collision normal
this.referenceNormal.copy(this.data.landingNormal);
// RING teleport indicator
const geo = new THREE.RingGeometry(0.25, 0.3, 32, 1);
geo.rotateX(-Math.PI / 2);
geo.translate(0, 0.02, 0);
const mat = new THREE.MeshBasicMaterial({ color: this.data.cursorColor });
const indicatorRing = new THREE.Mesh(geo, mat);
const group = new THREE.Group();
group.add(indicatorRing);
this.teleportIndicator = group;
this.teleportIndicator.visible = false;
if (this.data.cursorType === 'cylinder') {
// CYLINDER teleport indicator
const geoCyl = new THREE.CylinderGeometry(0.3, 0.3, 0.5, 32, 1, true);
geoCyl.translate(0, 0.25, 0);
// texture source MIT license https://github.com/fernandojsg/aframe-teleport-controls/blob/master/lib/cylinderTexture.js
const textureString =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAQCAYAAADXnxW3AAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhU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dvfN6b/fF9/XfFt1+cif9zsu72Xcn7q28T7xf9EDtQdlD3YfVP1v+3Njv3H9qwHeg89HcR/cGhYPP/pH1jw9DBY+Zj8uGDYbrnjg+OTniP3L96fynQ89kzyaeF/6i/suuFxYvfvjV69fO0ZjRoZfyl5O/bXyl/erA6xmv28bCxh6+yXgzMV70VvvtwXfcdx3vo98PT+R8IH8o/2j5sfVT0Kf7kxmTk/8EA5jz/GMzLdsAAAAgY0hSTQAAeiUAAICDAAD5/wAAgOkAAHUwAADqYAAAOpgAABdvkl/FRgAAADJJREFUeNpEx7ENgDAAAzArK0JA6f8X9oewlcWStU1wBGdwB08wgjeYm79jc2nbYH0DAC/+CORJxO5fAAAAAElFTkSuQmCC';
const textureCyl = new THREE.TextureLoader().load(textureString);
const matCyl = new THREE.MeshBasicMaterial({
color: this.data.cursorColor,
side: 'double',
map: textureCyl,
transparent: true,
depthTest: false
});
const indicatorCyl = new THREE.Mesh(geoCyl, matCyl);
group.add(indicatorCyl);
}
sceneEl.object3D.add(this.teleportIndicator);
// transition
this.transitioning = false;
this.transitionProgress = 0;
this.transitionCamPosStart = new THREE.Vector3();
this.transitionCamPosEnd = new THREE.Vector3();
this.transitionCamQuaternionStart = new THREE.Quaternion();
this.transitionCamQuaternionEnd = new THREE.Quaternion();
// Bind functions
this.updateRaycastObjects = this.updateRaycastObjects.bind(this);
this.getMouseState = this.getMouseState.bind(this);
this.getTeleportPosition = this.getTeleportPosition.bind(this);
this.isValidNormalsAngle = this.isValidNormalsAngle.bind(this);
this.transition = this.transition.bind(this);
this.mouseMove = this.mouseMove.bind(this);
this.mouseDown = this.mouseDown.bind(this);
this.mouseUp = this.mouseUp.bind(this);
this.easeInOutQuad = this.easeInOutQuad.bind(this);
this.teleportTo = this.teleportTo.bind(this);
this.hideCursor = this.hideCursor.bind(this);
this.updateRaycastObjects();
},
remove() {
this.cam = null;
this.canvas = null;
this.rayCastObjects.length = 0;
this.el.sceneEl.object3D.remove(this.teleportIndicator);
for (const child of this.teleportIndicator.children) {
child.material.dispose();
child.geometry.dispose();
}
this.teleportIndicator = null;
if (this.collisionMesh) {
this.collisionMesh.geometry.dispose();
this.collisionMesh.material.dispose();
this.collisionMesh = null;
}
},
update(oldData) {
if (typeof oldData.enabled === 'undefined') return;
if (!oldData.enabled && this.data.enabled) {
this.registerEventListeners();
}
if (oldData.enabled && !this.data.enabled) {
// Call unregisterEventListeners instead of pause that is a wrapped method unregistering tick method
// because we still want the tick method to use the component via the teleportTo api.
this.unregisterEventListeners();
}
},
registerEventListeners() {
const canvas = this.canvas;
canvas.addEventListener('mousedown', this.mouseDown, false);
canvas.addEventListener('mousemove', this.mouseMove, false);
canvas.addEventListener('mouseup', this.mouseUp, false);
canvas.addEventListener('touchstart', this.mouseDown, false);
canvas.addEventListener('touchmove', this.mouseMove, false);
canvas.addEventListener('touchend', this.mouseUp, false);
window.addEventListener('keydown', this.hideCursor, false);
},
unregisterEventListeners() {
this.transitioning = false;
this.hideCursor();
const canvas = this.canvas;
canvas.removeEventListener('mousedown', this.mouseDown);
canvas.removeEventListener('mousemove', this.mouseMove);
canvas.removeEventListener('mouseup', this.mouseUp);
canvas.removeEventListener('touchstart', this.mouseDown);
canvas.removeEventListener('touchmove', this.mouseMove);
canvas.removeEventListener('touchend', this.mouseUp);
window.removeEventListener('keydown', this.hideCursor);
},
play() {
if (!this.data.enabled) return;
this.registerEventListeners();
},
pause() {
this.unregisterEventListeners();
},
updateRaycastObjects() {
// updates the array of meshes we will need to raycast to
// clear the array of any existing meshes
this.rayCastObjects.length = 0;
if (this.data.collisionEntities !== '') {
// traverse collision entities and add their meshes to the rayCastEntities array.
const collisionEntities = this.el.sceneEl.querySelectorAll(this.data.collisionEntities);
collisionEntities.forEach((e) => {
e.object3D.traverse((child) => {
if (child.isMesh) {
// mark this mesh as a collision object
child.userData.collision = true;
this.rayCastObjects.push(child);
}
});
});
} else {
if (!this.collisionMesh) {
// if no collision entities are specified, create a default ground plane collision.
const geo = new THREE.PlaneGeometry(this.data.defaultPlaneSize, this.data.defaultPlaneSize, 1);
geo.rotateX(-Math.PI / 2);
const mat = new THREE.MeshNormalMaterial();
const collisionMesh = new THREE.Mesh(geo, mat);
// mark this mesh as a collision object
collisionMesh.userData.collision = true;
this.collisionMesh = collisionMesh;
}
this.rayCastObjects.push(this.collisionMesh);
}
// We may need some entities to be seen by the raycaster even though they are not teleportable.
// This prevents the user from unnesserily teleporting when clicking things like buttons or UI.
if (this.data.ignoreEntities !== '') {
const ignoreEntities = this.el.sceneEl.querySelectorAll(this.data.ignoreEntities);
ignoreEntities.forEach((e) => {
e.object3D.traverse((child) => {
if (child.isMesh) {
this.rayCastObjects.push(child);
}
});
});
}
},
getMouseState: (function () {
const coordinates = new THREE.Vector2();
return function (e) {
const rect = this.canvas.getBoundingClientRect();
if (e.clientX != null) {
coordinates.x = e.clientX - rect.left;
coordinates.y = e.clientY - rect.top;
return coordinates;
} else if (e.touches[0] != null) {
coordinates.x = e.touches[0].clientX - rect.left;
coordinates.y = e.touches[0].clientY - rect.top;
return coordinates;
}
};
})(),
getTeleportPosition: (function () {
const mouse = new THREE.Vector2();
return function (mouseX, mouseY) {
if (this.rayCastObjects.length !== 0) {
if (this.cam && this.canvas) {
const cam = this.cam;
const rect = this.canvas.getBoundingClientRect();
mouse.x = (mouseX / (rect.right - rect.left)) * 2 - 1;
mouse.y = -(mouseY / (rect.bottom - rect.top)) * 2 + 1;
this.rayCaster.setFromCamera(mouse, cam);
const intersects = this.rayCaster.intersectObjects(this.rayCastObjects);
if (intersects.length !== 0 && this.isValidNormalsAngle(intersects[0].face.normal, intersects[0].object)) {
if (intersects[0].object.userData.collision === true) {
return intersects[0].point;
}
return false;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
};
})(),
isValidNormalsAngle(collisionNormal, collisionObject) {
this.collisionObjectNormalMatrix.getNormalMatrix(collisionObject.matrixWorld);
this.collisionWorldNormal.copy(collisionNormal).applyNormalMatrix(this.collisionObjectNormalMatrix);
const angleNormals = this.referenceNormal.angleTo(this.collisionWorldNormal);
return THREE.MathUtils.RAD2DEG * angleNormals <= this.data.landingMaxAngle;
},
transition(destPos, destQuaternion = undefined) {
this.transitionProgress = 0;
this.transitionCamPosEnd.copy(destPos);
this.transitionCamPosStart.copy(this.camRig.position);
if (destQuaternion) {
this.transitionCamQuaternionEnd.copy(destQuaternion);
this.transitionCamQuaternionStart.copy(this.camRig.quaternion);
} else {
this.transitionCamQuaternionEnd.copy(this.camRig.quaternion);
this.transitionCamQuaternionStart.copy(this.transitionCamQuaternionEnd);
}
this.transitioning = true;
this.el.emit('navigation-start');
},
hideCursor() {
this.teleportIndicator.visible = false;
},
mouseMove(e) {
const mouseState = this.getMouseState(e);
this.mouseX = mouseState.x;
this.mouseY = mouseState.y;
},
mouseDown(e) {
this.updateRaycastObjects();
const mouseState = this.getMouseState(e);
this.mouseX = mouseState.x;
this.mouseY = mouseState.y;
this.mouseXOrig = mouseState.x;
this.mouseYOrig = mouseState.y;
},
mouseUp(e) {
if (this.mouseX === this.mouseXOrig && this.mouseY === this.mouseYOrig) {
const pos = this.getTeleportPosition(this.mouseX, this.mouseY);
if (pos) {
this.teleportIndicator.visible = true;
this.teleportIndicator.position.copy(pos);
this.transition(pos);
}
}
},
teleportTo(pos, quaternion = undefined) {
this.teleportIndicator.position.copy(pos);
if (!quaternion) {
this.transition(pos);
} else {
const destQuaternion = new THREE.Quaternion();
destQuaternion.setFromEuler(new THREE.Euler(0, this.camForRotation.rotation.y, 0));
destQuaternion.invert();
destQuaternion.multiply(quaternion);
this.transition(pos, destQuaternion);
}
// don't show the indicator when using via api
this.hideCursor();
},
easeInOutQuad(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
},
tick(time, delta) {
if (!this.transitioning && !this.mobile) {
const pos = this.getTeleportPosition(this.mouseX, this.mouseY);
if (pos) {
this.teleportIndicator.position.copy(pos);
}
}
if (this.transitioning) {
this.transitionProgress += delta * this.data.transitionSpeed;
const easeInOutTransitionProgress = this.easeInOutQuad(this.transitionProgress);
const value =
easeInOutTransitionProgress < 0.5 ? easeInOutTransitionProgress : 0.5 - 1 * (easeInOutTransitionProgress - 0.5);
this.teleportIndicator.scale.set(0.5 + value, 1, 0.5 + value);
// set camera position
const camPos = this.camRig.position;
camPos.lerpVectors(this.transitionCamPosStart, this.transitionCamPosEnd, easeInOutTransitionProgress);
this.camRig.quaternion.slerpQuaternions(
this.transitionCamQuaternionStart,
this.transitionCamQuaternionEnd,
easeInOutTransitionProgress
);
if (this.transitionProgress >= 1) {
this.transitioning = false;
camPos.copy(this.transitionCamPosEnd);
this.camRig.quaternion.copy(this.transitionCamQuaternionEnd);
this.el.emit('navigation-end');
}
}
}
});