import { Vector3 as ThreeVector3 } from "three";

/**
 * Compute position of focal point of the camera in world coordinate space. This is the "pinhole" where all perspective lines meet.
 * @param {THREE.PerspectiveCamera} camera
 * @param {Vector3} result
 */
export function compute_perspective_camera_focal_position(camera, result) {
    if (camera.isPerspectiveCamera !== true) {
        throw new TypeError('expected a perspective camera, got something else');
    }

    const focalLength = camera.getFocalLength();

    const v3 = new ThreeVector3(0, 0, 0.5);

    v3.unproject(camera);
    //get direction
    v3.sub(camera.position).normalize();

    //
    v3.multiplyScalar(-focalLength);

    v3.add(camera.position);

    result.set(v3.x, v3.y, v3.z);
}
