Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/animation.cpp
9903 views
1
/**************************************************************************/
2
/* animation.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "animation.h"
32
#include "animation.compat.inc"
33
34
#include "core/io/marshalls.h"
35
36
bool Animation::_set(const StringName &p_name, const Variant &p_value) {
37
String prop_name = p_name;
38
39
if (p_name == SNAME("_compression")) {
40
ERR_FAIL_COND_V(tracks.size() > 0, false); //can only set compression if no tracks exist
41
Dictionary comp = p_value;
42
ERR_FAIL_COND_V(!comp.has("fps"), false);
43
ERR_FAIL_COND_V(!comp.has("bounds"), false);
44
ERR_FAIL_COND_V(!comp.has("pages"), false);
45
ERR_FAIL_COND_V(!comp.has("format_version"), false);
46
uint32_t format_version = comp["format_version"];
47
ERR_FAIL_COND_V(format_version > Compression::FORMAT_VERSION, false); // version does not match this supported version
48
compression.fps = comp["fps"];
49
Array bounds = comp["bounds"];
50
compression.bounds.resize(bounds.size());
51
for (int i = 0; i < bounds.size(); i++) {
52
compression.bounds[i] = bounds[i];
53
}
54
Array pages = comp["pages"];
55
compression.pages.resize(pages.size());
56
for (int i = 0; i < pages.size(); i++) {
57
Dictionary page = pages[i];
58
ERR_FAIL_COND_V(!page.has("data"), false);
59
ERR_FAIL_COND_V(!page.has("time_offset"), false);
60
compression.pages[i].data = page["data"];
61
compression.pages[i].time_offset = page["time_offset"];
62
}
63
compression.enabled = true;
64
return true;
65
} else if (prop_name == SNAME("markers")) {
66
Array markers = p_value;
67
for (const Dictionary marker : markers) {
68
ERR_FAIL_COND_V(!marker.has("name"), false);
69
ERR_FAIL_COND_V(!marker.has("time"), false);
70
StringName marker_name = marker["name"];
71
double time = marker["time"];
72
_marker_insert(time, marker_names, MarkerKey(time, marker_name));
73
marker_times.insert(marker_name, time);
74
Color color = Color(1, 1, 1);
75
if (marker.has("color")) {
76
color = marker["color"];
77
}
78
marker_colors.insert(marker_name, color);
79
}
80
81
return true;
82
} else if (prop_name.begins_with("tracks/")) {
83
int track = prop_name.get_slicec('/', 1).to_int();
84
String what = prop_name.get_slicec('/', 2);
85
86
if (tracks.size() == track && what == "type") {
87
String type = p_value;
88
89
if (type == "position_3d") {
90
add_track(TYPE_POSITION_3D);
91
} else if (type == "rotation_3d") {
92
add_track(TYPE_ROTATION_3D);
93
} else if (type == "scale_3d") {
94
add_track(TYPE_SCALE_3D);
95
} else if (type == "blend_shape") {
96
add_track(TYPE_BLEND_SHAPE);
97
} else if (type == "value") {
98
add_track(TYPE_VALUE);
99
} else if (type == "method") {
100
add_track(TYPE_METHOD);
101
} else if (type == "bezier") {
102
add_track(TYPE_BEZIER);
103
} else if (type == "audio") {
104
add_track(TYPE_AUDIO);
105
} else if (type == "animation") {
106
add_track(TYPE_ANIMATION);
107
} else {
108
return false;
109
}
110
111
return true;
112
}
113
114
ERR_FAIL_INDEX_V(track, tracks.size(), false);
115
116
if (what == "path") {
117
track_set_path(track, p_value);
118
} else if (what == "compressed_track") {
119
int index = p_value;
120
ERR_FAIL_COND_V(!compression.enabled, false);
121
ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)index, compression.bounds.size(), false);
122
Track *t = tracks[track];
123
t->interpolation = INTERPOLATION_LINEAR; //only linear supported
124
switch (t->type) {
125
case TYPE_POSITION_3D: {
126
PositionTrack *tt = static_cast<PositionTrack *>(t);
127
tt->compressed_track = index;
128
} break;
129
case TYPE_ROTATION_3D: {
130
RotationTrack *rt = static_cast<RotationTrack *>(t);
131
rt->compressed_track = index;
132
} break;
133
case TYPE_SCALE_3D: {
134
ScaleTrack *st = static_cast<ScaleTrack *>(t);
135
st->compressed_track = index;
136
} break;
137
case TYPE_BLEND_SHAPE: {
138
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
139
bst->compressed_track = index;
140
} break;
141
default: {
142
return false;
143
}
144
}
145
return true;
146
} else if (what == "use_blend") {
147
if (track_get_type(track) == TYPE_AUDIO) {
148
audio_track_set_use_blend(track, p_value);
149
}
150
} else if (what == "interp") {
151
track_set_interpolation_type(track, InterpolationType(p_value.operator int()));
152
} else if (what == "loop_wrap") {
153
track_set_interpolation_loop_wrap(track, p_value);
154
} else if (what == "imported") {
155
track_set_imported(track, p_value);
156
} else if (what == "enabled") {
157
track_set_enabled(track, p_value);
158
} else if (what == "keys" || what == "key_values") {
159
if (track_get_type(track) == TYPE_POSITION_3D) {
160
PositionTrack *tt = static_cast<PositionTrack *>(tracks[track]);
161
Vector<real_t> values = p_value;
162
int vcount = values.size();
163
ERR_FAIL_COND_V(vcount % POSITION_TRACK_SIZE, false);
164
165
const real_t *r = values.ptr();
166
167
int64_t count = vcount / POSITION_TRACK_SIZE;
168
tt->positions.resize(count);
169
170
TKey<Vector3> *tw = tt->positions.ptrw();
171
for (int i = 0; i < count; i++) {
172
TKey<Vector3> &tk = tw[i];
173
const real_t *ofs = &r[i * POSITION_TRACK_SIZE];
174
tk.time = ofs[0];
175
tk.transition = ofs[1];
176
177
tk.value.x = ofs[2];
178
tk.value.y = ofs[3];
179
tk.value.z = ofs[4];
180
}
181
} else if (track_get_type(track) == TYPE_ROTATION_3D) {
182
RotationTrack *rt = static_cast<RotationTrack *>(tracks[track]);
183
Vector<real_t> values = p_value;
184
int vcount = values.size();
185
ERR_FAIL_COND_V(vcount % ROTATION_TRACK_SIZE, false);
186
187
const real_t *r = values.ptr();
188
189
int64_t count = vcount / ROTATION_TRACK_SIZE;
190
rt->rotations.resize(count);
191
192
TKey<Quaternion> *rw = rt->rotations.ptrw();
193
for (int i = 0; i < count; i++) {
194
TKey<Quaternion> &rk = rw[i];
195
const real_t *ofs = &r[i * ROTATION_TRACK_SIZE];
196
rk.time = ofs[0];
197
rk.transition = ofs[1];
198
199
rk.value.x = ofs[2];
200
rk.value.y = ofs[3];
201
rk.value.z = ofs[4];
202
rk.value.w = ofs[5];
203
}
204
} else if (track_get_type(track) == TYPE_SCALE_3D) {
205
ScaleTrack *st = static_cast<ScaleTrack *>(tracks[track]);
206
Vector<real_t> values = p_value;
207
int vcount = values.size();
208
ERR_FAIL_COND_V(vcount % SCALE_TRACK_SIZE, false);
209
210
const real_t *r = values.ptr();
211
212
int64_t count = vcount / SCALE_TRACK_SIZE;
213
st->scales.resize(count);
214
215
TKey<Vector3> *sw = st->scales.ptrw();
216
for (int i = 0; i < count; i++) {
217
TKey<Vector3> &sk = sw[i];
218
const real_t *ofs = &r[i * SCALE_TRACK_SIZE];
219
sk.time = ofs[0];
220
sk.transition = ofs[1];
221
222
sk.value.x = ofs[2];
223
sk.value.y = ofs[3];
224
sk.value.z = ofs[4];
225
}
226
} else if (track_get_type(track) == TYPE_BLEND_SHAPE) {
227
BlendShapeTrack *st = static_cast<BlendShapeTrack *>(tracks[track]);
228
Vector<real_t> values = p_value;
229
int vcount = values.size();
230
ERR_FAIL_COND_V(vcount % BLEND_SHAPE_TRACK_SIZE, false);
231
232
const real_t *r = values.ptr();
233
234
int64_t count = vcount / BLEND_SHAPE_TRACK_SIZE;
235
st->blend_shapes.resize(count);
236
237
TKey<float> *sw = st->blend_shapes.ptrw();
238
for (int i = 0; i < count; i++) {
239
TKey<float> &sk = sw[i];
240
const real_t *ofs = &r[i * BLEND_SHAPE_TRACK_SIZE];
241
sk.time = ofs[0];
242
sk.transition = ofs[1];
243
sk.value = ofs[2];
244
}
245
246
} else if (track_get_type(track) == TYPE_VALUE) {
247
ValueTrack *vt = static_cast<ValueTrack *>(tracks[track]);
248
Dictionary d = p_value;
249
ERR_FAIL_COND_V(!d.has("times"), false);
250
ERR_FAIL_COND_V(!d.has("values"), false);
251
if (d.has("cont")) {
252
bool v = d["cont"];
253
vt->update_mode = v ? UPDATE_CONTINUOUS : UPDATE_DISCRETE;
254
}
255
256
if (d.has("update")) {
257
int um = d["update"];
258
if (um < 0) {
259
um = 0;
260
} else if (um > 3) {
261
um = 3;
262
}
263
vt->update_mode = UpdateMode(um);
264
}
265
capture_included = capture_included || (vt->update_mode == UPDATE_CAPTURE);
266
267
Vector<real_t> times = d["times"];
268
Array values = d["values"];
269
270
ERR_FAIL_COND_V(times.size() != values.size(), false);
271
272
if (times.size()) {
273
int valcount = times.size();
274
275
const real_t *rt = times.ptr();
276
277
vt->values.resize(valcount);
278
279
for (int i = 0; i < valcount; i++) {
280
vt->values.write[i].time = rt[i];
281
vt->values.write[i].value = values[i];
282
}
283
284
if (d.has("transitions")) {
285
Vector<real_t> transitions = d["transitions"];
286
ERR_FAIL_COND_V(transitions.size() != valcount, false);
287
288
const real_t *rtr = transitions.ptr();
289
290
for (int i = 0; i < valcount; i++) {
291
vt->values.write[i].transition = rtr[i];
292
}
293
}
294
}
295
296
return true;
297
298
} else if (track_get_type(track) == TYPE_METHOD) {
299
while (track_get_key_count(track)) {
300
track_remove_key(track, 0); //well shouldn't be set anyway
301
}
302
303
Dictionary d = p_value;
304
ERR_FAIL_COND_V(!d.has("times"), false);
305
ERR_FAIL_COND_V(!d.has("values"), false);
306
307
Vector<real_t> times = d["times"];
308
Array values = d["values"];
309
310
ERR_FAIL_COND_V(times.size() != values.size(), false);
311
312
if (times.size()) {
313
int valcount = times.size();
314
315
const real_t *rt = times.ptr();
316
317
for (int i = 0; i < valcount; i++) {
318
track_insert_key(track, rt[i], values[i]);
319
}
320
321
if (d.has("transitions")) {
322
Vector<real_t> transitions = d["transitions"];
323
ERR_FAIL_COND_V(transitions.size() != valcount, false);
324
325
const real_t *rtr = transitions.ptr();
326
327
for (int i = 0; i < valcount; i++) {
328
track_set_key_transition(track, i, rtr[i]);
329
}
330
}
331
}
332
} else if (track_get_type(track) == TYPE_BEZIER) {
333
BezierTrack *bt = static_cast<BezierTrack *>(tracks[track]);
334
Dictionary d = p_value;
335
ERR_FAIL_COND_V(!d.has("times"), false);
336
ERR_FAIL_COND_V(!d.has("points"), false);
337
Vector<real_t> times = d["times"];
338
Vector<real_t> values = d["points"];
339
#ifdef TOOLS_ENABLED
340
Vector<int> handle_modes;
341
if (d.has("handle_modes")) {
342
handle_modes = d["handle_modes"];
343
} else {
344
handle_modes.resize_initialized(times.size());
345
}
346
#endif // TOOLS_ENABLED
347
348
ERR_FAIL_COND_V(times.size() * 5 != values.size(), false);
349
350
if (times.size()) {
351
int valcount = times.size();
352
353
const real_t *rt = times.ptr();
354
const real_t *rv = values.ptr();
355
#ifdef TOOLS_ENABLED
356
const int *rh = handle_modes.ptr();
357
#endif // TOOLS_ENABLED
358
359
bt->values.resize(valcount);
360
361
for (int i = 0; i < valcount; i++) {
362
bt->values.write[i].time = rt[i];
363
bt->values.write[i].transition = 0; //unused in bezier
364
bt->values.write[i].value.value = rv[i * 5 + 0];
365
bt->values.write[i].value.in_handle.x = rv[i * 5 + 1];
366
bt->values.write[i].value.in_handle.y = rv[i * 5 + 2];
367
bt->values.write[i].value.out_handle.x = rv[i * 5 + 3];
368
bt->values.write[i].value.out_handle.y = rv[i * 5 + 4];
369
#ifdef TOOLS_ENABLED
370
bt->values.write[i].value.handle_mode = static_cast<HandleMode>(rh[i]);
371
#endif // TOOLS_ENABLED
372
}
373
}
374
375
return true;
376
} else if (track_get_type(track) == TYPE_AUDIO) {
377
AudioTrack *ad = static_cast<AudioTrack *>(tracks[track]);
378
Dictionary d = p_value;
379
ERR_FAIL_COND_V(!d.has("times"), false);
380
ERR_FAIL_COND_V(!d.has("clips"), false);
381
382
Vector<real_t> times = d["times"];
383
Array clips = d["clips"];
384
385
ERR_FAIL_COND_V(clips.size() != times.size(), false);
386
387
if (times.size()) {
388
int valcount = times.size();
389
390
const real_t *rt = times.ptr();
391
392
ad->values.clear();
393
394
for (int i = 0; i < valcount; i++) {
395
Dictionary d2 = clips[i];
396
if (!d2.has("start_offset")) {
397
continue;
398
}
399
if (!d2.has("end_offset")) {
400
continue;
401
}
402
if (!d2.has("stream")) {
403
continue;
404
}
405
406
TKey<AudioKey> ak;
407
ak.time = rt[i];
408
ak.value.start_offset = d2["start_offset"];
409
ak.value.end_offset = d2["end_offset"];
410
ak.value.stream = d2["stream"];
411
412
ad->values.push_back(ak);
413
}
414
}
415
416
return true;
417
} else if (track_get_type(track) == TYPE_ANIMATION) {
418
AnimationTrack *an = static_cast<AnimationTrack *>(tracks[track]);
419
Dictionary d = p_value;
420
ERR_FAIL_COND_V(!d.has("times"), false);
421
ERR_FAIL_COND_V(!d.has("clips"), false);
422
423
Vector<real_t> times = d["times"];
424
Vector<String> clips = d["clips"];
425
426
ERR_FAIL_COND_V(clips.size() != times.size(), false);
427
428
if (times.size()) {
429
int valcount = times.size();
430
431
const real_t *rt = times.ptr();
432
const String *rc = clips.ptr();
433
434
an->values.resize(valcount);
435
436
for (int i = 0; i < valcount; i++) {
437
TKey<StringName> ak;
438
ak.time = rt[i];
439
ak.value = rc[i];
440
an->values.write[i] = ak;
441
}
442
}
443
444
return true;
445
} else {
446
return false;
447
}
448
} else {
449
return false;
450
}
451
#ifndef DISABLE_DEPRECATED
452
} else if (prop_name == "loop" && p_value.operator bool()) { // Compatibility with Godot 3.x.
453
loop_mode = Animation::LoopMode::LOOP_LINEAR;
454
return true;
455
#endif // DISABLE_DEPRECATED
456
} else {
457
return false;
458
}
459
460
return true;
461
}
462
463
bool Animation::_get(const StringName &p_name, Variant &r_ret) const {
464
String prop_name = p_name;
465
466
if (p_name == SNAME("_compression")) {
467
if (!compression.enabled) {
468
return false;
469
}
470
Dictionary comp;
471
comp["fps"] = compression.fps;
472
Array bounds;
473
bounds.resize(compression.bounds.size());
474
for (uint32_t i = 0; i < compression.bounds.size(); i++) {
475
bounds[i] = compression.bounds[i];
476
}
477
comp["bounds"] = bounds;
478
Array pages;
479
pages.resize(compression.pages.size());
480
for (uint32_t i = 0; i < compression.pages.size(); i++) {
481
Dictionary page;
482
page["data"] = compression.pages[i].data;
483
page["time_offset"] = compression.pages[i].time_offset;
484
pages[i] = page;
485
}
486
comp["pages"] = pages;
487
comp["format_version"] = Compression::FORMAT_VERSION;
488
489
r_ret = comp;
490
return true;
491
} else if (prop_name == SNAME("markers")) {
492
Array markers;
493
494
for (HashMap<StringName, double>::ConstIterator E = marker_times.begin(); E; ++E) {
495
Dictionary d;
496
d["name"] = E->key;
497
d["time"] = E->value;
498
d["color"] = marker_colors[E->key];
499
markers.push_back(d);
500
}
501
502
r_ret = markers;
503
} else if (prop_name == "length") {
504
r_ret = length;
505
} else if (prop_name == "loop_mode") {
506
r_ret = loop_mode;
507
} else if (prop_name == "step") {
508
r_ret = step;
509
} else if (prop_name.begins_with("tracks/")) {
510
int track = prop_name.get_slicec('/', 1).to_int();
511
String what = prop_name.get_slicec('/', 2);
512
ERR_FAIL_INDEX_V(track, tracks.size(), false);
513
if (what == "type") {
514
switch (track_get_type(track)) {
515
case TYPE_POSITION_3D:
516
r_ret = "position_3d";
517
break;
518
case TYPE_ROTATION_3D:
519
r_ret = "rotation_3d";
520
break;
521
case TYPE_SCALE_3D:
522
r_ret = "scale_3d";
523
break;
524
case TYPE_BLEND_SHAPE:
525
r_ret = "blend_shape";
526
break;
527
case TYPE_VALUE:
528
r_ret = "value";
529
break;
530
case TYPE_METHOD:
531
r_ret = "method";
532
break;
533
case TYPE_BEZIER:
534
r_ret = "bezier";
535
break;
536
case TYPE_AUDIO:
537
r_ret = "audio";
538
break;
539
case TYPE_ANIMATION:
540
r_ret = "animation";
541
break;
542
}
543
544
return true;
545
546
} else if (what == "path") {
547
r_ret = track_get_path(track);
548
} else if (what == "compressed_track") {
549
ERR_FAIL_COND_V(!compression.enabled, false);
550
Track *t = tracks[track];
551
switch (t->type) {
552
case TYPE_POSITION_3D: {
553
PositionTrack *tt = static_cast<PositionTrack *>(t);
554
r_ret = tt->compressed_track;
555
} break;
556
case TYPE_ROTATION_3D: {
557
RotationTrack *rt = static_cast<RotationTrack *>(t);
558
r_ret = rt->compressed_track;
559
} break;
560
case TYPE_SCALE_3D: {
561
ScaleTrack *st = static_cast<ScaleTrack *>(t);
562
r_ret = st->compressed_track;
563
} break;
564
case TYPE_BLEND_SHAPE: {
565
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
566
r_ret = bst->compressed_track;
567
} break;
568
default: {
569
r_ret = Variant();
570
ERR_FAIL_V(false);
571
}
572
}
573
574
return true;
575
} else if (what == "use_blend") {
576
if (track_get_type(track) == TYPE_AUDIO) {
577
r_ret = audio_track_is_use_blend(track);
578
}
579
} else if (what == "interp") {
580
r_ret = track_get_interpolation_type(track);
581
} else if (what == "loop_wrap") {
582
r_ret = track_get_interpolation_loop_wrap(track);
583
} else if (what == "imported") {
584
r_ret = track_is_imported(track);
585
} else if (what == "enabled") {
586
r_ret = track_is_enabled(track);
587
} else if (what == "keys") {
588
if (track_get_type(track) == TYPE_POSITION_3D) {
589
Vector<real_t> keys;
590
int kk = track_get_key_count(track);
591
keys.resize(kk * POSITION_TRACK_SIZE);
592
593
real_t *w = keys.ptrw();
594
595
int idx = 0;
596
for (int i = 0; i < track_get_key_count(track); i++) {
597
Vector3 loc;
598
position_track_get_key(track, i, &loc);
599
600
w[idx++] = track_get_key_time(track, i);
601
w[idx++] = track_get_key_transition(track, i);
602
w[idx++] = loc.x;
603
w[idx++] = loc.y;
604
w[idx++] = loc.z;
605
}
606
607
r_ret = keys;
608
return true;
609
} else if (track_get_type(track) == TYPE_ROTATION_3D) {
610
Vector<real_t> keys;
611
int kk = track_get_key_count(track);
612
keys.resize(kk * ROTATION_TRACK_SIZE);
613
614
real_t *w = keys.ptrw();
615
616
int idx = 0;
617
for (int i = 0; i < track_get_key_count(track); i++) {
618
Quaternion rot;
619
rotation_track_get_key(track, i, &rot);
620
621
w[idx++] = track_get_key_time(track, i);
622
w[idx++] = track_get_key_transition(track, i);
623
w[idx++] = rot.x;
624
w[idx++] = rot.y;
625
w[idx++] = rot.z;
626
w[idx++] = rot.w;
627
}
628
629
r_ret = keys;
630
return true;
631
632
} else if (track_get_type(track) == TYPE_SCALE_3D) {
633
Vector<real_t> keys;
634
int kk = track_get_key_count(track);
635
keys.resize(kk * SCALE_TRACK_SIZE);
636
637
real_t *w = keys.ptrw();
638
639
int idx = 0;
640
for (int i = 0; i < track_get_key_count(track); i++) {
641
Vector3 scale;
642
scale_track_get_key(track, i, &scale);
643
644
w[idx++] = track_get_key_time(track, i);
645
w[idx++] = track_get_key_transition(track, i);
646
w[idx++] = scale.x;
647
w[idx++] = scale.y;
648
w[idx++] = scale.z;
649
}
650
651
r_ret = keys;
652
return true;
653
} else if (track_get_type(track) == TYPE_BLEND_SHAPE) {
654
Vector<real_t> keys;
655
int kk = track_get_key_count(track);
656
keys.resize(kk * BLEND_SHAPE_TRACK_SIZE);
657
658
real_t *w = keys.ptrw();
659
660
int idx = 0;
661
for (int i = 0; i < track_get_key_count(track); i++) {
662
float bs;
663
blend_shape_track_get_key(track, i, &bs);
664
665
w[idx++] = track_get_key_time(track, i);
666
w[idx++] = track_get_key_transition(track, i);
667
w[idx++] = bs;
668
}
669
670
r_ret = keys;
671
return true;
672
} else if (track_get_type(track) == TYPE_VALUE) {
673
const ValueTrack *vt = static_cast<const ValueTrack *>(tracks[track]);
674
675
Dictionary d;
676
677
Vector<real_t> key_times;
678
Vector<real_t> key_transitions;
679
Array key_values;
680
681
int kk = vt->values.size();
682
683
key_times.resize(kk);
684
key_transitions.resize(kk);
685
key_values.resize(kk);
686
687
real_t *wti = key_times.ptrw();
688
real_t *wtr = key_transitions.ptrw();
689
690
int idx = 0;
691
692
const TKey<Variant> *vls = vt->values.ptr();
693
694
for (int i = 0; i < kk; i++) {
695
wti[idx] = vls[i].time;
696
wtr[idx] = vls[i].transition;
697
key_values[idx] = vls[i].value;
698
idx++;
699
}
700
701
d["times"] = key_times;
702
d["transitions"] = key_transitions;
703
d["values"] = key_values;
704
if (track_get_type(track) == TYPE_VALUE) {
705
d["update"] = value_track_get_update_mode(track);
706
}
707
708
r_ret = d;
709
710
return true;
711
712
} else if (track_get_type(track) == TYPE_METHOD) {
713
Dictionary d;
714
715
Vector<real_t> key_times;
716
Vector<real_t> key_transitions;
717
Array key_values;
718
719
int kk = track_get_key_count(track);
720
721
key_times.resize(kk);
722
key_transitions.resize(kk);
723
key_values.resize(kk);
724
725
real_t *wti = key_times.ptrw();
726
real_t *wtr = key_transitions.ptrw();
727
728
int idx = 0;
729
for (int i = 0; i < track_get_key_count(track); i++) {
730
wti[idx] = track_get_key_time(track, i);
731
wtr[idx] = track_get_key_transition(track, i);
732
key_values[idx] = track_get_key_value(track, i);
733
idx++;
734
}
735
736
d["times"] = key_times;
737
d["transitions"] = key_transitions;
738
d["values"] = key_values;
739
if (track_get_type(track) == TYPE_VALUE) {
740
d["update"] = value_track_get_update_mode(track);
741
}
742
743
r_ret = d;
744
745
return true;
746
} else if (track_get_type(track) == TYPE_BEZIER) {
747
const BezierTrack *bt = static_cast<const BezierTrack *>(tracks[track]);
748
749
Dictionary d;
750
751
Vector<real_t> key_times;
752
Vector<real_t> key_points;
753
754
int kk = bt->values.size();
755
756
key_times.resize(kk);
757
key_points.resize(kk * 5);
758
759
real_t *wti = key_times.ptrw();
760
real_t *wpo = key_points.ptrw();
761
762
#ifdef TOOLS_ENABLED
763
Vector<int> handle_modes;
764
handle_modes.resize(kk);
765
int *whm = handle_modes.ptrw();
766
#endif // TOOLS_ENABLED
767
768
int idx = 0;
769
770
const TKey<BezierKey> *vls = bt->values.ptr();
771
772
for (int i = 0; i < kk; i++) {
773
wti[idx] = vls[i].time;
774
wpo[idx * 5 + 0] = vls[i].value.value;
775
wpo[idx * 5 + 1] = vls[i].value.in_handle.x;
776
wpo[idx * 5 + 2] = vls[i].value.in_handle.y;
777
wpo[idx * 5 + 3] = vls[i].value.out_handle.x;
778
wpo[idx * 5 + 4] = vls[i].value.out_handle.y;
779
#ifdef TOOLS_ENABLED
780
whm[idx] = static_cast<int>(vls[i].value.handle_mode);
781
#endif // TOOLS_ENABLED
782
idx++;
783
}
784
785
d["times"] = key_times;
786
d["points"] = key_points;
787
#ifdef TOOLS_ENABLED
788
d["handle_modes"] = handle_modes;
789
#endif // TOOLS_ENABLED
790
791
r_ret = d;
792
793
return true;
794
} else if (track_get_type(track) == TYPE_AUDIO) {
795
const AudioTrack *ad = static_cast<const AudioTrack *>(tracks[track]);
796
797
Dictionary d;
798
799
Vector<real_t> key_times;
800
Array clips;
801
802
int kk = ad->values.size();
803
804
key_times.resize(kk);
805
806
real_t *wti = key_times.ptrw();
807
808
int idx = 0;
809
810
const TKey<AudioKey> *vls = ad->values.ptr();
811
812
for (int i = 0; i < kk; i++) {
813
wti[idx] = vls[i].time;
814
Dictionary clip;
815
clip["start_offset"] = vls[i].value.start_offset;
816
clip["end_offset"] = vls[i].value.end_offset;
817
clip["stream"] = vls[i].value.stream;
818
clips.push_back(clip);
819
idx++;
820
}
821
822
d["times"] = key_times;
823
d["clips"] = clips;
824
825
r_ret = d;
826
827
return true;
828
} else if (track_get_type(track) == TYPE_ANIMATION) {
829
const AnimationTrack *an = static_cast<const AnimationTrack *>(tracks[track]);
830
831
Dictionary d;
832
833
Vector<real_t> key_times;
834
Vector<String> clips;
835
836
int kk = an->values.size();
837
838
key_times.resize(kk);
839
clips.resize(kk);
840
841
real_t *wti = key_times.ptrw();
842
String *wcl = clips.ptrw();
843
844
const TKey<StringName> *vls = an->values.ptr();
845
846
for (int i = 0; i < kk; i++) {
847
wti[i] = vls[i].time;
848
wcl[i] = vls[i].value;
849
}
850
851
d["times"] = key_times;
852
d["clips"] = clips;
853
854
r_ret = d;
855
856
return true;
857
}
858
} else {
859
return false;
860
}
861
} else {
862
return false;
863
}
864
865
return true;
866
}
867
868
void Animation::_get_property_list(List<PropertyInfo> *p_list) const {
869
if (compression.enabled) {
870
p_list->push_back(PropertyInfo(Variant::DICTIONARY, "_compression", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
871
}
872
p_list->push_back(PropertyInfo(Variant::ARRAY, "markers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
873
for (int i = 0; i < tracks.size(); i++) {
874
p_list->push_back(PropertyInfo(Variant::STRING, "tracks/" + itos(i) + "/type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
875
p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/imported", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
876
p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
877
p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
878
if (track_is_compressed(i)) {
879
p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/compressed_track", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
880
} else {
881
p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
882
p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
883
p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
884
}
885
if (track_get_type(i) == TYPE_AUDIO) {
886
p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/use_blend", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
887
}
888
}
889
}
890
891
void Animation::reset_state() {
892
clear();
893
}
894
895
int Animation::add_track(TrackType p_type, int p_at_pos) {
896
if (p_at_pos < 0 || p_at_pos >= tracks.size()) {
897
p_at_pos = tracks.size();
898
}
899
900
switch (p_type) {
901
case TYPE_POSITION_3D: {
902
PositionTrack *tt = memnew(PositionTrack);
903
tracks.insert(p_at_pos, tt);
904
} break;
905
case TYPE_ROTATION_3D: {
906
RotationTrack *rt = memnew(RotationTrack);
907
tracks.insert(p_at_pos, rt);
908
} break;
909
case TYPE_SCALE_3D: {
910
ScaleTrack *st = memnew(ScaleTrack);
911
tracks.insert(p_at_pos, st);
912
} break;
913
case TYPE_BLEND_SHAPE: {
914
BlendShapeTrack *bst = memnew(BlendShapeTrack);
915
tracks.insert(p_at_pos, bst);
916
} break;
917
case TYPE_VALUE: {
918
tracks.insert(p_at_pos, memnew(ValueTrack));
919
920
} break;
921
case TYPE_METHOD: {
922
tracks.insert(p_at_pos, memnew(MethodTrack));
923
924
} break;
925
case TYPE_BEZIER: {
926
tracks.insert(p_at_pos, memnew(BezierTrack));
927
928
} break;
929
case TYPE_AUDIO: {
930
tracks.insert(p_at_pos, memnew(AudioTrack));
931
932
} break;
933
case TYPE_ANIMATION: {
934
tracks.insert(p_at_pos, memnew(AnimationTrack));
935
936
} break;
937
default: {
938
ERR_PRINT("Unknown track type");
939
}
940
}
941
emit_changed();
942
return p_at_pos;
943
}
944
945
void Animation::remove_track(int p_track) {
946
ERR_FAIL_INDEX(p_track, tracks.size());
947
Track *t = tracks[p_track];
948
949
switch (t->type) {
950
case TYPE_POSITION_3D: {
951
PositionTrack *tt = static_cast<PositionTrack *>(t);
952
ERR_FAIL_COND_MSG(tt->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
953
tt->positions.clear();
954
955
} break;
956
case TYPE_ROTATION_3D: {
957
RotationTrack *rt = static_cast<RotationTrack *>(t);
958
ERR_FAIL_COND_MSG(rt->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
959
rt->rotations.clear();
960
961
} break;
962
case TYPE_SCALE_3D: {
963
ScaleTrack *st = static_cast<ScaleTrack *>(t);
964
ERR_FAIL_COND_MSG(st->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
965
st->scales.clear();
966
967
} break;
968
case TYPE_BLEND_SHAPE: {
969
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
970
ERR_FAIL_COND_MSG(bst->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first.");
971
bst->blend_shapes.clear();
972
973
} break;
974
case TYPE_VALUE: {
975
ValueTrack *vt = static_cast<ValueTrack *>(t);
976
vt->values.clear();
977
978
} break;
979
case TYPE_METHOD: {
980
MethodTrack *mt = static_cast<MethodTrack *>(t);
981
mt->methods.clear();
982
983
} break;
984
case TYPE_BEZIER: {
985
BezierTrack *bz = static_cast<BezierTrack *>(t);
986
bz->values.clear();
987
988
} break;
989
case TYPE_AUDIO: {
990
AudioTrack *ad = static_cast<AudioTrack *>(t);
991
ad->values.clear();
992
993
} break;
994
case TYPE_ANIMATION: {
995
AnimationTrack *an = static_cast<AnimationTrack *>(t);
996
an->values.clear();
997
998
} break;
999
}
1000
1001
memdelete(t);
1002
tracks.remove_at(p_track);
1003
emit_changed();
1004
_check_capture_included();
1005
}
1006
1007
bool Animation::is_capture_included() const {
1008
return capture_included;
1009
}
1010
1011
void Animation::_check_capture_included() {
1012
capture_included = false;
1013
for (int i = 0; i < tracks.size(); i++) {
1014
if (tracks[i]->type == TYPE_VALUE) {
1015
ValueTrack *vt = static_cast<ValueTrack *>(tracks[i]);
1016
if (vt->update_mode == UPDATE_CAPTURE) {
1017
capture_included = true;
1018
break;
1019
}
1020
}
1021
}
1022
}
1023
1024
int Animation::get_track_count() const {
1025
return tracks.size();
1026
}
1027
1028
Animation::TrackType Animation::track_get_type(int p_track) const {
1029
ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_VALUE);
1030
return tracks[p_track]->type;
1031
}
1032
1033
void Animation::track_set_path(int p_track, const NodePath &p_path) {
1034
ERR_FAIL_INDEX(p_track, tracks.size());
1035
tracks[p_track]->path = p_path;
1036
_track_update_hash(p_track);
1037
emit_changed();
1038
}
1039
1040
NodePath Animation::track_get_path(int p_track) const {
1041
ERR_FAIL_INDEX_V(p_track, tracks.size(), NodePath());
1042
return tracks[p_track]->path;
1043
}
1044
1045
int Animation::find_track(const NodePath &p_path, const TrackType p_type) const {
1046
for (int i = 0; i < tracks.size(); i++) {
1047
if (tracks[i]->path == p_path && tracks[i]->type == p_type) {
1048
return i;
1049
}
1050
};
1051
return -1;
1052
}
1053
1054
Animation::TrackType Animation::get_cache_type(TrackType p_type) {
1055
if (p_type == Animation::TYPE_BEZIER) {
1056
return Animation::TYPE_VALUE;
1057
}
1058
if (p_type == Animation::TYPE_ROTATION_3D || p_type == Animation::TYPE_SCALE_3D) {
1059
return Animation::TYPE_POSITION_3D; // Reference them as position3D tracks, even if they modify rotation or scale.
1060
}
1061
return p_type;
1062
}
1063
1064
void Animation::_track_update_hash(int p_track) {
1065
NodePath track_path = tracks[p_track]->path;
1066
TrackType track_cache_type = get_cache_type(tracks[p_track]->type);
1067
tracks[p_track]->thash = StringName(String(track_path.get_concatenated_names()) + String(track_path.get_concatenated_subnames()) + itos(track_cache_type)).hash();
1068
}
1069
1070
Animation::TypeHash Animation::track_get_type_hash(int p_track) const {
1071
ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
1072
return tracks[p_track]->thash;
1073
}
1074
1075
void Animation::track_set_interpolation_type(int p_track, InterpolationType p_interp) {
1076
ERR_FAIL_INDEX(p_track, tracks.size());
1077
tracks[p_track]->interpolation = p_interp;
1078
emit_changed();
1079
}
1080
1081
Animation::InterpolationType Animation::track_get_interpolation_type(int p_track) const {
1082
ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
1083
return tracks[p_track]->interpolation;
1084
}
1085
1086
void Animation::track_set_interpolation_loop_wrap(int p_track, bool p_enable) {
1087
ERR_FAIL_INDEX(p_track, tracks.size());
1088
tracks[p_track]->loop_wrap = p_enable;
1089
emit_changed();
1090
}
1091
1092
bool Animation::track_get_interpolation_loop_wrap(int p_track) const {
1093
ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
1094
return tracks[p_track]->loop_wrap;
1095
}
1096
1097
template <typename T, typename V>
1098
int Animation::_insert(double p_time, T &p_keys, const V &p_value) {
1099
int idx = p_keys.size();
1100
1101
while (true) {
1102
// Condition for replacement.
1103
if (idx > 0 && Math::is_equal_approx((double)p_keys[idx - 1].time, p_time)) {
1104
float transition = p_keys[idx - 1].transition;
1105
p_keys.write[idx - 1] = p_value;
1106
p_keys.write[idx - 1].transition = transition;
1107
return idx - 1;
1108
1109
// Condition for insert.
1110
} else if (idx == 0 || p_keys[idx - 1].time < p_time) {
1111
p_keys.insert(idx, p_value);
1112
return idx;
1113
}
1114
1115
idx--;
1116
}
1117
1118
return -1;
1119
}
1120
1121
int Animation::_marker_insert(double p_time, Vector<MarkerKey> &p_keys, const MarkerKey &p_value) {
1122
int idx = p_keys.size();
1123
1124
while (true) {
1125
// Condition for replacement.
1126
if (idx > 0 && Math::is_equal_approx((double)p_keys[idx - 1].time, p_time)) {
1127
p_keys.write[idx - 1] = p_value;
1128
return idx - 1;
1129
1130
// Condition for insert.
1131
} else if (idx == 0 || p_keys[idx - 1].time < p_time) {
1132
p_keys.insert(idx, p_value);
1133
return idx;
1134
}
1135
1136
idx--;
1137
}
1138
1139
return -1;
1140
}
1141
1142
////
1143
1144
int Animation::position_track_insert_key(int p_track, double p_time, const Vector3 &p_position) {
1145
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1146
Track *t = tracks[p_track];
1147
ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, -1);
1148
1149
PositionTrack *tt = static_cast<PositionTrack *>(t);
1150
1151
ERR_FAIL_COND_V(tt->compressed_track >= 0, -1);
1152
1153
TKey<Vector3> tkey;
1154
tkey.time = p_time;
1155
tkey.value = p_position;
1156
1157
int ret = _insert(p_time, tt->positions, tkey);
1158
emit_changed();
1159
return ret;
1160
}
1161
1162
Error Animation::position_track_get_key(int p_track, int p_key, Vector3 *r_position) const {
1163
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1164
Track *t = tracks[p_track];
1165
1166
PositionTrack *tt = static_cast<PositionTrack *>(t);
1167
ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, ERR_INVALID_PARAMETER);
1168
1169
if (tt->compressed_track >= 0) {
1170
Vector3i key;
1171
double time;
1172
bool fetch_success = _fetch_compressed_by_index<3>(tt->compressed_track, p_key, key, time);
1173
if (!fetch_success) {
1174
return ERR_INVALID_PARAMETER;
1175
}
1176
1177
*r_position = _uncompress_pos_scale(tt->compressed_track, key);
1178
return OK;
1179
}
1180
1181
ERR_FAIL_INDEX_V(p_key, tt->positions.size(), ERR_INVALID_PARAMETER);
1182
1183
*r_position = tt->positions[p_key].value;
1184
1185
return OK;
1186
}
1187
1188
Error Animation::try_position_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation, bool p_backward) const {
1189
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1190
Track *t = tracks[p_track];
1191
ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, ERR_INVALID_PARAMETER);
1192
1193
PositionTrack *tt = static_cast<PositionTrack *>(t);
1194
1195
if (tt->compressed_track >= 0) {
1196
if (_pos_scale_interpolate_compressed(tt->compressed_track, p_time, *r_interpolation)) {
1197
return OK;
1198
} else {
1199
return ERR_UNAVAILABLE;
1200
}
1201
}
1202
1203
bool ok = false;
1204
1205
Vector3 tk = _interpolate(tt->positions, p_time, tt->interpolation, tt->loop_wrap, &ok, p_backward);
1206
1207
if (!ok) {
1208
return ERR_UNAVAILABLE;
1209
}
1210
*r_interpolation = tk;
1211
return OK;
1212
}
1213
1214
Vector3 Animation::position_track_interpolate(int p_track, double p_time, bool p_backward) const {
1215
Vector3 ret = Vector3(0, 0, 0);
1216
ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
1217
bool err = try_position_track_interpolate(p_track, p_time, &ret, p_backward);
1218
ERR_FAIL_COND_V_MSG(err, ret, "3D Position Track: '" + String(tracks[p_track]->path) + "' is unavailable.");
1219
return ret;
1220
}
1221
1222
////
1223
1224
int Animation::rotation_track_insert_key(int p_track, double p_time, const Quaternion &p_rotation) {
1225
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1226
Track *t = tracks[p_track];
1227
ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, -1);
1228
1229
RotationTrack *rt = static_cast<RotationTrack *>(t);
1230
1231
ERR_FAIL_COND_V(rt->compressed_track >= 0, -1);
1232
1233
TKey<Quaternion> tkey;
1234
tkey.time = p_time;
1235
tkey.value = p_rotation;
1236
1237
int ret = _insert(p_time, rt->rotations, tkey);
1238
emit_changed();
1239
return ret;
1240
}
1241
1242
Error Animation::rotation_track_get_key(int p_track, int p_key, Quaternion *r_rotation) const {
1243
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1244
Track *t = tracks[p_track];
1245
1246
RotationTrack *rt = static_cast<RotationTrack *>(t);
1247
ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, ERR_INVALID_PARAMETER);
1248
1249
if (rt->compressed_track >= 0) {
1250
Vector3i key;
1251
double time;
1252
bool fetch_success = _fetch_compressed_by_index<3>(rt->compressed_track, p_key, key, time);
1253
if (!fetch_success) {
1254
return ERR_INVALID_PARAMETER;
1255
}
1256
1257
*r_rotation = _uncompress_quaternion(key);
1258
return OK;
1259
}
1260
1261
ERR_FAIL_INDEX_V(p_key, rt->rotations.size(), ERR_INVALID_PARAMETER);
1262
1263
*r_rotation = rt->rotations[p_key].value;
1264
1265
return OK;
1266
}
1267
1268
Error Animation::try_rotation_track_interpolate(int p_track, double p_time, Quaternion *r_interpolation, bool p_backward) const {
1269
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1270
Track *t = tracks[p_track];
1271
ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, ERR_INVALID_PARAMETER);
1272
1273
RotationTrack *rt = static_cast<RotationTrack *>(t);
1274
1275
if (rt->compressed_track >= 0) {
1276
if (_rotation_interpolate_compressed(rt->compressed_track, p_time, *r_interpolation)) {
1277
return OK;
1278
} else {
1279
return ERR_UNAVAILABLE;
1280
}
1281
}
1282
1283
bool ok = false;
1284
1285
Quaternion tk = _interpolate(rt->rotations, p_time, rt->interpolation, rt->loop_wrap, &ok, p_backward);
1286
1287
if (!ok) {
1288
return ERR_UNAVAILABLE;
1289
}
1290
*r_interpolation = tk;
1291
return OK;
1292
}
1293
1294
Quaternion Animation::rotation_track_interpolate(int p_track, double p_time, bool p_backward) const {
1295
Quaternion ret = Quaternion(0, 0, 0, 1);
1296
ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
1297
bool err = try_rotation_track_interpolate(p_track, p_time, &ret, p_backward);
1298
ERR_FAIL_COND_V_MSG(err, ret, "3D Rotation Track: '" + String(tracks[p_track]->path) + "' is unavailable.");
1299
return ret;
1300
}
1301
1302
////
1303
1304
int Animation::scale_track_insert_key(int p_track, double p_time, const Vector3 &p_scale) {
1305
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1306
Track *t = tracks[p_track];
1307
ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, -1);
1308
1309
ScaleTrack *st = static_cast<ScaleTrack *>(t);
1310
1311
ERR_FAIL_COND_V(st->compressed_track >= 0, -1);
1312
1313
TKey<Vector3> tkey;
1314
tkey.time = p_time;
1315
tkey.value = p_scale;
1316
1317
int ret = _insert(p_time, st->scales, tkey);
1318
emit_changed();
1319
return ret;
1320
}
1321
1322
Error Animation::scale_track_get_key(int p_track, int p_key, Vector3 *r_scale) const {
1323
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1324
Track *t = tracks[p_track];
1325
1326
ScaleTrack *st = static_cast<ScaleTrack *>(t);
1327
ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, ERR_INVALID_PARAMETER);
1328
1329
if (st->compressed_track >= 0) {
1330
Vector3i key;
1331
double time;
1332
bool fetch_success = _fetch_compressed_by_index<3>(st->compressed_track, p_key, key, time);
1333
if (!fetch_success) {
1334
return ERR_INVALID_PARAMETER;
1335
}
1336
1337
*r_scale = _uncompress_pos_scale(st->compressed_track, key);
1338
return OK;
1339
}
1340
1341
ERR_FAIL_INDEX_V(p_key, st->scales.size(), ERR_INVALID_PARAMETER);
1342
1343
*r_scale = st->scales[p_key].value;
1344
1345
return OK;
1346
}
1347
1348
Error Animation::try_scale_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation, bool p_backward) const {
1349
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1350
Track *t = tracks[p_track];
1351
ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, ERR_INVALID_PARAMETER);
1352
1353
ScaleTrack *st = static_cast<ScaleTrack *>(t);
1354
1355
if (st->compressed_track >= 0) {
1356
if (_pos_scale_interpolate_compressed(st->compressed_track, p_time, *r_interpolation)) {
1357
return OK;
1358
} else {
1359
return ERR_UNAVAILABLE;
1360
}
1361
}
1362
1363
bool ok = false;
1364
1365
Vector3 tk = _interpolate(st->scales, p_time, st->interpolation, st->loop_wrap, &ok, p_backward);
1366
1367
if (!ok) {
1368
return ERR_UNAVAILABLE;
1369
}
1370
*r_interpolation = tk;
1371
return OK;
1372
}
1373
1374
Vector3 Animation::scale_track_interpolate(int p_track, double p_time, bool p_backward) const {
1375
Vector3 ret = Vector3(1, 1, 1);
1376
ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
1377
bool err = try_scale_track_interpolate(p_track, p_time, &ret, p_backward);
1378
ERR_FAIL_COND_V_MSG(err, ret, "3D Scale Track: '" + String(tracks[p_track]->path) + "' is unavailable.");
1379
return ret;
1380
}
1381
1382
////
1383
1384
int Animation::blend_shape_track_insert_key(int p_track, double p_time, float p_blend_shape) {
1385
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1386
Track *t = tracks[p_track];
1387
ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, -1);
1388
1389
BlendShapeTrack *st = static_cast<BlendShapeTrack *>(t);
1390
1391
ERR_FAIL_COND_V(st->compressed_track >= 0, -1);
1392
1393
TKey<float> tkey;
1394
tkey.time = p_time;
1395
tkey.value = p_blend_shape;
1396
1397
int ret = _insert(p_time, st->blend_shapes, tkey);
1398
emit_changed();
1399
return ret;
1400
}
1401
1402
Error Animation::blend_shape_track_get_key(int p_track, int p_key, float *r_blend_shape) const {
1403
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1404
Track *t = tracks[p_track];
1405
1406
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
1407
ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, ERR_INVALID_PARAMETER);
1408
1409
if (bst->compressed_track >= 0) {
1410
Vector3i key;
1411
double time;
1412
bool fetch_success = _fetch_compressed_by_index<1>(bst->compressed_track, p_key, key, time);
1413
if (!fetch_success) {
1414
return ERR_INVALID_PARAMETER;
1415
}
1416
1417
*r_blend_shape = _uncompress_blend_shape(key);
1418
return OK;
1419
}
1420
1421
ERR_FAIL_INDEX_V(p_key, bst->blend_shapes.size(), ERR_INVALID_PARAMETER);
1422
1423
*r_blend_shape = bst->blend_shapes[p_key].value;
1424
1425
return OK;
1426
}
1427
1428
Error Animation::try_blend_shape_track_interpolate(int p_track, double p_time, float *r_interpolation, bool p_backward) const {
1429
ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
1430
Track *t = tracks[p_track];
1431
ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, ERR_INVALID_PARAMETER);
1432
1433
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
1434
1435
if (bst->compressed_track >= 0) {
1436
if (_blend_shape_interpolate_compressed(bst->compressed_track, p_time, *r_interpolation)) {
1437
return OK;
1438
} else {
1439
return ERR_UNAVAILABLE;
1440
}
1441
}
1442
1443
bool ok = false;
1444
1445
float tk = _interpolate(bst->blend_shapes, p_time, bst->interpolation, bst->loop_wrap, &ok, p_backward);
1446
1447
if (!ok) {
1448
return ERR_UNAVAILABLE;
1449
}
1450
*r_interpolation = tk;
1451
return OK;
1452
}
1453
1454
float Animation::blend_shape_track_interpolate(int p_track, double p_time, bool p_backward) const {
1455
float ret = 0;
1456
ERR_FAIL_INDEX_V(p_track, tracks.size(), ret);
1457
bool err = try_blend_shape_track_interpolate(p_track, p_time, &ret, p_backward);
1458
ERR_FAIL_COND_V_MSG(err, ret, "Blend Shape Track: '" + String(tracks[p_track]->path) + "' is unavailable.");
1459
return ret;
1460
}
1461
1462
////
1463
1464
void Animation::track_remove_key_at_time(int p_track, double p_time) {
1465
int idx = track_find_key(p_track, p_time, FIND_MODE_APPROX);
1466
ERR_FAIL_COND(idx < 0);
1467
track_remove_key(p_track, idx);
1468
}
1469
1470
void Animation::track_remove_key(int p_track, int p_idx) {
1471
ERR_FAIL_INDEX(p_track, tracks.size());
1472
Track *t = tracks[p_track];
1473
1474
switch (t->type) {
1475
case TYPE_POSITION_3D: {
1476
PositionTrack *tt = static_cast<PositionTrack *>(t);
1477
1478
ERR_FAIL_COND(tt->compressed_track >= 0);
1479
1480
ERR_FAIL_INDEX(p_idx, tt->positions.size());
1481
tt->positions.remove_at(p_idx);
1482
1483
} break;
1484
case TYPE_ROTATION_3D: {
1485
RotationTrack *rt = static_cast<RotationTrack *>(t);
1486
1487
ERR_FAIL_COND(rt->compressed_track >= 0);
1488
1489
ERR_FAIL_INDEX(p_idx, rt->rotations.size());
1490
rt->rotations.remove_at(p_idx);
1491
1492
} break;
1493
case TYPE_SCALE_3D: {
1494
ScaleTrack *st = static_cast<ScaleTrack *>(t);
1495
1496
ERR_FAIL_COND(st->compressed_track >= 0);
1497
1498
ERR_FAIL_INDEX(p_idx, st->scales.size());
1499
st->scales.remove_at(p_idx);
1500
1501
} break;
1502
case TYPE_BLEND_SHAPE: {
1503
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
1504
1505
ERR_FAIL_COND(bst->compressed_track >= 0);
1506
1507
ERR_FAIL_INDEX(p_idx, bst->blend_shapes.size());
1508
bst->blend_shapes.remove_at(p_idx);
1509
1510
} break;
1511
case TYPE_VALUE: {
1512
ValueTrack *vt = static_cast<ValueTrack *>(t);
1513
ERR_FAIL_INDEX(p_idx, vt->values.size());
1514
vt->values.remove_at(p_idx);
1515
1516
} break;
1517
case TYPE_METHOD: {
1518
MethodTrack *mt = static_cast<MethodTrack *>(t);
1519
ERR_FAIL_INDEX(p_idx, mt->methods.size());
1520
mt->methods.remove_at(p_idx);
1521
1522
} break;
1523
case TYPE_BEZIER: {
1524
BezierTrack *bz = static_cast<BezierTrack *>(t);
1525
ERR_FAIL_INDEX(p_idx, bz->values.size());
1526
bz->values.remove_at(p_idx);
1527
1528
} break;
1529
case TYPE_AUDIO: {
1530
AudioTrack *ad = static_cast<AudioTrack *>(t);
1531
ERR_FAIL_INDEX(p_idx, ad->values.size());
1532
ad->values.remove_at(p_idx);
1533
1534
} break;
1535
case TYPE_ANIMATION: {
1536
AnimationTrack *an = static_cast<AnimationTrack *>(t);
1537
ERR_FAIL_INDEX(p_idx, an->values.size());
1538
an->values.remove_at(p_idx);
1539
1540
} break;
1541
}
1542
1543
emit_changed();
1544
}
1545
1546
int Animation::track_find_key(int p_track, double p_time, FindMode p_find_mode, bool p_limit, bool p_backward) const {
1547
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1548
Track *t = tracks[p_track];
1549
1550
switch (t->type) {
1551
case TYPE_POSITION_3D: {
1552
PositionTrack *tt = static_cast<PositionTrack *>(t);
1553
1554
if (tt->compressed_track >= 0) {
1555
double time;
1556
double time_next;
1557
Vector3i key;
1558
Vector3i key_next;
1559
uint32_t key_index;
1560
bool fetch_compressed_success = _fetch_compressed<3>(tt->compressed_track, p_time, key, time, key_next, time_next, &key_index);
1561
ERR_FAIL_COND_V(!fetch_compressed_success, -1);
1562
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
1563
return -1;
1564
}
1565
return key_index;
1566
}
1567
1568
int k = _find(tt->positions, p_time, p_backward, p_limit);
1569
if (k < 0 || k >= tt->positions.size()) {
1570
return -1;
1571
}
1572
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(tt->positions[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && tt->positions[k].time != p_time)) {
1573
return -1;
1574
}
1575
return k;
1576
1577
} break;
1578
case TYPE_ROTATION_3D: {
1579
RotationTrack *rt = static_cast<RotationTrack *>(t);
1580
1581
if (rt->compressed_track >= 0) {
1582
double time;
1583
double time_next;
1584
Vector3i key;
1585
Vector3i key_next;
1586
uint32_t key_index;
1587
bool fetch_compressed_success = _fetch_compressed<3>(rt->compressed_track, p_time, key, time, key_next, time_next, &key_index);
1588
ERR_FAIL_COND_V(!fetch_compressed_success, -1);
1589
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
1590
return -1;
1591
}
1592
return key_index;
1593
}
1594
1595
int k = _find(rt->rotations, p_time, p_backward, p_limit);
1596
if (k < 0 || k >= rt->rotations.size()) {
1597
return -1;
1598
}
1599
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(rt->rotations[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && rt->rotations[k].time != p_time)) {
1600
return -1;
1601
}
1602
return k;
1603
1604
} break;
1605
case TYPE_SCALE_3D: {
1606
ScaleTrack *st = static_cast<ScaleTrack *>(t);
1607
1608
if (st->compressed_track >= 0) {
1609
double time;
1610
double time_next;
1611
Vector3i key;
1612
Vector3i key_next;
1613
uint32_t key_index;
1614
bool fetch_compressed_success = _fetch_compressed<3>(st->compressed_track, p_time, key, time, key_next, time_next, &key_index);
1615
ERR_FAIL_COND_V(!fetch_compressed_success, -1);
1616
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
1617
return -1;
1618
}
1619
return key_index;
1620
}
1621
1622
int k = _find(st->scales, p_time, p_backward, p_limit);
1623
if (k < 0 || k >= st->scales.size()) {
1624
return -1;
1625
}
1626
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(st->scales[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && st->scales[k].time != p_time)) {
1627
return -1;
1628
}
1629
return k;
1630
1631
} break;
1632
case TYPE_BLEND_SHAPE: {
1633
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
1634
1635
if (bst->compressed_track >= 0) {
1636
double time;
1637
double time_next;
1638
Vector3i key;
1639
Vector3i key_next;
1640
uint32_t key_index;
1641
bool fetch_compressed_success = _fetch_compressed<1>(bst->compressed_track, p_time, key, time, key_next, time_next, &key_index);
1642
ERR_FAIL_COND_V(!fetch_compressed_success, -1);
1643
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(time, p_time)) || (p_find_mode == FIND_MODE_EXACT && time != p_time)) {
1644
return -1;
1645
}
1646
return key_index;
1647
}
1648
1649
int k = _find(bst->blend_shapes, p_time, p_backward, p_limit);
1650
if (k < 0 || k >= bst->blend_shapes.size()) {
1651
return -1;
1652
}
1653
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(bst->blend_shapes[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && bst->blend_shapes[k].time != p_time)) {
1654
return -1;
1655
}
1656
return k;
1657
1658
} break;
1659
case TYPE_VALUE: {
1660
ValueTrack *vt = static_cast<ValueTrack *>(t);
1661
int k = _find(vt->values, p_time, p_backward, p_limit);
1662
if (k < 0 || k >= vt->values.size()) {
1663
return -1;
1664
}
1665
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(vt->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && vt->values[k].time != p_time)) {
1666
return -1;
1667
}
1668
return k;
1669
1670
} break;
1671
case TYPE_METHOD: {
1672
MethodTrack *mt = static_cast<MethodTrack *>(t);
1673
int k = _find(mt->methods, p_time, p_backward, p_limit);
1674
if (k < 0 || k >= mt->methods.size()) {
1675
return -1;
1676
}
1677
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(mt->methods[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && mt->methods[k].time != p_time)) {
1678
return -1;
1679
}
1680
return k;
1681
1682
} break;
1683
case TYPE_BEZIER: {
1684
BezierTrack *bt = static_cast<BezierTrack *>(t);
1685
int k = _find(bt->values, p_time, p_backward, p_limit);
1686
if (k < 0 || k >= bt->values.size()) {
1687
return -1;
1688
}
1689
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(bt->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && bt->values[k].time != p_time)) {
1690
return -1;
1691
}
1692
return k;
1693
1694
} break;
1695
case TYPE_AUDIO: {
1696
AudioTrack *at = static_cast<AudioTrack *>(t);
1697
int k = _find(at->values, p_time, p_backward, p_limit);
1698
if (k < 0 || k >= at->values.size()) {
1699
return -1;
1700
}
1701
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(at->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && at->values[k].time != p_time)) {
1702
return -1;
1703
}
1704
return k;
1705
1706
} break;
1707
case TYPE_ANIMATION: {
1708
AnimationTrack *at = static_cast<AnimationTrack *>(t);
1709
int k = _find(at->values, p_time, p_backward, p_limit);
1710
if (k < 0 || k >= at->values.size()) {
1711
return -1;
1712
}
1713
if ((p_find_mode == FIND_MODE_APPROX && !Math::is_equal_approx(at->values[k].time, p_time)) || (p_find_mode == FIND_MODE_EXACT && at->values[k].time != p_time)) {
1714
return -1;
1715
}
1716
return k;
1717
1718
} break;
1719
}
1720
1721
return -1;
1722
}
1723
1724
int Animation::track_insert_key(int p_track, double p_time, const Variant &p_key, real_t p_transition) {
1725
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1726
Track *t = tracks[p_track];
1727
1728
int ret = -1;
1729
1730
switch (t->type) {
1731
case TYPE_POSITION_3D: {
1732
ERR_FAIL_COND_V((p_key.get_type() != Variant::VECTOR3) && (p_key.get_type() != Variant::VECTOR3I), -1);
1733
ret = position_track_insert_key(p_track, p_time, p_key);
1734
track_set_key_transition(p_track, ret, p_transition);
1735
1736
} break;
1737
case TYPE_ROTATION_3D: {
1738
ERR_FAIL_COND_V((p_key.get_type() != Variant::QUATERNION) && (p_key.get_type() != Variant::BASIS), -1);
1739
ret = rotation_track_insert_key(p_track, p_time, p_key);
1740
track_set_key_transition(p_track, ret, p_transition);
1741
1742
} break;
1743
case TYPE_SCALE_3D: {
1744
ERR_FAIL_COND_V((p_key.get_type() != Variant::VECTOR3) && (p_key.get_type() != Variant::VECTOR3I), -1);
1745
ret = scale_track_insert_key(p_track, p_time, p_key);
1746
track_set_key_transition(p_track, ret, p_transition);
1747
1748
} break;
1749
case TYPE_BLEND_SHAPE: {
1750
ERR_FAIL_COND_V((p_key.get_type() != Variant::FLOAT) && (p_key.get_type() != Variant::INT), -1);
1751
ret = blend_shape_track_insert_key(p_track, p_time, p_key);
1752
track_set_key_transition(p_track, ret, p_transition);
1753
1754
} break;
1755
case TYPE_VALUE: {
1756
ValueTrack *vt = static_cast<ValueTrack *>(t);
1757
1758
TKey<Variant> k;
1759
k.time = p_time;
1760
k.transition = p_transition;
1761
k.value = p_key;
1762
ret = _insert(p_time, vt->values, k);
1763
1764
} break;
1765
case TYPE_METHOD: {
1766
MethodTrack *mt = static_cast<MethodTrack *>(t);
1767
1768
ERR_FAIL_COND_V(p_key.get_type() != Variant::DICTIONARY, -1);
1769
1770
Dictionary d = p_key;
1771
ERR_FAIL_COND_V(!d.has("method") || !d["method"].is_string(), -1);
1772
ERR_FAIL_COND_V(!d.has("args") || !d["args"].is_array(), -1);
1773
1774
MethodKey k;
1775
1776
k.time = p_time;
1777
k.transition = p_transition;
1778
k.method = d["method"];
1779
k.params = d["args"];
1780
1781
ret = _insert(p_time, mt->methods, k);
1782
1783
} break;
1784
case TYPE_BEZIER: {
1785
BezierTrack *bt = static_cast<BezierTrack *>(t);
1786
1787
Array arr = p_key;
1788
ERR_FAIL_COND_V(arr.size() != 5, -1);
1789
1790
TKey<BezierKey> k;
1791
k.time = p_time;
1792
k.value.value = arr[0];
1793
k.value.in_handle.x = arr[1];
1794
k.value.in_handle.y = arr[2];
1795
k.value.out_handle.x = arr[3];
1796
k.value.out_handle.y = arr[4];
1797
ret = _insert(p_time, bt->values, k);
1798
1799
Vector<int> key_neighborhood;
1800
key_neighborhood.push_back(ret);
1801
if (ret > 0) {
1802
key_neighborhood.push_back(ret - 1);
1803
}
1804
if (ret < track_get_key_count(p_track) - 1) {
1805
key_neighborhood.push_back(ret + 1);
1806
}
1807
} break;
1808
case TYPE_AUDIO: {
1809
AudioTrack *at = static_cast<AudioTrack *>(t);
1810
1811
Dictionary k = p_key;
1812
ERR_FAIL_COND_V(!k.has("start_offset"), -1);
1813
ERR_FAIL_COND_V(!k.has("end_offset"), -1);
1814
ERR_FAIL_COND_V(!k.has("stream"), -1);
1815
1816
TKey<AudioKey> ak;
1817
ak.time = p_time;
1818
ak.value.start_offset = k["start_offset"];
1819
ak.value.end_offset = k["end_offset"];
1820
ak.value.stream = k["stream"];
1821
ret = _insert(p_time, at->values, ak);
1822
1823
} break;
1824
case TYPE_ANIMATION: {
1825
AnimationTrack *at = static_cast<AnimationTrack *>(t);
1826
1827
TKey<StringName> ak;
1828
ak.time = p_time;
1829
ak.value = p_key;
1830
1831
ret = _insert(p_time, at->values, ak);
1832
1833
} break;
1834
}
1835
1836
emit_changed();
1837
1838
return ret;
1839
}
1840
1841
int Animation::track_get_key_count(int p_track) const {
1842
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1843
Track *t = tracks[p_track];
1844
1845
switch (t->type) {
1846
case TYPE_POSITION_3D: {
1847
PositionTrack *tt = static_cast<PositionTrack *>(t);
1848
if (tt->compressed_track >= 0) {
1849
return _get_compressed_key_count(tt->compressed_track);
1850
}
1851
return tt->positions.size();
1852
} break;
1853
case TYPE_ROTATION_3D: {
1854
RotationTrack *rt = static_cast<RotationTrack *>(t);
1855
if (rt->compressed_track >= 0) {
1856
return _get_compressed_key_count(rt->compressed_track);
1857
}
1858
return rt->rotations.size();
1859
} break;
1860
case TYPE_SCALE_3D: {
1861
ScaleTrack *st = static_cast<ScaleTrack *>(t);
1862
if (st->compressed_track >= 0) {
1863
return _get_compressed_key_count(st->compressed_track);
1864
}
1865
return st->scales.size();
1866
} break;
1867
case TYPE_BLEND_SHAPE: {
1868
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
1869
if (bst->compressed_track >= 0) {
1870
return _get_compressed_key_count(bst->compressed_track);
1871
}
1872
return bst->blend_shapes.size();
1873
} break;
1874
case TYPE_VALUE: {
1875
ValueTrack *vt = static_cast<ValueTrack *>(t);
1876
return vt->values.size();
1877
1878
} break;
1879
case TYPE_METHOD: {
1880
MethodTrack *mt = static_cast<MethodTrack *>(t);
1881
return mt->methods.size();
1882
} break;
1883
case TYPE_BEZIER: {
1884
BezierTrack *bt = static_cast<BezierTrack *>(t);
1885
return bt->values.size();
1886
} break;
1887
case TYPE_AUDIO: {
1888
AudioTrack *at = static_cast<AudioTrack *>(t);
1889
return at->values.size();
1890
} break;
1891
case TYPE_ANIMATION: {
1892
AnimationTrack *at = static_cast<AnimationTrack *>(t);
1893
return at->values.size();
1894
} break;
1895
}
1896
1897
ERR_FAIL_V(-1);
1898
}
1899
1900
Variant Animation::track_get_key_value(int p_track, int p_key_idx) const {
1901
ERR_FAIL_INDEX_V(p_track, tracks.size(), Variant());
1902
Track *t = tracks[p_track];
1903
1904
switch (t->type) {
1905
case TYPE_POSITION_3D: {
1906
Vector3 value;
1907
position_track_get_key(p_track, p_key_idx, &value);
1908
return value;
1909
} break;
1910
case TYPE_ROTATION_3D: {
1911
Quaternion value;
1912
rotation_track_get_key(p_track, p_key_idx, &value);
1913
return value;
1914
} break;
1915
case TYPE_SCALE_3D: {
1916
Vector3 value;
1917
scale_track_get_key(p_track, p_key_idx, &value);
1918
return value;
1919
} break;
1920
case TYPE_BLEND_SHAPE: {
1921
float value;
1922
blend_shape_track_get_key(p_track, p_key_idx, &value);
1923
return value;
1924
} break;
1925
case TYPE_VALUE: {
1926
ValueTrack *vt = static_cast<ValueTrack *>(t);
1927
ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), Variant());
1928
return vt->values[p_key_idx].value;
1929
1930
} break;
1931
case TYPE_METHOD: {
1932
MethodTrack *mt = static_cast<MethodTrack *>(t);
1933
ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), Variant());
1934
Dictionary d;
1935
d["method"] = mt->methods[p_key_idx].method;
1936
d["args"] = mt->methods[p_key_idx].params;
1937
return d;
1938
1939
} break;
1940
case TYPE_BEZIER: {
1941
BezierTrack *bt = static_cast<BezierTrack *>(t);
1942
ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), Variant());
1943
1944
Array arr;
1945
arr.resize(5);
1946
arr[0] = bt->values[p_key_idx].value.value;
1947
arr[1] = bt->values[p_key_idx].value.in_handle.x;
1948
arr[2] = bt->values[p_key_idx].value.in_handle.y;
1949
arr[3] = bt->values[p_key_idx].value.out_handle.x;
1950
arr[4] = bt->values[p_key_idx].value.out_handle.y;
1951
return arr;
1952
1953
} break;
1954
case TYPE_AUDIO: {
1955
AudioTrack *at = static_cast<AudioTrack *>(t);
1956
ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
1957
1958
Dictionary k;
1959
k["start_offset"] = at->values[p_key_idx].value.start_offset;
1960
k["end_offset"] = at->values[p_key_idx].value.end_offset;
1961
k["stream"] = at->values[p_key_idx].value.stream;
1962
return k;
1963
1964
} break;
1965
case TYPE_ANIMATION: {
1966
AnimationTrack *at = static_cast<AnimationTrack *>(t);
1967
ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
1968
1969
return at->values[p_key_idx].value;
1970
1971
} break;
1972
}
1973
1974
ERR_FAIL_V(Variant());
1975
}
1976
1977
double Animation::track_get_key_time(int p_track, int p_key_idx) const {
1978
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
1979
Track *t = tracks[p_track];
1980
1981
switch (t->type) {
1982
case TYPE_POSITION_3D: {
1983
PositionTrack *tt = static_cast<PositionTrack *>(t);
1984
if (tt->compressed_track >= 0) {
1985
Vector3i value;
1986
double time;
1987
bool fetch_compressed_success = _fetch_compressed_by_index<3>(tt->compressed_track, p_key_idx, value, time);
1988
ERR_FAIL_COND_V(!fetch_compressed_success, false);
1989
return time;
1990
}
1991
ERR_FAIL_INDEX_V(p_key_idx, tt->positions.size(), -1);
1992
return tt->positions[p_key_idx].time;
1993
} break;
1994
case TYPE_ROTATION_3D: {
1995
RotationTrack *rt = static_cast<RotationTrack *>(t);
1996
if (rt->compressed_track >= 0) {
1997
Vector3i value;
1998
double time;
1999
bool fetch_compressed_success = _fetch_compressed_by_index<3>(rt->compressed_track, p_key_idx, value, time);
2000
ERR_FAIL_COND_V(!fetch_compressed_success, false);
2001
return time;
2002
}
2003
ERR_FAIL_INDEX_V(p_key_idx, rt->rotations.size(), -1);
2004
return rt->rotations[p_key_idx].time;
2005
} break;
2006
case TYPE_SCALE_3D: {
2007
ScaleTrack *st = static_cast<ScaleTrack *>(t);
2008
if (st->compressed_track >= 0) {
2009
Vector3i value;
2010
double time;
2011
bool fetch_compressed_success = _fetch_compressed_by_index<3>(st->compressed_track, p_key_idx, value, time);
2012
ERR_FAIL_COND_V(!fetch_compressed_success, false);
2013
return time;
2014
}
2015
ERR_FAIL_INDEX_V(p_key_idx, st->scales.size(), -1);
2016
return st->scales[p_key_idx].time;
2017
} break;
2018
case TYPE_BLEND_SHAPE: {
2019
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
2020
if (bst->compressed_track >= 0) {
2021
Vector3i value;
2022
double time;
2023
bool fetch_compressed_success = _fetch_compressed_by_index<1>(bst->compressed_track, p_key_idx, value, time);
2024
ERR_FAIL_COND_V(!fetch_compressed_success, false);
2025
return time;
2026
}
2027
ERR_FAIL_INDEX_V(p_key_idx, bst->blend_shapes.size(), -1);
2028
return bst->blend_shapes[p_key_idx].time;
2029
} break;
2030
case TYPE_VALUE: {
2031
ValueTrack *vt = static_cast<ValueTrack *>(t);
2032
ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
2033
return vt->values[p_key_idx].time;
2034
2035
} break;
2036
case TYPE_METHOD: {
2037
MethodTrack *mt = static_cast<MethodTrack *>(t);
2038
ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
2039
return mt->methods[p_key_idx].time;
2040
2041
} break;
2042
case TYPE_BEZIER: {
2043
BezierTrack *bt = static_cast<BezierTrack *>(t);
2044
ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), -1);
2045
return bt->values[p_key_idx].time;
2046
2047
} break;
2048
case TYPE_AUDIO: {
2049
AudioTrack *at = static_cast<AudioTrack *>(t);
2050
ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
2051
return at->values[p_key_idx].time;
2052
2053
} break;
2054
case TYPE_ANIMATION: {
2055
AnimationTrack *at = static_cast<AnimationTrack *>(t);
2056
ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
2057
return at->values[p_key_idx].time;
2058
2059
} break;
2060
}
2061
2062
ERR_FAIL_V(-1);
2063
}
2064
2065
void Animation::track_set_key_time(int p_track, int p_key_idx, double p_time) {
2066
ERR_FAIL_INDEX(p_track, tracks.size());
2067
Track *t = tracks[p_track];
2068
2069
switch (t->type) {
2070
case TYPE_POSITION_3D: {
2071
PositionTrack *tt = static_cast<PositionTrack *>(t);
2072
ERR_FAIL_COND(tt->compressed_track >= 0);
2073
ERR_FAIL_INDEX(p_key_idx, tt->positions.size());
2074
TKey<Vector3> key = tt->positions[p_key_idx];
2075
key.time = p_time;
2076
tt->positions.remove_at(p_key_idx);
2077
_insert(p_time, tt->positions, key);
2078
return;
2079
}
2080
case TYPE_ROTATION_3D: {
2081
RotationTrack *tt = static_cast<RotationTrack *>(t);
2082
ERR_FAIL_COND(tt->compressed_track >= 0);
2083
ERR_FAIL_INDEX(p_key_idx, tt->rotations.size());
2084
TKey<Quaternion> key = tt->rotations[p_key_idx];
2085
key.time = p_time;
2086
tt->rotations.remove_at(p_key_idx);
2087
_insert(p_time, tt->rotations, key);
2088
return;
2089
}
2090
case TYPE_SCALE_3D: {
2091
ScaleTrack *tt = static_cast<ScaleTrack *>(t);
2092
ERR_FAIL_COND(tt->compressed_track >= 0);
2093
ERR_FAIL_INDEX(p_key_idx, tt->scales.size());
2094
TKey<Vector3> key = tt->scales[p_key_idx];
2095
key.time = p_time;
2096
tt->scales.remove_at(p_key_idx);
2097
_insert(p_time, tt->scales, key);
2098
return;
2099
}
2100
case TYPE_BLEND_SHAPE: {
2101
BlendShapeTrack *tt = static_cast<BlendShapeTrack *>(t);
2102
ERR_FAIL_COND(tt->compressed_track >= 0);
2103
ERR_FAIL_INDEX(p_key_idx, tt->blend_shapes.size());
2104
TKey<float> key = tt->blend_shapes[p_key_idx];
2105
key.time = p_time;
2106
tt->blend_shapes.remove_at(p_key_idx);
2107
_insert(p_time, tt->blend_shapes, key);
2108
return;
2109
}
2110
case TYPE_VALUE: {
2111
ValueTrack *vt = static_cast<ValueTrack *>(t);
2112
ERR_FAIL_INDEX(p_key_idx, vt->values.size());
2113
TKey<Variant> key = vt->values[p_key_idx];
2114
key.time = p_time;
2115
vt->values.remove_at(p_key_idx);
2116
_insert(p_time, vt->values, key);
2117
return;
2118
}
2119
case TYPE_METHOD: {
2120
MethodTrack *mt = static_cast<MethodTrack *>(t);
2121
ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
2122
MethodKey key = mt->methods[p_key_idx];
2123
key.time = p_time;
2124
mt->methods.remove_at(p_key_idx);
2125
_insert(p_time, mt->methods, key);
2126
return;
2127
}
2128
case TYPE_BEZIER: {
2129
BezierTrack *bt = static_cast<BezierTrack *>(t);
2130
ERR_FAIL_INDEX(p_key_idx, bt->values.size());
2131
TKey<BezierKey> key = bt->values[p_key_idx];
2132
key.time = p_time;
2133
bt->values.remove_at(p_key_idx);
2134
_insert(p_time, bt->values, key);
2135
return;
2136
}
2137
case TYPE_AUDIO: {
2138
AudioTrack *at = static_cast<AudioTrack *>(t);
2139
ERR_FAIL_INDEX(p_key_idx, at->values.size());
2140
TKey<AudioKey> key = at->values[p_key_idx];
2141
key.time = p_time;
2142
at->values.remove_at(p_key_idx);
2143
_insert(p_time, at->values, key);
2144
return;
2145
}
2146
case TYPE_ANIMATION: {
2147
AnimationTrack *at = static_cast<AnimationTrack *>(t);
2148
ERR_FAIL_INDEX(p_key_idx, at->values.size());
2149
TKey<StringName> key = at->values[p_key_idx];
2150
key.time = p_time;
2151
at->values.remove_at(p_key_idx);
2152
_insert(p_time, at->values, key);
2153
return;
2154
}
2155
}
2156
2157
ERR_FAIL();
2158
}
2159
2160
real_t Animation::track_get_key_transition(int p_track, int p_key_idx) const {
2161
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
2162
Track *t = tracks[p_track];
2163
2164
switch (t->type) {
2165
case TYPE_POSITION_3D: {
2166
PositionTrack *tt = static_cast<PositionTrack *>(t);
2167
if (tt->compressed_track >= 0) {
2168
return 1.0;
2169
}
2170
ERR_FAIL_INDEX_V(p_key_idx, tt->positions.size(), -1);
2171
return tt->positions[p_key_idx].transition;
2172
} break;
2173
case TYPE_ROTATION_3D: {
2174
RotationTrack *rt = static_cast<RotationTrack *>(t);
2175
if (rt->compressed_track >= 0) {
2176
return 1.0;
2177
}
2178
ERR_FAIL_INDEX_V(p_key_idx, rt->rotations.size(), -1);
2179
return rt->rotations[p_key_idx].transition;
2180
} break;
2181
case TYPE_SCALE_3D: {
2182
ScaleTrack *st = static_cast<ScaleTrack *>(t);
2183
if (st->compressed_track >= 0) {
2184
return 1.0;
2185
}
2186
ERR_FAIL_INDEX_V(p_key_idx, st->scales.size(), -1);
2187
return st->scales[p_key_idx].transition;
2188
} break;
2189
case TYPE_BLEND_SHAPE: {
2190
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
2191
if (bst->compressed_track >= 0) {
2192
return 1.0;
2193
}
2194
ERR_FAIL_INDEX_V(p_key_idx, bst->blend_shapes.size(), -1);
2195
return bst->blend_shapes[p_key_idx].transition;
2196
} break;
2197
case TYPE_VALUE: {
2198
ValueTrack *vt = static_cast<ValueTrack *>(t);
2199
ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
2200
return vt->values[p_key_idx].transition;
2201
2202
} break;
2203
case TYPE_METHOD: {
2204
MethodTrack *mt = static_cast<MethodTrack *>(t);
2205
ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
2206
return mt->methods[p_key_idx].transition;
2207
2208
} break;
2209
case TYPE_BEZIER: {
2210
return 1; //bezier does not really use transitions
2211
} break;
2212
case TYPE_AUDIO: {
2213
return 1; //audio does not really use transitions
2214
} break;
2215
case TYPE_ANIMATION: {
2216
return 1; //animation does not really use transitions
2217
} break;
2218
}
2219
2220
ERR_FAIL_V(0);
2221
}
2222
2223
bool Animation::track_is_compressed(int p_track) const {
2224
ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
2225
Track *t = tracks[p_track];
2226
2227
switch (t->type) {
2228
case TYPE_POSITION_3D: {
2229
PositionTrack *tt = static_cast<PositionTrack *>(t);
2230
return tt->compressed_track >= 0;
2231
} break;
2232
case TYPE_ROTATION_3D: {
2233
RotationTrack *rt = static_cast<RotationTrack *>(t);
2234
return rt->compressed_track >= 0;
2235
} break;
2236
case TYPE_SCALE_3D: {
2237
ScaleTrack *st = static_cast<ScaleTrack *>(t);
2238
return st->compressed_track >= 0;
2239
} break;
2240
case TYPE_BLEND_SHAPE: {
2241
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
2242
return bst->compressed_track >= 0;
2243
} break;
2244
default: {
2245
return false; // Animation does not really use transitions.
2246
} break;
2247
}
2248
}
2249
2250
void Animation::track_set_key_value(int p_track, int p_key_idx, const Variant &p_value) {
2251
ERR_FAIL_INDEX(p_track, tracks.size());
2252
Track *t = tracks[p_track];
2253
2254
switch (t->type) {
2255
case TYPE_POSITION_3D: {
2256
ERR_FAIL_COND((p_value.get_type() != Variant::VECTOR3) && (p_value.get_type() != Variant::VECTOR3I));
2257
PositionTrack *tt = static_cast<PositionTrack *>(t);
2258
ERR_FAIL_COND(tt->compressed_track >= 0);
2259
ERR_FAIL_INDEX(p_key_idx, tt->positions.size());
2260
2261
tt->positions.write[p_key_idx].value = p_value;
2262
2263
} break;
2264
case TYPE_ROTATION_3D: {
2265
ERR_FAIL_COND((p_value.get_type() != Variant::QUATERNION) && (p_value.get_type() != Variant::BASIS));
2266
RotationTrack *rt = static_cast<RotationTrack *>(t);
2267
ERR_FAIL_COND(rt->compressed_track >= 0);
2268
ERR_FAIL_INDEX(p_key_idx, rt->rotations.size());
2269
2270
rt->rotations.write[p_key_idx].value = p_value;
2271
2272
} break;
2273
case TYPE_SCALE_3D: {
2274
ERR_FAIL_COND((p_value.get_type() != Variant::VECTOR3) && (p_value.get_type() != Variant::VECTOR3I));
2275
ScaleTrack *st = static_cast<ScaleTrack *>(t);
2276
ERR_FAIL_COND(st->compressed_track >= 0);
2277
ERR_FAIL_INDEX(p_key_idx, st->scales.size());
2278
2279
st->scales.write[p_key_idx].value = p_value;
2280
2281
} break;
2282
case TYPE_BLEND_SHAPE: {
2283
ERR_FAIL_COND((p_value.get_type() != Variant::FLOAT) && (p_value.get_type() != Variant::INT));
2284
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
2285
ERR_FAIL_COND(bst->compressed_track >= 0);
2286
ERR_FAIL_INDEX(p_key_idx, bst->blend_shapes.size());
2287
2288
bst->blend_shapes.write[p_key_idx].value = p_value;
2289
2290
} break;
2291
case TYPE_VALUE: {
2292
ValueTrack *vt = static_cast<ValueTrack *>(t);
2293
ERR_FAIL_INDEX(p_key_idx, vt->values.size());
2294
2295
vt->values.write[p_key_idx].value = p_value;
2296
2297
} break;
2298
case TYPE_METHOD: {
2299
MethodTrack *mt = static_cast<MethodTrack *>(t);
2300
ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
2301
2302
Dictionary d = p_value;
2303
2304
if (d.has("method")) {
2305
mt->methods.write[p_key_idx].method = d["method"];
2306
}
2307
if (d.has("args")) {
2308
mt->methods.write[p_key_idx].params = d["args"];
2309
}
2310
2311
} break;
2312
case TYPE_BEZIER: {
2313
BezierTrack *bt = static_cast<BezierTrack *>(t);
2314
ERR_FAIL_INDEX(p_key_idx, bt->values.size());
2315
2316
Array arr = p_value;
2317
ERR_FAIL_COND(arr.size() != 5);
2318
2319
bt->values.write[p_key_idx].value.value = arr[0];
2320
bt->values.write[p_key_idx].value.in_handle.x = arr[1];
2321
bt->values.write[p_key_idx].value.in_handle.y = arr[2];
2322
bt->values.write[p_key_idx].value.out_handle.x = arr[3];
2323
bt->values.write[p_key_idx].value.out_handle.y = arr[4];
2324
2325
} break;
2326
case TYPE_AUDIO: {
2327
AudioTrack *at = static_cast<AudioTrack *>(t);
2328
ERR_FAIL_INDEX(p_key_idx, at->values.size());
2329
2330
Dictionary k = p_value;
2331
ERR_FAIL_COND(!k.has("start_offset"));
2332
ERR_FAIL_COND(!k.has("end_offset"));
2333
ERR_FAIL_COND(!k.has("stream"));
2334
2335
at->values.write[p_key_idx].value.start_offset = k["start_offset"];
2336
at->values.write[p_key_idx].value.end_offset = k["end_offset"];
2337
at->values.write[p_key_idx].value.stream = k["stream"];
2338
2339
} break;
2340
case TYPE_ANIMATION: {
2341
AnimationTrack *at = static_cast<AnimationTrack *>(t);
2342
ERR_FAIL_INDEX(p_key_idx, at->values.size());
2343
2344
at->values.write[p_key_idx].value = p_value;
2345
2346
} break;
2347
}
2348
2349
emit_changed();
2350
}
2351
2352
void Animation::track_set_key_transition(int p_track, int p_key_idx, real_t p_transition) {
2353
ERR_FAIL_INDEX(p_track, tracks.size());
2354
Track *t = tracks[p_track];
2355
2356
switch (t->type) {
2357
case TYPE_POSITION_3D: {
2358
PositionTrack *tt = static_cast<PositionTrack *>(t);
2359
ERR_FAIL_COND(tt->compressed_track >= 0);
2360
ERR_FAIL_INDEX(p_key_idx, tt->positions.size());
2361
tt->positions.write[p_key_idx].transition = p_transition;
2362
} break;
2363
case TYPE_ROTATION_3D: {
2364
RotationTrack *rt = static_cast<RotationTrack *>(t);
2365
ERR_FAIL_COND(rt->compressed_track >= 0);
2366
ERR_FAIL_INDEX(p_key_idx, rt->rotations.size());
2367
rt->rotations.write[p_key_idx].transition = p_transition;
2368
} break;
2369
case TYPE_SCALE_3D: {
2370
ScaleTrack *st = static_cast<ScaleTrack *>(t);
2371
ERR_FAIL_COND(st->compressed_track >= 0);
2372
ERR_FAIL_INDEX(p_key_idx, st->scales.size());
2373
st->scales.write[p_key_idx].transition = p_transition;
2374
} break;
2375
case TYPE_BLEND_SHAPE: {
2376
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
2377
ERR_FAIL_COND(bst->compressed_track >= 0);
2378
ERR_FAIL_INDEX(p_key_idx, bst->blend_shapes.size());
2379
bst->blend_shapes.write[p_key_idx].transition = p_transition;
2380
} break;
2381
case TYPE_VALUE: {
2382
ValueTrack *vt = static_cast<ValueTrack *>(t);
2383
ERR_FAIL_INDEX(p_key_idx, vt->values.size());
2384
vt->values.write[p_key_idx].transition = p_transition;
2385
2386
} break;
2387
case TYPE_METHOD: {
2388
MethodTrack *mt = static_cast<MethodTrack *>(t);
2389
ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
2390
mt->methods.write[p_key_idx].transition = p_transition;
2391
2392
} break;
2393
case TYPE_BEZIER:
2394
case TYPE_AUDIO:
2395
case TYPE_ANIMATION: {
2396
// they don't use transition
2397
} break;
2398
}
2399
2400
emit_changed();
2401
}
2402
2403
template <typename K>
2404
int Animation::_find(const Vector<K> &p_keys, double p_time, bool p_backward, bool p_limit) const {
2405
int len = p_keys.size();
2406
if (len == 0) {
2407
return -2;
2408
}
2409
2410
int low = 0;
2411
int high = len - 1;
2412
int middle = 0;
2413
2414
#ifdef DEBUG_ENABLED
2415
if (low > high) {
2416
ERR_PRINT("low > high, this may be a bug.");
2417
}
2418
#endif
2419
2420
const K *keys = &p_keys[0];
2421
2422
while (low <= high) {
2423
middle = (low + high) / 2;
2424
2425
if (Math::is_equal_approx(p_time, (double)keys[middle].time)) { //match
2426
return middle;
2427
} else if (p_time < keys[middle].time) {
2428
high = middle - 1; //search low end of array
2429
} else {
2430
low = middle + 1; //search high end of array
2431
}
2432
}
2433
2434
if (!p_backward) {
2435
if (keys[middle].time > p_time) {
2436
middle--;
2437
}
2438
} else {
2439
if (keys[middle].time < p_time) {
2440
middle++;
2441
}
2442
}
2443
2444
if (p_limit && middle > -1 && middle < len) {
2445
double diff = length - keys[middle].time;
2446
if ((std::signbit(keys[middle].time) && !Math::is_zero_approx(keys[middle].time)) || (std::signbit(diff) && !Math::is_zero_approx(diff))) {
2447
ERR_PRINT_ONCE_ED("Found the key outside the animation range. Consider using the clean-up option in AnimationTrackEditor to fix it.");
2448
return -1;
2449
}
2450
}
2451
2452
return middle;
2453
}
2454
2455
// Linear interpolation for anytype.
2456
2457
Vector3 Animation::_interpolate(const Vector3 &p_a, const Vector3 &p_b, real_t p_c) const {
2458
return p_a.lerp(p_b, p_c);
2459
}
2460
2461
Quaternion Animation::_interpolate(const Quaternion &p_a, const Quaternion &p_b, real_t p_c) const {
2462
return p_a.slerp(p_b, p_c);
2463
}
2464
2465
Variant Animation::_interpolate(const Variant &p_a, const Variant &p_b, real_t p_c) const {
2466
return interpolate_variant(p_a, p_b, p_c);
2467
}
2468
2469
real_t Animation::_interpolate(const real_t &p_a, const real_t &p_b, real_t p_c) const {
2470
return Math::lerp(p_a, p_b, p_c);
2471
}
2472
2473
Variant Animation::_interpolate_angle(const Variant &p_a, const Variant &p_b, real_t p_c) const {
2474
Variant::Type type_a = p_a.get_type();
2475
Variant::Type type_b = p_b.get_type();
2476
uint32_t vformat = 1 << type_a;
2477
vformat |= 1 << type_b;
2478
if (vformat == ((1 << Variant::INT) | (1 << Variant::FLOAT)) || vformat == (1 << Variant::FLOAT)) {
2479
real_t a = p_a;
2480
real_t b = p_b;
2481
return Math::fposmod((float)Math::lerp_angle(a, b, p_c), (float)Math::TAU);
2482
}
2483
return _interpolate(p_a, p_b, p_c);
2484
}
2485
2486
// Cubic interpolation for anytype.
2487
2488
Vector3 Animation::_cubic_interpolate_in_time(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
2489
return p_a.cubic_interpolate_in_time(p_b, p_pre_a, p_post_b, p_c, p_b_t, p_pre_a_t, p_post_b_t);
2490
}
2491
2492
Quaternion Animation::_cubic_interpolate_in_time(const Quaternion &p_pre_a, const Quaternion &p_a, const Quaternion &p_b, const Quaternion &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
2493
return p_a.spherical_cubic_interpolate_in_time(p_b, p_pre_a, p_post_b, p_c, p_b_t, p_pre_a_t, p_post_b_t);
2494
}
2495
2496
Variant Animation::_cubic_interpolate_in_time(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
2497
return cubic_interpolate_in_time_variant(p_pre_a, p_a, p_b, p_post_b, p_c, p_pre_a_t, p_b_t, p_post_b_t);
2498
}
2499
2500
real_t Animation::_cubic_interpolate_in_time(const real_t &p_pre_a, const real_t &p_a, const real_t &p_b, const real_t &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
2501
return Math::cubic_interpolate_in_time(p_a, p_b, p_pre_a, p_post_b, p_c, p_b_t, p_pre_a_t, p_post_b_t);
2502
}
2503
2504
Variant Animation::_cubic_interpolate_angle_in_time(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, real_t p_c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t) const {
2505
Variant::Type type_a = p_a.get_type();
2506
Variant::Type type_b = p_b.get_type();
2507
Variant::Type type_pa = p_pre_a.get_type();
2508
Variant::Type type_pb = p_post_b.get_type();
2509
uint32_t vformat = 1 << type_a;
2510
vformat |= 1 << type_b;
2511
vformat |= 1 << type_pa;
2512
vformat |= 1 << type_pb;
2513
if (vformat == ((1 << Variant::INT) | (1 << Variant::FLOAT)) || vformat == (1 << Variant::FLOAT)) {
2514
real_t a = p_a;
2515
real_t b = p_b;
2516
real_t pa = p_pre_a;
2517
real_t pb = p_post_b;
2518
return Math::fposmod((float)Math::cubic_interpolate_angle_in_time(a, b, pa, pb, p_c, p_b_t, p_pre_a_t, p_post_b_t), (float)Math::TAU);
2519
}
2520
return _cubic_interpolate_in_time(p_pre_a, p_a, p_b, p_post_b, p_c, p_pre_a_t, p_b_t, p_post_b_t);
2521
}
2522
2523
template <typename T>
2524
T Animation::_interpolate(const Vector<TKey<T>> &p_keys, double p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok, bool p_backward) const {
2525
int len = _find(p_keys, length) + 1; // try to find last key (there may be more past the end)
2526
2527
if (len <= 0) {
2528
// (-1 or -2 returned originally) (plus one above)
2529
// meaning no keys, or only key time is larger than length
2530
if (p_ok) {
2531
*p_ok = false;
2532
}
2533
return T();
2534
} else if (len == 1) { // one key found (0+1), return it
2535
2536
if (p_ok) {
2537
*p_ok = true;
2538
}
2539
return p_keys[0].value;
2540
}
2541
2542
int idx = _find(p_keys, p_time, p_backward);
2543
2544
ERR_FAIL_COND_V(idx == -2, T());
2545
int maxi = len - 1;
2546
bool is_start_edge = p_backward ? idx >= len : idx == -1;
2547
bool is_end_edge = p_backward ? idx == 0 : idx >= maxi;
2548
2549
real_t c = 0.0;
2550
// Prepare for all cases of interpolation.
2551
real_t delta = 0.0;
2552
real_t from = 0.0;
2553
2554
int pre = -1;
2555
int next = -1;
2556
int post = -1;
2557
real_t pre_t = 0.0;
2558
real_t to_t = 0.0;
2559
real_t post_t = 0.0;
2560
2561
bool use_cubic = p_interp == INTERPOLATION_CUBIC || p_interp == INTERPOLATION_CUBIC_ANGLE;
2562
2563
if (!p_loop_wrap || loop_mode == LOOP_NONE) {
2564
if (is_start_edge) {
2565
idx = p_backward ? maxi : 0;
2566
}
2567
next = CLAMP(idx + (p_backward ? -1 : 1), 0, maxi);
2568
if (use_cubic) {
2569
pre = CLAMP(idx + (p_backward ? 1 : -1), 0, maxi);
2570
post = CLAMP(idx + (p_backward ? -2 : 2), 0, maxi);
2571
}
2572
} else if (loop_mode == LOOP_LINEAR) {
2573
if (is_start_edge) {
2574
idx = p_backward ? 0 : maxi;
2575
}
2576
next = Math::posmod(idx + (p_backward ? -1 : 1), len);
2577
if (use_cubic) {
2578
pre = Math::posmod(idx + (p_backward ? 1 : -1), len);
2579
post = Math::posmod(idx + (p_backward ? -2 : 2), len);
2580
}
2581
if (is_start_edge) {
2582
if (!p_backward) {
2583
real_t endtime = (length - p_keys[idx].time);
2584
if (endtime < 0) { // may be keys past the end
2585
endtime = 0;
2586
}
2587
delta = endtime + p_keys[next].time;
2588
from = endtime + p_time;
2589
} else {
2590
real_t endtime = p_keys[idx].time;
2591
if (endtime > length) { // may be keys past the end
2592
endtime = length;
2593
}
2594
delta = endtime + length - p_keys[next].time;
2595
from = endtime + length - p_time;
2596
}
2597
} else if (is_end_edge) {
2598
if (!p_backward) {
2599
delta = (length - p_keys[idx].time) + p_keys[next].time;
2600
from = p_time - p_keys[idx].time;
2601
} else {
2602
delta = p_keys[idx].time + (length - p_keys[next].time);
2603
from = (length - p_time) - (length - p_keys[idx].time);
2604
}
2605
}
2606
} else {
2607
if (is_start_edge) {
2608
idx = p_backward ? len : -1;
2609
}
2610
next = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? -1 : 1)) + 0.5f, (float)len) - 0.5f);
2611
if (use_cubic) {
2612
pre = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? 1 : -1)) + 0.5f, (float)len) - 0.5f);
2613
post = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? -2 : 2)) + 0.5f, (float)len) - 0.5f);
2614
}
2615
idx = (int)Math::round(Math::pingpong((float)idx + 0.5f, (float)len) - 0.5f);
2616
if (is_start_edge) {
2617
if (!p_backward) {
2618
real_t endtime = p_keys[idx].time;
2619
if (endtime < 0) { // may be keys past the end
2620
endtime = 0;
2621
}
2622
delta = endtime + p_keys[next].time;
2623
from = endtime + p_time;
2624
} else {
2625
real_t endtime = length - p_keys[idx].time;
2626
if (endtime > length) { // may be keys past the end
2627
endtime = length;
2628
}
2629
delta = endtime + length - p_keys[next].time;
2630
from = endtime + length - p_time;
2631
}
2632
} else if (is_end_edge) {
2633
if (!p_backward) {
2634
delta = length * 2.0 - p_keys[idx].time - p_keys[next].time;
2635
from = p_time - p_keys[idx].time;
2636
} else {
2637
delta = p_keys[idx].time + p_keys[next].time;
2638
from = (length - p_time) - (length - p_keys[idx].time);
2639
}
2640
}
2641
}
2642
2643
if (!is_start_edge && !is_end_edge) {
2644
if (!p_backward) {
2645
delta = p_keys[next].time - p_keys[idx].time;
2646
from = p_time - p_keys[idx].time;
2647
} else {
2648
delta = (length - p_keys[next].time) - (length - p_keys[idx].time);
2649
from = (length - p_time) - (length - p_keys[idx].time);
2650
}
2651
}
2652
2653
if (Math::is_zero_approx(delta)) {
2654
c = 0;
2655
} else {
2656
c = from / delta;
2657
}
2658
2659
if (p_ok) {
2660
*p_ok = true;
2661
}
2662
2663
real_t tr = p_keys[idx].transition;
2664
if (tr == 0) {
2665
// Don't interpolate if not needed.
2666
return p_keys[idx].value;
2667
}
2668
2669
if (tr != 1.0) {
2670
c = Math::ease(c, tr);
2671
}
2672
2673
switch (p_interp) {
2674
case INTERPOLATION_NEAREST: {
2675
return p_keys[idx].value;
2676
} break;
2677
case INTERPOLATION_LINEAR: {
2678
return _interpolate(p_keys[idx].value, p_keys[next].value, c);
2679
} break;
2680
case INTERPOLATION_LINEAR_ANGLE: {
2681
return _interpolate_angle(p_keys[idx].value, p_keys[next].value, c);
2682
} break;
2683
case INTERPOLATION_CUBIC:
2684
case INTERPOLATION_CUBIC_ANGLE: {
2685
if (!p_loop_wrap || loop_mode == LOOP_NONE) {
2686
pre_t = p_keys[pre].time - p_keys[idx].time;
2687
to_t = p_keys[next].time - p_keys[idx].time;
2688
post_t = p_keys[post].time - p_keys[idx].time;
2689
} else if (loop_mode == LOOP_LINEAR) {
2690
pre_t = pre > idx ? -length + p_keys[pre].time - p_keys[idx].time : p_keys[pre].time - p_keys[idx].time;
2691
to_t = next < idx ? length + p_keys[next].time - p_keys[idx].time : p_keys[next].time - p_keys[idx].time;
2692
post_t = next < idx || post <= idx ? length + p_keys[post].time - p_keys[idx].time : p_keys[post].time - p_keys[idx].time;
2693
} else {
2694
pre_t = p_keys[pre].time - p_keys[idx].time;
2695
to_t = p_keys[next].time - p_keys[idx].time;
2696
post_t = p_keys[post].time - p_keys[idx].time;
2697
2698
if ((pre > idx && idx == next && post < next) || (pre < idx && idx == next && post > next)) {
2699
pre_t = p_keys[idx].time - p_keys[pre].time;
2700
} else if (pre == idx) {
2701
pre_t = idx < next ? -p_keys[idx].time * 2.0 : (length - p_keys[idx].time) * 2.0;
2702
}
2703
2704
if (idx == next) {
2705
to_t = pre < idx ? (length - p_keys[idx].time) * 2.0 : -p_keys[idx].time * 2.0;
2706
post_t = p_keys[next].time - p_keys[post].time + to_t;
2707
} else if (next == post) {
2708
post_t = idx < next ? (length - p_keys[next].time) * 2.0 + to_t : -p_keys[next].time * 2.0 + to_t;
2709
}
2710
}
2711
2712
if (p_interp == INTERPOLATION_CUBIC_ANGLE) {
2713
return _cubic_interpolate_angle_in_time(
2714
p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c,
2715
pre_t, to_t, post_t);
2716
}
2717
return _cubic_interpolate_in_time(
2718
p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c,
2719
pre_t, to_t, post_t);
2720
} break;
2721
default:
2722
return p_keys[idx].value;
2723
}
2724
2725
// do a barrel roll
2726
}
2727
2728
Variant Animation::value_track_interpolate(int p_track, double p_time, bool p_backward) const {
2729
ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
2730
Track *t = tracks[p_track];
2731
ERR_FAIL_COND_V(t->type != TYPE_VALUE, Variant());
2732
ValueTrack *vt = static_cast<ValueTrack *>(t);
2733
2734
bool ok = false;
2735
2736
Variant res = _interpolate(vt->values, p_time, vt->update_mode == UPDATE_DISCRETE ? INTERPOLATION_NEAREST : vt->interpolation, vt->loop_wrap, &ok, p_backward);
2737
2738
if (ok) {
2739
return res;
2740
}
2741
2742
return Variant();
2743
}
2744
2745
void Animation::value_track_set_update_mode(int p_track, UpdateMode p_mode) {
2746
ERR_FAIL_INDEX(p_track, tracks.size());
2747
Track *t = tracks[p_track];
2748
ERR_FAIL_COND(t->type != TYPE_VALUE);
2749
ERR_FAIL_INDEX((int)p_mode, 3);
2750
2751
ValueTrack *vt = static_cast<ValueTrack *>(t);
2752
vt->update_mode = p_mode;
2753
2754
_check_capture_included();
2755
emit_changed();
2756
}
2757
2758
Animation::UpdateMode Animation::value_track_get_update_mode(int p_track) const {
2759
ERR_FAIL_INDEX_V(p_track, tracks.size(), UPDATE_CONTINUOUS);
2760
Track *t = tracks[p_track];
2761
ERR_FAIL_COND_V(t->type != TYPE_VALUE, UPDATE_CONTINUOUS);
2762
2763
ValueTrack *vt = static_cast<ValueTrack *>(t);
2764
return vt->update_mode;
2765
}
2766
2767
template <typename T>
2768
void Animation::_track_get_key_indices_in_range(const Vector<T> &p_array, double from_time, double to_time, List<int> *p_indices, bool p_is_backward) const {
2769
int len = p_array.size();
2770
if (len == 0) {
2771
return;
2772
}
2773
2774
int from = 0;
2775
int to = len - 1;
2776
2777
if (!p_is_backward) {
2778
while (p_array[from].time < from_time || Math::is_equal_approx(p_array[from].time, from_time)) {
2779
from++;
2780
if (to < from) {
2781
return;
2782
}
2783
}
2784
while (p_array[to].time > to_time && !Math::is_equal_approx(p_array[to].time, to_time)) {
2785
to--;
2786
if (to < from) {
2787
return;
2788
}
2789
}
2790
} else {
2791
while (p_array[from].time < from_time && !Math::is_equal_approx(p_array[from].time, from_time)) {
2792
from++;
2793
if (to < from) {
2794
return;
2795
}
2796
}
2797
while (p_array[to].time > to_time || Math::is_equal_approx(p_array[to].time, to_time)) {
2798
to--;
2799
if (to < from) {
2800
return;
2801
}
2802
}
2803
}
2804
2805
if (from == to) {
2806
p_indices->push_back(from);
2807
return;
2808
}
2809
2810
if (!p_is_backward) {
2811
for (int i = from; i <= to; i++) {
2812
p_indices->push_back(i);
2813
}
2814
} else {
2815
for (int i = to; i >= from; i--) {
2816
p_indices->push_back(i);
2817
}
2818
}
2819
}
2820
2821
void Animation::track_get_key_indices_in_range(int p_track, double p_time, double p_delta, List<int> *p_indices, Animation::LoopedFlag p_looped_flag) const {
2822
ERR_FAIL_INDEX(p_track, tracks.size());
2823
2824
if (p_delta == 0) {
2825
return; // Prevent to get key continuously.
2826
}
2827
2828
const Track *t = tracks[p_track];
2829
2830
double from_time = p_time - p_delta;
2831
double to_time = p_time;
2832
2833
bool is_backward = false;
2834
if (from_time > to_time) {
2835
is_backward = true;
2836
SWAP(from_time, to_time);
2837
}
2838
2839
switch (loop_mode) {
2840
case LOOP_NONE: {
2841
if (from_time < 0) {
2842
from_time = 0;
2843
}
2844
if (from_time > length) {
2845
from_time = length;
2846
}
2847
2848
if (to_time < 0) {
2849
to_time = 0;
2850
}
2851
if (to_time > length) {
2852
to_time = length;
2853
}
2854
} break;
2855
case LOOP_LINEAR: {
2856
if (from_time > length || from_time < 0) {
2857
from_time = Math::fposmod(from_time, length);
2858
}
2859
if (to_time > length || to_time < 0) {
2860
to_time = Math::fposmod(to_time, length);
2861
}
2862
2863
if (from_time > to_time) {
2864
// Handle loop by splitting.
2865
double anim_end = length + CMP_EPSILON;
2866
double anim_start = -CMP_EPSILON;
2867
2868
switch (t->type) {
2869
case TYPE_POSITION_3D: {
2870
const PositionTrack *tt = static_cast<const PositionTrack *>(t);
2871
if (tt->compressed_track >= 0) {
2872
_get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, length, p_indices);
2873
_get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, to_time, p_indices);
2874
} else {
2875
if (!is_backward) {
2876
_track_get_key_indices_in_range(tt->positions, from_time, anim_end, p_indices, is_backward);
2877
_track_get_key_indices_in_range(tt->positions, anim_start, to_time, p_indices, is_backward);
2878
} else {
2879
_track_get_key_indices_in_range(tt->positions, anim_start, to_time, p_indices, is_backward);
2880
_track_get_key_indices_in_range(tt->positions, from_time, anim_end, p_indices, is_backward);
2881
}
2882
}
2883
} break;
2884
case TYPE_ROTATION_3D: {
2885
const RotationTrack *rt = static_cast<const RotationTrack *>(t);
2886
if (rt->compressed_track >= 0) {
2887
_get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, length, p_indices);
2888
_get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, to_time, p_indices);
2889
} else {
2890
if (!is_backward) {
2891
_track_get_key_indices_in_range(rt->rotations, from_time, anim_end, p_indices, is_backward);
2892
_track_get_key_indices_in_range(rt->rotations, anim_start, to_time, p_indices, is_backward);
2893
} else {
2894
_track_get_key_indices_in_range(rt->rotations, anim_start, to_time, p_indices, is_backward);
2895
_track_get_key_indices_in_range(rt->rotations, from_time, anim_end, p_indices, is_backward);
2896
}
2897
}
2898
} break;
2899
case TYPE_SCALE_3D: {
2900
const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
2901
if (st->compressed_track >= 0) {
2902
_get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, length, p_indices);
2903
_get_compressed_key_indices_in_range<3>(st->compressed_track, 0, to_time, p_indices);
2904
} else {
2905
if (!is_backward) {
2906
_track_get_key_indices_in_range(st->scales, from_time, anim_end, p_indices, is_backward);
2907
_track_get_key_indices_in_range(st->scales, anim_start, to_time, p_indices, is_backward);
2908
} else {
2909
_track_get_key_indices_in_range(st->scales, anim_start, to_time, p_indices, is_backward);
2910
_track_get_key_indices_in_range(st->scales, from_time, anim_end, p_indices, is_backward);
2911
}
2912
}
2913
} break;
2914
case TYPE_BLEND_SHAPE: {
2915
const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
2916
if (bst->compressed_track >= 0) {
2917
_get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, length, p_indices);
2918
_get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, to_time, p_indices);
2919
} else {
2920
if (!is_backward) {
2921
_track_get_key_indices_in_range(bst->blend_shapes, from_time, anim_end, p_indices, is_backward);
2922
_track_get_key_indices_in_range(bst->blend_shapes, anim_start, to_time, p_indices, is_backward);
2923
} else {
2924
_track_get_key_indices_in_range(bst->blend_shapes, anim_start, to_time, p_indices, is_backward);
2925
_track_get_key_indices_in_range(bst->blend_shapes, from_time, anim_end, p_indices, is_backward);
2926
}
2927
}
2928
} break;
2929
case TYPE_VALUE: {
2930
const ValueTrack *vt = static_cast<const ValueTrack *>(t);
2931
if (!is_backward) {
2932
_track_get_key_indices_in_range(vt->values, from_time, anim_end, p_indices, is_backward);
2933
_track_get_key_indices_in_range(vt->values, anim_start, to_time, p_indices, is_backward);
2934
} else {
2935
_track_get_key_indices_in_range(vt->values, anim_start, to_time, p_indices, is_backward);
2936
_track_get_key_indices_in_range(vt->values, from_time, anim_end, p_indices, is_backward);
2937
}
2938
} break;
2939
case TYPE_METHOD: {
2940
const MethodTrack *mt = static_cast<const MethodTrack *>(t);
2941
if (!is_backward) {
2942
_track_get_key_indices_in_range(mt->methods, from_time, anim_end, p_indices, is_backward);
2943
_track_get_key_indices_in_range(mt->methods, anim_start, to_time, p_indices, is_backward);
2944
} else {
2945
_track_get_key_indices_in_range(mt->methods, anim_start, to_time, p_indices, is_backward);
2946
_track_get_key_indices_in_range(mt->methods, from_time, anim_end, p_indices, is_backward);
2947
}
2948
} break;
2949
case TYPE_BEZIER: {
2950
const BezierTrack *bz = static_cast<const BezierTrack *>(t);
2951
if (!is_backward) {
2952
_track_get_key_indices_in_range(bz->values, from_time, anim_end, p_indices, is_backward);
2953
_track_get_key_indices_in_range(bz->values, anim_start, to_time, p_indices, is_backward);
2954
} else {
2955
_track_get_key_indices_in_range(bz->values, anim_start, to_time, p_indices, is_backward);
2956
_track_get_key_indices_in_range(bz->values, from_time, anim_end, p_indices, is_backward);
2957
}
2958
} break;
2959
case TYPE_AUDIO: {
2960
const AudioTrack *ad = static_cast<const AudioTrack *>(t);
2961
if (!is_backward) {
2962
_track_get_key_indices_in_range(ad->values, from_time, anim_end, p_indices, is_backward);
2963
_track_get_key_indices_in_range(ad->values, anim_start, to_time, p_indices, is_backward);
2964
} else {
2965
_track_get_key_indices_in_range(ad->values, anim_start, to_time, p_indices, is_backward);
2966
_track_get_key_indices_in_range(ad->values, from_time, anim_end, p_indices, is_backward);
2967
}
2968
} break;
2969
case TYPE_ANIMATION: {
2970
const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
2971
if (!is_backward) {
2972
_track_get_key_indices_in_range(an->values, from_time, anim_end, p_indices, is_backward);
2973
_track_get_key_indices_in_range(an->values, anim_start, to_time, p_indices, is_backward);
2974
} else {
2975
_track_get_key_indices_in_range(an->values, anim_start, to_time, p_indices, is_backward);
2976
_track_get_key_indices_in_range(an->values, from_time, anim_end, p_indices, is_backward);
2977
}
2978
} break;
2979
}
2980
return;
2981
}
2982
2983
// Not from_time > to_time but most recent of looping...
2984
if (p_looped_flag != Animation::LOOPED_FLAG_NONE) {
2985
if (!is_backward && Math::is_equal_approx(from_time, 0)) {
2986
int edge = track_find_key(p_track, 0, FIND_MODE_EXACT);
2987
if (edge >= 0) {
2988
p_indices->push_back(edge);
2989
}
2990
} else if (is_backward && Math::is_equal_approx(to_time, length)) {
2991
int edge = track_find_key(p_track, length, FIND_MODE_EXACT);
2992
if (edge >= 0) {
2993
p_indices->push_back(edge);
2994
}
2995
}
2996
}
2997
} break;
2998
case LOOP_PINGPONG: {
2999
if (from_time > length || from_time < 0) {
3000
from_time = Math::pingpong(from_time, length);
3001
}
3002
if (to_time > length || to_time < 0) {
3003
to_time = Math::pingpong(to_time, length);
3004
}
3005
3006
if (p_looped_flag == Animation::LOOPED_FLAG_START) {
3007
// Handle loop by splitting.
3008
switch (t->type) {
3009
case TYPE_POSITION_3D: {
3010
const PositionTrack *tt = static_cast<const PositionTrack *>(t);
3011
if (tt->compressed_track >= 0) {
3012
_get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, from_time, p_indices);
3013
_get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, to_time, p_indices);
3014
} else {
3015
_track_get_key_indices_in_range(tt->positions, 0, from_time, p_indices, true);
3016
_track_get_key_indices_in_range(tt->positions, 0, to_time, p_indices, false);
3017
}
3018
} break;
3019
case TYPE_ROTATION_3D: {
3020
const RotationTrack *rt = static_cast<const RotationTrack *>(t);
3021
if (rt->compressed_track >= 0) {
3022
_get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, from_time, p_indices);
3023
_get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, to_time, p_indices);
3024
} else {
3025
_track_get_key_indices_in_range(rt->rotations, 0, from_time, p_indices, true);
3026
_track_get_key_indices_in_range(rt->rotations, 0, to_time, p_indices, false);
3027
}
3028
} break;
3029
case TYPE_SCALE_3D: {
3030
const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
3031
if (st->compressed_track >= 0) {
3032
_get_compressed_key_indices_in_range<3>(st->compressed_track, 0, from_time, p_indices);
3033
_get_compressed_key_indices_in_range<3>(st->compressed_track, 0, to_time, p_indices);
3034
} else {
3035
_track_get_key_indices_in_range(st->scales, 0, from_time, p_indices, true);
3036
_track_get_key_indices_in_range(st->scales, 0, to_time, p_indices, false);
3037
}
3038
} break;
3039
case TYPE_BLEND_SHAPE: {
3040
const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
3041
if (bst->compressed_track >= 0) {
3042
_get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, from_time, p_indices);
3043
_get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, to_time, p_indices);
3044
} else {
3045
_track_get_key_indices_in_range(bst->blend_shapes, 0, from_time, p_indices, true);
3046
_track_get_key_indices_in_range(bst->blend_shapes, 0, to_time, p_indices, false);
3047
}
3048
} break;
3049
case TYPE_VALUE: {
3050
const ValueTrack *vt = static_cast<const ValueTrack *>(t);
3051
_track_get_key_indices_in_range(vt->values, 0, from_time, p_indices, true);
3052
_track_get_key_indices_in_range(vt->values, 0, to_time, p_indices, false);
3053
} break;
3054
case TYPE_METHOD: {
3055
const MethodTrack *mt = static_cast<const MethodTrack *>(t);
3056
_track_get_key_indices_in_range(mt->methods, 0, from_time, p_indices, true);
3057
_track_get_key_indices_in_range(mt->methods, 0, to_time, p_indices, false);
3058
} break;
3059
case TYPE_BEZIER: {
3060
const BezierTrack *bz = static_cast<const BezierTrack *>(t);
3061
_track_get_key_indices_in_range(bz->values, 0, from_time, p_indices, true);
3062
_track_get_key_indices_in_range(bz->values, 0, to_time, p_indices, false);
3063
} break;
3064
case TYPE_AUDIO: {
3065
const AudioTrack *ad = static_cast<const AudioTrack *>(t);
3066
_track_get_key_indices_in_range(ad->values, 0, from_time, p_indices, true);
3067
_track_get_key_indices_in_range(ad->values, 0, to_time, p_indices, false);
3068
} break;
3069
case TYPE_ANIMATION: {
3070
const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
3071
_track_get_key_indices_in_range(an->values, 0, from_time, p_indices, true);
3072
_track_get_key_indices_in_range(an->values, 0, to_time, p_indices, false);
3073
} break;
3074
}
3075
return;
3076
}
3077
if (p_looped_flag == Animation::LOOPED_FLAG_END) {
3078
// Handle loop by splitting.
3079
switch (t->type) {
3080
case TYPE_POSITION_3D: {
3081
const PositionTrack *tt = static_cast<const PositionTrack *>(t);
3082
if (tt->compressed_track >= 0) {
3083
_get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, length, p_indices);
3084
_get_compressed_key_indices_in_range<3>(tt->compressed_track, to_time, length, p_indices);
3085
} else {
3086
_track_get_key_indices_in_range(tt->positions, from_time, length, p_indices, false);
3087
_track_get_key_indices_in_range(tt->positions, to_time, length, p_indices, true);
3088
}
3089
} break;
3090
case TYPE_ROTATION_3D: {
3091
const RotationTrack *rt = static_cast<const RotationTrack *>(t);
3092
if (rt->compressed_track >= 0) {
3093
_get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, length, p_indices);
3094
_get_compressed_key_indices_in_range<3>(rt->compressed_track, to_time, length, p_indices);
3095
} else {
3096
_track_get_key_indices_in_range(rt->rotations, from_time, length, p_indices, false);
3097
_track_get_key_indices_in_range(rt->rotations, to_time, length, p_indices, true);
3098
}
3099
} break;
3100
case TYPE_SCALE_3D: {
3101
const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
3102
if (st->compressed_track >= 0) {
3103
_get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, length, p_indices);
3104
_get_compressed_key_indices_in_range<3>(st->compressed_track, to_time, length, p_indices);
3105
} else {
3106
_track_get_key_indices_in_range(st->scales, from_time, length, p_indices, false);
3107
_track_get_key_indices_in_range(st->scales, to_time, length, p_indices, true);
3108
}
3109
} break;
3110
case TYPE_BLEND_SHAPE: {
3111
const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
3112
if (bst->compressed_track >= 0) {
3113
_get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, length, p_indices);
3114
_get_compressed_key_indices_in_range<1>(bst->compressed_track, to_time, length, p_indices);
3115
} else {
3116
_track_get_key_indices_in_range(bst->blend_shapes, from_time, length, p_indices, false);
3117
_track_get_key_indices_in_range(bst->blend_shapes, to_time, length, p_indices, true);
3118
}
3119
} break;
3120
case TYPE_VALUE: {
3121
const ValueTrack *vt = static_cast<const ValueTrack *>(t);
3122
_track_get_key_indices_in_range(vt->values, from_time, length, p_indices, false);
3123
_track_get_key_indices_in_range(vt->values, to_time, length, p_indices, true);
3124
} break;
3125
case TYPE_METHOD: {
3126
const MethodTrack *mt = static_cast<const MethodTrack *>(t);
3127
_track_get_key_indices_in_range(mt->methods, from_time, length, p_indices, false);
3128
_track_get_key_indices_in_range(mt->methods, to_time, length, p_indices, true);
3129
} break;
3130
case TYPE_BEZIER: {
3131
const BezierTrack *bz = static_cast<const BezierTrack *>(t);
3132
_track_get_key_indices_in_range(bz->values, from_time, length, p_indices, false);
3133
_track_get_key_indices_in_range(bz->values, to_time, length, p_indices, true);
3134
} break;
3135
case TYPE_AUDIO: {
3136
const AudioTrack *ad = static_cast<const AudioTrack *>(t);
3137
_track_get_key_indices_in_range(ad->values, from_time, length, p_indices, false);
3138
_track_get_key_indices_in_range(ad->values, to_time, length, p_indices, true);
3139
} break;
3140
case TYPE_ANIMATION: {
3141
const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
3142
_track_get_key_indices_in_range(an->values, from_time, length, p_indices, false);
3143
_track_get_key_indices_in_range(an->values, to_time, length, p_indices, true);
3144
} break;
3145
}
3146
return;
3147
}
3148
3149
// The edge will be pingponged in the next frame and processed there, so let's ignore it now...
3150
if (!is_backward && Math::is_equal_approx(to_time, length)) {
3151
to_time -= CMP_EPSILON;
3152
} else if (is_backward && Math::is_equal_approx(from_time, 0)) {
3153
from_time += CMP_EPSILON;
3154
}
3155
} break;
3156
}
3157
switch (t->type) {
3158
case TYPE_POSITION_3D: {
3159
const PositionTrack *tt = static_cast<const PositionTrack *>(t);
3160
if (tt->compressed_track >= 0) {
3161
_get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, to_time - from_time, p_indices);
3162
} else {
3163
_track_get_key_indices_in_range(tt->positions, from_time, to_time, p_indices, is_backward);
3164
}
3165
} break;
3166
case TYPE_ROTATION_3D: {
3167
const RotationTrack *rt = static_cast<const RotationTrack *>(t);
3168
if (rt->compressed_track >= 0) {
3169
_get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, to_time - from_time, p_indices);
3170
} else {
3171
_track_get_key_indices_in_range(rt->rotations, from_time, to_time, p_indices, is_backward);
3172
}
3173
} break;
3174
case TYPE_SCALE_3D: {
3175
const ScaleTrack *st = static_cast<const ScaleTrack *>(t);
3176
if (st->compressed_track >= 0) {
3177
_get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, to_time - from_time, p_indices);
3178
} else {
3179
_track_get_key_indices_in_range(st->scales, from_time, to_time, p_indices, is_backward);
3180
}
3181
} break;
3182
case TYPE_BLEND_SHAPE: {
3183
const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t);
3184
if (bst->compressed_track >= 0) {
3185
_get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, to_time - from_time, p_indices);
3186
} else {
3187
_track_get_key_indices_in_range(bst->blend_shapes, from_time, to_time, p_indices, is_backward);
3188
}
3189
} break;
3190
case TYPE_VALUE: {
3191
const ValueTrack *vt = static_cast<const ValueTrack *>(t);
3192
_track_get_key_indices_in_range(vt->values, from_time, to_time, p_indices, is_backward);
3193
} break;
3194
case TYPE_METHOD: {
3195
const MethodTrack *mt = static_cast<const MethodTrack *>(t);
3196
_track_get_key_indices_in_range(mt->methods, from_time, to_time, p_indices, is_backward);
3197
} break;
3198
case TYPE_BEZIER: {
3199
const BezierTrack *bz = static_cast<const BezierTrack *>(t);
3200
_track_get_key_indices_in_range(bz->values, from_time, to_time, p_indices, is_backward);
3201
} break;
3202
case TYPE_AUDIO: {
3203
const AudioTrack *ad = static_cast<const AudioTrack *>(t);
3204
_track_get_key_indices_in_range(ad->values, from_time, to_time, p_indices, is_backward);
3205
} break;
3206
case TYPE_ANIMATION: {
3207
const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
3208
_track_get_key_indices_in_range(an->values, from_time, to_time, p_indices, is_backward);
3209
} break;
3210
}
3211
}
3212
3213
void Animation::add_marker(const StringName &p_name, double p_time) {
3214
int idx = _find(marker_names, p_time);
3215
3216
if (idx >= 0 && idx < marker_names.size() && Math::is_equal_approx(p_time, marker_names[idx].time)) {
3217
marker_times.erase(marker_names[idx].name);
3218
marker_colors.erase(marker_names[idx].name);
3219
marker_names.write[idx].name = p_name;
3220
marker_times.insert(p_name, p_time);
3221
marker_colors.insert(p_name, Color(1, 1, 1));
3222
} else {
3223
_marker_insert(p_time, marker_names, MarkerKey(p_time, p_name));
3224
marker_times.insert(p_name, p_time);
3225
marker_colors.insert(p_name, Color(1, 1, 1));
3226
}
3227
}
3228
3229
void Animation::remove_marker(const StringName &p_name) {
3230
HashMap<StringName, double>::Iterator E = marker_times.find(p_name);
3231
ERR_FAIL_COND(!E);
3232
int idx = _find(marker_names, E->value);
3233
bool success = idx >= 0 && idx < marker_names.size() && Math::is_equal_approx(marker_names[idx].time, E->value);
3234
ERR_FAIL_COND(!success);
3235
marker_names.remove_at(idx);
3236
marker_times.remove(E);
3237
marker_colors.erase(p_name);
3238
}
3239
3240
bool Animation::has_marker(const StringName &p_name) const {
3241
return marker_times.has(p_name);
3242
}
3243
3244
StringName Animation::get_marker_at_time(double p_time) const {
3245
int idx = _find(marker_names, p_time);
3246
3247
if (idx >= 0 && idx < marker_names.size() && Math::is_equal_approx(marker_names[idx].time, p_time)) {
3248
return marker_names[idx].name;
3249
}
3250
3251
return StringName();
3252
}
3253
3254
StringName Animation::get_next_marker(double p_time) const {
3255
int idx = _find(marker_names, p_time);
3256
3257
if (idx >= -1 && idx < marker_names.size() - 1) {
3258
// _find ensures that the time at idx is always the closest time to p_time that is also smaller to it.
3259
// So we add 1 to get the next marker.
3260
return marker_names[idx + 1].name;
3261
}
3262
return StringName();
3263
}
3264
3265
StringName Animation::get_prev_marker(double p_time) const {
3266
int idx = _find(marker_names, p_time);
3267
3268
if (idx >= 0 && idx < marker_names.size()) {
3269
return marker_names[idx].name;
3270
}
3271
return StringName();
3272
}
3273
3274
double Animation::get_marker_time(const StringName &p_name) const {
3275
ERR_FAIL_COND_V(!marker_times.has(p_name), -1);
3276
return marker_times.get(p_name);
3277
}
3278
3279
PackedStringArray Animation::get_marker_names() const {
3280
PackedStringArray names;
3281
// We iterate on marker_names so the result is sorted by time.
3282
for (const MarkerKey &marker_name : marker_names) {
3283
names.push_back(marker_name.name);
3284
}
3285
return names;
3286
}
3287
3288
Color Animation::get_marker_color(const StringName &p_name) const {
3289
ERR_FAIL_COND_V(!marker_colors.has(p_name), Color());
3290
return marker_colors[p_name];
3291
}
3292
3293
void Animation::set_marker_color(const StringName &p_name, const Color &p_color) {
3294
marker_colors[p_name] = p_color;
3295
}
3296
3297
Vector<Variant> Animation::method_track_get_params(int p_track, int p_key_idx) const {
3298
ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector<Variant>());
3299
Track *t = tracks[p_track];
3300
ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector<Variant>());
3301
3302
MethodTrack *pm = static_cast<MethodTrack *>(t);
3303
3304
ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), Vector<Variant>());
3305
3306
const MethodKey &mk = pm->methods[p_key_idx];
3307
3308
return mk.params;
3309
}
3310
3311
StringName Animation::method_track_get_name(int p_track, int p_key_idx) const {
3312
ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
3313
Track *t = tracks[p_track];
3314
ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName());
3315
3316
MethodTrack *pm = static_cast<MethodTrack *>(t);
3317
3318
ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), StringName());
3319
3320
return pm->methods[p_key_idx].method;
3321
}
3322
3323
Array Animation::make_default_bezier_key(float p_value) {
3324
const double max_width = length / 2.0;
3325
Array new_point;
3326
new_point.resize(5);
3327
3328
new_point[0] = p_value;
3329
new_point[1] = MAX(-0.25, -max_width);
3330
new_point[2] = 0;
3331
new_point[3] = MIN(0.25, max_width);
3332
new_point[4] = 0;
3333
3334
return new_point;
3335
}
3336
3337
int Animation::bezier_track_insert_key(int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle) {
3338
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
3339
Track *t = tracks[p_track];
3340
ERR_FAIL_COND_V(t->type != TYPE_BEZIER, -1);
3341
3342
BezierTrack *bt = static_cast<BezierTrack *>(t);
3343
3344
TKey<BezierKey> k;
3345
k.time = p_time;
3346
k.value.value = p_value;
3347
k.value.in_handle = p_in_handle;
3348
if (k.value.in_handle.x > 0) {
3349
k.value.in_handle.x = 0;
3350
}
3351
k.value.out_handle = p_out_handle;
3352
if (k.value.out_handle.x < 0) {
3353
k.value.out_handle.x = 0;
3354
}
3355
3356
int key = _insert(p_time, bt->values, k);
3357
3358
emit_changed();
3359
3360
return key;
3361
}
3362
3363
void Animation::bezier_track_set_key_value(int p_track, int p_index, real_t p_value) {
3364
ERR_FAIL_INDEX(p_track, tracks.size());
3365
Track *t = tracks[p_track];
3366
ERR_FAIL_COND(t->type != TYPE_BEZIER);
3367
3368
BezierTrack *bt = static_cast<BezierTrack *>(t);
3369
3370
ERR_FAIL_INDEX(p_index, bt->values.size());
3371
3372
bt->values.write[p_index].value.value = p_value;
3373
3374
emit_changed();
3375
}
3376
3377
void Animation::bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio) {
3378
ERR_FAIL_INDEX(p_track, tracks.size());
3379
Track *t = tracks[p_track];
3380
ERR_FAIL_COND(t->type != TYPE_BEZIER);
3381
3382
BezierTrack *bt = static_cast<BezierTrack *>(t);
3383
3384
ERR_FAIL_INDEX(p_index, bt->values.size());
3385
3386
Vector2 in_handle = p_handle;
3387
if (in_handle.x > 0) {
3388
in_handle.x = 0;
3389
}
3390
bt->values.write[p_index].value.in_handle = in_handle;
3391
3392
#ifdef TOOLS_ENABLED
3393
if (bt->values[p_index].value.handle_mode == HANDLE_MODE_LINEAR) {
3394
bt->values.write[p_index].value.in_handle = Vector2();
3395
bt->values.write[p_index].value.out_handle = Vector2();
3396
} else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_BALANCED) {
3397
Transform2D xform;
3398
xform.set_scale(Vector2(1.0, 1.0 / p_balanced_value_time_ratio));
3399
3400
Vector2 vec_out = xform.xform(bt->values[p_index].value.out_handle);
3401
Vector2 vec_in = xform.xform(in_handle);
3402
3403
bt->values.write[p_index].value.out_handle = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length());
3404
} else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_MIRRORED) {
3405
bt->values.write[p_index].value.out_handle = -in_handle;
3406
}
3407
#endif // TOOLS_ENABLED
3408
3409
emit_changed();
3410
}
3411
3412
void Animation::bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio) {
3413
ERR_FAIL_INDEX(p_track, tracks.size());
3414
Track *t = tracks[p_track];
3415
ERR_FAIL_COND(t->type != TYPE_BEZIER);
3416
3417
BezierTrack *bt = static_cast<BezierTrack *>(t);
3418
3419
ERR_FAIL_INDEX(p_index, bt->values.size());
3420
3421
Vector2 out_handle = p_handle;
3422
if (out_handle.x < 0) {
3423
out_handle.x = 0;
3424
}
3425
bt->values.write[p_index].value.out_handle = out_handle;
3426
3427
#ifdef TOOLS_ENABLED
3428
if (bt->values[p_index].value.handle_mode == HANDLE_MODE_LINEAR) {
3429
bt->values.write[p_index].value.in_handle = Vector2();
3430
bt->values.write[p_index].value.out_handle = Vector2();
3431
} else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_BALANCED) {
3432
Transform2D xform;
3433
xform.set_scale(Vector2(1.0, 1.0 / p_balanced_value_time_ratio));
3434
3435
Vector2 vec_in = xform.xform(bt->values[p_index].value.in_handle);
3436
Vector2 vec_out = xform.xform(out_handle);
3437
3438
bt->values.write[p_index].value.in_handle = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length());
3439
} else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_MIRRORED) {
3440
bt->values.write[p_index].value.in_handle = -out_handle;
3441
}
3442
#endif // TOOLS_ENABLED
3443
3444
emit_changed();
3445
}
3446
3447
real_t Animation::bezier_track_get_key_value(int p_track, int p_index) const {
3448
ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
3449
Track *t = tracks[p_track];
3450
ERR_FAIL_COND_V(t->type != TYPE_BEZIER, 0);
3451
3452
BezierTrack *bt = static_cast<BezierTrack *>(t);
3453
3454
ERR_FAIL_INDEX_V(p_index, bt->values.size(), 0);
3455
3456
return bt->values[p_index].value.value;
3457
}
3458
3459
Vector2 Animation::bezier_track_get_key_in_handle(int p_track, int p_index) const {
3460
ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
3461
Track *t = tracks[p_track];
3462
ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
3463
3464
BezierTrack *bt = static_cast<BezierTrack *>(t);
3465
3466
ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
3467
3468
return bt->values[p_index].value.in_handle;
3469
}
3470
3471
Vector2 Animation::bezier_track_get_key_out_handle(int p_track, int p_index) const {
3472
ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
3473
Track *t = tracks[p_track];
3474
ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
3475
3476
BezierTrack *bt = static_cast<BezierTrack *>(t);
3477
3478
ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
3479
3480
return bt->values[p_index].value.out_handle;
3481
}
3482
3483
#ifdef TOOLS_ENABLED
3484
void Animation::bezier_track_set_key_handle_mode(int p_track, int p_index, HandleMode p_mode, HandleSetMode p_set_mode) {
3485
ERR_FAIL_INDEX(p_track, tracks.size());
3486
Track *t = tracks[p_track];
3487
ERR_FAIL_COND(t->type != TYPE_BEZIER);
3488
3489
BezierTrack *bt = static_cast<BezierTrack *>(t);
3490
3491
ERR_FAIL_INDEX(p_index, bt->values.size());
3492
3493
bt->values.write[p_index].value.handle_mode = p_mode;
3494
3495
if (p_mode != HANDLE_MODE_FREE && p_set_mode != HANDLE_SET_MODE_NONE) {
3496
Vector2 &in_handle = bt->values.write[p_index].value.in_handle;
3497
Vector2 &out_handle = bt->values.write[p_index].value.out_handle;
3498
bezier_track_calculate_handles(p_track, p_index, p_mode, p_set_mode, &in_handle, &out_handle);
3499
}
3500
3501
emit_changed();
3502
}
3503
3504
Animation::HandleMode Animation::bezier_track_get_key_handle_mode(int p_track, int p_index) const {
3505
ERR_FAIL_INDEX_V(p_track, tracks.size(), HANDLE_MODE_FREE);
3506
Track *t = tracks[p_track];
3507
ERR_FAIL_COND_V(t->type != TYPE_BEZIER, HANDLE_MODE_FREE);
3508
3509
BezierTrack *bt = static_cast<BezierTrack *>(t);
3510
3511
ERR_FAIL_INDEX_V(p_index, bt->values.size(), HANDLE_MODE_FREE);
3512
3513
return bt->values[p_index].value.handle_mode;
3514
}
3515
3516
bool Animation::bezier_track_calculate_handles(int p_track, int p_index, HandleMode p_mode, HandleSetMode p_set_mode, Vector2 *r_in_handle, Vector2 *r_out_handle) {
3517
ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
3518
Track *t = tracks[p_track];
3519
ERR_FAIL_COND_V(t->type != TYPE_BEZIER, false);
3520
3521
BezierTrack *bt = static_cast<BezierTrack *>(t);
3522
ERR_FAIL_INDEX_V(p_index, bt->values.size(), false);
3523
3524
int prev_key = MAX(0, p_index - 1);
3525
int next_key = MIN(bt->values.size() - 1, p_index + 1);
3526
if (prev_key == next_key) {
3527
return false;
3528
}
3529
3530
float time = bt->values[p_index].time;
3531
float prev_time = bt->values[prev_key].time;
3532
float prev_value = bt->values[prev_key].value.value;
3533
float next_time = bt->values[next_key].time;
3534
float next_value = bt->values[next_key].value.value;
3535
3536
return bezier_track_calculate_handles(time, prev_time, prev_value, next_time, next_value, p_mode, p_set_mode, r_in_handle, r_out_handle);
3537
}
3538
3539
bool Animation::bezier_track_calculate_handles(float p_time, float p_prev_time, float p_prev_value, float p_next_time, float p_next_value, HandleMode p_mode, HandleSetMode p_set_mode, Vector2 *r_in_handle, Vector2 *r_out_handle) {
3540
ERR_FAIL_COND_V(p_mode == HANDLE_MODE_FREE, false);
3541
ERR_FAIL_COND_V(p_set_mode == HANDLE_SET_MODE_NONE, false);
3542
3543
Vector2 in_handle;
3544
Vector2 out_handle;
3545
3546
if (p_mode == HANDLE_MODE_LINEAR) {
3547
in_handle = Vector2(0, 0);
3548
out_handle = Vector2(0, 0);
3549
} else if (p_mode == HANDLE_MODE_BALANCED) {
3550
if (p_set_mode == HANDLE_SET_MODE_RESET) {
3551
real_t handle_length = 1.0 / 3.0;
3552
in_handle.x = (p_prev_time - p_time) * handle_length;
3553
in_handle.y = 0;
3554
out_handle.x = (p_next_time - p_time) * handle_length;
3555
out_handle.y = 0;
3556
} else if (p_set_mode == HANDLE_SET_MODE_AUTO) {
3557
real_t handle_length = 1.0 / 6.0;
3558
real_t tangent = (p_next_value - p_prev_value) / (p_next_time - p_prev_time);
3559
in_handle.x = (p_prev_time - p_time) * handle_length;
3560
in_handle.y = in_handle.x * tangent;
3561
out_handle.x = (p_next_time - p_time) * handle_length;
3562
out_handle.y = out_handle.x * tangent;
3563
}
3564
} else if (p_mode == HANDLE_MODE_MIRRORED) {
3565
real_t handle_length = 1.0 / 4.0;
3566
real_t prev_interval = Math::abs(p_time - p_prev_time);
3567
real_t next_interval = Math::abs(p_time - p_next_time);
3568
real_t min_time = 0;
3569
if (Math::is_zero_approx(prev_interval)) {
3570
min_time = next_interval;
3571
} else if (Math::is_zero_approx(next_interval)) {
3572
min_time = prev_interval;
3573
} else {
3574
min_time = MIN(prev_interval, next_interval);
3575
}
3576
if (p_set_mode == HANDLE_SET_MODE_RESET) {
3577
in_handle.x = -min_time * handle_length;
3578
in_handle.y = 0;
3579
out_handle.x = min_time * handle_length;
3580
out_handle.y = 0;
3581
} else if (p_set_mode == HANDLE_SET_MODE_AUTO) {
3582
real_t tangent = (p_next_value - p_prev_value) / min_time;
3583
in_handle.x = -min_time * handle_length;
3584
in_handle.y = in_handle.x * tangent;
3585
out_handle.x = min_time * handle_length;
3586
out_handle.y = out_handle.x * tangent;
3587
}
3588
}
3589
3590
if (r_in_handle != nullptr) {
3591
*r_in_handle = in_handle;
3592
}
3593
3594
if (r_out_handle != nullptr) {
3595
*r_out_handle = out_handle;
3596
}
3597
3598
return true;
3599
}
3600
3601
#endif // TOOLS_ENABLED
3602
3603
real_t Animation::bezier_track_interpolate(int p_track, double p_time) const {
3604
//this uses a different interpolation scheme
3605
ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
3606
Track *track = tracks[p_track];
3607
ERR_FAIL_COND_V(track->type != TYPE_BEZIER, 0);
3608
3609
BezierTrack *bt = static_cast<BezierTrack *>(track);
3610
3611
int len = _find(bt->values, length) + 1; // try to find last key (there may be more past the end)
3612
3613
if (len <= 0) {
3614
// (-1 or -2 returned originally) (plus one above)
3615
return 0;
3616
} else if (len == 1) { // one key found (0+1), return it
3617
return bt->values[0].value.value;
3618
}
3619
3620
int idx = _find(bt->values, p_time);
3621
3622
ERR_FAIL_COND_V(idx == -2, 0);
3623
3624
//there really is no looping interpolation on bezier
3625
3626
if (idx < 0) {
3627
return bt->values[0].value.value;
3628
}
3629
3630
if (idx >= bt->values.size() - 1) {
3631
return bt->values[bt->values.size() - 1].value.value;
3632
}
3633
3634
double t = p_time - bt->values[idx].time;
3635
3636
int iterations = 10;
3637
3638
real_t duration = bt->values[idx + 1].time - bt->values[idx].time; // time duration between our two keyframes
3639
real_t low = 0.0; // 0% of the current animation segment
3640
real_t high = 1.0; // 100% of the current animation segment
3641
3642
Vector2 start(0, bt->values[idx].value.value);
3643
Vector2 start_out = start + bt->values[idx].value.out_handle;
3644
Vector2 end(duration, bt->values[idx + 1].value.value);
3645
Vector2 end_in = end + bt->values[idx + 1].value.in_handle;
3646
3647
//narrow high and low as much as possible
3648
for (int i = 0; i < iterations; i++) {
3649
real_t middle = (low + high) / 2;
3650
3651
Vector2 interp = start.bezier_interpolate(start_out, end_in, end, middle);
3652
3653
if (interp.x < t) {
3654
low = middle;
3655
} else {
3656
high = middle;
3657
}
3658
}
3659
3660
//interpolate the result:
3661
Vector2 low_pos = start.bezier_interpolate(start_out, end_in, end, low);
3662
Vector2 high_pos = start.bezier_interpolate(start_out, end_in, end, high);
3663
real_t c = (t - low_pos.x) / (high_pos.x - low_pos.x);
3664
3665
return low_pos.lerp(high_pos, c).y;
3666
}
3667
3668
int Animation::audio_track_insert_key(int p_track, double p_time, const Ref<Resource> &p_stream, real_t p_start_offset, real_t p_end_offset) {
3669
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
3670
Track *t = tracks[p_track];
3671
ERR_FAIL_COND_V(t->type != TYPE_AUDIO, -1);
3672
3673
AudioTrack *at = static_cast<AudioTrack *>(t);
3674
3675
TKey<AudioKey> k;
3676
k.time = p_time;
3677
k.value.stream = p_stream;
3678
k.value.start_offset = p_start_offset;
3679
if (k.value.start_offset < 0) {
3680
k.value.start_offset = 0;
3681
}
3682
k.value.end_offset = p_end_offset;
3683
if (k.value.end_offset < 0) {
3684
k.value.end_offset = 0;
3685
}
3686
3687
int key = _insert(p_time, at->values, k);
3688
3689
emit_changed();
3690
3691
return key;
3692
}
3693
3694
void Animation::audio_track_set_key_stream(int p_track, int p_key, const Ref<Resource> &p_stream) {
3695
ERR_FAIL_INDEX(p_track, tracks.size());
3696
Track *t = tracks[p_track];
3697
ERR_FAIL_COND(t->type != TYPE_AUDIO);
3698
3699
AudioTrack *at = static_cast<AudioTrack *>(t);
3700
3701
ERR_FAIL_INDEX(p_key, at->values.size());
3702
3703
at->values.write[p_key].value.stream = p_stream;
3704
3705
emit_changed();
3706
}
3707
3708
void Animation::audio_track_set_key_start_offset(int p_track, int p_key, real_t p_offset) {
3709
ERR_FAIL_INDEX(p_track, tracks.size());
3710
Track *t = tracks[p_track];
3711
ERR_FAIL_COND(t->type != TYPE_AUDIO);
3712
3713
AudioTrack *at = static_cast<AudioTrack *>(t);
3714
3715
ERR_FAIL_INDEX(p_key, at->values.size());
3716
3717
if (p_offset < 0) {
3718
p_offset = 0;
3719
}
3720
3721
at->values.write[p_key].value.start_offset = p_offset;
3722
3723
emit_changed();
3724
}
3725
3726
void Animation::audio_track_set_key_end_offset(int p_track, int p_key, real_t p_offset) {
3727
ERR_FAIL_INDEX(p_track, tracks.size());
3728
Track *t = tracks[p_track];
3729
ERR_FAIL_COND(t->type != TYPE_AUDIO);
3730
3731
AudioTrack *at = static_cast<AudioTrack *>(t);
3732
3733
ERR_FAIL_INDEX(p_key, at->values.size());
3734
3735
if (p_offset < 0) {
3736
p_offset = 0;
3737
}
3738
3739
at->values.write[p_key].value.end_offset = p_offset;
3740
3741
emit_changed();
3742
}
3743
3744
Ref<Resource> Animation::audio_track_get_key_stream(int p_track, int p_key) const {
3745
ERR_FAIL_INDEX_V(p_track, tracks.size(), Ref<Resource>());
3746
const Track *t = tracks[p_track];
3747
ERR_FAIL_COND_V(t->type != TYPE_AUDIO, Ref<Resource>());
3748
3749
const AudioTrack *at = static_cast<const AudioTrack *>(t);
3750
3751
ERR_FAIL_INDEX_V(p_key, at->values.size(), Ref<Resource>());
3752
3753
return at->values[p_key].value.stream;
3754
}
3755
3756
real_t Animation::audio_track_get_key_start_offset(int p_track, int p_key) const {
3757
ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
3758
const Track *t = tracks[p_track];
3759
ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
3760
3761
const AudioTrack *at = static_cast<const AudioTrack *>(t);
3762
3763
ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
3764
3765
return at->values[p_key].value.start_offset;
3766
}
3767
3768
real_t Animation::audio_track_get_key_end_offset(int p_track, int p_key) const {
3769
ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
3770
const Track *t = tracks[p_track];
3771
ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
3772
3773
const AudioTrack *at = static_cast<const AudioTrack *>(t);
3774
3775
ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
3776
3777
return at->values[p_key].value.end_offset;
3778
}
3779
3780
void Animation::audio_track_set_use_blend(int p_track, bool p_enable) {
3781
ERR_FAIL_INDEX(p_track, tracks.size());
3782
Track *t = tracks[p_track];
3783
ERR_FAIL_COND(t->type != TYPE_AUDIO);
3784
3785
AudioTrack *at = static_cast<AudioTrack *>(t);
3786
3787
at->use_blend = p_enable;
3788
emit_changed();
3789
}
3790
3791
bool Animation::audio_track_is_use_blend(int p_track) const {
3792
ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
3793
Track *t = tracks[p_track];
3794
ERR_FAIL_COND_V(t->type != TYPE_AUDIO, false);
3795
3796
AudioTrack *at = static_cast<AudioTrack *>(t);
3797
3798
return at->use_blend;
3799
}
3800
3801
//
3802
3803
int Animation::animation_track_insert_key(int p_track, double p_time, const StringName &p_animation) {
3804
ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
3805
Track *t = tracks[p_track];
3806
ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, -1);
3807
3808
AnimationTrack *at = static_cast<AnimationTrack *>(t);
3809
3810
TKey<StringName> k;
3811
k.time = p_time;
3812
k.value = p_animation;
3813
3814
int key = _insert(p_time, at->values, k);
3815
3816
emit_changed();
3817
3818
return key;
3819
}
3820
3821
void Animation::animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation) {
3822
ERR_FAIL_INDEX(p_track, tracks.size());
3823
Track *t = tracks[p_track];
3824
ERR_FAIL_COND(t->type != TYPE_ANIMATION);
3825
3826
AnimationTrack *at = static_cast<AnimationTrack *>(t);
3827
3828
ERR_FAIL_INDEX(p_key, at->values.size());
3829
3830
at->values.write[p_key].value = p_animation;
3831
3832
emit_changed();
3833
}
3834
3835
StringName Animation::animation_track_get_key_animation(int p_track, int p_key) const {
3836
ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
3837
const Track *t = tracks[p_track];
3838
ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, StringName());
3839
3840
const AnimationTrack *at = static_cast<const AnimationTrack *>(t);
3841
3842
ERR_FAIL_INDEX_V(p_key, at->values.size(), StringName());
3843
3844
return at->values[p_key].value;
3845
}
3846
3847
void Animation::set_length(real_t p_length) {
3848
if (p_length < ANIM_MIN_LENGTH) {
3849
p_length = ANIM_MIN_LENGTH;
3850
}
3851
length = p_length;
3852
emit_changed();
3853
}
3854
3855
real_t Animation::get_length() const {
3856
return length;
3857
}
3858
3859
void Animation::set_loop_mode(Animation::LoopMode p_loop_mode) {
3860
loop_mode = p_loop_mode;
3861
emit_changed();
3862
}
3863
3864
Animation::LoopMode Animation::get_loop_mode() const {
3865
return loop_mode;
3866
}
3867
3868
void Animation::track_set_imported(int p_track, bool p_imported) {
3869
ERR_FAIL_INDEX(p_track, tracks.size());
3870
tracks[p_track]->imported = p_imported;
3871
}
3872
3873
bool Animation::track_is_imported(int p_track) const {
3874
ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
3875
return tracks[p_track]->imported;
3876
}
3877
3878
void Animation::track_set_enabled(int p_track, bool p_enabled) {
3879
ERR_FAIL_INDEX(p_track, tracks.size());
3880
tracks[p_track]->enabled = p_enabled;
3881
emit_changed();
3882
}
3883
3884
bool Animation::track_is_enabled(int p_track) const {
3885
ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
3886
return tracks[p_track]->enabled;
3887
}
3888
3889
void Animation::track_move_up(int p_track) {
3890
if (p_track >= 0 && p_track < (tracks.size() - 1)) {
3891
SWAP(tracks.write[p_track], tracks.write[p_track + 1]);
3892
}
3893
3894
emit_changed();
3895
}
3896
3897
void Animation::track_move_down(int p_track) {
3898
if (p_track > 0 && p_track < tracks.size()) {
3899
SWAP(tracks.write[p_track], tracks.write[p_track - 1]);
3900
}
3901
3902
emit_changed();
3903
}
3904
3905
void Animation::track_move_to(int p_track, int p_to_index) {
3906
ERR_FAIL_INDEX(p_track, tracks.size());
3907
ERR_FAIL_INDEX(p_to_index, tracks.size() + 1);
3908
if (p_track == p_to_index || p_track == p_to_index - 1) {
3909
return;
3910
}
3911
3912
Track *track = tracks.get(p_track);
3913
tracks.remove_at(p_track);
3914
// Take into account that the position of the tracks that come after the one removed will change.
3915
tracks.insert(p_to_index > p_track ? p_to_index - 1 : p_to_index, track);
3916
3917
emit_changed();
3918
}
3919
3920
void Animation::track_swap(int p_track, int p_with_track) {
3921
ERR_FAIL_INDEX(p_track, tracks.size());
3922
ERR_FAIL_INDEX(p_with_track, tracks.size());
3923
if (p_track == p_with_track) {
3924
return;
3925
}
3926
SWAP(tracks.write[p_track], tracks.write[p_with_track]);
3927
3928
emit_changed();
3929
}
3930
3931
void Animation::set_step(real_t p_step) {
3932
step = p_step;
3933
emit_changed();
3934
}
3935
3936
real_t Animation::get_step() const {
3937
return step;
3938
}
3939
3940
void Animation::copy_track(int p_track, Ref<Animation> p_to_animation) {
3941
ERR_FAIL_COND(p_to_animation.is_null());
3942
ERR_FAIL_INDEX(p_track, get_track_count());
3943
int dst_track = p_to_animation->get_track_count();
3944
p_to_animation->add_track(track_get_type(p_track));
3945
3946
p_to_animation->track_set_path(dst_track, track_get_path(p_track));
3947
p_to_animation->track_set_imported(dst_track, track_is_imported(p_track));
3948
p_to_animation->track_set_enabled(dst_track, track_is_enabled(p_track));
3949
p_to_animation->track_set_interpolation_type(dst_track, track_get_interpolation_type(p_track));
3950
p_to_animation->track_set_interpolation_loop_wrap(dst_track, track_get_interpolation_loop_wrap(p_track));
3951
if (track_get_type(p_track) == TYPE_VALUE) {
3952
p_to_animation->value_track_set_update_mode(dst_track, value_track_get_update_mode(p_track));
3953
}
3954
3955
for (int i = 0; i < track_get_key_count(p_track); i++) {
3956
p_to_animation->track_insert_key(dst_track, track_get_key_time(p_track, i), track_get_key_value(p_track, i), track_get_key_transition(p_track, i));
3957
}
3958
}
3959
3960
void Animation::_bind_methods() {
3961
ClassDB::bind_method(D_METHOD("add_track", "type", "at_position"), &Animation::add_track, DEFVAL(-1));
3962
ClassDB::bind_method(D_METHOD("remove_track", "track_idx"), &Animation::remove_track);
3963
ClassDB::bind_method(D_METHOD("get_track_count"), &Animation::get_track_count);
3964
ClassDB::bind_method(D_METHOD("track_get_type", "track_idx"), &Animation::track_get_type);
3965
ClassDB::bind_method(D_METHOD("track_get_path", "track_idx"), &Animation::track_get_path);
3966
ClassDB::bind_method(D_METHOD("track_set_path", "track_idx", "path"), &Animation::track_set_path);
3967
ClassDB::bind_method(D_METHOD("find_track", "path", "type"), &Animation::find_track);
3968
3969
ClassDB::bind_method(D_METHOD("track_move_up", "track_idx"), &Animation::track_move_up);
3970
ClassDB::bind_method(D_METHOD("track_move_down", "track_idx"), &Animation::track_move_down);
3971
ClassDB::bind_method(D_METHOD("track_move_to", "track_idx", "to_idx"), &Animation::track_move_to);
3972
ClassDB::bind_method(D_METHOD("track_swap", "track_idx", "with_idx"), &Animation::track_swap);
3973
3974
ClassDB::bind_method(D_METHOD("track_set_imported", "track_idx", "imported"), &Animation::track_set_imported);
3975
ClassDB::bind_method(D_METHOD("track_is_imported", "track_idx"), &Animation::track_is_imported);
3976
3977
ClassDB::bind_method(D_METHOD("track_set_enabled", "track_idx", "enabled"), &Animation::track_set_enabled);
3978
ClassDB::bind_method(D_METHOD("track_is_enabled", "track_idx"), &Animation::track_is_enabled);
3979
3980
ClassDB::bind_method(D_METHOD("position_track_insert_key", "track_idx", "time", "position"), &Animation::position_track_insert_key);
3981
ClassDB::bind_method(D_METHOD("rotation_track_insert_key", "track_idx", "time", "rotation"), &Animation::rotation_track_insert_key);
3982
ClassDB::bind_method(D_METHOD("scale_track_insert_key", "track_idx", "time", "scale"), &Animation::scale_track_insert_key);
3983
ClassDB::bind_method(D_METHOD("blend_shape_track_insert_key", "track_idx", "time", "amount"), &Animation::blend_shape_track_insert_key);
3984
3985
ClassDB::bind_method(D_METHOD("position_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::position_track_interpolate, DEFVAL(false));
3986
ClassDB::bind_method(D_METHOD("rotation_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::rotation_track_interpolate, DEFVAL(false));
3987
ClassDB::bind_method(D_METHOD("scale_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::scale_track_interpolate, DEFVAL(false));
3988
ClassDB::bind_method(D_METHOD("blend_shape_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::blend_shape_track_interpolate, DEFVAL(false));
3989
3990
ClassDB::bind_method(D_METHOD("track_insert_key", "track_idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1));
3991
ClassDB::bind_method(D_METHOD("track_remove_key", "track_idx", "key_idx"), &Animation::track_remove_key);
3992
ClassDB::bind_method(D_METHOD("track_remove_key_at_time", "track_idx", "time"), &Animation::track_remove_key_at_time);
3993
ClassDB::bind_method(D_METHOD("track_set_key_value", "track_idx", "key", "value"), &Animation::track_set_key_value);
3994
ClassDB::bind_method(D_METHOD("track_set_key_transition", "track_idx", "key_idx", "transition"), &Animation::track_set_key_transition);
3995
ClassDB::bind_method(D_METHOD("track_set_key_time", "track_idx", "key_idx", "time"), &Animation::track_set_key_time);
3996
ClassDB::bind_method(D_METHOD("track_get_key_transition", "track_idx", "key_idx"), &Animation::track_get_key_transition);
3997
3998
ClassDB::bind_method(D_METHOD("track_get_key_count", "track_idx"), &Animation::track_get_key_count);
3999
ClassDB::bind_method(D_METHOD("track_get_key_value", "track_idx", "key_idx"), &Animation::track_get_key_value);
4000
ClassDB::bind_method(D_METHOD("track_get_key_time", "track_idx", "key_idx"), &Animation::track_get_key_time);
4001
ClassDB::bind_method(D_METHOD("track_find_key", "track_idx", "time", "find_mode", "limit", "backward"), &Animation::track_find_key, DEFVAL(FIND_MODE_NEAREST), DEFVAL(false), DEFVAL(false));
4002
4003
ClassDB::bind_method(D_METHOD("track_set_interpolation_type", "track_idx", "interpolation"), &Animation::track_set_interpolation_type);
4004
ClassDB::bind_method(D_METHOD("track_get_interpolation_type", "track_idx"), &Animation::track_get_interpolation_type);
4005
4006
ClassDB::bind_method(D_METHOD("track_set_interpolation_loop_wrap", "track_idx", "interpolation"), &Animation::track_set_interpolation_loop_wrap);
4007
ClassDB::bind_method(D_METHOD("track_get_interpolation_loop_wrap", "track_idx"), &Animation::track_get_interpolation_loop_wrap);
4008
4009
ClassDB::bind_method(D_METHOD("track_is_compressed", "track_idx"), &Animation::track_is_compressed);
4010
4011
ClassDB::bind_method(D_METHOD("value_track_set_update_mode", "track_idx", "mode"), &Animation::value_track_set_update_mode);
4012
ClassDB::bind_method(D_METHOD("value_track_get_update_mode", "track_idx"), &Animation::value_track_get_update_mode);
4013
4014
ClassDB::bind_method(D_METHOD("value_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::value_track_interpolate, DEFVAL(false));
4015
4016
ClassDB::bind_method(D_METHOD("method_track_get_name", "track_idx", "key_idx"), &Animation::method_track_get_name);
4017
ClassDB::bind_method(D_METHOD("method_track_get_params", "track_idx", "key_idx"), &Animation::method_track_get_params);
4018
4019
ClassDB::bind_method(D_METHOD("bezier_track_insert_key", "track_idx", "time", "value", "in_handle", "out_handle"), &Animation::bezier_track_insert_key, DEFVAL(Vector2()), DEFVAL(Vector2()));
4020
4021
ClassDB::bind_method(D_METHOD("bezier_track_set_key_value", "track_idx", "key_idx", "value"), &Animation::bezier_track_set_key_value);
4022
ClassDB::bind_method(D_METHOD("bezier_track_set_key_in_handle", "track_idx", "key_idx", "in_handle", "balanced_value_time_ratio"), &Animation::bezier_track_set_key_in_handle, DEFVAL(1.0));
4023
ClassDB::bind_method(D_METHOD("bezier_track_set_key_out_handle", "track_idx", "key_idx", "out_handle", "balanced_value_time_ratio"), &Animation::bezier_track_set_key_out_handle, DEFVAL(1.0));
4024
4025
ClassDB::bind_method(D_METHOD("bezier_track_get_key_value", "track_idx", "key_idx"), &Animation::bezier_track_get_key_value);
4026
ClassDB::bind_method(D_METHOD("bezier_track_get_key_in_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_in_handle);
4027
ClassDB::bind_method(D_METHOD("bezier_track_get_key_out_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_out_handle);
4028
4029
ClassDB::bind_method(D_METHOD("bezier_track_interpolate", "track_idx", "time"), &Animation::bezier_track_interpolate);
4030
4031
ClassDB::bind_method(D_METHOD("audio_track_insert_key", "track_idx", "time", "stream", "start_offset", "end_offset"), &Animation::audio_track_insert_key, DEFVAL(0), DEFVAL(0));
4032
ClassDB::bind_method(D_METHOD("audio_track_set_key_stream", "track_idx", "key_idx", "stream"), &Animation::audio_track_set_key_stream);
4033
ClassDB::bind_method(D_METHOD("audio_track_set_key_start_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_start_offset);
4034
ClassDB::bind_method(D_METHOD("audio_track_set_key_end_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_end_offset);
4035
ClassDB::bind_method(D_METHOD("audio_track_get_key_stream", "track_idx", "key_idx"), &Animation::audio_track_get_key_stream);
4036
ClassDB::bind_method(D_METHOD("audio_track_get_key_start_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_start_offset);
4037
ClassDB::bind_method(D_METHOD("audio_track_get_key_end_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_end_offset);
4038
ClassDB::bind_method(D_METHOD("audio_track_set_use_blend", "track_idx", "enable"), &Animation::audio_track_set_use_blend);
4039
ClassDB::bind_method(D_METHOD("audio_track_is_use_blend", "track_idx"), &Animation::audio_track_is_use_blend);
4040
4041
ClassDB::bind_method(D_METHOD("animation_track_insert_key", "track_idx", "time", "animation"), &Animation::animation_track_insert_key);
4042
ClassDB::bind_method(D_METHOD("animation_track_set_key_animation", "track_idx", "key_idx", "animation"), &Animation::animation_track_set_key_animation);
4043
ClassDB::bind_method(D_METHOD("animation_track_get_key_animation", "track_idx", "key_idx"), &Animation::animation_track_get_key_animation);
4044
4045
ClassDB::bind_method(D_METHOD("add_marker", "name", "time"), &Animation::add_marker);
4046
ClassDB::bind_method(D_METHOD("remove_marker", "name"), &Animation::remove_marker);
4047
ClassDB::bind_method(D_METHOD("has_marker", "name"), &Animation::has_marker);
4048
ClassDB::bind_method(D_METHOD("get_marker_at_time", "time"), &Animation::get_marker_at_time);
4049
ClassDB::bind_method(D_METHOD("get_next_marker", "time"), &Animation::get_next_marker);
4050
ClassDB::bind_method(D_METHOD("get_prev_marker", "time"), &Animation::get_prev_marker);
4051
ClassDB::bind_method(D_METHOD("get_marker_time", "name"), &Animation::get_marker_time);
4052
ClassDB::bind_method(D_METHOD("get_marker_names"), &Animation::get_marker_names);
4053
ClassDB::bind_method(D_METHOD("get_marker_color", "name"), &Animation::get_marker_color);
4054
ClassDB::bind_method(D_METHOD("set_marker_color", "name", "color"), &Animation::set_marker_color);
4055
4056
ClassDB::bind_method(D_METHOD("set_length", "time_sec"), &Animation::set_length);
4057
ClassDB::bind_method(D_METHOD("get_length"), &Animation::get_length);
4058
4059
ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &Animation::set_loop_mode);
4060
ClassDB::bind_method(D_METHOD("get_loop_mode"), &Animation::get_loop_mode);
4061
4062
ClassDB::bind_method(D_METHOD("set_step", "size_sec"), &Animation::set_step);
4063
ClassDB::bind_method(D_METHOD("get_step"), &Animation::get_step);
4064
4065
ClassDB::bind_method(D_METHOD("clear"), &Animation::clear);
4066
ClassDB::bind_method(D_METHOD("copy_track", "track_idx", "to_animation"), &Animation::copy_track);
4067
4068
ClassDB::bind_method(D_METHOD("optimize", "allowed_velocity_err", "allowed_angular_err", "precision"), &Animation::optimize, DEFVAL(0.01), DEFVAL(0.01), DEFVAL(3));
4069
ClassDB::bind_method(D_METHOD("compress", "page_size", "fps", "split_tolerance"), &Animation::compress, DEFVAL(8192), DEFVAL(120), DEFVAL(4.0));
4070
4071
ClassDB::bind_method(D_METHOD("is_capture_included"), &Animation::is_capture_included);
4072
4073
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001,suffix:s"), "set_length", "get_length");
4074
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode");
4075
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step", PROPERTY_HINT_RANGE, "0,4096,0.001,suffix:s"), "set_step", "get_step");
4076
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "capture_included", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "", "is_capture_included");
4077
4078
BIND_ENUM_CONSTANT(TYPE_VALUE);
4079
BIND_ENUM_CONSTANT(TYPE_POSITION_3D);
4080
BIND_ENUM_CONSTANT(TYPE_ROTATION_3D);
4081
BIND_ENUM_CONSTANT(TYPE_SCALE_3D);
4082
BIND_ENUM_CONSTANT(TYPE_BLEND_SHAPE);
4083
BIND_ENUM_CONSTANT(TYPE_METHOD);
4084
BIND_ENUM_CONSTANT(TYPE_BEZIER);
4085
BIND_ENUM_CONSTANT(TYPE_AUDIO);
4086
BIND_ENUM_CONSTANT(TYPE_ANIMATION);
4087
4088
BIND_ENUM_CONSTANT(INTERPOLATION_NEAREST);
4089
BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR);
4090
BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC);
4091
BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR_ANGLE);
4092
BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC_ANGLE);
4093
4094
BIND_ENUM_CONSTANT(UPDATE_CONTINUOUS);
4095
BIND_ENUM_CONSTANT(UPDATE_DISCRETE);
4096
BIND_ENUM_CONSTANT(UPDATE_CAPTURE);
4097
4098
BIND_ENUM_CONSTANT(LOOP_NONE);
4099
BIND_ENUM_CONSTANT(LOOP_LINEAR);
4100
BIND_ENUM_CONSTANT(LOOP_PINGPONG);
4101
4102
BIND_ENUM_CONSTANT(LOOPED_FLAG_NONE);
4103
BIND_ENUM_CONSTANT(LOOPED_FLAG_END);
4104
BIND_ENUM_CONSTANT(LOOPED_FLAG_START);
4105
4106
BIND_ENUM_CONSTANT(FIND_MODE_NEAREST);
4107
BIND_ENUM_CONSTANT(FIND_MODE_APPROX);
4108
BIND_ENUM_CONSTANT(FIND_MODE_EXACT);
4109
}
4110
4111
void Animation::clear() {
4112
for (int i = 0; i < tracks.size(); i++) {
4113
memdelete(tracks[i]);
4114
}
4115
tracks.clear();
4116
loop_mode = LOOP_NONE;
4117
length = 1;
4118
compression.enabled = false;
4119
compression.bounds.clear();
4120
compression.pages.clear();
4121
compression.fps = 120;
4122
emit_changed();
4123
}
4124
4125
bool Animation::_float_track_optimize_key(const TKey<float> t0, const TKey<float> t1, const TKey<float> t2, real_t p_allowed_velocity_err, real_t p_allowed_precision_error, bool p_is_nearest) {
4126
// Remove overlapping keys.
4127
if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
4128
return true;
4129
}
4130
if (std::abs(t0.value - t1.value) < p_allowed_precision_error && std::abs(t1.value - t2.value) < p_allowed_precision_error) {
4131
return true;
4132
}
4133
if (p_is_nearest) {
4134
return false;
4135
}
4136
// Calc velocities.
4137
double v0 = (t1.value - t0.value) / (t1.time - t0.time);
4138
double v1 = (t2.value - t1.value) / (t2.time - t1.time);
4139
// Avoid zero div but check equality.
4140
if (std::abs(v0 - v1) < p_allowed_precision_error) {
4141
return true;
4142
} else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) {
4143
return false;
4144
}
4145
if (!std::signbit(v0 * v1)) {
4146
v0 = std::abs(v0);
4147
v1 = std::abs(v1);
4148
double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
4149
if (ratio >= 1.0 - p_allowed_velocity_err) {
4150
return true;
4151
}
4152
}
4153
return false;
4154
}
4155
4156
bool Animation::_vector2_track_optimize_key(const TKey<Vector2> t0, const TKey<Vector2> t1, const TKey<Vector2> t2, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error, bool p_is_nearest) {
4157
// Remove overlapping keys.
4158
if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
4159
return true;
4160
}
4161
if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) {
4162
return true;
4163
}
4164
if (p_is_nearest) {
4165
return false;
4166
}
4167
// Calc velocities.
4168
Vector2 vc0 = (t1.value - t0.value) / (t1.time - t0.time);
4169
Vector2 vc1 = (t2.value - t1.value) / (t2.time - t1.time);
4170
double v0 = vc0.length();
4171
double v1 = vc1.length();
4172
// Avoid zero div but check equality.
4173
if (std::abs(v0 - v1) < p_allowed_precision_error) {
4174
return true;
4175
} else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) {
4176
return false;
4177
}
4178
// Check axis.
4179
if (vc0.normalized().dot(vc1.normalized()) >= 1.0 - p_allowed_angular_error * 2.0) {
4180
v0 = std::abs(v0);
4181
v1 = std::abs(v1);
4182
double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
4183
if (ratio >= 1.0 - p_allowed_velocity_err) {
4184
return true;
4185
}
4186
}
4187
return false;
4188
}
4189
4190
bool Animation::_vector3_track_optimize_key(const TKey<Vector3> t0, const TKey<Vector3> t1, const TKey<Vector3> t2, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error, bool p_is_nearest) {
4191
// Remove overlapping keys.
4192
if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
4193
return true;
4194
}
4195
if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) {
4196
return true;
4197
}
4198
if (p_is_nearest) {
4199
return false;
4200
}
4201
4202
// Calc velocities.
4203
Vector3 vc0 = (t1.value - t0.value) / (t1.time - t0.time);
4204
Vector3 vc1 = (t2.value - t1.value) / (t2.time - t1.time);
4205
double v0 = vc0.length();
4206
double v1 = vc1.length();
4207
// Avoid zero div but check equality.
4208
if (std::abs(v0 - v1) < p_allowed_precision_error) {
4209
return true;
4210
} else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) {
4211
return false;
4212
}
4213
// Check axis.
4214
if (vc0.normalized().dot(vc1.normalized()) >= 1.0 - p_allowed_angular_error * 2.0) {
4215
v0 = std::abs(v0);
4216
v1 = std::abs(v1);
4217
double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
4218
if (ratio >= 1.0 - p_allowed_velocity_err) {
4219
return true;
4220
}
4221
}
4222
return false;
4223
}
4224
4225
bool Animation::_quaternion_track_optimize_key(const TKey<Quaternion> t0, const TKey<Quaternion> t1, const TKey<Quaternion> t2, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error, bool p_is_nearest) {
4226
// Remove overlapping keys.
4227
if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) {
4228
return true;
4229
}
4230
if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) {
4231
return true;
4232
}
4233
if (p_is_nearest) {
4234
return false;
4235
}
4236
// Check axis.
4237
Quaternion q0 = t0.value * t1.value * t0.value.inverse();
4238
Quaternion q1 = t1.value * t2.value * t1.value.inverse();
4239
if (q0.get_axis().dot(q1.get_axis()) >= 1.0 - p_allowed_angular_error * 2.0) {
4240
double a0 = Math::acos(t0.value.dot(t1.value));
4241
double a1 = Math::acos(t1.value.dot(t2.value));
4242
if (a0 + a1 >= Math::PI / 2) {
4243
return false; // Rotation is more than 180 deg, keep key.
4244
}
4245
// Calc velocities.
4246
double v0 = a0 / (t1.time - t0.time);
4247
double v1 = a1 / (t2.time - t1.time);
4248
// Avoid zero div but check equality.
4249
if (std::abs(v0 - v1) < p_allowed_precision_error) {
4250
return true;
4251
} else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) {
4252
return false;
4253
}
4254
double ratio = v0 < v1 ? v0 / v1 : v1 / v0;
4255
if (ratio >= 1.0 - p_allowed_velocity_err) {
4256
return true;
4257
}
4258
}
4259
return false;
4260
}
4261
4262
void Animation::_position_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
4263
ERR_FAIL_INDEX(p_idx, tracks.size());
4264
ERR_FAIL_COND(tracks[p_idx]->type != TYPE_POSITION_3D);
4265
bool is_nearest = false;
4266
if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) {
4267
is_nearest = true;
4268
} else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) {
4269
return;
4270
}
4271
PositionTrack *tt = static_cast<PositionTrack *>(tracks[p_idx]);
4272
int i = 0;
4273
while (i < tt->positions.size() - 2) {
4274
TKey<Vector3> t0 = tt->positions[i];
4275
TKey<Vector3> t1 = tt->positions[i + 1];
4276
TKey<Vector3> t2 = tt->positions[i + 2];
4277
bool erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest);
4278
if (erase) {
4279
tt->positions.remove_at(i + 1);
4280
} else {
4281
i++;
4282
}
4283
}
4284
4285
if (tt->positions.size() == 2) {
4286
if ((tt->positions[0].value - tt->positions[1].value).length() < p_allowed_precision_error) {
4287
tt->positions.remove_at(1);
4288
}
4289
}
4290
}
4291
4292
void Animation::_rotation_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
4293
ERR_FAIL_INDEX(p_idx, tracks.size());
4294
ERR_FAIL_COND(tracks[p_idx]->type != TYPE_ROTATION_3D);
4295
bool is_nearest = false;
4296
if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) {
4297
is_nearest = true;
4298
} else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) {
4299
return;
4300
}
4301
RotationTrack *rt = static_cast<RotationTrack *>(tracks[p_idx]);
4302
int i = 0;
4303
while (i < rt->rotations.size() - 2) {
4304
TKey<Quaternion> t0 = rt->rotations[i];
4305
TKey<Quaternion> t1 = rt->rotations[i + 1];
4306
TKey<Quaternion> t2 = rt->rotations[i + 2];
4307
bool erase = _quaternion_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest);
4308
if (erase) {
4309
rt->rotations.remove_at(i + 1);
4310
} else {
4311
i++;
4312
}
4313
}
4314
4315
if (rt->rotations.size() == 2) {
4316
if ((rt->rotations[0].value - rt->rotations[1].value).length() < p_allowed_precision_error) {
4317
rt->rotations.remove_at(1);
4318
}
4319
}
4320
}
4321
4322
void Animation::_scale_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
4323
ERR_FAIL_INDEX(p_idx, tracks.size());
4324
ERR_FAIL_COND(tracks[p_idx]->type != TYPE_SCALE_3D);
4325
bool is_nearest = false;
4326
if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) {
4327
is_nearest = true;
4328
} else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) {
4329
return;
4330
}
4331
ScaleTrack *st = static_cast<ScaleTrack *>(tracks[p_idx]);
4332
int i = 0;
4333
while (i < st->scales.size() - 2) {
4334
TKey<Vector3> t0 = st->scales[i];
4335
TKey<Vector3> t1 = st->scales[i + 1];
4336
TKey<Vector3> t2 = st->scales[i + 2];
4337
bool erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest);
4338
if (erase) {
4339
st->scales.remove_at(i + 1);
4340
} else {
4341
i++;
4342
}
4343
}
4344
4345
if (st->scales.size() == 2) {
4346
if ((st->scales[0].value - st->scales[1].value).length() < p_allowed_precision_error) {
4347
st->scales.remove_at(1);
4348
}
4349
}
4350
}
4351
4352
void Animation::_blend_shape_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_precision_error) {
4353
ERR_FAIL_INDEX(p_idx, tracks.size());
4354
ERR_FAIL_COND(tracks[p_idx]->type != TYPE_BLEND_SHAPE);
4355
bool is_nearest = false;
4356
if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) {
4357
is_nearest = true;
4358
} else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) {
4359
return;
4360
}
4361
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(tracks[p_idx]);
4362
int i = 0;
4363
while (i < bst->blend_shapes.size() - 2) {
4364
TKey<float> t0 = bst->blend_shapes[i];
4365
TKey<float> t1 = bst->blend_shapes[i + 1];
4366
TKey<float> t2 = bst->blend_shapes[i + 2];
4367
4368
bool erase = _float_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_precision_error, is_nearest);
4369
if (erase) {
4370
bst->blend_shapes.remove_at(i + 1);
4371
} else {
4372
i++;
4373
}
4374
}
4375
4376
if (bst->blend_shapes.size() == 2) {
4377
if (std::abs(bst->blend_shapes[0].value - bst->blend_shapes[1].value) < p_allowed_precision_error) {
4378
bst->blend_shapes.remove_at(1);
4379
}
4380
}
4381
}
4382
4383
void Animation::_value_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error) {
4384
ERR_FAIL_INDEX(p_idx, tracks.size());
4385
ERR_FAIL_COND(tracks[p_idx]->type != TYPE_VALUE);
4386
bool is_nearest = false;
4387
if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) {
4388
is_nearest = true;
4389
} else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR && tracks[p_idx]->interpolation != INTERPOLATION_LINEAR_ANGLE) {
4390
return;
4391
}
4392
ValueTrack *vt = static_cast<ValueTrack *>(tracks[p_idx]);
4393
if (vt->values.is_empty()) {
4394
return;
4395
}
4396
Variant::Type type = vt->values[0].value.get_type();
4397
4398
// Special case for angle interpolation.
4399
bool is_using_angle = vt->interpolation == Animation::INTERPOLATION_LINEAR_ANGLE || vt->interpolation == Animation::INTERPOLATION_CUBIC_ANGLE;
4400
int i = 0;
4401
while (i < vt->values.size() - 2) {
4402
bool erase = false;
4403
switch (type) {
4404
case Variant::FLOAT: {
4405
TKey<float> t0;
4406
TKey<float> t1;
4407
TKey<float> t2;
4408
t0.time = vt->values[i].time;
4409
t1.time = vt->values[i + 1].time;
4410
t2.time = vt->values[i + 2].time;
4411
t0.value = vt->values[i].value;
4412
t1.value = vt->values[i + 1].value;
4413
t2.value = vt->values[i + 2].value;
4414
if (is_using_angle) {
4415
float diff1 = std::fmod(t1.value - t0.value, Math::TAU);
4416
t1.value = t0.value + std::fmod(2.0 * diff1, Math::TAU) - diff1;
4417
float diff2 = std::fmod(t2.value - t1.value, Math::TAU);
4418
t2.value = t1.value + std::fmod(2.0 * diff2, Math::TAU) - diff2;
4419
if (std::abs(std::abs(diff1) + std::abs(diff2)) >= Math::PI) {
4420
break; // Rotation is more than 180 deg, keep key.
4421
}
4422
}
4423
erase = _float_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_precision_error, is_nearest);
4424
} break;
4425
case Variant::VECTOR2: {
4426
TKey<Vector2> t0;
4427
TKey<Vector2> t1;
4428
TKey<Vector2> t2;
4429
t0.time = vt->values[i].time;
4430
t1.time = vt->values[i + 1].time;
4431
t2.time = vt->values[i + 2].time;
4432
t0.value = vt->values[i].value;
4433
t1.value = vt->values[i + 1].value;
4434
t2.value = vt->values[i + 2].value;
4435
erase = _vector2_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest);
4436
} break;
4437
case Variant::VECTOR3: {
4438
TKey<Vector3> t0;
4439
TKey<Vector3> t1;
4440
TKey<Vector3> t2;
4441
t0.time = vt->values[i].time;
4442
t1.time = vt->values[i + 1].time;
4443
t2.time = vt->values[i + 2].time;
4444
t0.value = vt->values[i].value;
4445
t1.value = vt->values[i + 1].value;
4446
t2.value = vt->values[i + 2].value;
4447
erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest);
4448
} break;
4449
case Variant::QUATERNION: {
4450
TKey<Quaternion> t0;
4451
TKey<Quaternion> t1;
4452
TKey<Quaternion> t2;
4453
t0.time = vt->values[i].time;
4454
t1.time = vt->values[i + 1].time;
4455
t2.time = vt->values[i + 2].time;
4456
t0.value = vt->values[i].value;
4457
t1.value = vt->values[i + 1].value;
4458
t2.value = vt->values[i + 2].value;
4459
erase = _quaternion_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest);
4460
} break;
4461
default: {
4462
} break;
4463
}
4464
4465
if (erase) {
4466
vt->values.remove_at(i + 1);
4467
} else {
4468
i++;
4469
}
4470
}
4471
4472
if (vt->values.size() == 2) {
4473
bool single_key = false;
4474
switch (type) {
4475
case Variant::FLOAT: {
4476
float val_0 = vt->values[0].value;
4477
float val_1 = vt->values[1].value;
4478
if (is_using_angle) {
4479
float diff1 = std::fmod(val_1 - val_0, Math::TAU);
4480
val_1 = val_0 + std::fmod(2.0 * diff1, Math::TAU) - diff1;
4481
}
4482
single_key = std::abs(val_0 - val_1) < p_allowed_precision_error;
4483
} break;
4484
case Variant::VECTOR2: {
4485
Vector2 val_0 = vt->values[0].value;
4486
Vector2 val_1 = vt->values[1].value;
4487
single_key = (val_0 - val_1).length() < p_allowed_precision_error;
4488
} break;
4489
case Variant::VECTOR3: {
4490
Vector3 val_0 = vt->values[0].value;
4491
Vector3 val_1 = vt->values[1].value;
4492
single_key = (val_0 - val_1).length() < p_allowed_precision_error;
4493
} break;
4494
case Variant::QUATERNION: {
4495
Quaternion val_0 = vt->values[0].value;
4496
Quaternion val_1 = vt->values[1].value;
4497
single_key = (val_0 - val_1).length() < p_allowed_precision_error;
4498
} break;
4499
default: {
4500
} break;
4501
}
4502
if (single_key) {
4503
vt->values.remove_at(1);
4504
}
4505
}
4506
}
4507
4508
void Animation::optimize(real_t p_allowed_velocity_err, real_t p_allowed_angular_err, int p_precision) {
4509
real_t precision = Math::pow(0.1, p_precision);
4510
for (int i = 0; i < tracks.size(); i++) {
4511
if (track_is_compressed(i)) {
4512
continue; //not possible to optimize compressed track
4513
}
4514
if (tracks[i]->type == TYPE_POSITION_3D) {
4515
_position_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
4516
} else if (tracks[i]->type == TYPE_ROTATION_3D) {
4517
_rotation_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
4518
} else if (tracks[i]->type == TYPE_SCALE_3D) {
4519
_scale_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
4520
} else if (tracks[i]->type == TYPE_BLEND_SHAPE) {
4521
_blend_shape_track_optimize(i, p_allowed_velocity_err, precision);
4522
} else if (tracks[i]->type == TYPE_VALUE) {
4523
_value_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision);
4524
}
4525
}
4526
}
4527
4528
#define print_animc(m_str)
4529
//#define print_animc(m_str) print_line(m_str);
4530
4531
struct AnimationCompressionDataState {
4532
enum {
4533
MIN_OPTIMIZE_PACKETS = 5,
4534
MAX_PACKETS = 16
4535
};
4536
4537
uint32_t components = 3;
4538
LocalVector<uint8_t> data; // Committed packets.
4539
struct PacketData {
4540
int32_t data[3] = { 0, 0, 0 };
4541
uint32_t frame = 0;
4542
};
4543
4544
float split_tolerance = 1.5;
4545
4546
LocalVector<PacketData> temp_packets;
4547
4548
//used for rollback if the new frame does not fit
4549
int32_t validated_packet_count = -1;
4550
4551
static int32_t _compute_delta16_signed(int32_t p_from, int32_t p_to) {
4552
int32_t delta = p_to - p_from;
4553
if (delta > 32767) {
4554
return delta - 65536; // use wrap around
4555
} else if (delta < -32768) {
4556
return 65536 + delta; // use wrap around
4557
}
4558
return delta;
4559
}
4560
4561
static uint32_t _compute_shift_bits_signed(int32_t p_delta) {
4562
if (p_delta == 0) {
4563
return 0;
4564
} else if (p_delta < 0) {
4565
p_delta = Math::abs(p_delta) - 1;
4566
if (p_delta == 0) {
4567
return 1;
4568
}
4569
}
4570
return nearest_shift((uint32_t)p_delta);
4571
}
4572
4573
void _compute_max_shifts(uint32_t p_from, uint32_t p_to, uint32_t *max_shifts, uint32_t &max_frame_delta_shift) const {
4574
for (uint32_t j = 0; j < components; j++) {
4575
max_shifts[j] = 0;
4576
}
4577
max_frame_delta_shift = 0;
4578
4579
for (uint32_t i = p_from + 1; i <= p_to; i++) {
4580
int32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame;
4581
max_frame_delta_shift = MAX(max_frame_delta_shift, nearest_shift((uint32_t)frame_delta));
4582
for (uint32_t j = 0; j < components; j++) {
4583
int32_t diff = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]);
4584
uint32_t shift = _compute_shift_bits_signed(diff);
4585
max_shifts[j] = MAX(shift, max_shifts[j]);
4586
}
4587
}
4588
}
4589
4590
bool insert_key(uint32_t p_frame, const Vector3i &p_key) {
4591
if (temp_packets.size() == MAX_PACKETS) {
4592
commit_temp_packets();
4593
}
4594
PacketData packet;
4595
packet.frame = p_frame;
4596
for (int i = 0; i < 3; i++) {
4597
ERR_FAIL_COND_V(p_key[i] > 65535, false); // Safety checks.
4598
packet.data[i] = p_key[i];
4599
}
4600
4601
temp_packets.push_back(packet);
4602
4603
if (temp_packets.size() >= MIN_OPTIMIZE_PACKETS) {
4604
uint32_t max_shifts[3] = { 0, 0, 0 }; // Base sizes, 16 bit
4605
uint32_t max_frame_delta_shift = 0;
4606
// Compute the average shift before the packet was added
4607
_compute_max_shifts(0, temp_packets.size() - 2, max_shifts, max_frame_delta_shift);
4608
4609
float prev_packet_size_avg = 0;
4610
prev_packet_size_avg = float(1 << max_frame_delta_shift);
4611
for (uint32_t i = 0; i < components; i++) {
4612
prev_packet_size_avg += float(1 << max_shifts[i]);
4613
}
4614
prev_packet_size_avg /= float(1 + components);
4615
4616
_compute_max_shifts(temp_packets.size() - 2, temp_packets.size() - 1, max_shifts, max_frame_delta_shift);
4617
4618
float new_packet_size_avg = 0;
4619
new_packet_size_avg = float(1 << max_frame_delta_shift);
4620
for (uint32_t i = 0; i < components; i++) {
4621
new_packet_size_avg += float(1 << max_shifts[i]);
4622
}
4623
new_packet_size_avg /= float(1 + components);
4624
4625
print_animc("packet count: " + rtos(temp_packets.size() - 1) + " size avg " + rtos(prev_packet_size_avg) + " new avg " + rtos(new_packet_size_avg));
4626
float ratio = (prev_packet_size_avg < new_packet_size_avg) ? (new_packet_size_avg / prev_packet_size_avg) : (prev_packet_size_avg / new_packet_size_avg);
4627
4628
if (ratio > split_tolerance) {
4629
print_animc("split!");
4630
temp_packets.resize(temp_packets.size() - 1);
4631
commit_temp_packets();
4632
temp_packets.push_back(packet);
4633
}
4634
}
4635
4636
return temp_packets.size() == 1; // First key
4637
}
4638
4639
uint32_t get_temp_packet_size() const {
4640
if (temp_packets.is_empty()) {
4641
return 0;
4642
} else if (temp_packets.size() == 1) {
4643
return components == 1 ? 4 : 8; // 1 component packet is 16 bits and 16 bits unused. 3 component packets is 48 bits and 16 bits unused
4644
}
4645
uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit
4646
uint32_t max_frame_delta_shift = 0;
4647
4648
_compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift);
4649
4650
uint32_t size_bits = 16; //base value (all 4 bits of shift sizes for x,y,z,time)
4651
size_bits += max_frame_delta_shift * (temp_packets.size() - 1); //times
4652
for (uint32_t j = 0; j < components; j++) {
4653
size_bits += 16; //base value
4654
uint32_t shift = max_shifts[j];
4655
if (shift > 0) {
4656
shift += 1; //if not zero, add sign bit
4657
}
4658
size_bits += shift * (temp_packets.size() - 1);
4659
}
4660
if (size_bits % 8 != 0) { //wrap to 8 bits
4661
size_bits += 8 - (size_bits % 8);
4662
}
4663
uint32_t size_bytes = size_bits / 8; //wrap to words
4664
if (size_bytes % 4 != 0) {
4665
size_bytes += 4 - (size_bytes % 4);
4666
}
4667
return size_bytes;
4668
}
4669
4670
static void _push_bits(LocalVector<uint8_t> &data, uint32_t &r_buffer, uint32_t &r_bits_used, uint32_t p_value, uint32_t p_bits) {
4671
r_buffer |= p_value << r_bits_used;
4672
r_bits_used += p_bits;
4673
while (r_bits_used >= 8) {
4674
uint8_t byte = r_buffer & 0xFF;
4675
data.push_back(byte);
4676
r_buffer >>= 8;
4677
r_bits_used -= 8;
4678
}
4679
}
4680
4681
void commit_temp_packets() {
4682
if (temp_packets.is_empty()) {
4683
return; // Nothing to do.
4684
}
4685
//#define DEBUG_PACKET_PUSH
4686
#ifdef DEBUG_PACKET_PUSH
4687
#ifndef _MSC_VER
4688
#warning Debugging packet push, disable this code in production to gain a bit more import performance.
4689
#endif
4690
uint32_t debug_packet_push = get_temp_packet_size();
4691
uint32_t debug_data_size = data.size();
4692
#endif
4693
// Store header
4694
4695
uint8_t header[8];
4696
uint32_t header_bytes = 0;
4697
for (uint32_t i = 0; i < components; i++) {
4698
encode_uint16(temp_packets[0].data[i], &header[header_bytes]);
4699
header_bytes += 2;
4700
}
4701
4702
uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit
4703
uint32_t max_frame_delta_shift = 0;
4704
4705
if (temp_packets.size() > 1) {
4706
_compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift);
4707
uint16_t shift_header = (max_frame_delta_shift - 1) << 12;
4708
for (uint32_t i = 0; i < components; i++) {
4709
shift_header |= max_shifts[i] << (4 * i);
4710
}
4711
4712
encode_uint16(shift_header, &header[header_bytes]);
4713
header_bytes += 2;
4714
}
4715
4716
while (header_bytes < 8 && header_bytes % 4 != 0) { // First cond needed to silence wrong GCC warning.
4717
header[header_bytes++] = 0;
4718
}
4719
4720
for (uint32_t i = 0; i < header_bytes; i++) {
4721
data.push_back(header[i]);
4722
}
4723
4724
if (temp_packets.size() == 1) {
4725
temp_packets.clear();
4726
validated_packet_count = 0;
4727
return; //only header stored, nothing else to do
4728
}
4729
4730
uint32_t bit_buffer = 0;
4731
uint32_t bits_used = 0;
4732
4733
for (uint32_t i = 1; i < temp_packets.size(); i++) {
4734
uint32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame;
4735
_push_bits(data, bit_buffer, bits_used, frame_delta, max_frame_delta_shift);
4736
4737
for (uint32_t j = 0; j < components; j++) {
4738
if (max_shifts[j] == 0) {
4739
continue; // Zero delta, do not store
4740
}
4741
int32_t delta = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]);
4742
4743
ERR_FAIL_COND(delta < -32768 || delta > 32767); // Safety check.
4744
4745
uint16_t deltau;
4746
if (delta < 0) {
4747
deltau = (Math::abs(delta) - 1) | (1 << max_shifts[j]);
4748
} else {
4749
deltau = delta;
4750
}
4751
_push_bits(data, bit_buffer, bits_used, deltau, max_shifts[j] + 1); // Include sign bit
4752
}
4753
}
4754
if (bits_used != 0) {
4755
ERR_FAIL_COND(bit_buffer > 0xFF); // Safety check.
4756
data.push_back(bit_buffer);
4757
}
4758
4759
while (data.size() % 4 != 0) {
4760
data.push_back(0); //pad to align with 4
4761
}
4762
4763
temp_packets.clear();
4764
validated_packet_count = 0;
4765
4766
#ifdef DEBUG_PACKET_PUSH
4767
ERR_FAIL_COND((data.size() - debug_data_size) != debug_packet_push);
4768
#endif
4769
}
4770
};
4771
4772
struct AnimationCompressionTimeState {
4773
struct Packet {
4774
uint32_t frame;
4775
uint32_t offset;
4776
uint32_t count;
4777
};
4778
4779
LocalVector<Packet> packets;
4780
//used for rollback
4781
int32_t key_index = 0;
4782
int32_t validated_packet_count = 0;
4783
int32_t validated_key_index = -1;
4784
bool needs_start_frame = false;
4785
};
4786
4787
Vector3i Animation::_compress_key(uint32_t p_track, const AABB &p_bounds, int32_t p_key, float p_time) {
4788
Vector3i values;
4789
TrackType tt = track_get_type(p_track);
4790
switch (tt) {
4791
case TYPE_POSITION_3D: {
4792
Vector3 pos;
4793
if (p_key >= 0) {
4794
position_track_get_key(p_track, p_key, &pos);
4795
} else {
4796
try_position_track_interpolate(p_track, p_time, &pos);
4797
}
4798
pos = (pos - p_bounds.position) / p_bounds.size;
4799
for (int j = 0; j < 3; j++) {
4800
values[j] = CLAMP(int32_t(pos[j] * 65535.0), 0, 65535);
4801
}
4802
} break;
4803
case TYPE_ROTATION_3D: {
4804
Quaternion rot;
4805
if (p_key >= 0) {
4806
rotation_track_get_key(p_track, p_key, &rot);
4807
} else {
4808
try_rotation_track_interpolate(p_track, p_time, &rot);
4809
}
4810
Vector3 axis = rot.get_axis();
4811
float angle = rot.get_angle();
4812
angle = Math::fposmod(double(angle), double(Math::PI * 2.0));
4813
Vector2 oct = axis.octahedron_encode();
4814
Vector3 rot_norm(oct.x, oct.y, angle / (Math::PI * 2.0)); // high resolution rotation in 0-1 angle.
4815
4816
for (int j = 0; j < 3; j++) {
4817
values[j] = CLAMP(int32_t(rot_norm[j] * 65535.0), 0, 65535);
4818
}
4819
} break;
4820
case TYPE_SCALE_3D: {
4821
Vector3 scale;
4822
if (p_key >= 0) {
4823
scale_track_get_key(p_track, p_key, &scale);
4824
} else {
4825
try_scale_track_interpolate(p_track, p_time, &scale);
4826
}
4827
scale = (scale - p_bounds.position) / p_bounds.size;
4828
for (int j = 0; j < 3; j++) {
4829
values[j] = CLAMP(int32_t(scale[j] * 65535.0), 0, 65535);
4830
}
4831
} break;
4832
case TYPE_BLEND_SHAPE: {
4833
float blend;
4834
if (p_key >= 0) {
4835
blend_shape_track_get_key(p_track, p_key, &blend);
4836
} else {
4837
try_blend_shape_track_interpolate(p_track, p_time, &blend);
4838
}
4839
4840
blend = (blend / float(Compression::BLEND_SHAPE_RANGE)) * 0.5 + 0.5;
4841
values[0] = CLAMP(int32_t(blend * 65535.0), 0, 65535);
4842
} break;
4843
default: {
4844
ERR_FAIL_V(Vector3i()); // Safety check.
4845
} break;
4846
}
4847
4848
return values;
4849
}
4850
4851
struct AnimationCompressionBufferBitsRead {
4852
uint32_t buffer = 0;
4853
uint32_t used = 0;
4854
const uint8_t *src_data = nullptr;
4855
4856
_FORCE_INLINE_ uint32_t read(uint32_t p_bits) {
4857
uint32_t output = 0;
4858
uint32_t written = 0;
4859
while (p_bits > 0) {
4860
if (used == 0) {
4861
used = 8;
4862
buffer = *src_data;
4863
src_data++;
4864
}
4865
uint32_t to_write = MIN(used, p_bits);
4866
output |= (buffer & ((1 << to_write) - 1)) << written;
4867
buffer >>= to_write;
4868
used -= to_write;
4869
p_bits -= to_write;
4870
written += to_write;
4871
}
4872
return output;
4873
}
4874
};
4875
4876
void Animation::compress(uint32_t p_page_size, uint32_t p_fps, float p_split_tolerance) {
4877
ERR_FAIL_COND_MSG(compression.enabled, "This animation is already compressed");
4878
4879
p_split_tolerance = CLAMP(p_split_tolerance, 1.1, 8.0);
4880
compression.pages.clear();
4881
4882
uint32_t base_page_size = 0; // Before compressing pages, compute how large the "end page" datablock is.
4883
LocalVector<uint32_t> tracks_to_compress;
4884
LocalVector<AABB> track_bounds;
4885
const uint32_t time_packet_size = 4;
4886
4887
const uint32_t track_header_size = 4 + 4 + 4; // pointer to time (4 bytes), amount of time keys (4 bytes) pointer to track data (4 bytes)
4888
4889
for (int i = 0; i < get_track_count(); i++) {
4890
TrackType type = track_get_type(i);
4891
if (type != TYPE_POSITION_3D && type != TYPE_ROTATION_3D && type != TYPE_SCALE_3D && type != TYPE_BLEND_SHAPE) {
4892
continue;
4893
}
4894
if (track_get_key_count(i) == 0) {
4895
continue; //do not compress, no keys
4896
}
4897
base_page_size += track_header_size; //pointer to beginning of each track timeline and amount of time keys
4898
base_page_size += time_packet_size; //for end of track time marker
4899
base_page_size += (type == TYPE_BLEND_SHAPE) ? 4 : 8; // at least the end of track packet (at much 8 bytes). This could be less, but have to be pessimistic.
4900
tracks_to_compress.push_back(i);
4901
4902
AABB bounds;
4903
4904
if (type == TYPE_POSITION_3D) {
4905
AABB aabb;
4906
int kcount = track_get_key_count(i);
4907
for (int j = 0; j < kcount; j++) {
4908
Vector3 pos;
4909
position_track_get_key(i, j, &pos);
4910
if (j == 0) {
4911
aabb.position = pos;
4912
} else {
4913
aabb.expand_to(pos);
4914
}
4915
}
4916
for (int j = 0; j < 3; j++) {
4917
// Can't have zero.
4918
if (aabb.size[j] < CMP_EPSILON) {
4919
aabb.size[j] = CMP_EPSILON;
4920
}
4921
}
4922
bounds = aabb;
4923
}
4924
if (type == TYPE_SCALE_3D) {
4925
AABB aabb;
4926
int kcount = track_get_key_count(i);
4927
for (int j = 0; j < kcount; j++) {
4928
Vector3 scale;
4929
scale_track_get_key(i, j, &scale);
4930
if (j == 0) {
4931
aabb.position = scale;
4932
} else {
4933
aabb.expand_to(scale);
4934
}
4935
}
4936
for (int j = 0; j < 3; j++) {
4937
// Can't have zero.
4938
if (aabb.size[j] < CMP_EPSILON) {
4939
aabb.size[j] = CMP_EPSILON;
4940
}
4941
}
4942
bounds = aabb;
4943
}
4944
4945
track_bounds.push_back(bounds);
4946
}
4947
4948
if (tracks_to_compress.is_empty()) {
4949
return; //nothing to compress
4950
}
4951
4952
print_animc("Anim Compression:");
4953
print_animc("-----------------");
4954
print_animc("Tracks to compress: " + itos(tracks_to_compress.size()));
4955
4956
uint32_t current_frame = 0;
4957
uint32_t base_page_frame = 0;
4958
double frame_len = 1.0 / double(p_fps);
4959
const uint32_t max_frames_per_page = 65536;
4960
4961
print_animc("Frame Len: " + rtos(frame_len));
4962
4963
LocalVector<AnimationCompressionDataState> data_tracks;
4964
LocalVector<AnimationCompressionTimeState> time_tracks;
4965
4966
data_tracks.resize(tracks_to_compress.size());
4967
time_tracks.resize(tracks_to_compress.size());
4968
4969
uint32_t needed_min_page_size = base_page_size;
4970
for (uint32_t i = 0; i < data_tracks.size(); i++) {
4971
data_tracks[i].split_tolerance = p_split_tolerance;
4972
if (track_get_type(tracks_to_compress[i]) == TYPE_BLEND_SHAPE) {
4973
data_tracks[i].components = 1;
4974
} else {
4975
data_tracks[i].components = 3;
4976
}
4977
needed_min_page_size += data_tracks[i].data.size() + data_tracks[i].get_temp_packet_size();
4978
}
4979
for (uint32_t i = 0; i < time_tracks.size(); i++) {
4980
needed_min_page_size += time_tracks[i].packets.size() * 4; // time packet is 32 bits
4981
}
4982
ERR_FAIL_COND_MSG(p_page_size < needed_min_page_size, "Cannot compress with the given page size");
4983
4984
while (true) {
4985
// Begin by finding the keyframe in all tracks with the time closest to the current time
4986
const uint32_t FRAME_MAX = 0xFFFFFFFF;
4987
const int32_t NO_TRACK_FOUND = -1;
4988
uint32_t best_frame = FRAME_MAX;
4989
uint32_t best_invalid_frame = FRAME_MAX;
4990
int32_t best_frame_track = NO_TRACK_FOUND; // Default is -1, which means all keyframes for this page are exhausted.
4991
bool start_frame = false;
4992
4993
for (uint32_t i = 0; i < tracks_to_compress.size(); i++) {
4994
uint32_t uncomp_track = tracks_to_compress[i];
4995
4996
if (time_tracks[i].key_index == track_get_key_count(uncomp_track)) {
4997
if (time_tracks[i].needs_start_frame) {
4998
start_frame = true;
4999
best_frame = base_page_frame;
5000
best_frame_track = i;
5001
time_tracks[i].needs_start_frame = false;
5002
break;
5003
} else {
5004
continue; // This track is exhausted (all keys were added already), don't consider.
5005
}
5006
}
5007
double key_time = track_get_key_time(uncomp_track, time_tracks[i].key_index);
5008
double result = key_time / frame_len;
5009
uint32_t key_frame = Math::fast_ftoi(result);
5010
if (time_tracks[i].needs_start_frame && key_frame > base_page_frame) {
5011
start_frame = true;
5012
best_frame = base_page_frame;
5013
best_frame_track = i;
5014
time_tracks[i].needs_start_frame = false;
5015
break;
5016
}
5017
5018
ERR_FAIL_COND(key_frame < base_page_frame); // Safety check, should never happen.
5019
5020
if (key_frame - base_page_frame >= max_frames_per_page) {
5021
// Invalid because beyond the max frames allowed per page
5022
best_invalid_frame = MIN(best_invalid_frame, key_frame);
5023
} else if (key_frame < best_frame) {
5024
best_frame = key_frame;
5025
best_frame_track = i;
5026
}
5027
}
5028
5029
print_animc("*KEY*: Current Frame: " + itos(current_frame) + " Best Frame: " + rtos(best_frame) + " Best Track: " + itos(best_frame_track) + " Start: " + String(start_frame ? "true" : "false"));
5030
5031
if (!start_frame && best_frame > current_frame) {
5032
// Any case where the current frame advanced, either because nothing was found or because something was found greater than the current one.
5033
print_animc("\tAdvance Condition.");
5034
bool rollback = false;
5035
5036
// The frame has advanced, time to validate the previous frame
5037
uint32_t current_page_size = base_page_size;
5038
for (const AnimationCompressionDataState &state : data_tracks) {
5039
uint32_t track_size = state.data.size(); // track size
5040
track_size += state.get_temp_packet_size(); // Add the temporary data
5041
if (track_size > Compression::MAX_DATA_TRACK_SIZE) {
5042
rollback = true; //track to large, time track can't point to keys any longer, because key offset is 12 bits
5043
break;
5044
}
5045
current_page_size += track_size;
5046
}
5047
for (const AnimationCompressionTimeState &state : time_tracks) {
5048
current_page_size += state.packets.size() * 4; // time packet is 32 bits
5049
}
5050
5051
if (!rollback && current_page_size > p_page_size) {
5052
rollback = true;
5053
}
5054
5055
print_animc("\tCurrent Page Size: " + itos(current_page_size) + "/" + itos(p_page_size) + " Rollback? " + String(rollback ? "YES!" : "no"));
5056
5057
if (rollback) {
5058
// Not valid any longer, so rollback and commit page
5059
5060
for (AnimationCompressionDataState &state : data_tracks) {
5061
state.temp_packets.resize(state.validated_packet_count);
5062
}
5063
for (AnimationCompressionTimeState &state : time_tracks) {
5064
state.key_index = state.validated_key_index; //rollback key
5065
state.packets.resize(state.validated_packet_count);
5066
}
5067
5068
} else {
5069
// All valid, so save rollback information
5070
for (AnimationCompressionDataState &state : data_tracks) {
5071
state.validated_packet_count = state.temp_packets.size();
5072
}
5073
for (AnimationCompressionTimeState &state : time_tracks) {
5074
state.validated_key_index = state.key_index;
5075
state.validated_packet_count = state.packets.size();
5076
}
5077
5078
// Accept this frame as the frame being processed (as long as it exists)
5079
if (best_frame != FRAME_MAX) {
5080
current_frame = best_frame;
5081
print_animc("\tValidated, New Current Frame: " + itos(current_frame));
5082
}
5083
}
5084
5085
if (rollback || best_frame == FRAME_MAX) {
5086
// Commit the page if had to rollback or if no track was found
5087
print_animc("\tCommiting page...");
5088
5089
// The end frame for the page depends entirely on whether its valid or
5090
// no more keys were found.
5091
// If not valid, then the end frame is the current frame (as this means the current frame is being rolled back
5092
// If valid, then the end frame is the next invalid one (in case more frames exist), or the current frame in case no more frames exist.
5093
uint32_t page_end_frame = (rollback || best_frame == FRAME_MAX) ? current_frame : best_invalid_frame;
5094
5095
print_animc("\tEnd Frame: " + itos(page_end_frame) + ", " + rtos(page_end_frame * frame_len) + "s");
5096
5097
// Add finalizer frames and commit pending tracks
5098
uint32_t finalizer_local_frame = page_end_frame - base_page_frame;
5099
5100
uint32_t total_page_size = 0;
5101
5102
for (uint32_t i = 0; i < data_tracks.size(); i++) {
5103
if (data_tracks[i].temp_packets.is_empty() || (data_tracks[i].temp_packets[data_tracks[i].temp_packets.size() - 1].frame) < finalizer_local_frame) {
5104
// Add finalizer frame if it makes sense
5105
Vector3i values = _compress_key(tracks_to_compress[i], track_bounds[i], -1, page_end_frame * frame_len);
5106
5107
bool first_key = data_tracks[i].insert_key(finalizer_local_frame, values);
5108
if (first_key) {
5109
AnimationCompressionTimeState::Packet p;
5110
p.count = 1;
5111
p.frame = finalizer_local_frame;
5112
p.offset = data_tracks[i].data.size();
5113
time_tracks[i].packets.push_back(p);
5114
} else {
5115
ERR_FAIL_COND(time_tracks[i].packets.is_empty());
5116
time_tracks[i].packets[time_tracks[i].packets.size() - 1].count++;
5117
}
5118
}
5119
5120
data_tracks[i].commit_temp_packets();
5121
total_page_size += data_tracks[i].data.size();
5122
total_page_size += time_tracks[i].packets.size() * 4;
5123
total_page_size += track_header_size;
5124
5125
print_animc("\tTrack " + itos(i) + " time packets: " + itos(time_tracks[i].packets.size()) + " Packet data: " + itos(data_tracks[i].data.size()));
5126
}
5127
5128
print_animc("\tTotal page Size: " + itos(total_page_size) + "/" + itos(p_page_size));
5129
5130
// Create Page
5131
Vector<uint8_t> page_data;
5132
page_data.resize(total_page_size);
5133
{
5134
uint8_t *page_ptr = page_data.ptrw();
5135
uint32_t base_offset = data_tracks.size() * track_header_size;
5136
5137
for (uint32_t i = 0; i < data_tracks.size(); i++) {
5138
encode_uint32(base_offset, page_ptr + (track_header_size * i + 0));
5139
uint16_t *key_time_ptr = (uint16_t *)(page_ptr + base_offset);
5140
for (uint32_t j = 0; j < time_tracks[i].packets.size(); j++) {
5141
key_time_ptr[j * 2 + 0] = uint16_t(time_tracks[i].packets[j].frame);
5142
uint16_t ptr = time_tracks[i].packets[j].offset / 4;
5143
ptr |= (time_tracks[i].packets[j].count - 1) << 12;
5144
key_time_ptr[j * 2 + 1] = ptr;
5145
base_offset += 4;
5146
}
5147
encode_uint32(time_tracks[i].packets.size(), page_ptr + (track_header_size * i + 4));
5148
encode_uint32(base_offset, page_ptr + (track_header_size * i + 8));
5149
memcpy(page_ptr + base_offset, data_tracks[i].data.ptr(), data_tracks[i].data.size());
5150
base_offset += data_tracks[i].data.size();
5151
5152
//reset track
5153
data_tracks[i].data.clear();
5154
data_tracks[i].temp_packets.clear();
5155
data_tracks[i].validated_packet_count = -1;
5156
5157
time_tracks[i].needs_start_frame = true; //Not required the first time, but from now on it is.
5158
time_tracks[i].packets.clear();
5159
time_tracks[i].validated_key_index = -1;
5160
time_tracks[i].validated_packet_count = 0;
5161
}
5162
}
5163
5164
Compression::Page page;
5165
page.data = page_data;
5166
page.time_offset = base_page_frame * frame_len;
5167
compression.pages.push_back(page);
5168
5169
if (!rollback && best_invalid_frame == FRAME_MAX) {
5170
break; // No more pages to add.
5171
}
5172
5173
current_frame = page_end_frame;
5174
base_page_frame = page_end_frame;
5175
5176
continue; // Start over
5177
}
5178
}
5179
5180
// A key was found for the current frame and all is ok
5181
5182
uint32_t comp_track = best_frame_track;
5183
Vector3i values;
5184
5185
if (start_frame) {
5186
// Interpolate
5187
values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], -1, base_page_frame * frame_len);
5188
} else {
5189
uint32_t key = time_tracks[comp_track].key_index;
5190
values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], key);
5191
time_tracks[comp_track].key_index++; //goto next key (but could be rolled back if beyond page size).
5192
}
5193
5194
bool first_key = data_tracks[comp_track].insert_key(best_frame - base_page_frame, values);
5195
if (first_key) {
5196
AnimationCompressionTimeState::Packet p;
5197
p.count = 1;
5198
p.frame = best_frame - base_page_frame;
5199
p.offset = data_tracks[comp_track].data.size();
5200
time_tracks[comp_track].packets.push_back(p);
5201
} else {
5202
ERR_CONTINUE(time_tracks[comp_track].packets.is_empty());
5203
time_tracks[comp_track].packets[time_tracks[comp_track].packets.size() - 1].count++;
5204
}
5205
}
5206
5207
compression.bounds = track_bounds;
5208
compression.fps = p_fps;
5209
compression.enabled = true;
5210
5211
for (uint32_t i = 0; i < tracks_to_compress.size(); i++) {
5212
Track *t = tracks[tracks_to_compress[i]];
5213
t->interpolation = INTERPOLATION_LINEAR; //only linear supported
5214
switch (t->type) {
5215
case TYPE_POSITION_3D: {
5216
PositionTrack *tt = static_cast<PositionTrack *>(t);
5217
tt->positions.clear();
5218
tt->compressed_track = i;
5219
} break;
5220
case TYPE_ROTATION_3D: {
5221
RotationTrack *rt = static_cast<RotationTrack *>(t);
5222
rt->rotations.clear();
5223
rt->compressed_track = i;
5224
} break;
5225
case TYPE_SCALE_3D: {
5226
ScaleTrack *st = static_cast<ScaleTrack *>(t);
5227
st->scales.clear();
5228
st->compressed_track = i;
5229
print_line("Scale Bounds " + itos(i) + ": " + String(track_bounds[i]));
5230
} break;
5231
case TYPE_BLEND_SHAPE: {
5232
BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t);
5233
bst->blend_shapes.clear();
5234
bst->compressed_track = i;
5235
} break;
5236
default: {
5237
}
5238
}
5239
}
5240
#if 1
5241
uint32_t orig_size = 0;
5242
for (int i = 0; i < get_track_count(); i++) {
5243
switch (track_get_type(i)) {
5244
case TYPE_SCALE_3D:
5245
case TYPE_POSITION_3D: {
5246
orig_size += sizeof(TKey<Vector3>) * track_get_key_count(i);
5247
} break;
5248
case TYPE_ROTATION_3D: {
5249
orig_size += sizeof(TKey<Quaternion>) * track_get_key_count(i);
5250
} break;
5251
case TYPE_BLEND_SHAPE: {
5252
orig_size += sizeof(TKey<float>) * track_get_key_count(i);
5253
} break;
5254
default: {
5255
}
5256
}
5257
}
5258
5259
uint32_t new_size = 0;
5260
for (const Compression::Page &page : compression.pages) {
5261
new_size += page.data.size();
5262
}
5263
5264
print_line("Original size: " + itos(orig_size) + " - Compressed size: " + itos(new_size) + " " + String::num(float(new_size) / float(orig_size) * 100, 2) + "% pages: " + itos(compression.pages.size()));
5265
#endif
5266
}
5267
5268
bool Animation::_rotation_interpolate_compressed(uint32_t p_compressed_track, double p_time, Quaternion &r_ret) const {
5269
Vector3i current;
5270
Vector3i next;
5271
double time_current;
5272
double time_next;
5273
5274
if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) {
5275
return false; //some sort of problem
5276
}
5277
5278
if (time_current >= p_time || time_current == time_next) {
5279
r_ret = _uncompress_quaternion(current);
5280
} else if (p_time >= time_next) {
5281
r_ret = _uncompress_quaternion(next);
5282
} else {
5283
double c = (p_time - time_current) / (time_next - time_current);
5284
Quaternion from = _uncompress_quaternion(current);
5285
Quaternion to = _uncompress_quaternion(next);
5286
r_ret = from.slerp(to, c);
5287
}
5288
5289
return true;
5290
}
5291
5292
bool Animation::_pos_scale_interpolate_compressed(uint32_t p_compressed_track, double p_time, Vector3 &r_ret) const {
5293
Vector3i current;
5294
Vector3i next;
5295
double time_current;
5296
double time_next;
5297
5298
if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) {
5299
return false; //some sort of problem
5300
}
5301
5302
if (time_current >= p_time || time_current == time_next) {
5303
r_ret = _uncompress_pos_scale(p_compressed_track, current);
5304
} else if (p_time >= time_next) {
5305
r_ret = _uncompress_pos_scale(p_compressed_track, next);
5306
} else {
5307
double c = (p_time - time_current) / (time_next - time_current);
5308
Vector3 from = _uncompress_pos_scale(p_compressed_track, current);
5309
Vector3 to = _uncompress_pos_scale(p_compressed_track, next);
5310
r_ret = from.lerp(to, c);
5311
}
5312
5313
return true;
5314
}
5315
bool Animation::_blend_shape_interpolate_compressed(uint32_t p_compressed_track, double p_time, float &r_ret) const {
5316
Vector3i current;
5317
Vector3i next;
5318
double time_current;
5319
double time_next;
5320
5321
if (!_fetch_compressed<1>(p_compressed_track, p_time, current, time_current, next, time_next)) {
5322
return false; //some sort of problem
5323
}
5324
5325
if (time_current >= p_time || time_current == time_next) {
5326
r_ret = _uncompress_blend_shape(current);
5327
} else if (p_time >= time_next) {
5328
r_ret = _uncompress_blend_shape(next);
5329
} else {
5330
float c = (p_time - time_current) / (time_next - time_current);
5331
float from = _uncompress_blend_shape(current);
5332
float to = _uncompress_blend_shape(next);
5333
r_ret = Math::lerp(from, to, c);
5334
}
5335
5336
return true;
5337
}
5338
5339
template <uint32_t COMPONENTS>
5340
bool Animation::_fetch_compressed(uint32_t p_compressed_track, double p_time, Vector3i &r_current_value, double &r_current_time, Vector3i &r_next_value, double &r_next_time, uint32_t *key_index) const {
5341
ERR_FAIL_COND_V(!compression.enabled, false);
5342
ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false);
5343
p_time = CLAMP(p_time, 0, length);
5344
if (key_index) {
5345
*key_index = 0;
5346
}
5347
5348
double frame_to_sec = 1.0 / double(compression.fps);
5349
5350
int32_t page_index = -1;
5351
for (uint32_t i = 0; i < compression.pages.size(); i++) {
5352
if (compression.pages[i].time_offset > p_time) {
5353
break;
5354
}
5355
page_index = i;
5356
}
5357
5358
ERR_FAIL_COND_V(page_index == -1, false); //should not happen
5359
5360
double page_base_time = compression.pages[page_index].time_offset;
5361
const uint8_t *page_data = compression.pages[page_index].data.ptr();
5362
// Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
5363
const uint32_t *indices = (const uint32_t *)page_data;
5364
const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
5365
uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
5366
5367
int32_t packet_idx = 0;
5368
double packet_time = double(time_keys[0]) * frame_to_sec + page_base_time;
5369
uint32_t base_frame = time_keys[0];
5370
5371
for (uint32_t i = 1; i < time_key_count; i++) {
5372
uint32_t f = time_keys[i * 2 + 0];
5373
double frame_time = double(f) * frame_to_sec + page_base_time;
5374
5375
if (frame_time > p_time) {
5376
break;
5377
}
5378
5379
if (key_index) {
5380
(*key_index) += (time_keys[(i - 1) * 2 + 1] >> 12) + 1;
5381
}
5382
5383
packet_idx = i;
5384
packet_time = frame_time;
5385
base_frame = f;
5386
}
5387
5388
const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]];
5389
5390
uint16_t time_key_data = time_keys[packet_idx * 2 + 1];
5391
uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits
5392
uint32_t data_count = (time_key_data >> 12) + 1;
5393
5394
const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset);
5395
5396
uint16_t decode[COMPONENTS];
5397
uint16_t decode_next[COMPONENTS];
5398
5399
for (uint32_t i = 0; i < COMPONENTS; i++) {
5400
decode[i] = data_key[i];
5401
decode_next[i] = data_key[i];
5402
}
5403
5404
double next_time = packet_time;
5405
5406
if (p_time > packet_time) { // If its equal or less, then don't bother
5407
if (data_count > 1) {
5408
//decode forward
5409
uint32_t bit_width[COMPONENTS];
5410
for (uint32_t i = 0; i < COMPONENTS; i++) {
5411
bit_width[i] = (data_key[COMPONENTS] >> (i * 4)) & 0xF;
5412
}
5413
5414
uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1;
5415
5416
AnimationCompressionBufferBitsRead buffer;
5417
5418
buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1];
5419
5420
for (uint32_t i = 1; i < data_count; i++) {
5421
uint32_t frame_delta = buffer.read(frame_bit_width);
5422
base_frame += frame_delta;
5423
5424
for (uint32_t j = 0; j < COMPONENTS; j++) {
5425
if (bit_width[j] == 0) {
5426
continue; // do none
5427
}
5428
uint32_t valueu = buffer.read(bit_width[j] + 1);
5429
bool sign = valueu & (1 << bit_width[j]);
5430
int16_t value = valueu & ((1 << bit_width[j]) - 1);
5431
if (sign) {
5432
value = -value - 1;
5433
}
5434
5435
decode_next[j] += value;
5436
}
5437
5438
next_time = double(base_frame) * frame_to_sec + page_base_time;
5439
if (p_time < next_time) {
5440
break;
5441
}
5442
5443
packet_time = next_time;
5444
5445
for (uint32_t j = 0; j < COMPONENTS; j++) {
5446
decode[j] = decode_next[j];
5447
}
5448
5449
if (key_index) {
5450
(*key_index)++;
5451
}
5452
}
5453
}
5454
5455
if (p_time > next_time) { // > instead of >= because if its equal, then it will be properly interpolated anyway
5456
// So, the last frame found still has a time that is less than the required frame,
5457
// will have to interpolate with the first frame of the next timekey.
5458
5459
if ((uint32_t)packet_idx < time_key_count - 1) { // Safety check but should not matter much, otherwise current next packet is last packet.
5460
5461
uint16_t time_key_data_next = time_keys[(packet_idx + 1) * 2 + 1];
5462
uint32_t data_offset_next = (time_key_data_next & 0xFFF) * 4; // Lower 12 bits
5463
5464
const uint16_t *data_key_next = (const uint16_t *)(data_keys_base + data_offset_next);
5465
base_frame = time_keys[(packet_idx + 1) * 2 + 0];
5466
next_time = double(base_frame) * frame_to_sec + page_base_time;
5467
for (uint32_t i = 0; i < COMPONENTS; i++) {
5468
decode_next[i] = data_key_next[i];
5469
}
5470
}
5471
}
5472
}
5473
5474
r_current_time = packet_time;
5475
r_next_time = next_time;
5476
5477
for (uint32_t i = 0; i < COMPONENTS; i++) {
5478
r_current_value[i] = decode[i];
5479
r_next_value[i] = decode_next[i];
5480
}
5481
5482
return true;
5483
}
5484
5485
template <uint32_t COMPONENTS>
5486
void Animation::_get_compressed_key_indices_in_range(uint32_t p_compressed_track, double p_time, double p_delta, List<int> *r_indices) const {
5487
ERR_FAIL_COND(!compression.enabled);
5488
ERR_FAIL_UNSIGNED_INDEX(p_compressed_track, compression.bounds.size());
5489
5490
double frame_to_sec = 1.0 / double(compression.fps);
5491
uint32_t key_index = 0;
5492
5493
for (uint32_t p = 0; p < compression.pages.size(); p++) {
5494
if (compression.pages[p].time_offset >= p_time + p_delta) {
5495
// Page beyond range
5496
return;
5497
}
5498
5499
// Page within range
5500
5501
uint32_t page_index = p;
5502
5503
double page_base_time = compression.pages[page_index].time_offset;
5504
const uint8_t *page_data = compression.pages[page_index].data.ptr();
5505
// Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
5506
const uint32_t *indices = (const uint32_t *)page_data;
5507
const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
5508
uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
5509
5510
for (uint32_t i = 0; i < time_key_count; i++) {
5511
uint32_t f = time_keys[i * 2 + 0];
5512
double frame_time = f * frame_to_sec + page_base_time;
5513
if (frame_time >= p_time + p_delta) {
5514
return;
5515
} else if (frame_time >= p_time) {
5516
r_indices->push_back(key_index);
5517
}
5518
5519
key_index++;
5520
5521
const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]];
5522
5523
uint16_t time_key_data = time_keys[i * 2 + 1];
5524
uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits
5525
uint32_t data_count = (time_key_data >> 12) + 1;
5526
5527
const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset);
5528
5529
if (data_count > 1) {
5530
//decode forward
5531
uint32_t bit_width[COMPONENTS];
5532
for (uint32_t j = 0; j < COMPONENTS; j++) {
5533
bit_width[j] = (data_key[COMPONENTS] >> (j * 4)) & 0xF;
5534
}
5535
5536
uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1;
5537
5538
AnimationCompressionBufferBitsRead buffer;
5539
5540
buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1];
5541
5542
for (uint32_t j = 1; j < data_count; j++) {
5543
uint32_t frame_delta = buffer.read(frame_bit_width);
5544
f += frame_delta;
5545
5546
frame_time = f * frame_to_sec + page_base_time;
5547
if (frame_time >= p_time + p_delta) {
5548
return;
5549
} else if (frame_time >= p_time) {
5550
r_indices->push_back(key_index);
5551
}
5552
5553
for (uint32_t k = 0; k < COMPONENTS; k++) {
5554
if (bit_width[k] == 0) {
5555
continue; // do none
5556
}
5557
buffer.read(bit_width[k] + 1); // skip
5558
}
5559
5560
key_index++;
5561
}
5562
}
5563
}
5564
}
5565
}
5566
5567
int Animation::_get_compressed_key_count(uint32_t p_compressed_track) const {
5568
ERR_FAIL_COND_V(!compression.enabled, -1);
5569
ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), -1);
5570
5571
int key_count = 0;
5572
5573
for (const Compression::Page &page : compression.pages) {
5574
const uint8_t *page_data = page.data.ptr();
5575
// Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
5576
const uint32_t *indices = (const uint32_t *)page_data;
5577
const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
5578
uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
5579
5580
for (uint32_t j = 0; j < time_key_count; j++) {
5581
key_count += (time_keys[j * 2 + 1] >> 12) + 1;
5582
}
5583
}
5584
5585
return key_count;
5586
}
5587
5588
Quaternion Animation::_uncompress_quaternion(const Vector3i &p_value) const {
5589
Vector3 axis = Vector3::octahedron_decode(Vector2(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0));
5590
float angle = (float(p_value.z) / 65535.0) * 2.0 * Math::PI;
5591
return Quaternion(axis, angle);
5592
}
5593
Vector3 Animation::_uncompress_pos_scale(uint32_t p_compressed_track, const Vector3i &p_value) const {
5594
Vector3 pos_norm(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0, float(p_value.z) / 65535.0);
5595
return compression.bounds[p_compressed_track].position + pos_norm * compression.bounds[p_compressed_track].size;
5596
}
5597
float Animation::_uncompress_blend_shape(const Vector3i &p_value) const {
5598
float bsn = float(p_value.x) / 65535.0;
5599
return (bsn * 2.0 - 1.0) * float(Compression::BLEND_SHAPE_RANGE);
5600
}
5601
5602
template <uint32_t COMPONENTS>
5603
bool Animation::_fetch_compressed_by_index(uint32_t p_compressed_track, int p_index, Vector3i &r_value, double &r_time) const {
5604
ERR_FAIL_COND_V(!compression.enabled, false);
5605
ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false);
5606
5607
for (const Compression::Page &page : compression.pages) {
5608
const uint8_t *page_data = page.data.ptr();
5609
// Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported.
5610
const uint32_t *indices = (const uint32_t *)page_data;
5611
const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]];
5612
uint32_t time_key_count = indices[p_compressed_track * 3 + 1];
5613
const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]];
5614
5615
for (uint32_t j = 0; j < time_key_count; j++) {
5616
uint32_t subkeys = (time_keys[j * 2 + 1] >> 12) + 1;
5617
if ((uint32_t)p_index < subkeys) {
5618
uint16_t data_offset = (time_keys[j * 2 + 1] & 0xFFF) * 4;
5619
5620
const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset);
5621
5622
uint16_t frame = time_keys[j * 2 + 0];
5623
uint16_t decode[COMPONENTS];
5624
5625
for (uint32_t k = 0; k < COMPONENTS; k++) {
5626
decode[k] = data_key[k];
5627
}
5628
5629
if (p_index > 0) {
5630
uint32_t bit_width[COMPONENTS];
5631
for (uint32_t k = 0; k < COMPONENTS; k++) {
5632
bit_width[k] = (data_key[COMPONENTS] >> (k * 4)) & 0xF;
5633
}
5634
uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1;
5635
5636
AnimationCompressionBufferBitsRead buffer;
5637
buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1];
5638
5639
for (int k = 0; k < p_index; k++) {
5640
uint32_t frame_delta = buffer.read(frame_bit_width);
5641
frame += frame_delta;
5642
for (uint32_t l = 0; l < COMPONENTS; l++) {
5643
if (bit_width[l] == 0) {
5644
continue; // do none
5645
}
5646
uint32_t valueu = buffer.read(bit_width[l] + 1);
5647
bool sign = valueu & (1 << bit_width[l]);
5648
int16_t value = valueu & ((1 << bit_width[l]) - 1);
5649
if (sign) {
5650
value = -value - 1;
5651
}
5652
5653
decode[l] += value;
5654
}
5655
}
5656
}
5657
5658
r_time = page.time_offset + double(frame) / double(compression.fps);
5659
for (uint32_t l = 0; l < COMPONENTS; l++) {
5660
r_value[l] = decode[l];
5661
}
5662
5663
return true;
5664
5665
} else {
5666
p_index -= subkeys;
5667
}
5668
}
5669
}
5670
5671
return false;
5672
}
5673
5674
// Helper math functions for Variant.
5675
bool Animation::is_variant_interpolatable(const Variant p_value) {
5676
Variant::Type type = p_value.get_type();
5677
return (type >= Variant::BOOL && type <= Variant::STRING_NAME) || type == Variant::ARRAY || type >= Variant::PACKED_INT32_ARRAY; // PackedByteArray is unsigned, so it would be better to ignore since blending uses float.
5678
}
5679
5680
bool Animation::validate_type_match(const Variant &p_from, Variant &r_to) {
5681
if (p_from.get_type() != r_to.get_type()) {
5682
// Cast r_to between double and int to avoid minor annoyances.
5683
if (p_from.get_type() == Variant::FLOAT && r_to.get_type() == Variant::INT) {
5684
r_to = double(r_to);
5685
} else if (p_from.get_type() == Variant::INT && r_to.get_type() == Variant::FLOAT) {
5686
r_to = int(r_to);
5687
} else {
5688
ERR_FAIL_V_MSG(false, "Type mismatch between initial and final value: " + Variant::get_type_name(p_from.get_type()) + " and " + Variant::get_type_name(r_to.get_type()));
5689
}
5690
}
5691
return true;
5692
}
5693
5694
Variant Animation::cast_to_blendwise(const Variant p_value) {
5695
switch (p_value.get_type()) {
5696
case Variant::BOOL:
5697
case Variant::INT: {
5698
return p_value.operator double();
5699
} break;
5700
case Variant::STRING:
5701
case Variant::STRING_NAME: {
5702
return string_to_array(p_value);
5703
} break;
5704
case Variant::RECT2I: {
5705
return p_value.operator Rect2();
5706
} break;
5707
case Variant::VECTOR2I: {
5708
return p_value.operator Vector2();
5709
} break;
5710
case Variant::VECTOR3I: {
5711
return p_value.operator Vector3();
5712
} break;
5713
case Variant::VECTOR4I: {
5714
return p_value.operator Vector4();
5715
} break;
5716
case Variant::PACKED_INT32_ARRAY: {
5717
return p_value.operator PackedFloat32Array();
5718
} break;
5719
case Variant::PACKED_INT64_ARRAY: {
5720
return p_value.operator PackedFloat64Array();
5721
} break;
5722
default: {
5723
} break;
5724
}
5725
return p_value;
5726
}
5727
5728
Variant Animation::cast_from_blendwise(const Variant p_value, const Variant::Type p_type) {
5729
switch (p_type) {
5730
case Variant::BOOL: {
5731
return p_value.operator real_t() >= 0.5;
5732
} break;
5733
case Variant::INT: {
5734
return (int64_t)Math::round(p_value.operator double());
5735
} break;
5736
case Variant::STRING: {
5737
return array_to_string(p_value);
5738
} break;
5739
case Variant::STRING_NAME: {
5740
return StringName(array_to_string(p_value));
5741
} break;
5742
case Variant::RECT2I: {
5743
return Rect2i(p_value.operator Rect2().round());
5744
} break;
5745
case Variant::VECTOR2I: {
5746
return Vector2i(p_value.operator Vector2().round());
5747
} break;
5748
case Variant::VECTOR3I: {
5749
return Vector3i(p_value.operator Vector3().round());
5750
} break;
5751
case Variant::VECTOR4I: {
5752
return Vector4i(p_value.operator Vector4().round());
5753
} break;
5754
case Variant::PACKED_INT32_ARRAY: {
5755
PackedFloat32Array old_val = p_value.operator PackedFloat32Array();
5756
PackedInt32Array new_val;
5757
new_val.resize(old_val.size());
5758
int *new_val_w = new_val.ptrw();
5759
for (int i = 0; i < old_val.size(); i++) {
5760
new_val_w[i] = (int32_t)Math::round(old_val[i]);
5761
}
5762
return new_val;
5763
} break;
5764
case Variant::PACKED_INT64_ARRAY: {
5765
PackedFloat64Array old_val = p_value.operator PackedFloat64Array();
5766
PackedInt64Array new_val;
5767
for (int i = 0; i < old_val.size(); i++) {
5768
new_val.push_back((int64_t)Math::round(old_val[i]));
5769
}
5770
return new_val;
5771
} break;
5772
default: {
5773
} break;
5774
}
5775
return p_value;
5776
}
5777
5778
Variant Animation::string_to_array(const Variant p_value) {
5779
if (!p_value.is_string()) {
5780
return p_value;
5781
};
5782
const String &str = p_value.operator String();
5783
PackedFloat32Array arr;
5784
for (int i = 0; i < str.length(); i++) {
5785
arr.push_back((float)str[i]);
5786
}
5787
return arr;
5788
}
5789
5790
Variant Animation::array_to_string(const Variant p_value) {
5791
if (!p_value.is_array()) {
5792
return p_value;
5793
};
5794
const PackedFloat32Array &arr = p_value.operator PackedFloat32Array();
5795
String str;
5796
for (int i = 0; i < arr.size(); i++) {
5797
char32_t c = (char32_t)Math::round(arr[i]);
5798
if (c == 0 || (c & 0xfffff800) == 0xd800 || c > 0x10ffff) {
5799
c = ' ';
5800
}
5801
str += c;
5802
}
5803
return str;
5804
}
5805
5806
Variant Animation::add_variant(const Variant &a, const Variant &b) {
5807
if (a.get_type() != b.get_type()) {
5808
if (a.is_num() && b.is_num()) {
5809
return add_variant(cast_to_blendwise(a), cast_to_blendwise(b));
5810
} else if (!a.is_array()) {
5811
return a;
5812
}
5813
}
5814
5815
switch (a.get_type()) {
5816
case Variant::NIL: {
5817
return Variant();
5818
} break;
5819
case Variant::FLOAT: {
5820
return (a.operator double()) + (b.operator double());
5821
} break;
5822
case Variant::RECT2: {
5823
const Rect2 ra = a.operator Rect2();
5824
const Rect2 rb = b.operator Rect2();
5825
return Rect2(ra.position + rb.position, ra.size + rb.size);
5826
} break;
5827
case Variant::PLANE: {
5828
const Plane pa = a.operator Plane();
5829
const Plane pb = b.operator Plane();
5830
return Plane(pa.normal + pb.normal, pa.d + pb.d);
5831
} break;
5832
case Variant::AABB: {
5833
const ::AABB aa = a.operator ::AABB();
5834
const ::AABB ab = b.operator ::AABB();
5835
return ::AABB(aa.position + ab.position, aa.size + ab.size);
5836
} break;
5837
case Variant::BASIS: {
5838
return (a.operator Basis()) * (b.operator Basis());
5839
} break;
5840
case Variant::QUATERNION: {
5841
return (a.operator Quaternion()) * (b.operator Quaternion());
5842
} break;
5843
case Variant::TRANSFORM2D: {
5844
return (a.operator Transform2D()) * (b.operator Transform2D());
5845
} break;
5846
case Variant::TRANSFORM3D: {
5847
return (a.operator Transform3D()) * (b.operator Transform3D());
5848
} break;
5849
case Variant::INT:
5850
case Variant::RECT2I:
5851
case Variant::VECTOR2I:
5852
case Variant::VECTOR3I:
5853
case Variant::VECTOR4I:
5854
case Variant::PACKED_INT32_ARRAY:
5855
case Variant::PACKED_INT64_ARRAY: {
5856
// Fallback the interpolatable value which needs casting.
5857
return cast_from_blendwise(add_variant(cast_to_blendwise(a), cast_to_blendwise(b)), a.get_type());
5858
} break;
5859
case Variant::BOOL:
5860
case Variant::STRING:
5861
case Variant::STRING_NAME: {
5862
// Specialized for Tween.
5863
return b;
5864
} break;
5865
case Variant::PACKED_BYTE_ARRAY: {
5866
// Skip.
5867
} break;
5868
default: {
5869
if (a.is_array()) {
5870
const Array arr_a = a.operator Array();
5871
const Array arr_b = b.operator Array();
5872
5873
int min_size = arr_a.size();
5874
int max_size = arr_b.size();
5875
bool is_a_larger = inform_variant_array(min_size, max_size);
5876
5877
Array result;
5878
result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
5879
result.resize(min_size);
5880
int i = 0;
5881
for (; i < min_size; i++) {
5882
result[i] = add_variant(arr_a[i], arr_b[i]);
5883
}
5884
if (min_size != max_size) {
5885
// Process with last element of the lesser array.
5886
// This is pretty funny and bizarre, but artists like to use it for polygon animation.
5887
Variant lesser_last;
5888
result.resize(max_size);
5889
if (is_a_larger) {
5890
if (i > 0) {
5891
lesser_last = arr_b[i - 1];
5892
} else {
5893
Variant vz = arr_a[i];
5894
vz.zero();
5895
lesser_last = vz;
5896
}
5897
for (; i < max_size; i++) {
5898
result[i] = add_variant(arr_a[i], lesser_last);
5899
}
5900
} else {
5901
if (i > 0) {
5902
lesser_last = arr_a[i - 1];
5903
} else {
5904
Variant vz = arr_b[i];
5905
vz.zero();
5906
lesser_last = vz;
5907
}
5908
for (; i < max_size; i++) {
5909
result[i] = add_variant(lesser_last, arr_b[i]);
5910
}
5911
}
5912
}
5913
return result;
5914
}
5915
} break;
5916
}
5917
return Variant::evaluate(Variant::OP_ADD, a, b);
5918
}
5919
5920
Variant Animation::subtract_variant(const Variant &a, const Variant &b) {
5921
if (a.get_type() != b.get_type()) {
5922
if (a.is_num() && b.is_num()) {
5923
return subtract_variant(cast_to_blendwise(a), cast_to_blendwise(b));
5924
} else if (!a.is_array()) {
5925
return a;
5926
}
5927
}
5928
5929
switch (a.get_type()) {
5930
case Variant::NIL: {
5931
return Variant();
5932
} break;
5933
case Variant::FLOAT: {
5934
return (a.operator double()) - (b.operator double());
5935
} break;
5936
case Variant::RECT2: {
5937
const Rect2 ra = a.operator Rect2();
5938
const Rect2 rb = b.operator Rect2();
5939
return Rect2(ra.position - rb.position, ra.size - rb.size);
5940
} break;
5941
case Variant::PLANE: {
5942
const Plane pa = a.operator Plane();
5943
const Plane pb = b.operator Plane();
5944
return Plane(pa.normal - pb.normal, pa.d - pb.d);
5945
} break;
5946
case Variant::AABB: {
5947
const ::AABB aa = a.operator ::AABB();
5948
const ::AABB ab = b.operator ::AABB();
5949
return ::AABB(aa.position - ab.position, aa.size - ab.size);
5950
} break;
5951
case Variant::BASIS: {
5952
return (b.operator Basis()).inverse() * (a.operator Basis());
5953
} break;
5954
case Variant::QUATERNION: {
5955
return (b.operator Quaternion()).inverse() * (a.operator Quaternion());
5956
} break;
5957
case Variant::TRANSFORM2D: {
5958
return (b.operator Transform2D()).affine_inverse() * (a.operator Transform2D());
5959
} break;
5960
case Variant::TRANSFORM3D: {
5961
return (b.operator Transform3D()).affine_inverse() * (a.operator Transform3D());
5962
} break;
5963
case Variant::INT:
5964
case Variant::RECT2I:
5965
case Variant::VECTOR2I:
5966
case Variant::VECTOR3I:
5967
case Variant::VECTOR4I:
5968
case Variant::PACKED_INT32_ARRAY:
5969
case Variant::PACKED_INT64_ARRAY: {
5970
// Fallback the interpolatable value which needs casting.
5971
return cast_from_blendwise(subtract_variant(cast_to_blendwise(a), cast_to_blendwise(b)), a.get_type());
5972
} break;
5973
case Variant::BOOL:
5974
case Variant::STRING:
5975
case Variant::STRING_NAME: {
5976
// Specialized for Tween.
5977
return a;
5978
} break;
5979
case Variant::PACKED_BYTE_ARRAY: {
5980
// Skip.
5981
} break;
5982
default: {
5983
if (a.is_array()) {
5984
const Array arr_a = a.operator Array();
5985
const Array arr_b = b.operator Array();
5986
5987
int min_size = arr_a.size();
5988
int max_size = arr_b.size();
5989
bool is_a_larger = inform_variant_array(min_size, max_size);
5990
5991
Array result;
5992
result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
5993
result.resize(min_size);
5994
int i = 0;
5995
for (; i < min_size; i++) {
5996
result[i] = subtract_variant(arr_a[i], arr_b[i]);
5997
}
5998
if (min_size != max_size) {
5999
// Process with last element of the lesser array.
6000
// This is pretty funny and bizarre, but artists like to use it for polygon animation.
6001
Variant lesser_last;
6002
result.resize(max_size);
6003
if (is_a_larger) {
6004
if (i > 0) {
6005
lesser_last = arr_b[i - 1];
6006
} else {
6007
Variant vz = arr_a[i];
6008
vz.zero();
6009
lesser_last = vz;
6010
}
6011
for (; i < max_size; i++) {
6012
result[i] = subtract_variant(arr_a[i], lesser_last);
6013
}
6014
} else {
6015
if (i > 0) {
6016
lesser_last = arr_a[i - 1];
6017
} else {
6018
Variant vz = arr_b[i];
6019
vz.zero();
6020
lesser_last = vz;
6021
}
6022
for (; i < max_size; i++) {
6023
result[i] = subtract_variant(lesser_last, arr_b[i]);
6024
}
6025
}
6026
}
6027
return result;
6028
}
6029
} break;
6030
}
6031
return Variant::evaluate(Variant::OP_SUBTRACT, a, b);
6032
}
6033
6034
Variant Animation::blend_variant(const Variant &a, const Variant &b, float c) {
6035
if (a.get_type() != b.get_type()) {
6036
if (a.is_num() && b.is_num()) {
6037
return blend_variant(cast_to_blendwise(a), cast_to_blendwise(b), c);
6038
} else if (!a.is_array()) {
6039
return a;
6040
}
6041
}
6042
6043
switch (a.get_type()) {
6044
case Variant::NIL: {
6045
return Variant();
6046
} break;
6047
case Variant::FLOAT: {
6048
return (a.operator double()) + (b.operator double()) * c;
6049
} break;
6050
case Variant::VECTOR2: {
6051
return (a.operator Vector2()) + (b.operator Vector2()) * c;
6052
} break;
6053
case Variant::RECT2: {
6054
const Rect2 ra = a.operator Rect2();
6055
const Rect2 rb = b.operator Rect2();
6056
return Rect2(ra.position + rb.position * c, ra.size + rb.size * c);
6057
} break;
6058
case Variant::VECTOR3: {
6059
return (a.operator Vector3()) + (b.operator Vector3()) * c;
6060
} break;
6061
case Variant::VECTOR4: {
6062
return (a.operator Vector4()) + (b.operator Vector4()) * c;
6063
} break;
6064
case Variant::PLANE: {
6065
const Plane pa = a.operator Plane();
6066
const Plane pb = b.operator Plane();
6067
return Plane(pa.normal + pb.normal * c, pa.d + pb.d * c);
6068
} break;
6069
case Variant::COLOR: {
6070
return (a.operator Color()) + (b.operator Color()) * c;
6071
} break;
6072
case Variant::AABB: {
6073
const ::AABB aa = a.operator ::AABB();
6074
const ::AABB ab = b.operator ::AABB();
6075
return ::AABB(aa.position + ab.position * c, aa.size + ab.size * c);
6076
} break;
6077
case Variant::BASIS: {
6078
return (a.operator Basis()) + (b.operator Basis()) * c;
6079
} break;
6080
case Variant::QUATERNION: {
6081
return (a.operator Quaternion()) * Quaternion().slerp((b.operator Quaternion()), c);
6082
} break;
6083
case Variant::TRANSFORM2D: {
6084
return (a.operator Transform2D()) * Transform2D().interpolate_with((b.operator Transform2D()), c);
6085
} break;
6086
case Variant::TRANSFORM3D: {
6087
return (a.operator Transform3D()) * Transform3D().interpolate_with((b.operator Transform3D()), c);
6088
} break;
6089
case Variant::BOOL:
6090
case Variant::INT:
6091
case Variant::RECT2I:
6092
case Variant::VECTOR2I:
6093
case Variant::VECTOR3I:
6094
case Variant::VECTOR4I:
6095
case Variant::PACKED_INT32_ARRAY:
6096
case Variant::PACKED_INT64_ARRAY: {
6097
// Fallback the interpolatable value which needs casting.
6098
return cast_from_blendwise(blend_variant(cast_to_blendwise(a), cast_to_blendwise(b), c), a.get_type());
6099
} break;
6100
case Variant::STRING:
6101
case Variant::STRING_NAME: {
6102
Array arr_a = cast_to_blendwise(a);
6103
Array arr_b = cast_to_blendwise(b);
6104
int min_size = arr_a.size();
6105
int max_size = arr_b.size();
6106
bool is_a_larger = inform_variant_array(min_size, max_size);
6107
int mid_size = interpolate_variant(arr_a.size(), arr_b.size(), c);
6108
if (is_a_larger) {
6109
arr_a.resize(mid_size);
6110
} else {
6111
arr_b.resize(mid_size);
6112
}
6113
return cast_from_blendwise(blend_variant(arr_a, arr_b, c), a.get_type());
6114
} break;
6115
case Variant::PACKED_BYTE_ARRAY: {
6116
// Skip.
6117
} break;
6118
default: {
6119
if (a.is_array()) {
6120
const Array arr_a = a.operator Array();
6121
const Array arr_b = b.operator Array();
6122
6123
int min_size = arr_a.size();
6124
int max_size = arr_b.size();
6125
bool is_a_larger = inform_variant_array(min_size, max_size);
6126
6127
Array result;
6128
result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
6129
result.resize(min_size);
6130
int i = 0;
6131
for (; i < min_size; i++) {
6132
result[i] = blend_variant(arr_a[i], arr_b[i], c);
6133
}
6134
if (min_size != max_size) {
6135
// Process with last element of the lesser array.
6136
// This is pretty funny and bizarre, but artists like to use it for polygon animation.
6137
Variant lesser_last;
6138
if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) {
6139
result.resize(max_size);
6140
if (i > 0) {
6141
lesser_last = arr_b[i - 1];
6142
} else {
6143
Variant vz = arr_a[i];
6144
vz.zero();
6145
lesser_last = vz;
6146
}
6147
for (; i < max_size; i++) {
6148
result[i] = blend_variant(arr_a[i], lesser_last, c);
6149
}
6150
} else if (!is_a_larger && !Math::is_zero_approx(c)) {
6151
result.resize(max_size);
6152
if (i > 0) {
6153
lesser_last = arr_a[i - 1];
6154
} else {
6155
Variant vz = arr_b[i];
6156
vz.zero();
6157
lesser_last = vz;
6158
}
6159
for (; i < max_size; i++) {
6160
result[i] = blend_variant(lesser_last, arr_b[i], c);
6161
}
6162
}
6163
}
6164
return result;
6165
}
6166
} break;
6167
}
6168
return c < 0.5 ? a : b;
6169
}
6170
6171
Variant Animation::interpolate_variant(const Variant &a, const Variant &b, float c, bool p_snap_array_element) {
6172
if (a.get_type() != b.get_type()) {
6173
if (a.is_num() && b.is_num()) {
6174
return interpolate_variant(cast_to_blendwise(a), cast_to_blendwise(b), c);
6175
} else if (!a.is_array()) {
6176
return a;
6177
}
6178
}
6179
6180
switch (a.get_type()) {
6181
case Variant::NIL: {
6182
return Variant();
6183
} break;
6184
case Variant::FLOAT: {
6185
return Math::lerp(a.operator double(), b.operator double(), (double)c);
6186
} break;
6187
case Variant::VECTOR2: {
6188
return (a.operator Vector2()).lerp(b.operator Vector2(), c);
6189
} break;
6190
case Variant::RECT2: {
6191
const Rect2 ra = a.operator Rect2();
6192
const Rect2 rb = b.operator Rect2();
6193
return Rect2(ra.position.lerp(rb.position, c), ra.size.lerp(rb.size, c));
6194
} break;
6195
case Variant::VECTOR3: {
6196
return (a.operator Vector3()).lerp(b.operator Vector3(), c);
6197
} break;
6198
case Variant::VECTOR4: {
6199
return (a.operator Vector4()).lerp(b.operator Vector4(), c);
6200
} break;
6201
case Variant::PLANE: {
6202
const Plane pa = a.operator Plane();
6203
const Plane pb = b.operator Plane();
6204
return Plane(pa.normal.lerp(pb.normal, c), Math::lerp((double)pa.d, (double)pb.d, (double)c));
6205
} break;
6206
case Variant::COLOR: {
6207
return (a.operator Color()).lerp(b.operator Color(), c);
6208
} break;
6209
case Variant::AABB: {
6210
const ::AABB aa = a.operator ::AABB();
6211
const ::AABB ab = b.operator ::AABB();
6212
return ::AABB(aa.position.lerp(ab.position, c), aa.size.lerp(ab.size, c));
6213
} break;
6214
case Variant::BASIS: {
6215
return (a.operator Basis()).lerp(b.operator Basis(), c);
6216
} break;
6217
case Variant::QUATERNION: {
6218
return (a.operator Quaternion()).slerp(b.operator Quaternion(), c);
6219
} break;
6220
case Variant::TRANSFORM2D: {
6221
return (a.operator Transform2D()).interpolate_with(b.operator Transform2D(), c);
6222
} break;
6223
case Variant::TRANSFORM3D: {
6224
return (a.operator Transform3D()).interpolate_with(b.operator Transform3D(), c);
6225
} break;
6226
case Variant::BOOL:
6227
case Variant::INT:
6228
case Variant::RECT2I:
6229
case Variant::VECTOR2I:
6230
case Variant::VECTOR3I:
6231
case Variant::VECTOR4I:
6232
case Variant::PACKED_INT32_ARRAY:
6233
case Variant::PACKED_INT64_ARRAY: {
6234
// Fallback the interpolatable value which needs casting.
6235
return cast_from_blendwise(interpolate_variant(cast_to_blendwise(a), cast_to_blendwise(b), c), a.get_type());
6236
} break;
6237
case Variant::STRING:
6238
case Variant::STRING_NAME: {
6239
Array arr_a = cast_to_blendwise(a);
6240
Array arr_b = cast_to_blendwise(b);
6241
int min_size = arr_a.size();
6242
int max_size = arr_b.size();
6243
bool is_a_larger = inform_variant_array(min_size, max_size);
6244
int mid_size = interpolate_variant(arr_a.size(), arr_b.size(), c);
6245
if (is_a_larger) {
6246
arr_a.resize(mid_size);
6247
} else {
6248
arr_b.resize(mid_size);
6249
}
6250
return cast_from_blendwise(interpolate_variant(arr_a, arr_b, c, true), a.get_type());
6251
} break;
6252
case Variant::PACKED_BYTE_ARRAY: {
6253
// Skip.
6254
} break;
6255
default: {
6256
if (a.is_array()) {
6257
const Array arr_a = a.operator Array();
6258
const Array arr_b = b.operator Array();
6259
6260
int min_size = arr_a.size();
6261
int max_size = arr_b.size();
6262
bool is_a_larger = inform_variant_array(min_size, max_size);
6263
6264
Array result;
6265
result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
6266
result.resize(min_size);
6267
int i = 0;
6268
for (; i < min_size; i++) {
6269
result[i] = interpolate_variant(arr_a[i], arr_b[i], c);
6270
}
6271
if (min_size != max_size) {
6272
// Process with last element of the lesser array.
6273
// This is pretty funny and bizarre, but artists like to use it for polygon animation.
6274
Variant lesser_last;
6275
if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) {
6276
result.resize(max_size);
6277
if (p_snap_array_element) {
6278
c = 0;
6279
}
6280
if (i > 0) {
6281
lesser_last = arr_b[i - 1];
6282
} else {
6283
Variant vz = arr_a[i];
6284
vz.zero();
6285
lesser_last = vz;
6286
}
6287
for (; i < max_size; i++) {
6288
result[i] = interpolate_variant(arr_a[i], lesser_last, c);
6289
}
6290
} else if (!is_a_larger && !Math::is_zero_approx(c)) {
6291
result.resize(max_size);
6292
if (p_snap_array_element) {
6293
c = 1;
6294
}
6295
if (i > 0) {
6296
lesser_last = arr_a[i - 1];
6297
} else {
6298
Variant vz = arr_b[i];
6299
vz.zero();
6300
lesser_last = vz;
6301
}
6302
for (; i < max_size; i++) {
6303
result[i] = interpolate_variant(lesser_last, arr_b[i], c);
6304
}
6305
}
6306
}
6307
return result;
6308
}
6309
} break;
6310
}
6311
return c < 0.5 ? a : b;
6312
}
6313
6314
Variant Animation::cubic_interpolate_in_time_variant(const Variant &pre_a, const Variant &a, const Variant &b, const Variant &post_b, float c, real_t p_pre_a_t, real_t p_b_t, real_t p_post_b_t, bool p_snap_array_element) {
6315
if (pre_a.get_type() != a.get_type() || pre_a.get_type() != b.get_type() || pre_a.get_type() != post_b.get_type()) {
6316
if (pre_a.is_num() && a.is_num() && b.is_num() && post_b.is_num()) {
6317
return cubic_interpolate_in_time_variant(cast_to_blendwise(pre_a), cast_to_blendwise(a), cast_to_blendwise(b), cast_to_blendwise(post_b), c, p_pre_a_t, p_b_t, p_post_b_t, p_snap_array_element);
6318
} else if (!a.is_array()) {
6319
return a;
6320
}
6321
}
6322
6323
switch (a.get_type()) {
6324
case Variant::NIL: {
6325
return Variant();
6326
} break;
6327
case Variant::FLOAT: {
6328
return Math::cubic_interpolate_in_time(a.operator double(), b.operator double(), pre_a.operator double(), post_b.operator double(), (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t);
6329
} break;
6330
case Variant::VECTOR2: {
6331
return (a.operator Vector2()).cubic_interpolate_in_time(b.operator Vector2(), pre_a.operator Vector2(), post_b.operator Vector2(), c, p_b_t, p_pre_a_t, p_post_b_t);
6332
} break;
6333
case Variant::RECT2: {
6334
const Rect2 rpa = pre_a.operator Rect2();
6335
const Rect2 ra = a.operator Rect2();
6336
const Rect2 rb = b.operator Rect2();
6337
const Rect2 rpb = post_b.operator Rect2();
6338
return Rect2(
6339
ra.position.cubic_interpolate_in_time(rb.position, rpa.position, rpb.position, c, p_b_t, p_pre_a_t, p_post_b_t),
6340
ra.size.cubic_interpolate_in_time(rb.size, rpa.size, rpb.size, c, p_b_t, p_pre_a_t, p_post_b_t));
6341
} break;
6342
case Variant::VECTOR3: {
6343
return (a.operator Vector3()).cubic_interpolate_in_time(b.operator Vector3(), pre_a.operator Vector3(), post_b.operator Vector3(), c, p_b_t, p_pre_a_t, p_post_b_t);
6344
} break;
6345
case Variant::VECTOR4: {
6346
return (a.operator Vector4()).cubic_interpolate_in_time(b.operator Vector4(), pre_a.operator Vector4(), post_b.operator Vector4(), c, p_b_t, p_pre_a_t, p_post_b_t);
6347
} break;
6348
case Variant::PLANE: {
6349
const Plane ppa = pre_a.operator Plane();
6350
const Plane pa = a.operator Plane();
6351
const Plane pb = b.operator Plane();
6352
const Plane ppb = post_b.operator Plane();
6353
return Plane(
6354
pa.normal.cubic_interpolate_in_time(pb.normal, ppa.normal, ppb.normal, c, p_b_t, p_pre_a_t, p_post_b_t),
6355
Math::cubic_interpolate_in_time((double)pa.d, (double)pb.d, (double)ppa.d, (double)ppb.d, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t));
6356
} break;
6357
case Variant::COLOR: {
6358
const Color cpa = pre_a.operator Color();
6359
const Color ca = a.operator Color();
6360
const Color cb = b.operator Color();
6361
const Color cpb = post_b.operator Color();
6362
return Color(
6363
Math::cubic_interpolate_in_time((double)ca.r, (double)cb.r, (double)cpa.r, (double)cpb.r, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t),
6364
Math::cubic_interpolate_in_time((double)ca.g, (double)cb.g, (double)cpa.g, (double)cpb.g, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t),
6365
Math::cubic_interpolate_in_time((double)ca.b, (double)cb.b, (double)cpa.b, (double)cpb.b, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t),
6366
Math::cubic_interpolate_in_time((double)ca.a, (double)cb.a, (double)cpa.a, (double)cpb.a, (double)c, (double)p_b_t, (double)p_pre_a_t, (double)p_post_b_t));
6367
} break;
6368
case Variant::AABB: {
6369
const ::AABB apa = pre_a.operator ::AABB();
6370
const ::AABB aa = a.operator ::AABB();
6371
const ::AABB ab = b.operator ::AABB();
6372
const ::AABB apb = post_b.operator ::AABB();
6373
return AABB(
6374
aa.position.cubic_interpolate_in_time(ab.position, apa.position, apb.position, c, p_b_t, p_pre_a_t, p_post_b_t),
6375
aa.size.cubic_interpolate_in_time(ab.size, apa.size, apb.size, c, p_b_t, p_pre_a_t, p_post_b_t));
6376
} break;
6377
case Variant::BASIS: {
6378
const Basis bpa = pre_a.operator Basis();
6379
const Basis ba = a.operator Basis();
6380
const Basis bb = b.operator Basis();
6381
const Basis bpb = post_b.operator Basis();
6382
return Basis(
6383
ba.rows[0].cubic_interpolate_in_time(bb.rows[0], bpa.rows[0], bpb.rows[0], c, p_b_t, p_pre_a_t, p_post_b_t),
6384
ba.rows[1].cubic_interpolate_in_time(bb.rows[1], bpa.rows[1], bpb.rows[1], c, p_b_t, p_pre_a_t, p_post_b_t),
6385
ba.rows[2].cubic_interpolate_in_time(bb.rows[2], bpa.rows[2], bpb.rows[2], c, p_b_t, p_pre_a_t, p_post_b_t));
6386
} break;
6387
case Variant::QUATERNION: {
6388
return (a.operator Quaternion()).spherical_cubic_interpolate_in_time(b.operator Quaternion(), pre_a.operator Quaternion(), post_b.operator Quaternion(), c, p_b_t, p_pre_a_t, p_post_b_t);
6389
} break;
6390
case Variant::TRANSFORM2D: {
6391
const Transform2D tpa = pre_a.operator Transform2D();
6392
const Transform2D ta = a.operator Transform2D();
6393
const Transform2D tb = b.operator Transform2D();
6394
const Transform2D tpb = post_b.operator Transform2D();
6395
// TODO: May cause unintended skew, we needs spherical_cubic_interpolate_in_time() for angle and Transform2D::cubic_interpolate_with().
6396
return Transform2D(
6397
ta[0].cubic_interpolate_in_time(tb[0], tpa[0], tpb[0], c, p_b_t, p_pre_a_t, p_post_b_t),
6398
ta[1].cubic_interpolate_in_time(tb[1], tpa[1], tpb[1], c, p_b_t, p_pre_a_t, p_post_b_t),
6399
ta[2].cubic_interpolate_in_time(tb[2], tpa[2], tpb[2], c, p_b_t, p_pre_a_t, p_post_b_t));
6400
} break;
6401
case Variant::TRANSFORM3D: {
6402
const Transform3D tpa = pre_a.operator Transform3D();
6403
const Transform3D ta = a.operator Transform3D();
6404
const Transform3D tb = b.operator Transform3D();
6405
const Transform3D tpb = post_b.operator Transform3D();
6406
// TODO: May cause unintended skew, we needs Transform3D::cubic_interpolate_with().
6407
return Transform3D(
6408
ta.basis.rows[0].cubic_interpolate_in_time(tb.basis.rows[0], tpa.basis.rows[0], tpb.basis.rows[0], c, p_b_t, p_pre_a_t, p_post_b_t),
6409
ta.basis.rows[1].cubic_interpolate_in_time(tb.basis.rows[1], tpa.basis.rows[1], tpb.basis.rows[1], c, p_b_t, p_pre_a_t, p_post_b_t),
6410
ta.basis.rows[2].cubic_interpolate_in_time(tb.basis.rows[2], tpa.basis.rows[2], tpb.basis.rows[2], c, p_b_t, p_pre_a_t, p_post_b_t),
6411
ta.origin.cubic_interpolate_in_time(tb.origin, tpa.origin, tpb.origin, c, p_b_t, p_pre_a_t, p_post_b_t));
6412
} break;
6413
case Variant::BOOL:
6414
case Variant::INT:
6415
case Variant::RECT2I:
6416
case Variant::VECTOR2I:
6417
case Variant::VECTOR3I:
6418
case Variant::VECTOR4I:
6419
case Variant::PACKED_INT32_ARRAY:
6420
case Variant::PACKED_INT64_ARRAY: {
6421
// Fallback the interpolatable value which needs casting.
6422
return cast_from_blendwise(cubic_interpolate_in_time_variant(cast_to_blendwise(pre_a), cast_to_blendwise(a), cast_to_blendwise(b), cast_to_blendwise(post_b), c, p_pre_a_t, p_b_t, p_post_b_t, p_snap_array_element), a.get_type());
6423
} break;
6424
case Variant::STRING:
6425
case Variant::STRING_NAME: {
6426
// TODO:
6427
// String interpolation works on both the character array size and the character code, to apply cubic interpolation neatly,
6428
// we need to figure out how to interpolate well in cases where there are fewer than 4 keys. So, for now, fallback to linear interpolation.
6429
return interpolate_variant(a, b, c);
6430
} break;
6431
case Variant::PACKED_BYTE_ARRAY: {
6432
// Skip.
6433
} break;
6434
default: {
6435
if (a.is_array()) {
6436
const Array arr_pa = pre_a.operator Array();
6437
const Array arr_a = a.operator Array();
6438
const Array arr_b = b.operator Array();
6439
const Array arr_pb = post_b.operator Array();
6440
6441
int min_size = arr_a.size();
6442
int max_size = arr_b.size();
6443
bool is_a_larger = inform_variant_array(min_size, max_size);
6444
6445
Array result;
6446
result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant());
6447
result.resize(min_size);
6448
6449
if (min_size == 0 && max_size == 0) {
6450
return result;
6451
}
6452
6453
Variant vz;
6454
if (is_a_larger) {
6455
vz = arr_a[0];
6456
} else {
6457
vz = arr_b[0];
6458
}
6459
vz.zero();
6460
Variant pre_last = arr_pa.size() ? arr_pa[arr_pa.size() - 1] : vz;
6461
Variant post_last = arr_pb.size() ? arr_pb[arr_pb.size() - 1] : vz;
6462
6463
int i = 0;
6464
for (; i < min_size; i++) {
6465
result[i] = cubic_interpolate_in_time_variant(i >= arr_pa.size() ? pre_last : arr_pa[i], arr_a[i], arr_b[i], i >= arr_pb.size() ? post_last : arr_pb[i], c, p_pre_a_t, p_b_t, p_post_b_t);
6466
}
6467
if (min_size != max_size) {
6468
// Process with last element of the lesser array.
6469
// This is pretty funny and bizarre, but artists like to use it for polygon animation.
6470
Variant lesser_last = vz;
6471
if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) {
6472
result.resize(max_size);
6473
if (p_snap_array_element) {
6474
c = 0;
6475
}
6476
if (i > 0) {
6477
lesser_last = arr_b[i - 1];
6478
}
6479
for (; i < max_size; i++) {
6480
result[i] = cubic_interpolate_in_time_variant(i >= arr_pa.size() ? pre_last : arr_pa[i], arr_a[i], lesser_last, i >= arr_pb.size() ? post_last : arr_pb[i], c, p_pre_a_t, p_b_t, p_post_b_t);
6481
}
6482
} else if (!is_a_larger && !Math::is_zero_approx(c)) {
6483
result.resize(max_size);
6484
if (p_snap_array_element) {
6485
c = 1;
6486
}
6487
if (i > 0) {
6488
lesser_last = arr_a[i - 1];
6489
}
6490
for (; i < max_size; i++) {
6491
result[i] = cubic_interpolate_in_time_variant(i >= arr_pa.size() ? pre_last : arr_pa[i], lesser_last, arr_b[i], i >= arr_pb.size() ? post_last : arr_pb[i], c, p_pre_a_t, p_b_t, p_post_b_t);
6492
}
6493
}
6494
}
6495
return result;
6496
}
6497
} break;
6498
}
6499
return c < 0.5 ? a : b;
6500
}
6501
6502
bool Animation::inform_variant_array(int &r_min, int &r_max) {
6503
if (r_min <= r_max) {
6504
return false;
6505
}
6506
SWAP(r_min, r_max);
6507
return true;
6508
}
6509
6510
Animation::Animation() {
6511
}
6512
6513
Animation::~Animation() {
6514
for (int i = 0; i < tracks.size(); i++) {
6515
memdelete(tracks[i]);
6516
}
6517
}
6518
6519