import { v3_array_matrix4_rotate } from "../../../../core/geom/vec3/v3_array_matrix4_rotate.js";


/**
 *
 * @param {number[]} output
 * @param {number[]} hit
 * @param {number[]|Float32Array} m 4x4 matrix
 */
export function ray_hit_apply_transform(output, hit, m) {

    // transform position
    const p_x = hit[0];
    const p_y = hit[1];
    const p_z = hit[2];

    // compute perspective projection
    const w = 1 / (m[3] * p_x + m[7] * p_y + m[11] * p_z + m[15]);

    const result_p_x = (m[0] * p_x + m[4] * p_y + m[8] * p_z + m[12]) * w;
    const result_p_y = (m[1] * p_x + m[5] * p_y + m[9] * p_z + m[13]) * w;
    const result_p_z = (m[2] * p_x + m[6] * p_y + m[10] * p_z + m[14]) * w;

    output[0] = result_p_x;
    output[1] = result_p_y;
    output[2] = result_p_z;

    // transform normal
    v3_array_matrix4_rotate(output, 3, hit, 3, m);

}
