Debian Codegraph @

scene/resources/animation.h

raw symbols SCIP
1 /**************************************************************************/
2 /* animation.h */
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 #pragma once
32
33 #include "core/io/resource.h"
34 #include "core/templates/local_vector.h"
35
36 #define ANIM_MIN_LENGTH 0.001
37
38 class Animation : public Resource {
39 GDCLASS(Animation, Resource);
40 RES_BASE_EXTENSION("anim");
41
42 public:
43 typedef uint32_t TypeHash;
44
45 static inline String PARAMETERS_BASE_PATH = "parameters/";
46 static constexpr real_t DEFAULT_STEP = 1.0 / 30;
47
48 enum TrackType : uint8_t {
49 TYPE_VALUE, // Set a value in a property, can be interpolated.
50 TYPE_POSITION_3D, // Position 3D track, can be compressed.
51 TYPE_ROTATION_3D, // Rotation 3D track, can be compressed.
52 TYPE_SCALE_3D, // Scale 3D track, can be compressed.
53 TYPE_BLEND_SHAPE, // Blend Shape track, can be compressed.
54 TYPE_METHOD, // Call any method on a specific node.
55 TYPE_BEZIER, // Bezier curve.
56 TYPE_AUDIO,
57 TYPE_ANIMATION,
58 };
59
60 enum InterpolationType : uint8_t {
61 INTERPOLATION_NEAREST,
62 INTERPOLATION_LINEAR,
63 INTERPOLATION_CUBIC,
64 INTERPOLATION_LINEAR_ANGLE,
65 INTERPOLATION_CUBIC_ANGLE,
66 };
67
68 enum UpdateMode : uint8_t {
69 UPDATE_CONTINUOUS,
70 UPDATE_DISCRETE,
71 UPDATE_CAPTURE,
72 };
73
74 enum LoopMode : uint8_t {
75 LOOP_NONE,
76 LOOP_LINEAR,
77 LOOP_PINGPONG,
78 };
79
80 // LoopedFlag is used in Animataion to "process the keys at both ends correct".
81 enum LoopedFlag : uint8_t {
82 LOOPED_FLAG_NONE,
83 LOOPED_FLAG_END,
84 LOOPED_FLAG_START,
85 };
86
87 enum FindMode : uint8_t {
88 FIND_MODE_NEAREST,
89 FIND_MODE_APPROX,
90 FIND_MODE_EXACT,
91 };
92
93 #ifdef TOOLS_ENABLED
94 enum HandleMode {
95 HANDLE_MODE_FREE,
96 HANDLE_MODE_LINEAR,
97 HANDLE_MODE_BALANCED,
98 HANDLE_MODE_MIRRORED,
99 };
100 enum HandleSetMode {
101 HANDLE_SET_MODE_NONE,
102 HANDLE_SET_MODE_RESET,
103 HANDLE_SET_MODE_AUTO,
104 };
105 #endif // TOOLS_ENABLED
106
107 struct Track {
108 TrackType type = TrackType::TYPE_ANIMATION;
109 InterpolationType interpolation = INTERPOLATION_LINEAR;
110 bool loop_wrap = true;
111 NodePath path; // Path to something.
112 TypeHash thash = 0; // Hash by Path + SubPath + TrackType.
113 bool imported = false;
114 bool enabled = true;
115 virtual ~Track() {}
116 };
117
118 private:
119 struct Key {
120 real_t transition = 1.0;
121 double time = 0.0; // Time in secs.
122 };
123
124 // Transform key holds either Vector3 or Quaternion.
125 template <typename T>
126 struct TKey : public Key {
127 T value;
128 };
129
130 const int32_t POSITION_TRACK_SIZE = 5;
131 const int32_t ROTATION_TRACK_SIZE = 6;
132 const int32_t SCALE_TRACK_SIZE = 5;
133 const int32_t BLEND_SHAPE_TRACK_SIZE = 3;
134
135 /* POSITION TRACK */
136
137 struct PositionTrack : public Track {
138 LocalVector<TKey<Vector3>> positions;
139 int32_t compressed_track = -1;
140 PositionTrack() { type = TYPE_POSITION_3D; }
141 };
142
143 /* ROTATION TRACK */
144
145 struct RotationTrack : public Track {
146 LocalVector<TKey<Quaternion>> rotations;
147 int32_t compressed_track = -1;
148 RotationTrack() { type = TYPE_ROTATION_3D; }
149 };
150
151 /* SCALE TRACK */
152
153 struct ScaleTrack : public Track {
154 LocalVector<TKey<Vector3>> scales;
155 int32_t compressed_track = -1;
156 ScaleTrack() { type = TYPE_SCALE_3D; }
157 };
158
159 /* BLEND SHAPE TRACK */
160
161 struct BlendShapeTrack : public Track {
162 LocalVector<TKey<float>> blend_shapes;
163 int32_t compressed_track = -1;
164 BlendShapeTrack() { type = TYPE_BLEND_SHAPE; }
165 };
166
167 /* PROPERTY VALUE TRACK */
168
169 struct ValueTrack : public Track {
170 UpdateMode update_mode = UPDATE_CONTINUOUS;
171 LocalVector<TKey<Variant>> values;
172
173 ValueTrack() {
174 type = TYPE_VALUE;
175 }
176 };
177
178 /* METHOD TRACK */
179
180 struct MethodKey : public Key {
181 StringName method;
182 Vector<Variant> params;
183 };
184
185 struct MethodTrack : public Track {
186 LocalVector<MethodKey> methods;
187 MethodTrack() { type = TYPE_METHOD; }
188 };
189
190 /* BEZIER TRACK */
191
192 struct BezierKey {
193 Vector2 in_handle; // Relative (x always <0)
194 Vector2 out_handle; // Relative (x always >0)
195 real_t value = 0.0;
196 #ifdef TOOLS_ENABLED
197 HandleMode handle_mode = HANDLE_MODE_FREE;
198 #endif // TOOLS_ENABLED
199 };
200
201 struct BezierTrack : public Track {
202 LocalVector<TKey<BezierKey>> values;
203
204 BezierTrack() {
205 type = TYPE_BEZIER;
206 }
207 };
208
209 /* AUDIO TRACK */
210
211 struct AudioKey {
212 Ref<Resource> stream;
213 real_t start_offset = 0.0; // Offset from start.
214 real_t end_offset = 0.0; // Offset from end, if 0 then full length or infinite.
215 AudioKey() {
216 }
217 };
218
219 struct AudioTrack : public Track {
220 LocalVector<TKey<AudioKey>> values;
221 bool use_blend = true;
222
223 AudioTrack() {
224 type = TYPE_AUDIO;
225 }
226 };
227
228 /* ANIMATION TRACK */
229
230 struct AnimationTrack : public Track {
231 LocalVector<TKey<StringName>> values;
232
233 AnimationTrack() {
234 type = TYPE_ANIMATION;
235 }
236 };
237
238 /* Marker */
239
240 struct MarkerKey {
241 double time;
242 StringName name;
243 MarkerKey(double p_time, const StringName &p_name) :
244 time(p_time), name(p_name) {}
245 MarkerKey() = default;
246 };
247
248 LocalVector<MarkerKey> marker_names; // time -> name
249 HashMap<StringName, double> marker_times; // name -> time
250 HashMap<StringName, Color> marker_colors; // name -> color
251
252 LocalVector<Track *> tracks;
253
254 template <typename T, typename V>
255 int _insert(double p_time, T &p_keys, const V &p_value);
256
257 int _marker_insert(double p_time, LocalVector<MarkerKey> &p_keys, const MarkerKey &p_value);
258
259 template <typename K>
260
261 inline int _find(const LocalVector<K> &p_keys, double p_time, bool p_backward = false, bool p_limit = false) const;
262
263 _FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, real_t p_c) const;
264 _FORCE_INLINE_ Quaternion _interpolate(const Quaternion &p_a, const Quaternion &p_b, real_t p_c) const;
265 _FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, real_t p_c) const;
266 _FORCE_INLINE_ real_t _interpolate(const real_t &p_a, const real_t &p_b, real_t p_c) const;
267 _FORCE_INLINE_ Variant _interpolate_angle(const Variant &p_a, const Variant &p_b, real_t p_c) const;
268
269 _FORCE_INLINE_ Vector3 _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;
270 _FORCE_INLINE_ Quaternion _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;
271 _FORCE_INLINE_ Variant _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;
272 _FORCE_INLINE_ real_t _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;
273 _FORCE_INLINE_ Variant _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;
274
275 template <typename T>
276 _FORCE_INLINE_ T _interpolate(const LocalVector<TKey<T>> &p_keys, double p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok, bool p_backward = false) const;
277
278 template <typename T>
279 _FORCE_INLINE_ void _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;
280
281 double length = 1.0;
282 real_t step = DEFAULT_STEP;
283 LoopMode loop_mode = LOOP_NONE;
284 bool capture_included = false;
285 void _check_capture_included();
286
287 void _track_update_hash(int p_track);
288
289 /* Animation compression page format (version 1):
290 *
291 * Animation uses bitwidth based compression separated into small pages. The intention is that pages fit easily in the cache, so decoding is cache efficient.
292 * The page-based nature also makes future animation streaming from disk possible.
293 *
294 * Actual format:
295 *
296 * num_compressed_tracks = bounds.size()
297 * header : (x num_compressed_tracks)
298 * -------
299 * timeline_keys_offset : uint32_t - offset to time keys
300 * timeline_size : uint32_t - amount of time keys
301 * data_keys_offset : uint32_t offset to key data
302 *
303 * time key (uint32_t):
304 * ------------------
305 * frame : bits 0-15 - time offset of key, computed as: page.time_offset + frame * (1.0/fps)
306 * data_key_offset : bits 16-27 - offset to key data, computed as: data_keys_offset * 4 + data_key_offset
307 * data_key_count : bits 28-31 - amount of data keys pointed to, computed as: data_key_count+1 (max 16)
308 *
309 * data key:
310 * ---------
311 * X / Blend Shape : uint16_t - X coordinate of XYZ vector key, or Blend Shape value. If Blend shape, Y and Z are not present and can be ignored.
312 * Y : uint16_t
313 * Z : uint16_t
314 * If data_key_count+1 > 1 (if more than 1 key is stored):
315 * data_bitwidth : uint16_t - This is only present if data_key_count > 1. Contains delta bitwidth information.
316 * X / Blend Shape delta bitwidth: bits 0-3 -
317 * if 0, nothing is present for X (use the first key-value for subsequent keys),
318 * else assume the number of bits present for each element (+ 1 for sign). Assumed always 16 bits, delta max signed 15 bits, with underflow and overflow supported.
319 * Y delta bitwidth : bits 4-7
320 * Z delta bitwidth : bits 8-11
321 * FRAME delta bitwidth : 12-15 bits - always present (obviously), actual bitwidth is FRAME+1
322 * Data key is 4 bytes long for Blend Shapes, 8 bytes long for pos/rot/scale.
323 *
324 * delta keys:
325 * -----------
326 * Compressed format is packed in the following format after the data key, containing delta keys one after the next in a tightly bit packed fashion.
327 * FRAME bits -> X / Blend Shape Bits (if bitwidth > 0) -> Y Bits (if not Blend Shape and Y Bitwidth > 0) -> Z Bits (if not Blend Shape and Z Bitwidth > 0)
328 *
329 * data key format:
330 * ----------------
331 * Decoding keys means starting from the base key and going key by key applying deltas until the proper position is reached needed for interpolation.
332 * Resulting values are uint32_t
333 * data for X / Blend Shape, Y and Z must be normalized first: unorm = float(data) / 65535.0
334 * **Blend Shape**: (unorm * 2.0 - 1.0) * Compression::BLEND_SHAPE_RANGE
335 * **Pos/Scale**: unorm_vec3 * bounds[track].size + bounds[track].position
336 * **Rotation**: Quaternion(Vector3::octahedron_decode(unorm_vec3.xy),unorm_vec3.z * Math::PI * 2.0)
337 * **Frame**: page.time_offset + frame * (1.0/fps)
338 */
339
340 struct Compression {
341 enum {
342 MAX_DATA_TRACK_SIZE = 16384,
343 BLEND_SHAPE_RANGE = 8, // -8.0 to 8.0.
344 FORMAT_VERSION = 1
345 };
346 struct Page {
347 Vector<uint8_t> data;
348 double time_offset;
349 };
350
351 uint32_t fps = 120;
352 LocalVector<Page> pages;
353 LocalVector<AABB> bounds; // Used by position and scale tracks (which contain index to track and index to bounds).
354 bool enabled = false;
355 } compression;
356
357 Vector3i _compress_key(uint32_t p_track, const AABB &p_bounds, int32_t p_key = -1, float p_time = 0.0);
358 bool _rotation_interpolate_compressed(uint32_t p_compressed_track, double p_time, Quaternion &r_ret) const;
359 bool _pos_scale_interpolate_compressed(uint32_t p_compressed_track, double p_time, Vector3 &r_ret) const;
360 bool _blend_shape_interpolate_compressed(uint32_t p_compressed_track, double p_time, float &r_ret) const;
361 template <uint32_t COMPONENTS>
362 bool _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 = nullptr) const;
363 template <uint32_t COMPONENTS>
364 bool _fetch_compressed_by_index(uint32_t p_compressed_track, int p_index, Vector3i &r_value, double &r_time) const;
365 int _get_compressed_key_count(uint32_t p_compressed_track) const;
366 template <uint32_t COMPONENTS>
367 void _get_compressed_key_indices_in_range(uint32_t p_compressed_track, double p_time, double p_delta, List<int> *r_indices) const;
368 _FORCE_INLINE_ Quaternion _uncompress_quaternion(const Vector3i &p_value) const;
369 _FORCE_INLINE_ Vector3 _uncompress_pos_scale(uint32_t p_compressed_track, const Vector3i &p_value) const;
370 _FORCE_INLINE_ float _uncompress_blend_shape(const Vector3i &p_value) const;
371
372 // bind helpers
373 private:
374 bool _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);
375 bool _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);
376 bool _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);
377 bool _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);
378
379 void _position_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error);
380 void _rotation_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_error, real_t p_allowed_precision_error);
381 void _scale_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error);
382 void _blend_shape_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_precision_error);
383 void _value_track_optimize(int p_idx, real_t p_allowed_velocity_err, real_t p_allowed_angular_err, real_t p_allowed_precision_error);
384
385 protected:
386 bool _set(const StringName &p_name, const Variant &p_value);
387 bool _get(const StringName &p_name, Variant &r_ret) const;
388 void _get_property_list(List<PropertyInfo> *p_list) const;
389
390 virtual void reset_state() override;
391
392 static void _bind_methods();
393
394 static bool inform_variant_array(int &r_min, int &r_max); // Returns true if max and min are swapped.
395
396 #ifndef DISABLE_DEPRECATED
397 Vector3 _position_track_interpolate_bind_compat_86629(int p_track, double p_time) const;
398 Quaternion _rotation_track_interpolate_bind_compat_86629(int p_track, double p_time) const;
399 Vector3 _scale_track_interpolate_bind_compat_86629(int p_track, double p_time) const;
400 float _blend_shape_track_interpolate_bind_compat_86629(int p_track, double p_time) const;
401 Variant _value_track_interpolate_bind_compat_86629(int p_track, double p_time) const;
402 int _track_find_key_bind_compat_92861(int p_track, double p_time, FindMode p_find_mode = FIND_MODE_NEAREST) const;
403 static void _bind_compatibility_methods();
404 #endif // DISABLE_DEPRECATED
405
406 public:
407 int add_track(TrackType p_type, int p_at_pos = -1);
408 void remove_track(int p_track);
409
410 _FORCE_INLINE_ const LocalVector<Track *> &get_tracks() {
411 return tracks;
412 }
413
414 bool is_capture_included() const;
415
416 int get_track_count() const;
417 TrackType track_get_type(int p_track) const;
418
419 void track_set_path(int p_track, const NodePath &p_path);
420 NodePath track_get_path(int p_track) const;
421 int find_track(const NodePath &p_path, const TrackType p_type) const;
422
423 TypeHash track_get_type_hash(int p_track) const;
424
425 void track_move_up(int p_track);
426 void track_move_down(int p_track);
427 void track_move_to(int p_track, int p_to_index);
428 void track_swap(int p_track, int p_with_track);
429
430 void track_set_imported(int p_track, bool p_imported);
431 bool track_is_imported(int p_track) const;
432
433 void track_set_enabled(int p_track, bool p_enabled);
434 bool track_is_enabled(int p_track) const;
435
436 int track_insert_key(int p_track, double p_time, const Variant &p_key, real_t p_transition = 1);
437 void track_set_key_transition(int p_track, int p_key_idx, real_t p_transition);
438 void track_set_key_value(int p_track, int p_key_idx, const Variant &p_value);
439 void track_set_key_time(int p_track, int p_key_idx, double p_time);
440 int track_find_key(int p_track, double p_time, FindMode p_find_mode = FIND_MODE_NEAREST, bool p_limit = false, bool p_backward = false) const;
441 void track_remove_key(int p_track, int p_idx);
442 void track_remove_key_at_time(int p_track, double p_time);
443 int track_get_key_count(int p_track) const;
444 Variant track_get_key_value(int p_track, int p_key_idx) const;
445 double track_get_key_time(int p_track, int p_key_idx) const;
446 real_t track_get_key_transition(int p_track, int p_key_idx) const;
447 bool track_is_compressed(int p_track) const;
448
449 int position_track_insert_key(int p_track, double p_time, const Vector3 &p_position);
450 Error position_track_get_key(int p_track, int p_key, Vector3 *r_position) const;
451 Error try_position_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation, bool p_backward = false) const;
452 Vector3 position_track_interpolate(int p_track, double p_time, bool p_backward = false) const;
453
454 int rotation_track_insert_key(int p_track, double p_time, const Quaternion &p_rotation);
455 Error rotation_track_get_key(int p_track, int p_key, Quaternion *r_rotation) const;
456 Error try_rotation_track_interpolate(int p_track, double p_time, Quaternion *r_interpolation, bool p_backward = false) const;
457 Quaternion rotation_track_interpolate(int p_track, double p_time, bool p_backward = false) const;
458
459 int scale_track_insert_key(int p_track, double p_time, const Vector3 &p_scale);
460 Error scale_track_get_key(int p_track, int p_key, Vector3 *r_scale) const;
461 Error try_scale_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation, bool p_backward = false) const;
462 Vector3 scale_track_interpolate(int p_track, double p_time, bool p_backward = false) const;
463
464 int blend_shape_track_insert_key(int p_track, double p_time, float p_blend);
465 Error blend_shape_track_get_key(int p_track, int p_key, float *r_blend) const;
466 Error try_blend_shape_track_interpolate(int p_track, double p_time, float *r_blend, bool p_backward = false) const;
467 float blend_shape_track_interpolate(int p_track, double p_time, bool p_backward = false) const;
468
469 void track_set_interpolation_type(int p_track, InterpolationType p_interp);
470 InterpolationType track_get_interpolation_type(int p_track) const;
471
472 Array make_default_bezier_key(float p_value);
473 int bezier_track_insert_key(int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle);
474 void bezier_track_set_key_value(int p_track, int p_index, real_t p_value);
475 void bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio = 1.0);
476 void bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle, real_t p_balanced_value_time_ratio = 1.0);
477 real_t bezier_track_get_key_value(int p_track, int p_index) const;
478 Vector2 bezier_track_get_key_in_handle(int p_track, int p_index) const;
479 Vector2 bezier_track_get_key_out_handle(int p_track, int p_index) const;
480 #ifdef TOOLS_ENABLED
481 void bezier_track_set_key_handle_mode(int p_track, int p_index, HandleMode p_mode, HandleSetMode p_set_mode = HANDLE_SET_MODE_NONE);
482 HandleMode bezier_track_get_key_handle_mode(int p_track, int p_index) const;
483 bool 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);
484 bool 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);
485 #endif // TOOLS_ENABLED
486
487 real_t bezier_track_interpolate(int p_track, double p_time) const;
488
489 int audio_track_insert_key(int p_track, double p_time, const Ref<Resource> &p_stream, real_t p_start_offset = 0, real_t p_end_offset = 0);
490 void audio_track_set_key_stream(int p_track, int p_key, const Ref<Resource> &p_stream);
491 void audio_track_set_key_start_offset(int p_track, int p_key, real_t p_offset);
492 void audio_track_set_key_end_offset(int p_track, int p_key, real_t p_offset);
493 Ref<Resource> audio_track_get_key_stream(int p_track, int p_key) const;
494 real_t audio_track_get_key_start_offset(int p_track, int p_key) const;
495 real_t audio_track_get_key_end_offset(int p_track, int p_key) const;
496 void audio_track_set_use_blend(int p_track, bool p_enable);
497 bool audio_track_is_use_blend(int p_track) const;
498
499 int animation_track_insert_key(int p_track, double p_time, const StringName &p_animation);
500 void animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation);
501 StringName animation_track_get_key_animation(int p_track, int p_key) const;
502
503 void track_set_interpolation_loop_wrap(int p_track, bool p_enable);
504 bool track_get_interpolation_loop_wrap(int p_track) const;
505
506 Variant value_track_interpolate(int p_track, double p_time, bool p_backward = false) const;
507 void value_track_set_update_mode(int p_track, UpdateMode p_mode);
508 UpdateMode value_track_get_update_mode(int p_track) const;
509
510 Vector<Variant> method_track_get_params(int p_track, int p_key_idx) const;
511 StringName method_track_get_name(int p_track, int p_key_idx) const;
512
513 void copy_track(int p_track, Ref<Animation> p_to_animation);
514
515 void 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 = Animation::LOOPED_FLAG_NONE) const;
516
517 void add_marker(const StringName &p_name, double p_time);
518 void remove_marker(const StringName &p_name);
519 bool has_marker(const StringName &p_name) const;
520 StringName get_marker_at_time(double p_time) const;
521 StringName get_next_marker(double p_time) const;
522 StringName get_prev_marker(double p_time) const;
523 double get_marker_time(const StringName &p_time) const;
524 PackedStringArray get_marker_names() const;
525 Color get_marker_color(const StringName &p_name) const;
526 void set_marker_color(const StringName &p_name, const Color &p_color);
527
528 void set_length(real_t p_length);
529 real_t get_length() const;
530
531 void set_loop_mode(LoopMode p_loop_mode);
532 LoopMode get_loop_mode() const;
533
534 void set_step(real_t p_step);
535 real_t get_step() const;
536
537 void clear();
538
539 void optimize(real_t p_allowed_velocity_err = 0.01, real_t p_allowed_angular_err = 0.01, int p_precision = 3);
540 void compress(uint32_t p_page_size = 8192, uint32_t p_fps = 120, float p_split_tolerance = 4.0); // 4.0 seems to be the split tolerance sweet spot from many tests.
541
542 // Helper functions for Rotation.
543 static double interpolate_via_rest(double p_from, double p_to, double p_weight, double p_rest = 0.0); // Deterministic slerp to prevent to cross the inverted rest axis.
544 static Quaternion interpolate_via_rest(const Quaternion &p_from, const Quaternion &p_to, real_t p_weight, const Quaternion &p_rest = Quaternion()); // Deterministic slerp to prevent to cross the inverted rest axis.
545
546 // Helper functions for Variant.
547 static bool is_variant_interpolatable(const Variant p_value);
548 static bool validate_type_match(const Variant &p_from, Variant &r_to);
549
550 static Variant cast_to_blendwise(const Variant p_value);
551 static Variant cast_from_blendwise(const Variant p_value, const Variant::Type p_type);
552
553 static Variant string_to_array(const Variant p_value);
554 static Variant array_to_string(const Variant p_value);
555
556 static Variant add_variant(const Variant &a, const Variant &b);
557 static Variant subtract_variant(const Variant &a, const Variant &b);
558 static Variant blend_variant(const Variant &a, const Variant &b, float c);
559 static Variant interpolate_variant(const Variant &a, const Variant &b, float c, bool p_snap_array_element = false);
560 static Variant 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 = false);
561
562 static bool is_less_or_equal_approx(double a, double b) {
563 return a < b || Math::is_equal_approx(a, b);
564 }
565
566 static bool is_less_approx(double a, double b) {
567 return a < b && !Math::is_equal_approx(a, b);
568 }
569
570 static bool is_greater_or_equal_approx(double a, double b) {
571 return a > b || Math::is_equal_approx(a, b);
572 }
573
574 static bool is_greater_approx(double a, double b) {
575 return a > b && !Math::is_equal_approx(a, b);
576 }
577
578 static TrackType get_cache_type(TrackType p_type);
579
580 Animation();
581 ~Animation();
582 };
583
584 VARIANT_ENUM_CAST(Animation::TrackType);
585 VARIANT_ENUM_CAST(Animation::InterpolationType);
586 VARIANT_ENUM_CAST(Animation::UpdateMode);
587 VARIANT_ENUM_CAST(Animation::LoopMode);
588 VARIANT_ENUM_CAST(Animation::LoopedFlag);
589 VARIANT_ENUM_CAST(Animation::FindMode);
590 #ifdef TOOLS_ENABLED
591 VARIANT_ENUM_CAST(Animation::HandleMode);
592 VARIANT_ENUM_CAST(Animation::HandleSetMode);
593 #endif // TOOLS_ENABLED