瀏覽代碼

Add support for arraybuffer during texture serialization

David Catuhe 5 年之前
父節點
當前提交
5f1085b58a
共有 3 個文件被更改,包括 10 次插入36 次删除
  1. 3 0
      src/Materials/Textures/texture.ts
  2. 7 2
      src/Misc/fileTools.ts
  3. 0 34
      src/Misc/tools.ts

+ 3 - 0
src/Materials/Textures/texture.ts

@@ -12,6 +12,7 @@ import { TimingTools } from '../../Misc/timingTools';
 import { InstantiationTools } from '../../Misc/instantiationTools';
 import { Plane } from '../../Maths/math.plane';
 import { StringTools } from '../../Misc/stringTools';
+import { FileTools } from '../../Misc/fileTools';
 
 declare type CubeTexture = import("../../Materials/Textures/cubeTexture").CubeTexture;
 declare type MirrorTexture = import("../../Materials/Textures/mirrorTexture").MirrorTexture;
@@ -605,6 +606,8 @@ export class Texture extends BaseTexture {
             if (typeof this._buffer === "string" && (this._buffer as string).substr(0, 5) === "data:") {
                 serializationObject.base64String = this._buffer;
                 serializationObject.name = serializationObject.name.replace("data:", "");
+            } else if (this.url && StringTools.StartsWith(this.url, "data:") && this._buffer instanceof Uint8Array) {
+                serializationObject.base64String = "data:image/png;base64," + FileTools.ArrayBufferToBase64(this._buffer);
             }
         }
 

+ 7 - 2
src/Misc/fileTools.ts

@@ -122,7 +122,12 @@ export class FileTools {
         }
     }
 
-    private static _ArrayBufferToBase64(buffer: ArrayBuffer | ArrayBufferView) {
+    /**
+     * Encode an array buffer into a base64 string
+     * @param buffer defines the buffer to encode
+     * @returns a string containing the base64 version of the buffer
+     */
+    public 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;
@@ -150,7 +155,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,` + this.ArrayBufferToBase64(input);
             }
         }
         else if (input instanceof Blob) {

+ 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