collada.cpp 73 KB

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