animation_tree_player.h 13 KB

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