-
Notifications
You must be signed in to change notification settings - Fork 12
/
SpinControls.js
675 lines (433 loc) · 18.8 KB
/
SpinControls.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/**
* @author Eberhard Graether / http://egraether.com/
* @author Mark Lundin / http://mark-lundin.com
* @author Simone Manini / http://daron1337.github.io
* @author Luca Antiga / http://lantiga.github.io
* @author Paul Elliott / http://vizworkshop.com
*/
var SpinControls = function ( object, trackBallRadius, camera, domElement ) {
var _this = this;
this.object = object;
this.trackballRadius = trackBallRadius;
this.camera = camera;
this.domElement = ( domElement !== undefined ) ? domElement : document;
// API
this.enabled = true;
this.rotateSensitivity = 1.0; // Keep at 1 for direct touching feel
this.relativelySpinOffTrackball = true; // Rotation continues relatively when pointer is beyond trackball
this.enableDamping = true; // True for movement with momentum after pointer release on control.update
this.dampingFactor = 5; // Increase for more friction
this.spinAxisConstraint; // Set to a THREE.Vector3 to limit spinning to about an axis
// Raycast projects pointer line through camera frustum for accurate trackball control.
// Shoemake has direct touching feel of pointer on orthographically projected sphere but jumps at sphere edge.
// Holyroyd smooths between sphere and hyperbola to avoid jump at sphere edge.
// Azimuthal from Yasuhiro Fujii has unlimited rotation behond the sphere edge.
this.POINTER_SPHERE_MAPPING = { SHOEMAKE: 'shoemake', HOLROYD: 'holroyd',
AZIMUTHAL: 'azimuthal', RAYCAST: 'raycast' };
// Base this on angle change around sphere edge?
this.offTrackBallVelocityGainMap = {
'shoemake': 20,
'holroyd': 8,
'azimuthal': 8,
'raycast': 20
};
// Internals
this._pointerMapping = this.POINTER_SPHERE_MAPPING.RAYCAST;
this._offTrackBallVelocityGain = this.offTrackBallVelocityGainMap[this._pointerMapping];
this._pointerUpVelDamping = 2000;
this.screen = { left: 0, top: 0, width: 0, height: 0 };
var _angularVelocity = new THREE.Vector3(0, 0, 0),
_lastQuaternion = new THREE.Quaternion(),
_lastVelTime,
_pointOnSphere = new THREE.Vector3(),
_pointerScreen = new THREE.Vector2(),
_pointOnSphereOld = new THREE.Vector3(),
_lastPointerEventTime = 0,
_wasLastPointerEventOnSphere = false,
_isPointerDown = false,
_EPS = 0.000001;
var changeEvent = { type: 'change' };
var startEvent = { type: 'start' };
var endEvent = { type: 'end' };
this.update = ( function () {
var currentTime;
var lastTime = performance.now() / 1000.0;
var deltaTime;
return function update() {
currentTime = performance.now() / 1000.0;
deltaTime = currentTime - lastTime;
lastTime = currentTime;
if( !_isPointerDown && _this.enableDamping ) {
_angularVelocity.multiplyScalar( 1 / ( deltaTime * _this.dampingFactor + 1 ) );
_this.applyVelocity();
}
if( !_this.enableDamping ) {
_lastVelTime = performance.now(); // ToDo Avoid this hack. Causes trackball drift.
}
_this.hasPointerMovedThisFrame = false;
};
}() );
this.updateAngularVelocity = ( function () {
var q0 = new THREE.Quaternion(),
q1 = new THREE.Quaternion(),
q0Conj = new THREE.Quaternion(); //for path independent rotation
return function updateAngularVelocity( p1, p0, timeDelta ) {
// path independent rotation from Shoemake
q0Conj.set(p0.x, p0.y, p0.z, 0.0)
q0Conj.normalize();
q0Conj.conjugate();
q1.set(p1.x, p1.y, p1.z, 0.0).multiply(q0Conj);
timeDelta *= 2.0; // divide angleDelta by 2 to keep sphere under pointer. Might break algorithm properties, TODO: perhaps investigate.
// path dependent
// q1.setFromUnitVectors(p0, p1);
q0.set(p0.x, p0.y, p0.z, 1.0);
var angleSpeed = q1.angleTo(q0) / timeDelta;
// Just set velocity because we are touching trackball without sliding
_angularVelocity.crossVectors( p0, p1);
_angularVelocity.setLength( angleSpeed );
_this.applyVelocity();
};
}() );
this.applyVelocity = ( function () {
var quat = new THREE.Quaternion(),
normalizedAxis = new THREE.Vector3(),
deltaAngle,
deltaTime,
timeStamp;
return function applyVelocity() {
timeStamp = performance.now();
deltaTime = ( timeStamp - _lastVelTime ) / 1000.0;
_lastVelTime = timeStamp;
if( _this.spinAxisConstraint ) {
normalizedAxis.copy( _this.spinAxisConstraint );
deltaAngle = normalizedAxis.dot( _angularVelocity ) ;
} else {
normalizedAxis.copy( _angularVelocity );
deltaAngle = _angularVelocity.length();
}
if ( deltaAngle && deltaTime ) {
normalizedAxis.normalize();
quat.setFromAxisAngle( normalizedAxis, deltaAngle * deltaTime * _this.rotateSensitivity );
_this.object.quaternion.normalize();
_this.object.quaternion.premultiply(quat);
// using small-angle approximation cos(x/2) = 1 - x^2 / 8
if ( 8 * ( 1 - _lastQuaternion.dot( _this.object.quaternion ) ) > _EPS) {
_this.dispatchEvent( changeEvent );
_lastQuaternion.copy( _this.object.quaternion );
}
}
};
}() );
this.onWindowResize = ( function () {
if ( _this.domElement === document ) {
_this.screen.left = 0;
_this.screen.top = 0;
_this.screen.width = window.innerWidth;
_this.screen.height = window.innerHeight;
} else {
var box = _this.domElement.getBoundingClientRect();
var d = _this.domElement.ownerDocument.documentElement;
_this.screen.left = box.left + window.pageXOffset - d.clientLeft;
_this.screen.top = box.top + window.pageYOffset - d.clientTop;
_this.screen.width = box.width;
_this.screen.height = box.height;
}
} );
this.resetInputAfterCameraMovement = ( function () {
if( _isPointerDown ) {
// Need to update camera.matrixWorldInverse if camera is moved
// and renderer has not updated matrixWorldInverse yet.
_this.camera.updateWorldMatrix(true, false);
_this.camera.matrixWorldInverse.copy( _this.camera.matrixWorld ).invert();
_pointOnSphere.copy( getPointerInSphere( getPointerInNdc( _pointerScreen.x, _pointerScreen.y ) ) );
}
} );
var getPointerInNdc = ( function () {
var vector = new THREE.Vector2();
return function getPointerInNdc( pageX, pageY ) {
vector.set(
( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ),
( _this.screen.height + 2 * ( _this.screen.top - pageY ) ) / _this.screen.height
);
return vector;
};
}() );
this.getPointerInNdc = getPointerInNdc; // Handy for CameraSpinControls
// Find vector from object to pointer in screen space
var getObjectToPointer = ( function () {
var objPos = new THREE.Vector3(),
objEdgePos = new THREE.Vector3(),
offset = new THREE.Vector3(),
objToPointer = new THREE.Vector2(),
cameraRot = new THREE.Quaternion();
return function getObjectToPointer( pointerNdcScreen ) {
_this.object.updateWorldMatrix( true, false );
objPos.setFromMatrixPosition( _this.object.matrixWorld );
_this.camera.updateWorldMatrix( true, false );
// Need to update camera.matrixWorldInverse if camera moved before renderer.render
_this.camera.matrixWorldInverse.copy( _this.camera.matrixWorld ).invert();
objPos.project( _this.camera ); // position in ndc/screen
objToPointer.set( objPos.x, objPos.y );
objToPointer.subVectors( pointerNdcScreen, objToPointer );
// Normalize objToPointer by object screen size
// so objToPointer of length 1 is 1 object radius distance from object center.
objEdgePos.setFromMatrixPosition( _this.object.matrixWorld ); // objEdgePos is still aspirational on this line
offset.set( _this.trackballRadius, 0, 0 );
offset.applyQuaternion( cameraRot.setFromRotationMatrix( _this.camera.matrixWorld ) );
objEdgePos.add( offset );
objEdgePos.project( _this.camera ); // position in ndc/screen
objEdgePos.z = 0;
objPos.z = 0;
var objRadiusNDC = objEdgePos.distanceTo( objPos );
objToPointer.x /= objRadiusNDC;
objToPointer.y /= objRadiusNDC;
if ( _this.camera.aspect ) { // Perspective camera probably
objToPointer.y /= _this.camera.aspect;
}
return objToPointer;
}
}() );
// Finds point on sphere in world coordinate space
var getPointerInSphere = ( function () {
var point = new THREE.Vector3(),
objPos = new THREE.Vector3(),
objToPointer = new THREE.Vector2(),
cameraRot = new THREE.Quaternion(),
trackBallSphere = new THREE.Sphere(),
ray = new THREE.Ray();
return function getPointerInSphere( ndc ) {
objToPointer.copy( getObjectToPointer( ndc ) );
cameraRot.setFromRotationMatrix( _this.camera.matrixWorld );
if ( _this._pointerMapping === _this.POINTER_SPHERE_MAPPING.RAYCAST ) {
if ( objToPointer.lengthSq() < 1 ) {
objPos.setFromMatrixPosition( _this.object.matrixWorld );
trackBallSphere.set( objPos, _this.trackballRadius );
ray.origin.copy( _this.camera.position );
ray.direction.set( ndc.x, ndc.y, .5 );
ray.direction.unproject( _this.camera ); // In world space
ray.direction.sub( _this.camera.position ).normalize(); // Subtract to put around origin
ray.intersectSphere( trackBallSphere, point );
point.sub( objPos );
point.normalize(); // updateAngularVelocity expects unit vectors
} else {
// Shoemake project on edge of sphere
objToPointer.normalize();
point.set( objToPointer.x, objToPointer.y, 0.0 );
point.applyQuaternion( cameraRot );
}
}
// Pointer mapping code below derived from Yasuhiro Fujii's https://mimosa-pudica.net/3d-rotation/
else if ( _this._pointerMapping === _this.POINTER_SPHERE_MAPPING.HOLROYD ) {
var t = objToPointer.lengthSq();
if (t < 0.5) {
point.set( objToPointer.x, objToPointer.y, Math.sqrt( 1.0 - t ) );
} else {
point.set( objToPointer.x, objToPointer.y, 1.0 / ( 2.0 * Math.sqrt( t ) ) );
point.normalize();
}
point.applyQuaternion( cameraRot ); // Rotate from looking down z axis to camera direction
} else if ( _this._pointerMapping === _this.POINTER_SPHERE_MAPPING.SHOEMAKE ) {
var t = objToPointer.lengthSq();
if (t < 1.0) {
point.set( objToPointer.x, objToPointer.y, Math.sqrt( 1.0 - t ) );
} else {
objToPointer.normalize();
point.set( objToPointer.x, objToPointer.y, 0.0 );
}
point.applyQuaternion( cameraRot );
} else if ( _this._pointerMapping === _this.POINTER_SPHERE_MAPPING.AZIMUTHAL ) {
var t = ( Math.PI / 2.0 ) * objToPointer.length();
var sined = t < Number.EPSILON ? 1.0 : Math.sin( t ) / t;
objToPointer.multiplyScalar( ( Math.PI / 2.0 ) * sined );
point.set( objToPointer.x, objToPointer.y, Math.cos( t ) );
point.applyQuaternion( cameraRot );
}
return point;
};
}() );
this.onPointerDown = function( pointerScreenX, pointerScreenY, time ) {
var pointerNdc = getPointerInNdc( pointerScreenX, pointerScreenY );
var objToPointer = getObjectToPointer( pointerNdc );
if ( objToPointer.lengthSq() < 1 ) {
_wasLastPointerEventOnSphere = true;
_pointOnSphere.copy( getPointerInSphere( pointerNdc ) );
} else {
_wasLastPointerEventOnSphere = false;
}
_pointerScreen.set( pointerScreenX, pointerScreenY );
_lastPointerEventTime = time;
_angularVelocity.set( 0, 0, 0 );
_isPointerDown = true;
}
// Finds point on sphere in world coordinate space
this.onPointerMove = ( function () {
var pointerNdc = new THREE.Vector3(),
objToPointer = new THREE.Vector2();
// for relative movement off sphere
var deltaMouse = new THREE.Vector2(),
lastNdc = new THREE.Vector2(),
objectPos = new THREE.Vector3(),
objectToCamera = new THREE.Vector3(),
polarVel = new THREE.Vector3(),
lastPointOnSphere = new THREE.Vector3();
return function onPointerMove( pointerScreenX, pointerScreenY, time ) {
var deltaTime = ( time - _lastPointerEventTime ) / 1000.0;
_lastPointerEventTime = time;
_pointOnSphereOld.copy( _pointOnSphere );
pointerNdc.copy( getPointerInNdc( pointerScreenX, pointerScreenY ) );
objToPointer.copy( getObjectToPointer( pointerNdc ) );
if ( objToPointer.lengthSq() < 1 || !this.relativelySpinOffTrackball ) {
// Pointer is within radius of trackball circle on the screen
// or relative rotation off trackball disabled
_pointOnSphere.copy( getPointerInSphere( pointerNdc ) );
if ( _wasLastPointerEventOnSphere ) {
// Still on sphere
if( deltaTime > 0 ) { // Sometimes zero due to timer precision?
_this.updateAngularVelocity( _pointOnSphere, _pointOnSphereOld, deltaTime );
}
}
else {
// Moved onto sphere
_angularVelocity.set( 0, 0, 0 );
_lastVelTime = time;
}
_wasLastPointerEventOnSphere = true;
} else {
// Pointer off trackball
if ( _wasLastPointerEventOnSphere ) {
// Just moved off trackball
_angularVelocity.set( 0, 0, 0 );
_lastVelTime = time;
}
else {
// Pointer still off trackball this frame
if( deltaTime > 0 ) { // Sometimes zero due to timer precision?
// Relatively spin towards pointer from trackball center by change in distance amount
// Simplify by finding pointer's delta polar coordinates with THREE.Sphere?
lastNdc.copy( getPointerInNdc( _pointerScreen.x, _pointerScreen.y ) );
deltaMouse.subVectors(pointerNdc, lastNdc);
// Find change in pointer radius to trackball center
objectPos.setFromMatrixPosition( _this.object.matrixWorld );
if ( _this.camera.isPerspectiveCamera ) {
objectToCamera.copy( _this.camera.position ).sub( objectPos );
} else { // Assuming orthographic
_this.camera.getWorldDirection( objectToCamera );
objectToCamera.negate();
}
_pointOnSphere.copy( getPointerInSphere( pointerNdc ) );
// Radius angular velocity direction
_angularVelocity.crossVectors( objectToCamera, _pointOnSphere );
// Find radius change over time
var ndcToBall;
if ( _this.camera.isPerspectiveCamera ) {
ndcToBall = ( 2 / _this.camera.fov ) // NDC per field of view degree
/ Math.atan( _this.trackballRadius / objectToCamera.length() ); // Ball field of view angle size
} else { //Assume orthographic
ndcToBall = _this.trackballRadius / ( ( _this.camera.top - _this.camera.bottom ) / _this.camera.zoom * 2 );
}
objToPointer.normalize();
var deltaRadius = deltaMouse.dot( objToPointer ) * ndcToBall / deltaTime;
_angularVelocity.setLength( deltaRadius * _this._offTrackBallVelocityGain ); // Just set it because we are touching trackball without sliding
// Find polar angle change
lastPointOnSphere.copy( getPointerInSphere( lastNdc ) );
var angle = lastPointOnSphere.angleTo( _pointOnSphere ) / deltaTime;
polarVel.crossVectors( lastPointOnSphere, _pointOnSphere );
polarVel.setLength( angle );
_angularVelocity.add( polarVel );
_this.applyVelocity();
}
}
_wasLastPointerEventOnSphere = false;
}
_pointerScreen.set( pointerScreenX, pointerScreenY );
_this.hasPointerMovedThisFrame = true;
}
}() );
// call like this: spinControl.setPointerToSphereMapping(spinControl.POINTER_SPHERE_MAPPING.SHOEMAKE)
this.setPointerToSphereMapping = function ( mappingTechnique ) {
_this._pointerMapping = mappingTechnique;
_this._offTrackBallVelocityGain = _this.offTrackBallVelocityGainMap[_this._pointerMapping];
}
// listeners
this.handlePointerDown = function ( event ) {
event.preventDefault(); // Prevent the browser from scrolling.
event.stopImmediatePropagation(); // Stop other controls working.
// Manually set the focus since calling preventDefault above
// prevents the browser from setting it automatically.
_this.domElement.focus ? _this.domElement.focus() : window.focus();
_this.dispatchEvent( startEvent );
}
this.handlePointerUp = function ( event ) {
event.preventDefault();
if( !_this.hasPointerMovedThisFrame ) {
// To support subtle touches do big dampening, not just zeroing velocity
var deltaTime = ( event.timeStamp - _lastPointerEventTime ) / 1000.0;
_angularVelocity.multiplyScalar( 1 / ( _this._pointerUpVelDamping * Math.pow(deltaTime, 2) + _this.dampingFactor * deltaTime + 1) );
}
_isPointerDown = false;
_this.dispatchEvent( endEvent );
}
function onMouseDown( event ) {
if ( _this.enabled === false || event.button !== 0 ) return;
_this.onPointerDown( event.pageX, event.pageY, event.timeStamp );
document.addEventListener( 'mousemove', onMouseMove, false );
document.addEventListener( 'mouseup', onMouseUp, false );
_this.handlePointerDown( event );
}
function onMouseMove( event ) {
if ( _this.enabled === false ) return;
event.preventDefault();
_this.onPointerMove( event.pageX, event.pageY, event.timeStamp );
}
function onMouseUp( event ) {
if ( _this.enabled === false ) return;
document.removeEventListener( 'mousemove', onMouseMove );
document.removeEventListener( 'mouseup', onMouseUp );
_this.handlePointerUp( event );
}
// For camera controls to stop spin with 2 finger pinch
this.cancelSpin = ( function () {
_angularVelocity.set( 0, 0, 0 );
} );
// Function broken out for CameraSpinControls to use in touch end if going from 2 fingers to 1
this.handleTouchStart = function( event ) {
_this.onPointerDown( event.pageX, event.pageY, event.timeStamp );
_this.applyVelocity(); //TODO Should not be needed here
}
function onTouchStart( event ) {
if ( _this.enabled === false ) return;
_this.handleTouchStart( event );
_this.handlePointerDown( event );
}
function onTouchMove( event ) {
if ( _this.enabled === false || !_isPointerDown ) return;
event.preventDefault();
event.stopImmediatePropagation(); // Prevent other controls from working.
_this.onPointerMove( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, event.timeStamp );
}
function onTouchEnd( event ) {
if( _this.enabled === false ) return;
_this.handlePointerUp( event );
// override handlePointerUp if finger still down
if( event.touches.length > 0 ) {
_isPointerDown = true;
}
}
this.dispose = function () {
_this.domElement.removeEventListener( 'mousedown', onMouseDown );
document.removeEventListener( 'mousemove', onMouseMove );
document.removeEventListener( 'mouseup', onMouseUp );
_this.domElement.removeEventListener( 'touchstart', onTouchStart );
_this.domElement.removeEventListener( 'touchmove', onTouchMove );
_this.domElement.removeEventListener( 'touchend', onTouchEnd );
};
_this.domElement.addEventListener( 'mousedown', onMouseDown );
_this.domElement.addEventListener( 'touchstart', onTouchStart, {passive: false} );
_this.domElement.addEventListener( 'touchmove', onTouchMove, {passive: false} );
_this.domElement.addEventListener( 'touchend', onTouchEnd, {passive: false} );
_this.onWindowResize();
// force an update at start
_this.update();
};
SpinControls.prototype = Object.create( THREE.EventDispatcher.prototype );
SpinControls.prototype.constructor = SpinControls;