animation.cpp 44 KB

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