Search code examples
three.jscolladasketchup

How to clone Collada object including multiple meshes in Three.js


I've loaded a .dae model including multiple meshes, and tried to clone it but failed. Following is the code I used.

loader.load( 'assets/model/deck.dae', function ( collada ) {
    deck = collada.scene;
    console.log(deck);
    window.referenceModel = deck.children[0];       
    refObject  = window.referenceModel;     
} );
var deckClone = new THREE.Mesh( refObject.geometry, refObject.material );
scene.add( deckClone );

This code works well on sample .dae file consists of one mesh but doesn't work well on the other .dae file made by myself. .dae file I made consists of several mesh groups. I will attach those files. This is sample .dae file. https://drive.google.com/file/d/13BCp6avslnpb1O8Q6xCqjE-ueojgz1AD/view And this is .dae file I made myself. https://drive.google.com/file/d/1BTIMs0IHHqrixvj45NXcZoh1PhEnm2Qr/view

I want to know how to clone objects from the second .dae files, or how to convert the second one to .dae file with the same structure (one mesh) file.


Solution

  • You're only targeting ONE child of your scene when you do deck.children[0] so of course it's only going to clone that one mesh.

    Try the .clone() method as mentioned in the docs, which will traverse through all its descendants and clone them for you.

    loader.load( 'assets/model/deck.dae', function ( collada ) {
        deck = collada.scene;
        var deckClone = deck.clone(true);
        scene.add(deckClone);
    } );
    

    Also, you were adding deckClone outside the .load() callback function, which means it's going to be performed before the .dae file gets loaded, and you'll run into errors and problems that way. Make sure you add your cloned object to the scene inside the callback function.