animation_player.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*************************************************************************/
  2. /* animation_player.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_PLAYER_H
  31. #define ANIMATION_PLAYER_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/3d/mesh_instance_3d.h"
  34. #include "scene/3d/node_3d.h"
  35. #include "scene/3d/skeleton_3d.h"
  36. #include "scene/resources/animation.h"
  37. #ifdef TOOLS_ENABLED
  38. class AnimatedValuesBackup : public RefCounted {
  39. GDCLASS(AnimatedValuesBackup, RefCounted);
  40. struct Entry {
  41. Object *object = nullptr;
  42. Vector<StringName> subpath; // Unused if bone
  43. int bone_idx = -1; // -1 if not a bone
  44. Variant value;
  45. };
  46. Vector<Entry> entries;
  47. friend class AnimationPlayer;
  48. protected:
  49. static void _bind_methods();
  50. public:
  51. void update_skeletons();
  52. void restore() const;
  53. };
  54. #endif
  55. class AnimationPlayer : public Node {
  56. GDCLASS(AnimationPlayer, Node);
  57. OBJ_CATEGORY("Animation Nodes");
  58. public:
  59. enum AnimationProcessCallback {
  60. ANIMATION_PROCESS_PHYSICS,
  61. ANIMATION_PROCESS_IDLE,
  62. ANIMATION_PROCESS_MANUAL,
  63. };
  64. enum AnimationMethodCallMode {
  65. ANIMATION_METHOD_CALL_DEFERRED,
  66. ANIMATION_METHOD_CALL_IMMEDIATE,
  67. };
  68. private:
  69. enum {
  70. NODE_CACHE_UPDATE_MAX = 1024,
  71. BLEND_FROM_MAX = 3
  72. };
  73. enum SpecialProperty {
  74. SP_NONE,
  75. SP_NODE2D_POS,
  76. SP_NODE2D_ROT,
  77. SP_NODE2D_SCALE,
  78. };
  79. uint32_t setup_pass = 1;
  80. struct TrackNodeCache {
  81. NodePath path;
  82. uint32_t id = 0;
  83. RES resource;
  84. Node *node = nullptr;
  85. Node2D *node_2d = nullptr;
  86. #ifndef _3D_DISABLED
  87. Node3D *node_3d = nullptr;
  88. Skeleton3D *skeleton = nullptr;
  89. MeshInstance3D *node_blend_shape = nullptr;
  90. int blend_shape_idx = -1;
  91. #endif // _3D_DISABLED
  92. int bone_idx = -1;
  93. // accumulated transforms
  94. bool loc_used = false;
  95. bool rot_used = false;
  96. bool scale_used = false;
  97. Vector3 loc_accum;
  98. Quaternion rot_accum;
  99. Vector3 scale_accum;
  100. float blend_shape_accum = 0;
  101. uint64_t accum_pass = 0;
  102. bool audio_playing = false;
  103. float audio_start = 0.0;
  104. float audio_len = 0.0;
  105. bool animation_playing = false;
  106. struct PropertyAnim {
  107. TrackNodeCache *owner = nullptr;
  108. SpecialProperty special = SP_NONE; //small optimization
  109. Vector<StringName> subpath;
  110. Object *object = nullptr;
  111. Variant value_accum;
  112. uint64_t accum_pass = 0;
  113. Variant capture;
  114. };
  115. Map<StringName, PropertyAnim> property_anim;
  116. struct BezierAnim {
  117. Vector<StringName> bezier_property;
  118. TrackNodeCache *owner = nullptr;
  119. float bezier_accum = 0.0;
  120. Object *object = nullptr;
  121. uint64_t accum_pass = 0;
  122. };
  123. Map<StringName, BezierAnim> bezier_anim;
  124. uint32_t last_setup_pass = 0;
  125. TrackNodeCache() {}
  126. };
  127. struct TrackNodeCacheKey {
  128. ObjectID id;
  129. int bone_idx = -1;
  130. int blend_shape_idx = -1;
  131. inline bool operator<(const TrackNodeCacheKey &p_right) const {
  132. if (id == p_right.id) {
  133. if (blend_shape_idx == p_right.blend_shape_idx) {
  134. return bone_idx < p_right.bone_idx;
  135. } else {
  136. return blend_shape_idx < p_right.blend_shape_idx;
  137. }
  138. } else {
  139. return id < p_right.id;
  140. }
  141. }
  142. };
  143. Map<TrackNodeCacheKey, TrackNodeCache> node_cache_map;
  144. TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
  145. int cache_update_size = 0;
  146. TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
  147. int cache_update_prop_size = 0;
  148. TrackNodeCache::BezierAnim *cache_update_bezier[NODE_CACHE_UPDATE_MAX];
  149. int cache_update_bezier_size = 0;
  150. Set<TrackNodeCache *> playing_caches;
  151. uint64_t accum_pass = 1;
  152. float speed_scale = 1.0;
  153. float default_blend_time = 0.0;
  154. struct AnimationData {
  155. String name;
  156. StringName next;
  157. Vector<TrackNodeCache *> node_cache;
  158. Ref<Animation> animation;
  159. };
  160. Map<StringName, AnimationData> animation_set;
  161. struct BlendKey {
  162. StringName from;
  163. StringName to;
  164. bool operator<(const BlendKey &bk) const { return from == bk.from ? String(to) < String(bk.to) : String(from) < String(bk.from); }
  165. };
  166. Map<BlendKey, float> blend_times;
  167. struct PlaybackData {
  168. AnimationData *from = nullptr;
  169. float pos = 0.0;
  170. float speed_scale = 1.0;
  171. };
  172. struct Blend {
  173. PlaybackData data;
  174. float blend_time = 0.0;
  175. float blend_left = 0.0;
  176. };
  177. struct Playback {
  178. List<Blend> blend;
  179. PlaybackData current;
  180. StringName assigned;
  181. bool seeked = false;
  182. bool started = false;
  183. } playback;
  184. List<StringName> queued;
  185. bool end_reached = false;
  186. bool end_notify = false;
  187. String autoplay;
  188. bool reset_on_save = true;
  189. AnimationProcessCallback process_callback = ANIMATION_PROCESS_IDLE;
  190. AnimationMethodCallMode method_call_mode = ANIMATION_METHOD_CALL_DEFERRED;
  191. bool processing = false;
  192. bool active = true;
  193. NodePath root;
  194. void _animation_process_animation(AnimationData *p_anim, double p_time, double p_delta, float p_interp, bool p_is_current = true, bool p_seeked = false, bool p_started = false);
  195. void _ensure_node_caches(AnimationData *p_anim, Node *p_root_override = nullptr);
  196. void _animation_process_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started);
  197. void _animation_process2(double p_delta, bool p_started);
  198. void _animation_update_transforms();
  199. void _animation_process(double p_delta);
  200. void _node_removed(Node *p_node);
  201. void _stop_playing_caches();
  202. // bind helpers
  203. Vector<String> _get_animation_list() const {
  204. List<StringName> animations;
  205. get_animation_list(&animations);
  206. Vector<String> ret;
  207. while (animations.size()) {
  208. ret.push_back(animations.front()->get());
  209. animations.pop_front();
  210. }
  211. return ret;
  212. }
  213. void _animation_changed();
  214. void _ref_anim(const Ref<Animation> &p_anim);
  215. void _unref_anim(const Ref<Animation> &p_anim);
  216. void _set_process(bool p_process, bool p_force = false);
  217. bool playing = false;
  218. protected:
  219. bool _set(const StringName &p_name, const Variant &p_value);
  220. bool _get(const StringName &p_name, Variant &r_ret) const;
  221. virtual void _validate_property(PropertyInfo &property) const override;
  222. void _get_property_list(List<PropertyInfo> *p_list) const;
  223. void _notification(int p_what);
  224. static void _bind_methods();
  225. public:
  226. StringName find_animation(const Ref<Animation> &p_animation) const;
  227. Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation);
  228. void remove_animation(const StringName &p_name);
  229. void rename_animation(const StringName &p_name, const StringName &p_new_name);
  230. bool has_animation(const StringName &p_name) const;
  231. Ref<Animation> get_animation(const StringName &p_name) const;
  232. void get_animation_list(List<StringName> *p_animations) const;
  233. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time);
  234. float get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  235. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  236. StringName animation_get_next(const StringName &p_animation) const;
  237. void set_default_blend_time(float p_default);
  238. float get_default_blend_time() const;
  239. void play(const StringName &p_name = StringName(), float p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  240. void play_backwards(const StringName &p_name = StringName(), float p_custom_blend = -1);
  241. void queue(const StringName &p_name);
  242. Vector<String> get_queue();
  243. void clear_queue();
  244. void stop(bool p_reset = true);
  245. bool is_playing() const;
  246. String get_current_animation() const;
  247. void set_current_animation(const String &p_anim);
  248. String get_assigned_animation() const;
  249. void set_assigned_animation(const String &p_anim);
  250. void set_active(bool p_active);
  251. bool is_active() const;
  252. bool is_valid() const;
  253. void set_speed_scale(float p_speed);
  254. float get_speed_scale() const;
  255. float get_playing_speed() const;
  256. void set_autoplay(const String &p_name);
  257. String get_autoplay() const;
  258. void set_reset_on_save_enabled(bool p_enabled);
  259. bool is_reset_on_save_enabled() const;
  260. void set_process_callback(AnimationProcessCallback p_mode);
  261. AnimationProcessCallback get_process_callback() const;
  262. void set_method_call_mode(AnimationMethodCallMode p_mode);
  263. AnimationMethodCallMode get_method_call_mode() const;
  264. void seek(double p_time, bool p_update = false);
  265. void seek_delta(double p_time, float p_delta);
  266. float get_current_animation_position() const;
  267. float get_current_animation_length() const;
  268. void advance(float p_time);
  269. void set_root(const NodePath &p_root);
  270. NodePath get_root() const;
  271. void clear_caches(); ///< must be called by hand if an animation was modified after added
  272. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  273. #ifdef TOOLS_ENABLED
  274. Ref<AnimatedValuesBackup> backup_animated_values(Node *p_root_override = nullptr);
  275. Ref<AnimatedValuesBackup> apply_reset(bool p_user_initiated = false);
  276. bool can_apply_reset() const;
  277. #endif
  278. AnimationPlayer();
  279. ~AnimationPlayer();
  280. };
  281. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessCallback);
  282. VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode);
  283. #endif