animation_mixer.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**************************************************************************/
  2. /* animation_mixer.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. #pragma once
  31. #include "core/templates/a_hash_map.h"
  32. #include "scene/animation/tween.h"
  33. #include "scene/main/node.h"
  34. #include "scene/resources/animation.h"
  35. #include "scene/resources/animation_library.h"
  36. #include "scene/resources/audio_stream_polyphonic.h"
  37. class AnimatedValuesBackup;
  38. class AnimationMixer : public Node {
  39. GDCLASS(AnimationMixer, Node);
  40. friend AnimatedValuesBackup;
  41. #ifdef TOOLS_ENABLED
  42. bool editing = false;
  43. bool dummy = false;
  44. #endif // TOOLS_ENABLED
  45. bool reset_on_save = true;
  46. bool is_GDVIRTUAL_CALL_post_process_key_value = true;
  47. public:
  48. enum AnimationCallbackModeProcess {
  49. ANIMATION_CALLBACK_MODE_PROCESS_PHYSICS,
  50. ANIMATION_CALLBACK_MODE_PROCESS_IDLE,
  51. ANIMATION_CALLBACK_MODE_PROCESS_MANUAL,
  52. };
  53. enum AnimationCallbackModeMethod {
  54. ANIMATION_CALLBACK_MODE_METHOD_DEFERRED,
  55. ANIMATION_CALLBACK_MODE_METHOD_IMMEDIATE,
  56. };
  57. enum AnimationCallbackModeDiscrete {
  58. ANIMATION_CALLBACK_MODE_DISCRETE_DOMINANT,
  59. ANIMATION_CALLBACK_MODE_DISCRETE_RECESSIVE,
  60. ANIMATION_CALLBACK_MODE_DISCRETE_FORCE_CONTINUOUS,
  61. };
  62. /* ---- Data ---- */
  63. struct AnimationLibraryData {
  64. StringName name;
  65. Ref<AnimationLibrary> library;
  66. bool operator<(const AnimationLibraryData &p_data) const { return name.operator String() < p_data.name.operator String(); }
  67. };
  68. struct AnimationData {
  69. String name;
  70. Ref<Animation> animation;
  71. StringName animation_library;
  72. uint64_t last_update = 0;
  73. };
  74. struct PlaybackInfo {
  75. double time = 0.0;
  76. double delta = 0.0;
  77. double start = 0.0;
  78. double end = 0.0;
  79. bool seeked = false;
  80. bool is_external_seeking = false;
  81. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  82. real_t weight = 0.0;
  83. Vector<real_t> track_weights;
  84. };
  85. struct AnimationInstance {
  86. AnimationData animation_data;
  87. PlaybackInfo playback_info;
  88. };
  89. protected:
  90. /* ---- Data lists ---- */
  91. LocalVector<AnimationLibraryData> animation_libraries;
  92. AHashMap<StringName, AnimationData> animation_set; // HashMap<Library name + Animation name, AnimationData>
  93. TypedArray<StringName> _get_animation_library_list() const;
  94. Vector<String> _get_animation_list() const {
  95. List<StringName> animations;
  96. get_animation_list(&animations);
  97. Vector<String> ret;
  98. while (animations.size()) {
  99. ret.push_back(animations.front()->get());
  100. animations.pop_front();
  101. }
  102. return ret;
  103. }
  104. // For caches.
  105. uint64_t animation_set_update_pass = 1;
  106. void _animation_set_cache_update();
  107. // Signals.
  108. virtual void _animation_added(const StringName &p_name, const StringName &p_library);
  109. virtual void _animation_removed(const StringName &p_name, const StringName &p_library);
  110. virtual void _animation_renamed(const StringName &p_name, const StringName &p_to_name, const StringName &p_library);
  111. virtual void _animation_changed(const StringName &p_name);
  112. /* ---- General settings for animation ---- */
  113. AnimationCallbackModeProcess callback_mode_process = ANIMATION_CALLBACK_MODE_PROCESS_IDLE;
  114. AnimationCallbackModeMethod callback_mode_method = ANIMATION_CALLBACK_MODE_METHOD_DEFERRED;
  115. AnimationCallbackModeDiscrete callback_mode_discrete = ANIMATION_CALLBACK_MODE_DISCRETE_RECESSIVE;
  116. int audio_max_polyphony = 32;
  117. NodePath root_node;
  118. bool processing = false;
  119. bool active = true;
  120. void _set_process(bool p_process, bool p_force = false);
  121. /* ---- Caches for blending ---- */
  122. bool cache_valid = false;
  123. uint64_t setup_pass = 1;
  124. uint64_t process_pass = 1;
  125. struct TrackCache {
  126. bool root_motion = false;
  127. uint64_t setup_pass = 0;
  128. Animation::TrackType type = Animation::TrackType::TYPE_ANIMATION;
  129. NodePath path;
  130. int blend_idx = -1;
  131. ObjectID object_id;
  132. real_t total_weight = 0.0;
  133. TrackCache() = default;
  134. TrackCache(const TrackCache &p_other) :
  135. root_motion(p_other.root_motion),
  136. setup_pass(p_other.setup_pass),
  137. type(p_other.type),
  138. object_id(p_other.object_id),
  139. total_weight(p_other.total_weight) {}
  140. virtual ~TrackCache() {}
  141. };
  142. struct TrackCacheTransform : public TrackCache {
  143. #ifndef _3D_DISABLED
  144. ObjectID skeleton_id;
  145. #endif // _3D_DISABLED
  146. int bone_idx = -1;
  147. bool loc_used = false;
  148. bool rot_used = false;
  149. bool scale_used = false;
  150. Vector3 init_loc = Vector3(0, 0, 0);
  151. Quaternion init_rot = Quaternion(0, 0, 0, 1);
  152. Vector3 init_scale = Vector3(1, 1, 1);
  153. Vector3 loc;
  154. Quaternion rot;
  155. Vector3 scale;
  156. TrackCacheTransform(const TrackCacheTransform &p_other) :
  157. TrackCache(p_other),
  158. #ifndef _3D_DISABLED
  159. skeleton_id(p_other.skeleton_id),
  160. #endif
  161. bone_idx(p_other.bone_idx),
  162. loc_used(p_other.loc_used),
  163. rot_used(p_other.rot_used),
  164. scale_used(p_other.scale_used),
  165. init_loc(p_other.init_loc),
  166. init_rot(p_other.init_rot),
  167. init_scale(p_other.init_scale),
  168. loc(p_other.loc),
  169. rot(p_other.rot),
  170. scale(p_other.scale) {
  171. }
  172. TrackCacheTransform() {
  173. type = Animation::TYPE_POSITION_3D;
  174. }
  175. };
  176. struct RootMotionCache {
  177. Vector3 loc = Vector3(0, 0, 0);
  178. Quaternion rot = Quaternion(0, 0, 0, 1);
  179. Vector3 scale = Vector3(1, 1, 1);
  180. };
  181. struct TrackCacheBlendShape : public TrackCache {
  182. float init_value = 0;
  183. float value = 0;
  184. int shape_index = -1;
  185. TrackCacheBlendShape(const TrackCacheBlendShape &p_other) :
  186. TrackCache(p_other),
  187. init_value(p_other.init_value),
  188. value(p_other.value),
  189. shape_index(p_other.shape_index) {}
  190. TrackCacheBlendShape() { type = Animation::TYPE_BLEND_SHAPE; }
  191. };
  192. struct TrackCacheValue : public TrackCache {
  193. Variant init_value;
  194. Variant value;
  195. Vector<StringName> subpath;
  196. // TODO: There are many boolean, can be packed into one integer.
  197. bool is_init = false;
  198. bool use_continuous = false;
  199. bool use_discrete = false;
  200. bool is_using_angle = false;
  201. bool is_variant_interpolatable = true;
  202. Variant element_size;
  203. TrackCacheValue(const TrackCacheValue &p_other) :
  204. TrackCache(p_other),
  205. init_value(p_other.init_value),
  206. value(p_other.value),
  207. subpath(p_other.subpath),
  208. is_init(p_other.is_init),
  209. use_continuous(p_other.use_continuous),
  210. use_discrete(p_other.use_discrete),
  211. is_using_angle(p_other.is_using_angle),
  212. is_variant_interpolatable(p_other.is_variant_interpolatable),
  213. element_size(p_other.element_size) {}
  214. TrackCacheValue() { type = Animation::TYPE_VALUE; }
  215. ~TrackCacheValue() {
  216. // Clear ref to avoid leaking.
  217. init_value = Variant();
  218. value = Variant();
  219. }
  220. };
  221. struct TrackCacheMethod : public TrackCache {
  222. TrackCacheMethod() { type = Animation::TYPE_METHOD; }
  223. };
  224. // Audio stream information for each audio stream placed on the track.
  225. struct PlayingAudioStreamInfo {
  226. AudioStreamPlaybackPolyphonic::ID index = -1; // ID retrieved from AudioStreamPlaybackPolyphonic.
  227. double start = 0.0;
  228. double len = 0.0;
  229. };
  230. // Audio track information for mixng and ending.
  231. struct PlayingAudioTrackInfo {
  232. AHashMap<int, PlayingAudioStreamInfo> stream_info;
  233. double length = 0.0;
  234. double time = 0.0;
  235. real_t volume = 0.0;
  236. bool loop = false;
  237. bool backward = false;
  238. bool use_blend = false;
  239. };
  240. struct TrackCacheAudio : public TrackCache {
  241. Ref<AudioStreamPolyphonic> audio_stream;
  242. Ref<AudioStreamPlaybackPolyphonic> audio_stream_playback;
  243. HashMap<ObjectID, PlayingAudioTrackInfo> playing_streams; // Key is Animation resource ObjectID.
  244. AudioServer::PlaybackType playback_type;
  245. StringName bus;
  246. TrackCacheAudio(const TrackCacheAudio &p_other) :
  247. TrackCache(p_other),
  248. audio_stream(p_other.audio_stream),
  249. audio_stream_playback(p_other.audio_stream_playback),
  250. playing_streams(p_other.playing_streams),
  251. playback_type(p_other.playback_type) {}
  252. TrackCacheAudio() {
  253. type = Animation::TYPE_AUDIO;
  254. }
  255. };
  256. struct TrackCacheAnimation : public TrackCache {
  257. bool playing = false;
  258. TrackCacheAnimation() {
  259. type = Animation::TYPE_ANIMATION;
  260. }
  261. };
  262. RootMotionCache root_motion_cache;
  263. AHashMap<Animation::TypeHash, TrackCache *, HashHasher> track_cache;
  264. AHashMap<Ref<Animation>, LocalVector<TrackCache *>> animation_track_num_to_track_cache;
  265. HashSet<TrackCache *> playing_caches;
  266. Vector<Node *> playing_audio_stream_players;
  267. // Helpers.
  268. void _clear_caches();
  269. void _clear_audio_streams();
  270. void _clear_playing_caches();
  271. void _init_root_motion_cache();
  272. bool _update_caches();
  273. void _create_track_num_to_track_cache_for_animation(Ref<Animation> &p_animation);
  274. /* ---- Audio ---- */
  275. AudioServer::PlaybackType playback_type;
  276. /* ---- Blending processor ---- */
  277. LocalVector<AnimationInstance> animation_instances;
  278. AHashMap<NodePath, int> track_map;
  279. int track_count = 0;
  280. bool deterministic = false;
  281. /* ---- Root motion accumulator for Skeleton3D ---- */
  282. NodePath root_motion_track;
  283. bool root_motion_local = false;
  284. Vector3 root_motion_position = Vector3(0, 0, 0);
  285. Quaternion root_motion_rotation = Quaternion(0, 0, 0, 1);
  286. Vector3 root_motion_scale = Vector3(0, 0, 0);
  287. Vector3 root_motion_position_accumulator = Vector3(0, 0, 0);
  288. Quaternion root_motion_rotation_accumulator = Quaternion(0, 0, 0, 1);
  289. Vector3 root_motion_scale_accumulator = Vector3(1, 1, 1);
  290. bool _set(const StringName &p_name, const Variant &p_value);
  291. bool _get(const StringName &p_name, Variant &r_ret) const;
  292. void _get_property_list(List<PropertyInfo> *p_list) const;
  293. void _notification(int p_what);
  294. virtual void _validate_property(PropertyInfo &p_property) const;
  295. #ifdef TOOLS_ENABLED
  296. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  297. #endif
  298. static void _bind_methods();
  299. void _node_removed(Node *p_node);
  300. // Helper for extended class.
  301. virtual void _set_active(bool p_active);
  302. virtual void _remove_animation(const StringName &p_name);
  303. virtual void _rename_animation(const StringName &p_from_name, const StringName &p_to_name);
  304. /* ---- Blending processor ---- */
  305. virtual void _process_animation(double p_delta, bool p_update_only = false);
  306. // For post process with retrieved key value during blending.
  307. virtual Variant _post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant &p_value, ObjectID p_object_id, int p_object_sub_idx = -1);
  308. Variant post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, ObjectID p_object_id, int p_object_sub_idx = -1);
  309. GDVIRTUAL5RC(Variant, _post_process_key_value, Ref<Animation>, int, Variant, ObjectID, int);
  310. void _blend_init();
  311. virtual bool _blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map);
  312. virtual void _blend_capture(double p_delta);
  313. void _blend_calc_total_weight(); // For indeterministic blending.
  314. void _blend_process(double p_delta, bool p_update_only = false);
  315. void _blend_apply();
  316. virtual void _blend_post_process();
  317. void _call_object(ObjectID p_object_id, const StringName &p_method, const Vector<Variant> &p_params, bool p_deferred);
  318. /* ---- Capture feature ---- */
  319. struct CaptureCache {
  320. Ref<Animation> animation;
  321. double remain = 0.0;
  322. double step = 0.0;
  323. Tween::TransitionType trans_type = Tween::TRANS_LINEAR;
  324. Tween::EaseType ease_type = Tween::EASE_IN;
  325. void clear() {
  326. animation.unref();
  327. remain = 0.0;
  328. step = 0.0;
  329. }
  330. ~CaptureCache() {
  331. clear();
  332. }
  333. } capture_cache;
  334. void blend_capture(double p_delta); // To blend capture track with all other animations.
  335. #ifndef DISABLE_DEPRECATED
  336. virtual Variant _post_process_key_value_bind_compat_86687(const Ref<Animation> &p_anim, int p_track, Variant p_value, Object *p_object, int p_object_idx = -1);
  337. static void _bind_compatibility_methods();
  338. #endif // DISABLE_DEPRECATED
  339. public:
  340. /* ---- Data lists ---- */
  341. Dictionary *get_animation_libraries();
  342. void get_animation_library_list(List<StringName> *p_animations) const;
  343. Ref<AnimationLibrary> get_animation_library(const StringName &p_name) const;
  344. bool has_animation_library(const StringName &p_name) const;
  345. StringName get_animation_library_name(const Ref<AnimationLibrary> &p_animation_library) const;
  346. StringName find_animation_library(const Ref<Animation> &p_animation) const;
  347. Error add_animation_library(const StringName &p_name, const Ref<AnimationLibrary> &p_animation_library);
  348. void remove_animation_library(const StringName &p_name);
  349. void rename_animation_library(const StringName &p_name, const StringName &p_new_name);
  350. void get_animation_list(List<StringName> *p_animations) const;
  351. Ref<Animation> get_animation(const StringName &p_name) const;
  352. bool has_animation(const StringName &p_name) const;
  353. StringName find_animation(const Ref<Animation> &p_animation) const;
  354. /* ---- General settings for animation ---- */
  355. void set_active(bool p_active);
  356. bool is_active() const;
  357. void set_deterministic(bool p_deterministic);
  358. bool is_deterministic() const;
  359. void set_root_node(const NodePath &p_path);
  360. NodePath get_root_node() const;
  361. void set_callback_mode_process(AnimationCallbackModeProcess p_mode);
  362. AnimationCallbackModeProcess get_callback_mode_process() const;
  363. void set_callback_mode_method(AnimationCallbackModeMethod p_mode);
  364. AnimationCallbackModeMethod get_callback_mode_method() const;
  365. void set_callback_mode_discrete(AnimationCallbackModeDiscrete p_mode);
  366. AnimationCallbackModeDiscrete get_callback_mode_discrete() const;
  367. /* ---- Audio ---- */
  368. void set_audio_max_polyphony(int p_audio_max_polyphony);
  369. int get_audio_max_polyphony() const;
  370. /* ---- Root motion accumulator for Skeleton3D ---- */
  371. void set_root_motion_track(const NodePath &p_track);
  372. NodePath get_root_motion_track() const;
  373. void set_root_motion_local(bool p_enabled);
  374. bool is_root_motion_local() const;
  375. Vector3 get_root_motion_position() const;
  376. Quaternion get_root_motion_rotation() const;
  377. Vector3 get_root_motion_scale() const;
  378. Vector3 get_root_motion_position_accumulator() const;
  379. Quaternion get_root_motion_rotation_accumulator() const;
  380. Vector3 get_root_motion_scale_accumulator() const;
  381. /* ---- Blending processor ---- */
  382. void make_animation_instance(const StringName &p_name, const PlaybackInfo p_playback_info);
  383. void clear_animation_instances();
  384. virtual void advance(double p_time);
  385. virtual void clear_caches(); // Must be called by hand if an animation was modified after added.
  386. /* ---- Capture feature ---- */
  387. void capture(const StringName &p_name, double p_duration, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
  388. /* ---- Reset on save ---- */
  389. void set_reset_on_save_enabled(bool p_enabled);
  390. bool is_reset_on_save_enabled() const;
  391. bool can_apply_reset() const;
  392. void _build_backup_track_cache();
  393. Ref<AnimatedValuesBackup> make_backup();
  394. void restore(const Ref<AnimatedValuesBackup> &p_backup);
  395. void reset();
  396. #ifdef TOOLS_ENABLED
  397. Ref<AnimatedValuesBackup> apply_reset(bool p_user_initiated = false);
  398. void set_editing(bool p_editing);
  399. bool is_editing() const;
  400. void set_dummy(bool p_dummy);
  401. bool is_dummy() const;
  402. #endif // TOOLS_ENABLED
  403. AnimationMixer();
  404. ~AnimationMixer();
  405. };
  406. class AnimatedValuesBackup : public RefCounted {
  407. GDCLASS(AnimatedValuesBackup, RefCounted);
  408. AHashMap<Animation::TypeHash, AnimationMixer::TrackCache *, HashHasher> data;
  409. public:
  410. void set_data(const AHashMap<Animation::TypeHash, AnimationMixer::TrackCache *, HashHasher> p_data);
  411. AHashMap<Animation::TypeHash, AnimationMixer::TrackCache *, HashHasher> get_data() const;
  412. void clear_data();
  413. AnimationMixer::TrackCache *get_cache_copy(AnimationMixer::TrackCache *p_cache) const;
  414. ~AnimatedValuesBackup() { clear_data(); }
  415. };
  416. VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeProcess);
  417. VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeMethod);
  418. VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeDiscrete);