-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawingComponents.js
593 lines (514 loc) · 18.9 KB
/
DrawingComponents.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
AFRAME.registerComponent('modify-materials', {
init: function () {
// Wait for model to load.
this.el.addEventListener('model-loaded', () => {
// Grab the mesh / scene.
const obj = this.el.getObject3D('mesh');
obj.position.set(0,.055,0)
// Go over the submeshes and modify materials we want.
obj.traverse(node => {
// set the scale to be .3 of original model
if(node.scale != null){
node.scale.set(.3,.3,.3)
}
// set material to be flat shaded, ignores lighting
if(node.material != null){
node.material = new THREE.MeshBasicMaterial({ map: node.material.map });
}
});
});
}
});
// component to look at drawing when in the lobby
AFRAME.registerComponent('look-at', {
schema: { type: 'selector' },
init: function () {},
tick: function () {
this.el.object3D.lookAt(this.data.object3D.position)
}
});
// component that indicates which hand is the brush and what the color is for that brush
AFRAME.registerComponent('brush', {
schema: {
active : {type: 'boolean', default: false},
color: {type: 'string', default: 'white'}
},
init: function () {}
});
// component that indicates if the trigger is pressed for either hand
AFRAME.registerComponent('pressed', {
schema: {type: 'boolean', default: false},
init: function () {}
});
// listeners for right hand
AFRAME.registerComponent('button-listener-r', {
init: function () {
var el = this.el;
// when trigger is pressed, set trigger to be down and go on to the next stroke
el.addEventListener('triggerdown', function (evt) {
this.setAttribute('pressed',true)
nextStroke()
});
// when trigger is done being pressed, set trigger to be up
el.addEventListener('triggerup', function (evt) {
this.setAttribute('pressed',false)
});
// when abutton is pressed, move right hand color forward by 1
el.addEventListener('abuttondown', function (evt) {
changeColor(true,true)
});
// when bbutton is pressed, move right hand color backward by 1
el.addEventListener('bbuttondown', function (evt) {
changeColor(true,false)
});
// when thumbstick is pressed, make the right hand the drawing hand
el.addEventListener('thumbstickdown', function (evt){
changeBrushHand(true)
})
// when grip is pressed, redo the last change
el.addEventListener('gripdown', function (evt) {
redo()
});
}
});
// listeners for left hand
AFRAME.registerComponent('button-listener-l', {
init: function () {
var el = this.el;
// when trigger is pressed, set trigger to be down and go on to the next stroke
el.addEventListener('triggerdown', function (evt) {
this.setAttribute('pressed',true)
nextStroke()
});
// when trigger is done being pressed, set trigger to be up
el.addEventListener('triggerup', function (evt) {
this.setAttribute('pressed',false)
});
// when xbutton is pressed, move left hand color forward by 1
el.addEventListener('xbuttondown', function (evt) {
changeColor(false,true)
});
// when ybutton is pressed, move left hand color backward by 1
el.addEventListener('ybuttondown', function (evt) {
changeColor(false,false)
});
// when thumbstick is pressed, make the left hand the drawing hand
el.addEventListener('thumbstickdown', function (evt){
changeBrushHand(false)
})
// when grip is pressed, undo the last change
el.addEventListener('gripdown', function (evt) {
undo()
});
}
});
AFRAME.registerComponent('thumbstick-logging',{
init: function () {
this.el.addEventListener('thumbstickmoved', this.logThumbstick);
},
logThumbstick: function (evt) {
if(!this.el.getAttribute('brush').active){
if(evt.detail.x != 0 && evt.detail.y != 0){
moveRig(evt.detail.x,evt.detail.y)
}
}
}
});
/* global DeviceOrientationEvent */
var bind = AFRAME.utils.bind;
// To avoid recalculation at every mouse movement tick
var PI_2 = Math.PI / 2;
AFRAME.registerComponent('look-controls-custom', {
dependencies: ['position', 'rotation'],
schema: {
enabled: {default: true},
magicWindowTrackingEnabled: {default: true},
pointerLockEnabled: {default: false},
reverseMouseDrag: {default: false},
reverseTouchDrag: {default: false},
touchEnabled: {default: true},
mouseEnabled: {default: true}
},
init: function () {
this.deltaYaw = 0;
this.previousHMDPosition = new THREE.Vector3();
this.hmdQuaternion = new THREE.Quaternion();
this.magicWindowAbsoluteEuler = new THREE.Euler();
this.magicWindowDeltaEuler = new THREE.Euler();
this.position = new THREE.Vector3();
this.magicWindowObject = new THREE.Object3D();
this.rotation = {};
this.deltaRotation = {};
this.savedPose = null;
this.pointerLocked = false;
this.setupMouseControls();
this.bindMethods();
this.previousMouseEvent = {};
this.setupMagicWindowControls();
// To save / restore camera pose
this.savedPose = {
position: new THREE.Vector3(),
rotation: new THREE.Euler()
};
// Call enter VR handler if the scene has entered VR before the event listeners attached.
if (this.el.sceneEl.is('vr-mode') || this.el.sceneEl.is('ar-mode')) { this.onEnterVR(); }
},
setupMagicWindowControls: function () {
var magicWindowControls;
var data = this.data;
// Only on mobile devices and only enabled if DeviceOrientation permission has been granted.
if (AFRAME.utils.device.isMobile() || AFRAME.utils.device.isMobileDeviceRequestingDesktopSite()) {
magicWindowControls = this.magicWindowControls = new THREE.DeviceOrientationControls(this.magicWindowObject);
if (typeof DeviceOrientationEvent !== 'undefined' && DeviceOrientationEvent.requestPermission) {
magicWindowControls.enabled = false;
if (this.el.sceneEl.components['device-orientation-permission-ui'].permissionGranted) {
magicWindowControls.enabled = data.magicWindowTrackingEnabled;
} else {
this.el.sceneEl.addEventListener('deviceorientationpermissiongranted', function () {
magicWindowControls.enabled = data.magicWindowTrackingEnabled;
});
}
}
}
},
update: function (oldData) {
var data = this.data;
// Disable grab cursor classes if no longer enabled.
if (data.enabled !== oldData.enabled) {
this.updateGrabCursor(data.enabled);
}
// Reset magic window eulers if tracking is disabled.
if (oldData && !data.magicWindowTrackingEnabled && oldData.magicWindowTrackingEnabled) {
this.magicWindowAbsoluteEuler.set(0, 0, 0);
this.magicWindowDeltaEuler.set(0, 0, 0);
}
// Pass on magic window tracking setting to magicWindowControls.
if (this.magicWindowControls) {
this.magicWindowControls.enabled = data.magicWindowTrackingEnabled;
}
if (oldData && !data.pointerLockEnabled !== oldData.pointerLockEnabled) {
this.removeEventListeners();
this.addEventListeners();
if (this.pointerLocked) { this.exitPointerLock(); }
}
},
tick: function (t) {
var data = this.data;
if (!data.enabled) { return; }
this.updateOrientation();
},
play: function () {
this.addEventListeners();
},
pause: function () {
this.removeEventListeners();
if (this.pointerLocked) { this.exitPointerLock(); }
},
remove: function () {
this.removeEventListeners();
if (this.pointerLocked) { this.exitPointerLock(); }
},
bindMethods: function () {
this.onMouseDown = bind(this.onMouseDown, this);
this.onMouseMove = bind(this.onMouseMove, this);
this.onMouseUp = bind(this.onMouseUp, this);
this.onTouchStart = bind(this.onTouchStart, this);
this.onTouchMove = bind(this.onTouchMove, this);
this.onTouchEnd = bind(this.onTouchEnd, this);
this.onEnterVR = bind(this.onEnterVR, this);
this.onExitVR = bind(this.onExitVR, this);
this.onPointerLockChange = bind(this.onPointerLockChange, this);
this.onPointerLockError = bind(this.onPointerLockError, this);
},
/**
* Set up states and Object3Ds needed to store rotation data.
*/
setupMouseControls: function () {
this.mouseDown = false;
this.pitchObject = new THREE.Object3D();
this.yawObject = new THREE.Object3D();
this.yawObject.position.y = 10;
this.yawObject.add(this.pitchObject);
},
/**
* Add mouse and touch event listeners to canvas.
*/
addEventListeners: function () {
var sceneEl = this.el.sceneEl;
var canvasEl = sceneEl.canvas;
// Wait for canvas to load.
if (!canvasEl) {
sceneEl.addEventListener('render-target-loaded', bind(this.addEventListeners, this));
return;
}
// Mouse events.
canvasEl.addEventListener('mousedown', this.onMouseDown, false);
window.addEventListener('mousemove', this.onMouseMove, false);
window.addEventListener('mouseup', this.onMouseUp, false);
// Touch events.
canvasEl.addEventListener('touchstart', this.onTouchStart);
window.addEventListener('touchmove', this.onTouchMove);
window.addEventListener('touchend', this.onTouchEnd);
// sceneEl events.
sceneEl.addEventListener('enter-vr', this.onEnterVR);
sceneEl.addEventListener('exit-vr', this.onExitVR);
// Pointer Lock events.
if (this.data.pointerLockEnabled) {
document.addEventListener('pointerlockchange', this.onPointerLockChange, false);
document.addEventListener('mozpointerlockchange', this.onPointerLockChange, false);
document.addEventListener('pointerlockerror', this.onPointerLockError, false);
}
},
/**
* Remove mouse and touch event listeners from canvas.
*/
removeEventListeners: function () {
var sceneEl = this.el.sceneEl;
var canvasEl = sceneEl && sceneEl.canvas;
if (!canvasEl) { return; }
// Mouse events.
canvasEl.removeEventListener('mousedown', this.onMouseDown);
window.removeEventListener('mousemove', this.onMouseMove);
window.removeEventListener('mouseup', this.onMouseUp);
// Touch events.
canvasEl.removeEventListener('touchstart', this.onTouchStart);
window.removeEventListener('touchmove', this.onTouchMove);
window.removeEventListener('touchend', this.onTouchEnd);
// sceneEl events.
sceneEl.removeEventListener('enter-vr', this.onEnterVR);
sceneEl.removeEventListener('exit-vr', this.onExitVR);
// Pointer Lock events.
document.removeEventListener('pointerlockchange', this.onPointerLockChange, false);
document.removeEventListener('mozpointerlockchange', this.onPointerLockChange, false);
document.removeEventListener('pointerlockerror', this.onPointerLockError, false);
},
/**
* Update orientation for mobile, mouse drag, and headset.
* Mouse-drag only enabled if HMD is not active.
*/
updateOrientation: function () {
var object3D = this.el.object3D;
var pitchObject = this.pitchObject;
var yawObject = this.yawObject;
var sceneEl = this.el.sceneEl;
// In VR or AR mode, THREE is in charge of updating the camera pose.
if ((sceneEl.is('vr-mode') || sceneEl.is('ar-mode')) && sceneEl.checkHeadsetConnected()) {
// With WebXR THREE applies headset pose to the object3D internally.
return;
}
this.updateMagicWindowOrientation();
// On mobile, do camera rotation with touch events and sensors.
object3D.rotation.x = this.magicWindowDeltaEuler.x + pitchObject.rotation.x;
object3D.rotation.y = this.magicWindowDeltaEuler.y + yawObject.rotation.y;
object3D.rotation.z = this.magicWindowDeltaEuler.z;
},
updateMagicWindowOrientation: function () {
var magicWindowAbsoluteEuler = this.magicWindowAbsoluteEuler;
var magicWindowDeltaEuler = this.magicWindowDeltaEuler;
// Calculate magic window HMD quaternion.
if (this.magicWindowControls && this.magicWindowControls.enabled) {
this.magicWindowControls.update();
magicWindowAbsoluteEuler.setFromQuaternion(this.magicWindowObject.quaternion, 'YXZ');
if (!this.previousMagicWindowYaw && magicWindowAbsoluteEuler.y !== 0) {
this.previousMagicWindowYaw = magicWindowAbsoluteEuler.y;
}
if (this.previousMagicWindowYaw) {
magicWindowDeltaEuler.x = magicWindowAbsoluteEuler.x;
magicWindowDeltaEuler.y += magicWindowAbsoluteEuler.y - this.previousMagicWindowYaw;
magicWindowDeltaEuler.z = magicWindowAbsoluteEuler.z;
this.previousMagicWindowYaw = magicWindowAbsoluteEuler.y;
}
}
},
/**
* Translate mouse drag into rotation.
*
* Dragging up and down rotates the camera around the X-axis (yaw).
* Dragging left and right rotates the camera around the Y-axis (pitch).
*/
onMouseMove: function (evt) {
var direction;
var movementX;
var movementY;
var pitchObject = this.pitchObject;
var previousMouseEvent = this.previousMouseEvent;
var yawObject = this.yawObject;
// Not dragging or not enabled.
if (!this.data.enabled || (!this.mouseDown && !this.pointerLocked)) { return; }
// Calculate delta.
if (this.pointerLocked) {
movementX = evt.movementX || evt.mozMovementX || 0;
movementY = evt.movementY || evt.mozMovementY || 0;
} else {
movementX = evt.screenX - previousMouseEvent.screenX;
movementY = evt.screenY - previousMouseEvent.screenY;
}
this.previousMouseEvent.screenX = evt.screenX;
this.previousMouseEvent.screenY = evt.screenY;
// Calculate rotation.
direction = this.data.reverseMouseDrag ? 1 : -1;
yawObject.rotation.y += movementX * 0.002 * direction * sens;
pitchObject.rotation.x += movementY * 0.002 * direction * sens;
pitchObject.rotation.x = Math.max(-PI_2, Math.min(PI_2, pitchObject.rotation.x));
},
/**
* Register mouse down to detect mouse drag.
*/
onMouseDown: function (evt) {
var sceneEl = this.el.sceneEl;
if (!this.data.enabled || !this.data.mouseEnabled || ((sceneEl.is('vr-mode') || sceneEl.is('ar-mode')) && sceneEl.checkHeadsetConnected())) { return; }
// Handle only primary button.
if (evt.button !== 0) { return; }
var canvasEl = sceneEl && sceneEl.canvas;
this.mouseDown = true;
this.previousMouseEvent.screenX = evt.screenX;
this.previousMouseEvent.screenY = evt.screenY;
this.showGrabbingCursor();
if (this.data.pointerLockEnabled && !this.pointerLocked) {
if (canvasEl.requestPointerLock) {
canvasEl.requestPointerLock();
} else if (canvasEl.mozRequestPointerLock) {
canvasEl.mozRequestPointerLock();
}
}
},
/**
* Shows grabbing cursor on scene
*/
showGrabbingCursor: function () {
this.el.sceneEl.canvas.style.cursor = 'grabbing';
},
/**
* Hides grabbing cursor on scene
*/
hideGrabbingCursor: function () {
this.el.sceneEl.canvas.style.cursor = '';
},
/**
* Register mouse up to detect release of mouse drag.
*/
onMouseUp: function () {
this.mouseDown = false;
this.hideGrabbingCursor();
},
/**
* Register touch down to detect touch drag.
*/
onTouchStart: function (evt) {
if (evt.touches.length !== 1 ||
!this.data.touchEnabled ||
this.el.sceneEl.is('vr-mode') ||
this.el.sceneEl.is('ar-mode')) { return; }
this.touchStart = {
x: evt.touches[0].pageX,
y: evt.touches[0].pageY
};
this.touchStarted = true;
},
/**
* Translate touch move to Y-axis rotation.
*/
onTouchMove: function (evt) {
var direction;
var canvas = this.el.sceneEl.canvas;
var deltaY;
var yawObject = this.yawObject;
if (!this.touchStarted || !this.data.touchEnabled) { return; }
deltaY = 2 * Math.PI * (evt.touches[0].pageX - this.touchStart.x) / canvas.clientWidth;
direction = this.data.reverseTouchDrag ? 1 : -1;
// Limit touch orientaion to to yaw (y axis).
yawObject.rotation.y -= deltaY * 0.5 * direction;
this.touchStart = {
x: evt.touches[0].pageX,
y: evt.touches[0].pageY
};
},
/**
* Register touch end to detect release of touch drag.
*/
onTouchEnd: function () {
this.touchStarted = false;
},
/**
* Save pose.
*/
onEnterVR: function () {
var sceneEl = this.el.sceneEl;
if (!sceneEl.checkHeadsetConnected()) { return; }
this.saveCameraPose();
this.el.object3D.position.set(0, 0, 0);
this.el.object3D.rotation.set(0, 0, 0);
if (sceneEl.hasWebXR) {
this.el.object3D.matrixAutoUpdate = false;
this.el.object3D.updateMatrix();
}
},
/**
* Restore the pose.
*/
onExitVR: function () {
if (!this.el.sceneEl.checkHeadsetConnected()) { return; }
this.restoreCameraPose();
this.previousHMDPosition.set(0, 0, 0);
this.el.object3D.matrixAutoUpdate = true;
},
/**
* Update Pointer Lock state.
*/
onPointerLockChange: function () {
this.pointerLocked = !!(document.pointerLockElement || document.mozPointerLockElement);
},
/**
* Recover from Pointer Lock error.
*/
onPointerLockError: function () {
this.pointerLocked = false;
},
// Exits pointer-locked mode.
exitPointerLock: function () {
document.exitPointerLock();
this.pointerLocked = false;
},
/**
* Toggle the feature of showing/hiding the grab cursor.
*/
updateGrabCursor: function (enabled) {
var sceneEl = this.el.sceneEl;
function enableGrabCursor () { sceneEl.canvas.classList.add('a-grab-cursor'); }
function disableGrabCursor () { sceneEl.canvas.classList.remove('a-grab-cursor'); }
if (!sceneEl.canvas) {
if (enabled) {
sceneEl.addEventListener('render-target-loaded', enableGrabCursor);
} else {
sceneEl.addEventListener('render-target-loaded', disableGrabCursor);
}
return;
}
if (enabled) {
enableGrabCursor();
return;
}
disableGrabCursor();
},
/**
* Save camera pose before entering VR to restore later if exiting.
*/
saveCameraPose: function () {
var el = this.el;
this.savedPose.position.copy(el.object3D.position);
this.savedPose.rotation.copy(el.object3D.rotation);
this.hasSavedPose = true;
},
/**
* Reset camera pose to before entering VR.
*/
restoreCameraPose: function () {
var el = this.el;
var savedPose = this.savedPose;
if (!this.hasSavedPose) { return; }
// Reset camera orientation.
el.object3D.position.copy(savedPose.position);
el.object3D.rotation.copy(savedPose.rotation);
this.hasSavedPose = false;
}
});