Przeglądaj źródła

code feedback changes

Kacey Coley 7 lat temu
rodzic
commit
4b1ed13154

+ 45 - 51
serializers/src/glTF/2.0/babylon.glTFExporter.ts

@@ -595,7 +595,7 @@ module BABYLON.GLTF2 {
          * @returns GLTFData with glTF file data
          */
         public _generateGLTFAsync(glTFPrefix: string): Promise<GLTFData> {
-            return this.generateBinaryAsync().then(binaryBuffer => {
+            return this._generateBinaryAsync().then(binaryBuffer => {
                 const jsonText = this.generateJSON(false, glTFPrefix, true);
                 const bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
 
@@ -622,7 +622,7 @@ module BABYLON.GLTF2 {
          * Creates a binary buffer for glTF
          * @returns array buffer for binary data
          */
-        private generateBinaryAsync(): Promise<ArrayBuffer> {
+        private _generateBinaryAsync(): Promise<ArrayBuffer> {
             let binaryWriter = new _BinaryWriter(4);
             return this.createSceneAsync(this.babylonScene, binaryWriter).then(() => {
                 return binaryWriter.getArrayBuffer();
@@ -648,7 +648,7 @@ module BABYLON.GLTF2 {
          * @returns object with glb filename as key and data as value
          */
         public _generateGLBAsync(glTFPrefix: string): Promise<GLTFData> {
-            return this.generateBinaryAsync().then(binaryBuffer => {
+            return this._generateBinaryAsync().then(binaryBuffer => {
                 const jsonText = this.generateJSON(true);
                 const glbFileName = glTFPrefix + '.glb';
                 const headerLength = 12;
@@ -952,8 +952,7 @@ module BABYLON.GLTF2 {
                         let materialIndex: Nullable<number> = null;
                         if (babylonMaterial) {
                             if (babylonMaterial instanceof MultiMaterial) {
-                                const babylonMultiMaterial = babylonMaterial as MultiMaterial;
-                                babylonMaterial = babylonMultiMaterial.subMaterials[submesh.materialIndex];
+                                babylonMaterial = babylonMaterial.subMaterials[submesh.materialIndex];
                                 if (babylonMaterial) {
                                     materialIndex = this.babylonScene.materials.indexOf(babylonMaterial);
                                 }
@@ -1053,60 +1052,55 @@ module BABYLON.GLTF2 {
          * @param binaryWriter Buffer to write binary data to
          */
         private createSceneAsync(babylonScene: Scene, binaryWriter: _BinaryWriter): Promise<void> {
-            if (this.setNodeTransformation.length) {
-                const scene: IScene = { nodes: [] };
-                let glTFNodeIndex: number;
-                let glTFNode: INode;
-                let directDescendents: Node[];
-                const nodes = [...babylonScene.transformNodes, ...babylonScene.meshes];
-
-                return _GLTFMaterial._ConvertMaterialsToGLTFAsync(babylonScene.materials, ImageMimeType.PNG, this.images, this.textures, this.samplers, this.materials, this.imageData, true).then(() => {
-                    this.nodeMap = this.createNodeMapAndAnimations(babylonScene, nodes, this.shouldExportTransformNode, binaryWriter);
-
-                    this.totalByteLength = binaryWriter.getByteOffset();
-
-
-                    // Build Hierarchy with the node map.
-                    for (let babylonTransformNode of nodes) {
-                        glTFNodeIndex = this.nodeMap[babylonTransformNode.uniqueId];
-                        if (glTFNodeIndex != null) {
-                            glTFNode = this.nodes[glTFNodeIndex];
-                            if (!babylonTransformNode.parent) {
-                                if (!this.shouldExportTransformNode(babylonTransformNode)) {
-                                    Tools.Log("Omitting " + babylonTransformNode.name + " from scene.");
-                                }
-                                else {
-                                    if (this.convertToRightHandedSystem) {
-                                        if (glTFNode.translation) {
-                                            glTFNode.translation[2] *= -1;
-                                            glTFNode.translation[0] *= -1;
-                                        }
-                                        glTFNode.rotation = glTFNode.rotation ? Quaternion.FromArray([0, 1, 0, 0]).multiply(Quaternion.FromArray(glTFNode.rotation)).asArray() : (Quaternion.FromArray([0, 1, 0, 0])).asArray();
+            const scene: IScene = { nodes: [] };
+            let glTFNodeIndex: number;
+            let glTFNode: INode;
+            let directDescendents: Node[];
+            const nodes = [...babylonScene.transformNodes, ...babylonScene.meshes];
+
+            return _GLTFMaterial._ConvertMaterialsToGLTFAsync(babylonScene.materials, ImageMimeType.PNG, this.images, this.textures, this.samplers, this.materials, this.imageData, true).then(() => {
+                this.nodeMap = this.createNodeMapAndAnimations(babylonScene, nodes, this.shouldExportTransformNode, binaryWriter);
+
+                this.totalByteLength = binaryWriter.getByteOffset();
+
+
+                // Build Hierarchy with the node map.
+                for (let babylonTransformNode of nodes) {
+                    glTFNodeIndex = this.nodeMap[babylonTransformNode.uniqueId];
+                    if (glTFNodeIndex != null) {
+                        glTFNode = this.nodes[glTFNodeIndex];
+                        if (!babylonTransformNode.parent) {
+                            if (!this.shouldExportTransformNode(babylonTransformNode)) {
+                                Tools.Log("Omitting " + babylonTransformNode.name + " from scene.");
+                            }
+                            else {
+                                if (this.convertToRightHandedSystem) {
+                                    if (glTFNode.translation) {
+                                        glTFNode.translation[2] *= -1;
+                                        glTFNode.translation[0] *= -1;
                                     }
-
-                                    scene.nodes.push(glTFNodeIndex);
+                                    glTFNode.rotation = glTFNode.rotation ? Quaternion.FromArray([0, 1, 0, 0]).multiply(Quaternion.FromArray(glTFNode.rotation)).asArray() : (Quaternion.FromArray([0, 1, 0, 0])).asArray();
                                 }
+
+                                scene.nodes.push(glTFNodeIndex);
                             }
+                        }
 
-                            directDescendents = babylonTransformNode.getDescendants(true);
-                            if (!glTFNode.children && directDescendents && directDescendents.length) {
-                                glTFNode.children = [];
-                                for (let descendent of directDescendents) {
-                                    if (this.nodeMap[descendent.uniqueId] != null) {
-                                        glTFNode.children.push(this.nodeMap[descendent.uniqueId]);
-                                    }
+                        directDescendents = babylonTransformNode.getDescendants(true);
+                        if (!glTFNode.children && directDescendents && directDescendents.length) {
+                            glTFNode.children = [];
+                            for (let descendent of directDescendents) {
+                                if (this.nodeMap[descendent.uniqueId] != null) {
+                                    glTFNode.children.push(this.nodeMap[descendent.uniqueId]);
                                 }
                             }
                         }
-                    };
-                    if (scene.nodes.length) {
-                        this.scenes.push(scene);
                     }
-                });
-            }
-            else {
-                return Promise.resolve();
-            }
+                };
+                if (scene.nodes.length) {
+                    this.scenes.push(scene);
+                }
+            });
         }
 
         /**

+ 26 - 29
serializers/src/glTF/2.0/babylon.glTFMaterial.ts

@@ -106,9 +106,7 @@ module BABYLON.GLTF2 {
                 }
             }
 
-            return Promise.all(promises).then(() => {
-
-            });
+            return Promise.all(promises).then(() => { /* do nothing */ });
         }
 
         /**
@@ -330,7 +328,7 @@ module BABYLON.GLTF2 {
             if (hasTextureCoords) {
                 if (babylonStandardMaterial.diffuseTexture) {
                     let promise = _GLTFMaterial._ExportTextureAsync(babylonStandardMaterial.diffuseTexture, mimeType, images, textures, samplers, imageData, useAlpha).then(glTFTexture => {
-                        if (glTFTexture != null) {
+                        if (glTFTexture) {
                             glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
                         }
                     });
@@ -386,9 +384,7 @@ module BABYLON.GLTF2 {
 
             materials.push(glTFMaterial);
 
-            return Promise.all(promises).then(() => {
-
-            });
+            return Promise.all(promises).then(() => { /* do nothing */ });
         }
 
         /**
@@ -398,27 +394,29 @@ module BABYLON.GLTF2 {
          * @returns Promise with texture
          */
         public static _SetAlphaToOneAsync(texture: BaseTexture, useAlpha: boolean): Promise<Texture> {
-            if (useAlpha) {
-                return Promise.resolve(texture as Texture);
-            }
-            else {
-                const scene = texture.getScene();
-                if (scene == null) {
-                    return Promise.reject(`Scene not available for texture ${texture.name}`);
+            return Promise.resolve().then((() => {
+                if (useAlpha) {
+                    return Promise.resolve(texture as Texture);
                 }
                 else {
-                    const proceduralTexture = new ProceduralTexture('texture', texture.getSize(), 'setAlphaToOne', scene);
-                    if (proceduralTexture == null) {
-                        return Promise.reject(`Cannot create procedural texture for ${texture.name}!`);
+                    const scene = texture.getScene();
+                    if (scene == null) {
+                        return Promise.reject(`Scene not available for texture ${texture.name}`);
                     }
                     else {
-                        proceduralTexture.setTexture('textureSampler', texture as Texture);
-                        return scene.whenReadyAsync().then(() => {
-                            return proceduralTexture;
-                        });
+                        const proceduralTexture = new ProceduralTexture('texture', texture.getSize(), 'setAlphaToOne', scene);
+                        if (proceduralTexture == null) {
+                            return Promise.reject(`Cannot create procedural texture for ${texture.name}!`);
+                        }
+                        else {
+                            proceduralTexture.setTexture('textureSampler', texture as Texture);
+                            return scene.whenReadyAsync().then(() => {
+                                return proceduralTexture;
+                            });
+                        }
                     }
                 }
-            }
+            }));
         }
 
         /**
@@ -477,7 +475,7 @@ module BABYLON.GLTF2 {
             if (hasTextureCoords) {
                 if (babylonPBRMetalRoughMaterial.baseTexture != null) {
                     let promise = _GLTFMaterial._ExportTextureAsync(babylonPBRMetalRoughMaterial.baseTexture, mimeType, images, textures, samplers, imageData, useAlpha).then(glTFTexture => {
-                        if (glTFTexture != null) {
+                        if (glTFTexture) {
                             glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
                         }
                     });
@@ -507,7 +505,7 @@ module BABYLON.GLTF2 {
                 }
                 if (babylonPBRMetalRoughMaterial.emissiveTexture) {
                     let promise = _GLTFMaterial._ExportTextureAsync(babylonPBRMetalRoughMaterial.emissiveTexture, mimeType, images, textures, samplers, imageData, useAlpha).then(glTFTexture => {
-                        if (glTFTexture != null) {
+                        if (glTFTexture) {
                             glTFMaterial.emissiveTexture = glTFTexture;
                         }
                     });
@@ -523,8 +521,7 @@ module BABYLON.GLTF2 {
 
             materials.push(glTFMaterial);
 
-            return Promise.all(promises).then(() => {
-            });
+            return Promise.all(promises).then(() => { /* do nothing */ });
         }
 
         /**
@@ -840,7 +837,7 @@ module BABYLON.GLTF2 {
                 }
                 if (babylonPBRMaterial.metallicTexture) {
                     let promise = _GLTFMaterial._ExportTextureAsync(babylonPBRMaterial.metallicTexture, mimeType, images, textures, samplers, imageData, useAlpha).then(glTFTexture => {
-                        if (glTFTexture != null) {
+                        if (glTFTexture) {
                             glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFTexture;
                         }
                     });
@@ -1115,7 +1112,7 @@ module BABYLON.GLTF2 {
                     }
                     if (babylonPBRMaterial.emissiveTexture) {
                         let promise = _GLTFMaterial._ExportTextureAsync(babylonPBRMaterial.emissiveTexture, mimeType, images, textures, samplers, imageData, useAlpha).then(glTFTexture => {
-                            if (glTFTexture != null) {
+                            if (glTFTexture) {
                                 glTFMaterial.emissiveTexture = glTFTexture;
                             }
                         });
@@ -1129,7 +1126,7 @@ module BABYLON.GLTF2 {
                 glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
                 materials.push(glTFMaterial);
             }
-            return Promise.all(promises).then(result => { });
+            return Promise.all(promises).then(result => { /* do nothing */ });
         }
 
         private static GetPixelsFromTexture(babylonTexture: Texture): Uint8Array | Float32Array {