浏览代码

Merge pull request #2410 from bghgary/loader-updates

Loader updates
David Catuhe 8 年之前
父节点
当前提交
8e86d0d565

+ 5 - 2
loaders/src/OBJ/babylon.objFileLoader.ts

@@ -867,6 +867,9 @@ module BABYLON {
         }
 
     }
-    //Add this loader into the register plugin
-    BABYLON.SceneLoader.RegisterPlugin(new OBJFileLoader());
+
+    if (BABYLON.SceneLoader) {
+        //Add this loader into the register plugin
+        BABYLON.SceneLoader.RegisterPlugin(new OBJFileLoader());
+    }
 }

+ 3 - 1
loaders/src/STL/babylon.stlFileLoader.ts

@@ -191,5 +191,7 @@ module BABYLON {
         }
     }
 
-    BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
+    if (BABYLON.SceneLoader) {
+        BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
+    }
 }

+ 1 - 1
loaders/src/glTF/1.0/babylon.glTFLoader.ts

@@ -1731,5 +1731,5 @@ module BABYLON.GLTF1 {
         }
     };
 
-    BABYLON.GLTFFileLoader.GLTFLoaderV1 = new GLTFLoader();
+    BABYLON.GLTFFileLoader.CreateGLTFLoaderV1 = () => new GLTFLoader();
 }

+ 13 - 9
loaders/src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts

@@ -26,23 +26,27 @@ module BABYLON.GLTF2.Extensions {
 
             loader.createPbrMaterial(material);
             loader.loadMaterialBaseProperties(material);
+            this._loadSpecularGlossinessProperties(loader, material, properties);
+            assign(material.babylonMaterial);
+            return true;
+        }
+
+        private _loadSpecularGlossinessProperties(loader: GLTFLoader, material: IGLTFMaterial, properties: IKHRMaterialsPbrSpecularGlossiness): void {
+            var babylonMaterial = material.babylonMaterial as PBRMaterial;
 
-            material.babylonMaterial.albedoColor = properties.diffuseFactor ? Color3.FromArray(properties.diffuseFactor) : new Color3(1, 1, 1);
-            material.babylonMaterial.reflectivityColor = properties.specularFactor ? Color3.FromArray(properties.specularFactor) : new Color3(1, 1, 1);
-            material.babylonMaterial.microSurface = properties.glossinessFactor === undefined ? 1 : properties.glossinessFactor;
+            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.microSurface = properties.glossinessFactor === undefined ? 1 : properties.glossinessFactor;
 
             if (properties.diffuseTexture) {
-                material.babylonMaterial.albedoTexture = loader.loadTexture(properties.diffuseTexture);
+                babylonMaterial.albedoTexture = loader.loadTexture(properties.diffuseTexture);
                 loader.loadMaterialAlphaProperties(material);
             }
 
             if (properties.specularGlossinessTexture) {
-                material.babylonMaterial.reflectivityTexture = loader.loadTexture(properties.specularGlossinessTexture);
-                material.babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
+                babylonMaterial.reflectivityTexture = loader.loadTexture(properties.specularGlossinessTexture);
+                babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
             }
-
-            assign(material.babylonMaterial);
-            return true;
         }
     }
 

+ 48 - 30
loaders/src/glTF/2.0/babylon.glTFLoader.ts

@@ -2,6 +2,7 @@
 
 module BABYLON.GLTF2 {
     export class GLTFLoader implements IGLTFLoader {
+        private _parent: GLTFFileLoader;
         private _gltf: IGLTF;
         private _errors: string[];
         private _babylonScene: Scene;
@@ -40,6 +41,10 @@ module BABYLON.GLTF2 {
             return this._babylonScene;
         }
 
+        public constructor(parent: GLTFFileLoader) {
+            this._parent = parent;
+        }
+
         public importMeshAsync(meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onError: () => void): void {
             this._loadAsync(meshesNames, scene, data, rootUrl, () => {
                 var meshes = [];
@@ -106,6 +111,8 @@ module BABYLON.GLTF2 {
             this._errors.forEach(error => Tools.Error(error));
             this._errors = [];
             this._clear();
+
+            this._parent.onComplete();
         }
 
         private _loadData(data: IGLTFLoaderData): void {
@@ -346,10 +353,12 @@ module BABYLON.GLTF2 {
                             if (this._renderReady) {
                                 babylonSubMaterial.forceCompilation(babylonMesh, babylonSubMaterial => {
                                     babylonMultiMaterial.subMaterials[i] = babylonSubMaterial;
+                                    this._parent.onMaterialLoaded(babylonSubMaterial);
                                 });
                             }
                             else {
                                 babylonMultiMaterial.subMaterials[i] = babylonSubMaterial;
+                                this._parent.onMaterialLoaded(babylonSubMaterial);
                             }
                         });
                     }
@@ -360,7 +369,8 @@ module BABYLON.GLTF2 {
                         // TODO: optimize this so that sub meshes can be created without being overwritten after setting vertex data.
                         // Sub meshes must be cleared and created after setting vertex data because of mesh._createGlobalSubMesh.
                         babylonMesh.subMeshes = [];
-                        subMeshInfos.forEach(info => new SubMesh(info.materialIndex, info.verticesStart, info.verticesCount, info.indicesStart, info.indicesCount, babylonMesh));                    }
+                        subMeshInfos.forEach(info => new SubMesh(info.materialIndex, info.verticesStart, info.verticesCount, info.indicesStart, info.indicesCount, babylonMesh));
+                    }
                 });
             }
         }
