Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fox example #13

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Assets

- glTF animated fox from [glTF Sample Models][fox]
- Low poly fox [by PixelMannen] (CC0 1.0 Universal)
- Rigging and animation [by @tomkranis on Sketchfab] ([CC-BY 4.0])

[fox]: https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Fox
[by PixelMannen]: https://opengameart.org/content/fox-and-shiba
[by @tomkranis on Sketchfab]: https://sketchfab.com/models/371dea88d7e04a76af5763f2a36866bc
[CC-BY 4.0]: https://creativecommons.org/licenses/by/4.0/
5 changes: 5 additions & 0 deletions assets/animated_scenes/fox.animscn.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(
source: "models/Fox.glb#Scene0",
path_to_player: ["root"],
animation_graph: "animation_graphs/fox.animgraph.ron",
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(
source: "models/character_rigged.gltf#Scene0",
path_to_player: ["Main Controller"],
animation_graph: "animation_graphs/locomotion.animgraph.ron",
animation_graph: "animation_graphs/human.animgraph.ron",
)
35 changes: 35 additions & 0 deletions assets/animation_graphs/fox.animgraph.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(
nodes: [
(name: "Walk Clip", node: Clip("animations/fox_walk.anim.ron", None)),
(name: "Run Clip", node: Clip("animations/fox_run.anim.ron", None)),
(name: "Blend", node: Blend),
(name: "Loop Walk", node: Loop),
(name: "Loop Run", node: Loop),
(name: "Speed", node: Speed),

(name: "Param graph", node: Graph("animation_graphs/velocity_to_params.animgraph.ron")),
],
input_parameters: {
"Target Speed": F32(1.5),
"Blend Start": F32(0.5),
"Blend End": F32(1.5),
},
output_pose_spec: true,
input_parameter_edges: [
("Target Speed", ("Param graph", "Target Speed")),
("Blend Start", ("Param graph", "Blend Start")),
("Blend End", ("Param graph", "Blend End")),
],
output_pose_edge: Some("Speed"),
parameter_edges: [
(("Param graph", "blend_fac"),("Blend", "Factor")),
(("Param graph", "speed_fac"),("Speed", "Speed")),
],
pose_edges: [
("Walk Clip", ("Loop Walk", "Pose In")),
("Run Clip", ("Loop Run", "Pose In")),
("Loop Walk", ("Blend", "Pose In 1")),
("Loop Run", ("Blend", "Pose In 2")),
("Blend", ("Speed", "Pose In")),
],
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(
nodes: [
(name: "Walk Clip", node: Clip("animations/walk.anim.ron", Some(1.))),
(name: "Walk Clip 2", node: Clip("animations/walk.anim.ron", Some(1.))),
(name: "Run Clip", node: Clip("animations/run.anim.ron", Some(1.))),
(name: "Run Clip 2", node: Clip("animations/run.anim.ron", Some(1.))),
(name: "Walk Clip", node: Clip("animations/human_walk.anim.ron", Some(1.))),
(name: "Walk Clip 2", node: Clip("animations/human_walk.anim.ron", Some(1.))),
(name: "Run Clip", node: Clip("animations/human_run.anim.ron", Some(1.))),
(name: "Run Clip 2", node: Clip("animations/human_run.anim.ron", Some(1.))),
(name: "Walk Flip LR", node: FlipLR),
(name: "Run Flip LR", node: FlipLR),
(name: "Walk Chain", node: Chain),
Expand Down
6 changes: 6 additions & 0 deletions assets/animations/fox_run.anim.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(
source: GltfNamed(
path: "models/Fox.glb",
animation_name: "Run",
),
)
6 changes: 6 additions & 0 deletions assets/animations/fox_walk.anim.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(
source: GltfNamed(
path: "models/Fox.glb",
animation_name: "Walk",
),
)
File renamed without changes.
File renamed without changes.
Binary file added assets/models/Fox.glb
Binary file not shown.
111 changes: 111 additions & 0 deletions examples/fox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use bevy::{pbr::CascadeShadowConfigBuilder, prelude::*};
use bevy_animation_graph::core::animated_scene::{AnimatedSceneBundle, AnimatedSceneInstance};
use bevy_animation_graph::prelude::*;
use std::f32::consts::PI;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(AnimationGraphPlugin)
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.1,
})
.add_systems(Startup, setup)
.add_systems(Update, keyboard_animation_control)
.run();
}

#[derive(Component)]
struct Human;

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(3., 3., 3.).looking_at(Vec3::new(0.0, 0.875, 0.0), Vec3::Y),
..default()
});

// Plane
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(500000.0).into()),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});

// Light
commands.spawn(DirectionalLightBundle {
transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
directional_light: DirectionalLight {
shadows_enabled: true,
..default()
},
cascade_shadow_config: CascadeShadowConfigBuilder {
first_cascade_far_bound: 10.0,
num_cascades: 3,
minimum_distance: 0.3,
maximum_distance: 100.0,
..default()
}
.into(),
..default()
});

// Animated character
commands.spawn((
AnimatedSceneBundle {
animated_scene: asset_server.load("animated_scenes/fox.animscn.ron"),
transform: Transform::from_scale(Vec3::splat(0.01)),
..default()
},
Human,
));

println!("Controls:");
println!("\tSPACE: Play/Pause animation");
println!("\tR: Reset animation");
println!("\tUp/Down: Increase/decrease movement speed");
}

fn keyboard_animation_control(
keyboard_input: Res<Input<KeyCode>>,
human_character: Query<&AnimatedSceneInstance, With<Human>>,
mut animation_players: Query<&mut AnimationGraphPlayer>,
mut velocity: Local<f32>,
time: Res<Time>,
) {
let Ok(AnimatedSceneInstance { player_entity }) = human_character.get_single() else {
return;
};

let Ok(mut player) = animation_players.get_mut(*player_entity) else {
return;
};

if keyboard_input.just_pressed(KeyCode::Space) {
if player.is_paused() {
player.resume();
} else {
player.pause();
}
}
if keyboard_input.just_pressed(KeyCode::R) {
player.reset();
}

if keyboard_input.pressed(KeyCode::Up) {
*velocity += 0.5 * time.delta_seconds();
}
if keyboard_input.pressed(KeyCode::Down) {
*velocity -= 0.5 * time.delta_seconds();
}

*velocity = velocity.max(0.);

player.set_input_parameter("Target Speed", (*velocity).into());
}
2 changes: 1 addition & 1 deletion examples/human.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn setup(
// Animated character
commands.spawn((
AnimatedSceneBundle {
animated_scene: asset_server.load("animated_scenes/character.animscn.ron"),
animated_scene: asset_server.load("animated_scenes/human.animscn.ron"),
transform: Transform::from_xyz(0., 0., 0.),
..default()
},
Expand Down