animation_tree.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*************************************************************************/
  2. /* animation_tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_GRAPH_PLAYER_H
  31. #define ANIMATION_GRAPH_PLAYER_H
  32. #include "animation_player.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "scene/3d/spatial.h"
  35. #include "scene/resources/animation.h"
  36. class AnimationNodeBlendTree;
  37. class AnimationPlayer;
  38. class AnimationTree;
  39. class AnimationNode : public Resource {
  40. GDCLASS(AnimationNode, Resource);
  41. public:
  42. enum FilterAction {
  43. FILTER_IGNORE,
  44. FILTER_PASS,
  45. FILTER_STOP,
  46. FILTER_BLEND
  47. };
  48. struct Input {
  49. String name;
  50. };
  51. Vector<Input> inputs;
  52. float process_input(int p_input, float p_time, bool p_seek, float p_blend);
  53. friend class AnimationTree;
  54. struct AnimationState {
  55. Ref<Animation> animation;
  56. float time;
  57. float delta;
  58. const Vector<float> *track_blends;
  59. float blend;
  60. bool seeked;
  61. };
  62. struct State {
  63. int track_count;
  64. HashMap<NodePath, int> track_map;
  65. List<AnimationState> animation_states;
  66. bool valid;
  67. AnimationPlayer *player;
  68. AnimationTree *tree;
  69. String invalid_reasons;
  70. uint64_t last_pass;
  71. };
  72. Vector<float> blends;
  73. State *state;
  74. float _pre_process(const StringName &p_base_path, AnimationNode *p_parent, State *p_state, float p_time, bool p_seek, const Vector<StringName> &p_connections);
  75. void _pre_update_animations(HashMap<NodePath, int> *track_map);
  76. //all this is temporary
  77. StringName base_path;
  78. Vector<StringName> connections;
  79. AnimationNode *parent;
  80. HashMap<NodePath, bool> filter;
  81. bool filter_enabled;
  82. Array _get_filters() const;
  83. void _set_filters(const Array &p_filters);
  84. friend class AnimationNodeBlendTree;
  85. float _blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true, float *r_max = nullptr);
  86. protected:
  87. void blend_animation(const StringName &p_animation, float p_time, float p_delta, bool p_seeked, float p_blend);
  88. float blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true);
  89. float blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true);
  90. void make_invalid(const String &p_reason);
  91. static void _bind_methods();
  92. void _validate_property(PropertyInfo &property) const;
  93. void _set_parent(Object *p_parent);
  94. public:
  95. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  96. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  97. void set_parameter(const StringName &p_name, const Variant &p_value);
  98. Variant get_parameter(const StringName &p_name) const;
  99. struct ChildNode {
  100. StringName name;
  101. Ref<AnimationNode> node;
  102. };
  103. virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
  104. virtual float process(float p_time, bool p_seek);
  105. virtual String get_caption() const;
  106. int get_input_count() const;
  107. String get_input_name(int p_input);
  108. void add_input(const String &p_name);
  109. void set_input_name(int p_input, const String &p_name);
  110. void remove_input(int p_index);
  111. void set_filter_path(const NodePath &p_path, bool p_enable);
  112. bool is_path_filtered(const NodePath &p_path) const;
  113. void set_filter_enabled(bool p_enable);
  114. bool is_filter_enabled() const;
  115. virtual bool has_filter() const;
  116. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
  117. AnimationNode();
  118. };
  119. VARIANT_ENUM_CAST(AnimationNode::FilterAction)
  120. //root node does not allow inputs
  121. class AnimationRootNode : public AnimationNode {
  122. GDCLASS(AnimationRootNode, AnimationNode);
  123. public:
  124. AnimationRootNode() {}
  125. };
  126. class AnimationTree : public Node {
  127. GDCLASS(AnimationTree, Node);
  128. public:
  129. enum AnimationProcessMode {
  130. ANIMATION_PROCESS_PHYSICS,
  131. ANIMATION_PROCESS_IDLE,
  132. ANIMATION_PROCESS_MANUAL,
  133. };
  134. private:
  135. struct TrackCache {
  136. bool root_motion;
  137. uint64_t setup_pass;
  138. uint64_t process_pass;
  139. Animation::TrackType type;
  140. Object *object;
  141. ObjectID object_id;
  142. TrackCache() {
  143. root_motion = false;
  144. setup_pass = 0;
  145. process_pass = 0;
  146. object = nullptr;
  147. object_id = 0;
  148. }
  149. virtual ~TrackCache() {}
  150. };
  151. struct TrackCacheTransform : public TrackCache {
  152. Spatial *spatial;
  153. Skeleton *skeleton;
  154. int bone_idx;
  155. Vector3 loc;
  156. Quat rot;
  157. float rot_blend_accum;
  158. Vector3 scale;
  159. TrackCacheTransform() {
  160. type = Animation::TYPE_TRANSFORM;
  161. spatial = nullptr;
  162. bone_idx = -1;
  163. skeleton = nullptr;
  164. }
  165. };
  166. struct TrackCacheValue : public TrackCache {
  167. Variant value;
  168. Vector<StringName> subpath;
  169. TrackCacheValue() { type = Animation::TYPE_VALUE; }
  170. };
  171. struct TrackCacheMethod : public TrackCache {
  172. TrackCacheMethod() { type = Animation::TYPE_METHOD; }
  173. };
  174. struct TrackCacheBezier : public TrackCache {
  175. float value;
  176. Vector<StringName> subpath;
  177. TrackCacheBezier() {
  178. type = Animation::TYPE_BEZIER;
  179. value = 0;
  180. }
  181. };
  182. struct TrackCacheAudio : public TrackCache {
  183. bool playing;
  184. float start;
  185. float len;
  186. TrackCacheAudio() {
  187. type = Animation::TYPE_AUDIO;
  188. playing = false;
  189. start = 0;
  190. len = 0;
  191. }
  192. };
  193. struct TrackCacheAnimation : public TrackCache {
  194. bool playing;
  195. TrackCacheAnimation() {
  196. type = Animation::TYPE_ANIMATION;
  197. playing = false;
  198. }
  199. };
  200. HashMap<NodePath, TrackCache *> track_cache;
  201. Set<TrackCache *> playing_caches;
  202. Ref<AnimationNode> root;
  203. AnimationProcessMode process_mode;
  204. bool active;
  205. NodePath animation_player;
  206. AnimationNode::State state;
  207. bool cache_valid;
  208. void _node_removed(Node *p_node);
  209. void _caches_cleared();
  210. void _clear_caches();
  211. bool _update_caches(AnimationPlayer *player);
  212. void _process_graph(float p_delta);
  213. uint64_t setup_pass;
  214. uint64_t process_pass;
  215. bool started;
  216. NodePath root_motion_track;
  217. Transform root_motion_transform;
  218. friend class AnimationNode;
  219. bool properties_dirty;
  220. void _tree_changed();
  221. void _update_properties();
  222. List<PropertyInfo> properties;
  223. HashMap<StringName, HashMap<StringName, StringName>> property_parent_map;
  224. HashMap<StringName, Variant> property_map;
  225. struct Activity {
  226. uint64_t last_pass;
  227. float activity;
  228. };
  229. HashMap<StringName, Vector<Activity>> input_activity_map;
  230. HashMap<StringName, Vector<Activity> *> input_activity_map_get;
  231. void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node);
  232. ObjectID last_animation_player;
  233. protected:
  234. bool _set(const StringName &p_name, const Variant &p_value);
  235. bool _get(const StringName &p_name, Variant &r_ret) const;
  236. void _get_property_list(List<PropertyInfo> *p_list) const;
  237. void _notification(int p_what);
  238. static void _bind_methods();
  239. public:
  240. void set_tree_root(const Ref<AnimationNode> &p_root);
  241. Ref<AnimationNode> get_tree_root() const;
  242. void set_active(bool p_active);
  243. bool is_active() const;
  244. void set_process_mode(AnimationProcessMode p_mode);
  245. AnimationProcessMode get_process_mode() const;
  246. void set_animation_player(const NodePath &p_player);
  247. NodePath get_animation_player() const;
  248. virtual String get_configuration_warning() const;
  249. bool is_state_invalid() const;
  250. String get_invalid_state_reason() const;
  251. void set_root_motion_track(const NodePath &p_track);
  252. NodePath get_root_motion_track() const;
  253. Transform get_root_motion_transform() const;
  254. float get_connection_activity(const StringName &p_path, int p_connection) const;
  255. void advance(float p_time);
  256. void rename_parameter(const String &p_base, const String &p_new_base);
  257. uint64_t get_last_process_pass() const;
  258. AnimationTree();
  259. ~AnimationTree();
  260. };
  261. VARIANT_ENUM_CAST(AnimationTree::AnimationProcessMode)
  262. #endif // ANIMATION_GRAPH_PLAYER_H