Browse Source

SelectionBox: correctly apply frustum fix

The fix is relevant for both orthographic and perspective camera.

Also, a case where only one coordinate is identical (i.e. the
selection is a line) also causes invalid frustum. This is less
important if you're testing for a single point, because it won't
be inside anyway, but it breaks as soon as you test for
intersection of the box with a bigger object (bounding sphere
or bounding box).
Paweł Marczewski 5 years ago
parent
commit
3e1ddb2f91
2 changed files with 26 additions and 4 deletions
  1. 14 2
      examples/js/interactive/SelectionBox.js
  2. 12 2
      examples/jsm/interactive/SelectionBox.js

+ 14 - 2
examples/js/interactive/SelectionBox.js

@@ -54,6 +54,20 @@ THREE.SelectionBox = ( function () {
 		startPoint = startPoint || this.startPoint;
 		endPoint = endPoint || this.endPoint;
 
+		// Avoid invalid frustum
+
+		if ( startPoint.x === endPoint.x ) {
+
+			endPoint.x += Number.EPSILON;
+
+		}
+
+		if ( startPoint.y === endPoint.y ) {
+
+			endPoint.y += Number.EPSILON;
+
+		}
+
 		this.camera.updateProjectionMatrix();
 		this.camera.updateMatrixWorld();
 
@@ -102,8 +116,6 @@ THREE.SelectionBox = ( function () {
 
 		} else if ( this.camera.isOrthographicCamera ) {
 
-			if ( startPoint.equals( endPoint ) ) endPoint.addScalar( Number.EPSILON ); // avoid invalid frustum
-
 			var left = Math.min( startPoint.x, endPoint.x );
 			var top = Math.max( startPoint.y, endPoint.y );
 			var right = Math.max( startPoint.x, endPoint.x );

+ 12 - 2
examples/jsm/interactive/SelectionBox.js

@@ -59,6 +59,18 @@ var SelectionBox = ( function () {
 		startPoint = startPoint || this.startPoint;
 		endPoint = endPoint || this.endPoint;
 
+		if ( startPoint.x === endPoint.x ) {
+
+			endPoint.x += Number.EPSILON;
+
+		}
+
+		if ( startPoint.y === endPoint.y ) {
+
+			endPoint.y += Number.EPSILON;
+
+		}
+
 		this.camera.updateProjectionMatrix();
 		this.camera.updateMatrixWorld();
 
@@ -107,8 +119,6 @@ var SelectionBox = ( function () {
 
 		} else if ( this.camera.isOrthographicCamera ) {
 
-			if ( startPoint.equals( endPoint ) ) endPoint.addScalar( Number.EPSILON ); // avoid invalid frustum
-
 			var left = Math.min( startPoint.x, endPoint.x );
 			var top = Math.max( startPoint.y, endPoint.y );
 			var right = Math.max( startPoint.x, endPoint.x );