-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathmatrix.rs
More file actions
78 lines (68 loc) · 2.41 KB
/
matrix.rs
File metadata and controls
78 lines (68 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! a set of common SPIR-V Matrices, used for intrinsics
use glam::{Affine3A, Mat3, Mat3A, Mat4, Vec3, Vec3A};
/// A Matrix with 4 columns of [`Vec3`], very similar to glam's [`Affine3A`].
///
/// Primarily used in ray tracing extensions to represent object rotation, scale and translation.
#[derive(Clone, Copy, Default, PartialEq)]
#[repr(C)]
#[spirv(matrix)]
#[allow(missing_docs)]
pub struct Matrix4x3 {
pub x: Vec3A,
pub y: Vec3A,
pub z: Vec3A,
pub w: Vec3A,
}
/// The `from_*` fn signatures should match [`Affine3A`], to make it easier to switch to [`Affine3A`] later.
/// The `to_*` fn signatures are custom
impl Matrix4x3 {
/// Convert from glam's [`Affine3A`]
pub fn from_affine3a(affine: Affine3A) -> Self {
Self {
x: affine.x_axis,
y: affine.y_axis,
z: affine.z_axis,
w: affine.w_axis,
}
}
/// Creates an affine transform from a 3x3 matrix (expressing scale, shear and
/// rotation)
pub fn from_mat3(mat3: Mat3) -> Self {
Self::from_affine3a(Affine3A::from_mat3(mat3))
}
/// Creates an affine transform from a 3x3 matrix (expressing scale, shear and rotation)
/// and a translation vector.
///
/// Equivalent to `Affine3A::from_translation(translation) * Affine3A::from_mat3(mat3)`
pub fn from_mat3_translation(mat3: Mat3, translation: Vec3) -> Self {
Self::from_affine3a(Affine3A::from_mat3_translation(mat3, translation))
}
/// The given `Mat4` must be an affine transform,
/// i.e. contain no perspective transform.
pub fn from_mat4(m: Mat4) -> Self {
Self::from_affine3a(Affine3A::from_mat4(m))
}
/// Convert to glam's [`Affine3A`]
pub fn to_affine3a(self) -> Affine3A {
Affine3A {
matrix3: Mat3A {
x_axis: self.x,
y_axis: self.y,
z_axis: self.z,
},
translation: self.w,
}
}
/// Creates a 3x3 matrix representing the rotation and scale, cutting off the translation
pub fn to_mat3a(self) -> Mat3A {
self.to_affine3a().matrix3
}
/// Creates a 3x3 matrix representing the rotation and scale, cutting off the translation
pub fn to_mat3(self) -> Mat3 {
Mat3::from(self.to_mat3a())
}
/// Creates a 4x4 matrix from this affine transform
pub fn to_mat4(self) -> Mat4 {
Mat4::from(self.to_affine3a())
}
}