spatial::Mat4
On this page
37approx_eqcoldeterminantfrom_axis_anglefrom_colsfrom_cols_arrayfrom_diagonalfrom_mat3from_quatfrom_rotation_xfrom_rotation_yfrom_rotation_zfrom_scalefrom_scale_rotation_translationfrom_translationidentityinverseis_finitelook_at_lhlook_at_rhorthographic_lhorthographic_lh_glorthographic_rhorthographic_rh_glperspective_lhperspective_lh_glperspective_rhperspective_rh_glproject_pointrowto_cols_arrayto_scale_rotation_translationtransform_pointtransform_vectortransposetry_inversezero
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.
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));
Sourcepub const fn from_cols(§x_axis: Vec4<T>, y_axis: Vec4<T>, z_axis: Vec4<T>, w_axis: Vec4<T>) -> Mat4<T> The matrix of its columns.
nabiuse spatial::{Mat4, Vec4}; assert(Mat4::from_cols(Vec4::X, Vec4::Y, Vec4::Z, Vec4::W) == Mat4::IDENTITY);From sixteen values, column by column.
nabilet 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);The sixteen values column by column, as a GPU reads them.
nabiuse spatial::{Mat4, Vec3}; assert(Mat4::from_translation(Vec3::new(5.0, 6.0, 7.0)).to_cols_array()[12] == 5.0);The identity.
nabiassert(spatial::Mat4::<f32>::identity() == spatial::Mat4::IDENTITY);All zero.
nabiassert(spatial::Mat4::<f32>::zero().determinant() == 0.0);The diagonal matrix of
d.nabiuse spatial::{Mat4, Vec4}; assert(Mat4::from_diagonal(Vec4::splat(2.0)).determinant() == 16.0);mas the upper-left 3x3, translation zero.nabiuse spatial::{Mat3, Mat4}; assert(Mat4::from_mat3(Mat3::IDENTITY) == Mat4::IDENTITY);The translation by
t.nabiuse spatial::{Mat4, Vec3}; assert(Mat4::from_translation(Vec3::ONE).transform_point(Vec3::ZERO) == Vec3::ONE);The scale by
salong each axis.nabiuse spatial::{Mat4, Vec3}; assert(Mat4::from_scale(Vec3::splat(2.0)).transform_point(Vec3::ONE) == Vec3::splat(2.0));The rotation of a unit quaternion.
nabiuse 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));The rotation by
angleradians about the unit vectoraxis.nabiuse 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));The rotation by
angleradians about +x.nabiuse spatial::{Mat3, Mat4}; assert(Mat4::from_rotation_x(0.5) == Mat4::from_mat3(Mat3::from_rotation_x(0.5)));The rotation by
angleradians about +y.nabiuse spatial::{Mat3, Mat4}; assert(Mat4::from_rotation_y(0.5) == Mat4::from_mat3(Mat3::from_rotation_y(0.5)));The rotation by
angleradians about +z.nabiuse spatial::{Mat3, Mat4}; assert(Mat4::from_rotation_z(0.5) == Mat4::from_mat3(Mat3::from_rotation_z(0.5)));
Sourcepub fn from_scale_rotation_translation(§scale: Vec3<T>, rotation: Quat<T>, translation: Vec3<T>) -> Mat4<T> Scale by
scale, rotate byrotation, then move bytranslation: the model matrix of an object.nabiuse 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));The scale, rotation and translation of a matrix built by
from_scale_rotation_translation. A negative determinant puts the mirror inscale.x. Shear and projection are not represented.nabiuse 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);A right-handed view matrix: the camera at
eyelooks atcenter,uproughly up. View space has +x right, +y up and the camera looking down -z (OpenGL, Vulkan, Metal and WebGPU conventions; pair with a_rhprojection).nabiuse 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));A left-handed view matrix: +x right, +y up and the camera looking down +z (Direct3D's convention; pair with a
_lhprojection).nabiuse 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));A right-handed perspective projection with depth
0..1, for Vulkan, Metal, Direct3D and WebGPU:fov_yis the vertical field of view in radians,aspectwidth / height, and points at distancenearandfarin front of the camera (z = -near, -far) map to depth 0 and 1. Vulkan's clip space has +y down: flipy_axis.yor the viewport for it.nabiuse 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);A left-handed perspective projection with depth
0..1(Direct3D withlook_at_lh): points at z = near, far map to depth 0 and 1.nabiuse 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);A right-handed perspective projection with depth
-1..1, for OpenGL (gluPerspective): points at z = -near, -far map to depth -1 and 1.nabiuse 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);A left-handed perspective projection with depth
-1..1: points at z = near, far map to depth -1 and 1.nabiuse 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);A right-handed orthographic projection with depth
0..1: the boxleft..right,bottom..top, z =-near..-farmaps to x, y in-1..1and depth0..1.nabiuse 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));A left-handed orthographic projection with depth
0..1: z =near..farmaps to depth0..1.nabiuse 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);A right-handed orthographic projection with depth
-1..1, for OpenGL (glOrtho): z =-near..-farmaps to depth-1..1.nabiuse 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);A left-handed orthographic projection with depth
-1..1: z =near..farmaps to depth-1..1.nabiuse 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);The point
ptransformed withw = 1, translation included and no perspective divide: for model and view matrices. Useproject_pointthrough a projection.nabiuse spatial::{Mat4, Vec3}; assert(Mat4::from_translation(Vec3::X).transform_point(Vec3::Y) == Vec3::new(1.0, 1.0, 0.0));The direction
vtransformed withw = 0: translation left out.nabiuse spatial::{Mat4, Vec3}; assert(Mat4::from_translation(Vec3::X).transform_vector(Vec3::Y) == Vec3::Y);The point
pwithw = 1transformed, then divided by itsw: a point through a projection, to normalized device coordinates.nabiuse 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);Column
i(0 to 3).nabiassert(spatial::Mat4::IDENTITY.col(3) == spatial::Vec4::W);Row
i(0 to 3).nabiuse spatial::{Mat4, Vec3, Vec4}; assert(Mat4::from_translation(Vec3::ONE).row(0) == Vec4::new(1.0, 0.0, 0.0, 1.0));The transpose.
nabiuse spatial::{Mat4, Vec3}; let m = Mat4::from_translation(Vec3::ONE); assert(m.transpose().transpose() == m && m.transpose().x_axis.w == 1.0);The determinant.
nabiuse spatial::{Mat4, Vec3}; assert(Mat4::from_scale(Vec3::new(1.0, 2.0, 3.0)).determinant() == 6.0);The inverse. A singular matrix gives infinite or NaN entries;
try_inversegivesnonefor it instead. For a rigid transform (rotation and translation only)Affine3::inverseis cheaper.nabiuse 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));The inverse, or
nonewhen the determinant is zero or the result is not finite.nabiassert(spatial::Mat4::<f32>::zero().try_inverse() == none);Whether every entry is within
epsofo's.nabiuse spatial::Mat4; assert(Mat4::IDENTITY.approx_eq(Mat4::from_rotation_z(1e-7), 1e-6));Whether no entry is NaN or infinite.
nabiassert(spatial::Mat4::IDENTITY.is_finite());