animation_player.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**************************************************************************/
  2. /* animation_player.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_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. #include "scene/resources/animation_library.h"
  38. #include "scene/resources/audio_stream_polyphonic.h"
  39. #ifdef TOOLS_ENABLED
  40. class AnimatedValuesBackup : public RefCounted {
  41. GDCLASS(AnimatedValuesBackup, RefCounted);
  42. struct Entry {
  43. Object *object = nullptr;
  44. Vector<StringName> subpath; // Unused if bone
  45. int bone_idx = -1; // -1 if not a bone
  46. Variant value;
  47. };
  48. Vector<Entry> entries;
  49. friend class AnimationPlayer;
  50. protected:
  51. static void _bind_methods();
  52. public:
  53. void update_skeletons();
  54. void restore() const;
  55. };
  56. #endif
  57. class AnimationPlayer : public Node {
  58. GDCLASS(AnimationPlayer, Node);
  59. public:
  60. enum AnimationProcessCallback {
  61. ANIMATION_PROCESS_PHYSICS,
  62. ANIMATION_PROCESS_IDLE,
  63. ANIMATION_PROCESS_MANUAL,
  64. };
  65. enum AnimationMethodCallMode {
  66. ANIMATION_METHOD_CALL_DEFERRED,
  67. ANIMATION_METHOD_CALL_IMMEDIATE,
  68. };
  69. private:
  70. enum {
  71. NODE_CACHE_UPDATE_MAX = 1024,
  72. BLEND_FROM_MAX = 3
  73. };
  74. enum SpecialProperty {
  75. SP_NONE,
  76. SP_NODE2D_POS,
  77. SP_NODE2D_ROT,
  78. SP_NODE2D_SCALE,
  79. };
  80. uint32_t setup_pass = 1;
  81. struct TrackNodeCache {
  82. NodePath path;
  83. uint32_t id = 0;
  84. Ref<Resource> resource;
  85. Node *node = nullptr;
  86. Node2D *node_2d = nullptr;
  87. #ifndef _3D_DISABLED
  88. Node3D *node_3d = nullptr;
  89. Skeleton3D *skeleton = nullptr;
  90. MeshInstance3D *node_blend_shape = nullptr;
  91. int blend_shape_idx = -1;
  92. #endif // _3D_DISABLED
  93. int bone_idx = -1;
  94. // accumulated transforms
  95. bool loc_used = false;
  96. bool rot_used = false;
  97. bool scale_used = false;
  98. Vector3 init_loc = Vector3(0, 0, 0);
  99. Quaternion init_rot = Quaternion(0, 0, 0, 1);
  100. Vector3 init_scale = Vector3(1, 1, 1);
  101. Vector3 loc_accum;
  102. Quaternion rot_accum;
  103. Vector3 scale_accum;
  104. float blend_shape_accum = 0;
  105. uint64_t accum_pass = 0;
  106. bool audio_playing = false;
  107. float audio_start = 0.0;
  108. float audio_len = 0.0;
  109. bool animation_playing = false;
  110. struct PropertyAnim {
  111. TrackNodeCache *owner = nullptr;
  112. SpecialProperty special = SP_NONE; //small optimization
  113. Vector<StringName> subpath;
  114. Object *object = nullptr;
  115. Variant value_accum;
  116. uint64_t accum_pass = 0;
  117. Variant capture;
  118. };
  119. HashMap<StringName, PropertyAnim> property_anim;
  120. struct BezierAnim {
  121. Vector<StringName> bezier_property;
  122. TrackNodeCache *owner = nullptr;
  123. float bezier_accum = 0.0;
  124. Object *object = nullptr;
  125. uint64_t accum_pass = 0;
  126. };
  127. HashMap<StringName, BezierAnim> bezier_anim;
  128. struct PlayingAudioStreamInfo {
  129. AudioStreamPlaybackPolyphonic::ID index = -1;
  130. double start = 0.0;
  131. double len = 0.0;
  132. };
  133. struct AudioAnim {
  134. Ref<AudioStreamPolyphonic> audio_stream;
  135. Ref<AudioStreamPlaybackPolyphonic> audio_stream_playback;
  136. HashMap<int, PlayingAudioStreamInfo> playing_streams;
  137. Object *object = nullptr;
  138. uint64_t accum_pass = 0;
  139. double length = 0.0;
  140. double time = 0.0;
  141. bool loop = false;
  142. bool backward = false;
  143. };
  144. HashMap<StringName, AudioAnim> audio_anim;
  145. uint32_t last_setup_pass = 0;
  146. TrackNodeCache() {}
  147. };
  148. struct TrackNodeCacheKey {
  149. ObjectID id;
  150. int bone_idx = -1;
  151. int blend_shape_idx = -1;
  152. static uint32_t hash(const TrackNodeCacheKey &p_key) {
  153. uint32_t h = hash_one_uint64(p_key.id);
  154. h = hash_murmur3_one_32(p_key.bone_idx, h);
  155. return hash_fmix32(hash_murmur3_one_32(p_key.blend_shape_idx, h));
  156. }
  157. inline bool operator==(const TrackNodeCacheKey &p_right) const {
  158. return id == p_right.id && bone_idx == p_right.bone_idx && blend_shape_idx == p_right.blend_shape_idx;
  159. }
  160. inline bool operator<(const TrackNodeCacheKey &p_right) const {
  161. if (id == p_right.id) {
  162. if (blend_shape_idx == p_right.blend_shape_idx) {
  163. return bone_idx < p_right.bone_idx;
  164. } else {
  165. return blend_shape_idx < p_right.blend_shape_idx;
  166. }
  167. } else {
  168. return id < p_right.id;
  169. }
  170. }
  171. };
  172. HashMap<TrackNodeCacheKey, TrackNodeCache, TrackNodeCacheKey> node_cache_map;
  173. TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
  174. int cache_update_size = 0;
  175. TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
  176. int cache_update_prop_size = 0;
  177. TrackNodeCache::BezierAnim *cache_update_bezier[NODE_CACHE_UPDATE_MAX];
  178. int cache_update_bezier_size = 0;
  179. TrackNodeCache::AudioAnim *cache_update_audio[NODE_CACHE_UPDATE_MAX];
  180. int cache_update_audio_size = 0;
  181. HashSet<TrackNodeCache *> playing_caches;
  182. Vector<Node *> playing_audio_stream_players;
  183. uint64_t accum_pass = 1;
  184. float speed_scale = 1.0;
  185. double default_blend_time = 0.0;
  186. bool is_stopping = false;
  187. struct AnimationData {
  188. String name;
  189. StringName next;
  190. Vector<TrackNodeCache *> node_cache;
  191. Ref<Animation> animation;
  192. StringName animation_library;
  193. uint64_t last_update = 0;
  194. };
  195. HashMap<StringName, AnimationData> animation_set;
  196. struct AnimationLibraryData {
  197. StringName name;
  198. Ref<AnimationLibrary> library;
  199. bool operator<(const AnimationLibraryData &p_data) const { return name.operator String() < p_data.name.operator String(); }
  200. };
  201. LocalVector<AnimationLibraryData> animation_libraries;
  202. struct BlendKey {
  203. StringName from;
  204. StringName to;
  205. static uint32_t hash(const BlendKey &p_key) {
  206. return hash_one_uint64((uint64_t(p_key.from.hash()) << 32) | uint32_t(p_key.to.hash()));
  207. }
  208. bool operator==(const BlendKey &bk) const {
  209. return from == bk.from && to == bk.to;
  210. }
  211. bool operator<(const BlendKey &bk) const {
  212. if (from == bk.from) {
  213. return to < bk.to;
  214. } else {
  215. return from < bk.from;
  216. }
  217. }
  218. };
  219. HashMap<BlendKey, double, BlendKey> blend_times;
  220. struct PlaybackData {
  221. AnimationData *from = nullptr;
  222. double pos = 0.0;
  223. float speed_scale = 1.0;
  224. };
  225. struct Blend {
  226. PlaybackData data;
  227. double blend_time = 0.0;
  228. double blend_left = 0.0;
  229. };
  230. struct Playback {
  231. List<Blend> blend;
  232. PlaybackData current;
  233. StringName assigned;
  234. bool seeked = false;
  235. bool started = false;
  236. } playback;
  237. List<StringName> queued;
  238. bool end_reached = false;
  239. bool end_notify = false;
  240. String autoplay;
  241. bool reset_on_save = true;
  242. AnimationProcessCallback process_callback = ANIMATION_PROCESS_IDLE;
  243. AnimationMethodCallMode method_call_mode = ANIMATION_METHOD_CALL_DEFERRED;
  244. int audio_max_polyphony = 32;
  245. bool movie_quit_on_finish = false;
  246. bool processing = false;
  247. bool active = true;
  248. NodePath root;
  249. void _animation_process_animation(AnimationData *p_anim, double p_prev_time, double p_time, double p_delta, float p_interp, bool p_is_current = true, bool p_seeked = false, bool p_started = false, Animation::LoopedFlag p_looped_flag = Animation::LOOPED_FLAG_NONE);
  250. void _ensure_node_caches(AnimationData *p_anim, Node *p_root_override = nullptr);
  251. void _animation_process_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started);
  252. void _animation_process2(double p_delta, bool p_started);
  253. void _animation_update_transforms();
  254. void _animation_process(double p_delta);
  255. void _node_removed(Node *p_node);
  256. void _clear_audio_streams();
  257. void _stop_playing_caches(bool p_reset);
  258. // bind helpers
  259. Vector<String> _get_animation_list() const {
  260. List<StringName> animations;
  261. get_animation_list(&animations);
  262. Vector<String> ret;
  263. while (animations.size()) {
  264. ret.push_back(animations.front()->get());
  265. animations.pop_front();
  266. }
  267. return ret;
  268. }
  269. void _animation_changed(const StringName &p_name);
  270. void _set_process(bool p_process, bool p_force = false);
  271. void _stop_internal(bool p_reset, bool p_keep_state);
  272. bool playing = false;
  273. uint64_t animation_set_update_pass = 1;
  274. void _animation_set_cache_update();
  275. void _animation_added(const StringName &p_name, const StringName &p_library);
  276. void _animation_removed(const StringName &p_name, const StringName &p_library);
  277. void _animation_renamed(const StringName &p_name, const StringName &p_to_name, const StringName &p_library);
  278. void _rename_animation(const StringName &p_from_name, const StringName &p_to_name);
  279. TypedArray<StringName> _get_animation_library_list() const;
  280. protected:
  281. bool _set(const StringName &p_name, const Variant &p_value);
  282. bool _get(const StringName &p_name, Variant &r_ret) const;
  283. void _validate_property(PropertyInfo &p_property) const;
  284. void _get_property_list(List<PropertyInfo> *p_list) const;
  285. void _notification(int p_what);
  286. static void _bind_methods();
  287. GDVIRTUAL5RC(Variant, _post_process_key_value, Ref<Animation>, int, Variant, Object *, int);
  288. Variant post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx = -1);
  289. virtual Variant _post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx = -1);
  290. public:
  291. StringName find_animation(const Ref<Animation> &p_animation) const;
  292. StringName find_animation_library(const Ref<Animation> &p_animation) const;
  293. Error add_animation_library(const StringName &p_name, const Ref<AnimationLibrary> &p_animation_library);
  294. void remove_animation_library(const StringName &p_name);
  295. void rename_animation_library(const StringName &p_name, const StringName &p_new_name);
  296. Ref<AnimationLibrary> get_animation_library(const StringName &p_name) const;
  297. void get_animation_library_list(List<StringName> *p_animations) const;
  298. bool has_animation_library(const StringName &p_name) const;
  299. Ref<Animation> get_animation(const StringName &p_name) const;
  300. void get_animation_list(List<StringName> *p_animations) const;
  301. bool has_animation(const StringName &p_name) const;
  302. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time);
  303. double get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  304. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  305. StringName animation_get_next(const StringName &p_animation) const;
  306. void set_default_blend_time(double p_default);
  307. double get_default_blend_time() const;
  308. void play(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  309. void play_backwards(const StringName &p_name = StringName(), double p_custom_blend = -1);
  310. void queue(const StringName &p_name);
  311. Vector<String> get_queue();
  312. void clear_queue();
  313. void pause();
  314. void stop(bool p_keep_state = false);
  315. bool is_playing() const;
  316. String get_current_animation() const;
  317. void set_current_animation(const String &p_anim);
  318. String get_assigned_animation() const;
  319. void set_assigned_animation(const String &p_anim);
  320. void set_active(bool p_active);
  321. bool is_active() const;
  322. bool is_valid() const;
  323. void set_speed_scale(float p_speed);
  324. float get_speed_scale() const;
  325. float get_playing_speed() const;
  326. void set_autoplay(const String &p_name);
  327. String get_autoplay() const;
  328. void set_reset_on_save_enabled(bool p_enabled);
  329. bool is_reset_on_save_enabled() const;
  330. void set_process_callback(AnimationProcessCallback p_mode);
  331. AnimationProcessCallback get_process_callback() const;
  332. void set_method_call_mode(AnimationMethodCallMode p_mode);
  333. AnimationMethodCallMode get_method_call_mode() const;
  334. void set_audio_max_polyphony(int p_audio_max_polyphony);
  335. int get_audio_max_polyphony() const;
  336. void set_movie_quit_on_finish_enabled(bool p_enabled);
  337. bool is_movie_quit_on_finish_enabled() const;
  338. void seek(double p_time, bool p_update = false);
  339. void seek_delta(double p_time, double p_delta);
  340. double get_current_animation_position() const;
  341. double get_current_animation_length() const;
  342. void advance(double p_time);
  343. void set_root(const NodePath &p_root);
  344. NodePath get_root() const;
  345. void clear_caches(); ///< must be called by hand if an animation was modified after added
  346. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  347. #ifdef TOOLS_ENABLED
  348. Ref<AnimatedValuesBackup> backup_animated_values(Node *p_root_override = nullptr);
  349. Ref<AnimatedValuesBackup> apply_reset(bool p_user_initiated = false);
  350. bool can_apply_reset() const;
  351. #endif
  352. AnimationPlayer();
  353. ~AnimationPlayer();
  354. };
  355. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessCallback);
  356. VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode);
  357. #endif // ANIMATION_PLAYER_H