I am importing .glb meshes into my BabylonJS game. The meshes are exported from Blender.
I am aware that glb/gltf meshes with multiple materials will be represented as separate meshes in BabylonJS. In my case, the meshes will always have multiple materials applied.
When investigating BABYLON.Mesh.MergeMeshes , I can’t seem to get things working. Intellisense mentions @param meshes — defines he vertices source. They should all be of the same material. Entries can empty – this seems to indicate it won’t be possible to merge my meshes into one.
Is there some way to proceed here such that I can access the mesh as a single object? The number of materials is highly variable, and may be 10-20+, so it’s not really feasible to access each mesh individually for the purposes of positioning. Additionally, it’s not clear how to even control a mesh that is actually an array of Meshes.
I have searched far and wide for answers and not finding anything that seems to satisfy this question. Any help here is appreciated, thanks.
But if you only want to merge the meshes for positioning purposes, I recommend using TransformNodes instead: Transform Node | Babylon.js Documentation (babylonjs.com) - you can have multiple meshes as children of a TransformNode, and any transformation applied to it will be passed down to the children. Indeed, that’s how most of the more complex GLB files will be organized - multiple individual meshes “grouped” as children of a TransformNode, like here:
Thanks @carolhmj - sorry I was not fully clear when originally writing this, but my mesh has animations on it that are generated in Blender. So while positioning can be handled via TransformNode, I have no idea how to do more interactive things with the mesh. Does merging it allow me to access the animations?
Thx @carolhmj - for some reason I still can’t get this working and access the merged mesh.
I have a playground example here where I’m trying to import a semi-complex .glb (silly looking as it is) and the merged mesh can’t be accessed (console logs as “null”) despite my best efforts.
I do need to use the asset manager or some async manner of loading as I will have multiple .glbs
Any help you can offer here is appreciated, been pulling my hair out over this.
@labris thank you, it was not apparent to me that the root node is not needed. This works when we manually enumerate all of the elements in the array. Since I will not always know the full list of elements (and do not want to hardcode/manually list them all)
I attempted to use the spread operator to reference element[1] → element[n] and received an error mesh.ts:3854 Uncaught (in promise) TypeError: e[l].getTotalVertices is not a function
Pls uncomment lines 91 or 92 to see the problem. This error displays in the Playground but in my app it simply fails silently. I seem to be missing something fundamental here – any idea what I’m doing wrong?