Browse Source

fix sign/comparison in Frustum.containsSphere and add unit tests.

Ben Houston 12 years ago
parent
commit
af4b73c0fc
2 changed files with 32 additions and 8 deletions
  1. 6 8
      src/math/Frustum.js
  2. 26 0
      test/unit/math/Frustum.js

+ 6 - 8
src/math/Frustum.js

@@ -72,12 +72,8 @@ THREE.Frustum.prototype = {
 
 
 	contains: function ( object ) {
 	contains: function ( object ) {
 
 
-		var matrix = object.matrixWorld;
-
-		var sphere = THREE.Frustum.__s0.set(
-			matrix.getPosition(), 
-			- object.geometry.boundingSphere.radius * matrix.getMaxScaleOnAxis()
-			);
+		var sphere = THREE.Frustum.__s0.copy( object.geometry.boundingSphere );
+		sphere.transform( object.matrixWorld );
 
 
 		return this.containsSphere( sphere );
 		return this.containsSphere( sphere );
 
 
@@ -86,12 +82,14 @@ THREE.Frustum.prototype = {
 	containsSphere: function ( sphere ) {
 	containsSphere: function ( sphere ) {
 		
 		
 		var planes = this.planes;
 		var planes = this.planes;
+		var center = sphere.center;
+		var negRadius = -sphere.radius;
 		
 		
 		for ( var i = 0; i < 6; i ++ ) {
 		for ( var i = 0; i < 6; i ++ ) {
 
 
-			var distance = planes[ i ].distanceToPoint( sphere.center );
+			var distance = planes[ i ].distanceToPoint( center );
 
 
-			if( distance <= sphere.radius ) {
+			if( distance < negRadius ) {
 
 
 				return false;
 				return false;
 
 

+ 26 - 0
test/unit/math/Frustum.js

@@ -106,6 +106,32 @@ test( "setFromMatrix/makeFrustum/containsPoint", function() {
 	ok( ! a.containsPoint( new THREE.Vector3( 0, 0, -101 ) ), "Passed!" );
 	ok( ! a.containsPoint( new THREE.Vector3( 0, 0, -101 ) ), "Passed!" );
 });
 });
 
 
+test( "setFromMatrix/makeFrustum/containsSphere", function() {
+	var m = new THREE.Matrix4().makeFrustum( -1, 1, -1, 1, 1, 100 )
+	var a = new THREE.Frustum().setFromMatrix( m );
+
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 0 ) ), "Passed!" );
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 0.9 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 1.1 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -50 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -1.001 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( -1, -1, -1.001 ), 0 ) ), "Passed!" );
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( -1.1, -1.1, -1.001 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( -1.1, -1.1, -1.001 ), 0.5 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 1, 1, -1.001 ), 0 ) ), "Passed!" );
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( 1.1, 1.1, -1.001 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 1.1, 1.1, -1.001 ), 0.5 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -99.999 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( -99.999, -99.999, -99.999 ), 0 ) ), "Passed!" );
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( -100.1, -100.1, -100.1 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( -100.1, -100.1, -100.1 ), 0.5 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 99.999, 99.999, -99.999 ), 0 ) ), "Passed!" );
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( 100.1, 100.1, -100.1 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 100.1, 100.1, -100.1 ), 0.2 ) ), "Passed!" );
+	ok( ! a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -101 ), 0 ) ), "Passed!" );
+	ok( a.containsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -101 ), 1.1 ) ), "Passed!" );
+});
+
 test( "transform", function() {
 test( "transform", function() {
 
 
 	var p0 = new THREE.Plane( unit3, -1 );
 	var p0 = new THREE.Plane( unit3, -1 );