Selaa lähdekoodia

Merge pull request #7438 from Mugen87/dev

Add plane/box and plane/sphere intersection test
Mr.doob 9 vuotta sitten
vanhempi
commit
a627687fe1
3 muutettua tiedostoa jossa 73 lisäystä ja 0 poistoa
  1. 47 0
      src/math/Box3.js
  2. 12 0
      src/math/Plane.js
  3. 14 0
      src/math/Sphere.js

+ 47 - 0
src/math/Box3.js

@@ -281,6 +281,53 @@ THREE.Box3.prototype = {
 
 	},
 
+	intersectsPlane: function ( plane ) {
+
+		// We compute the minimum and maximum dot product values. If those values
+		// are on the same side (back or front) of the plane, then there is no intersection.
+
+		var min, max;
+
+		if( plane.normal.x > 0 ) {
+
+			min = plane.normal.x * this.min.x;
+			max = plane.normal.x * this.max.x;
+
+		} else {
+
+			min = plane.normal.x * this.max.x;
+			max = plane.normal.x * this.min.x;
+
+		}
+
+		if( plane.normal.y > 0 ) {
+
+			min += plane.normal.y * this.min.y;
+			max += plane.normal.y * this.max.y;
+
+		} else {
+
+			min += plane.normal.y * this.max.y;
+			max += plane.normal.y * this.min.y;
+
+		}
+
+		if( plane.normal.z > 0 ) {
+
+			min += plane.normal.z * this.min.z;
+			max += plane.normal.z * this.max.z;
+
+		} else {
+
+			min += plane.normal.z * this.max.z;
+			max += plane.normal.z * this.min.z;
+
+		}
+
+		return ( min <= plane.constant && max >= plane.constant );
+
+	},
+
 	clampPoint: function ( point, optionalTarget ) {
 
 		var result = optionalTarget || new THREE.Vector3();

+ 12 - 0
src/math/Plane.js

@@ -181,6 +181,18 @@ THREE.Plane.prototype = {
 
 	},
 
+	intersectsBox: function ( box ) {
+
+		return box.intersectsPlane( this );
+
+	},
+
+	intersectsSphere: function ( sphere ) {
+
+		return sphere.intersectsPlane( this );
+
+	},
+
 	coplanarPoint: function ( optionalTarget ) {
 
 		var result = optionalTarget || new THREE.Vector3();

+ 14 - 0
src/math/Sphere.js

@@ -104,6 +104,20 @@ THREE.Sphere.prototype = {
 
 	},
 
+	intersectsPlane: function ( plane ) {
+
+		// We use the following equation to compute the signed distance from
+		// the center of the sphere to the plane.
+		//
+		// distance = q * n - d
+		//
+		// If this distance is greater than the radius of the sphere,
+		// then there is no intersection.
+
+		return ( ( this.center.dot( plane.normal ) - plane.constant ) <= this.radius );
+
+	},
+
 	clampPoint: function ( point, optionalTarget ) {
 
 		var deltaLengthSq = this.center.distanceToSquared( point );