From 4d0911ecc5747265c4ccb955afb0fd8b8925028c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Mon, 4 Apr 2022 23:32:04 +0200 Subject: [PATCH 1/3] add AnimationPlayer component only on scene roots that are also animation roots --- crates/bevy_gltf/src/loader.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 4680cce54844d..4b706788bfd85 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -140,9 +140,10 @@ async fn load_gltf<'a, 'b>( }; #[cfg(feature = "bevy_animation")] - let (animations, named_animations) = { + let (animations, named_animations, animation_roots) = { let mut animations = vec![]; let mut named_animations = HashMap::default(); + let mut animation_roots = HashSet::default(); for animation in gltf.animations() { let mut animation_clip = AnimationClip::default(); for channel in animation.channels() { @@ -190,6 +191,7 @@ async fn load_gltf<'a, 'b>( }; if let Some(path) = paths.get(&node.index()) { + animation_roots.insert(path[0].clone()); animation_clip.add_curve_to_path( EntityPath { parts: path.clone(), @@ -215,7 +217,7 @@ async fn load_gltf<'a, 'b>( } animations.push(handle); } - (animations, named_animations) + (animations, named_animations, animation_roots) }; let mut meshes = vec![]; @@ -467,10 +469,20 @@ async fn load_gltf<'a, 'b>( } #[cfg(feature = "bevy_animation")] - if !animations.is_empty() { - world - .entity_mut(*node_index_to_entity_map.get(&0).unwrap()) - .insert(AnimationPlayer::default()); + { + // for each node root in a scene, check if it's the root of an animation + // if it is, add the AnimationPlayer component + for node in scene.nodes() { + if animation_roots + .iter() + .find(|name| name == &&node_name(&node)) + .is_some() + { + world + .entity_mut(*node_index_to_entity_map.get(&node.index()).unwrap()) + .insert(AnimationPlayer::default()); + } + } } for (&entity, &skin_index) in &entity_to_skin_index_map { From ce11a54c5f402964ad5ba66e1b473a4e460b7593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Mon, 4 Apr 2022 23:48:30 +0200 Subject: [PATCH 2/3] clippy :heart: --- crates/bevy_gltf/src/loader.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 4b706788bfd85..491eb94c5d803 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -473,11 +473,7 @@ async fn load_gltf<'a, 'b>( // for each node root in a scene, check if it's the root of an animation // if it is, add the AnimationPlayer component for node in scene.nodes() { - if animation_roots - .iter() - .find(|name| name == &&node_name(&node)) - .is_some() - { + if animation_roots.iter().any(|name| name == &node_name(&node)) { world .entity_mut(*node_index_to_entity_map.get(&node.index()).unwrap()) .insert(AnimationPlayer::default()); From 117d33874c463e6e697f0ae575fc2d0f250d29bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Tue, 5 Apr 2022 22:20:20 +0200 Subject: [PATCH 3/3] keep the root node index for animations --- crates/bevy_gltf/src/loader.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 491eb94c5d803..b0ddd3d7ab4c9 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -130,10 +130,11 @@ async fn load_gltf<'a, 'b>( #[cfg(feature = "bevy_animation")] let paths = { - let mut paths = HashMap::>::new(); + let mut paths = HashMap::)>::new(); for scene in gltf.scenes() { for node in scene.nodes() { - paths_recur(node, &[], &mut paths); + let root_index = node.index(); + paths_recur(node, &[], &mut paths, root_index); } } paths @@ -190,8 +191,8 @@ async fn load_gltf<'a, 'b>( return Err(GltfError::MissingAnimationSampler(animation.index())); }; - if let Some(path) = paths.get(&node.index()) { - animation_roots.insert(path[0].clone()); + if let Some((root_index, path)) = paths.get(&node.index()) { + animation_roots.insert(root_index); animation_clip.add_curve_to_path( EntityPath { parts: path.clone(), @@ -473,7 +474,7 @@ async fn load_gltf<'a, 'b>( // for each node root in a scene, check if it's the root of an animation // if it is, add the AnimationPlayer component for node in scene.nodes() { - if animation_roots.iter().any(|name| name == &node_name(&node)) { + if animation_roots.contains(&node.index()) { world .entity_mut(*node_index_to_entity_map.get(&node.index()).unwrap()) .insert(AnimationPlayer::default()); @@ -534,13 +535,18 @@ fn node_name(node: &Node) -> Name { Name::new(name) } -fn paths_recur(node: Node, current_path: &[Name], paths: &mut HashMap>) { +fn paths_recur( + node: Node, + current_path: &[Name], + paths: &mut HashMap)>, + root_index: usize, +) { let mut path = current_path.to_owned(); path.push(node_name(&node)); for child in node.children() { - paths_recur(child, &path, paths); + paths_recur(child, &path, paths, root_index); } - paths.insert(node.index(), path); + paths.insert(node.index(), (root_index, path)); } /// Loads a glTF texture as a bevy [`Image`] and returns it together with its label.