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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
#include "animation_blend_space.h"
void AnimationNodeBlendSpace::add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index) {
ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
ERR_FAIL_COND(p_node.is_null());
ERR_FAIL_COND(p_node->get_parent().is_valid());
ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
if (p_at_index == -1 || p_at_index == blend_points_used) {
p_at_index = blend_points_used;
} else {
for (int i = blend_points_used; i > p_at_index; i--) {
blend_points[i] = blend_points[i - 1];
}
for (int i = 0; i < triangles.size(); i++) {
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] >= p_at_index) {
triangles[i].points[j]++;
}
}
}
}
blend_points[p_at_index].node = p_node;
blend_points[p_at_index].position = p_position;
blend_points[p_at_index].node->set_parent(this);
blend_points[p_at_index].node->set_graph_player(get_graph_player());
blend_points_used++;
}
void AnimationNodeBlendSpace::set_blend_point_position(int p_point, const Vector2 &p_position) {
ERR_FAIL_INDEX(p_point, blend_points_used);
blend_points[p_point].position = p_position;
}
void AnimationNodeBlendSpace::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
ERR_FAIL_INDEX(p_point, blend_points_used);
ERR_FAIL_COND(p_node.is_null());
if (blend_points[p_point].node.is_valid()) {
blend_points[p_point].node->set_parent(NULL);
blend_points[p_point].node->set_graph_player(NULL);
}
blend_points[p_point].node = p_node;
blend_points[p_point].node->set_parent(this);
blend_points[p_point].node->set_graph_player(get_graph_player());
}
Vector2 AnimationNodeBlendSpace::get_blend_point_position(int p_point) const {
ERR_FAIL_INDEX_V(p_point, blend_points_used, Vector2());
return blend_points[p_point].position;
}
Ref<AnimationRootNode> AnimationNodeBlendSpace::get_blend_point_node(int p_point) const {
ERR_FAIL_INDEX_V(p_point, blend_points_used, Ref<AnimationRootNode>());
return blend_points[p_point].node;
}
void AnimationNodeBlendSpace::remove_blend_point(int p_point) {
ERR_FAIL_INDEX(p_point, blend_points_used);
blend_points[p_point].node->set_parent(NULL);
blend_points[p_point].node->set_graph_player(NULL);
for (int i = 0; i < triangles.size(); i++) {
bool erase = false;
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] == p_point) {
erase = true;
break;
} else if (triangles[i].points[j] > p_point) {
triangles[i].points[j]--;
}
}
if (erase) {
triangles.remove(i);
i--;
}
}
for (int i = p_point; i < blend_points_used - 1; i++) {
blend_points[i] = blend_points[i + 1];
}
blend_points_used--;
}
int AnimationNodeBlendSpace::get_blend_point_count() const {
return blend_points_used;
}
bool AnimationNodeBlendSpace::has_triangle(int p_x, int p_y, int p_z) const {
ERR_FAIL_INDEX_V(p_x, blend_points_used, false);
ERR_FAIL_INDEX_V(p_y, blend_points_used, false);
ERR_FAIL_INDEX_V(p_z, blend_points_used, false);
BlendTriangle t;
t.points[0] = p_x;
t.points[1] = p_y;
t.points[2] = p_z;
SortArray<int> sort;
sort.sort(t.points, 3);
for (int i = 0; i < triangles.size(); i++) {
bool all_equal = true;
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] != t.points[j]) {
all_equal = false;
break;
}
}
if (all_equal)
return true;
}
return false;
}
void AnimationNodeBlendSpace::add_triangle(int p_x, int p_y, int p_z, int p_at_index) {
ERR_FAIL_INDEX(p_x, blend_points_used);
ERR_FAIL_INDEX(p_y, blend_points_used);
ERR_FAIL_INDEX(p_z, blend_points_used);
BlendTriangle t;
t.points[0] = p_x;
t.points[1] = p_y;
t.points[2] = p_z;
SortArray<int> sort;
sort.sort(t.points, 3);
for (int i = 0; i < triangles.size(); i++) {
bool all_equal = true;
for (int j = 0; j < 3; j++) {
if (triangles[i].points[j] != t.points[j]) {
all_equal = false;
break;
}
}
ERR_FAIL_COND(all_equal);
}
if (p_at_index == -1 || p_at_index == triangles.size()) {
triangles.push_back(t);
} else {
triangles.insert(p_at_index, t);
}
}
int AnimationNodeBlendSpace::get_triangle_point(int p_triangle, int p_point) {
ERR_FAIL_INDEX_V(p_point, 3, -1);
ERR_FAIL_INDEX_V(p_triangle, triangles.size(), -1);
return triangles[p_triangle].points[p_point];
}
void AnimationNodeBlendSpace::remove_triangle(int p_triangle) {
ERR_FAIL_INDEX(p_triangle, triangles.size());
triangles.remove(p_triangle);
}
int AnimationNodeBlendSpace::get_triangle_count() const {
return triangles.size();
}
void AnimationNodeBlendSpace::set_min_space(const Vector2 &p_min) {
min_space = p_min;
if (min_space.x >= max_space.x) {
min_space.x = max_space.x - 1;
}
if (min_space.y >= max_space.y) {
min_space.y = max_space.y - 1;
}
}
Vector2 AnimationNodeBlendSpace::get_min_space() const {
return min_space;
}
void AnimationNodeBlendSpace::set_max_space(const Vector2 &p_max) {
max_space = p_max;
if (max_space.x <= min_space.x) {
max_space.x = min_space.x + 1;
}
if (max_space.y <= min_space.y) {
max_space.y = min_space.y + 1;
}
}
Vector2 AnimationNodeBlendSpace::get_max_space() const {
return max_space;
}
void AnimationNodeBlendSpace::set_snap(const Vector2 &p_snap) {
snap = p_snap;
}
Vector2 AnimationNodeBlendSpace::get_snap() const {
return snap;
}
void AnimationNodeBlendSpace::set_blend_pos(const Vector2 &p_pos) {
blend_pos = p_pos;
}
Vector2 AnimationNodeBlendSpace::get_blend_pos() const {
return blend_pos;
}
void AnimationNodeBlendSpace::set_x_label(const String &p_label) {
x_label = p_label;
}
String AnimationNodeBlendSpace::get_x_label() const {
return x_label;
}
void AnimationNodeBlendSpace::set_y_label(const String &p_label) {
y_label = p_label;
}
String AnimationNodeBlendSpace::get_y_label() const {
return y_label;
}
void AnimationNodeBlendSpace::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
if (p_index == blend_points_used) {
add_blend_point(p_node, Vector2());
} else {
set_blend_point_node(p_index, p_node);
}
}
void AnimationNodeBlendSpace::_set_triangles(const Vector<int> &p_triangles) {
ERR_FAIL_COND(p_triangles.size() % 3 != 0);
for (int i = 0; i < p_triangles.size(); i += 3) {
add_triangle(p_triangles[i + 0], p_triangles[i + 1], p_triangles[i + 2]);
}
}
Vector<int> AnimationNodeBlendSpace::_get_triangles() const {
Vector<int> t;
t.resize(triangles.size() * 3);
for (int i = 0; i < triangles.size(); i++) {
t[i * 3 + 0] = triangles[i].points[0];
t[i * 3 + 1] = triangles[i].points[1];
t[i * 3 + 2] = triangles[i].points[2];
}
return t;
}
Vector2 AnimationNodeBlendSpace::get_closest_point(const Vector2 &p_point) {
if (triangles.size() == 0)
return Vector2();
Vector2 best_point;
bool first = true;
for (int i = 0; i < triangles.size(); i++) {
Vector2 points[3];
for (int j = 0; j < 3; j++) {
points[j] = get_blend_point_position(get_triangle_point(i, j));
}
if (Geometry::is_point_in_triangle(p_point, points[0], points[1], points[2])) {
return p_point;
}
for (int j = 0; j < 3; j++) {
Vector2 s[2] = {
points[j],
points[(j + 1) % 3]
};
Vector2 closest = Geometry::get_closest_point_to_segment_2d(p_point, s);
if (first || closest.distance_to(p_point) < best_point.distance_to(p_point)) {
best_point = closest;
first = false;
}
}
}
return best_point;
}
void AnimationNodeBlendSpace::_blend_triangle(const Vector2 &p_pos, const Vector2 *p_points, float *r_weights) {
if (p_pos.distance_squared_to(p_points[0]) < CMP_EPSILON2) {
r_weights[0] = 1;
r_weights[1] = 0;
r_weights[2] = 0;
return;
}
if (p_pos.distance_squared_to(p_points[1]) < CMP_EPSILON2) {
r_weights[0] = 0;
r_weights[1] = 1;
r_weights[2] = 0;
return;
}
if (p_pos.distance_squared_to(p_points[2]) < CMP_EPSILON2) {
r_weights[0] = 0;
r_weights[1] = 0;
r_weights[2] = 1;
return;
}
Vector2 v0 = p_points[1] - p_points[0];
Vector2 v1 = p_points[2] - p_points[0];
Vector2 v2 = p_pos - p_points[0];
float d00 = v0.dot(v0);
float d01 = v0.dot(v1);
float d11 = v1.dot(v1);
float d20 = v2.dot(v0);
float d21 = v2.dot(v1);
float denom = (d00 * d11 - d01 * d01);
if (denom == 0) {
r_weights[0] = 1;
r_weights[1] = 0;
r_weights[2] = 0;
return;
}
float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0f - v - w;
r_weights[0] = u;
r_weights[1] = v;
r_weights[2] = w;
}
float AnimationNodeBlendSpace::process(float p_time, bool p_seek) {
if (triangles.size() == 0)
return 0;
Vector2 best_point;
bool first = true;
int blend_triangle = -1;
float blend_weights[3] = { 0, 0, 0 };
for (int i = 0; i < triangles.size(); i++) {
Vector2 points[3];
for (int j = 0; j < 3; j++) {
points[j] = get_blend_point_position(get_triangle_point(i, j));
}
if (Geometry::is_point_in_triangle(blend_pos, points[0], points[1], points[2])) {
blend_triangle = i;
_blend_triangle(blend_pos, points, blend_weights);
break;
}
for (int j = 0; j < 3; j++) {
Vector2 s[2] = {
points[j],
points[(j + 1) % 3]
};
Vector2 closest = Geometry::get_closest_point_to_segment_2d(blend_pos, s);
if (first || closest.distance_to(blend_pos) < best_point.distance_to(blend_pos)) {
best_point = closest;
blend_triangle = i;
first = false;
float d = s[0].distance_to(s[1]);
if (d == 0.0) {
blend_weights[j] = 1.0;
blend_weights[(j + 1) % 3] = 0.0;
blend_weights[(j + 2) % 3] = 0.0;
} else {
float c = s[0].distance_to(closest) / d;
blend_weights[j] = 1.0 - c;
blend_weights[(j + 1) % 3] = c;
blend_weights[(j + 2) % 3] = 0.0;
}
}
}
}
ERR_FAIL_COND_V(blend_triangle == -1, 0); //should never reach here
int triangle_points[3];
for (int j = 0; j < 3; j++) {
triangle_points[j] = get_triangle_point(blend_triangle, j);
}
first = true;
float mind;
for (int i = 0; i < blend_points_used; i++) {
bool found = false;
for (int j = 0; j < 3; j++) {
if (i == triangle_points[j]) {
//blend with the given weight
float t = blend_node(blend_points[i].node, p_time, p_seek, blend_weights[j], FILTER_IGNORE, false);
if (first || t < mind) {
mind = t;
first = false;
}
found = true;
break;
}
}
if (!found) {
//ignore
blend_node(blend_points[i].node, p_time, p_seek, 0, FILTER_IGNORE, false);
}
}
return mind;
}
String AnimationNodeBlendSpace::get_caption() const {
return "BlendSpace";
}
void AnimationNodeBlendSpace::_validate_property(PropertyInfo &property) const {
if (property.name.begins_with("blend_point_")) {
String left = property.name.get_slicec('/', 0);
int idx = left.get_slicec('_', 2).to_int();
if (idx >= blend_points_used) {
property.usage = 0;
}
}
AnimationRootNode::_validate_property(property);
}
void AnimationNodeBlendSpace::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace::add_blend_point, DEFVAL(-1));
ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace::set_blend_point_position);
ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace::get_blend_point_position);
ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace::set_blend_point_node);
ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace::get_blend_point_node);
ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace::remove_blend_point);
ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace::get_blend_point_count);
ClassDB::bind_method(D_METHOD("add_triangle", "x", "y", "z", "at_index"), &AnimationNodeBlendSpace::add_triangle, DEFVAL(-1));
ClassDB::bind_method(D_METHOD("get_triangle_point", "triangle", "point"), &AnimationNodeBlendSpace::get_triangle_point);
ClassDB::bind_method(D_METHOD("remove_triangle", "triangle"), &AnimationNodeBlendSpace::remove_triangle);
ClassDB::bind_method(D_METHOD("get_triangle_count"), &AnimationNodeBlendSpace::get_triangle_count);
ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace::set_min_space);
ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace::get_min_space);
ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace::set_max_space);
ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace::get_max_space);
ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace::set_snap);
ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace::get_snap);
ClassDB::bind_method(D_METHOD("set_blend_pos", "pos"), &AnimationNodeBlendSpace::set_blend_pos);
ClassDB::bind_method(D_METHOD("get_blend_pos"), &AnimationNodeBlendSpace::get_blend_pos);
ClassDB::bind_method(D_METHOD("set_x_label", "text"), &AnimationNodeBlendSpace::set_x_label);
ClassDB::bind_method(D_METHOD("get_x_label"), &AnimationNodeBlendSpace::get_x_label);
ClassDB::bind_method(D_METHOD("set_y_label", "text"), &AnimationNodeBlendSpace::set_y_label);
ClassDB::bind_method(D_METHOD("get_y_label"), &AnimationNodeBlendSpace::get_y_label);
ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace::_add_blend_point);
ClassDB::bind_method(D_METHOD("_set_triangles", "triangles"), &AnimationNodeBlendSpace::_set_triangles);
ClassDB::bind_method(D_METHOD("_get_triangles"), &AnimationNodeBlendSpace::_get_triangles);
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE), "_add_blend_point", "get_blend_point_node", i);
ADD_PROPERTYI(PropertyInfo(Variant::VECTOR2, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
}
ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "triangles", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_triangles", "_get_triangles");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_min_space", "get_min_space");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_max_space", "get_max_space");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_snap", "get_snap");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "blend_pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_blend_pos", "get_blend_pos");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "x_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_x_label", "get_x_label");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "y_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_y_label", "get_y_label");
}
AnimationNodeBlendSpace::AnimationNodeBlendSpace() {
blend_points_used = 0;
max_space = Vector2(1, 1);
min_space = Vector2(-1, -1);
snap = Vector2(0.1, 0.1);
x_label = "x";
y_label = "y";
}
AnimationNodeBlendSpace::~AnimationNodeBlendSpace() {
for (int i = 0; i < blend_points_used; i++) {
blend_points[i].node->set_parent(this);
blend_points[i].node->set_graph_player(get_graph_player());
}
}
|