From 74d7ac4568006ccbcac165bbd0f82d84592ab83e Mon Sep 17 00:00:00 2001 From: Nicolas Daures Date: Fri, 4 Aug 2023 06:59:52 +0200 Subject: [PATCH] Add zoom sensitivity and pan sensitivity attributes (#4412) --- packages/model-viewer/src/features/controls.ts | 16 ++++++++++++++++ .../src/three-components/SmoothControls.ts | 12 +++++++----- packages/modelviewer.dev/data/docs.json | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/packages/model-viewer/src/features/controls.ts b/packages/model-viewer/src/features/controls.ts index e5be99cfaf..e41daf21f7 100644 --- a/packages/model-viewer/src/features/controls.ts +++ b/packages/model-viewer/src/features/controls.ts @@ -241,6 +241,8 @@ export declare interface ControlsInterface { interactionPromptStyle: InteractionPromptStyle; interactionPromptThreshold: number; orbitSensitivity: number; + zoomSensitivity: number; + panSensitivity: number; touchAction: TouchAction; interpolationDecay: number; disableZoom: boolean; @@ -336,6 +338,12 @@ export const ControlsMixin = >( @property({type: Number, attribute: 'orbit-sensitivity'}) orbitSensitivity: number = 1; + @property({type: Number, attribute: 'zoom-sensitivity'}) + zoomSensitivity: number = 1; + + @property({type: Number, attribute: 'pan-sensitivity'}) + panSensitivity: number = 1; + @property({type: String, attribute: 'touch-action'}) touchAction: TouchAction = TouchAction.NONE; @@ -523,6 +531,14 @@ export const ControlsMixin = >( controls.orbitSensitivity = this.orbitSensitivity; } + if (changedProperties.has('zoomSensitivity')) { + controls.zoomSensitivity = this.zoomSensitivity; + } + + if (changedProperties.has('panSensitivity')) { + controls.panSensitivity = this.panSensitivity; + } + if (changedProperties.has('interpolationDecay')) { controls.setDamperDecayTime(this.interpolationDecay); scene.setTargetDamperDecayTime(this.interpolationDecay); diff --git a/packages/model-viewer/src/three-components/SmoothControls.ts b/packages/model-viewer/src/three-components/SmoothControls.ts index b15d75611c..8d942a4615 100644 --- a/packages/model-viewer/src/three-components/SmoothControls.ts +++ b/packages/model-viewer/src/three-components/SmoothControls.ts @@ -127,6 +127,8 @@ export interface PointerChangeEvent extends ThreeEvent { */ export class SmoothControls extends EventDispatcher { public orbitSensitivity = 1; + public zoomSensitivity = 1; + public panSensitivity = 1; public inputSensitivity = 1; public changeSource = ChangeSource.NONE; @@ -528,7 +530,7 @@ export class SmoothControls extends EventDispatcher { if (!this._disableZoom) { const touchDistance = this.twoTouchDistance(this.pointers[0], this.pointers[1]); - const deltaZoom = ZOOM_SENSITIVITY * + const deltaZoom = ZOOM_SENSITIVITY * this.zoomSensitivity * (this.lastSeparation - touchDistance) * 50 / this.scene.height; this.lastSeparation = touchDistance; @@ -584,7 +586,7 @@ export class SmoothControls extends EventDispatcher { private initializePan() { const {theta, phi} = this.spherical; const psi = theta - this.scene.yaw; - this.panPerPixel = PAN_SENSITIVITY / this.scene.height; + this.panPerPixel = PAN_SENSITIVITY * this.panSensitivity / this.scene.height; this.panProjection.set( -Math.cos(psi), -Math.cos(phi) * Math.sin(psi), @@ -819,7 +821,7 @@ export class SmoothControls extends EventDispatcher { this.changeSource = ChangeSource.USER_INTERACTION; const deltaZoom = (event as WheelEvent).deltaY * - ((event as WheelEvent).deltaMode == 1 ? 18 : 1) * ZOOM_SENSITIVITY / 30; + ((event as WheelEvent).deltaMode == 1 ? 18 : 1) * ZOOM_SENSITIVITY * this.zoomSensitivity / 30; this.userAdjustOrbit(0, 0, deltaZoom); event.preventDefault(); @@ -855,10 +857,10 @@ export class SmoothControls extends EventDispatcher { let relevantKey = true; switch (event.key) { case 'PageUp': - this.userAdjustOrbit(0, 0, ZOOM_SENSITIVITY); + this.userAdjustOrbit(0, 0, ZOOM_SENSITIVITY * this.zoomSensitivity); break; case 'PageDown': - this.userAdjustOrbit(0, 0, -1 * ZOOM_SENSITIVITY); + this.userAdjustOrbit(0, 0, -1 * ZOOM_SENSITIVITY * this.zoomSensitivity); break; case 'ArrowUp': this.userAdjustOrbit(0, -KEYBOARD_ORBIT_INCREMENT, 0); diff --git a/packages/modelviewer.dev/data/docs.json b/packages/modelviewer.dev/data/docs.json index 66db660340..5f94c8ba99 100644 --- a/packages/modelviewer.dev/data/docs.json +++ b/packages/modelviewer.dev/data/docs.json @@ -494,6 +494,24 @@ "options": "any number" } }, + { + "name": "zoom-sensitivity", + "htmlName": "zoomSensitivity", + "description": "Adjusts the speed of zoom interactions.", + "default": { + "default": "1", + "options": "any number" + } + }, + { + "name": "pan-sensitivity", + "htmlName": "panSensitivity", + "description": "Adjusts the speed of pan interactions.", + "default": { + "default": "1", + "options": "any number" + } + }, { "name": "auto-rotate", "htmlName": "autoRotate",