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.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.load(modelLoop, function(gltf){	// modelLoop take the filename

            model = new THREE.Object3D();


            gltf.scene.traverse(function (child) {
            } );
            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 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: