How to add a layer to a glb file with a multisub material?

Hi all.

This is my first question here and that makes me kind of new to Three JS. I have used so many hours try to figure out why I am only able to add layers to some of my imported glb files. And now I understand that only glb files with a single material will be added to a layer. Can this code be part of the problem, any idea how to fix it? :

model.add(object.scene.children[0]);
model.children[0].layers.set (10);

Thanks for all help.

I’m afraid layers and materials have nothing in common. If you want to set a layer for a glTF asset, do it like so:

gltf.scene.traverse( function ( child ) {

	child.layers.set( 10 )
 
} );

const assetPath = ‘…/model/greenhouse/’; // modell-folder
const loader = new THREE.GLTFLoader();

        loader.setPath(assetPath);
        loader.load(modelLoop, function(gltf){	// modelLoop take the filename
    
            scene.add(gltf.scene.children[0]);

            model = new THREE.Object3D();

            model.add(gltf.scene.children[0]);

            gltf.scene.traverse(function (child) {
                child.layers.set(10);
            } );
            
            scene.add(model);
            if (model.isMesh) {  
                model.castShadow = true;
                model.receiveShadow = true;
            }
        });

I did not make it working, so I must have misunderstand something?

What exactly does not work? Do you want to use layers in combination with your camera?

I know how to enable the different layers in a camera. So what i want is to load 30 .glb files, I want to set them to layer 10, and I want to enable cast and receive shadows. I am pretty close except for set the layer. I tried to add your code but don’t quite understand how to do so. This is my project, most for learning Three JS and Javascript http://www.tinclown.com/ Thanks for helping.

You have to add scene.add(gltf.scene.children[0]); after you configure the layers via traverse(). Otherwise the traverse will not work since the children have a different parent ( scene).

Thanks for all help, now it works smooth :slight_smile: