Three.js add an object to a group but keep global

2019-01-06 23:36发布

问题:

I want to move an object from one group (or world/scene) to another group, but keep it's global transformation intact. Basically, I don't want to see the object change.

basically, something like this:

//store current world transformation
var origWorldMatrix = myObject.matrixWorld.clone();

//move object to a group (that is positioned and rotated arbitrarily)
someGroup.add( myObject );

//restore previous world transformation
myObject.matrixWorld.copy( origWorldMatrix );

However, this doesn't seem to work. I guess because the world matrix is always updated the next frame, based on the local position/rotation/scale properties. I've tried to use this with matrixAutoUpdate = false, but that doesn't seem to work either.

The result I am trying to accomplish seems like something that should be simple to do, so I hope I am missing something obvious. Can anybody give me a clue on how do do this?

Thanks!

回答1:

The methods you need to utilize can be found in the file /examples/js/utils/SceneUtils.js.

// remove child from parent and add it to scene
THREE.SceneUtils.detach( child, parent, scene );

// remove child from scene and add it to parent
THREE.SceneUtils.attach( child, scene, parent );

Study the source code of attach() and detach() so you understand what they are doing.

three.js r.96