Просмотр исходного кода

Ray: Handle empty spheres in intersectSphere(). (#34016)

sean-kim05 1 неделя назад
Родитель
Сommit
9fc2604e2a
2 измененных файлов с 9 добавлено и 0 удалено
  1. 2 0
      src/math/Ray.js
  2. 7 0
      test/unit/src/math/Ray.tests.js

+ 2 - 0
src/math/Ray.js

@@ -306,6 +306,8 @@ class Ray {
 	 */
 	intersectSphere( sphere, target ) {
 
+		if ( sphere.radius < 0 ) return null; // handle empty spheres, see #31187
+
 		_vector.subVectors( sphere.center, this.origin );
 		const tca = _vector.dot( this.direction );
 		const d2 = _vector.dot( _vector ) - tca * tca;

+ 7 - 0
test/unit/src/math/Ray.tests.js

@@ -260,6 +260,13 @@ export default QUnit.module( 'Maths', () => {
 			a0.intersectSphere( b, point );
 			assert.ok( point.distanceTo( new Vector3( 0, 0, - 5 ) ) < TOL, 'Passed!' );
 
+			// empty sphere ( negative radius ) can never be intersected, so the
+			// target must be left untouched and the return value must be null,
+			// consistent with intersectsSphere()
+			b = new Sphere( new Vector3( 0, 0, - 1 ), - 1 );
+			assert.strictEqual( a0.intersectSphere( b, point.copy( posInf3 ) ), null, 'Passed!' );
+			assert.ok( point.equals( posInf3 ), 'Passed!' );
+
 		} );
 
 		QUnit.test( 'intersectsSphere', ( assert ) => {

粤ICP备19079148号