Browse Source

Restore mesh.js

webglzhang 5 years ago
parent
commit
613981b0a0
1 changed files with 133 additions and 158 deletions
  1. 133 158
      src/objects/Mesh.js

+ 133 - 158
src/objects/Mesh.js

@@ -21,10 +21,6 @@ var _inverseMatrix = new Matrix4();
 var _ray = new Ray();
 var _sphere = new Sphere();
 
-var _matrixWorld = new Matrix4();
-var _instanceMatrix = new Matrix4();
-var _executionTimes = 1;
-
 var _vA = new Vector3();
 var _vB = new Vector3();
 var _vC = new Vector3();
@@ -147,194 +143,163 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		if ( material === undefined ) return;
 
-		//Determine how many times raycast will execute
-
-		if ( this.isInstancedMesh ) {
+		// Checking boundingSphere distance to ray
 
-			_executionTimes = this.count;
-
-		} else {
-
-			_executionTimes = 1;
-
-		}
+		if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
 
-		for ( var instanceId = 0; instanceId < _executionTimes; instanceId ++ ) {
+		_sphere.copy( geometry.boundingSphere );
+		_sphere.applyMatrix4( matrixWorld );
 
-			//Determine the world matrix for this calculation
+		if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
 
-			if ( this.isInstancedMesh ) {
-
-				_instanceMatrix = this.getMatrixAt( instanceId );
-				_matrixWorld.multiplyMatrices( matrixWorld, _instanceMatrix );
-
-			} else {
-
-				_matrixWorld = matrixWorld;
-
-			}
+		//
 
-			// Checking boundingSphere distance to ray
+		_inverseMatrix.getInverse( matrixWorld );
+		_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
 
-			if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
+		// Check boundingBox before continuing
 
-			_sphere.copy( geometry.boundingSphere );
-			_sphere.applyMatrix4( _matrixWorld );
+		if ( geometry.boundingBox !== null ) {
 
-			if ( raycaster.ray.intersectsSphere( _sphere ) === false ) continue;
+			if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
 
-			//Transform the ray into the local space of the model
-
-			_inverseMatrix.getInverse( _matrixWorld );
-			_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
-
-			// Check boundingBox before continuing
-
-			if ( geometry.boundingBox !== null ) {
-
-				if ( _ray.intersectsBox( geometry.boundingBox ) === false ) continue;
+		}
 
-			}
+		// check unsupported draw modes
 
-			var intersection;
+		if ( this.drawMode !== TrianglesDrawMode ) {
 
-			if ( geometry.isBufferGeometry ) {
+			console.warn( 'THREE.Mesh: TriangleStripDrawMode and TriangleFanDrawMode are not supported by .raycast().' );
+			return;
 
-				var a, b, c;
-				var index = geometry.index;
-				var position = geometry.attributes.position;
-				var morphPosition = geometry.morphAttributes.position;
-				var uv = geometry.attributes.uv;
-				var uv2 = geometry.attributes.uv2;
-				var groups = geometry.groups;
-				var drawRange = geometry.drawRange;
-				var i, j, il, jl;
-				var group, groupMaterial;
-				var start, end;
+		}
 
-				if ( index !== null ) {
+		var intersection;
 
-					// indexed buffer geometry
+		if ( geometry.isBufferGeometry ) {
 
-					if ( Array.isArray( material ) ) {
+			var a, b, c;
+			var index = geometry.index;
+			var position = geometry.attributes.position;
+			var morphPosition = geometry.morphAttributes.position;
+			var morphTargetsRelative = geometry.morphTargetsRelative;
+			var uv = geometry.attributes.uv;
+			var uv2 = geometry.attributes.uv2;
+			var groups = geometry.groups;
+			var drawRange = geometry.drawRange;
+			var i, j, il, jl;
+			var group, groupMaterial;
+			var start, end;
 
-						for ( i = 0, il = groups.length; i < il; i ++ ) {
+			if ( index !== null ) {
 
-							group = groups[ i ];
-							groupMaterial = material[ group.materialIndex ];
+				// indexed buffer geometry
 
-							start = Math.max( group.start, drawRange.start );
-							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
+				if ( Array.isArray( material ) ) {
 
-							for ( j = start, jl = end; j < jl; j += 3 ) {
+					for ( i = 0, il = groups.length; i < il; i ++ ) {
 
-								a = index.getX( j );
-								b = index.getX( j + 1 );
-								c = index.getX( j + 2 );
+						group = groups[ i ];
+						groupMaterial = material[ group.materialIndex ];
 
-								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
+						start = Math.max( group.start, drawRange.start );
+						end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
 
-								if ( intersection ) {
+						for ( j = start, jl = end; j < jl; j += 3 ) {
 
-									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
-									intersection.face.materialIndex = group.materialIndex;
+							a = index.getX( j );
+							b = index.getX( j + 1 );
+							c = index.getX( j + 2 );
 
-									intersection.instanceId = instanceId;
+							intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-									intersects.push( intersection );
+							if ( intersection ) {
 
-								}
+								intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
+								intersection.face.materialIndex = group.materialIndex;
+								intersects.push( intersection );
 
 							}
 
 						}
 
-					} else {
-
-						start = Math.max( 0, drawRange.start );
-						end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
-
-						for ( i = start, il = end; i < il; i += 3 ) {
+					}
 
-							a = index.getX( i );
-							b = index.getX( i + 1 );
-							c = index.getX( i + 2 );
+				} else {
 
-							intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
+					start = Math.max( 0, drawRange.start );
+					end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
 
-							if ( intersection ) {
+					for ( i = start, il = end; i < il; i += 3 ) {
 
-								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
+						a = index.getX( i );
+						b = index.getX( i + 1 );
+						c = index.getX( i + 2 );
 
-								intersection.instanceId = instanceId;
+						intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-								intersects.push( intersection );
+						if ( intersection ) {
 
-							}
+							intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
+							intersects.push( intersection );
 
 						}
 
 					}
 
-				} else if ( position !== undefined ) {
-
-					// non-indexed buffer geometry
-
-					if ( Array.isArray( material ) ) {
+				}
 
-						for ( i = 0, il = groups.length; i < il; i ++ ) {
+			} else if ( position !== undefined ) {
 
-							group = groups[ i ];
-							groupMaterial = material[ group.materialIndex ];
+				// non-indexed buffer geometry
 
-							start = Math.max( group.start, drawRange.start );
-							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
+				if ( Array.isArray( material ) ) {
 
-							for ( j = start, jl = end; j < jl; j += 3 ) {
+					for ( i = 0, il = groups.length; i < il; i ++ ) {
 
-								a = j;
-								b = j + 1;
-								c = j + 2;
+						group = groups[ i ];
+						groupMaterial = material[ group.materialIndex ];
 
-								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
+						start = Math.max( group.start, drawRange.start );
+						end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
 
-								if ( intersection ) {
+						for ( j = start, jl = end; j < jl; j += 3 ) {
 
-									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
-									intersection.face.materialIndex = group.materialIndex;
+							a = j;
+							b = j + 1;
+							c = j + 2;
 
-									intersection.instanceId = instanceId;
+							intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-									intersects.push( intersection );
+							if ( intersection ) {
 
-								}
+								intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
+								intersection.face.materialIndex = group.materialIndex;
+								intersects.push( intersection );
 
 							}
 
 						}
 
-					} else {
-
-						start = Math.max( 0, drawRange.start );
-						end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
+					}
 
-						for ( i = start, il = end; i < il; i += 3 ) {
+				} else {
 
-							a = i;
-							b = i + 1;
-							c = i + 2;
+					start = Math.max( 0, drawRange.start );
+					end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
 
-							intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
+					for ( i = start, il = end; i < il; i += 3 ) {
 
-							if ( intersection ) {
+						a = i;
+						b = i + 1;
+						c = i + 2;
 
-								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
+						intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-								intersection.instanceId = instanceId;
+						if ( intersection ) {
 
-								intersects.push( intersection );
-
-							}
+							intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
+							intersects.push( intersection );
 
 						}
 
@@ -342,50 +307,50 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 				}
 
-			} else if ( geometry.isGeometry ) {
-
-				var fvA, fvB, fvC;
-				var isMultiMaterial = Array.isArray( material );
+			}
 
-				var vertices = geometry.vertices;
-				var faces = geometry.faces;
-				var uvs;
+		} else if ( geometry.isGeometry ) {
 
-				var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
-				if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
+			var fvA, fvB, fvC;
+			var isMultiMaterial = Array.isArray( material );
 
-				for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
+			var vertices = geometry.vertices;
+			var faces = geometry.faces;
+			var uvs;
 
-					var face = faces[ f ];
-					var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
+			var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
+			if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
 
-					if ( faceMaterial === undefined ) continue;
+			for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
 
-					fvA = vertices[ face.a ];
-					fvB = vertices[ face.b ];
-					fvC = vertices[ face.c ];
+				var face = faces[ f ];
+				var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
 
-					intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint );
+				if ( faceMaterial === undefined ) continue;
 
-					if ( intersection ) {
+				fvA = vertices[ face.a ];
+				fvB = vertices[ face.b ];
+				fvC = vertices[ face.c ];
 
-						if ( uvs && uvs[ f ] ) {
+				intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint );
 
-							var uvs_f = uvs[ f ];
-							_uvA.copy( uvs_f[ 0 ] );
-							_uvB.copy( uvs_f[ 1 ] );
-							_uvC.copy( uvs_f[ 2 ] );
+				if ( intersection ) {
 
-							intersection.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() );
+					if ( uvs && uvs[ f ] ) {
 
-						}
+						var uvs_f = uvs[ f ];
+						_uvA.copy( uvs_f[ 0 ] );
+						_uvB.copy( uvs_f[ 1 ] );
+						_uvC.copy( uvs_f[ 2 ] );
 
-						intersection.face = face;
-						intersection.faceIndex = f;
-						intersects.push( intersection );
+						intersection.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() );
 
 					}
 
+					intersection.face = face;
+					intersection.faceIndex = f;
+					intersects.push( intersection );
+
 				}
 
 			}
@@ -419,7 +384,7 @@ function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point
 	if ( intersect === null ) return null;
 
 	_intersectionPointWorld.copy( point );
-	_intersectionPointWorld.applyMatrix4( _matrixWorld );
+	_intersectionPointWorld.applyMatrix4( object.matrixWorld );
 
 	var distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
 
@@ -433,7 +398,7 @@ function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point
 
 }
 
-function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c ) {
+function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ) {
 
 	_vA.fromBufferAttribute( position, a );
 	_vB.fromBufferAttribute( position, b );
@@ -458,9 +423,19 @@ function checkBufferGeometryIntersection( object, material, raycaster, ray, posi
 			_tempB.fromBufferAttribute( morphAttribute, b );
 			_tempC.fromBufferAttribute( morphAttribute, c );
 
-			_morphA.addScaledVector( _tempA.sub( _vA ), influence );
-			_morphB.addScaledVector( _tempB.sub( _vB ), influence );
-			_morphC.addScaledVector( _tempC.sub( _vC ), influence );
+			if ( morphTargetsRelative ) {
+
+				_morphA.addScaledVector( _tempA, influence );
+				_morphB.addScaledVector( _tempB, influence );
+				_morphC.addScaledVector( _tempC, influence );
+
+			} else {
+
+				_morphA.addScaledVector( _tempA.sub( _vA ), influence );
+				_morphB.addScaledVector( _tempB.sub( _vB ), influence );
+				_morphC.addScaledVector( _tempC.sub( _vC ), influence );
+
+			}
 
 		}