tridify_rs/core/
transform.rs

1use glam::{Affine3A, Mat4, Quat, Vec3};
2
3/// Representation for position, rotation and scale.
4pub struct Transform {
5    affine: Affine3A,
6}
7
8impl Transform {
9    pub fn new(position: Vec3, rotation: Quat, scale: Vec3) -> Self {
10        Self {
11            affine: Affine3A::from_scale_rotation_translation(scale, rotation, position),
12        }
13    }
14
15    /// Create transform based only on position. Rotation and scale will have default values.
16    pub fn from_pos(position: Vec3) -> Self { Transform::new(position, Quat::IDENTITY, Vec3::ONE) }
17
18    /// Create transform based on position and view direction.
19    pub fn from_look_to(eye: Vec3, forward: Vec3, up: Vec3) -> Self {
20        Self {
21            affine: Affine3A::look_to_lh(eye, forward, up),
22        }
23    }
24
25    /// Create transform based on position and view point.
26    pub fn from_look_at(eye: Vec3, center: Vec3, up: Vec3) -> Self {
27        Self {
28            affine: Affine3A::look_at_lh(eye, center, up),
29        }
30    }
31
32    pub fn build_matrix(&self) -> Mat4 { Mat4::from(self.affine) }
33}
34impl Default for Transform {
35    fn default() -> Self { Transform::new(Vec3::ZERO, Quat::IDENTITY, Vec3::ONE) }
36}