Skip to content

Commit

Permalink
Add a new blend sync mode option, fix speed node (#65)
Browse files Browse the repository at this point in the history
* Added a `blend_sync_mode` setting to the Blend node, allow users to
select whether the second input should be synced to the same timestamp
as the first, or no syncing should be done.
* The speed node should not be applying a speed factor for absolute
timestamps
  • Loading branch information
mbrea-c authored Sep 22, 2024
1 parent 3227ca6 commit ec0bb92
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ impl AssetLoader for AnimationGraphLoader {
*override_interpolation,
)
.wrapped(&serial_node.name),
AnimationNodeTypeSerial::Blend { mode } => {
BlendNode::new(*mode).wrapped(&serial_node.name)
AnimationNodeTypeSerial::Blend { mode, sync_mode } => {
BlendNode::new(*mode, *sync_mode).wrapped(&serial_node.name)
}
AnimationNodeTypeSerial::Chain {
interpolation_period,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{pin, AnimationGraph, Extra};
use crate::{
core::{animation_clip::Interpolation, edge_data::AnimationEvent},
flipping::config::FlipConfig,
nodes::{BlendMode, ChainDecay, CompareOp, RotationMode, RotationSpace},
nodes::{BlendMode, BlendSyncMode, ChainDecay, CompareOp, RotationMode, RotationSpace},
prelude::{AnimationNode, AnimationNodeType, DataSpec, DataValue},
utils::ordered_map::OrderedMap,
};
Expand Down Expand Up @@ -62,6 +62,8 @@ pub enum AnimationNodeTypeSerial {
Blend {
#[serde(default)]
mode: BlendMode,
#[serde(default)]
sync_mode: BlendSyncMode,
},
FlipLR {
#[serde(default)]
Expand Down Expand Up @@ -162,7 +164,10 @@ impl From<&AnimationNodeType> for AnimationNodeTypeSerial {
AnimationNodeType::Chain(n) => AnimationNodeTypeSerial::Chain {
interpolation_period: n.interpolation_period,
},
AnimationNodeType::Blend(n) => AnimationNodeTypeSerial::Blend { mode: n.mode },
AnimationNodeType::Blend(n) => AnimationNodeTypeSerial::Blend {
mode: n.mode,
sync_mode: n.sync_mode,
},
AnimationNodeType::FlipLR(n) => AnimationNodeTypeSerial::FlipLR {
config: n.config.clone(),
},
Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_animation_graph/src/core/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use super::{
systems::{animation_player, animation_player_deferred_gizmos},
};
use crate::nodes::{
AbsF32, AddF32, BlendNode, ChainNode, ClampF32, ClipNode, CompareF32, DivF32, DummyNode,
FireEventNode, FlipLRNode, GraphNode, LoopNode, MulF32, PaddingNode, RotationArcNode,
RotationNode, SpeedNode, SubF32, TwoBoneIKNode,
AbsF32, AddF32, BlendMode, BlendNode, BlendSyncMode, ChainNode, ClampF32, ClipNode, CompareF32,
DivF32, DummyNode, FireEventNode, FlipLRNode, GraphNode, LoopNode, MulF32, PaddingNode,
RotationArcNode, RotationNode, SpeedNode, SubF32, TwoBoneIKNode,
};
use crate::prelude::{
config::{FlipConfig, FlipNameMapper, PatternMapper, PatternMapperSerial},
Expand Down Expand Up @@ -87,6 +87,8 @@ impl AnimationGraphPlugin {
.register_type::<FlipNameMapper>()
.register_type::<PatternMapper>()
.register_type::<PatternMapperSerial>()
.register_type::<BlendMode>()
.register_type::<BlendSyncMode>()
.register_type::<()>()
.register_type_data::<(), ReflectDefault>()
// --- Node registrations
Expand Down
30 changes: 27 additions & 3 deletions crates/bevy_animation_graph/src/nodes/blend_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ pub enum BlendMode {
Difference,
}

#[derive(Reflect, Clone, Copy, Debug, Default, Serialize, Deserialize)]
#[reflect(Default)]
pub enum BlendSyncMode {
/// Sets the absolute timestamp of input 2 equal to the timestamp from input 1
#[default]
Absolute,
/// Propagates the same time update that was received, does not try to sync the inputs.
NoSync,
}

#[derive(Reflect, Clone, Debug, Default)]
#[reflect(Default)]
pub struct BlendNode {
pub mode: BlendMode,
pub sync_mode: BlendSyncMode,
}

impl BlendNode {
Expand All @@ -31,8 +42,8 @@ impl BlendNode {
pub const IN_TIME_B: &'static str = "time B";
pub const OUT_POSE: &'static str = "pose";

pub fn new(mode: BlendMode) -> Self {
Self { mode }
pub fn new(mode: BlendMode, sync_mode: BlendSyncMode) -> Self {
Self { mode, sync_mode }
}

pub fn wrapped(self, name: impl Into<String>) -> AnimationNode {
Expand All @@ -58,9 +69,22 @@ impl NodeLike for BlendNode {

fn update(&self, mut ctx: PassContext) -> Result<(), GraphError> {
let input = ctx.time_update_fwd()?;

ctx.set_time_update_back(Self::IN_TIME_A, input);
let in_frame_1: Pose = ctx.data_back(Self::IN_POSE_A)?.val();
ctx.set_time_update_back(Self::IN_TIME_B, TimeUpdate::Absolute(in_frame_1.timestamp));

match self.sync_mode {
BlendSyncMode::Absolute => {
ctx.set_time_update_back(
Self::IN_TIME_B,
TimeUpdate::Absolute(in_frame_1.timestamp),
);
}
BlendSyncMode::NoSync => {
ctx.set_time_update_back(Self::IN_TIME_B, input);
}
};

let in_frame_2: Pose = ctx.data_back(Self::IN_POSE_B)?.val();

let out = match self.mode {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_animation_graph/src/nodes/speed_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ impl NodeLike for SpeedNode {
let input = ctx.time_update_fwd()?;
let fw_upd = match input {
TimeUpdate::Delta(dt) => TimeUpdate::Delta(dt * speed),
TimeUpdate::Absolute(t) => TimeUpdate::Absolute(t * speed),
TimeUpdate::Absolute(t) => TimeUpdate::Absolute(t),
};

ctx.set_time_update_back(Self::IN_TIME, fw_upd);
let mut in_pose: Pose = ctx.data_back(Self::IN_POSE)?.val();

Expand Down

0 comments on commit ec0bb92

Please sign in to comment.