Materials of GLB are treated as seperate objects, incurring excessive recursion when attempting to illuminate single objects with multiple materials

Hi,

As the title suggests, the materials of my imported GLB are being treated as seperate objects in my threeJS project.

If I recurse on the scene, scene.traverse(log_func), and then my log_func just prints the name of the objects to the console as so, console.log(child.name), multiple things are logged for a singular object (see the images below).

How do I ‘merge’ these into one object? I am asking this because in my GLB I have multiple objects, and want to implement a function whereby when a user hovers on the object, the object turns red. Currently, I just derive the ‘prefix’ of the object name (eg. ‘3ds’ from ‘3ds_10’), then recursively traverse the scene to find all of the other objects with 3ds as a prefix, and make them red as the scene.traverse property checks if each object has the relevant prefix - however I believe this may be performance intensive if I increase the amount of materials and/or objects in my GLB later on. I want to know how I can put all of the 3ds objects into one ‘folder’, and then just traverse through that ‘folder’ rather than every object.

Thanks - sorry if this is a noob Q, I’m new to JS and threeJS.

images:
image of JS log:

image of how the 3ds object is set up in blender:

my current idea btw is to just have an array of arrays, where each array is titled based on the prefix, and then each internal array has the prefix_x objects - but some guidance would be much appreciated

If you open https://gltf-viewer.donmccurdy.com/, open the JavaScript console, and then load your glTF, it should print the full structure of the glTF scene as it appears in three.js, which may be helpful.

I expect that your multi-material meshes in Blender are being loaded into three.js as a THREE.Group containing as children a list of meshes, 3ds_#. Meshes created by GLTFLoader have only one material.

however I believe this may be performance intensive if I increase the amount of materials and/or objects in my GLB later on

If the number of objects OR materials gets into the 100s or 1000s this could be a performance issue, yes. To have fewer objects you’d need to have fewer materials — this usually means using vertex colors or textures, perhaps a texture atlas if necessary. If you’re not sure how to do this you could share the glTF or Blend file and we may be able to help.

2 Likes

Yeah, that’s what’s happening

I ended up implementing this
let curr_group = scene.getobjbyname(‘prefix’)
curr_group.traverse(func_for_all_meshes_in_grp)
and it worked how I intended

Thanks!

1 Like