Browse Source

Merge pull request #19278 from kaisalmen/OBJLoader2LastLine

OBJLoader2: Fix parsing of last line
Mr.doob 5 years ago
parent
commit
220c70fb71
2 changed files with 22 additions and 14 deletions
  1. 21 13
      examples/jsm/loaders/obj2/OBJLoader2Parser.js
  2. 1 1
      examples/models/obj/verify/verify.obj

+ 21 - 13
examples/jsm/loaders/obj2/OBJLoader2Parser.js

@@ -344,9 +344,13 @@ OBJLoader2Parser.prototype = {
 		this.globalCounts.totalBytes = length;
 		let buffer = new Array( 128 );
 
-		for ( let code, word = '', bufferPointer = 0, slashesCount = 0, i = 0; i < length; i ++ ) {
+		let bufferPointer = 0;
+		let slashesCount = 0;
+		let word = '';
+		let currentByte = 0;
+		for ( let code, currentByte = 0; currentByte < length; currentByte ++ ) {
 
-			code = arrayBufferView[ i ];
+			code = arrayBufferView[ currentByte ];
 			switch ( code ) {
 
 				// space
@@ -363,11 +367,8 @@ OBJLoader2Parser.prototype = {
 
 				// LF
 				case 10:
-					if ( word.length > 0 ) buffer[ bufferPointer ++ ] = word;
+					this._processLine( buffer, bufferPointer, slashesCount, word, currentByte);
 					word = '';
-					this.globalCounts.lineByte = this.globalCounts.currentByte;
-					this.globalCounts.currentByte = i;
-					this._processLine( buffer, bufferPointer, slashesCount );
 					bufferPointer = 0;
 					slashesCount = 0;
 					break;
@@ -384,6 +385,7 @@ OBJLoader2Parser.prototype = {
 
 		}
 
+		this._processLine( buffer, bufferPointer, slashesCount, word, currentByte);
 		this._finalizeParsing();
 		if ( this.logging.enabled ) console.timeEnd( 'OBJLoader2Parser.execute' );
 
@@ -404,9 +406,13 @@ OBJLoader2Parser.prototype = {
 		this.globalCounts.totalBytes = length;
 		let buffer = new Array( 128 );
 
-		for ( let char, word = '', bufferPointer = 0, slashesCount = 0, i = 0; i < length; i ++ ) {
+		let bufferPointer = 0;
+		let slashesCount = 0;
+		let word = '';
+		let currentByte = 0;
+		for ( let char; currentByte < length; currentByte ++ ) {
 
-			char = text[ i ];
+			char = text[ currentByte ];
 			switch ( char ) {
 
 				case ' ':
@@ -421,11 +427,8 @@ OBJLoader2Parser.prototype = {
 					break;
 
 				case '\n':
-					if ( word.length > 0 ) buffer[ bufferPointer ++ ] = word;
+					this._processLine( buffer, bufferPointer, slashesCount, word, currentByte );
 					word = '';
-					this.globalCounts.lineByte = this.globalCounts.currentByte;
-					this.globalCounts.currentByte = i;
-					this._processLine( buffer, bufferPointer, slashesCount );
 					bufferPointer = 0;
 					slashesCount = 0;
 					break;
@@ -440,15 +443,20 @@ OBJLoader2Parser.prototype = {
 
 		}
 
+		this._processLine( buffer, bufferPointer, word, slashesCount );
 		this._finalizeParsing();
 		if ( this.logging.enabled ) console.timeEnd( 'OBJLoader2Parser.executeLegacy' );
 
 	},
 
-	_processLine: function ( buffer, bufferPointer, slashesCount ) {
+	_processLine: function ( buffer, bufferPointer, slashesCount, word, currentByte ) {
 
+		this.globalCounts.lineByte = this.globalCounts.currentByte;
+		this.globalCounts.currentByte = currentByte;
 		if ( bufferPointer < 1 ) return;
 
+		if ( word.length > 0 ) buffer[ bufferPointer ++ ] = word;
+
 		let reconstructString = function ( content, legacyMode, start, stop ) {
 
 			let line = '';

+ 1 - 1
examples/models/obj/verify/verify.obj

@@ -212,4 +212,4 @@ v 290 40 -10
 v 310 40 -10
 v 310 60 -10
 
-l -8/-2 -7/-1 -6/-2 -5/-1
+l -8/-2 -7/-1 -6/-2 -5/-1