Assigning of submesh id changed between babylon versions

Hello,
I have an application that uses one mesh with submeshes. I then pick from this mesh and use the submesh id to do something. I noticed that when creating the submeshes, the first submesh id is 0 with some older version of babylon (7.x) while the first submesh id is 1 in babylonjs 8.x.

I prepared a playground to show this: Babylon.js Playground

You see two spheres. These are part of one mesh but configured as submeshes. In the console, you can already see the list of two submeshes. Investigating them shows the assigned ids for both submeshes. When clicking on either sphere, the submesh id is shown in the console.

When switching the playground between babylon 7.54 and latest, I see a different behavior:

  • Ids of 0 and 1 one for 7.54
  • Ids of 1 and 2 for 8 (latest).

Is this change known and intended?

Best regards,
Axel

1 Like

cc @AlitarSemiramis

1 Like

Looking at this :slight_smile:

Hey! I looked at this. I have blamed SubMesh code and it hasn’t changed in several years, so I don’t know what changed between 7.x and 8.x behavior, but I can explain what is going on with your code and how to address it.

This line here is the issue:

combinedMesh.subMeshes = [0,1].map((index) =>
        new BABYLON.SubMesh(0, index * vertexCount, vertexCount, index * indexCount, indexCount, combinedMesh)
);

Before executing your map, your combinedMesh.subMeshes already contains one submesh, the combined spheres.

Then you call new SubMesh, which will add the new SubMeshes to the combinedMesh, and each submesh id is calculated by:

this._id = mesh.subMeshes.length - 1;

So, your first new creates the submesh of the first sphere, which has id 1, and the second has id 2 (because id 0 is the original combined mesh). Then you replace the array of meshes, which has 3 submeshes (combined, sphere1 and sphere2), by the new one that only has 2, but their ids are 1 and 2 already.

You can address this by removing the original submesh before doing your code, something like this:

Submesh id | Babylon.js Playground

I hope it helps!

1 Like

Thank you for the reply, I got it working.

1 Like