Browse Source

Support base64 image fallback
when Blob not available in glTF loader

Gary Hsu 5 years ago
parent
commit
f62ea34ed9
4 changed files with 37 additions and 46 deletions
  1. 1 1
      loaders/src/glTF/2.0/glTFLoader.ts
  2. 2 11
      src/Misc/fileTools.ts
  3. 34 0
      src/Misc/stringTools.ts
  4. 0 34
      src/Misc/tools.ts

+ 1 - 1
loaders/src/glTF/2.0/glTFLoader.ts

@@ -1897,7 +1897,7 @@ export class GLTFLoader implements IGLTFLoader {
             if (!this._disposed) {
                 deferred.reject(new Error(`${context}: ${(exception && exception.message) ? exception.message : message || "Failed to load texture"}`));
             }
-        });
+        }, undefined, undefined, undefined, image.mimeType);
         promises.push(deferred.promise);
 
         if (!url) {

+ 2 - 11
src/Misc/fileTools.ts

@@ -7,6 +7,7 @@ import { Observable } from './observable';
 import { FilesInputStore } from './filesInputStore';
 import { RetryStrategy } from './retryStrategy';
 import { BaseError } from './baseError';
+import { StringTools } from './stringTools';
 
 /** @ignore */
 export class LoadFileError extends BaseError {
@@ -122,16 +123,6 @@ export class FileTools {
         }
     }
 
-    private static _ArrayBufferToBase64(buffer: ArrayBuffer | ArrayBufferView) {
-        var binary = '';
-        var bytes = (buffer as ArrayBufferView).buffer ? new Uint8Array((buffer as ArrayBufferView).buffer) : new Uint8Array(buffer as ArrayBuffer);
-        var len = bytes.byteLength;
-        for (var i = 0; i < len; i++) {
-            binary += String.fromCharCode(bytes[i]);
-        }
-        return window.btoa(binary);
-    }
-
     /**
      * Loads an image as an HTMLImageElement.
      * @param input url string, ArrayBuffer, or Blob to load
@@ -150,7 +141,7 @@ export class FileTools {
                 url = URL.createObjectURL(new Blob([input]));
                 usingObjectURL = true;
             } else {
-                url = `data:${mimeType || "image/jpg"};base64,` + this._ArrayBufferToBase64(input);
+                url = `data:${mimeType || "image/jpg"};base64,` + StringTools.EncodeArrayBufferToBase64(input);
             }
         }
         else if (input instanceof Blob) {

+ 34 - 0
src/Misc/stringTools.ts

@@ -39,4 +39,38 @@ export class StringTools {
 
         return result;
     }
+
+    /**
+     * Encode a buffer to a base64 string
+     * @param buffer defines the buffer to encode
+     * @returns the encoded string
+     */
+    public static EncodeArrayBufferToBase64(buffer: ArrayBuffer | ArrayBufferView): string {
+        var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+        var output = "";
+        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
+        var i = 0;
+        var bytes = ArrayBuffer.isView(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength) : new Uint8Array(buffer);
+
+        while (i < bytes.length) {
+            chr1 = bytes[i++];
+            chr2 = i < bytes.length ? bytes[i++] : Number.NaN;
+            chr3 = i < bytes.length ? bytes[i++] : Number.NaN;
+
+            enc1 = chr1 >> 2;
+            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
+            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
+            enc4 = chr3 & 63;
+
+            if (isNaN(chr2)) {
+                enc3 = enc4 = 64;
+            } else if (isNaN(chr3)) {
+                enc4 = 64;
+            }
+            output += keyStr.charAt(enc1) + keyStr.charAt(enc2) +
+                keyStr.charAt(enc3) + keyStr.charAt(enc4);
+        }
+
+        return output;
+    }
 }

+ 0 - 34
src/Misc/tools.ts

@@ -260,40 +260,6 @@ export class Tools {
     }
 
     /**
-     * Encode a buffer to a base64 string
-     * @param buffer defines the buffer to encode
-     * @returns the encoded string
-     */
-    public static EncodeArrayBufferTobase64(buffer: ArrayBuffer): string {
-        var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
-        var output = "";
-        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
-        var i = 0;
-        var bytes = new Uint8Array(buffer);
-
-        while (i < bytes.length) {
-            chr1 = bytes[i++];
-            chr2 = i < bytes.length ? bytes[i++] : Number.NaN; // Not sure if the index
-            chr3 = i < bytes.length ? bytes[i++] : Number.NaN; // checks are needed here
-
-            enc1 = chr1 >> 2;
-            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
-            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
-            enc4 = chr3 & 63;
-
-            if (isNaN(chr2)) {
-                enc3 = enc4 = 64;
-            } else if (isNaN(chr3)) {
-                enc4 = 64;
-            }
-            output += keyStr.charAt(enc1) + keyStr.charAt(enc2) +
-                keyStr.charAt(enc3) + keyStr.charAt(enc4);
-        }
-
-        return "data:image/png;base64," + output;
-    }
-
-    /**
      * Returns an array if obj is not an array
      * @param obj defines the object to evaluate as an array
      * @param allowsNullUndefined defines a boolean indicating if obj is allowed to be null or undefined