Mesh.dispose() and intersectsMesh()

Here is original playground from Documentation showing mesh intersections - Babylon.js Playground

Let dispose all meshes except baloons:

https://playground.babylonjs.com/#KQV9SA#135

Still baloons change their colors when intersecting the place of the disposed mesh.

So the question is: how to properly dispose meshes in this case in order they wouldnt show intersections again after their disposal?

Looking around and I’m scratching my head on this myself. My solution to this was to simply keep track of meshes you are deleting. I think just because you delete the mesh doesn’t mean the object’s memory (aka the position and physics) is gone.

https://playground.babylonjs.com/#KQV9SA#136

I then set the deleted object to null and do a check before the collision detection function. This seems a little hacky to me so is there a better way to do this? @sebavan

1 Like

When intersecting only the bounding info and couple more are check to speed it up, in your case, you need to manage the list on your side adding extra checks and so on would not fit all use cases and would in the end bloat the engine. Internally in action manager for instance, we check the mesh against the other ones of the scene, and ondispose remove them from the scene which is exactly the same.

As Inspector shows, there are no meshes to intersect. Seems that bounding info is not deleted after mesh disposal. Is there some way to delete this bounding info (or maybe there is some function to update something regarding bounding info parameters)?

But you still have a mesh even if not part of the scene actually you should not call intersect on disposed meshes. removing the bounding info or other hacks would fail as the intersect function will fail to find what it needs. I sometimes use “dummy” mesh outside a scene for intersection only and in this case we would be stuck.

As intersect is quite perf intensive the best option is to not call it.

2 Likes

What will be the best function in this case if intersects function is so dangerous for performance?

It is not dangerous but useless to call on disposed meshes as it will incur an unneeded perf hit.

Why not only calling it on what you need ?

Thank you for your answers!
So, for my application I decided not to dispose meshes but hide them (with some condition) for later reuse.
I mark @msDestiny14 answer as the solution to my question :slight_smile:

2 Likes