From 13485e95c99f47b12e736983c369cc2d67c460ef Mon Sep 17 00:00:00 2001 From: aecsocket <43144841+aecsocket@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:55:40 +0100 Subject: [PATCH] More node types (#67) Adds: - `ConstEntityPath` - `NormalizeVec3` - `LengthVec3` - `ConstVec3` --- .../src/core/animation_graph/loader.rs | 21 +++++++-- .../src/core/animation_graph/serial.rs | 20 +++++++- .../src/core/animation_node.rs | 33 ++++++++++--- .../src/nodes/arithmetic/vec3/const_vec3.rs | 39 +++++++++++++++ .../src/nodes/arithmetic/vec3/length.rs | 47 +++++++++++++++++++ .../src/nodes/arithmetic/vec3/mod.rs | 6 +++ .../src/nodes/arithmetic/vec3/normalize.rs | 47 +++++++++++++++++++ .../src/nodes/const_entity_path.rs | 40 ++++++++++++++++ crates/bevy_animation_graph/src/nodes/mod.rs | 2 + 9 files changed, 243 insertions(+), 12 deletions(-) create mode 100644 crates/bevy_animation_graph/src/nodes/arithmetic/vec3/const_vec3.rs create mode 100644 crates/bevy_animation_graph/src/nodes/arithmetic/vec3/length.rs create mode 100644 crates/bevy_animation_graph/src/nodes/arithmetic/vec3/normalize.rs create mode 100644 crates/bevy_animation_graph/src/nodes/const_entity_path.rs diff --git a/crates/bevy_animation_graph/src/core/animation_graph/loader.rs b/crates/bevy_animation_graph/src/core/animation_graph/loader.rs index 0835d81..7a490bf 100644 --- a/crates/bevy_animation_graph/src/core/animation_graph/loader.rs +++ b/crates/bevy_animation_graph/src/core/animation_graph/loader.rs @@ -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, }; @@ -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); } diff --git a/crates/bevy_animation_graph/src/core/animation_graph/serial.rs b/crates/bevy_animation_graph/src/core/animation_graph/serial.rs index 5a8cd44..4272d66 100644 --- a/crates/bevy_animation_graph/src/core/animation_graph/serial.rs +++ b/crates/bevy_animation_graph/src/core/animation_graph/serial.rs @@ -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, // /// Inverted, indexed by output node name. @@ -112,6 +118,10 @@ pub enum AnimationNodeTypeSerial { Dummy, Fsm(String), Graph(String), + ConstEntityPath(EntityPath), + NormalizeVec3, + LengthVec3, + ConstVec3(Vec3), } impl From<&AnimationGraph> for AnimationGraphSerial { @@ -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), } } } diff --git a/crates/bevy_animation_graph/src/core/animation_node.rs b/crates/bevy_animation_graph/src/core/animation_node.rs index dd717de..36cb8e9 100644 --- a/crates/bevy_animation_graph/src/core/animation_node.rs +++ b/crates/bevy_animation_graph/src/core/animation_node.rs @@ -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}, }; @@ -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), @@ -197,7 +205,6 @@ pub enum AnimationNodeType { // --- Bool nodes // ------------------------------------------------ - ConstBool(ConstBool), // ------------------------------------------------ // --- EventQueue nodes @@ -211,6 +218,8 @@ pub enum AnimationNodeType { BuildVec3(BuildVec3Node), DecomposeVec3(DecomposeVec3Node), LerpVec3(LerpVec3Node), + NormalizeVec3(NormalizeVec3Node), + LengthVec3(LengthVec3Node), // ------------------------------------------------ // --- Quat arithmetic nodes @@ -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), } } @@ -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), } } @@ -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, } } } diff --git a/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/const_vec3.rs b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/const_vec3.rs new file mode 100644 index 0000000..bf34be3 --- /dev/null +++ b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/const_vec3.rs @@ -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) -> 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 { + [(Self::OUTPUT.into(), DataSpec::Vec3)].into() + } +} diff --git a/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/length.rs b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/length.rs new file mode 100644 index 0000000..133d45f --- /dev/null +++ b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/length.rs @@ -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) -> 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 { + [(Self::INPUT.into(), DataSpec::Vec3)].into() + } + + fn data_output_spec(&self, _: SpecContext) -> PinMap { + [(Self::OUTPUT.into(), DataSpec::F32)].into() + } + + fn display_name(&self) -> String { + "Length Vec3".into() + } +} diff --git a/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/mod.rs b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/mod.rs index fdebaa8..96de942 100644 --- a/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/mod.rs +++ b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/mod.rs @@ -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::*; diff --git a/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/normalize.rs b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/normalize.rs new file mode 100644 index 0000000..be5a0a0 --- /dev/null +++ b/crates/bevy_animation_graph/src/nodes/arithmetic/vec3/normalize.rs @@ -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) -> 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 { + [(Self::INPUT.into(), DataSpec::Vec3)].into() + } + + fn data_output_spec(&self, _: SpecContext) -> PinMap { + [(Self::OUTPUT.into(), DataSpec::Vec3)].into() + } + + fn display_name(&self) -> String { + "Normalize Vec3".into() + } +} diff --git a/crates/bevy_animation_graph/src/nodes/const_entity_path.rs b/crates/bevy_animation_graph/src/nodes/const_entity_path.rs new file mode 100644 index 0000000..e6d54c0 --- /dev/null +++ b/crates/bevy_animation_graph/src/nodes/const_entity_path.rs @@ -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) -> 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 { + [(Self::OUTPUT.into(), DataSpec::EntityPath)].into() + } +} diff --git a/crates/bevy_animation_graph/src/nodes/mod.rs b/crates/bevy_animation_graph/src/nodes/mod.rs index 33d8e0e..9bf4ad0 100644 --- a/crates/bevy_animation_graph/src/nodes/mod.rs +++ b/crates/bevy_animation_graph/src/nodes/mod.rs @@ -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; @@ -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::*;