packages

spatial::Mat4

On this page

Mat4Source

pub struct Mat4<T = f32>

A 4x4 matrix of four column vectors: a 3D transform in homogeneous coordinates, a camera view (look_at_rh) or a projection (perspective_rh, ...). Mat4 alone is Mat4<f32>. #[repr(C)]: sixteen Ts column by column, the layout of a GLSL mat4, WGSL mat4x4<f32>, HLSL column_major float4x4 and glTF; upload it as it is, or to_cols_array(). Products run on SIMD lanes where the target has them.

nabi
use spatial::{Mat4, Quat, Vec3};

let m = Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) * Mat4::from_quat(Quat::from_rotation_z(f32::PI / 2.0));
assert(m.transform_point(Vec3::X).approx_eq(Vec3::new(1.0, 1.0, 0.0), 1e-6));
  • pub x_axis: Vec4<T>§

    Source
  • pub y_axis: Vec4<T>§

    Source
  • pub z_axis: Vec4<T>§

    Source
  • pub w_axis: Vec4<T>§

    Source
  • pub const fn from_cols(x_axis: Vec4<T>, y_axis: Vec4<T>, z_axis: Vec4<T>, w_axis: Vec4<T>) -> Mat4<T>§

    Source

    The matrix of its columns.

    nabi
    use spatial::{Mat4, Vec4};
    
    assert(Mat4::from_cols(Vec4::X, Vec4::Y, Vec4::Z, Vec4::W) == Mat4::IDENTITY);
    
  • pub fn from_cols_array(a: [T; 16]) -> Mat4<T>§

    Source

    From sixteen values, column by column.

    nabi
    let m = spatial::Mat4::from_cols_array([1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 5.0, 6.0, 7.0, 1.0]);
    assert(m.w_axis.x == 5.0);
    
  • pub fn to_cols_array(self) -> [T; 16]§

    Source

    The sixteen values column by column, as a GPU reads them.

    nabi
    use spatial::{Mat4, Vec3};
    
    assert(Mat4::from_translation(Vec3::new(5.0, 6.0, 7.0)).to_cols_array()[12] == 5.0);
    
  • pub fn identity() -> Mat4<T>§

    Source

    The identity.

    nabi
    assert(spatial::Mat4::<f32>::identity() == spatial::Mat4::IDENTITY);
    
  • pub fn zero() -> Mat4<T>§

    Source

    All zero.

    nabi
    assert(spatial::Mat4::<f32>::zero().determinant() == 0.0);
    
  • pub fn from_diagonal(d: Vec4<T>) -> Mat4<T>§

    Source

    The diagonal matrix of d.

    nabi
    use spatial::{Mat4, Vec4};
    
    assert(Mat4::from_diagonal(Vec4::splat(2.0)).determinant() == 16.0);
    
  • pub fn from_mat3(m: Mat3<T>) -> Mat4<T>§

    Source

    m as the upper-left 3x3, translation zero.

    nabi
    use spatial::{Mat3, Mat4};
    
    assert(Mat4::from_mat3(Mat3::IDENTITY) == Mat4::IDENTITY);
    
  • pub fn from_translation(t: Vec3<T>) -> Mat4<T>§

    Source

    The translation by t.

    nabi
    use spatial::{Mat4, Vec3};
    
    assert(Mat4::from_translation(Vec3::ONE).transform_point(Vec3::ZERO) == Vec3::ONE);
    
  • pub fn from_scale(s: Vec3<T>) -> Mat4<T>§

    Source

    The scale by s along each axis.

    nabi
    use spatial::{Mat4, Vec3};
    
    assert(Mat4::from_scale(Vec3::splat(2.0)).transform_point(Vec3::ONE) == Vec3::splat(2.0));
    
  • pub fn from_quat(q: Quat<T>) -> Mat4<T>§

    Source

    The rotation of a unit quaternion.

    nabi
    use spatial::{Mat4, Quat, Vec3};
    
    let q = Quat::from_rotation_x(1.0);
    assert(Mat4::from_quat(q).transform_vector(Vec3::Y).approx_eq(q * Vec3::Y, 1e-6));
    
  • pub fn from_axis_angle(axis: Vec3<T>, angle: T) -> Mat4<T>§

    Source

    The rotation by angle radians about the unit vector axis.

    nabi
    use spatial::{Mat4, Vec3};
    
    let m = Mat4::from_axis_angle(Vec3::Z, f32::PI / 2.0);
    assert(m.transform_vector(Vec3::X).approx_eq(Vec3::Y, 1e-6));
    
  • pub fn from_rotation_x(angle: T) -> Mat4<T>§

    Source

    The rotation by angle radians about +x.

    nabi
    use spatial::{Mat3, Mat4};
    
    assert(Mat4::from_rotation_x(0.5) == Mat4::from_mat3(Mat3::from_rotation_x(0.5)));
    
  • pub fn from_rotation_y(angle: T) -> Mat4<T>§

    Source

    The rotation by angle radians about +y.

    nabi
    use spatial::{Mat3, Mat4};
    
    assert(Mat4::from_rotation_y(0.5) == Mat4::from_mat3(Mat3::from_rotation_y(0.5)));
    
  • pub fn from_rotation_z(angle: T) -> Mat4<T>§

    Source

    The rotation by angle radians about +z.

    nabi
    use spatial::{Mat3, Mat4};
    
    assert(Mat4::from_rotation_z(0.5) == Mat4::from_mat3(Mat3::from_rotation_z(0.5)));
    
  • pub fn from_scale_rotation_translation(scale: Vec3<T>, rotation: Quat<T>, translation: Vec3<T>) -> Mat4<T>§

    Source

    Scale by scale, rotate by rotation, then move by translation: the model matrix of an object.

    nabi
    use spatial::{Mat4, Quat, Vec3};
    
    let m = Mat4::from_scale_rotation_translation(Vec3::splat(2.0), Quat::IDENTITY, Vec3::X);
    assert(m.transform_point(Vec3::ONE) == Vec3::new(3.0, 2.0, 2.0));
    
  • pub fn to_scale_rotation_translation(self) -> (Vec3<T>, Quat<T>, Vec3<T>)§

    Source

    The scale, rotation and translation of a matrix built by from_scale_rotation_translation. A negative determinant puts the mirror in scale.x. Shear and projection are not represented.

    nabi
    use spatial::{Mat4, Quat, Vec3};
    
    let q = Quat::from_rotation_y(0.5);
    let (s, r, t) = Mat4::from_scale_rotation_translation(Vec3::new(1.0, 2.0, 3.0), q, Vec3::X).to_scale_rotation_translation();
    assert(s.approx_eq(Vec3::new(1.0, 2.0, 3.0), 1e-5) && r.approx_eq(q, 1e-5) && t == Vec3::X);
    
  • pub fn look_at_rh(eye: Vec3<T>, center: Vec3<T>, up: Vec3<T>) -> Mat4<T>§

    Source

    A right-handed view matrix: the camera at eye looks at center, up roughly up. View space has +x right, +y up and the camera looking down -z (OpenGL, Vulkan, Metal and WebGPU conventions; pair with a _rh projection).

    nabi
    use spatial::{Mat4, Vec3};
    
    let v = Mat4::look_at_rh(Vec3::new(0.0, 0.0, 5.0), Vec3::ZERO, Vec3::Y);
    assert(v.transform_point(Vec3::ZERO).approx_eq(Vec3::new(0.0, 0.0, -5.0), 1e-6));
    
  • pub fn look_at_lh(eye: Vec3<T>, center: Vec3<T>, up: Vec3<T>) -> Mat4<T>§

    Source

    A left-handed view matrix: +x right, +y up and the camera looking down +z (Direct3D's convention; pair with a _lh projection).

    nabi
    use spatial::{Mat4, Vec3};
    
    let v = Mat4::look_at_lh(Vec3::new(0.0, 0.0, -5.0), Vec3::ZERO, Vec3::Y);
    assert(v.transform_point(Vec3::ZERO).approx_eq(Vec3::new(0.0, 0.0, 5.0), 1e-6));
    
  • pub fn perspective_rh(fov_y: T, aspect: T, near: T, far: T) -> Mat4<T>§

    Source

    A right-handed perspective projection with depth 0..1, for Vulkan, Metal, Direct3D and WebGPU: fov_y is the vertical field of view in radians, aspect width / height, and points at distance near and far in front of the camera (z = -near, -far) map to depth 0 and 1. Vulkan's clip space has +y down: flip y_axis.y or the viewport for it.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::<f64>::perspective_rh(1.0, 16.0 / 9.0, 0.1, 100.0);
    assert((p.project_point(Vec3::new(0.0, 0.0, -0.1)).z - 0.0).abs() < 1e-12);
    assert((p.project_point(Vec3::new(0.0, 0.0, -100.0)).z - 1.0).abs() < 1e-12);
    
  • pub fn perspective_lh(fov_y: T, aspect: T, near: T, far: T) -> Mat4<T>§

    Source

    A left-handed perspective projection with depth 0..1 (Direct3D with look_at_lh): points at z = near, far map to depth 0 and 1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::<f64>::perspective_lh(1.0, 1.0, 0.1, 100.0);
    assert((p.project_point(Vec3::new(0.0, 0.0, 100.0)).z - 1.0).abs() < 1e-12);
    
  • pub fn perspective_rh_gl(fov_y: T, aspect: T, near: T, far: T) -> Mat4<T>§

    Source

    A right-handed perspective projection with depth -1..1, for OpenGL (gluPerspective): points at z = -near, -far map to depth -1 and 1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::<f64>::perspective_rh_gl(1.0, 1.0, 0.1, 100.0);
    assert((p.project_point(Vec3::new(0.0, 0.0, -0.1)).z + 1.0).abs() < 1e-12);
    
  • pub fn perspective_lh_gl(fov_y: T, aspect: T, near: T, far: T) -> Mat4<T>§

    Source

    A left-handed perspective projection with depth -1..1: points at z = near, far map to depth -1 and 1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::<f64>::perspective_lh_gl(1.0, 1.0, 0.1, 100.0);
    assert((p.project_point(Vec3::new(0.0, 0.0, 0.1)).z + 1.0).abs() < 1e-12);
    
  • pub fn orthographic_rh(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T>§

    Source

    A right-handed orthographic projection with depth 0..1: the box left..right, bottom..top, z = -near..-far maps to x, y in -1..1 and depth 0..1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::orthographic_rh(-2.0, 2.0, -1.0, 1.0, 0.0, 10.0);
    assert(p.transform_point(Vec3::new(2.0, 1.0, -10.0)).approx_eq(Vec3::ONE, 1e-6));
    
  • pub fn orthographic_lh(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T>§

    Source

    A left-handed orthographic projection with depth 0..1: z = near..far maps to depth 0..1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::orthographic_lh(-1.0, 1.0, -1.0, 1.0, 0.0, 10.0);
    assert(p.transform_point(Vec3::new(0.0, 0.0, 10.0)).z == 1.0);
    
  • pub fn orthographic_rh_gl(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T>§

    Source

    A right-handed orthographic projection with depth -1..1, for OpenGL (glOrtho): z = -near..-far maps to depth -1..1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::orthographic_rh_gl(-1.0, 1.0, -1.0, 1.0, 1.0, 3.0);
    assert(p.transform_point(Vec3::new(0.0, 0.0, -1.0)).z == -1.0);
    
  • pub fn orthographic_lh_gl(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T>§

    Source

    A left-handed orthographic projection with depth -1..1: z = near..far maps to depth -1..1.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::orthographic_lh_gl(-1.0, 1.0, -1.0, 1.0, 1.0, 3.0);
    assert(p.transform_point(Vec3::new(0.0, 0.0, 3.0)).z == 1.0);
    
  • pub fn transform_point(self, p: Vec3<T>) -> Vec3<T>§

    Source

    The point p transformed with w = 1, translation included and no perspective divide: for model and view matrices. Use project_point through a projection.

    nabi
    use spatial::{Mat4, Vec3};
    
    assert(Mat4::from_translation(Vec3::X).transform_point(Vec3::Y) == Vec3::new(1.0, 1.0, 0.0));
    
  • pub fn transform_vector(self, v: Vec3<T>) -> Vec3<T>§

    Source

    The direction v transformed with w = 0: translation left out.

    nabi
    use spatial::{Mat4, Vec3};
    
    assert(Mat4::from_translation(Vec3::X).transform_vector(Vec3::Y) == Vec3::Y);
    
  • pub fn project_point(self, p: Vec3<T>) -> Vec3<T>§

    Source

    The point p with w = 1 transformed, then divided by its w: a point through a projection, to normalized device coordinates.

    nabi
    use spatial::{Mat4, Vec3};
    
    let p = Mat4::perspective_rh(f32::PI / 2.0, 1.0, 1.0, 10.0);
    assert((p.project_point(Vec3::new(1.0, 0.0, -1.0)).x - 1.0).abs() < 1e-6);
    
  • pub fn col(self, i: usize) -> Vec4<T>§

    Source

    Column i (0 to 3).

    nabi
    assert(spatial::Mat4::IDENTITY.col(3) == spatial::Vec4::W);
    
  • pub fn row(self, i: usize) -> Vec4<T>§

    Source

    Row i (0 to 3).

    nabi
    use spatial::{Mat4, Vec3, Vec4};
    
    assert(Mat4::from_translation(Vec3::ONE).row(0) == Vec4::new(1.0, 0.0, 0.0, 1.0));
    
  • pub fn transpose(self) -> Mat4<T>§

    Source

    The transpose.

    nabi
    use spatial::{Mat4, Vec3};
    
    let m = Mat4::from_translation(Vec3::ONE);
    assert(m.transpose().transpose() == m && m.transpose().x_axis.w == 1.0);
    
  • pub fn determinant(self) -> T§

    Source

    The determinant.

    nabi
    use spatial::{Mat4, Vec3};
    
    assert(Mat4::from_scale(Vec3::new(1.0, 2.0, 3.0)).determinant() == 6.0);
    
  • pub fn inverse(self) -> Mat4<T>§

    Source

    The inverse. A singular matrix gives infinite or NaN entries; try_inverse gives none for it instead. For a rigid transform (rotation and translation only) Affine3::inverse is cheaper.

    nabi
    use spatial::{DMat4, DQuat, DVec3};
    
    let m = DMat4::from_scale_rotation_translation(DVec3::splat(2.0), DQuat::from_rotation_x(0.5), DVec3::ONE);
    assert((m.inverse() * m).approx_eq(DMat4::IDENTITY, 1e-12));
    
  • pub fn try_inverse(self) -> Mat4<T> | none§

    Source

    The inverse, or none when the determinant is zero or the result is not finite.

    nabi
    assert(spatial::Mat4::<f32>::zero().try_inverse() == none);
    
  • pub fn approx_eq(self, o: Mat4<T>, eps: T) -> bool§

    Source

    Whether every entry is within eps of o's.

    nabi
    use spatial::Mat4;
    
    assert(Mat4::IDENTITY.approx_eq(Mat4::from_rotation_z(1e-7), 1e-6));
    
  • pub fn is_finite(self) -> bool§

    Source

    Whether no entry is NaN or infinite.

    nabi
    assert(spatial::Mat4::IDENTITY.is_finite());
    
Trait implementations 7
  • impl<T: Real> Mul for Mat4<T>
  • impl<T: Real> Mul<Vec4<T>> for Mat4<T>
  • impl<T: Real> Mul<T> for Mat4<T>
  • impl<T: Real> Add for Mat4<T>
  • impl<T: Real> Sub for Mat4<T>
  • impl<T: Real> MulAssign for Mat4<T>
  • impl<T: Real> Display for Mat4<T>