pub struct Transform {
pub translation: Vec3,
pub rotation: Quat,
pub scale: Vec3,
}Expand description
Fields§
§translation: Vec3世界空间中的位置
rotation: Quat四元数表示的旋转
scale: Vec3各轴的缩放因子
Implementations§
Source§impl Transform
impl Transform
Sourcepub fn from_translation(translation: Vec3) -> Self
pub fn from_translation(translation: Vec3) -> Self
从位置创建变换
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let transform = Transform::from_translation(Vec3::new(1.0, 2.0, 3.0));
assert_eq!(transform.translation, Vec3::new(1.0, 2.0, 3.0));Sourcepub fn from_rotation(rotation: Quat) -> Self
pub fn from_rotation(rotation: Quat) -> Self
从旋转创建变换
§示例
use anvilkit_core::math::Transform;
use glam::Quat;
let rotation = Quat::from_rotation_y(std::f32::consts::PI / 4.0);
let transform = Transform::from_rotation(rotation);
assert_eq!(transform.rotation, rotation);Sourcepub fn from_scale(scale: Vec3) -> Self
pub fn from_scale(scale: Vec3) -> Self
从缩放创建变换
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let transform = Transform::from_scale(Vec3::splat(2.0));
assert_eq!(transform.scale, Vec3::splat(2.0));Sourcepub fn from_xyz(x: f32, y: f32, z: f32) -> Self
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self
从 XYZ 坐标创建变换
§示例
use anvilkit_core::math::Transform;
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
assert_eq!(transform.translation.x, 1.0);
assert_eq!(transform.translation.y, 2.0);
assert_eq!(transform.translation.z, 3.0);Sourcepub fn from_xy(x: f32, y: f32) -> Self
pub fn from_xy(x: f32, y: f32) -> Self
从 XY 坐标创建 2D 变换(Z = 0)
§示例
use anvilkit_core::math::Transform;
let transform = Transform::from_xy(1.0, 2.0);
assert_eq!(transform.translation.z, 0.0);Sourcepub fn with_translation(self, translation: Vec3) -> Self
pub fn with_translation(self, translation: Vec3) -> Self
设置位置(链式调用)
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let transform = Transform::IDENTITY
.with_translation(Vec3::new(1.0, 2.0, 3.0));Sourcepub fn with_rotation(self, rotation: Quat) -> Self
pub fn with_rotation(self, rotation: Quat) -> Self
设置旋转(链式调用)
Sourcepub fn with_scale(self, scale: Vec3) -> Self
pub fn with_scale(self, scale: Vec3) -> Self
设置缩放(链式调用)
Sourcepub fn compute_matrix(&self) -> Mat4
pub fn compute_matrix(&self) -> Mat4
将变换转换为 4x4 变换矩阵
矩阵的计算顺序为:缩放 → 旋转 → 平移
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
let matrix = transform.compute_matrix();
// 验证平移部分
assert_eq!(matrix.w_axis.truncate(), Vec3::new(1.0, 2.0, 3.0));Sourcepub fn transform_point(&self, point: Vec3) -> Vec3
pub fn transform_point(&self, point: Vec3) -> Vec3
应用变换到点
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
let point = Vec3::ZERO;
let transformed = transform.transform_point(point);
assert_eq!(transformed, Vec3::new(1.0, 2.0, 3.0));Sourcepub fn transform_vector(&self, vector: Vec3) -> Vec3
pub fn transform_vector(&self, vector: Vec3) -> Vec3
应用变换到向量(忽略平移)
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let transform = Transform::from_scale(Vec3::splat(2.0));
let vector = Vec3::new(1.0, 1.0, 1.0);
let transformed = transform.transform_vector(vector);
assert_eq!(transformed, Vec3::new(2.0, 2.0, 2.0));Sourcepub fn mul_transform(&self, other: &Transform) -> Transform
pub fn mul_transform(&self, other: &Transform) -> Transform
组合两个变换(self * other)
§示例
use anvilkit_core::math::Transform;
use glam::Vec3;
let parent = Transform::from_xyz(1.0, 0.0, 0.0);
let child = Transform::from_xyz(0.0, 1.0, 0.0);
let combined = parent.mul_transform(&child);
assert_eq!(combined.translation, Vec3::new(1.0, 1.0, 0.0));Sourcepub fn from_matrix(matrix: Mat4) -> Self
pub fn from_matrix(matrix: Mat4) -> Self
Trait Implementations§
Source§impl From<Transform> for GlobalTransform
impl From<Transform> for GlobalTransform
impl Copy for Transform
impl StructuralPartialEq for Transform
Auto Trait Implementations§
impl Freeze for Transform
impl RefUnwindSafe for Transform
impl Send for Transform
impl Sync for Transform
impl Unpin for Transform
impl UnsafeUnpin for Transform
impl UnwindSafe for Transform
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more