import Vector2 from "../../core/geom/Vector2.js";
import Vector3 from "../../core/geom/Vector3.js";

/**
 *
 * @param {Engine} engine
 * @param {Vector2} [position] if not specified, current pointer position will be used
 * @returns {{origin:Vector3, direction:Vector3}}
 */
export function make_ray_from_viewport_position(engine, position) {

    let _p = position;

    if (position === undefined) {
        _p = engine.devices.pointer.position;
    }

    const ndc = new Vector2();

    engine.graphics.normalizeViewportPoint(_p, ndc);

    const origin = new Vector3();
    const direction = new Vector3();

    engine.graphics.viewportProjectionRay(ndc.x, ndc.y, origin, direction);

    return { origin, direction };
}
