浏览代码

Added intersectsBox method to Frustum

jotinha 12 年之前
父节点
当前提交
2665e62aae
共有 1 个文件被更改,包括 38 次插入0 次删除
  1. 38 0
      src/math/Frustum.js

+ 38 - 0
src/math/Frustum.js

@@ -131,6 +131,44 @@ THREE.Frustum.prototype = {
 
 	},
 
+	intersectsBox = function() {
+
+		var p1 = new THREE.Vector3(),
+			p2 = new THREE.Vector3();
+
+		return function( box ) {
+
+			var planes = this.planes;
+			
+			for ( var i = 0; i < 6 ; i ++ ) {
+			
+				var plane = planes[i];
+				
+				p1.x = plane.normal.x > 0 ? box.min.x : box.max.x;
+				p2.x = plane.normal.x > 0 ? box.max.x : box.min.x;
+				p1.y = plane.normal.y > 0 ? box.min.y : box.max.y;
+				p2.y = plane.normal.y > 0 ? box.max.y : box.min.y;
+				p1.z = plane.normal.z > 0 ? box.min.z : box.max.z;
+				p2.z = plane.normal.z > 0 ? box.max.z : box.min.z;
+
+				var d1 = plane.distanceToPoint( p1 );
+				var d2 = plane.distanceToPoint( p2 );
+				
+				//if both outside plane, no intersection
+
+				if ( d1 < 0 && d2 < 0) {		
+					
+					return false;
+		
+				}
+			}
+
+			return true;
+		};
+
+	}();
+
+
 	containsPoint: function ( point ) {
 
 		var planes = this.planes;