From 344fcab08071cbd3257345e109af44586ef70869 Mon Sep 17 00:00:00 2001
From: Garrett Johnson <garrett.kjohnson@gmail.com>
Date: Wed, 17 Jan 2024 08:22:19 +0900
Subject: [PATCH] Field rename, fix rotation height snapping (#451)

* Update zoom member names

* Fix error

* Fix rotation offset snapping

* formatting
---
 example/src/controls/TileControls.js | 38 ++++++++++++++++------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/example/src/controls/TileControls.js b/example/src/controls/TileControls.js
index 649246301..3fcb37779 100644
--- a/example/src/controls/TileControls.js
+++ b/example/src/controls/TileControls.js
@@ -47,17 +47,17 @@ export class TileControls {
 		this.rotationSpeed = 5;
 		this.minAltitude = 0;
 		this.maxAltitude = 0.45 * Math.PI;
-		this.minDistance = 10;
-		this.maxDistance = Infinity;
+		this.minZoomDistance = 10;
+		this.maxZoomDistance = Infinity;
 		this.reorientOnDrag = true;
 		this.reorientOnZoom = false;
 
 		// internal state
 		this.pointerTracker = new PointerTracker();
+		this.actionHeightOffset = 0;
 
 		this.dragPointSet = false;
 		this.dragPoint = new Vector3();
-		this.startDragPoint = new Vector3();
 
 		this.rotationPointSet = false;
 		this.rotationPoint = new Vector3();
@@ -102,7 +102,7 @@ export class TileControls {
 
 		if ( this.domElement ) {
 
-			throw new Error( 'GlobeControls: Controls already attached to element' );
+			throw new Error( 'TileControls: Controls already attached to element' );
 
 		}
 
@@ -207,7 +207,6 @@ export class TileControls {
 
 						this.state = DRAG;
 						this.dragPoint.copy( hit.point );
-						this.startDragPoint.copy( hit.point );
 						this.dragPointSet = true;
 
 						this.pivotMesh.position.copy( hit.point );
@@ -413,6 +412,7 @@ export class TileControls {
 		this.rotationPointSet = false;
 		this.scene.remove( this.pivotMesh );
 		this.pivotMesh.visible = true;
+		this.actionHeightOffset = 0;
 
 	}
 
@@ -422,7 +422,6 @@ export class TileControls {
 			camera,
 			cameraRadius,
 			dragPoint,
-			startDragPoint,
 			up,
 		} = this;
 
@@ -447,17 +446,23 @@ export class TileControls {
 		// when dragging the camera and drag point may be moved
 		// to accommodate terrain so we try to move it back down
 		// to the original point.
-		if ( this.state === DRAG ) {
+		if ( ( this.state === DRAG || this.state === ROTATE ) && this.actionHeightOffset !== 0 ) {
 
-			_delta.subVectors( startDragPoint, dragPoint );
-			camera.position.add( _delta );
-			dragPoint.copy( startDragPoint );
+			const { actionHeightOffset } = this;
+			camera.position.addScaledVector( up, - actionHeightOffset );
+			dragPoint.addScaledVector( up, - actionHeightOffset );
 
 			// adjust the height
-			hit.distance -= _delta.length();
+			if ( hit ) {
+
+				hit.distance -= actionHeightOffset;
+
+			}
 
 		}
 
+		this.actionHeightOffset = 0;
+
 		if ( hit ) {
 
 			const dist = hit.distance;
@@ -466,6 +471,7 @@ export class TileControls {
 				const delta = cameraRadius - dist;
 				camera.position.addScaledVector( up, delta );
 				dragPoint.addScaledVector( up, delta );
+				this.actionHeightOffset = delta;
 
 			}
 
@@ -486,8 +492,8 @@ export class TileControls {
 			zoomPoint,
 			zoomDirection,
 			camera,
-			minDistance,
-			maxDistance,
+			minZoomDistance,
+			maxZoomDistance,
 			raycaster,
 			pointerTracker,
 			domElement,
@@ -518,14 +524,14 @@ export class TileControls {
 			// scale the distance based on how far there is to move
 			if ( scale < 0 ) {
 
-				const remainingDistance = Math.min( 0, dist - maxDistance );
+				const remainingDistance = Math.min( 0, dist - maxZoomDistance );
 				scale = scale * ( dist - 0 ) * 0.01;
 				scale = Math.max( scale, remainingDistance );
 
 			} else {
 
-				const remainingDistance = Math.max( 0, dist - minDistance );
-				scale = scale * ( dist - minDistance ) * 0.01;
+				const remainingDistance = Math.max( 0, dist - minZoomDistance );
+				scale = scale * ( dist - minZoomDistance ) * 0.01;
 				scale = Math.min( scale, remainingDistance );
 
 			}