How to select a root Object3D using Raycaster

半腔热情 提交于 2019-12-17 16:52:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!