Browse Source

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 years ago
parent
commit
d88c5f3ade
1 changed files with 33 additions and 22 deletions
  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 uvA = new THREE.Vector2();
 	var uvB = new THREE.Vector2();
 	var uvB = new THREE.Vector2();
 	var uvC = new THREE.Vector2();
 	var uvC = new THREE.Vector2();
+	var bary = new THREE.Vector3();
+	var pInter = new THREE.Vector3();
 
 
 	return function raycast( raycaster, intersects ) {
 	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 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 );
 			uv1.multiplyScalar( bary.x );
 			uv2.multiplyScalar( bary.y );
 			uv2.multiplyScalar( bary.y );
@@ -171,21 +173,24 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 						if ( intersectionPoint === null ) continue;
 						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.
 						// intersectionPoint in UV coordinates.
 						var uv = undefined;
 						var uv = undefined;
 						if ( material.map && attributes.uv !== undefined ) {
 						if ( material.map && attributes.uv !== undefined ) {
+
 							var uvs = attributes.uv.array;
 							var uvs = attributes.uv.array;
 							uvA.fromArray( uvs, a * 2 );
 							uvA.fromArray( uvs, a * 2 );
 							uvB.fromArray( uvs, b * 2 );
 							uvB.fromArray( uvs, b * 2 );
 							uvC.fromArray( uvs, c * 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( {
 						intersects.push( {
 
 
@@ -224,21 +229,24 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 					if ( intersectionPoint === null ) continue;
 					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.
 					// intersectionPoint in UV coordinates.
 					var uv = undefined;
 					var uv = undefined;
 					if ( material.map && attributes.uv !== undefined ) {
 					if ( material.map && attributes.uv !== undefined ) {
+
 						var uvs = attributes.uv.array;
 						var uvs = attributes.uv.array;
 						uvA.fromArray( uvs, i );
 						uvA.fromArray( uvs, i );
 						uvB.fromArray( uvs, i + 2 );
 						uvB.fromArray( uvs, i + 2 );
 						uvC.fromArray( uvs, i + 4 );
 						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;
 					a = i / 3;
 					b = a + 1;
 					b = a + 1;
@@ -323,21 +331,24 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 				if ( intersectionPoint === null ) continue;
 				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.
 				// intersectionPoint in UV coordinates.
 				var uv = undefined;
 				var uv = undefined;
 				if ( material.map && geometry.faceVertexUvs[ 0 ] !== undefined ) {
 				if ( material.map && geometry.faceVertexUvs[ 0 ] !== undefined ) {
+
 					var uvs = geometry.faceVertexUvs[ 0 ][ f ];
 					var uvs = geometry.faceVertexUvs[ 0 ][ f ];
 					uvA.copy( uvs[ 0 ] );
 					uvA.copy( uvs[ 0 ] );
 					uvB.copy( uvs[ 1 ] );
 					uvB.copy( uvs[ 1 ] );
 					uvC.copy( uvs[ 2 ] );
 					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( {
 				intersects.push( {