I have multiple models, click the button to switch models. When I switch models, the material of the previous model will pollute the next model. Why is this?

2.

normal should be this

This might have to do with how your are replacing your models? If you are replacing the model, wouldn’t you have to replace the material too?

Another solution could be to have all the models loaded and just show/hide the active/non-active ones. This will be expensive up front since you have to load in all the models. Or you can load them only once they are first used.

Would be able to isolate what you are doing in a playground so we can a further look? :slight_smile:

Changing the model also has to change the material, a model comes with its own material

[image]

It feels like a cache, is there a way to clear the cache of the previous model, or destroy the previous model directly

You can dispose a mesh and then replace it with a new one. This might be expensive though?

Can you give me an example

[image] :stuck_out_tongue_winking_eye:

Sure let me give it a shot. :+1:

thank you very much :heart:

model : https://dochub-mft.oss-cn-hangzhou.aliyuncs.com/狮子家-盾牌.gltf

model : https://dochub-mft.oss-cn-hangzhou.aliyuncs.com/粉红面具.gltf

you can use these model

Alrighty! I have a basic demo working. I went with simple models to get the solution in a generic form. This will also isolate the issue for debugging.

https://playground.babylonjs.com/#CBGEQX#700

In this solution, I have a variable of the mainMesh, this is the mesh that is visable.
var mainMesh = null; Then I load in a mesh (in this case the skull) and set the mainMesh to it.

Next I create 2 buttons using the Babylon GUI. When each button is pressed I call: mainMesh.dispose(); Then I load in a new mesh and replace mainMesh with the new mesh. And repeat (lines 40-53 and lines 55-66)

Something interesting I noticed is that after loading each mesh for the first time, each load afterwards was relatively quick. Guess there is some caching happening. :joy:

2 Likes

thanks for your help,this demo resolve my problem,thank you. :heart:

1 Like