Skip to content

Commit

Permalink
More node types (#67)
Browse files Browse the repository at this point in the history
Adds:
- `ConstEntityPath`
- `NormalizeVec3`
- `LengthVec3`
- `ConstVec3`
  • Loading branch information
aecsocket authored Sep 24, 2024
1 parent 2487d3e commit 13485e9
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 12 deletions.
21 changes: 17 additions & 4 deletions crates/bevy_animation_graph/src/core/animation_graph/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use crate::{
core::{animation_clip::GraphClip, errors::AssetLoaderError},
nodes::{
AbsF32, AddF32, BlendNode, BuildVec3Node, ChainNode, ClampF32, ClipNode, CompareF32,
ConstBool, ConstF32, DecomposeVec3Node, DivF32, FSMNode, FireEventNode, FlipLRNode,
FromEulerNode, GraphNode, IntoEulerNode, InvertQuatNode, LerpVec3Node, LoopNode, MulF32,
MulQuatNode, PaddingNode, RotationArcNode, RotationNode, SelectF32, SlerpQuatNode,
SpeedNode, SubF32, TwoBoneIKNode,
ConstBool, ConstEntityPath, ConstF32, ConstVec3Node, DecomposeVec3Node, DivF32, FSMNode,
FireEventNode, FlipLRNode, FromEulerNode, GraphNode, IntoEulerNode, InvertQuatNode,
LengthVec3Node, LerpVec3Node, LoopNode, MulF32, MulQuatNode, NormalizeVec3Node,
PaddingNode, RotationArcNode, RotationNode, SelectF32, SlerpQuatNode, SpeedNode, SubF32,
TwoBoneIKNode,
},
prelude::DummyNode,
};
Expand Down Expand Up @@ -218,6 +219,18 @@ impl AssetLoader for AnimationGraphLoader {
InvertQuatNode::new().wrapped(&serial_node.name)
}
AnimationNodeTypeSerial::SelectF32 => SelectF32::new().wrapped(&serial_node.name),
AnimationNodeTypeSerial::ConstEntityPath(n) => {
ConstEntityPath::new(n.clone()).wrapped(&serial_node.name)
}
AnimationNodeTypeSerial::NormalizeVec3 => {
NormalizeVec3Node::new().wrapped(&serial_node.name)
}
AnimationNodeTypeSerial::LengthVec3 => {
LengthVec3Node::new().wrapped(&serial_node.name)
}
AnimationNodeTypeSerial::ConstVec3(n) => {
ConstVec3Node::new(*n).wrapped(&serial_node.name)
}
};
graph.add_node(node);
}
Expand Down
20 changes: 18 additions & 2 deletions crates/bevy_animation_graph/src/core/animation_graph/serial.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use super::{pin, AnimationGraph, Extra};
use crate::{
core::{animation_clip::Interpolation, edge_data::AnimationEvent},
core::{
animation_clip::{EntityPath, Interpolation},
edge_data::AnimationEvent,
},
flipping::config::FlipConfig,
nodes::{BlendMode, BlendSyncMode, ChainDecay, CompareOp, RotationMode, RotationSpace},
prelude::{AnimationNode, AnimationNodeType, DataSpec, DataValue},
utils::ordered_map::OrderedMap,
};
use bevy::{math::EulerRot, utils::HashMap};
use bevy::{
math::{EulerRot, Vec3},
utils::HashMap,
};
use serde::{Deserialize, Serialize};
// pub nodes: HashMap<String, AnimationNode>,
// /// Inverted, indexed by output node name.
Expand Down Expand Up @@ -112,6 +118,10 @@ pub enum AnimationNodeTypeSerial {
Dummy,
Fsm(String),
Graph(String),
ConstEntityPath(EntityPath),
NormalizeVec3,
LengthVec3,
ConstVec3(Vec3),
}

