Refactor from Mat4 to Affine3A

This commit is contained in:
Danny McGee
2024-01-18 19:18:00 -05:00
parent 5cee4482ef
commit 9bd6ea2240
2 changed files with 10 additions and 21 deletions
+3 -4
View File
@@ -73,7 +73,7 @@ fn compute_inverse_bindposes(
q_bvh_joints: Query<(Entity, &GlobalTransform), (With<BvhJoint>, Without<InvBindPose>)>, q_bvh_joints: Query<(Entity, &GlobalTransform), (With<BvhJoint>, Without<InvBindPose>)>,
) { ) {
for (ent, world_xform) in q_bvh_joints.iter() { for (ent, world_xform) in q_bvh_joints.iter() {
let inv_bind_pose = world_xform.compute_matrix().inverse(); let inv_bind_pose = world_xform.affine().inverse();
cmd.entity(ent).insert(InvBindPose(inv_bind_pose)); cmd.entity(ent).insert(InvBindPose(inv_bind_pose));
} }
} }
@@ -192,7 +192,6 @@ fn retarget_bvh(
>, >,
q_world_xforms: Query<&GlobalTransform>, q_world_xforms: Query<&GlobalTransform>,
) { ) {
// let retarget_map = RetargetMap::mocap_to_rigify();
for (source_name, source_inv_bind_pose, source_world_xform) in q_source_bones.iter() { for (source_name, source_inv_bind_pose, source_world_xform) in q_source_bones.iter() {
// Lookup the target name in the retarget map // Lookup the target name in the retarget map
let Some(target_name) = r_retarget_map.source_to_target.get(source_name.as_str()) else { let Some(target_name) = r_retarget_map.source_to_target.get(source_name.as_str()) else {
@@ -239,14 +238,14 @@ fn retarget_bvh(
// orthonormalize the matrices inbetween operations. // orthonormalize the matrices inbetween operations.
// [psuedo] local_mat = source_inv_bind_mat * source_world_mat; // [psuedo] local_mat = source_inv_bind_mat * source_world_mat;
let mut local_mat = source_world_xform.compute_matrix() * (**source_inv_bind_pose); let mut local_mat = source_world_xform.affine() * (**source_inv_bind_pose);
// [psuedo] local_mat = target_world_mat * local_mat * target_inv_parent_mat; // [psuedo] local_mat = target_world_mat * local_mat * target_inv_parent_mat;
let parent_world_xform = match q_world_xforms.get(**target_parent_ent) { let parent_world_xform = match q_world_xforms.get(**target_parent_ent) {
Ok(world_xform) => world_xform, Ok(world_xform) => world_xform,
Err(_) => &GlobalTransform::IDENTITY, Err(_) => &GlobalTransform::IDENTITY,
}; };
let target_inv_parent_mat = parent_world_xform.compute_matrix().inverse(); let target_inv_parent_mat = parent_world_xform.affine().inverse();
local_mat = target_inv_parent_mat * local_mat * (**target_bind_pose_global); local_mat = target_inv_parent_mat * local_mat * (**target_bind_pose_global);
// [psuedo] target_xform.rotation = local_mat.rotation; // [psuedo] target_xform.rotation = local_mat.rotation;
+7 -17
View File
@@ -6,14 +6,10 @@ use bevy::{
tonemapping::Tonemapping, tonemapping::Tonemapping,
}, },
gltf::Gltf, gltf::Gltf,
math::{vec2, vec3}, math::{vec2, vec3, Affine3A},
pbr::ShadowFilteringMethod, pbr::ShadowFilteringMethod,
prelude::*, prelude::*,
render::{ render::{mesh::skinning::SkinnedMesh, primitives::Aabb, view::NoFrustumCulling},
mesh::skinning::{SkinnedMesh, SkinnedMeshInverseBindposes},
primitives::Aabb,
view::NoFrustumCulling,
},
scene::InstanceId, scene::InstanceId,
}; };
@@ -127,10 +123,10 @@ struct TestBvh(Handle<Bvh>); // TODO
pub struct BindPoseLocal(pub Transform); pub struct BindPoseLocal(pub Transform);
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, Deref, DerefMut)]
pub struct BindPoseGlobal(pub Mat4); pub struct BindPoseGlobal(pub Affine3A);
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, Deref, DerefMut)]
pub struct InvBindPose(pub Mat4); pub struct InvBindPose(pub Affine3A);
fn initialize(mut cmd: Commands, r_assets: Res<AssetServer>) { fn initialize(mut cmd: Commands, r_assets: Res<AssetServer>) {
// Camera, graphics settings // Camera, graphics settings
@@ -282,7 +278,6 @@ fn tag_scene_elements(
r_scene_spawner: Res<SceneSpawner>, r_scene_spawner: Res<SceneSpawner>,
r_active_scene: Res<ActiveScene>, r_active_scene: Res<ActiveScene>,
ra_gltfs: Res<Assets<Gltf>>, ra_gltfs: Res<Assets<Gltf>>,
ra_inv_bindposes: Res<Assets<SkinnedMeshInverseBindposes>>,
// A "glTF mesh" is actually a container for one or more "glTF primitives", // A "glTF mesh" is actually a container for one or more "glTF primitives",
// which is basically a bundle of vertices paired with the material used to // which is basically a bundle of vertices paired with the material used to
// shade them. It's those "sub-meshes" that carry the actual mesh handles, // shade them. It's those "sub-meshes" that carry the actual mesh handles,
@@ -317,20 +312,15 @@ fn tag_scene_elements(
{ {
cmd.entity(ent).insert(NoFrustumCulling); cmd.entity(ent).insert(NoFrustumCulling);
let inv_bind_poses = ra_inv_bindposes for &joint in skinned_mesh.joints.iter() {
.get(skinned_mesh.inverse_bindposes.clone())
.unwrap();
for (idx, &joint) in skinned_mesh.joints.iter().enumerate() {
let inv_bind_pose = inv_bind_poses[idx];
let global_bind_pose = q_xforms.p0().compute_global_transform(joint).unwrap(); let global_bind_pose = q_xforms.p0().compute_global_transform(joint).unwrap();
let local_bind_pose = *q_xforms.p1().get(joint).unwrap(); let local_bind_pose = *q_xforms.p1().get(joint).unwrap();
cmd.entity(joint).insert(( cmd.entity(joint).insert((
SkeletalJoint(Color::BLACK), SkeletalJoint(Color::BLACK),
BindPoseLocal(local_bind_pose), BindPoseLocal(local_bind_pose),
BindPoseGlobal(global_bind_pose.compute_matrix()), BindPoseGlobal(global_bind_pose.affine()),
InvBindPose(inv_bind_pose), InvBindPose(global_bind_pose.affine().inverse()),
)); ));
} }
} }