问题
I have a parent Object3D
that has child meshes. How can I use Raycaster
to select only the root parent object?
my example
回答1:
If you have a parent Object3D
that has multiple child meshes, and you want to select the parent by raycasting, you can do the following:
Add the parent object to the array of objects:
var objects = [];
...
objects.push( root_parent_object );
Add to each child object a pointer to the root parent object:
child.userData.parent = root_parent_object;
Pass in the recursive flag to intersectObjects()
.
var intersects = raycaster.intersectObjects( objects, true );
Now when the raycaster intersects a child object, you can obtain the root object.
three.js r.68
回答2:
I had similar need using collada objects exported from Sketchup. Meshes have a trail of multiple parents ahead of them so all I did was this:
if (intersects.length >0){
par = intersects[ 0 ].object.parent;
while(par.type !== "Group"){
par = par.parent;
}
groupName = par.name;
}
来源:https://stackoverflow.com/questions/26202064/how-to-select-a-root-object3d-using-raycaster