Browse Source

add Triangle.uvIntersection

06wj 7 years ago
parent
commit
f6f4d8d3c9
3 changed files with 27 additions and 35 deletions
  1. 24 0
      src/math/Triangle.js
  2. 2 18
      src/objects/Mesh.js
  3. 1 17
      src/objects/Sprite.js

+ 24 - 0
src/math/Triangle.js

@@ -1,3 +1,4 @@
+import { Vector2 } from './Vector2.js';
 import { Vector3 } from './Vector3.js';
 import { Vector3 } from './Vector3.js';
 
 
 /**
 /**
@@ -94,6 +95,29 @@ Object.assign( Triangle, {
 
 
 	}(),
 	}(),
 
 
+	uvIntersection: function () {
+
+		var barycoord = new Vector3();
+		var v1 = new Vector2();
+		var v2 = new Vector2();
+		var v3 = new Vector2();
+
+		return function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3, result ) {
+
+			this.getBarycoord( point, p1, p2, p3, barycoord );
+
+			v1.copy( uv1 ).multiplyScalar( barycoord.x );
+			v2.copy( uv2 ).multiplyScalar( barycoord.y );
+			v3.copy( uv3 ).multiplyScalar( barycoord.z );
+
+			v1.add( v2 ).add( v3 );
+
+			return result.copy( v1 );
+
+		};
+
+	}(),
+
 	containsPoint: function () {
 	containsPoint: function () {
 
 
 		var v1 = new Vector3();
 		var v1 = new Vector3();

+ 2 - 18
src/objects/Mesh.js

@@ -140,25 +140,9 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 		var uvB = new Vector2();
 		var uvB = new Vector2();
 		var uvC = new Vector2();
 		var uvC = new Vector2();
 
 
-		var barycoord = new Vector3();
-
 		var intersectionPoint = new Vector3();
 		var intersectionPoint = new Vector3();
 		var intersectionPointWorld = new Vector3();
 		var intersectionPointWorld = new Vector3();
 
 
-		function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {
-
-			Triangle.getBarycoord( 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();
-
-		}
-
 		function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
 		function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
 
 
 			var intersect;
 			var intersect;
@@ -206,7 +190,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 					uvB.fromBufferAttribute( uv, b );
 					uvB.fromBufferAttribute( uv, b );
 					uvC.fromBufferAttribute( uv, c );
 					uvC.fromBufferAttribute( uv, c );
 
 
-					intersection.uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
+					intersection.uv = Triangle.uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() );
 
 
 				}
 				}
 
 
@@ -448,7 +432,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 							uvB.copy( uvs_f[ 1 ] );
 							uvB.copy( uvs_f[ 1 ] );
 							uvC.copy( uvs_f[ 2 ] );
 							uvC.copy( uvs_f[ 2 ] );
 
 
-							intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
+							intersection.uv = Triangle.uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC, new Vector2() );
 
 
 						}
 						}
 
 

+ 1 - 17
src/objects/Sprite.js

@@ -98,22 +98,6 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 
 		}
 		}
 
 
-		var barycoord = new Vector3();
-
-		function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {
-
-			Triangle.getBarycoord( 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 ) {
 
 
 			worldScale.setFromMatrixScale( this.matrixWorld );
 			worldScale.setFromMatrixScale( this.matrixWorld );
@@ -165,7 +149,7 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 
 				distance: distance,
 				distance: distance,
 				point: intersectPoint.clone(),
 				point: intersectPoint.clone(),
-				uv: uvIntersection( intersectPoint, vA, vB, vC, uvA, uvB, uvC ),
+				uv: Triangle.uvIntersection( intersectPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ),
 				face: null,
 				face: null,
 				object: this
 				object: this