Kaynağa Gözat

Mesh: raycast code clean up.

Mr.doob 10 yıl önce
ebeveyn
işleme
c4f09efa8c
1 değiştirilmiş dosya ile 49 ekleme ve 53 silme
  1. 49 53
      src/objects/Mesh.js

+ 49 - 53
src/objects/Mesh.js

@@ -72,8 +72,25 @@ 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();
+
+	var barycoord = new THREE.Vector3();
+
+	var intersectionPoint = new THREE.Vector3();
+	var intersectionPointWorld = new THREE.Vector3();
+
+	function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {
+
+		THREE.Triangle.barycoordFromPoint( point, p1, p2, p3, barycoord );
+
+		uv1.multiplyScalar( barycoord.x );
+		uv2.multiplyScalar( barycoord.y );
+		uv3.multiplyScalar( barycoord.z );
+
+		uv1.add( uv2 ).add( uv3 );
+
+		return uv1.clone();
+
+	}
 
 
 	return function raycast( raycaster, intersects ) {
 	return function raycast( raycaster, intersects ) {
 
 
@@ -112,21 +129,6 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 		var a, b, c;
 		var a, b, c;
 
 
-		var textureIntersection = function ( pIntersection, p1, p2, p3, uv1, uv2, uv3 ) {
-
-			THREE.Triangle.barycoordFromPoint( pIntersection, p1, p2, p3, bary );
-
-			uv1.multiplyScalar( bary.x );
-			uv2.multiplyScalar( bary.y );
-			uv3.multiplyScalar( bary.z );
-
-			uv1.add( uv2 );
-			uv1.add( uv3 );
-
-			return uv1.clone();
-
-		};
-
 		if ( geometry instanceof THREE.BufferGeometry ) {
 		if ( geometry instanceof THREE.BufferGeometry ) {
 
 
 			var index = geometry.index;
 			var index = geometry.index;
@@ -166,39 +168,37 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 						if ( material.side === THREE.BackSide ) {
 						if ( material.side === THREE.BackSide ) {
 
 
-							var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
+							if ( ray.intersectTriangle( vC, vB, vA, true, intersectionPoint ) === null ) continue;
 
 
 						} else {
 						} else {
 
 
-							var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
+							if ( ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide, intersectionPoint ) === null ) continue;
 
 
 						}
 						}
 
 
-						if ( intersectionPoint === null ) continue;
-
-						pInter.copy( intersectionPoint );
-						intersectionPoint.applyMatrix4( this.matrixWorld );
+						intersectionPointWorld.copy( intersectionPoint );
+						intersectionPointWorld.applyMatrix4( this.matrixWorld );
 
 
-						var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+						var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
 
 
 						if ( distance < raycaster.near || distance > raycaster.far ) continue;
 						if ( distance < raycaster.near || distance > raycaster.far ) continue;
 
 
-						// intersectionPoint in UV coordinates.
-						var uv = undefined;
-						if ( material.map && attributes.uv !== undefined ) {
+						var uv;
+
+						if ( 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( pInter, vA, vB, vC, uvA, uvB, uvC );
+							uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
 
 
 						}
 						}
 
 
 						intersects.push( {
 						intersects.push( {
 
 
 							distance: distance,
 							distance: distance,
-							point: intersectionPoint,
+							point: intersectionPointWorld.clone(),
 							uv: uv,
 							uv: uv,
 							face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
 							face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
 							faceIndex: Math.floor( i / 3 ), // triangle number in indices buffer semantics
 							faceIndex: Math.floor( i / 3 ), // triangle number in indices buffer semantics
@@ -222,32 +222,30 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 					if ( material.side === THREE.BackSide ) {
 					if ( material.side === THREE.BackSide ) {
 
 
-						var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
+						if ( ray.intersectTriangle( vC, vB, vA, true, intersectionPoint ) === null ) continue;
 
 
 					} else {
 					} else {
 
 
-						var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
+						if ( ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide, intersectionPoint ) === null ) continue;
 
 
 					}
 					}
 
 
-					if ( intersectionPoint === null ) continue;
-
-					pInter.copy( intersectionPoint );
-					intersectionPoint.applyMatrix4( this.matrixWorld );
+					intersectionPointWorld.copy( intersectionPoint );
+					intersectionPointWorld.applyMatrix4( this.matrixWorld );
 
 
-					var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+					var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
 
 
 					if ( distance < raycaster.near || distance > raycaster.far ) continue;
 					if ( distance < raycaster.near || distance > raycaster.far ) continue;
 
 
-					// intersectionPoint in UV coordinates.
-					var uv = undefined;
-					if ( material.map && attributes.uv !== undefined ) {
+					var uv;
+
+					if ( 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( pInter, vA, vB, vC, uvA, uvB, uvC );
+						uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
 
 
 					}
 					}
 
 
@@ -258,7 +256,7 @@ THREE.Mesh.prototype.raycast = ( function () {
 					intersects.push( {
 					intersects.push( {
 
 
 						distance: distance,
 						distance: distance,
-						point: intersectionPoint,
+						point: intersectionPointWorld.clone(),
 						uv: uv,
 						uv: uv,
 						face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
 						face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
 						index: a, // triangle number in positions buffer semantics
 						index: a, // triangle number in positions buffer semantics
@@ -324,39 +322,37 @@ THREE.Mesh.prototype.raycast = ( function () {
 
 
 				if ( faceMaterial.side === THREE.BackSide ) {
 				if ( faceMaterial.side === THREE.BackSide ) {
 
 
-					var intersectionPoint = ray.intersectTriangle( c, b, a, true );
+					if ( ray.intersectTriangle( c, b, a, true, intersectionPoint ) === null ) continue;
 
 
 				} else {
 				} else {
 
 
-					var intersectionPoint = ray.intersectTriangle( a, b, c, faceMaterial.side !== THREE.DoubleSide );
+					if ( ray.intersectTriangle( a, b, c, faceMaterial.side !== THREE.DoubleSide, intersectionPoint ) === null ) continue;
 
 
 				}
 				}
 
 
-				if ( intersectionPoint === null ) continue;
+				intersectionPointWorld.copy( intersectionPoint );
+				intersectionPointWorld.applyMatrix4( this.matrixWorld );
 
 
-				pInter.copy( intersectionPoint );
-				intersectionPoint.applyMatrix4( this.matrixWorld );
-
-				var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
+				var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
 
 
 				if ( distance < raycaster.near || distance > raycaster.far ) continue;
 				if ( distance < raycaster.near || distance > raycaster.far ) continue;
 
 
-				// intersectionPoint in UV coordinates.
-				var uv = undefined;
-				if ( material.map && geometry.faceVertexUvs[ 0 ] !== undefined ) {
+				var uv;
+
+				if ( 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( pInter, a, b, c, uvA, uvB, uvC );
+					uv = uvIntersection( intersectionPoint, a, b, c, uvA, uvB, uvC );
 
 
 				}
 				}
 
 
 				intersects.push( {
 				intersects.push( {
 
 
 					distance: distance,
 					distance: distance,
-					point: intersectionPoint,
+					point: intersectionPointWorld.clone(),
 					uv: uv,
 					uv: uv,
 					face: face,
 					face: face,
 					faceIndex: f,
 					faceIndex: f,