animation_tree.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**************************************************************************/
  2. /* animation_tree.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_TREE_H
  31. #define ANIMATION_TREE_H
  32. #include "animation_player.h"
  33. #include "scene/3d/node_3d.h"
  34. #include "scene/3d/skeleton_3d.h"
  35. #include "scene/resources/animation.h"
  36. #include "scene/resources/audio_stream_polyphonic.h"
  37. class AnimationNodeBlendTree;
  38. class AnimationNodeStartState;
  39. class AnimationNodeEndState;
  40. class AnimationPlayer;
  41. class AnimationTree;
  42. class AnimationNode : public Resource {
  43. GDCLASS(AnimationNode, Resource);
  44. public:
  45. enum FilterAction {
  46. FILTER_IGNORE,
  47. FILTER_PASS,
  48. FILTER_STOP,
  49. FILTER_BLEND
  50. };
  51. struct Input {
  52. String name;
  53. };
  54. Vector<Input> inputs;
  55. friend class AnimationTree;
  56. struct AnimationState {
  57. Ref<Animation> animation;
  58. double time = 0.0;
  59. double delta = 0.0;
  60. const Vector<real_t> *track_blends = nullptr;
  61. real_t blend = 0.0;
  62. bool seeked = false;
  63. bool is_external_seeking = false;
  64. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  65. };
  66. struct State {
  67. int track_count = 0;
  68. HashMap<NodePath, int> track_map;
  69. List<AnimationState> animation_states;
  70. bool valid = false;
  71. AnimationPlayer *player = nullptr;
  72. AnimationTree *tree = nullptr;
  73. String invalid_reasons;
  74. uint64_t last_pass = 0;
  75. };
  76. Vector<real_t> blends;
  77. State *state = nullptr;
  78. double _pre_process(const StringName &p_base_path, AnimationNode *p_parent, State *p_state, double p_time, bool p_seek, bool p_is_external_seeking, const Vector<StringName> &p_connections);
  79. //all this is temporary
  80. StringName base_path;
  81. Vector<StringName> connections;
  82. AnimationNode *parent = nullptr;
  83. HashMap<NodePath, bool> filter;
  84. bool filter_enabled = false;
  85. Array _get_filters() const;
  86. void _set_filters(const Array &p_filters);
  87. friend class AnimationNodeBlendTree;
  88. double _blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, real_t *r_max = nullptr);
  89. protected:
  90. void blend_animation(const StringName &p_animation, double p_time, double p_delta, bool p_seeked, bool p_is_external_seeking, real_t p_blend, Animation::LoopedFlag p_looped_flag = Animation::LOOPED_FLAG_NONE);
  91. double blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true);
  92. double blend_input(int p_input, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true);
  93. void make_invalid(const String &p_reason);
  94. AnimationTree *get_animation_tree() const;
  95. static void _bind_methods();
  96. void _validate_property(PropertyInfo &p_property) const;
  97. GDVIRTUAL0RC(Dictionary, _get_child_nodes)
  98. GDVIRTUAL0RC(Array, _get_parameter_list)
  99. GDVIRTUAL1RC(Ref<AnimationNode>, _get_child_by_name, StringName)
  100. GDVIRTUAL1RC(Variant, _get_parameter_default_value, StringName)
  101. GDVIRTUAL1RC(bool, _is_parameter_read_only, StringName)
  102. GDVIRTUAL3RC(double, _process, double, bool, bool)
  103. GDVIRTUAL0RC(String, _get_caption)
  104. GDVIRTUAL0RC(bool, _has_filter)
  105. public:
  106. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  107. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  108. virtual bool is_parameter_read_only(const StringName &p_parameter) const;
  109. void set_parameter(const StringName &p_name, const Variant &p_value);
  110. Variant get_parameter(const StringName &p_name) const;
  111. struct ChildNode {
  112. StringName name;
  113. Ref<AnimationNode> node;
  114. };
  115. virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
  116. virtual double process(double p_time, bool p_seek, bool p_is_external_seeking);
  117. virtual String get_caption() const;
  118. virtual bool add_input(const String &p_name);
  119. virtual void remove_input(int p_index);
  120. virtual bool set_input_name(int p_input, const String &p_name);
  121. virtual String get_input_name(int p_input) const;
  122. int get_input_count() const;
  123. int find_input(const String &p_name) const;
  124. void set_filter_path(const NodePath &p_path, bool p_enable);
  125. bool is_path_filtered(const NodePath &p_path) const;
  126. void set_filter_enabled(bool p_enable);
  127. bool is_filter_enabled() const;
  128. virtual bool has_filter() const;
  129. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
  130. AnimationNode();
  131. };
  132. VARIANT_ENUM_CAST(AnimationNode::FilterAction)
  133. //root node does not allow inputs
  134. class AnimationRootNode : public AnimationNode {
  135. GDCLASS(AnimationRootNode, AnimationNode);
  136. protected:
  137. virtual void _tree_changed();
  138. virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
  139. virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
  140. public:
  141. AnimationRootNode() {}
  142. };
  143. class AnimationNodeStartState : public AnimationRootNode {
  144. GDCLASS(AnimationNodeStartState, AnimationRootNode);
  145. };
  146. class AnimationNodeEndState : public AnimationRootNode {
  147. GDCLASS(AnimationNodeEndState, AnimationRootNode);
  148. };
  149. class AnimationTree : public Node {
  150. GDCLASS(AnimationTree, Node);
  151. public:
  152. enum AnimationProcessCallback {
  153. ANIMATION_PROCESS_PHYSICS,
  154. ANIMATION_PROCESS_IDLE,
  155. ANIMATION_PROCESS_MANUAL,
  156. };
  157. private:
  158. struct TrackCache {
  159. bool root_motion = false;
  160. uint64_t setup_pass = 0;
  161. Animation::TrackType type = Animation::TrackType::TYPE_ANIMATION;
  162. Object *object = nullptr;
  163. ObjectID object_id;
  164. TrackCache() {
  165. }
  166. virtual ~TrackCache() {}
  167. };
  168. struct TrackCacheTransform : public TrackCache {
  169. #ifndef _3D_DISABLED
  170. Node3D *node_3d = nullptr;
  171. Skeleton3D *skeleton = nullptr;
  172. #endif // _3D_DISABLED
  173. int bone_idx = -1;
  174. bool loc_used = false;
  175. bool rot_used = false;
  176. bool scale_used = false;
  177. Vector3 init_loc = Vector3(0, 0, 0);
  178. Quaternion init_rot = Quaternion(0, 0, 0, 1);
  179. Vector3 init_scale = Vector3(1, 1, 1);
  180. Vector3 loc;
  181. Quaternion rot;
  182. Vector3 scale;
  183. TrackCacheTransform() {
  184. type = Animation::TYPE_POSITION_3D;
  185. }
  186. };
  187. struct RootMotionCache {
  188. Vector3 loc = Vector3(0, 0, 0);
  189. Quaternion rot = Quaternion(0, 0, 0, 1);
  190. Vector3 scale = Vector3(1, 1, 1);
  191. };
  192. struct TrackCacheBlendShape : public TrackCache {
  193. MeshInstance3D *mesh_3d = nullptr;
  194. float init_value = 0;
  195. float value = 0;
  196. int shape_index = -1;
  197. TrackCacheBlendShape() { type = Animation::TYPE_BLEND_SHAPE; }
  198. };
  199. struct TrackCacheValue : public TrackCache {
  200. Variant init_value;
  201. Variant value;
  202. Vector<StringName> subpath;
  203. bool is_discrete = false;
  204. bool is_using_angle = false;
  205. TrackCacheValue() { type = Animation::TYPE_VALUE; }
  206. };
  207. struct TrackCacheMethod : public TrackCache {
  208. TrackCacheMethod() { type = Animation::TYPE_METHOD; }
  209. };
  210. struct TrackCacheBezier : public TrackCache {
  211. real_t init_value = 0.0;
  212. real_t value = 0.0;
  213. Vector<StringName> subpath;
  214. TrackCacheBezier() {
  215. type = Animation::TYPE_BEZIER;
  216. }
  217. };
  218. // Audio stream information for each audio stream placed on the track.
  219. struct PlayingAudioStreamInfo {
  220. AudioStreamPlaybackPolyphonic::ID index = -1; // ID retrieved from AudioStreamPlaybackPolyphonic.
  221. double start = 0.0;
  222. double len = 0.0;
  223. };
  224. // Audio track information for mixng and ending.
  225. struct PlayingAudioTrackInfo {
  226. HashMap<int, PlayingAudioStreamInfo> stream_info;
  227. double length = 0.0;
  228. double time = 0.0;
  229. real_t volume = 0.0;
  230. bool loop = false;
  231. bool backward = false;
  232. bool use_blend = false;
  233. };
  234. struct TrackCacheAudio : public TrackCache {
  235. Ref<AudioStreamPolyphonic> audio_stream;
  236. Ref<AudioStreamPlaybackPolyphonic> audio_stream_playback;
  237. HashMap<ObjectID, PlayingAudioTrackInfo> playing_streams; // Key is Animation resource ObjectID.
  238. TrackCacheAudio() {
  239. type = Animation::TYPE_AUDIO;
  240. }
  241. };
  242. struct TrackCacheAnimation : public TrackCache {
  243. bool playing = false;
  244. TrackCacheAnimation() {
  245. type = Animation::TYPE_ANIMATION;
  246. }
  247. };
  248. RootMotionCache root_motion_cache;
  249. HashMap<NodePath, TrackCache *> track_cache;
  250. HashSet<TrackCache *> playing_caches;
  251. Vector<Node *> playing_audio_stream_players;
  252. Ref<AnimationNode> root;
  253. NodePath advance_expression_base_node = NodePath(String("."));
  254. AnimationProcessCallback process_callback = ANIMATION_PROCESS_IDLE;
  255. bool active = false;
  256. NodePath animation_player;
  257. int audio_max_polyphony = 32;
  258. AnimationNode::State state;
  259. bool cache_valid = false;
  260. void _node_removed(Node *p_node);
  261. void _setup_animation_player();
  262. void _animation_player_changed();
  263. void _clear_caches();
  264. void _clear_playing_caches();
  265. void _clear_audio_streams();
  266. bool _update_caches(AnimationPlayer *player);
  267. void _process_graph(double p_delta);
  268. uint64_t setup_pass = 1;
  269. uint64_t process_pass = 1;
  270. bool started = true;
  271. NodePath root_motion_track;
  272. Vector3 root_motion_position = Vector3(0, 0, 0);
  273. Quaternion root_motion_rotation = Quaternion(0, 0, 0, 1);
  274. Vector3 root_motion_scale = Vector3(0, 0, 0);
  275. Vector3 root_motion_position_accumulator = Vector3(0, 0, 0);
  276. Quaternion root_motion_rotation_accumulator = Quaternion(0, 0, 0, 1);
  277. Vector3 root_motion_scale_accumulator = Vector3(1, 1, 1);
  278. friend class AnimationNode;
  279. bool properties_dirty = true;
  280. void _tree_changed();
  281. void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
  282. void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
  283. void _update_properties();
  284. List<PropertyInfo> properties;
  285. HashMap<StringName, HashMap<StringName, StringName>> property_parent_map;
  286. HashMap<ObjectID, StringName> property_reference_map;
  287. HashMap<StringName, Pair<Variant, bool>> property_map; // Property value and read-only flag.
  288. struct Activity {
  289. uint64_t last_pass = 0;
  290. real_t activity = 0.0;
  291. };
  292. HashMap<StringName, Vector<Activity>> input_activity_map;
  293. HashMap<StringName, Vector<Activity> *> input_activity_map_get;
  294. void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node);
  295. ObjectID last_animation_player;
  296. protected:
  297. bool _set(const StringName &p_name, const Variant &p_value);
  298. bool _get(const StringName &p_name, Variant &r_ret) const;
  299. void _get_property_list(List<PropertyInfo> *p_list) const;
  300. void _notification(int p_what);
  301. static void _bind_methods();
  302. GDVIRTUAL5RC(Variant, _post_process_key_value, Ref<Animation>, int, Variant, Object *, int);
  303. 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);
  304. 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);
  305. public:
  306. void set_tree_root(const Ref<AnimationNode> &p_root);
  307. Ref<AnimationNode> get_tree_root() const;
  308. void set_active(bool p_active);
  309. bool is_active() const;
  310. void set_process_callback(AnimationProcessCallback p_mode);
  311. AnimationProcessCallback get_process_callback() const;
  312. void set_animation_player(const NodePath &p_player);
  313. NodePath get_animation_player() const;
  314. void set_advance_expression_base_node(const NodePath &p_advance_expression_base_node);
  315. NodePath get_advance_expression_base_node() const;
  316. void set_audio_max_polyphony(int p_audio_max_polyphony);
  317. int get_audio_max_polyphony() const;
  318. PackedStringArray get_configuration_warnings() const override;
  319. bool is_state_invalid() const;
  320. String get_invalid_state_reason() const;
  321. void set_root_motion_track(const NodePath &p_track);
  322. NodePath get_root_motion_track() const;
  323. Vector3 get_root_motion_position() const;
  324. Quaternion get_root_motion_rotation() const;
  325. Vector3 get_root_motion_scale() const;
  326. Vector3 get_root_motion_position_accumulator() const;
  327. Quaternion get_root_motion_rotation_accumulator() const;
  328. Vector3 get_root_motion_scale_accumulator() const;
  329. real_t get_connection_activity(const StringName &p_path, int p_connection) const;
  330. void advance(double p_time);
  331. uint64_t get_last_process_pass() const;
  332. AnimationTree();
  333. ~AnimationTree();
  334. };
  335. VARIANT_ENUM_CAST(AnimationTree::AnimationProcessCallback)
  336. #endif // ANIMATION_TREE_H