Skip to content

Commit a89b536

Browse files
committed
implement add_directry to animation tree node add2 and add3
1 parent a6bb4f7 commit a89b536

6 files changed

+72
-14
lines changed

doc/classes/AnimationNodeAdd2.xml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<member name="sync" type="bool" setter="set_use_sync" getter="is_using_sync" default="false">
1616
If [code]true[/code], sets the [code]optimization[/code] to [code]false[/code] when calling [method AnimationNode.blend_input], forcing the blended animations to update every frame.
1717
</member>
18+
<member name="add_directly" type="bool" setter="set_add_directly" getter="get_add_directly" default="false">
19+
If [code]false[/code], adds the blended transformation after the subtraction of the difference. If [code]true[/code], adds the second transformation to first it directly.
20+
</member>
1821
</members>
1922
<constants>
2023
</constants>

doc/classes/AnimationNodeAdd3.xml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<member name="sync" type="bool" setter="set_use_sync" getter="is_using_sync" default="false">
2020
If [code]true[/code], sets the [code]optimization[/code] to [code]false[/code] when calling [method AnimationNode.blend_input], forcing the blended animations to update every frame.
2121
</member>
22+
<member name="add_directly" type="bool" setter="set_add_directly" getter="get_add_directly" default="false">
23+
If [code]false[/code], adds the blended transformation after the subtraction of the difference. If [code]true[/code], adds the first or third transformation to second it directly.
24+
</member>
2225
</members>
2326
<constants>
2427
</constants>

scene/animation/animation_blend_tree.cpp

+34-3
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,22 @@ bool AnimationNodeAdd2::is_using_sync() const {
385385
return sync;
386386
}
387387

388+
void AnimationNodeAdd2::set_add_directly(bool p_add_directly) {
389+
add_directly = p_add_directly;
390+
}
391+
392+
bool AnimationNodeAdd2::get_add_directly() const {
393+
return add_directly;
394+
}
395+
388396
bool AnimationNodeAdd2::has_filter() const {
389397
return true;
390398
}
391399

392400
float AnimationNodeAdd2::process(float p_time, bool p_seek) {
393401
float amount = get_parameter(add_amount);
394402
float rem0 = blend_input(0, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
395-
blend_input(1, p_time, p_seek, amount, FILTER_PASS, !sync);
403+
blend_input(1, p_time, p_seek, amount, FILTER_PASS, !sync, add_directly);
396404

397405
return rem0;
398406
}
@@ -401,14 +409,19 @@ void AnimationNodeAdd2::_bind_methods() {
401409
ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeAdd2::set_use_sync);
402410
ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeAdd2::is_using_sync);
403411

412+
ClassDB::bind_method(D_METHOD("set_add_directly", "enable"), &AnimationNodeAdd2::set_add_directly);
413+
ClassDB::bind_method(D_METHOD("get_add_directly"), &AnimationNodeAdd2::get_add_directly);
414+
404415
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
416+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "add_directly"), "set_add_directly", "get_add_directly");
405417
}
406418

407419
AnimationNodeAdd2::AnimationNodeAdd2() {
408420
add_amount = "add_amount";
409421
add_input("in");
410422
add_input("add");
411423
sync = false;
424+
add_directly = false;
412425
}
413426

414427
////////////////////////////////////////////////
@@ -433,15 +446,28 @@ bool AnimationNodeAdd3::is_using_sync() const {
433446
return sync;
434447
}
435448

449+
void AnimationNodeAdd3::set_add_directly(bool p_add_directly) {
450+
add_directly = p_add_directly;
451+
}
452+
453+
bool AnimationNodeAdd3::get_add_directly() const {
454+
return add_directly;
455+
}
456+
436457
bool AnimationNodeAdd3::has_filter() const {
437458
return true;
438459
}
439460

440461
float AnimationNodeAdd3::process(float p_time, bool p_seek) {
441462
float amount = get_parameter(add_amount);
442-
blend_input(0, p_time, p_seek, MAX(0, -amount), FILTER_PASS, !sync);
443463
float rem0 = blend_input(1, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
444-
blend_input(2, p_time, p_seek, MAX(0, amount), FILTER_PASS, !sync);
464+
if (amount < 0) {
465+
blend_input(0, p_time, p_seek, -amount, FILTER_PASS, !sync, add_directly);
466+
blend_input(2, p_time, p_seek, 0, FILTER_PASS, !sync);
467+
} else {
468+
blend_input(2, p_time, p_seek, amount, FILTER_PASS, !sync, add_directly);
469+
blend_input(0, p_time, p_seek, 0, FILTER_PASS, !sync);
470+
}
445471

446472
return rem0;
447473
}
@@ -450,7 +476,11 @@ void AnimationNodeAdd3::_bind_methods() {
450476
ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeAdd3::set_use_sync);
451477
ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeAdd3::is_using_sync);
452478

479+
ClassDB::bind_method(D_METHOD("set_add_directly", "enable"), &AnimationNodeAdd3::set_add_directly);
480+
ClassDB::bind_method(D_METHOD("get_add_directly"), &AnimationNodeAdd3::get_add_directly);
481+
453482
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
483+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "add_directly"), "set_add_directly", "get_add_directly");
454484
}
455485

