提交 af4b73c0 编写于 作者: B Ben Houston

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

上级 427a7b3e
......@@ -72,12 +72,8 @@ THREE.Frustum.prototype = {
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 );
......@@ -86,12 +82,14 @@ THREE.Frustum.prototype = {
containsSphere: function ( sphere ) {
var planes = this.planes;
var center = sphere.center;
var negRadius = -sphere.radius;
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;
......
......@@ -106,6 +106,32 @@ test( "setFromMatrix/makeFrustum/containsPoint", function() {
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() {
var p0 = new THREE.Plane( unit3, -1 );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册