Function perspectiveMat4

  • Returns a 4x4 perspective projection matrix based on the given field of view, aspect ratio, and near/far clipping planes.

    This function generates a perspective projection matrix, which transforms 3D coordinates into 2D space. The matrix maps the frustum defined by the near and far planes and the field of view into the canonical view volume.

    Parameters

    • fovyrad: number

      The vertical field of view (in radians).

    • aspectratio: number

      The aspect ratio (width / height) of the viewport.

    • znear: number

      The distance to the near clipping plane.

    • zfar: number

      The distance to the far clipping plane.

    • Optionalm: FloatArrayParam

      An optional destination matrix to store the result. If not provided, a new matrix is created.

    Returns FloatArrayParam

    The resulting 4x4 perspective projection matrix. If m is provided, it will be modified; otherwise, a new matrix is returned.

    const fov = Math.PI / 4; // 45 degrees in radians
    const aspect = 16 / 9;
    const znear = 0.1;
    const zfar = 1000;
    const matrix = perspectiveMat4(fov, aspect, znear, zfar);
    console.log(matrix);