456486
AnimationNodeAdd3::AnimationNodeAdd3() {
@@ -459,6 +489,7 @@ AnimationNodeAdd3::AnimationNodeAdd3() {
459489
add_input("in");
460490
add_input("+add");
461491
sync = false;
492+
add_directly = false;
462493
}
463494

464495
/////////////////////////////////////////////

scene/animation/animation_blend_tree.h

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class AnimationNodeAdd2 : public AnimationNode {
134134

135135
StringName add_amount;
136136
bool sync;
137+
bool add_directly;
137138

138139
protected:
139140
static void _bind_methods();
@@ -147,6 +148,9 @@ class AnimationNodeAdd2 : public AnimationNode {
147148
void set_use_sync(bool p_sync);
148149
bool is_using_sync() const;
149150

151+
void set_add_directly(bool p_add_directly);
152+
bool get_add_directly() const;
153+
150154
virtual bool has_filter() const override;
151155
virtual float process(float p_time, bool p_seek) override;
152156

@@ -158,6 +162,7 @@ class AnimationNodeAdd3 : public AnimationNode {
158162

159163
StringName add_amount;
160164
bool sync;
165+
bool add_directly;
161166

162167
protected:
163168
static void _bind_methods();
@@ -171,6 +176,9 @@ class AnimationNodeAdd3 : public AnimationNode {
171176
void set_use_sync(bool p_sync);
172177
bool is_using_sync() const;
173178

179+
void set_add_directly(bool p_add_directly);
180+
bool get_add_directly() const;
181+
174182
virtual bool has_filter() const override;
175183
virtual float process(float p_time, bool p_seek) override;
176184

scene/animation/animation_tree.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ void AnimationNode::blend_animation(const StringName &p_animation, float p_time,
112112
anim_state.time = p_time;
113113
anim_state.animation = animation;
114114
anim_state.seeked = p_seeked;
115+
anim_state.add_directly = add_directly;
115116

116117
state->animation_states.push_back(anim_state);
117118
}
@@ -141,7 +142,7 @@ void AnimationNode::make_invalid(const String &p_reason) {
141142
state->invalid_reasons += "- " + p_reason;
142143
}
143144

144-
float AnimationNode::blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize) {
145+
float AnimationNode::blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize, bool p_add_directly) {
145146
ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
146147
ERR_FAIL_COND_V(!state, 0);
147148

@@ -154,6 +155,9 @@ float AnimationNode::blend_input(int p_input, float p_time, bool p_seek, float p
154155
String name = blend_tree->get_node_name(Ref<AnimationNode>(this));
155156
make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), name));
156157
return 0;
158+
} else {
159+
Ref<AnimationNode> node = blend_tree->get_node(node_name);
160+
node->add_directly = p_add_directly;
157161
}
158162

159163
Ref<AnimationNode> node = blend_tree->get_node(node_name);
@@ -411,7 +415,7 @@ void AnimationNode::_bind_methods() {
411415

412416
ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "blend"), &AnimationNode::blend_animation);
413417
ClassDB::bind_method(D_METHOD("blend_node", "name", "node", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_node, DEFVAL(FILTER_IGNORE), DEFVAL(true));
414-
ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true));
418+
ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "blend", "filter", "optimize", "add_directly"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true), DEFVAL(false));
415419

416420
ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
417421
ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);
@@ -922,16 +926,23 @@ void AnimationTree::_process_graph(float p_delta) {
922926
continue;
923927
}
924928

925-
t->loc = t->loc.lerp(loc, blend);
926-
if (t->rot_blend_accum == 0) {
927-
t->rot = rot;
928-
t->rot_blend_accum = blend;
929+
if (!as.add_directly) {
930+
t->loc = t->loc.lerp(loc, blend);
931+
if (t->rot_blend_accum == 0) {
932+
t->rot = rot;
933+
t->rot_blend_accum = blend;
934+
} else {
935+
float rot_total = t->rot_blend_accum + blend;
936+
t->rot = rot.slerp(t->rot, t->rot_blend_accum / rot_total).normalized();
937+
t->rot_blend_accum = rot_total;
938+
}
939+
t->scale = t->scale.lerp(scale, blend);
929940
} else {
930-
float rot_total = t->rot_blend_accum + blend;
931-
t->rot = rot.slerp(t->rot, t->rot_blend_accum / rot_total).normalized();
932-
t->rot_blend_accum = rot_total;
941+
t->loc += loc * blend;
942+
t->scale = t->scale.lerp(scale, blend);
943+
Quat q = Quat().slerp(rot.normalized(), blend).normalized();
944+
t->rot = (t->rot * q).normalized();
933945
}
934-
t->scale = t->scale.lerp(scale, blend);
935946
}
936947

937948
} break;

scene/animation/animation_tree.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class AnimationNode : public Resource {
6868
const Vector<float> *track_blends;
6969
float blend;
7070
bool seeked;
71+
bool add_directly;
7172
};
7273

7374
struct State {
@@ -94,6 +95,7 @@ class AnimationNode : public Resource {
9495

9596
HashMap<NodePath, bool> filter;
9697
bool filter_enabled;
98+
bool add_directly;
9799

98100
Array _get_filters() const;
99101
void _set_filters(const Array &p_filters);
@@ -103,7 +105,7 @@ class AnimationNode : public Resource {
103105
protected:
104106
void blend_animation(const StringName &p_animation, float p_time, float p_delta, bool p_seeked, float p_blend);
105107
float blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true);
106-
float blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true);
108+
float blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true, bool p_add_directly = false);
107109
void make_invalid(const String &p_reason);
108110

109111
static void _bind_methods();

0 commit comments

Comments
 (0)