animation.cpp 50 KB

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