Consequences of disposing materials in use?

Hi there,

I’m currently investigating methods to free up memory and increase performance in our application running three.

We have started disposing materials when no longer needed and one thought struck me. What happens if one disposes a material that is actually still in use? That is, referenced in a mesh that is currently in the active scene graph.

Will that have negative impacts on performance? Such as if the shader program is released (if applicable) and then having to be re-created next render? Or will nothing happen in the case of the material being in use?

I’ve tried digging around both documentation and source code but haven’t managed to figure it out yet (and it’s a hard thing to search around for :slight_smile:)

Many thanks
BR

Potentially yes. If you dispose a material and the respective shader program is not referenced by any other materials, the shader program is deleted. If you need it next frame (because the materials is still in use), it has to be recreated from scratch.

Ok thanks!

What if I dispose a material that share it’s shader with another not-disposed material. Will nothing at all happen (since the shader program is still in use)? Are there any downsides to do this? We have certain scenarios where we would want to dispose materials but we cannot be 100% sure that a potentially cloned material (which probably shares the shader) is in the scene graph or not.

three.js internally knows how many materials share the same shader program. If you call dispose() in such a case, the program will be kept for the other materials. There are no downsides in doing this since it is the intended workflow.

1 Like

Ok great. Many thanks for your fast replies.