Browse Source

Merge pull request #6516 from TrevorDev/disposeMultiMaterialFix

getMaterialByID should return the most recently created material with…
David Catuhe 6 years ago
parent
commit
47498b5ca1
3 changed files with 19 additions and 1 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 3 1
      src/Materials/multiMaterial.ts
  3. 15 0
      src/scene.ts

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

@@ -78,6 +78,7 @@
 - Avoid using default utility layer in gizmo manager to support multiple scenes ([TrevorDev](https://github.com/TrevorDev))
 - Fix bug when adding and removing observers in quick succession ([sable](https://github.com/thscott))
 - Cannon and Ammo forceUpdate will no longer cause an unexpected exception ([TrevorDev](https://github.com/TrevorDev))
+- Loading the same multi-material twice and disposing one should not impact the other ([TrevorDev](https://github.com/TrevorDev))
 
 ## Breaking changes
 - Setting mesh.scaling to a new vector will no longer automatically call forceUpdate (this should be done manually when needed) ([TrevorDev](https://github.com/TrevorDev))

+ 3 - 1
src/Materials/multiMaterial.ts

@@ -235,7 +235,9 @@ export class MultiMaterial extends Material {
             var subMatId = parsedMultiMaterial.materials[matIndex];
 
             if (subMatId) {
-                multiMaterial.subMaterials.push(scene.getMaterialByID(subMatId));
+                // If the same multimaterial is loaded twice, the 2nd multimaterial needs to reference the latest material by that id which
+                // is why this lookup should use getLastMaterialByID instead of getMaterialByID
+                multiMaterial.subMaterials.push(scene.getLastMaterialByID(subMatId));
             } else {
                 multiMaterial.subMaterials.push(null);
             }

+ 15 - 0
src/scene.ts

@@ -2449,6 +2449,21 @@ export class Scene extends AbstractScene implements IAnimatable {
     }
 
     /**
+     * Gets a the last added material using a given id
+     * @param id defines the material's ID
+     * @return the last material with the given id or null if none found.
+     */
+    public getLastMaterialByID(id: string): Nullable<Material> {
+        for (var index = this.materials.length - 1; index >= 0; index--) {
+            if (this.materials[index].id === id) {
+                return this.materials[index];
+            }
+        }
+
+        return null;
+    }
+
+    /**
      * Gets a material using its name
      * @param name defines the material's name
      * @return the material or null if none found.