Skip to content

Commit

Permalink
add AnimationPlayer component only on scene roots that are also anima…
Browse files Browse the repository at this point in the history
…tion roots (bevyengine#4417)

# Objective

- Fix bevyengine#4416 
- The scene has two root nodes, with the second one being the animation root

## Solution

- Check all scene root nodes, and add the `AnimationPlayer` component to nodes that are also animation roots
  • Loading branch information
mockersf authored and aevyrie committed Jun 7, 2022
1 parent 557c54b commit 909f439
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,21 @@ async fn load_gltf<'a, 'b>(

#[cfg(feature = "bevy_animation")]
let paths = {
let mut paths = HashMap::<usize, Vec<Name>>::new();
let mut paths = HashMap::<usize, (usize, Vec<Name>)>::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
};

#[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() {
Expand Down Expand Up @@ -192,7 +194,8 @@ async fn load_gltf<'a, 'b>(
return Err(GltfError::MissingAnimationSampler(animation.index()));
};

if let Some(path) = paths.get(&node.index()) {
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(),
Expand All @@ -218,7 +221,7 @@ async fn load_gltf<'a, 'b>(
}
animations.push(handle);
}
(animations, named_animations)
(animations, named_animations, animation_roots)
};

let mut meshes = vec![];
Expand Down Expand Up @@ -470,10 +473,16 @@ 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.contains(&node.index()) {
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 {
Expand Down Expand Up @@ -529,13 +538,18 @@ fn node_name(node: &Node) -> Name {
Name::new(name)
}

fn paths_recur(node: Node, current_path: &[Name], paths: &mut HashMap<usize, Vec<Name>>) {
fn paths_recur(
node: Node,
current_path: &[Name],
paths: &mut HashMap<usize, (usize, Vec<Name>)>,
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.
Expand Down

0 comments on commit 909f439

Please sign in to comment.