浏览代码

Objects: Remove IIFEs from Mesh.

Mugen87 6 年之前
父节点
当前提交
f82f55dee2
共有 1 个文件被更改,包括 220 次插入214 次删除
  1. 220 214
      src/objects/Mesh.js

+ 220 - 214
src/objects/Mesh.js

@@ -17,6 +17,13 @@ import { BufferGeometry } from '../core/BufferGeometry.js';
  * @author jonobr1 / http://jonobr1.com/
  */
 
+var _inverseMatrix, _ray, _sphere;
+var _vA, _vB, _vC;
+var _tempA, _tempB, _tempC;
+var _morphA, _morphB, _morphC;
+var _uvA, _uvB, _uvC;
+var _intersectionPoint, _intersectionPointWorld;
+
 function Mesh( geometry, material ) {
 
 	Object3D.call( this );
@@ -112,357 +119,356 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 	},
 
-	raycast: ( function () {
+	raycast: function ( raycaster, intersects ) {
 
-		var inverseMatrix = new Matrix4();
-		var ray = new Ray();
-		var sphere = new Sphere();
+		if ( _intersectionPointWorld === undefined ) {
 
-		var vA = new Vector3();
-		var vB = new Vector3();
-		var vC = new Vector3();
+			_inverseMatrix = new Matrix4();
+			_ray = new Ray();
+			_sphere = new Sphere();
 
-		var tempA = new Vector3();
-		var tempB = new Vector3();
-		var tempC = new Vector3();
+			_vA = new Vector3();
+			_vB = new Vector3();
+			_vC = new Vector3();
 
-		var morphA = new Vector3();
-		var morphB = new Vector3();
-		var morphC = new Vector3();
+			_tempA = new Vector3();
+			_tempB = new Vector3();
+			_tempC = new Vector3();
 
-		var uvA = new Vector2();
-		var uvB = new Vector2();
-		var uvC = new Vector2();
+			_morphA = new Vector3();
+			_morphB = new Vector3();
+			_morphC = new Vector3();
 
-		var intersectionPoint = new Vector3();
-		var intersectionPointWorld = new Vector3();
+			_uvA = new Vector2();
+			_uvB = new Vector2();
+			_uvC = new Vector2();
 
-		function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
+			_intersectionPoint = new Vector3();
+			_intersectionPointWorld = new Vector3();
 
-			var intersect;
+		}
 
-			if ( material.side === BackSide ) {
+		var geometry = this.geometry;
+		var material = this.material;
+		var matrixWorld = this.matrixWorld;
 
-				intersect = ray.intersectTriangle( pC, pB, pA, true, point );
+		if ( material === undefined ) return;
 
-			} else {
+		// Checking boundingSphere distance to ray
 
-				intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
+		if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
 
-			}
+		_sphere.copy( geometry.boundingSphere );
+		_sphere.applyMatrix4( matrixWorld );
 
-			if ( intersect === null ) return null;
+		if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
 
-			intersectionPointWorld.copy( point );
-			intersectionPointWorld.applyMatrix4( object.matrixWorld );
+		//
 
-			var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
+		_inverseMatrix.getInverse( matrixWorld );
+		_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
 
-			if ( distance < raycaster.near || distance > raycaster.far ) return null;
+		// Check boundingBox before continuing
 
-			return {
-				distance: distance,
-				point: intersectionPointWorld.clone(),
-				object: object
-			};
+		if ( geometry.boundingBox !== null ) {
 
-		}
+			if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
 
-		function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c ) {
+		}
 
-			vA.fromBufferAttribute( position, a );
-			vB.fromBufferAttribute( position, b );
-			vC.fromBufferAttribute( position, c );
+		var intersection;
 
-			var morphInfluences = object.morphTargetInfluences;
+		if ( geometry.isBufferGeometry ) {
 
-			if ( material.morphTargets && morphPosition && morphInfluences ) {
+			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;
 
-				morphA.set( 0, 0, 0 );
-				morphB.set( 0, 0, 0 );
-				morphC.set( 0, 0, 0 );
+			if ( index !== null ) {
 
-				for ( var i = 0, il = morphPosition.length; i < il; i ++ ) {
+				// indexed buffer geometry
 
-					var influence = morphInfluences[ i ];
-					var morphAttribute = morphPosition[ i ];
+				if ( Array.isArray( material ) ) {
 
-					if ( influence === 0 ) continue;
+					for ( i = 0, il = groups.length; i < il; i ++ ) {
 
-					tempA.fromBufferAttribute( morphAttribute, a );
-					tempB.fromBufferAttribute( morphAttribute, b );
-					tempC.fromBufferAttribute( morphAttribute, c );
+						group = groups[ i ];
+						groupMaterial = material[ group.materialIndex ];
 
-					morphA.addScaledVector( tempA.sub( vA ), influence );
-					morphB.addScaledVector( tempB.sub( vB ), influence );
-					morphC.addScaledVector( tempC.sub( vC ), influence );
+						start = Math.max( group.start, drawRange.start );
+						end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
 
-				}
+						for ( j = start, jl = end; j < jl; j += 3 ) {
 
-				vA.add( morphA );
-				vB.add( morphB );
-				vC.add( morphC );
+							a = index.getX( j );
+							b = index.getX( j + 1 );
+							c = index.getX( j + 2 );
 
-			}
+							intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
 
-			var intersection = checkIntersection( object, material, raycaster, ray, vA, vB, vC, intersectionPoint );
-
-			if ( intersection ) {
+							if ( intersection ) {
 
-				if ( uv ) {
+								intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
+								intersection.face.materialIndex = group.materialIndex;
+								intersects.push( intersection );
 
-					uvA.fromBufferAttribute( uv, a );
-					uvB.fromBufferAttribute( uv, b );
-					uvC.fromBufferAttribute( uv, c );
+							}
 
-					intersection.uv = Triangle.getUV( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() );
+						}
 
-				}
+					}
 
-				if ( uv2 ) {
+				} else {
 
-					uvA.fromBufferAttribute( uv2, a );
-					uvB.fromBufferAttribute( uv2, b );
-					uvC.fromBufferAttribute( uv2, c );
+					start = Math.max( 0, drawRange.start );
+					end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
 
-					intersection.uv2 = Triangle.getUV( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() );
+					for ( i = start, il = end; i < il; i += 3 ) {
 
-				}
+						a = index.getX( i );
+						b = index.getX( i + 1 );
+						c = index.getX( i + 2 );
 
-				var face = new Face3( a, b, c );
-				Triangle.getNormal( vA, vB, vC, face.normal );
+						intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
 
-				intersection.face = face;
+						if ( intersection ) {
 
-			}
+							intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
+							intersects.push( intersection );
 
-			return intersection;
+						}
 
-		}
+					}
 
-		return function raycast( raycaster, intersects ) {
+				}
 
-			var geometry = this.geometry;
-			var material = this.material;
-			var matrixWorld = this.matrixWorld;
+			} else if ( position !== undefined ) {
 
-			if ( material === undefined ) return;
+				// non-indexed buffer geometry
 
-			// Checking boundingSphere distance to ray
+				if ( Array.isArray( material ) ) {
 
-			if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
+					for ( i = 0, il = groups.length; i < il; i ++ ) {
 
-			sphere.copy( geometry.boundingSphere );
-			sphere.applyMatrix4( matrixWorld );
+						group = groups[ i ];
+						groupMaterial = material[ group.materialIndex ];
 
-			if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
+						start = Math.max( group.start, drawRange.start );
+						end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
 
-			//
+						for ( j = start, jl = end; j < jl; j += 3 ) {
 
-			inverseMatrix.getInverse( matrixWorld );
-			ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
+							a = j;
+							b = j + 1;
+							c = j + 2;
 
-			// Check boundingBox before continuing
+							intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
 
-			if ( geometry.boundingBox !== null ) {
+							if ( intersection ) {
 
-				if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
+								intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
+								intersection.face.materialIndex = group.materialIndex;
+								intersects.push( intersection );
 
-			}
+							}
 
-			var intersection;
+						}
 
-			if ( geometry.isBufferGeometry ) {
+					}
 
-				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;
+				} else {
 
-				if ( index !== null ) {
+					start = Math.max( 0, drawRange.start );
+					end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
 
-					// indexed buffer geometry
+					for ( i = start, il = end; i < il; i += 3 ) {
 
-					if ( Array.isArray( material ) ) {
+						a = i;
+						b = i + 1;
+						c = i + 2;
 
-						for ( i = 0, il = groups.length; i < il; i ++ ) {
+						intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
 
-							group = groups[ i ];
-							groupMaterial = material[ group.materialIndex ];
+						if ( intersection ) {
 
-							start = Math.max( group.start, drawRange.start );
-							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
+							intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
+							intersects.push( intersection );
 
-							for ( j = start, jl = end; j < jl; j += 3 ) {
+						}
 
-								a = index.getX( j );
-								b = index.getX( j + 1 );
-								c = index.getX( j + 2 );
+					}
 
-								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
+				}
 
-								if ( intersection ) {
+			}
 
-									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
-									intersection.face.materialIndex = group.materialIndex;
-									intersects.push( intersection );
+		} else if ( geometry.isGeometry ) {
 
-								}
+			var fvA, fvB, fvC;
+			var isMultiMaterial = Array.isArray( material );
 
-							}
+			var vertices = geometry.vertices;
+			var faces = geometry.faces;
+			var uvs;
 
-						}
+			var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
+			if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
 
-					} else {
+			for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
 
-						start = Math.max( 0, drawRange.start );
-						end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
+				var face = faces[ f ];
+				var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
 
-						for ( i = start, il = end; i < il; i += 3 ) {
+				if ( faceMaterial === undefined ) continue;
 
-							a = index.getX( i );
-							b = index.getX( i + 1 );
-							c = index.getX( i + 2 );
+				fvA = vertices[ face.a ];
+				fvB = vertices[ face.b ];
+				fvC = vertices[ face.c ];
 
-							intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
+				intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint );
 
-							if ( intersection ) {
+				if ( intersection ) {
 
-								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
-								intersects.push( intersection );
+					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.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() );
 
 					}
 
-				} else if ( position !== undefined ) {
+					intersection.face = face;
+					intersection.faceIndex = f;
+					intersects.push( intersection );
 
-					// non-indexed buffer geometry
+				}
 
-					if ( Array.isArray( material ) ) {
+			}
 
-						for ( i = 0, il = groups.length; i < il; i ++ ) {
+		}
 
-							group = groups[ i ];
-							groupMaterial = material[ group.materialIndex ];
+	},
 
-							start = Math.max( group.start, drawRange.start );
-							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
+	clone: function () {
 
-							for ( j = start, jl = end; j < jl; j += 3 ) {
+		return new this.constructor( this.geometry, this.material ).copy( this );
 
-								a = j;
-								b = j + 1;
-								c = j + 2;
+	}
 
-								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
+} );
 
-								if ( intersection ) {
+function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
 
-									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
-									intersection.face.materialIndex = group.materialIndex;
-									intersects.push( intersection );
+	var intersect;
 
-								}
+	if ( material.side === BackSide ) {
 
-							}
+		intersect = ray.intersectTriangle( pC, pB, pA, true, point );
 
-						}
+	} else {
 
-					} else {
+		intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
 
-						start = Math.max( 0, drawRange.start );
-						end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
+	}
 
-						for ( i = start, il = end; i < il; i += 3 ) {
+	if ( intersect === null ) return null;
 
-							a = i;
-							b = i + 1;
-							c = i + 2;
+	_intersectionPointWorld.copy( point );
+	_intersectionPointWorld.applyMatrix4( object.matrixWorld );
 
-							intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
+	var distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
 
-							if ( intersection ) {
+	if ( distance < raycaster.near || distance > raycaster.far ) return null;
 
-								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
-								intersects.push( intersection );
+	return {
+		distance: distance,
+		point: _intersectionPointWorld.clone(),
+		object: object
+	};
 
-							}
+}
 
-						}
+function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c ) {
 
-					}
+	_vA.fromBufferAttribute( position, a );
+	_vB.fromBufferAttribute( position, b );
+	_vC.fromBufferAttribute( position, c );
 
