Pārlūkot izejas kodu

Moved the downloadFiles function to an instance function

Kacey Coley 7 gadi atpakaļ
vecāks
revīzija
c88431df52

+ 2 - 1
Tools/Gulp/config.json

@@ -1557,7 +1557,8 @@
             {
                 "files": [
                     "../../serializers/src/glTF/2.0/babylon.glTFSerializer.ts",
-                    "../../serializers/src/glTF/2.0/babylon.glTFExporter.ts"
+                    "../../serializers/src/glTF/2.0/babylon.glTFExporter.ts",
+                    "../../serializers/src/glTF/2.0/babylon.glTFData.ts"
                 ],
                 "output": "babylon.glTF2Serializer.js"
             }

+ 49 - 0
serializers/src/glTF/2.0/babylon.glTFData.ts

@@ -0,0 +1,49 @@
+module BABYLON {
+    /**
+     * Class for holding and downloading glTF file data
+     */
+    export class _GLTFData {
+        _glTFFiles: {[fileName: string]: string | Blob};
+    
+        public constructor() {
+            this._glTFFiles = {};
+        }
+        /**
+         * Downloads glTF data.
+         */
+        public downloadFiles(): void {
+             /**
+             * Checks for a matching suffix at the end of a string (for ES5 and lower)
+             * @param str 
+             * @param suffix 
+             * 
+             * @returns {boolean} indicating whether the suffix matches or not
+             */
+            function endsWith(str: string, suffix: string): boolean {
+                return str.indexOf(suffix, str.length - suffix.length) !== -1;
+            }
+            for (let key in this._glTFFiles) {
+                let link = document.createElement('a');
+                document.body.appendChild(link);
+                link.setAttribute("type", "hidden");
+                link.download = key;
+                let blob = this._glTFFiles[key];
+                let mimeType;
+                
+                if (endsWith(key, ".glb")) {
+                    mimeType = {type: "model/gltf-binary"};
+                }
+                else if (endsWith(key, ".bin")) {
+                    mimeType = {type: "application/octet-stream"};
+                }
+                else if (endsWith(key, ".gltf")) {
+                    mimeType = {type: "model/gltf+json"};
+                }
+    
+                link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
+                link.click();
+            }
+    
+        } 
+    }
+}

+ 15 - 13
serializers/src/glTF/2.0/babylon.glTFExporter.ts

@@ -52,8 +52,8 @@ module BABYLON {
         bufferViews: _IGLTFBufferView[];
         accessors: _IGLTFAccessor[];
     }
