Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_gltf/src/convert_coordinates.rs
6595 views
1
use core::f32::consts::PI;
2
3
use bevy_math::{Mat4, Quat, Vec3};
4
use bevy_transform::components::Transform;
5
6
pub(crate) trait ConvertCoordinates {
7
/// Converts the glTF coordinates to Bevy's coordinate system.
8
/// - glTF:
9
/// - forward: Z
10
/// - up: Y
11
/// - right: -X
12
/// - Bevy:
13
/// - forward: -Z
14
/// - up: Y
15
/// - right: X
16
///
17
/// See <https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#coordinate-system-and-units>
18
fn convert_coordinates(self) -> Self;
19
}
20
21
pub(crate) trait ConvertCameraCoordinates {
22
/// Like `convert_coordinates`, but uses the following for the lens rotation:
23
/// - forward: -Z
24
/// - up: Y
25
/// - right: X
26
///
27
/// The same convention is used for lights.
28
/// See <https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#view-matrix>
29
fn convert_camera_coordinates(self) -> Self;
30
}
31
32
impl ConvertCoordinates for Vec3 {
33
fn convert_coordinates(self) -> Self {
34
Vec3::new(-self.x, self.y, -self.z)
35
}
36
}
37
38
impl ConvertCoordinates for [f32; 3] {
39
fn convert_coordinates(self) -> Self {
40
[-self[0], self[1], -self[2]]
41
}
42
}
43
44
impl ConvertCoordinates for [f32; 4] {
45
fn convert_coordinates(self) -> Self {
46
// Solution of q' = r q r*
47
[-self[0], self[1], -self[2], self[3]]
48
}
49
}
50
51
impl ConvertCoordinates for Quat {
52
fn convert_coordinates(self) -> Self {
53
// Solution of q' = r q r*
54
Quat::from_array([-self.x, self.y, -self.z, self.w])
55
}
56
}
57
58
impl ConvertCoordinates for Mat4 {
59
fn convert_coordinates(self) -> Self {
60
let m: Mat4 = Mat4::from_scale(Vec3::new(-1.0, 1.0, -1.0));
61
// Same as the original matrix
62
let m_inv = m;
63
m_inv * self * m
64
}
65
}
66
67
impl ConvertCoordinates for Transform {
68
fn convert_coordinates(mut self) -> Self {
69
self.translation = self.translation.convert_coordinates();
70
self.rotation = self.rotation.convert_coordinates();
71
self
72
}
73
}
74
75
impl ConvertCameraCoordinates for Transform {
76
fn convert_camera_coordinates(mut self) -> Self {
77
self.translation = self.translation.convert_coordinates();
78
self.rotate_y(PI);
79
self
80
}
81
}
82
83