From 83516c5cf28d5f3b5e0af7ba8921e8e676260067 Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Sat, 10 Apr 2021 13:20:06 -0400 Subject: [PATCH] Add 3 or more finger touch pan + zoom support --- CameraSpinControls.js | 112 +++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/CameraSpinControls.js b/CameraSpinControls.js index 3ccccfc..ff32441 100644 --- a/CameraSpinControls.js +++ b/CameraSpinControls.js @@ -802,45 +802,40 @@ CameraSpinControls = function ( camera, domElement ) { event.preventDefault(); - switch ( event.touches.length ) { + if ( event.touches.length === 1 ) { + // 1 finger touch: rotate + if ( scope.enableRotate === false ) return; - case 1: // one-fingered touch: rotate - - if ( scope.enableRotate === false ) return; - - state = STATE.ROTATE; - - if ( scope.startTrackballScreenCenter ) { - - scope.target.setFromMatrixPosition(scope.trackballToObject); - var startDistance = scope.target.length(); - scope.target.set(0, 0, -startDistance); - scope.target.applyQuaternion(scope.object.quaternion); - scope.target.add(scope.object.position); - scope.setTargetPosition(scope.target); - - } - - break; - - case 2: // two-fingered touch: dolly-pan + state = STATE.ROTATE; + + if ( scope.startTrackballScreenCenter ) { - if ( scope.enableZoom === false - && scope.enablePan === false - && scope.enableRotate === false) return; + scope.target.setFromMatrixPosition(scope.trackballToObject); + var startDistance = scope.target.length(); + scope.target.set(0, 0, -startDistance); + scope.target.applyQuaternion(scope.object.quaternion); + scope.target.add(scope.object.position); + scope.setTargetPosition(scope.target); - handleTouchStartDollyPanRoll( event ); + } - state = STATE.TOUCH_DOLLY_PAN; + } else if ( event.touches.length >= 2 ) { + + // 2+ finger touch: dolly-pan + + if ( scope.enableZoom === false + && scope.enablePan === false + && scope.enableRotate === false) return; - scope.spinControl.cancelSpin(); - scope.spinControl.enabled = false; + handleTouchStartDollyPanRoll( event ); - break; + state = STATE.TOUCH_DOLLY_PAN; - default: + scope.spinControl.cancelSpin(); + scope.spinControl.enabled = false; + } else { - state = STATE.NONE; + state = STATE.NONE; } @@ -859,27 +854,19 @@ CameraSpinControls = function ( camera, domElement ) { event.preventDefault(); event.stopPropagation(); - switch ( event.touches.length ) { - - // case 1: // one-fingered touch: rotate - - // if ( scope.enableRotate === false ) return; - - // break; + if ( event.touches.length >= 2) { - case 2: // two-fingered touch: dolly-pan + // dolly-pan + if ( scope.enableZoom === false && scope.enablePan === false ) return; - if ( scope.enableZoom === false && scope.enablePan === false ) return; + handleTouchMoveDollyPanRoll( event ); - handleTouchMoveDollyPanRoll( event ); - - break; - - default: + } else { - state = STATE.NONE; + state = STATE.NONE; } + // 1 finger touch events are consumed by underlying SpinControls } @@ -887,30 +874,41 @@ CameraSpinControls = function ( camera, domElement ) { if ( scope.enabled === false ) return; - if( event.touches.length === 1 ) { + if( event.touches.length === 0 ) { + + state = STATE.NONE; + endEvent.state = state; + scope.dispatchEvent( endEvent ); + + } else if( event.touches.length === 1 ) { + + if ( scope.startTrackballScreenCenter ) { + + // Set pivot point back to center if going from 2+ fingers to 1 + scope.target.setFromMatrixPosition(scope.trackballToObject); + var startDistance = scope.target.length(); + scope.target.set(0, 0, -startDistance); + scope.target.applyQuaternion(scope.object.quaternion); + scope.target.add(scope.object.position); + scope.setTargetPosition(scope.target); + + } state = STATE.ROTATE; scope.spinControl.enabled = true; scope.spinControl.handleTouchStart( event ); - } else if( event.touches.length === 0 ) { - // } else { - - state = STATE.NONE; - endEvent.state = state; - scope.dispatchEvent( endEvent ); + } else if( event.touches.length >= 2 ) { + + handleTouchStartDollyPanRoll( event ); } - } function onContextMenu( event ) { event.preventDefault(); - // if ( scope.enabled === false ) return; - - // event.preventDefault(); }