【发布时间】:2019-12-01 20:08:28
【问题描述】:
我在包含 Threejs r71 的 autodesk forge 工作,我想使用 raycaster 来检测点云中不同元素的点击。
感谢您使用 ThreeJs r71 执行此操作的示例代码。
现在,我使用 forge api 注册了一个扩展,并在其中运行下面的代码。它创建一个点云并将点定位在预定位置(保存在 cameraInfo 数组中)。
let geometry = new THREE.Geometry();
this.cameraInfo.forEach( function(e) {
geometry.vertices.push(e.position);
}
)
const material = new THREE.PointCloudMaterial( { size: 150, color: 0Xff0000, sizeAttenuation: true } );
this.points = new THREE.PointCloud( geometry, material );
this.scene.add(this.points);
/* Set up event listeners */
document.addEventListener('mousemove', event => {
// console.log('mouse move!');
let mouse = {
x: ( event.clientX / window.innerWidth ) * 2 - 1,
y: - ( event.clientY / window.innerHeight ) * 2 + 1
};
let raycaster = new THREE.Raycaster();
raycaster.params.PointCloud.threshold = 15;
let vector = new THREE.Vector3(mouse.x, mouse.y, 0.5).unproject(this.camera);
raycaster.ray.set(this.camera.position, vector.sub(this.camera.position).normalize());
this.scene.updateMatrixWorld();
let intersects = raycaster.intersectObject(this.points);
if (intersects.length > 0) {
const hitIndex = intersects[0].index;
const hitPoint = this.points.geometry.vertices[ hitIndex ];
console.log(hitIndex);
console.log(hitPoint);
}
}, false);
输出似乎不合逻辑。在某些相机位置,它会不断地告诉我它正在与点云中的一个项目相交(无论鼠标在哪里)。而且在某些摄像头位置,它根本不会检测到交叉路口。
TLDR:它实际上并没有检测到我的点云和鼠标的交叉点。
【问题讨论】:
标签: javascript three.js autodesk-forge