提交 49fd9f60 编写于 作者: M Mr.doob 提交者: GitHub

Merge pull request #11355 from takahirox/TextDecoder

Improve the ArrayBuffer/String converting performance with TextDecoder
......@@ -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 ++ ) {
......
......@@ -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 ] );
......
......@@ -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 ] );
......
......@@ -35,9 +35,16 @@ THREE.PCDLoader.prototype = {
binarryToStr: function ( data ) {
var text = "";
var charArray = new Uint8Array( data );
for ( var i = 0; i < data.byteLength; i ++ ) {
if ( window.TextDecoder !== undefined ) {
return new TextDecoder().decode( charArray );
}
var text = "";
for ( var i = 0, il = data.byteLength; i < il; i ++ ) {
text += String.fromCharCode( charArray[ i ] );
......
......@@ -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 ) );
}
......
......@@ -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 {
......
......@@ -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 ] );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册