animation.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*************************************************************************/
  2. /* animation.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "resource.h"
  33. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. class Animation : public Resource {
  37. OBJ_TYPE(Animation, Resource);
  38. RES_BASE_EXTENSION("anm");
  39. public:
  40. enum LoopMode {
  41. LOOP_NONE,
  42. LOOP_ENABLED,
  43. LOOP_WRAP
  44. };
  45. enum TrackType {
  46. TYPE_VALUE, ///< Set a value in a property, can be interpolated.
  47. TYPE_TRANSFORM, ///< Transform a node or a bone.
  48. TYPE_METHOD, ///< Call any method on a specific node.
  49. };
  50. enum InterpolationType {
  51. INTERPOLATION_NEAREST,
  52. INTERPOLATION_LINEAR,
  53. INTERPOLATION_CUBIC
  54. };
  55. enum UpdateMode {
  56. UPDATE_CONTINUOUS,
  57. UPDATE_DISCRETE,
  58. UPDATE_TRIGGER,
  59. };
  60. private:
  61. struct Track {
  62. TrackType type;
  63. InterpolationType interpolation;
  64. NodePath path; // path to something
  65. bool imported;
  66. Track() {
  67. interpolation = INTERPOLATION_LINEAR;
  68. imported = false;
  69. }
  70. virtual ~Track() {}
  71. };
  72. struct Key {
  73. float transition;
  74. float time; // time in secs
  75. Key() { transition = 1; }
  76. };
  77. // transform key holds either Vector3 or Quaternion
  78. template <class T>
  79. struct TKey : public Key {
  80. float time;
  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. Vector<Track *> tracks;
  113. /*
  114. template<class T>
  115. int _insert_pos(float p_time, T& p_keys);*/
  116. template <class T>
  117. void _clear(T &p_keys);
  118. template <class T, class V>
  119. int _insert(float p_time, T &p_keys, const V &p_value);
  120. template <class K>
  121. inline int _find(const Vector<K> &p_keys, float p_time) const;
  122. _FORCE_INLINE_ Animation::TransformKey _interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const;
  123. _FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const;
  124. _FORCE_INLINE_ Quat _interpolate(const Quat &p_a, const Quat &p_b, float p_c) const;
  125. _FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, float p_c) const;
  126. _FORCE_INLINE_ float _interpolate(const float &p_a, const float &p_b, float p_c) const;
  127. _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;
  128. _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;
  129. _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;
  130. _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;
  131. _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;
  132. template <class T>
  133. _FORCE_INLINE_ T _interpolate(const Vector<TKey<T> > &p_keys, float p_time, InterpolationType p_interp, bool *p_ok) const;
  134. _FORCE_INLINE_ void _value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const;
  135. _FORCE_INLINE_ void _method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const;
  136. float length;
  137. float step;
  138. bool loop;
  139. // bind helpers
  140. private:
  141. Array _transform_track_interpolate(int p_track, float p_time) const {
  142. Vector3 loc;
  143. Quat rot;
  144. Vector3 scale;
  145. transform_track_interpolate(p_track, p_time, &loc, &rot, &scale);
  146. Array ret;
  147. ret.push_back(loc);
  148. ret.push_back(rot);
  149. ret.push_back(scale);
  150. return ret;
  151. }
  152. DVector<int> _value_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  153. List<int> idxs;
  154. value_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  155. DVector<int> idxr;
  156. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  157. idxr.push_back(E->get());
  158. }
  159. return idxr;
  160. }
  161. DVector<int> _method_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  162. List<int> idxs;
  163. method_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  164. DVector<int> idxr;
  165. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  166. idxr.push_back(E->get());
  167. }
  168. return idxr;
  169. }
  170. 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);
  171. void _transform_track_optimize(int p_idx, float p_allowed_err = 0.05, float p_alowed_angular_err = 0.01, float p_max_optimizable_angle = Math_PI * 0.125);
  172. protected:
  173. bool _set(const StringName &p_name, const Variant &p_value);
  174. bool _get(const StringName &p_name, Variant &r_ret) const;
  175. void _get_property_list(List<PropertyInfo> *p_list) const;
  176. static void _bind_methods();
  177. public:
  178. int add_track(TrackType p_type, int p_at_pos = -1);
  179. void remove_track(int p_track);
  180. int get_track_count() const;
  181. TrackType track_get_type(int p_track) const;
  182. void track_set_path(int p_track, const NodePath &p_path);
  183. NodePath track_get_path(int p_track) const;
  184. int find_track(const NodePath &p_path) const;
  185. // transform
  186. void track_move_up(int p_track);
  187. void track_move_down(int p_track);
  188. void track_set_imported(int p_track, bool p_imported);
  189. bool track_is_imported(int p_track) const;
  190. 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());
  191. void track_insert_key(int p_track, float p_time, const Variant &p_key, float p_transition = 1);
  192. void track_set_key_transition(int p_track, int p_key_idx, float p_transition);
  193. void track_set_key_value(int p_track, int p_key_idx, const Variant &p_value);
  194. int track_find_key(int p_track, float p_time, bool p_exact = false) const;
  195. void track_remove_key(int p_track, int p_idx);
  196. void track_remove_key_at_pos(int p_track, float p_pos);
  197. int track_get_key_count(int p_track) const;
  198. Variant track_get_key_value(int p_track, int p_key_idx) const;
  199. float track_get_key_time(int p_track, int p_key_idx) const;
  200. float track_get_key_transition(int p_track, int p_key_idx) const;
  201. Error transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  202. void track_set_interpolation_type(int p_track, InterpolationType p_interp);
  203. InterpolationType track_get_interpolation_type(int p_track) const;
  204. Error transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  205. Variant value_track_interpolate(int p_track, float p_time) const;
  206. void value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  207. void value_track_set_update_mode(int p_track, UpdateMode p_mode);
  208. UpdateMode value_track_get_update_mode(int p_track) const;
  209. void method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  210. Vector<Variant> method_track_get_params(int p_track, int p_key_idx) const;
  211. StringName method_track_get_name(int p_track, int p_key_idx) const;
  212. void set_length(float p_length);
  213. float get_length() const;
  214. void set_loop(bool p_enabled);
  215. bool has_loop() const;
  216. void set_step(float p_step);
  217. float get_step() const;
  218. void clear();
  219. 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);
  220. Animation();
  221. ~Animation();
  222. };
  223. VARIANT_ENUM_CAST(Animation::TrackType);
  224. VARIANT_ENUM_CAST(Animation::InterpolationType);
  225. VARIANT_ENUM_CAST(Animation::UpdateMode);
  226. #endif