animation_tree_player.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**************************************************************************/
  2. /* animation_tree_player.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_PLAYER_H
  31. #define ANIMATION_TREE_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 AnimationTreePlayer : public Node {
  37. GDCLASS(AnimationTreePlayer, Node);
  38. OBJ_CATEGORY("Animation Nodes");
  39. public:
  40. enum AnimationProcessMode {
  41. ANIMATION_PROCESS_PHYSICS,
  42. ANIMATION_PROCESS_IDLE,
  43. };
  44. enum NodeType {
  45. NODE_OUTPUT,
  46. NODE_ANIMATION,
  47. NODE_ONESHOT,
  48. NODE_MIX,
  49. NODE_BLEND2,
  50. NODE_BLEND3,
  51. NODE_BLEND4,
  52. NODE_TIMESCALE,
  53. NODE_TIMESEEK,
  54. NODE_TRANSITION,
  55. NODE_MAX,
  56. };
  57. enum ConnectError {
  58. CONNECT_OK,
  59. CONNECT_INCOMPLETE,
  60. CONNECT_CYCLE
  61. };
  62. private:
  63. enum {
  64. DISCONNECTED = -1,
  65. };
  66. struct TrackKey {
  67. uint32_t id;
  68. StringName subpath_concatenated;
  69. int bone_idx;
  70. inline bool operator<(const TrackKey &p_right) const {
  71. if (id == p_right.id) {
  72. if (bone_idx == p_right.bone_idx) {
  73. return subpath_concatenated < p_right.subpath_concatenated;
  74. } else {
  75. return bone_idx < p_right.bone_idx;
  76. }
  77. } else {
  78. return id < p_right.id;
  79. }
  80. }
  81. };
  82. struct Track {
  83. uint32_t id;
  84. Object *object;
  85. Spatial *spatial;
  86. Skeleton *skeleton;
  87. int bone_idx;
  88. Vector<StringName> subpath;
  89. Vector3 loc;
  90. Quat rot;
  91. Vector3 scale;
  92. Variant value;
  93. bool skip;
  94. Track() :
  95. id(0),
  96. object(nullptr),
  97. spatial(nullptr),
  98. skeleton(nullptr),
  99. bone_idx(-1),
  100. skip(false) {}
  101. };
  102. typedef Map<TrackKey, Track> TrackMap;
  103. TrackMap track_map;
  104. struct Input {
  105. StringName node;
  106. //Input() { node=-1; }
  107. };
  108. struct NodeBase {
  109. bool cycletest;
  110. NodeType type;
  111. Point2 pos;
  112. Vector<Input> inputs;
  113. NodeBase() { cycletest = false; };
  114. virtual ~NodeBase() { cycletest = false; }
  115. };
  116. struct NodeOut : public NodeBase {
  117. NodeOut() {
  118. type = NODE_OUTPUT;
  119. inputs.resize(1);
  120. }
  121. };
  122. struct AnimationNode : public NodeBase {
  123. Ref<Animation> animation;
  124. struct TrackRef {
  125. int local_track;
  126. Track *track;
  127. float weight;
  128. };
  129. uint64_t last_version;
  130. List<TrackRef> tref;
  131. AnimationNode *next;
  132. float time;
  133. float step;
  134. String from;
  135. bool skip;
  136. HashMap<NodePath, bool> filter;
  137. AnimationNode() {
  138. type = NODE_ANIMATION;
  139. next = nullptr;
  140. last_version = 0;
  141. skip = false;
  142. }
  143. };
  144. struct OneShotNode : public NodeBase {
  145. bool active;
  146. bool start;
  147. float fade_in;
  148. float fade_out;
  149. bool autorestart;
  150. float autorestart_delay;
  151. float autorestart_random_delay;
  152. bool mix;
  153. float time;
  154. float remaining;
  155. float autorestart_remaining;
  156. HashMap<NodePath, bool> filter;
  157. OneShotNode() {
  158. type = NODE_ONESHOT;
  159. fade_in = 0;
  160. fade_out = 0;
  161. inputs.resize(2);
  162. autorestart = false;
  163. autorestart_delay = 1;
  164. autorestart_remaining = 0;
  165. mix = false;
  166. active = false;
  167. start = false;
  168. }
  169. };
  170. struct MixNode : public NodeBase {
  171. float amount;
  172. MixNode() {
  173. type = NODE_MIX;
  174. inputs.resize(2);
  175. }
  176. };
  177. struct Blend2Node : public NodeBase {
  178. float value;
  179. HashMap<NodePath, bool> filter;
  180. Blend2Node() {
  181. type = NODE_BLEND2;
  182. value = 0;
  183. inputs.resize(2);
  184. }
  185. };
  186. struct Blend3Node : public NodeBase {
  187. float value;
  188. Blend3Node() {
  189. type = NODE_BLEND3;
  190. value = 0;
  191. inputs.resize(3);
  192. }
  193. };
  194. struct Blend4Node : public NodeBase {
  195. Point2 value;
  196. Blend4Node() {
  197. type = NODE_BLEND4;
  198. inputs.resize(4);
  199. }
  200. };
  201. struct TimeScaleNode : public NodeBase {
  202. float scale;
  203. TimeScaleNode() {
  204. type = NODE_TIMESCALE;
  205. scale = 1;
  206. inputs.resize(1);
  207. }
  208. };
  209. struct TimeSeekNode : public NodeBase {
  210. float seek_pos;
  211. TimeSeekNode() {
  212. type = NODE_TIMESEEK;
  213. inputs.resize(1);
  214. seek_pos = -1;
  215. }
  216. };
  217. struct TransitionNode : public NodeBase {
  218. struct InputData {
  219. bool auto_advance;
  220. InputData() { auto_advance = false; }
  221. };
  222. Vector<InputData> input_data;
  223. float prev_time;
  224. float prev_xfading;
  225. int prev;
  226. bool switched;
  227. float time;
  228. int current;
  229. float xfade;
  230. TransitionNode() {
  231. type = NODE_TRANSITION;
  232. xfade = 0;
  233. inputs.resize(1);
  234. input_data.resize(1);
  235. current = 0;
  236. prev = -1;
  237. prev_time = 0;
  238. prev_xfading = 0;
  239. switched = false;
  240. }
  241. void set_current(int p_current);
  242. };
  243. void _update_sources();
  244. StringName out_name;
  245. NodeOut *out;
  246. NodePath base_path;
  247. NodePath master;
  248. ConnectError last_error;
  249. AnimationNode *active_list;
  250. AnimationProcessMode animation_process_mode;
  251. bool processing;
  252. bool active;
  253. bool dirty_caches;
  254. Map<StringName, NodeBase *> node_map;
  255. // return time left to finish animation
  256. float _process_node(const StringName &p_node, AnimationNode **r_prev_anim, float p_time, bool p_seek = false, float p_fallback_weight = 1.0, HashMap<NodePath, float> *p_weights = nullptr);
  257. void _process_animation(float p_delta);
  258. bool reset_request;
  259. ConnectError _cycle_test(const StringName &p_at_node);
  260. void _clear_cycle_test();
  261. Track *_find_track(const NodePath &p_path);
  262. void _recompute_caches();
  263. void _recompute_caches(const StringName &p_node);
  264. PoolVector<String> _get_node_list();
  265. void _compute_weights(float *p_fallback_weight, HashMap<NodePath, float> *p_weights, float p_coeff, const HashMap<NodePath, bool> *p_filter = nullptr, float p_filtered_coeff = 0);
  266. protected:
  267. bool _set(const StringName &p_name, const Variant &p_value);
  268. bool _get(const StringName &p_name, Variant &r_ret) const;
  269. void _get_property_list(List<PropertyInfo> *p_list) const;
  270. void _notification(int p_what);
  271. static void _bind_methods();
  272. public:
  273. void add_node(NodeType p_type, const StringName &p_node); // nodes must be >0 node 0 is built-in (exit)
  274. bool node_exists(const StringName &p_name) const;
  275. Error node_rename(const StringName &p_node, const StringName &p_new_name);
  276. int node_get_input_count(const StringName &p_node) const;
  277. StringName node_get_input_source(const StringName &p_node, int p_input) const;
  278. String get_configuration_warning() const;
  279. /* ANIMATION NODE */
  280. void animation_node_set_animation(const StringName &p_node, const Ref<Animation> &p_animation);
  281. Ref<Animation> animation_node_get_animation(const StringName &p_node) const;
  282. void animation_node_set_master_animation(const StringName &p_node, const String &p_master_animation);
  283. String animation_node_get_master_animation(const StringName &p_node) const;
  284. float animation_node_get_position(const StringName &p_node) const;
  285. void animation_node_set_filter_path(const StringName &p_node, const NodePath &p_track_path, bool p_filter);
  286. void animation_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const;
  287. bool animation_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const;
  288. /* ONE SHOT NODE */
  289. void oneshot_node_set_fadein_time(const StringName &p_node, float p_time);
  290. void oneshot_node_set_fadeout_time(const StringName &p_node, float p_time);
  291. float oneshot_node_get_fadein_time(const StringName &p_node) const;
  292. float oneshot_node_get_fadeout_time(const StringName &p_node) const;
  293. void oneshot_node_set_autorestart(const StringName &p_node, bool p_active);
  294. void oneshot_node_set_autorestart_delay(const StringName &p_node, float p_time);
  295. void oneshot_node_set_autorestart_random_delay(const StringName &p_node, float p_time);
  296. bool oneshot_node_has_autorestart(const StringName &p_node) const;
  297. float oneshot_node_get_autorestart_delay(const StringName &p_node) const;
  298. float oneshot_node_get_autorestart_random_delay(const StringName &p_node) const;
  299. void oneshot_node_set_mix_mode(const StringName &p_node, bool p_mix);
  300. bool oneshot_node_get_mix_mode(const StringName &p_node) const;
  301. void oneshot_node_start(const StringName &p_node);
  302. void oneshot_node_stop(const StringName &p_node);
  303. bool oneshot_node_is_active(const StringName &p_node) const;
  304. void oneshot_node_set_filter_path(const StringName &p_node, const NodePath &p_filter, bool p_enable);
  305. void oneshot_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const;
  306. bool oneshot_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const;
  307. /* MIX/BLEND NODES */
  308. void mix_node_set_amount(const StringName &p_node, float p_amount);
  309. float mix_node_get_amount(const StringName &p_node) const;
  310. void blend2_node_set_amount(const StringName &p_node, float p_amount);
  311. float blend2_node_get_amount(const StringName &p_node) const;
  312. void blend2_node_set_filter_path(const StringName &p_node, const NodePath &p_filter, bool p_enable);
  313. void blend2_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const;
  314. bool blend2_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const;
  315. void blend3_node_set_amount(const StringName &p_node, float p_amount);
  316. float blend3_node_get_amount(const StringName &p_node) const;
  317. void blend4_node_set_amount(const StringName &p_node, const Point2 &p_amount);
  318. Point2 blend4_node_get_amount(const StringName &p_node) const;
  319. /* TIMESCALE/TIMESEEK NODES */
  320. void timescale_node_set_scale(const StringName &p_node, float p_scale);
  321. float timescale_node_get_scale(const StringName &p_node) const;
  322. void timeseek_node_seek(const StringName &p_node, float p_pos);
  323. /* TRANSITION NODE */
  324. void transition_node_set_input_count(const StringName &p_node, int p_inputs); // used for transition node
  325. int transition_node_get_input_count(const StringName &p_node) const;
  326. void transition_node_delete_input(const StringName &p_node, int p_input); // used for transition node
  327. void transition_node_set_input_auto_advance(const StringName &p_node, int p_input, bool p_auto_advance); // used for transition node
  328. bool transition_node_has_input_auto_advance(const StringName &p_node, int p_input) const;
  329. void transition_node_set_xfade_time(const StringName &p_node, float p_time); // used for transition node
  330. float transition_node_get_xfade_time(const StringName &p_node) const;
  331. void transition_node_set_current(const StringName &p_node, int p_current);
  332. int transition_node_get_current(const StringName &p_node) const;
  333. void node_set_position(const StringName &p_node, const Vector2 &p_pos); //for display
  334. /* GETS */
  335. Point2 node_get_position(const StringName &p_node) const; //for display
  336. NodeType node_get_type(const StringName &p_node) const;
  337. void get_node_list(List<StringName> *p_node_list) const;
  338. void remove_node(const StringName &p_node);
  339. Error connect_nodes(const StringName &p_src_node, const StringName &p_dst_node, int p_dst_input);
  340. bool are_nodes_connected(const StringName &p_src_node, const StringName &p_dst_node, int p_dst_input) const;
  341. void disconnect_nodes(const StringName &p_node, int p_input);
  342. void set_base_path(const NodePath &p_path);
  343. NodePath get_base_path() const;
  344. void set_master_player(const NodePath &p_path);
  345. NodePath get_master_player() const;
  346. struct Connection {
  347. StringName src_node;
  348. StringName dst_node;
  349. int dst_input;
  350. };
  351. void get_connection_list(List<Connection> *p_connections) const;
  352. /* playback */
  353. void set_active(bool p_active);
  354. bool is_active() const;
  355. void reset();
  356. void recompute_caches();
  357. ConnectError get_last_error() const;
  358. void set_animation_process_mode(AnimationProcessMode p_mode);
  359. AnimationProcessMode get_animation_process_mode() const;
  360. void _set_process(bool p_process, bool p_force = false);
  361. void advance(float p_time);
  362. AnimationTreePlayer();
  363. ~AnimationTreePlayer();
  364. };
  365. VARIANT_ENUM_CAST(AnimationTreePlayer::NodeType);
  366. VARIANT_ENUM_CAST(AnimationTreePlayer::AnimationProcessMode);
  367. #endif // ANIMATION_TREE_PLAYER_H