[SOLVED] Ammo performance optimization and glb mesh number detection

Hi guys,

I am loading glbs from external urls, and sometimes because of the mesh number of the object is large, and caused Ammo OOM, so I am wondering Is there a way to calculate:

  1. The mesh number of the external glb asset?
  2. The total number of all mesh colliders in the scene.

And how can I release the memory(disbale) of a specific object’s mesh collider.

Thanks for your help!

There is a small overhead for each Ammo object you create, but unless you are making hundreds of them, it shouldn’t matter much. You can always debug pause in rigidbody component system’s onUpdate method to see the current counts. You can also access private properties, e.g. to get amount of dynamic bodies:

const count = this.app.systems.rigidbody._dynamic.length();

I would not recommend doing it, though. If you are making primitives only, you can create thousands. Mesh colliders are different, as they occupy more memory, but each is different, depending on vertex count.

If you don’t know the vertex count, but you have a mesh, you could do something like this to get amount of vertices:

const count = mesh.getPositions();

But it is easier to control from the source of your GLB, e.g. if some designer or you, creating a model, you should specify the limit of vertex count they should not go over.

Ammo Wasm has about 64 Mb of memory to use, and that includes the Ammo instance itself. Not sure how much memory is left for you to use exactly.

For example, in JS we use 64 bit floats, which is 8 bytes per number. Say you have a model with 100 000 vertices. That is 100 000 * 3 (x,y,z) = 300 000 numbers, or 300 000 * 8 = 2.4 Mb (plus some for index buffer). Will it fit to Wasm memory? Definitely. How many? Hard to say. A dozen should be no issue.

To release the memory - destroy the entity with a collider. That should remove it from the physics world.

You can also clear the internal cache for the selected mesh:

const triMesh = this.app.systems.collision._triMeshCache[mesh.id];
if (triMesh) {
    delete this.app.systems.collision._triMeshCache[mesh.id]

Generally, you should remove objects from the world that are no longer needed, or that are too far to be relevant.