function intersectObjects(pointer,camera,obj) {
var domElement = viewer.impl.canvas;
var pointerVector = new THREE.Vector3();
var pointerDir = new THREE.Vector3();
var ray = new THREE.Raycaster();
var rect = domElement.getBoundingClientRect();
var x = ( ( pointer.clientX - rect.left ) / rect.width ) * 2 - 1;
var y = - ( ( pointer.clientY - rect.top ) / rect.height ) * 2 + 1;
if (camera.isPerspective) {
pointerVector.set(x, y, 0.5);
pointerVector.unproject(camera);
ray.set(camera.position, pointerVector.sub(camera.position).normalize());
} else {
pointerVector.set(x, y, -1);
pointerVector.unproject(camera);
pointerDir.set(0, 0, -1);
ray.set(pointerVector, pointerDir.transformDirection(camera.matrixWorld));
}
var intersections = ray.intersectObjects( obj, true );
return intersections[0] ? intersections[0] : false;
}
window.onclick = () => {
var camera = NOP_VIEWER.getCamera();
var pointerVector = new THREE.Vector3();
var pointerDir = new THREE.Vector3();
var x = (window.event.clientX / window.innerWidth) * 2 - 1;
var y = - (window.event.clientY / window.innerHeight) * 2 + 1;
if (camera.isPerspective) { //判断透视相机和正交相机
pointerVector.set(x, y, 0.5);
pointerVector.unproject(camera);
var raycaster = new THREE.Raycaster(camera.position, pointerVector.sub(camera.position).normalize());
} else {
pointerVector.set(x, y, -1);
pointerVector.unproject(camera);
pointerDir.set(0, 0, -1);
var raycaster = new THREE.Raycaster(pointerVector, pointerDir.transformDirection(camera.matrixWorld));
}
// 获取raycaster直线和所有模型相交的数组集合
var objs = NOP_VIEWER.impl.scene.children;
var intersects = raycaster.intersectObjects(objs, true);
console.log(intersects);
if (intersects.length > 0) {
intersects[0].object.material.color.set(0xff0000);
}
}
您好,按照这样写后,点击物体会返回空数组。但是点击某个角度后,会把所有objs中的物体都上色。返回的是
发布评论
评论(3)
利用Raycaster的方法,但是这个跟three.js的方法不太一样,需要做一些设置,从源码得出这个方法,需要对透视相机和正交相机做出区别:
你好,请问是如何实现在forge中加入自己的元素的呢?我根据https://forge.autodesk.com/bl...,在模型内并没有生成对应的图形,但是程序也并没有报错,该生成的图形对象也都有。
请问您是怎么实现的在forge中添加元素的呢?