animation.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /*************************************************************************/
  2. /* animation.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation.h"
  31. #include "geometry.h"
  32. bool Animation::_set(const StringName &p_name, const Variant &p_value) {
  33. String name = p_name;
  34. if (name == "length")
  35. set_length(p_value);
  36. else if (name == "loop")
  37. set_loop(p_value);
  38. else if (name == "step")
  39. set_step(p_value);
  40. else if (name.begins_with("tracks/")) {
  41. int track = name.get_slicec('/', 1).to_int();
  42. String what = name.get_slicec('/', 2);
  43. if (tracks.size() == track && what == "type") {
  44. String type = p_value;
  45. if (type == "transform") {
  46. add_track(TYPE_TRANSFORM);
  47. } else if (type == "value") {
  48. add_track(TYPE_VALUE);
  49. } else if (type == "method") {
  50. add_track(TYPE_METHOD);
  51. } else {
  52. return false;
  53. }
  54. return true;
  55. }
  56. ERR_FAIL_INDEX_V(track, tracks.size(), false);
  57. if (what == "path")
  58. track_set_path(track, p_value);
  59. else if (what == "interp")
  60. track_set_interpolation_type(track, InterpolationType(p_value.operator int()));
  61. else if (what == "imported")
  62. track_set_imported(track, p_value);
  63. else if (what == "keys" || what == "key_values") {
  64. if (track_get_type(track) == TYPE_TRANSFORM) {
  65. TransformTrack *tt = static_cast<TransformTrack *>(tracks[track]);
  66. DVector<float> values = p_value;
  67. int vcount = values.size();
  68. #if 0 // old compatibility hack
  69. if ((vcount%11) == 0) {
  70. DVector<float>::Read r = values.read();
  71. tt->transforms.resize(vcount/11);
  72. for(int i=0;i<(vcount/11);i++) {
  73. TKey<TransformKey> &tk=tt->transforms[i];
  74. const float *ofs=&r[i*11];
  75. tk.time=ofs[0];
  76. tk.value.loc.x=ofs[1];
  77. tk.value.loc.y=ofs[2];
  78. tk.value.loc.z=ofs[3];
  79. tk.value.rot.x=ofs[4];
  80. tk.value.rot.y=ofs[5];
  81. tk.value.rot.z=ofs[6];
  82. tk.value.rot.w=ofs[7];
  83. tk.value.scale.x=ofs[8];
  84. tk.value.scale.y=ofs[9];
  85. tk.value.scale.z=ofs[10];
  86. }
  87. return true;
  88. }
  89. #endif
  90. ERR_FAIL_COND_V(vcount % 12, false); // shuld be multiple of 11
  91. DVector<float>::Read r = values.read();
  92. tt->transforms.resize(vcount / 12);
  93. for (int i = 0; i < (vcount / 12); i++) {
  94. TKey<TransformKey> &tk = tt->transforms[i];
  95. const float *ofs = &r[i * 12];
  96. tk.time = ofs[0];
  97. tk.transition = ofs[1];
  98. tk.value.loc.x = ofs[2];
  99. tk.value.loc.y = ofs[3];
  100. tk.value.loc.z = ofs[4];
  101. tk.value.rot.x = ofs[5];
  102. tk.value.rot.y = ofs[6];
  103. tk.value.rot.z = ofs[7];
  104. tk.value.rot.w = ofs[8];
  105. tk.value.scale.x = ofs[9];
  106. tk.value.scale.y = ofs[10];
  107. tk.value.scale.z = ofs[11];
  108. }
  109. } else if (track_get_type(track) == TYPE_VALUE) {
  110. ValueTrack *vt = static_cast<ValueTrack *>(tracks[track]);
  111. Dictionary d = p_value;
  112. ERR_FAIL_COND_V(!d.has("times"), false);
  113. ERR_FAIL_COND_V(!d.has("values"), false);
  114. if (d.has("cont")) {
  115. bool v = d["cont"];
  116. vt->update_mode = v ? UPDATE_CONTINUOUS : UPDATE_DISCRETE;
  117. }
  118. if (d.has("update")) {
  119. int um = d["update"];
  120. if (um < 0)
  121. um = 0;
  122. else if (um > 2)
  123. um = 2;
  124. vt->update_mode = UpdateMode(um);
  125. }
  126. DVector<float> times = d["times"];
  127. Array values = d["values"];
  128. ERR_FAIL_COND_V(times.size() != values.size(), false);
  129. if (times.size()) {
  130. int valcount = times.size();
  131. DVector<float>::Read rt = times.read();
  132. vt->values.resize(valcount);
  133. for (int i = 0; i < valcount; i++) {
  134. vt->values[i].time = rt[i];
  135. vt->values[i].value = values[i];
  136. }
  137. if (d.has("transitions")) {
  138. DVector<float> transitions = d["transitions"];
  139. ERR_FAIL_COND_V(transitions.size() != valcount, false);
  140. DVector<float>::Read rtr = transitions.read();
  141. for (int i = 0; i < valcount; i++) {
  142. vt->values[i].transition = rtr[i];
  143. }
  144. }
  145. }
  146. return true;
  147. } else {
  148. while (track_get_key_count(track))
  149. track_remove_key(track, 0); //well shouldn't be set anyway
  150. Dictionary d = p_value;
  151. ERR_FAIL_COND_V(!d.has("times"), false);
  152. ERR_FAIL_COND_V(!d.has("values"), false);
  153. DVector<float> times = d["times"];
  154. Array values = d["values"];
  155. ERR_FAIL_COND_V(times.size() != values.size(), false);
  156. if (times.size()) {
  157. int valcount = times.size();
  158. DVector<float>::Read rt = times.read();
  159. for (int i = 0; i < valcount; i++) {
  160. track_insert_key(track, rt[i], values[i]);
  161. }
  162. if (d.has("transitions")) {
  163. DVector<float> transitions = d["transitions"];
  164. ERR_FAIL_COND_V(transitions.size() != valcount, false);
  165. DVector<float>::Read rtr = transitions.read();
  166. for (int i = 0; i < valcount; i++) {
  167. track_set_key_transition(track, i, rtr[i]);
  168. }
  169. }
  170. }
  171. }
  172. } else
  173. return false;
  174. } else
  175. return false;
  176. return true;
  177. }
  178. bool Animation::_get(const StringName &p_name, Variant &r_ret) const {
  179. String name = p_name;
  180. if (name == "length")
  181. r_ret = length;
  182. else if (name == "loop")
  183. r_ret = loop;
  184. else if (name == "step")
  185. r_ret = step;
  186. else if (name.begins_with("tracks/")) {
  187. int track = name.get_slicec('/', 1).to_int();
  188. String what = name.get_slicec('/', 2);
  189. ERR_FAIL_INDEX_V(track, tracks.size(), false);
  190. if (what == "type") {
  191. switch (track_get_type(track)) {
  192. case TYPE_TRANSFORM: r_ret = "transform"; break;
  193. case TYPE_VALUE: r_ret = "value"; break;
  194. case TYPE_METHOD: r_ret = "method"; break;
  195. }
  196. return true;
  197. } else if (what == "path")
  198. r_ret = track_get_path(track);
  199. else if (what == "interp")
  200. r_ret = track_get_interpolation_type(track);
  201. else if (what == "imported")
  202. r_ret = track_is_imported(track);
  203. else if (what == "keys") {
  204. if (track_get_type(track) == TYPE_TRANSFORM) {
  205. DVector<real_t> keys;
  206. int kk = track_get_key_count(track);
  207. keys.resize(kk * 12);
  208. DVector<real_t>::Write w = keys.write();
  209. int idx = 0;
  210. for (int i = 0; i < track_get_key_count(track); i++) {
  211. Vector3 loc;
  212. Quat rot;
  213. Vector3 scale;
  214. transform_track_get_key(track, i, &loc, &rot, &scale);
  215. w[idx++] = track_get_key_time(track, i);
  216. w[idx++] = track_get_key_transition(track, i);
  217. w[idx++] = loc.x;
  218. w[idx++] = loc.y;
  219. w[idx++] = loc.z;
  220. w[idx++] = rot.x;
  221. w[idx++] = rot.y;
  222. w[idx++] = rot.z;
  223. w[idx++] = rot.w;
  224. w[idx++] = scale.x;
  225. w[idx++] = scale.y;
  226. w[idx++] = scale.z;
  227. }
  228. w = DVector<real_t>::Write();
  229. r_ret = keys;
  230. return true;
  231. } else if (track_get_type(track) == TYPE_VALUE) {
  232. const ValueTrack *vt = static_cast<const ValueTrack *>(tracks[track]);
  233. Dictionary d;
  234. DVector<float> key_times;
  235. DVector<float> key_transitions;
  236. Array key_values;
  237. int kk = vt->values.size();
  238. key_times.resize(kk);
  239. key_transitions.resize(kk);
  240. key_values.resize(kk);
  241. DVector<float>::Write wti = key_times.write();
  242. DVector<float>::Write wtr = key_transitions.write();
  243. int idx = 0;
  244. const TKey<Variant> *vls = vt->values.ptr();
  245. for (int i = 0; i < kk; i++) {
  246. wti[idx] = vls[i].time;
  247. wtr[idx] = vls[i].transition;
  248. key_values[idx] = vls[i].value;
  249. idx++;
  250. }
  251. wti = DVector<float>::Write();
  252. wtr = DVector<float>::Write();
  253. d["times"] = key_times;
  254. d["transitions"] = key_transitions;
  255. d["values"] = key_values;
  256. if (track_get_type(track) == TYPE_VALUE) {
  257. d["update"] = value_track_get_update_mode(track);
  258. }
  259. r_ret = d;
  260. return true;
  261. } else {
  262. Dictionary d;
  263. DVector<float> key_times;
  264. DVector<float> key_transitions;
  265. Array key_values;
  266. int kk = track_get_key_count(track);
  267. key_times.resize(kk);
  268. key_transitions.resize(kk);
  269. key_values.resize(kk);
  270. DVector<float>::Write wti = key_times.write();
  271. DVector<float>::Write wtr = key_transitions.write();
  272. int idx = 0;
  273. for (int i = 0; i < track_get_key_count(track); i++) {
  274. wti[idx] = track_get_key_time(track, i);
  275. wtr[idx] = track_get_key_transition(track, i);
  276. key_values[idx] = track_get_key_value(track, i);
  277. idx++;
  278. }
  279. wti = DVector<float>::Write();
  280. wtr = DVector<float>::Write();
  281. d["times"] = key_times;
  282. d["transitions"] = key_transitions;
  283. d["values"] = key_values;
  284. if (track_get_type(track) == TYPE_VALUE) {
  285. d["update"] = value_track_get_update_mode(track);
  286. }
  287. r_ret = d;
  288. return true;
  289. }
  290. } else
  291. return false;
  292. } else
  293. return false;
  294. return true;
  295. }
  296. void Animation::_get_property_list(List<PropertyInfo> *p_list) const {
  297. p_list->push_back(PropertyInfo(Variant::REAL, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001"));
  298. p_list->push_back(PropertyInfo(Variant::BOOL, "loop"));
  299. p_list->push_back(PropertyInfo(Variant::REAL, "step", PROPERTY_HINT_RANGE, "0,4096,0.001"));
  300. for (int i = 0; i < tracks.size(); i++) {
  301. p_list->push_back(PropertyInfo(Variant::STRING, "tracks/" + itos(i) + "/type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  302. p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  303. p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  304. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/imported", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  305. p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  306. }
  307. }
  308. int Animation::add_track(TrackType p_type, int p_at_pos) {
  309. if (p_at_pos < 0 || p_at_pos >= tracks.size())
  310. p_at_pos = tracks.size();
  311. switch (p_type) {
  312. case TYPE_TRANSFORM: {
  313. TransformTrack *tt = memnew(TransformTrack);
  314. tracks.insert(p_at_pos, tt);
  315. } break;
  316. case TYPE_VALUE: {
  317. tracks.insert(p_at_pos, memnew(ValueTrack));
  318. } break;
  319. case TYPE_METHOD: {
  320. tracks.insert(p_at_pos, memnew(MethodTrack));
  321. } break;
  322. default: {
  323. ERR_PRINT("Unknown track type");
  324. }
  325. }
  326. emit_changed();
  327. return p_at_pos;
  328. }
  329. void Animation::remove_track(int p_track) {
  330. ERR_FAIL_INDEX(p_track, tracks.size());
  331. Track *t = tracks[p_track];
  332. switch (t->type) {
  333. case TYPE_TRANSFORM: {
  334. TransformTrack *tt = static_cast<TransformTrack *>(t);
  335. _clear(tt->transforms);
  336. } break;
  337. case TYPE_VALUE: {
  338. ValueTrack *vt = static_cast<ValueTrack *>(t);
  339. _clear(vt->values);
  340. } break;
  341. case TYPE_METHOD: {
  342. MethodTrack *mt = static_cast<MethodTrack *>(t);
  343. _clear(mt->methods);
  344. } break;
  345. }
  346. memdelete(t);
  347. tracks.remove(p_track);
  348. emit_changed();
  349. }
  350. int Animation::get_track_count() const {
  351. return tracks.size();
  352. }
  353. Animation::TrackType Animation::track_get_type(int p_track) const {
  354. ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_TRANSFORM);
  355. return tracks[p_track]->type;
  356. }
  357. void Animation::track_set_path(int p_track, const NodePath &p_path) {
  358. ERR_FAIL_INDEX(p_track, tracks.size());
  359. tracks[p_track]->path = p_path;
  360. emit_changed();
  361. }
  362. NodePath Animation::track_get_path(int p_track) const {
  363. ERR_FAIL_INDEX_V(p_track, tracks.size(), NodePath());
  364. return tracks[p_track]->path;
  365. }
  366. int Animation::find_track(const NodePath &p_path) const {
  367. for (int i = 0; i < tracks.size(); i++) {
  368. if (tracks[i]->path == p_path)
  369. return i;
  370. };
  371. return -1;
  372. };
  373. void Animation::track_set_interpolation_type(int p_track, InterpolationType p_interp) {
  374. ERR_FAIL_INDEX(p_track, tracks.size());
  375. ERR_FAIL_INDEX(p_interp, 3);
  376. tracks[p_track]->interpolation = p_interp;
  377. emit_changed();
  378. }
  379. Animation::InterpolationType Animation::track_get_interpolation_type(int p_track) const {
  380. ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
  381. return tracks[p_track]->interpolation;
  382. }
  383. // transform
  384. /*
  385. template<class T>
  386. int Animation::_insert_pos(float p_time, T& p_keys) {
  387. // simple, linear time inset that should be fast enough in reality.
  388. int idx=p_keys.size();
  389. while(true) {
  390. if (idx==0 || p_keys[idx-1].time < p_time) {
  391. //condition for insertion.
  392. p_keys.insert(idx,T());
  393. return idx;
  394. } else if (p_keys[idx-1].time == p_time) {
  395. // condition for replacing.
  396. return idx-1;
  397. }
  398. idx--;
  399. }
  400. }
  401. */
  402. template <class T, class V>
  403. int Animation::_insert(float p_time, T &p_keys, const V &p_value) {
  404. int idx = p_keys.size();
  405. while (true) {
  406. if (idx == 0 || p_keys[idx - 1].time < p_time) {
  407. //condition for insertion.
  408. p_keys.insert(idx, p_value);
  409. return idx;
  410. } else if (p_keys[idx - 1].time == p_time) {
  411. // condition for replacing.
  412. p_keys[idx - 1] = p_value;
  413. return idx - 1;
  414. }
  415. idx--;
  416. }
  417. return -1;
  418. }
  419. template <class T>
  420. void Animation::_clear(T &p_keys) {
  421. p_keys.clear();
  422. }
  423. Error Animation::transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const {
  424. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  425. Track *t = tracks[p_track];
  426. TransformTrack *tt = static_cast<TransformTrack *>(t);
  427. ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER);
  428. ERR_FAIL_INDEX_V(p_key, tt->transforms.size(), ERR_INVALID_PARAMETER);
  429. if (r_loc)
  430. *r_loc = tt->transforms[p_key].value.loc;
  431. if (r_rot)
  432. *r_rot = tt->transforms[p_key].value.rot;
  433. if (r_scale)
  434. *r_scale = tt->transforms[p_key].value.scale;
  435. return OK;
  436. }
  437. int Animation::transform_track_insert_key(int p_track, float p_time, const Vector3 p_loc, const Quat &p_rot, const Vector3 &p_scale) {
  438. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  439. Track *t = tracks[p_track];
  440. ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, -1);
  441. TransformTrack *tt = static_cast<TransformTrack *>(t);
  442. TKey<TransformKey> tkey;
  443. tkey.time = p_time;
  444. tkey.value.loc = p_loc;
  445. tkey.value.rot = p_rot;
  446. tkey.value.scale = p_scale;
  447. int ret = _insert(p_time, tt->transforms, tkey);
  448. emit_changed();
  449. return ret;
  450. }
  451. void Animation::track_remove_key_at_pos(int p_track, float p_pos) {
  452. int idx = track_find_key(p_track, p_pos, true);
  453. ERR_FAIL_COND(idx < 0);
  454. track_remove_key(p_track, idx);
  455. }
  456. void Animation::track_remove_key(int p_track, int p_idx) {
  457. ERR_FAIL_INDEX(p_track, tracks.size());
  458. Track *t = tracks[p_track];
  459. switch (t->type) {
  460. case TYPE_TRANSFORM: {
  461. TransformTrack *tt = static_cast<TransformTrack *>(t);
  462. ERR_FAIL_INDEX(p_idx, tt->transforms.size());
  463. tt->transforms.remove(p_idx);
  464. } break;
  465. case TYPE_VALUE: {
  466. ValueTrack *vt = static_cast<ValueTrack *>(t);
  467. ERR_FAIL_INDEX(p_idx, vt->values.size());
  468. vt->values.remove(p_idx);
  469. } break;
  470. case TYPE_METHOD: {
  471. MethodTrack *mt = static_cast<MethodTrack *>(t);
  472. ERR_FAIL_INDEX(p_idx, mt->methods.size());
  473. mt->methods.remove(p_idx);
  474. } break;
  475. }
  476. emit_changed();
  477. }
  478. int Animation::track_find_key(int p_track, float p_time, bool p_exact) const {
  479. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  480. Track *t = tracks[p_track];
  481. switch (t->type) {
  482. case TYPE_TRANSFORM: {
  483. TransformTrack *tt = static_cast<TransformTrack *>(t);
  484. int k = _find(tt->transforms, p_time);
  485. if (k < 0 || k >= tt->transforms.size())
  486. return -1;
  487. if (tt->transforms[k].time != p_time && p_exact)
  488. return -1;
  489. return k;
  490. } break;
  491. case TYPE_VALUE: {
  492. ValueTrack *vt = static_cast<ValueTrack *>(t);
  493. int k = _find(vt->values, p_time);
  494. if (k < 0 || k >= vt->values.size())
  495. return -1;
  496. if (vt->values[k].time != p_time && p_exact)
  497. return -1;
  498. return k;
  499. } break;
  500. case TYPE_METHOD: {
  501. MethodTrack *mt = static_cast<MethodTrack *>(t);
  502. int k = _find(mt->methods, p_time);
  503. if (k < 0 || k >= mt->methods.size())
  504. return -1;
  505. if (mt->methods[k].time != p_time && p_exact)
  506. return -1;
  507. return k;
  508. } break;
  509. }
  510. return -1;
  511. }
  512. void Animation::track_insert_key(int p_track, float p_time, const Variant &p_value, float p_transition) {
  513. ERR_FAIL_INDEX(p_track, tracks.size());
  514. Track *t = tracks[p_track];
  515. switch (t->type) {
  516. case TYPE_TRANSFORM: {
  517. Dictionary d = p_value;
  518. Vector3 loc;
  519. if (d.has("loc"))
  520. loc = d["loc"];
  521. Quat rot;
  522. if (d.has("rot"))
  523. rot = d["rot"];
  524. Vector3 scale;
  525. if (d.has("scale"))
  526. scale = d["scale"];
  527. int idx = transform_track_insert_key(p_track, p_time, loc, rot, scale);
  528. track_set_key_transition(p_track, idx, p_transition);
  529. } break;
  530. case TYPE_VALUE: {
  531. ValueTrack *vt = static_cast<ValueTrack *>(t);
  532. TKey<Variant> k;
  533. k.time = p_time;
  534. k.transition = p_transition;
  535. k.value = p_value;
  536. _insert(p_time, vt->values, k);
  537. } break;
  538. case TYPE_METHOD: {
  539. MethodTrack *mt = static_cast<MethodTrack *>(t);
  540. ERR_FAIL_COND(p_value.get_type() != Variant::DICTIONARY);
  541. Dictionary d = p_value;
  542. ERR_FAIL_COND(!d.has("method") || d["method"].get_type() != Variant::STRING);
  543. ERR_FAIL_COND(!d.has("args") || !d["args"].is_array());
  544. MethodKey k;
  545. k.time = p_time;
  546. k.transition = p_transition;
  547. k.method = d["method"];
  548. k.params = d["args"];
  549. _insert(p_time, mt->methods, k);
  550. } break;
  551. }
  552. emit_changed();
  553. }
  554. int Animation::track_get_key_count(int p_track) const {
  555. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  556. Track *t = tracks[p_track];
  557. switch (t->type) {
  558. case TYPE_TRANSFORM: {
  559. TransformTrack *tt = static_cast<TransformTrack *>(t);
  560. return tt->transforms.size();
  561. } break;
  562. case TYPE_VALUE: {
  563. ValueTrack *vt = static_cast<ValueTrack *>(t);
  564. return vt->values.size();
  565. } break;
  566. case TYPE_METHOD: {
  567. MethodTrack *mt = static_cast<MethodTrack *>(t);
  568. return mt->methods.size();
  569. } break;
  570. }
  571. ERR_FAIL_V(-1);
  572. }
  573. Variant Animation::track_get_key_value(int p_track, int p_key_idx) const {
  574. ERR_FAIL_INDEX_V(p_track, tracks.size(), Variant());
  575. Track *t = tracks[p_track];
  576. switch (t->type) {
  577. case TYPE_TRANSFORM: {
  578. TransformTrack *tt = static_cast<TransformTrack *>(t);
  579. ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), Variant());
  580. Dictionary d;
  581. d["loc"] = tt->transforms[p_key_idx].value.loc;
  582. d["rot"] = tt->transforms[p_key_idx].value.rot;
  583. d["scale"] = tt->transforms[p_key_idx].value.scale;
  584. return d;
  585. } break;
  586. case TYPE_VALUE: {
  587. ValueTrack *vt = static_cast<ValueTrack *>(t);
  588. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), Variant());
  589. return vt->values[p_key_idx].value;
  590. } break;
  591. case TYPE_METHOD: {
  592. MethodTrack *mt = static_cast<MethodTrack *>(t);
  593. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), Variant());
  594. Dictionary d;
  595. d["method"] = mt->methods[p_key_idx].method;
  596. d["args"] = mt->methods[p_key_idx].params;
  597. return d;
  598. } break;
  599. }
  600. ERR_FAIL_V(Variant());
  601. }
  602. float Animation::track_get_key_time(int p_track, int p_key_idx) const {
  603. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  604. Track *t = tracks[p_track];
  605. switch (t->type) {
  606. case TYPE_TRANSFORM: {
  607. TransformTrack *tt = static_cast<TransformTrack *>(t);
  608. ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1);
  609. return tt->transforms[p_key_idx].time;
  610. } break;
  611. case TYPE_VALUE: {
  612. ValueTrack *vt = static_cast<ValueTrack *>(t);
  613. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
  614. return vt->values[p_key_idx].time;
  615. } break;
  616. case TYPE_METHOD: {
  617. MethodTrack *mt = static_cast<MethodTrack *>(t);
  618. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
  619. return mt->methods[p_key_idx].time;
  620. } break;
  621. }
  622. ERR_FAIL_V(-1);
  623. }
  624. float Animation::track_get_key_transition(int p_track, int p_key_idx) const {
  625. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  626. Track *t = tracks[p_track];
  627. switch (t->type) {
  628. case TYPE_TRANSFORM: {
  629. TransformTrack *tt = static_cast<TransformTrack *>(t);
  630. ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1);
  631. return tt->transforms[p_key_idx].transition;
  632. } break;
  633. case TYPE_VALUE: {
  634. ValueTrack *vt = static_cast<ValueTrack *>(t);
  635. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
  636. return vt->values[p_key_idx].transition;
  637. } break;
  638. case TYPE_METHOD: {
  639. MethodTrack *mt = static_cast<MethodTrack *>(t);
  640. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
  641. return mt->methods[p_key_idx].transition;
  642. } break;
  643. }
  644. ERR_FAIL_V(0);
  645. }
  646. void Animation::track_set_key_value(int p_track, int p_key_idx, const Variant &p_value) {
  647. ERR_FAIL_INDEX(p_track, tracks.size());
  648. Track *t = tracks[p_track];
  649. switch (t->type) {
  650. case TYPE_TRANSFORM: {
  651. TransformTrack *tt = static_cast<TransformTrack *>(t);
  652. ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
  653. Dictionary d = p_value;
  654. if (d.has("loc"))
  655. tt->transforms[p_key_idx].value.loc = d["loc"];
  656. if (d.has("rot"))
  657. tt->transforms[p_key_idx].value.rot = d["rot"];
  658. if (d.has("scale"))
  659. tt->transforms[p_key_idx].value.scale = d["scale"];
  660. } break;
  661. case TYPE_VALUE: {
  662. ValueTrack *vt = static_cast<ValueTrack *>(t);
  663. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  664. vt->values[p_key_idx].value = p_value;
  665. } break;
  666. case TYPE_METHOD: {
  667. MethodTrack *mt = static_cast<MethodTrack *>(t);
  668. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  669. Dictionary d = p_value;
  670. if (d.has("method"))
  671. mt->methods[p_key_idx].method = d["method"];
  672. if (d.has("args"))
  673. mt->methods[p_key_idx].params = d["args"];
  674. } break;
  675. }
  676. }
  677. void Animation::track_set_key_transition(int p_track, int p_key_idx, float p_transition) {
  678. ERR_FAIL_INDEX(p_track, tracks.size());
  679. Track *t = tracks[p_track];
  680. switch (t->type) {
  681. case TYPE_TRANSFORM: {
  682. TransformTrack *tt = static_cast<TransformTrack *>(t);
  683. ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
  684. tt->transforms[p_key_idx].transition = p_transition;
  685. } break;
  686. case TYPE_VALUE: {
  687. ValueTrack *vt = static_cast<ValueTrack *>(t);
  688. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  689. vt->values[p_key_idx].transition = p_transition;
  690. } break;
  691. case TYPE_METHOD: {
  692. MethodTrack *mt = static_cast<MethodTrack *>(t);
  693. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  694. mt->methods[p_key_idx].transition = p_transition;
  695. } break;
  696. }
  697. }
  698. template <class K>
  699. int Animation::_find(const Vector<K> &p_keys, float p_time) const {
  700. int len = p_keys.size();
  701. if (len == 0)
  702. return -2;
  703. int low = 0;
  704. int high = len - 1;
  705. int middle;
  706. const K *keys = &p_keys[0];
  707. while (low <= high) {
  708. middle = (low + high) / 2;
  709. if (p_time == keys[middle].time) { //match
  710. return middle;
  711. } else if (p_time < keys[middle].time)
  712. high = middle - 1; //search low end of array
  713. else
  714. low = middle + 1; //search high end of array
  715. }
  716. if (keys[middle].time > p_time)
  717. middle--;
  718. return middle;
  719. }
  720. Animation::TransformKey Animation::_interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const {
  721. TransformKey ret;
  722. ret.loc = _interpolate(p_a.loc, p_b.loc, p_c);
  723. ret.rot = _interpolate(p_a.rot, p_b.rot, p_c);
  724. ret.scale = _interpolate(p_a.scale, p_b.scale, p_c);
  725. return ret;
  726. }
  727. Vector3 Animation::_interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const {
  728. return p_a.linear_interpolate(p_b, p_c);
  729. }
  730. Quat Animation::_interpolate(const Quat &p_a, const Quat &p_b, float p_c) const {
  731. return p_a.slerp(p_b, p_c);
  732. }
  733. Variant Animation::_interpolate(const Variant &p_a, const Variant &p_b, float p_c) const {
  734. Variant dst;
  735. Variant::interpolate(p_a, p_b, p_c, dst);
  736. return dst;
  737. }
  738. float Animation::_interpolate(const float &p_a, const float &p_b, float p_c) const {
  739. return p_a * (1.0 - p_c) + p_b * p_c;
  740. }
  741. Animation::TransformKey Animation::_cubic_interpolate(const Animation::TransformKey &p_pre_a, const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, const Animation::TransformKey &p_post_b, float p_c) const {
  742. Animation::TransformKey tk;
  743. tk.loc = p_a.loc.cubic_interpolate(p_b.loc, p_pre_a.loc, p_post_b.loc, p_c);
  744. tk.scale = p_a.scale.cubic_interpolate(p_b.scale, p_pre_a.scale, p_post_b.scale, p_c);
  745. tk.rot = p_a.rot.cubic_slerp(p_b.rot, p_pre_a.rot, p_post_b.rot, p_c);
  746. return tk;
  747. }
  748. Vector3 Animation::_cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, float p_c) const {
  749. return p_a.cubic_interpolate(p_b, p_pre_a, p_post_b, p_c);
  750. }
  751. Quat Animation::_cubic_interpolate(const Quat &p_pre_a, const Quat &p_a, const Quat &p_b, const Quat &p_post_b, float p_c) const {
  752. return p_a.cubic_slerp(p_b, p_pre_a, p_post_b, p_c);
  753. }
  754. Variant Animation::_cubic_interpolate(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, float p_c) const {
  755. Variant::Type type_a = p_a.get_type();
  756. Variant::Type type_b = p_b.get_type();
  757. Variant::Type type_pa = p_pre_a.get_type();
  758. Variant::Type type_pb = p_post_b.get_type();
  759. //make int and real play along
  760. uint32_t vformat = 1 << type_a;
  761. vformat |= 1 << type_b;
  762. vformat |= 1 << type_pa;
  763. vformat |= 1 << type_pb;
  764. if (vformat == ((1 << Variant::INT) | (1 << Variant::REAL)) || vformat == (1 << Variant::REAL)) {
  765. //mix of real and int
  766. real_t p0 = p_pre_a;
  767. real_t p1 = p_a;
  768. real_t p2 = p_b;
  769. real_t p3 = p_post_b;
  770. float t = p_c;
  771. float t2 = t * t;
  772. float t3 = t2 * t;
  773. return 0.5f * ((p1 * 2.0f) +
  774. (-p0 + p2) * t +
  775. (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
  776. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
  777. } else if ((vformat & (vformat - 1))) {
  778. return p_a; //can't interpolate, mix of types
  779. }
  780. switch (type_a) {
  781. case Variant::VECTOR2: {
  782. Vector2 a = p_a;
  783. Vector2 b = p_b;
  784. Vector2 pa = p_pre_a;
  785. Vector2 pb = p_post_b;
  786. return a.cubic_interpolate(b, pa, pb, p_c);
  787. } break;
  788. case Variant::RECT2: {
  789. Rect2 a = p_a;
  790. Rect2 b = p_b;
  791. Rect2 pa = p_pre_a;
  792. Rect2 pb = p_post_b;
  793. return Rect2(
  794. a.pos.cubic_interpolate(b.pos, pa.pos, pb.pos, p_c),
  795. a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c));
  796. } break;
  797. case Variant::VECTOR3: {
  798. Vector3 a = p_a;
  799. Vector3 b = p_b;
  800. Vector3 pa = p_pre_a;
  801. Vector3 pb = p_post_b;
  802. return a.cubic_interpolate(b, pa, pb, p_c);
  803. } break;
  804. case Variant::QUAT: {
  805. Quat a = p_a;
  806. Quat b = p_b;
  807. Quat pa = p_pre_a;
  808. Quat pb = p_post_b;
  809. return a.cubic_slerp(b, pa, pb, p_c);
  810. } break;
  811. case Variant::_AABB: {
  812. AABB a = p_a;
  813. AABB b = p_b;
  814. AABB pa = p_pre_a;
  815. AABB pb = p_post_b;
  816. return AABB(
  817. a.pos.cubic_interpolate(b.pos, pa.pos, pb.pos, p_c),
  818. a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c));
  819. } break;
  820. default: {
  821. return _interpolate(p_a, p_b, p_c);
  822. }
  823. }
  824. return Variant();
  825. }
  826. float Animation::_cubic_interpolate(const float &p_pre_a, const float &p_a, const float &p_b, const float &p_post_b, float p_c) const {
  827. return _interpolate(p_a, p_b, p_c);
  828. }
  829. template <class T>
  830. T Animation::_interpolate(const Vector<TKey<T> > &p_keys, float p_time, InterpolationType p_interp, bool *p_ok) const {
  831. int len = _find(p_keys, length) + 1; // try to find last key (there may be more past the end)
  832. if (len <= 0) {
  833. // (-1 or -2 returned originally) (plus one above)
  834. // meaning no keys, or only key time is larger than length
  835. if (p_ok)
  836. *p_ok = false;
  837. return T();
  838. } else if (len == 1) { // one key found (0+1), return it
  839. if (p_ok)
  840. *p_ok = true;
  841. return p_keys[0].value;
  842. }
  843. int idx = _find(p_keys, p_time);
  844. ERR_FAIL_COND_V(idx == -2, T());
  845. if (p_ok)
  846. *p_ok = true;
  847. int next = 0;
  848. float c = 0;
  849. // prepare for all cases of interpolation
  850. if (loop) {
  851. // loop
  852. if (idx >= 0) {
  853. if ((idx + 1) < len) {
  854. next = idx + 1;
  855. float delta = p_keys[next].time - p_keys[idx].time;
  856. float from = p_time - p_keys[idx].time;
  857. if (Math::absf(delta) > CMP_EPSILON)
  858. c = from / delta;
  859. else
  860. c = 0;
  861. } else {
  862. next = 0;
  863. float delta = (length - p_keys[idx].time) + p_keys[next].time;
  864. float from = p_time - p_keys[idx].time;
  865. if (Math::absf(delta) > CMP_EPSILON)
  866. c = from / delta;
  867. else
  868. c = 0;
  869. }
  870. } else {
  871. // on loop, behind first key
  872. idx = len - 1;
  873. next = 0;
  874. float endtime = (length - p_keys[idx].time);
  875. if (endtime < 0) // may be keys past the end
  876. endtime = 0;
  877. float delta = endtime + p_keys[next].time;
  878. float from = endtime + p_time;
  879. if (Math::absf(delta) > CMP_EPSILON)
  880. c = from / delta;
  881. else
  882. c = 0;
  883. }
  884. } else { // no loop
  885. if (idx >= 0) {
  886. if ((idx + 1) < len) {
  887. next = idx + 1;
  888. float delta = p_keys[next].time - p_keys[idx].time;
  889. float from = p_time - p_keys[idx].time;
  890. if (Math::absf(delta) > CMP_EPSILON)
  891. c = from / delta;
  892. else
  893. c = 0;
  894. } else {
  895. next = idx;
  896. }
  897. } else if (idx < 0) {
  898. idx = next = 0;
  899. }
  900. }
  901. float tr = p_keys[idx].transition;
  902. if (tr == 0 || idx == next) {
  903. // don't interpolate if not needed
  904. return p_keys[idx].value;
  905. }
  906. if (tr != 1.0) {
  907. c = Math::ease(c, tr);
  908. }
  909. switch (p_interp) {
  910. case INTERPOLATION_NEAREST: {
  911. return p_keys[idx].value;
  912. } break;
  913. case INTERPOLATION_LINEAR: {
  914. return _interpolate(p_keys[idx].value, p_keys[next].value, c);
  915. } break;
  916. case INTERPOLATION_CUBIC: {
  917. int pre = idx - 1;
  918. if (pre < 0)
  919. pre = 0;
  920. int post = next + 1;
  921. if (post >= len)
  922. post = next;
  923. return _cubic_interpolate(p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c);
  924. } break;
  925. default: return p_keys[idx].value;
  926. }
  927. // do a barrel roll
  928. }
  929. Error Animation::transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const {
  930. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  931. Track *t = tracks[p_track];
  932. ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER);
  933. TransformTrack *tt = static_cast<TransformTrack *>(t);
  934. bool ok;
  935. TransformKey tk = _interpolate(tt->transforms, p_time, tt->interpolation, &ok);
  936. if (!ok) // ??
  937. return ERR_UNAVAILABLE;
  938. if (r_loc)
  939. *r_loc = tk.loc;
  940. if (r_rot)
  941. *r_rot = tk.rot;
  942. if (r_scale)
  943. *r_scale = tk.scale;
  944. return OK;
  945. }
  946. Variant Animation::value_track_interpolate(int p_track, float p_time) const {
  947. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  948. Track *t = tracks[p_track];
  949. ERR_FAIL_COND_V(t->type != TYPE_VALUE, Variant());
  950. ValueTrack *vt = static_cast<ValueTrack *>(t);
  951. bool ok;
  952. Variant res = _interpolate(vt->values, p_time, vt->update_mode == UPDATE_CONTINUOUS ? vt->interpolation : INTERPOLATION_NEAREST, &ok);
  953. if (ok) {
  954. return res;
  955. }
  956. return Variant();
  957. }
  958. void Animation::_value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const {
  959. if (from_time != length && to_time == length)
  960. to_time = length * 1.01; //include a little more if at the end
  961. int to = _find(vt->values, to_time);
  962. // can't really send the events == time, will be sent in the next frame.
  963. // if event>=len then it will probably never be requested by the anim player.
  964. if (to >= 0 && vt->values[to].time >= to_time)
  965. to--;
  966. if (to < 0)
  967. return; // not bother
  968. int from = _find(vt->values, from_time);
  969. // position in the right first event.+
  970. if (from < 0 || vt->values[from].time < from_time)
  971. from++;
  972. int max = vt->values.size();
  973. for (int i = from; i <= to; i++) {
  974. ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
  975. p_indices->push_back(i);
  976. }
  977. }
  978. void Animation::value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
  979. ERR_FAIL_INDEX(p_track, tracks.size());
  980. Track *t = tracks[p_track];
  981. ERR_FAIL_COND(t->type != TYPE_VALUE);
  982. ValueTrack *vt = static_cast<ValueTrack *>(t);
  983. float from_time = p_time - p_delta;
  984. float to_time = p_time;
  985. if (from_time > to_time)
  986. SWAP(from_time, to_time);
  987. if (loop) {
  988. from_time = Math::fposmod(from_time, length);
  989. to_time = Math::fposmod(to_time, length);
  990. if (from_time > to_time) {
  991. // handle loop by splitting
  992. _value_track_get_key_indices_in_range(vt, length - from_time, length, p_indices);
  993. _value_track_get_key_indices_in_range(vt, 0, to_time, p_indices);
  994. return;
  995. }
  996. } else {
  997. if (from_time < 0)
  998. from_time = 0;
  999. if (from_time > length)
  1000. from_time = length;
  1001. if (to_time < 0)
  1002. to_time = 0;
  1003. if (to_time > length)
  1004. to_time = length;
  1005. }
  1006. _value_track_get_key_indices_in_range(vt, from_time, to_time, p_indices);
  1007. }
  1008. void Animation::value_track_set_update_mode(int p_track, UpdateMode p_mode) {
  1009. ERR_FAIL_INDEX(p_track, tracks.size());
  1010. Track *t = tracks[p_track];
  1011. ERR_FAIL_COND(t->type != TYPE_VALUE);
  1012. ERR_FAIL_INDEX(p_mode, 3);
  1013. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1014. vt->update_mode = p_mode;
  1015. }
  1016. Animation::UpdateMode Animation::value_track_get_update_mode(int p_track) const {
  1017. ERR_FAIL_INDEX_V(p_track, tracks.size(), UPDATE_CONTINUOUS);
  1018. Track *t = tracks[p_track];
  1019. ERR_FAIL_COND_V(t->type != TYPE_VALUE, UPDATE_CONTINUOUS);
  1020. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1021. return vt->update_mode;
  1022. }
  1023. void Animation::_method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const {
  1024. if (from_time != length && to_time == length)
  1025. to_time = length * 1.01; //include a little more if at the end
  1026. int to = _find(mt->methods, to_time);
  1027. // can't really send the events == time, will be sent in the next frame.
  1028. // if event>=len then it will probably never be requested by the anim player.
  1029. if (to >= 0 && mt->methods[to].time >= to_time)
  1030. to--;
  1031. if (to < 0)
  1032. return; // not bother
  1033. int from = _find(mt->methods, from_time);
  1034. // position in the right first event.+
  1035. if (from < 0 || mt->methods[from].time < from_time)
  1036. from++;
  1037. int max = mt->methods.size();
  1038. for (int i = from; i <= to; i++) {
  1039. ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
  1040. p_indices->push_back(i);
  1041. }
  1042. }
  1043. void Animation::method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
  1044. ERR_FAIL_INDEX(p_track, tracks.size());
  1045. Track *t = tracks[p_track];
  1046. ERR_FAIL_COND(t->type != TYPE_METHOD);
  1047. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1048. float from_time = p_time - p_delta;
  1049. float to_time = p_time;
  1050. if (from_time > to_time)
  1051. SWAP(from_time, to_time);
  1052. if (loop) {
  1053. if (from_time > length || from_time < 0)
  1054. from_time = Math::fposmod(from_time, length);
  1055. if (to_time > length || to_time < 0)
  1056. to_time = Math::fposmod(to_time, length);
  1057. if (from_time > to_time) {
  1058. // handle loop by splitting
  1059. _method_track_get_key_indices_in_range(mt, from_time, length, p_indices);
  1060. _method_track_get_key_indices_in_range(mt, 0, to_time, p_indices);
  1061. return;
  1062. }
  1063. } else {
  1064. if (from_time < 0)
  1065. from_time = 0;
  1066. if (from_time > length)
  1067. from_time = length;
  1068. if (to_time < 0)
  1069. to_time = 0;
  1070. if (to_time > length)
  1071. to_time = length;
  1072. }
  1073. _method_track_get_key_indices_in_range(mt, from_time, to_time, p_indices);
  1074. }
  1075. Vector<Variant> Animation::method_track_get_params(int p_track, int p_key_idx) const {
  1076. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector<Variant>());
  1077. Track *t = tracks[p_track];
  1078. ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector<Variant>());
  1079. MethodTrack *pm = static_cast<MethodTrack *>(t);
  1080. ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), Vector<Variant>());
  1081. const MethodKey &mk = pm->methods[p_key_idx];
  1082. return mk.params;
  1083. }
  1084. StringName Animation::method_track_get_name(int p_track, int p_key_idx) const {
  1085. ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
  1086. Track *t = tracks[p_track];
  1087. ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName());
  1088. MethodTrack *pm = static_cast<MethodTrack *>(t);
  1089. ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), StringName());
  1090. return pm->methods[p_key_idx].method;
  1091. }
  1092. void Animation::set_length(float p_length) {
  1093. ERR_FAIL_COND(length < 0);
  1094. length = p_length;
  1095. emit_changed();
  1096. }
  1097. float Animation::get_length() const {
  1098. return length;
  1099. }
  1100. void Animation::set_loop(bool p_enabled) {
  1101. loop = p_enabled;
  1102. emit_changed();
  1103. }
  1104. bool Animation::has_loop() const {
  1105. return loop;
  1106. }
  1107. void Animation::track_move_up(int p_track) {
  1108. if (p_track >= 0 && p_track < (tracks.size() - 1)) {
  1109. SWAP(tracks[p_track], tracks[p_track + 1]);
  1110. }
  1111. emit_changed();
  1112. }
  1113. void Animation::track_set_imported(int p_track, bool p_imported) {
  1114. ERR_FAIL_INDEX(p_track, tracks.size());
  1115. tracks[p_track]->imported = p_imported;
  1116. }
  1117. bool Animation::track_is_imported(int p_track) const {
  1118. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  1119. return tracks[p_track]->imported;
  1120. }
  1121. void Animation::track_move_down(int p_track) {
  1122. if (p_track > 0 && p_track < tracks.size()) {
  1123. SWAP(tracks[p_track], tracks[p_track - 1]);
  1124. }
  1125. emit_changed();
  1126. }
  1127. void Animation::set_step(float p_step) {
  1128. step = p_step;
  1129. emit_changed();
  1130. }
  1131. float Animation::get_step() const {
  1132. return step;
  1133. }
  1134. void Animation::_bind_methods() {
  1135. ObjectTypeDB::bind_method(_MD("add_track", "type", "at_pos"), &Animation::add_track, DEFVAL(-1));
  1136. ObjectTypeDB::bind_method(_MD("remove_track", "idx"), &Animation::remove_track);
  1137. ObjectTypeDB::bind_method(_MD("get_track_count"), &Animation::get_track_count);
  1138. ObjectTypeDB::bind_method(_MD("track_get_type", "idx"), &Animation::track_get_type);
  1139. ObjectTypeDB::bind_method(_MD("track_get_path", "idx"), &Animation::track_get_path);
  1140. ObjectTypeDB::bind_method(_MD("track_set_path", "idx", "path"), &Animation::track_set_path);
  1141. ObjectTypeDB::bind_method(_MD("find_track", "path"), &Animation::find_track);
  1142. ObjectTypeDB::bind_method(_MD("track_move_up", "idx"), &Animation::track_move_up);
  1143. ObjectTypeDB::bind_method(_MD("track_move_down", "idx"), &Animation::track_move_down);
  1144. ObjectTypeDB::bind_method(_MD("track_set_imported", "idx", "imported"), &Animation::track_set_imported);
  1145. ObjectTypeDB::bind_method(_MD("track_is_imported", "idx"), &Animation::track_is_imported);
  1146. ObjectTypeDB::bind_method(_MD("transform_track_insert_key", "idx", "time", "loc", "rot", "scale"), &Animation::transform_track_insert_key);
  1147. ObjectTypeDB::bind_method(_MD("track_insert_key", "idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1));
  1148. ObjectTypeDB::bind_method(_MD("track_remove_key", "idx", "key_idx"), &Animation::track_remove_key);
  1149. ObjectTypeDB::bind_method(_MD("track_remove_key_at_pos", "idx", "pos"), &Animation::track_remove_key_at_pos);
  1150. ObjectTypeDB::bind_method(_MD("track_set_key_value", "idx", "key", "value"), &Animation::track_set_key_value);
  1151. ObjectTypeDB::bind_method(_MD("track_set_key_transition", "idx", "key_idx", "transition"), &Animation::track_set_key_transition);
  1152. ObjectTypeDB::bind_method(_MD("track_get_key_transition", "idx", "key_idx"), &Animation::track_get_key_transition);
  1153. ObjectTypeDB::bind_method(_MD("track_get_key_count", "idx"), &Animation::track_get_key_count);
  1154. ObjectTypeDB::bind_method(_MD("track_get_key_value", "idx", "key_idx"), &Animation::track_get_key_value);
  1155. ObjectTypeDB::bind_method(_MD("track_get_key_time", "idx", "key_idx"), &Animation::track_get_key_time);
  1156. ObjectTypeDB::bind_method(_MD("track_find_key", "idx", "time", "exact"), &Animation::track_find_key, DEFVAL(false));
  1157. ObjectTypeDB::bind_method(_MD("track_set_interpolation_type", "idx", "interpolation"), &Animation::track_set_interpolation_type);
  1158. ObjectTypeDB::bind_method(_MD("track_get_interpolation_type", "idx"), &Animation::track_get_interpolation_type);
  1159. ObjectTypeDB::bind_method(_MD("transform_track_interpolate", "idx", "time_sec"), &Animation::_transform_track_interpolate);
  1160. ObjectTypeDB::bind_method(_MD("value_track_set_update_mode", "idx", "mode"), &Animation::value_track_set_update_mode);
  1161. ObjectTypeDB::bind_method(_MD("value_track_get_update_mode", "idx"), &Animation::value_track_get_update_mode);
  1162. ObjectTypeDB::bind_method(_MD("value_track_get_key_indices", "idx", "time_sec", "delta"), &Animation::_value_track_get_key_indices);
  1163. ObjectTypeDB::bind_method(_MD("method_track_get_key_indices", "idx", "time_sec", "delta"), &Animation::_method_track_get_key_indices);
  1164. ObjectTypeDB::bind_method(_MD("method_track_get_name", "idx", "key_idx"), &Animation::method_track_get_name);
  1165. ObjectTypeDB::bind_method(_MD("method_track_get_params", "idx", "key_idx"), &Animation::method_track_get_params);
  1166. ObjectTypeDB::bind_method(_MD("set_length", "time_sec"), &Animation::set_length);
  1167. ObjectTypeDB::bind_method(_MD("get_length"), &Animation::get_length);
  1168. ObjectTypeDB::bind_method(_MD("set_loop", "enabled"), &Animation::set_loop);
  1169. ObjectTypeDB::bind_method(_MD("has_loop"), &Animation::has_loop);
  1170. ObjectTypeDB::bind_method(_MD("set_step", "size_sec"), &Animation::set_step);
  1171. ObjectTypeDB::bind_method(_MD("get_step"), &Animation::get_step);
  1172. ObjectTypeDB::bind_method(_MD("clear"), &Animation::clear);
  1173. BIND_CONSTANT(TYPE_VALUE);
  1174. BIND_CONSTANT(TYPE_TRANSFORM);
  1175. BIND_CONSTANT(TYPE_METHOD);
  1176. BIND_CONSTANT(INTERPOLATION_NEAREST);
  1177. BIND_CONSTANT(INTERPOLATION_LINEAR);
  1178. BIND_CONSTANT(INTERPOLATION_CUBIC);
  1179. BIND_CONSTANT(UPDATE_CONTINUOUS);
  1180. BIND_CONSTANT(UPDATE_DISCRETE);
  1181. BIND_CONSTANT(UPDATE_TRIGGER);
  1182. }
  1183. void Animation::clear() {
  1184. for (int i = 0; i < tracks.size(); i++)
  1185. memdelete(tracks[i]);
  1186. tracks.clear();
  1187. loop = false;
  1188. length = 1;
  1189. }
  1190. bool Animation::_transform_track_optimize_key(const TKey<TransformKey> &t0, const TKey<TransformKey> &t1, const TKey<TransformKey> &t2, float p_alowed_linear_err, float p_alowed_angular_err, float p_max_optimizable_angle, const Vector3 &p_norm) {
  1191. real_t c = (t1.time - t0.time) / (t2.time - t0.time);
  1192. real_t t[3] = { -1, -1, -1 };
  1193. { //translation
  1194. const Vector3 &v0 = t0.value.loc;
  1195. const Vector3 &v1 = t1.value.loc;
  1196. const Vector3 &v2 = t2.value.loc;
  1197. if (v0.distance_to(v2) < CMP_EPSILON) {
  1198. //0 and 2 are close, let's see if 1 is close
  1199. if (v0.distance_to(v1) > CMP_EPSILON) {
  1200. //not close, not optimizable
  1201. return false;
  1202. }
  1203. } else {
  1204. Vector3 pd = (v2 - v0);
  1205. float d0 = pd.dot(v0);
  1206. float d1 = pd.dot(v1);
  1207. float d2 = pd.dot(v2);
  1208. if (d1 < d0 || d1 > d2) {
  1209. return false;
  1210. }
  1211. Vector3 s[2] = { v0, v2 };
  1212. real_t d = Geometry::get_closest_point_to_segment(v1, s).distance_to(v1);
  1213. if (d > pd.length() * p_alowed_linear_err) {
  1214. return false; //beyond allowed error for colinearity
  1215. }
  1216. if (p_norm != Vector3() && Math::acos(pd.normalized().dot(p_norm)) > p_alowed_angular_err)
  1217. return false;
  1218. t[0] = (d1 - d0) / (d2 - d0);
  1219. }
  1220. }
  1221. { //rotation
  1222. const Quat &q0 = t0.value.rot;
  1223. const Quat &q1 = t1.value.rot;
  1224. const Quat &q2 = t2.value.rot;
  1225. //localize both to rotation from q0
  1226. if ((q0 - q2).length() < CMP_EPSILON) {
  1227. if ((q0 - q1).length() > CMP_EPSILON)
  1228. return false;
  1229. } else {
  1230. Quat r02 = (q0.inverse() * q2).normalized();
  1231. Quat r01 = (q0.inverse() * q1).normalized();
  1232. Vector3 v02, v01;
  1233. real_t a02, a01;
  1234. r02.get_axis_and_angle(v02, a02);
  1235. r01.get_axis_and_angle(v01, a01);
  1236. if (Math::abs(a02) > p_max_optimizable_angle)
  1237. return false;
  1238. if (v01.dot(v02) < 0) {
  1239. //make sure both rotations go the same way to compare
  1240. v02 = -v02;
  1241. a02 = -a02;
  1242. }
  1243. real_t err_01 = Math::acos(v01.normalized().dot(v02.normalized())) / Math_PI;
  1244. if (err_01 > p_alowed_angular_err) {
  1245. //not rotating in the same axis
  1246. return false;
  1247. }
  1248. if (a01 * a02 < 0) {
  1249. //not rotating in the same direction
  1250. return false;
  1251. }
  1252. real_t tr = a01 / a02;
  1253. if (tr < 0 || tr > 1)
  1254. return false; //rotating too much or too less
  1255. t[1] = tr;
  1256. }
  1257. }
  1258. { //scale
  1259. const Vector3 &v0 = t0.value.scale;
  1260. const Vector3 &v1 = t1.value.scale;
  1261. const Vector3 &v2 = t2.value.scale;
  1262. if (v0.distance_to(v2) < CMP_EPSILON) {
  1263. //0 and 2 are close, let's see if 1 is close
  1264. if (v0.distance_to(v1) > CMP_EPSILON) {
  1265. //not close, not optimizable
  1266. return false;
  1267. }
  1268. } else {
  1269. Vector3 pd = (v2 - v0);
  1270. float d0 = pd.dot(v0);
  1271. float d1 = pd.dot(v1);
  1272. float d2 = pd.dot(v2);
  1273. if (d1 < d0 || d1 > d2) {
  1274. return false; //beyond segment range
  1275. }
  1276. Vector3 s[2] = { v0, v2 };
  1277. real_t d = Geometry::get_closest_point_to_segment(v1, s).distance_to(v1);
  1278. if (d > pd.length() * p_alowed_linear_err) {
  1279. return false; //beyond allowed error for colinearity
  1280. }
  1281. t[2] = (d1 - d0) / (d2 - d0);
  1282. }
  1283. }
  1284. bool erase = false;
  1285. if (t[0] == -1 && t[1] == -1 && t[2] == -1) {
  1286. erase = true;
  1287. } else {
  1288. erase = true;
  1289. real_t lt = -1;
  1290. for (int j = 0; j < 3; j++) {
  1291. //search for t on first, one must be it
  1292. if (t[j] != -1) {
  1293. lt = t[j]; //official t
  1294. //validate rest
  1295. for (int k = j + 1; k < 3; k++) {
  1296. if (t[k] == -1)
  1297. continue;
  1298. if (Math::abs(lt - t[k]) > p_alowed_linear_err) {
  1299. erase = false;
  1300. break;
  1301. }
  1302. }
  1303. break;
  1304. }
  1305. }
  1306. ERR_FAIL_COND_V(lt == -1, false);
  1307. if (erase) {
  1308. if (Math::abs(lt - c) > p_alowed_linear_err) {
  1309. //todo, evaluate changing the transition if this fails?
  1310. //this could be done as a second pass and would be
  1311. //able to optimize more
  1312. erase = false;
  1313. } else {
  1314. //print_line(itos(i)+"because of interp");
  1315. }
  1316. }
  1317. }
  1318. return erase;
  1319. }
  1320. void Animation::_transform_track_optimize(int p_idx, float p_alowed_linear_err, float p_alowed_angular_err, float p_max_optimizable_angle) {
  1321. ERR_FAIL_INDEX(p_idx, tracks.size());
  1322. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_TRANSFORM);
  1323. TransformTrack *tt = static_cast<TransformTrack *>(tracks[p_idx]);
  1324. bool prev_erased = false;
  1325. TKey<TransformKey> first_erased;
  1326. Vector3 norm;
  1327. for (int i = 1; i < tt->transforms.size() - 1; i++) {
  1328. TKey<TransformKey> &t0 = tt->transforms[i - 1];
  1329. TKey<TransformKey> &t1 = tt->transforms[i];
  1330. TKey<TransformKey> &t2 = tt->transforms[i + 1];
  1331. bool erase = _transform_track_optimize_key(t0, t1, t2, p_alowed_linear_err, p_alowed_angular_err, p_max_optimizable_angle, norm);
  1332. if (erase && !prev_erased) {
  1333. norm = (t2.value.loc - t1.value.loc).normalized();
  1334. }
  1335. if (prev_erased && !_transform_track_optimize_key(t0, first_erased, t2, p_alowed_linear_err, p_alowed_angular_err, p_max_optimizable_angle, norm)) {
  1336. //avoid error to go beyond first erased key
  1337. erase = false;
  1338. }
  1339. if (erase) {
  1340. if (!prev_erased) {
  1341. first_erased = t1;
  1342. prev_erased = true;
  1343. }
  1344. tt->transforms.remove(i);
  1345. i--;
  1346. } else {
  1347. prev_erased = false;
  1348. norm = Vector3();
  1349. }
  1350. // print_line(itos(i)+" could be eliminated: "+rtos(tr));
  1351. //}
  1352. }
  1353. }
  1354. void Animation::optimize(float p_allowed_linear_err, float p_allowed_angular_err, float p_angle_max) {
  1355. for (int i = 0; i < tracks.size(); i++) {
  1356. if (tracks[i]->type == TYPE_TRANSFORM)
  1357. _transform_track_optimize(i, p_allowed_linear_err, p_allowed_angular_err, p_angle_max);
  1358. }
  1359. }
  1360. Animation::Animation() {
  1361. step = 0.1;
  1362. loop = false;
  1363. length = 1;
  1364. }
  1365. Animation::~Animation() {
  1366. for (int i = 0; i < tracks.size(); i++)
  1367. memdelete(tracks[i]);
  1368. }