Jump to content

Explicit disposing of materials necessary?


Babylonian
 Share

Recommended Posts

I'm working on a project where a large number of objects get created, then are destroyed (with .dispose()), on an ongoing basis. Using latest code from the version builder having learned my lesson :-)

I see the number of materials (in scene.materials) gradually get larger and larger. And the browser using more and more memory.... So it looks like the materials are not being dispose()'ed of when the mesh that uses them is disposed.

I'm using the construct from the examples, i.e.

var mesh = new BABYLON.Mesh.CreateXXX(...)
mesh.material = new BABYLON.StandardMaterial(...)

EDIT: It does NOT always seem to help to explicitly dispose of the material before the mesh, e.g.

mesh.material.dispose();
mesh.dispose();

..and it has some side-effects if a material is used by another mesh.

But should that be necessary?

Also, can it help to store a single material that can be used by a number of meshes? Or does the material data get duplicated when assigned to a mesh in any case?
Cheers...

Edited by Babylonian
did some more testing...
Link to comment
Share on other sites

Yes, materials and meshes are different assets with different lifecycles, and disposing one does not automatically dispose the other. Babylon will not duplicate resources if you reuse one material for lots of meshes.

As such, code like your sample code is only necessary if all your meshes need their own unique materials. Otherwise, it's better to create only the materials you need and keep them, even as meshes get dynamically created and disposed.

Link to comment
Share on other sites

Adding to the wonderful answer before me - 

If you dispose a material, and there are "side effects" it means you shouldn't dispose it :). It is being used somewhere else.

Pay attention, that just like mesh-material disposing (mesh.dispose() doesn't automatically dispose the material), textures are not disposed when you dispose the material. You have to manually follow this, or force texture dispose, which might result in the same side effects you are experiencing when disposing materials (or worse). 

A quick rule of thumb - reuse as much as possible, but try to do it at the lowest level. If you have 5 meshes, and they all use the same diffuse texture, but each has a different bump textures, these are:

5 meshes that can be disposed individually, 5 materials that can be disposed individually, 5 bump textures that should be disposed when not used, 1(!) diffuse texture that should only disposed when not used.

Another thing to notice - when creating a texture from the same URL, Babylon will reuse the WebGL texture buffer so the GPU is using the same memory. The texture object itself is NOT the same. so, calling 5 times new Texture("sameUrl.jpg"...) will create 5 objects, all pointing at the same WebGL buffer. If you dispose one of them, the GPU RAM will be cleared, but the other 4 will still reference it. Babylon will try doing its best to prevent you from doing that (it counts the number of uses of each buffer), but if you try really hard, you will break it. 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...