-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
285 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use beet::prelude::*; | ||
use beet_examples::prelude::*; | ||
use bevy::animation::RepeatAnimation; | ||
use bevy::prelude::*; | ||
use std::time::Duration; | ||
|
||
|
||
pub fn animation_demo(mut commands: Commands) { | ||
// camera | ||
commands.spawn(( | ||
BundlePlaceholder::Camera3d, | ||
Transform::from_xyz(10.0, 10.0, 15.0) | ||
.looking_at(Vec3::new(0.0, 2.0, 0.0), Vec3::Y), | ||
)); | ||
|
||
let mut graph = AnimationGraphPlaceholder::default(); | ||
|
||
let anim1_clip = | ||
AssetPlaceholder::<AnimationClip>::new("Fox.glb#Animation0"); | ||
let anim1_index = graph.add_clip(anim1_clip.clone(), 1.0, graph.root); | ||
let anim2_clip = | ||
AssetPlaceholder::<AnimationClip>::new("Fox.glb#Animation1"); | ||
let anim2_index = graph.add_clip(anim2_clip.clone(), 1.0, graph.root); | ||
|
||
let transition_duration = Duration::from_secs_f32(0.5); | ||
|
||
commands | ||
.spawn(( | ||
Transform::from_scale(Vec3::splat(0.1)), | ||
BundlePlaceholder::Scene("Fox.glb#Scene0".into()), | ||
graph, | ||
AnimationTransitions::new(), | ||
)) | ||
.with_children(|parent| { | ||
let agent = parent.parent_entity(); | ||
parent | ||
.spawn(( | ||
Name::new("Animation Behavior"), | ||
Running, | ||
SequenceSelector, | ||
Repeat, | ||
)) | ||
.with_children(|parent| { | ||
parent.spawn(( | ||
Name::new("Idle"), | ||
TargetAgent(agent), | ||
PlayAnimation::new(anim1_index) | ||
.repeat(RepeatAnimation::Count(1)) | ||
.with_transition_duration(transition_duration), | ||
anim1_clip, | ||
InsertOnAnimationEnd::new( | ||
anim1_index, | ||
RunResult::Success, | ||
) | ||
.with_transition_duration(transition_duration), | ||
)); | ||
parent.spawn(( | ||
Name::new("Walking"), | ||
TargetAgent(agent), | ||
PlayAnimation::new(anim2_index) | ||
.repeat(RepeatAnimation::Count(4)) | ||
.with_transition_duration(transition_duration), | ||
anim2_clip, | ||
InsertOnAnimationEnd::new( | ||
anim2_index, | ||
RunResult::Success, | ||
) | ||
.with_transition_duration(transition_duration), | ||
)); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use beet_examples::prelude::*; | ||
use bevy::pbr::CascadeShadowConfigBuilder; | ||
use bevy::prelude::*; | ||
use std::f32::consts::PI; | ||
|
||
|
||
pub fn setup_ground_3d(mut commands: Commands) { | ||
commands.spawn(BundlePlaceholder::Pbr { | ||
mesh: MeshPlaceholder::Plane3d { | ||
plane: Plane3d::default(), | ||
width: 100., | ||
height: 100., | ||
}, | ||
material: MaterialPlaceholder::Color(Color::srgb(0.3, 0.5, 0.3)), | ||
}); | ||
} | ||
|
||
|
||
pub fn setup_lighting_3d(mut commands: Commands) { | ||
// 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: 20.0, | ||
maximum_distance: 40.0, | ||
..default() | ||
} | ||
.into(), | ||
..default() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
crates/beet_examples/src/serde_utils/animation_graph_placeholder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::*; | ||
use bevy::prelude::*; | ||
|
||
pub struct AnimationGraphPlaceholderPlugin; | ||
impl Plugin for AnimationGraphPlaceholderPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Update, init_animation_asset) | ||
.register_type::<AnimationGraphPlaceholder>(); | ||
} | ||
} | ||
|
||
|
||
#[derive(Debug, Default, Component, Reflect)] | ||
#[reflect(Default, Component)] | ||
pub struct AnimationGraphPlaceholder { | ||
pub root: AnimationNodeIndex, | ||
pub clips: Vec<AnimationClipPlaceholder>, | ||
} | ||
|
||
impl AnimationGraphPlaceholder { | ||
pub fn add_clip( | ||
&mut self, | ||
clip: AssetPlaceholder<AnimationClip>, | ||
weight: f32, | ||
parent: AnimationNodeIndex, | ||
) -> AnimationNodeIndex { | ||
self.clips.push(AnimationClipPlaceholder { | ||
clip, | ||
parent, | ||
weight, | ||
}); | ||
// thie first index is root | ||
// we can safely determine the indices because we control | ||
// the order in which they are added, see [`init_animation_asset`] | ||
AnimationNodeIndex::new(self.clips.len()) | ||
} | ||
} | ||
|
||
|
||
#[derive(Debug, Reflect)] | ||
pub struct AnimationClipPlaceholder { | ||
pub clip: AssetPlaceholder<AnimationClip>, | ||
pub parent: AnimationNodeIndex, | ||
pub weight: f32, | ||
} | ||
|
||
/// This works in unison with the [`AssetPlaceholderPlugin`] | ||
/// [`init_asset`] | ||
/// [`init_asset`]: super::init_asset | ||
pub fn init_animation_asset( | ||
mut commands: Commands, | ||
mut lookup: ResMut<AssetPlaceholderLookup<AnimationClip>>, | ||
mut asset_server: ResMut<AssetServer>, | ||
mut graphs: ResMut<Assets<AnimationGraph>>, | ||
query: Query< | ||
(Entity, &AnimationGraphPlaceholder), | ||
Added<AnimationGraphPlaceholder>, | ||
>, | ||
) { | ||
for (entity, placeholder) in query.iter() { | ||
let mut graph = AnimationGraph::new(); | ||
|
||
for clip in &placeholder.clips { | ||
let handle = | ||
lookup.get_or_create(&mut asset_server, &clip.clip.path); | ||
graph.add_clip(handle, clip.weight, clip.parent); | ||
} | ||
let handle = graphs.add(graph); | ||
|
||
commands | ||
.entity(entity) | ||
.insert(handle) | ||
.remove::<AnimationGraphPlaceholder>(); | ||
} | ||
} |
Oops, something went wrong.