threejs raycaster只能选中mesh吗?选不到object3d
导入模型代码如下
let that = this; const loader = new GLTFLoader();
loader.load(
"model/km01.glb",
(gltf) => {
console.log(gltf.scene);
let vmobj = gltf.scene.getObjectByName("km-01");
vmobj.traverse((obj) => {
if (obj.parent.name==="km-01") {
that.granaryArr.push(obj);
}
});
console.log(that.granaryArr)
that.group.add(gltf.scene);
},
undefined,
function (error) {
console.error(error);
}
);
that.granaryArr是4个object3D组成的数组
每一个object3d由410个mesh组成,外形是一栋楼
我现在通过raycaster只能选中mesh,选不中整个楼的本身。求教各位大佬。raycaster相关代码如下
// 坐标转换 coordinate(event) {
var Sx = event.clientX;
var Sy = event.clientY;
this.x = (Sx / window.innerWidth) * 2 - 1;
this.y = -(Sy / window.innerHeight) * 2 + 1;
this.radial();
},
// 创建射线功能
radial() {
let that = this;
const loader = new GLTFLoader();
that.raycaster = new Three.Raycaster();
that.raycaster.setFromCamera(
new Three.Vector2(this.x, this.y),
that.camera
);
let intersects = that.raycaster.intersectObjects(that.granaryArr,true);
if (intersects.length > 0) {
that.chooseMesh = intersects[0].object
this.tabsDom.position.copy(this.chooseMesh.point)
} else {
this.chooseMesh = null;
}
},
回答:
给你一个思路,可以使用递归找到Scene
findParentIsScene(obj) { if(obj.parent instanceof Scene){
return obj;
}else {
return this.findParentIsScene(obj.parent)
}
}
然后在对这个obj做一些事情比如单机事件监听
this.findParentIsScene(e.intersection[0].object).dispatchEvent({ type: "click",
});
以上是 threejs raycaster只能选中mesh吗?选不到object3d 的全部内容, 来源链接: utcz.com/p/937186.html