Path: blob/main/crates/bevy_gltf/src/convert_coordinates.rs
6595 views
use core::f32::consts::PI;12use bevy_math::{Mat4, Quat, Vec3};3use bevy_transform::components::Transform;45pub(crate) trait ConvertCoordinates {6/// Converts the glTF coordinates to Bevy's coordinate system.7/// - glTF:8/// - forward: Z9/// - up: Y10/// - right: -X11/// - Bevy:12/// - forward: -Z13/// - up: Y14/// - right: X15///16/// See <https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#coordinate-system-and-units>17fn convert_coordinates(self) -> Self;18}1920pub(crate) trait ConvertCameraCoordinates {21/// Like `convert_coordinates`, but uses the following for the lens rotation:22/// - forward: -Z23/// - up: Y24/// - right: X25///26/// The same convention is used for lights.27/// See <https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#view-matrix>28fn convert_camera_coordinates(self) -> Self;29}3031impl ConvertCoordinates for Vec3 {32fn convert_coordinates(self) -> Self {33Vec3::new(-self.x, self.y, -self.z)34}35}3637impl ConvertCoordinates for [f32; 3] {38fn convert_coordinates(self) -> Self {39[-self[0], self[1], -self[2]]40}41}4243impl ConvertCoordinates for [f32; 4] {44fn convert_coordinates(self) -> Self {45// Solution of q' = r q r*46[-self[0], self[1], -self[2], self[3]]47}48}4950impl ConvertCoordinates for Quat {51fn convert_coordinates(self) -> Self {52// Solution of q' = r q r*53Quat::from_array([-self.x, self.y, -self.z, self.w])54}55}5657impl ConvertCoordinates for Mat4 {58fn convert_coordinates(self) -> Self {59let m: Mat4 = Mat4::from_scale(Vec3::new(-1.0, 1.0, -1.0));60// Same as the original matrix61let m_inv = m;62m_inv * self * m63}64}6566impl ConvertCoordinates for Transform {67fn convert_coordinates(mut self) -> Self {68self.translation = self.translation.convert_coordinates();69self.rotation = self.rotation.convert_coordinates();70self71}72}7374impl ConvertCameraCoordinates for Transform {75fn convert_camera_coordinates(mut self) -> Self {76self.translation = self.translation.convert_coordinates();77self.rotate_y(PI);78self79}80}818283