1

I have a Three.js Scene with several meshes moving around. I want to take a snap shot and copy all of the mesh locations by placing a new instance of a mesh (with the same geometry) in the scene in the same location & with the same rotation. I can't simply copy the mesh's .position & .rotation because the mesh is a child to other meshes. I tried to .clone() the mesh's matrixWorld but that didn't work. How do you inherit the world location of a mesh?

This is what I am trying to do:

// mesh is an existing mesh loaded into a scene
// geom is an existing geometry definition

var material = new THREE.MeshFaceMaterial();

var newMesh = new THREE.Mesh( geom, material);

newMesh.matrixWorld = mesh.matrixWorld.clone();

scene.add(newMesh);

Any help would be much appreciated.

dimpok
  • 11
  • 3

1 Answers1

2

It's as simple as this:

newMesh.position.copy( mesh.matrixWorld.getPosition() );

EDIT: See this updated answer, instead.

Community
  • 1
  • 1
WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Thanks for the reply. This works for the initial position of the mesh. But as I mentioned before the position values don't correspond to the matrixWorld values because the mesh is a child of a rotating mash chain. Here is an example of what I am talking about: dimitriipokrovskii2.appspot.com/robot_arm_joints.html So if you look at mesh3 the mesh3.matrixWorld.getPosition() is x: 0 y:750 z:0. If you move the robot around with your keys and look at mesh3.matrixWorld.getPosition() it returns the same value. If you look at the actual worldMatrix it stores the correct information. – dimpok Dec 05 '12 at 22:42
  • 1
    You are using a version of three.js that is about 1 year old. Please update to the current version r.53. This should all work as expected. – WestLangley Dec 05 '12 at 22:58
  • Just a heads up WestLangley. I tried it with the the new Three.js library r53 and it still doesn't work. When you issue mesh.matrixWorld.getPosition() it returns only any position changes made to that mesh. It doesn't take into account the actual location in world space. – dimpok Dec 07 '12 at 04:43
  • `mesh.matrixWorld.getPosition()` gets the actual location in world space. It is updated every render, unless you turned those updates off. If you are still having problems, please provide a simple live jsfiddle example. – WestLangley Dec 07 '12 at 14:49