Skip to main content

he3d_core/
transform.rs

1use he3d_math::{Mat4, Quat, Vec3};
2
3/// A world-space placement: position, orientation, and (optionally) scale.
4/// 一个世界空间中的放置:位置、朝向和(可选的)缩放。
5///
6/// Scale defaults to one and is intentionally distinct from position so a renderer or physics step can ignore it without ambiguity.
7/// 缩放默认为 1,并刻意与位置区分开,以便渲染器或物理步进能够毫无歧义地忽略它。
8#[derive(Clone, Copy, PartialEq, Debug)]
9pub struct Transform {
10   pub position: Vec3,
11   pub orientation: Quat,
12   pub scale: Vec3,
13}
14
15impl Transform {
16   pub const fn from_position(position: Vec3) -> Self {
17      Self {
18         position,
19         orientation: Quat::IDENTITY,
20         scale: Vec3::ONE,
21      }
22   }
23
24   pub const fn from_position_orientation(position: Vec3, orientation: Quat) -> Self {
25      Self {
26         position,
27         orientation,
28         scale: Vec3::ONE,
29      }
30   }
31
32   /// Compose into a single model matrix (scale → rotate → translate).
33   /// 组合成单个模型矩阵(缩放 → 旋转 → 平移)。
34   #[inline]
35   pub fn to_matrix(self) -> Mat4 {
36      Mat4::from_scale_rotation_translation(self.scale, self.orientation, self.position)
37   }
38
39   /// Local +Z transformed into world space.
40   /// 局部 +Z 轴变换到世界空间。
41   #[inline]
42   pub fn forward(self) -> Vec3 {
43      self.orientation * Vec3::Z
44   }
45
46   /// Local +Y transformed into world space.
47   /// 局部 +Y 轴变换到世界空间。
48   #[inline]
49   pub fn up(self) -> Vec3 {
50      self.orientation * Vec3::Y
51   }
52
53   /// Local +X transformed into world space.
54   /// 局部 +X 轴变换到世界空间。
55   #[inline]
56   pub fn right(self) -> Vec3 {
57      self.orientation * Vec3::X
58   }
59}
60
61impl Default for Transform {
62   fn default() -> Self {
63      Self {
64         position: Vec3::ZERO,
65         orientation: Quat::IDENTITY,
66         scale: Vec3::ONE,
67      }
68   }
69}