Selaa lähdekoodia

getMaterialByID should return the most recently created material with that id

Trevor Baron 6 vuotta sitten
vanhempi
commit
ba081f8ea2
2 muutettua tiedostoa jossa 6 lisäystä ja 2 poistoa
  1. 1 0
      dist/preview release/what's new.md
  2. 5 2
      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))

+ 5 - 2
src/scene.ts

@@ -2434,12 +2434,15 @@ export class Scene extends AbstractScene implements IAnimatable {
     }
 
     /**
-     * get a material using its id
+     * get a material using its id (If multiple materials have the same id the last one will be returned)
      * @param id defines the material's ID
      * @return the material or null if none found.
      */
     public getMaterialByID(id: string): Nullable<Material> {
-        for (var index = 0; index < this.materials.length; index++) {
+        // When loading a multimaterial it will lookup its materials by id, if the same multimaterial is loaded twice
+        // the 2nd multimaterial needs to reference the latest material by that id which is why this lookup will return
+        // the last material with that id instead of the first
+        for (var index = this.materials.length - 1; index >= 0; index--) {
             if (this.materials[index].id === id) {
                 return this.materials[index];
             }