Selaa lähdekoodia

Loaders: Consolidate text decoding.

Don McCurdy 7 vuotta sitten
vanhempi
commit
f3701a2b7e

+ 4 - 11
examples/js/loaders/3MFLoader.js

@@ -88,23 +88,16 @@ THREE.ThreeMFLoader.prototype = {
 
 			}
 
-			if ( window.TextDecoder === undefined ) {
-
-				console.error( 'THREE.ThreeMFLoader: TextDecoder not present. Please use a TextDecoder polyfill.' );
-				return null;
-
-			}
-
-			var relsView = new DataView( zip.file( relsName ).asArrayBuffer() );
-			var relsFileText = new TextDecoder( 'utf-8' ).decode( relsView );
+			var relsView = new Uint8Array( zip.file( relsName ).asArrayBuffer() );
+			var relsFileText = THREE.Loader.decodeText( relsView );
 			rels = parseRelsXml( relsFileText );
 
 			for ( var i = 0; i < modelPartNames.length; i ++ ) {
 
 				var modelPart = modelPartNames[ i ];
-				var view = new DataView( zip.file( modelPart ).asArrayBuffer() );
+				var view = new Uint8Array( zip.file( modelPart ).asArrayBuffer() );
 
-				var fileText = new TextDecoder( 'utf-8' ).decode( view );
+				var fileText = THREE.Loader.decodeText( view );
 				var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
 
 				if ( xmlData.documentElement.nodeName.toLowerCase() !== 'model' ) {

+ 1 - 9
examples/js/loaders/AMFLoader.js

@@ -14,7 +14,6 @@
  *
  * Materials now supported, material colors supported
  * Zip support, requires jszip
- * TextDecoder polyfill required by some browsers (particularly IE, Edge)
  * No constellation support (yet)!
  *
  */
@@ -87,14 +86,7 @@ THREE.AMFLoader.prototype = {
 
 			}
 
-			if ( window.TextDecoder === undefined ) {
-
-				console.log( 'THREE.AMFLoader: TextDecoder not present. Please use TextDecoder polyfill.' );
-				return null;
-
-			}
-
-			var fileText = new TextDecoder( 'utf-8' ).decode( view );
+			var fileText = THREE.Loader.decodeText( view );
 			var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
 
 			if ( xmlData.documentElement.nodeName.toLowerCase() !== 'amf' ) {

+ 1 - 11
examples/js/loaders/BinaryLoader.js

@@ -245,17 +245,7 @@ THREE.BinaryLoader.prototype = {
 
 			function parseString( data, offset, length ) {
 
-				var charArray = new Uint8Array( data, offset, length );
-
-				var text = "";
-
-				for ( var i = 0; i < length; i ++ ) {
-
-					text += String.fromCharCode( charArray[ i ] );
-
-				}
-
-				return text;
+				return THREE.Loader.decodeText( new Uint8Array( data, offset, length ) );
 
 			}
 

+ 2 - 32
examples/js/loaders/FBXLoader.js

@@ -3630,21 +3630,7 @@
 
 		getString: function ( size ) {
 
-			var s = '';
-
-			while ( size > 0 ) {
-
-				var value = this.getUint8();
-				size --;
-
-				if ( value === 0 ) break;
-
-				s += String.fromCharCode( value );
-
-			}
-
-			// Manage UTF8 encoding
-			s = decodeURIComponent( escape( s ) );
+			var s = THREE.Loader.decodeText( this.getUint8Array( size ) );
 
 			this.skip( size );
 
@@ -3762,23 +3748,7 @@
 		if ( from === undefined ) from = 0;
 		if ( to === undefined ) to = buffer.byteLength;
 
-		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 ++ ) {
-
-			s += String.fromCharCode( array[ i ] );
-
-		}
-
-		return s;
+		return THREE.Loader.decodeText( new Uint8Array( buffer, from, to ) );
 
 	}
 

+ 4 - 27
examples/js/loaders/GLTFLoader.js

@@ -77,7 +77,7 @@ THREE.GLTFLoader = ( function () {
 
 			} else {
 
-				var magic = convertUint8ArrayToString( new Uint8Array( data, 0, 4 ) );
+				var magic = THREE.Loader.decodeText( new Uint8Array( data, 0, 4 ) );
 
 				if ( magic === BINARY_EXTENSION_HEADER_MAGIC ) {
 
@@ -96,7 +96,7 @@ THREE.GLTFLoader = ( function () {
 
 				} else {
 
-					content = convertUint8ArrayToString( new Uint8Array( data ) );
+					content = THREE.Loader.decodeText( new Uint8Array( data ) );
 
 				}
 
@@ -410,7 +410,7 @@ THREE.GLTFLoader = ( function () {
 		var headerView = new DataView( data, 0, BINARY_EXTENSION_HEADER_LENGTH );
 
 		this.header = {
-			magic: convertUint8ArrayToString( new Uint8Array( data.slice( 0, 4 ) ) ),
+			magic: THREE.Loader.decodeText( new Uint8Array( data.slice( 0, 4 ) ) ),
 			version: headerView.getUint32( 4, true ),
 			length: headerView.getUint32( 8, true )
 		};
@@ -439,7 +439,7 @@ THREE.GLTFLoader = ( function () {
 			if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON ) {
 
 				var contentArray = new Uint8Array( data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength );
-				this.content = convertUint8ArrayToString( contentArray );
+				this.content = THREE.Loader.decodeText( contentArray );
 
 			} else if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN ) {
 
@@ -1035,29 +1035,6 @@ THREE.GLTFLoader = ( function () {
 
 	}
 
-	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, il = array.length; i < il; i ++ ) {
-
-			s += String.fromCharCode( array[ i ] );
-
-		}
-
-		return s;
-
-	}
-
 	/**
 	 * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material
 	 */

+ 1 - 23
examples/js/loaders/PCDLoader.js

@@ -36,28 +36,6 @@ THREE.PCDLoader.prototype = {
 
 	parse: function ( data, url ) {
 
-		function binaryToStr( data ) {
-
-			var charArray = new Uint8Array( data );
-
-			if ( window.TextDecoder !== undefined ) {
-
-				return new TextDecoder().decode( charArray );
-
-			}
-
-			var text = '';
-
-			for ( var i = 0, l = data.byteLength; i < l; i ++ ) {
-
-				text += String.fromCharCode( charArray[ i ] );
-
-			}
-
-			return text;
-
-		}
-
 		function parseHeader( data ) {
 
 			var PCDheader = {};
@@ -167,7 +145,7 @@ THREE.PCDLoader.prototype = {
 
 		}
 
-		var textData = binaryToStr( data );
+		var textData = THREE.Loader.decodeText( data );
 
 		// parse header (always ascii format)
 

+ 1 - 23
examples/js/loaders/PLYLoader.js

@@ -61,28 +61,6 @@ THREE.PLYLoader.prototype = {
 
 	parse: function ( data ) {
 
-		function bin2str( buf ) {
-
-			var array_buffer = new Uint8Array( buf );
-
-			if ( window.TextDecoder !== undefined ) {
-
-				return new TextDecoder().decode( array_buffer );
-
-			}
-
-			var str = '';
-
-			for ( var i = 0, il = buf.byteLength; i < il; i ++ ) {
-
-				str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
-
-			}
-
-			return str;
-
-		}
-
 		function parseHeader( data ) {
 
 			var patternHeader = /ply([\s\S]*)end_header\s/;
@@ -483,7 +461,7 @@ THREE.PLYLoader.prototype = {
 
 		if ( data instanceof ArrayBuffer ) {
 
-			var text = bin2str( data );
+			var text = THREE.Loader.decodeText( new Uint8Array( data ) );
 			var header = parseHeader( text );
 
 			geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header );

+ 3 - 21
examples/js/loaders/STLLoader.js

@@ -278,30 +278,12 @@ THREE.STLLoader.prototype = {
 
 			if ( typeof buffer !== 'string' ) {
 
-				var array_buffer = new Uint8Array( buffer );
-
-				if ( window.TextDecoder !== undefined ) {
-
-					return new TextDecoder().decode( array_buffer );
-
-				}
-
-				var str = '';
-
-				for ( var i = 0, il = buffer.byteLength; i < il; i ++ ) {
-
-					str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
-
-				}
-
-				return str;
-
-			} else {
-
-				return buffer;
+				return THREE.Loader.decodeText( new Uint8Array( buffer ) );
 
 			}
 
+			return buffer;
+
 		}
 
 		function ensureBinary( buffer ) {

+ 1 - 1
examples/js/loaders/VTKLoader.js

@@ -1186,7 +1186,7 @@ Object.assign( THREE.VTKLoader.prototype, THREE.EventDispatcher.prototype, {
 		}
 
 		// get the 5 first lines of the files to check if there is the key word binary
-		var meta = String.fromCharCode.apply( null, new Uint8Array( data, 0, 250 ) ).split( '\n' );
+		var meta = THREE.Loader.decodeText( new Uint8Array( data, 0, 250 ) ).split( '\n' );
 
 		if ( meta[ 0 ].indexOf( 'xml' ) !== - 1 ) {
 

+ 2 - 9
examples/js/loaders/XLoader.js

@@ -370,14 +370,7 @@
 
 				if ( typeof buf !== "string" ) {
 
-					var array_buffer = new Uint8Array( buf );
-					var str = '';
-					for ( var i = 0; i < buf.byteLength; i ++ ) {
-
-						str += String.fromCharCode( array_buffer[ i ] );
-
-					}
-					return str;
+					return THREE.Loader.decodeText( new Uint8Array( buf ) );
 
 				} else {
 
@@ -400,7 +393,7 @@
 			key: '_parseBinary',
 			value: function _parseBinary( data ) {
 
-				return this._parseASCII( String.fromCharCode.apply( null, data ) );
+				return this._parseASCII( THREE.Loader.decodeText( new Uint8Array( data ) ) );
 
 			}
 		}, {

+ 6 - 29
examples/js/loaders/deprecated/LegacyGLTFLoader.js

@@ -54,7 +54,7 @@ THREE.LegacyGLTFLoader = ( function () {
 			var content;
 			var extensions = {};
 
-			var magic = convertUint8ArrayToString( new Uint8Array( data, 0, 4 ) );
+			var magic = THREE.Loader.decodeText( new Uint8Array( data, 0, 4 ) );
 
 			if ( magic === BINARY_EXTENSION_HEADER_DEFAULTS.magic ) {
 
@@ -63,7 +63,7 @@ THREE.LegacyGLTFLoader = ( function () {
 
 			} else {
 
-				content = convertUint8ArrayToString( new Uint8Array( data ) );
+				content = THREE.Loader.decodeText( new Uint8Array( data ) );
 
 			}
 
@@ -358,7 +358,7 @@ THREE.LegacyGLTFLoader = ( function () {
 		var headerView = new DataView( data, 0, BINARY_EXTENSION_HEADER_LENGTH );
 
 		var header = {
-			magic: convertUint8ArrayToString( new Uint8Array( data.slice( 0, 4 ) ) ),
+			magic: THREE.Loader.decodeText( new Uint8Array( data.slice( 0, 4 ) ) ),
 			version: headerView.getUint32( 4, true ),
 			length: headerView.getUint32( 8, true ),
 			contentLength: headerView.getUint32( 12, true ),
@@ -380,7 +380,7 @@ THREE.LegacyGLTFLoader = ( function () {
 		var contentArray = new Uint8Array( data, BINARY_EXTENSION_HEADER_LENGTH, header.contentLength );
 
 		this.header = header;
-		this.content = convertUint8ArrayToString( contentArray );
+		this.content = THREE.Loader.decodeText( contentArray );
 		this.body = data.slice( BINARY_EXTENSION_HEADER_LENGTH + header.contentLength, header.length );
 
 	}
@@ -390,7 +390,7 @@ THREE.LegacyGLTFLoader = ( function () {
 		var bufferView = bufferViews[ shader.extensions[ EXTENSIONS.KHR_BINARY_GLTF ].bufferView ];
 		var array = new Uint8Array( bufferView );
 
-		return convertUint8ArrayToString( array );
+		return THREE.Loader.decodeText( array );
 
 	};
 
@@ -398,7 +398,7 @@ THREE.LegacyGLTFLoader = ( function () {
 
 		var metadata = source.extensions[ EXTENSIONS.KHR_BINARY_GLTF ];
 		var bufferView = bufferViews[ metadata.bufferView ];
-		var stringData = convertUint8ArrayToString( new Uint8Array( bufferView ) );
+		var stringData = THREE.Loader.decodeText( new Uint8Array( bufferView ) );
 
 		return 'data:' + metadata.mimeType + ';base64,' + btoa( stringData );
 
@@ -664,29 +664,6 @@ THREE.LegacyGLTFLoader = ( function () {
 
 	}
 
-	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, il = array.length; i < il; i ++ ) {
-
-			s += String.fromCharCode( array[ i ] );
-
-		}
-
-		return s;
-
-	}
-
 	// Three.js seems too dependent on attribute names so globally
 	// replace those in the shader code
 	function replaceTHREEShaderAttributes( shaderText, technique ) {

+ 1 - 11
examples/js/loaders/sea3d/SEA3D.js

@@ -277,17 +277,7 @@ SEA3D.Stream.prototype.readMatrix = function () {
 
 SEA3D.Stream.prototype.readUTF8 = function ( len ) {
 
-	var buffer = this.readBytes( len );
-
-	if ( window.TextDecoder ) {
-
-		return new TextDecoder().decode( buffer );
-
-	} else {
-
-		return decodeURIComponent( escape( String.fromCharCode.apply( null, new Uint8Array( buffer ) ) ) );
-
-	}
+	return THREE.Loader.decodeText( new Uint8Array( this.readBytes( len ) ) );
 
 };
 

+ 23 - 0
src/loaders/Loader.js

@@ -351,5 +351,28 @@ Object.assign( Loader.prototype, {
 
 } );
 
+Loader.decodeText = function ( array ) {
+
+	if ( typeof 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, il = array.length; i < il; i ++ ) {
+
+		// Implicitly assumes little-endian.
+		s += String.fromCharCode( array[ i ] );
+
+	}
+
+	return s;
+
+};
 
 export { Loader };