Change glTF loading behavior so that cameras face the right way

This commit is contained in:
Jan Hohenheim 2025-07-14 11:53:44 +02:00
parent 33bed5dd70
commit 476a3e0693
No known key found for this signature in database
2 changed files with 7 additions and 32 deletions

View File

@ -1,11 +1,11 @@
use core::f32::consts::PI;
use std::f32::consts::PI;
use bevy_math::{Mat4, Quat, Vec3};
use bevy_transform::components::Transform;
pub(crate) trait ConvertCoordinates {
/// Converts the glTF coordinates to Bevy's coordinate system.
/// - glTF:
/// Converts the glTF model coordinates to Bevy's coordinate system.
/// - glTF (models):
/// - forward: Z
/// - up: Y
/// - right: -X
@ -18,16 +18,6 @@ pub(crate) trait ConvertCoordinates {
fn convert_coordinates(self) -> Self;
}
pub(crate) trait ConvertCameraCoordinates {
/// Like `convert_coordinates`, but uses the following for the lens rotation:
/// - forward: -Z
/// - up: Y
/// - right: X
///
/// See <https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#view-matrix>
fn convert_camera_coordinates(self) -> Self;
}
impl ConvertCoordinates for Vec3 {
fn convert_coordinates(self) -> Self {
Vec3::new(-self.x, self.y, -self.z)
@ -66,15 +56,7 @@ impl ConvertCoordinates for Mat4 {
impl ConvertCoordinates for Transform {
fn convert_coordinates(mut self) -> Self {
self.translation = self.translation.convert_coordinates();
self.rotation = self.rotation.convert_coordinates();
self
}
}
impl ConvertCameraCoordinates for Transform {
fn convert_camera_coordinates(mut self) -> Self {
self.translation = self.translation.convert_coordinates();
self.rotate_y(PI);
self.rotation *= Quat::from_rotation_y(PI);
self
}
}

View File

@ -10,10 +10,7 @@ use itertools::Itertools;
#[cfg(feature = "bevy_animation")]
use bevy_platform::collections::{HashMap, HashSet};
use crate::{
convert_coordinates::{ConvertCameraCoordinates as _, ConvertCoordinates as _},
GltfError,
};
use crate::{convert_coordinates::ConvertCoordinates as _, GltfError};
pub(crate) fn node_name(node: &Node) -> Name {
let name = node
@ -44,12 +41,8 @@ pub(crate) fn node_transform(node: &Node, convert_coordinates: bool) -> Transfor
scale: Vec3::from(scale),
},
};
if convert_coordinates {
if node.camera().is_some() {
transform.convert_camera_coordinates()
} else {
transform.convert_coordinates()
}
if convert_coordinates && node.camera().is_none() && node.light().is_none() {
transform.convert_coordinates()
} else {
transform
}