editor_import_collada.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /*************************************************************************/
  2. /* editor_import_collada.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_import_collada.h"
  31. #include "core/os/os.h"
  32. #include "editor/collada/collada.h"
  33. #include "editor/editor_node.h"
  34. #include "scene/3d/camera.h"
  35. #include "scene/3d/light.h"
  36. #include "scene/3d/mesh_instance.h"
  37. #include "scene/3d/path.h"
  38. #include "scene/3d/skeleton.h"
  39. #include "scene/3d/spatial.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/resources/animation.h"
  42. #include "scene/resources/packed_scene.h"
  43. #include "scene/resources/surface_tool.h"
  44. #include <iostream>
  45. struct ColladaImport {
  46. Collada collada;
  47. Spatial *scene;
  48. Vector<Ref<Animation> > animations;
  49. struct NodeMap {
  50. //String path;
  51. Spatial *node;
  52. int bone;
  53. List<int> anim_tracks;
  54. NodeMap() {
  55. node = NULL;
  56. bone = -1;
  57. }
  58. };
  59. bool found_ambient;
  60. Color ambient;
  61. bool found_directional;
  62. bool force_make_tangents;
  63. bool apply_mesh_xform_to_vertices;
  64. bool use_mesh_builtin_materials;
  65. float bake_fps;
  66. Map<String, NodeMap> node_map; //map from collada node to engine node
  67. Map<String, String> node_name_map; //map from collada node to engine node
  68. Map<String, Ref<ArrayMesh> > mesh_cache;
  69. Map<String, Ref<Curve3D> > curve_cache;
  70. Map<String, Ref<Material> > material_cache;
  71. Map<Collada::Node *, Skeleton *> skeleton_map;
  72. Map<Skeleton *, Map<String, int> > skeleton_bone_map;
  73. Set<String> valid_animated_nodes;
  74. Vector<int> valid_animated_properties;
  75. Map<String, bool> bones_with_animation;
  76. Error _populate_skeleton(Skeleton *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent);
  77. Error _create_scene_skeletons(Collada::Node *p_node);
  78. Error _create_scene(Collada::Node *p_node, Spatial *p_parent);
  79. Error _create_resources(Collada::Node *p_node, bool p_use_compression);
  80. Error _create_material(const String &p_target);
  81. Error _create_mesh_surfaces(bool p_optimize, Ref<ArrayMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ArrayMesh> > p_morph_meshes = Vector<Ref<ArrayMesh> >(), bool p_use_compression = false, bool p_use_mesh_material = false);
  82. Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false, bool p_use_compression = false);
  83. void _fix_param_animation_tracks();
  84. void create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks);
  85. void create_animations(bool p_make_tracks_in_all_bones, bool p_import_value_tracks);
  86. Set<String> tracks_in_clips;
  87. Vector<String> missing_textures;
  88. void _pre_process_lights(Collada::Node *p_node);
  89. ColladaImport() {
  90. found_ambient = false;
  91. found_directional = false;
  92. force_make_tangents = false;
  93. apply_mesh_xform_to_vertices = true;
  94. bake_fps = 15;
  95. }
  96. };
  97. Error ColladaImport::_populate_skeleton(Skeleton *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent) {
  98. if (p_node->type != Collada::Node::TYPE_JOINT)
  99. return OK;
  100. Collada::NodeJoint *joint = static_cast<Collada::NodeJoint *>(p_node);
  101. p_skeleton->add_bone(p_node->name);
  102. if (p_parent >= 0)
  103. p_skeleton->set_bone_parent(r_bone, p_parent);
  104. NodeMap nm;
  105. nm.node = p_skeleton;
  106. nm.bone = r_bone;
  107. node_map[p_node->id] = nm;
  108. node_name_map[p_node->name] = p_node->id;
  109. skeleton_bone_map[p_skeleton][joint->sid] = r_bone;
  110. if (collada.state.bone_rest_map.has(joint->sid)) {
  111. p_skeleton->set_bone_rest(r_bone, collada.fix_transform(collada.state.bone_rest_map[joint->sid]));
  112. //should map this bone to something for animation?
  113. } else {
  114. WARN_PRINT("Collada: Joint has no rest.");
  115. }
  116. int id = r_bone++;
  117. for (int i = 0; i < p_node->children.size(); i++) {
  118. Error err = _populate_skeleton(p_skeleton, p_node->children[i], r_bone, id);
  119. if (err)
  120. return err;
  121. }
  122. return OK;
  123. }
  124. void ColladaImport::_pre_process_lights(Collada::Node *p_node) {
  125. if (p_node->type == Collada::Node::TYPE_LIGHT) {
  126. Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node);
  127. if (collada.state.light_data_map.has(light->light)) {
  128. Collada::LightData &ld = collada.state.light_data_map[light->light];
  129. if (ld.mode == Collada::LightData::MODE_AMBIENT) {
  130. found_ambient = true;
  131. ambient = ld.color;
  132. }
  133. if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) {
  134. found_directional = true;
  135. }
  136. }
  137. }
  138. for (int i = 0; i < p_node->children.size(); i++)
  139. _pre_process_lights(p_node->children[i]);
  140. }
  141. Error ColladaImport::_create_scene_skeletons(Collada::Node *p_node) {
  142. if (p_node->type == Collada::Node::TYPE_SKELETON) {
  143. Skeleton *sk = memnew(Skeleton);
  144. int bone = 0;
  145. for (int i = 0; i < p_node->children.size(); i++) {
  146. _populate_skeleton(sk, p_node->children[i], bone, -1);
  147. }
  148. sk->localize_rests(); //after creating skeleton, rests must be localized...!
  149. skeleton_map[p_node] = sk;
  150. }
  151. for (int i = 0; i < p_node->children.size(); i++) {
  152. Error err = _create_scene_skeletons(p_node->children[i]);
  153. if (err)
  154. return err;
  155. }
  156. return OK;
  157. }
  158. Error ColladaImport::_create_scene(Collada::Node *p_node, Spatial *p_parent) {
  159. Spatial *node = NULL;
  160. switch (p_node->type) {
  161. case Collada::Node::TYPE_NODE: {
  162. node = memnew(Spatial);
  163. } break;
  164. case Collada::Node::TYPE_JOINT: {
  165. return OK; // do nothing
  166. } break;
  167. case Collada::Node::TYPE_LIGHT: {
  168. //node = memnew( Light)
  169. Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node);
  170. if (collada.state.light_data_map.has(light->light)) {
  171. Collada::LightData &ld = collada.state.light_data_map[light->light];
  172. if (ld.mode == Collada::LightData::MODE_AMBIENT) {
  173. if (found_directional)
  174. return OK; //do nothing not needed
  175. if (!bool(GLOBAL_DEF("collada/use_ambient", false)))
  176. return OK;
  177. //well, it's an ambient light..
  178. Light *l = memnew(DirectionalLight);
  179. //l->set_color(Light::COLOR_AMBIENT,ld.color);
  180. //l->set_color(Light::COLOR_DIFFUSE,Color(0,0,0));
  181. //l->set_color(Light::COLOR_SPECULAR,Color(0,0,0));
  182. node = l;
  183. } else if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) {
  184. //well, it's an ambient light..
  185. Light *l = memnew(DirectionalLight);
  186. /*
  187. if (found_ambient) //use it here
  188. l->set_color(Light::COLOR_AMBIENT,ambient);
  189. l->set_color(Light::COLOR_DIFFUSE,ld.color);
  190. l->set_color(Light::COLOR_SPECULAR,Color(1,1,1));
  191. */
  192. node = l;
  193. } else {
  194. Light *l;
  195. if (ld.mode == Collada::LightData::MODE_OMNI)
  196. l = memnew(OmniLight);
  197. else {
  198. l = memnew(SpotLight);
  199. //l->set_parameter(Light::PARAM_SPOT_ANGLE,ld.spot_angle);
  200. //l->set_parameter(Light::PARAM_SPOT_ATTENUATION,ld.spot_exp);
  201. }
  202. //
  203. //l->set_color(Light::COLOR_DIFFUSE,ld.color);
  204. //l->set_color(Light::COLOR_SPECULAR,Color(1,1,1));
  205. //l->approximate_opengl_attenuation(ld.constant_att,ld.linear_att,ld.quad_att);
  206. node = l;
  207. }
  208. } else {
  209. node = memnew(Spatial);
  210. }
  211. } break;
  212. case Collada::Node::TYPE_CAMERA: {
  213. Collada::NodeCamera *cam = static_cast<Collada::NodeCamera *>(p_node);
  214. Camera *camera = memnew(Camera);
  215. if (collada.state.camera_data_map.has(cam->camera)) {
  216. const Collada::CameraData &cd = collada.state.camera_data_map[cam->camera];
  217. switch (cd.mode) {
  218. case Collada::CameraData::MODE_ORTHOGONAL: {
  219. if (cd.orthogonal.y_mag) {
  220. camera->set_keep_aspect_mode(Camera::KEEP_HEIGHT);
  221. camera->set_orthogonal(cd.orthogonal.y_mag * 2.0, cd.z_near, cd.z_far);
  222. } else if (!cd.orthogonal.y_mag && cd.orthogonal.x_mag) {
  223. camera->set_keep_aspect_mode(Camera::KEEP_WIDTH);
  224. camera->set_orthogonal(cd.orthogonal.x_mag * 2.0, cd.z_near, cd.z_far);
  225. }
  226. } break;
  227. case Collada::CameraData::MODE_PERSPECTIVE: {
  228. if (cd.perspective.y_fov) {
  229. camera->set_perspective(cd.perspective.y_fov, cd.z_near, cd.z_far);
  230. } else if (!cd.perspective.y_fov && cd.perspective.x_fov) {
  231. camera->set_perspective(cd.perspective.x_fov / cd.aspect, cd.z_near, cd.z_far);
  232. }
  233. } break;
  234. }
  235. }
  236. node = camera;
  237. } break;
  238. case Collada::Node::TYPE_GEOMETRY: {
  239. Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node);
  240. if (collada.state.curve_data_map.has(ng->source)) {
  241. node = memnew(Path);
  242. } else {
  243. //mesh since nothing else
  244. node = memnew(MeshInstance);
  245. //Object::cast_to<MeshInstance>(node)->set_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT, true);
  246. }
  247. } break;
  248. case Collada::Node::TYPE_SKELETON: {
  249. ERR_FAIL_COND_V(!skeleton_map.has(p_node), ERR_CANT_CREATE);
  250. Skeleton *sk = skeleton_map[p_node];
  251. node = sk;
  252. } break;
  253. }
  254. if (p_node->name != "")
  255. node->set_name(p_node->name);
  256. NodeMap nm;
  257. nm.node = node;
  258. node_map[p_node->id] = nm;
  259. node_name_map[node->get_name()] = p_node->id;
  260. Transform xf = p_node->default_transform;
  261. xf = collada.fix_transform(xf) * p_node->post_transform;
  262. node->set_transform(xf);
  263. p_parent->add_child(node);
  264. node->set_owner(scene);
  265. if (p_node->empty_draw_type != "") {
  266. node->set_meta("empty_draw_type", Variant(p_node->empty_draw_type));
  267. }
  268. for (int i = 0; i < p_node->children.size(); i++) {
  269. Error err = _create_scene(p_node->children[i], node);
  270. if (err)
  271. return err;
  272. }
  273. return OK;
  274. }
  275. Error ColladaImport::_create_material(const String &p_target) {
  276. ERR_FAIL_COND_V(material_cache.has(p_target), ERR_ALREADY_EXISTS);
  277. ERR_FAIL_COND_V(!collada.state.material_map.has(p_target), ERR_INVALID_PARAMETER);
  278. Collada::Material &src_mat = collada.state.material_map[p_target];
  279. ERR_FAIL_COND_V(!collada.state.effect_map.has(src_mat.instance_effect), ERR_INVALID_PARAMETER);
  280. Collada::Effect &effect = collada.state.effect_map[src_mat.instance_effect];
  281. Ref<SpatialMaterial> material = memnew(SpatialMaterial);
  282. if (src_mat.name != "")
  283. material->set_name(src_mat.name);
  284. else if (effect.name != "")
  285. material->set_name(effect.name);
  286. // DIFFUSE
  287. if (effect.diffuse.texture != "") {
  288. String texfile = effect.get_texture_path(effect.diffuse.texture, collada);
  289. if (texfile != "") {
  290. if (texfile.begins_with("/")) {
  291. texfile = texfile.replace_first("/", "res://");
  292. }
  293. Ref<Texture> texture = ResourceLoader::load(texfile, "Texture");
  294. if (texture.is_valid()) {
  295. material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture);
  296. material->set_albedo(Color(1, 1, 1, 1));
  297. //material->set_parameter(SpatialMaterial::PARAM_DIFFUSE,Color(1,1,1,1));
  298. } else {
  299. missing_textures.push_back(texfile.get_file());
  300. }
  301. }
  302. } else {
  303. material->set_albedo(effect.diffuse.color);
  304. }
  305. // SPECULAR
  306. if (effect.specular.texture != "") {
  307. String texfile = effect.get_texture_path(effect.specular.texture, collada);
  308. if (texfile != "") {
  309. if (texfile.begins_with("/")) {
  310. texfile = texfile.replace_first("/", "res://");
  311. }
  312. Ref<Texture> texture = ResourceLoader::load(texfile, "Texture");
  313. if (texture.is_valid()) {
  314. material->set_texture(SpatialMaterial::TEXTURE_METALLIC, texture);
  315. material->set_specular(1.0);
  316. //material->set_texture(SpatialMaterial::PARAM_SPECULAR,texture);
  317. //material->set_parameter(SpatialMaterial::PARAM_SPECULAR,Color(1,1,1,1));
  318. } else {
  319. missing_textures.push_back(texfile.get_file());
  320. }
  321. }
  322. } else {
  323. material->set_metallic(effect.specular.color.get_v());
  324. }
  325. // EMISSION
  326. if (effect.emission.texture != "") {
  327. String texfile = effect.get_texture_path(effect.emission.texture, collada);
  328. if (texfile != "") {
  329. if (texfile.begins_with("/")) {
  330. texfile = texfile.replace_first("/", "res://");
  331. }
  332. Ref<Texture> texture = ResourceLoader::load(texfile, "Texture");
  333. if (texture.is_valid()) {
  334. material->set_feature(SpatialMaterial::FEATURE_EMISSION, true);
  335. material->set_texture(SpatialMaterial::TEXTURE_EMISSION, texture);
  336. material->set_emission(Color(1, 1, 1, 1));
  337. //material->set_parameter(SpatialMaterial::PARAM_EMISSION,Color(1,1,1,1));
  338. } else {
  339. missing_textures.push_back(texfile.get_file());
  340. }
  341. }
  342. } else {
  343. if (effect.emission.color != Color()) {
  344. material->set_feature(SpatialMaterial::FEATURE_EMISSION, true);
  345. material->set_emission(effect.emission.color);
  346. }
  347. }
  348. // NORMAL
  349. if (effect.bump.texture != "") {
  350. String texfile = effect.get_texture_path(effect.bump.texture, collada);
  351. if (texfile != "") {
  352. if (texfile.begins_with("/")) {
  353. texfile = texfile.replace_first("/", "res://");
  354. }
  355. Ref<Texture> texture = ResourceLoader::load(texfile, "Texture");
  356. if (texture.is_valid()) {
  357. material->set_feature(SpatialMaterial::FEATURE_NORMAL_MAPPING, true);
  358. material->set_texture(SpatialMaterial::TEXTURE_NORMAL, texture);
  359. //material->set_emission(Color(1,1,1,1));
  360. //material->set_texture(SpatialMaterial::PARAM_NORMAL,texture);
  361. } else {
  362. //missing_textures.push_back(texfile.get_file());
  363. }
  364. }
  365. }
  366. float roughness = (effect.shininess - 1.0) / 510;
  367. material->set_roughness(roughness);
  368. if (effect.double_sided) {
  369. material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  370. }
  371. material->set_flag(SpatialMaterial::FLAG_UNSHADED, effect.unshaded);
  372. material_cache[p_target] = material;
  373. return OK;
  374. }
  375. Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<ArrayMesh> &p_mesh, const Map<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ArrayMesh> > p_morph_meshes, bool p_use_compression, bool p_use_mesh_material) {
  376. bool local_xform_mirror = p_local_xform.basis.determinant() < 0;
  377. if (p_morph_data) {
  378. //add morphie target
  379. ERR_FAIL_COND_V(!p_morph_data->targets.has("MORPH_TARGET"), ERR_INVALID_DATA);
  380. String mt = p_morph_data->targets["MORPH_TARGET"];
  381. ERR_FAIL_COND_V(!p_morph_data->sources.has(mt), ERR_INVALID_DATA);
  382. int morph_targets = p_morph_data->sources[mt].sarray.size();
  383. for (int i = 0; i < morph_targets; i++) {
  384. String target = p_morph_data->sources[mt].sarray[i];
  385. ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(target), ERR_INVALID_DATA);
  386. String name = collada.state.mesh_data_map[target].name;
  387. p_mesh->add_blend_shape(name);
  388. }
  389. if (p_morph_data->mode == "RELATIVE")
  390. p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_RELATIVE);
  391. else if (p_morph_data->mode == "NORMALIZED")
  392. p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED);
  393. }
  394. int surface = 0;
  395. for (int p_i = 0; p_i < meshdata.primitives.size(); p_i++) {
  396. const Collada::MeshData::Primitives &p = meshdata.primitives[p_i];
  397. /* VERTEX SOURCE */
  398. ERR_FAIL_COND_V(!p.sources.has("VERTEX"), ERR_INVALID_DATA);
  399. String vertex_src_id = p.sources["VERTEX"].source;
  400. int vertex_ofs = p.sources["VERTEX"].offset;
  401. ERR_FAIL_COND_V(!meshdata.vertices.has(vertex_src_id), ERR_INVALID_DATA);
  402. ERR_FAIL_COND_V(!meshdata.vertices[vertex_src_id].sources.has("POSITION"), ERR_INVALID_DATA);
  403. String position_src_id = meshdata.vertices[vertex_src_id].sources["POSITION"];
  404. ERR_FAIL_COND_V(!meshdata.sources.has(position_src_id), ERR_INVALID_DATA);
  405. const Collada::MeshData::Source *vertex_src = &meshdata.sources[position_src_id];
  406. /* NORMAL SOURCE */
  407. const Collada::MeshData::Source *normal_src = NULL;
  408. int normal_ofs = 0;
  409. if (p.sources.has("NORMAL")) {
  410. String normal_source_id = p.sources["NORMAL"].source;
  411. normal_ofs = p.sources["NORMAL"].offset;
  412. ERR_FAIL_COND_V(!meshdata.sources.has(normal_source_id), ERR_INVALID_DATA);
  413. normal_src = &meshdata.sources[normal_source_id];
  414. }
  415. const Collada::MeshData::Source *binormal_src = NULL;
  416. int binormal_ofs = 0;
  417. if (p.sources.has("TEXBINORMAL")) {
  418. String binormal_source_id = p.sources["TEXBINORMAL"].source;
  419. binormal_ofs = p.sources["TEXBINORMAL"].offset;
  420. ERR_FAIL_COND_V(!meshdata.sources.has(binormal_source_id), ERR_INVALID_DATA);
  421. binormal_src = &meshdata.sources[binormal_source_id];
  422. }
  423. const Collada::MeshData::Source *tangent_src = NULL;
  424. int tangent_ofs = 0;
  425. if (p.sources.has("TEXTANGENT")) {
  426. String tangent_source_id = p.sources["TEXTANGENT"].source;
  427. tangent_ofs = p.sources["TEXTANGENT"].offset;
  428. ERR_FAIL_COND_V(!meshdata.sources.has(tangent_source_id), ERR_INVALID_DATA);
  429. tangent_src = &meshdata.sources[tangent_source_id];
  430. }
  431. const Collada::MeshData::Source *uv_src = NULL;
  432. int uv_ofs = 0;
  433. if (p.sources.has("TEXCOORD0")) {
  434. String uv_source_id = p.sources["TEXCOORD0"].source;
  435. uv_ofs = p.sources["TEXCOORD0"].offset;
  436. ERR_FAIL_COND_V(!meshdata.sources.has(uv_source_id), ERR_INVALID_DATA);
  437. uv_src = &meshdata.sources[uv_source_id];
  438. }
  439. const Collada::MeshData::Source *uv2_src = NULL;
  440. int uv2_ofs = 0;
  441. if (p.sources.has("TEXCOORD1")) {
  442. String uv2_source_id = p.sources["TEXCOORD1"].source;
  443. uv2_ofs = p.sources["TEXCOORD1"].offset;
  444. ERR_FAIL_COND_V(!meshdata.sources.has(uv2_source_id), ERR_INVALID_DATA);
  445. uv2_src = &meshdata.sources[uv2_source_id];
  446. }
  447. const Collada::MeshData::Source *color_src = NULL;
  448. int color_ofs = 0;
  449. if (p.sources.has("COLOR")) {
  450. String color_source_id = p.sources["COLOR"].source;
  451. color_ofs = p.sources["COLOR"].offset;
  452. ERR_FAIL_COND_V(!meshdata.sources.has(color_source_id), ERR_INVALID_DATA);
  453. color_src = &meshdata.sources[color_source_id];
  454. }
  455. //find largest source..
  456. /************************/
  457. /* ADD WEIGHTS IF EXIST */
  458. /************************/
  459. Map<int, Vector<Collada::Vertex::Weight> > pre_weights;
  460. bool has_weights = false;
  461. if (p_skin_controller) {
  462. const Collada::SkinControllerData::Source *weight_src = NULL;
  463. int weight_ofs = 0;
  464. if (p_skin_controller->weights.sources.has("WEIGHT")) {
  465. String weight_id = p_skin_controller->weights.sources["WEIGHT"].source;
  466. weight_ofs = p_skin_controller->weights.sources["WEIGHT"].offset;
  467. if (p_skin_controller->sources.has(weight_id)) {
  468. weight_src = &p_skin_controller->sources[weight_id];
  469. }
  470. }
  471. int joint_ofs = 0;
  472. if (p_skin_controller->weights.sources.has("JOINT")) {
  473. joint_ofs = p_skin_controller->weights.sources["JOINT"].offset;
  474. }
  475. //should be OK, given this was pre-checked.
  476. int index_ofs = 0;
  477. int wstride = p_skin_controller->weights.sources.size();
  478. for (int w_i = 0; w_i < p_skin_controller->weights.sets.size(); w_i++) {
  479. int amount = p_skin_controller->weights.sets[w_i];
  480. Vector<Collada::Vertex::Weight> weights;
  481. for (int a_i = 0; a_i < amount; a_i++) {
  482. Collada::Vertex::Weight w;
  483. int read_from = index_ofs + a_i * wstride;
  484. ERR_FAIL_INDEX_V(read_from + wstride - 1, p_skin_controller->weights.indices.size(), ERR_INVALID_DATA);
  485. int weight_index = p_skin_controller->weights.indices[read_from + weight_ofs];
  486. ERR_FAIL_INDEX_V(weight_index, weight_src->array.size(), ERR_INVALID_DATA);
  487. w.weight = weight_src->array[weight_index];
  488. int bone_index = p_skin_controller->weights.indices[read_from + joint_ofs];
  489. if (bone_index == -1)
  490. continue; //ignore this weight (refers to bind shape)
  491. ERR_FAIL_INDEX_V(bone_index, bone_remap.size(), ERR_INVALID_DATA);
  492. w.bone_idx = bone_remap[bone_index];
  493. weights.push_back(w);
  494. }
  495. /* FIX WEIGHTS */
  496. weights.sort();
  497. if (weights.size() > 4) {
  498. //cap to 4 and make weights add up 1
  499. weights.resize(4);
  500. }
  501. //make sure weights always add up to 1
  502. float total = 0;
  503. for (int i = 0; i < weights.size(); i++)
  504. total += weights[i].weight;
  505. if (total)
  506. for (int i = 0; i < weights.size(); i++)
  507. weights.write[i].weight /= total;
  508. if (weights.size() == 0 || total == 0) { //if nothing, add a weight to bone 0
  509. //no weights assigned
  510. Collada::Vertex::Weight w;
  511. w.bone_idx = 0;
  512. w.weight = 1.0;
  513. weights.clear();
  514. weights.push_back(w);
  515. }
  516. pre_weights[w_i] = weights;
  517. /*
  518. for(Set<int>::Element *E=vertex_map[w_i].front();E;E=E->next()) {
  519. int dst = E->get();
  520. ERR_EXPLAIN("invalid vertex index in array");
  521. ERR_FAIL_INDEX_V(dst,vertex_array.size(),ERR_INVALID_DATA);
  522. vertex_array[dst].weights=weights;
  523. }*/
  524. index_ofs += wstride * amount;
  525. }
  526. //vertices need to be localized
  527. has_weights = true;
  528. }
  529. Set<Collada::Vertex> vertex_set; //vertex set will be the vertices
  530. List<int> indices_list; //indices will be the indices
  531. //Map<int,Set<int> > vertex_map; //map vertices (for setting skinning/morph)
  532. /**************************/
  533. /* CREATE PRIMITIVE ARRAY */
  534. /**************************/
  535. // The way collada uses indices is more optimal, and friendlier with 3D modelling software,
  536. // because it can index everything, not only vertices (similar to how the WII works).
  537. // This is, however, more incompatible with standard video cards, so arrays must be converted.
  538. // Must convert to GL/DX format.
  539. int _prim_ofs = 0;
  540. int vertidx = 0;
  541. for (int p_i = 0; p_i < p.count; p_i++) {
  542. int amount;
  543. if (p.polygons.size()) {
  544. ERR_FAIL_INDEX_V(p_i, p.polygons.size(), ERR_INVALID_DATA);
  545. amount = p.polygons[p_i];
  546. } else {
  547. amount = 3; //triangles;
  548. }
  549. //COLLADA_PRINT("amount: "+itos(amount));
  550. int prev2[2] = { 0, 0 };
  551. for (int j = 0; j < amount; j++) {
  552. int src = _prim_ofs;
  553. //_prim_ofs+=p.sources.size()
  554. ERR_FAIL_INDEX_V(src, p.indices.size(), ERR_INVALID_DATA);
  555. Collada::Vertex vertex;
  556. if (!p_optimize)
  557. vertex.uid = vertidx++;
  558. int vertex_index = p.indices[src + vertex_ofs]; //used for index field (later used by controllers)
  559. int vertex_pos = (vertex_src->stride ? vertex_src->stride : 3) * vertex_index;
  560. ERR_FAIL_INDEX_V(vertex_pos, vertex_src->array.size(), ERR_INVALID_DATA);
  561. vertex.vertex = Vector3(vertex_src->array[vertex_pos + 0], vertex_src->array[vertex_pos + 1], vertex_src->array[vertex_pos + 2]);
  562. if (pre_weights.has(vertex_index)) {
  563. vertex.weights = pre_weights[vertex_index];
  564. }
  565. if (normal_src) {
  566. int normal_pos = (normal_src->stride ? normal_src->stride : 3) * p.indices[src + normal_ofs];
  567. ERR_FAIL_INDEX_V(normal_pos, normal_src->array.size(), ERR_INVALID_DATA);
  568. vertex.normal = Vector3(normal_src->array[normal_pos + 0], normal_src->array[normal_pos + 1], normal_src->array[normal_pos + 2]);
  569. if (tangent_src && binormal_src) {
  570. int binormal_pos = (binormal_src->stride ? binormal_src->stride : 3) * p.indices[src + binormal_ofs];
  571. ERR_FAIL_INDEX_V(binormal_pos, binormal_src->array.size(), ERR_INVALID_DATA);
  572. Vector3 binormal = Vector3(binormal_src->array[binormal_pos + 0], binormal_src->array[binormal_pos + 1], binormal_src->array[binormal_pos + 2]);
  573. int tangent_pos = (tangent_src->stride ? tangent_src->stride : 3) * p.indices[src + tangent_ofs];
  574. ERR_FAIL_INDEX_V(tangent_pos, tangent_src->array.size(), ERR_INVALID_DATA);
  575. Vector3 tangent = Vector3(tangent_src->array[tangent_pos + 0], tangent_src->array[tangent_pos + 1], tangent_src->array[tangent_pos + 2]);
  576. vertex.tangent.normal = tangent;
  577. vertex.tangent.d = vertex.normal.cross(tangent).dot(binormal) > 0 ? 1 : -1;
  578. }
  579. }
  580. if (uv_src) {
  581. int uv_pos = (uv_src->stride ? uv_src->stride : 2) * p.indices[src + uv_ofs];
  582. ERR_FAIL_INDEX_V(uv_pos, uv_src->array.size(), ERR_INVALID_DATA);
  583. vertex.uv = Vector3(uv_src->array[uv_pos + 0], 1.0 - uv_src->array[uv_pos + 1], 0);
  584. }
  585. if (uv2_src) {
  586. int uv2_pos = (uv2_src->stride ? uv2_src->stride : 2) * p.indices[src + uv2_ofs];
  587. ERR_FAIL_INDEX_V(uv2_pos, uv2_src->array.size(), ERR_INVALID_DATA);
  588. vertex.uv2 = Vector3(uv2_src->array[uv2_pos + 0], 1.0 - uv2_src->array[uv2_pos + 1], 0);
  589. }
  590. if (color_src) {
  591. int color_pos = (color_src->stride ? color_src->stride : 3) * p.indices[src + color_ofs]; // colors are RGB in collada..
  592. ERR_FAIL_INDEX_V(color_pos, color_src->array.size(), ERR_INVALID_DATA);
  593. vertex.color = Color(color_src->array[color_pos + 0], color_src->array[color_pos + 1], color_src->array[color_pos + 2], (color_src->stride > 3) ? color_src->array[color_pos + 3] : 1.0);
  594. }
  595. #ifndef NO_UP_AXIS_SWAP
  596. if (collada.state.up_axis == Vector3::AXIS_Z) {
  597. Vector3 bn = vertex.normal.cross(vertex.tangent.normal) * vertex.tangent.d;
  598. SWAP(vertex.vertex.z, vertex.vertex.y);
  599. vertex.vertex.z = -vertex.vertex.z;
  600. SWAP(vertex.normal.z, vertex.normal.y);
  601. vertex.normal.z = -vertex.normal.z;
  602. SWAP(vertex.tangent.normal.z, vertex.tangent.normal.y);
  603. vertex.tangent.normal.z = -vertex.tangent.normal.z;
  604. SWAP(bn.z, bn.y);
  605. bn.z = -bn.z;
  606. vertex.tangent.d = vertex.normal.cross(vertex.tangent.normal).dot(bn) > 0 ? 1 : -1;
  607. }
  608. #endif
  609. vertex.fix_unit_scale(collada);
  610. int index = 0;
  611. //COLLADA_PRINT("vertex: "+vertex.vertex);
  612. if (vertex_set.has(vertex)) {
  613. index = vertex_set.find(vertex)->get().idx;
  614. } else {
  615. index = vertex_set.size();
  616. vertex.idx = index;
  617. vertex_set.insert(vertex);
  618. }
  619. /* if (!vertex_map.has(vertex_index))
  620. vertex_map[vertex_index]=Set<int>();
  621. vertex_map[vertex_index].insert(index); //should be outside..*/
  622. //build triangles if needed
  623. if (j == 0)
  624. prev2[0] = index;
  625. if (j >= 2) {
  626. //insert indices in reverse order (collada uses CCW as frontface)
  627. if (local_xform_mirror) {
  628. indices_list.push_back(prev2[0]);
  629. indices_list.push_back(prev2[1]);
  630. indices_list.push_back(index);
  631. } else {
  632. indices_list.push_back(prev2[0]);
  633. indices_list.push_back(index);
  634. indices_list.push_back(prev2[1]);
  635. }
  636. }
  637. prev2[1] = index;
  638. _prim_ofs += p.vertex_size;
  639. }
  640. }
  641. Vector<Collada::Vertex> vertex_array; //there we go, vertex array
  642. vertex_array.resize(vertex_set.size());
  643. for (Set<Collada::Vertex>::Element *F = vertex_set.front(); F; F = F->next()) {
  644. vertex_array.write[F->get().idx] = F->get();
  645. }
  646. if (has_weights) {
  647. //if skeleton, localize
  648. Transform local_xform = p_local_xform;
  649. for (int i = 0; i < vertex_array.size(); i++) {
  650. vertex_array.write[i].vertex = local_xform.xform(vertex_array[i].vertex);
  651. vertex_array.write[i].normal = local_xform.basis.xform(vertex_array[i].normal).normalized();
  652. vertex_array.write[i].tangent.normal = local_xform.basis.xform(vertex_array[i].tangent.normal).normalized();
  653. if (local_xform_mirror) {
  654. //i shouldn't do this? wtf?
  655. //vertex_array[i].normal*=-1.0;
  656. //vertex_array[i].tangent.normal*=-1.0;
  657. }
  658. }
  659. }
  660. /*****************/
  661. /* MAKE SURFACES */
  662. /*****************/
  663. {
  664. Ref<SpatialMaterial> material;
  665. {
  666. if (p_material_map.has(p.material)) {
  667. String target = p_material_map[p.material].target;
  668. if (!material_cache.has(target)) {
  669. Error err = _create_material(target);
  670. if (!err)
  671. material = material_cache[target];
  672. } else
  673. material = material_cache[target];
  674. } else if (p.material != "") {
  675. WARN_PRINTS("Collada: Unreferenced material in geometry instance: " + p.material);
  676. }
  677. }
  678. Ref<SurfaceTool> surftool;
  679. surftool.instance();
  680. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  681. for (int k = 0; k < vertex_array.size(); k++) {
  682. if (normal_src) {
  683. surftool->add_normal(vertex_array[k].normal);
  684. if (binormal_src && tangent_src) {
  685. surftool->add_tangent(vertex_array[k].tangent);
  686. }
  687. }
  688. if (uv_src) {
  689. surftool->add_uv(Vector2(vertex_array[k].uv.x, vertex_array[k].uv.y));
  690. }
  691. if (uv2_src) {
  692. surftool->add_uv2(Vector2(vertex_array[k].uv2.x, vertex_array[k].uv2.y));
  693. }
  694. if (color_src) {
  695. surftool->add_color(vertex_array[k].color);
  696. }
  697. if (has_weights) {
  698. Vector<float> weights;
  699. Vector<int> bones;
  700. weights.resize(VS::ARRAY_WEIGHTS_SIZE);
  701. bones.resize(VS::ARRAY_WEIGHTS_SIZE);
  702. //float sum=0.0;
  703. for (int l = 0; l < VS::ARRAY_WEIGHTS_SIZE; l++) {
  704. if (l < vertex_array[k].weights.size()) {
  705. weights.write[l] = vertex_array[k].weights[l].weight;
  706. bones.write[l] = vertex_array[k].weights[l].bone_idx;
  707. //sum += vertex_array[k].weights[l].weight;
  708. } else {
  709. weights.write[l] = 0;
  710. bones.write[l] = 0;
  711. }
  712. }
  713. surftool->add_bones(bones);
  714. surftool->add_weights(weights);
  715. }
  716. surftool->add_vertex(vertex_array[k].vertex);
  717. }
  718. for (List<int>::Element *E = indices_list.front(); E; E = E->next()) {
  719. surftool->add_index(E->get());
  720. }
  721. if (!normal_src) {
  722. //should always be normals
  723. surftool->generate_normals();
  724. }
  725. if ((!binormal_src || !tangent_src) && normal_src && uv_src && force_make_tangents) {
  726. surftool->generate_tangents();
  727. }
  728. ////////////////////////////
  729. // FINALLY CREATE SUFRACE //
  730. ////////////////////////////
  731. Array d = surftool->commit_to_arrays();
  732. d.resize(VS::ARRAY_MAX);
  733. Array mr;
  734. ////////////////////////////
  735. // THEN THE MORPH TARGETS //
  736. ////////////////////////////
  737. for (int mi = 0; mi < p_morph_meshes.size(); mi++) {
  738. Array a = p_morph_meshes[mi]->surface_get_arrays(surface);
  739. //add valid weight and bone arrays if they exist, TODO check if they are unique to shape (generally not)
  740. if (has_weights) {
  741. a[Mesh::ARRAY_WEIGHTS] = d[Mesh::ARRAY_WEIGHTS];
  742. a[Mesh::ARRAY_BONES] = d[Mesh::ARRAY_BONES];
  743. }
  744. a[Mesh::ARRAY_INDEX] = Variant();
  745. //a.resize(Mesh::ARRAY_MAX); //no need for index
  746. mr.push_back(a);
  747. }
  748. p_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, d, mr, p_use_compression ? Mesh::ARRAY_COMPRESS_DEFAULT : 0);
  749. if (material.is_valid()) {
  750. if (p_use_mesh_material) {
  751. p_mesh->surface_set_material(surface, material);
  752. }
  753. p_mesh->surface_set_name(surface, material->get_name());
  754. }
  755. }
  756. /*****************/
  757. /* FIND MATERIAL */
  758. /*****************/
  759. surface++;
  760. }
  761. return OK;
  762. }
  763. Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compression) {
  764. if (p_node->type == Collada::Node::TYPE_GEOMETRY && node_map.has(p_node->id)) {
  765. Spatial *node = node_map[p_node->id].node;
  766. Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node);
  767. if (Object::cast_to<Path>(node)) {
  768. Path *path = Object::cast_to<Path>(node);
  769. String curve = ng->source;
  770. if (curve_cache.has(ng->source)) {
  771. path->set_curve(curve_cache[ng->source]);
  772. } else {
  773. Ref<Curve3D> c = memnew(Curve3D);
  774. const Collada::CurveData &cd = collada.state.curve_data_map[ng->source];
  775. ERR_FAIL_COND_V(!cd.control_vertices.has("POSITION"), ERR_INVALID_DATA);
  776. ERR_FAIL_COND_V(!cd.control_vertices.has("IN_TANGENT"), ERR_INVALID_DATA);
  777. ERR_FAIL_COND_V(!cd.control_vertices.has("OUT_TANGENT"), ERR_INVALID_DATA);
  778. ERR_FAIL_COND_V(!cd.control_vertices.has("INTERPOLATION"), ERR_INVALID_DATA);
  779. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["POSITION"]), ERR_INVALID_DATA);
  780. const Collada::CurveData::Source &vertices = cd.sources[cd.control_vertices["POSITION"]];
  781. ERR_FAIL_COND_V(vertices.stride != 3, ERR_INVALID_DATA);
  782. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["IN_TANGENT"]), ERR_INVALID_DATA);
  783. const Collada::CurveData::Source &in_tangents = cd.sources[cd.control_vertices["IN_TANGENT"]];
  784. ERR_FAIL_COND_V(in_tangents.stride != 3, ERR_INVALID_DATA);
  785. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["OUT_TANGENT"]), ERR_INVALID_DATA);
  786. const Collada::CurveData::Source &out_tangents = cd.sources[cd.control_vertices["OUT_TANGENT"]];
  787. ERR_FAIL_COND_V(out_tangents.stride != 3, ERR_INVALID_DATA);
  788. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["INTERPOLATION"]), ERR_INVALID_DATA);
  789. const Collada::CurveData::Source &interps = cd.sources[cd.control_vertices["INTERPOLATION"]];
  790. ERR_FAIL_COND_V(interps.stride != 1, ERR_INVALID_DATA);
  791. const Collada::CurveData::Source *tilts = NULL;
  792. if (cd.control_vertices.has("TILT") && cd.sources.has(cd.control_vertices["TILT"]))
  793. tilts = &cd.sources[cd.control_vertices["TILT"]];
  794. int pc = vertices.array.size() / 3;
  795. for (int i = 0; i < pc; i++) {
  796. Vector3 pos(vertices.array[i * 3 + 0], vertices.array[i * 3 + 1], vertices.array[i * 3 + 2]);
  797. Vector3 in(in_tangents.array[i * 3 + 0], in_tangents.array[i * 3 + 1], in_tangents.array[i * 3 + 2]);
  798. Vector3 out(out_tangents.array[i * 3 + 0], out_tangents.array[i * 3 + 1], out_tangents.array[i * 3 + 2]);
  799. #ifndef NO_UP_AXIS_SWAP
  800. if (collada.state.up_axis == Vector3::AXIS_Z) {
  801. SWAP(pos.y, pos.z);
  802. pos.z = -pos.z;
  803. SWAP(in.y, in.z);
  804. in.z = -in.z;
  805. SWAP(out.y, out.z);
  806. out.z = -out.z;
  807. }
  808. #endif
  809. pos *= collada.state.unit_scale;
  810. in *= collada.state.unit_scale;
  811. out *= collada.state.unit_scale;
  812. c->add_point(pos, in - pos, out - pos);
  813. if (tilts)
  814. c->set_point_tilt(i, tilts->array[i]);
  815. }
  816. curve_cache[ng->source] = c;
  817. path->set_curve(c);
  818. }
  819. }
  820. if (Object::cast_to<MeshInstance>(node)) {
  821. Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node);
  822. MeshInstance *mi = Object::cast_to<MeshInstance>(node);
  823. ERR_FAIL_COND_V(!mi, ERR_BUG);
  824. Collada::SkinControllerData *skin = NULL;
  825. Collada::MorphControllerData *morph = NULL;
  826. String meshid;
  827. Transform apply_xform;
  828. Vector<int> bone_remap;
  829. Vector<Ref<ArrayMesh> > morphs;
  830. if (ng->controller) {
  831. String ngsource = ng->source;
  832. if (collada.state.skin_controller_data_map.has(ngsource)) {
  833. ERR_FAIL_COND_V(!collada.state.skin_controller_data_map.has(ngsource), ERR_INVALID_DATA);
  834. skin = &collada.state.skin_controller_data_map[ngsource];
  835. Vector<String> skeletons = ng->skeletons;
  836. ERR_FAIL_COND_V(skeletons.empty(), ERR_INVALID_DATA);
  837. String skname = skeletons[0];
  838. ERR_FAIL_COND_V(!node_map.has(skname), ERR_INVALID_DATA);
  839. NodeMap nmsk = node_map[skname];
  840. Skeleton *sk = Object::cast_to<Skeleton>(nmsk.node);
  841. ERR_FAIL_COND_V(!sk, ERR_INVALID_DATA);
  842. ERR_FAIL_COND_V(!skeleton_bone_map.has(sk), ERR_INVALID_DATA);
  843. Map<String, int> &bone_remap_map = skeleton_bone_map[sk];
  844. meshid = skin->base;
  845. if (collada.state.morph_controller_data_map.has(meshid)) {
  846. //it's a morph!!
  847. morph = &collada.state.morph_controller_data_map[meshid];
  848. ngsource = meshid;
  849. meshid = morph->mesh;
  850. } else {
  851. ngsource = "";
  852. }
  853. if (apply_mesh_xform_to_vertices) {
  854. apply_xform = collada.fix_transform(p_node->default_transform);
  855. node->set_transform(Transform());
  856. } else {
  857. apply_xform = Transform();
  858. }
  859. ERR_FAIL_COND_V(!skin->weights.sources.has("JOINT"), ERR_INVALID_DATA);
  860. String joint_id = skin->weights.sources["JOINT"].source;
  861. ERR_FAIL_COND_V(!skin->sources.has(joint_id), ERR_INVALID_DATA);
  862. Collada::SkinControllerData::Source *joint_src = &skin->sources[joint_id];
  863. bone_remap.resize(joint_src->sarray.size());
  864. for (int i = 0; i < bone_remap.size(); i++) {
  865. String str = joint_src->sarray[i];
  866. ERR_FAIL_COND_V(!bone_remap_map.has(str), ERR_INVALID_DATA);
  867. bone_remap.write[i] = bone_remap_map[str];
  868. }
  869. }
  870. if (collada.state.morph_controller_data_map.has(ngsource)) {
  871. //it's a morph!!
  872. morph = &collada.state.morph_controller_data_map[ngsource];
  873. meshid = morph->mesh;
  874. Vector<String> targets;
  875. morph->targets.has("MORPH_TARGET");
  876. String target = morph->targets["MORPH_TARGET"];
  877. bool valid = false;
  878. if (morph->sources.has(target)) {
  879. valid = true;
  880. Vector<String> names = morph->sources[target].sarray;
  881. for (int i = 0; i < names.size(); i++) {
  882. String meshid = names[i];
  883. if (collada.state.mesh_data_map.has(meshid)) {
  884. Ref<ArrayMesh> mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  885. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
  886. mesh->set_name(meshdata.name);
  887. Error err = _create_mesh_surfaces(false, mesh, ng->material_map, meshdata, apply_xform, bone_remap, skin, NULL, Vector<Ref<ArrayMesh> >(), false);
  888. ERR_FAIL_COND_V(err, err);
  889. morphs.push_back(mesh);
  890. } else {
  891. valid = false;
  892. }
  893. }
  894. }
  895. if (!valid)
  896. morphs.clear();
  897. ngsource = "";
  898. }
  899. if (ngsource != "") {
  900. ERR_EXPLAIN("Controller Instance Source '" + ngsource + "' is neither skin or morph!");
  901. ERR_FAIL_V(ERR_INVALID_DATA);
  902. }
  903. } else {
  904. meshid = ng->source;
  905. }
  906. Ref<ArrayMesh> mesh;
  907. if (mesh_cache.has(meshid)) {
  908. mesh = mesh_cache[meshid];
  909. } else {
  910. if (collada.state.mesh_data_map.has(meshid)) {
  911. //bleh, must ignore invalid
  912. ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA);
  913. mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  914. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
  915. mesh->set_name(meshdata.name);
  916. Error err = _create_mesh_surfaces(morphs.size() == 0, mesh, ng->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, p_use_compression, use_mesh_builtin_materials);
  917. ERR_FAIL_COND_V(err, err);
  918. mesh_cache[meshid] = mesh;
  919. } else {
  920. WARN_PRINTS("Collada: Will not import geometry: " + meshid);
  921. }
  922. }
  923. if (!mesh.is_null()) {
  924. mi->set_mesh(mesh);
  925. if (!use_mesh_builtin_materials) {
  926. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
  927. for (int i = 0; i < meshdata.primitives.size(); i++) {
  928. String matname = meshdata.primitives[i].material;
  929. if (ng->material_map.has(matname)) {
  930. String target = ng->material_map[matname].target;
  931. Ref<Material> material;
  932. if (!material_cache.has(target)) {
  933. Error err = _create_material(target);
  934. if (!err)
  935. material = material_cache[target];
  936. } else
  937. material = material_cache[target];
  938. mi->set_surface_material(i, material);
  939. } else if (matname != "") {
  940. WARN_PRINTS("Collada: Unreferenced material in geometry instance: " + matname);
  941. }
  942. }
  943. }
  944. }
  945. }
  946. }
  947. for (int i = 0; i < p_node->children.size(); i++) {
  948. Error err = _create_resources(p_node->children[i], p_use_compression);
  949. if (err)
  950. return err;
  951. }
  952. return OK;
  953. }
  954. Error ColladaImport::load(const String &p_path, int p_flags, bool p_force_make_tangents, bool p_use_compression) {
  955. Error err = collada.load(p_path, p_flags);
  956. ERR_FAIL_COND_V(err, err);
  957. force_make_tangents = p_force_make_tangents;
  958. ERR_FAIL_COND_V(!collada.state.visual_scene_map.has(collada.state.root_visual_scene), ERR_INVALID_DATA);
  959. Collada::VisualScene &vs = collada.state.visual_scene_map[collada.state.root_visual_scene];
  960. scene = memnew(Spatial); // root
  961. //determine what's going on with the lights
  962. for (int i = 0; i < vs.root_nodes.size(); i++) {
  963. _pre_process_lights(vs.root_nodes[i]);
  964. }
  965. //import scene
  966. for (int i = 0; i < vs.root_nodes.size(); i++) {
  967. Error err = _create_scene_skeletons(vs.root_nodes[i]);
  968. if (err != OK) {
  969. memdelete(scene);
  970. ERR_FAIL_COND_V(err, err);
  971. }
  972. }
  973. for (int i = 0; i < vs.root_nodes.size(); i++) {
  974. Error err = _create_scene(vs.root_nodes[i], scene);
  975. if (err != OK) {
  976. memdelete(scene);
  977. ERR_FAIL_COND_V(err, err);
  978. }
  979. Error err2 = _create_resources(vs.root_nodes[i], p_use_compression);
  980. if (err2 != OK) {
  981. memdelete(scene);
  982. ERR_FAIL_COND_V(err2, err2);
  983. }
  984. }
  985. //optatively, set unit scale in the root
  986. scene->set_transform(collada.get_root_transform());
  987. return OK;
  988. }
  989. void ColladaImport::_fix_param_animation_tracks() {
  990. for (Map<String, Collada::Node *>::Element *E = collada.state.scene_map.front(); E; E = E->next()) {
  991. Collada::Node *n = E->get();
  992. switch (n->type) {
  993. case Collada::Node::TYPE_NODE: {
  994. // ? do nothing
  995. } break;
  996. case Collada::Node::TYPE_JOINT: {
  997. } break;
  998. case Collada::Node::TYPE_SKELETON: {
  999. } break;
  1000. case Collada::Node::TYPE_LIGHT: {
  1001. } break;
  1002. case Collada::Node::TYPE_CAMERA: {
  1003. } break;
  1004. case Collada::Node::TYPE_GEOMETRY: {
  1005. Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(n);
  1006. // test source(s)
  1007. String source = ng->source;
  1008. while (source != "") {
  1009. if (collada.state.skin_controller_data_map.has(source)) {
  1010. const Collada::SkinControllerData &skin = collada.state.skin_controller_data_map[source];
  1011. //nothing to animate here i think
  1012. source = skin.base;
  1013. } else if (collada.state.morph_controller_data_map.has(source)) {
  1014. const Collada::MorphControllerData &morph = collada.state.morph_controller_data_map[source];
  1015. if (morph.targets.has("MORPH_WEIGHT") && morph.targets.has("MORPH_TARGET")) {
  1016. String weights = morph.targets["MORPH_WEIGHT"];
  1017. String targets = morph.targets["MORPH_TARGET"];
  1018. //fails here
  1019. if (morph.sources.has(targets) && morph.sources.has(weights)) {
  1020. const Collada::MorphControllerData::Source &weight_src = morph.sources[weights];
  1021. const Collada::MorphControllerData::Source &target_src = morph.sources[targets];
  1022. ERR_FAIL_COND(weight_src.array.size() != target_src.sarray.size());
  1023. for (int i = 0; i < weight_src.array.size(); i++) {
  1024. String track_name = weights + "(" + itos(i) + ")";
  1025. String mesh_name = target_src.sarray[i];
  1026. if (collada.state.mesh_name_map.has(mesh_name) && collada.state.referenced_tracks.has(track_name)) {
  1027. const Vector<int> &rt = collada.state.referenced_tracks[track_name];
  1028. for (int rti = 0; rti < rt.size(); rti++) {
  1029. Collada::AnimationTrack *at = &collada.state.animation_tracks.write[rt[rti]];
  1030. at->target = E->key();
  1031. at->param = "morph/" + collada.state.mesh_name_map[mesh_name];
  1032. at->property = true;
  1033. //at->param
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. source = morph.mesh;
  1040. } else {
  1041. source = ""; // for now nothing else supported
  1042. }
  1043. }
  1044. } break;
  1045. }
  1046. }
  1047. }
  1048. void ColladaImport::create_animations(bool p_make_tracks_in_all_bones, bool p_import_value_tracks) {
  1049. _fix_param_animation_tracks();
  1050. for (int i = 0; i < collada.state.animation_clips.size(); i++) {
  1051. for (int j = 0; j < collada.state.animation_clips[i].tracks.size(); j++)
  1052. tracks_in_clips.insert(collada.state.animation_clips[i].tracks[j]);
  1053. }
  1054. for (int i = 0; i < collada.state.animation_tracks.size(); i++) {
  1055. const Collada::AnimationTrack &at = collada.state.animation_tracks[i];
  1056. String node;
  1057. if (!node_map.has(at.target)) {
  1058. if (node_name_map.has(at.target)) {
  1059. node = node_name_map[at.target];
  1060. } else {
  1061. WARN_PRINTS("Collada: Couldn't find node: " + at.target);
  1062. continue;
  1063. }
  1064. } else {
  1065. node = at.target;
  1066. }
  1067. if (at.property) {
  1068. valid_animated_properties.push_back(i);
  1069. } else {
  1070. node_map[node].anim_tracks.push_back(i);
  1071. valid_animated_nodes.insert(node);
  1072. }
  1073. }
  1074. create_animation(-1, p_make_tracks_in_all_bones, p_import_value_tracks);
  1075. for (int i = 0; i < collada.state.animation_clips.size(); i++)
  1076. create_animation(i, p_make_tracks_in_all_bones, p_import_value_tracks);
  1077. }
  1078. void ColladaImport::create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks) {
  1079. Ref<Animation> animation = Ref<Animation>(memnew(Animation));
  1080. if (p_clip == -1) {
  1081. animation->set_name("default");
  1082. } else {
  1083. animation->set_name(collada.state.animation_clips[p_clip].name);
  1084. }
  1085. for (Map<String, NodeMap>::Element *E = node_map.front(); E; E = E->next()) {
  1086. if (E->get().bone < 0)
  1087. continue;
  1088. bones_with_animation[E->key()] = false;
  1089. }
  1090. //store and validate tracks
  1091. if (p_clip == -1) {
  1092. //main anim
  1093. }
  1094. Set<int> track_filter;
  1095. if (p_clip == -1) {
  1096. for (int i = 0; i < collada.state.animation_clips.size(); i++) {
  1097. int tc = collada.state.animation_clips[i].tracks.size();
  1098. for (int j = 0; j < tc; j++) {
  1099. String n = collada.state.animation_clips[i].tracks[j];
  1100. if (collada.state.by_id_tracks.has(n)) {
  1101. const Vector<int> &ti = collada.state.by_id_tracks[n];
  1102. for (int k = 0; k < ti.size(); k++) {
  1103. track_filter.insert(ti[k]);
  1104. }
  1105. }
  1106. }
  1107. }
  1108. } else {
  1109. int tc = collada.state.animation_clips[p_clip].tracks.size();
  1110. for (int j = 0; j < tc; j++) {
  1111. String n = collada.state.animation_clips[p_clip].tracks[j];
  1112. if (collada.state.by_id_tracks.has(n)) {
  1113. const Vector<int> &ti = collada.state.by_id_tracks[n];
  1114. for (int k = 0; k < ti.size(); k++) {
  1115. track_filter.insert(ti[k]);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. //animation->set_loop(true);
  1121. //create animation tracks
  1122. Vector<float> base_snapshots;
  1123. float f = 0;
  1124. float snapshot_interval = 1.0 / bake_fps; //should be customizable somewhere...
  1125. float anim_length = collada.state.animation_length;
  1126. if (p_clip >= 0 && collada.state.animation_clips[p_clip].end)
  1127. anim_length = collada.state.animation_clips[p_clip].end;
  1128. while (f < anim_length) {
  1129. base_snapshots.push_back(f);
  1130. f += snapshot_interval;
  1131. if (f >= anim_length) {
  1132. base_snapshots.push_back(anim_length);
  1133. }
  1134. }
  1135. animation->set_length(anim_length);
  1136. bool tracks_found = false;
  1137. for (Set<String>::Element *E = valid_animated_nodes.front(); E; E = E->next()) {
  1138. // take snapshots
  1139. if (!collada.state.scene_map.has(E->get())) {
  1140. continue;
  1141. }
  1142. NodeMap &nm = node_map[E->get()];
  1143. String path = scene->get_path_to(nm.node);
  1144. if (nm.bone >= 0) {
  1145. Skeleton *sk = static_cast<Skeleton *>(nm.node);
  1146. String name = sk->get_bone_name(nm.bone);
  1147. path = path + ":" + name;
  1148. }
  1149. bool found_anim = false;
  1150. Collada::Node *cn = collada.state.scene_map[E->get()];
  1151. if (cn->ignore_anim) {
  1152. continue;
  1153. }
  1154. animation->add_track(Animation::TYPE_TRANSFORM);
  1155. int track = animation->get_track_count() - 1;
  1156. animation->track_set_path(track, path);
  1157. animation->track_set_imported(track, true); //helps merging later
  1158. Vector<float> snapshots = base_snapshots;
  1159. if (nm.anim_tracks.size() == 1) {
  1160. //use snapshot keys from anim track instead, because this was most likely exported baked
  1161. const Collada::AnimationTrack &at = collada.state.animation_tracks[nm.anim_tracks.front()->get()];
  1162. snapshots.clear();
  1163. for (int i = 0; i < at.keys.size(); i++)
  1164. snapshots.push_back(at.keys[i].time);
  1165. }
  1166. for (int i = 0; i < snapshots.size(); i++) {
  1167. for (List<int>::Element *ET = nm.anim_tracks.front(); ET; ET = ET->next()) {
  1168. //apply tracks
  1169. if (p_clip == -1) {
  1170. if (track_filter.has(ET->get())) {
  1171. continue;
  1172. }
  1173. } else {
  1174. if (!track_filter.has(ET->get()))
  1175. continue;
  1176. }
  1177. found_anim = true;
  1178. const Collada::AnimationTrack &at = collada.state.animation_tracks[ET->get()];
  1179. int xform_idx = -1;
  1180. for (int j = 0; j < cn->xform_list.size(); j++) {
  1181. if (cn->xform_list[j].id == at.param) {
  1182. xform_idx = j;
  1183. break;
  1184. }
  1185. }
  1186. if (xform_idx == -1) {
  1187. WARN_PRINTS("Collada: Couldn't find matching node " + at.target + " xform for track " + at.param);
  1188. continue;
  1189. }
  1190. ERR_CONTINUE(xform_idx == -1);
  1191. Vector<float> data = at.get_value_at_time(snapshots[i]);
  1192. ERR_CONTINUE(data.empty());
  1193. Collada::Node::XForm &xf = cn->xform_list.write[xform_idx];
  1194. if (at.component == "ANGLE") {
  1195. ERR_CONTINUE(data.size() != 1);
  1196. ERR_CONTINUE(xf.op != Collada::Node::XForm::OP_ROTATE);
  1197. ERR_CONTINUE(xf.data.size() < 4);
  1198. xf.data.write[3] = data[0];
  1199. } else if (at.component == "X" || at.component == "Y" || at.component == "Z") {
  1200. int cn = at.component[0] - 'X';
  1201. ERR_CONTINUE(cn >= xf.data.size());
  1202. ERR_CONTINUE(data.size() > 1);
  1203. xf.data.write[cn] = data[0];
  1204. } else if (data.size() == xf.data.size()) {
  1205. xf.data = data;
  1206. } else {
  1207. ERR_EXPLAIN("Component " + at.component + " has datasize " + itos(data.size()) + ", xfdatasize " + itos(xf.data.size()));
  1208. ERR_CONTINUE(data.size() != xf.data.size());
  1209. }
  1210. }
  1211. Transform xform = cn->compute_transform(collada);
  1212. xform = collada.fix_transform(xform) * cn->post_transform;
  1213. if (nm.bone >= 0) {
  1214. //make bone transform relative to rest (in case of skeleton)
  1215. Skeleton *sk = Object::cast_to<Skeleton>(nm.node);
  1216. if (sk) {
  1217. xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform;
  1218. } else {
  1219. ERR_PRINT("Collada: Invalid skeleton");
  1220. }
  1221. }
  1222. Vector3 s = xform.basis.get_scale();
  1223. bool singular_matrix = Math::is_equal_approx(s.x, 0.0f) || Math::is_equal_approx(s.y, 0.0f) || Math::is_equal_approx(s.z, 0.0f);
  1224. Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat();
  1225. Vector3 l = xform.origin;
  1226. animation->transform_track_insert_key(track, snapshots[i], l, q, s);
  1227. }
  1228. if (nm.bone >= 0) {
  1229. if (found_anim)
  1230. bones_with_animation[E->get()] = true;
  1231. }
  1232. if (found_anim)
  1233. tracks_found = true;
  1234. else {
  1235. animation->remove_track(track);
  1236. }
  1237. }
  1238. if (p_make_tracks_in_all_bones) {
  1239. //some bones may lack animation, but since we don't store pose as a property, we must add keyframes!
  1240. for (Map<String, bool>::Element *E = bones_with_animation.front(); E; E = E->next()) {
  1241. if (E->get())
  1242. continue;
  1243. NodeMap &nm = node_map[E->key()];
  1244. String path = scene->get_path_to(nm.node);
  1245. ERR_CONTINUE(nm.bone < 0);
  1246. Skeleton *sk = static_cast<Skeleton *>(nm.node);
  1247. String name = sk->get_bone_name(nm.bone);
  1248. path = path + ":" + name;
  1249. Collada::Node *cn = collada.state.scene_map[E->key()];
  1250. if (cn->ignore_anim) {
  1251. WARN_PRINTS("Collada: Ignoring animation on node: " + path);
  1252. continue;
  1253. }
  1254. animation->add_track(Animation::TYPE_TRANSFORM);
  1255. int track = animation->get_track_count() - 1;
  1256. animation->track_set_path(track, path);
  1257. animation->track_set_imported(track, true); //helps merging later
  1258. Transform xform = cn->compute_transform(collada);
  1259. xform = collada.fix_transform(xform) * cn->post_transform;
  1260. xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform;
  1261. Vector3 s = xform.basis.get_scale();
  1262. bool singular_matrix = Math::is_equal_approx(s.x, 0.0f) || Math::is_equal_approx(s.y, 0.0f) || Math::is_equal_approx(s.z, 0.0f);
  1263. Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat();
  1264. Vector3 l = xform.origin;
  1265. animation->transform_track_insert_key(track, 0, l, q, s);
  1266. tracks_found = true;
  1267. }
  1268. }
  1269. if (p_import_value_tracks) {
  1270. for (int i = 0; i < valid_animated_properties.size(); i++) {
  1271. int ti = valid_animated_properties[i];
  1272. if (p_clip == -1) {
  1273. if (track_filter.has(ti))
  1274. continue;
  1275. } else {
  1276. if (!track_filter.has(ti))
  1277. continue;
  1278. }
  1279. const Collada::AnimationTrack &at = collada.state.animation_tracks[ti];
  1280. // take snapshots
  1281. if (!collada.state.scene_map.has(at.target))
  1282. continue;
  1283. NodeMap &nm = node_map[at.target];
  1284. String path = scene->get_path_to(nm.node);
  1285. animation->add_track(Animation::TYPE_VALUE);
  1286. int track = animation->get_track_count() - 1;
  1287. path = path + ":" + at.param;
  1288. animation->track_set_path(track, path);
  1289. animation->track_set_imported(track, true); //helps merging later
  1290. for (int i = 0; i < at.keys.size(); i++) {
  1291. float time = at.keys[i].time;
  1292. Variant value;
  1293. Vector<float> data = at.keys[i].data;
  1294. if (data.size() == 1) {
  1295. //push a float
  1296. value = data[0];
  1297. } else if (data.size() == 16) {
  1298. //matrix
  1299. WARN_PRINT("Collada: Value keys for matrices not supported.");
  1300. } else {
  1301. WARN_PRINTS("Collada: Unexpected amount of value keys: " + itos(data.size()));
  1302. }
  1303. animation->track_insert_key(track, time, value);
  1304. }
  1305. tracks_found = true;
  1306. }
  1307. }
  1308. if (tracks_found) {
  1309. animations.push_back(animation);
  1310. }
  1311. }
  1312. /*********************************************************************************/
  1313. /*************************************** SCENE ***********************************/
  1314. /*********************************************************************************/
  1315. uint32_t EditorSceneImporterCollada::get_import_flags() const {
  1316. return IMPORT_SCENE | IMPORT_ANIMATION;
  1317. }
  1318. void EditorSceneImporterCollada::get_extensions(List<String> *r_extensions) const {
  1319. r_extensions->push_back("dae");
  1320. }
  1321. Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
  1322. ColladaImport state;
  1323. uint32_t flags = Collada::IMPORT_FLAG_SCENE;
  1324. if (p_flags & IMPORT_ANIMATION)
  1325. flags |= Collada::IMPORT_FLAG_ANIMATION;
  1326. state.use_mesh_builtin_materials = !(p_flags & IMPORT_MATERIALS_IN_INSTANCES);
  1327. state.bake_fps = p_bake_fps;
  1328. Error err = state.load(p_path, flags, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, p_flags & EditorSceneImporter::IMPORT_USE_COMPRESSION);
  1329. ERR_FAIL_COND_V(err != OK, NULL);
  1330. if (state.missing_textures.size()) {
  1331. /*
  1332. for(int i=0;i<state.missing_textures.size();i++) {
  1333. EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
  1334. }
  1335. */
  1336. if (r_missing_deps) {
  1337. for (int i = 0; i < state.missing_textures.size(); i++) {
  1338. //EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
  1339. r_missing_deps->push_back(state.missing_textures[i]);
  1340. }
  1341. }
  1342. }
  1343. if (p_flags & IMPORT_ANIMATION) {
  1344. state.create_animations(p_flags & IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
  1345. AnimationPlayer *ap = memnew(AnimationPlayer);
  1346. for (int i = 0; i < state.animations.size(); i++) {
  1347. String name;
  1348. if (state.animations[i]->get_name() == "")
  1349. name = "default";
  1350. else
  1351. name = state.animations[i]->get_name();
  1352. if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
  1353. if (name.begins_with("loop") || name.ends_with("loop") || name.begins_with("cycle") || name.ends_with("cycle")) {
  1354. state.animations.write[i]->set_loop(true);
  1355. }
  1356. }
  1357. ap->add_animation(name, state.animations[i]);
  1358. }
  1359. state.scene->add_child(ap);
  1360. ap->set_owner(state.scene);
  1361. }
  1362. return state.scene;
  1363. }
  1364. Ref<Animation> EditorSceneImporterCollada::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
  1365. ColladaImport state;
  1366. state.use_mesh_builtin_materials = false;
  1367. Error err = state.load(p_path, Collada::IMPORT_FLAG_ANIMATION, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS);
  1368. ERR_FAIL_COND_V(err != OK, RES());
  1369. state.create_animations(p_flags & EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
  1370. if (state.scene)
  1371. memdelete(state.scene);
  1372. if (state.animations.size() == 0)
  1373. return Ref<Animation>();
  1374. Ref<Animation> anim = state.animations[0];
  1375. String base = p_path.get_basename().to_lower();
  1376. if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
  1377. if (base.begins_with("loop") || base.ends_with("loop") || base.begins_with("cycle") || base.ends_with("cycle")) {
  1378. anim->set_loop(true);
  1379. }
  1380. }
  1381. return anim;
  1382. }
  1383. EditorSceneImporterCollada::EditorSceneImporterCollada() {
  1384. }