0

I have an object created from importing an .obj I want to clone this object and give it it's own coordinates. To be more precise, I have a wall, for which I attach another object (a window) as a child. It all works fine when dragging the window along the wall. Now, when I want to clone the wall and rotate it 180 degrees, dragging the child along it's parent goes exactly the opposite way from the mouse movement. I would like to reuse the same obj wall for all sides of my building.

1 Answer 1

1

You are describing two problems.

One is how to clone an object and give it a different position:

var wallCopy = wall.clone();
scene.add(wallCopy);
wallCopy.position.set( 10,20,30 );

The other question is moving an object in Object space instead of World space. I.e. if you change the position.x of an object (the window) that is a child of a rotated object, the object will move relative to its rotated parent.

If you are building some sort of editor, you can use SceneUtils to make this easier...

https://threejs.org/docs/#examples/utils/SceneUtils

At the start of your edit, you would detach the window and attach it to the scene, using SceneUtils.detach... then apply your edit movement, and when the movement is done, attach it back to the wall using SceneUtils.attach.

SceneUtils takes care of keeping the visual transform consistent between the detach and attach operations between different places on the scene hierarchy.

edit: So a way to handle the case you describe in comments, without doing anything fiddly, is to just use more intermediate nodes. You can always set up your scaling and stuff on the child nodes, (You can always update thier matrices and then set their matrixAutoUpdate = false; if you're paranoid about the performance impact.}

Here's what I mean:

{"root", scale:1, children:[anchor1, anchor2]} {"anchor1" scale:1, children:[wall]} {"anchor2" scale:1, children:[wall2]}

Then you can just manipulate the anchors or root to move things around, and the scaling of "wall" will be isolated to its own little sub tree.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for your answer but I don't believe that this is my case. Let me give you another example: - I load an external obj (wall) , then clone it, rename it wall2 and resize it. - Now, if I add a child to wall 2, that child would automatically be distorted, based on parent's transformations. I want my newly added child to keep it's original appearance Hope this makes my issue clearer.
Updated my answer above ^
Unfortunately detaching and reattaching the child doesn't do the trick. See these images: prntscr.com/jxhfkx prntscr.com/jxhggg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.