impl From<&AnimationGraph> for AnimationGraphSerial {
Expand Down Expand Up @@ -219,6 +229,12 @@ impl From<&AnimationNodeType> for AnimationNodeTypeSerial {
interpolation_period: n.interpolation_period,
},
AnimationNodeType::SelectF32(_) => AnimationNodeTypeSerial::SelectF32,
AnimationNodeType::ConstEntityPath(n) => {
AnimationNodeTypeSerial::ConstEntityPath(n.path.clone())
}
AnimationNodeType::NormalizeVec3(_) => AnimationNodeTypeSerial::NormalizeVec3,
AnimationNodeType::LengthVec3(_) => AnimationNodeTypeSerial::LengthVec3,
AnimationNodeType::ConstVec3(n) => AnimationNodeTypeSerial::ConstVec3(n.constant),
}
}
}
33 changes: 27 additions & 6 deletions crates/bevy_animation_graph/src/core/animation_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use super::{
use crate::{
nodes::{
AbsF32, AddF32, BlendNode, BuildVec3Node, ChainNode, ClampF32, ClipNode, CompareF32,
ConstBool, ConstF32, DecomposeVec3Node, DivF32, DummyNode, FSMNode, FireEventNode,
FlipLRNode, FromEulerNode, GraphNode, IntoEulerNode, InvertQuatNode, LerpVec3Node,
LoopNode, MulF32, MulQuatNode, PaddingNode, RotationArcNode, RotationNode, SelectF32,
SlerpQuatNode, SpeedNode, SubF32, TwoBoneIKNode,
ConstBool, ConstEntityPath, ConstF32, ConstVec3Node, DecomposeVec3Node, DivF32, DummyNode,
FSMNode, FireEventNode, FlipLRNode, FromEulerNode, GraphNode, IntoEulerNode,
InvertQuatNode, LengthVec3Node, LerpVec3Node, LoopNode, MulF32, MulQuatNode,
NormalizeVec3Node, PaddingNode, RotationArcNode, RotationNode, SelectF32, SlerpQuatNode,
SpeedNode, SubF32, TwoBoneIKNode,
},
prelude::{PassContext, SpecContext},
};
Expand Down Expand Up @@ -182,9 +183,16 @@ pub enum AnimationNodeType {
TwoBoneIK(TwoBoneIKNode),
// ------------------------------------------------

// --- F32 arithmetic nodes
// --- Constant nodes
// ------------------------------------------------
ConstBool(ConstBool),
ConstF32(ConstF32),
ConstVec3(ConstVec3Node),
ConstEntityPath(ConstEntityPath),
// ------------------------------------------------

// --- F32 arithmetic nodes
// ------------------------------------------------
AddF32(AddF32),
MulF32(MulF32),
DivF32(DivF32),
Expand All @@ -197,7 +205,6 @@ pub enum AnimationNodeType {

// --- Bool nodes
// ------------------------------------------------
ConstBool(ConstBool),
// ------------------------------------------------

// --- EventQueue nodes
Expand All @@ -211,6 +218,8 @@ pub enum AnimationNodeType {
BuildVec3(BuildVec3Node),
DecomposeVec3(DecomposeVec3Node),
LerpVec3(LerpVec3Node),
NormalizeVec3(NormalizeVec3Node),
LengthVec3(LengthVec3Node),
// ------------------------------------------------

// --- Quat arithmetic nodes
Expand Down Expand Up @@ -278,6 +287,10 @@ impl AnimationNodeType {
AnimationNodeType::FireEvent(n) => f(n),
AnimationNodeType::Dummy(n) => f(n),
AnimationNodeType::Custom(n) => f(n.node.lock().unwrap().deref()),
AnimationNodeType::ConstEntityPath(n) => f(n),
AnimationNodeType::NormalizeVec3(n) => f(n),
AnimationNodeType::LengthVec3(n) => f(n),
AnimationNodeType::ConstVec3(n) => f(n),
}
}

Expand Down Expand Up @@ -326,6 +339,10 @@ impl AnimationNodeType {
let mut nod = n.node.lock().unwrap();
f(nod.deref_mut())
}
AnimationNodeType::ConstEntityPath(n) => f(n),
AnimationNodeType::NormalizeVec3(n) => f(n),
AnimationNodeType::LengthVec3(n) => f(n),
AnimationNodeType::ConstVec3(n) => f(n),
}
}

Expand Down Expand Up @@ -368,6 +385,10 @@ impl AnimationNodeType {
AnimationNodeType::Graph(n) => n,
AnimationNodeType::Dummy(n) => n,
AnimationNodeType::Custom(_) => todo!(),
AnimationNodeType::ConstEntityPath(n) => n,
AnimationNodeType::NormalizeVec3(n) => n,
AnimationNodeType::LengthVec3(n) => n,
AnimationNodeType::ConstVec3(n) => n,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::core::animation_graph::PinMap;
use crate::core::animation_node::{AnimationNode, AnimationNodeType, NodeLike};
use crate::core::errors::GraphError;
use crate::core::prelude::DataSpec;
use crate::prelude::{PassContext, SpecContext};
use bevy::prelude::*;

#[derive(Reflect, Clone, Debug, Default)]
#[reflect(Default)]
pub struct ConstVec3Node {
pub constant: Vec3,
}

impl ConstVec3Node {
pub const OUTPUT: &'static str = "out";

pub fn new(constant: Vec3) -> Self {
Self { constant }
}

pub fn wrapped(self, name: impl Into<String>) -> AnimationNode {
AnimationNode::new_from_nodetype(name.into(), AnimationNodeType::ConstVec3(self))
}
}

impl NodeLike for ConstVec3Node {
fn display_name(&self) -> String {
"Vec3".into()
}

fn update(&self, mut ctx: PassContext) -> Result<(), GraphError> {
ctx.set_data_fwd(Self::OUTPUT, self.constant);
Ok(())
}

fn data_output_spec(&self, _ctx: SpecContext) -> PinMap<DataSpec> {
[(Self::OUTPUT.into(), DataSpec::Vec3)].into()
}
}
47 changes: 47 additions & 0 deletions crates/bevy_animation_graph/src/nodes/arithmetic/vec3/length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::core::animation_graph::PinMap;
use crate::core::animation_node::{AnimationNode, AnimationNodeType, NodeLike};
use crate::core::errors::GraphError;
use crate::core::prelude::DataSpec;
use crate::prelude::{PassContext, SpecContext};
use crate::utils::unwrap::UnwrapVal;
use bevy::prelude::*;

#[derive(Reflect, Clone, Debug, Default)]
#[reflect(Default)]
pub struct LengthVec3Node {}

impl LengthVec3Node {
pub const INPUT: &'static str = "in";
pub const OUTPUT: &'static str = "out";

pub fn new() -> Self {
Self {}
}

pub fn wrapped(self, name: impl Into<String>) -> AnimationNode {
AnimationNode::new_from_nodetype(name.into(), AnimationNodeType::LengthVec3(self))
}
}

impl NodeLike for LengthVec3Node {
fn update(&self, mut ctx: PassContext) -> Result<(), GraphError> {
let input: Vec3 = ctx.data_back(Self::INPUT)?.val();
let output = input.length();

ctx.set_data_fwd(Self::OUTPUT, output);

Ok(())
}

fn data_input_spec(&self, _: SpecContext) -> PinMap<DataSpec> {
[(Self::INPUT.into(), DataSpec::Vec3)].into()
}

fn data_output_spec(&self, _: SpecContext) -> PinMap<DataSpec> {
[(Self::OUTPUT.into(), DataSpec::F32)].into()
}

fn display_name(&self) -> String {
"Length Vec3".into()
}
}
6 changes: 6 additions & 0 deletions crates/bevy_animation_graph/src/nodes/arithmetic/vec3/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
mod const_vec3;
mod from_f32;
mod into_f32;
mod length;
mod lerp;
mod normalize;
mod rotation_arc;

pub use const_vec3::*;
pub use from_f32::*;
pub use into_f32::*;
pub use length::*;
pub use lerp::*;
pub use normalize::*;
pub use rotation_arc::*;
47 changes: 47 additions & 0 deletions crates/bevy_animation_graph/src/nodes/arithmetic/vec3/normalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::core::animation_graph::PinMap;
use crate::core::animation_node::{AnimationNode, AnimationNodeType, NodeLike};
use crate::core::errors::GraphError;
use crate::core::prelude::DataSpec;
use crate::prelude::{PassContext, SpecContext};
use crate::utils::unwrap::UnwrapVal;
use bevy::prelude::*;

#[derive(Reflect, Clone, Debug, Default)]
#[reflect(Default)]
pub struct NormalizeVec3Node {}

impl NormalizeVec3Node {
pub const INPUT: &'static str = "in";
pub const OUTPUT: &'static str = "out";

pub fn new() -> Self {
Self {}
}

pub fn wrapped(self, name: impl Into<String>) -> AnimationNode {
AnimationNode::new_from_nodetype(name.into(), AnimationNodeType::NormalizeVec3(self))
}
}

impl NodeLike for NormalizeVec3Node {
fn update(&self, mut ctx: PassContext) -> Result<(), GraphError> {
let input: Vec3 = ctx.data_back(Self::INPUT)?.val();
let output = input.normalize_or_zero();

ctx.set_data_fwd(Self::OUTPUT, output);

Ok(())
}

fn data_input_spec(&self, _: SpecContext) -> PinMap<DataSpec> {
[(Self::INPUT.into(), DataSpec::Vec3)].into()
}

fn data_output_spec(&self, _: SpecContext) -> PinMap<DataSpec> {
[(Self::OUTPUT.into(), DataSpec::Vec3)].into()
}

fn display_name(&self) -> String {
"Normalize Vec3".into()
}
}
40 changes: 40 additions & 0 deletions crates/bevy_animation_graph/src/nodes/const_entity_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::core::animation_clip::EntityPath;
use crate::core::animation_graph::PinMap;
use crate::core::animation_node::{AnimationNode, AnimationNodeType, NodeLike};
use crate::core::errors::GraphError;
use crate::core::prelude::DataSpec;
use crate::prelude::{DataValue, PassContext, SpecContext};
use bevy::prelude::*;

#[derive(Reflect, Clone, Debug, Default)]
#[reflect(Default)]
pub struct ConstEntityPath {
pub path: EntityPath,
}

impl ConstEntityPath {
pub const OUTPUT: &'static str = "out";

pub fn new(path: EntityPath) -> Self {
Self { path }
}

pub fn wrapped(self, name: impl Into<String>) -> AnimationNode {
AnimationNode::new_from_nodetype(name.into(), AnimationNodeType::ConstEntityPath(self))
}
}

impl NodeLike for ConstEntityPath {
fn display_name(&self) -> String {
"Entity Path".into()
}

fn update(&self, mut ctx: PassContext) -> Result<(), GraphError> {
ctx.set_data_fwd(Self::OUTPUT, DataValue::EntityPath(self.path.clone()));
Ok(())
}

fn data_output_spec(&self, _ctx: SpecContext) -> PinMap<DataSpec> {
[(Self::OUTPUT.into(), DataSpec::EntityPath)].into()
}
}
2 changes: 2 additions & 0 deletions crates/bevy_animation_graph/src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod arithmetic;
pub mod blend_node;
pub mod chain_node;
pub mod clip_node;
pub mod const_entity_path;
pub mod dummy_node;
pub mod flip_lr_node;
pub mod graph_node;
Expand All @@ -23,6 +24,7 @@ pub use graph_node::*;
pub use loop_node::*;
pub use rotation_node::*;
// pub use space_conversion::*;
pub use const_entity_path::*;
pub use fsm_node::*;
pub use padding::*;
pub use speed_node::*;
Expand Down

0 comments on commit 13485e9

Please sign in to comment.