浏览代码

More glTF loader error handling

Gary Hsu 7 年之前
父节点
当前提交
3bfe36d362

+ 18 - 8
loaders/src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts

@@ -14,33 +14,43 @@ module BABYLON.GLTF2.Extensions {
             return "KHR_materials_pbrSpecularGlossiness";
             return "KHR_materials_pbrSpecularGlossiness";
         }
         }
 
 
-        protected _loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
+        protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
             return this._loadExtension<IKHRMaterialsPbrSpecularGlossiness>(material, (extension, onComplete) => {
             return this._loadExtension<IKHRMaterialsPbrSpecularGlossiness>(material, (extension, onComplete) => {
                 loader._createPbrMaterial(material);
                 loader._createPbrMaterial(material);
-                loader._loadMaterialBaseProperties(material);
-                this._loadSpecularGlossinessProperties(loader, material, extension);
+                loader._loadMaterialBaseProperties(context, material);
+                this._loadSpecularGlossinessProperties(loader, context, material, extension);
                 assign(material.babylonMaterial, true);
                 assign(material.babylonMaterial, true);
             });
             });
         }
         }
 
 
-        private _loadSpecularGlossinessProperties(loader: GLTFLoader, material: IGLTFMaterial, properties: IKHRMaterialsPbrSpecularGlossiness): void {
+        private _loadSpecularGlossinessProperties(loader: GLTFLoader, context: string, material: IGLTFMaterial, properties: IKHRMaterialsPbrSpecularGlossiness): void {
             var babylonMaterial = material.babylonMaterial as PBRMaterial;
             var babylonMaterial = material.babylonMaterial as PBRMaterial;
 
 
             babylonMaterial.albedoColor = properties.diffuseFactor ? Color3.FromArray(properties.diffuseFactor) : new Color3(1, 1, 1);
             babylonMaterial.albedoColor = properties.diffuseFactor ? Color3.FromArray(properties.diffuseFactor) : new Color3(1, 1, 1);
             babylonMaterial.reflectivityColor = properties.specularFactor ? Color3.FromArray(properties.specularFactor) : new Color3(1, 1, 1);
             babylonMaterial.reflectivityColor = properties.specularFactor ? Color3.FromArray(properties.specularFactor) : new Color3(1, 1, 1);
-            babylonMaterial.microSurface = properties.glossinessFactor === undefined ? 1 : properties.glossinessFactor;
+            babylonMaterial.microSurface = properties.glossinessFactor == null ? 1 : properties.glossinessFactor;
 
 
             if (properties.diffuseTexture) {
             if (properties.diffuseTexture) {
-                babylonMaterial.albedoTexture = loader._loadTexture(properties.diffuseTexture);
+                var texture = GLTFUtils.GetArrayItem(loader._gltf.textures, properties.diffuseTexture.index);
+                if (!texture) {
+                    throw new Error(context + ": Failed to find diffuse texture " + properties.diffuseTexture.index);
+                }
+
+                babylonMaterial.albedoTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.diffuseTexture.texCoord);
             }
             }
 
 
             if (properties.specularGlossinessTexture) {
             if (properties.specularGlossinessTexture) {
-                babylonMaterial.reflectivityTexture = loader._loadTexture(properties.specularGlossinessTexture);
+                var texture = GLTFUtils.GetArrayItem(loader._gltf.textures, properties.specularGlossinessTexture.index);
+                if (!texture) {
+                    throw new Error(context + ": Failed to find diffuse texture " + properties.specularGlossinessTexture.index);
+                }
+
+                babylonMaterial.reflectivityTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.specularGlossinessTexture.texCoord);
                 babylonMaterial.reflectivityTexture.hasAlpha = true;
                 babylonMaterial.reflectivityTexture.hasAlpha = true;
                 babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
                 babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
             }
             }
 
 
-            loader._loadMaterialAlphaProperties(material, properties.diffuseFactor);
+            loader._loadMaterialAlphaProperties(context, material, properties.diffuseFactor);
         }
         }
     }
     }
 
 

+ 18 - 18
loaders/src/glTF/2.0/Extensions/MSFT_lod.ts

@@ -16,37 +16,37 @@ module BABYLON.GLTF2.Extensions {
             return "MSFT_lod";
             return "MSFT_lod";
         }
         }
 
 
