collada.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  1. /*************************************************************************/
  2. /* collada.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef TOOLS_ENABLED
  31. #include "collada.h"
  32. #include "stdio.h"
  33. //#define DEBUG_DEFAULT_ANIMATION
  34. //#define DEBUG_COLLADA
  35. #ifdef DEBUG_COLLADA
  36. #define COLLADA_PRINT(m_what) print_line(m_what)
  37. #else
  38. #define COLLADA_PRINT(m_what)
  39. #endif
  40. #define COLLADA_IMPORT_SCALE_SCENE
  41. /* HELPERS */
  42. String Collada::Effect::get_texture_path(const String &p_source, Collada &state) const {
  43. String image = p_source;
  44. ERR_FAIL_COND_V(!state.state.image_map.has(image), "");
  45. return state.state.image_map[image].path;
  46. }
  47. Transform Collada::get_root_transform() const {
  48. Transform unit_scale_transform;
  49. #ifndef COLLADA_IMPORT_SCALE_SCENE
  50. unit_scale_transform.scale(Vector3(state.unit_scale, state.unit_scale, state.unit_scale));
  51. #endif
  52. return unit_scale_transform;
  53. }
  54. void Collada::Vertex::fix_unit_scale(Collada &state) {
  55. #ifdef COLLADA_IMPORT_SCALE_SCENE
  56. vertex *= state.state.unit_scale;
  57. #endif
  58. }
  59. static String _uri_to_id(const String &p_uri) {
  60. if (p_uri.begins_with("#"))
  61. return p_uri.substr(1, p_uri.size() - 1);
  62. else
  63. return p_uri;
  64. }
  65. /** HELPER FUNCTIONS **/
  66. Transform Collada::fix_transform(const Transform &p_transform) {
  67. Transform tr = p_transform;
  68. #ifndef NO_UP_AXIS_SWAP
  69. if (state.up_axis != Vector3::AXIS_Y) {
  70. for (int i = 0; i < 3; i++)
  71. SWAP(tr.basis[1][i], tr.basis[state.up_axis][i]);
  72. for (int i = 0; i < 3; i++)
  73. SWAP(tr.basis[i][1], tr.basis[i][state.up_axis]);
  74. SWAP(tr.origin[1], tr.origin[state.up_axis]);
  75. tr.basis[state.up_axis][0] = -tr.basis[state.up_axis][0];
  76. tr.basis[state.up_axis][1] = -tr.basis[state.up_axis][1];
  77. tr.basis[0][state.up_axis] = -tr.basis[0][state.up_axis];
  78. tr.basis[1][state.up_axis] = -tr.basis[1][state.up_axis];
  79. tr.origin[state.up_axis] = -tr.origin[state.up_axis];
  80. }
  81. #endif
  82. // tr.scale(Vector3(state.unit_scale.unit_scale.unit_scale));
  83. return tr;
  84. //return state.matrix_fix * p_transform;
  85. }
  86. static Transform _read_transform_from_array(const Vector<float> &array, int ofs = 0) {
  87. Transform tr;
  88. // i wonder why collada matrices are transposed, given that's opposed to opengl..
  89. tr.basis.elements[0][0] = array[0 + ofs];
  90. tr.basis.elements[0][1] = array[1 + ofs];
  91. tr.basis.elements[0][2] = array[2 + ofs];
  92. tr.basis.elements[1][0] = array[4 + ofs];
  93. tr.basis.elements[1][1] = array[5 + ofs];
  94. tr.basis.elements[1][2] = array[6 + ofs];
  95. tr.basis.elements[2][0] = array[8 + ofs];
  96. tr.basis.elements[2][1] = array[9 + ofs];
  97. tr.basis.elements[2][2] = array[10 + ofs];
  98. tr.origin.x = array[3 + ofs];
  99. tr.origin.y = array[7 + ofs];
  100. tr.origin.z = array[11 + ofs];
  101. return tr;
  102. }
  103. /* STRUCTURES */
  104. Transform Collada::Node::compute_transform(Collada &state) const {
  105. Transform xform;
  106. for (int i = 0; i < xform_list.size(); i++) {
  107. Transform xform_step;
  108. const XForm &xf = xform_list[i];
  109. switch (xf.op) {
  110. case XForm::OP_ROTATE: {
  111. if (xf.data.size() >= 4) {
  112. xform_step.rotate(Vector3(xf.data[0], xf.data[1], xf.data[2]), -Math::deg2rad(xf.data[3]));
  113. }
  114. } break;
  115. case XForm::OP_SCALE: {
  116. if (xf.data.size() >= 3) {
  117. xform_step.scale(Vector3(xf.data[0], xf.data[1], xf.data[2]));
  118. }
  119. } break;
  120. case XForm::OP_TRANSLATE: {
  121. if (xf.data.size() >= 3) {
  122. xform_step.origin = Vector3(xf.data[0], xf.data[1], xf.data[2]);
  123. }
  124. } break;
  125. case XForm::OP_MATRIX: {
  126. if (xf.data.size() >= 16) {
  127. xform_step = _read_transform_from_array(xf.data, 0);
  128. }
  129. } break;
  130. default: {
  131. }
  132. }
  133. xform = xform * xform_step;
  134. }
  135. #ifdef COLLADA_IMPORT_SCALE_SCENE
  136. xform.origin *= state.state.unit_scale;
  137. #endif
  138. return xform;
  139. }
  140. Transform Collada::Node::get_transform() const {
  141. return default_transform;
  142. }
  143. Transform Collada::Node::get_global_transform() const {
  144. if (parent)
  145. return parent->get_global_transform() * default_transform;
  146. else
  147. return default_transform;
  148. }
  149. Vector<float> Collada::AnimationTrack::get_value_at_time(float p_time) {
  150. ERR_FAIL_COND_V(keys.size() == 0, Vector<float>());
  151. int i = 0;
  152. for (i = 0; i < keys.size(); i++) {
  153. if (keys[i].time > p_time)
  154. break;
  155. }
  156. if (i == 0)
  157. return keys[0].data;
  158. if (i == keys.size())
  159. return keys[keys.size() - 1].data;
  160. switch (keys[i].interp_type) {
  161. case INTERP_BEZIER: //wait for bezier
  162. case INTERP_LINEAR: {
  163. float c = (p_time - keys[i - 1].time) / (keys[i].time - keys[i - 1].time);
  164. if (keys[i].data.size() == 16) {
  165. //interpolate a matrix
  166. Transform src = _read_transform_from_array(keys[i - 1].data);
  167. Transform dst = _read_transform_from_array(keys[i].data);
  168. Transform interp = c < 0.001 ? src : src.interpolate_with(dst, c);
  169. Vector<float> ret;
  170. ret.resize(16);
  171. Transform tr;
  172. // i wonder why collada matrices are transposed, given that's opposed to opengl..
  173. ret[0] = interp.basis.elements[0][0];
  174. ret[1] = interp.basis.elements[0][1];
  175. ret[2] = interp.basis.elements[0][2];
  176. ret[4] = interp.basis.elements[1][0];
  177. ret[5] = interp.basis.elements[1][1];
  178. ret[6] = interp.basis.elements[1][2];
  179. ret[8] = interp.basis.elements[2][0];
  180. ret[9] = interp.basis.elements[2][1];
  181. ret[10] = interp.basis.elements[2][2];
  182. ret[3] = interp.origin.x;
  183. ret[7] = interp.origin.y;
  184. ret[11] = interp.origin.z;
  185. ret[12] = 0;
  186. ret[13] = 0;
  187. ret[14] = 0;
  188. ret[15] = 1;
  189. return ret;
  190. } else {
  191. Vector<float> dest;
  192. dest.resize(keys[i].data.size());
  193. for (int j = 0; j < dest.size(); j++) {
  194. dest[j] = keys[i].data[j] * c + keys[i - 1].data[j] * (1.0 - c);
  195. }
  196. return dest;
  197. //interpolate one by one
  198. }
  199. } break;
  200. }
  201. ERR_FAIL_V(Vector<float>());
  202. }
  203. void Collada::_parse_asset(XMLParser &parser) {
  204. while (parser.read() == OK) {
  205. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  206. String name = parser.get_node_name();
  207. if (name == "up_axis") {
  208. parser.read();
  209. if (parser.get_node_data() == "X_UP")
  210. state.up_axis = Vector3::AXIS_X;
  211. if (parser.get_node_data() == "Y_UP")
  212. state.up_axis = Vector3::AXIS_Y;
  213. if (parser.get_node_data() == "Z_UP")
  214. state.up_axis = Vector3::AXIS_Z;
  215. COLLADA_PRINT("up axis: " + parser.get_node_data());
  216. } else if (name == "unit") {
  217. state.unit_scale = parser.get_attribute_value("meter").to_double();
  218. COLLADA_PRINT("unit scale: " + rtos(state.unit_scale));
  219. }
  220. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "asset")
  221. break; //end of <asset>
  222. }
  223. }
  224. void Collada::_parse_image(XMLParser &parser) {
  225. String id = parser.get_attribute_value("id");
  226. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  227. if (!parser.is_empty())
  228. parser.skip_section();
  229. return;
  230. }
  231. Image image;
  232. if (state.version < State::Version(1, 4, 0)) {
  233. /* <1.4 */
  234. String path = parser.get_attribute_value("source").strip_edges();
  235. if (path.find("://") == -1 && path.is_rel_path()) {
  236. // path is relative to file being loaded, so convert to a resource path
  237. image.path = Globals::get_singleton()->localize_path(state.local_path.get_base_dir() + "/" + path.percent_decode());
  238. }
  239. } else {
  240. while (parser.read() == OK) {
  241. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  242. String name = parser.get_node_name();
  243. if (name == "init_from") {
  244. parser.read();
  245. String path = parser.get_node_data().strip_edges().percent_decode();
  246. if (path.find("://") == -1 && path.is_rel_path()) {
  247. // path is relative to file being loaded, so convert to a resource path
  248. path = Globals::get_singleton()->localize_path(state.local_path.get_base_dir() + "/" + path);
  249. } else if (path.find("file:///") == 0) {
  250. path = path.replace_first("file:///", "");
  251. path = Globals::get_singleton()->localize_path(path);
  252. }
  253. image.path = path;
  254. }
  255. if (name == "data") {
  256. ERR_PRINT("COLLADA Embedded image data not supported!");
  257. } else if (name == "extra" && !parser.is_empty())
  258. parser.skip_section();
  259. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "image")
  260. break; //end of <asset>
  261. }
  262. }
  263. state.image_map[id] = image;
  264. }
  265. void Collada::_parse_material(XMLParser &parser) {
  266. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  267. if (!parser.is_empty())
  268. parser.skip_section();
  269. return;
  270. }
  271. Material material;
  272. String id = parser.get_attribute_value("id");
  273. if (parser.has_attribute("name"))
  274. material.name = parser.get_attribute_value("name");
  275. if (state.version < State::Version(1, 4, 0)) {
  276. /* <1.4 */
  277. ERR_PRINT("Collada Materials < 1.4 are not supported (yet)");
  278. } else {
  279. while (parser.read() == OK) {
  280. if (parser.get_node_type() == XMLParser::NODE_ELEMENT && parser.get_node_name() == "instance_effect") {
  281. material.instance_effect = _uri_to_id(parser.get_attribute_value("url"));
  282. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "material")
  283. break; //end of <asset>
  284. }
  285. }
  286. state.material_map[id] = material;
  287. }
  288. //! reads floats from inside of xml element until end of xml element
  289. Vector<float> Collada::_read_float_array(XMLParser &parser) {
  290. if (parser.is_empty())
  291. return Vector<float>();
  292. Vector<String> splitters;
  293. splitters.push_back(" ");
  294. splitters.push_back("\n");
  295. splitters.push_back("\r");
  296. splitters.push_back("\t");
  297. Vector<float> array;
  298. while (parser.read() == OK) {
  299. // TODO: check for comments inside the element
  300. // and ignore them.
  301. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  302. // parse float data
  303. String str = parser.get_node_data();
  304. array = str.split_floats_mk(splitters, false);
  305. //array=str.split_floats(" ",false);
  306. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END)
  307. break; // end parsing text
  308. }
  309. return array;
  310. }
  311. Vector<String> Collada::_read_string_array(XMLParser &parser) {
  312. if (parser.is_empty())
  313. return Vector<String>();
  314. Vector<String> array;
  315. while (parser.read() == OK) {
  316. // TODO: check for comments inside the element
  317. // and ignore them.
  318. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  319. // parse String data
  320. String str = parser.get_node_data();
  321. array = str.split_spaces();
  322. //for(int i=0;i<array.size();i++) {
  323. // print_line(itos(i)+": "+array[i]);
  324. //}
  325. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END)
  326. break; // end parsing text
  327. }
  328. return array;
  329. }
  330. Transform Collada::_read_transform(XMLParser &parser) {
  331. if (parser.is_empty())
  332. return Transform();
  333. Vector<float> array;
  334. while (parser.read() == OK) {
  335. // TODO: check for comments inside the element
  336. // and ignore them.
  337. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  338. // parse float data
  339. String str = parser.get_node_data();
  340. array = str.split_floats(" ", false);
  341. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END)
  342. break; // end parsing text
  343. }
  344. ERR_FAIL_COND_V(array.size() != 16, Transform());
  345. return _read_transform_from_array(array);
  346. }
  347. String Collada::_read_empty_draw_type(XMLParser &parser) {
  348. String empty_draw_type = "";
  349. if (parser.is_empty())
  350. return empty_draw_type;
  351. while (parser.read() == OK) {
  352. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  353. empty_draw_type = parser.get_node_data();
  354. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END)
  355. break; // end parsing text
  356. }
  357. return empty_draw_type;
  358. }
  359. Variant Collada::_parse_param(XMLParser &parser) {
  360. if (parser.is_empty())
  361. return Variant();
  362. String from = parser.get_node_name();
  363. Variant data;
  364. while (parser.read() == OK) {
  365. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  366. if (parser.get_node_name() == "float") {
  367. parser.read();
  368. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  369. data = parser.get_node_data().to_double();
  370. }
  371. } else if (parser.get_node_name() == "float2") {
  372. Vector<float> v2 = _read_float_array(parser);
  373. if (v2.size() >= 2) {
  374. data = Vector2(v2[0], v2[1]);
  375. }
  376. } else if (parser.get_node_name() == "float3") {
  377. Vector<float> v3 = _read_float_array(parser);
  378. if (v3.size() >= 3) {
  379. data = Vector3(v3[0], v3[1], v3[2]);
  380. }
  381. } else if (parser.get_node_name() == "float4") {
  382. Vector<float> v4 = _read_float_array(parser);
  383. if (v4.size() >= 4) {
  384. data = Color(v4[0], v4[1], v4[2], v4[3]);
  385. }
  386. } else if (parser.get_node_name() == "sampler2D") {
  387. while (parser.read() == OK) {
  388. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  389. if (parser.get_node_name() == "source") {
  390. parser.read();
  391. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  392. data = parser.get_node_data();
  393. }
  394. }
  395. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "sampler2D")
  396. break;
  397. }
  398. } else if (parser.get_node_name() == "surface") {
  399. while (parser.read() == OK) {
  400. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  401. if (parser.get_node_name() == "init_from") {
  402. parser.read();
  403. if (parser.get_node_type() == XMLParser::NODE_TEXT) {
  404. data = parser.get_node_data();
  405. }
  406. }
  407. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "surface")
  408. break;
  409. }
  410. }
  411. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == from)
  412. break;
  413. }
  414. COLLADA_PRINT("newparam ending " + parser.get_node_name());
  415. return data;
  416. }
  417. void Collada::_parse_effect_material(XMLParser &parser, Effect &effect, String &id) {
  418. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  419. if (!parser.is_empty())
  420. parser.skip_section();
  421. return;
  422. }
  423. while (parser.read() == OK) {
  424. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  425. // first come the tags we descend, but ignore the top-levels
  426. COLLADA_PRINT("node name: " + parser.get_node_name());
  427. if (!parser.is_empty() && (parser.get_node_name() == "profile_COMMON" || parser.get_node_name() == "technique" || parser.get_node_name() == "extra")) {
  428. _parse_effect_material(parser, effect, id); // try again
  429. } else if (parser.get_node_name() == "newparam") {
  430. String name = parser.get_attribute_value("sid");
  431. Variant value = _parse_param(parser);
  432. effect.params[name] = value;
  433. COLLADA_PRINT("param: " + name + " value:" + String(value));
  434. } else if (parser.get_node_name() == "constant" ||
  435. parser.get_node_name() == "lambert" ||
  436. parser.get_node_name() == "phong" ||
  437. parser.get_node_name() == "blinn") {
  438. COLLADA_PRINT("shade model: " + parser.get_node_name());
  439. while (parser.read() == OK) {
  440. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  441. String what = parser.get_node_name();
  442. if (what == "emission" ||
  443. what == "diffuse" ||
  444. what == "specular" ||
  445. what == "reflective") {
  446. // color or texture types
  447. while (parser.read() == OK) {
  448. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  449. if (parser.get_node_name() == "color") {
  450. Vector<float> colorarr = _read_float_array(parser);
  451. COLLADA_PRINT("colorarr size: " + rtos(colorarr.size()));
  452. if (colorarr.size() >= 3) {
  453. // alpha strangely not allright? maybe it needs to be multiplied by value as a channel intensity
  454. Color color(colorarr[0], colorarr[1], colorarr[2], 1.0);
  455. if (what == "diffuse")
  456. effect.diffuse.color = color;
  457. if (what == "specular")
  458. effect.specular.color = color;
  459. if (what == "emission")
  460. effect.emission.color = color;
  461. COLLADA_PRINT(what + " color: " + color);
  462. }
  463. } else if (parser.get_node_name() == "texture") {
  464. String sampler = parser.get_attribute_value("texture");
  465. if (!effect.params.has(sampler)) {
  466. ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data());
  467. } else {
  468. String surface = effect.params[sampler];
  469. if (!effect.params.has(surface)) {
  470. ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data());
  471. } else {
  472. String uri = effect.params[surface];
  473. if (what == "diffuse") {
  474. effect.diffuse.texture = uri;
  475. } else if (what == "specular") {
  476. effect.specular.texture = uri;
  477. } else if (what == "emission") {
  478. effect.emission.texture = uri;
  479. } else if (what == "bump") {
  480. if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") {
  481. WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported.")
  482. }
  483. effect.bump.texture = uri;
  484. }
  485. COLLADA_PRINT(what + " texture: " + uri);
  486. }
  487. }
  488. } else if (!parser.is_empty())
  489. parser.skip_section();
  490. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == what)
  491. break;
  492. }
  493. } else if (what == "shininess") {
  494. #if 1
  495. effect.shininess = _parse_param(parser);
  496. #else
  497. parser.read();
  498. float shininess = parser.get_node_data().to_double();
  499. effect.shininess = shininess;
  500. COLLADA_PRINT("shininess: " + rtos(shininess));
  501. #endif
  502. }
  503. }
  504. if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && (parser.get_node_name() == "constant" ||
  505. parser.get_node_name() == "lambert" ||
  506. parser.get_node_name() == "phong" ||
  507. parser.get_node_name() == "blinn"))
  508. break;
  509. }
  510. } else if (parser.get_node_name() == "double_sided" || parser.get_node_name() == "show_double_sided") { // colladamax / google earth
  511. // 3DS Max / Google Earth double sided extension
  512. parser.read();
  513. effect.found_double_sided = true;
  514. effect.double_sided = parser.get_node_data().to_int();
  515. COLLADA_PRINT("double sided: " + itos(parser.get_node_data().to_int()));
  516. } else if (parser.get_node_name() == "unshaded") {
  517. parser.read();
  518. effect.unshaded = parser.get_node_data().to_int();
  519. } else if (parser.get_node_name() == "bump") {
  520. // color or texture types
  521. while (parser.read() == OK) {
  522. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  523. if (parser.get_node_name() == "texture") {
  524. String sampler = parser.get_attribute_value("texture");
  525. if (!effect.params.has(sampler)) {
  526. ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data());
  527. } else {
  528. String surface = effect.params[sampler];
  529. if (!effect.params.has(surface)) {
  530. ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data());
  531. } else {
  532. String uri = effect.params[surface];
  533. if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") {
  534. WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported.")
  535. }
  536. effect.bump.texture = uri;
  537. COLLADA_PRINT(" bump: " + uri);
  538. }
  539. }
  540. } else if (!parser.is_empty())
  541. parser.skip_section();
  542. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "bump")
  543. break;
  544. }
  545. } else if (!parser.is_empty())
  546. parser.skip_section();
  547. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END &&
  548. (parser.get_node_name() == "effect" ||
  549. parser.get_node_name() == "profile_COMMON" ||
  550. parser.get_node_name() == "technique" ||
  551. parser.get_node_name() == "extra"))
  552. break;
  553. }
  554. }
  555. void Collada::_parse_effect(XMLParser &parser) {
  556. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  557. if (!parser.is_empty())
  558. parser.skip_section();
  559. return;
  560. }
  561. String id = parser.get_attribute_value("id");
  562. Effect effect;
  563. if (parser.has_attribute("name"))
  564. effect.name = parser.get_attribute_value("name");
  565. _parse_effect_material(parser, effect, id);
  566. state.effect_map[id] = effect;
  567. COLLADA_PRINT("Effect ID:" + id);
  568. }
  569. void Collada::_parse_camera(XMLParser &parser) {
  570. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  571. if (!parser.is_empty())
  572. parser.skip_section();
  573. return;
  574. }
  575. String id = parser.get_attribute_value("id");
  576. state.camera_data_map[id] = CameraData();
  577. CameraData &camera = state.camera_data_map[id];
  578. while (parser.read() == OK) {
  579. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  580. String name = parser.get_node_name();
  581. if (name == "perspective") {
  582. camera.mode = CameraData::MODE_PERSPECTIVE;
  583. } else if (name == "orthographic") {
  584. camera.mode = CameraData::MODE_ORTHOGONAL;
  585. } else if (name == "xfov") {
  586. parser.read();
  587. camera.perspective.x_fov = parser.get_node_data().to_double();
  588. } else if (name == "yfov") {
  589. parser.read();
  590. camera.perspective.y_fov = parser.get_node_data().to_double();
  591. } else if (name == "xmag") {
  592. parser.read();
  593. camera.orthogonal.x_mag = parser.get_node_data().to_double();
  594. } else if (name == "ymag") {
  595. parser.read();
  596. camera.orthogonal.y_mag = parser.get_node_data().to_double();
  597. } else if (name == "aspect_ratio") {
  598. parser.read();
  599. camera.aspect = parser.get_node_data().to_double();
  600. } else if (name == "znear") {
  601. parser.read();
  602. camera.z_near = parser.get_node_data().to_double();
  603. } else if (name == "zfar") {
  604. parser.read();
  605. camera.z_far = parser.get_node_data().to_double();
  606. }
  607. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "camera")
  608. break; //end of <asset>
  609. }
  610. COLLADA_PRINT("Camera ID:" + id);
  611. }
  612. void Collada::_parse_light(XMLParser &parser) {
  613. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  614. if (!parser.is_empty())
  615. parser.skip_section();
  616. return;
  617. }
  618. String id = parser.get_attribute_value("id");
  619. state.light_data_map[id] = LightData();
  620. LightData &light = state.light_data_map[id];
  621. while (parser.read() == OK) {
  622. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  623. String name = parser.get_node_name();
  624. if (name == "ambient") {
  625. light.mode = LightData::MODE_AMBIENT;
  626. } else if (name == "directional") {
  627. light.mode = LightData::MODE_DIRECTIONAL;
  628. } else if (name == "point") {
  629. light.mode = LightData::MODE_OMNI;
  630. } else if (name == "spot") {
  631. light.mode = LightData::MODE_SPOT;
  632. } else if (name == "color") {
  633. parser.read();
  634. Vector<float> colorarr = _read_float_array(parser);
  635. COLLADA_PRINT("colorarr size: " + rtos(colorarr.size()));
  636. if (colorarr.size() >= 4) {
  637. // alpha strangely not allright? maybe it needs to be multiplied by value as a channel intensity
  638. Color color(colorarr[0], colorarr[1], colorarr[2], 1.0);
  639. light.color = color;
  640. }
  641. } else if (name == "constant_attenuation") {
  642. parser.read();
  643. light.constant_att = parser.get_node_data().to_double();
  644. } else if (name == "linear_attenuation") {
  645. parser.read();
  646. light.linear_att = parser.get_node_data().to_double();
  647. } else if (name == "quadratic_attenuation") {
  648. parser.read();
  649. light.quad_att = parser.get_node_data().to_double();
  650. } else if (name == "falloff_angle") {
  651. parser.read();
  652. light.spot_angle = parser.get_node_data().to_double();
  653. } else if (name == "falloff_exponent") {
  654. parser.read();
  655. light.spot_exp = parser.get_node_data().to_double();
  656. }
  657. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "light")
  658. break; //end of <asset>
  659. }
  660. COLLADA_PRINT("Light ID:" + id);
  661. }
  662. void Collada::_parse_curve_geometry(XMLParser &parser, String p_id, String p_name) {
  663. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  664. if (!parser.is_empty())
  665. parser.skip_section();
  666. return;
  667. }
  668. //load everything into a pre dictionary
  669. state.curve_data_map[p_id] = CurveData();
  670. CurveData &curvedata = state.curve_data_map[p_id];
  671. curvedata.name = p_name;
  672. COLLADA_PRINT("curve name: " + p_name);
  673. String current_source;
  674. // handles geometry node and the curve childs in this loop
  675. // read sources with arrays and accessor for each curve
  676. if (parser.is_empty()) {
  677. return;
  678. }
  679. while (parser.read() == OK) {
  680. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  681. String section = parser.get_node_name();
  682. if (section == "source") {
  683. String id = parser.get_attribute_value("id");
  684. curvedata.sources[id] = CurveData::Source();
  685. current_source = id;
  686. COLLADA_PRINT("source data: " + id);
  687. } else if (section == "float_array" || section == "array") {
  688. // create a new array and read it.
  689. if (curvedata.sources.has(current_source)) {
  690. curvedata.sources[current_source].array = _read_float_array(parser);
  691. COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values.");
  692. }
  693. } else if (section == "Name_array") {
  694. // create a new array and read it.
  695. if (curvedata.sources.has(current_source)) {
  696. curvedata.sources[current_source].sarray = _read_string_array(parser);
  697. COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values.");
  698. }
  699. } else if (section == "technique_common") {
  700. //skip it
  701. } else if (section == "accessor") { // child of source (below a technique tag)
  702. if (curvedata.sources.has(current_source)) {
  703. curvedata.sources[current_source].stride = parser.get_attribute_value("stride").to_int();
  704. COLLADA_PRINT("section: " + current_source + " stride " + itos(curvedata.sources[current_source].stride));
  705. }
  706. } else if (section == "control_vertices") {
  707. while (parser.read() == OK) {
  708. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  709. if (parser.get_node_name() == "input") {
  710. String semantic = parser.get_attribute_value("semantic");
  711. String source = _uri_to_id(parser.get_attribute_value("source"));
  712. curvedata.control_vertices[semantic] = source;
  713. COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source);
  714. }
  715. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section)
  716. break;
  717. }
  718. } else if (!parser.is_empty()) {
  719. parser.skip_section();
  720. }
  721. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "spline")
  722. break;
  723. }
  724. }
  725. void Collada::_parse_mesh_geometry(XMLParser &parser, String p_id, String p_name) {
  726. if (!(state.import_flags & IMPORT_FLAG_SCENE)) {
  727. if (!parser.is_empty())
  728. parser.skip_section();
  729. return;
  730. }
  731. //load everything into a pre dictionary
  732. state.mesh_data_map[p_id] = MeshData();
  733. MeshData &meshdata = state.mesh_data_map[p_id];
  734. meshdata.name = p_name;
  735. COLLADA_PRINT("mesh name: " + p_name);
  736. String current_source;
  737. // handles geometry node and the mesh childs in this loop
  738. // read sources with arrays and accessor for each mesh
  739. if (parser.is_empty()) {
  740. return;
  741. }
  742. while (parser.read() == OK) {
  743. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  744. String section = parser.get_node_name();
  745. if (section == "source") {
  746. String id = parser.get_attribute_value("id");
  747. meshdata.sources[id] = MeshData::Source();
  748. current_source = id;
  749. COLLADA_PRINT("source data: " + id);
  750. } else if (section == "float_array" || section == "array" || section == "float_array") {
  751. // create a new array and read it.
  752. if (meshdata.sources.has(current_source)) {
  753. meshdata.sources[current_source].array = _read_float_array(parser);
  754. COLLADA_PRINT("section: " + current_source + " read " + itos(meshdata.sources[current_source].array.size()) + " values.");
  755. }
  756. } else if (section == "technique_common") {
  757. //skip it
  758. } else if (section == "accessor") { // child of source (below a technique tag)
  759. if (meshdata.sources.has(current_source)) {
  760. meshdata.sources[current_source].stride = parser.get_attribute_value("stride").to_int();
  761. COLLADA_PRINT("section: " + current_source + " stride " + itos(meshdata.sources[current_source].stride));
  762. }
  763. } else if (section == "vertices") {
  764. MeshData::Vertices vert;
  765. String id = parser.get_attribute_value("id");
  766. while (parser.read() == OK) {
  767. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  768. if (parser.get_node_name() == "input") {
  769. String semantic = parser.get_attribute_value("semantic");
  770. String source = _uri_to_id(parser.get_attribute_value("source"));
  771. vert.sources[semantic] = source;
  772. COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source);
  773. }
  774. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section)
  775. break;
  776. }
  777. meshdata.vertices[id] = vert;
  778. } else if (section == "triangles" || section == "polylist" || section == "polygons") {
  779. bool polygons = (section == "polygons");
  780. if (polygons) {
  781. WARN_PRINT("Primitive type \"polygons\" is not well supported (concave shapes may fail). To ensure that the geometry is properly imported, please re-export using \"triangles\" or \"polylist\".");
  782. }
  783. MeshData::Primitives prim;
  784. if (parser.has_attribute("material"))
  785. prim.material = parser.get_attribute_value("material");
  786. prim.count = parser.get_attribute_value("count").to_int();
  787. prim.vertex_size = 0;
  788. int last_ref = 0;
  789. while (parser.read() == OK) {
  790. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  791. if (parser.get_node_name() == "input") {
  792. String semantic = parser.get_attribute_value("semantic");
  793. String source = _uri_to_id(parser.get_attribute_value("source"));
  794. if (semantic == "TEXCOORD") {
  795. /*
  796. if (parser.has_attribute("set"))// a texcoord
  797. semantic+=parser.get_attribute_value("set");
  798. else
  799. semantic="TEXCOORD0";*/
  800. semantic = "TEXCOORD" + itos(last_ref++);
  801. }
  802. int offset = parser.get_attribute_value("offset").to_int();
  803. MeshData::Primitives::SourceRef sref;
  804. sref.source = source;
  805. sref.offset = offset;
  806. prim.sources[semantic] = sref;
  807. prim.vertex_size = MAX(prim.vertex_size, offset + 1);
  808. COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset));
  809. } else if (parser.get_node_name() == "p") { //indices
  810. Vector<float> values = _read_float_array(parser);
  811. if (polygons) {
  812. prim.polygons.push_back(values.size() / prim.vertex_size);
  813. int from = prim.indices.size();
  814. prim.indices.resize(from + values.size());
  815. for (int i = 0; i < values.size(); i++)
  816. prim.indices[from + i] = values[i];
  817. } else if (prim.vertex_size > 0) {
  818. prim.indices = values;
  819. }
  820. COLLADA_PRINT("read " + itos(values.size()) + " index values");
  821. } else if (parser.get_node_name() == "vcount") { // primitive
  822. Vector<float> values = _read_float_array(parser);
  823. prim.polygons = values;
  824. COLLADA_PRINT("read " + itos(values.size()) + " polygon values");
  825. }
  826. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section)
  827. break;
  828. }
  829. meshdata.primitives.push_back(prim);
  830. } else if (parser.get_node_name() == "double_sided") {
  831. parser.read();
  832. meshdata.found_double_sided = true;
  833. meshdata.double_sided = parser.get_node_data().to_int();
  834. } else if (parser.get_node_name() == "polygons") {
  835. ERR_PRINT("Primitive type \"polygons\" not supported, re-export using \"polylist\" or \"triangles\".");
  836. } else if (!parser.is_empty()) {
  837. parser.skip_section();
  838. }
  839. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "mesh")
  840. break;
  841. }
  842. }
  843. void Collada::_parse_skin_controller(XMLParser &parser, String p_id) {
  844. state.skin_controller_data_map[p_id] = SkinControllerData();
  845. SkinControllerData &skindata = state.skin_controller_data_map[p_id];
  846. skindata.base = _uri_to_id(parser.get_attribute_value("source"));
  847. String current_source;
  848. while (parser.read() == OK) {
  849. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  850. String section = parser.get_node_name();
  851. if (section == "bind_shape_matrix") {
  852. skindata.bind_shape = _read_transform(parser);
  853. #ifdef COLLADA_IMPORT_SCALE_SCENE
  854. skindata.bind_shape.origin *= state.unit_scale;
  855. #endif
  856. COLLADA_PRINT("skeleton bind shape transform: " + skindata.bind_shape);
  857. } else if (section == "source") {
  858. String id = parser.get_attribute_value("id");
  859. skindata.sources[id] = SkinControllerData::Source();
  860. current_source = id;
  861. COLLADA_PRINT("source data: " + id);
  862. } else if (section == "float_array" || section == "array") {
  863. // create a new array and read it.
  864. if (skindata.sources.has(current_source)) {
  865. skindata.sources[current_source].array = _read_float_array(parser);
  866. COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values.");
  867. }
  868. } else if (section == "Name_array" || section == "IDREF_array") {
  869. // create a new array and read it.
  870. if (section == "IDREF_array")
  871. skindata.use_idrefs = true;
  872. if (skindata.sources.has(current_source)) {
  873. skindata.sources[current_source].sarray = _read_string_array(parser);
  874. if (section == "IDREF_array") {
  875. Vector<String> sa = skindata.sources[current_source].sarray;
  876. for (int i = 0; i < sa.size(); i++)
  877. state.idref_joints.insert(sa[i]);
  878. }
  879. COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values.");
  880. }
  881. } else if (section == "technique_common") {
  882. //skip it
  883. } else if (section == "accessor") { // child of source (below a technique tag)
  884. if (skindata.sources.has(current_source)) {
  885. int stride = 1;
  886. if (parser.has_attribute("stride"))
  887. stride = parser.get_attribute_value("stride").to_int();
  888. skindata.sources[current_source].stride = stride;
  889. COLLADA_PRINT("section: " + current_source + " stride " + itos(skindata.sources[current_source].stride));
  890. }
  891. } else if (section == "joints") {
  892. SkinControllerData::Joints joint;
  893. while (parser.read() == OK) {
  894. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  895. if (parser.get_node_name() == "input") {
  896. String semantic = parser.get_attribute_value("semantic");
  897. String source = _uri_to_id(parser.get_attribute_value("source"));
  898. joint.sources[semantic] = source;
  899. COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source);
  900. }
  901. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section)
  902. break;
  903. }
  904. skindata.joints = joint;
  905. } else if (section == "vertex_weights") {
  906. SkinControllerData::Weights weights;
  907. weights.count = parser.get_attribute_value("count").to_int();
  908. while (parser.read() == OK) {
  909. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  910. if (parser.get_node_name() == "input") {
  911. String semantic = parser.get_attribute_value("semantic");
  912. String source = _uri_to_id(parser.get_attribute_value("source"));
  913. int offset = parser.get_attribute_value("offset").to_int();
  914. SkinControllerData::Weights::SourceRef sref;
  915. sref.source = source;
  916. sref.offset = offset;
  917. weights.sources[semantic] = sref;
  918. COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset));
  919. } else if (parser.get_node_name() == "v") { //indices
  920. Vector<float> values = _read_float_array(parser);
  921. weights.indices = values;
  922. COLLADA_PRINT("read " + itos(values.size()) + " index values");
  923. } else if (parser.get_node_name() == "vcount") { // weightsitive
  924. Vector<float> values = _read_float_array(parser);
  925. weights.sets = values;
  926. COLLADA_PRINT("read " + itos(values.size()) + " polygon values");
  927. }
  928. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section)
  929. break;
  930. }
  931. skindata.weights = weights;
  932. } // else if (!parser.is_empty())
  933. // parser.skip_section();
  934. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "skin")
  935. break;
  936. }
  937. /* STORE REST MATRICES */
  938. Vector<Transform> rests;
  939. ERR_FAIL_COND(!skindata.joints.sources.has("JOINT"));
  940. ERR_FAIL_COND(!skindata.joints.sources.has("INV_BIND_MATRIX"));
  941. String joint_arr = skindata.joints.sources["JOINT"];
  942. String ibm = skindata.joints.sources["INV_BIND_MATRIX"];
  943. ERR_FAIL_COND(!skindata.sources.has(joint_arr));
  944. ERR_FAIL_COND(!skindata.sources.has(ibm));
  945. SkinControllerData::Source &joint_source = skindata.sources[joint_arr];
  946. SkinControllerData::Source &ibm_source = skindata.sources[ibm];
  947. ERR_FAIL_COND(joint_source.sarray.size() != ibm_source.array.size() / 16);
  948. for (int i = 0; i < joint_source.sarray.size(); i++) {
  949. String name = joint_source.sarray[i];
  950. Transform xform = _read_transform_from_array(ibm_source.array, i * 16); // <- this is a mistake, it must be applied to vertices
  951. xform.affine_invert(); // inverse for rest, because it's an inverse
  952. #ifdef COLLADA_IMPORT_SCALE_SCENE
  953. xform.origin *= state.unit_scale;
  954. #endif
  955. skindata.bone_rest_map[name] = xform;
  956. }
  957. }
  958. void Collada::_parse_morph_controller(XMLParser &parser, String p_id) {
  959. state.morph_controller_data_map[p_id] = MorphControllerData();
  960. MorphControllerData &morphdata = state.morph_controller_data_map[p_id];
  961. print_line("morph source: " + parser.get_attribute_value("source") + " id: " + p_id);
  962. morphdata.mesh = _uri_to_id(parser.get_attribute_value("source"));
  963. print_line("morph source2: " + morphdata.mesh);
  964. morphdata.mode = parser.get_attribute_value("method");
  965. printf("JJmorph: %p\n", &morphdata);
  966. String current_source;
  967. while (parser.read() == OK) {
  968. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  969. String section = parser.get_node_name();
  970. if (section == "source") {
  971. String id = parser.get_attribute_value("id");
  972. morphdata.sources[id] = MorphControllerData::Source();
  973. current_source = id;
  974. COLLADA_PRINT("source data: " + id);
  975. } else if (section == "float_array" || section == "array") {
  976. // create a new array and read it.
  977. if (morphdata.sources.has(current_source)) {
  978. morphdata.sources[current_source].array = _read_float_array(parser);
  979. COLLADA_PRINT("section: " + current_source + " read " + itos(morphdata.sources[current_source].array.size()) + " values.");
  980. }
  981. } else if (section == "Name_array" || section == "IDREF_array") {
  982. // create a new array and read it.
  983. //if (section=="IDREF_array")
  984. // morphdata.use_idrefs=true;
  985. if (morphdata.sources.has(current_source)) {
  986. morphdata.sources[current_source].sarray = _read_string_array(parser);
  987. /*
  988. if (section=="IDREF_array") {
  989. Vector<String> sa = morphdata.sources[current_source].sarray;
  990. for(int i=0;i<sa.size();i++)
  991. state.idref_joints.insert(sa[i]);
  992. }*/
  993. COLLADA_PRINT("section: " + current_source + " read " + itos(morphdata.sources[current_source].array.size()) + " values.");
  994. }
  995. } else if (section == "technique_common") {
  996. //skip it
  997. } else if (section == "accessor") { // child of source (below a technique tag)
  998. if (morphdata.sources.has(current_source)) {
  999. int stride = 1;
  1000. if (parser.has_attribute("stride"))
  1001. stride = parser.get_attribute_value("stride").to_int();
  1002. morphdata.sources[current_source].stride = stride;
  1003. COLLADA_PRINT("section: " + current_source + " stride " + itos(morphdata.sources[current_source].stride));
  1004. }
  1005. } else if (section == "targets") {
  1006. while (parser.read() == OK) {
  1007. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1008. if (parser.get_node_name() == "input") {
  1009. String semantic = parser.get_attribute_value("semantic");
  1010. String source = _uri_to_id(parser.get_attribute_value("source"));
  1011. morphdata.targets[semantic] = source;
  1012. COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source);
  1013. }
  1014. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section)
  1015. break;
  1016. }
  1017. }
  1018. // else if (!parser.is_empty())
  1019. // parser.skip_section();
  1020. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "morph")
  1021. break;
  1022. }
  1023. if (morphdata.targets.has("MORPH_WEIGHT")) {
  1024. state.morph_name_map[morphdata.targets["MORPH_WEIGHT"]] = p_id;
  1025. }
  1026. }
  1027. void Collada::_parse_controller(XMLParser &parser) {
  1028. String id = parser.get_attribute_value("id");
  1029. if (parser.is_empty()) {
  1030. return;
  1031. }
  1032. while (parser.read() == OK) {
  1033. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1034. String section = parser.get_node_name();
  1035. if (section == "skin") {
  1036. _parse_skin_controller(parser, id);
  1037. } else if (section == "morph") {
  1038. _parse_morph_controller(parser, id);
  1039. }
  1040. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "controller")
  1041. break;
  1042. }
  1043. }
  1044. Collada::Node *Collada::_parse_visual_instance_geometry(XMLParser &parser) {
  1045. String type = parser.get_node_name();
  1046. NodeGeometry *geom = memnew(NodeGeometry);
  1047. geom->controller = type == "instance_controller";
  1048. geom->source = _uri_to_id(parser.get_attribute_value_safe("url"));
  1049. if (parser.is_empty()) //nothing else to parse...
  1050. return geom;
  1051. // try to find also many materials and skeletons!
  1052. while (parser.read() == OK) {
  1053. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1054. if (parser.get_node_name() == "instance_material") {
  1055. String symbol = parser.get_attribute_value("symbol");
  1056. String target = _uri_to_id(parser.get_attribute_value("target"));
  1057. NodeGeometry::Material mat;
  1058. mat.target = target;
  1059. geom->material_map[symbol] = mat;
  1060. COLLADA_PRINT("uses material: '" + target + "' on primitive'" + symbol + "'");
  1061. } else if (parser.get_node_name() == "skeleton") {
  1062. parser.read();
  1063. String uri = _uri_to_id(parser.get_node_data());
  1064. if (uri != "") {
  1065. geom->skeletons.push_back(uri);
  1066. }
  1067. }
  1068. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == type)
  1069. break;
  1070. }
  1071. if (geom->controller) {
  1072. if (geom->skeletons.empty()) {
  1073. //XSI style
  1074. if (state.skin_controller_data_map.has(geom->source)) {
  1075. SkinControllerData *skin = &state.skin_controller_data_map[geom->source];
  1076. //case where skeletons reference bones with IDREF (XSI)
  1077. ERR_FAIL_COND_V(!skin->joints.sources.has("JOINT"), geom);
  1078. String joint_arr = skin->joints.sources["JOINT"];
  1079. ERR_FAIL_COND_V(!skin->sources.has(joint_arr), geom);
  1080. Collada::SkinControllerData::Source &joint_source = skin->sources[joint_arr];
  1081. geom->skeletons = joint_source.sarray; //quite crazy, but should work.
  1082. }
  1083. }
  1084. }
  1085. return geom;
  1086. }
  1087. Collada::Node *Collada::_parse_visual_instance_camera(XMLParser &parser) {
  1088. String type = parser.get_node_name();
  1089. NodeCamera *cam = memnew(NodeCamera);
  1090. cam->camera = _uri_to_id(parser.get_attribute_value_safe("url"));
  1091. if (state.up_axis == Vector3::AXIS_Z) //collada weirdness
  1092. cam->post_transform.basis.rotate(Vector3(1, 0, 0), Math_PI * 0.5);
  1093. if (parser.is_empty()) //nothing else to parse...
  1094. return cam;
  1095. while (parser.read() == OK) {
  1096. if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_camera")
  1097. break;
  1098. }
  1099. return cam;
  1100. }
  1101. Collada::Node *Collada::_parse_visual_instance_light(XMLParser &parser) {
  1102. String type = parser.get_node_name();
  1103. NodeLight *cam = memnew(NodeLight);
  1104. cam->light = _uri_to_id(parser.get_attribute_value_safe("url"));
  1105. if (state.up_axis == Vector3::AXIS_Z) //collada weirdness
  1106. cam->post_transform.basis.rotate(Vector3(1, 0, 0), Math_PI * 0.5);
  1107. if (parser.is_empty()) //nothing else to parse...
  1108. return cam;
  1109. while (parser.read() == OK) {
  1110. if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_light")
  1111. break;
  1112. }
  1113. return cam;
  1114. }
  1115. Collada::Node *Collada::_parse_visual_node_instance_data(XMLParser &parser) {
  1116. String instance_type = parser.get_node_name();
  1117. if (instance_type == "instance_geometry" || instance_type == "instance_controller") {
  1118. return _parse_visual_instance_geometry(parser);
  1119. } else if (instance_type == "instance_camera") {
  1120. return _parse_visual_instance_camera(parser);
  1121. } else if (instance_type == "instance_light") {
  1122. return _parse_visual_instance_light(parser);
  1123. }
  1124. if (parser.is_empty()) //nothing else to parse...
  1125. return NULL;
  1126. while (parser.read() == OK) {
  1127. if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == instance_type)
  1128. break;
  1129. }
  1130. return NULL;
  1131. }
  1132. Collada::Node *Collada::_parse_visual_scene_node(XMLParser &parser) {
  1133. String name;
  1134. String id = parser.get_attribute_value_safe("id");
  1135. bool found_name = false;
  1136. if (id == "") {
  1137. id = "%NODEID%" + itos(Math::rand());
  1138. } else {
  1139. found_name = true;
  1140. }
  1141. Vector<Node::XForm> xform_list;
  1142. Vector<Node *> children;
  1143. String empty_draw_type = "";
  1144. Node *node = NULL;
  1145. name = parser.has_attribute("name") ? parser.get_attribute_value_safe("name") : parser.get_attribute_value_safe("id");
  1146. if (name == "") {
  1147. name = id;
  1148. } else {
  1149. found_name = true;
  1150. }
  1151. if ((parser.has_attribute("type") && parser.get_attribute_value("type") == "JOINT") || state.idref_joints.has(name)) {
  1152. // handle a bone
  1153. NodeJoint *joint = memnew(NodeJoint);
  1154. if (parser.has_attribute("sid")) { //bones may not have sid
  1155. joint->sid = parser.get_attribute_value("sid");
  1156. // state.bone_map[joint->sid]=joint;
  1157. } else if (state.idref_joints.has(name)) {
  1158. joint->sid = name; //kind of a cheat but..
  1159. } else if (parser.has_attribute("name")) {
  1160. joint->sid = parser.get_attribute_value_safe("name");
  1161. }
  1162. if (joint->sid != "") {
  1163. state.sid_to_node_map[joint->sid] = id;
  1164. }
  1165. node = joint;
  1166. }
  1167. while (parser.read() == OK) {
  1168. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1169. String section = parser.get_node_name();
  1170. if (section == "translate") {
  1171. Node::XForm xf;
  1172. if (parser.has_attribute("sid")) {
  1173. xf.id = parser.get_attribute_value("sid");
  1174. }
  1175. xf.op = Node::XForm::OP_TRANSLATE;
  1176. Vector<float> xlt = _read_float_array(parser);
  1177. xf.data = xlt;
  1178. xform_list.push_back(xf);
  1179. } else if (section == "rotate") {
  1180. Node::XForm xf;
  1181. if (parser.has_attribute("sid")) {
  1182. xf.id = parser.get_attribute_value("sid");
  1183. }
  1184. xf.op = Node::XForm::OP_ROTATE;
  1185. Vector<float> rot = _read_float_array(parser);
  1186. xf.data = rot;
  1187. xform_list.push_back(xf);
  1188. } else if (section == "scale") {
  1189. Node::XForm xf;
  1190. if (parser.has_attribute("sid")) {
  1191. xf.id = parser.get_attribute_value("sid");
  1192. }
  1193. xf.op = Node::XForm::OP_SCALE;
  1194. Vector<float> scale = _read_float_array(parser);
  1195. xf.data = scale;
  1196. xform_list.push_back(xf);
  1197. } else if (section == "matrix") {
  1198. Node::XForm xf;
  1199. if (parser.has_attribute("sid")) {
  1200. xf.id = parser.get_attribute_value("sid");
  1201. }
  1202. xf.op = Node::XForm::OP_MATRIX;
  1203. Vector<float> matrix = _read_float_array(parser);
  1204. xf.data = matrix;
  1205. String mtx;
  1206. for (int i = 0; i < matrix.size(); i++)
  1207. mtx += " " + rtos(matrix[i]);
  1208. xform_list.push_back(xf);
  1209. } else if (section == "visibility") {
  1210. Node::XForm xf;
  1211. if (parser.has_attribute("sid")) {
  1212. xf.id = parser.get_attribute_value("sid");
  1213. }
  1214. xf.op = Node::XForm::OP_VISIBILITY;
  1215. Vector<float> visible = _read_float_array(parser);
  1216. xf.data = visible;
  1217. xform_list.push_back(xf);
  1218. } else if (section == "empty_draw_type") {
  1219. empty_draw_type = _read_empty_draw_type(parser);
  1220. } else if (section == "technique" || section == "extra") {
  1221. } else if (section != "node") {
  1222. //usually what defines the type of node
  1223. //print_line(" dont know what to do with "+section);
  1224. if (section.begins_with("instance_")) {
  1225. if (!node) {
  1226. node = _parse_visual_node_instance_data(parser);
  1227. } else {
  1228. ERR_PRINT("Multiple instance_* not supported.");
  1229. }
  1230. }
  1231. } else if (section == "node") {
  1232. /* Found a child node!! what to do..*/
  1233. Node *child = _parse_visual_scene_node(parser);
  1234. children.push_back(child);
  1235. }
  1236. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "node")
  1237. break;
  1238. }
  1239. if (!node) {
  1240. node = memnew(Node); //generic node, nothing of relevance found
  1241. }
  1242. node->noname = !found_name;
  1243. node->xform_list = xform_list;
  1244. node->children = children;
  1245. for (int i = 0; i < children.size(); i++) {
  1246. node->children[i]->parent = node;
  1247. }
  1248. node->name = name;
  1249. node->id = id;
  1250. node->empty_draw_type = empty_draw_type;
  1251. if (node->children.size() == 1) {
  1252. if (node->children[0]->noname && !node->noname) {
  1253. node->children[0]->name = node->name;
  1254. node->name = node->name + "-base";
  1255. }
  1256. }
  1257. node->default_transform = node->compute_transform(*this);
  1258. state.scene_map[id] = node;
  1259. return node;
  1260. }
  1261. void Collada::_parse_visual_scene(XMLParser &parser) {
  1262. String id = parser.get_attribute_value("id");
  1263. if (parser.is_empty()) {
  1264. return;
  1265. }
  1266. state.visual_scene_map[id] = VisualScene();
  1267. VisualScene &vscene = state.visual_scene_map[id];
  1268. if (parser.has_attribute("name"))
  1269. vscene.name = parser.get_attribute_value("name");
  1270. while (parser.read() == OK) {
  1271. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1272. String section = parser.get_node_name();
  1273. if (section == "node") {
  1274. vscene.root_nodes.push_back(_parse_visual_scene_node(parser));
  1275. }
  1276. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "visual_scene")
  1277. break;
  1278. }
  1279. COLLADA_PRINT("Scene ID:" + id);
  1280. }
  1281. void Collada::_parse_animation(XMLParser &parser) {
  1282. if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) {
  1283. if (!parser.is_empty())
  1284. parser.skip_section();
  1285. return;
  1286. }
  1287. Map<String, Vector<float> > float_sources;
  1288. Map<String, Vector<String> > string_sources;
  1289. Map<String, int> source_strides;
  1290. Map<String, Map<String, String> > samplers;
  1291. Map<String, Vector<String> > source_param_names;
  1292. Map<String, Vector<String> > source_param_types;
  1293. String id = "";
  1294. if (parser.has_attribute("id"))
  1295. id = parser.get_attribute_value("id");
  1296. String current_source;
  1297. String current_sampler;
  1298. Vector<String> channel_sources;
  1299. Vector<String> channel_targets;
  1300. while (parser.read() == OK) {
  1301. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1302. String name = parser.get_node_name();
  1303. if (name == "source") {
  1304. current_source = parser.get_attribute_value("id");
  1305. source_param_names[current_source] = Vector<String>();
  1306. source_param_types[current_source] = Vector<String>();
  1307. } else if (name == "float_array") {
  1308. if (current_source != "") {
  1309. float_sources[current_source] = _read_float_array(parser);
  1310. }
  1311. } else if (name == "Name_array") {
  1312. if (current_source != "") {
  1313. string_sources[current_source] = _read_string_array(parser);
  1314. }
  1315. } else if (name == "accessor") {
  1316. if (current_source != "" && parser.has_attribute("stride")) {
  1317. source_strides[current_source] = parser.get_attribute_value("stride").to_int();
  1318. }
  1319. } else if (name == "sampler") {
  1320. current_sampler = parser.get_attribute_value("id");
  1321. samplers[current_sampler] = Map<String, String>();
  1322. } else if (name == "param") {
  1323. if (parser.has_attribute("name"))
  1324. source_param_names[current_source].push_back(parser.get_attribute_value("name"));
  1325. else
  1326. source_param_names[current_source].push_back("");
  1327. if (parser.has_attribute("type"))
  1328. source_param_types[current_source].push_back(parser.get_attribute_value("type"));
  1329. else
  1330. source_param_types[current_source].push_back("");
  1331. } else if (name == "input") {
  1332. if (current_sampler != "") {
  1333. samplers[current_sampler][parser.get_attribute_value("semantic")] = parser.get_attribute_value("source");
  1334. }
  1335. } else if (name == "channel") {
  1336. channel_sources.push_back(parser.get_attribute_value("source"));
  1337. channel_targets.push_back(parser.get_attribute_value("target"));
  1338. }
  1339. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation")
  1340. break; //end of <asset>
  1341. }
  1342. for (int i = 0; i < channel_sources.size(); i++) {
  1343. String source = _uri_to_id(channel_sources[i]);
  1344. String target = channel_targets[i];
  1345. if (!samplers.has(source)) {
  1346. print_line("channel lacks source: " + source);
  1347. }
  1348. ERR_CONTINUE(!samplers.has(source));
  1349. Map<String, String> &sampler = samplers[source];
  1350. ERR_CONTINUE(!sampler.has("INPUT")); //no input semantic? wtf?
  1351. String input_id = _uri_to_id(sampler["INPUT"]);
  1352. COLLADA_PRINT("input id is " + input_id);
  1353. ERR_CONTINUE(!float_sources.has(input_id));
  1354. ERR_CONTINUE(!sampler.has("OUTPUT"));
  1355. String output_id = _uri_to_id(sampler["OUTPUT"]);
  1356. ERR_CONTINUE(!float_sources.has(output_id));
  1357. ERR_CONTINUE(!source_param_names.has(output_id));
  1358. Vector<String> &names = source_param_names[output_id];
  1359. for (int l = 0; l < names.size(); l++) {
  1360. String name = names[l];
  1361. Vector<float> &time_keys = float_sources[input_id];
  1362. int key_count = time_keys.size();
  1363. AnimationTrack track; //begin crating track
  1364. track.id = id;
  1365. track.keys.resize(key_count);
  1366. for (int j = 0; j < key_count; j++) {
  1367. track.keys[j].time = time_keys[j];
  1368. state.animation_length = MAX(state.animation_length, time_keys[j]);
  1369. }
  1370. //now read actual values
  1371. int stride = 1;
  1372. if (source_strides.has(output_id))
  1373. stride = source_strides[output_id];
  1374. int output_len = stride / names.size();
  1375. ERR_CONTINUE(output_len == 0);
  1376. ERR_CONTINUE(!float_sources.has(output_id));
  1377. Vector<float> &output = float_sources[output_id];
  1378. ERR_EXPLAIN("Wrong number of keys in output");
  1379. ERR_CONTINUE((output.size() / stride) != key_count);
  1380. for (int j = 0; j < key_count; j++) {
  1381. track.keys[j].data.resize(output_len);
  1382. for (int k = 0; k < output_len; k++)
  1383. track.keys[j].data[k] = output[l + j * stride + k]; //super weird but should work
  1384. }
  1385. if (sampler.has("INTERPOLATION")) {
  1386. String interp_id = _uri_to_id(sampler["INTERPOLATION"]);
  1387. ERR_CONTINUE(!string_sources.has(interp_id));
  1388. Vector<String> &interps = string_sources[interp_id];
  1389. ERR_CONTINUE(interps.size() != key_count);
  1390. for (int j = 0; j < key_count; j++) {
  1391. if (interps[j] == "BEZIER")
  1392. track.keys[j].interp_type = AnimationTrack::INTERP_BEZIER;
  1393. else
  1394. track.keys[j].interp_type = AnimationTrack::INTERP_LINEAR;
  1395. }
  1396. }
  1397. if (sampler.has("IN_TANGENT") && sampler.has("OUT_TANGENT")) {
  1398. //bezier control points..
  1399. String intangent_id = _uri_to_id(sampler["IN_TANGENT"]);
  1400. ERR_CONTINUE(!float_sources.has(intangent_id));
  1401. Vector<float> &intangents = float_sources[intangent_id];
  1402. ERR_CONTINUE(intangents.size() != key_count * 2 * names.size());
  1403. String outangent_id = _uri_to_id(sampler["OUT_TANGENT"]);
  1404. ERR_CONTINUE(!float_sources.has(outangent_id));
  1405. Vector<float> &outangents = float_sources[outangent_id];
  1406. ERR_CONTINUE(outangents.size() != key_count * 2 * names.size());
  1407. for (int j = 0; j < key_count; j++) {
  1408. track.keys[j].in_tangent = Vector2(intangents[j * 2 * names.size() + 0 + l * 2], intangents[j * 2 * names.size() + 1 + l * 2]);
  1409. track.keys[j].out_tangent = Vector2(outangents[j * 2 * names.size() + 0 + l * 2], outangents[j * 2 * names.size() + 1 + l * 2]);
  1410. }
  1411. }
  1412. if (target.find("/") != -1) { //transform component
  1413. track.target = target.get_slicec('/', 0);
  1414. track.param = target.get_slicec('/', 1);
  1415. if (track.param.find(".") != -1)
  1416. track.component = track.param.get_slice(".", 1).to_upper();
  1417. track.param = track.param.get_slice(".", 0);
  1418. if (names.size() > 1 && track.component == "") {
  1419. //this is a guess because the collada spec is ambiguous here...
  1420. //i suppose if you have many names (outputs) you can't use a component and i should abide to that.
  1421. track.component = name;
  1422. }
  1423. } else {
  1424. track.target = target;
  1425. }
  1426. print_line("TARGET: " + track.target);
  1427. state.animation_tracks.push_back(track);
  1428. if (!state.referenced_tracks.has(target))
  1429. state.referenced_tracks[target] = Vector<int>();
  1430. state.referenced_tracks[target].push_back(state.animation_tracks.size() - 1);
  1431. if (id != "") {
  1432. if (!state.by_id_tracks.has(id))
  1433. state.by_id_tracks[id] = Vector<int>();
  1434. state.by_id_tracks[id].push_back(state.animation_tracks.size() - 1);
  1435. }
  1436. COLLADA_PRINT("loaded animation with " + itos(key_count) + " keys");
  1437. }
  1438. }
  1439. }
  1440. void Collada::_parse_animation_clip(XMLParser &parser) {
  1441. if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) {
  1442. if (!parser.is_empty())
  1443. parser.skip_section();
  1444. return;
  1445. }
  1446. AnimationClip clip;
  1447. if (parser.has_attribute("name"))
  1448. clip.name = parser.get_attribute_value("name");
  1449. else if (parser.has_attribute("id"))
  1450. clip.name = parser.get_attribute_value("id");
  1451. if (parser.has_attribute("start"))
  1452. clip.begin = parser.get_attribute_value("start").to_double();
  1453. if (parser.has_attribute("end"))
  1454. clip.end = parser.get_attribute_value("end").to_double();
  1455. while (parser.read() == OK) {
  1456. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1457. String name = parser.get_node_name();
  1458. if (name == "instance_animation") {
  1459. String url = _uri_to_id(parser.get_attribute_value("url"));
  1460. clip.tracks.push_back(url);
  1461. }
  1462. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation_clip")
  1463. break; //end of <asset>
  1464. }
  1465. state.animation_clips.push_back(clip);
  1466. print_line("found anim clip: " + clip.name);
  1467. }
  1468. void Collada::_parse_scene(XMLParser &parser) {
  1469. if (parser.is_empty()) {
  1470. return;
  1471. }
  1472. while (parser.read() == OK) {
  1473. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1474. String name = parser.get_node_name();
  1475. if (name == "instance_visual_scene") {
  1476. state.root_visual_scene = _uri_to_id(parser.get_attribute_value("url"));
  1477. print_line("***ROOT VISUAL SCENE: " + state.root_visual_scene);
  1478. }
  1479. if (name == "instance_physics_scene") {
  1480. state.root_physics_scene = _uri_to_id(parser.get_attribute_value("url"));
  1481. }
  1482. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "scene")
  1483. break; //end of <asset>
  1484. }
  1485. }
  1486. void Collada::_parse_library(XMLParser &parser) {
  1487. if (parser.is_empty()) {
  1488. return;
  1489. }
  1490. while (parser.read() == OK) {
  1491. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1492. String name = parser.get_node_name();
  1493. COLLADA_PRINT("library name is: " + name);
  1494. if (name == "image") {
  1495. _parse_image(parser);
  1496. } else if (name == "material") {
  1497. _parse_material(parser);
  1498. } else if (name == "effect") {
  1499. _parse_effect(parser);
  1500. } else if (name == "camera") {
  1501. _parse_camera(parser);
  1502. } else if (name == "light") {
  1503. _parse_light(parser);
  1504. } else if (name == "geometry") {
  1505. String id = parser.get_attribute_value("id");
  1506. String name = parser.get_attribute_value_safe("name");
  1507. while (parser.read() == OK) {
  1508. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1509. if (parser.get_node_name() == "mesh") {
  1510. state.mesh_name_map[id] = (name != "") ? name : id;
  1511. _parse_mesh_geometry(parser, id, name);
  1512. } else if (parser.get_node_name() == "spline") {
  1513. state.mesh_name_map[id] = (name != "") ? name : id;
  1514. _parse_curve_geometry(parser, id, name);
  1515. } else if (!parser.is_empty())
  1516. parser.skip_section();
  1517. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "geometry")
  1518. break;
  1519. }
  1520. } else if (name == "controller") {
  1521. _parse_controller(parser);
  1522. } else if (name == "animation") {
  1523. _parse_animation(parser);
  1524. } else if (name == "animation_clip") {
  1525. _parse_animation_clip(parser);
  1526. } else if (name == "visual_scene") {
  1527. COLLADA_PRINT("visual scene");
  1528. _parse_visual_scene(parser);
  1529. } else if (!parser.is_empty())
  1530. parser.skip_section();
  1531. } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name().begins_with("library_"))
  1532. break; //end of <asset>
  1533. }
  1534. }
  1535. void Collada::_joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner) {
  1536. if (p_node->type == Node::TYPE_JOINT) {
  1537. NodeJoint *nj = static_cast<NodeJoint *>(p_node);
  1538. nj->owner = p_owner;
  1539. for (int i = 0; i < nj->children.size(); i++) {
  1540. _joint_set_owner(nj->children[i], p_owner);
  1541. }
  1542. }
  1543. }
  1544. void Collada::_create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton) {
  1545. Node *node = *p_node;
  1546. if (node->type == Node::TYPE_JOINT) {
  1547. if (!p_skeleton) {
  1548. // ohohohoohoo it's a joint node, time to work!
  1549. NodeSkeleton *sk = memnew(NodeSkeleton);
  1550. *p_node = sk;
  1551. sk->children.push_back(node);
  1552. sk->parent = node->parent;
  1553. node->parent = sk;
  1554. p_skeleton = sk;
  1555. }
  1556. NodeJoint *nj = static_cast<NodeJoint *>(node);
  1557. nj->owner = p_skeleton;
  1558. } else {
  1559. p_skeleton = NULL;
  1560. }
  1561. for (int i = 0; i < node->children.size(); i++) {
  1562. _create_skeletons(&node->children[i], p_skeleton);
  1563. }
  1564. }
  1565. bool Collada::_remove_node(Node *p_parent, Node *p_node) {
  1566. for (int i = 0; i < p_parent->children.size(); i++) {
  1567. if (p_parent->children[i] == p_node) {
  1568. p_parent->children.remove(i);
  1569. return true;
  1570. }
  1571. if (_remove_node(p_parent->children[i], p_node))
  1572. return true;
  1573. }
  1574. return false;
  1575. }
  1576. void Collada::_remove_node(VisualScene *p_vscene, Node *p_node) {
  1577. for (int i = 0; i < p_vscene->root_nodes.size(); i++) {
  1578. if (p_vscene->root_nodes[i] == p_node) {
  1579. p_vscene->root_nodes.remove(i);
  1580. return;
  1581. }
  1582. if (_remove_node(p_vscene->root_nodes[i], p_node))
  1583. return;
  1584. }
  1585. ERR_PRINT("ERROR: Not found node to remove?");
  1586. }
  1587. void Collada::_merge_skeletons(VisualScene *p_vscene, Node *p_node) {
  1588. if (p_node->type == Node::TYPE_GEOMETRY) {
  1589. NodeGeometry *gnode = static_cast<NodeGeometry *>(p_node);
  1590. if (gnode->controller) {
  1591. // recount skeletons used
  1592. Set<NodeSkeleton *> skeletons;
  1593. for (int i = 0; i < gnode->skeletons.size(); i++) {
  1594. String nodeid = gnode->skeletons[i];
  1595. ERR_CONTINUE(!state.scene_map.has(nodeid)); //weird, it should have it...
  1596. NodeJoint *nj = SAFE_CAST<NodeJoint *>(state.scene_map[nodeid]);
  1597. ERR_CONTINUE(!nj); //broken collada
  1598. if (!nj->owner) {
  1599. print_line("no owner for: " + String(nodeid));
  1600. }
  1601. ERR_CONTINUE(!nj->owner); //weird, node should have a skeleton owner
  1602. skeletons.insert(nj->owner);
  1603. }
  1604. if (skeletons.size() > 1) {
  1605. //do the merger!!
  1606. Set<NodeSkeleton *>::Element *E = skeletons.front();
  1607. NodeSkeleton *base = E->get();
  1608. for (E = E->next(); E; E = E->next()) {
  1609. NodeSkeleton *merged = E->get();
  1610. _remove_node(p_vscene, merged);
  1611. for (int i = 0; i < merged->children.size(); i++) {
  1612. _joint_set_owner(merged->children[i], base);
  1613. base->children.push_back(merged->children[i]);
  1614. merged->children[i]->parent = base;
  1615. }
  1616. merged->children.clear(); //take children from it
  1617. memdelete(merged);
  1618. }
  1619. }
  1620. }
  1621. }
  1622. for (int i = 0; i < p_node->children.size(); i++) {
  1623. _merge_skeletons(p_vscene, p_node->children[i]);
  1624. }
  1625. }
  1626. void Collada::_merge_skeletons2(VisualScene *p_vscene) {
  1627. for (Map<String, SkinControllerData>::Element *E = state.skin_controller_data_map.front(); E; E = E->next()) {
  1628. SkinControllerData &cd = E->get();
  1629. NodeSkeleton *skeleton = NULL;
  1630. for (Map<String, Transform>::Element *F = cd.bone_rest_map.front(); F; F = F->next()) {
  1631. String name;
  1632. if (!state.sid_to_node_map.has(F->key())) {
  1633. continue;
  1634. }
  1635. name = state.sid_to_node_map[F->key()];
  1636. if (!state.scene_map.has(name)) {
  1637. print_line("no foundie node for: " + name);
  1638. }
  1639. ERR_CONTINUE(!state.scene_map.has(name));
  1640. Node *node = state.scene_map[name];
  1641. ERR_CONTINUE(node->type != Node::TYPE_JOINT);
  1642. if (node->type != Node::TYPE_JOINT)
  1643. continue;
  1644. NodeSkeleton *sk = NULL;
  1645. while (node && !sk) {
  1646. if (node->type == Node::TYPE_SKELETON) {
  1647. sk = static_cast<NodeSkeleton *>(node);
  1648. }
  1649. node = node->parent;
  1650. }
  1651. ERR_CONTINUE(!sk);
  1652. if (!sk)
  1653. continue; //bleh
  1654. if (!skeleton) {
  1655. skeleton = sk;
  1656. continue;
  1657. }
  1658. if (skeleton != sk) {
  1659. //whoa.. wtf, merge.
  1660. print_line("MERGED BONES!!");
  1661. //NodeSkeleton *merged = E->get();
  1662. _remove_node(p_vscene, sk);
  1663. for (int i = 0; i < sk->children.size(); i++) {
  1664. _joint_set_owner(sk->children[i], skeleton);
  1665. skeleton->children.push_back(sk->children[i]);
  1666. sk->children[i]->parent = skeleton;
  1667. }
  1668. sk->children.clear(); //take children from it
  1669. memdelete(sk);
  1670. }
  1671. }
  1672. }
  1673. }
  1674. bool Collada::_optimize_skeletons(VisualScene *p_vscene, Node *p_node) {
  1675. Node *node = p_node;
  1676. if (node->type == Node::TYPE_SKELETON && node->parent && node->parent->type == Node::TYPE_NODE && node->parent->children.size() == 1) {
  1677. //replace parent by this...
  1678. Node *parent = node->parent;
  1679. //i wonder if this is allright.. i think it is since created skeleton (first joint) is already animated by bone..
  1680. node->id = parent->id;
  1681. node->name = parent->name;
  1682. node->xform_list = parent->xform_list;
  1683. node->default_transform = parent->default_transform;
  1684. state.scene_map[node->id] = node;
  1685. node->parent = parent->parent;
  1686. if (parent->parent) {
  1687. Node *gp = parent->parent;
  1688. bool found = false;
  1689. for (int i = 0; i < gp->children.size(); i++) {
  1690. if (gp->children[i] == parent) {
  1691. gp->children[i] = node;
  1692. found = true;
  1693. break;
  1694. }
  1695. }
  1696. if (!found) {
  1697. ERR_PRINT("BUG");
  1698. }
  1699. } else {
  1700. bool found = false;
  1701. for (int i = 0; i < p_vscene->root_nodes.size(); i++) {
  1702. if (p_vscene->root_nodes[i] == parent) {
  1703. p_vscene->root_nodes[i] = node;
  1704. found = true;
  1705. break;
  1706. }
  1707. }
  1708. if (!found) {
  1709. ERR_PRINT("BUG");
  1710. }
  1711. }
  1712. parent->children.clear();
  1713. memdelete(parent);
  1714. return true;
  1715. }
  1716. for (int i = 0; i < node->children.size(); i++) {
  1717. if (_optimize_skeletons(p_vscene, node->children[i]))
  1718. return false; //stop processing, go up
  1719. }
  1720. return false;
  1721. }
  1722. bool Collada::_move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List<Node *> *p_mgeom) {
  1723. // bind shape matrix escala los huesos y los hace gigantes, asi la matriz despues achica
  1724. // al modelo?
  1725. // solucion: aplicarle la bind shape matrix a los VERTICES, y si el objeto viene con escala, se la dejo me parece!
  1726. if (p_node->type == Node::TYPE_GEOMETRY) {
  1727. NodeGeometry *ng = static_cast<NodeGeometry *>(p_node);
  1728. if (ng->ignore_anim)
  1729. return false; //already made child of skeleton and processeg
  1730. if (ng->controller && ng->skeletons.size()) {
  1731. String nodeid = ng->skeletons[0];
  1732. ERR_FAIL_COND_V(!state.scene_map.has(nodeid), false); //weird, it should have it...
  1733. NodeJoint *nj = SAFE_CAST<NodeJoint *>(state.scene_map[nodeid]);
  1734. ERR_FAIL_COND_V(!nj, false);
  1735. if (!nj->owner) {
  1736. print_line("Has no owner: " + nj->name);
  1737. }
  1738. ERR_FAIL_COND_V(!nj->owner, false); //weird, node should have a skeleton owner
  1739. NodeSkeleton *sk = nj->owner;
  1740. Node *p = sk->parent;
  1741. bool node_is_parent_of_skeleton = false;
  1742. while (p) {
  1743. if (p == p_node) {
  1744. node_is_parent_of_skeleton = true;
  1745. break;
  1746. }
  1747. p = p->parent; // try again
  1748. }
  1749. ERR_FAIL_COND_V(node_is_parent_of_skeleton, false);
  1750. //this should be correct
  1751. ERR_FAIL_COND_V(!state.skin_controller_data_map.has(ng->source), false);
  1752. SkinControllerData &skin = state.skin_controller_data_map[ng->source];
  1753. Transform skel_inv = sk->get_global_transform().affine_inverse();
  1754. p_node->default_transform = skel_inv * (skin.bind_shape /* p_node->get_global_transform()*/); // i honestly have no idea what to do with a previous model xform.. most exporters ignore it
  1755. //make rests relative to the skeleton (they seem to be always relative to world)
  1756. for (Map<String, Transform>::Element *E = skin.bone_rest_map.front(); E; E = E->next()) {
  1757. E->get() = skel_inv * E->get(); //make the bone rest local to the skeleton
  1758. state.bone_rest_map[E->key()] = E->get(); // make it remember where the bone is globally, now that it's relative
  1759. }
  1760. //but most exporters seem to work only if i do this..
  1761. //p_node->default_transform = p_node->get_global_transform();
  1762. //p_node->default_transform=Transform(); //this seems to be correct, because bind shape makes the object local to the skeleton
  1763. p_node->ignore_anim = true; // collada may animate this later, if it does, then this is not supported (redo your original asset and don't animate the base mesh)
  1764. p_node->parent = sk;
  1765. //sk->children.push_back(0,p_node); //avoid INFINIT loop
  1766. p_mgeom->push_back(p_node);
  1767. return true;
  1768. }
  1769. }
  1770. for (int i = 0; i < p_node->children.size(); i++) {
  1771. if (_move_geometry_to_skeletons(p_vscene, p_node->children[i], p_mgeom)) {
  1772. p_node->children.remove(i);
  1773. i--;
  1774. }
  1775. }
  1776. return false;
  1777. }
  1778. void Collada::_find_morph_nodes(VisualScene *p_vscene, Node *p_node) {
  1779. if (p_node->type == Node::TYPE_GEOMETRY) {
  1780. NodeGeometry *nj = static_cast<NodeGeometry *>(p_node);
  1781. if (nj->controller) {
  1782. String base = nj->source;
  1783. while (base != "" && !state.mesh_data_map.has(base)) {
  1784. if (state.skin_controller_data_map.has(base)) {
  1785. SkinControllerData &sk = state.skin_controller_data_map[base];
  1786. base = sk.base;
  1787. } else if (state.morph_controller_data_map.has(base)) {
  1788. state.morph_ownership_map[base] = nj->id;
  1789. break;
  1790. } else {
  1791. ERR_EXPLAIN("Invalid scene");
  1792. ERR_FAIL();
  1793. }
  1794. }
  1795. }
  1796. }
  1797. for (int i = 0; i < p_node->children.size(); i++) {
  1798. _find_morph_nodes(p_vscene, p_node->children[i]);
  1799. }
  1800. }
  1801. void Collada::_optimize() {
  1802. for (Map<String, VisualScene>::Element *E = state.visual_scene_map.front(); E; E = E->next()) {
  1803. VisualScene &vs = E->get();
  1804. for (int i = 0; i < vs.root_nodes.size(); i++) {
  1805. _create_skeletons(&vs.root_nodes[i]);
  1806. }
  1807. #if 1
  1808. for (int i = 0; i < vs.root_nodes.size(); i++) {
  1809. _merge_skeletons(&vs, vs.root_nodes[i]);
  1810. }
  1811. _merge_skeletons2(&vs);
  1812. for (int i = 0; i < vs.root_nodes.size(); i++) {
  1813. _optimize_skeletons(&vs, vs.root_nodes[i]);
  1814. }
  1815. for (int i = 0; i < vs.root_nodes.size(); i++) {
  1816. List<Node *> mgeom;
  1817. if (_move_geometry_to_skeletons(&vs, vs.root_nodes[i], &mgeom)) {
  1818. vs.root_nodes.remove(i);
  1819. i--;
  1820. }
  1821. while (!mgeom.empty()) {
  1822. Node *n = mgeom.front()->get();
  1823. n->parent->children.push_back(n);
  1824. mgeom.pop_front();
  1825. }
  1826. }
  1827. #endif
  1828. for (int i = 0; i < vs.root_nodes.size(); i++) {
  1829. _find_morph_nodes(&vs, vs.root_nodes[i]);
  1830. }
  1831. }
  1832. }
  1833. int Collada::get_uv_channel(String p_name) {
  1834. if (!channel_map.has(p_name)) {
  1835. ERR_FAIL_COND_V(channel_map.size() == 2, 0);
  1836. channel_map[p_name] = channel_map.size();
  1837. }
  1838. return channel_map[p_name];
  1839. }
  1840. Error Collada::load(const String &p_path, int p_flags) {
  1841. Ref<XMLParser> parserr = memnew(XMLParser);
  1842. XMLParser &parser = *parserr.ptr();
  1843. Error err = parser.open(p_path);
  1844. ERR_FAIL_COND_V(err, err);
  1845. state.local_path = Globals::get_singleton()->localize_path(p_path);
  1846. state.import_flags = p_flags;
  1847. /* Skip headers */
  1848. err = OK;
  1849. while ((err = parser.read()) == OK) {
  1850. if (parser.get_node_type() == XMLParser::NODE_ELEMENT) {
  1851. if (parser.get_node_name() == "COLLADA") {
  1852. break;
  1853. } else if (!parser.is_empty())
  1854. parser.skip_section(); // unknown section, likely headers
  1855. }
  1856. }
  1857. ERR_FAIL_COND_V(err != OK, ERR_FILE_CORRUPT);
  1858. /* Start loading Collada */
  1859. {
  1860. //version
  1861. String version = parser.get_attribute_value("version");
  1862. state.version.major = version.get_slice(".", 0).to_int();
  1863. state.version.minor = version.get_slice(".", 1).to_int();
  1864. state.version.rev = version.get_slice(".", 2).to_int();
  1865. COLLADA_PRINT("Collada VERSION: " + version);
  1866. }
  1867. while ((err = parser.read()) == OK) {
  1868. /* Read all the main sections.. */
  1869. if (parser.get_node_type() != XMLParser::NODE_ELEMENT)
  1870. continue; //no idea what this may be, but skipping anyway
  1871. String section = parser.get_node_name();
  1872. COLLADA_PRINT("section: " + section);
  1873. if (section == "asset") {
  1874. _parse_asset(parser);
  1875. } else if (section.begins_with("library_")) {
  1876. _parse_library(parser);
  1877. } else if (section == "scene") {
  1878. _parse_scene(parser);
  1879. } else if (!parser.is_empty()) {
  1880. parser.skip_section(); // unknown section, likely headers
  1881. }
  1882. }
  1883. _optimize();
  1884. return OK;
  1885. }
  1886. Collada::Collada() {
  1887. }
  1888. #endif