bevy_math/
affine3.rs

1use glam::{Affine3A, Mat3, Vec3, Vec3Swizzles, Vec4};
2
3#[cfg(feature = "bevy_reflect")]
4use bevy_reflect::Reflect;
5
6/// Reduced-size version of `glam::Affine3A` for use when storage has
7/// significant performance impact. Convert to `glam::Affine3A` to do
8/// non-trivial calculations.
9#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
10pub struct Affine3 {
11    /// Scaling, rotation, shears, and other non-translation affine transforms
12    pub matrix3: Mat3,
13    /// Translation
14    pub translation: Vec3,
15}
16
17impl Affine3 {
18    /// Calculates the transpose of the affine 4x3 matrix to a 3x4 and formats it for packing into GPU buffers
19    #[inline]
20    pub fn to_transpose(&self) -> [Vec4; 3] {
21        let transpose_3x3 = self.matrix3.transpose();
22        [
23            transpose_3x3.x_axis.extend(self.translation.x),
24            transpose_3x3.y_axis.extend(self.translation.y),
25            transpose_3x3.z_axis.extend(self.translation.z),
26        ]
27    }
28
29    /// Calculates the inverse transpose of the 3x3 matrix and formats it for packing into GPU buffers
30    #[inline]
31    pub fn inverse_transpose_3x3(&self) -> ([Vec4; 2], f32) {
32        let inverse_transpose_3x3 = Affine3A::from(self).inverse().matrix3.transpose();
33        (
34            [
35                (inverse_transpose_3x3.x_axis, inverse_transpose_3x3.y_axis.x).into(),
36                (
37                    inverse_transpose_3x3.y_axis.yz(),
38                    inverse_transpose_3x3.z_axis.xy(),
39                )
40                    .into(),
41            ],
42            inverse_transpose_3x3.z_axis.z,
43        )
44    }
45}
46
47impl From<&Affine3A> for Affine3 {
48    fn from(affine: &Affine3A) -> Self {
49        Self {
50            matrix3: affine.matrix3.into(),
51            translation: affine.translation.into(),
52        }
53    }
54}
55
56impl From<&Affine3> for Affine3A {
57    fn from(affine3: &Affine3) -> Self {
58        Self {
59            matrix3: affine3.matrix3.into(),
60            translation: affine3.translation.into(),
61        }
62    }
63}