animation.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*************************************************************************/
  2. /* animation.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #define ANIM_MIN_LENGTH 0.001
  34. class Animation : public Resource {
  35. GDCLASS(Animation, Resource);
  36. RES_BASE_EXTENSION("anim");
  37. public:
  38. enum TrackType {
  39. TYPE_VALUE, ///< Set a value in a property, can be interpolated.
  40. TYPE_TRANSFORM, ///< Transform a node or a bone.
  41. TYPE_METHOD, ///< Call any method on a specific node.
  42. TYPE_BEZIER, ///< Bezier curve
  43. TYPE_AUDIO,
  44. TYPE_ANIMATION,
  45. };
  46. enum InterpolationType {
  47. INTERPOLATION_NEAREST,
  48. INTERPOLATION_LINEAR,
  49. INTERPOLATION_CUBIC
  50. };
  51. enum UpdateMode {
  52. UPDATE_CONTINUOUS,
  53. UPDATE_DISCRETE,
  54. UPDATE_TRIGGER,
  55. UPDATE_CAPTURE,
  56. };
  57. private:
  58. struct Track {
  59. TrackType type = TrackType::TYPE_ANIMATION;
  60. InterpolationType interpolation = INTERPOLATION_LINEAR;
  61. bool loop_wrap = true;
  62. NodePath path; // path to something
  63. bool imported = false;
  64. bool enabled = false;
  65. Track() {}
  66. virtual ~Track() {}
  67. };
  68. struct Key {
  69. float transition = 1.0;
  70. float time = 0.0; // time in secs
  71. };
  72. // transform key holds either Vector3 or Quaternion
  73. template <class T>
  74. struct TKey : public Key {
  75. T value;
  76. };
  77. struct TransformKey {
  78. Vector3 loc;
  79. Quat rot;
  80. Vector3 scale;
  81. };
  82. /* TRANSFORM TRACK */
  83. struct TransformTrack : public Track {
  84. Vector<TKey<TransformKey>> transforms;
  85. TransformTrack() { type = TYPE_TRANSFORM; }
  86. };
  87. /* PROPERTY VALUE TRACK */
  88. struct ValueTrack : public Track {
  89. UpdateMode update_mode = UPDATE_CONTINUOUS;
  90. bool update_on_seek = false;
  91. Vector<TKey<Variant>> values;
  92. ValueTrack() {
  93. type = TYPE_VALUE;
  94. }
  95. };
  96. /* METHOD TRACK */
  97. struct MethodKey : public Key {
  98. StringName method;
  99. Vector<Variant> params;
  100. };
  101. struct MethodTrack : public Track {
  102. Vector<MethodKey> methods;
  103. MethodTrack() { type = TYPE_METHOD; }
  104. };
  105. /* BEZIER TRACK */
  106. struct BezierKey {
  107. Vector2 in_handle; //relative (x always <0)
  108. Vector2 out_handle; //relative (x always >0)
  109. float value = 0.0;
  110. };
  111. struct BezierTrack : public Track {
  112. Vector<TKey<BezierKey>> values;
  113. BezierTrack() {
  114. type = TYPE_BEZIER;
  115. }
  116. };
  117. /* AUDIO TRACK */
  118. struct AudioKey {
  119. RES stream;
  120. float start_offset = 0.0; //offset from start
  121. float end_offset = 0.0; //offset from end, if 0 then full length or infinite
  122. AudioKey() {
  123. }
  124. };
  125. struct AudioTrack : public Track {
  126. Vector<TKey<AudioKey>> values;
  127. AudioTrack() {
  128. type = TYPE_AUDIO;
  129. }
  130. };
  131. /* AUDIO TRACK */
  132. struct AnimationTrack : public Track {
  133. Vector<TKey<StringName>> values;
  134. AnimationTrack() {
  135. type = TYPE_ANIMATION;
  136. }
  137. };
  138. Vector<Track *> tracks;
  139. /*
  140. template<class T>
  141. int _insert_pos(float p_time, T& p_keys);*/
  142. template <class T>
  143. void _clear(T &p_keys);
  144. template <class T, class V>
  145. int _insert(float p_time, T &p_keys, const V &p_value);
  146. template <class K>
  147. inline int _find(const Vector<K> &p_keys, float p_time) const;
  148. _FORCE_INLINE_ Animation::TransformKey _interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const;
  149. _FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const;
  150. _FORCE_INLINE_ Quat _interpolate(const Quat &p_a, const Quat &p_b, float p_c) const;
  151. _FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, float p_c) const;
  152. _FORCE_INLINE_ float _interpolate(const float &p_a, const float &p_b, float p_c) const;
  153. _FORCE_INLINE_ Animation::TransformKey _cubic_interpolate(const Animation::TransformKey &p_pre_a, const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, const Animation::TransformKey &p_post_b, float p_c) const;
  154. _FORCE_INLINE_ Vector3 _cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, float p_c) const;
  155. _FORCE_INLINE_ Quat _cubic_interpolate(const Quat &p_pre_a, const Quat &p_a, const Quat &p_b, const Quat &p_post_b, float p_c) const;
  156. _FORCE_INLINE_ Variant _cubic_interpolate(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, float p_c) const;
  157. _FORCE_INLINE_ float _cubic_interpolate(const float &p_pre_a, const float &p_a, const float &p_b, const float &p_post_b, float p_c) const;
  158. template <class T>
  159. _FORCE_INLINE_ T _interpolate(const Vector<TKey<T>> &p_keys, float p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const;
  160. template <class T>
  161. _FORCE_INLINE_ void _track_get_key_indices_in_range(const Vector<T> &p_array, float from_time, float to_time, List<int> *p_indices) const;
  162. _FORCE_INLINE_ void _value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const;
  163. _FORCE_INLINE_ void _method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const;
  164. float length = 1.0;
  165. float step = 0.1;
  166. bool loop = false;
  167. // bind helpers
  168. private:
  169. Array _transform_track_interpolate(int p_track, float p_time) const {
  170. Vector3 loc;
  171. Quat rot;
  172. Vector3 scale;
  173. transform_track_interpolate(p_track, p_time, &loc, &rot, &scale);
  174. Array ret;
  175. ret.push_back(loc);
  176. ret.push_back(rot);
  177. ret.push_back(scale);
  178. return ret;
  179. }
  180. Vector<int> _value_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  181. List<int> idxs;
  182. value_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  183. Vector<int> idxr;
  184. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  185. idxr.push_back(E->get());
  186. }
  187. return idxr;
  188. }
  189. Vector<int> _method_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  190. List<int> idxs;
  191. method_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  192. Vector<int> idxr;
  193. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  194. idxr.push_back(E->get());
  195. }
  196. return idxr;
  197. }
  198. bool _transform_track_optimize_key(const TKey<TransformKey> &t0, const TKey<TransformKey> &t1, const TKey<TransformKey> &t2, float p_alowed_linear_err, float p_alowed_angular_err, float p_max_optimizable_angle, const Vector3 &p_norm);
  199. void _transform_track_optimize(int p_idx, float p_allowed_linear_err = 0.05, float p_allowed_angular_err = 0.01, float p_max_optimizable_angle = Math_PI * 0.125);
  200. protected:
  201. bool _set(const StringName &p_name, const Variant &p_value);
  202. bool _get(const StringName &p_name, Variant &r_ret) const;
  203. void _get_property_list(List<PropertyInfo> *p_list) const;
  204. virtual void reset_state() override;
  205. static void _bind_methods();
  206. public:
  207. int add_track(TrackType p_type, int p_at_pos = -1);
  208. void remove_track(int p_track);
  209. int get_track_count() const;
  210. TrackType track_get_type(int p_track) const;
  211. void track_set_path(int p_track, const NodePath &p_path);
  212. NodePath track_get_path(int p_track) const;
  213. int find_track(const NodePath &p_path) const;
  214. // transform
  215. void track_move_up(int p_track);
  216. void track_move_down(int p_track);
  217. void track_move_to(int p_track, int p_to_index);
  218. void track_swap(int p_track, int p_with_track);
  219. void track_set_imported(int p_track, bool p_imported);
  220. bool track_is_imported(int p_track) const;
  221. void track_set_enabled(int p_track, bool p_enabled);
  222. bool track_is_enabled(int p_track) const;
  223. void track_insert_key(int p_track, float p_time, const Variant &p_key, float p_transition = 1);
  224. void track_set_key_transition(int p_track, int p_key_idx, float p_transition);
  225. void track_set_key_value(int p_track, int p_key_idx, const Variant &p_value);
  226. void track_set_key_time(int p_track, int p_key_idx, float p_time);
  227. int track_find_key(int p_track, float p_time, bool p_exact = false) const;
  228. void track_remove_key(int p_track, int p_idx);
  229. void track_remove_key_at_time(int p_track, float p_time);
  230. int track_get_key_count(int p_track) const;
  231. Variant track_get_key_value(int p_track, int p_key_idx) const;
  232. float track_get_key_time(int p_track, int p_key_idx) const;
  233. float track_get_key_transition(int p_track, int p_key_idx) const;
  234. int transform_track_insert_key(int p_track, float p_time, const Vector3 &p_loc, const Quat &p_rot = Quat(), const Vector3 &p_scale = Vector3());
  235. Error transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  236. void track_set_interpolation_type(int p_track, InterpolationType p_interp);
  237. InterpolationType track_get_interpolation_type(int p_track) const;
  238. int bezier_track_insert_key(int p_track, float p_time, float p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle);
  239. void bezier_track_set_key_value(int p_track, int p_index, float p_value);
  240. void bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle);
  241. void bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle);
  242. float bezier_track_get_key_value(int p_track, int p_index) const;
  243. Vector2 bezier_track_get_key_in_handle(int p_track, int p_index) const;
  244. Vector2 bezier_track_get_key_out_handle(int p_track, int p_index) const;
  245. float bezier_track_interpolate(int p_track, float p_time) const;
  246. int audio_track_insert_key(int p_track, float p_time, const RES &p_stream, float p_start_offset = 0, float p_end_offset = 0);
  247. void audio_track_set_key_stream(int p_track, int p_key, const RES &p_stream);
  248. void audio_track_set_key_start_offset(int p_track, int p_key, float p_offset);
  249. void audio_track_set_key_end_offset(int p_track, int p_key, float p_offset);
  250. RES audio_track_get_key_stream(int p_track, int p_key) const;
  251. float audio_track_get_key_start_offset(int p_track, int p_key) const;
  252. float audio_track_get_key_end_offset(int p_track, int p_key) const;
  253. int animation_track_insert_key(int p_track, float p_time, const StringName &p_animation);
  254. void animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation);
  255. StringName animation_track_get_key_animation(int p_track, int p_key) const;
  256. void track_set_interpolation_loop_wrap(int p_track, bool p_enable);
  257. bool track_get_interpolation_loop_wrap(int p_track) const;
  258. Error transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  259. Variant value_track_interpolate(int p_track, float p_time) const;
  260. void value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  261. void value_track_set_update_mode(int p_track, UpdateMode p_mode);
  262. UpdateMode value_track_get_update_mode(int p_track) const;
  263. void method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  264. Vector<Variant> method_track_get_params(int p_track, int p_key_idx) const;
  265. StringName method_track_get_name(int p_track, int p_key_idx) const;
  266. void copy_track(int p_track, Ref<Animation> p_to_animation);
  267. void track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  268. void set_length(float p_length);
  269. float get_length() const;
  270. void set_loop(bool p_enabled);
  271. bool has_loop() const;
  272. void set_step(float p_step);
  273. float get_step() const;
  274. void clear();
  275. void optimize(float p_allowed_linear_err = 0.05, float p_allowed_angular_err = 0.01, float p_max_optimizable_angle = Math_PI * 0.125);
  276. Animation();
  277. ~Animation();
  278. };
  279. VARIANT_ENUM_CAST(Animation::TrackType);
  280. VARIANT_ENUM_CAST(Animation::InterpolationType);
  281. VARIANT_ENUM_CAST(Animation::UpdateMode);
  282. #endif