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   /// Translation in world space.
11   /// 世界空间中的平移。
12   pub position: Vec3,
13   /// Rotation in world space.
14   /// 世界空间中的旋转。
15   pub orientation: Quat,
16   /// Uniform or per-axis scale.
17   /// 均匀或按轴缩放。
18   pub scale: Vec3,
19}
20
21impl Transform {
22   /// Place an object at `position` with identity rotation and unit scale.
23   /// 在 `position` 处放置对象,旋转为单位旋转、缩放为单位缩放。
24   #[must_use]
25   pub const fn from_position(position: Vec3) -> Self {
26      Self {
27         position,
28         orientation: Quat::IDENTITY,
29         scale: Vec3::ONE,
30      }
31   }
32
33   /// Place an object at `position` with the given `orientation` and unit scale.
34   /// 在 `position` 处以给定的 `orientation` 放置对象,缩放为单位缩放。
35   #[must_use]
36   pub const fn from_position_orientation(position: Vec3, orientation: Quat) -> Self {
37      Self {
38         position,
39         orientation,
40         scale: Vec3::ONE,
41      }
42   }
43
44   /// Compose into a single model matrix (scale → rotate → translate).
45   /// 组合成单个模型矩阵(缩放 → 旋转 → 平移)。
46   #[inline]
47   #[must_use]
48   pub fn to_matrix(self) -> Mat4 {
49      Mat4::from_scale_rotation_translation(self.scale, self.orientation, self.position)
50   }
51
52   /// Local +Z transformed into world space.
53   /// 局部 +Z 轴变换到世界空间。
54   #[inline]
55   #[must_use]
56   pub fn forward(self) -> Vec3 {
57      self.orientation * Vec3::Z
58   }
59
60   /// Local +Y transformed into world space.
61   /// 局部 +Y 轴变换到世界空间。
62   #[inline]
63   #[must_use]
64   pub fn up(self) -> Vec3 {
65      self.orientation * Vec3::Y
66   }
67
68   /// Local +X transformed into world space.
69   /// 局部 +X 轴变换到世界空间。
70   #[inline]
71   #[must_use]
72   pub fn right(self) -> Vec3 {
73      self.orientation * Vec3::X
74   }
75}
76
77impl Default for Transform {
78   fn default() -> Self {
79      Self {
80         position: Vec3::ZERO,
81         orientation: Quat::IDENTITY,
82         scale: Vec3::ONE,
83      }
84   }
85}