@@ -811,29 +821,31 @@ module BABYLON.GLTF2 {
         }
 
         private _loadMaterialMetallicRoughnessProperties(material: IGLTFMaterial): void {
+            var babylonMaterial = material.babylonMaterial as PBRMaterial;
+
             // Ensure metallic workflow
-            material.babylonMaterial.metallic = 1;
-            material.babylonMaterial.roughness = 1;
+            babylonMaterial.metallic = 1;
+            babylonMaterial.roughness = 1;
 
             var properties = material.pbrMetallicRoughness;
             if (!properties) {
                 return;
             }
 
-            material.babylonMaterial.albedoColor = properties.baseColorFactor ? Color3.FromArray(properties.baseColorFactor) : new Color3(1, 1, 1);
-            material.babylonMaterial.metallic = properties.metallicFactor === undefined ? 1 : properties.metallicFactor;
-            material.babylonMaterial.roughness = properties.roughnessFactor === undefined ? 1 : properties.roughnessFactor;
+            babylonMaterial.albedoColor = properties.baseColorFactor ? Color3.FromArray(properties.baseColorFactor) : new Color3(1, 1, 1);
+            babylonMaterial.metallic = properties.metallicFactor === undefined ? 1 : properties.metallicFactor;
+            babylonMaterial.roughness = properties.roughnessFactor === undefined ? 1 : properties.roughnessFactor;
 
             if (properties.baseColorTexture) {
-                material.babylonMaterial.albedoTexture = this.loadTexture(properties.baseColorTexture);
+                babylonMaterial.albedoTexture = this.loadTexture(properties.baseColorTexture);
                 this.loadMaterialAlphaProperties(material);
             }
 
             if (properties.metallicRoughnessTexture) {
-                material.babylonMaterial.metallicTexture = this.loadTexture(properties.metallicRoughnessTexture);
-                material.babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
-                material.babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
-                material.babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
+                babylonMaterial.metallicTexture = this.loadTexture(properties.metallicRoughnessTexture);
+                babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
+                babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
+                babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
             }
         }
 
@@ -857,52 +869,57 @@ module BABYLON.GLTF2 {
         }
 
         public createPbrMaterial(material: IGLTFMaterial): void {
-            material.babylonMaterial = new PBRMaterial(material.name || "mat" + material.index, this._babylonScene);
-            material.babylonMaterial.sideOrientation = Material.CounterClockWiseSideOrientation;
-            material.babylonMaterial.useScalarInLinearSpace = true;
+            var babylonMaterial = new PBRMaterial(material.name || "mat" + material.index, this._babylonScene);
+            babylonMaterial.sideOrientation = Material.CounterClockWiseSideOrientation;
+            babylonMaterial.useScalarInLinearSpace = true;
+            material.babylonMaterial = babylonMaterial;
         }
 
         public loadMaterialBaseProperties(material: IGLTFMaterial): void {
-            material.babylonMaterial.useEmissiveAsIllumination = (material.emissiveFactor || material.emissiveTexture) ? true : false;
-            material.babylonMaterial.emissiveColor = material.emissiveFactor ? Color3.FromArray(material.emissiveFactor) : new Color3(0, 0, 0);
+            var babylonMaterial = material.babylonMaterial as PBRMaterial;
+
+            babylonMaterial.useEmissiveAsIllumination = (material.emissiveFactor || material.emissiveTexture) ? true : false;
+            babylonMaterial.emissiveColor = material.emissiveFactor ? Color3.FromArray(material.emissiveFactor) : new Color3(0, 0, 0);
             if (material.doubleSided) {
-                material.babylonMaterial.backFaceCulling = false;
-                material.babylonMaterial.twoSidedLighting = true;
+                babylonMaterial.backFaceCulling = false;
+                babylonMaterial.twoSidedLighting = true;
             }
 
             if (material.normalTexture) {
-                material.babylonMaterial.bumpTexture = this.loadTexture(material.normalTexture);
+                babylonMaterial.bumpTexture = this.loadTexture(material.normalTexture);
                 if (material.normalTexture.scale !== undefined) {
-                    material.babylonMaterial.bumpTexture.level = material.normalTexture.scale;
+                    babylonMaterial.bumpTexture.level = material.normalTexture.scale;
                 }
             }
 
             if (material.occlusionTexture) {
-                material.babylonMaterial.ambientTexture = this.loadTexture(material.occlusionTexture);
-                material.babylonMaterial.useAmbientInGrayScale = true;
+                babylonMaterial.ambientTexture = this.loadTexture(material.occlusionTexture);
+                babylonMaterial.useAmbientInGrayScale = true;
                 if (material.occlusionTexture.strength !== undefined) {
-                    material.babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
+                    babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
                 }
             }
 
             if (material.emissiveTexture) {
-                material.babylonMaterial.emissiveTexture = this.loadTexture(material.emissiveTexture);
+                babylonMaterial.emissiveTexture = this.loadTexture(material.emissiveTexture);
             }
         }
 
         public loadMaterialAlphaProperties(material: IGLTFMaterial): void {
+            var babylonMaterial = material.babylonMaterial as PBRMaterial;
+
             var alphaMode = material.alphaMode || "OPAQUE";
             switch (alphaMode) {
                 case "OPAQUE":
                     // default is opaque
                     break;
                 case "MASK":
-                    material.babylonMaterial.albedoTexture.hasAlpha = true;
-                    material.babylonMaterial.useAlphaFromAlbedoTexture = false;
+                    babylonMaterial.albedoTexture.hasAlpha = true;
+                    babylonMaterial.useAlphaFromAlbedoTexture = false;
                     break;
                 case "BLEND":
-                    material.babylonMaterial.albedoTexture.hasAlpha = true;
-                    material.babylonMaterial.useAlphaFromAlbedoTexture = true;
+                    babylonMaterial.albedoTexture.hasAlpha = true;
+                    babylonMaterial.useAlphaFromAlbedoTexture = true;
                     break;
                 default:
                     Tools.Warn("Invalid alpha mode '" + material.alphaMode + "'");
@@ -970,15 +987,16 @@ module BABYLON.GLTF2 {
             babylonTexture.coordinatesIndex = texCoord;
             babylonTexture.wrapU = GLTFUtils.GetWrapMode(sampler.wrapS);
             babylonTexture.wrapV = GLTFUtils.GetWrapMode(sampler.wrapT);
-            babylonTexture.name = texture.name;
+            babylonTexture.name = texture.name || "texture" + textureInfo.index;
 
             // Cache the texture
             texture.babylonTextures = texture.babylonTextures || [];
             texture.babylonTextures[texCoord] = babylonTexture;
 
+            this._parent.onTextureLoaded(babylonTexture);
             return babylonTexture;
         }
     }
 
-    BABYLON.GLTFFileLoader.GLTFLoaderV2 = new GLTFLoader();
+    BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = parent => new GLTFLoader(parent);
 }

