Currently, I am loading a GLTF asset and using it in the scene multiple times.
I am trying to modify the material color of all meshes inside a specific object that is a GLTF model.
traverseMaterials(object, (material) => {
material.color.set(
object.info.model.color
);
});
Although this solution works, it ends up changing the materials of all other GLTF objects as well.
My objective is to only change the specified object and its children's mesh materials colors, not affect all instances of used GLTF models.
I attempted the following approach with no success:
traverseMaterials(object, (material) => {
let clonedMaterial = material.clone();
material = clonedMaterial;
material.color.set(
object.info.model.color
);
});
Below is the traverseMaterials function for your reference:
function traverseMaterials (object, callback) {
object.traverse((node) => {
if (!node.isMesh) return;
const materials = Array.isArray(node.material)
? node.material
: [node.material];
materials.forEach(callback);
});
}