瀏覽代碼

add texture/materials to assetContainer

Trevor Baron 6 年之前
父節點
當前提交
016b4b7093
共有 2 個文件被更改,包括 14 次插入0 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 13 0
      loaders/src/glTF/glTFFileLoader.ts

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

@@ -207,6 +207,7 @@
 - Add missing dependencies for files to support including them from a direct path (eg. import "@babylonjs/core/Helpers/sceneHelpers";) ([TrevorDev](https://github.com/TrevorDev))
 - AssetContainer should not dispose objects it doesn't contain, Support for environmentTexture ([TrevorDev](https://github.com/TrevorDev))
 - Fix `mesh.visibility` not working properly when certain material properties are set that changes the interpretation of alpha (e.g. refraction, specular over alpha, etc.) ([bghgary](https://github.com/bghgary))
+- Fix material and texture leak when loading/removing GLTF with AssetContainer ([TrevorDev](https://github.com/TrevorDev))
 
 ### Core Engine
 - Fixed a bug with `mesh.alwaysSelectAsActiveMesh` preventing layerMask to be taken in account ([Deltakosh](https://github.com/deltakosh))

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

@@ -503,12 +503,25 @@ export class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISc
         return this._parseAsync(scene, data, rootUrl, fileName).then((loaderData) => {
             this._log(`Loading ${fileName || ""}`);
             this._loader = this._getLoader(loaderData);
+
+            // Get materials/textures when loading to add to container
+            let materials: Array<Material> = [];
+            this.onMaterialLoadedObservable.add((material) => {
+                materials.push(material);
+            });
+            let textures: Array<BaseTexture> = [];
+            this.onTextureLoadedObservable.add((texture) => {
+                textures.push(texture);
+            });
+
             return this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress, fileName).then((result) => {
                 const container = new AssetContainer(scene);
                 Array.prototype.push.apply(container.meshes, result.meshes);
                 Array.prototype.push.apply(container.particleSystems, result.particleSystems);
                 Array.prototype.push.apply(container.skeletons, result.skeletons);
                 Array.prototype.push.apply(container.animationGroups, result.animationGroups);
+                Array.prototype.push.apply(container.materials, materials);
+                Array.prototype.push.apply(container.textures, textures);
                 container.removeAllFromScene();
                 return container;
             });