animation_graph_player.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. #include "animation_graph_player.h"
  2. #include "animation_blend_tree.h"
  3. #include "core/method_bind_ext.gen.inc"
  4. #include "engine.h"
  5. #include "scene/scene_string_names.h"
  6. #include "servers/audio/audio_stream.h"
  7. void AnimationNode::blend_animation(const StringName &p_animation, float p_time, float p_delta, bool p_seeked, float p_blend) {
  8. ERR_FAIL_COND(!state);
  9. ERR_FAIL_COND(!state->player->has_animation(p_animation));
  10. Ref<Animation> animation = state->player->get_animation(p_animation);
  11. if (animation.is_null()) {
  12. Ref<AnimationNodeBlendTree> btree = get_parent();
  13. if (btree.is_valid()) {
  14. String name = btree->get_node_name(Ref<AnimationNodeAnimation>(this));
  15. make_invalid(vformat(RTR("In node '%s', invalid animation: '%s'."), name, p_animation));
  16. } else {
  17. make_invalid(vformat(RTR("Invalid animation: '%s'."), p_animation));
  18. }
  19. return;
  20. }
  21. ERR_FAIL_COND(!animation.is_valid());
  22. AnimationState anim_state;
  23. anim_state.blend = p_blend;
  24. anim_state.track_blends = &blends;
  25. anim_state.delta = p_delta;
  26. anim_state.time = p_time;
  27. anim_state.animation = animation;
  28. anim_state.seeked = p_seeked;
  29. state->animation_states.push_back(anim_state);
  30. }
  31. float AnimationNode::_pre_process(State *p_state, float p_time, bool p_seek) {
  32. state = p_state;
  33. float t = process(p_time, p_seek);
  34. state = NULL;
  35. return t;
  36. }
  37. void AnimationNode::make_invalid(const String &p_reason) {
  38. ERR_FAIL_COND(!state);
  39. state->valid = false;
  40. if (state->invalid_reasons != String()) {
  41. state->invalid_reasons += "\n";
  42. }
  43. state->invalid_reasons += "- " + p_reason;
  44. }
  45. float AnimationNode::blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize) {
  46. ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
  47. ERR_FAIL_COND_V(!state, 0);
  48. ERR_FAIL_COND_V(!get_graph_player(), 0); //should not happen, but used to catch bugs
  49. Ref<AnimationNodeBlendTree> tree = get_parent();
  50. if (!tree.is_valid() && get_graph_player()->get_graph_root().ptr() != this) {
  51. make_invalid(RTR("Can't blend input because node is not in a tree"));
  52. return 0;
  53. }
  54. ERR_FAIL_COND_V(!tree.is_valid(), 0); //should not happen
  55. StringName anim_name = inputs[p_input].connected_to;
  56. Ref<AnimationNode> node = tree->get_node(anim_name);
  57. if (node.is_null()) {
  58. String name = tree->get_node_name(Ref<AnimationNodeAnimation>(this));
  59. make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), name));
  60. return 0;
  61. }
  62. inputs[p_input].last_pass = state->last_pass;
  63. return _blend_node(node, p_time, p_seek, p_blend, p_filter, p_optimize, &inputs[p_input].activity);
  64. }
  65. float AnimationNode::blend_node(Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize) {
  66. return _blend_node(p_node, p_time, p_seek, p_blend, p_filter, p_optimize);
  67. }
  68. float AnimationNode::_blend_node(Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize, float *r_max) {
  69. ERR_FAIL_COND_V(!p_node.is_valid(), 0);
  70. ERR_FAIL_COND_V(!state, 0);
  71. int blend_count = blends.size();
  72. if (p_node->blends.size() != blend_count) {
  73. p_node->blends.resize(blend_count);
  74. }
  75. float *blendw = p_node->blends.ptrw();
  76. const float *blendr = blends.ptr();
  77. bool any_valid = false;
  78. if (has_filter() && is_filter_enabled() && p_filter != FILTER_IGNORE) {
  79. for (int i = 0; i < blend_count; i++) {
  80. blendw[i] = 0.0; //all to zero by default
  81. }
  82. const NodePath *K = NULL;
  83. while ((K = filter.next(K))) {
  84. if (!state->track_map.has(*K)) {
  85. continue;
  86. }
  87. int idx = state->track_map[*K];
  88. blendw[idx] = 1.0; //filtered goes to one
  89. }
  90. switch (p_filter) {
  91. case FILTER_IGNORE:
  92. break; //will not happen anyway
  93. case FILTER_PASS: {
  94. //values filtered pass, the rest dont
  95. for (int i = 0; i < blend_count; i++) {
  96. if (blendw[i] == 0) //not filtered, does not pass
  97. continue;
  98. blendw[i] = blendr[i] * p_blend;
  99. if (blendw[i] > CMP_EPSILON) {
  100. any_valid = true;
  101. }
  102. }
  103. } break;
  104. case FILTER_STOP: {
  105. //values filtered dont pass, the rest are blended
  106. for (int i = 0; i < blend_count; i++) {
  107. if (blendw[i] > 0) //filtered, does not pass
  108. continue;
  109. blendw[i] = blendr[i] * p_blend;
  110. if (blendw[i] > CMP_EPSILON) {
  111. any_valid = true;
  112. }
  113. }
  114. } break;
  115. case FILTER_BLEND: {
  116. //filtered values are blended, the rest are passed without blending
  117. for (int i = 0; i < blend_count; i++) {
  118. if (blendw[i] == 1.0) {
  119. blendw[i] = blendr[i] * p_blend; //filtered, blend
  120. } else {
  121. blendw[i] = blendr[i]; //not filtered, do not blend
  122. }
  123. if (blendw[i] > CMP_EPSILON) {
  124. any_valid = true;
  125. }
  126. }
  127. } break;
  128. }
  129. } else {
  130. for (int i = 0; i < blend_count; i++) {
  131. //regular blend
  132. blendw[i] = blendr[i] * p_blend;
  133. if (blendw[i] > CMP_EPSILON) {
  134. any_valid = true;
  135. }
  136. }
  137. }
  138. if (r_max) {
  139. *r_max = 0;
  140. for (int i = 0; i < blend_count; i++) {
  141. *r_max = MAX(*r_max, blendw[i]);
  142. }
  143. }
  144. if (!p_seek && p_optimize && !any_valid) //pointless to go on, all are zero
  145. return 0;
  146. return p_node->_pre_process(state, p_time, p_seek);
  147. }
  148. int AnimationNode::get_input_count() const {
  149. return inputs.size();
  150. }
  151. String AnimationNode::get_input_name(int p_input) {
  152. ERR_FAIL_INDEX_V(p_input, inputs.size(), String());
  153. return inputs[p_input].name;
  154. }
  155. float AnimationNode::get_input_activity(int p_input) const {
  156. ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
  157. if (!get_graph_player())
  158. return 0;
  159. if (get_graph_player()->get_last_process_pass() != inputs[p_input].last_pass) {
  160. return 0;
  161. }
  162. return inputs[p_input].activity;
  163. }
  164. StringName AnimationNode::get_input_connection(int p_input) {
  165. ERR_FAIL_INDEX_V(p_input, inputs.size(), StringName());
  166. return inputs[p_input].connected_to;
  167. }
  168. void AnimationNode::set_input_connection(int p_input, const StringName &p_connection) {
  169. ERR_FAIL_INDEX(p_input, inputs.size());
  170. inputs[p_input].connected_to = p_connection;
  171. }
  172. String AnimationNode::get_caption() const {
  173. return "Node";
  174. }
  175. void AnimationNode::add_input(const String &p_name) {
  176. //root nodes cant add inputs
  177. ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != NULL)
  178. Input input;
  179. ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1);
  180. input.name = p_name;
  181. input.activity = 0;
  182. input.last_pass = 0;
  183. inputs.push_back(input);
  184. emit_changed();
  185. }
  186. void AnimationNode::set_input_name(int p_input, const String &p_name) {
  187. ERR_FAIL_INDEX(p_input, inputs.size());
  188. ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1);
  189. inputs[p_input].name = p_name;
  190. emit_changed();
  191. }
  192. void AnimationNode::remove_input(int p_index) {
  193. ERR_FAIL_INDEX(p_index, inputs.size());
  194. inputs.remove(p_index);
  195. emit_changed();
  196. }
  197. void AnimationNode::set_parent(AnimationNode *p_parent) {
  198. parent = p_parent; //do not use ref because parent contains children
  199. }
  200. Ref<AnimationNode> AnimationNode::get_parent() const {
  201. if (parent) {
  202. return Ref<AnimationNode>(parent);
  203. }
  204. return Ref<AnimationNode>();
  205. }
  206. AnimationGraphPlayer *AnimationNode::get_graph_player() const {
  207. return player;
  208. }
  209. AnimationPlayer *AnimationNode::get_player() const {
  210. ERR_FAIL_COND_V(!state, NULL);
  211. return state->player;
  212. }
  213. float AnimationNode::process(float p_time, bool p_seek) {
  214. if (get_script_instance()) {
  215. return get_script_instance()->call("process", p_time, p_seek);
  216. }
  217. return 0;
  218. }
  219. void AnimationNode::set_filter_path(const NodePath &p_path, bool p_enable) {
  220. if (p_enable) {
  221. filter[p_path] = true;
  222. } else {
  223. filter.erase(p_path);
  224. }
  225. }
  226. void AnimationNode::set_filter_enabled(bool p_enable) {
  227. filter_enabled = p_enable;
  228. }
  229. bool AnimationNode::is_filter_enabled() const {
  230. return filter_enabled;
  231. }
  232. bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
  233. return filter.has(p_path);
  234. }
  235. bool AnimationNode::has_filter() const {
  236. return false;
  237. }
  238. void AnimationNode::set_position(const Vector2 &p_position) {
  239. position = p_position;
  240. }
  241. Vector2 AnimationNode::get_position() const {
  242. return position;
  243. }
  244. void AnimationNode::set_graph_player(AnimationGraphPlayer *p_player) {
  245. if (player != NULL && p_player == NULL) {
  246. emit_signal("removed_from_graph");
  247. }
  248. player = p_player;
  249. }
  250. Array AnimationNode::_get_filters() const {
  251. Array paths;
  252. const NodePath *K = NULL;
  253. while ((K = filter.next(K))) {
  254. paths.push_back(String(*K)); //use strings, so sorting is possible
  255. }
  256. paths.sort(); //done so every time the scene is saved, it does not change
  257. return paths;
  258. }
  259. void AnimationNode::_set_filters(const Array &p_filters) {
  260. filter.clear();
  261. for (int i = 0; i < p_filters.size(); i++) {
  262. set_filter_path(p_filters[i], true);
  263. }
  264. }
  265. void AnimationNode::_validate_property(PropertyInfo &property) const {
  266. if (!has_filter() && (property.name == "filter_enabled" || property.name == "filters")) {
  267. property.usage = 0;
  268. }
  269. }
  270. void AnimationNode::_bind_methods() {
  271. ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
  272. ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
  273. ClassDB::bind_method(D_METHOD("get_input_connection", "input"), &AnimationNode::get_input_connection);
  274. ClassDB::bind_method(D_METHOD("get_input_activity", "input"), &AnimationNode::get_input_activity);
  275. ClassDB::bind_method(D_METHOD("add_input", "name"), &AnimationNode::add_input);
  276. ClassDB::bind_method(D_METHOD("remove_input", "index"), &AnimationNode::remove_input);
  277. ClassDB::bind_method(D_METHOD("set_filter_path", "path", "enable"), &AnimationNode::set_filter_path);
  278. ClassDB::bind_method(D_METHOD("is_path_filtered", "path"), &AnimationNode::is_path_filtered);
  279. ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
  280. ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
  281. ClassDB::bind_method(D_METHOD("set_position", "position"), &AnimationNode::set_position);
  282. ClassDB::bind_method(D_METHOD("get_position"), &AnimationNode::get_position);
  283. ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
  284. ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
  285. ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "blend"), &AnimationNode::blend_animation);
  286. ClassDB::bind_method(D_METHOD("blend_node", "node", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_node, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  287. ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  288. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_filter_enabled", "is_filter_enabled");
  289. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");
  290. BIND_VMETHOD(MethodInfo("process", PropertyInfo(Variant::REAL, "time"), PropertyInfo(Variant::BOOL, "seek")));
  291. ADD_SIGNAL(MethodInfo("removed_from_graph"));
  292. BIND_ENUM_CONSTANT(FILTER_IGNORE);
  293. BIND_ENUM_CONSTANT(FILTER_PASS);
  294. BIND_ENUM_CONSTANT(FILTER_STOP);
  295. BIND_ENUM_CONSTANT(FILTER_BLEND);
  296. }
  297. AnimationNode::AnimationNode() {
  298. state = NULL;
  299. parent = NULL;
  300. player = NULL;
  301. set_local_to_scene(true);
  302. filter_enabled = false;
  303. }
  304. ////////////////////
  305. void AnimationGraphPlayer::set_graph_root(const Ref<AnimationNode> &p_root) {
  306. if (root.is_valid()) {
  307. root->set_graph_player(NULL);
  308. }
  309. if (p_root.is_valid()) {
  310. ERR_EXPLAIN("root node already set to another player");
  311. ERR_FAIL_COND(p_root->player);
  312. }
  313. root = p_root;
  314. if (root.is_valid()) {
  315. root->set_graph_player(this);
  316. }
  317. update_configuration_warning();
  318. }
  319. Ref<AnimationNode> AnimationGraphPlayer::get_graph_root() const {
  320. return root;
  321. }
  322. void AnimationGraphPlayer::set_active(bool p_active) {
  323. if (active == p_active)
  324. return;
  325. active = p_active;
  326. started = active;
  327. if (process_mode == ANIMATION_PROCESS_IDLE) {
  328. set_process_internal(active);
  329. } else {
  330. set_physics_process_internal(active);
  331. }
  332. if (!active && is_inside_tree()) {
  333. for (Set<TrackCache *>::Element *E = playing_caches.front(); E; E = E->next()) {
  334. if (ObjectDB::get_instance(E->get()->object_id)) {
  335. E->get()->object->call("stop");
  336. }
  337. }
  338. playing_caches.clear();
  339. }
  340. }
  341. bool AnimationGraphPlayer::is_active() const {
  342. return active;
  343. }
  344. void AnimationGraphPlayer::set_process_mode(AnimationProcessMode p_mode) {
  345. if (process_mode == p_mode)
  346. return;
  347. bool was_active = is_active();
  348. if (was_active) {
  349. set_active(false);
  350. }
  351. process_mode = p_mode;
  352. if (was_active) {
  353. set_active(true);
  354. }
  355. }
  356. AnimationGraphPlayer::AnimationProcessMode AnimationGraphPlayer::get_process_mode() const {
  357. return process_mode;
  358. }
  359. void AnimationGraphPlayer::_node_removed(Node *p_node) {
  360. cache_valid = false;
  361. }
  362. bool AnimationGraphPlayer::_update_caches(AnimationPlayer *player) {
  363. setup_pass++;
  364. if (!player->has_node(player->get_root())) {
  365. ERR_PRINT("AnimationGraphPlayer: AnimationPlayer root is invalid.");
  366. set_active(false);
  367. return false;
  368. }
  369. Node *parent = player->get_node(player->get_root());
  370. List<StringName> sname;
  371. player->get_animation_list(&sname);
  372. for (List<StringName>::Element *E = sname.front(); E; E = E->next()) {
  373. Ref<Animation> anim = player->get_animation(E->get());
  374. for (int i = 0; i < anim->get_track_count(); i++) {
  375. NodePath path = anim->track_get_path(i);
  376. Animation::TrackType track_type = anim->track_get_type(i);
  377. TrackCache *track = NULL;
  378. if (track_cache.has(path)) {
  379. track = track_cache.get(path);
  380. }
  381. //if not valid, delete track
  382. if (track && (track->type != track_type || ObjectDB::get_instance(track->object_id) == NULL)) {
  383. playing_caches.erase(track);
  384. memdelete(track);
  385. track_cache.erase(path);
  386. track = NULL;
  387. }
  388. if (!track) {
  389. RES resource;
  390. Vector<StringName> leftover_path;
  391. Node *child = parent->get_node_and_resource(path, resource, leftover_path);
  392. if (!child) {
  393. ERR_PRINTS("AnimationGraphPlayer: '" + String(E->get()) + "', couldn't resolve track: '" + String(path) + "'");
  394. continue;
  395. }
  396. if (!child->is_connected("tree_exited", this, "_node_removed")) {
  397. child->connect("tree_exited", this, "_node_removed", varray(child));
  398. }
  399. switch (track_type) {
  400. case Animation::TYPE_VALUE: {
  401. TrackCacheValue *track_value = memnew(TrackCacheValue);
  402. if (resource.is_valid()) {
  403. track_value->object = resource.ptr();
  404. } else {
  405. track_value->object = child;
  406. }
  407. track_value->subpath = leftover_path;
  408. track_value->object_id = track_value->object->get_instance_id();
  409. track = track_value;
  410. } break;
  411. case Animation::TYPE_TRANSFORM: {
  412. Spatial *spatial = Object::cast_to<Spatial>(child);
  413. if (!spatial) {
  414. ERR_PRINTS("AnimationGraphPlayer: '" + String(E->get()) + "', transform track does not point to spatial: '" + String(path) + "'");
  415. continue;
  416. }
  417. TrackCacheTransform *track_xform = memnew(TrackCacheTransform);
  418. track_xform->spatial = spatial;
  419. track_xform->skeleton = NULL;
  420. track_xform->bone_idx = -1;
  421. if (path.get_subname_count() == 1 && Object::cast_to<Skeleton>(spatial)) {
  422. Skeleton *sk = Object::cast_to<Skeleton>(spatial);
  423. int bone_idx = sk->find_bone(path.get_subname(0));
  424. if (bone_idx != -1 && !sk->is_bone_ignore_animation(bone_idx)) {
  425. track_xform->skeleton = sk;
  426. track_xform->bone_idx = bone_idx;
  427. }
  428. }
  429. track_xform->object = spatial;
  430. track_xform->object_id = track_xform->object->get_instance_id();
  431. track = track_xform;
  432. } break;
  433. case Animation::TYPE_METHOD: {
  434. TrackCacheMethod *track_method = memnew(TrackCacheMethod);
  435. if (resource.is_valid()) {
  436. track_method->object = resource.ptr();
  437. } else {
  438. track_method->object = child;
  439. }
  440. track_method->object_id = track_method->object->get_instance_id();
  441. track = track_method;
  442. } break;
  443. case Animation::TYPE_BEZIER: {
  444. TrackCacheBezier *track_bezier = memnew(TrackCacheBezier);
  445. if (resource.is_valid()) {
  446. track_bezier->object = resource.ptr();
  447. } else {
  448. track_bezier->object = child;
  449. }
  450. track_bezier->subpath = leftover_path;
  451. track_bezier->object_id = track_bezier->object->get_instance_id();
  452. track = track_bezier;
  453. } break;
  454. case Animation::TYPE_AUDIO: {
  455. TrackCacheAudio *track_audio = memnew(TrackCacheAudio);
  456. track_audio->object = child;
  457. track_audio->object_id = track_audio->object->get_instance_id();
  458. track = track_audio;
  459. } break;
  460. case Animation::TYPE_ANIMATION: {
  461. TrackCacheAnimation *track_animation = memnew(TrackCacheAnimation);
  462. track_animation->object = child;
  463. track_animation->object_id = track_animation->object->get_instance_id();
  464. track = track_animation;
  465. } break;
  466. }
  467. track_cache[path] = track;
  468. }
  469. track->setup_pass = setup_pass;
  470. }
  471. }
  472. List<NodePath> to_delete;
  473. const NodePath *K = NULL;
  474. while ((K = track_cache.next(K))) {
  475. TrackCache *tc = track_cache[*K];
  476. if (tc->setup_pass != setup_pass) {
  477. to_delete.push_back(*K);
  478. }
  479. }
  480. while (to_delete.front()) {
  481. NodePath np = to_delete.front()->get();
  482. memdelete(track_cache[np]);
  483. track_cache.erase(np);
  484. to_delete.pop_front();
  485. }
  486. state.track_map.clear();
  487. K = NULL;
  488. int idx = 0;
  489. while ((K = track_cache.next(K))) {
  490. state.track_map[*K] = idx;
  491. idx++;
  492. }
  493. state.track_count = idx;
  494. cache_valid = true;
  495. return true;
  496. }
  497. void AnimationGraphPlayer::_clear_caches() {
  498. const NodePath *K = NULL;
  499. while ((K = track_cache.next(K))) {
  500. memdelete(track_cache[*K]);
  501. }
  502. playing_caches.clear();
  503. track_cache.clear();
  504. cache_valid = false;
  505. }
  506. void AnimationGraphPlayer::_process_graph(float p_delta) {
  507. //check all tracks, see if they need modification
  508. if (!root.is_valid()) {
  509. ERR_PRINT("AnimationGraphPlayer: root AnimationNode is not set, disabling playback.");
  510. set_active(false);
  511. cache_valid = false;
  512. return;
  513. }
  514. if (!has_node(animation_player)) {
  515. ERR_PRINT("AnimationGraphPlayer: no valid AnimationPlayer path set, disabling playback");
  516. set_active(false);
  517. cache_valid = false;
  518. return;
  519. }
  520. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  521. if (!player) {
  522. ERR_PRINT("AnimationGraphPlayer: path points to a node not an AnimationPlayer, disabling playback");
  523. set_active(false);
  524. cache_valid = false;
  525. return;
  526. }
  527. if (!cache_valid) {
  528. if (!_update_caches(player)) {
  529. return;
  530. }
  531. }
  532. { //setup
  533. process_pass++;
  534. state.valid = true;
  535. state.invalid_reasons = "";
  536. state.animation_states.clear(); //will need to be re-created
  537. state.valid = true;
  538. state.player = player;
  539. state.last_pass = process_pass;
  540. // root source blends
  541. root->blends.resize(state.track_count);
  542. float *src_blendsw = root->blends.ptrw();
  543. for (int i = 0; i < state.track_count; i++) {
  544. src_blendsw[i] = 1.0; //by default all go to 1 for the root input
  545. }
  546. }
  547. //process
  548. {
  549. if (started) {
  550. //if started, seek
  551. root->_pre_process(&state, 0, true);
  552. started = false;
  553. }
  554. root->_pre_process(&state, p_delta, false);
  555. }
  556. if (!state.valid) {
  557. return; //state is not valid. do nothing.
  558. }
  559. //apply value/transform/bezier blends to track caches and execute method/audio/animation tracks
  560. {
  561. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  562. for (List<AnimationNode::AnimationState>::Element *E = state.animation_states.front(); E; E = E->next()) {
  563. const AnimationNode::AnimationState &as = E->get();
  564. Ref<Animation> a = as.animation;
  565. float time = as.time;
  566. float delta = as.delta;
  567. bool seeked = as.seeked;
  568. for (int i = 0; i < a->get_track_count(); i++) {
  569. NodePath path = a->track_get_path(i);
  570. TrackCache *track = track_cache[path];
  571. if (track->type != a->track_get_type(i)) {
  572. continue; //may happen should not
  573. }
  574. ERR_CONTINUE(!state.track_map.has(path));
  575. int blend_idx = state.track_map[path];
  576. ERR_CONTINUE(blend_idx < 0 || blend_idx >= state.track_count);
  577. float blend = (*as.track_blends)[blend_idx];
  578. if (blend < CMP_EPSILON)
  579. continue; //nothing to blend
  580. switch (track->type) {
  581. case Animation::TYPE_TRANSFORM: {
  582. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  583. Vector3 loc;
  584. Quat rot;
  585. Vector3 scale;
  586. Error err = a->transform_track_interpolate(i, time, &loc, &rot, &scale);
  587. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  588. scale -= Vector3(1.0, 1.0, 1.0); //helps make it work properly with Add nodes
  589. if (err != OK)
  590. continue;
  591. if (t->process_pass != process_pass) {
  592. t->process_pass = process_pass;
  593. t->loc = Vector3();
  594. t->rot = Quat();
  595. t->scale = Vector3();
  596. }
  597. t->loc = t->loc.linear_interpolate(loc, blend);
  598. t->rot = t->rot.slerp(rot, blend);
  599. t->scale = t->scale.linear_interpolate(scale, blend);
  600. } break;
  601. case Animation::TYPE_VALUE: {
  602. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  603. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  604. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) { //delta == 0 means seek
  605. Variant value = a->value_track_interpolate(i, time);
  606. if (value == Variant())
  607. continue;
  608. if (t->process_pass != process_pass) {
  609. Variant::CallError ce;
  610. t->value = Variant::construct(value.get_type(), NULL, 0, ce); //reset
  611. t->process_pass = process_pass;
  612. }
  613. Variant::interpolate(t->value, value, blend, t->value);
  614. } else if (delta != 0) {
  615. List<int> indices;
  616. a->value_track_get_key_indices(i, time, delta, &indices);
  617. for (List<int>::Element *F = indices.front(); F; F = F->next()) {
  618. Variant value = a->track_get_key_value(i, F->get());
  619. t->object->set_indexed(t->subpath, value);
  620. }
  621. }
  622. } break;
  623. case Animation::TYPE_METHOD: {
  624. if (delta == 0) {
  625. continue;
  626. }
  627. TrackCacheMethod *t = static_cast<TrackCacheMethod *>(track);
  628. List<int> indices;
  629. a->method_track_get_key_indices(i, time, delta, &indices);
  630. for (List<int>::Element *E = indices.front(); E; E = E->next()) {
  631. StringName method = a->method_track_get_name(i, E->get());
  632. Vector<Variant> params = a->method_track_get_params(i, E->get());
  633. int s = params.size();
  634. ERR_CONTINUE(s > VARIANT_ARG_MAX);
  635. if (can_call) {
  636. t->object->call_deferred(
  637. method,
  638. s >= 1 ? params[0] : Variant(),
  639. s >= 2 ? params[1] : Variant(),
  640. s >= 3 ? params[2] : Variant(),
  641. s >= 4 ? params[3] : Variant(),
  642. s >= 5 ? params[4] : Variant());
  643. }
  644. }
  645. } break;
  646. case Animation::TYPE_BEZIER: {
  647. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  648. float bezier = a->bezier_track_interpolate(i, time);
  649. if (t->process_pass != process_pass) {
  650. t->value = 0;
  651. t->process_pass = process_pass;
  652. }
  653. t->value = Math::lerp(t->value, bezier, blend);
  654. } break;
  655. case Animation::TYPE_AUDIO: {
  656. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  657. if (seeked) {
  658. //find whathever should be playing
  659. int idx = a->track_find_key(i, time);
  660. if (idx < 0)
  661. continue;
  662. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  663. if (!stream.is_valid()) {
  664. t->object->call("stop");
  665. t->playing = false;
  666. playing_caches.erase(t);
  667. } else {
  668. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  669. start_ofs += time - a->track_get_key_time(i, idx);
  670. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  671. float len = stream->get_length();
  672. if (start_ofs > len - end_ofs) {
  673. t->object->call("stop");
  674. t->playing = false;
  675. playing_caches.erase(t);
  676. continue;
  677. }
  678. t->object->call("set_stream", stream);
  679. t->object->call("play", start_ofs);
  680. t->playing = true;
  681. playing_caches.insert(t);
  682. if (len && end_ofs > 0) { //force a end at a time
  683. t->len = len - start_ofs - end_ofs;
  684. } else {
  685. t->len = 0;
  686. }
  687. t->start = time;
  688. }
  689. } else {
  690. //find stuff to play
  691. List<int> to_play;
  692. a->track_get_key_indices_in_range(i, time, delta, &to_play);
  693. if (to_play.size()) {
  694. int idx = to_play.back()->get();
  695. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  696. if (!stream.is_valid()) {
  697. t->object->call("stop");
  698. t->playing = false;
  699. playing_caches.erase(t);
  700. } else {
  701. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  702. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  703. float len = stream->get_length();
  704. t->object->call("set_stream", stream);
  705. t->object->call("play", start_ofs);
  706. t->playing = true;
  707. playing_caches.insert(t);
  708. if (len && end_ofs > 0) { //force a end at a time
  709. t->len = len - start_ofs - end_ofs;
  710. } else {
  711. t->len = 0;
  712. }
  713. t->start = time;
  714. }
  715. } else if (t->playing) {
  716. if (t->start > time || (t->len > 0 && time - t->start < t->len)) {
  717. //time to stop
  718. t->object->call("stop");
  719. t->playing = false;
  720. playing_caches.erase(t);
  721. }
  722. }
  723. }
  724. } break;
  725. case Animation::TYPE_ANIMATION: {
  726. TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track);
  727. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(t->object);
  728. if (!player)
  729. continue;
  730. if (delta == 0 || seeked) {
  731. //seek
  732. int idx = a->track_find_key(i, time);
  733. if (idx < 0)
  734. continue;
  735. float pos = a->track_get_key_time(i, idx);
  736. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  737. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name))
  738. continue;
  739. Ref<Animation> anim = player->get_animation(anim_name);
  740. float at_anim_pos;
  741. if (anim->has_loop()) {
  742. at_anim_pos = Math::fposmod(time - pos, anim->get_length()); //seek to loop
  743. } else {
  744. at_anim_pos = MAX(anim->get_length(), time - pos); //seek to end
  745. }
  746. if (player->is_playing() || seeked) {
  747. player->play(anim_name);
  748. player->seek(at_anim_pos);
  749. t->playing = true;
  750. playing_caches.insert(t);
  751. } else {
  752. player->set_assigned_animation(anim_name);
  753. player->seek(at_anim_pos, true);
  754. }
  755. } else {
  756. //find stuff to play
  757. List<int> to_play;
  758. a->track_get_key_indices_in_range(i, time, delta, &to_play);
  759. if (to_play.size()) {
  760. int idx = to_play.back()->get();
  761. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  762. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  763. if (playing_caches.has(t)) {
  764. playing_caches.erase(t);
  765. player->stop();
  766. t->playing = false;
  767. }
  768. } else {
  769. player->play(anim_name);
  770. t->playing = true;
  771. playing_caches.insert(t);
  772. }
  773. }
  774. }
  775. } break;
  776. }
  777. }
  778. }
  779. }
  780. {
  781. // finally, set the tracks
  782. const NodePath *K = NULL;
  783. while ((K = track_cache.next(K))) {
  784. TrackCache *track = track_cache[*K];
  785. if (track->process_pass != process_pass)
  786. continue; //not processed, ignore
  787. switch (track->type) {
  788. case Animation::TYPE_TRANSFORM: {
  789. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  790. Transform xform;
  791. xform.origin = t->loc;
  792. t->scale += Vector3(1.0, 1.0, 1.0); //helps make it work properly with Add nodes
  793. xform.basis.set_quat_scale(t->rot, t->scale);
  794. if (t->skeleton && t->bone_idx >= 0) {
  795. t->skeleton->set_bone_pose(t->bone_idx, xform);
  796. } else {
  797. t->spatial->set_transform(xform);
  798. }
  799. } break;
  800. case Animation::TYPE_VALUE: {
  801. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  802. t->object->set_indexed(t->subpath, t->value);
  803. } break;
  804. case Animation::TYPE_BEZIER: {
  805. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  806. t->object->set_indexed(t->subpath, t->value);
  807. } break;
  808. default: {} //the rest dont matter
  809. }
  810. }
  811. }
  812. }
  813. void AnimationGraphPlayer::_notification(int p_what) {
  814. if (active && p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS && process_mode == ANIMATION_PROCESS_PHYSICS) {
  815. _process_graph(get_physics_process_delta_time());
  816. }
  817. if (active && p_what == NOTIFICATION_INTERNAL_PROCESS && process_mode == ANIMATION_PROCESS_IDLE) {
  818. _process_graph(get_process_delta_time());
  819. }
  820. if (p_what == NOTIFICATION_EXIT_TREE) {
  821. _clear_caches();
  822. }
  823. }
  824. void AnimationGraphPlayer::set_animation_player(const NodePath &p_player) {
  825. animation_player = p_player;
  826. update_configuration_warning();
  827. }
  828. NodePath AnimationGraphPlayer::get_animation_player() const {
  829. return animation_player;
  830. }
  831. bool AnimationGraphPlayer::is_state_invalid() const {
  832. return !state.valid;
  833. }
  834. String AnimationGraphPlayer::get_invalid_state_reason() const {
  835. return state.invalid_reasons;
  836. }
  837. uint64_t AnimationGraphPlayer::get_last_process_pass() const {
  838. return process_pass;
  839. }
  840. String AnimationGraphPlayer::get_configuration_warning() const {
  841. String warning = Node::get_configuration_warning();
  842. if (!root.is_valid()) {
  843. if (warning != String()) {
  844. warning += "\n";
  845. }
  846. warning += TTR("A root AnimationNode for the graph is not set.");
  847. }
  848. if (!has_node(animation_player)) {
  849. if (warning != String()) {
  850. warning += "\n";
  851. }
  852. warning += TTR("Path to an AnimationPlayer node containing animations is not set.");
  853. return warning;
  854. }
  855. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  856. if (!player) {
  857. if (warning != String()) {
  858. warning += "\n";
  859. }
  860. warning += TTR("Path set for AnimationPlayer does not lead to an AnimationPlayer node.");
  861. return warning;
  862. }
  863. if (!player->has_node(player->get_root())) {
  864. if (warning != String()) {
  865. warning += "\n";
  866. }
  867. warning += TTR("AnimationPlayer root is not a valid node.");
  868. return warning;
  869. }
  870. return warning;
  871. }
  872. void AnimationGraphPlayer::_bind_methods() {
  873. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationGraphPlayer::set_active);
  874. ClassDB::bind_method(D_METHOD("is_active"), &AnimationGraphPlayer::is_active);
  875. ClassDB::bind_method(D_METHOD("set_graph_root", "root"), &AnimationGraphPlayer::set_graph_root);
  876. ClassDB::bind_method(D_METHOD("get_graph_root"), &AnimationGraphPlayer::get_graph_root);
  877. ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &AnimationGraphPlayer::set_process_mode);
  878. ClassDB::bind_method(D_METHOD("get_process_mode"), &AnimationGraphPlayer::get_process_mode);
  879. ClassDB::bind_method(D_METHOD("set_animation_player", "root"), &AnimationGraphPlayer::set_animation_player);
  880. ClassDB::bind_method(D_METHOD("get_animation_player"), &AnimationGraphPlayer::get_animation_player);
  881. ClassDB::bind_method(D_METHOD("_node_removed"), &AnimationGraphPlayer::_node_removed);
  882. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "graph_root", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE), "set_graph_root", "get_graph_root");
  883. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "anim_player"), "set_animation_player", "get_animation_player");
  884. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  885. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
  886. }
  887. AnimationGraphPlayer::AnimationGraphPlayer() {
  888. process_mode = ANIMATION_PROCESS_IDLE;
  889. active = false;
  890. cache_valid = false;
  891. setup_pass = 1;
  892. started = true;
  893. }
  894. AnimationGraphPlayer::~AnimationGraphPlayer() {
  895. if (root.is_valid()) {
  896. root->player = NULL;
  897. }
  898. }