+ 1 - 1
loaders/src/glTF/2.0/babylon.glTFLoaderInterfaces.ts

@@ -185,7 +185,7 @@ module BABYLON.GLTF2 {
 
         // Runtime values
         index?: number;
-        babylonMaterial?: PBRMaterial;
+        babylonMaterial?: Material;
     }
 
     export interface IGLTFMeshPrimitive extends IGLTFProperty {

+ 18 - 15
loaders/src/glTF/babylon.glTFFileLoader.ts

@@ -7,17 +7,23 @@ module BABYLON {
     }
 
     export interface IGLTFLoader {
-        importMeshAsync: (meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onsuccess: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onerror: () => void) => void;
-        loadAsync: (scene: Scene, data: IGLTFLoaderData, rootUrl: string, onsuccess: () => void, onerror: () => void) => void;
+        importMeshAsync: (meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onError: () => void) => void;
+        loadAsync: (scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onError: () => void) => void;
     }
 
     export class GLTFFileLoader implements ISceneLoaderPluginAsync {
-        public static GLTFLoaderV1: IGLTFLoader = null;
-        public static GLTFLoaderV2: IGLTFLoader = null;
+        public static CreateGLTFLoaderV1: (parent: GLTFFileLoader) => IGLTFLoader;
+        public static CreateGLTFLoaderV2: (parent: GLTFFileLoader) => IGLTFLoader;
 
+        // V1 options
         public static HomogeneousCoordinates: boolean = false;
         public static IncrementalLoading: boolean = true;
 
+        // V2 options
+        public onTextureLoaded: (texture: BaseTexture) => void;
+        public onMaterialLoaded: (material: Material) => void;
+        public onComplete: () => void;
+
         public extensions: ISceneLoaderPluginExtensions = {
             ".gltf": { isBinary: false },
             ".glb": { isBinary: true }
@@ -79,19 +85,14 @@ module BABYLON {
                 }
             }
 
-            var loaders = {
-                1: GLTFFileLoader.GLTFLoaderV1,
-                2: GLTFFileLoader.GLTFLoaderV2
+            var createLoader = {
+                1: GLTFFileLoader.CreateGLTFLoaderV1,
+                2: GLTFFileLoader.CreateGLTFLoaderV2
             };
 
-            var loader = loaders[version.major];
-            if (loader === undefined) {
-                Tools.Error("Unsupported version");
-                return null;
-            }
-
+            var loader = createLoader[version.major](this);
             if (loader === null) {
-                Tools.Error("v" + version.major + " loader is not available");
+                Tools.Error("Unsupported version");
                 return null;
             }
 
@@ -279,5 +280,7 @@ module BABYLON {
         }
     }
 
-    BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
+    if (BABYLON.SceneLoader) {
+        BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
+    }
 }