animation.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/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;
  60. InterpolationType interpolation;
  61. bool loop_wrap;
  62. NodePath path; // path to something
  63. bool imported;
  64. bool enabled;
  65. Track() {
  66. interpolation = INTERPOLATION_LINEAR;
  67. imported = false;
  68. loop_wrap = true;
  69. enabled = true;
  70. }
  71. virtual ~Track() {}
  72. };
  73. struct Key {
  74. float transition;
  75. float time; // time in secs
  76. Key() { transition = 1; }
  77. };
  78. // transform key holds either Vector3 or Quaternion
  79. template <class T>
  80. struct TKey : public Key {
  81. T value;
  82. };
  83. struct TransformKey {
  84. Vector3 loc;
  85. Quat rot;
  86. Vector3 scale;
  87. };
  88. /* TRANSFORM TRACK */
  89. struct TransformTrack : public Track {
  90. Vector<TKey<TransformKey>> transforms;
  91. TransformTrack() { type = TYPE_TRANSFORM; }
  92. };
  93. /* PROPERTY VALUE TRACK */
  94. struct ValueTrack : public Track {
  95. UpdateMode update_mode;
  96. bool update_on_seek;
  97. Vector<TKey<Variant>> values;
  98. ValueTrack() {
  99. type = TYPE_VALUE;
  100. update_mode = UPDATE_CONTINUOUS;
  101. }
  102. };
  103. /* METHOD TRACK */
  104. struct MethodKey : public Key {
  105. StringName method;
  106. Vector<Variant> params;
  107. };
  108. struct MethodTrack : public Track {
  109. Vector<MethodKey> methods;
  110. MethodTrack() { type = TYPE_METHOD; }
  111. };
  112. /* BEZIER TRACK */
  113. struct BezierKey {
  114. Vector2 in_handle; //relative (x always <0)
  115. Vector2 out_handle; //relative (x always >0)
  116. float value;
  117. };
  118. struct BezierTrack : public Track {
  119. Vector<TKey<BezierKey>> values;
  120. BezierTrack() {
  121. type = TYPE_BEZIER;
  122. }
  123. };
  124. /* AUDIO TRACK */
  125. struct AudioKey {
  126. RES stream;
  127. float start_offset; //offset from start
  128. float end_offset; //offset from end, if 0 then full length or infinite
  129. AudioKey() {
  130. start_offset = 0;
  131. end_offset = 0;
  132. }
  133. };
  134. struct AudioTrack : public Track {
  135. Vector<TKey<AudioKey>> values;
  136. AudioTrack() {
  137. type = TYPE_AUDIO;
  138. }
  139. };
  140. /* AUDIO TRACK */
  141. struct AnimationTrack : public Track {
  142. Vector<TKey<StringName>> values;
  143. AnimationTrack() {
  144. type = TYPE_ANIMATION;
  145. }
  146. };
  147. Vector<Track *> tracks;
  148. /*
  149. template<class T>
  150. int _insert_pos(float p_time, T& p_keys);*/
  151. template <class T>
  152. void _clear(T &p_keys);
  153. template <class T, class V>
  154. int _insert(float p_time, T &p_keys, const V &p_value);
  155. template <class K>
  156. inline int _find(const Vector<K> &p_keys, float p_time) const;
  157. _FORCE_INLINE_ Animation::TransformKey _interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const;
  158. _FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const;
  159. _FORCE_INLINE_ Quat _interpolate(const Quat &p_a, const Quat &p_b, float p_c) const;
  160. _FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, float p_c) const;
  161. _FORCE_INLINE_ float _interpolate(const float &p_a, const float &p_b, float p_c) const;
  162. _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;
  163. _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;
  164. _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;
  165. _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;
  166. _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;
  167. template <class T>
  168. _FORCE_INLINE_ T _interpolate(const Vector<TKey<T>> &p_keys, float p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const;
  169. template <class T>
  170. _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;
  171. _FORCE_INLINE_ void _value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const;
  172. _FORCE_INLINE_ void _method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const;
  173. float length;
  174. float step;
  175. bool loop;
  176. // bind helpers
  177. private:
  178. Array _transform_track_interpolate(int p_track, float p_time) const {
  179. Vector3 loc;
  180. Quat rot;
  181. Vector3 scale;
  182. transform_track_interpolate(p_track, p_time, &loc, &rot, &scale);
  183. Array ret;
  184. ret.push_back(loc);
  185. ret.push_back(rot);
  186. ret.push_back(scale);
  187. return ret;
  188. }
  189. PoolVector<int> _value_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  190. List<int> idxs;
  191. value_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  192. PoolVector<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. PoolVector<int> _method_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  199. List<int> idxs;
  200. method_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  201. PoolVector<int> idxr;
  202. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  203. idxr.push_back(E->get());
  204. }
  205. return idxr;
  206. }
  207. 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);
  208. 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);
  209. protected:
  210. bool _set(const StringName &p_name, const Variant &p_value);
  211. bool _get(const StringName &p_name, Variant &r_ret) const;
  212. void _get_property_list(List<PropertyInfo> *p_list) const;
  213. static void _bind_methods();
  214. public:
  215. int add_track(TrackType p_type, int p_at_pos = -1);
  216. void remove_track(int p_track);
  217. int get_track_count() const;
  218. TrackType track_get_type(int p_track) const;
  219. void track_set_path(int p_track, const NodePath &p_path);
  220. NodePath track_get_path(int p_track) const;
  221. int find_track(const NodePath &p_path) const;
  222. // transform
  223. void track_move_up(int p_track);
  224. void track_move_down(int p_track);
  225. void track_move_to(int p_track, int p_to_index);
  226. void track_swap(int p_track, int p_with_track);
  227. void track_set_imported(int p_track, bool p_imported);
  228. bool track_is_imported(int p_track) const;
  229. void track_set_enabled(int p_track, bool p_enabled);
  230. bool track_is_enabled(int p_track) const;
  231. void track_insert_key(int p_track, float p_time, const Variant &p_key, float p_transition = 1);
  232. void track_set_key_transition(int p_track, int p_key_idx, float p_transition);
  233. void track_set_key_value(int p_track, int p_key_idx, const Variant &p_value);
  234. void track_set_key_time(int p_track, int p_key_idx, float p_time);
  235. int track_find_key(int p_track, float p_time, bool p_exact = false) const;
  236. void track_remove_key(int p_track, int p_idx);
  237. void track_remove_key_at_position(int p_track, float p_pos);
  238. int track_get_key_count(int p_track) const;
  239. Variant track_get_key_value(int p_track, int p_key_idx) const;
  240. float track_get_key_time(int p_track, int p_key_idx) const;
  241. float track_get_key_transition(int p_track, int p_key_idx) const;
  242. 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());
  243. Error transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  244. void track_set_interpolation_type(int p_track, InterpolationType p_interp);
  245. InterpolationType track_get_interpolation_type(int p_track) const;
  246. int bezier_track_insert_key(int p_track, float p_time, float p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle);
  247. void bezier_track_set_key_value(int p_track, int p_index, float p_value);
  248. void bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle);
  249. void bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle);
  250. float bezier_track_get_key_value(int p_track, int p_index) const;
  251. Vector2 bezier_track_get_key_in_handle(int p_track, int p_index) const;
  252. Vector2 bezier_track_get_key_out_handle(int p_track, int p_index) const;
  253. float bezier_track_interpolate(int p_track, float p_time) const;
  254. 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);
  255. void audio_track_set_key_stream(int p_track, int p_key, const RES &p_stream);
  256. void audio_track_set_key_start_offset(int p_track, int p_key, float p_offset);
  257. void audio_track_set_key_end_offset(int p_track, int p_key, float p_offset);
  258. RES audio_track_get_key_stream(int p_track, int p_key) const;
  259. float audio_track_get_key_start_offset(int p_track, int p_key) const;
  260. float audio_track_get_key_end_offset(int p_track, int p_key) const;
  261. int animation_track_insert_key(int p_track, float p_time, const StringName &p_animation);
  262. void animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation);
  263. StringName animation_track_get_key_animation(int p_track, int p_key) const;
  264. void track_set_interpolation_loop_wrap(int p_track, bool p_enable);
  265. bool track_get_interpolation_loop_wrap(int p_track) const;
  266. Error transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  267. Variant value_track_interpolate(int p_track, float p_time) const;
  268. void value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  269. void value_track_set_update_mode(int p_track, UpdateMode p_mode);
  270. UpdateMode value_track_get_update_mode(int p_track) const;
  271. void method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  272. Vector<Variant> method_track_get_params(int p_track, int p_key_idx) const;
  273. StringName method_track_get_name(int p_track, int p_key_idx) const;
  274. void copy_track(int p_track, Ref<Animation> p_to_animation);
  275. void track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  276. void set_length(float p_length);
  277. float get_length() const;
  278. void set_loop(bool p_enabled);
  279. bool has_loop() const;
  280. void set_step(float p_step);
  281. float get_step() const;
  282. void clear();
  283. 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);
  284. Animation();
  285. ~Animation();
  286. };
  287. VARIANT_ENUM_CAST(Animation::TrackType);
  288. VARIANT_ENUM_CAST(Animation::InterpolationType);
  289. VARIANT_ENUM_CAST(Animation::UpdateMode);
  290. #endif // ANIMATION_H