-        protected _traverseNode(loader: GLTFLoader, index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean {
-            var node = loader._getArrayItem(loader._gltf.nodes, index, "Node");
-            if (!node) {
-                return true;
-            }
-
+        protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean {
             return this._loadExtension<IMSFTLOD>(node, (extension, onComplete) => {
             return this._loadExtension<IMSFTLOD>(node, (extension, onComplete) => {
                 for (var i = extension.ids.length - 1; i >= 0; i--) {
                 for (var i = extension.ids.length - 1; i >= 0; i--) {
-                    loader._traverseNode(extension.ids[i], action, parentNode);
+                    var lodNode = GLTFUtils.GetArrayItem(loader._gltf.nodes, extension.ids[i]);
+                    if (!lodNode) {
+                        throw new Error(context + ": Failed to find node " + extension.ids[i]);
+                    }
+
+                    loader._traverseNode(context, lodNode, action, parentNode);
                 }
                 }
 
 
-                loader._traverseNode(index, action, parentNode);
+                loader._traverseNode(context, node, action, parentNode);
                 onComplete();
                 onComplete();
             });
             });
         }
         }
 
 
-        protected _loadNode(loader: GLTFLoader, node: IGLTFNode): boolean {
+        protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean {
             return this._loadExtension<IMSFTLOD>(node, (extension, onComplete) => {
             return this._loadExtension<IMSFTLOD>(node, (extension, onComplete) => {
                 var nodes = [node.index, ...extension.ids].map(index => loader._gltf.nodes[index]);
                 var nodes = [node.index, ...extension.ids].map(index => loader._gltf.nodes[index]);
 
 
                 loader._addLoaderPendingData(node);
                 loader._addLoaderPendingData(node);
-                this._loadNodeLOD(loader, nodes, nodes.length - 1, () => {
+                this._loadNodeLOD(loader, context, nodes, nodes.length - 1, () => {
                     loader._removeLoaderPendingData(node);
                     loader._removeLoaderPendingData(node);
                     onComplete();
                     onComplete();
                 });
                 });
             });
             });
         }
         }
 
 
-        private _loadNodeLOD(loader: GLTFLoader, nodes: IGLTFNode[], index: number, onComplete: () => void): void {
+        private _loadNodeLOD(loader: GLTFLoader, context: string, nodes: IGLTFNode[], index: number, onComplete: () => void): void {
             loader._whenAction(() => {
             loader._whenAction(() => {
-                loader._loadNode(nodes[index]);
+                loader._loadNode(context, nodes[index]);
             }, () => {
             }, () => {
                 if (index !== nodes.length - 1) {
                 if (index !== nodes.length - 1) {
                     var previousNode = nodes[index + 1];
                     var previousNode = nodes[index + 1];
@@ -59,17 +59,17 @@ module BABYLON.GLTF2.Extensions {
                 }
                 }
 
 
                 setTimeout(() => {
                 setTimeout(() => {
-                    this._loadNodeLOD(loader, nodes, index - 1, onComplete);
+                    this._loadNodeLOD(loader, context, nodes, index - 1, onComplete);
                 }, MSFTLOD.MinimalLODDelay);
                 }, MSFTLOD.MinimalLODDelay);
             });
             });
         }
         }
 
 
-        protected _loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
+        protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
             return this._loadExtension<IMSFTLOD>(material, (extension, onComplete) => {
             return this._loadExtension<IMSFTLOD>(material, (extension, onComplete) => {
                 var materials = [material.index, ...extension.ids].map(index => loader._gltf.materials[index]);
                 var materials = [material.index, ...extension.ids].map(index => loader._gltf.materials[index]);
 
 
                 loader._addLoaderPendingData(material);
                 loader._addLoaderPendingData(material);
-                this._loadMaterialLOD(loader, materials, materials.length - 1, assign, () => {
+                this._loadMaterialLOD(loader, context, materials, materials.length - 1, assign, () => {
                     material.extensions[this.name] = extension;
                     material.extensions[this.name] = extension;
                     loader._removeLoaderPendingData(material);
                     loader._removeLoaderPendingData(material);
                     onComplete();
                     onComplete();
@@ -77,8 +77,8 @@ module BABYLON.GLTF2.Extensions {
             });
             });
         }
         }
 
 
-        private _loadMaterialLOD(loader: GLTFLoader, materials: IGLTFMaterial[], index: number, assign: (babylonMaterial: Material, isNew: boolean) => void, onComplete: () => void): void {
-            loader._loadMaterial(materials[index], (babylonMaterial, isNew) => {
+        private _loadMaterialLOD(loader: GLTFLoader, context: string, materials: IGLTFMaterial[], index: number, assign: (babylonMaterial: Material, isNew: boolean) => void, onComplete: () => void): void {
+            loader._loadMaterial(context, materials[index], (babylonMaterial, isNew) => {
                 assign(babylonMaterial, isNew);
                 assign(babylonMaterial, isNew);
 
 
                 if (index === 0) {
                 if (index === 0) {
@@ -91,7 +91,7 @@ module BABYLON.GLTF2.Extensions {
                 loader._executeWhenRenderReady(() => {
                 loader._executeWhenRenderReady(() => {
                     BaseTexture.WhenAllReady(babylonMaterial.getActiveTextures(), () => {
                     BaseTexture.WhenAllReady(babylonMaterial.getActiveTextures(), () => {
                         setTimeout(() => {
                         setTimeout(() => {
-                            this._loadMaterialLOD(loader, materials, index - 1, assign, onComplete);
+                            this._loadMaterialLOD(loader, context, materials, index - 1, assign, onComplete);
                         }, MSFTLOD.MinimalLODDelay);
                         }, MSFTLOD.MinimalLODDelay);
                     });
                     });
                 });
                 });

文件差异内容过多而无法显示
+ 380 - 369
loaders/src/glTF/2.0/babylon.glTFLoader.ts


+ 9 - 9
loaders/src/glTF/2.0/babylon.glTFLoaderExtension.ts

@@ -6,11 +6,11 @@ module BABYLON.GLTF2 {
 
 
         public abstract get name(): string;
         public abstract get name(): string;
 
 
-        protected _traverseNode(loader: GLTFLoader, index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean { return false; }
+        protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean { return false; }
 
 
-        protected _loadNode(loader: GLTFLoader, node: IGLTFNode): boolean { return false; }
+        protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean { return false; }
 
 
-        protected _loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean { return false; }
+        protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean { return false; }
 
 
         protected _loadExtension<T>(property: IGLTFProperty, action: (extension: T, onComplete: () => void) => void): boolean {
         protected _loadExtension<T>(property: IGLTFProperty, action: (extension: T, onComplete: () => void) => void): boolean {
             if (!property.extensions) {
             if (!property.extensions) {
@@ -39,16 +39,16 @@ module BABYLON.GLTF2 {
 
 
         public static _Extensions: GLTFLoaderExtension[] = [];
         public static _Extensions: GLTFLoaderExtension[] = [];
 
 
-        public static TraverseNode(loader: GLTFLoader, index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean {
-            return this._ApplyExtensions(extension => extension._traverseNode(loader, index, action, parentNode));
+        public static TraverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean {
+            return this._ApplyExtensions(extension => extension._traverseNode(loader, context, node, action, parentNode));
         }
         }
 
 
-        public static LoadNode(loader: GLTFLoader, node: IGLTFNode): boolean {
-            return this._ApplyExtensions(extension => extension._loadNode(loader, node));
+        public static LoadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean {
+            return this._ApplyExtensions(extension => extension._loadNode(loader, context, node));
         }
         }
 
 
-        public static LoadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
-            return this._ApplyExtensions(extension => extension._loadMaterial(loader, material, assign));
+        public static LoadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
+            return this._ApplyExtensions(extension => extension._loadMaterial(loader, context, material, assign));
         }
         }
 
 
         private static _ApplyExtensions(action: (extension: GLTFLoaderExtension) => boolean) {
         private static _ApplyExtensions(action: (extension: GLTFLoaderExtension) => boolean) {

+ 20 - 2
loaders/src/glTF/2.0/babylon.glTFLoaderInterfaces.ts

@@ -82,6 +82,9 @@ module BABYLON.GLTF2 {
         max: number[];
         max: number[];
         min: number[];
         min: number[];
         sparse?: IGLTFAccessorSparse;
         sparse?: IGLTFAccessorSparse;
+
+        // Runtime values
+        index?: number;
     }
     }
 
 
     export interface IGLTFAnimationChannel extends IGLTFProperty {
     export interface IGLTFAnimationChannel extends IGLTFProperty {
@@ -105,6 +108,7 @@ module BABYLON.GLTF2 {
         samplers: IGLTFAnimationSampler[];
         samplers: IGLTFAnimationSampler[];
 
 
         // Runtime values
         // Runtime values
+        index?: number;
         targets?: any[];
         targets?: any[];
     }
     }
 
 
@@ -120,8 +124,9 @@ module BABYLON.GLTF2 {
         byteLength: number;
         byteLength: number;
 
 
         // Runtime values
         // Runtime values
-        loadedData: ArrayBufferView;
-        loadedObservable: Observable<IGLTFBuffer>;
+        index?: number;
+        loadedData?: ArrayBufferView;
+        loadedObservable?: Observable<IGLTFBuffer>;
     }
     }
 
 
     export interface IGLTFBufferView extends IGLTFChildRootProperty {
     export interface IGLTFBufferView extends IGLTFChildRootProperty {
@@ -129,6 +134,9 @@ module BABYLON.GLTF2 {
         byteOffset?: number;
         byteOffset?: number;
         byteLength: number;
         byteLength: number;
         byteStride?: number;
         byteStride?: number;
+
+        // Runtime values
+        index?: number;
     }
     }
 
 
     export interface IGLTFCameraOrthographic extends IGLTFProperty {
     export interface IGLTFCameraOrthographic extends IGLTFProperty {
@@ -155,6 +163,9 @@ module BABYLON.GLTF2 {
         uri?: string;
         uri?: string;
         mimeType?: string;
         mimeType?: string;
         bufferView?: number;
         bufferView?: number;
+
+        // Runtime values
+        index?: number;
     }
     }
 
 
     export interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
     export interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
@@ -199,6 +210,9 @@ module BABYLON.GLTF2 {
     export interface IGLTFMesh extends IGLTFChildRootProperty {
     export interface IGLTFMesh extends IGLTFChildRootProperty {
         primitives: IGLTFMeshPrimitive[];
         primitives: IGLTFMeshPrimitive[];
         weights?: number[];
         weights?: number[];
+
+        // Runtime values
+        index?: number;
     }
     }
 
 
     export interface IGLTFNode extends IGLTFChildRootProperty {
     export interface IGLTFNode extends IGLTFChildRootProperty {
@@ -229,6 +243,9 @@ module BABYLON.GLTF2 {
 
 
     export interface IGLTFScene extends IGLTFChildRootProperty {
     export interface IGLTFScene extends IGLTFChildRootProperty {
         nodes: number[];
         nodes: number[];
+
+        // Runtime values
+        index?: number;
     }
     }
 
 
     export interface IGLTFSkin extends IGLTFChildRootProperty {
     export interface IGLTFSkin extends IGLTFChildRootProperty {
@@ -246,6 +263,7 @@ module BABYLON.GLTF2 {
         source: number;
         source: number;
 
 
         // Runtime values
         // Runtime values
+        index?: number;
         url?: string;
         url?: string;
         dataReadyObservable?: Observable<IGLTFTexture>;
         dataReadyObservable?: Observable<IGLTFTexture>;
     }
     }

+ 20 - 0
loaders/src/glTF/2.0/babylon.glTFLoaderUtils.ts

@@ -35,6 +35,26 @@ module BABYLON.GLTF2 {
             }
             }
         }
         }
 
 
+        public static ValidateUri(uri: string): boolean {
+            return (uri.indexOf("..") === -1);
+        }
+
+        public static AssignIndices(array: Array<{index?: number}>): void {
+            if (array) {
+                for (var index = 0; index < array.length; index++) {
+                    array[index].index = index;
+                }
+            }
+        }
+
+        public static GetArrayItem<T>(array: ArrayLike<T>, index: number): T {
+            if (!array || !array[index]) {
+                return null;
+            }
+
+            return array[index];
+        }
+
         public static GetTextureWrapMode(mode: ETextureWrapMode): number {
         public static GetTextureWrapMode(mode: ETextureWrapMode): number {
             // Set defaults if undefined
             // Set defaults if undefined
             mode = mode === undefined ? ETextureWrapMode.REPEAT : mode;
             mode = mode === undefined ? ETextureWrapMode.REPEAT : mode;