animation.h 25 KB

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