Просмотр исходного кода

Added the loadAllMaterials property (#8680)

Popov72 5 лет назад
Родитель
Сommit
ac1bb8a175

+ 1 - 0
dist/preview release/what's new.md

@@ -118,6 +118,7 @@
 - Added missing "pluginExtension" parameter to SceneLoader.ImportAnimations. ([phenry20](https://github.com/phenry20))
 - Added support for .glb file loading through a base64 encoded filename ([Popov72](https://github.com/Popov72))
 - Fixed issue with loading screen hiding too early when loading multiple assets concurrently. ([bghgary](https://github.com/bghgary))
+- Added the `loadAllMaterials` property to the gLTF loader to load materials even if not used by any mesh ([Popov72](https://github.com/Popov72))
 
 ### Serializers
 

+ 33 - 0
loaders/src/glTF/2.0/glTFLoader.ts

@@ -309,6 +309,39 @@ export class GLTFLoader implements IGLTFLoader {
                 promises.push(this.loadSceneAsync(`/scenes/${scene.index}`, scene));
             }
 
+            if (this.parent.loadAllMaterials && this._gltf.materials) {
+                for (let m = 0; m < this._gltf.materials.length; ++m) {
+                    const material = this._gltf.materials[m];
+                    const context = "/materials/" + m;
+                    const babylonDrawMode = Material.TriangleFillMode;
+                    let babylonData = material._data ? material._data[babylonDrawMode] : null;
+
+                    if (babylonData) {
+                        continue;
+                    }
+
+                    this.logOpen(`${context} ${material.name || ""}`);
+
+                    const babylonMaterial = this.createMaterial(context, material, babylonDrawMode);
+
+                    babylonData = {
+                        babylonMaterial: babylonMaterial,
+                        babylonMeshes: [],
+                        promise: this.loadMaterialPropertiesAsync(context, material, babylonMaterial)
+                    };
+
+                    promises.push(babylonData.promise);
+
+                    material._data = material._data ?? {};
+                    material._data[babylonDrawMode] = babylonData;
+
+                    GLTFLoader.AddPointerMetadata(babylonMaterial, context);
+                    this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
+
+                    this.logClose();
+                }
+            }
+
             // Restore the blocking of material dirty.
             this._babylonScene.blockMaterialDirtyMechanism = oldBlockMaterialDirtyMechanism;
 

+ 5 - 0
loaders/src/glTF/glTFFileLoader.ts

@@ -240,6 +240,11 @@ export class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISc
     public alwaysComputeBoundingBox = false;
 
     /**
+     * If true, load all materials defined in the file, even if not used by any mesh. Defaults to false.
+     */
+    public loadAllMaterials = false;
+
+    /**
      * Function called before loading a url referenced by the asset.
      */
     public preprocessUrlAsync = (url: string) => Promise.resolve(url);