use serde::{Deserialize, Serialize};
use bevy_math::{Mat4, Quat, Vec3};
use bevy_transform::components::Transform;
pub(crate) trait ConvertCoordinates {
fn convert_coordinates(self) -> Self;
}
impl ConvertCoordinates for Vec3 {
fn convert_coordinates(self) -> Self {
Vec3::new(-self.x, self.y, -self.z)
}
}
impl ConvertCoordinates for [f32; 3] {
fn convert_coordinates(self) -> Self {
[-self[0], self[1], -self[2]]
}
}
impl ConvertCoordinates for [f32; 4] {
fn convert_coordinates(self) -> Self {
[-self[0], self[1], -self[2], self[3]]
}
}
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)]
pub struct GltfConvertCoordinates {
pub rotate_scene_entity: bool,
pub rotate_meshes: bool,
}
impl GltfConvertCoordinates {
const CONVERSION_TRANSFORM: Transform =
Transform::from_rotation(Quat::from_xyzw(0.0, 1.0, 0.0, 0.0));
fn conversion_mat4() -> Mat4 {
Mat4::from_scale(Vec3::new(-1.0, 1.0, -1.0))
}
pub(crate) fn scene_conversion_transform(&self) -> Transform {
if self.rotate_scene_entity {
Self::CONVERSION_TRANSFORM
} else {
Transform::IDENTITY
}
}
pub(crate) fn mesh_conversion_transform(&self) -> Transform {
if self.rotate_meshes {
Self::CONVERSION_TRANSFORM
} else {
Transform::IDENTITY
}
}
pub(crate) fn mesh_conversion_transform_inverse(&self) -> Transform {
self.mesh_conversion_transform()
}
pub(crate) fn mesh_conversion_mat4(&self) -> Mat4 {
if self.rotate_meshes {
Self::conversion_mat4()
} else {
Mat4::IDENTITY
}
}
}