-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathWebARRocksHandThreeControls.js
More file actions
599 lines (471 loc) · 17 KB
/
Copy pathWebARRocksHandThreeControls.js
File metadata and controls
599 lines (471 loc) · 17 KB
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
/**
* Copyright 2020 WebAR.rocks ( https://webar.rocks )
*
* WARNING: YOU SHOULD NOT MODIFY THIS FILE OTHERWISE WEBAR.ROCKS
* WON'T BE RESPONSIBLE TO MAINTAIN AND KEEP YOUR ADDED FEATURES
* WEBAR.ROCKS WON'T BE LIABLE FOR BREAKS IN YOUR ADDED FUNCTIONNALITIES
*
* WEBAR.ROCKS KEEP THE RIGHT TO WORK ON AN UNMODIFIED VERSION OF THIS SCRIPT.
*
* THIS FILE IS A HELPER AND SHOULD NOT BE MODIFIED TO IMPLEMENT A SPECIFIC USER SCENARIO
* OR TO ADDRESS A SPECIFIC USE CASE.
*/
// Require hammer.js and Tween.js
const WebARRocksHandThreeControls = (function(){
const _defaultSpec = {
domElement: null,
isEnabled: true,
threeCamera: null,
threeObject: null,
parentPoseMat: null,
scaleRange: [0.5, 3],
scaleFactor: 0.5,
rotYFactor: 0.5, // number of rotation around Y axis if the user pan on the full screen
mode0: 'hand',
isMirrorX: false,
transitionDuration: 500, // in ms
rotXRange: [-Math.PI/3, Math.PI/3], // rotation X range in radians
eulerOrder: 'XYZ',
switchFromPinchToPanDScale: 0.2,
minDelayPanAfterPinch: 100,
debugAppendCubeAtOrigin: false
};
let _spec = null;
let _hm = null;
const _dragTypes = {
none: -1,
pan1: 0,
pan2: 1,
pinch: 2
};
const _transformTypes = {
none: -1,
rotate: 0,
translate: 1,
scale: 2
};
const _mapDragTypesToTransform = {};
let _dragType = _dragTypes.none;
const _three = {
eulerObj: null,
eulerView: null,
quaternionObj: null,
quaternionView: null,
quaternionObjectToCamera: null,
quaternionCameraToObject: null,
mat4: null,
vec4: null,
posViewRef: null,
posObject: null,
posCenterClipped: null,
posClipped: null
};
const _pose0 = {
euler: null,
quaternion: null,
position: null,
scale: -1,
scale0: 1,
parentPoseMatPrevious: null
};
const _timestamps = {
lastPinch: -1
};
const _modes = {
undef: -1,
hand: 0,
manual: 1
};
const _prev = {
rx: 0,
ry: 0
};
let _mode = _modes.undef;
let _isTransition = false;
const _tweens = {
transition: []
}
let _trackerParentPose0 = null, _trackerParentPose = null;
const _deg2rad = Math.PI/180;
function get_width(){
return window.innerWidth;
}
function get_height(){
return window.innerHeight;
}
function get_timestamp(){
return Date.now();
}
function extract_dxFromHMEvent(e){
let dx = e.deltaX;
if (_spec.isMirrorX){
dx *= -1;
}
return dx;
}
function extract_dyFromHMEvent(e){
return e.deltaY;
}
function save_pose(){
console.log('INFO in WebARRocksHandThreeControls: save_pose()');
_prev.rx = 0;
_prev.ry = 0;
_pose0.position.copy(_spec.threeObject.position);
_pose0.scale = _spec.threeObject.scale.x;
}
function scale(eventScale){
let s = _pose0.scale * (1 + _spec.scaleFactor * (eventScale - 1));
s = Math.min(s, _spec.scaleRange[1]);
s = Math.max(s, _spec.scaleRange[0]);
_spec.threeObject.scale.set(s, s, s);
}
function rotate(dx, dy){
const w = get_width(), h = get_height();
const rkx = _spec.rotYFactor * 2 * Math.PI;
const rky = rkx * h / w;
const ry = rkx * dx / w;
const rx = (_mode === _modes.manual) ? rky * dy / h : 0;
// compute differences:
const dry = ry - _prev.ry;
const drx = rx - _prev.rx;
_prev.rx = rx;
_prev.ry = ry;
// update rotation euler angle:
let ex = _pose0.euler.x + drx;
let ey = _pose0.euler.y + dry;
// clamp rotation around X axis:
ex = Math.max(_spec.rotXRange[0], ex);
ex = Math.min(_spec.rotXRange[1], ex);
_pose0.euler.set(ex, ey, 0, _spec.eulerOrder);
// apply rotation around Y axis in object ref:
_three.eulerObj.set(0, ey, 0, _spec.eulerOrder);
_three.quaternionObj.setFromEuler(_three.eulerObj);
// apply rotation arounx X axis in view ref:
_three.eulerView.set(ex, 0, 0, _spec.eulerOrder);
_three.quaternionView.setFromEuler(_three.eulerView);
// quaternion from object to camera (view ref);
const q = _three.quaternionObjectToCamera;
const qInv = _three.quaternionCameraToObject;
_spec.threeObject.quaternion.copy(_pose0.quaternion);
_spec.threeObject.getWorldQuaternion(q);
q.premultiply(_spec.threeCamera.quaternion);
qInv.copy(q).invert();
_three.quaternionView.premultiply(qInv).multiply(q);
_spec.threeObject.quaternion.multiply(_three.quaternionView);
_spec.threeObject.quaternion.multiply(_three.quaternionObj);
}
function translate(dx, dy){
//_spec.threeObject.updateMatrixWorld();
const posViewRef = _spec.threeObject.getWorldPosition(_three.posViewRef);
// position of the 3D object in 3D camera ref:
posViewRef.applyMatrix4(_spec.threeCamera.matrixWorldInverse);
const dZToCamera = -posViewRef.z;
// get displacement in viewport coordinates (between -1 and 1):
const w = get_width(), h = get_height();
let dxn = dx / w;
let dyn = - dy/ h;
// coordinates in world ref of center and displaced point:
_three.posCenterClipped.set(-dxn, -dyn, 1).unproject(_spec.threeCamera);
_three.posClipped.set(dxn, dyn, 1).unproject(_spec.threeCamera);
// get displacement on camera far plane in world coo:
_three.posClipped.sub(_three.posCenterClipped);
// go from far plane to object plane
_three.posClipped.multiplyScalar(dZToCamera / _spec.threeCamera.far);
// position in world coordinates:
const deltaPosWorld = _three.posClipped;
// translate posWorld to object co:
const posObject = _three.vec4;
const matrixWorld = _spec.threeObject.parent.matrixWorld;
const matrixWorldInv = _three.mat4.copy(matrixWorld).invert();
posObject.copy(deltaPosWorld).setW(0).applyMatrix4(matrixWorldInv).add(_pose0.position).setW(1);
_spec.threeObject.position.copy(posObject);
}
function clamp_positionToViewport(){
//const posViewRef = _spec.threeObject.getWorldPosition(_three.posViewRef);
_spec.threeObject.parent.updateMatrix();
_spec.threeObject.parent.updateMatrixWorld();
const matrixWorld = _spec.threeObject.parent.matrixWorld;
const matrixWorldInv = _three.mat4.copy(matrixWorld).invert();
const posViewRef = _three.posViewRef;
// first, to camera co:
posViewRef.copy(_spec.threeObject.position).applyMatrix4(matrixWorld).applyMatrix4(_spec.threeCamera.matrixWorldInverse);
// compute the values of the ellipse inside the frustum;
const tanHalfFovRad = Math.tan(_spec.threeCamera.getEffectiveFOV() * _deg2rad / 2);
const ry = 0.8 * -posViewRef.z * tanHalfFovRad;
const rx = 0.8 * ry * _spec.threeCamera.aspect;
const xClamped = Math.min(Math.max(posViewRef.x, -rx), rx);
const yClamped = Math.min(Math.max(posViewRef.y, -ry), ry);
const dxClamped = xClamped - posViewRef.x;
const dyClamped = yClamped - posViewRef.y;
//posViewRef.setX(xClamped).setY(yClamped);
posViewRef.setX(posViewRef.x + 0.1 * dxClamped).setY(posViewRef.y + 0.1 * dyClamped);
// go back to object co:
const posObject = _three.posObject;
posObject.copy(posViewRef).applyMatrix4(_spec.threeCamera.matrixWorld).applyMatrix4(matrixWorldInv);
_spec.threeObject.position.copy(posObject);
}
function set_transitionState(){
_isTransition = true;
_pose0.euler.set(0, 0, 0, _spec.eulerOrder);
_pose0.quaternion.copy(_spec.threeObject.quaternion);
// top all current transition tweens:
_tweens.transition.forEach(function(tt){
tt.stop();
});
_tweens.transition.splice(0);
}
function allocate_threeStuffs(){
_three.eulerObj = new THREE.Euler();
_three.eulerView = new THREE.Euler();
_three.quaternionObj = new THREE.Quaternion();
_three.quaternionView = new THREE.Quaternion();
_three.quaternionObjectToCamera = new THREE.Quaternion();
_three.quaternionCameraToObject = new THREE.Quaternion();
_three.posViewRef = new THREE.Vector3();
_three.posObject = new THREE.Vector3();
_three.posCenterClipped = new THREE.Vector3();
_three.posClipped = new THREE.Vector3();
_three.mat4 = new THREE.Matrix4();
_three.vec4 = new THREE.Vector4();
_pose0.euler = new THREE.Euler();
_pose0.quaternion = new THREE.Quaternion();
_pose0.position = new THREE.Vector3();
_pose0.parentPoseMatPrevious = new THREE.Matrix4();
}
function rotate_orTranslate(hmEvent){
const dx = extract_dxFromHMEvent(hmEvent), dy = extract_dyFromHMEvent(hmEvent);
const transformType = _mapDragTypesToTransform[_dragType];
if (transformType === _transformTypes.translate){
translate(dx, dy);
} else if (transformType === _transformTypes.rotate){
rotate(dx, dy);
}
}
function init_hmPinch(){
const hmPinch = new Hammer.Pinch();
_hm.add([hmPinch]);
_hm.on('pinch', function(e){
if (!that.is_interactive()) return;
if (_dragType === _dragTypes.pan2 && Math.abs(1 - e.scale) > _spec.switchFromPinchToPanDScale) {
_dragType = _dragTypes.pinch;
_pose0.scale0 = e.scale;
}
if(_dragType === _dragTypes.pinch && e.scale > 0){
scale(e.scale / _pose0.scale0);
} else if (_dragType === _dragTypes.pan2){
rotate_orTranslate(e);
}
})
_hm.on('pinchstart', function(e){
if (_dragType !== _dragTypes.none || !that.is_interactive()){
return;
}
_dragType = _dragTypes.pan2;
save_pose();
});
_hm.on('pinchend', function(e){
if (_dragType !== _dragTypes.pinch && _dragType !== _dragTypes.pan2){
return;
}
_timestamps.lastPinch = get_timestamp();
_dragType = _dragTypes.none;
});
}
function init_hmPan(){
const hmPan = new Hammer.Pan({ dragMaxTouches: 2 });
_hm.add([hmPan]);
_hm.on('panstart', function(e){
if (_dragType !== _dragTypes.none || !that.is_interactive()){
return;
}
const currentTimestamp = get_timestamp();
if (currentTimestamp - _timestamps.lastPinch < _spec.minDelayPanAfterPinch){
return;
}
_dragType = _dragTypes.pan1;
save_pose();
});
_hm.on('pan', function(e){
if (!that.is_interactive() || _dragType !== _dragTypes.pan1) return;
rotate_orTranslate(e);
});
_hm.on('panend', function(e){
if (_dragType !== _dragTypes.pan1){
return;
}
_dragType = _dragTypes.none;
});
}
function extract_trackerParentPose(mat, trackerParentPose){
trackerParentPose.position.setFromMatrixPosition(mat);
trackerParentPose.quaternion.setFromRotationMatrix(mat);
}
function do_tweenHandDetectedSmoothDisplacementForParent(){
const tweenHandDetectedSmoothDisplacement = new TWEEN.Tween({t: 0}).to({t: 1}, _spec.transitionDuration).onUpdate(function(v){
const t = v.t;
// what pose should be without tweening:
extract_trackerParentPose(_spec.parentPoseMat, _trackerParentPose);
// mix with start pose:
_trackerParentPose.position.lerp(_trackerParentPose0.position, 1-t);
_trackerParentPose.quaternion.slerp(_trackerParentPose0.quaternion, 1-t);
// update movement matrix:
const mat = _spec.parentPoseMat;
mat.makeRotationFromQuaternion(_trackerParentPose.quaternion);
mat.setPosition(_trackerParentPose.position);
}).easing(TWEEN.Easing.Quadratic.Out);
tweenHandDetectedSmoothDisplacement.start();
_tweens.transition.push(tweenHandDetectedSmoothDisplacement)
}
function init_trackerParentPoses(){
const create_pose = function(){
return {
position: new THREE.Vector3(),
quaternion: new THREE.Quaternion()
};
}
_trackerParentPose = create_pose();
_trackerParentPose0 = create_pose();
}
function set_mode(newMode){
if (newMode === _modes.hand){
_mapDragTypesToTransform[_dragTypes.pan1] = _transformTypes.rotate;
_mapDragTypesToTransform[_dragTypes.pan2] = _transformTypes.none;
} else if (newMode === _modes.manual){
_mapDragTypesToTransform[_dragTypes.pan1] = _transformTypes.translate;
_mapDragTypesToTransform[_dragTypes.pan2] = _transformTypes.rotate;
}
_mode = newMode;
}
const that = {
init: function(spec){
console.log('INFO in WebARRocksHandThreeControls: init()');
_spec = Object.assign({}, _defaultSpec, spec);
set_mode({
'hand': _modes.hand,
'manual': _modes.manual
}[_spec.mode0]);
// init THREE.js stuffs:
allocate_threeStuffs();
init_trackerParentPoses();
// init hammer.js
_hm = new Hammer.Manager(_spec.domElement);
init_hmPinch();
init_hmPan();
},
is_interactive: function(){
return (_spec.isEnabled && !_isTransition);
},
attach: function(threeObject, parentPoseMat){
_spec.threeObject = threeObject;
_spec.parentPoseMat = parentPoseMat;
if (_spec.debugAppendCubeAtOrigin){
const s = 0.5;
const debugCube = new THREE.Mesh(new THREE.BoxGeometry(s,s,s), new THREE.MeshNormalMaterial({}));
threeObject.add(debugCube);
}
},
tick: function(){
if (_mode === _modes.manual){
clamp_positionToViewport();
_pose0.parentPoseMatPrevious.copy(_spec.parentPoseMat);
}
},
toggle: function(isEnabled){
if (!isEnabled){
_dragType = _dragTypes.none;
}
_spec.isEnabled = isEnabled;
},
update: function(newSpec){
if (!_spec) return;
Object.assign(_spec, newSpec);
},
to_manual: function(){
if (_mode === _modes.manual){
return Promise.reject();
}
set_transitionState();
// start quaternion:
const qStart = new THREE.Quaternion().setFromRotationMatrix(_spec.parentPoseMat);
// compute quaternion with no rotation around Z view axis, qEnd:
const qWorld = new THREE.Quaternion();
_spec.threeObject.getWorldQuaternion(qWorld);
const euler = new THREE.Euler().setFromQuaternion(qWorld,'ZXY');
euler.set(euler.x, euler.y, 0, 'ZXY');
//euler.set(0, euler.y, 0, 'YXZ');
const qEndWorld = new THREE.Quaternion().setFromEuler(euler);
const qWorldInv = qWorld.clone().invert();
// transformation in world ref:
const qWorldTransf = qWorldInv.clone().premultiply(qEndWorld);
const qEnd = qStart.clone().premultiply(qWorldTransf);
const qSlerp = new THREE.Quaternion();
const pos = new THREE.Vector3();
return new Promise(function(accept, reject){
// we need to reset rotation around view Z axis for parent:
const tweenResetRotZViewAxis = new TWEEN.Tween({t: 0}).to({t: 1}, _spec.transitionDuration).onUpdate(function(v){
const t = v.t;
qSlerp.slerpQuaternions(qStart, qEnd, t);
// update movement matrix:
const mat = _spec.parentPoseMat;
pos.setFromMatrixPosition(mat); // save position
mat.makeRotationFromQuaternion(qSlerp);
mat.setPosition(pos); // restore position
}).easing(TWEEN.Easing.Quadratic.Out);
_tweens.transition.push(tweenResetRotZViewAxis);
tweenResetRotZViewAxis.onComplete(function(){
_isTransition = false;
set_mode(_modes.manual);
accept();
});
tweenResetRotZViewAxis.start();
});
},
to_hand: function(){
if (_mode === _modes.hand){
return Promise.reject();
}
set_transitionState();
extract_trackerParentPose(_pose0.parentPoseMatPrevious, _trackerParentPose0);
return new Promise(function(accept, reject){
// remove translation and rx components:
const tweenTranslation = new TWEEN.Tween(_spec.threeObject.position).to({
x:0, y:0, z:0
}, _spec.transitionDuration).easing(TWEEN.Easing.Quadratic.Out);
// compute the quaternion where only ry remains:
const eulerTo = new THREE.Euler().setFromQuaternion(_spec.threeObject.quaternion, 'YXZ');
eulerTo.set(0, eulerTo.y, 0);
const quaternionTo = new THREE.Quaternion().setFromEuler(eulerTo);
const quaternionFrom = _spec.threeObject.quaternion.clone();
const tweenRotation = new TWEEN.Tween({t: 0}).to({t: 1}, _spec.transitionDuration).onUpdate(function(v){
const t = v.t;
_spec.threeObject.quaternion.copy(quaternionFrom).slerp(quaternionTo, t);
}).easing(TWEEN.Easing.Quadratic.Out);
tweenTranslation.start();
tweenRotation.start();
_tweens.transition.push(tweenTranslation, tweenRotation);
do_tweenHandDetectedSmoothDisplacementForParent();
tweenTranslation.onComplete(function(){
_isTransition = false;
set_mode(_modes.hand);
accept();
})
});
},
destroy: function(){
if (TWEEN){
TWEEN.removeAll();
}
_tweens.transition.splice(0);
_dragType = _dragTypes.none;
_isTransition = false;
_mode = _modes.undef;
}
} // end that
return that;
})();
// Export ES6 module:
try {
module.exports = WebARRocksHandThreeControls;
} catch(e){
console.log('ES6 Module not exported');
}