From 95c6eae828b79ff754bc3101ec1f735b7f8bc007 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Wed, 28 Jun 2023 08:48:08 -0700 Subject: [PATCH] Get selected animation working Animation API is good but incomplete. Cannot find animations related to an entity which means multiple animations would likely break this flow. With more precise asset control of Animations + Entities it would be fine, like in the example, but not "Load the GLTF and figure it out in the code" like I'm trying to do. --- bin/gltf-inspect.rs | 80 ++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/bin/gltf-inspect.rs b/bin/gltf-inspect.rs index 32d41ad..527a748 100644 --- a/bin/gltf-inspect.rs +++ b/bin/gltf-inspect.rs @@ -14,6 +14,7 @@ use bevy::{ Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, }, }, + utils::HashSet, }; fn main() { @@ -32,7 +33,8 @@ fn main() { .add_startup_system(spawn_base_ui) .add_system(spawn_models) .add_system(spawn_ui) - .add_system(spawn_animation) + .add_system(catalog_animations) + .add_system(play_animation) .add_system(rotate_model) .add_system(zoom_model) .add_system(scroll) @@ -45,7 +47,7 @@ fn main() { /// Stores GLTF handles for later use #[derive(Resource)] struct Models { - _handles: Vec>, + handles: Vec>, } /// @@ -80,12 +82,12 @@ struct ManageActive(Option); /// Load all GLTF models on startup fn load_models(mut commands: Commands, ass: Res) { let weak_handles = ass.load_folder("models").expect("Load gltfs"); - let _handles: Vec> = weak_handles + let handles: Vec> = weak_handles .iter() .map(|weak| weak.clone().typed::()) .collect(); - // info!("Scene Handles: {:#?}", _handles); - commands.insert_resource(Models { _handles }); + // info!("Scene Handles: {:#?}", handles); + commands.insert_resource(Models { handles }); } /// @@ -274,10 +276,10 @@ fn spawn_models( )); }); } - AssetEvent::Removed { handle: _handle } => { + AssetEvent::Removed { .. } => { todo!("Remove deleted scene") } - AssetEvent::Modified { handle: _handle } => { + AssetEvent::Modified { .. } => { todo!("Update modified scene") } } @@ -321,26 +323,55 @@ fn spawn_ui( } } -fn spawn_animation( - mut animation_evr: EventReader>, - mut player: Query<&mut AnimationPlayer>, - mut playing: Local>>, +fn catalog_animations( + mut events: EventReader>, clips: Res>, ) { - for event in animation_evr.iter() { + for event in events.iter() { match event { AssetEvent::Created { handle } => { - if !playing.contains(handle) { - if let Ok(mut player) = player.get_single_mut() { - player.start(handle.clone()).repeat(); - (*playing).push(handle.clone()); + if let Some(clip) = clips.get(handle) { + info!("Event: {:#?}", clip.curves()); + } + } + _ => (), + } + } +} + +fn play_animation( + mut events: EventReader, + mut players: Query<&mut AnimationPlayer>, + children: Query<&Children>, + models: Res, + gltfs: Res>, +) { + for event in events.iter() { + match event { + ManageActive(None) => { + info!("Disabling all animations"); + for mut player in players.iter_mut() { + player.stop_repeating(); + } + } + ManageActive(Some(entity)) => { + info!("Setting {:?} Active Animation", entity); + for child in children.iter_descendants(*entity) { + if let Ok(mut player) = players.get_mut(child) { + for gltf_handle in models.handles.iter() { + if let Some(gltf) = gltfs.get(gltf_handle) { + for animation_handle in gltf.animations.iter() { + player.start(animation_handle.clone()).repeat(); + } + } else { + info!("Failed to get GLTF handle"); + } + } } else { - info!("Could not load animation player"); + info!("Failed to get active animation player"); } } } - AssetEvent::Removed { .. } => (), - AssetEvent::Modified { .. } => (), } } } @@ -494,9 +525,14 @@ fn manage_active( } ManageActive(Some(entity)) => { for child in query.iter_descendants(*entity) { - if let Some(mut entity_commands) = commands.get_entity(child) { - // entity_commands.log_components(); - entity_commands.insert(Active); + if let Some(mut child_commands) = commands.get_entity(child) { + // info!( + // "Active entity components: {:?} {:?}", + // child, + // names.get(child) + // ); + // child_commands.log_components(); + child_commands.insert(Active); } } }