Pārlūkot izejas kodu

Merge pull request #2412 from bghgary/loader-fix

Add missing null checks to glTF loader
David Catuhe 8 gadi atpakaļ
vecāks
revīzija
ad887a9ce6
1 mainītis faili ar 17 papildinājumiem un 6 dzēšanām
  1. 17 6
      loaders/src/glTF/2.0/babylon.glTFLoader.ts

+ 17 - 6
loaders/src/glTF/2.0/babylon.glTFLoader.ts

@@ -112,7 +112,9 @@ module BABYLON.GLTF2 {
             this._errors = [];
             this._clear();
 
-            this._parent.onComplete();
+            if (this._parent.onComplete) {
+                this._parent.onComplete();
+            }
         }
 
         private _loadData(data: IGLTFLoaderData): void {
@@ -352,13 +354,11 @@ module BABYLON.GLTF2 {
                         this.loadMaterial(primitive.material, (babylonSubMaterial: Material) => {
                             if (this._renderReady) {
                                 babylonSubMaterial.forceCompilation(babylonMesh, babylonSubMaterial => {
-                                    babylonMultiMaterial.subMaterials[i] = babylonSubMaterial;
-                                    this._parent.onMaterialLoaded(babylonSubMaterial);
+                                    this._assignMaterial(babylonMultiMaterial, i, babylonSubMaterial);
                                 });
                             }
                             else {
-                                babylonMultiMaterial.subMaterials[i] = babylonSubMaterial;
-                                this._parent.onMaterialLoaded(babylonSubMaterial);
+                                this._assignMaterial(babylonMultiMaterial, i, babylonSubMaterial);
                             }
                         });
                     }
@@ -375,6 +375,14 @@ module BABYLON.GLTF2 {
             }
         }
 
+        private _assignMaterial(multiMaterial: MultiMaterial, index: number, subMaterial: Material): void {
+            multiMaterial.subMaterials[index] = subMaterial;
+
+            if (this._parent.onMaterialLoaded) {
+                this._parent.onMaterialLoaded(subMaterial);
+            }
+        }
+
         private _loadVertexDataAsync(primitive: IGLTFMeshPrimitive, onSuccess: (vertexData: VertexData) => void): void {
             var attributes = primitive.attributes;
             if (!attributes) {
@@ -993,7 +1001,10 @@ module BABYLON.GLTF2 {
             texture.babylonTextures = texture.babylonTextures || [];
             texture.babylonTextures[texCoord] = babylonTexture;
 
-            this._parent.onTextureLoaded(babylonTexture);
+            if (this._parent.onTextureLoaded) {
+                this._parent.onTextureLoaded(babylonTexture);
+            }
+
             return babylonTexture;
         }
     }