瀏覽代碼

Add 3D cursor and limit to orbit camera target (#26558)

* Add a limit to the orbit controls target centered around a 3D cursor

* add docs and update naming to reflect the use better

* improve legibility of docs

* Update OrbitControls.js

Code style clean up.

* Update OrbitControls.js

More clean up.

* Update OrbitControls.js

Move clamping further up.

---------

Co-authored-by: Tom Schwarz <[email protected]>
Co-authored-by: Michael Herzog <[email protected]>
schwyzl 1 年之前
父節點
當前提交
2563342997
共有 2 個文件被更改,包括 27 次插入1 次删除
  1. 15 0
      docs/examples/en/controls/OrbitControls.html
  2. 12 1
      examples/jsm/controls/OrbitControls.js

+ 15 - 0
docs/examples/en/controls/OrbitControls.html

@@ -181,6 +181,16 @@ controls.keys = {
 			How far you can zoom out ( [page:OrthographicCamera] only ). Default is Infinity.
 		</p>
 
+		<h3>[property:Float minTargetRadius]</h3>
+		<p>
+			How close you can get the target to the 3D [page:.cursor]. Default is 0.
+		</p>
+
+		<h3>[property:Float maxTargetRadius]</h3>
+		<p>
+			How far you can move the target from the 3D [page:.cursor]. Default is Infinity.
+		</p>
+
 		<h3>[property:Float minAzimuthAngle]</h3>
 		<p>
 			How far you can orbit horizontally, lower limit. If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI ). Default is Infinity.
@@ -252,6 +262,11 @@ controls.mouseButtons = {
 			the focus of the controls.
 		</p>
 
+		<h3>[property:Vector3 cursor]</h3>
+		<p>
+			The focus point of the [page:.minTargetRadius] and [page:.maxTargetRadius] limits. It can be updated manually at any point to change the center of interest for the [page:.target].
+		</p>
+
 		<h3>[property:Object touches]</h3>
 		<p>
 			This object contains references to the touch actions used by the controls.

+ 12 - 1
examples/jsm/controls/OrbitControls.js

@@ -41,6 +41,9 @@ class OrbitControls extends EventDispatcher {
 		// "target" sets the location of focus, where the object orbits around
 		this.target = new Vector3();
 
+		// Sets the 3D cursor (similar to Blender), from which the maxTargetRadius takes effect
+		this.cursor = new Vector3();
+
 		// How far you can dolly in and out ( PerspectiveCamera only )
 		this.minDistance = 0;
 		this.maxDistance = Infinity;
@@ -49,6 +52,10 @@ class OrbitControls extends EventDispatcher {
 		this.minZoom = 0;
 		this.maxZoom = Infinity;
 
+		// Limit camera target within a spherical area around the cursor
+		this.minTargetRadius = 0;
+		this.maxTargetRadius = Infinity;
+
 		// How far you can orbit vertically, upper and lower limits.
 		// Range is 0 to Math.PI radians.
 		this.minPolarAngle = 0; // radians
@@ -249,6 +256,11 @@ class OrbitControls extends EventDispatcher {
 
 				}
 
+				// Limit the target distance from the cursor to create a sphere around the center of interest
+				scope.target.sub( scope.cursor );
+				scope.target.clampLength( scope.minTargetRadius, scope.maxTargetRadius );
+				scope.target.add( scope.cursor );
+
 				// adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
 				// we adjust zoom later in these cases
 				if ( scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera ) {
@@ -261,7 +273,6 @@ class OrbitControls extends EventDispatcher {
 
 				}
 
-
 				offset.setFromSpherical( spherical );
 
 				// rotate offset back to "camera-up-vector-is-up" space