forked from AVGP/a-touch-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-touch-controls.js
409 lines (350 loc) · 12 KB
/
a-touch-controls.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
/*
var registerComponent = require('../core/component').registerComponent;
var THREE = require('../lib/three');
var DEFAULT_CAMERA_HEIGHT = require('../constants').DEFAULT_CAMERA_HEIGHT;
var bind = require('../utils/bind');
*/
// To avoid recalculation at every mouse movement tick
var GRABBING_CLASS = 'a-grabbing';
var DEFAULT_CAMERA_HEIGHT = 0;
var PI_2 = Math.PI / 2;
var radToDeg = THREE.Math.radToDeg;
/**
* look-controls. Update entity pose, factoring mouse, touch, and WebVR API data.
*/
AFRAME.registerComponent('touch-controls', {
dependencies: ['position', 'rotation'],
schema: {
enabled: {default: true},
touchEnabled: {default: true},
reverseMouseDrag: {default: false},
hmdEnabled: { default: false },
standing: {default: true}
},
init: function () {
var sceneEl = this.el.sceneEl;
this.previousHMDPosition = new THREE.Vector3();
this.hmdQuaternion = new THREE.Quaternion();
this.hmdEuler = new THREE.Euler();
this.position = {};
this.rotation = {};
this.setupMouseControls();
this.setupHMDControls();
this.bindMethods();
// Reset previous HMD position when we exit VR.
sceneEl.addEventListener('exit-vr', this.onExitVR);
},
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 pitch and yaw if disabling HMD.
if (oldData && !data.hmdEnabled && !oldData.hmdEnabled) {
this.pitchObject.rotation.set(0, 0, 0);
this.yawObject.rotation.set(0, 0, 0);
}
},
tick: function (t) {
var data = this.data;
if (!data.enabled) { return; }
this.controls.standing = data.standing;
this.controls.userHeight = this.getUserHeight();
this.controls.update();
this.updateOrientation();
this.updatePosition();
},
/**
* Return user height to use for standing poses, where a device doesn't provide an offset.
*/
getUserHeight: function () {
var el = this.el;
var userHeight = el.hasAttribute('camera') && el.getAttribute('camera').userHeight || DEFAULT_CAMERA_HEIGHT;
return userHeight;
},
play: function () {
this.addEventListeners();
},
pause: function () {
this.removeEventListeners();
},
remove: function () {
this.removeEventListeners();
},
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.onExitVR = bind(this.onExitVR, 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);
},
/**
* Set up VR controls that will copy data to the dolly.
*/
setupHMDControls: function () {
this.dolly = new THREE.Object3D();
this.euler = new THREE.Euler();
this.controls = new THREE.VRControls(this.dolly);
this.controls.userHeight = 0.0;
},
/**
* 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);
},
/**
* 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);
canvasEl.removeEventListener('mousemove', this.onMouseMove);
canvasEl.removeEventListener('mouseup', this.onMouseUp);
canvasEl.removeEventListener('mouseout', this.onMouseUp);
// Touch events.
canvasEl.removeEventListener('touchstart', this.onTouchStart);
canvasEl.removeEventListener('touchmove', this.onTouchMove);
canvasEl.removeEventListener('touchend', this.onTouchEnd);
},
/**
* Update orientation for mobile, mouse drag, and headset.
* Mouse-drag only enabled if HMD is not active.
*/
updateOrientation: function () {
var currentRotation;
var deltaRotation;
var hmdEuler = this.hmdEuler;
var hmdQuaternion = this.hmdQuaternion;
var pitchObject = this.pitchObject;
var yawObject = this.yawObject;
var sceneEl = this.el.sceneEl;
var rotation = this.rotation;
// Calculate HMD quaternion.
hmdQuaternion = hmdQuaternion.copy(this.dolly.quaternion);
hmdEuler.setFromQuaternion(hmdQuaternion, 'YXZ');
if (sceneEl.isMobile && !sceneEl.is('vr-mode')) {
// nah...
rotation = {
x: radToDeg(pitchObject.rotation.x),
y: radToDeg(yawObject.rotation.y)
};
} else if(sceneEl.isMobile && sceneEl.is('vr-mode')) {
rotation.x = radToDeg(hmdEuler.x) + radToDeg(pitchObject.rotation.x);
rotation.y = radToDeg(hmdEuler.y) + radToDeg(yawObject.rotation.y);
rotation.z = radToDeg(hmdEuler.z);
} else if (!sceneEl.is('vr-mode') || isNullVector(hmdEuler) || !this.data.hmdEnabled) {
// Mouse drag if WebVR not active (not connected, no incoming sensor data).
currentRotation = this.el.getAttribute('rotation');
deltaRotation = this.calculateDeltaRotation();
if (this.data.reverseMouseDrag) {
rotation.x = currentRotation.x - deltaRotation.x;
rotation.y = currentRotation.y - deltaRotation.y;
rotation.z = currentRotation.z;
} else {
rotation.x = currentRotation.x + deltaRotation.x;
rotation.y = currentRotation.y + deltaRotation.y;
rotation.z = currentRotation.z;
}
} else {
// Mouse rotation ignored with an active headset. Use headset rotation.
rotation.x = radToDeg(hmdEuler.x);
rotation.y = radToDeg(hmdEuler.y);
rotation.z = radToDeg(hmdEuler.z);
}
this.el.setAttribute('rotation', rotation);
},
/**
* Calculate delta rotation for mouse-drag and touch-drag.
*/
calculateDeltaRotation: function () {
var currentRotationX = radToDeg(this.pitchObject.rotation.x);
var currentRotationY = radToDeg(this.yawObject.rotation.y);
var deltaRotation;
deltaRotation = {
x: currentRotationX - (this.previousRotationX || 0),
y: currentRotationY - (this.previousRotationY || 0)
};
// Store current rotation for next tick.
this.previousRotationX = currentRotationX;
this.previousRotationY = currentRotationY;
return deltaRotation;
},
/**
* Handle positional tracking.
*/
updatePosition: (function () {
var deltaHMDPosition = new THREE.Vector3();
return function () {
var el = this.el;
var currentHMDPosition;
var currentPosition;
var position = this.position;
var previousHMDPosition = this.previousHMDPosition;
var sceneEl = this.el.sceneEl;
if (!sceneEl.is('vr-mode')) { return; }
// Calculate change in position.
currentHMDPosition = this.calculateHMDPosition();
deltaHMDPosition.copy(currentHMDPosition).sub(previousHMDPosition);
if (isNullVector(deltaHMDPosition)) { return; }
previousHMDPosition.copy(currentHMDPosition);
currentPosition = el.getAttribute('position');
position.x = currentPosition.x + deltaHMDPosition.x;
position.y = currentPosition.y + deltaHMDPosition.y;
position.z = currentPosition.z + deltaHMDPosition.z;
el.setAttribute('position', position);
};
})(),
/**
* Get headset position from VRControls.
*/
calculateHMDPosition: (function () {
var position = new THREE.Vector3();
return function () {
this.dolly.updateMatrix();
position.setFromMatrixPosition(this.dolly.matrix);
return position;
};
})(),
/**
* 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 (event) {
var pitchObject = this.pitchObject;
var yawObject = this.yawObject;
var previousMouseEvent = this.previousMouseEvent;
var movementX;
var movementY;
// Not dragging or not enabled.
if (!this.mouseDown || !this.data.enabled) { return; }
// Calculate delta.
movementX = event.movementX || event.mozMovementX;
movementY = event.movementY || event.mozMovementY;
if (movementX === undefined || movementY === undefined) {
movementX = event.screenX - previousMouseEvent.screenX;
movementY = event.screenY - previousMouseEvent.screenY;
}
this.previousMouseEvent = event;
// Calculate rotation.
yawObject.rotation.y -= movementX * 0.002;
pitchObject.rotation.x -= movementY * 0.002;
pitchObject.rotation.x = Math.max(-PI_2, Math.min(PI_2, pitchObject.rotation.x));
},
/**
* Register mouse down to detect mouse drag.
*/
onMouseDown: function (evt) {
if (!this.data.enabled) { return; }
// Handle only primary button.
if (evt.button !== 0) { return; }
this.mouseDown = true;
this.previousMouseEvent = evt;
document.body.classList.add(GRABBING_CLASS);
},
/**
* Register mouse up to detect release of mouse drag.
*/
onMouseUp: function () {
this.mouseDown = false;
document.body.classList.remove(GRABBING_CLASS);
},
/**
* Register touch down to detect touch drag.
*/
onTouchStart: function (evt) {
if (evt.touches.length !== 1 || !this.data.touchEnabled) { 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 canvas = this.el.sceneEl.canvas;
var deltaY, deltaX;
var yawObject = this.yawObject;
var pitchObject = this.pitchObject;
if (!this.touchStarted || !this.data.touchEnabled) { return; }
deltaY = 2 * Math.PI * (evt.touches[0].pageX - this.touchStart.x) / canvas.clientWidth;
deltaX = 2 * Math.PI * (evt.touches[0].pageY - this.touchStart.y) / this.el.sceneEl.canvas.clientHeight;
// Limit touch orientaion to to yaw (y axis).
yawObject.rotation.y -= deltaY * 0.5;
pitchObject.rotation.x -= deltaX * 0.5;
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;
},
onExitVR: function () {
this.previousHMDPosition.set(0, 0, 0);
},
/**
* 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();
}
});
function isNullVector (vector) {
return vector.x === 0 && vector.y === 0 && vector.z === 0;
}
function bind(fn, ctx) {
return fn.bind(ctx)
}