浏览代码

Make BinaryLoader work on iOS 4.3.3 and some older browsers.

Antonio Vázquez Blanco 12 年之前
父节点
当前提交
3e922bc40a
共有 1 个文件被更改,包括 18 次插入20 次删除
  1. 18 20
      examples/js/loaders/BinaryLoader.js

+ 18 - 20
examples/js/loaders/BinaryLoader.js

@@ -39,6 +39,9 @@ THREE.BinaryLoader.prototype.loadAjaxJSON = function ( context, url, callback, t
 
 
 	var xhr = new XMLHttpRequest();
 	var xhr = new XMLHttpRequest();
 
 
+	texturePath = texturePath && ( typeof texturePath === "string" ) ? texturePath : this.extractUrlBase( url );
+	binaryPath = binaryPath && ( typeof binaryPath === "string" ) ? binaryPath : this.extractUrlBase( url );
+
 	xhr.onreadystatechange = function () {
 	xhr.onreadystatechange = function () {
 
 
 		if ( xhr.readyState == 4 ) {
 		if ( xhr.readyState == 4 ) {
@@ -68,8 +71,6 @@ THREE.BinaryLoader.prototype.loadAjaxBuffers = function ( json, callback, binary
 	var xhr = new XMLHttpRequest(),
 	var xhr = new XMLHttpRequest(),
 		url = binaryPath + "/" + json.buffers;
 		url = binaryPath + "/" + json.buffers;
 
 
-	var length = 0;
-
 	xhr.onreadystatechange = function () {
 	xhr.onreadystatechange = function () {
 
 
 		if ( xhr.readyState == 4 ) {
 		if ( xhr.readyState == 4 ) {
@@ -78,6 +79,14 @@ THREE.BinaryLoader.prototype.loadAjaxBuffers = function ( json, callback, binary
 
 
 				var buffer = xhr.response;
 				var buffer = xhr.response;
 				if ( buffer === undefined ) buffer = ( new Uint8Array( xhr.responseBody ) ).buffer; // IEWEBGL needs this
 				if ( buffer === undefined ) buffer = ( new Uint8Array( xhr.responseBody ) ).buffer; // IEWEBGL needs this
+				if ( buffer.byteLength == 0 ) {  // iOS and other XMLHttpRequest level 1
+					var buffer = new ArrayBuffer( xhr.responseText.length );
+					var bufView = new Uint8Array( buffer );
+					for ( var i=0, strLen=xhr.responseText.length; i<strLen; i++ ) {
+						bufView[i] = xhr.responseText.charCodeAt(i) & 0xff;
+					}
+				}
+
 				THREE.BinaryLoader.prototype.createBinModel( buffer, callback, texturePath, json.materials );
 				THREE.BinaryLoader.prototype.createBinModel( buffer, callback, texturePath, json.materials );
 
 
 			} else {
 			} else {
@@ -86,30 +95,19 @@ THREE.BinaryLoader.prototype.loadAjaxBuffers = function ( json, callback, binary
 
 
 			}
 			}
 
 
-		} else if ( xhr.readyState == 3 ) {
-
-			if ( callbackProgress ) {
-
-				if ( length == 0 ) {
-
-					length = xhr.getResponseHeader( "Content-Length" );
-
-				}
-
-				callbackProgress( { total: length, loaded: xhr.responseText.length } );
-
-			}
-
-		} else if ( xhr.readyState == 2 ) {
-
-			length = xhr.getResponseHeader( "Content-Length" );
-
 		}
 		}
 
 
 	};
 	};
+	xhr.addEventListener( "progress", function(event) {
+		if ( event.lengthComputable )
+			callbackProgress( event );
+	}, false);
+
 
 
 	xhr.open( "GET", url, true );
 	xhr.open( "GET", url, true );
 	xhr.responseType = "arraybuffer";
 	xhr.responseType = "arraybuffer";
+	if ( 'overrideMimeType' in xhr )
+		xhr.overrideMimeType('text\/plain; charset=x-user-defined');
 	xhr.send( null );
 	xhr.send( null );
 
 
 };
 };