diff --git a/Cargo.toml b/Cargo.toml index 0c1b353..10953c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,9 @@ path = "bin/camera-image-ui.rs" name = "ui-wtf" path = "bin/ui-wtf.rs" +[[bin]] +name = "animation-wtf" +path = "bin/animation-wtf.rs" + [dependencies] bevy = "0.10" diff --git a/bin/animation-wtf.rs b/bin/animation-wtf.rs new file mode 100644 index 0000000..61ea428 --- /dev/null +++ b/bin/animation-wtf.rs @@ -0,0 +1,75 @@ +use std::f32::consts::PI; + +use bevy::{pbr::CascadeShadowConfigBuilder, prelude::*}; + +#[derive(Resource)] +struct AnimationClips(Vec>); + +fn main() { + App::new() + .add_plugins(DefaultPlugins.set(WindowPlugin { + primary_window: Some(Window { + title: "Animation WTF".into(), + resolution: (640., 480.).into(), + ..default() + }), + ..default() + })) + .add_startup_system(init) + .add_system(spawn_animation) + .run(); +} + +fn init(mut commands: Commands, server: Res) { + // Load gltf/animation assets + commands.insert_resource(AnimationClips(vec![ + server.load("models/monkey-nod.glb#Animation0") + ])); + + commands + .spawn(SpatialBundle { ..default() }) + .with_children(|parent| { + // spawn camera + parent.spawn(Camera3dBundle { + transform: Transform::from_xyz(10.0, 10.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }); + + parent.spawn(DirectionalLightBundle { + transform: Transform::from_rotation(Quat::from_euler( + EulerRot::ZYX, + 0.0, + 1.0, + -PI / 4.0, + )), + directional_light: DirectionalLight { + shadows_enabled: true, + ..default() + }, + cascade_shadow_config: CascadeShadowConfigBuilder { + first_cascade_far_bound: 20.0, + maximum_distance: 40.0, + ..default() + } + .into(), + ..default() + }); + parent.spawn(SceneBundle { + scene: server.load("models/monkey-nod.glb#Scene0"), + ..default() + }); + }); +} + +fn spawn_animation( + animations: Res, + mut player: Query<&mut AnimationPlayer>, + mut done: Local, +) { + if !*done { + if let Ok(mut player) = player.get_single_mut() { + player.play(animations.0[0].clone_weak()).repeat(); + *done = true; + } + } +} diff --git a/bin/gltf-inspect.rs b/bin/gltf-inspect.rs index 776afa6..32d41ad 100644 --- a/bin/gltf-inspect.rs +++ b/bin/gltf-inspect.rs @@ -32,6 +32,7 @@ fn main() { .add_startup_system(spawn_base_ui) .add_system(spawn_models) .add_system(spawn_ui) + .add_system(spawn_animation) .add_system(rotate_model) .add_system(zoom_model) .add_system(scroll) @@ -70,7 +71,9 @@ struct Container; #[derive(Component)] struct Active; -// Event +/// +/// Event for managing which entities are tagged "Active" +#[derive(Debug)] struct ManageActive(Option); /// @@ -81,7 +84,7 @@ fn load_models(mut commands: Commands, ass: Res) { .iter() .map(|weak| weak.clone().typed::()) .collect(); - info!("Scene Handles: {:#?}", _handles); + // info!("Scene Handles: {:#?}", _handles); commands.insert_resource(Models { _handles }); } @@ -318,9 +321,33 @@ fn spawn_ui( } } +fn spawn_animation( + mut animation_evr: EventReader>, + mut player: Query<&mut AnimationPlayer>, + mut playing: Local>>, + clips: Res>, +) { + for event in animation_evr.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()); + } else { + info!("Could not load animation player"); + } + } + } + AssetEvent::Removed { .. } => (), + AssetEvent::Modified { .. } => (), + } + } +} + /// /// Rotate a model as part of inspection -/// TODO: Only modify selected entities +/// TODO: move light with model (want to see shadows) fn rotate_model( buttons: Res>, mut mouse_evr: EventReader, @@ -410,11 +437,12 @@ fn select( scene_cam.is_active = true; // Set relevant entities active - let message = parents.iter() - .find(|&parent| parent_search.iter_descendants(parent) - .find(|&entity| Some(entity) == *selected) - .is_some() - ); + let message = parents.iter().find(|&parent| { + parent_search + .iter_descendants(parent) + .find(|&entity| Some(entity) == *selected) + .is_some() + }); events.send(ManageActive(message)); } } @@ -426,7 +454,7 @@ fn select( .. } => { if let Some(s) = *selected { - // Set all inactive + // Set all inactive events.send(ManageActive(None)); // Disable scene camera @@ -455,6 +483,7 @@ fn manage_active( current: Query>, ) { for event in events.iter() { + info!("Setting active: {:?}", event); match event { ManageActive(None) => { for entity in current.iter() { @@ -466,8 +495,7 @@ fn manage_active( ManageActive(Some(entity)) => { for child in query.iter_descendants(*entity) { if let Some(mut entity_commands) = commands.get_entity(child) { - info!("Setting active: {:?}", child); - entity_commands.log_components(); + // entity_commands.log_components(); entity_commands.insert(Active); } }