collada.cpp 72 KB

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