use glam::{Mat4, Quat, Vec3};
use crate::{
transforms::{Transform, TransformKey},
AwsmRenderer,
};
use super::GltfPopulateContext;
use crate::gltf::error::Result;
impl AwsmRenderer {
pub(super) fn populate_gltf_node_transform<'a, 'b: 'a, 'c: 'a>(
&'a mut self,
ctx: &'c GltfPopulateContext,
gltf_node: &'b gltf::Node<'b>,
parent_transform_key: Option<TransformKey>,
) -> Result<()> {
let transform = transform_gltf_node(gltf_node);
let transform_key = self.transforms.insert(transform, parent_transform_key);
ctx.key_lookups
.lock()
.unwrap()
.insert_transform(gltf_node, transform_key);
for child in gltf_node.children() {
self.populate_gltf_node_transform(ctx, &child, Some(transform_key))?;
}
Ok(())
}
}
pub fn transform_gltf_node(node: &gltf::Node) -> Transform {
match node.transform() {
gltf::scene::Transform::Matrix {
matrix: gltf_matrix,
} => {
let matrix: Mat4 = Mat4::from_cols_array_2d(&gltf_matrix);
Transform::from(matrix)
}
gltf::scene::Transform::Decomposed {
translation,
rotation,
scale,
} => Transform::from(
glam::Mat4::from_translation(Vec3::from_array(translation))
* glam::Mat4::from_quat(Quat::from_array(rotation))
* glam::Mat4::from_scale(Vec3::from_array(scale)),
),
}
}