three 屏幕坐标转世界坐标

export function transScreenPositionToWorld(
    event: MouseEvent,
    dom: HTMLElement,
    camera: THREE.PerspectiveCamera,
): THREE.Vector2 {
    // 鼠标移动位置 和 3D位置转换
    const pos = new THREE.Vector3();

    const vecX = (event.clientX / dom.clientWidth) * 2 - 1;
    const vecY = -(event.clientY / dom.clientHeight) * 2 + 1;
    const vec = new THREE.Vector3(vecX, vecY, 0);

    vec.unproject(camera);
    vec.sub(camera.position).normalize();

    const distance = -camera.position.z / vec.z;
    pos.copy(camera.position).add(vec.multiplyScalar(distance));

    return new THREE.Vector2(pos.x, pos.y);
}

你可能感兴趣的:(webgl和threejs,前端)