Browse Source

Improve the converting performance from ArrayBuffer to String with TextCoder and some tunings.

Takahiro 8 years ago
parent
commit
d681e0f081

+ 6 - 0
examples/js/loaders/FBXLoader.js

@@ -4960,6 +4960,12 @@
 
 		var array = new Uint8Array( buffer, from, to );
 
+		if ( window.TextDecoder !== undefined ) {
+
+			return new TextDecoder().decode( array );
+
+		}
+
 		var s = '';
 
 		for ( var i = 0, il = array.length; i < il; i ++ ) {

+ 10 - 3
examples/js/loaders/GLTF2Loader.js

@@ -978,13 +978,20 @@ THREE.GLTF2Loader = ( function () {
 
 	}
 
-	// Avoid the String.fromCharCode.apply(null, array) shortcut, which
-	// throws a "maximum call stack size exceeded" error for large arrays.
 	function convertUint8ArrayToString( array ) {
 
+		if ( window.TextDecoder !== undefined ) {
+
+			return new TextDecoder().decode( array );
+
+		}
+
+		// Avoid the String.fromCharCode.apply(null, array) shortcut, which
+		// throws a "maximum call stack size exceeded" error for large arrays.
+
 		var s = '';
 
-		for ( var i = 0; i < array.length; i ++ ) {
+		for ( var i = 0, il = array.length; i < il; i ++ ) {
 
 			s += String.fromCharCode( array[ i ] );
 

+ 10 - 3
examples/js/loaders/GLTFLoader.js

@@ -662,13 +662,20 @@ THREE.GLTFLoader = ( function () {
 
 	}
 
-	// Avoid the String.fromCharCode.apply(null, array) shortcut, which
-	// throws a "maximum call stack size exceeded" error for large arrays.
 	function convertUint8ArrayToString( array ) {
 
+		if ( window.TextDecoder !== undefined ) {
+
+			//return new TextDecoder().decode( array );
+
+		}
+
+		// Avoid the String.fromCharCode.apply(null, array) shortcut, which
+		// throws a "maximum call stack size exceeded" error for large arrays.
+
 		var s = '';
 
-		for ( var i = 0; i < array.length; i ++ ) {
+		for ( var i = 0, il = array.length; i < il; i ++ ) {
 
 			s += String.fromCharCode( array[ i ] );
 

+ 14 - 14
examples/js/loaders/PLYLoader.js

@@ -61,19 +61,19 @@ THREE.PLYLoader.prototype = {
 
 	parse: function ( data ) {
 
-		function isASCII( data ) {
+		function bin2str( buf ) {
 
-			var header = parseHeader( bin2str( data ) );
-			return header.format === 'ascii';
+			var array_buffer = new Uint8Array( buf );
 
-		}
+			if ( window.TextDecoder !== undefined ) {
 
-		function bin2str( buf ) {
+				return new TextDecoder().decode( array_buffer );
+
+			}
 
-			var array_buffer = new Uint8Array( buf );
 			var str = '';
 
-			for ( var i = 0; i < buf.byteLength; i ++ ) {
+			for ( var i = 0, il = buf.byteLength; i < il; i ++ ) {
 
 				str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
 
@@ -249,7 +249,7 @@ THREE.PLYLoader.prototype = {
 
 		}
 
-		function parseASCII( data ) {
+		function parseASCII( data, header ) {
 
 			// PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
 
@@ -263,8 +263,6 @@ THREE.PLYLoader.prototype = {
 
 			var result;
 
-			var header = parseHeader( data );
-
 			var patternBody = /end_header\s([\s\S]*)$/;
 			var body = '';
 			if ( ( result = patternBody.exec( data ) ) !== null ) {
@@ -446,7 +444,7 @@ THREE.PLYLoader.prototype = {
 
 		}
 
-		function parseBinary( data ) {
+		function parseBinary( data, header ) {
 
 			var buffer = {
 				indices : [],
@@ -456,7 +454,6 @@ THREE.PLYLoader.prototype = {
 				colors : []
 			};
 
-			var header = parseHeader( bin2str( data ) );
 			var little_endian = ( header.format === 'binary_little_endian' );
 			var body = new DataView( data, header.headerLength );
 			var result, loc = 0;
@@ -486,11 +483,14 @@ THREE.PLYLoader.prototype = {
 
 		if ( data instanceof ArrayBuffer ) {
 
-			geometry = isASCII( data ) ? parseASCII( bin2str( data ) ) : parseBinary( data );
+			var text = bin2str( data );
+			var header = parseHeader( text );
+
+			geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header );
 
 		} else {
 
-			geometry = parseASCII( data );
+			geometry = parseASCII( data, parseHeader( data ) );
 
 		}
 

+ 13 - 4
examples/js/loaders/STLLoader.js

@@ -245,13 +245,22 @@ THREE.STLLoader.prototype = {
 		if ( typeof buf !== "string" ) {
 
 			var array_buffer = new Uint8Array( buf );
-			var strArray = [];
-			for ( var i = 0; i < buf.byteLength; i ++ ) {
 
-				strArray.push(String.fromCharCode( array_buffer[ i ] )); // implicitly assumes little-endian
+			if ( window.TextDecoder !== undefined ) {
+
+				return new TextDecoder().decode( array_buffer );
 
 			}
-			return strArray.join('');
+
+			var str = '';
+
+			for ( var i = 0, il = buf.byteLength; i < il; i ++ ) {
+
+				str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
+
+			}
+
+			return str;
 
 		} else {
 

+ 8 - 1
examples/js/loaders/XLoader.js

@@ -362,8 +362,15 @@ THREE.XLoader = function () {
 		if ( typeof buf !== "string" ) {
 
 			var array_buffer = new Uint8Array( buf );
+
+			if ( window.TextDecoder !== undefined ) {
+
+				return new TextDecoder().decode( array_buffer );
+
+			}
+
 			var str = '';
-			for ( var i = 0; i < buf.byteLength; i ++ ) {
+			for ( var i = 0, il = buf.byteLength; i < il; i ++ ) {
 
 				str += String.fromCharCode( array_buffer[ i ] );