-    export class _GLTF2Exporter {
 
+    export class _GLTF2Exporter {
         private bufferViews: _IGLTFBufferView[];
         private accessors: _IGLTFAccessor[];
         private nodes: _IGLTFNode[];
@@ -282,7 +282,7 @@ module BABYLON {
         private generateJSON(glb: boolean, glTFPrefix?: string, prettyPrint?: boolean): string {
             let buffer: _IGLTFBuffer = { byteLength: this.totalByteLength };
 
-            let glTf: _IGLTF = {
+            let glTF: _IGLTF = {
                 buffers: [buffer],
                 asset: this.asset,
                 meshes: this.meshes,
@@ -292,14 +292,14 @@ module BABYLON {
                 accessors: this.accessors
             };
             if (this.scenes.length > 0) {
-                glTf.scene = 0;
+                glTF.scene = 0;
             }
 
             if (!glb) {
                 buffer.uri = glTFPrefix + ".bin";
             }
 
-            let jsonText = prettyPrint ? JSON.stringify(glTf, null, 2) : JSON.stringify(glTf);
+            let jsonText = prettyPrint ? JSON.stringify(glTF, null, 2) : JSON.stringify(glTF);
 
             return jsonText;
         }
@@ -310,7 +310,7 @@ module BABYLON {
          * @returns {[x: string]: string | Blob} object with glTF json tex filename 
          * and binary file name as keys and their data as values
          */
-        public _generateGLTF(glTFPrefix: string): { [x: string]: string | Blob } {
+        public _generateGLTF(glTFPrefix: string): _GLTFData {
             const jsonText = this.generateJSON(false, glTFPrefix, true);
             const binaryBuffer = this.generateBinary();
             const bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
@@ -318,10 +318,11 @@ module BABYLON {
             const glTFFileName = glTFPrefix + '.gltf';
             const glTFBinFile = glTFPrefix + '.bin';
 
-            return {
-                [glTFFileName]: jsonText,
-                [glTFBinFile]: bin
-            };
+            let container = new _GLTFData();
+            container._glTFFiles[glTFFileName] = jsonText;
+            container._glTFFiles[glTFBinFile] = bin;
+
+            return container;
         }
         /**
          * Creates a binary buffer for glTF
@@ -345,7 +346,7 @@ module BABYLON {
          * 
          * @returns {[glbFileName: string]: Blob} object with glb filename as key and data as value
          */
-        public _generateGLB(glTFPrefix: string): { [glbFileName: string]: Blob } {
+        public _generateGLB(glTFPrefix: string): _GLTFData {
             const jsonText = this.generateJSON(true);
             const binaryBuffer = this.generateBinary();
             let glbFileName = glTFPrefix + '.glb';
@@ -399,9 +400,10 @@ module BABYLON {
             // binary data
             let glbFile = new Blob([headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer, binPaddingBuffer], { type: 'application/octet-stream' });
 
-            return {
-                [glbFileName]: glbFile
-            };
+            let container = new _GLTFData();
+            container._glTFFiles[glbFileName] = glbFile;
+
+            return container;
         }
         /**
          * Sets the TRS for each node

+ 2 - 40
serializers/src/glTF/2.0/babylon.glTFSerializer.ts

@@ -11,7 +11,7 @@ module BABYLON {
          * @returns {[fileName: string]: string | Blob} Returns an object with a .gltf, .glb and associates textures
          * as keys and their data and paths as values.
          */
-        public static GLTF(scene: BABYLON.Scene, filename: string): {[fileName: string]: string | Blob} {
+        public static GLTF(scene: BABYLON.Scene, filename: string): _GLTFData {
             let glTFPrefix = filename.replace(/\.[^/.]+$/, "");
             let gltfGenerator = new _GLTF2Exporter(scene);
 
@@ -24,49 +24,11 @@ module BABYLON {
          * 
          * @returns {[fileName: string]: string | Blob} Returns an object with a .glb filename as key and data as value
          */
-        public static GLB(scene: BABYLON.Scene, filename: string): {[fileName: string]: string | Blob} {
+        public static GLB(scene: BABYLON.Scene, filename: string): _GLTFData {
             let glTFPrefix = filename.replace(/\.[^/.]+$/, "");        
             let gltfGenerator = new _GLTF2Exporter(scene);
 
             return gltfGenerator._generateGLB(glTFPrefix);
         }
-        /**
-         * Downloads data from glTF object.
-         * 
-         * @param gltfData glTF object with keys being file names and values being data
-         */
-        public static downloadFiles(gltfData: {[fileName: string]: string | Blob} ): void {
-            /**
-             * Checks for a matching suffix at the end of a string (for ES5 and lower)
-             * @param str 
-             * @param suffix 
-             * 
-             * @returns {boolean} indicating whether the suffix matches or not
-             */
-            function endsWith(str: string, suffix: string): boolean {
-                return str.indexOf(suffix, str.length - suffix.length) !== -1;
-            }
-            for (let key in gltfData) {
-                let link = document.createElement('a');
-                document.body.appendChild(link);
-                link.setAttribute("type", "hidden");
-                link.download = key;
-                let blob = gltfData[key];
-                let mimeType;
-                
-                if (endsWith(key, ".glb")) {
-                    mimeType = {type: "model/gltf-binary"};
-                }
-                else if (endsWith(key, ".bin")) {
-                    mimeType = {type: "application/octet-stream"};
-                }
-                else if (endsWith(key, ".gltf")) {
-                    mimeType = {type: "model/gltf+json"};
-                }
-
-                link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
-                link.click();
-            }
-        }
     }
 }