| 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() == (uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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.ptr(); |
| 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.ptr(); |
| 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.ptr(); |
| 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.ptr(); |
| 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[i].time = rt[i]; |
| 281 | vt->values[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[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[i].time = rt[i]; |
| 363 | bt->values[i].transition = 0; //unused in bezier |
| 364 | bt->values[i].value.value = rv[i * 5 + 0]; |
| 365 | bt->values[i].value.in_handle.x = rv[i * 5 + 1]; |
| 366 | bt->values[i].value.in_handle.y = rv[i * 5 + 2]; |
| 367 | bt->values[i].value.out_handle.x = rv[i * 5 + 3]; |
| 368 | bt->values[i].value.out_handle.y = rv[i * 5 + 4]; |
| 369 | #ifdef TOOLS_ENABLED |
| 370 | bt->values[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[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_UNSIGNED_INDEX_V((uint32_t)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 (uint32_t 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 ((uint32_t)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_UNSIGNED_INDEX((uint32_t)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 (uint32_t 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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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 (uint32_t 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 | const NodePath &track_path = tracks[p_track]->path; |
| 1066 | const TrackType track_cache_type = get_cache_type(tracks[p_track]->type); |
| 1067 | tracks[p_track]->thash = HashMapHasherDefault::hash(Pair<const NodePath &, TrackType>(track_path, track_cache_type)); |
| 1068 | } |
| 1069 | |
| 1070 | Animation::TypeHash Animation::track_get_type_hash(int p_track) const { |
| 1071 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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[idx - 1] = p_value; |
| 1106 | p_keys[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, LocalVector<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[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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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 ((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)p_key_idx, tt->positions.size()); |
| 2260 | |
| 2261 | tt->positions[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_UNSIGNED_INDEX((uint32_t)p_key_idx, rt->rotations.size()); |
| 2269 | |
| 2270 | rt->rotations[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_UNSIGNED_INDEX((uint32_t)p_key_idx, st->scales.size()); |
| 2278 | |
| 2279 | st->scales[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_UNSIGNED_INDEX((uint32_t)p_key_idx, bst->blend_shapes.size()); |
| 2287 | |
| 2288 | bst->blend_shapes[p_key_idx].value = p_value; |
| 2289 | |
| 2290 | } break; |
| 2291 | case TYPE_VALUE: { |
| 2292 | ValueTrack *vt = static_cast<ValueTrack *>(t); |
| 2293 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key_idx, vt->values.size()); |
| 2294 | |
| 2295 | vt->values[p_key_idx].value = p_value; |
| 2296 | |
| 2297 | } break; |
| 2298 | case TYPE_METHOD: { |
| 2299 | MethodTrack *mt = static_cast<MethodTrack *>(t); |
| 2300 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key_idx, mt->methods.size()); |
| 2301 | |
| 2302 | Dictionary d = p_value; |
| 2303 | |
| 2304 | if (d.has("method")) { |
| 2305 | mt->methods[p_key_idx].method = d["method"]; |
| 2306 | } |
| 2307 | if (d.has("args")) { |
| 2308 | mt->methods[p_key_idx].params = d["args"]; |
| 2309 | } |
| 2310 | |
| 2311 | } break; |
| 2312 | case TYPE_BEZIER: { |
| 2313 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 2314 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key_idx, bt->values.size()); |
| 2315 | |
| 2316 | Array arr = p_value; |
| 2317 | ERR_FAIL_COND(arr.size() != 5); |
| 2318 | |
| 2319 | bt->values[p_key_idx].value.value = arr[0]; |
| 2320 | bt->values[p_key_idx].value.in_handle.x = arr[1]; |
| 2321 | bt->values[p_key_idx].value.in_handle.y = arr[2]; |
| 2322 | bt->values[p_key_idx].value.out_handle.x = arr[3]; |
| 2323 | bt->values[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_UNSIGNED_INDEX((uint32_t)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[p_key_idx].value.start_offset = k["start_offset"]; |
| 2336 | at->values[p_key_idx].value.end_offset = k["end_offset"]; |
| 2337 | at->values[p_key_idx].value.stream = k["stream"]; |
| 2338 | |
| 2339 | } break; |
| 2340 | case TYPE_ANIMATION: { |
| 2341 | AnimationTrack *at = static_cast<AnimationTrack *>(t); |
| 2342 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key_idx, at->values.size()); |
| 2343 | |
| 2344 | at->values[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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX((uint32_t)p_key_idx, tt->positions.size()); |
| 2361 | tt->positions[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_UNSIGNED_INDEX((uint32_t)p_key_idx, rt->rotations.size()); |
| 2367 | rt->rotations[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_UNSIGNED_INDEX((uint32_t)p_key_idx, st->scales.size()); |
| 2373 | st->scales[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_UNSIGNED_INDEX((uint32_t)p_key_idx, bst->blend_shapes.size()); |
| 2379 | bst->blend_shapes[p_key_idx].transition = p_transition; |
| 2380 | } break; |
| 2381 | case TYPE_VALUE: { |
| 2382 | ValueTrack *vt = static_cast<ValueTrack *>(t); |
| 2383 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key_idx, vt->values.size()); |
| 2384 | vt->values[p_key_idx].transition = p_transition; |
| 2385 | |
| 2386 | } break; |
| 2387 | case TYPE_METHOD: { |
| 2388 | MethodTrack *mt = static_cast<MethodTrack *>(t); |
| 2389 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key_idx, mt->methods.size()); |
| 2390 | mt->methods[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 LocalVector<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 LocalVector<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_UNSIGNED_INDEX_V((uint32_t)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_UNSIGNED_INDEX((uint32_t)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_UNSIGNED_INDEX_V((uint32_t)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 LocalVector<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, double p_start, double p_end, List<int> *p_indices, Animation::LoopedFlag p_looped_flag) const { |
| 2822 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)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 | double start = p_start; |
| 2833 | double end = p_end; |
| 2834 | if (start < 0) { |
| 2835 | start = 0; |
| 2836 | } |
| 2837 | if (end > length) { |
| 2838 | end = length; |
| 2839 | } |
| 2840 | |
| 2841 | bool is_backward = false; |
| 2842 | if (from_time > to_time) { |
| 2843 | is_backward = true; |
| 2844 | SWAP(from_time, to_time); |
| 2845 | } |
| 2846 | |
| 2847 | switch (loop_mode) { |
| 2848 | case LOOP_NONE: { |
| 2849 | if (from_time < 0) { |
| 2850 | from_time = 0; |
| 2851 | } |
| 2852 | if (from_time > end) { |
| 2853 | from_time = end; |
| 2854 | } |
| 2855 | |
| 2856 | if (to_time < 0) { |
| 2857 | to_time = 0; |
| 2858 | } |
| 2859 | if (to_time > end) { |
| 2860 | to_time = end; |
| 2861 | } |
| 2862 | } break; |
| 2863 | case LOOP_LINEAR: { |
| 2864 | if (from_time > end || from_time < start) { |
| 2865 | from_time = Math::fposmod(from_time, end); |
| 2866 | } |
| 2867 | if (to_time > end || to_time < start) { |
| 2868 | to_time = Math::fposmod(to_time, end); |
| 2869 | } |
| 2870 | |
| 2871 | if (from_time > to_time) { |
| 2872 | // Handle loop by splitting. |
| 2873 | double anim_end = end + CMP_EPSILON; |
| 2874 | double anim_start = start - CMP_EPSILON; |
| 2875 | |
| 2876 | switch (t->type) { |
| 2877 | case TYPE_POSITION_3D: { |
| 2878 | const PositionTrack *tt = static_cast<const PositionTrack *>(t); |
| 2879 | if (tt->compressed_track >= 0) { |
| 2880 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, end, p_indices); |
| 2881 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, start, to_time, p_indices); |
| 2882 | } else { |
| 2883 | if (!is_backward) { |
| 2884 | _track_get_key_indices_in_range(tt->positions, from_time, anim_end, p_indices, is_backward); |
| 2885 | _track_get_key_indices_in_range(tt->positions, anim_start, to_time, p_indices, is_backward); |
| 2886 | } else { |
| 2887 | _track_get_key_indices_in_range(tt->positions, anim_start, to_time, p_indices, is_backward); |
| 2888 | _track_get_key_indices_in_range(tt->positions, from_time, anim_end, p_indices, is_backward); |
| 2889 | } |
| 2890 | } |
| 2891 | } break; |
| 2892 | case TYPE_ROTATION_3D: { |
| 2893 | const RotationTrack *rt = static_cast<const RotationTrack *>(t); |
| 2894 | if (rt->compressed_track >= 0) { |
| 2895 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, end, p_indices); |
| 2896 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, start, to_time, p_indices); |
| 2897 | } else { |
| 2898 | if (!is_backward) { |
| 2899 | _track_get_key_indices_in_range(rt->rotations, from_time, anim_end, p_indices, is_backward); |
| 2900 | _track_get_key_indices_in_range(rt->rotations, anim_start, to_time, p_indices, is_backward); |
| 2901 | } else { |
| 2902 | _track_get_key_indices_in_range(rt->rotations, anim_start, to_time, p_indices, is_backward); |
| 2903 | _track_get_key_indices_in_range(rt->rotations, from_time, anim_end, p_indices, is_backward); |
| 2904 | } |
| 2905 | } |
| 2906 | } break; |
| 2907 | case TYPE_SCALE_3D: { |
| 2908 | const ScaleTrack *st = static_cast<const ScaleTrack *>(t); |
| 2909 | if (st->compressed_track >= 0) { |
| 2910 | _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, end, p_indices); |
| 2911 | _get_compressed_key_indices_in_range<3>(st->compressed_track, start, to_time, p_indices); |
| 2912 | } else { |
| 2913 | if (!is_backward) { |
| 2914 | _track_get_key_indices_in_range(st->scales, from_time, anim_end, p_indices, is_backward); |
| 2915 | _track_get_key_indices_in_range(st->scales, anim_start, to_time, p_indices, is_backward); |
| 2916 | } else { |
| 2917 | _track_get_key_indices_in_range(st->scales, anim_start, to_time, p_indices, is_backward); |
| 2918 | _track_get_key_indices_in_range(st->scales, from_time, anim_end, p_indices, is_backward); |
| 2919 | } |
| 2920 | } |
| 2921 | } break; |
| 2922 | case TYPE_BLEND_SHAPE: { |
| 2923 | const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t); |
| 2924 | if (bst->compressed_track >= 0) { |
| 2925 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, end, p_indices); |
| 2926 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, start, to_time, p_indices); |
| 2927 | } else { |
| 2928 | if (!is_backward) { |
| 2929 | _track_get_key_indices_in_range(bst->blend_shapes, from_time, anim_end, p_indices, is_backward); |
| 2930 | _track_get_key_indices_in_range(bst->blend_shapes, anim_start, to_time, p_indices, is_backward); |
| 2931 | } else { |
| 2932 | _track_get_key_indices_in_range(bst->blend_shapes, anim_start, to_time, p_indices, is_backward); |
| 2933 | _track_get_key_indices_in_range(bst->blend_shapes, from_time, anim_end, p_indices, is_backward); |
| 2934 | } |
| 2935 | } |
| 2936 | } break; |
| 2937 | case TYPE_VALUE: { |
| 2938 | const ValueTrack *vt = static_cast<const ValueTrack *>(t); |
| 2939 | if (!is_backward) { |
| 2940 | _track_get_key_indices_in_range(vt->values, from_time, anim_end, p_indices, is_backward); |
| 2941 | _track_get_key_indices_in_range(vt->values, anim_start, to_time, p_indices, is_backward); |
| 2942 | } else { |
| 2943 | _track_get_key_indices_in_range(vt->values, anim_start, to_time, p_indices, is_backward); |
| 2944 | _track_get_key_indices_in_range(vt->values, from_time, anim_end, p_indices, is_backward); |
| 2945 | } |
| 2946 | } break; |
| 2947 | case TYPE_METHOD: { |
| 2948 | const MethodTrack *mt = static_cast<const MethodTrack *>(t); |
| 2949 | if (!is_backward) { |
| 2950 | _track_get_key_indices_in_range(mt->methods, from_time, anim_end, p_indices, is_backward); |
| 2951 | _track_get_key_indices_in_range(mt->methods, anim_start, to_time, p_indices, is_backward); |
| 2952 | } else { |
| 2953 | _track_get_key_indices_in_range(mt->methods, anim_start, to_time, p_indices, is_backward); |
| 2954 | _track_get_key_indices_in_range(mt->methods, from_time, anim_end, p_indices, is_backward); |
| 2955 | } |
| 2956 | } break; |
| 2957 | case TYPE_BEZIER: { |
| 2958 | const BezierTrack *bz = static_cast<const BezierTrack *>(t); |
| 2959 | if (!is_backward) { |
| 2960 | _track_get_key_indices_in_range(bz->values, from_time, anim_end, p_indices, is_backward); |
| 2961 | _track_get_key_indices_in_range(bz->values, anim_start, to_time, p_indices, is_backward); |
| 2962 | } else { |
| 2963 | _track_get_key_indices_in_range(bz->values, anim_start, to_time, p_indices, is_backward); |
| 2964 | _track_get_key_indices_in_range(bz->values, from_time, anim_end, p_indices, is_backward); |
| 2965 | } |
| 2966 | } break; |
| 2967 | case TYPE_AUDIO: { |
| 2968 | const AudioTrack *ad = static_cast<const AudioTrack *>(t); |
| 2969 | if (!is_backward) { |
| 2970 | _track_get_key_indices_in_range(ad->values, from_time, anim_end, p_indices, is_backward); |
| 2971 | _track_get_key_indices_in_range(ad->values, anim_start, to_time, p_indices, is_backward); |
| 2972 | } else { |
| 2973 | _track_get_key_indices_in_range(ad->values, anim_start, to_time, p_indices, is_backward); |
| 2974 | _track_get_key_indices_in_range(ad->values, from_time, anim_end, p_indices, is_backward); |
| 2975 | } |
| 2976 | } break; |
| 2977 | case TYPE_ANIMATION: { |
| 2978 | const AnimationTrack *an = static_cast<const AnimationTrack *>(t); |
| 2979 | if (!is_backward) { |
| 2980 | _track_get_key_indices_in_range(an->values, from_time, anim_end, p_indices, is_backward); |
| 2981 | _track_get_key_indices_in_range(an->values, anim_start, to_time, p_indices, is_backward); |
| 2982 | } else { |
| 2983 | _track_get_key_indices_in_range(an->values, anim_start, to_time, p_indices, is_backward); |
| 2984 | _track_get_key_indices_in_range(an->values, from_time, anim_end, p_indices, is_backward); |
| 2985 | } |
| 2986 | } break; |
| 2987 | } |
| 2988 | return; |
| 2989 | } |
| 2990 | |
| 2991 | // Not from_time > to_time but most recent of looping... |
| 2992 | if (p_looped_flag != Animation::LOOPED_FLAG_NONE) { |
| 2993 | if (!is_backward && Math::is_equal_approx(from_time, start)) { |
| 2994 | int edge = track_find_key(p_track, start, FIND_MODE_EXACT); |
| 2995 | if (edge >= 0) { |
| 2996 | p_indices->push_back(edge); |
| 2997 | } |
| 2998 | } else if (is_backward && Math::is_equal_approx(to_time, end)) { |
| 2999 | int edge = track_find_key(p_track, end, FIND_MODE_EXACT); |
| 3000 | if (edge >= 0) { |
| 3001 | p_indices->push_back(edge); |
| 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | } break; |
| 3006 | case LOOP_PINGPONG: { |
| 3007 | if (from_time > end || from_time < start) { |
| 3008 | from_time = Math::pingpong(from_time, length); |
| 3009 | } |
| 3010 | if (to_time > end || to_time < start) { |
| 3011 | to_time = Math::pingpong(to_time, length); |
| 3012 | } |
| 3013 | |
| 3014 | if (p_looped_flag == Animation::LOOPED_FLAG_START) { |
| 3015 | // Handle loop by splitting. |
| 3016 | switch (t->type) { |
| 3017 | case TYPE_POSITION_3D: { |
| 3018 | const PositionTrack *tt = static_cast<const PositionTrack *>(t); |
| 3019 | if (tt->compressed_track >= 0) { |
| 3020 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, start, from_time, p_indices); |
| 3021 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, start, to_time, p_indices); |
| 3022 | } else { |
| 3023 | _track_get_key_indices_in_range(tt->positions, start, from_time, p_indices, true); |
| 3024 | _track_get_key_indices_in_range(tt->positions, start, to_time, p_indices, false); |
| 3025 | } |
| 3026 | } break; |
| 3027 | case TYPE_ROTATION_3D: { |
| 3028 | const RotationTrack *rt = static_cast<const RotationTrack *>(t); |
| 3029 | if (rt->compressed_track >= 0) { |
| 3030 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, start, from_time, p_indices); |
| 3031 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, start, to_time, p_indices); |
| 3032 | } else { |
| 3033 | _track_get_key_indices_in_range(rt->rotations, start, from_time, p_indices, true); |
| 3034 | _track_get_key_indices_in_range(rt->rotations, start, to_time, p_indices, false); |
| 3035 | } |
| 3036 | } break; |
| 3037 | case TYPE_SCALE_3D: { |
| 3038 | const ScaleTrack *st = static_cast<const ScaleTrack *>(t); |
| 3039 | if (st->compressed_track >= 0) { |
| 3040 | _get_compressed_key_indices_in_range<3>(st->compressed_track, start, from_time, p_indices); |
| 3041 | _get_compressed_key_indices_in_range<3>(st->compressed_track, start, to_time, p_indices); |
| 3042 | } else { |
| 3043 | _track_get_key_indices_in_range(st->scales, start, from_time, p_indices, true); |
| 3044 | _track_get_key_indices_in_range(st->scales, start, to_time, p_indices, false); |
| 3045 | } |
| 3046 | } break; |
| 3047 | case TYPE_BLEND_SHAPE: { |
| 3048 | const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t); |
| 3049 | if (bst->compressed_track >= 0) { |
| 3050 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, start, from_time, p_indices); |
| 3051 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, start, to_time, p_indices); |
| 3052 | } else { |
| 3053 | _track_get_key_indices_in_range(bst->blend_shapes, start, from_time, p_indices, true); |
| 3054 | _track_get_key_indices_in_range(bst->blend_shapes, start, to_time, p_indices, false); |
| 3055 | } |
| 3056 | } break; |
| 3057 | case TYPE_VALUE: { |
| 3058 | const ValueTrack *vt = static_cast<const ValueTrack *>(t); |
| 3059 | _track_get_key_indices_in_range(vt->values, start, from_time, p_indices, true); |
| 3060 | _track_get_key_indices_in_range(vt->values, start, to_time, p_indices, false); |
| 3061 | } break; |
| 3062 | case TYPE_METHOD: { |
| 3063 | const MethodTrack *mt = static_cast<const MethodTrack *>(t); |
| 3064 | _track_get_key_indices_in_range(mt->methods, start, from_time, p_indices, true); |
| 3065 | _track_get_key_indices_in_range(mt->methods, start, to_time, p_indices, false); |
| 3066 | } break; |
| 3067 | case TYPE_BEZIER: { |
| 3068 | const BezierTrack *bz = static_cast<const BezierTrack *>(t); |
| 3069 | _track_get_key_indices_in_range(bz->values, start, from_time, p_indices, true); |
| 3070 | _track_get_key_indices_in_range(bz->values, start, to_time, p_indices, false); |
| 3071 | } break; |
| 3072 | case TYPE_AUDIO: { |
| 3073 | const AudioTrack *ad = static_cast<const AudioTrack *>(t); |
| 3074 | _track_get_key_indices_in_range(ad->values, start, from_time, p_indices, true); |
| 3075 | _track_get_key_indices_in_range(ad->values, start, to_time, p_indices, false); |
| 3076 | } break; |
| 3077 | case TYPE_ANIMATION: { |
| 3078 | const AnimationTrack *an = static_cast<const AnimationTrack *>(t); |
| 3079 | _track_get_key_indices_in_range(an->values, start, from_time, p_indices, true); |
| 3080 | _track_get_key_indices_in_range(an->values, start, to_time, p_indices, false); |
| 3081 | } break; |
| 3082 | } |
| 3083 | return; |
| 3084 | } |
| 3085 | if (p_looped_flag == Animation::LOOPED_FLAG_END) { |
| 3086 | // Handle loop by splitting. |
| 3087 | switch (t->type) { |
| 3088 | case TYPE_POSITION_3D: { |
| 3089 | const PositionTrack *tt = static_cast<const PositionTrack *>(t); |
| 3090 | if (tt->compressed_track >= 0) { |
| 3091 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, end, p_indices); |
| 3092 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, to_time, end, p_indices); |
| 3093 | } else { |
| 3094 | _track_get_key_indices_in_range(tt->positions, from_time, end, p_indices, false); |
| 3095 | _track_get_key_indices_in_range(tt->positions, to_time, end, p_indices, true); |
| 3096 | } |
| 3097 | } break; |
| 3098 | case TYPE_ROTATION_3D: { |
| 3099 | const RotationTrack *rt = static_cast<const RotationTrack *>(t); |
| 3100 | if (rt->compressed_track >= 0) { |
| 3101 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, end, p_indices); |
| 3102 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, to_time, end, p_indices); |
| 3103 | } else { |
| 3104 | _track_get_key_indices_in_range(rt->rotations, from_time, end, p_indices, false); |
| 3105 | _track_get_key_indices_in_range(rt->rotations, to_time, end, p_indices, true); |
| 3106 | } |
| 3107 | } break; |
| 3108 | case TYPE_SCALE_3D: { |
| 3109 | const ScaleTrack *st = static_cast<const ScaleTrack *>(t); |
| 3110 | if (st->compressed_track >= 0) { |
| 3111 | _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, end, p_indices); |
| 3112 | _get_compressed_key_indices_in_range<3>(st->compressed_track, to_time, end, p_indices); |
| 3113 | } else { |
| 3114 | _track_get_key_indices_in_range(st->scales, from_time, end, p_indices, false); |
| 3115 | _track_get_key_indices_in_range(st->scales, to_time, end, p_indices, true); |
| 3116 | } |
| 3117 | } break; |
| 3118 | case TYPE_BLEND_SHAPE: { |
| 3119 | const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t); |
| 3120 | if (bst->compressed_track >= 0) { |
| 3121 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, end, p_indices); |
| 3122 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, to_time, end, p_indices); |
| 3123 | } else { |
| 3124 | _track_get_key_indices_in_range(bst->blend_shapes, from_time, end, p_indices, false); |
| 3125 | _track_get_key_indices_in_range(bst->blend_shapes, to_time, end, p_indices, true); |
| 3126 | } |
| 3127 | } break; |
| 3128 | case TYPE_VALUE: { |
| 3129 | const ValueTrack *vt = static_cast<const ValueTrack *>(t); |
| 3130 | _track_get_key_indices_in_range(vt->values, from_time, end, p_indices, false); |
| 3131 | _track_get_key_indices_in_range(vt->values, to_time, end, p_indices, true); |
| 3132 | } break; |
| 3133 | case TYPE_METHOD: { |
| 3134 | const MethodTrack *mt = static_cast<const MethodTrack *>(t); |
| 3135 | _track_get_key_indices_in_range(mt->methods, from_time, end, p_indices, false); |
| 3136 | _track_get_key_indices_in_range(mt->methods, to_time, end, p_indices, true); |
| 3137 | } break; |
| 3138 | case TYPE_BEZIER: { |
| 3139 | const BezierTrack *bz = static_cast<const BezierTrack *>(t); |
| 3140 | _track_get_key_indices_in_range(bz->values, from_time, end, p_indices, false); |
| 3141 | _track_get_key_indices_in_range(bz->values, to_time, end, p_indices, true); |
| 3142 | } break; |
| 3143 | case TYPE_AUDIO: { |
| 3144 | const AudioTrack *ad = static_cast<const AudioTrack *>(t); |
| 3145 | _track_get_key_indices_in_range(ad->values, from_time, end, p_indices, false); |
| 3146 | _track_get_key_indices_in_range(ad->values, to_time, end, p_indices, true); |
| 3147 | } break; |
| 3148 | case TYPE_ANIMATION: { |
| 3149 | const AnimationTrack *an = static_cast<const AnimationTrack *>(t); |
| 3150 | _track_get_key_indices_in_range(an->values, from_time, end, p_indices, false); |
| 3151 | _track_get_key_indices_in_range(an->values, to_time, end, p_indices, true); |
| 3152 | } break; |
| 3153 | } |
| 3154 | return; |
| 3155 | } |
| 3156 | |
| 3157 | // The edge will be pingponged in the next frame and processed there, so let's ignore it now... |
| 3158 | if (!is_backward && Math::is_equal_approx(to_time, end)) { |
| 3159 | to_time -= CMP_EPSILON; |
| 3160 | } else if (is_backward && Math::is_equal_approx(from_time, start)) { |
| 3161 | from_time += CMP_EPSILON; |
| 3162 | } |
| 3163 | } break; |
| 3164 | } |
| 3165 | switch (t->type) { |
| 3166 | case TYPE_POSITION_3D: { |
| 3167 | const PositionTrack *tt = static_cast<const PositionTrack *>(t); |
| 3168 | if (tt->compressed_track >= 0) { |
| 3169 | _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, to_time - from_time, p_indices); |
| 3170 | } else { |
| 3171 | _track_get_key_indices_in_range(tt->positions, from_time, to_time, p_indices, is_backward); |
| 3172 | } |
| 3173 | } break; |
| 3174 | case TYPE_ROTATION_3D: { |
| 3175 | const RotationTrack *rt = static_cast<const RotationTrack *>(t); |
| 3176 | if (rt->compressed_track >= 0) { |
| 3177 | _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, to_time - from_time, p_indices); |
| 3178 | } else { |
| 3179 | _track_get_key_indices_in_range(rt->rotations, from_time, to_time, p_indices, is_backward); |
| 3180 | } |
| 3181 | } break; |
| 3182 | case TYPE_SCALE_3D: { |
| 3183 | const ScaleTrack *st = static_cast<const ScaleTrack *>(t); |
| 3184 | if (st->compressed_track >= 0) { |
| 3185 | _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, to_time - from_time, p_indices); |
| 3186 | } else { |
| 3187 | _track_get_key_indices_in_range(st->scales, from_time, to_time, p_indices, is_backward); |
| 3188 | } |
| 3189 | } break; |
| 3190 | case TYPE_BLEND_SHAPE: { |
| 3191 | const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t); |
| 3192 | if (bst->compressed_track >= 0) { |
| 3193 | _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, to_time - from_time, p_indices); |
| 3194 | } else { |
| 3195 | _track_get_key_indices_in_range(bst->blend_shapes, from_time, to_time, p_indices, is_backward); |
| 3196 | } |
| 3197 | } break; |
| 3198 | case TYPE_VALUE: { |
| 3199 | const ValueTrack *vt = static_cast<const ValueTrack *>(t); |
| 3200 | _track_get_key_indices_in_range(vt->values, from_time, to_time, p_indices, is_backward); |
| 3201 | } break; |
| 3202 | case TYPE_METHOD: { |
| 3203 | const MethodTrack *mt = static_cast<const MethodTrack *>(t); |
| 3204 | _track_get_key_indices_in_range(mt->methods, from_time, to_time, p_indices, is_backward); |
| 3205 | } break; |
| 3206 | case TYPE_BEZIER: { |
| 3207 | const BezierTrack *bz = static_cast<const BezierTrack *>(t); |
| 3208 | _track_get_key_indices_in_range(bz->values, from_time, to_time, p_indices, is_backward); |
| 3209 | } break; |
| 3210 | case TYPE_AUDIO: { |
| 3211 | const AudioTrack *ad = static_cast<const AudioTrack *>(t); |
| 3212 | _track_get_key_indices_in_range(ad->values, from_time, to_time, p_indices, is_backward); |
| 3213 | } break; |
| 3214 | case TYPE_ANIMATION: { |
| 3215 | const AnimationTrack *an = static_cast<const AnimationTrack *>(t); |
| 3216 | _track_get_key_indices_in_range(an->values, from_time, to_time, p_indices, is_backward); |
| 3217 | } break; |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | void Animation::add_marker(const StringName &p_name, double p_time) { |
| 3222 | int idx = _find(marker_names, p_time); |
| 3223 | |
| 3224 | if ((uint32_t)idx < marker_names.size() && Math::is_equal_approx(p_time, marker_names[idx].time)) { |
| 3225 | marker_times.erase(marker_names[idx].name); |
| 3226 | marker_colors.erase(marker_names[idx].name); |
| 3227 | marker_names[idx].name = p_name; |
| 3228 | marker_times.insert(p_name, p_time); |
| 3229 | marker_colors.insert(p_name, Color(1, 1, 1)); |
| 3230 | } else { |
| 3231 | _marker_insert(p_time, marker_names, MarkerKey(p_time, p_name)); |
| 3232 | marker_times.insert(p_name, p_time); |
| 3233 | marker_colors.insert(p_name, Color(1, 1, 1)); |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | void Animation::remove_marker(const StringName &p_name) { |
| 3238 | HashMap<StringName, double>::Iterator E = marker_times.find(p_name); |
| 3239 | ERR_FAIL_COND(!E); |
| 3240 | int idx = _find(marker_names, E->value); |
| 3241 | bool success = (uint32_t)idx < marker_names.size() && Math::is_equal_approx(marker_names[idx].time, E->value); |
| 3242 | ERR_FAIL_COND(!success); |
| 3243 | marker_names.remove_at(idx); |
| 3244 | marker_times.remove(E); |
| 3245 | marker_colors.erase(p_name); |
| 3246 | } |
| 3247 | |
| 3248 | bool Animation::has_marker(const StringName &p_name) const { |
| 3249 | return marker_times.has(p_name); |
| 3250 | } |
| 3251 | |
| 3252 | StringName Animation::get_marker_at_time(double p_time) const { |
| 3253 | int idx = _find(marker_names, p_time); |
| 3254 | |
| 3255 | if ((uint32_t)idx < marker_names.size() && Math::is_equal_approx(marker_names[idx].time, p_time)) { |
| 3256 | return marker_names[idx].name; |
| 3257 | } |
| 3258 | |
| 3259 | return StringName(); |
| 3260 | } |
| 3261 | |
| 3262 | StringName Animation::get_next_marker(double p_time) const { |
| 3263 | int idx = _find(marker_names, p_time); |
| 3264 | |
| 3265 | if (idx >= -1 && idx < (int)marker_names.size() - 1) { |
| 3266 | // _find ensures that the time at idx is always the closest time to p_time that is also smaller to it. |
| 3267 | // So we add 1 to get the next marker. |
| 3268 | return marker_names[idx + 1].name; |
| 3269 | } |
| 3270 | return StringName(); |
| 3271 | } |
| 3272 | |
| 3273 | StringName Animation::get_prev_marker(double p_time) const { |
| 3274 | int idx = _find(marker_names, p_time); |
| 3275 | |
| 3276 | if ((uint32_t)idx < marker_names.size()) { |
| 3277 | return marker_names[idx].name; |
| 3278 | } |
| 3279 | return StringName(); |
| 3280 | } |
| 3281 | |
| 3282 | double Animation::get_marker_time(const StringName &p_name) const { |
| 3283 | ERR_FAIL_COND_V(!marker_times.has(p_name), -1); |
| 3284 | return marker_times.get(p_name); |
| 3285 | } |
| 3286 | |
| 3287 | PackedStringArray Animation::get_marker_names() const { |
| 3288 | PackedStringArray names; |
| 3289 | // We iterate on marker_names so the result is sorted by time. |
| 3290 | for (const MarkerKey &marker_name : marker_names) { |
| 3291 | names.push_back(marker_name.name); |
| 3292 | } |
| 3293 | return names; |
| 3294 | } |
| 3295 | |
| 3296 | Color Animation::get_marker_color(const StringName &p_name) const { |
| 3297 | ERR_FAIL_COND_V(!marker_colors.has(p_name), Color()); |
| 3298 | return marker_colors[p_name]; |
| 3299 | } |
| 3300 | |
| 3301 | void Animation::set_marker_color(const StringName &p_name, const Color &p_color) { |
| 3302 | marker_colors[p_name] = p_color; |
| 3303 | } |
| 3304 | |
| 3305 | Vector<Variant> Animation::method_track_get_params(int p_track, int p_key_idx) const { |
| 3306 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), Vector<Variant>()); |
| 3307 | Track *t = tracks[p_track]; |
| 3308 | ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector<Variant>()); |
| 3309 | |
| 3310 | MethodTrack *pm = static_cast<MethodTrack *>(t); |
| 3311 | |
| 3312 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_key_idx, pm->methods.size(), Vector<Variant>()); |
| 3313 | |
| 3314 | const MethodKey &mk = pm->methods[p_key_idx]; |
| 3315 | |
| 3316 | return mk.params; |
| 3317 | } |
| 3318 | |
| 3319 | StringName Animation::method_track_get_name(int p_track, int p_key_idx) const { |
| 3320 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), StringName()); |
| 3321 | Track *t = tracks[p_track]; |
| 3322 | ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName()); |
| 3323 | |
| 3324 | MethodTrack *pm = static_cast<MethodTrack *>(t); |
| 3325 | |
| 3326 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_key_idx, pm->methods.size(), StringName()); |
| 3327 | |
| 3328 | return pm->methods[p_key_idx].method; |
| 3329 | } |
| 3330 | |
| 3331 | Array Animation::make_default_bezier_key(float p_value) { |
| 3332 | const double max_width = length / 2.0; |
| 3333 | Array new_point; |
| 3334 | new_point.resize(5); |
| 3335 | |
| 3336 | new_point[0] = p_value; |
| 3337 | new_point[1] = MAX(-0.25, -max_width); |
| 3338 | new_point[2] = 0; |
| 3339 | new_point[3] = MIN(0.25, max_width); |
| 3340 | new_point[4] = 0; |
| 3341 | |
| 3342 | return new_point; |
| 3343 | } |
| 3344 | |
| 3345 | 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) { |
| 3346 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), -1); |
| 3347 | Track *t = tracks[p_track]; |
| 3348 | ERR_FAIL_COND_V(t->type != TYPE_BEZIER, -1); |
| 3349 | |
| 3350 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3351 | |
| 3352 | TKey<BezierKey> k; |
| 3353 | k.time = p_time; |
| 3354 | k.value.value = p_value; |
| 3355 | k.value.in_handle = p_in_handle; |
| 3356 | if (k.value.in_handle.x > 0) { |
| 3357 | k.value.in_handle.x = 0; |
| 3358 | } |
| 3359 | k.value.out_handle = p_out_handle; |
| 3360 | if (k.value.out_handle.x < 0) { |
| 3361 | k.value.out_handle.x = 0; |
| 3362 | } |
| 3363 | |
| 3364 | int key = _insert(p_time, bt->values, k); |
| 3365 | |
| 3366 | emit_changed(); |
| 3367 | |
| 3368 | return key; |
| 3369 | } |
| 3370 | |
| 3371 | void Animation::bezier_track_set_key_value(int p_track, int p_index, real_t p_value) { |
| 3372 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3373 | Track *t = tracks[p_track]; |
| 3374 | ERR_FAIL_COND(t->type != TYPE_BEZIER); |
| 3375 | |
| 3376 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3377 | |
| 3378 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, bt->values.size()); |
| 3379 | |
| 3380 | bt->values[p_index].value.value = p_value; |
| 3381 | |
| 3382 | emit_changed(); |
| 3383 | } |
| 3384 | |
| 3385 | 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) { |
| 3386 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3387 | Track *t = tracks[p_track]; |
| 3388 | ERR_FAIL_COND(t->type != TYPE_BEZIER); |
| 3389 | |
| 3390 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3391 | |
| 3392 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, bt->values.size()); |
| 3393 | |
| 3394 | Vector2 in_handle = p_handle; |
| 3395 | if (in_handle.x > 0) { |
| 3396 | in_handle.x = 0; |
| 3397 | } |
| 3398 | bt->values[p_index].value.in_handle = in_handle; |
| 3399 | |
| 3400 | #ifdef TOOLS_ENABLED |
| 3401 | if (bt->values[p_index].value.handle_mode == HANDLE_MODE_LINEAR) { |
| 3402 | bt->values[p_index].value.in_handle = Vector2(); |
| 3403 | bt->values[p_index].value.out_handle = Vector2(); |
| 3404 | } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_BALANCED) { |
| 3405 | Transform2D xform; |
| 3406 | xform.set_scale(Vector2(1.0, 1.0 / p_balanced_value_time_ratio)); |
| 3407 | |
| 3408 | Vector2 vec_out = xform.xform(bt->values[p_index].value.out_handle); |
| 3409 | Vector2 vec_in = xform.xform(in_handle); |
| 3410 | |
| 3411 | bt->values[p_index].value.out_handle = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length()); |
| 3412 | } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_MIRRORED) { |
| 3413 | bt->values[p_index].value.out_handle = -in_handle; |
| 3414 | } |
| 3415 | #endif // TOOLS_ENABLED |
| 3416 | |
| 3417 | emit_changed(); |
| 3418 | } |
| 3419 | |
| 3420 | 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) { |
| 3421 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3422 | Track *t = tracks[p_track]; |
| 3423 | ERR_FAIL_COND(t->type != TYPE_BEZIER); |
| 3424 | |
| 3425 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3426 | |
| 3427 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, bt->values.size()); |
| 3428 | |
| 3429 | Vector2 out_handle = p_handle; |
| 3430 | if (out_handle.x < 0) { |
| 3431 | out_handle.x = 0; |
| 3432 | } |
| 3433 | bt->values[p_index].value.out_handle = out_handle; |
| 3434 | |
| 3435 | #ifdef TOOLS_ENABLED |
| 3436 | if (bt->values[p_index].value.handle_mode == HANDLE_MODE_LINEAR) { |
| 3437 | bt->values[p_index].value.in_handle = Vector2(); |
| 3438 | bt->values[p_index].value.out_handle = Vector2(); |
| 3439 | } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_BALANCED) { |
| 3440 | Transform2D xform; |
| 3441 | xform.set_scale(Vector2(1.0, 1.0 / p_balanced_value_time_ratio)); |
| 3442 | |
| 3443 | Vector2 vec_in = xform.xform(bt->values[p_index].value.in_handle); |
| 3444 | Vector2 vec_out = xform.xform(out_handle); |
| 3445 | |
| 3446 | bt->values[p_index].value.in_handle = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length()); |
| 3447 | } else if (bt->values[p_index].value.handle_mode == HANDLE_MODE_MIRRORED) { |
| 3448 | bt->values[p_index].value.in_handle = -out_handle; |
| 3449 | } |
| 3450 | #endif // TOOLS_ENABLED |
| 3451 | |
| 3452 | emit_changed(); |
| 3453 | } |
| 3454 | |
| 3455 | real_t Animation::bezier_track_get_key_value(int p_track, int p_index) const { |
| 3456 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), 0); |
| 3457 | Track *t = tracks[p_track]; |
| 3458 | ERR_FAIL_COND_V(t->type != TYPE_BEZIER, 0); |
| 3459 | |
| 3460 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3461 | |
| 3462 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, bt->values.size(), 0); |
| 3463 | |
| 3464 | return bt->values[p_index].value.value; |
| 3465 | } |
| 3466 | |
| 3467 | Vector2 Animation::bezier_track_get_key_in_handle(int p_track, int p_index) const { |
| 3468 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), Vector2()); |
| 3469 | Track *t = tracks[p_track]; |
| 3470 | ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2()); |
| 3471 | |
| 3472 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3473 | |
| 3474 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, bt->values.size(), Vector2()); |
| 3475 | |
| 3476 | return bt->values[p_index].value.in_handle; |
| 3477 | } |
| 3478 | |
| 3479 | Vector2 Animation::bezier_track_get_key_out_handle(int p_track, int p_index) const { |
| 3480 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), Vector2()); |
| 3481 | Track *t = tracks[p_track]; |
| 3482 | ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2()); |
| 3483 | |
| 3484 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3485 | |
| 3486 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, bt->values.size(), Vector2()); |
| 3487 | |
| 3488 | return bt->values[p_index].value.out_handle; |
| 3489 | } |
| 3490 | |
| 3491 | #ifdef TOOLS_ENABLED |
| 3492 | void Animation::bezier_track_set_key_handle_mode(int p_track, int p_index, HandleMode p_mode, HandleSetMode p_set_mode) { |
| 3493 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3494 | Track *t = tracks[p_track]; |
| 3495 | ERR_FAIL_COND(t->type != TYPE_BEZIER); |
| 3496 | |
| 3497 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3498 | |
| 3499 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, bt->values.size()); |
| 3500 | |
| 3501 | bt->values[p_index].value.handle_mode = p_mode; |
| 3502 | |
| 3503 | if (p_mode != HANDLE_MODE_FREE && p_set_mode != HANDLE_SET_MODE_NONE) { |
| 3504 | Vector2 &in_handle = bt->values[p_index].value.in_handle; |
| 3505 | Vector2 &out_handle = bt->values[p_index].value.out_handle; |
| 3506 | bezier_track_calculate_handles(p_track, p_index, p_mode, p_set_mode, &in_handle, &out_handle); |
| 3507 | } |
| 3508 | |
| 3509 | emit_changed(); |
| 3510 | } |
| 3511 | |
| 3512 | Animation::HandleMode Animation::bezier_track_get_key_handle_mode(int p_track, int p_index) const { |
| 3513 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), HANDLE_MODE_FREE); |
| 3514 | Track *t = tracks[p_track]; |
| 3515 | ERR_FAIL_COND_V(t->type != TYPE_BEZIER, HANDLE_MODE_FREE); |
| 3516 | |
| 3517 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3518 | |
| 3519 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, bt->values.size(), HANDLE_MODE_FREE); |
| 3520 | |
| 3521 | return bt->values[p_index].value.handle_mode; |
| 3522 | } |
| 3523 | |
| 3524 | 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) { |
| 3525 | ERR_FAIL_INDEX_V(p_track, (int)tracks.size(), false); |
| 3526 | Track *t = tracks[p_track]; |
| 3527 | ERR_FAIL_COND_V(t->type != TYPE_BEZIER, false); |
| 3528 | |
| 3529 | BezierTrack *bt = static_cast<BezierTrack *>(t); |
| 3530 | ERR_FAIL_INDEX_V(p_index, (int)bt->values.size(), false); |
| 3531 | |
| 3532 | int prev_key = MAX(0, p_index - 1); |
| 3533 | int next_key = MIN((int)bt->values.size() - 1, p_index + 1); |
| 3534 | if (prev_key == next_key) { |
| 3535 | return false; |
| 3536 | } |
| 3537 | |
| 3538 | float time = bt->values[p_index].time; |
| 3539 | float prev_time = bt->values[prev_key].time; |
| 3540 | float prev_value = bt->values[prev_key].value.value; |
| 3541 | float next_time = bt->values[next_key].time; |
| 3542 | float next_value = bt->values[next_key].value.value; |
| 3543 | |
| 3544 | 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); |
| 3545 | } |
| 3546 | |
| 3547 | 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) { |
| 3548 | ERR_FAIL_COND_V(p_mode == HANDLE_MODE_FREE, false); |
| 3549 | ERR_FAIL_COND_V(p_set_mode == HANDLE_SET_MODE_NONE, false); |
| 3550 | |
| 3551 | Vector2 in_handle; |
| 3552 | Vector2 out_handle; |
| 3553 | |
| 3554 | if (p_mode == HANDLE_MODE_LINEAR) { |
| 3555 | in_handle = Vector2(0, 0); |
| 3556 | out_handle = Vector2(0, 0); |
| 3557 | } else if (p_mode == HANDLE_MODE_BALANCED) { |
| 3558 | if (p_set_mode == HANDLE_SET_MODE_RESET) { |
| 3559 | real_t handle_length = 1.0 / 3.0; |
| 3560 | in_handle.x = (p_prev_time - p_time) * handle_length; |
| 3561 | in_handle.y = 0; |
| 3562 | out_handle.x = (p_next_time - p_time) * handle_length; |
| 3563 | out_handle.y = 0; |
| 3564 | } else if (p_set_mode == HANDLE_SET_MODE_AUTO) { |
| 3565 | real_t handle_length = 1.0 / 6.0; |
| 3566 | real_t tangent = (p_next_value - p_prev_value) / (p_next_time - p_prev_time); |
| 3567 | in_handle.x = (p_prev_time - p_time) * handle_length; |
| 3568 | in_handle.y = in_handle.x * tangent; |
| 3569 | out_handle.x = (p_next_time - p_time) * handle_length; |
| 3570 | out_handle.y = out_handle.x * tangent; |
| 3571 | } |
| 3572 | } else if (p_mode == HANDLE_MODE_MIRRORED) { |
| 3573 | real_t handle_length = 1.0 / 4.0; |
| 3574 | real_t prev_interval = Math::abs(p_time - p_prev_time); |
| 3575 | real_t next_interval = Math::abs(p_time - p_next_time); |
| 3576 | real_t min_time = 0; |
| 3577 | if (Math::is_zero_approx(prev_interval)) { |
| 3578 | min_time = next_interval; |
| 3579 | } else if (Math::is_zero_approx(next_interval)) { |
| 3580 | min_time = prev_interval; |
| 3581 | } else { |
| 3582 | min_time = MIN(prev_interval, next_interval); |
| 3583 | } |
| 3584 | if (p_set_mode == HANDLE_SET_MODE_RESET) { |
| 3585 | in_handle.x = -min_time * handle_length; |
| 3586 | in_handle.y = 0; |
| 3587 | out_handle.x = min_time * handle_length; |
| 3588 | out_handle.y = 0; |
| 3589 | } else if (p_set_mode == HANDLE_SET_MODE_AUTO) { |
| 3590 | real_t tangent = (p_next_value - p_prev_value) / min_time; |
| 3591 | in_handle.x = -min_time * handle_length; |
| 3592 | in_handle.y = in_handle.x * tangent; |
| 3593 | out_handle.x = min_time * handle_length; |
| 3594 | out_handle.y = out_handle.x * tangent; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | if (r_in_handle != nullptr) { |
| 3599 | *r_in_handle = in_handle; |
| 3600 | } |
| 3601 | |
| 3602 | if (r_out_handle != nullptr) { |
| 3603 | *r_out_handle = out_handle; |
| 3604 | } |
| 3605 | |
| 3606 | return true; |
| 3607 | } |
| 3608 | |
| 3609 | #endif // TOOLS_ENABLED |
| 3610 | |
| 3611 | real_t Animation::bezier_track_interpolate(int p_track, double p_time) const { |
| 3612 | //this uses a different interpolation scheme |
| 3613 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), 0); |
| 3614 | Track *track = tracks[p_track]; |
| 3615 | ERR_FAIL_COND_V(track->type != TYPE_BEZIER, 0); |
| 3616 | |
| 3617 | BezierTrack *bt = static_cast<BezierTrack *>(track); |
| 3618 | |
| 3619 | int len = _find(bt->values, length) + 1; // try to find last key (there may be more past the end) |
| 3620 | |
| 3621 | if (len <= 0) { |
| 3622 | // (-1 or -2 returned originally) (plus one above) |
| 3623 | return 0; |
| 3624 | } else if (len == 1) { // one key found (0+1), return it |
| 3625 | return bt->values[0].value.value; |
| 3626 | } |
| 3627 | |
| 3628 | int idx = _find(bt->values, p_time); |
| 3629 | |
| 3630 | ERR_FAIL_COND_V(idx == -2, 0); |
| 3631 | |
| 3632 | //there really is no looping interpolation on bezier |
| 3633 | |
| 3634 | if (idx < 0) { |
| 3635 | return bt->values[0].value.value; |
| 3636 | } |
| 3637 | |
| 3638 | if (idx >= (int)bt->values.size() - 1) { |
| 3639 | return bt->values[bt->values.size() - 1].value.value; |
| 3640 | } |
| 3641 | |
| 3642 | double t = p_time - bt->values[idx].time; |
| 3643 | |
| 3644 | int iterations = 10; |
| 3645 | |
| 3646 | real_t duration = bt->values[idx + 1].time - bt->values[idx].time; // time duration between our two keyframes |
| 3647 | real_t low = 0.0; // 0% of the current animation segment |
| 3648 | real_t high = 1.0; // 100% of the current animation segment |
| 3649 | |
| 3650 | Vector2 start(0, bt->values[idx].value.value); |
| 3651 | Vector2 start_out = start + bt->values[idx].value.out_handle; |
| 3652 | Vector2 end(duration, bt->values[idx + 1].value.value); |
| 3653 | Vector2 end_in = end + bt->values[idx + 1].value.in_handle; |
| 3654 | |
| 3655 | //narrow high and low as much as possible |
| 3656 | for (int i = 0; i < iterations; i++) { |
| 3657 | real_t middle = (low + high) / 2; |
| 3658 | |
| 3659 | Vector2 interp = start.bezier_interpolate(start_out, end_in, end, middle); |
| 3660 | |
| 3661 | if (interp.x < t) { |
| 3662 | low = middle; |
| 3663 | } else { |
| 3664 | high = middle; |
| 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | //interpolate the result: |
| 3669 | Vector2 low_pos = start.bezier_interpolate(start_out, end_in, end, low); |
| 3670 | Vector2 high_pos = start.bezier_interpolate(start_out, end_in, end, high); |
| 3671 | real_t c = (t - low_pos.x) / (high_pos.x - low_pos.x); |
| 3672 | |
| 3673 | return low_pos.lerp(high_pos, c).y; |
| 3674 | } |
| 3675 | |
| 3676 | 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) { |
| 3677 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), -1); |
| 3678 | Track *t = tracks[p_track]; |
| 3679 | ERR_FAIL_COND_V(t->type != TYPE_AUDIO, -1); |
| 3680 | |
| 3681 | AudioTrack *at = static_cast<AudioTrack *>(t); |
| 3682 | |
| 3683 | TKey<AudioKey> k; |
| 3684 | k.time = p_time; |
| 3685 | k.value.stream = p_stream; |
| 3686 | k.value.start_offset = p_start_offset; |
| 3687 | if (k.value.start_offset < 0) { |
| 3688 | k.value.start_offset = 0; |
| 3689 | } |
| 3690 | k.value.end_offset = p_end_offset; |
| 3691 | if (k.value.end_offset < 0) { |
| 3692 | k.value.end_offset = 0; |
| 3693 | } |
| 3694 | |
| 3695 | int key = _insert(p_time, at->values, k); |
| 3696 | |
| 3697 | emit_changed(); |
| 3698 | |
| 3699 | return key; |
| 3700 | } |
| 3701 | |
| 3702 | void Animation::audio_track_set_key_stream(int p_track, int p_key, const Ref<Resource> &p_stream) { |
| 3703 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3704 | Track *t = tracks[p_track]; |
| 3705 | ERR_FAIL_COND(t->type != TYPE_AUDIO); |
| 3706 | |
| 3707 | AudioTrack *at = static_cast<AudioTrack *>(t); |
| 3708 | |
| 3709 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key, at->values.size()); |
| 3710 | |
| 3711 | at->values[p_key].value.stream = p_stream; |
| 3712 | |
| 3713 | emit_changed(); |
| 3714 | } |
| 3715 | |
| 3716 | void Animation::audio_track_set_key_start_offset(int p_track, int p_key, real_t p_offset) { |
| 3717 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3718 | Track *t = tracks[p_track]; |
| 3719 | ERR_FAIL_COND(t->type != TYPE_AUDIO); |
| 3720 | |
| 3721 | AudioTrack *at = static_cast<AudioTrack *>(t); |
| 3722 | |
| 3723 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key, at->values.size()); |
| 3724 | |
| 3725 | if (p_offset < 0) { |
| 3726 | p_offset = 0; |
| 3727 | } |
| 3728 | |
| 3729 | at->values[p_key].value.start_offset = p_offset; |
| 3730 | |
| 3731 | emit_changed(); |
| 3732 | } |
| 3733 | |
| 3734 | void Animation::audio_track_set_key_end_offset(int p_track, int p_key, real_t p_offset) { |
| 3735 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3736 | Track *t = tracks[p_track]; |
| 3737 | ERR_FAIL_COND(t->type != TYPE_AUDIO); |
| 3738 | |
| 3739 | AudioTrack *at = static_cast<AudioTrack *>(t); |
| 3740 | |
| 3741 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key, at->values.size()); |
| 3742 | |
| 3743 | if (p_offset < 0) { |
| 3744 | p_offset = 0; |
| 3745 | } |
| 3746 | |
| 3747 | at->values[p_key].value.end_offset = p_offset; |
| 3748 | |
| 3749 | emit_changed(); |
| 3750 | } |
| 3751 | |
| 3752 | Ref<Resource> Animation::audio_track_get_key_stream(int p_track, int p_key) const { |
| 3753 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), Ref<Resource>()); |
| 3754 | const Track *t = tracks[p_track]; |
| 3755 | ERR_FAIL_COND_V(t->type != TYPE_AUDIO, Ref<Resource>()); |
| 3756 | |
| 3757 | const AudioTrack *at = static_cast<const AudioTrack *>(t); |
| 3758 | |
| 3759 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_key, at->values.size(), Ref<Resource>()); |
| 3760 | |
| 3761 | return at->values[p_key].value.stream; |
| 3762 | } |
| 3763 | |
| 3764 | real_t Animation::audio_track_get_key_start_offset(int p_track, int p_key) const { |
| 3765 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), 0); |
| 3766 | const Track *t = tracks[p_track]; |
| 3767 | ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0); |
| 3768 | |
| 3769 | const AudioTrack *at = static_cast<const AudioTrack *>(t); |
| 3770 | |
| 3771 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_key, at->values.size(), 0); |
| 3772 | |
| 3773 | return at->values[p_key].value.start_offset; |
| 3774 | } |
| 3775 | |
| 3776 | real_t Animation::audio_track_get_key_end_offset(int p_track, int p_key) const { |
| 3777 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), 0); |
| 3778 | const Track *t = tracks[p_track]; |
| 3779 | ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0); |
| 3780 | |
| 3781 | const AudioTrack *at = static_cast<const AudioTrack *>(t); |
| 3782 | |
| 3783 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_key, at->values.size(), 0); |
| 3784 | |
| 3785 | return at->values[p_key].value.end_offset; |
| 3786 | } |
| 3787 | |
| 3788 | void Animation::audio_track_set_use_blend(int p_track, bool p_enable) { |
| 3789 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3790 | Track *t = tracks[p_track]; |
| 3791 | ERR_FAIL_COND(t->type != TYPE_AUDIO); |
| 3792 | |
| 3793 | AudioTrack *at = static_cast<AudioTrack *>(t); |
| 3794 | |
| 3795 | at->use_blend = p_enable; |
| 3796 | emit_changed(); |
| 3797 | } |
| 3798 | |
| 3799 | bool Animation::audio_track_is_use_blend(int p_track) const { |
| 3800 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), false); |
| 3801 | Track *t = tracks[p_track]; |
| 3802 | ERR_FAIL_COND_V(t->type != TYPE_AUDIO, false); |
| 3803 | |
| 3804 | AudioTrack *at = static_cast<AudioTrack *>(t); |
| 3805 | |
| 3806 | return at->use_blend; |
| 3807 | } |
| 3808 | |
| 3809 | // |
| 3810 | |
| 3811 | int Animation::animation_track_insert_key(int p_track, double p_time, const StringName &p_animation) { |
| 3812 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), -1); |
| 3813 | Track *t = tracks[p_track]; |
| 3814 | ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, -1); |
| 3815 | |
| 3816 | AnimationTrack *at = static_cast<AnimationTrack *>(t); |
| 3817 | |
| 3818 | TKey<StringName> k; |
| 3819 | k.time = p_time; |
| 3820 | k.value = p_animation; |
| 3821 | |
| 3822 | int key = _insert(p_time, at->values, k); |
| 3823 | |
| 3824 | emit_changed(); |
| 3825 | |
| 3826 | return key; |
| 3827 | } |
| 3828 | |
| 3829 | void Animation::animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation) { |
| 3830 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3831 | Track *t = tracks[p_track]; |
| 3832 | ERR_FAIL_COND(t->type != TYPE_ANIMATION); |
| 3833 | |
| 3834 | AnimationTrack *at = static_cast<AnimationTrack *>(t); |
| 3835 | |
| 3836 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_key, at->values.size()); |
| 3837 | |
| 3838 | at->values[p_key].value = p_animation; |
| 3839 | |
| 3840 | emit_changed(); |
| 3841 | } |
| 3842 | |
| 3843 | StringName Animation::animation_track_get_key_animation(int p_track, int p_key) const { |
| 3844 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), StringName()); |
| 3845 | const Track *t = tracks[p_track]; |
| 3846 | ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, StringName()); |
| 3847 | |
| 3848 | const AnimationTrack *at = static_cast<const AnimationTrack *>(t); |
| 3849 | |
| 3850 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_key, at->values.size(), StringName()); |
| 3851 | |
| 3852 | return at->values[p_key].value; |
| 3853 | } |
| 3854 | |
| 3855 | void Animation::set_length(real_t p_length) { |
| 3856 | if (p_length < ANIM_MIN_LENGTH) { |
| 3857 | p_length = ANIM_MIN_LENGTH; |
| 3858 | } |
| 3859 | length = p_length; |
| 3860 | emit_changed(); |
| 3861 | } |
| 3862 | |
| 3863 | real_t Animation::get_length() const { |
| 3864 | return length; |
| 3865 | } |
| 3866 | |
| 3867 | void Animation::set_loop_mode(Animation::LoopMode p_loop_mode) { |
| 3868 | loop_mode = p_loop_mode; |
| 3869 | emit_changed(); |
| 3870 | } |
| 3871 | |
| 3872 | Animation::LoopMode Animation::get_loop_mode() const { |
| 3873 | return loop_mode; |
| 3874 | } |
| 3875 | |
| 3876 | void Animation::track_set_imported(int p_track, bool p_imported) { |
| 3877 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3878 | tracks[p_track]->imported = p_imported; |
| 3879 | } |
| 3880 | |
| 3881 | bool Animation::track_is_imported(int p_track) const { |
| 3882 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), false); |
| 3883 | return tracks[p_track]->imported; |
| 3884 | } |
| 3885 | |
| 3886 | void Animation::track_set_enabled(int p_track, bool p_enabled) { |
| 3887 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3888 | tracks[p_track]->enabled = p_enabled; |
| 3889 | emit_changed(); |
| 3890 | } |
| 3891 | |
| 3892 | bool Animation::track_is_enabled(int p_track) const { |
| 3893 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_track, tracks.size(), false); |
| 3894 | return tracks[p_track]->enabled; |
| 3895 | } |
| 3896 | |
| 3897 | void Animation::track_move_up(int p_track) { |
| 3898 | if (p_track < ((int)tracks.size() - 1)) { |
| 3899 | SWAP(tracks[p_track], tracks[p_track + 1]); |
| 3900 | } |
| 3901 | |
| 3902 | emit_changed(); |
| 3903 | } |
| 3904 | |
| 3905 | void Animation::track_move_down(int p_track) { |
| 3906 | if ((uint32_t)p_track < tracks.size()) { |
| 3907 | SWAP(tracks[p_track], tracks[p_track - 1]); |
| 3908 | } |
| 3909 | |
| 3910 | emit_changed(); |
| 3911 | } |
| 3912 | |
| 3913 | void Animation::track_move_to(int p_track, int p_to_index) { |
| 3914 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3915 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_to_index, tracks.size() + 1); |
| 3916 | if (p_track == p_to_index || p_track == p_to_index - 1) { |
| 3917 | return; |
| 3918 | } |
| 3919 | |
| 3920 | Track *track = tracks[p_track]; |
| 3921 | tracks.remove_at(p_track); |
| 3922 | // Take into account that the position of the tracks that come after the one removed will change. |
| 3923 | tracks.insert(p_to_index > p_track ? p_to_index - 1 : p_to_index, track); |
| 3924 | |
| 3925 | emit_changed(); |
| 3926 | } |
| 3927 | |
| 3928 | void Animation::track_swap(int p_track, int p_with_track) { |
| 3929 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_track, tracks.size()); |
| 3930 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_with_track, tracks.size()); |
| 3931 | if (p_track == p_with_track) { |
| 3932 | return; |
| 3933 | } |
| 3934 | SWAP(tracks[p_track], tracks[p_with_track]); |
| 3935 | |
| 3936 | emit_changed(); |
| 3937 | } |
| 3938 | |
| 3939 | void Animation::set_step(real_t p_step) { |
| 3940 | step = p_step; |
| 3941 | emit_changed(); |
| 3942 | } |
| 3943 | |
| 3944 | real_t Animation::get_step() const { |
| 3945 | return step; |
| 3946 | } |
| 3947 | |
| 3948 | void Animation::copy_track(int p_track, Ref<Animation> p_to_animation) { |
| 3949 | ERR_FAIL_COND(p_to_animation.is_null()); |
| 3950 | ERR_FAIL_INDEX(p_track, get_track_count()); |
| 3951 | int dst_track = p_to_animation->get_track_count(); |
| 3952 | p_to_animation->add_track(track_get_type(p_track)); |
| 3953 | |
| 3954 | p_to_animation->track_set_path(dst_track, track_get_path(p_track)); |
| 3955 | p_to_animation->track_set_imported(dst_track, track_is_imported(p_track)); |
| 3956 | p_to_animation->track_set_enabled(dst_track, track_is_enabled(p_track)); |
| 3957 | p_to_animation->track_set_interpolation_type(dst_track, track_get_interpolation_type(p_track)); |
| 3958 | p_to_animation->track_set_interpolation_loop_wrap(dst_track, track_get_interpolation_loop_wrap(p_track)); |
| 3959 | if (track_get_type(p_track) == TYPE_VALUE) { |
| 3960 | p_to_animation->value_track_set_update_mode(dst_track, value_track_get_update_mode(p_track)); |
| 3961 | } |
| 3962 | if (track_get_type(p_track) == TYPE_AUDIO) { |
| 3963 | p_to_animation->audio_track_set_use_blend(dst_track, audio_track_is_use_blend(p_track)); |
| 3964 | } |
| 3965 | |
| 3966 | for (int i = 0; i < track_get_key_count(p_track); i++) { |
| 3967 | 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)); |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | void Animation::_bind_methods() { |
| 3972 | ClassDB::bind_method(D_METHOD("add_track", "type", "at_position"), &Animation::add_track, DEFVAL(-1)); |
| 3973 | ClassDB::bind_method(D_METHOD("remove_track", "track_idx"), &Animation::remove_track); |
| 3974 | ClassDB::bind_method(D_METHOD("get_track_count"), &Animation::get_track_count); |
| 3975 | ClassDB::bind_method(D_METHOD("track_get_type", "track_idx"), &Animation::track_get_type); |
| 3976 | ClassDB::bind_method(D_METHOD("track_get_path", "track_idx"), &Animation::track_get_path); |
| 3977 | ClassDB::bind_method(D_METHOD("track_set_path", "track_idx", "path"), &Animation::track_set_path); |
| 3978 | ClassDB::bind_method(D_METHOD("find_track", "path", "type"), &Animation::find_track); |
| 3979 | |
| 3980 | ClassDB::bind_method(D_METHOD("track_move_up", "track_idx"), &Animation::track_move_up); |
| 3981 | ClassDB::bind_method(D_METHOD("track_move_down", "track_idx"), &Animation::track_move_down); |
| 3982 | ClassDB::bind_method(D_METHOD("track_move_to", "track_idx", "to_idx"), &Animation::track_move_to); |
| 3983 | ClassDB::bind_method(D_METHOD("track_swap", "track_idx", "with_idx"), &Animation::track_swap); |
| 3984 | |
| 3985 | ClassDB::bind_method(D_METHOD("track_set_imported", "track_idx", "imported"), &Animation::track_set_imported); |
| 3986 | ClassDB::bind_method(D_METHOD("track_is_imported", "track_idx"), &Animation::track_is_imported); |
| 3987 | |
| 3988 | ClassDB::bind_method(D_METHOD("track_set_enabled", "track_idx", "enabled"), &Animation::track_set_enabled); |
| 3989 | ClassDB::bind_method(D_METHOD("track_is_enabled", "track_idx"), &Animation::track_is_enabled); |
| 3990 | |
| 3991 | ClassDB::bind_method(D_METHOD("position_track_insert_key", "track_idx", "time", "position"), &Animation::position_track_insert_key); |
| 3992 | ClassDB::bind_method(D_METHOD("rotation_track_insert_key", "track_idx", "time", "rotation"), &Animation::rotation_track_insert_key); |
| 3993 | ClassDB::bind_method(D_METHOD("scale_track_insert_key", "track_idx", "time", "scale"), &Animation::scale_track_insert_key); |
| 3994 | ClassDB::bind_method(D_METHOD("blend_shape_track_insert_key", "track_idx", "time", "amount"), &Animation::blend_shape_track_insert_key); |
| 3995 | |
| 3996 | ClassDB::bind_method(D_METHOD("position_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::position_track_interpolate, DEFVAL(false)); |
| 3997 | ClassDB::bind_method(D_METHOD("rotation_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::rotation_track_interpolate, DEFVAL(false)); |
| 3998 | ClassDB::bind_method(D_METHOD("scale_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::scale_track_interpolate, DEFVAL(false)); |
| 3999 | ClassDB::bind_method(D_METHOD("blend_shape_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::blend_shape_track_interpolate, DEFVAL(false)); |
| 4000 | |
| 4001 | ClassDB::bind_method(D_METHOD("track_insert_key", "track_idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1)); |
| 4002 | ClassDB::bind_method(D_METHOD("track_remove_key", "track_idx", "key_idx"), &Animation::track_remove_key); |
| 4003 | ClassDB::bind_method(D_METHOD("track_remove_key_at_time", "track_idx", "time"), &Animation::track_remove_key_at_time); |
| 4004 | ClassDB::bind_method(D_METHOD("track_set_key_value", "track_idx", "key", "value"), &Animation::track_set_key_value); |
| 4005 | ClassDB::bind_method(D_METHOD("track_set_key_transition", "track_idx", "key_idx", "transition"), &Animation::track_set_key_transition); |
| 4006 | ClassDB::bind_method(D_METHOD("track_set_key_time", "track_idx", "key_idx", "time"), &Animation::track_set_key_time); |
| 4007 | ClassDB::bind_method(D_METHOD("track_get_key_transition", "track_idx", "key_idx"), &Animation::track_get_key_transition); |
| 4008 | |
| 4009 | ClassDB::bind_method(D_METHOD("track_get_key_count", "track_idx"), &Animation::track_get_key_count); |
| 4010 | ClassDB::bind_method(D_METHOD("track_get_key_value", "track_idx", "key_idx"), &Animation::track_get_key_value); |
| 4011 | ClassDB::bind_method(D_METHOD("track_get_key_time", "track_idx", "key_idx"), &Animation::track_get_key_time); |
| 4012 | 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)); |
| 4013 | |
| 4014 | ClassDB::bind_method(D_METHOD("track_set_interpolation_type", "track_idx", "interpolation"), &Animation::track_set_interpolation_type); |
| 4015 | ClassDB::bind_method(D_METHOD("track_get_interpolation_type", "track_idx"), &Animation::track_get_interpolation_type); |
| 4016 | |
| 4017 | ClassDB::bind_method(D_METHOD("track_set_interpolation_loop_wrap", "track_idx", "interpolation"), &Animation::track_set_interpolation_loop_wrap); |
| 4018 | ClassDB::bind_method(D_METHOD("track_get_interpolation_loop_wrap", "track_idx"), &Animation::track_get_interpolation_loop_wrap); |
| 4019 | |
| 4020 | ClassDB::bind_method(D_METHOD("track_is_compressed", "track_idx"), &Animation::track_is_compressed); |
| 4021 | |
| 4022 | ClassDB::bind_method(D_METHOD("value_track_set_update_mode", "track_idx", "mode"), &Animation::value_track_set_update_mode); |
| 4023 | ClassDB::bind_method(D_METHOD("value_track_get_update_mode", "track_idx"), &Animation::value_track_get_update_mode); |
| 4024 | |
| 4025 | ClassDB::bind_method(D_METHOD("value_track_interpolate", "track_idx", "time_sec", "backward"), &Animation::value_track_interpolate, DEFVAL(false)); |
| 4026 | |
| 4027 | ClassDB::bind_method(D_METHOD("method_track_get_name", "track_idx", "key_idx"), &Animation::method_track_get_name); |
| 4028 | ClassDB::bind_method(D_METHOD("method_track_get_params", "track_idx", "key_idx"), &Animation::method_track_get_params); |
| 4029 | |
| 4030 | 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())); |
| 4031 | |
| 4032 | ClassDB::bind_method(D_METHOD("bezier_track_set_key_value", "track_idx", "key_idx", "value"), &Animation::bezier_track_set_key_value); |
| 4033 | 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)); |
| 4034 | 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)); |
| 4035 | |
| 4036 | ClassDB::bind_method(D_METHOD("bezier_track_get_key_value", "track_idx", "key_idx"), &Animation::bezier_track_get_key_value); |
| 4037 | ClassDB::bind_method(D_METHOD("bezier_track_get_key_in_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_in_handle); |
| 4038 | ClassDB::bind_method(D_METHOD("bezier_track_get_key_out_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_out_handle); |
| 4039 | |
| 4040 | ClassDB::bind_method(D_METHOD("bezier_track_interpolate", "track_idx", "time"), &Animation::bezier_track_interpolate); |
| 4041 | |
| 4042 | 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)); |
| 4043 | ClassDB::bind_method(D_METHOD("audio_track_set_key_stream", "track_idx", "key_idx", "stream"), &Animation::audio_track_set_key_stream); |
| 4044 | ClassDB::bind_method(D_METHOD("audio_track_set_key_start_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_start_offset); |
| 4045 | ClassDB::bind_method(D_METHOD("audio_track_set_key_end_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_end_offset); |
| 4046 | ClassDB::bind_method(D_METHOD("audio_track_get_key_stream", "track_idx", "key_idx"), &Animation::audio_track_get_key_stream); |
| 4047 | ClassDB::bind_method(D_METHOD("audio_track_get_key_start_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_start_offset); |
| 4048 | ClassDB::bind_method(D_METHOD("audio_track_get_key_end_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_end_offset); |
| 4049 | ClassDB::bind_method(D_METHOD("audio_track_set_use_blend", "track_idx", "enable"), &Animation::audio_track_set_use_blend); |
| 4050 | ClassDB::bind_method(D_METHOD("audio_track_is_use_blend", "track_idx"), &Animation::audio_track_is_use_blend); |
| 4051 | |
| 4052 | ClassDB::bind_method(D_METHOD("animation_track_insert_key", "track_idx", "time", "animation"), &Animation::animation_track_insert_key); |
| 4053 | ClassDB::bind_method(D_METHOD("animation_track_set_key_animation", "track_idx", "key_idx", "animation"), &Animation::animation_track_set_key_animation); |
| 4054 | ClassDB::bind_method(D_METHOD("animation_track_get_key_animation", "track_idx", "key_idx"), &Animation::animation_track_get_key_animation); |
| 4055 | |
| 4056 | ClassDB::bind_method(D_METHOD("add_marker", "name", "time"), &Animation::add_marker); |
| 4057 | ClassDB::bind_method(D_METHOD("remove_marker", "name"), &Animation::remove_marker); |
| 4058 | ClassDB::bind_method(D_METHOD("has_marker", "name"), &Animation::has_marker); |
| 4059 | ClassDB::bind_method(D_METHOD("get_marker_at_time", "time"), &Animation::get_marker_at_time); |
| 4060 | ClassDB::bind_method(D_METHOD("get_next_marker", "time"), &Animation::get_next_marker); |
| 4061 | ClassDB::bind_method(D_METHOD("get_prev_marker", "time"), &Animation::get_prev_marker); |
| 4062 | ClassDB::bind_method(D_METHOD("get_marker_time", "name"), &Animation::get_marker_time); |
| 4063 | ClassDB::bind_method(D_METHOD("get_marker_names"), &Animation::get_marker_names); |
| 4064 | ClassDB::bind_method(D_METHOD("get_marker_color", "name"), &Animation::get_marker_color); |
| 4065 | ClassDB::bind_method(D_METHOD("set_marker_color", "name", "color"), &Animation::set_marker_color); |
| 4066 | |
| 4067 | ClassDB::bind_method(D_METHOD("set_length", "time_sec"), &Animation::set_length); |
| 4068 | ClassDB::bind_method(D_METHOD("get_length"), &Animation::get_length); |
| 4069 | |
| 4070 | ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &Animation::set_loop_mode); |
| 4071 | ClassDB::bind_method(D_METHOD("get_loop_mode"), &Animation::get_loop_mode); |
| 4072 | |
| 4073 | ClassDB::bind_method(D_METHOD("set_step", "size_sec"), &Animation::set_step); |
| 4074 | ClassDB::bind_method(D_METHOD("get_step"), &Animation::get_step); |
| 4075 | |
| 4076 | ClassDB::bind_method(D_METHOD("clear"), &Animation::clear); |
| 4077 | ClassDB::bind_method(D_METHOD("copy_track", "track_idx", "to_animation"), &Animation::copy_track); |
| 4078 | |
| 4079 | ClassDB::bind_method(D_METHOD("optimize", "allowed_velocity_err", "allowed_angular_err", "precision"), &Animation::optimize, DEFVAL(0.01), DEFVAL(0.01), DEFVAL(3)); |
| 4080 | ClassDB::bind_method(D_METHOD("compress", "page_size", "fps", "split_tolerance"), &Animation::compress, DEFVAL(8192), DEFVAL(120), DEFVAL(4.0)); |
| 4081 | |
| 4082 | ClassDB::bind_method(D_METHOD("is_capture_included"), &Animation::is_capture_included); |
| 4083 | |
| 4084 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001,suffix:s"), "set_length", "get_length"); |
| 4085 | ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode"); |
| 4086 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step", PROPERTY_HINT_RANGE, "0,4096,0.001,suffix:s"), "set_step", "get_step"); |
| 4087 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "capture_included", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "", "is_capture_included"); |
| 4088 | |
| 4089 | BIND_ENUM_CONSTANT(TYPE_VALUE); |
| 4090 | BIND_ENUM_CONSTANT(TYPE_POSITION_3D); |
| 4091 | BIND_ENUM_CONSTANT(TYPE_ROTATION_3D); |
| 4092 | BIND_ENUM_CONSTANT(TYPE_SCALE_3D); |
| 4093 | BIND_ENUM_CONSTANT(TYPE_BLEND_SHAPE); |
| 4094 | BIND_ENUM_CONSTANT(TYPE_METHOD); |
| 4095 | BIND_ENUM_CONSTANT(TYPE_BEZIER); |
| 4096 | BIND_ENUM_CONSTANT(TYPE_AUDIO); |
| 4097 | BIND_ENUM_CONSTANT(TYPE_ANIMATION); |
| 4098 | |
| 4099 | BIND_ENUM_CONSTANT(INTERPOLATION_NEAREST); |
| 4100 | BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR); |
| 4101 | BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC); |
| 4102 | BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR_ANGLE); |
| 4103 | BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC_ANGLE); |
| 4104 | |
| 4105 | BIND_ENUM_CONSTANT(UPDATE_CONTINUOUS); |
| 4106 | BIND_ENUM_CONSTANT(UPDATE_DISCRETE); |
| 4107 | BIND_ENUM_CONSTANT(UPDATE_CAPTURE); |
| 4108 | |
| 4109 | BIND_ENUM_CONSTANT(LOOP_NONE); |
| 4110 | BIND_ENUM_CONSTANT(LOOP_LINEAR); |
| 4111 | BIND_ENUM_CONSTANT(LOOP_PINGPONG); |
| 4112 | |
| 4113 | BIND_ENUM_CONSTANT(LOOPED_FLAG_NONE); |
| 4114 | BIND_ENUM_CONSTANT(LOOPED_FLAG_END); |
| 4115 | BIND_ENUM_CONSTANT(LOOPED_FLAG_START); |
| 4116 | |
| 4117 | BIND_ENUM_CONSTANT(FIND_MODE_NEAREST); |
| 4118 | BIND_ENUM_CONSTANT(FIND_MODE_APPROX); |
| 4119 | BIND_ENUM_CONSTANT(FIND_MODE_EXACT); |
| 4120 | } |
| 4121 | |
| 4122 | void Animation::clear() { |
| 4123 | for (uint32_t i = 0; i < tracks.size(); i++) { |
| 4124 | memdelete(tracks[i]); |
| 4125 | } |
| 4126 | tracks.clear(); |
| 4127 | loop_mode = LOOP_NONE; |
| 4128 | length = 1; |
| 4129 | compression.enabled = false; |
| 4130 | compression.bounds.clear(); |
| 4131 | compression.pages.clear(); |
| 4132 | compression.fps = 120; |
| 4133 | emit_changed(); |
| 4134 | } |
| 4135 | |
| 4136 | 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) { |
| 4137 | // Remove overlapping keys. |
| 4138 | if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) { |
| 4139 | return true; |
| 4140 | } |
| 4141 | if (std::abs(t0.value - t1.value) < p_allowed_precision_error && std::abs(t1.value - t2.value) < p_allowed_precision_error) { |
| 4142 | return true; |
| 4143 | } |
| 4144 | if (p_is_nearest) { |
| 4145 | return false; |
| 4146 | } |
| 4147 | // Calc velocities. |
| 4148 | double v0 = (t1.value - t0.value) / (t1.time - t0.time); |
| 4149 | double v1 = (t2.value - t1.value) / (t2.time - t1.time); |
| 4150 | // Avoid zero div but check equality. |
| 4151 | if (std::abs(v0 - v1) < p_allowed_precision_error) { |
| 4152 | return true; |
| 4153 | } else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) { |
| 4154 | return false; |
| 4155 | } |
| 4156 | if (!std::signbit(v0 * v1)) { |
| 4157 | v0 = std::abs(v0); |
| 4158 | v1 = std::abs(v1); |
| 4159 | double ratio = v0 < v1 ? v0 / v1 : v1 / v0; |
| 4160 | if (ratio >= 1.0 - p_allowed_velocity_err) { |
| 4161 | return true; |
| 4162 | } |
| 4163 | } |
| 4164 | return false; |
| 4165 | } |
| 4166 | |
| 4167 | 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) { |
| 4168 | // Remove overlapping keys. |
| 4169 | if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) { |
| 4170 | return true; |
| 4171 | } |
| 4172 | if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) { |
| 4173 | return true; |
| 4174 | } |
| 4175 | if (p_is_nearest) { |
| 4176 | return false; |
| 4177 | } |
| 4178 | // Calc velocities. |
| 4179 | Vector2 vc0 = (t1.value - t0.value) / (t1.time - t0.time); |
| 4180 | Vector2 vc1 = (t2.value - t1.value) / (t2.time - t1.time); |
| 4181 | double v0 = vc0.length(); |
| 4182 | double v1 = vc1.length(); |
| 4183 | // Avoid zero div but check equality. |
| 4184 | if (std::abs(v0 - v1) < p_allowed_precision_error) { |
| 4185 | return true; |
| 4186 | } else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) { |
| 4187 | return false; |
| 4188 | } |
| 4189 | // Check axis. |
| 4190 | if (vc0.normalized().dot(vc1.normalized()) >= 1.0 - p_allowed_angular_error * 2.0) { |
| 4191 | v0 = std::abs(v0); |
| 4192 | v1 = std::abs(v1); |
| 4193 | double ratio = v0 < v1 ? v0 / v1 : v1 / v0; |
| 4194 | if (ratio >= 1.0 - p_allowed_velocity_err) { |
| 4195 | return true; |
| 4196 | } |
| 4197 | } |
| 4198 | return false; |
| 4199 | } |
| 4200 | |
| 4201 | 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) { |
| 4202 | // Remove overlapping keys. |
| 4203 | if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) { |
| 4204 | return true; |
| 4205 | } |
| 4206 | if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) { |
| 4207 | return true; |
| 4208 | } |
| 4209 | if (p_is_nearest) { |
| 4210 | return false; |
| 4211 | } |
| 4212 | |
| 4213 | // Calc velocities. |
| 4214 | Vector3 vc0 = (t1.value - t0.value) / (t1.time - t0.time); |
| 4215 | Vector3 vc1 = (t2.value - t1.value) / (t2.time - t1.time); |
| 4216 | double v0 = vc0.length(); |
| 4217 | double v1 = vc1.length(); |
| 4218 | // Avoid zero div but check equality. |
| 4219 | if (std::abs(v0 - v1) < p_allowed_precision_error) { |
| 4220 | return true; |
| 4221 | } else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) { |
| 4222 | return false; |
| 4223 | } |
| 4224 | // Check axis. |
| 4225 | if (vc0.normalized().dot(vc1.normalized()) >= 1.0 - p_allowed_angular_error * 2.0) { |
| 4226 | v0 = std::abs(v0); |
| 4227 | v1 = std::abs(v1); |
| 4228 | double ratio = v0 < v1 ? v0 / v1 : v1 / v0; |
| 4229 | if (ratio >= 1.0 - p_allowed_velocity_err) { |
| 4230 | return true; |
| 4231 | } |
| 4232 | } |
| 4233 | return false; |
| 4234 | } |
| 4235 | |
| 4236 | 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) { |
| 4237 | // Remove overlapping keys. |
| 4238 | if (Math::is_equal_approx(t0.time, t1.time) || Math::is_equal_approx(t1.time, t2.time)) { |
| 4239 | return true; |
| 4240 | } |
| 4241 | if ((t0.value - t1.value).length() < p_allowed_precision_error && (t1.value - t2.value).length() < p_allowed_precision_error) { |
| 4242 | return true; |
| 4243 | } |
| 4244 | if (p_is_nearest) { |
| 4245 | return false; |
| 4246 | } |
| 4247 | // Check axis. |
| 4248 | Quaternion q0 = t0.value * t1.value * t0.value.inverse(); |
| 4249 | Quaternion q1 = t1.value * t2.value * t1.value.inverse(); |
| 4250 | if (q0.get_axis().dot(q1.get_axis()) >= 1.0 - p_allowed_angular_error * 2.0) { |
| 4251 | double a0 = Math::acos(t0.value.dot(t1.value)); |
| 4252 | double a1 = Math::acos(t1.value.dot(t2.value)); |
| 4253 | if (a0 + a1 >= Math::PI / 2) { |
| 4254 | return false; // Rotation is more than 180 deg, keep key. |
| 4255 | } |
| 4256 | // Calc velocities. |
| 4257 | double v0 = a0 / (t1.time - t0.time); |
| 4258 | double v1 = a1 / (t2.time - t1.time); |
| 4259 | // Avoid zero div but check equality. |
| 4260 | if (std::abs(v0 - v1) < p_allowed_precision_error) { |
| 4261 | return true; |
| 4262 | } else if (std::abs(v0) < p_allowed_precision_error || std::abs(v1) < p_allowed_precision_error) { |
| 4263 | return false; |
| 4264 | } |
| 4265 | double ratio = v0 < v1 ? v0 / v1 : v1 / v0; |
| 4266 | if (ratio >= 1.0 - p_allowed_velocity_err) { |
| 4267 | return true; |
| 4268 | } |
| 4269 | } |
| 4270 | return false; |
| 4271 | } |
| 4272 | |
| 4273 | 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) { |
| 4274 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_idx, tracks.size()); |
| 4275 | ERR_FAIL_COND(tracks[p_idx]->type != TYPE_POSITION_3D); |
| 4276 | bool is_nearest = false; |
| 4277 | if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) { |
| 4278 | is_nearest = true; |
| 4279 | } else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) { |
| 4280 | return; |
| 4281 | } |
| 4282 | PositionTrack *tt = static_cast<PositionTrack *>(tracks[p_idx]); |
| 4283 | int i = 0; |
| 4284 | while (i < (int)tt->positions.size() - 2) { |
| 4285 | TKey<Vector3> t0 = tt->positions[i]; |
| 4286 | TKey<Vector3> t1 = tt->positions[i + 1]; |
| 4287 | TKey<Vector3> t2 = tt->positions[i + 2]; |
| 4288 | bool erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest); |
| 4289 | if (erase) { |
| 4290 | tt->positions.remove_at(i + 1); |
| 4291 | } else { |
| 4292 | i++; |
| 4293 | } |
| 4294 | } |
| 4295 | |
| 4296 | if (tt->positions.size() == 2) { |
| 4297 | if ((tt->positions[0].value - tt->positions[1].value).length() < p_allowed_precision_error) { |
| 4298 | tt->positions.remove_at(1); |
| 4299 | } |
| 4300 | } |
| 4301 | } |
| 4302 | |
| 4303 | 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) { |
| 4304 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_idx, tracks.size()); |
| 4305 | ERR_FAIL_COND(tracks[p_idx]->type != TYPE_ROTATION_3D); |
| 4306 | bool is_nearest = false; |
| 4307 | if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) { |
| 4308 | is_nearest = true; |
| 4309 | } else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) { |
| 4310 | return; |
| 4311 | } |
| 4312 | RotationTrack *rt = static_cast<RotationTrack *>(tracks[p_idx]); |
| 4313 | int i = 0; |
| 4314 | while (i < (int)rt->rotations.size() - 2) { |
| 4315 | TKey<Quaternion> t0 = rt->rotations[i]; |
| 4316 | TKey<Quaternion> t1 = rt->rotations[i + 1]; |
| 4317 | TKey<Quaternion> t2 = rt->rotations[i + 2]; |
| 4318 | bool erase = _quaternion_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest); |
| 4319 | if (erase) { |
| 4320 | rt->rotations.remove_at(i + 1); |
| 4321 | } else { |
| 4322 | i++; |
| 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | if (rt->rotations.size() == 2) { |
| 4327 | if ((rt->rotations[0].value - rt->rotations[1].value).length() < p_allowed_precision_error) { |
| 4328 | rt->rotations.remove_at(1); |
| 4329 | } |
| 4330 | } |
| 4331 | } |
| 4332 | |
| 4333 | 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) { |
| 4334 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_idx, tracks.size()); |
| 4335 | ERR_FAIL_COND(tracks[p_idx]->type != TYPE_SCALE_3D); |
| 4336 | bool is_nearest = false; |
| 4337 | if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) { |
| 4338 | is_nearest = true; |
| 4339 | } else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) { |
| 4340 | return; |
| 4341 | } |
| 4342 | ScaleTrack *st = static_cast<ScaleTrack *>(tracks[p_idx]); |
| 4343 | int i = 0; |
| 4344 | while (i < (int)st->scales.size() - 2) { |
| 4345 | TKey<Vector3> t0 = st->scales[i]; |
| 4346 | TKey<Vector3> t1 = st->scales[i + 1]; |
| 4347 | TKey<Vector3> t2 = st->scales[i + 2]; |
| 4348 | bool erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest); |
| 4349 | if (erase) { |
| 4350 | st->scales.remove_at(i + 1); |
| 4351 | } else { |
| 4352 | i++; |
| 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | if (st->scales.size() == 2) { |
| 4357 | if ((st->scales[0].value - st->scales[1].value).length() < p_allowed_precision_error) { |
| 4358 | st->scales.remove_at(1); |
| 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | void Animation::_blend_shape_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_precision_error) { |
| 4364 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_idx, tracks.size()); |
| 4365 | ERR_FAIL_COND(tracks[p_idx]->type != TYPE_BLEND_SHAPE); |
| 4366 | bool is_nearest = false; |
| 4367 | if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) { |
| 4368 | is_nearest = true; |
| 4369 | } else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR) { |
| 4370 | return; |
| 4371 | } |
| 4372 | BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(tracks[p_idx]); |
| 4373 | int i = 0; |
| 4374 | while (i < (int)bst->blend_shapes.size() - 2) { |
| 4375 | TKey<float> t0 = bst->blend_shapes[i]; |
| 4376 | TKey<float> t1 = bst->blend_shapes[i + 1]; |
| 4377 | TKey<float> t2 = bst->blend_shapes[i + 2]; |
| 4378 | |
| 4379 | bool erase = _float_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_precision_error, is_nearest); |
| 4380 | if (erase) { |
| 4381 | bst->blend_shapes.remove_at(i + 1); |
| 4382 | } else { |
| 4383 | i++; |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | if (bst->blend_shapes.size() == 2) { |
| 4388 | if (std::abs(bst->blend_shapes[0].value - bst->blend_shapes[1].value) < p_allowed_precision_error) { |
| 4389 | bst->blend_shapes.remove_at(1); |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | 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) { |
| 4395 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_idx, tracks.size()); |
| 4396 | ERR_FAIL_COND(tracks[p_idx]->type != TYPE_VALUE); |
| 4397 | bool is_nearest = false; |
| 4398 | if (tracks[p_idx]->interpolation == INTERPOLATION_NEAREST) { |
| 4399 | is_nearest = true; |
| 4400 | } else if (tracks[p_idx]->interpolation != INTERPOLATION_LINEAR && tracks[p_idx]->interpolation != INTERPOLATION_LINEAR_ANGLE) { |
| 4401 | return; |
| 4402 | } |
| 4403 | ValueTrack *vt = static_cast<ValueTrack *>(tracks[p_idx]); |
| 4404 | if (vt->values.is_empty()) { |
| 4405 | return; |
| 4406 | } |
| 4407 | Variant::Type type = vt->values[0].value.get_type(); |
| 4408 | |
| 4409 | // Special case for angle interpolation. |
| 4410 | bool is_using_angle = vt->interpolation == Animation::INTERPOLATION_LINEAR_ANGLE || vt->interpolation == Animation::INTERPOLATION_CUBIC_ANGLE; |
| 4411 | int i = 0; |
| 4412 | while (i < (int)vt->values.size() - 2) { |
| 4413 | bool erase = false; |
| 4414 | switch (type) { |
| 4415 | case Variant::FLOAT: { |
| 4416 | TKey<float> t0; |
| 4417 | TKey<float> t1; |
| 4418 | TKey<float> t2; |
| 4419 | t0.time = vt->values[i].time; |
| 4420 | t1.time = vt->values[i + 1].time; |
| 4421 | t2.time = vt->values[i + 2].time; |
| 4422 | t0.value = vt->values[i].value; |
| 4423 | t1.value = vt->values[i + 1].value; |
| 4424 | t2.value = vt->values[i + 2].value; |
| 4425 | if (is_using_angle) { |
| 4426 | float diff1 = std::fmod(t1.value - t0.value, Math::TAU); |
| 4427 | t1.value = t0.value + std::fmod(2.0 * diff1, Math::TAU) - diff1; |
| 4428 | float diff2 = std::fmod(t2.value - t1.value, Math::TAU); |
| 4429 | t2.value = t1.value + std::fmod(2.0 * diff2, Math::TAU) - diff2; |
| 4430 | if (std::abs(std::abs(diff1) + std::abs(diff2)) >= Math::PI) { |
| 4431 | break; // Rotation is more than 180 deg, keep key. |
| 4432 | } |
| 4433 | } |
| 4434 | erase = _float_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_precision_error, is_nearest); |
| 4435 | } break; |
| 4436 | case Variant::VECTOR2: { |
| 4437 | TKey<Vector2> t0; |
| 4438 | TKey<Vector2> t1; |
| 4439 | TKey<Vector2> t2; |
| 4440 | t0.time = vt->values[i].time; |
| 4441 | t1.time = vt->values[i + 1].time; |
| 4442 | t2.time = vt->values[i + 2].time; |
| 4443 | t0.value = vt->values[i].value; |
| 4444 | t1.value = vt->values[i + 1].value; |
| 4445 | t2.value = vt->values[i + 2].value; |
| 4446 | erase = _vector2_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest); |
| 4447 | } break; |
| 4448 | case Variant::VECTOR3: { |
| 4449 | TKey<Vector3> t0; |
| 4450 | TKey<Vector3> t1; |
| 4451 | TKey<Vector3> t2; |
| 4452 | t0.time = vt->values[i].time; |
| 4453 | t1.time = vt->values[i + 1].time; |
| 4454 | t2.time = vt->values[i + 2].time; |
| 4455 | t0.value = vt->values[i].value; |
| 4456 | t1.value = vt->values[i + 1].value; |
| 4457 | t2.value = vt->values[i + 2].value; |
| 4458 | erase = _vector3_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest); |
| 4459 | } break; |
| 4460 | case Variant::QUATERNION: { |
| 4461 | TKey<Quaternion> t0; |
| 4462 | TKey<Quaternion> t1; |
| 4463 | TKey<Quaternion> t2; |
| 4464 | t0.time = vt->values[i].time; |
| 4465 | t1.time = vt->values[i + 1].time; |
| 4466 | t2.time = vt->values[i + 2].time; |
| 4467 | t0.value = vt->values[i].value; |
| 4468 | t1.value = vt->values[i + 1].value; |
| 4469 | t2.value = vt->values[i + 2].value; |
| 4470 | erase = _quaternion_track_optimize_key(t0, t1, t2, p_allowed_velocity_err, p_allowed_angular_err, p_allowed_precision_error, is_nearest); |
| 4471 | } break; |
| 4472 | default: { |
| 4473 | } break; |
| 4474 | } |
| 4475 | |
| 4476 | if (erase) { |
| 4477 | vt->values.remove_at(i + 1); |
| 4478 | } else { |
| 4479 | i++; |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | if (vt->values.size() == 2) { |
| 4484 | bool single_key = false; |
| 4485 | switch (type) { |
| 4486 | case Variant::FLOAT: { |
| 4487 | float val_0 = vt->values[0].value; |
| 4488 | float val_1 = vt->values[1].value; |
| 4489 | if (is_using_angle) { |
| 4490 | float diff1 = std::fmod(val_1 - val_0, Math::TAU); |
| 4491 | val_1 = val_0 + std::fmod(2.0 * diff1, Math::TAU) - diff1; |
| 4492 | } |
| 4493 | single_key = std::abs(val_0 - val_1) < p_allowed_precision_error; |
| 4494 | } break; |
| 4495 | case Variant::VECTOR2: { |
| 4496 | Vector2 val_0 = vt->values[0].value; |
| 4497 | Vector2 val_1 = vt->values[1].value; |
| 4498 | single_key = (val_0 - val_1).length() < p_allowed_precision_error; |
| 4499 | } break; |
| 4500 | case Variant::VECTOR3: { |
| 4501 | Vector3 val_0 = vt->values[0].value; |
| 4502 | Vector3 val_1 = vt->values[1].value; |
| 4503 | single_key = (val_0 - val_1).length() < p_allowed_precision_error; |
| 4504 | } break; |
| 4505 | case Variant::QUATERNION: { |
| 4506 | Quaternion val_0 = vt->values[0].value; |
| 4507 | Quaternion val_1 = vt->values[1].value; |
| 4508 | single_key = (val_0 - val_1).length() < p_allowed_precision_error; |
| 4509 | } break; |
| 4510 | default: { |
| 4511 | } break; |
| 4512 | } |
| 4513 | if (single_key) { |
| 4514 | vt->values.remove_at(1); |
| 4515 | } |
| 4516 | } |
| 4517 | } |
| 4518 | |
| 4519 | void Animation::optimize(real_t p_allowed_velocity_err, real_t p_allowed_angular_err, int p_precision) { |
| 4520 | real_t precision = Math::pow(0.1, p_precision); |
| 4521 | for (uint32_t i = 0; i < tracks.size(); i++) { |
| 4522 | if (track_is_compressed(i)) { |
| 4523 | continue; //not possible to optimize compressed track |
| 4524 | } |
| 4525 | if (tracks[i]->type == TYPE_POSITION_3D) { |
| 4526 | _position_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision); |
| 4527 | } else if (tracks[i]->type == TYPE_ROTATION_3D) { |
| 4528 | _rotation_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision); |
| 4529 | } else if (tracks[i]->type == TYPE_SCALE_3D) { |
| 4530 | _scale_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision); |
| 4531 | } else if (tracks[i]->type == TYPE_BLEND_SHAPE) { |
| 4532 | _blend_shape_track_optimize(i, p_allowed_velocity_err, precision); |
| 4533 | } else if (tracks[i]->type == TYPE_VALUE) { |
| 4534 | _value_track_optimize(i, p_allowed_velocity_err, p_allowed_angular_err, precision); |
| 4535 | } |
| 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | #define print_animc(m_str) |
| 4540 | //#define print_animc(m_str) print_line(m_str); |
| 4541 | |
| 4542 | struct AnimationCompressionDataState { |
| 4543 | enum { |
| 4544 | MIN_OPTIMIZE_PACKETS = 5, |
| 4545 | MAX_PACKETS = 16 |
| 4546 | }; |
| 4547 | |
| 4548 | uint32_t components = 3; |
| 4549 | LocalVector<uint8_t> data; // Committed packets. |
| 4550 | struct PacketData { |
| 4551 | int32_t data[3] = { 0, 0, 0 }; |
| 4552 | uint32_t frame = 0; |
| 4553 | }; |
| 4554 | |
| 4555 | float split_tolerance = 1.5; |
| 4556 | |
| 4557 | LocalVector<PacketData> temp_packets; |
| 4558 | |
| 4559 | //used for rollback if the new frame does not fit |
| 4560 | int32_t validated_packet_count = -1; |
| 4561 | |
| 4562 | static int32_t _compute_delta16_signed(int32_t p_from, int32_t p_to) { |
| 4563 | int32_t delta = p_to - p_from; |
| 4564 | if (delta > 32767) { |
| 4565 | return delta - 65536; // use wrap around |
| 4566 | } else if (delta < -32768) { |
| 4567 | return 65536 + delta; // use wrap around |
| 4568 | } |
| 4569 | return delta; |
| 4570 | } |
| 4571 | |
| 4572 | static uint32_t _compute_shift_bits_signed(int32_t p_delta) { |
| 4573 | if (p_delta == 0) { |
| 4574 | return 0; |
| 4575 | } else if (p_delta < 0) { |
| 4576 | p_delta = Math::abs(p_delta) - 1; |
| 4577 | if (p_delta == 0) { |
| 4578 | return 1; |
| 4579 | } |
| 4580 | } |
| 4581 | return nearest_shift((uint32_t)p_delta); |
| 4582 | } |
| 4583 | |
| 4584 | void _compute_max_shifts(uint32_t p_from, uint32_t p_to, uint32_t *max_shifts, uint32_t &max_frame_delta_shift) const { |
| 4585 | for (uint32_t j = 0; j < components; j++) { |
| 4586 | max_shifts[j] = 0; |
| 4587 | } |
| 4588 | max_frame_delta_shift = 0; |
| 4589 | |
| 4590 | for (uint32_t i = p_from + 1; i <= p_to; i++) { |
| 4591 | int32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame; |
| 4592 | max_frame_delta_shift = MAX(max_frame_delta_shift, nearest_shift((uint32_t)frame_delta)); |
| 4593 | for (uint32_t j = 0; j < components; j++) { |
| 4594 | int32_t diff = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]); |
| 4595 | uint32_t shift = _compute_shift_bits_signed(diff); |
| 4596 | max_shifts[j] = MAX(shift, max_shifts[j]); |
| 4597 | } |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | bool insert_key(uint32_t p_frame, const Vector3i &p_key) { |
| 4602 | if (temp_packets.size() == MAX_PACKETS) { |
| 4603 | commit_temp_packets(); |
| 4604 | } |
| 4605 | PacketData packet; |
| 4606 | packet.frame = p_frame; |
| 4607 | for (int i = 0; i < 3; i++) { |
| 4608 | ERR_FAIL_COND_V(p_key[i] > 65535, false); // Safety checks. |
| 4609 | packet.data[i] = p_key[i]; |
| 4610 | } |
| 4611 | |
| 4612 | temp_packets.push_back(packet); |
| 4613 | |
| 4614 | if (temp_packets.size() >= MIN_OPTIMIZE_PACKETS) { |
| 4615 | uint32_t max_shifts[3] = { 0, 0, 0 }; // Base sizes, 16 bit |
| 4616 | uint32_t max_frame_delta_shift = 0; |
| 4617 | // Compute the average shift before the packet was added |
| 4618 | _compute_max_shifts(0, temp_packets.size() - 2, max_shifts, max_frame_delta_shift); |
| 4619 | |
| 4620 | float prev_packet_size_avg = 0; |
| 4621 | prev_packet_size_avg = float(1 << max_frame_delta_shift); |
| 4622 | for (uint32_t i = 0; i < components; i++) { |
| 4623 | prev_packet_size_avg += float(1 << max_shifts[i]); |
| 4624 | } |
| 4625 | prev_packet_size_avg /= float(1 + components); |
| 4626 | |
| 4627 | _compute_max_shifts(temp_packets.size() - 2, temp_packets.size() - 1, max_shifts, max_frame_delta_shift); |
| 4628 | |
| 4629 | float new_packet_size_avg = 0; |
| 4630 | new_packet_size_avg = float(1 << max_frame_delta_shift); |
| 4631 | for (uint32_t i = 0; i < components; i++) { |
| 4632 | new_packet_size_avg += float(1 << max_shifts[i]); |
| 4633 | } |
| 4634 | new_packet_size_avg /= float(1 + components); |
| 4635 | |
| 4636 | print_animc("packet count: " + rtos(temp_packets.size() - 1) + " size avg " + rtos(prev_packet_size_avg) + " new avg " + rtos(new_packet_size_avg)); |
| 4637 | 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); |
| 4638 | |
| 4639 | if (ratio > split_tolerance) { |
| 4640 | print_animc("split!"); |
| 4641 | temp_packets.resize(temp_packets.size() - 1); |
| 4642 | commit_temp_packets(); |
| 4643 | temp_packets.push_back(packet); |
| 4644 | } |
| 4645 | } |
| 4646 | |
| 4647 | return temp_packets.size() == 1; // First key |
| 4648 | } |
| 4649 | |
| 4650 | uint32_t get_temp_packet_size() const { |
| 4651 | if (temp_packets.is_empty()) { |
| 4652 | return 0; |
| 4653 | } else if (temp_packets.size() == 1) { |
| 4654 | 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 |
| 4655 | } |
| 4656 | uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit |
| 4657 | uint32_t max_frame_delta_shift = 0; |
| 4658 | |
| 4659 | _compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift); |
| 4660 | |
| 4661 | uint32_t size_bits = 16; //base value (all 4 bits of shift sizes for x,y,z,time) |
| 4662 | size_bits += max_frame_delta_shift * (temp_packets.size() - 1); //times |
| 4663 | for (uint32_t j = 0; j < components; j++) { |
| 4664 | size_bits += 16; //base value |
| 4665 | uint32_t shift = max_shifts[j]; |
| 4666 | if (shift > 0) { |
| 4667 | shift += 1; //if not zero, add sign bit |
| 4668 | } |
| 4669 | size_bits += shift * (temp_packets.size() - 1); |
| 4670 | } |
| 4671 | if (size_bits % 8 != 0) { //wrap to 8 bits |
| 4672 | size_bits += 8 - (size_bits % 8); |
| 4673 | } |
| 4674 | uint32_t size_bytes = size_bits / 8; //wrap to words |
| 4675 | if (size_bytes % 4 != 0) { |
| 4676 | size_bytes += 4 - (size_bytes % 4); |
| 4677 | } |
| 4678 | return size_bytes; |
| 4679 | } |
| 4680 | |
| 4681 | 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) { |
| 4682 | r_buffer |= p_value << r_bits_used; |
| 4683 | r_bits_used += p_bits; |
| 4684 | while (r_bits_used >= 8) { |
| 4685 | uint8_t byte = r_buffer & 0xFF; |
| 4686 | data.push_back(byte); |
| 4687 | r_buffer >>= 8; |
| 4688 | r_bits_used -= 8; |
| 4689 | } |
| 4690 | } |
| 4691 | |
| 4692 | void commit_temp_packets() { |
| 4693 | if (temp_packets.is_empty()) { |
| 4694 | return; // Nothing to do. |
| 4695 | } |
| 4696 | //#define DEBUG_PACKET_PUSH |
| 4697 | #ifdef DEBUG_PACKET_PUSH |
| 4698 | #ifndef _MSC_VER |
| 4699 | #warning Debugging packet push, disable this code in production to gain a bit more import performance. |
| 4700 | #endif |
| 4701 | uint32_t debug_packet_push = get_temp_packet_size(); |
| 4702 | uint32_t debug_data_size = data.size(); |
| 4703 | #endif |
| 4704 | // Store header |
| 4705 | |
| 4706 | uint8_t header[8]; |
| 4707 | uint32_t header_bytes = 0; |
| 4708 | for (uint32_t i = 0; i < components; i++) { |
| 4709 | encode_uint16(temp_packets[0].data[i], &header[header_bytes]); |
| 4710 | header_bytes += 2; |
| 4711 | } |
| 4712 | |
| 4713 | uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit |
| 4714 | uint32_t max_frame_delta_shift = 0; |
| 4715 | |
| 4716 | if (temp_packets.size() > 1) { |
| 4717 | _compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift); |
| 4718 | uint16_t shift_header = (max_frame_delta_shift - 1) << 12; |
| 4719 | for (uint32_t i = 0; i < components; i++) { |
| 4720 | shift_header |= max_shifts[i] << (4 * i); |
| 4721 | } |
| 4722 | |
| 4723 | encode_uint16(shift_header, &header[header_bytes]); |
| 4724 | header_bytes += 2; |
| 4725 | } |
| 4726 | |
| 4727 | while (header_bytes < 8 && header_bytes % 4 != 0) { // First cond needed to silence wrong GCC warning. |
| 4728 | header[header_bytes++] = 0; |
| 4729 | } |
| 4730 | |
| 4731 | for (uint32_t i = 0; i < header_bytes; i++) { |
| 4732 | data.push_back(header[i]); |
| 4733 | } |
| 4734 | |
| 4735 | if (temp_packets.size() == 1) { |
| 4736 | temp_packets.clear(); |
| 4737 | validated_packet_count = 0; |
| 4738 | return; //only header stored, nothing else to do |
| 4739 | } |
| 4740 | |
| 4741 | uint32_t bit_buffer = 0; |
| 4742 | uint32_t bits_used = 0; |
| 4743 | |
| 4744 | for (uint32_t i = 1; i < temp_packets.size(); i++) { |
| 4745 | uint32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame; |
| 4746 | _push_bits(data, bit_buffer, bits_used, frame_delta, max_frame_delta_shift); |
| 4747 | |
| 4748 | for (uint32_t j = 0; j < components; j++) { |
| 4749 | if (max_shifts[j] == 0) { |
| 4750 | continue; // Zero delta, do not store |
| 4751 | } |
| 4752 | int32_t delta = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]); |
| 4753 | |
| 4754 | ERR_FAIL_COND(delta < -32768 || delta > 32767); // Safety check. |
| 4755 | |
| 4756 | uint16_t deltau; |
| 4757 | if (delta < 0) { |
| 4758 | deltau = (Math::abs(delta) - 1) | (1 << max_shifts[j]); |
| 4759 | } else { |
| 4760 | deltau = delta; |
| 4761 | } |
| 4762 | _push_bits(data, bit_buffer, bits_used, deltau, max_shifts[j] + 1); // Include sign bit |
| 4763 | } |
| 4764 | } |
| 4765 | if (bits_used != 0) { |
| 4766 | ERR_FAIL_COND(bit_buffer > 0xFF); // Safety check. |
| 4767 | data.push_back(bit_buffer); |
| 4768 | } |
| 4769 | |
| 4770 | while (data.size() % 4 != 0) { |
| 4771 | data.push_back(0); //pad to align with 4 |
| 4772 | } |
| 4773 | |
| 4774 | temp_packets.clear(); |
| 4775 | validated_packet_count = 0; |
| 4776 | |
| 4777 | #ifdef DEBUG_PACKET_PUSH |
| 4778 | ERR_FAIL_COND((data.size() - debug_data_size) != debug_packet_push); |
| 4779 | #endif |
| 4780 | } |
| 4781 | }; |
| 4782 | |
| 4783 | struct AnimationCompressionTimeState { |
| 4784 | struct Packet { |
| 4785 | uint32_t frame; |
| 4786 | uint32_t offset; |
| 4787 | uint32_t count; |
| 4788 | }; |
| 4789 | |
| 4790 | LocalVector<Packet> packets; |
| 4791 | //used for rollback |
| 4792 | int32_t key_index = 0; |
| 4793 | int32_t validated_packet_count = 0; |
| 4794 | int32_t validated_key_index = -1; |
| 4795 | bool needs_start_frame = false; |
| 4796 | }; |
| 4797 | |
| 4798 | Vector3i Animation::_compress_key(uint32_t p_track, const AABB &p_bounds, int32_t p_key, float p_time) { |
| 4799 | Vector3i values; |
| 4800 | TrackType tt = track_get_type(p_track); |
| 4801 | switch (tt) { |
| 4802 | case TYPE_POSITION_3D: { |
| 4803 | Vector3 pos; |
| 4804 | if (p_key >= 0) { |
| 4805 | position_track_get_key(p_track, p_key, &pos); |
| 4806 | } else { |
| 4807 | try_position_track_interpolate(p_track, p_time, &pos); |
| 4808 | } |
| 4809 | pos = (pos - p_bounds.position) / p_bounds.size; |
| 4810 | for (int j = 0; j < 3; j++) { |
| 4811 | values[j] = CLAMP(int32_t(pos[j] * 65535.0), 0, 65535); |
| 4812 | } |
| 4813 | } break; |
| 4814 | case TYPE_ROTATION_3D: { |
| 4815 | Quaternion rot; |
| 4816 | if (p_key >= 0) { |
| 4817 | rotation_track_get_key(p_track, p_key, &rot); |
| 4818 | } else { |
| 4819 | try_rotation_track_interpolate(p_track, p_time, &rot); |
| 4820 | } |
| 4821 | Vector3 axis = rot.get_axis(); |
| 4822 | float angle = rot.get_angle(); |
| 4823 | angle = Math::fposmod(double(angle), double(Math::PI * 2.0)); |
| 4824 | Vector2 oct = axis.octahedron_encode(); |
| 4825 | Vector3 rot_norm(oct.x, oct.y, angle / (Math::PI * 2.0)); // high resolution rotation in 0-1 angle. |
| 4826 | |
| 4827 | for (int j = 0; j < 3; j++) { |
| 4828 | values[j] = CLAMP(int32_t(rot_norm[j] * 65535.0), 0, 65535); |
| 4829 | } |
| 4830 | } break; |
| 4831 | case TYPE_SCALE_3D: { |
| 4832 | Vector3 scale; |
| 4833 | if (p_key >= 0) { |
| 4834 | scale_track_get_key(p_track, p_key, &scale); |
| 4835 | } else { |
| 4836 | try_scale_track_interpolate(p_track, p_time, &scale); |
| 4837 | } |
| 4838 | scale = (scale - p_bounds.position) / p_bounds.size; |
| 4839 | for (int j = 0; j < 3; j++) { |
| 4840 | values[j] = CLAMP(int32_t(scale[j] * 65535.0), 0, 65535); |
| 4841 | } |
| 4842 | } break; |
| 4843 | case TYPE_BLEND_SHAPE: { |
| 4844 | float blend; |
| 4845 | if (p_key >= 0) { |
| 4846 | blend_shape_track_get_key(p_track, p_key, &blend); |
| 4847 | } else { |
| 4848 | try_blend_shape_track_interpolate(p_track, p_time, &blend); |
| 4849 | } |
| 4850 | |
| 4851 | blend = (blend / float(Compression::BLEND_SHAPE_RANGE)) * 0.5 + 0.5; |
| 4852 | values[0] = CLAMP(int32_t(blend * 65535.0), 0, 65535); |
| 4853 | } break; |
| 4854 | default: { |
| 4855 | ERR_FAIL_V(Vector3i()); // Safety check. |
| 4856 | } break; |
| 4857 | } |
| 4858 | |
| 4859 | return values; |
| 4860 | } |
| 4861 | |
| 4862 | struct AnimationCompressionBufferBitsRead { |
| 4863 | uint32_t buffer = 0; |
| 4864 | uint32_t used = 0; |
| 4865 | const uint8_t *src_data = nullptr; |
| 4866 | |
| 4867 | _FORCE_INLINE_ uint32_t read(uint32_t p_bits) { |
| 4868 | uint32_t output = 0; |
| 4869 | uint32_t written = 0; |
| 4870 | while (p_bits > 0) { |
| 4871 | if (used == 0) { |
| 4872 | used = 8; |
| 4873 | buffer = *src_data; |
| 4874 | src_data++; |
| 4875 | } |
| 4876 | uint32_t to_write = MIN(used, p_bits); |
| 4877 | output |= (buffer & ((1 << to_write) - 1)) << written; |
| 4878 | buffer >>= to_write; |
| 4879 | used -= to_write; |
| 4880 | p_bits -= to_write; |
| 4881 | written += to_write; |
| 4882 | } |
| 4883 | return output; |
| 4884 | } |
| 4885 | }; |
| 4886 | |
| 4887 | void Animation::compress(uint32_t p_page_size, uint32_t p_fps, float p_split_tolerance) { |
| 4888 | ERR_FAIL_COND_MSG(compression.enabled, "This animation is already compressed"); |
| 4889 | |
| 4890 | p_split_tolerance = CLAMP(p_split_tolerance, 1.1, 8.0); |
| 4891 | compression.pages.clear(); |
| 4892 | |
| 4893 | uint32_t base_page_size = 0; // Before compressing pages, compute how large the "end page" datablock is. |
| 4894 | LocalVector<uint32_t> tracks_to_compress; |
| 4895 | LocalVector<AABB> track_bounds; |
| 4896 | const uint32_t time_packet_size = 4; |
| 4897 | |
| 4898 | 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) |
| 4899 | |
| 4900 | for (int i = 0; i < get_track_count(); i++) { |
| 4901 | TrackType type = track_get_type(i); |
| 4902 | if (type != TYPE_POSITION_3D && type != TYPE_ROTATION_3D && type != TYPE_SCALE_3D && type != TYPE_BLEND_SHAPE) { |
| 4903 | continue; |
| 4904 | } |
| 4905 | if (track_get_key_count(i) == 0) { |
| 4906 | continue; //do not compress, no keys |
| 4907 | } |
| 4908 | base_page_size += track_header_size; //pointer to beginning of each track timeline and amount of time keys |
| 4909 | base_page_size += time_packet_size; //for end of track time marker |
| 4910 | 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. |
| 4911 | tracks_to_compress.push_back(i); |
| 4912 | |
| 4913 | AABB bounds; |
| 4914 | |
| 4915 | if (type == TYPE_POSITION_3D) { |
| 4916 | AABB aabb; |
| 4917 | int kcount = track_get_key_count(i); |
| 4918 | for (int j = 0; j < kcount; j++) { |
| 4919 | Vector3 pos; |
| 4920 | position_track_get_key(i, j, &pos); |
| 4921 | if (j == 0) { |
| 4922 | aabb.position = pos; |
| 4923 | } else { |
| 4924 | aabb.expand_to(pos); |
| 4925 | } |
| 4926 | } |
| 4927 | for (int j = 0; j < 3; j++) { |
| 4928 | // Can't have zero. |
| 4929 | if (aabb.size[j] < CMP_EPSILON) { |
| 4930 | aabb.size[j] = CMP_EPSILON; |
| 4931 | } |
| 4932 | } |
| 4933 | bounds = aabb; |
| 4934 | } |
| 4935 | if (type == TYPE_SCALE_3D) { |
| 4936 | AABB aabb; |
| 4937 | int kcount = track_get_key_count(i); |
| 4938 | for (int j = 0; j < kcount; j++) { |
| 4939 | Vector3 scale; |
| 4940 | scale_track_get_key(i, j, &scale); |
| 4941 | if (j == 0) { |
| 4942 | aabb.position = scale; |
| 4943 | } else { |
| 4944 | aabb.expand_to(scale); |
| 4945 | } |
| 4946 | } |
| 4947 | for (int j = 0; j < 3; j++) { |
| 4948 | // Can't have zero. |
| 4949 | if (aabb.size[j] < CMP_EPSILON) { |
| 4950 | aabb.size[j] = CMP_EPSILON; |
| 4951 | } |
| 4952 | } |
| 4953 | bounds = aabb; |
| 4954 | } |
| 4955 | |
| 4956 | track_bounds.push_back(bounds); |
| 4957 | } |
| 4958 | |
| 4959 | if (tracks_to_compress.is_empty()) { |
| 4960 | return; //nothing to compress |
| 4961 | } |
| 4962 | |
| 4963 | print_animc("Anim Compression:"); |
| 4964 | print_animc("-----------------"); |
| 4965 | print_animc("Tracks to compress: " + itos(tracks_to_compress.size())); |
| 4966 | |
| 4967 | uint32_t current_frame = 0; |
| 4968 | uint32_t base_page_frame = 0; |
| 4969 | double frame_len = 1.0 / double(p_fps); |
| 4970 | const uint32_t max_frames_per_page = 65536; |
| 4971 | |
| 4972 | print_animc("Frame Len: " + rtos(frame_len)); |
| 4973 | |
| 4974 | LocalVector<AnimationCompressionDataState> data_tracks; |
| 4975 | LocalVector<AnimationCompressionTimeState> time_tracks; |
| 4976 | |
| 4977 | data_tracks.resize(tracks_to_compress.size()); |
| 4978 | time_tracks.resize(tracks_to_compress.size()); |
| 4979 | |
| 4980 | uint32_t needed_min_page_size = base_page_size; |
| 4981 | for (uint32_t i = 0; i < data_tracks.size(); i++) { |
| 4982 | data_tracks[i].split_tolerance = p_split_tolerance; |
| 4983 | if (track_get_type(tracks_to_compress[i]) == TYPE_BLEND_SHAPE) { |
| 4984 | data_tracks[i].components = 1; |
| 4985 | } else { |
| 4986 | data_tracks[i].components = 3; |
| 4987 | } |
| 4988 | needed_min_page_size += data_tracks[i].data.size() + data_tracks[i].get_temp_packet_size(); |
| 4989 | } |
| 4990 | for (uint32_t i = 0; i < time_tracks.size(); i++) { |
| 4991 | needed_min_page_size += time_tracks[i].packets.size() * 4; // time packet is 32 bits |
| 4992 | } |
| 4993 | ERR_FAIL_COND_MSG(p_page_size < needed_min_page_size, "Cannot compress with the given page size"); |
| 4994 | |
| 4995 | while (true) { |
| 4996 | // Begin by finding the keyframe in all tracks with the time closest to the current time |
| 4997 | const uint32_t FRAME_MAX = 0xFFFFFFFF; |
| 4998 | const int32_t NO_TRACK_FOUND = -1; |
| 4999 | uint32_t best_frame = FRAME_MAX; |
| 5000 | uint32_t best_invalid_frame = FRAME_MAX; |
| 5001 | int32_t best_frame_track = NO_TRACK_FOUND; // Default is -1, which means all keyframes for this page are exhausted. |
| 5002 | bool start_frame = false; |
| 5003 | |
| 5004 | for (uint32_t i = 0; i < tracks_to_compress.size(); i++) { |
| 5005 | uint32_t uncomp_track = tracks_to_compress[i]; |
| 5006 | |
| 5007 | if (time_tracks[i].key_index == track_get_key_count(uncomp_track)) { |
| 5008 | if (time_tracks[i].needs_start_frame) { |
| 5009 | start_frame = true; |
| 5010 | best_frame = base_page_frame; |
| 5011 | best_frame_track = i; |
| 5012 | time_tracks[i].needs_start_frame = false; |
| 5013 | break; |
| 5014 | } else { |
| 5015 | continue; // This track is exhausted (all keys were added already), don't consider. |
| 5016 | } |
| 5017 | } |
| 5018 | double key_time = track_get_key_time(uncomp_track, time_tracks[i].key_index); |
| 5019 | double result = key_time / frame_len; |
| 5020 | uint32_t key_frame = Math::fast_ftoi(result); |
| 5021 | if (time_tracks[i].needs_start_frame && key_frame > base_page_frame) { |
| 5022 | start_frame = true; |
| 5023 | best_frame = base_page_frame; |
| 5024 | best_frame_track = i; |
| 5025 | time_tracks[i].needs_start_frame = false; |
| 5026 | break; |
| 5027 | } |
| 5028 | |
| 5029 | ERR_FAIL_COND(key_frame < base_page_frame); // Safety check, should never happen. |
| 5030 | |
| 5031 | if (key_frame - base_page_frame >= max_frames_per_page) { |
| 5032 | // Invalid because beyond the max frames allowed per page |
| 5033 | best_invalid_frame = MIN(best_invalid_frame, key_frame); |
| 5034 | } else if (key_frame < best_frame) { |
| 5035 | best_frame = key_frame; |
| 5036 | best_frame_track = i; |
| 5037 | } |
| 5038 | } |
| 5039 | |
| 5040 | 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")); |
| 5041 | |
| 5042 | if (!start_frame && best_frame > current_frame) { |
| 5043 | // Any case where the current frame advanced, either because nothing was found or because something was found greater than the current one. |
| 5044 | print_animc("\tAdvance Condition."); |
| 5045 | bool rollback = false; |
| 5046 | |
| 5047 | // The frame has advanced, time to validate the previous frame |
| 5048 | uint32_t current_page_size = base_page_size; |
| 5049 | for (const AnimationCompressionDataState &state : data_tracks) { |
| 5050 | uint32_t track_size = state.data.size(); // track size |
| 5051 | track_size += state.get_temp_packet_size(); // Add the temporary data |
| 5052 | if (track_size > Compression::MAX_DATA_TRACK_SIZE) { |
| 5053 | rollback = true; //track to large, time track can't point to keys any longer, because key offset is 12 bits |
| 5054 | break; |
| 5055 | } |
| 5056 | current_page_size += track_size; |
| 5057 | } |
| 5058 | for (const AnimationCompressionTimeState &state : time_tracks) { |
| 5059 | current_page_size += state.packets.size() * 4; // time packet is 32 bits |
| 5060 | } |
| 5061 | |
| 5062 | if (!rollback && current_page_size > p_page_size) { |
| 5063 | rollback = true; |
| 5064 | } |
| 5065 | |
| 5066 | print_animc("\tCurrent Page Size: " + itos(current_page_size) + "/" + itos(p_page_size) + " Rollback? " + String(rollback ? "YES!" : "no")); |
| 5067 | |
| 5068 | if (rollback) { |
| 5069 | // Not valid any longer, so rollback and commit page |
| 5070 | |
| 5071 | for (AnimationCompressionDataState &state : data_tracks) { |
| 5072 | state.temp_packets.resize(state.validated_packet_count); |
| 5073 | } |
| 5074 | for (AnimationCompressionTimeState &state : time_tracks) { |
| 5075 | state.key_index = state.validated_key_index; //rollback key |
| 5076 | state.packets.resize(state.validated_packet_count); |
| 5077 | } |
| 5078 | |
| 5079 | } else { |
| 5080 | // All valid, so save rollback information |
| 5081 | for (AnimationCompressionDataState &state : data_tracks) { |
| 5082 | state.validated_packet_count = state.temp_packets.size(); |
| 5083 | } |
| 5084 | for (AnimationCompressionTimeState &state : time_tracks) { |
| 5085 | state.validated_key_index = state.key_index; |
| 5086 | state.validated_packet_count = state.packets.size(); |
| 5087 | } |
| 5088 | |
| 5089 | // Accept this frame as the frame being processed (as long as it exists) |
| 5090 | if (best_frame != FRAME_MAX) { |
| 5091 | current_frame = best_frame; |
| 5092 | print_animc("\tValidated, New Current Frame: " + itos(current_frame)); |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | if (rollback || best_frame == FRAME_MAX) { |
| 5097 | // Commit the page if had to rollback or if no track was found |
| 5098 | print_animc("\tCommiting page..."); |
| 5099 | |
| 5100 | // The end frame for the page depends entirely on whether its valid or |
| 5101 | // no more keys were found. |
| 5102 | // If not valid, then the end frame is the current frame (as this means the current frame is being rolled back |
| 5103 | // 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. |
| 5104 | uint32_t page_end_frame = (rollback || best_frame == FRAME_MAX) ? current_frame : best_invalid_frame; |
| 5105 | |
| 5106 | print_animc("\tEnd Frame: " + itos(page_end_frame) + ", " + rtos(page_end_frame * frame_len) + "s"); |
| 5107 | |
| 5108 | // Add finalizer frames and commit pending tracks |
| 5109 | uint32_t finalizer_local_frame = page_end_frame - base_page_frame; |
| 5110 | |
| 5111 | uint32_t total_page_size = 0; |
| 5112 | |
| 5113 | for (uint32_t i = 0; i < data_tracks.size(); i++) { |
| 5114 | if (data_tracks[i].temp_packets.is_empty() || (data_tracks[i].temp_packets[data_tracks[i].temp_packets.size() - 1].frame) < finalizer_local_frame) { |
| 5115 | // Add finalizer frame if it makes sense |
| 5116 | Vector3i values = _compress_key(tracks_to_compress[i], track_bounds[i], -1, page_end_frame * frame_len); |
| 5117 | |
| 5118 | bool first_key = data_tracks[i].insert_key(finalizer_local_frame, values); |
| 5119 | if (first_key) { |
| 5120 | AnimationCompressionTimeState::Packet p; |
| 5121 | p.count = 1; |
| 5122 | p.frame = finalizer_local_frame; |
| 5123 | p.offset = data_tracks[i].data.size(); |
| 5124 | time_tracks[i].packets.push_back(p); |
| 5125 | } else { |
| 5126 | ERR_FAIL_COND(time_tracks[i].packets.is_empty()); |
| 5127 | time_tracks[i].packets[time_tracks[i].packets.size() - 1].count++; |
| 5128 | } |
| 5129 | } |
| 5130 | |
| 5131 | data_tracks[i].commit_temp_packets(); |
| 5132 | total_page_size += data_tracks[i].data.size(); |
| 5133 | total_page_size += time_tracks[i].packets.size() * 4; |
| 5134 | total_page_size += track_header_size; |
| 5135 | |
| 5136 | print_animc("\tTrack " + itos(i) + " time packets: " + itos(time_tracks[i].packets.size()) + " Packet data: " + itos(data_tracks[i].data.size())); |
| 5137 | } |
| 5138 | |
| 5139 | print_animc("\tTotal page Size: " + itos(total_page_size) + "/" + itos(p_page_size)); |
| 5140 | |
| 5141 | // Create Page |
| 5142 | Vector<uint8_t> page_data; |
| 5143 | page_data.resize(total_page_size); |
| 5144 | { |
| 5145 | uint8_t *page_ptr = page_data.ptrw(); |
| 5146 | uint32_t base_offset = data_tracks.size() * track_header_size; |
| 5147 | |
| 5148 | for (uint32_t i = 0; i < data_tracks.size(); i++) { |
| 5149 | encode_uint32(base_offset, page_ptr + (track_header_size * i + 0)); |
| 5150 | uint16_t *key_time_ptr = (uint16_t *)(page_ptr + base_offset); |
| 5151 | for (uint32_t j = 0; j < time_tracks[i].packets.size(); j++) { |
| 5152 | key_time_ptr[j * 2 + 0] = uint16_t(time_tracks[i].packets[j].frame); |
| 5153 | uint16_t ptr = time_tracks[i].packets[j].offset / 4; |
| 5154 | ptr |= (time_tracks[i].packets[j].count - 1) << 12; |
| 5155 | key_time_ptr[j * 2 + 1] = ptr; |
| 5156 | base_offset += 4; |
| 5157 | } |
| 5158 | encode_uint32(time_tracks[i].packets.size(), page_ptr + (track_header_size * i + 4)); |
| 5159 | encode_uint32(base_offset, page_ptr + (track_header_size * i + 8)); |
| 5160 | memcpy(page_ptr + base_offset, data_tracks[i].data.ptr(), data_tracks[i].data.size()); |
| 5161 | base_offset += data_tracks[i].data.size(); |
| 5162 | |
| 5163 | //reset track |
| 5164 | data_tracks[i].data.clear(); |
| 5165 | data_tracks[i].temp_packets.clear(); |
| 5166 | data_tracks[i].validated_packet_count = -1; |
| 5167 | |
| 5168 | time_tracks[i].needs_start_frame = true; //Not required the first time, but from now on it is. |
| 5169 | time_tracks[i].packets.clear(); |
| 5170 | time_tracks[i].validated_key_index = -1; |
| 5171 | time_tracks[i].validated_packet_count = 0; |
| 5172 | } |
| 5173 | } |
| 5174 | |
| 5175 | Compression::Page page; |
| 5176 | page.data = page_data; |
| 5177 | page.time_offset = base_page_frame * frame_len; |
| 5178 | compression.pages.push_back(page); |
| 5179 | |
| 5180 | if (!rollback && best_invalid_frame == FRAME_MAX) { |
| 5181 | break; // No more pages to add. |
| 5182 | } |
| 5183 | |
| 5184 | current_frame = page_end_frame; |
| 5185 | base_page_frame = page_end_frame; |
| 5186 | |
| 5187 | continue; // Start over |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | // A key was found for the current frame and all is ok |
| 5192 | |
| 5193 | uint32_t comp_track = best_frame_track; |
| 5194 | Vector3i values; |
| 5195 | |
| 5196 | if (start_frame) { |
| 5197 | // Interpolate |
| 5198 | values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], -1, base_page_frame * frame_len); |
| 5199 | } else { |
| 5200 | uint32_t key = time_tracks[comp_track].key_index; |
| 5201 | values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], key); |
| 5202 | time_tracks[comp_track].key_index++; //goto next key (but could be rolled back if beyond page size). |
| 5203 | } |
| 5204 | |
| 5205 | bool first_key = data_tracks[comp_track].insert_key(best_frame - base_page_frame, values); |
| 5206 | if (first_key) { |
| 5207 | AnimationCompressionTimeState::Packet p; |
| 5208 | p.count = 1; |
| 5209 | p.frame = best_frame - base_page_frame; |
| 5210 | p.offset = data_tracks[comp_track].data.size(); |
| 5211 | time_tracks[comp_track].packets.push_back(p); |
| 5212 | } else { |
| 5213 | ERR_CONTINUE(time_tracks[comp_track].packets.is_empty()); |
| 5214 | time_tracks[comp_track].packets[time_tracks[comp_track].packets.size() - 1].count++; |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | compression.bounds = track_bounds; |
| 5219 | compression.fps = p_fps; |
| 5220 | compression.enabled = true; |
| 5221 | |
| 5222 | for (uint32_t i = 0; i < tracks_to_compress.size(); i++) { |
| 5223 | Track *t = tracks[tracks_to_compress[i]]; |
| 5224 | t->interpolation = INTERPOLATION_LINEAR; //only linear supported |
| 5225 | switch (t->type) { |
| 5226 | case TYPE_POSITION_3D: { |
| 5227 | PositionTrack *tt = static_cast<PositionTrack *>(t); |
| 5228 | tt->positions.clear(); |
| 5229 | tt->compressed_track = i; |
| 5230 | } break; |
| 5231 | case TYPE_ROTATION_3D: { |
| 5232 | RotationTrack *rt = static_cast<RotationTrack *>(t); |
| 5233 | rt->rotations.clear(); |
| 5234 | rt->compressed_track = i; |
| 5235 | } break; |
| 5236 | case TYPE_SCALE_3D: { |
| 5237 | ScaleTrack *st = static_cast<ScaleTrack *>(t); |
| 5238 | st->scales.clear(); |
| 5239 | st->compressed_track = i; |
| 5240 | print_line("Scale Bounds " + itos(i) + ": " + String(track_bounds[i])); |
| 5241 | } break; |
| 5242 | case TYPE_BLEND_SHAPE: { |
| 5243 | BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); |
| 5244 | bst->blend_shapes.clear(); |
| 5245 | bst->compressed_track = i; |
| 5246 | } break; |
| 5247 | default: { |
| 5248 | } |
| 5249 | } |
| 5250 | } |
| 5251 | #if 1 |
| 5252 | uint32_t orig_size = 0; |
| 5253 | for (int i = 0; i < get_track_count(); i++) { |
| 5254 | switch (track_get_type(i)) { |
| 5255 | case TYPE_SCALE_3D: |
| 5256 | case TYPE_POSITION_3D: { |
| 5257 | orig_size += sizeof(TKey<Vector3>) * track_get_key_count(i); |
| 5258 | } break; |
| 5259 | case TYPE_ROTATION_3D: { |
| 5260 | orig_size += sizeof(TKey<Quaternion>) * track_get_key_count(i); |
| 5261 | } break; |
| 5262 | case TYPE_BLEND_SHAPE: { |
| 5263 | orig_size += sizeof(TKey<float>) * track_get_key_count(i); |
| 5264 | } break; |
| 5265 | default: { |
| 5266 | } |
| 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | uint32_t new_size = 0; |
| 5271 | for (const Compression::Page &page : compression.pages) { |
| 5272 | new_size += page.data.size(); |
| 5273 | } |
| 5274 | |
| 5275 | 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())); |
| 5276 | #endif |
| 5277 | } |
| 5278 | |
| 5279 | bool Animation::_rotation_interpolate_compressed(uint32_t p_compressed_track, double p_time, Quaternion &r_ret) const { |
| 5280 | Vector3i current; |
| 5281 | Vector3i next; |
| 5282 | double time_current; |
| 5283 | double time_next; |
| 5284 | |
| 5285 | if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) { |
| 5286 | return false; //some sort of problem |
| 5287 | } |
| 5288 | |
| 5289 | if (time_current >= p_time || time_current == time_next) { |
| 5290 | r_ret = _uncompress_quaternion(current); |
| 5291 | } else if (p_time >= time_next) { |
| 5292 | r_ret = _uncompress_quaternion(next); |
| 5293 | } else { |
| 5294 | double c = (p_time - time_current) / (time_next - time_current); |
| 5295 | Quaternion from = _uncompress_quaternion(current); |
| 5296 | Quaternion to = _uncompress_quaternion(next); |
| 5297 | r_ret = from.slerp(to, c); |
| 5298 | } |
| 5299 | |
| 5300 | return true; |
| 5301 | } |
| 5302 | |
| 5303 | bool Animation::_pos_scale_interpolate_compressed(uint32_t p_compressed_track, double p_time, Vector3 &r_ret) const { |
| 5304 | Vector3i current; |
| 5305 | Vector3i next; |
| 5306 | double time_current; |
| 5307 | double time_next; |
| 5308 | |
| 5309 | if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) { |
| 5310 | return false; //some sort of problem |
| 5311 | } |
| 5312 | |
| 5313 | if (time_current >= p_time || time_current == time_next) { |
| 5314 | r_ret = _uncompress_pos_scale(p_compressed_track, current); |
| 5315 | } else if (p_time >= time_next) { |
| 5316 | r_ret = _uncompress_pos_scale(p_compressed_track, next); |
| 5317 | } else { |
| 5318 | double c = (p_time - time_current) / (time_next - time_current); |
| 5319 | Vector3 from = _uncompress_pos_scale(p_compressed_track, current); |
| 5320 | Vector3 to = _uncompress_pos_scale(p_compressed_track, next); |
| 5321 | r_ret = from.lerp(to, c); |
| 5322 | } |
| 5323 | |
| 5324 | return true; |
| 5325 | } |
| 5326 | bool Animation::_blend_shape_interpolate_compressed(uint32_t p_compressed_track, double p_time, float &r_ret) const { |
| 5327 | Vector3i current; |
| 5328 | Vector3i next; |
| 5329 | double time_current; |
| 5330 | double time_next; |
| 5331 | |
| 5332 | if (!_fetch_compressed<1>(p_compressed_track, p_time, current, time_current, next, time_next)) { |
| 5333 | return false; //some sort of problem |
| 5334 | } |
| 5335 | |
| 5336 | if (time_current >= p_time || time_current == time_next) { |
| 5337 | r_ret = _uncompress_blend_shape(current); |
| 5338 | } else if (p_time >= time_next) { |
| 5339 | r_ret = _uncompress_blend_shape(next); |
| 5340 | } else { |
| 5341 | float c = (p_time - time_current) / (time_next - time_current); |
| 5342 | float from = _uncompress_blend_shape(current); |
| 5343 | float to = _uncompress_blend_shape(next); |
| 5344 | r_ret = Math::lerp(from, to, c); |
| 5345 | } |
| 5346 | |
| 5347 | return true; |
| 5348 | } |
| 5349 | |
| 5350 | template <uint32_t COMPONENTS> |
| 5351 | 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 { |
| 5352 | ERR_FAIL_COND_V(!compression.enabled, false); |
| 5353 | ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false); |
| 5354 | p_time = CLAMP(p_time, 0, length); |
| 5355 | if (key_index) { |
| 5356 | *key_index = 0; |
| 5357 | } |
| 5358 | |
| 5359 | double frame_to_sec = 1.0 / double(compression.fps); |
| 5360 | |
| 5361 | int32_t page_index = -1; |
| 5362 | for (uint32_t i = 0; i < compression.pages.size(); i++) { |
| 5363 | if (compression.pages[i].time_offset > p_time) { |
| 5364 | break; |
| 5365 | } |
| 5366 | page_index = i; |
| 5367 | } |
| 5368 | |
| 5369 | ERR_FAIL_COND_V(page_index == -1, false); //should not happen |
| 5370 | |
| 5371 | double page_base_time = compression.pages[page_index].time_offset; |
| 5372 | const uint8_t *page_data = compression.pages[page_index].data.ptr(); |
| 5373 | // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported. |
| 5374 | const uint32_t *indices = (const uint32_t *)page_data; |
| 5375 | const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; |
| 5376 | uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; |
| 5377 | |
| 5378 | int32_t packet_idx = 0; |
| 5379 | double packet_time = double(time_keys[0]) * frame_to_sec + page_base_time; |
| 5380 | uint32_t base_frame = time_keys[0]; |
| 5381 | |
| 5382 | for (uint32_t i = 1; i < time_key_count; i++) { |
| 5383 | uint32_t f = time_keys[i * 2 + 0]; |
| 5384 | double frame_time = double(f) * frame_to_sec + page_base_time; |
| 5385 | |
| 5386 | if (frame_time > p_time) { |
| 5387 | break; |
| 5388 | } |
| 5389 | |
| 5390 | if (key_index) { |
| 5391 | (*key_index) += (time_keys[(i - 1) * 2 + 1] >> 12) + 1; |
| 5392 | } |
| 5393 | |
| 5394 | packet_idx = i; |
| 5395 | packet_time = frame_time; |
| 5396 | base_frame = f; |
| 5397 | } |
| 5398 | |
| 5399 | const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]]; |
| 5400 | |
| 5401 | uint16_t time_key_data = time_keys[packet_idx * 2 + 1]; |
| 5402 | uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits |
| 5403 | uint32_t data_count = (time_key_data >> 12) + 1; |
| 5404 | |
| 5405 | const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset); |
| 5406 | |
| 5407 | uint16_t decode[COMPONENTS]; |
| 5408 | uint16_t decode_next[COMPONENTS]; |
| 5409 | |
| 5410 | for (uint32_t i = 0; i < COMPONENTS; i++) { |
| 5411 | decode[i] = data_key[i]; |
| 5412 | decode_next[i] = data_key[i]; |
| 5413 | } |
| 5414 | |
| 5415 | double next_time = packet_time; |
| 5416 | |
| 5417 | if (p_time > packet_time) { // If its equal or less, then don't bother |
| 5418 | if (data_count > 1) { |
| 5419 | //decode forward |
| 5420 | uint32_t bit_width[COMPONENTS]; |
| 5421 | for (uint32_t i = 0; i < COMPONENTS; i++) { |
| 5422 | bit_width[i] = (data_key[COMPONENTS] >> (i * 4)) & 0xF; |
| 5423 | } |
| 5424 | |
| 5425 | uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1; |
| 5426 | |
| 5427 | AnimationCompressionBufferBitsRead buffer; |
| 5428 | |
| 5429 | buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1]; |
| 5430 | |
| 5431 | for (uint32_t i = 1; i < data_count; i++) { |
| 5432 | uint32_t frame_delta = buffer.read(frame_bit_width); |
| 5433 | base_frame += frame_delta; |
| 5434 | |
| 5435 | for (uint32_t j = 0; j < COMPONENTS; j++) { |
| 5436 | if (bit_width[j] == 0) { |
| 5437 | continue; // do none |
| 5438 | } |
| 5439 | uint32_t valueu = buffer.read(bit_width[j] + 1); |
| 5440 | bool sign = valueu & (1 << bit_width[j]); |
| 5441 | int16_t value = valueu & ((1 << bit_width[j]) - 1); |
| 5442 | if (sign) { |
| 5443 | value = -value - 1; |
| 5444 | } |
| 5445 | |
| 5446 | decode_next[j] += value; |
| 5447 | } |
| 5448 | |
| 5449 | next_time = double(base_frame) * frame_to_sec + page_base_time; |
| 5450 | if (p_time < next_time) { |
| 5451 | break; |
| 5452 | } |
| 5453 | |
| 5454 | packet_time = next_time; |
| 5455 | |
| 5456 | for (uint32_t j = 0; j < COMPONENTS; j++) { |
| 5457 | decode[j] = decode_next[j]; |
| 5458 | } |
| 5459 | |
| 5460 | if (key_index) { |
| 5461 | (*key_index)++; |
| 5462 | } |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | if (p_time > next_time) { // > instead of >= because if its equal, then it will be properly interpolated anyway |
| 5467 | // So, the last frame found still has a time that is less than the required frame, |
| 5468 | // will have to interpolate with the first frame of the next timekey. |
| 5469 | |
| 5470 | if ((uint32_t)packet_idx < time_key_count - 1) { // Safety check but should not matter much, otherwise current next packet is last packet. |
| 5471 | |
| 5472 | uint16_t time_key_data_next = time_keys[(packet_idx + 1) * 2 + 1]; |
| 5473 | uint32_t data_offset_next = (time_key_data_next & 0xFFF) * 4; // Lower 12 bits |
| 5474 | |
| 5475 | const uint16_t *data_key_next = (const uint16_t *)(data_keys_base + data_offset_next); |
| 5476 | base_frame = time_keys[(packet_idx + 1) * 2 + 0]; |
| 5477 | next_time = double(base_frame) * frame_to_sec + page_base_time; |
| 5478 | for (uint32_t i = 0; i < COMPONENTS; i++) { |
| 5479 | decode_next[i] = data_key_next[i]; |
| 5480 | } |
| 5481 | } |
| 5482 | } |
| 5483 | } |
| 5484 | |
| 5485 | r_current_time = packet_time; |
| 5486 | r_next_time = next_time; |
| 5487 | |
| 5488 | for (uint32_t i = 0; i < COMPONENTS; i++) { |
| 5489 | r_current_value[i] = decode[i]; |
| 5490 | r_next_value[i] = decode_next[i]; |
| 5491 | } |
| 5492 | |
| 5493 | return true; |
| 5494 | } |
| 5495 | |
| 5496 | template <uint32_t COMPONENTS> |
| 5497 | void Animation::_get_compressed_key_indices_in_range(uint32_t p_compressed_track, double p_time, double p_delta, List<int> *r_indices) const { |
| 5498 | ERR_FAIL_COND(!compression.enabled); |
| 5499 | ERR_FAIL_UNSIGNED_INDEX(p_compressed_track, compression.bounds.size()); |
| 5500 | |
| 5501 | double frame_to_sec = 1.0 / double(compression.fps); |
| 5502 | uint32_t key_index = 0; |
| 5503 | |
| 5504 | for (uint32_t p = 0; p < compression.pages.size(); p++) { |
| 5505 | if (compression.pages[p].time_offset >= p_time + p_delta) { |
| 5506 | // Page beyond range |
| 5507 | return; |
| 5508 | } |
| 5509 | |
| 5510 | // Page within range |
| 5511 | |
| 5512 | uint32_t page_index = p; |
| 5513 | |
| 5514 | double page_base_time = compression.pages[page_index].time_offset; |
| 5515 | const uint8_t *page_data = compression.pages[page_index].data.ptr(); |
| 5516 | // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported. |
| 5517 | const uint32_t *indices = (const uint32_t *)page_data; |
| 5518 | const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; |
| 5519 | uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; |
| 5520 | |
| 5521 | for (uint32_t i = 0; i < time_key_count; i++) { |
| 5522 | uint32_t f = time_keys[i * 2 + 0]; |
| 5523 | double frame_time = f * frame_to_sec + page_base_time; |
| 5524 | if (frame_time >= p_time + p_delta) { |
| 5525 | return; |
| 5526 | } else if (frame_time >= p_time) { |
| 5527 | r_indices->push_back(key_index); |
| 5528 | } |
| 5529 | |
| 5530 | key_index++; |
| 5531 | |
| 5532 | const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]]; |
| 5533 | |
| 5534 | uint16_t time_key_data = time_keys[i * 2 + 1]; |
| 5535 | uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits |
| 5536 | uint32_t data_count = (time_key_data >> 12) + 1; |
| 5537 | |
| 5538 | const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset); |
| 5539 | |
| 5540 | if (data_count > 1) { |
| 5541 | //decode forward |
| 5542 | uint32_t bit_width[COMPONENTS]; |
| 5543 | for (uint32_t j = 0; j < COMPONENTS; j++) { |
| 5544 | bit_width[j] = (data_key[COMPONENTS] >> (j * 4)) & 0xF; |
| 5545 | } |
| 5546 | |
| 5547 | uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1; |
| 5548 | |
| 5549 | AnimationCompressionBufferBitsRead buffer; |
| 5550 | |
| 5551 | buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1]; |
| 5552 | |
| 5553 | for (uint32_t j = 1; j < data_count; j++) { |
| 5554 | uint32_t frame_delta = buffer.read(frame_bit_width); |
| 5555 | f += frame_delta; |
| 5556 | |
| 5557 | frame_time = f * frame_to_sec + page_base_time; |
| 5558 | if (frame_time >= p_time + p_delta) { |
| 5559 | return; |
| 5560 | } else if (frame_time >= p_time) { |
| 5561 | r_indices->push_back(key_index); |
| 5562 | } |
| 5563 | |
| 5564 | for (uint32_t k = 0; k < COMPONENTS; k++) { |
| 5565 | if (bit_width[k] == 0) { |
| 5566 | continue; // do none |
| 5567 | } |
| 5568 | buffer.read(bit_width[k] + 1); // skip |
| 5569 | } |
| 5570 | |
| 5571 | key_index++; |
| 5572 | } |
| 5573 | } |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | |
| 5578 | int Animation::_get_compressed_key_count(uint32_t p_compressed_track) const { |
| 5579 | ERR_FAIL_COND_V(!compression.enabled, -1); |
| 5580 | ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), -1); |
| 5581 | |
| 5582 | int key_count = 0; |
| 5583 | |
| 5584 | for (const Compression::Page &page : compression.pages) { |
| 5585 | const uint8_t *page_data = page.data.ptr(); |
| 5586 | // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported. |
| 5587 | const uint32_t *indices = (const uint32_t *)page_data; |
| 5588 | const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; |
| 5589 | uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; |
| 5590 | |
| 5591 | for (uint32_t j = 0; j < time_key_count; j++) { |
| 5592 | key_count += (time_keys[j * 2 + 1] >> 12) + 1; |
| 5593 | } |
| 5594 | } |
| 5595 | |
| 5596 | return key_count; |
| 5597 | } |
| 5598 | |
| 5599 | Quaternion Animation::_uncompress_quaternion(const Vector3i &p_value) const { |
| 5600 | Vector3 axis = Vector3::octahedron_decode(Vector2(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0)); |
| 5601 | float angle = (float(p_value.z) / 65535.0) * 2.0 * Math::PI; |
| 5602 | return Quaternion(axis, angle); |
| 5603 | } |
| 5604 | Vector3 Animation::_uncompress_pos_scale(uint32_t p_compressed_track, const Vector3i &p_value) const { |
| 5605 | Vector3 pos_norm(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0, float(p_value.z) / 65535.0); |
| 5606 | return compression.bounds[p_compressed_track].position + pos_norm * compression.bounds[p_compressed_track].size; |
| 5607 | } |
| 5608 | float Animation::_uncompress_blend_shape(const Vector3i &p_value) const { |
| 5609 | float bsn = float(p_value.x) / 65535.0; |
| 5610 | return (bsn * 2.0 - 1.0) * float(Compression::BLEND_SHAPE_RANGE); |
| 5611 | } |
| 5612 | |
| 5613 | template <uint32_t COMPONENTS> |
| 5614 | bool Animation::_fetch_compressed_by_index(uint32_t p_compressed_track, int p_index, Vector3i &r_value, double &r_time) const { |
| 5615 | ERR_FAIL_COND_V(!compression.enabled, false); |
| 5616 | ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false); |
| 5617 | |
| 5618 | for (const Compression::Page &page : compression.pages) { |
| 5619 | const uint8_t *page_data = page.data.ptr(); |
| 5620 | // Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported. |
| 5621 | const uint32_t *indices = (const uint32_t *)page_data; |
| 5622 | const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; |
| 5623 | uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; |
| 5624 | const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]]; |
| 5625 | |
| 5626 | for (uint32_t j = 0; j < time_key_count; j++) { |
| 5627 | uint32_t subkeys = (time_keys[j * 2 + 1] >> 12) + 1; |
| 5628 | if ((uint32_t)p_index < subkeys) { |
| 5629 | uint16_t data_offset = (time_keys[j * 2 + 1] & 0xFFF) * 4; |
| 5630 | |
| 5631 | const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset); |
| 5632 | |
| 5633 | uint16_t frame = time_keys[j * 2 + 0]; |
| 5634 | uint16_t decode[COMPONENTS]; |
| 5635 | |
| 5636 | for (uint32_t k = 0; k < COMPONENTS; k++) { |
| 5637 | decode[k] = data_key[k]; |
| 5638 | } |
| 5639 | |
| 5640 | if (p_index > 0) { |
| 5641 | uint32_t bit_width[COMPONENTS]; |
| 5642 | for (uint32_t k = 0; k < COMPONENTS; k++) { |
| 5643 | bit_width[k] = (data_key[COMPONENTS] >> (k * 4)) & 0xF; |
| 5644 | } |
| 5645 | uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1; |
| 5646 | |
| 5647 | AnimationCompressionBufferBitsRead buffer; |
| 5648 | buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1]; |
| 5649 | |
| 5650 | for (int k = 0; k < p_index; k++) { |
| 5651 | uint32_t frame_delta = buffer.read(frame_bit_width); |
| 5652 | frame += frame_delta; |
| 5653 | for (uint32_t l = 0; l < COMPONENTS; l++) { |
| 5654 | if (bit_width[l] == 0) { |
| 5655 | continue; // do none |
| 5656 | } |
| 5657 | uint32_t valueu = buffer.read(bit_width[l] + 1); |
| 5658 | bool sign = valueu & (1 << bit_width[l]); |
| 5659 | int16_t value = valueu & ((1 << bit_width[l]) - 1); |
| 5660 | if (sign) { |
| 5661 | value = -value - 1; |
| 5662 | } |
| 5663 | |
| 5664 | decode[l] += value; |
| 5665 | } |
| 5666 | } |
| 5667 | } |
| 5668 | |
| 5669 | r_time = page.time_offset + double(frame) / double(compression.fps); |
| 5670 | for (uint32_t l = 0; l < COMPONENTS; l++) { |
| 5671 | r_value[l] = decode[l]; |
| 5672 | } |
| 5673 | |
| 5674 | return true; |
| 5675 | |
| 5676 | } else { |
| 5677 | p_index -= subkeys; |
| 5678 | } |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | return false; |
| 5683 | } |
| 5684 | |
| 5685 | // Helper functions for Rotation. |
| 5686 | double Animation::interpolate_via_rest(double p_from, double p_to, double p_weight, double p_rest) { |
| 5687 | double rot_a = Math::fposmod(p_from, Math::TAU); |
| 5688 | double rot_b = Math::fposmod(p_to, Math::TAU); |
| 5689 | double rot_rest = Math::fposmod(p_rest, Math::TAU); |
| 5690 | if (rot_rest < Math::PI) { |
| 5691 | rot_a = rot_a > rot_rest + Math::PI ? rot_a - Math::TAU : rot_a; |
| 5692 | rot_b = rot_b > rot_rest + Math::PI ? rot_b - Math::TAU : rot_b; |
| 5693 | } else { |
| 5694 | rot_a = rot_a < rot_rest - Math::PI ? rot_a + Math::TAU : rot_a; |
| 5695 | rot_b = rot_b < rot_rest - Math::PI ? rot_b + Math::TAU : rot_b; |
| 5696 | } |
| 5697 | return Math::fposmod(rot_a + (rot_b - rot_rest) * p_weight, Math::TAU); |
| 5698 | } |
| 5699 | |
| 5700 | Quaternion Animation::interpolate_via_rest(const Quaternion &p_from, const Quaternion &p_to, real_t p_weight, const Quaternion &p_rest) { |
| 5701 | #ifdef MATH_CHECKS |
| 5702 | ERR_FAIL_COND_V_MSG(!p_from.is_normalized(), Quaternion(), "The start quaternion must be normalized."); |
| 5703 | ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized."); |
| 5704 | ERR_FAIL_COND_V_MSG(!p_rest.is_normalized(), Quaternion(), "The rest quaternion must be normalized."); |
| 5705 | #endif |
| 5706 | return (p_from * Quaternion().slerp(p_rest.inverse() * p_to, p_weight)).normalized(); |
| 5707 | } |
| 5708 | |
| 5709 | // Helper math functions for Variant. |
| 5710 | bool Animation::is_variant_interpolatable(const Variant p_value) { |
| 5711 | Variant::Type type = p_value.get_type(); |
| 5712 | 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. |
| 5713 | } |
| 5714 | |
| 5715 | bool Animation::validate_type_match(const Variant &p_from, Variant &r_to) { |
| 5716 | if (p_from.get_type() != r_to.get_type()) { |
| 5717 | // Cast r_to between double and int to avoid minor annoyances. |
| 5718 | if (p_from.get_type() == Variant::FLOAT && r_to.get_type() == Variant::INT) { |
| 5719 | r_to = double(r_to); |
| 5720 | } else if (p_from.get_type() == Variant::INT && r_to.get_type() == Variant::FLOAT) { |
| 5721 | r_to = int(r_to); |
| 5722 | } else { |
| 5723 | 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())); |
| 5724 | } |
| 5725 | } |
| 5726 | return true; |
| 5727 | } |
| 5728 | |
| 5729 | Variant Animation::cast_to_blendwise(const Variant p_value) { |
| 5730 | switch (p_value.get_type()) { |
| 5731 | case Variant::BOOL: |
| 5732 | case Variant::INT: { |
| 5733 | return p_value.operator double(); |
| 5734 | } break; |
| 5735 | case Variant::STRING: |
| 5736 | case Variant::STRING_NAME: { |
| 5737 | return string_to_array(p_value); |
| 5738 | } break; |
| 5739 | case Variant::RECT2I: { |
| 5740 | return p_value.operator Rect2(); |
| 5741 | } break; |
| 5742 | case Variant::VECTOR2I: { |
| 5743 | return p_value.operator Vector2(); |
| 5744 | } break; |
| 5745 | case Variant::VECTOR3I: { |
| 5746 | return p_value.operator Vector3(); |
| 5747 | } break; |
| 5748 | case Variant::VECTOR4I: { |
| 5749 | return p_value.operator Vector4(); |
| 5750 | } break; |
| 5751 | case Variant::PACKED_INT32_ARRAY: { |
| 5752 | return p_value.operator PackedFloat32Array(); |
| 5753 | } break; |
| 5754 | case Variant::PACKED_INT64_ARRAY: { |
| 5755 | return p_value.operator PackedFloat64Array(); |
| 5756 | } break; |
| 5757 | default: { |
| 5758 | } break; |
| 5759 | } |
| 5760 | return p_value; |
| 5761 | } |
| 5762 | |
| 5763 | Variant Animation::cast_from_blendwise(const Variant p_value, const Variant::Type p_type) { |
| 5764 | switch (p_type) { |
| 5765 | case Variant::BOOL: { |
| 5766 | return p_value.operator real_t() >= 0.5; |
| 5767 | } break; |
| 5768 | case Variant::INT: { |
| 5769 | return (int64_t)Math::round(p_value.operator double()); |
| 5770 | } break; |
| 5771 | case Variant::STRING: { |
| 5772 | return array_to_string(p_value); |
| 5773 | } break; |
| 5774 | case Variant::STRING_NAME: { |
| 5775 | return StringName(array_to_string(p_value)); |
| 5776 | } break; |
| 5777 | case Variant::RECT2I: { |
| 5778 | return Rect2i(p_value.operator Rect2().round()); |
| 5779 | } break; |
| 5780 | case Variant::VECTOR2I: { |
| 5781 | return Vector2i(p_value.operator Vector2().round()); |
| 5782 | } break; |
| 5783 | case Variant::VECTOR3I: { |
| 5784 | return Vector3i(p_value.operator Vector3().round()); |
| 5785 | } break; |
| 5786 | case Variant::VECTOR4I: { |
| 5787 | return Vector4i(p_value.operator Vector4().round()); |
| 5788 | } break; |
| 5789 | case Variant::PACKED_INT32_ARRAY: { |
| 5790 | PackedFloat32Array old_val = p_value.operator PackedFloat32Array(); |
| 5791 | PackedInt32Array new_val; |
| 5792 | new_val.resize(old_val.size()); |
| 5793 | int *new_val_w = new_val.ptrw(); |
| 5794 | for (int i = 0; i < old_val.size(); i++) { |
| 5795 | new_val_w[i] = (int32_t)Math::round(old_val[i]); |
| 5796 | } |
| 5797 | return new_val; |
| 5798 | } break; |
| 5799 | case Variant::PACKED_INT64_ARRAY: { |
| 5800 | PackedFloat64Array old_val = p_value.operator PackedFloat64Array(); |
| 5801 | PackedInt64Array new_val; |
| 5802 | for (int i = 0; i < old_val.size(); i++) { |
| 5803 | new_val.push_back((int64_t)Math::round(old_val[i])); |
| 5804 | } |
| 5805 | return new_val; |
| 5806 | } break; |
| 5807 | default: { |
| 5808 | } break; |
| 5809 | } |
| 5810 | return p_value; |
| 5811 | } |
| 5812 | |
| 5813 | Variant Animation::string_to_array(const Variant p_value) { |
| 5814 | if (!p_value.is_string()) { |
| 5815 | return p_value; |
| 5816 | }; |
| 5817 | const String &str = p_value.operator String(); |
| 5818 | PackedFloat32Array arr; |
| 5819 | for (int i = 0; i < str.length(); i++) { |
| 5820 | arr.push_back((float)str[i]); |
| 5821 | } |
| 5822 | return arr; |
| 5823 | } |
| 5824 | |
| 5825 | Variant Animation::array_to_string(const Variant p_value) { |
| 5826 | if (!p_value.is_array()) { |
| 5827 | return p_value; |
| 5828 | }; |
| 5829 | const PackedFloat32Array &arr = p_value.operator PackedFloat32Array(); |
| 5830 | String str; |
| 5831 | for (int i = 0; i < arr.size(); i++) { |
| 5832 | char32_t c = (char32_t)Math::round(arr[i]); |
| 5833 | if (c == 0 || (c & 0xfffff800) == 0xd800 || c > 0x10ffff) { |
| 5834 | c = ' '; |
| 5835 | } |
| 5836 | str += c; |
| 5837 | } |
| 5838 | return str; |
| 5839 | } |
| 5840 | |
| 5841 | Variant Animation::add_variant(const Variant &a, const Variant &b) { |
| 5842 | if (a.get_type() != b.get_type()) { |
| 5843 | if (a.is_num() && b.is_num()) { |
| 5844 | return add_variant(cast_to_blendwise(a), cast_to_blendwise(b)); |
| 5845 | } else if (!a.is_array()) { |
| 5846 | return a; |
| 5847 | } |
| 5848 | } |
| 5849 | |
| 5850 | switch (a.get_type()) { |
| 5851 | case Variant::NIL: { |
| 5852 | return Variant(); |
| 5853 | } break; |
| 5854 | case Variant::FLOAT: { |
| 5855 | return (a.operator double()) + (b.operator double()); |
| 5856 | } break; |
| 5857 | case Variant::RECT2: { |
| 5858 | const Rect2 ra = a.operator Rect2(); |
| 5859 | const Rect2 rb = b.operator Rect2(); |
| 5860 | return Rect2(ra.position + rb.position, ra.size + rb.size); |
| 5861 | } break; |
| 5862 | case Variant::PLANE: { |
| 5863 | const Plane pa = a.operator Plane(); |
| 5864 | const Plane pb = b.operator Plane(); |
| 5865 | return Plane(pa.normal + pb.normal, pa.d + pb.d); |
| 5866 | } break; |
| 5867 | case Variant::AABB: { |
| 5868 | const ::AABB aa = a.operator ::AABB(); |
| 5869 | const ::AABB ab = b.operator ::AABB(); |
| 5870 | return ::AABB(aa.position + ab.position, aa.size + ab.size); |
| 5871 | } break; |
| 5872 | case Variant::BASIS: { |
| 5873 | return (a.operator Basis()) * (b.operator Basis()); |
| 5874 | } break; |
| 5875 | case Variant::QUATERNION: { |
| 5876 | return (a.operator Quaternion()) * (b.operator Quaternion()); |
| 5877 | } break; |
| 5878 | case Variant::TRANSFORM2D: { |
| 5879 | return (a.operator Transform2D()) * (b.operator Transform2D()); |
| 5880 | } break; |
| 5881 | case Variant::TRANSFORM3D: { |
| 5882 | return (a.operator Transform3D()) * (b.operator Transform3D()); |
| 5883 | } break; |
| 5884 | case Variant::INT: |
| 5885 | case Variant::RECT2I: |
| 5886 | case Variant::VECTOR2I: |
| 5887 | case Variant::VECTOR3I: |
| 5888 | case Variant::VECTOR4I: |
| 5889 | case Variant::PACKED_INT32_ARRAY: |
| 5890 | case Variant::PACKED_INT64_ARRAY: { |
| 5891 | // Fallback the interpolatable value which needs casting. |
| 5892 | return cast_from_blendwise(add_variant(cast_to_blendwise(a), cast_to_blendwise(b)), a.get_type()); |
| 5893 | } break; |
| 5894 | case Variant::BOOL: |
| 5895 | case Variant::STRING: |
| 5896 | case Variant::STRING_NAME: { |
| 5897 | // Specialized for Tween. |
| 5898 | return b; |
| 5899 | } break; |
| 5900 | case Variant::PACKED_BYTE_ARRAY: { |
| 5901 | // Skip. |
| 5902 | } break; |
| 5903 | default: { |
| 5904 | if (a.is_array()) { |
| 5905 | const Array arr_a = a.operator Array(); |
| 5906 | const Array arr_b = b.operator Array(); |
| 5907 | |
| 5908 | int min_size = arr_a.size(); |
| 5909 | int max_size = arr_b.size(); |
| 5910 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 5911 | |
| 5912 | Array result; |
| 5913 | result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant()); |
| 5914 | result.resize(min_size); |
| 5915 | int i = 0; |
| 5916 | for (; i < min_size; i++) { |
| 5917 | result[i] = add_variant(arr_a[i], arr_b[i]); |
| 5918 | } |
| 5919 | if (min_size != max_size) { |
| 5920 | // Process with last element of the lesser array. |
| 5921 | // This is pretty funny and bizarre, but artists like to use it for polygon animation. |
| 5922 | Variant lesser_last; |
| 5923 | result.resize(max_size); |
| 5924 | if (is_a_larger) { |
| 5925 | if (i > 0) { |
| 5926 | lesser_last = arr_b[i - 1]; |
| 5927 | } else { |
| 5928 | Variant vz = arr_a[i]; |
| 5929 | vz.zero(); |
| 5930 | lesser_last = vz; |
| 5931 | } |
| 5932 | for (; i < max_size; i++) { |
| 5933 | result[i] = add_variant(arr_a[i], lesser_last); |
| 5934 | } |
| 5935 | } else { |
| 5936 | if (i > 0) { |
| 5937 | lesser_last = arr_a[i - 1]; |
| 5938 | } else { |
| 5939 | Variant vz = arr_b[i]; |
| 5940 | vz.zero(); |
| 5941 | lesser_last = vz; |
| 5942 | } |
| 5943 | for (; i < max_size; i++) { |
| 5944 | result[i] = add_variant(lesser_last, arr_b[i]); |
| 5945 | } |
| 5946 | } |
| 5947 | } |
| 5948 | return result; |
| 5949 | } |
| 5950 | } break; |
| 5951 | } |
| 5952 | return Variant::evaluate(Variant::OP_ADD, a, b); |
| 5953 | } |
| 5954 | |
| 5955 | Variant Animation::subtract_variant(const Variant &a, const Variant &b) { |
| 5956 | if (a.get_type() != b.get_type()) { |
| 5957 | if (a.is_num() && b.is_num()) { |
| 5958 | return subtract_variant(cast_to_blendwise(a), cast_to_blendwise(b)); |
| 5959 | } else if (!a.is_array()) { |
| 5960 | return a; |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | switch (a.get_type()) { |
| 5965 | case Variant::NIL: { |
| 5966 | return Variant(); |
| 5967 | } break; |
| 5968 | case Variant::FLOAT: { |
| 5969 | return (a.operator double()) - (b.operator double()); |
| 5970 | } break; |
| 5971 | case Variant::RECT2: { |
| 5972 | const Rect2 ra = a.operator Rect2(); |
| 5973 | const Rect2 rb = b.operator Rect2(); |
| 5974 | return Rect2(ra.position - rb.position, ra.size - rb.size); |
| 5975 | } break; |
| 5976 | case Variant::PLANE: { |
| 5977 | const Plane pa = a.operator Plane(); |
| 5978 | const Plane pb = b.operator Plane(); |
| 5979 | return Plane(pa.normal - pb.normal, pa.d - pb.d); |
| 5980 | } break; |
| 5981 | case Variant::AABB: { |
| 5982 | const ::AABB aa = a.operator ::AABB(); |
| 5983 | const ::AABB ab = b.operator ::AABB(); |
| 5984 | return ::AABB(aa.position - ab.position, aa.size - ab.size); |
| 5985 | } break; |
| 5986 | case Variant::BASIS: { |
| 5987 | return (b.operator Basis()).inverse() * (a.operator Basis()); |
| 5988 | } break; |
| 5989 | case Variant::QUATERNION: { |
| 5990 | return (b.operator Quaternion()).inverse() * (a.operator Quaternion()); |
| 5991 | } break; |
| 5992 | case Variant::TRANSFORM2D: { |
| 5993 | return (b.operator Transform2D()).affine_inverse() * (a.operator Transform2D()); |
| 5994 | } break; |
| 5995 | case Variant::TRANSFORM3D: { |
| 5996 | return (b.operator Transform3D()).affine_inverse() * (a.operator Transform3D()); |
| 5997 | } break; |
| 5998 | case Variant::INT: |
| 5999 | case Variant::RECT2I: |
| 6000 | case Variant::VECTOR2I: |
| 6001 | case Variant::VECTOR3I: |
| 6002 | case Variant::VECTOR4I: |
| 6003 | case Variant::PACKED_INT32_ARRAY: |
| 6004 | case Variant::PACKED_INT64_ARRAY: { |
| 6005 | // Fallback the interpolatable value which needs casting. |
| 6006 | return cast_from_blendwise(subtract_variant(cast_to_blendwise(a), cast_to_blendwise(b)), a.get_type()); |
| 6007 | } break; |
| 6008 | case Variant::BOOL: |
| 6009 | case Variant::STRING: |
| 6010 | case Variant::STRING_NAME: { |
| 6011 | // Specialized for Tween. |
| 6012 | return a; |
| 6013 | } break; |
| 6014 | case Variant::PACKED_BYTE_ARRAY: { |
| 6015 | // Skip. |
| 6016 | } break; |
| 6017 | default: { |
| 6018 | if (a.is_array()) { |
| 6019 | const Array arr_a = a.operator Array(); |
| 6020 | const Array arr_b = b.operator Array(); |
| 6021 | |
| 6022 | int min_size = arr_a.size(); |
| 6023 | int max_size = arr_b.size(); |
| 6024 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 6025 | |
| 6026 | Array result; |
| 6027 | result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant()); |
| 6028 | result.resize(min_size); |
| 6029 | int i = 0; |
| 6030 | for (; i < min_size; i++) { |
| 6031 | result[i] = subtract_variant(arr_a[i], arr_b[i]); |
| 6032 | } |
| 6033 | if (min_size != max_size) { |
| 6034 | // Process with last element of the lesser array. |
| 6035 | // This is pretty funny and bizarre, but artists like to use it for polygon animation. |
| 6036 | Variant lesser_last; |
| 6037 | result.resize(max_size); |
| 6038 | if (is_a_larger) { |
| 6039 | if (i > 0) { |
| 6040 | lesser_last = arr_b[i - 1]; |
| 6041 | } else { |
| 6042 | Variant vz = arr_a[i]; |
| 6043 | vz.zero(); |
| 6044 | lesser_last = vz; |
| 6045 | } |
| 6046 | for (; i < max_size; i++) { |
| 6047 | result[i] = subtract_variant(arr_a[i], lesser_last); |
| 6048 | } |
| 6049 | } else { |
| 6050 | if (i > 0) { |
| 6051 | lesser_last = arr_a[i - 1]; |
| 6052 | } else { |
| 6053 | Variant vz = arr_b[i]; |
| 6054 | vz.zero(); |
| 6055 | lesser_last = vz; |
| 6056 | } |
| 6057 | for (; i < max_size; i++) { |
| 6058 | result[i] = subtract_variant(lesser_last, arr_b[i]); |
| 6059 | } |
| 6060 | } |
| 6061 | } |
| 6062 | return result; |
| 6063 | } |
| 6064 | } break; |
| 6065 | } |
| 6066 | return Variant::evaluate(Variant::OP_SUBTRACT, a, b); |
| 6067 | } |
| 6068 | |
| 6069 | Variant Animation::blend_variant(const Variant &a, const Variant &b, float c) { |
| 6070 | if (a.get_type() != b.get_type()) { |
| 6071 | if (a.is_num() && b.is_num()) { |
| 6072 | return blend_variant(cast_to_blendwise(a), cast_to_blendwise(b), c); |
| 6073 | } else if (!a.is_array()) { |
| 6074 | return a; |
| 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | switch (a.get_type()) { |
| 6079 | case Variant::NIL: { |
| 6080 | return Variant(); |
| 6081 | } break; |
| 6082 | case Variant::FLOAT: { |
| 6083 | return (a.operator double()) + (b.operator double()) * c; |
| 6084 | } break; |
| 6085 | case Variant::VECTOR2: { |
| 6086 | return (a.operator Vector2()) + (b.operator Vector2()) * c; |
| 6087 | } break; |
| 6088 | case Variant::RECT2: { |
| 6089 | const Rect2 ra = a.operator Rect2(); |
| 6090 | const Rect2 rb = b.operator Rect2(); |
| 6091 | return Rect2(ra.position + rb.position * c, ra.size + rb.size * c); |
| 6092 | } break; |
| 6093 | case Variant::VECTOR3: { |
| 6094 | return (a.operator Vector3()) + (b.operator Vector3()) * c; |
| 6095 | } break; |
| 6096 | case Variant::VECTOR4: { |
| 6097 | return (a.operator Vector4()) + (b.operator Vector4()) * c; |
| 6098 | } break; |
| 6099 | case Variant::PLANE: { |
| 6100 | const Plane pa = a.operator Plane(); |
| 6101 | const Plane pb = b.operator Plane(); |
| 6102 | return Plane(pa.normal + pb.normal * c, pa.d + pb.d * c); |
| 6103 | } break; |
| 6104 | case Variant::COLOR: { |
| 6105 | return (a.operator Color()) + (b.operator Color()) * c; |
| 6106 | } break; |
| 6107 | case Variant::AABB: { |
| 6108 | const ::AABB aa = a.operator ::AABB(); |
| 6109 | const ::AABB ab = b.operator ::AABB(); |
| 6110 | return ::AABB(aa.position + ab.position * c, aa.size + ab.size * c); |
| 6111 | } break; |
| 6112 | case Variant::BASIS: { |
| 6113 | return (a.operator Basis()) + (b.operator Basis()) * c; |
| 6114 | } break; |
| 6115 | case Variant::QUATERNION: { |
| 6116 | return (a.operator Quaternion()) * Quaternion().slerp((b.operator Quaternion()), c); |
| 6117 | } break; |
| 6118 | case Variant::TRANSFORM2D: { |
| 6119 | return (a.operator Transform2D()) * Transform2D().interpolate_with((b.operator Transform2D()), c); |
| 6120 | } break; |
| 6121 | case Variant::TRANSFORM3D: { |
| 6122 | return (a.operator Transform3D()) * Transform3D().interpolate_with((b.operator Transform3D()), c); |
| 6123 | } break; |
| 6124 | case Variant::BOOL: |
| 6125 | case Variant::INT: |
| 6126 | case Variant::RECT2I: |
| 6127 | case Variant::VECTOR2I: |
| 6128 | case Variant::VECTOR3I: |
| 6129 | case Variant::VECTOR4I: |
| 6130 | case Variant::PACKED_INT32_ARRAY: |
| 6131 | case Variant::PACKED_INT64_ARRAY: { |
| 6132 | // Fallback the interpolatable value which needs casting. |
| 6133 | return cast_from_blendwise(blend_variant(cast_to_blendwise(a), cast_to_blendwise(b), c), a.get_type()); |
| 6134 | } break; |
| 6135 | case Variant::STRING: |
| 6136 | case Variant::STRING_NAME: { |
| 6137 | Array arr_a = cast_to_blendwise(a); |
| 6138 | Array arr_b = cast_to_blendwise(b); |
| 6139 | int min_size = arr_a.size(); |
| 6140 | int max_size = arr_b.size(); |
| 6141 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 6142 | int mid_size = interpolate_variant(arr_a.size(), arr_b.size(), c); |
| 6143 | if (is_a_larger) { |
| 6144 | arr_a.resize(mid_size); |
| 6145 | } else { |
| 6146 | arr_b.resize(mid_size); |
| 6147 | } |
| 6148 | return cast_from_blendwise(blend_variant(arr_a, arr_b, c), a.get_type()); |
| 6149 | } break; |
| 6150 | case Variant::PACKED_BYTE_ARRAY: { |
| 6151 | // Skip. |
| 6152 | } break; |
| 6153 | default: { |
| 6154 | if (a.is_array()) { |
| 6155 | const Array arr_a = a.operator Array(); |
| 6156 | const Array arr_b = b.operator Array(); |
| 6157 | |
| 6158 | int min_size = arr_a.size(); |
| 6159 | int max_size = arr_b.size(); |
| 6160 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 6161 | |
| 6162 | Array result; |
| 6163 | result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant()); |
| 6164 | result.resize(min_size); |
| 6165 | int i = 0; |
| 6166 | for (; i < min_size; i++) { |
| 6167 | result[i] = blend_variant(arr_a[i], arr_b[i], c); |
| 6168 | } |
| 6169 | if (min_size != max_size) { |
| 6170 | // Process with last element of the lesser array. |
| 6171 | // This is pretty funny and bizarre, but artists like to use it for polygon animation. |
| 6172 | Variant lesser_last; |
| 6173 | if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) { |
| 6174 | result.resize(max_size); |
| 6175 | if (i > 0) { |
| 6176 | lesser_last = arr_b[i - 1]; |
| 6177 | } else { |
| 6178 | Variant vz = arr_a[i]; |
| 6179 | vz.zero(); |
| 6180 | lesser_last = vz; |
| 6181 | } |
| 6182 | for (; i < max_size; i++) { |
| 6183 | result[i] = blend_variant(arr_a[i], lesser_last, c); |
| 6184 | } |
| 6185 | } else if (!is_a_larger && !Math::is_zero_approx(c)) { |
| 6186 | result.resize(max_size); |
| 6187 | if (i > 0) { |
| 6188 | lesser_last = arr_a[i - 1]; |
| 6189 | } else { |
| 6190 | Variant vz = arr_b[i]; |
| 6191 | vz.zero(); |
| 6192 | lesser_last = vz; |
| 6193 | } |
| 6194 | for (; i < max_size; i++) { |
| 6195 | result[i] = blend_variant(lesser_last, arr_b[i], c); |
| 6196 | } |
| 6197 | } |
| 6198 | } |
| 6199 | return result; |
| 6200 | } |
| 6201 | } break; |
| 6202 | } |
| 6203 | return c < 0.5 ? a : b; |
| 6204 | } |
| 6205 | |
| 6206 | Variant Animation::interpolate_variant(const Variant &a, const Variant &b, float c, bool p_snap_array_element) { |
| 6207 | if (a.get_type() != b.get_type()) { |
| 6208 | if (a.is_num() && b.is_num()) { |
| 6209 | return interpolate_variant(cast_to_blendwise(a), cast_to_blendwise(b), c); |
| 6210 | } else if (!a.is_array()) { |
| 6211 | return a; |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | switch (a.get_type()) { |
| 6216 | case Variant::NIL: { |
| 6217 | return Variant(); |
| 6218 | } break; |
| 6219 | case Variant::FLOAT: { |
| 6220 | return Math::lerp(a.operator double(), b.operator double(), (double)c); |
| 6221 | } break; |
| 6222 | case Variant::VECTOR2: { |
| 6223 | return (a.operator Vector2()).lerp(b.operator Vector2(), c); |
| 6224 | } break; |
| 6225 | case Variant::RECT2: { |
| 6226 | const Rect2 ra = a.operator Rect2(); |
| 6227 | const Rect2 rb = b.operator Rect2(); |
| 6228 | return Rect2(ra.position.lerp(rb.position, c), ra.size.lerp(rb.size, c)); |
| 6229 | } break; |
| 6230 | case Variant::VECTOR3: { |
| 6231 | return (a.operator Vector3()).lerp(b.operator Vector3(), c); |
| 6232 | } break; |
| 6233 | case Variant::VECTOR4: { |
| 6234 | return (a.operator Vector4()).lerp(b.operator Vector4(), c); |
| 6235 | } break; |
| 6236 | case Variant::PLANE: { |
| 6237 | const Plane pa = a.operator Plane(); |
| 6238 | const Plane pb = b.operator Plane(); |
| 6239 | return Plane(pa.normal.lerp(pb.normal, c), Math::lerp((double)pa.d, (double)pb.d, (double)c)); |
| 6240 | } break; |
| 6241 | case Variant::COLOR: { |
| 6242 | return (a.operator Color()).lerp(b.operator Color(), c); |
| 6243 | } break; |
| 6244 | case Variant::AABB: { |
| 6245 | const ::AABB aa = a.operator ::AABB(); |
| 6246 | const ::AABB ab = b.operator ::AABB(); |
| 6247 | return ::AABB(aa.position.lerp(ab.position, c), aa.size.lerp(ab.size, c)); |
| 6248 | } break; |
| 6249 | case Variant::BASIS: { |
| 6250 | return (a.operator Basis()).lerp(b.operator Basis(), c); |
| 6251 | } break; |
| 6252 | case Variant::QUATERNION: { |
| 6253 | return (a.operator Quaternion()).slerp(b.operator Quaternion(), c); |
| 6254 | } break; |
| 6255 | case Variant::TRANSFORM2D: { |
| 6256 | return (a.operator Transform2D()).interpolate_with(b.operator Transform2D(), c); |
| 6257 | } break; |
| 6258 | case Variant::TRANSFORM3D: { |
| 6259 | return (a.operator Transform3D()).interpolate_with(b.operator Transform3D(), c); |
| 6260 | } break; |
| 6261 | case Variant::BOOL: |
| 6262 | case Variant::INT: |
| 6263 | case Variant::RECT2I: |
| 6264 | case Variant::VECTOR2I: |
| 6265 | case Variant::VECTOR3I: |
| 6266 | case Variant::VECTOR4I: |
| 6267 | case Variant::PACKED_INT32_ARRAY: |
| 6268 | case Variant::PACKED_INT64_ARRAY: { |
| 6269 | // Fallback the interpolatable value which needs casting. |
| 6270 | return cast_from_blendwise(interpolate_variant(cast_to_blendwise(a), cast_to_blendwise(b), c), a.get_type()); |
| 6271 | } break; |
| 6272 | case Variant::STRING: |
| 6273 | case Variant::STRING_NAME: { |
| 6274 | Array arr_a = cast_to_blendwise(a); |
| 6275 | Array arr_b = cast_to_blendwise(b); |
| 6276 | int min_size = arr_a.size(); |
| 6277 | int max_size = arr_b.size(); |
| 6278 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 6279 | int mid_size = interpolate_variant(arr_a.size(), arr_b.size(), c); |
| 6280 | if (is_a_larger) { |
| 6281 | arr_a.resize(mid_size); |
| 6282 | } else { |
| 6283 | arr_b.resize(mid_size); |
| 6284 | } |
| 6285 | return cast_from_blendwise(interpolate_variant(arr_a, arr_b, c, true), a.get_type()); |
| 6286 | } break; |
| 6287 | case Variant::PACKED_BYTE_ARRAY: { |
| 6288 | // Skip. |
| 6289 | } break; |
| 6290 | default: { |
| 6291 | if (a.is_array()) { |
| 6292 | const Array arr_a = a.operator Array(); |
| 6293 | const Array arr_b = b.operator Array(); |
| 6294 | |
| 6295 | int min_size = arr_a.size(); |
| 6296 | int max_size = arr_b.size(); |
| 6297 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 6298 | |
| 6299 | Array result; |
| 6300 | result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant()); |
| 6301 | result.resize(min_size); |
| 6302 | int i = 0; |
| 6303 | for (; i < min_size; i++) { |
| 6304 | result[i] = interpolate_variant(arr_a[i], arr_b[i], c); |
| 6305 | } |
| 6306 | if (min_size != max_size) { |
| 6307 | // Process with last element of the lesser array. |
| 6308 | // This is pretty funny and bizarre, but artists like to use it for polygon animation. |
| 6309 | Variant lesser_last; |
| 6310 | if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) { |
| 6311 | result.resize(max_size); |
| 6312 | if (p_snap_array_element) { |
| 6313 | c = 0; |
| 6314 | } |
| 6315 | if (i > 0) { |
| 6316 | lesser_last = arr_b[i - 1]; |
| 6317 | } else { |
| 6318 | Variant vz = arr_a[i]; |
| 6319 | vz.zero(); |
| 6320 | lesser_last = vz; |
| 6321 | } |
| 6322 | for (; i < max_size; i++) { |
| 6323 | result[i] = interpolate_variant(arr_a[i], lesser_last, c); |
| 6324 | } |
| 6325 | } else if (!is_a_larger && !Math::is_zero_approx(c)) { |
| 6326 | result.resize(max_size); |
| 6327 | if (p_snap_array_element) { |
| 6328 | c = 1; |
| 6329 | } |
| 6330 | if (i > 0) { |
| 6331 | lesser_last = arr_a[i - 1]; |
| 6332 | } else { |
| 6333 | Variant vz = arr_b[i]; |
| 6334 | vz.zero(); |
| 6335 | lesser_last = vz; |
| 6336 | } |
| 6337 | for (; i < max_size; i++) { |
| 6338 | result[i] = interpolate_variant(lesser_last, arr_b[i], c); |
| 6339 | } |
| 6340 | } |
| 6341 | } |
| 6342 | return result; |
| 6343 | } |
| 6344 | } break; |
| 6345 | } |
| 6346 | return c < 0.5 ? a : b; |
| 6347 | } |
| 6348 | |
| 6349 | 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) { |
| 6350 | if (pre_a.get_type() != a.get_type() || pre_a.get_type() != b.get_type() || pre_a.get_type() != post_b.get_type()) { |
| 6351 | if (pre_a.is_num() && a.is_num() && b.is_num() && post_b.is_num()) { |
| 6352 | 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); |
| 6353 | } else if (!a.is_array()) { |
| 6354 | return a; |
| 6355 | } |
| 6356 | } |
| 6357 | |
| 6358 | switch (a.get_type()) { |
| 6359 | case Variant::NIL: { |
| 6360 | return Variant(); |
| 6361 | } break; |
| 6362 | case Variant::FLOAT: { |
| 6363 | 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); |
| 6364 | } break; |
| 6365 | case Variant::VECTOR2: { |
| 6366 | 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); |
| 6367 | } break; |
| 6368 | case Variant::RECT2: { |
| 6369 | const Rect2 rpa = pre_a.operator Rect2(); |
| 6370 | const Rect2 ra = a.operator Rect2(); |
| 6371 | const Rect2 rb = b.operator Rect2(); |
| 6372 | const Rect2 rpb = post_b.operator Rect2(); |
| 6373 | return Rect2( |
| 6374 | ra.position.cubic_interpolate_in_time(rb.position, rpa.position, rpb.position, c, p_b_t, p_pre_a_t, p_post_b_t), |
| 6375 | ra.size.cubic_interpolate_in_time(rb.size, rpa.size, rpb.size, c, p_b_t, p_pre_a_t, p_post_b_t)); |
| 6376 | } break; |
| 6377 | case Variant::VECTOR3: { |
| 6378 | 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); |
| 6379 | } break; |
| 6380 | case Variant::VECTOR4: { |
| 6381 | 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); |
| 6382 | } break; |
| 6383 | case Variant::PLANE: { |
| 6384 | const Plane ppa = pre_a.operator Plane(); |
| 6385 | const Plane pa = a.operator Plane(); |
| 6386 | const Plane pb = b.operator Plane(); |
| 6387 | const Plane ppb = post_b.operator Plane(); |
| 6388 | return Plane( |
| 6389 | pa.normal.cubic_interpolate_in_time(pb.normal, ppa.normal, ppb.normal, c, p_b_t, p_pre_a_t, p_post_b_t), |
| 6390 | 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)); |
| 6391 | } break; |
| 6392 | case Variant::COLOR: { |
| 6393 | const Color cpa = pre_a.operator Color(); |
| 6394 | const Color ca = a.operator Color(); |
| 6395 | const Color cb = b.operator Color(); |
| 6396 | const Color cpb = post_b.operator Color(); |
| 6397 | return Color( |
| 6398 | 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), |
| 6399 | 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), |
| 6400 | 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), |
| 6401 | 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)); |
| 6402 | } break; |
| 6403 | case Variant::AABB: { |
| 6404 | const ::AABB apa = pre_a.operator ::AABB(); |
| 6405 | const ::AABB aa = a.operator ::AABB(); |
| 6406 | const ::AABB ab = b.operator ::AABB(); |
| 6407 | const ::AABB apb = post_b.operator ::AABB(); |
| 6408 | return AABB( |
| 6409 | aa.position.cubic_interpolate_in_time(ab.position, apa.position, apb.position, c, p_b_t, p_pre_a_t, p_post_b_t), |
| 6410 | aa.size.cubic_interpolate_in_time(ab.size, apa.size, apb.size, c, p_b_t, p_pre_a_t, p_post_b_t)); |
| 6411 | } break; |
| 6412 | case Variant::BASIS: { |
| 6413 | const Basis bpa = pre_a.operator Basis(); |
| 6414 | const Basis ba = a.operator Basis(); |
| 6415 | const Basis bb = b.operator Basis(); |
| 6416 | const Basis bpb = post_b.operator Basis(); |
| 6417 | return Basis( |
| 6418 | 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), |
| 6419 | 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), |
| 6420 | 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)); |
| 6421 | } break; |
| 6422 | case Variant::QUATERNION: { |
| 6423 | 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); |
| 6424 | } break; |
| 6425 | case Variant::TRANSFORM2D: { |
| 6426 | const Transform2D tpa = pre_a.operator Transform2D(); |
| 6427 | const Transform2D ta = a.operator Transform2D(); |
| 6428 | const Transform2D tb = b.operator Transform2D(); |
| 6429 | const Transform2D tpb = post_b.operator Transform2D(); |
| 6430 | // TODO: May cause unintended skew, we needs spherical_cubic_interpolate_in_time() for angle and Transform2D::cubic_interpolate_with(). |
| 6431 | return Transform2D( |
| 6432 | ta[0].cubic_interpolate_in_time(tb[0], tpa[0], tpb[0], c, p_b_t, p_pre_a_t, p_post_b_t), |
| 6433 | ta[1].cubic_interpolate_in_time(tb[1], tpa[1], tpb[1], c, p_b_t, p_pre_a_t, p_post_b_t), |
| 6434 | ta[2].cubic_interpolate_in_time(tb[2], tpa[2], tpb[2], c, p_b_t, p_pre_a_t, p_post_b_t)); |
| 6435 | } break; |
| 6436 | case Variant::TRANSFORM3D: { |
| 6437 | const Transform3D tpa = pre_a.operator Transform3D(); |
| 6438 | const Transform3D ta = a.operator Transform3D(); |
| 6439 | const Transform3D tb = b.operator Transform3D(); |
| 6440 | const Transform3D tpb = post_b.operator Transform3D(); |
| 6441 | // TODO: May cause unintended skew, we needs Transform3D::cubic_interpolate_with(). |
| 6442 | return Transform3D( |
| 6443 | 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), |
| 6444 | 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), |
| 6445 | 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), |
| 6446 | ta.origin.cubic_interpolate_in_time(tb.origin, tpa.origin, tpb.origin, c, p_b_t, p_pre_a_t, p_post_b_t)); |
| 6447 | } break; |
| 6448 | case Variant::BOOL: |
| 6449 | case Variant::INT: |
| 6450 | case Variant::RECT2I: |
| 6451 | case Variant::VECTOR2I: |
| 6452 | case Variant::VECTOR3I: |
| 6453 | case Variant::VECTOR4I: |
| 6454 | case Variant::PACKED_INT32_ARRAY: |
| 6455 | case Variant::PACKED_INT64_ARRAY: { |
| 6456 | // Fallback the interpolatable value which needs casting. |
| 6457 | 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()); |
| 6458 | } break; |
| 6459 | case Variant::STRING: |
| 6460 | case Variant::STRING_NAME: { |
| 6461 | // TODO: |
| 6462 | // String interpolation works on both the character array size and the character code, to apply cubic interpolation neatly, |
| 6463 | // 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. |
| 6464 | return interpolate_variant(a, b, c); |
| 6465 | } break; |
| 6466 | case Variant::PACKED_BYTE_ARRAY: { |
| 6467 | // Skip. |
| 6468 | } break; |
| 6469 | default: { |
| 6470 | if (a.is_array()) { |
| 6471 | const Array arr_pa = pre_a.operator Array(); |
| 6472 | const Array arr_a = a.operator Array(); |
| 6473 | const Array arr_b = b.operator Array(); |
| 6474 | const Array arr_pb = post_b.operator Array(); |
| 6475 | |
| 6476 | int min_size = arr_a.size(); |
| 6477 | int max_size = arr_b.size(); |
| 6478 | bool is_a_larger = inform_variant_array(min_size, max_size); |
| 6479 | |
| 6480 | Array result; |
| 6481 | result.set_typed(MAX(arr_a.get_typed_builtin(), arr_b.get_typed_builtin()), StringName(), Variant()); |
| 6482 | result.resize(min_size); |
| 6483 | |
| 6484 | if (min_size == 0 && max_size == 0) { |
| 6485 | return result; |
| 6486 | } |
| 6487 | |
| 6488 | Variant vz; |
| 6489 | if (is_a_larger) { |
| 6490 | vz = arr_a[0]; |
| 6491 | } else { |
| 6492 | vz = arr_b[0]; |
| 6493 | } |
| 6494 | vz.zero(); |
| 6495 | Variant pre_last = arr_pa.size() ? arr_pa[arr_pa.size() - 1] : vz; |
| 6496 | Variant post_last = arr_pb.size() ? arr_pb[arr_pb.size() - 1] : vz; |
| 6497 | |
| 6498 | int i = 0; |
| 6499 | for (; i < min_size; i++) { |
| 6500 | 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); |
| 6501 | } |
| 6502 | if (min_size != max_size) { |
| 6503 | // Process with last element of the lesser array. |
| 6504 | // This is pretty funny and bizarre, but artists like to use it for polygon animation. |
| 6505 | Variant lesser_last = vz; |
| 6506 | if (is_a_larger && !Math::is_equal_approx(c, 1.0f)) { |
| 6507 | result.resize(max_size); |
| 6508 | if (p_snap_array_element) { |
| 6509 | c = 0; |
| 6510 | } |
| 6511 | if (i > 0) { |
| 6512 | lesser_last = arr_b[i - 1]; |
| 6513 | } |
| 6514 | for (; i < max_size; i++) { |
| 6515 | 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); |
| 6516 | } |
| 6517 | } else if (!is_a_larger && !Math::is_zero_approx(c)) { |
| 6518 | result.resize(max_size); |
| 6519 | if (p_snap_array_element) { |
| 6520 | c = 1; |
| 6521 | } |
| 6522 | if (i > 0) { |
| 6523 | lesser_last = arr_a[i - 1]; |
| 6524 | } |
| 6525 | for (; i < max_size; i++) { |
| 6526 | 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); |
| 6527 | } |
| 6528 | } |
| 6529 | } |
| 6530 | return result; |
| 6531 | } |
| 6532 | } break; |
| 6533 | } |
| 6534 | return c < 0.5 ? a : b; |
| 6535 | } |
| 6536 | |
| 6537 | bool Animation::inform_variant_array(int &r_min, int &r_max) { |
| 6538 | if (r_min <= r_max) { |
| 6539 | return false; |
| 6540 | } |
| 6541 | SWAP(r_min, r_max); |
| 6542 | return true; |
| 6543 | } |
| 6544 | |
| 6545 | Animation::Animation() { |
| 6546 | } |
| 6547 | |
| 6548 | Animation::~Animation() { |
| 6549 | for (uint32_t i = 0; i < tracks.size(); i++) { |
| 6550 | memdelete(tracks[i]); |
| 6551 | } |
| 6552 | } |