animation_mixer.h 18 KB

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