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

Implement the AnimationGraph, allowing for multiple animations to be blended together. #11989

Merged
merged 44 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
70983aa
Implement the `AnimationGraph`, allowing for multiple animations to be
pcwalton Feb 19, 2024
e7d9148
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Feb 28, 2024
a80931a
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Feb 28, 2024
32791b5
Add transition behavior back
pcwalton Feb 28, 2024
bad1df8
Fix `morph_targets` example
pcwalton Feb 28, 2024
a7f8651
Fix `irradiance_volumes` example
pcwalton Feb 28, 2024
48e89e7
Fix the `animated_transform` example
pcwalton Feb 28, 2024
9008072
Update `many_foxes`
pcwalton Feb 28, 2024
1424fa1
Introduce an animation graph example.
pcwalton Feb 28, 2024
43f5c36
Use the more convenient APIs in the examples
pcwalton Feb 28, 2024
c70e269
Add some API documentation
pcwalton Feb 28, 2024
1cda046
Update `README.md`
pcwalton Feb 28, 2024
fc581f8
wip
pcwalton Feb 29, 2024
8a09a1d
Flesh out the example
pcwalton Mar 1, 2024
bce3f06
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 1, 2024
3982f8c
Minor improvements
pcwalton Mar 1, 2024
75f774f
Fill out the remaining documentation
pcwalton Mar 1, 2024
3c1b721
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 1, 2024
b68b33c
Doc-check police
pcwalton Mar 1, 2024
5864ebc
Address review comment
pcwalton Mar 1, 2024
91ebf5b
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 3, 2024
4bff644
RelativeCursorPosition
rparrett Mar 2, 2024
84658d8
Load all clips alongside the animation graph
pcwalton Mar 4, 2024
3b48487
Rename `asset_paths` to `manifest`
pcwalton Mar 4, 2024
7347ef9
Address review comments
pcwalton Mar 4, 2024
2a76e9b
Address review comments
pcwalton Mar 4, 2024
15536db
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 4, 2024
6628857
Rustfmt police
pcwalton Mar 4, 2024
2ec8fd3
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 4, 2024
f78861f
Serialize asset paths directly as @cart suggested instead of using asset
pcwalton Mar 4, 2024
ffe7494
Remove debug code
pcwalton Mar 4, 2024
a32e3a3
Address review comments
pcwalton Mar 5, 2024
10879a1
wasm32 fixes
pcwalton Mar 5, 2024
e4de00c
Add normalization to `AnimationTransitions`
pcwalton Mar 5, 2024
283a4d0
Make animation clip application order-independent
pcwalton Mar 5, 2024
532422c
Fix `animated_fox`
pcwalton Mar 5, 2024
a58ed81
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 5, 2024
5dcd0f9
Example tidying
rparrett Mar 4, 2024
5ba4f31
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 6, 2024
64cc20a
Switch to `Local<ThreadLocal<RefCell<AnimationGraphEvaluator>>>`
pcwalton Mar 7, 2024
4df1de9
Add `#[reflect(Serialize, Debug)]` to `AnimationGraph`
pcwalton Mar 7, 2024
b8c1c47
Switch to a BTreeMap
pcwalton Mar 7, 2024
6927108
Merge remote-tracking branch 'origin/main' into animation-graph-asset
pcwalton Mar 7, 2024
bf9ab8b
Demote the warning to trace since it happens regularly when loading glTF
pcwalton Mar 7, 2024
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
5 changes: 5 additions & 0 deletions crates/bevy_animation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ keywords = ["bevy"]
bevy_app = { path = "../bevy_app", version = "0.13.0" }
bevy_asset = { path = "../bevy_asset", version = "0.13.0" }
bevy_core = { path = "../bevy_core", version = "0.13.0" }
bevy_derive = { path = "../bevy_derive", version = "0.13.0" }
bevy_log = { path = "../bevy_log", version = "0.13.0" }
bevy_math = { path = "../bevy_math", version = "0.13.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.13.0", features = [
"bevy",
"petgraph",
] }
bevy_render = { path = "../bevy_render", version = "0.13.0" }
bevy_time = { path = "../bevy_time", version = "0.13.0" }
Expand All @@ -29,5 +31,8 @@ bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.13.0" }
sha1_smol = { version = "1.0" }
uuid = { version = "1.7", features = ["v5"] }

# other
petgraph = "0.6"

[lints]
workspace = true
109 changes: 109 additions & 0 deletions crates/bevy_animation/src/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! The animation graph.

use std::ops::{Index, IndexMut};

use bevy_asset::{Asset, Handle};
use bevy_reflect::Reflect;
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::visit::{Dfs, Visitable};
use petgraph::Graph;

use crate::AnimationClip;

#[derive(Asset, Reflect, Debug)]
pub struct AnimationGraph {
#[reflect(ignore)]
pcwalton marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) graph: AnimationDiGraph,
#[reflect(ignore)]
pub root: NodeIndex,
// Cached topological ordering.
//#[reflect(ignore)]
//schedule: Vec<AnimationNodeIndex>,
}

pub type AnimationDiGraph = DiGraph<AnimationGraphNode, (), u32>;

pub type AnimationNodeIndex = NodeIndex<u32>;

#[derive(Reflect, Debug)]
pub struct AnimationGraphNode {
pub clip: Option<Handle<AnimationClip>>,
pub weight: f32,
}

impl AnimationGraph {
pub fn new() -> Self {
let mut graph = DiGraph::default();
let root = graph.add_node(AnimationGraphNode::default());
Self { graph, root }
}

pub fn add_clip(
&mut self,
clip: Handle<AnimationClip>,
weight: f32,
parent: AnimationNodeIndex,
) -> AnimationNodeIndex {
let node_index = self.graph.add_node(AnimationGraphNode {
clip: Some(clip),
weight,
});
self.graph.add_edge(parent, node_index, ());
node_index
}

pub fn add_blend(&mut self, weight: f32, parent: AnimationNodeIndex) -> AnimationNodeIndex {
let node_index = self
.graph
.add_node(AnimationGraphNode { clip: None, weight });
self.graph.add_edge(parent, node_index, ());
node_index
}

pub fn add_edge(&mut self, from: NodeIndex, to: NodeIndex) {
self.graph.add_edge(from, to, ());
}

pub fn get(&self, animation: AnimationNodeIndex) -> Option<&AnimationGraphNode> {
self.graph.node_weight(animation)
}

pub fn get_mut(&mut self, animation: AnimationNodeIndex) -> Option<&mut AnimationGraphNode> {
self.graph.node_weight_mut(animation)
}

pub fn dfs(
pcwalton marked this conversation as resolved.
Show resolved Hide resolved
&self,
) -> Dfs<AnimationNodeIndex, <Graph<AnimationGraphNode, ()> as Visitable>::Map> {
Dfs::new(&self.graph, self.root)
}
}

impl Index<AnimationNodeIndex> for AnimationGraph {
type Output = AnimationGraphNode;

fn index(&self, index: AnimationNodeIndex) -> &Self::Output {
&self.graph[index]
}
}

impl IndexMut<AnimationNodeIndex> for AnimationGraph {
fn index_mut(&mut self, index: AnimationNodeIndex) -> &mut Self::Output {
&mut self.graph[index]
}
}

impl Default for AnimationGraphNode {
fn default() -> Self {
Self {
clip: None,
weight: 1.0,
}
}
}

impl Default for AnimationGraph {
fn default() -> Self {
Self::new()
}
}
Loading
Loading