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