Does loading a .GLB file load a scene?

Hi,

I am using a GLTF loader to load a .GLB file.

My logic is very similar to what we see here: three.js docs

My question is this:
We create a scene and then after we have loaded the .GLB file we use scene.add(gltf.scene);

Does this mean there are actually two scenes on screen?

No, additional scene are declared with

new THREE.Scene();

Still, a single object will be created including everything in a single “package”
(animations, geometries or whatever it could find inside your file)

It’s a personal view. But I suggest to not add complete gltf.scene, as it could become a hassle to target and manage specific parts of your loaded files.

1 Like

This is exactly the problem I am experiencing.

I do create a new scene using new THREE.scene and then I load the glb file and and that glb.scene to the first scene I created.

I then want to target specific parts.

So what you are describing is what I am experiencing. How can I load just the 3D model inside the .glb file and not the entire scene?

many variations exists, depends your needs.
The most common way is to traverse the gltf.scene and look for specific content.
But beware about special cases like animations: they are stored at the root of the object.

A basic function to get all the mesh inside your file:

gltfLoader.load(something, function(gltf) {

const content = gltf.scene;

content.traverse(function(child) {
    if (child.isMesh) {
        //retrieve geometry/map/skeleton or whatsoever here...
    }
});

});

Just in case you are unsure where stuff is stored.
output the whole gltf in the console.log , and check manually.

Thanks for the advice, will take a look.

When you look type of THREE.Scene() you will see object and when you using scene.add you are putting object under something object.

@Oxyn

I do not have the option “isMesh” but I do have the option “isObject3D”.

Do you perhaps have an example of how to retrieve the geometry?

Thank you.

In the end this helped me. Thank you.

const sceneMeshes = new Array()

        var theObj = object.scene.children[0]

        theObj.scale.set(1, 1, 1)

        this.scene.add(theObj)

        sceneMeshes.push(theObj)

I’d note that the gltf.scene object is actually a THREE.Group. It represents the contents of a “scene” declared in the glTF file but doesn’t need to be used that way in your application.

I do not recommend just grabbing the first child of the group unless you’re very sure about the exact content of your model. If you discard parent objects, you may also be discarding the position/rotation/scale that would be necessary for the objects to appear in the right locations.