animation_mixer.h 17 KB

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