animation_tree.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /*************************************************************************/
  2. /* animation_tree.cpp */
  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. #include "animation_tree.h"
  31. #include "animation_blend_tree.h"
  32. #include "core/config/engine.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/audio/audio_stream.h"
  35. void AnimationNode::get_parameter_list(List<PropertyInfo> *r_list) const {
  36. Array parameters;
  37. if (GDVIRTUAL_CALL(_get_parameter_list, parameters)) {
  38. for (int i = 0; i < parameters.size(); i++) {
  39. Dictionary d = parameters[i];
  40. ERR_CONTINUE(d.is_empty());
  41. r_list->push_back(PropertyInfo::from_dict(d));
  42. }
  43. }
  44. }
  45. Variant AnimationNode::get_parameter_default_value(const StringName &p_parameter) const {
  46. Variant ret;
  47. if (GDVIRTUAL_CALL(_get_parameter_default_value, p_parameter, ret)) {
  48. return ret;
  49. }
  50. return Variant();
  51. }
  52. void AnimationNode::set_parameter(const StringName &p_name, const Variant &p_value) {
  53. ERR_FAIL_COND(!state);
  54. ERR_FAIL_COND(!state->tree->property_parent_map.has(base_path));
  55. ERR_FAIL_COND(!state->tree->property_parent_map[base_path].has(p_name));
  56. StringName path = state->tree->property_parent_map[base_path][p_name];
  57. state->tree->property_map[path] = p_value;
  58. }
  59. Variant AnimationNode::get_parameter(const StringName &p_name) const {
  60. ERR_FAIL_COND_V(!state, Variant());
  61. ERR_FAIL_COND_V(!state->tree->property_parent_map.has(base_path), Variant());
  62. ERR_FAIL_COND_V(!state->tree->property_parent_map[base_path].has(p_name), Variant());
  63. StringName path = state->tree->property_parent_map[base_path][p_name];
  64. return state->tree->property_map[path];
  65. }
  66. void AnimationNode::get_child_nodes(List<ChildNode> *r_child_nodes) {
  67. Dictionary cn;
  68. if (GDVIRTUAL_CALL(_get_child_nodes, cn)) {
  69. List<Variant> keys;
  70. cn.get_key_list(&keys);
  71. for (const Variant &E : keys) {
  72. ChildNode child;
  73. child.name = E;
  74. child.node = cn[E];
  75. r_child_nodes->push_back(child);
  76. }
  77. }
  78. }
  79. void AnimationNode::blend_animation(const StringName &p_animation, real_t p_time, real_t p_delta, bool p_seeked, real_t p_blend) {
  80. ERR_FAIL_COND(!state);
  81. ERR_FAIL_COND(!state->player->has_animation(p_animation));
  82. Ref<Animation> animation = state->player->get_animation(p_animation);
  83. if (animation.is_null()) {
  84. AnimationNodeBlendTree *btree = Object::cast_to<AnimationNodeBlendTree>(parent);
  85. if (btree) {
  86. String name = btree->get_node_name(Ref<AnimationNodeAnimation>(this));
  87. make_invalid(vformat(RTR("In node '%s', invalid animation: '%s'."), name, p_animation));
  88. } else {
  89. make_invalid(vformat(RTR("Invalid animation: '%s'."), p_animation));
  90. }
  91. return;
  92. }
  93. ERR_FAIL_COND(!animation.is_valid());
  94. AnimationState anim_state;
  95. anim_state.blend = p_blend;
  96. anim_state.track_blends = &blends;
  97. anim_state.delta = p_delta;
  98. anim_state.time = p_time;
  99. anim_state.animation = animation;
  100. anim_state.seeked = p_seeked;
  101. state->animation_states.push_back(anim_state);
  102. }
  103. real_t AnimationNode::_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) {
  104. base_path = p_base_path;
  105. parent = p_parent;
  106. connections = p_connections;
  107. state = p_state;
  108. real_t t = process(p_time, p_seek);
  109. state = nullptr;
  110. parent = nullptr;
  111. base_path = StringName();
  112. connections.clear();
  113. return t;
  114. }
  115. void AnimationNode::make_invalid(const String &p_reason) {
  116. ERR_FAIL_COND(!state);
  117. state->valid = false;
  118. if (state->invalid_reasons != String()) {
  119. state->invalid_reasons += "\n";
  120. }
  121. state->invalid_reasons += String::utf8("• ") + p_reason;
  122. }
  123. real_t AnimationNode::blend_input(int p_input, real_t p_time, bool p_seek, real_t p_blend, FilterAction p_filter, bool p_optimize) {
  124. ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
  125. ERR_FAIL_COND_V(!state, 0);
  126. AnimationNodeBlendTree *blend_tree = Object::cast_to<AnimationNodeBlendTree>(parent);
  127. ERR_FAIL_COND_V(!blend_tree, 0);
  128. StringName node_name = connections[p_input];
  129. if (!blend_tree->has_node(node_name)) {
  130. String name = blend_tree->get_node_name(Ref<AnimationNode>(this));
  131. make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), name));
  132. return 0;
  133. }
  134. Ref<AnimationNode> node = blend_tree->get_node(node_name);
  135. //inputs.write[p_input].last_pass = state->last_pass;
  136. real_t activity = 0.0;
  137. real_t ret = _blend_node(node_name, blend_tree->get_node_connection_array(node_name), nullptr, node, p_time, p_seek, p_blend, p_filter, p_optimize, &activity);
  138. Vector<AnimationTree::Activity> *activity_ptr = state->tree->input_activity_map.getptr(base_path);
  139. if (activity_ptr && p_input < activity_ptr->size()) {
  140. activity_ptr->write[p_input].last_pass = state->last_pass;
  141. activity_ptr->write[p_input].activity = activity;
  142. }
  143. return ret;
  144. }
  145. real_t AnimationNode::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, bool p_optimize) {
  146. return _blend_node(p_sub_path, Vector<StringName>(), this, p_node, p_time, p_seek, p_blend, p_filter, p_optimize);
  147. }
  148. real_t AnimationNode::_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, bool p_optimize, real_t *r_max) {
  149. ERR_FAIL_COND_V(!p_node.is_valid(), 0);
  150. ERR_FAIL_COND_V(!state, 0);
  151. int blend_count = blends.size();
  152. if (p_node->blends.size() != blend_count) {
  153. p_node->blends.resize(blend_count);
  154. }
  155. real_t *blendw = p_node->blends.ptrw();
  156. const real_t *blendr = blends.ptr();
  157. bool any_valid = false;
  158. if (has_filter() && is_filter_enabled() && p_filter != FILTER_IGNORE) {
  159. for (int i = 0; i < blend_count; i++) {
  160. blendw[i] = 0.0; //all to zero by default
  161. }
  162. const NodePath *K = nullptr;
  163. while ((K = filter.next(K))) {
  164. if (!state->track_map.has(*K)) {
  165. continue;
  166. }
  167. int idx = state->track_map[*K];
  168. blendw[idx] = 1.0; //filtered goes to one
  169. }
  170. switch (p_filter) {
  171. case FILTER_IGNORE:
  172. break; //will not happen anyway
  173. case FILTER_PASS: {
  174. //values filtered pass, the rest don't
  175. for (int i = 0; i < blend_count; i++) {
  176. if (blendw[i] == 0) { //not filtered, does not pass
  177. continue;
  178. }
  179. blendw[i] = blendr[i] * p_blend;
  180. if (blendw[i] > CMP_EPSILON) {
  181. any_valid = true;
  182. }
  183. }
  184. } break;
  185. case FILTER_STOP: {
  186. //values filtered don't pass, the rest are blended
  187. for (int i = 0; i < blend_count; i++) {
  188. if (blendw[i] > 0) { //filtered, does not pass
  189. continue;
  190. }
  191. blendw[i] = blendr[i] * p_blend;
  192. if (blendw[i] > CMP_EPSILON) {
  193. any_valid = true;
  194. }
  195. }
  196. } break;
  197. case FILTER_BLEND: {
  198. //filtered values are blended, the rest are passed without blending
  199. for (int i = 0; i < blend_count; i++) {
  200. if (blendw[i] == 1.0) {
  201. blendw[i] = blendr[i] * p_blend; //filtered, blend
  202. } else {
  203. blendw[i] = blendr[i]; //not filtered, do not blend
  204. }
  205. if (blendw[i] > CMP_EPSILON) {
  206. any_valid = true;
  207. }
  208. }
  209. } break;
  210. }
  211. } else {
  212. for (int i = 0; i < blend_count; i++) {
  213. //regular blend
  214. blendw[i] = blendr[i] * p_blend;
  215. if (blendw[i] > CMP_EPSILON) {
  216. any_valid = true;
  217. }
  218. }
  219. }
  220. if (r_max) {
  221. *r_max = 0;
  222. for (int i = 0; i < blend_count; i++) {
  223. *r_max = MAX(*r_max, blendw[i]);
  224. }
  225. }
  226. if (!p_seek && p_optimize && !any_valid) { //pointless to go on, all are zero
  227. return 0;
  228. }
  229. String new_path;
  230. AnimationNode *new_parent;
  231. //this is the slowest part of processing, but as strings process in powers of 2, and the paths always exist, it will not result in that many allocations
  232. if (p_new_parent) {
  233. new_parent = p_new_parent;
  234. new_path = String(base_path) + String(p_subpath) + "/";
  235. } else {
  236. ERR_FAIL_COND_V(!parent, 0);
  237. new_parent = parent;
  238. new_path = String(parent->base_path) + String(p_subpath) + "/";
  239. }
  240. return p_node->_pre_process(new_path, new_parent, state, p_time, p_seek, p_connections);
  241. }
  242. int AnimationNode::get_input_count() const {
  243. return inputs.size();
  244. }
  245. String AnimationNode::get_input_name(int p_input) {
  246. ERR_FAIL_INDEX_V(p_input, inputs.size(), String());
  247. return inputs[p_input].name;
  248. }
  249. String AnimationNode::get_caption() const {
  250. String ret;
  251. if (GDVIRTUAL_CALL(_get_caption, ret)) {
  252. return ret;
  253. }
  254. return "Node";
  255. }
  256. void AnimationNode::add_input(const String &p_name) {
  257. //root nodes can't add inputs
  258. ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != nullptr);
  259. Input input;
  260. ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1);
  261. input.name = p_name;
  262. inputs.push_back(input);
  263. emit_changed();
  264. }
  265. void AnimationNode::set_input_name(int p_input, const String &p_name) {
  266. ERR_FAIL_INDEX(p_input, inputs.size());
  267. ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1);
  268. inputs.write[p_input].name = p_name;
  269. emit_changed();
  270. }
  271. void AnimationNode::remove_input(int p_index) {
  272. ERR_FAIL_INDEX(p_index, inputs.size());
  273. inputs.remove(p_index);
  274. emit_changed();
  275. }
  276. double AnimationNode::process(double p_time, bool p_seek) {
  277. double ret;
  278. if (GDVIRTUAL_CALL(_process, p_time, p_seek, ret)) {
  279. return ret;
  280. }
  281. return 0;
  282. }
  283. void AnimationNode::set_filter_path(const NodePath &p_path, bool p_enable) {
  284. if (p_enable) {
  285. filter[p_path] = true;
  286. } else {
  287. filter.erase(p_path);
  288. }
  289. }
  290. void AnimationNode::set_filter_enabled(bool p_enable) {
  291. filter_enabled = p_enable;
  292. }
  293. bool AnimationNode::is_filter_enabled() const {
  294. return filter_enabled;
  295. }
  296. bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
  297. return filter.has(p_path);
  298. }
  299. bool AnimationNode::has_filter() const {
  300. bool ret;
  301. if (GDVIRTUAL_CALL(_has_filter, ret)) {
  302. return ret;
  303. }
  304. return false;
  305. }
  306. Array AnimationNode::_get_filters() const {
  307. Array paths;
  308. const NodePath *K = nullptr;
  309. while ((K = filter.next(K))) {
  310. paths.push_back(String(*K)); //use strings, so sorting is possible
  311. }
  312. paths.sort(); //done so every time the scene is saved, it does not change
  313. return paths;
  314. }
  315. void AnimationNode::_set_filters(const Array &p_filters) {
  316. filter.clear();
  317. for (int i = 0; i < p_filters.size(); i++) {
  318. set_filter_path(p_filters[i], true);
  319. }
  320. }
  321. void AnimationNode::_validate_property(PropertyInfo &property) const {
  322. if (!has_filter() && (property.name == "filter_enabled" || property.name == "filters")) {
  323. property.usage = PROPERTY_USAGE_NONE;
  324. }
  325. }
  326. Ref<AnimationNode> AnimationNode::get_child_by_name(const StringName &p_name) {
  327. Ref<AnimationNode> ret;
  328. if (GDVIRTUAL_CALL(_get_child_by_name, p_name, ret)) {
  329. return ret;
  330. }
  331. return Ref<AnimationNode>();
  332. }
  333. void AnimationNode::_bind_methods() {
  334. ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
  335. ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
  336. ClassDB::bind_method(D_METHOD("add_input", "name"), &AnimationNode::add_input);
  337. ClassDB::bind_method(D_METHOD("remove_input", "index"), &AnimationNode::remove_input);
  338. ClassDB::bind_method(D_METHOD("set_filter_path", "path", "enable"), &AnimationNode::set_filter_path);
  339. ClassDB::bind_method(D_METHOD("is_path_filtered", "path"), &AnimationNode::is_path_filtered);
  340. ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
  341. ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
  342. ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
  343. ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
  344. ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "blend"), &AnimationNode::blend_animation);
  345. ClassDB::bind_method(D_METHOD("blend_node", "name", "node", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_node, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  346. ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  347. ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
  348. ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);
  349. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_filter_enabled", "is_filter_enabled");
  350. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");
  351. GDVIRTUAL_BIND(_get_child_nodes);
  352. GDVIRTUAL_BIND(_get_parameter_list);
  353. GDVIRTUAL_BIND(_get_child_by_name, "name");
  354. GDVIRTUAL_BIND(_get_parameter_default_value, "parameter");
  355. GDVIRTUAL_BIND(_process, "time", "seek");
  356. GDVIRTUAL_BIND(_get_caption);
  357. GDVIRTUAL_BIND(_has_filter);
  358. ADD_SIGNAL(MethodInfo("removed_from_graph"));
  359. ADD_SIGNAL(MethodInfo("tree_changed"));
  360. BIND_ENUM_CONSTANT(FILTER_IGNORE);
  361. BIND_ENUM_CONSTANT(FILTER_PASS);
  362. BIND_ENUM_CONSTANT(FILTER_STOP);
  363. BIND_ENUM_CONSTANT(FILTER_BLEND);
  364. }
  365. AnimationNode::AnimationNode() {
  366. }
  367. ////////////////////
  368. void AnimationTree::set_tree_root(const Ref<AnimationNode> &p_root) {
  369. if (root.is_valid()) {
  370. root->disconnect("tree_changed", callable_mp(this, &AnimationTree::_tree_changed));
  371. }
  372. root = p_root;
  373. if (root.is_valid()) {
  374. root->connect("tree_changed", callable_mp(this, &AnimationTree::_tree_changed));
  375. }
  376. properties_dirty = true;
  377. update_configuration_warnings();
  378. }
  379. Ref<AnimationNode> AnimationTree::get_tree_root() const {
  380. return root;
  381. }
  382. void AnimationTree::set_active(bool p_active) {
  383. if (active == p_active) {
  384. return;
  385. }
  386. active = p_active;
  387. started = active;
  388. if (process_callback == ANIMATION_PROCESS_IDLE) {
  389. set_process_internal(active);
  390. } else {
  391. set_physics_process_internal(active);
  392. }
  393. if (!active && is_inside_tree()) {
  394. for (Set<TrackCache *>::Element *E = playing_caches.front(); E; E = E->next()) {
  395. if (ObjectDB::get_instance(E->get()->object_id)) {
  396. E->get()->object->call("stop");
  397. }
  398. }
  399. playing_caches.clear();
  400. }
  401. }
  402. bool AnimationTree::is_active() const {
  403. return active;
  404. }
  405. void AnimationTree::set_process_callback(AnimationProcessCallback p_mode) {
  406. if (process_callback == p_mode) {
  407. return;
  408. }
  409. bool was_active = is_active();
  410. if (was_active) {
  411. set_active(false);
  412. }
  413. process_callback = p_mode;
  414. if (was_active) {
  415. set_active(true);
  416. }
  417. }
  418. AnimationTree::AnimationProcessCallback AnimationTree::get_process_callback() const {
  419. return process_callback;
  420. }
  421. void AnimationTree::_node_removed(Node *p_node) {
  422. cache_valid = false;
  423. }
  424. bool AnimationTree::_update_caches(AnimationPlayer *player) {
  425. setup_pass++;
  426. if (!player->has_node(player->get_root())) {
  427. ERR_PRINT("AnimationTree: AnimationPlayer root is invalid.");
  428. set_active(false);
  429. return false;
  430. }
  431. Node *parent = player->get_node(player->get_root());
  432. List<StringName> sname;
  433. player->get_animation_list(&sname);
  434. for (const StringName &E : sname) {
  435. Ref<Animation> anim = player->get_animation(E);
  436. for (int i = 0; i < anim->get_track_count(); i++) {
  437. NodePath path = anim->track_get_path(i);
  438. Animation::TrackType track_type = anim->track_get_type(i);
  439. Animation::TrackType track_cache_type = track_type;
  440. if (track_cache_type == Animation::TYPE_POSITION_3D || track_cache_type == Animation::TYPE_ROTATION_3D || track_cache_type == Animation::TYPE_SCALE_3D) {
  441. track_cache_type = Animation::TYPE_POSITION_3D; //reference them as position3D tracks, even if they modify rotation or scale
  442. }
  443. TrackCache *track = nullptr;
  444. if (track_cache.has(path)) {
  445. track = track_cache.get(path);
  446. }
  447. //if not valid, delete track
  448. if (track && (track->type != track_cache_type || ObjectDB::get_instance(track->object_id) == nullptr)) {
  449. playing_caches.erase(track);
  450. memdelete(track);
  451. track_cache.erase(path);
  452. track = nullptr;
  453. }
  454. if (!track) {
  455. RES resource;
  456. Vector<StringName> leftover_path;
  457. Node *child = parent->get_node_and_resource(path, resource, leftover_path);
  458. if (!child) {
  459. ERR_PRINT("AnimationTree: '" + String(E) + "', couldn't resolve track: '" + String(path) + "'");
  460. continue;
  461. }
  462. if (!child->is_connected("tree_exited", callable_mp(this, &AnimationTree::_node_removed))) {
  463. child->connect("tree_exited", callable_mp(this, &AnimationTree::_node_removed), varray(child));
  464. }
  465. switch (track_type) {
  466. case Animation::TYPE_VALUE: {
  467. TrackCacheValue *track_value = memnew(TrackCacheValue);
  468. if (resource.is_valid()) {
  469. track_value->object = resource.ptr();
  470. } else {
  471. track_value->object = child;
  472. }
  473. track_value->subpath = leftover_path;
  474. track_value->object_id = track_value->object->get_instance_id();
  475. track = track_value;
  476. } break;
  477. case Animation::TYPE_POSITION_3D:
  478. case Animation::TYPE_ROTATION_3D:
  479. case Animation::TYPE_SCALE_3D: {
  480. #ifndef _3D_DISABLED
  481. Node3D *node_3d = Object::cast_to<Node3D>(child);
  482. if (!node_3d) {
  483. ERR_PRINT("AnimationTree: '" + String(E) + "', transform track does not point to Node3D: '" + String(path) + "'");
  484. continue;
  485. }
  486. TrackCacheTransform *track_xform = memnew(TrackCacheTransform);
  487. track_xform->type = Animation::TYPE_POSITION_3D;
  488. track_xform->node_3d = node_3d;
  489. track_xform->skeleton = nullptr;
  490. track_xform->bone_idx = -1;
  491. if (path.get_subname_count() == 1 && Object::cast_to<Skeleton3D>(node_3d)) {
  492. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node_3d);
  493. track_xform->skeleton = sk;
  494. int bone_idx = sk->find_bone(path.get_subname(0));
  495. if (bone_idx != -1) {
  496. track_xform->bone_idx = bone_idx;
  497. }
  498. }
  499. track_xform->object = node_3d;
  500. track_xform->object_id = track_xform->object->get_instance_id();
  501. track = track_xform;
  502. switch (track_type) {
  503. case Animation::TYPE_POSITION_3D: {
  504. track_xform->loc_used = true;
  505. } break;
  506. case Animation::TYPE_ROTATION_3D: {
  507. track_xform->rot_used = true;
  508. } break;
  509. case Animation::TYPE_SCALE_3D: {
  510. track_xform->scale_used = true;
  511. } break;
  512. default: {
  513. }
  514. }
  515. #endif // _3D_DISABLED
  516. } break;
  517. case Animation::TYPE_BLEND_SHAPE: {
  518. #ifndef _3D_DISABLED
  519. if (path.get_subname_count() != 1) {
  520. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not contain a blend shape subname: '" + String(path) + "'");
  521. continue;
  522. }
  523. MeshInstance3D *mesh_3d = Object::cast_to<MeshInstance3D>(child);
  524. if (!mesh_3d) {
  525. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not point to MeshInstance3D: '" + String(path) + "'");
  526. continue;
  527. }
  528. StringName blend_shape_name = path.get_subname(0);
  529. int blend_shape_idx = mesh_3d->find_blend_shape_by_name(blend_shape_name);
  530. if (blend_shape_idx == -1) {
  531. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track points to a non-existing name: '" + String(blend_shape_name) + "'");
  532. continue;
  533. }
  534. TrackCacheBlendShape *track_bshape = memnew(TrackCacheBlendShape);
  535. track_bshape->mesh_3d = mesh_3d;
  536. track_bshape->shape_index = blend_shape_idx;
  537. track_bshape->object = mesh_3d;
  538. track_bshape->object_id = mesh_3d->get_instance_id();
  539. track = track_bshape;
  540. #endif
  541. } break;
  542. case Animation::TYPE_METHOD: {
  543. TrackCacheMethod *track_method = memnew(TrackCacheMethod);
  544. if (resource.is_valid()) {
  545. track_method->object = resource.ptr();
  546. } else {
  547. track_method->object = child;
  548. }
  549. track_method->object_id = track_method->object->get_instance_id();
  550. track = track_method;
  551. } break;
  552. case Animation::TYPE_BEZIER: {
  553. TrackCacheBezier *track_bezier = memnew(TrackCacheBezier);
  554. if (resource.is_valid()) {
  555. track_bezier->object = resource.ptr();
  556. } else {
  557. track_bezier->object = child;
  558. }
  559. track_bezier->subpath = leftover_path;
  560. track_bezier->object_id = track_bezier->object->get_instance_id();
  561. track = track_bezier;
  562. } break;
  563. case Animation::TYPE_AUDIO: {
  564. TrackCacheAudio *track_audio = memnew(TrackCacheAudio);
  565. track_audio->object = child;
  566. track_audio->object_id = track_audio->object->get_instance_id();
  567. track = track_audio;
  568. } break;
  569. case Animation::TYPE_ANIMATION: {
  570. TrackCacheAnimation *track_animation = memnew(TrackCacheAnimation);
  571. track_animation->object = child;
  572. track_animation->object_id = track_animation->object->get_instance_id();
  573. track = track_animation;
  574. } break;
  575. default: {
  576. ERR_PRINT("Animation corrupted (invalid track type)");
  577. continue;
  578. }
  579. }
  580. track_cache[path] = track;
  581. } else if (track_cache_type == Animation::TYPE_POSITION_3D) {
  582. TrackCacheTransform *track_xform = static_cast<TrackCacheTransform *>(track);
  583. if (track->setup_pass != setup_pass) {
  584. track_xform->loc_used = false;
  585. track_xform->rot_used = false;
  586. track_xform->scale_used = false;
  587. }
  588. switch (track_type) {
  589. case Animation::TYPE_POSITION_3D: {
  590. track_xform->loc_used = true;
  591. } break;
  592. case Animation::TYPE_ROTATION_3D: {
  593. track_xform->rot_used = true;
  594. } break;
  595. case Animation::TYPE_SCALE_3D: {
  596. track_xform->scale_used = true;
  597. } break;
  598. default: {
  599. }
  600. }
  601. }
  602. track->setup_pass = setup_pass;
  603. }
  604. }
  605. List<NodePath> to_delete;
  606. const NodePath *K = nullptr;
  607. while ((K = track_cache.next(K))) {
  608. TrackCache *tc = track_cache[*K];
  609. if (tc->setup_pass != setup_pass) {
  610. to_delete.push_back(*K);
  611. }
  612. }
  613. while (to_delete.front()) {
  614. NodePath np = to_delete.front()->get();
  615. memdelete(track_cache[np]);
  616. track_cache.erase(np);
  617. to_delete.pop_front();
  618. }
  619. state.track_map.clear();
  620. K = nullptr;
  621. int idx = 0;
  622. while ((K = track_cache.next(K))) {
  623. state.track_map[*K] = idx;
  624. idx++;
  625. }
  626. state.track_count = idx;
  627. cache_valid = true;
  628. return true;
  629. }
  630. void AnimationTree::_clear_caches() {
  631. const NodePath *K = nullptr;
  632. while ((K = track_cache.next(K))) {
  633. memdelete(track_cache[*K]);
  634. }
  635. playing_caches.clear();
  636. track_cache.clear();
  637. cache_valid = false;
  638. }
  639. void AnimationTree::_process_graph(real_t p_delta) {
  640. _update_properties(); //if properties need updating, update them
  641. //check all tracks, see if they need modification
  642. root_motion_transform = Transform3D();
  643. if (!root.is_valid()) {
  644. ERR_PRINT("AnimationTree: root AnimationNode is not set, disabling playback.");
  645. set_active(false);
  646. cache_valid = false;
  647. return;
  648. }
  649. if (!has_node(animation_player)) {
  650. ERR_PRINT("AnimationTree: no valid AnimationPlayer path set, disabling playback");
  651. set_active(false);
  652. cache_valid = false;
  653. return;
  654. }
  655. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  656. ObjectID current_animation_player;
  657. if (player) {
  658. current_animation_player = player->get_instance_id();
  659. }
  660. if (last_animation_player != current_animation_player) {
  661. if (last_animation_player.is_valid()) {
  662. Object *old_player = ObjectDB::get_instance(last_animation_player);
  663. if (old_player) {
  664. old_player->disconnect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  665. }
  666. }
  667. if (player) {
  668. player->connect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  669. }
  670. last_animation_player = current_animation_player;
  671. }
  672. if (!player) {
  673. ERR_PRINT("AnimationTree: path points to a node not an AnimationPlayer, disabling playback");
  674. set_active(false);
  675. cache_valid = false;
  676. return;
  677. }
  678. if (!cache_valid) {
  679. if (!_update_caches(player)) {
  680. return;
  681. }
  682. }
  683. { //setup
  684. process_pass++;
  685. state.valid = true;
  686. state.invalid_reasons = "";
  687. state.animation_states.clear(); //will need to be re-created
  688. state.valid = true;
  689. state.player = player;
  690. state.last_pass = process_pass;
  691. state.tree = this;
  692. // root source blends
  693. root->blends.resize(state.track_count);
  694. real_t *src_blendsw = root->blends.ptrw();
  695. for (int i = 0; i < state.track_count; i++) {
  696. src_blendsw[i] = 1.0; //by default all go to 1 for the root input
  697. }
  698. }
  699. //process
  700. {
  701. if (started) {
  702. //if started, seek
  703. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, 0, true, Vector<StringName>());
  704. started = false;
  705. }
  706. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, p_delta, false, Vector<StringName>());
  707. }
  708. if (!state.valid) {
  709. return; //state is not valid. do nothing.
  710. }
  711. //apply value/transform/bezier blends to track caches and execute method/audio/animation tracks
  712. {
  713. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  714. for (const AnimationNode::AnimationState &as : state.animation_states) {
  715. Ref<Animation> a = as.animation;
  716. double time = as.time;
  717. double delta = as.delta;
  718. real_t weight = as.blend;
  719. bool seeked = as.seeked;
  720. for (int i = 0; i < a->get_track_count(); i++) {
  721. NodePath path = a->track_get_path(i);
  722. ERR_CONTINUE(!track_cache.has(path));
  723. TrackCache *track = track_cache[path];
  724. Animation::TrackType ttype = a->track_get_type(i);
  725. if (ttype != Animation::TYPE_POSITION_3D && ttype != Animation::TYPE_ROTATION_3D && ttype != Animation::TYPE_SCALE_3D && track->type != ttype) {
  726. //broken animation, but avoid error spamming
  727. continue;
  728. }
  729. track->root_motion = root_motion_track == path;
  730. ERR_CONTINUE(!state.track_map.has(path));
  731. int blend_idx = state.track_map[path];
  732. ERR_CONTINUE(blend_idx < 0 || blend_idx >= state.track_count);
  733. real_t blend = (*as.track_blends)[blend_idx] * weight;
  734. if (blend < CMP_EPSILON) {
  735. continue; //nothing to blend
  736. }
  737. switch (ttype) {
  738. case Animation::TYPE_POSITION_3D: {
  739. #ifndef _3D_DISABLED
  740. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  741. if (t->process_pass != process_pass) {
  742. t->process_pass = process_pass;
  743. t->loc = Vector3();
  744. t->rot = Quaternion();
  745. t->rot_blend_accum = 0;
  746. t->scale = Vector3(1, 1, 1);
  747. }
  748. if (track->root_motion) {
  749. real_t prev_time = time - delta;
  750. if (prev_time < 0) {
  751. if (!a->has_loop()) {
  752. prev_time = 0;
  753. } else {
  754. prev_time = a->get_length() + prev_time;
  755. }
  756. }
  757. Vector3 loc[2];
  758. if (prev_time > time) {
  759. Error err = a->position_track_interpolate(i, prev_time, &loc[0]);
  760. if (err != OK) {
  761. continue;
  762. }
  763. a->position_track_interpolate(i, a->get_length(), &loc[1]);
  764. t->loc += (loc[1] - loc[0]) * blend;
  765. prev_time = 0;
  766. }
  767. Error err = a->position_track_interpolate(i, prev_time, &loc[0]);
  768. if (err != OK) {
  769. continue;
  770. }
  771. a->position_track_interpolate(i, time, &loc[1]);
  772. t->loc += (loc[1] - loc[0]) * blend;
  773. prev_time = 0;
  774. } else {
  775. Vector3 loc;
  776. Error err = a->position_track_interpolate(i, time, &loc);
  777. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  778. if (err != OK) {
  779. continue;
  780. }
  781. t->loc = t->loc.lerp(loc, blend);
  782. }
  783. #endif // _3D_DISABLED
  784. } break;
  785. case Animation::TYPE_ROTATION_3D: {
  786. #ifndef _3D_DISABLED
  787. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  788. if (t->process_pass != process_pass) {
  789. t->process_pass = process_pass;
  790. t->loc = Vector3();
  791. t->rot = Quaternion();
  792. t->rot_blend_accum = 0;
  793. t->scale = Vector3(1, 1, 1);
  794. }
  795. if (track->root_motion) {
  796. real_t prev_time = time - delta;
  797. if (prev_time < 0) {
  798. if (!a->has_loop()) {
  799. prev_time = 0;
  800. } else {
  801. prev_time = a->get_length() + prev_time;
  802. }
  803. }
  804. Quaternion rot[2];
  805. if (prev_time > time) {
  806. Error err = a->rotation_track_interpolate(i, prev_time, &rot[0]);
  807. if (err != OK) {
  808. continue;
  809. }
  810. a->rotation_track_interpolate(i, a->get_length(), &rot[1]);
  811. Quaternion q = Quaternion().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
  812. t->rot = (t->rot * q).normalized();
  813. prev_time = 0;
  814. }
  815. Error err = a->rotation_track_interpolate(i, prev_time, &rot[0]);
  816. if (err != OK) {
  817. continue;
  818. }
  819. a->rotation_track_interpolate(i, time, &rot[1]);
  820. Quaternion q = Quaternion().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
  821. t->rot = (t->rot * q).normalized();
  822. prev_time = 0;
  823. } else {
  824. Quaternion rot;
  825. Error err = a->rotation_track_interpolate(i, time, &rot);
  826. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  827. if (err != OK) {
  828. continue;
  829. }
  830. if (t->rot_blend_accum == 0) {
  831. t->rot = rot;
  832. t->rot_blend_accum = blend;
  833. } else {
  834. real_t rot_total = t->rot_blend_accum + blend;
  835. t->rot = rot.slerp(t->rot, t->rot_blend_accum / rot_total).normalized();
  836. t->rot_blend_accum = rot_total;
  837. }
  838. }
  839. #endif // _3D_DISABLED
  840. } break;
  841. case Animation::TYPE_SCALE_3D: {
  842. #ifndef _3D_DISABLED
  843. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  844. if (t->process_pass != process_pass) {
  845. t->process_pass = process_pass;
  846. t->loc = Vector3();
  847. t->rot = Quaternion();
  848. t->rot_blend_accum = 0;
  849. t->scale = Vector3(1, 1, 1);
  850. }
  851. if (track->root_motion) {
  852. real_t prev_time = time - delta;
  853. if (prev_time < 0) {
  854. if (!a->has_loop()) {
  855. prev_time = 0;
  856. } else {
  857. prev_time = a->get_length() + prev_time;
  858. }
  859. }
  860. Vector3 scale[2];
  861. if (prev_time > time) {
  862. Error err = a->scale_track_interpolate(i, prev_time, &scale[0]);
  863. if (err != OK) {
  864. continue;
  865. }
  866. a->scale_track_interpolate(i, a->get_length(), &scale[1]);
  867. t->scale += (scale[1] - scale[0]) * blend;
  868. prev_time = 0;
  869. }
  870. Error err = a->scale_track_interpolate(i, prev_time, &scale[0]);
  871. if (err != OK) {
  872. continue;
  873. }
  874. a->scale_track_interpolate(i, time, &scale[1]);
  875. t->scale += (scale[1] - scale[0]) * blend;
  876. prev_time = 0;
  877. } else {
  878. Vector3 scale;
  879. Error err = a->scale_track_interpolate(i, time, &scale);
  880. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  881. if (err != OK) {
  882. continue;
  883. }
  884. t->scale = t->scale.lerp(scale, blend);
  885. }
  886. #endif // _3D_DISABLED
  887. } break;
  888. case Animation::TYPE_BLEND_SHAPE: {
  889. #ifndef _3D_DISABLED
  890. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  891. if (t->process_pass != process_pass) {
  892. t->process_pass = process_pass;
  893. t->value = 0;
  894. }
  895. float value;
  896. Error err = a->blend_shape_track_interpolate(i, time, &value);
  897. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  898. if (err != OK) {
  899. continue;
  900. }
  901. t->value = Math::lerp(t->value, value, blend);
  902. #endif // _3D_DISABLED
  903. } break;
  904. case Animation::TYPE_VALUE: {
  905. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  906. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  907. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) { //delta == 0 means seek
  908. Variant value = a->value_track_interpolate(i, time);
  909. if (value == Variant()) {
  910. continue;
  911. }
  912. if (t->process_pass != process_pass) {
  913. t->value = value;
  914. t->process_pass = process_pass;
  915. }
  916. Variant::interpolate(t->value, value, blend, t->value);
  917. } else {
  918. List<int> indices;
  919. a->value_track_get_key_indices(i, time, delta, &indices);
  920. for (int &F : indices) {
  921. Variant value = a->track_get_key_value(i, F);
  922. t->object->set_indexed(t->subpath, value);
  923. }
  924. }
  925. } break;
  926. case Animation::TYPE_METHOD: {
  927. if (delta == 0) {
  928. continue;
  929. }
  930. TrackCacheMethod *t = static_cast<TrackCacheMethod *>(track);
  931. List<int> indices;
  932. a->method_track_get_key_indices(i, time, delta, &indices);
  933. for (int &F : indices) {
  934. StringName method = a->method_track_get_name(i, F);
  935. Vector<Variant> params = a->method_track_get_params(i, F);
  936. int s = params.size();
  937. static_assert(VARIANT_ARG_MAX == 8, "This code needs to be updated if VARIANT_ARG_MAX != 8");
  938. ERR_CONTINUE(s > VARIANT_ARG_MAX);
  939. if (can_call) {
  940. t->object->call_deferred(
  941. method,
  942. s >= 1 ? params[0] : Variant(),
  943. s >= 2 ? params[1] : Variant(),
  944. s >= 3 ? params[2] : Variant(),
  945. s >= 4 ? params[3] : Variant(),
  946. s >= 5 ? params[4] : Variant(),
  947. s >= 6 ? params[5] : Variant(),
  948. s >= 7 ? params[6] : Variant(),
  949. s >= 8 ? params[7] : Variant());
  950. }
  951. }
  952. } break;
  953. case Animation::TYPE_BEZIER: {
  954. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  955. real_t bezier = a->bezier_track_interpolate(i, time);
  956. if (t->process_pass != process_pass) {
  957. t->value = bezier;
  958. t->process_pass = process_pass;
  959. }
  960. t->value = Math::lerp(t->value, bezier, blend);
  961. } break;
  962. case Animation::TYPE_AUDIO: {
  963. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  964. if (seeked) {
  965. //find whatever should be playing
  966. int idx = a->track_find_key(i, time);
  967. if (idx < 0) {
  968. continue;
  969. }
  970. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  971. if (!stream.is_valid()) {
  972. t->object->call("stop");
  973. t->playing = false;
  974. playing_caches.erase(t);
  975. } else {
  976. real_t start_ofs = a->audio_track_get_key_start_offset(i, idx);
  977. start_ofs += time - a->track_get_key_time(i, idx);
  978. real_t end_ofs = a->audio_track_get_key_end_offset(i, idx);
  979. real_t len = stream->get_length();
  980. if (start_ofs > len - end_ofs) {
  981. t->object->call("stop");
  982. t->playing = false;
  983. playing_caches.erase(t);
  984. continue;
  985. }
  986. t->object->call("set_stream", stream);
  987. t->object->call("play", start_ofs);
  988. t->playing = true;
  989. playing_caches.insert(t);
  990. if (len && end_ofs > 0) { //force an end at a time
  991. t->len = len - start_ofs - end_ofs;
  992. } else {
  993. t->len = 0;
  994. }
  995. t->start = time;
  996. }
  997. } else {
  998. //find stuff to play
  999. List<int> to_play;
  1000. a->track_get_key_indices_in_range(i, time, delta, &to_play);
  1001. if (to_play.size()) {
  1002. int idx = to_play.back()->get();
  1003. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  1004. if (!stream.is_valid()) {
  1005. t->object->call("stop");
  1006. t->playing = false;
  1007. playing_caches.erase(t);
  1008. } else {
  1009. real_t start_ofs = a->audio_track_get_key_start_offset(i, idx);
  1010. real_t end_ofs = a->audio_track_get_key_end_offset(i, idx);
  1011. real_t len = stream->get_length();
  1012. t->object->call("set_stream", stream);
  1013. t->object->call("play", start_ofs);
  1014. t->playing = true;
  1015. playing_caches.insert(t);
  1016. if (len && end_ofs > 0) { //force an end at a time
  1017. t->len = len - start_ofs - end_ofs;
  1018. } else {
  1019. t->len = 0;
  1020. }
  1021. t->start = time;
  1022. }
  1023. } else if (t->playing) {
  1024. bool loop = a->has_loop();
  1025. bool stop = false;
  1026. if (!loop && time < t->start) {
  1027. stop = true;
  1028. } else if (t->len > 0) {
  1029. real_t len = t->start > time ? (a->get_length() - t->start) + time : time - t->start;
  1030. if (len > t->len) {
  1031. stop = true;
  1032. }
  1033. }
  1034. if (stop) {
  1035. //time to stop
  1036. t->object->call("stop");
  1037. t->playing = false;
  1038. playing_caches.erase(t);
  1039. }
  1040. }
  1041. }
  1042. real_t db = Math::linear2db(MAX(blend, 0.00001));
  1043. if (t->object->has_method("set_unit_db")) {
  1044. t->object->call("set_unit_db", db);
  1045. } else {
  1046. t->object->call("set_volume_db", db);
  1047. }
  1048. } break;
  1049. case Animation::TYPE_ANIMATION: {
  1050. TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track);
  1051. AnimationPlayer *player2 = Object::cast_to<AnimationPlayer>(t->object);
  1052. if (!player2) {
  1053. continue;
  1054. }
  1055. if (delta == 0 || seeked) {
  1056. //seek
  1057. int idx = a->track_find_key(i, time);
  1058. if (idx < 0) {
  1059. continue;
  1060. }
  1061. double pos = a->track_get_key_time(i, idx);
  1062. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  1063. if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) {
  1064. continue;
  1065. }
  1066. Ref<Animation> anim = player2->get_animation(anim_name);
  1067. real_t at_anim_pos;
  1068. if (anim->has_loop()) {
  1069. at_anim_pos = Math::fposmod(time - pos, (double)anim->get_length()); //seek to loop
  1070. } else {
  1071. at_anim_pos = MAX(anim->get_length(), time - pos); //seek to end
  1072. }
  1073. if (player2->is_playing() || seeked) {
  1074. player2->play(anim_name);
  1075. player2->seek(at_anim_pos);
  1076. t->playing = true;
  1077. playing_caches.insert(t);
  1078. } else {
  1079. player2->set_assigned_animation(anim_name);
  1080. player2->seek(at_anim_pos, true);
  1081. }
  1082. } else {
  1083. //find stuff to play
  1084. List<int> to_play;
  1085. a->track_get_key_indices_in_range(i, time, delta, &to_play);
  1086. if (to_play.size()) {
  1087. int idx = to_play.back()->get();
  1088. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  1089. if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) {
  1090. if (playing_caches.has(t)) {
  1091. playing_caches.erase(t);
  1092. player2->stop();
  1093. t->playing = false;
  1094. }
  1095. } else {
  1096. player2->play(anim_name);
  1097. t->playing = true;
  1098. playing_caches.insert(t);
  1099. }
  1100. }
  1101. }
  1102. } break;
  1103. }
  1104. }
  1105. }
  1106. }
  1107. {
  1108. // finally, set the tracks
  1109. const NodePath *K = nullptr;
  1110. while ((K = track_cache.next(K))) {
  1111. TrackCache *track = track_cache[*K];
  1112. if (track->process_pass != process_pass) {
  1113. continue; //not processed, ignore
  1114. }
  1115. switch (track->type) {
  1116. case Animation::TYPE_POSITION_3D: {
  1117. #ifndef _3D_DISABLED
  1118. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  1119. if (t->root_motion) {
  1120. Transform3D xform;
  1121. xform.origin = t->loc;
  1122. xform.basis.set_quaternion_scale(t->rot, t->scale);
  1123. root_motion_transform = xform;
  1124. } else if (t->skeleton && t->bone_idx >= 0) {
  1125. if (t->loc_used) {
  1126. t->skeleton->set_bone_pose_position(t->bone_idx, t->loc);
  1127. }
  1128. if (t->rot_used) {
  1129. t->skeleton->set_bone_pose_rotation(t->bone_idx, t->rot);
  1130. }
  1131. if (t->scale_used) {
  1132. t->skeleton->set_bone_pose_scale(t->bone_idx, t->scale);
  1133. }
  1134. } else if (!t->skeleton) {
  1135. if (t->loc_used) {
  1136. t->node_3d->set_position(t->loc);
  1137. }
  1138. if (t->rot_used) {
  1139. t->node_3d->set_rotation(t->rot.get_euler());
  1140. }
  1141. if (t->scale_used) {
  1142. t->node_3d->set_scale(t->scale);
  1143. }
  1144. }
  1145. #endif // _3D_DISABLED
  1146. } break;
  1147. case Animation::TYPE_BLEND_SHAPE: {
  1148. #ifndef _3D_DISABLED
  1149. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  1150. if (t->mesh_3d) {
  1151. t->mesh_3d->set_blend_shape_value(t->shape_index, t->value);
  1152. }
  1153. #endif // _3D_DISABLED
  1154. } break;
  1155. case Animation::TYPE_VALUE: {
  1156. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  1157. t->object->set_indexed(t->subpath, t->value);
  1158. } break;
  1159. case Animation::TYPE_BEZIER: {
  1160. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  1161. t->object->set_indexed(t->subpath, t->value);
  1162. } break;
  1163. default: {
  1164. } //the rest don't matter
  1165. }
  1166. }
  1167. }
  1168. }
  1169. void AnimationTree::advance(real_t p_time) {
  1170. _process_graph(p_time);
  1171. }
  1172. void AnimationTree::_notification(int p_what) {
  1173. if (active && p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS && process_callback == ANIMATION_PROCESS_PHYSICS) {
  1174. _process_graph(get_physics_process_delta_time());
  1175. }
  1176. if (active && p_what == NOTIFICATION_INTERNAL_PROCESS && process_callback == ANIMATION_PROCESS_IDLE) {
  1177. _process_graph(get_process_delta_time());
  1178. }
  1179. if (p_what == NOTIFICATION_EXIT_TREE) {
  1180. _clear_caches();
  1181. if (last_animation_player.is_valid()) {
  1182. Object *player = ObjectDB::get_instance(last_animation_player);
  1183. if (player) {
  1184. player->disconnect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  1185. }
  1186. }
  1187. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  1188. if (last_animation_player.is_valid()) {
  1189. Object *player = ObjectDB::get_instance(last_animation_player);
  1190. if (player) {
  1191. player->connect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  1192. }
  1193. }
  1194. }
  1195. }
  1196. void AnimationTree::set_animation_player(const NodePath &p_player) {
  1197. animation_player = p_player;
  1198. update_configuration_warnings();
  1199. }
  1200. NodePath AnimationTree::get_animation_player() const {
  1201. return animation_player;
  1202. }
  1203. bool AnimationTree::is_state_invalid() const {
  1204. return !state.valid;
  1205. }
  1206. String AnimationTree::get_invalid_state_reason() const {
  1207. return state.invalid_reasons;
  1208. }
  1209. uint64_t AnimationTree::get_last_process_pass() const {
  1210. return process_pass;
  1211. }
  1212. TypedArray<String> AnimationTree::get_configuration_warnings() const {
  1213. TypedArray<String> warnings = Node::get_configuration_warnings();
  1214. if (!root.is_valid()) {
  1215. warnings.push_back(TTR("No root AnimationNode for the graph is set."));
  1216. }
  1217. if (!has_node(animation_player)) {
  1218. warnings.push_back(TTR("Path to an AnimationPlayer node containing animations is not set."));
  1219. } else {
  1220. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  1221. if (!player) {
  1222. warnings.push_back(TTR("Path set for AnimationPlayer does not lead to an AnimationPlayer node."));
  1223. } else if (!player->has_node(player->get_root())) {
  1224. warnings.push_back(TTR("The AnimationPlayer root node is not a valid node."));
  1225. }
  1226. }
  1227. return warnings;
  1228. }
  1229. void AnimationTree::set_root_motion_track(const NodePath &p_track) {
  1230. root_motion_track = p_track;
  1231. }
  1232. NodePath AnimationTree::get_root_motion_track() const {
  1233. return root_motion_track;
  1234. }
  1235. Transform3D AnimationTree::get_root_motion_transform() const {
  1236. return root_motion_transform;
  1237. }
  1238. void AnimationTree::_tree_changed() {
  1239. if (properties_dirty) {
  1240. return;
  1241. }
  1242. call_deferred(SNAME("_update_properties"));
  1243. properties_dirty = true;
  1244. }
  1245. void AnimationTree::_update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node) {
  1246. ERR_FAIL_COND(node.is_null());
  1247. if (!property_parent_map.has(p_base_path)) {
  1248. property_parent_map[p_base_path] = HashMap<StringName, StringName>();
  1249. }
  1250. if (node->get_input_count() && !input_activity_map.has(p_base_path)) {
  1251. Vector<Activity> activity;
  1252. for (int i = 0; i < node->get_input_count(); i++) {
  1253. Activity a;
  1254. a.activity = 0;
  1255. a.last_pass = 0;
  1256. activity.push_back(a);
  1257. }
  1258. input_activity_map[p_base_path] = activity;
  1259. input_activity_map_get[String(p_base_path).substr(0, String(p_base_path).length() - 1)] = &input_activity_map[p_base_path];
  1260. }
  1261. List<PropertyInfo> plist;
  1262. node->get_parameter_list(&plist);
  1263. for (PropertyInfo &pinfo : plist) {
  1264. StringName key = pinfo.name;
  1265. if (!property_map.has(p_base_path + key)) {
  1266. property_map[p_base_path + key] = node->get_parameter_default_value(key);
  1267. }
  1268. property_parent_map[p_base_path][key] = p_base_path + key;
  1269. pinfo.name = p_base_path + key;
  1270. properties.push_back(pinfo);
  1271. }
  1272. List<AnimationNode::ChildNode> children;
  1273. node->get_child_nodes(&children);
  1274. for (const AnimationNode::ChildNode &E : children) {
  1275. _update_properties_for_node(p_base_path + E.name + "/", E.node);
  1276. }
  1277. }
  1278. void AnimationTree::_update_properties() {
  1279. if (!properties_dirty) {
  1280. return;
  1281. }
  1282. properties.clear();
  1283. property_parent_map.clear();
  1284. input_activity_map.clear();
  1285. input_activity_map_get.clear();
  1286. if (root.is_valid()) {
  1287. _update_properties_for_node(SceneStringNames::get_singleton()->parameters_base_path, root);
  1288. }
  1289. properties_dirty = false;
  1290. notify_property_list_changed();
  1291. }
  1292. bool AnimationTree::_set(const StringName &p_name, const Variant &p_value) {
  1293. if (properties_dirty) {
  1294. _update_properties();
  1295. }
  1296. if (property_map.has(p_name)) {
  1297. property_map[p_name] = p_value;
  1298. return true;
  1299. }
  1300. return false;
  1301. }
  1302. bool AnimationTree::_get(const StringName &p_name, Variant &r_ret) const {
  1303. if (properties_dirty) {
  1304. const_cast<AnimationTree *>(this)->_update_properties();
  1305. }
  1306. if (property_map.has(p_name)) {
  1307. r_ret = property_map[p_name];
  1308. return true;
  1309. }
  1310. return false;
  1311. }
  1312. void AnimationTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1313. if (properties_dirty) {
  1314. const_cast<AnimationTree *>(this)->_update_properties();
  1315. }
  1316. for (const PropertyInfo &E : properties) {
  1317. p_list->push_back(E);
  1318. }
  1319. }
  1320. void AnimationTree::rename_parameter(const String &p_base, const String &p_new_base) {
  1321. //rename values first
  1322. for (const PropertyInfo &E : properties) {
  1323. if (E.name.begins_with(p_base)) {
  1324. String new_name = E.name.replace_first(p_base, p_new_base);
  1325. property_map[new_name] = property_map[E.name];
  1326. }
  1327. }
  1328. //update tree second
  1329. properties_dirty = true;
  1330. _update_properties();
  1331. }
  1332. real_t AnimationTree::get_connection_activity(const StringName &p_path, int p_connection) const {
  1333. if (!input_activity_map_get.has(p_path)) {
  1334. return 0;
  1335. }
  1336. const Vector<Activity> *activity = input_activity_map_get[p_path];
  1337. if (!activity || p_connection < 0 || p_connection >= activity->size()) {
  1338. return 0;
  1339. }
  1340. if ((*activity)[p_connection].last_pass != process_pass) {
  1341. return 0;
  1342. }
  1343. return (*activity)[p_connection].activity;
  1344. }
  1345. void AnimationTree::_bind_methods() {
  1346. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationTree::set_active);
  1347. ClassDB::bind_method(D_METHOD("is_active"), &AnimationTree::is_active);
  1348. ClassDB::bind_method(D_METHOD("set_tree_root", "root"), &AnimationTree::set_tree_root);
  1349. ClassDB::bind_method(D_METHOD("get_tree_root"), &AnimationTree::get_tree_root);
  1350. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationTree::set_process_callback);
  1351. ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationTree::get_process_callback);
  1352. ClassDB::bind_method(D_METHOD("set_animation_player", "root"), &AnimationTree::set_animation_player);
  1353. ClassDB::bind_method(D_METHOD("get_animation_player"), &AnimationTree::get_animation_player);
  1354. ClassDB::bind_method(D_METHOD("set_root_motion_track", "path"), &AnimationTree::set_root_motion_track);
  1355. ClassDB::bind_method(D_METHOD("get_root_motion_track"), &AnimationTree::get_root_motion_track);
  1356. ClassDB::bind_method(D_METHOD("get_root_motion_transform"), &AnimationTree::get_root_motion_transform);
  1357. ClassDB::bind_method(D_METHOD("_update_properties"), &AnimationTree::_update_properties);
  1358. ClassDB::bind_method(D_METHOD("rename_parameter", "old_name", "new_name"), &AnimationTree::rename_parameter);
  1359. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationTree::advance);
  1360. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tree_root", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode"), "set_tree_root", "get_tree_root");
  1361. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "anim_player", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationPlayer"), "set_animation_player", "get_animation_player");
  1362. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  1363. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
  1364. ADD_GROUP("Root Motion", "root_motion_");
  1365. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_motion_track"), "set_root_motion_track", "get_root_motion_track");
  1366. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1367. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1368. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1369. }
  1370. AnimationTree::AnimationTree() {
  1371. }
  1372. AnimationTree::~AnimationTree() {
  1373. }