Переглянути джерело

moved ray triangle intersection to ray

jotinha 12 роки тому
батько
коміт
d403e841ca
3 змінених файлів з 72 додано та 84 видалено
  1. 8 15
      src/core/Raycaster.js
  2. 64 0
      src/math/Ray.js
  3. 0 69
      src/math/Triangle.js

+ 8 - 15
src/core/Raycaster.js

@@ -164,7 +164,7 @@
 							positions[ c * 3 + 2 ]
 						);
 
-						var interPoint = THREE.Triangle.intersectionRay( localRay, vA, vB, vC, material.side !== THREE.DoubleSide );
+						var interPoint = localRay.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
 
 						if ( !interPoint ) continue;
 
@@ -212,13 +212,10 @@
 						a = vertices[ face.a ];
 						b = vertices[ face.b ];
 						c = vertices[ face.c ];
-						interPoint = THREE.Triangle.intersectionRay( localRay, a, b, c, material.side !== THREE.DoubleSide );
+						
+						interPoint = localRay.intersectTriangle( a, b, c, material.side !== THREE.DoubleSide );
 
-						if ( !interPoint ) {
-
-							continue;
-
-						}
+						if ( !interPoint ) continue;
 
 					} else if ( face instanceof THREE.Face4 ) {
 
@@ -226,15 +223,11 @@
 						b = vertices[ face.b ];
 						c = vertices[ face.c ];
 						d = vertices[ face.d ];
-						interPoint = THREE.Triangle.intersectionRay( localRay, a, b, d, material.side !== THREE.DoubleSide );
 
-						if( !interPoint ) {
-
-						 interPoint = THREE.Triangle.intersectionRay( localRay, b, c, d, material.side !== THREE.DoubleSide  );
-
-						 if( !interPoint ) continue;
-
-						}
+						interPoint = localRay.intersectTriangle( a, b, d, material.side !== THREE.DoubleSide ) ||
+									 localRay.intersectTriangle( b, c, d, material.side !== THREE.DoubleSide );
+								 
+						if( !interPoint ) continue;
 
 					} else {
 

+ 64 - 0
src/math/Ray.js

@@ -362,6 +362,70 @@ THREE.Ray.prototype = {
 
 	},
 
+	intersectTriangle: function() {
+
+		// Compute the offset origin, edges, and normal.
+		var diff = new THREE.Vector3();
+		var edge1 = new THREE.Vector3();
+		var edge2 = new THREE.Vector3();
+		var normal = new THREE.Vector3();
+
+		return function ( a, b, c, backfaceCulling, optionalTarget ) {
+
+			//from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp
+
+			edge1.subVectors( b, a );
+			edge2.subVectors( c, a );
+			normal.crossVectors( edge1, edge2 );
+
+			// Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
+			// E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
+			//   |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
+			//   |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
+			//   |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
+			var DdN = this.direction.dot(normal);
+			var sign;
+			if ( DdN > 0 ) {
+
+					if ( backfaceCulling ) return null;
+					sign = 1;
+
+			} else if ( DdN < 0 ) {
+
+					sign = - 1;
+					DdN = - DdN;
+
+			} else return null;
+
+			diff.subVectors( this.origin, a );
+			var DdQxE2 = sign * this.direction.dot( edge2.crossVectors( diff, edge2 ) );
+
+			// b1 < 0, no intersection
+			if ( DdQxE2 < 0 )
+				return null;
+
+			var DdE1xQ = sign * this.direction.dot( edge1.cross( diff ) );
+			// b2 < 0, no intersection
+			if ( DdE1xQ < 0 )
+				return null;
+
+			// b1+b2 > 1, no intersection
+			if ( DdQxE2 + DdE1xQ > DdN )
+				return null
+
+			// Line intersects triangle, check if ray does.
+			var QdN = - sign * diff.dot( normal );
+			// t < 0, no intersection
+			if ( QdN < 0 )
+				return null
+
+			// Ray intersects triangle.
+			return this.at( QdN / DdN, optionalTarget );
+	
+		}
+	
+	}(),
+
 	applyMatrix4: function ( matrix4 ) {
 
 		this.direction.add( this.origin ).applyMatrix4( matrix4 );

+ 0 - 69
src/math/Triangle.js

@@ -92,69 +92,6 @@ THREE.Triangle.containsPoint = function() {
 
 }();
 
-THREE.Triangle.intersectionRay = function ()	{
-
-	// Compute the offset origin, edges, and normal.
-	var diff = new THREE.Vector3();
-	var edge1 = new THREE.Vector3();
-	var edge2 = new THREE.Vector3();
-	var normal = new THREE.Vector3();
-
-	return function ( ray, a, b, c, backfaceCulling ) {
-
-		//from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp
-
-		edge1.subVectors( b, a );
-		edge2.subVectors( c, a );
-		normal.crossVectors( edge1, edge2 );
-
-		// Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
-		// E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
-		//   |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
-		//   |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
-		//   |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
-		var DdN = ray.direction.dot(normal);
-		var sign;
-		if ( DdN > 0 ) {
-
-				if ( backfaceCulling ) return null;
-				sign = 1;
-
-		} else if ( DdN < 0 ) {
-
-				sign = - 1;
-				DdN = - DdN;
-
-		} else return null;
-
-		diff.subVectors( ray.origin, a );
-		var DdQxE2 = sign * ray.direction.dot( edge2.crossVectors( diff, edge2 ) );
-
-		// b1 < 0, no intersection
-		if ( DdQxE2 < 0 )
-			return null;
-
-		var DdE1xQ = sign * ray.direction.dot( edge1.cross( diff ) );
-		// b2 < 0, no intersection
-		if ( DdE1xQ < 0 )
-			return null;
-
-		// b1+b2 > 1, no intersection
-		if ( DdQxE2 + DdE1xQ > DdN )
-			return null
-
-		// Line intersects triangle, check if ray does.
-		var QdN = - sign * diff.dot( normal );
-		// t < 0, no intersection
-		if ( QdN < 0 )
-			return null
-
-		// Ray intersects triangle.
-		return ray.at( QdN / DdN );
-	}
-
-}();
-
 THREE.Triangle.prototype = {
 
 	constructor: THREE.Triangle,
@@ -238,12 +175,6 @@ THREE.Triangle.prototype = {
 
 	},
 
-	intersectionRay: function ( ray, backfaceCulling ) {
-
-		return THREE.Triangle.intersectionRay( ray, this.a, this.b, this.c, backfaceCulling );
-
-	},
-
 	equals: function ( triangle ) {
 
 		return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );