Animation example + adding animaiton to GLTF inspector

main
Elijah Voigt 2 years ago
parent 8b257b1914
commit c7209ee246

@ -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"

@ -0,0 +1,75 @@
use std::f32::consts::PI;
use bevy::{pbr::CascadeShadowConfigBuilder, prelude::*};
#[derive(Resource)]
struct AnimationClips(Vec<Handle<AnimationClip>>);
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<AssetServer>) {
// 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<AnimationClips>,
mut player: Query<&mut AnimationPlayer>,
mut done: Local<bool>,
) {
if !*done {
if let Ok(mut player) = player.get_single_mut() {
player.play(animations.0[0].clone_weak()).repeat();
*done = true;
}
}
}

@ -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<Entity>);
///
@ -81,7 +84,7 @@ fn load_models(mut commands: Commands, ass: Res<AssetServer>) {
.iter()
.map(|weak| weak.clone().typed::<Gltf>())
.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<AssetEvent<AnimationClip>>,
mut player: Query<&mut AnimationPlayer>,
mut playing: Local<Vec<Handle<AnimationClip>>>,
clips: Res<Assets<AnimationClip>>,
) {
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<Input<MouseButton>>,
mut mouse_evr: EventReader<MouseMotion>,
@ -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)
let message = parents.iter().find(|&parent| {
parent_search
.iter_descendants(parent)
.find(|&entity| Some(entity) == *selected)
.is_some()
);
});
events.send(ManageActive(message));
}
}
@ -455,6 +483,7 @@ fn manage_active(
current: Query<Entity, With<Active>>,
) {
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);
}
}

Loading…
Cancel
Save