浏览代码

With WestLangley suggestions

Add a new variable to avoid instantiation of a Vector3 in each call to
THREE.Triangle.barycoordFromPoint().

Defer calculus after the near plane / far plane test. Need to do a copy
of intersectionPoint due to the applyMatrix4.

Test with a PlaneBufferGeometry and a BoxGeometry. Didn’t test with
indexed BufferGeometry (don’t have any in my program) but I’m quite
comfortable the uvA, uvB and uvC are the right one in that case.
rfm1201 10 年之前
父节点
当前提交
d88c5f3ade
共有 1 个文件被更改,包括 33 次插入22 次删除
  1. 33 22
      src/objects/Mesh.js

+ 33 - 22
src/objects/Mesh.js

@@ -73,6 +73,8 @@ THREE.Mesh.prototype.raycast = ( function () {
 	var uvA = new THREE.Vector2();
 	var uvB = new THREE.Vector2();
 	var uvC = new THREE.Vector2();
+	var bary = new THREE.Vector3();
+	var pInter = new THREE.Vector3();
 
 	return function raycast( raycaster, intersects ) {
 
@@ -113,7 +115,7 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 		var textureIntersection = function ( pIntersection, p1, p2, p3, uv1, uv2, uv3 ) {
 
-			var bary = THREE.Triangle.barycoordFromPoint( pIntersection, p1, p2, p3 );
+			THREE.Triangle.barycoordFromPoint( pIntersection, p1, p2, p3, bary );
 
 			uv1.multiplyScalar( bary.x );
 			uv2.multiplyScalar( bary.y );
@@ -171,21 +173,24 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 						if ( intersectionPoint === null ) continue;
 
+						pInter.copy(intersectionPoint);
+						intersectionPoint.applyMatrix4( this.matrixWorld );
+
+						var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+
+						if ( distance < raycaster.near || distance > raycaster.far ) continue;
+
 						// intersectionPoint in UV coordinates.
 						var uv = undefined;
 						if ( material.map && attributes.uv !== undefined ) {
+
 							var uvs = attributes.uv.array;
 							uvA.fromArray( uvs, a * 2 );
 							uvB.fromArray( uvs, b * 2 );
 							uvC.fromArray( uvs, c * 2 );
-							uv = textureIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC )
-						}
+							uv = textureIntersection( pInter, vA, vB, vC, uvA, uvB, uvC );
 
-						intersectionPoint.applyMatrix4( this.matrixWorld );
-
-						var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
-
-						if ( distance < raycaster.near || distance > raycaster.far ) continue;
+						}
 
 						intersects.push( {
 
@@ -224,21 +229,24 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 					if ( intersectionPoint === null ) continue;
 
+					pInter.copy(intersectionPoint);
+					intersectionPoint.applyMatrix4( this.matrixWorld );
+
+					var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+
+					if ( distance < raycaster.near || distance > raycaster.far ) continue;
+
 					// intersectionPoint in UV coordinates.
 					var uv = undefined;
 					if ( material.map && attributes.uv !== undefined ) {
+
 						var uvs = attributes.uv.array;
 						uvA.fromArray( uvs, i );
 						uvB.fromArray( uvs, i + 2 );
 						uvC.fromArray( uvs, i + 4 );
-						uv = textureIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC )
-					}
-
-					intersectionPoint.applyMatrix4( this.matrixWorld );
+						uv = textureIntersection( pInter, vA, vB, vC, uvA, uvB, uvC );
 
-					var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
-
-					if ( distance < raycaster.near || distance > raycaster.far ) continue;
+					}
 
 					a = i / 3;
 					b = a + 1;
@@ -323,21 +331,24 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 				if ( intersectionPoint === null ) continue;
 
+				pInter.copy(intersectionPoint);
+				intersectionPoint.applyMatrix4( this.matrixWorld );
+
+				var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+
+				if ( distance < raycaster.near || distance > raycaster.far ) continue;
+
 				// intersectionPoint in UV coordinates.
 				var uv = undefined;
 				if ( material.map && geometry.faceVertexUvs[ 0 ] !== undefined ) {
+
 					var uvs = geometry.faceVertexUvs[ 0 ][ f ];
 					uvA.copy( uvs[ 0 ] );
 					uvB.copy( uvs[ 1 ] );
 					uvC.copy( uvs[ 2 ] );
-					uv = textureIntersection( intersectionPoint, a, b, c, uvA, uvB, uvC )
-				}
-
-				intersectionPoint.applyMatrix4( this.matrixWorld );
-
-				var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+					uv = textureIntersection( pInter, a, b, c, uvA, uvB, uvC );
 
-				if ( distance < raycaster.near || distance > raycaster.far ) continue;
+				}
 
 				intersects.push( {