浏览代码

Merge remote-tracking branch 'stephomi/dev-ray' into dev

Mr.doob 12 年之前
父节点
当前提交
267f622cc3
共有 3 个文件被更改,包括 12 次插入12 次删除
  1. 6 6
      src/core/Raycaster.js
  2. 2 2
      src/math/Ray.js
  3. 4 4
      test/unit/math/Ray.js

+ 6 - 6
src/core/Raycaster.js

@@ -353,20 +353,20 @@
 
 			for ( var i = 0; i < nbVertices - 1; i = i + step ) {
 
-				localRay.distanceToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
-				interSegment.applyMatrix4( object.matrixWorld );
-				interRay.applyMatrix4( object.matrixWorld );
+				var distSq = localRay.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
 
-				if ( interRay.distanceToSquared( interSegment ) <= precisionSq ) {
+				if ( distSq <= precisionSq ) {
 
-					var distance = raycaster.ray.origin.distanceTo( interRay );
+					var distance = localRay.origin.distanceTo( interRay );
 
 					if ( raycaster.near <= distance && distance <= raycaster.far ) {
 
 						intersects.push( {
 
 							distance: distance,
-							point: interSegment.clone(),
+							// What do we want? intersection point on the ray or on the segment??
+							// point: raycaster.ray.at( distance ),
+							point: interSegment.clone().applyMatrix4( object.matrixWorld ),
 							face: null,
 							faceIndex: null,
 							object: object

+ 2 - 2
src/math/Ray.js

@@ -61,7 +61,7 @@ THREE.Ray.prototype = {
 
 		if ( directionDistance < 0 ) {
 
-			return this.origin.clone();
+			return result.copy( this.origin );
 
 		}
 
@@ -93,7 +93,7 @@ THREE.Ray.prototype = {
 
 	}(),
 
-	distanceToSegment: function( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
+	distanceSqToSegment: function( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
 
 		// from http://www.geometrictools.com/LibMathematics/Distance/Wm5DistRay3Segment3.cpp
 		// It returns the min distance between the ray and the segment

+ 4 - 4
test/unit/math/Ray.js

@@ -182,7 +182,7 @@ test( "applyMatrix4", function() {
 });
 
 
-test( "distanceToSegment", function() {
+test( "distanceSqToSegment", function() {
 	var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
 	var ptOnLine = new THREE.Vector3();
 	var ptOnSegment = new THREE.Vector3();
@@ -190,7 +190,7 @@ test( "distanceToSegment", function() {
 	//segment in front of the ray
 	var v0 = new THREE.Vector3( 3, 5, 50 );
 	var v1 = new THREE.Vector3( 50, 50, 50 ); // just a far away point
-	var distSqr = a.distanceToSegment( v0, v1, ptOnLine, ptOnSegment );
+	var distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
 
 	ok( ptOnSegment.distanceTo( v0 ) < 0.0001, "Passed!" );
 	ok( ptOnLine.distanceTo( new THREE.Vector3(1, 1, 50) ) < 0.0001, "Passed!" );
@@ -200,7 +200,7 @@ test( "distanceToSegment", function() {
 	//segment behind the ray
 	v0 = new THREE.Vector3( -50, -50, -50 ); // just a far away point
 	v1 = new THREE.Vector3( -3, -5, -4 );
-	distSqr = a.distanceToSegment( v0, v1, ptOnLine, ptOnSegment );
+	distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
 
 	ok( ptOnSegment.distanceTo( v1 ) < 0.0001, "Passed!" );
 	ok( ptOnLine.distanceTo( one3 ) < 0.0001, "Passed!" );
@@ -210,7 +210,7 @@ test( "distanceToSegment", function() {
 	//exact intersection between the ray and the segment
 	v0 = new THREE.Vector3( -50, -50, -50 );
 	v1 = new THREE.Vector3( 50, 50, 50 );
-	distSqr = a.distanceToSegment( v0, v1, ptOnLine, ptOnSegment );
+	distSqr = a.distanceSqToSegment( v0, v1, ptOnLine, ptOnSegment );
 
 	ok( ptOnSegment.distanceTo( one3 ) < 0.0001, "Passed!" );
 	ok( ptOnLine.distanceTo( one3 ) < 0.0001, "Passed!" );