animation.cpp 48 KB

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