Browse Source

replaced more for loops and refactored TextParser

Lewy Blue 7 years ago
parent
commit
7635d6de33
1 changed files with 65 additions and 98 deletions
  1. 65 98
      examples/js/loaders/FBXLoader.js

+ 65 - 98
examples/js/loaders/FBXLoader.js

@@ -348,7 +348,6 @@
 
 		var children = connections.get( textureNode.id ).children;
 
-		// embedded texture
 		if ( children !== undefined && children.length > 0 && imageMap.has( children[ 0 ].ID ) ) {
 
 			fileName = imageMap.get( children[ 0 ].ID );
@@ -807,19 +806,17 @@
 		var faceWeights = [];
 		var faceWeightIndices = [];
 
-		for ( var polygonVertexIndex = 0; polygonVertexIndex < vertexIndices.length; polygonVertexIndex ++ ) {
-
-			var vertexIndex = vertexIndices[ polygonVertexIndex ];
+		vertexIndices.forEach( function ( vertexIndex, polygonVertexIndex ) {
 
 			var endOfFace = false;
 
-			// Face index and vertex index arrays are combined in a single array
-			// A cube with quad faces looks like this:
-			// PolygonVertexIndex: *24 {
-			//  a: 0, 1, 3, -3, 2, 3, 5, -5, 4, 5, 7, -7, 6, 7, 1, -1, 1, 7, 5, -4, 6, 0, 2, -5
-			//  }
-			// Negative numbers mark the end of a face - first face here is 0, 1, 3, -3
-			// to find index of last vertex multiply by -1 and subtract 1: -3 * - 1 - 1 = 2
+						// Face index and vertex index arrays are combined in a single array
+						// A cube with quad faces looks like this:
+						// PolygonVertexIndex: *24 {
+						//  a: 0, 1, 3, -3, 2, 3, 5, -5, 4, 5, 7, -7, 6, 7, 1, -1, 1, 7, 5, -4, 6, 0, 2, -5
+						//  }
+						// Negative numbers mark the end of a face - first face here is 0, 1, 3, -3
+						// to find index of last vertex multiply by -1 and subtract 1: -3 * - 1 - 1 = 2
 			if ( vertexIndex < 0 ) {
 
 				vertexIndex = vertexIndex ^ - 1; // equivalent to ( x * -1 ) - 1
@@ -845,14 +842,13 @@
 
 				if ( weightTable[ vertexIndex ] !== undefined ) {
 
-					var array = weightTable[ vertexIndex ];
+					weightTable[ vertexIndex ].forEach( function ( wt ) {
 
-					for ( var j = 0; j < array.length; j ++ ) {
+						weights.push( wt.weight );
+						weightIndices.push( wt.id );
 
-						weights.push( array[ j ].weight );
-						weightIndices.push( array[ j ].id );
+					} );
 
-					}
 
 				}
 
@@ -896,10 +892,10 @@
 				}
 
 				// if the weight array is shorter than 4 pad with 0s
-				for ( var i = weights.length; i < 4; ++ i ) {
+				while ( weights.length < 4 ) {
 
-					weights[ i ] = 0;
-					weightIndices[ i ] = 0;
+					weights.push( 0 );
+					weightIndices.push( 0 );
 
 				}
 
@@ -928,9 +924,9 @@
 
 			if ( uvInfo ) {
 
-				for ( var i = 0; i < uvInfo.length; i ++ ) {
+				uvInfo.forEach( function ( uv, i ) {
 
-					var data = getData( polygonVertexIndex, polygonIndex, vertexIndex, uvInfo[ i ] );
+					var data = getData( polygonVertexIndex, polygonIndex, vertexIndex, uv );
 
 					if ( faceUVs[ i ] === undefined ) {
 
@@ -938,12 +934,10 @@
 
 					}
 
-					faceUVs[ i ].push(
-						data[ 0 ],
-						data[ 1 ]
-					);
+					faceUVs[ i ].push( data[ 0 ] );
+					faceUVs[ i ].push( data[ 1 ] );
 
-				}
+				} );
 
 			}
 
@@ -1067,7 +1061,7 @@
 				endOfFace = false;
 				faceLength = 0;
 
-				// reset arrays for the next face
+							// reset arrays for the next face
 				vertexPositionIndexes = [];
 				faceNormals = [];
 				faceColors = [];
@@ -1077,7 +1071,7 @@
 
 			}
 
-		}
+		} );
 
 		var geo = new THREE.BufferGeometry();
 		geo.name = geometryNode.name;
@@ -2751,93 +2745,56 @@
 			this.currentProp = [];
 			this.currentPropName = '';
 
-			var split = text.split( '\n' );
-
-			for ( var lineNum = 0; lineNum < split.length; lineNum ++ ) {
-
-				var l = split[ lineNum ];
-
-				// skip comment line
-				if ( l.match( /^[\s\t]*;/ ) ) {
-
-					continue;
-
-				}
-
-				// skip empty line
-				if ( l.match( /^[\s\t]*$/ ) ) {
-
-					continue;
-
-				}
-
-				// beginning of node
-				var beginningOfNodeExp = new RegExp( '^\\t{' + this.currentIndent + '}(\\w+):(.*){', '' );
-				var match = l.match( beginningOfNodeExp );
-
-				if ( match ) {
+			var self = this;
 
-					var nodeName = match[ 1 ].trim().replace( /^"/, '' ).replace( /"$/, '' );
+			var split = text.split( '\n' );
 
-					var nodeAttrs = match[ 2 ].split( ',' ).map( function ( attr ) {
+			split.forEach( function ( line, i ) {
 
-						return attr.trim().replace( /^"/, '' ).replace( /"$/, '' );
+				var matchComment = line.match( /^[\s\t]*;/ );
+				var matchEmpty = line.match( /^[\s\t]*$/ );
 
-					} );
+				if ( matchComment || matchEmpty ) return;
 
-					this.parseNodeBegin( l, nodeName, nodeAttrs || null );
-					continue;
+				var matchBeginning = line.match( '^\\t{' + self.currentIndent + '}(\\w+):(.*){', '' );
+				var matchProperty = line.match( '^\\t{' + ( self.currentIndent ) + '}(\\w+):[\\s\\t\\r\\n](.*)' );
+				var matchEnd = line.match( '^\\t{' + ( self.currentIndent - 1 ) + '}}' );
 
-				}
+				if ( matchBeginning ) {
 
-				// node's property
-				var propExp = new RegExp( '^\\t{' + ( this.currentIndent ) + '}(\\w+):[\\s\\t\\r\\n](.*)' );
-				var match = l.match( propExp );
+					self.parseNodeBegin( line, matchBeginning );
 
-				if ( match ) {
+				} else if ( matchProperty ) {
 
-					var propName = match[ 1 ].replace( /^"/, '' ).replace( /"$/, '' ).trim();
-					var propValue = match[ 2 ].replace( /^"/, '' ).replace( /"$/, '' ).trim();
+					self.parseNodeProperty( line, matchProperty, split[ ++ i ] );
 
-					// for special case: base64 image data follows "Content: ," line
-					//	Content: ,
-					//	 "iVB..."
-					if ( propName === 'Content' && propValue === ',' ) {
+				} else if ( matchEnd ) {
 
-						propValue = split[ ++ lineNum ].replace( /"/g, '' ).replace( /,$/, '' ).trim();
+					self.nodeEnd();
 
-					}
+				} else if ( line.match( /^[^\s\t}]/ ) ) {
 
-					this.parseNodeProperty( l, propName, propValue );
-					continue;
+					// large arrays are split over multiple lines terminated with a ',' character
+					// if this is encountered the line needs to be joined to the previous line
+					self.parseNodePropertyContinued( line );
 
 				}
 
-				// end of node
-				var endOfNodeExp = new RegExp( '^\\t{' + ( this.currentIndent - 1 ) + '}}' );
-
-				if ( l.match( endOfNodeExp ) ) {
+			} );
 
-					this.nodeEnd();
-					continue;
+			return this.allNodes;
 
-				}
+		},
 
-				// large arrays are split over multiple lines terminated with a ',' character
-				// if this is encountered the line needs to be joined to the previous line
-				if ( l.match( /^[^\s\t}]/ ) ) {
+		parseNodeBegin: function ( line, property ) {
 
-					this.parseNodePropertyContinued( l );
+			var nodeName = property[ 1 ].trim().replace( /^"/, '' ).replace( /"$/, '' );
 
-				}
+			var nodeAttrs = property[ 2 ].split( ',' ).map( function ( attr ) {
 
-			}
+				return attr.trim().replace( /^"/, '' ).replace( /"$/, '' );
 
-			return this.allNodes;
-
-		},
-
-		parseNodeBegin: function ( line, nodeName, nodeAttrs ) {
+			} );
 
 			var node = { 'name': nodeName, properties: {}, 'subNodes': {} };
 			var attrs = this.parseNodeAttr( nodeAttrs );
@@ -2938,7 +2895,19 @@
 
 		},
 
-		parseNodeProperty: function ( line, propName, propValue ) {
+		parseNodeProperty: function ( line, property, contentLine ) {
+
+			var propName = property[ 1 ].replace( /^"/, '' ).replace( /"$/, '' ).trim();
+			var propValue = property[ 2 ].replace( /^"/, '' ).replace( /"$/, '' ).trim();
+
+			// for special case: base64 image data follows "Content: ," line
+			//	Content: ,
+			//	 "iVB..."
+			if ( propName === 'Content' && propValue === ',' ) {
+
+				propValue = contentLine.replace( /"/g, '' ).replace( /,$/, '' ).trim();
+
+			}
 
 			var currentNode = this.getCurrentNode();
 			var parentName = currentNode.name;
@@ -3919,13 +3888,11 @@
 	// Used internally by the TextParser
 	function parseNumberArray( value ) {
 
-		var array = value.split( ',' );
+		var array = value.split( ',' ).map( function ( val ) {
 
-		for ( var i = 0, l = array.length; i < l; i ++ ) {
+			return parseFloat( val );
 
-			array[ i ] = parseFloat( array[ i ] );
-
-		}
+		} );
 
 		return array;