0

forge中自己加入的元素怎么通过鼠标选中

1 个回答

0

利用Raycaster的方法,但是这个跟three.js的方法不太一样,需要做一些设置,从源码得出这个方法,需要对透视相机和正交相机做出区别:

    
function intersectObjects(pointer,camera,obj) {
    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;
}