-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynced_animation_node.cpp
More file actions
395 lines (321 loc) · 12.5 KB
/
synced_animation_node.cpp
File metadata and controls
395 lines (321 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// Created by martin on 03.12.25.
//
#include "synced_animation_node.h"
void SyncedAnimationNode::_bind_methods() {
ADD_SIGNAL(MethodInfo("tree_changed"));
ADD_SIGNAL(MethodInfo("animation_node_renamed", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name")));
ADD_SIGNAL(MethodInfo("animation_node_removed", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "name")));
}
void SyncedAnimationNode::get_parameter_list(List<PropertyInfo> *r_list) const {
}
Variant SyncedAnimationNode::get_parameter_default_value(const StringName &p_parameter) const {
return Variant();
}
bool SyncedAnimationNode::is_parameter_read_only(const StringName &p_parameter) const {
return false;
}
void SyncedAnimationNode::set_parameter(const StringName &p_name, const Variant &p_value) {
}
Variant SyncedAnimationNode::get_parameter(const StringName &p_name) const {
return Variant();
}
void SyncedAnimationNode::_tree_changed() {
emit_signal(SNAME("tree_changed"));
}
void SyncedAnimationNode::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
emit_signal(SNAME("animation_node_renamed"), p_oid, p_old_name, p_new_name);
}
void SyncedAnimationNode::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
emit_signal(SNAME("animation_node_removed"), p_oid, p_node);
}
void SyncedBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
for (const Ref<SyncedAnimationNode> &node : tree_graph.nodes) {
String prop_name = node->name;
if (prop_name != "Output") {
p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
}
p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
}
p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
}
bool SyncedBlendTree::_get(const StringName &p_name, Variant &r_value) const {
String prop_name = p_name;
if (prop_name.begins_with("nodes/")) {
String node_name = prop_name.get_slicec('/', 1);
String what = prop_name.get_slicec('/', 2);
int node_index = find_node_index_by_name(node_name);
if (what == "node") {
if (node_index != -1) {
r_value = tree_graph.nodes[node_index];
return true;
}
}
if (what == "position") {
if (node_index != -1) {
r_value = tree_graph.nodes[node_index]->position;
return true;
}
}
} else if (prop_name == "node_connections") {
Array conns;
conns.resize(tree_graph.connections.size() * 3);
int idx = 0;
for (const BlendTreeConnection &connection : tree_graph.connections) {
conns[idx * 3 + 0] = connection.target_node->name;
conns[idx * 3 + 1] = connection.target_node->get_input_index(connection.target_port_name);
conns[idx * 3 + 2] = connection.source_node->name;
idx++;
}
r_value = conns;
return true;
}
return false;
}
bool SyncedBlendTree::_set(const StringName &p_name, const Variant &p_value) {
String prop_name = p_name;
if (prop_name.begins_with("nodes/")) {
String node_name = prop_name.get_slicec('/', 1);
String what = prop_name.get_slicec('/', 2);
if (what == "node") {
Ref<SyncedAnimationNode> anode = p_value;
if (anode.is_valid()) {
anode->name = node_name;
add_node(anode);
}
return true;
}
if (what == "position") {
int node_index = find_node_index_by_name(node_name);
if (node_index > -1) {
tree_graph.nodes[node_index]->position = p_value;
}
return true;
}
} else if (prop_name == "node_connections") {
Array conns = p_value;
ERR_FAIL_COND_V(conns.size() % 3 != 0, false);
for (int i = 0; i < conns.size(); i += 3) {
int target_node_index = find_node_index_by_name(conns[i]);
int target_node_port_index = conns[i + 1];
int source_node_index = find_node_index_by_name(conns[i + 2]);
Ref<SyncedAnimationNode> target_node = tree_graph.nodes[target_node_index];
Vector<StringName> target_input_names;
target_node->get_input_names(target_input_names);
add_connection(tree_graph.nodes[source_node_index], target_node, target_input_names[target_node_port_index]);
}
return true;
}
return false;
}
void AnimationData::sample_from_animation(const Ref<Animation> &animation, const Skeleton3D *skeleton_3d, double p_time) {
GodotProfileZone("AnimationData::sample_from_animation");
const LocalVector<Animation::Track *> tracks = animation->get_tracks();
Animation::Track *const *tracks_ptr = tracks.ptr();
int count = tracks.size();
for (int i = 0; i < count; i++) {
const Animation::Track *animation_track = tracks_ptr[i];
if (!animation_track->enabled) {
continue;
}
Animation::TrackType ttype = animation_track->type;
switch (ttype) {
case Animation::TYPE_POSITION_3D:
case Animation::TYPE_ROTATION_3D: {
TransformTrackValue *transform_track_value = get_value<TransformTrackValue>(animation_track->thash);
if (transform_track_value->bone_idx != -1) {
switch (ttype) {
case Animation::TYPE_POSITION_3D: {
animation->try_position_track_interpolate(i, p_time, &transform_track_value->loc);
transform_track_value->loc_used = true;
break;
}
case Animation::TYPE_ROTATION_3D: {
animation->try_rotation_track_interpolate(i, p_time, &transform_track_value->rot);
transform_track_value->rot_used = true;
break;
}
default: {
assert(false && !"Not yet implemented");
break;
}
}
} else {
// TODO
assert(false && !"Not yet implemented");
}
break;
}
default: {
// TODO
assert(false && !"Not yet implemented");
break;
}
}
}
}
void AnimationData::allocate_track_value(const Animation::Track *animation_track, const Skeleton3D *skeleton_3d) {
switch (animation_track->type) {
case Animation::TrackType::TYPE_ROTATION_3D:
case Animation::TrackType::TYPE_POSITION_3D: {
size_t value_offset = 0;
AnimationData::TransformTrackValue *transform_track_value = nullptr;
if (value_buffer_offset.has(animation_track->thash)) {
value_offset = value_buffer_offset[animation_track->thash];
transform_track_value = reinterpret_cast<AnimationData::TransformTrackValue *>(&buffer[value_offset]);
} else {
value_offset = buffer.size();
value_buffer_offset.insert(animation_track->thash, buffer.size());
buffer.resize(buffer.size() + sizeof(AnimationData::TransformTrackValue));
transform_track_value = new (reinterpret_cast<AnimationData::TransformTrackValue *>(&buffer[value_offset])) AnimationData::TransformTrackValue();
}
assert(transform_track_value != nullptr);
if (animation_track->path.get_subname_count() == 1) {
transform_track_value->bone_idx = skeleton_3d->find_bone(animation_track->path.get_subname(0));
}
if (animation_track->type == Animation::TrackType::TYPE_POSITION_3D) {
transform_track_value->loc_used = true;
} else if (animation_track->type == Animation::TrackType::TYPE_ROTATION_3D) {
transform_track_value->rot_used = true;
}
break;
}
default:
break;
}
}
void AnimationData::allocate_track_values(const Ref<Animation> &animation, const Skeleton3D *skeleton_3d) {
GodotProfileZone("AnimationData::allocate_track_values");
const LocalVector<Animation::Track *> tracks = animation->get_tracks();
Animation::Track *const *tracks_ptr = tracks.ptr();
int count = tracks.size();
for (int i = 0; i < count; i++) {
const Animation::Track *animation_track = tracks_ptr[i];
if (!animation_track->enabled) {
continue;
}
allocate_track_value(animation_track, skeleton_3d);
}
}
void AnimationDataAllocator::register_track_values(const Ref<Animation> &animation, const Skeleton3D *skeleton_3d) {
default_data.allocate_track_values(animation, skeleton_3d);
}
bool AnimationSamplerNode::initialize(GraphEvaluationContext &context) {
SyncedAnimationNode::initialize(context);
animation = context.animation_player->get_animation(animation_name);
if (!animation.is_valid()) {
print_error(vformat("Cannot initialize node %s: animation '%s' not found in animation player.", name, animation_name));
return false;
}
context.animation_data_allocator.register_track_values(animation, context.skeleton_3d);
node_time_info.loop_mode = animation->get_loop_mode();
// Initialize Sync Track from marker
LocalVector<float> sync_markers;
int marker_index = 0;
StringName marker_name = itos(marker_index);
while (animation->has_marker(marker_name)) {
sync_markers.push_back(animation->get_marker_time(marker_name));
marker_index++;
marker_name = itos(marker_index);
}
if (sync_markers.size() > 0) {
node_time_info.sync_track = SyncTrack::create_from_markers(animation->get_length(), sync_markers);
} else {
node_time_info.sync_track = SyncTrack::create_from_markers(animation->get_length(), { 0 });
}
return true;
}
void AnimationSamplerNode::update_time(double p_time) {
SyncedAnimationNode::update_time(p_time);
if (node_time_info.is_synced) {
// Any potential looping has already been performed in the sync-controlling node.
return;
}
if (node_time_info.loop_mode != Animation::LOOP_NONE) {
if (node_time_info.loop_mode == Animation::LOOP_LINEAR) {
if (!Math::is_zero_approx(animation->get_length())) {
node_time_info.position = Math::fposmod(node_time_info.position, static_cast<double>(animation->get_length()));
}
} else {
assert(false && !"Ping-pong looping not yet supported");
}
}
}
void AnimationSamplerNode::evaluate(GraphEvaluationContext &context, const LocalVector<AnimationData *> &inputs, AnimationData &output) {
GodotProfileZone("AnimationSamplerNode::evaluate");
assert(inputs.size() == 0);
if (node_time_info.is_synced) {
node_time_info.position = node_time_info.sync_track.calc_ratio_from_sync_time(node_time_info.sync_position) * animation->get_length();
}
output.sample_from_animation(animation, context.skeleton_3d, node_time_info.position);
}
void AnimationSamplerNode::set_animation(const StringName &p_name) {
animation_name = p_name;
}
StringName AnimationSamplerNode::get_animation() const {
return animation_name;
}
void AnimationSamplerNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationSamplerNode::set_animation);
ClassDB::bind_method(D_METHOD("get_animation"), &AnimationSamplerNode::get_animation);
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
}
void AnimationBlend2Node::evaluate(GraphEvaluationContext &context, const LocalVector<AnimationData *> &inputs, AnimationData &output) {
GodotProfileZone("AnimationBlend2Node::evaluate");
output = std::move(*inputs[0]);
output.blend(*inputs[1], blend_weight);
}
void AnimationBlend2Node::set_use_sync(bool p_sync) {
sync = p_sync;
}
bool AnimationBlend2Node::is_using_sync() const {
return sync;
}
void AnimationBlend2Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationBlend2Node::set_use_sync);
ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationBlend2Node::is_using_sync);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
}
void AnimationBlend2Node::get_parameter_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::FLOAT, blend_weight_pname, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
}
void AnimationBlend2Node::set_parameter(const StringName &p_name, const Variant &p_value) {
_set(p_name, p_value);
}
Variant AnimationBlend2Node::get_parameter(const StringName &p_name) const {
Variant result;
_get(p_name, result);
return result;
}
Variant AnimationBlend2Node::get_parameter_default_value(const StringName &p_parameter) const {
if (p_parameter == blend_weight_pname) {
return blend_weight;
}
return Variant();
}
void AnimationBlend2Node::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::FLOAT, blend_weight_pname, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
p_list->push_back(PropertyInfo(Variant::BOOL, sync_pname));
}
bool AnimationBlend2Node::_get(const StringName &p_name, Variant &r_value) const {
if (p_name == blend_weight_pname) {
r_value = blend_weight;
return true;
}
if (p_name == sync_pname) {
r_value = sync;
return true;
}
return false;
}
bool AnimationBlend2Node::_set(const StringName &p_name, const Variant &p_value) {
if (p_name == blend_weight_pname) {
blend_weight = p_value;
return true;
}
if (p_name == sync_pname) {
sync = p_value;
return true;
}
return false;
}