three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Late binding of geometry to mesh

Open et-nowis opened this issue 15 years ago • 16 comments

The current Mesh constructor computes the bounding sphere of the input geometry and initializes its own bounding/morph target dictionary.

Does it make sense to have it support late binding? e.g.

// construct a hierarchy top-down
var rootObject = new THREE.Object3D();
scene.addChild(rootObject);

var mesh = new THREE.Mesh();
rootObject.addChild(mesh);

var geometry = new THREE.Geometry();
// fully constructing geometry
mesh.setGeometry(geometry); // TADA!!

et-nowis avatar May 01 '11 03:05 et-nowis

I like it! Will add :)

mrdoob avatar Jun 10 '11 08:06 mrdoob

Is the setGeometry method implemented?

linzhp avatar Apr 10 '12 22:04 linzhp

Not yet...

mrdoob avatar Apr 10 '12 23:04 mrdoob

What's the suggested workaround for now if I want to change the geometry of a Mesh after it has been added to scene.

My use case is like this: two wheels are connected to an axle. Users should be able to drag them around as a whole, so I merged them (1 axle + 2 wheels) into one geometry and make a Mesh out of it. On the other hand, users should be able to change the length of the axle but the wheel geometries should keep the same. What I am currently doing is to create another axle according to the new length, and attache the wheels onto it. But how to update the Mesh object to use this new geometry? Or any better way of doing this?

linzhp avatar Apr 11 '12 00:04 linzhp

Does the new axle have the same number of vertices/faces as the old one?

mrdoob avatar Apr 11 '12 01:04 mrdoob

Yes. The axle is actually a cylinder. The only variable is its length.

linzhp avatar Apr 11 '12 02:04 linzhp

So what about doing...

var compose = new THREE.Object3D();
compose.add( wheel1 );
compose.add( axle );
compose.add( wheel2 );
scene.add( compose );

// say that the axle is 100 of length
axle.scale.x = scale;
wheel1.position.x = - 50 * scale;
wheel2.position.y = 50 * scale;

mrdoob avatar Apr 11 '12 03:04 mrdoob

This works for the change-the-length requirement, but doesn't work for the dragging part. In order for users to drag the whole wheel pair (1 axle + 2 wheels), I used ray.intersectObject() to detect which object is clicked. If the 2 wheels and 1 axle are not merged into one Mesh object, they could not be selected and dragged together.

linzhp avatar Apr 11 '12 03:04 linzhp

What do you think linzhp, Don't merge the objects, you have a Group mapping, let say {Group1 : [Axle1, Wheel1, Wheel2]}

function pickObjects() {
  picked = ray.intersectObject();
  if (picked inside Group1) {
    picked = Group1
  }
}

then move the three things together

et-nowis avatar Apr 11 '12 03:04 et-nowis

Exactly. wheel1.parent is what you need.

mrdoob avatar Apr 11 '12 03:04 mrdoob

Well, that may work. In fact, I can check if the picked object is one of scene.children, if not, then recursively check the children of scene.children until it is found and go back to pick the child of scene that own the object. However, I wonder if there is any more elegant way of doing this

linzhp avatar Apr 11 '12 04:04 linzhp

Elegant how? This is already much better than creating a new cylinder every time ;)

mrdoob avatar Apr 11 '12 04:04 mrdoob

Yeah, there is a trade-off. Alright, I will take this approach. Thanks @mrdoob and @et-nowis

linzhp avatar Apr 11 '12 04:04 linzhp

This doesn't seem to be implemented. Is there any chance it could be? It's exactly what I need. :D

trumank avatar Nov 13 '12 04:11 trumank

I have created two nodes as mesh object and a link between them as cylinder or line. when I am dragging the node then link is also dragging fine respect to node. But for this when dragging the node every time, I am crating new link and removing old link. This is not optimal solution. Any idea for optimal solution. (i.e. without creating new link or removing old link we can dragged node with link) Thanks @mrdoob and @linzhp

navneetk-egnaroinc avatar Jun 05 '15 07:06 navneetk-egnaroinc

@navneetk-egnaroinc As stated in the guidelines, help requests should be directed to stackoverflow. This board is for bugs and feature requests.

WestLangley avatar Jun 05 '15 15:06 WestLangley