MrGarretto
MrGarretto

Reputation: 282

Three.js getting the parent object (a group) from it's children

In my scene, there are many object groups (Object3Ds), and I have setup a system for clicking / hovering over them to do certain things. When it uses the raycaster to find which objects are under the mouse, it returns the individual object, not the group (which I need).

The code I used to get the objects under the cursor looks like so:

raycaster.setFromCamera(mouse, camera);
clickobjstore = raycaster.intersectObjects(objects, true);

// The following doesn't work because intersects[0] is not the group, it's the object within the group!
for (j = 0; j < intersects[0].object.children.length; j++) {
    intersects[0].object.children[j].material.color.setHex(0x1A75FF);
}

Upvotes: 2

Views: 5293

Answers (1)

Falk Thiele
Falk Thiele

Reputation: 4494

The Object3D class stores a reference to the parent-Object for you:

var objectGroup = intersects[0].parent;

for (j = 0; j < objectGroup.children.length; j++) {
    objectGroup.children[j].material.color.setHex(0x1A75FF);
}

Upvotes: 5

Related Questions