-				}
+	var morphInfluences = object.morphTargetInfluences;
 
-			} else if ( geometry.isGeometry ) {
+	if ( material.morphTargets && morphPosition && morphInfluences ) {
 
-				var fvA, fvB, fvC;
-				var isMultiMaterial = Array.isArray( material );
+		_morphA.set( 0, 0, 0 );
+		_morphB.set( 0, 0, 0 );
+		_morphC.set( 0, 0, 0 );
 
-				var vertices = geometry.vertices;
-				var faces = geometry.faces;
-				var uvs;
+		for ( var i = 0, il = morphPosition.length; i < il; i ++ ) {
 
-				var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
-				if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
+			var influence = morphInfluences[ i ];
+			var morphAttribute = morphPosition[ i ];
 
-				for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
+			if ( influence === 0 ) continue;
 
-					var face = faces[ f ];
-					var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
+			_tempA.fromBufferAttribute( morphAttribute, a );
+			_tempB.fromBufferAttribute( morphAttribute, b );
+			_tempC.fromBufferAttribute( morphAttribute, c );
 
-					if ( faceMaterial === undefined ) continue;
+			_morphA.addScaledVector( _tempA.sub( _vA ), influence );
+			_morphB.addScaledVector( _tempB.sub( _vB ), influence );
+			_morphC.addScaledVector( _tempC.sub( _vC ), influence );
 
-					fvA = vertices[ face.a ];
-					fvB = vertices[ face.b ];
-					fvC = vertices[ face.c ];
+		}
 
-					intersection = checkIntersection( this, faceMaterial, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
+		_vA.add( _morphA );
+		_vB.add( _morphB );
+		_vC.add( _morphC );
 
-					if ( intersection ) {
+	}
 
-						if ( uvs && uvs[ f ] ) {
+	var intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _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 ( uv ) {
 
-						}
+			_uvA.fromBufferAttribute( uv, a );
+			_uvB.fromBufferAttribute( uv, b );
+			_uvC.fromBufferAttribute( uv, c );
 
-						intersection.face = face;
-						intersection.faceIndex = f;
-						intersects.push( intersection );
+			intersection.uv = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
 
-					}
+		}
 
-				}
+		if ( uv2 ) {
 
-			}
+			_uvA.fromBufferAttribute( uv2, a );
+			_uvB.fromBufferAttribute( uv2, b );
+			_uvC.fromBufferAttribute( uv2, c );
 
-		};
+			intersection.uv2 = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
 
-	}() ),
+		}
 
-	clone: function () {
+		var face = new Face3( a, b, c );
+		Triangle.getNormal( _vA, _vB, _vC, face.normal );
 
-		return new this.constructor( this.geometry, this.material ).copy( this );
+		intersection.face = face;
 
 	}
 
-} );
+	return intersection;
 
+}
 
 export { Mesh };