How to clone an object3d in Three.js?

2019-01-18 23:58发布

问题:

I want to clone a model loaded with loader, I found this issue on github,but the solution doesn't work. It seems there has been an structural change in Object3D.

How can I clone an Object3D in current stable version of Three.js?

回答1:

In this new version of three.js you have a method clone.

For example, I use a queen from chess and I had to duplicate multiple times:

// queen is a mesh
var newQueen = queen.clone();

// make sure to re position to be able to see the new queen!
newQueen.position.set(100,100,100); // or any other coordinates

It will work with any mesh.

I used three.js r61.



回答2:

Actually mrdoob's answer is your answer...

The loader output a geometry you use to create a mesh. You need to create a new mesh with the loader-created mesh's geometry and material.

for ( var i = 0; i < 10; i ++ ) {
    var mesh = new THREE.Mesh( loadedMesh.geometry, loadedMesh.material );
    mesh.position.set( i * 100, 0, 0 );
    scene.add( mesh );
}

You want to clone a Mesh and not an Object3D because the output of the loader is a Mesh.