Transparency in multi material with sub-meshes

Oh man, was that ever a long debug process.
I worked on it all day and finally found the answer.

When you create a mesh it automatically adds a sub-mesh that covers the whole surface of the mesh. If you don’t clear this out, then the whole mesh gets drawn twice, once with default material and then again for the sub-meshes you defined.

In my case, by transparent sub-meshes were being drawn transparently, revealing the mesh drawn with the default material.

The fix is to this line of code:
sphere.subMeshes = [];

This deletes the default sub-mesh and only draws the sub-meshes that you added.

2 Likes