editor_import_collada.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  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. struct ColladaImport {
  45. Collada collada;
  46. Spatial *scene;
  47. Vector<Ref<Animation> > animations;
  48. struct NodeMap {
  49. //String path;
  50. Spatial *node;
  51. int bone;
  52. List<int> anim_tracks;
  53. NodeMap() {
  54. node = NULL;
  55. bone = -1;
  56. }
  57. };
  58. bool found_ambient;
  59. Color ambient;
  60. bool found_directional;
  61. bool force_make_tangents;
  62. bool apply_mesh_xform_to_vertices;
  63. bool use_mesh_builtin_materials;
  64. float bake_fps;
  65. Map<String, NodeMap> node_map; //map from collada node to engine node
  66. Map<String, String> node_name_map; //map from collada node to engine node
  67. Map<String, Ref<ArrayMesh> > mesh_cache;
  68. Map<String, Ref<Curve3D> > curve_cache;
  69. Map<String, Ref<Material> > material_cache;
  70. Map<Collada::Node *, Skeleton *> skeleton_map;
  71. Map<Skeleton *, Map<String, int> > skeleton_bone_map;
  72. Set<String> valid_animated_nodes;
  73. Vector<int> valid_animated_properties;
  74. Map<String, bool> bones_with_animation;
  75. Error _populate_skeleton(Skeleton *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent);
  76. Error _create_scene_skeletons(Collada::Node *p_node);
  77. Error _create_scene(Collada::Node *p_node, Spatial *p_parent);
  78. Error _create_resources(Collada::Node *p_node, bool p_use_compression);
  79. Error _create_material(const String &p_target);
  80. 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);
  81. Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false, bool p_use_compression = false);
  82. void _fix_param_animation_tracks();
  83. void create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks);
  84. void create_animations(bool p_make_tracks_in_all_bones, bool p_import_value_tracks);
  85. Set<String> tracks_in_clips;
  86. Vector<String> missing_textures;
  87. void _pre_process_lights(Collada::Node *p_node);
  88. ColladaImport() {
  89. found_ambient = false;
  90. found_directional = false;
  91. force_make_tangents = false;
  92. apply_mesh_xform_to_vertices = true;
  93. bake_fps = 15;
  94. }
  95. };
  96. Error ColladaImport::_populate_skeleton(Skeleton *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent) {
  97. if (p_node->type != Collada::Node::TYPE_JOINT)
  98. return OK;
  99. Collada::NodeJoint *joint = static_cast<Collada::NodeJoint *>(p_node);
  100. p_skeleton->add_bone(p_node->name);
  101. if (p_parent >= 0)
  102. p_skeleton->set_bone_parent(r_bone, p_parent);
  103. NodeMap nm;
  104. nm.node = p_skeleton;
  105. nm.bone = r_bone;
  106. node_map[p_node->id] = nm;
  107. node_name_map[p_node->name] = p_node->id;
  108. skeleton_bone_map[p_skeleton][joint->sid] = r_bone;
  109. if (collada.state.bone_rest_map.has(joint->sid)) {
  110. p_skeleton->set_bone_rest(r_bone, collada.fix_transform(collada.state.bone_rest_map[joint->sid]));
  111. //should map this bone to something for animation?
  112. } else {
  113. WARN_PRINT("Collada: Joint has no rest.");
  114. }
  115. int id = r_bone++;
  116. for (int i = 0; i < p_node->children.size(); i++) {
  117. Error err = _populate_skeleton(p_skeleton, p_node->children[i], r_bone, id);
  118. if (err)
  119. return err;
  120. }
  121. return OK;
  122. }
  123. void ColladaImport::_pre_process_lights(Collada::Node *p_node) {
  124. if (p_node->type == Collada::Node::TYPE_LIGHT) {
  125. Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node);
  126. if (collada.state.light_data_map.has(light->light)) {
  127. Collada::LightData &ld = collada.state.light_data_map[light->light];
  128. if (ld.mode == Collada::LightData::MODE_AMBIENT) {
  129. found_ambient = true;
  130. ambient = ld.color;
  131. }
  132. if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) {
  133. found_directional = true;
  134. }
  135. }
  136. }
  137. for (int i = 0; i < p_node->children.size(); i++)
  138. _pre_process_lights(p_node->children[i]);
  139. }
  140. Error ColladaImport::_create_scene_skeletons(Collada::Node *p_node) {
  141. if (p_node->type == Collada::Node::TYPE_SKELETON) {
  142. Skeleton *sk = memnew(Skeleton);
  143. int bone = 0;
  144. sk->set_use_bones_in_world_transform(true); // This improves compatibility in Collada
  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. index_ofs += wstride * amount;
  518. }
  519. //vertices need to be localized
  520. has_weights = true;
  521. }
  522. Set<Collada::Vertex> vertex_set; //vertex set will be the vertices
  523. List<int> indices_list; //indices will be the indices
  524. /**************************/
  525. /* CREATE PRIMITIVE ARRAY */
  526. /**************************/
  527. // The way collada uses indices is more optimal, and friendlier with 3D modelling software,
  528. // because it can index everything, not only vertices (similar to how the WII works).
  529. // This is, however, more incompatible with standard video cards, so arrays must be converted.
  530. // Must convert to GL/DX format.
  531. int _prim_ofs = 0;
  532. int vertidx = 0;
  533. for (int p_j = 0; p_j < p.count; p_j++) {
  534. int amount;
  535. if (p.polygons.size()) {
  536. ERR_FAIL_INDEX_V(p_j, p.polygons.size(), ERR_INVALID_DATA);
  537. amount = p.polygons[p_j];
  538. } else {
  539. amount = 3; //triangles;
  540. }
  541. //COLLADA_PRINT("amount: "+itos(amount));
  542. int prev2[2] = { 0, 0 };
  543. for (int j = 0; j < amount; j++) {
  544. int src = _prim_ofs;
  545. //_prim_ofs+=p.sources.size()
  546. ERR_FAIL_INDEX_V(src, p.indices.size(), ERR_INVALID_DATA);
  547. Collada::Vertex vertex;
  548. if (!p_optimize)
  549. vertex.uid = vertidx++;
  550. int vertex_index = p.indices[src + vertex_ofs]; //used for index field (later used by controllers)
  551. int vertex_pos = (vertex_src->stride ? vertex_src->stride : 3) * vertex_index;
  552. ERR_FAIL_INDEX_V(vertex_pos, vertex_src->array.size(), ERR_INVALID_DATA);
  553. vertex.vertex = Vector3(vertex_src->array[vertex_pos + 0], vertex_src->array[vertex_pos + 1], vertex_src->array[vertex_pos + 2]);
  554. if (pre_weights.has(vertex_index)) {
  555. vertex.weights = pre_weights[vertex_index];
  556. }
  557. if (normal_src) {
  558. int normal_pos = (normal_src->stride ? normal_src->stride : 3) * p.indices[src + normal_ofs];
  559. ERR_FAIL_INDEX_V(normal_pos, normal_src->array.size(), ERR_INVALID_DATA);
  560. vertex.normal = Vector3(normal_src->array[normal_pos + 0], normal_src->array[normal_pos + 1], normal_src->array[normal_pos + 2]);
  561. if (tangent_src && binormal_src) {
  562. int binormal_pos = (binormal_src->stride ? binormal_src->stride : 3) * p.indices[src + binormal_ofs];
  563. ERR_FAIL_INDEX_V(binormal_pos, binormal_src->array.size(), ERR_INVALID_DATA);
  564. Vector3 binormal = Vector3(binormal_src->array[binormal_pos + 0], binormal_src->array[binormal_pos + 1], binormal_src->array[binormal_pos + 2]);
  565. int tangent_pos = (tangent_src->stride ? tangent_src->stride : 3) * p.indices[src + tangent_ofs];
  566. ERR_FAIL_INDEX_V(tangent_pos, tangent_src->array.size(), ERR_INVALID_DATA);
  567. Vector3 tangent = Vector3(tangent_src->array[tangent_pos + 0], tangent_src->array[tangent_pos + 1], tangent_src->array[tangent_pos + 2]);
  568. vertex.tangent.normal = tangent;
  569. vertex.tangent.d = vertex.normal.cross(tangent).dot(binormal) > 0 ? 1 : -1;
  570. }
  571. }
  572. if (uv_src) {
  573. int uv_pos = (uv_src->stride ? uv_src->stride : 2) * p.indices[src + uv_ofs];
  574. ERR_FAIL_INDEX_V(uv_pos, uv_src->array.size(), ERR_INVALID_DATA);
  575. vertex.uv = Vector3(uv_src->array[uv_pos + 0], 1.0 - uv_src->array[uv_pos + 1], 0);
  576. }
  577. if (uv2_src) {
  578. int uv2_pos = (uv2_src->stride ? uv2_src->stride : 2) * p.indices[src + uv2_ofs];
  579. ERR_FAIL_INDEX_V(uv2_pos, uv2_src->array.size(), ERR_INVALID_DATA);
  580. vertex.uv2 = Vector3(uv2_src->array[uv2_pos + 0], 1.0 - uv2_src->array[uv2_pos + 1], 0);
  581. }
  582. if (color_src) {
  583. int color_pos = (color_src->stride ? color_src->stride : 3) * p.indices[src + color_ofs]; // colors are RGB in collada..
  584. ERR_FAIL_INDEX_V(color_pos, color_src->array.size(), ERR_INVALID_DATA);
  585. 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);
  586. }
  587. #ifndef NO_UP_AXIS_SWAP
  588. if (collada.state.up_axis == Vector3::AXIS_Z) {
  589. Vector3 bn = vertex.normal.cross(vertex.tangent.normal) * vertex.tangent.d;
  590. SWAP(vertex.vertex.z, vertex.vertex.y);
  591. vertex.vertex.z = -vertex.vertex.z;
  592. SWAP(vertex.normal.z, vertex.normal.y);
  593. vertex.normal.z = -vertex.normal.z;
  594. SWAP(vertex.tangent.normal.z, vertex.tangent.normal.y);
  595. vertex.tangent.normal.z = -vertex.tangent.normal.z;
  596. SWAP(bn.z, bn.y);
  597. bn.z = -bn.z;
  598. vertex.tangent.d = vertex.normal.cross(vertex.tangent.normal).dot(bn) > 0 ? 1 : -1;
  599. }
  600. #endif
  601. vertex.fix_unit_scale(collada);
  602. int index = 0;
  603. //COLLADA_PRINT("vertex: "+vertex.vertex);
  604. if (vertex_set.has(vertex)) {
  605. index = vertex_set.find(vertex)->get().idx;
  606. } else {
  607. index = vertex_set.size();
  608. vertex.idx = index;
  609. vertex_set.insert(vertex);
  610. }
  611. //build triangles if needed
  612. if (j == 0)
  613. prev2[0] = index;
  614. if (j >= 2) {
  615. //insert indices in reverse order (collada uses CCW as frontface)
  616. if (local_xform_mirror) {
  617. indices_list.push_back(prev2[0]);
  618. indices_list.push_back(prev2[1]);
  619. indices_list.push_back(index);
  620. } else {
  621. indices_list.push_back(prev2[0]);
  622. indices_list.push_back(index);
  623. indices_list.push_back(prev2[1]);
  624. }
  625. }
  626. prev2[1] = index;
  627. _prim_ofs += p.vertex_size;
  628. }
  629. }
  630. Vector<Collada::Vertex> vertex_array; //there we go, vertex array
  631. vertex_array.resize(vertex_set.size());
  632. for (Set<Collada::Vertex>::Element *F = vertex_set.front(); F; F = F->next()) {
  633. vertex_array.write[F->get().idx] = F->get();
  634. }
  635. if (has_weights) {
  636. //if skeleton, localize
  637. Transform local_xform = p_local_xform;
  638. for (int i = 0; i < vertex_array.size(); i++) {
  639. vertex_array.write[i].vertex = local_xform.xform(vertex_array[i].vertex);
  640. vertex_array.write[i].normal = local_xform.basis.xform(vertex_array[i].normal).normalized();
  641. vertex_array.write[i].tangent.normal = local_xform.basis.xform(vertex_array[i].tangent.normal).normalized();
  642. if (local_xform_mirror) {
  643. //i shouldn't do this? wtf?
  644. //vertex_array[i].normal*=-1.0;
  645. //vertex_array[i].tangent.normal*=-1.0;
  646. }
  647. }
  648. }
  649. /*****************/
  650. /* MAKE SURFACES */
  651. /*****************/
  652. {
  653. Ref<SpatialMaterial> material;
  654. {
  655. if (p_material_map.has(p.material)) {
  656. String target = p_material_map[p.material].target;
  657. if (!material_cache.has(target)) {
  658. Error err = _create_material(target);
  659. if (!err)
  660. material = material_cache[target];
  661. } else
  662. material = material_cache[target];
  663. } else if (p.material != "") {
  664. WARN_PRINTS("Collada: Unreferenced material in geometry instance: " + p.material);
  665. }
  666. }
  667. Ref<SurfaceTool> surftool;
  668. surftool.instance();
  669. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  670. for (int k = 0; k < vertex_array.size(); k++) {
  671. if (normal_src) {
  672. surftool->add_normal(vertex_array[k].normal);
  673. if (binormal_src && tangent_src) {
  674. surftool->add_tangent(vertex_array[k].tangent);
  675. }
  676. }
  677. if (uv_src) {
  678. surftool->add_uv(Vector2(vertex_array[k].uv.x, vertex_array[k].uv.y));
  679. }
  680. if (uv2_src) {
  681. surftool->add_uv2(Vector2(vertex_array[k].uv2.x, vertex_array[k].uv2.y));
  682. }
  683. if (color_src) {
  684. surftool->add_color(vertex_array[k].color);
  685. }
  686. if (has_weights) {
  687. Vector<float> weights;
  688. Vector<int> bones;
  689. weights.resize(VS::ARRAY_WEIGHTS_SIZE);
  690. bones.resize(VS::ARRAY_WEIGHTS_SIZE);
  691. //float sum=0.0;
  692. for (int l = 0; l < VS::ARRAY_WEIGHTS_SIZE; l++) {
  693. if (l < vertex_array[k].weights.size()) {
  694. weights.write[l] = vertex_array[k].weights[l].weight;
  695. bones.write[l] = vertex_array[k].weights[l].bone_idx;
  696. //sum += vertex_array[k].weights[l].weight;
  697. } else {
  698. weights.write[l] = 0;
  699. bones.write[l] = 0;
  700. }
  701. }
  702. surftool->add_bones(bones);
  703. surftool->add_weights(weights);
  704. }
  705. surftool->add_vertex(vertex_array[k].vertex);
  706. }
  707. for (List<int>::Element *E = indices_list.front(); E; E = E->next()) {
  708. surftool->add_index(E->get());
  709. }
  710. if (!normal_src) {
  711. //should always be normals
  712. surftool->generate_normals();
  713. }
  714. if ((!binormal_src || !tangent_src) && normal_src && uv_src && force_make_tangents) {
  715. surftool->generate_tangents();
  716. }
  717. ////////////////////////////
  718. // FINALLY CREATE SUFRACE //
  719. ////////////////////////////
  720. Array d = surftool->commit_to_arrays();
  721. d.resize(VS::ARRAY_MAX);
  722. Array mr;
  723. ////////////////////////////
  724. // THEN THE MORPH TARGETS //
  725. ////////////////////////////
  726. for (int mi = 0; mi < p_morph_meshes.size(); mi++) {
  727. Array a = p_morph_meshes[mi]->surface_get_arrays(surface);
  728. //add valid weight and bone arrays if they exist, TODO check if they are unique to shape (generally not)
  729. if (has_weights) {
  730. a[Mesh::ARRAY_WEIGHTS] = d[Mesh::ARRAY_WEIGHTS];
  731. a[Mesh::ARRAY_BONES] = d[Mesh::ARRAY_BONES];
  732. }
  733. a[Mesh::ARRAY_INDEX] = Variant();
  734. //a.resize(Mesh::ARRAY_MAX); //no need for index
  735. mr.push_back(a);
  736. }
  737. p_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, d, mr, p_use_compression ? Mesh::ARRAY_COMPRESS_DEFAULT : 0);
  738. if (material.is_valid()) {
  739. if (p_use_mesh_material) {
  740. p_mesh->surface_set_material(surface, material);
  741. }
  742. p_mesh->surface_set_name(surface, material->get_name());
  743. }
  744. }
  745. /*****************/
  746. /* FIND MATERIAL */
  747. /*****************/
  748. surface++;
  749. }
  750. return OK;
  751. }
  752. Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compression) {
  753. if (p_node->type == Collada::Node::TYPE_GEOMETRY && node_map.has(p_node->id)) {
  754. Spatial *node = node_map[p_node->id].node;
  755. Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node);
  756. if (Object::cast_to<Path>(node)) {
  757. Path *path = Object::cast_to<Path>(node);
  758. String curve = ng->source;
  759. if (curve_cache.has(ng->source)) {
  760. path->set_curve(curve_cache[ng->source]);
  761. } else {
  762. Ref<Curve3D> c = memnew(Curve3D);
  763. const Collada::CurveData &cd = collada.state.curve_data_map[ng->source];
  764. ERR_FAIL_COND_V(!cd.control_vertices.has("POSITION"), ERR_INVALID_DATA);
  765. ERR_FAIL_COND_V(!cd.control_vertices.has("IN_TANGENT"), ERR_INVALID_DATA);
  766. ERR_FAIL_COND_V(!cd.control_vertices.has("OUT_TANGENT"), ERR_INVALID_DATA);
  767. ERR_FAIL_COND_V(!cd.control_vertices.has("INTERPOLATION"), ERR_INVALID_DATA);
  768. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["POSITION"]), ERR_INVALID_DATA);
  769. const Collada::CurveData::Source &vertices = cd.sources[cd.control_vertices["POSITION"]];
  770. ERR_FAIL_COND_V(vertices.stride != 3, ERR_INVALID_DATA);
  771. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["IN_TANGENT"]), ERR_INVALID_DATA);
  772. const Collada::CurveData::Source &in_tangents = cd.sources[cd.control_vertices["IN_TANGENT"]];
  773. ERR_FAIL_COND_V(in_tangents.stride != 3, ERR_INVALID_DATA);
  774. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["OUT_TANGENT"]), ERR_INVALID_DATA);
  775. const Collada::CurveData::Source &out_tangents = cd.sources[cd.control_vertices["OUT_TANGENT"]];
  776. ERR_FAIL_COND_V(out_tangents.stride != 3, ERR_INVALID_DATA);
  777. ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["INTERPOLATION"]), ERR_INVALID_DATA);
  778. const Collada::CurveData::Source &interps = cd.sources[cd.control_vertices["INTERPOLATION"]];
  779. ERR_FAIL_COND_V(interps.stride != 1, ERR_INVALID_DATA);
  780. const Collada::CurveData::Source *tilts = NULL;
  781. if (cd.control_vertices.has("TILT") && cd.sources.has(cd.control_vertices["TILT"]))
  782. tilts = &cd.sources[cd.control_vertices["TILT"]];
  783. int pc = vertices.array.size() / 3;
  784. for (int i = 0; i < pc; i++) {
  785. Vector3 pos(vertices.array[i * 3 + 0], vertices.array[i * 3 + 1], vertices.array[i * 3 + 2]);
  786. Vector3 in(in_tangents.array[i * 3 + 0], in_tangents.array[i * 3 + 1], in_tangents.array[i * 3 + 2]);
  787. Vector3 out(out_tangents.array[i * 3 + 0], out_tangents.array[i * 3 + 1], out_tangents.array[i * 3 + 2]);
  788. #ifndef NO_UP_AXIS_SWAP
  789. if (collada.state.up_axis == Vector3::AXIS_Z) {
  790. SWAP(pos.y, pos.z);
  791. pos.z = -pos.z;
  792. SWAP(in.y, in.z);
  793. in.z = -in.z;
  794. SWAP(out.y, out.z);
  795. out.z = -out.z;
  796. }
  797. #endif
  798. pos *= collada.state.unit_scale;
  799. in *= collada.state.unit_scale;
  800. out *= collada.state.unit_scale;
  801. c->add_point(pos, in - pos, out - pos);
  802. if (tilts)
  803. c->set_point_tilt(i, tilts->array[i]);
  804. }
  805. curve_cache[ng->source] = c;
  806. path->set_curve(c);
  807. }
  808. }
  809. if (Object::cast_to<MeshInstance>(node)) {
  810. Collada::NodeGeometry *ng2 = static_cast<Collada::NodeGeometry *>(p_node);
  811. MeshInstance *mi = Object::cast_to<MeshInstance>(node);
  812. ERR_FAIL_COND_V(!mi, ERR_BUG);
  813. Collada::SkinControllerData *skin = NULL;
  814. Collada::MorphControllerData *morph = NULL;
  815. String meshid;
  816. Transform apply_xform;
  817. Vector<int> bone_remap;
  818. Vector<Ref<ArrayMesh> > morphs;
  819. if (ng2->controller) {
  820. String ngsource = ng2->source;
  821. if (collada.state.skin_controller_data_map.has(ngsource)) {
  822. ERR_FAIL_COND_V(!collada.state.skin_controller_data_map.has(ngsource), ERR_INVALID_DATA);
  823. skin = &collada.state.skin_controller_data_map[ngsource];
  824. Vector<String> skeletons = ng2->skeletons;
  825. ERR_FAIL_COND_V(skeletons.empty(), ERR_INVALID_DATA);
  826. String skname = skeletons[0];
  827. ERR_FAIL_COND_V(!node_map.has(skname), ERR_INVALID_DATA);
  828. NodeMap nmsk = node_map[skname];
  829. Skeleton *sk = Object::cast_to<Skeleton>(nmsk.node);
  830. ERR_FAIL_COND_V(!sk, ERR_INVALID_DATA);
  831. ERR_FAIL_COND_V(!skeleton_bone_map.has(sk), ERR_INVALID_DATA);
  832. Map<String, int> &bone_remap_map = skeleton_bone_map[sk];
  833. meshid = skin->base;
  834. if (collada.state.morph_controller_data_map.has(meshid)) {
  835. //it's a morph!!
  836. morph = &collada.state.morph_controller_data_map[meshid];
  837. ngsource = meshid;
  838. meshid = morph->mesh;
  839. } else {
  840. ngsource = "";
  841. }
  842. if (apply_mesh_xform_to_vertices) {
  843. apply_xform = collada.fix_transform(p_node->default_transform);
  844. node->set_transform(Transform());
  845. } else {
  846. apply_xform = Transform();
  847. }
  848. ERR_FAIL_COND_V(!skin->weights.sources.has("JOINT"), ERR_INVALID_DATA);
  849. String joint_id = skin->weights.sources["JOINT"].source;
  850. ERR_FAIL_COND_V(!skin->sources.has(joint_id), ERR_INVALID_DATA);
  851. Collada::SkinControllerData::Source *joint_src = &skin->sources[joint_id];
  852. bone_remap.resize(joint_src->sarray.size());
  853. for (int i = 0; i < bone_remap.size(); i++) {
  854. String str = joint_src->sarray[i];
  855. ERR_FAIL_COND_V(!bone_remap_map.has(str), ERR_INVALID_DATA);
  856. bone_remap.write[i] = bone_remap_map[str];
  857. }
  858. }
  859. if (collada.state.morph_controller_data_map.has(ngsource)) {
  860. //it's a morph!!
  861. morph = &collada.state.morph_controller_data_map[ngsource];
  862. meshid = morph->mesh;
  863. if (morph->targets.has("MORPH_TARGET")) {
  864. String target = morph->targets["MORPH_TARGET"];
  865. bool valid = false;
  866. if (morph->sources.has(target)) {
  867. valid = true;
  868. Vector<String> names = morph->sources[target].sarray;
  869. for (int i = 0; i < names.size(); i++) {
  870. String meshid2 = names[i];
  871. if (collada.state.mesh_data_map.has(meshid2)) {
  872. Ref<ArrayMesh> mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  873. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2];
  874. mesh->set_name(meshdata.name);
  875. Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, NULL, Vector<Ref<ArrayMesh> >(), false);
  876. ERR_FAIL_COND_V(err, err);
  877. morphs.push_back(mesh);
  878. } else {
  879. valid = false;
  880. }
  881. }
  882. }
  883. if (!valid)
  884. morphs.clear();
  885. ngsource = "";
  886. }
  887. }
  888. ERR_FAIL_COND_V_MSG(ngsource != "", ERR_INVALID_DATA, "Controller instance source '" + ngsource + "' is neither skin or morph!");
  889. } else {
  890. meshid = ng2->source;
  891. }
  892. Ref<ArrayMesh> mesh;
  893. if (mesh_cache.has(meshid)) {
  894. mesh = mesh_cache[meshid];
  895. } else {
  896. if (collada.state.mesh_data_map.has(meshid)) {
  897. //bleh, must ignore invalid
  898. ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA);
  899. mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  900. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
  901. mesh->set_name(meshdata.name);
  902. Error err = _create_mesh_surfaces(morphs.size() == 0, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, p_use_compression, use_mesh_builtin_materials);
  903. ERR_FAIL_COND_V_MSG(err, err, "Cannot create mesh surface.");
  904. mesh_cache[meshid] = mesh;
  905. } else {
  906. WARN_PRINTS("Collada: Will not import geometry: " + meshid);
  907. }
  908. }
  909. if (!mesh.is_null()) {
  910. mi->set_mesh(mesh);
  911. if (!use_mesh_builtin_materials) {
  912. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
  913. for (int i = 0; i < meshdata.primitives.size(); i++) {
  914. String matname = meshdata.primitives[i].material;
  915. if (ng2->material_map.has(matname)) {
  916. String target = ng2->material_map[matname].target;
  917. Ref<Material> material;
  918. if (!material_cache.has(target)) {
  919. Error err = _create_material(target);
  920. if (!err)
  921. material = material_cache[target];
  922. } else
  923. material = material_cache[target];
  924. mi->set_surface_material(i, material);
  925. } else if (matname != "") {
  926. WARN_PRINTS("Collada: Unreferenced material in geometry instance: " + matname);
  927. }
  928. }
  929. }
  930. }
  931. }
  932. }
  933. for (int i = 0; i < p_node->children.size(); i++) {
  934. Error err = _create_resources(p_node->children[i], p_use_compression);
  935. if (err)
  936. return err;
  937. }
  938. return OK;
  939. }
  940. Error ColladaImport::load(const String &p_path, int p_flags, bool p_force_make_tangents, bool p_use_compression) {
  941. Error err = collada.load(p_path, p_flags);
  942. ERR_FAIL_COND_V_MSG(err, err, "Cannot load file '" + p_path + "'.");
  943. force_make_tangents = p_force_make_tangents;
  944. ERR_FAIL_COND_V(!collada.state.visual_scene_map.has(collada.state.root_visual_scene), ERR_INVALID_DATA);
  945. Collada::VisualScene &vs = collada.state.visual_scene_map[collada.state.root_visual_scene];
  946. scene = memnew(Spatial); // root
  947. //determine what's going on with the lights
  948. for (int i = 0; i < vs.root_nodes.size(); i++) {
  949. _pre_process_lights(vs.root_nodes[i]);
  950. }
  951. //import scene
  952. for (int i = 0; i < vs.root_nodes.size(); i++) {
  953. Error err2 = _create_scene_skeletons(vs.root_nodes[i]);
  954. if (err2 != OK) {
  955. memdelete(scene);
  956. ERR_FAIL_COND_V(err2, err2);
  957. }
  958. }
  959. for (int i = 0; i < vs.root_nodes.size(); i++) {
  960. Error err2 = _create_scene(vs.root_nodes[i], scene);
  961. if (err2 != OK) {
  962. memdelete(scene);
  963. ERR_FAIL_COND_V(err2, err2);
  964. }
  965. Error err3 = _create_resources(vs.root_nodes[i], p_use_compression);
  966. if (err3 != OK) {
  967. memdelete(scene);
  968. ERR_FAIL_COND_V(err3, err3);
  969. }
  970. }
  971. //optatively, set unit scale in the root
  972. scene->set_transform(collada.get_root_transform());
  973. return OK;
  974. }
  975. void ColladaImport::_fix_param_animation_tracks() {
  976. for (Map<String, Collada::Node *>::Element *E = collada.state.scene_map.front(); E; E = E->next()) {
  977. Collada::Node *n = E->get();
  978. switch (n->type) {
  979. case Collada::Node::TYPE_NODE: {
  980. // ? do nothing
  981. } break;
  982. case Collada::Node::TYPE_JOINT: {
  983. } break;
  984. case Collada::Node::TYPE_SKELETON: {
  985. } break;
  986. case Collada::Node::TYPE_LIGHT: {
  987. } break;
  988. case Collada::Node::TYPE_CAMERA: {
  989. } break;
  990. case Collada::Node::TYPE_GEOMETRY: {
  991. Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(n);
  992. // test source(s)
  993. String source = ng->source;
  994. while (source != "") {
  995. if (collada.state.skin_controller_data_map.has(source)) {
  996. const Collada::SkinControllerData &skin = collada.state.skin_controller_data_map[source];
  997. //nothing to animate here i think
  998. source = skin.base;
  999. } else if (collada.state.morph_controller_data_map.has(source)) {
  1000. const Collada::MorphControllerData &morph = collada.state.morph_controller_data_map[source];
  1001. if (morph.targets.has("MORPH_WEIGHT") && morph.targets.has("MORPH_TARGET")) {
  1002. String weights = morph.targets["MORPH_WEIGHT"];
  1003. String targets = morph.targets["MORPH_TARGET"];
  1004. //fails here
  1005. if (morph.sources.has(targets) && morph.sources.has(weights)) {
  1006. const Collada::MorphControllerData::Source &weight_src = morph.sources[weights];
  1007. const Collada::MorphControllerData::Source &target_src = morph.sources[targets];
  1008. ERR_FAIL_COND(weight_src.array.size() != target_src.sarray.size());
  1009. for (int i = 0; i < weight_src.array.size(); i++) {
  1010. String track_name = weights + "(" + itos(i) + ")";
  1011. String mesh_name = target_src.sarray[i];
  1012. if (collada.state.mesh_name_map.has(mesh_name) && collada.state.referenced_tracks.has(track_name)) {
  1013. const Vector<int> &rt = collada.state.referenced_tracks[track_name];
  1014. for (int rti = 0; rti < rt.size(); rti++) {
  1015. Collada::AnimationTrack *at = &collada.state.animation_tracks.write[rt[rti]];
  1016. at->target = E->key();
  1017. at->param = "morph/" + collada.state.mesh_name_map[mesh_name];
  1018. at->property = true;
  1019. //at->param
  1020. }
  1021. }
  1022. }
  1023. }
  1024. }
  1025. source = morph.mesh;
  1026. } else {
  1027. source = ""; // for now nothing else supported
  1028. }
  1029. }
  1030. } break;
  1031. }
  1032. }
  1033. }
  1034. void ColladaImport::create_animations(bool p_make_tracks_in_all_bones, bool p_import_value_tracks) {
  1035. _fix_param_animation_tracks();
  1036. for (int i = 0; i < collada.state.animation_clips.size(); i++) {
  1037. for (int j = 0; j < collada.state.animation_clips[i].tracks.size(); j++)
  1038. tracks_in_clips.insert(collada.state.animation_clips[i].tracks[j]);
  1039. }
  1040. for (int i = 0; i < collada.state.animation_tracks.size(); i++) {
  1041. const Collada::AnimationTrack &at = collada.state.animation_tracks[i];
  1042. String node;
  1043. if (!node_map.has(at.target)) {
  1044. if (node_name_map.has(at.target)) {
  1045. node = node_name_map[at.target];
  1046. } else {
  1047. WARN_PRINTS("Collada: Couldn't find node: " + at.target);
  1048. continue;
  1049. }
  1050. } else {
  1051. node = at.target;
  1052. }
  1053. if (at.property) {
  1054. valid_animated_properties.push_back(i);
  1055. } else {
  1056. node_map[node].anim_tracks.push_back(i);
  1057. valid_animated_nodes.insert(node);
  1058. }
  1059. }
  1060. create_animation(-1, p_make_tracks_in_all_bones, p_import_value_tracks);
  1061. for (int i = 0; i < collada.state.animation_clips.size(); i++)
  1062. create_animation(i, p_make_tracks_in_all_bones, p_import_value_tracks);
  1063. }
  1064. void ColladaImport::create_animation(int p_clip, bool p_make_tracks_in_all_bones, bool p_import_value_tracks) {
  1065. Ref<Animation> animation = Ref<Animation>(memnew(Animation));
  1066. if (p_clip == -1) {
  1067. animation->set_name("default");
  1068. } else {
  1069. animation->set_name(collada.state.animation_clips[p_clip].name);
  1070. }
  1071. for (Map<String, NodeMap>::Element *E = node_map.front(); E; E = E->next()) {
  1072. if (E->get().bone < 0)
  1073. continue;
  1074. bones_with_animation[E->key()] = false;
  1075. }
  1076. //store and validate tracks
  1077. if (p_clip == -1) {
  1078. //main anim
  1079. }
  1080. Set<int> track_filter;
  1081. if (p_clip == -1) {
  1082. for (int i = 0; i < collada.state.animation_clips.size(); i++) {
  1083. int tc = collada.state.animation_clips[i].tracks.size();
  1084. for (int j = 0; j < tc; j++) {
  1085. String n = collada.state.animation_clips[i].tracks[j];
  1086. if (collada.state.by_id_tracks.has(n)) {
  1087. const Vector<int> &ti = collada.state.by_id_tracks[n];
  1088. for (int k = 0; k < ti.size(); k++) {
  1089. track_filter.insert(ti[k]);
  1090. }
  1091. }
  1092. }
  1093. }
  1094. } else {
  1095. int tc = collada.state.animation_clips[p_clip].tracks.size();
  1096. for (int j = 0; j < tc; j++) {
  1097. String n = collada.state.animation_clips[p_clip].tracks[j];
  1098. if (collada.state.by_id_tracks.has(n)) {
  1099. const Vector<int> &ti = collada.state.by_id_tracks[n];
  1100. for (int k = 0; k < ti.size(); k++) {
  1101. track_filter.insert(ti[k]);
  1102. }
  1103. }
  1104. }
  1105. }
  1106. //animation->set_loop(true);
  1107. //create animation tracks
  1108. Vector<float> base_snapshots;
  1109. float f = 0;
  1110. float snapshot_interval = 1.0 / bake_fps; //should be customizable somewhere...
  1111. float anim_length = collada.state.animation_length;
  1112. if (p_clip >= 0 && collada.state.animation_clips[p_clip].end)
  1113. anim_length = collada.state.animation_clips[p_clip].end;
  1114. while (f < anim_length) {
  1115. base_snapshots.push_back(f);
  1116. f += snapshot_interval;
  1117. if (f >= anim_length) {
  1118. base_snapshots.push_back(anim_length);
  1119. }
  1120. }
  1121. animation->set_length(anim_length);
  1122. bool tracks_found = false;
  1123. for (Set<String>::Element *E = valid_animated_nodes.front(); E; E = E->next()) {
  1124. // take snapshots
  1125. if (!collada.state.scene_map.has(E->get())) {
  1126. continue;
  1127. }
  1128. NodeMap &nm = node_map[E->get()];
  1129. String path = scene->get_path_to(nm.node);
  1130. if (nm.bone >= 0) {
  1131. Skeleton *sk = static_cast<Skeleton *>(nm.node);
  1132. String name = sk->get_bone_name(nm.bone);
  1133. path = path + ":" + name;
  1134. }
  1135. bool found_anim = false;
  1136. Collada::Node *cn = collada.state.scene_map[E->get()];
  1137. if (cn->ignore_anim) {
  1138. continue;
  1139. }
  1140. animation->add_track(Animation::TYPE_TRANSFORM);
  1141. int track = animation->get_track_count() - 1;
  1142. animation->track_set_path(track, path);
  1143. animation->track_set_imported(track, true); //helps merging later
  1144. Vector<float> snapshots = base_snapshots;
  1145. if (nm.anim_tracks.size() == 1) {
  1146. //use snapshot keys from anim track instead, because this was most likely exported baked
  1147. const Collada::AnimationTrack &at = collada.state.animation_tracks[nm.anim_tracks.front()->get()];
  1148. snapshots.clear();
  1149. for (int i = 0; i < at.keys.size(); i++)
  1150. snapshots.push_back(at.keys[i].time);
  1151. }
  1152. for (int i = 0; i < snapshots.size(); i++) {
  1153. for (List<int>::Element *ET = nm.anim_tracks.front(); ET; ET = ET->next()) {
  1154. //apply tracks
  1155. if (p_clip == -1) {
  1156. if (track_filter.has(ET->get())) {
  1157. continue;
  1158. }
  1159. } else {
  1160. if (!track_filter.has(ET->get()))
  1161. continue;
  1162. }
  1163. found_anim = true;
  1164. const Collada::AnimationTrack &at = collada.state.animation_tracks[ET->get()];
  1165. int xform_idx = -1;
  1166. for (int j = 0; j < cn->xform_list.size(); j++) {
  1167. if (cn->xform_list[j].id == at.param) {
  1168. xform_idx = j;
  1169. break;
  1170. }
  1171. }
  1172. if (xform_idx == -1) {
  1173. WARN_PRINTS("Collada: Couldn't find matching node " + at.target + " xform for track " + at.param + ".");
  1174. continue;
  1175. }
  1176. Vector<float> data = at.get_value_at_time(snapshots[i]);
  1177. ERR_CONTINUE(data.empty());
  1178. Collada::Node::XForm &xf = cn->xform_list.write[xform_idx];
  1179. if (at.component == "ANGLE") {
  1180. ERR_CONTINUE(data.size() != 1);
  1181. ERR_CONTINUE(xf.op != Collada::Node::XForm::OP_ROTATE);
  1182. ERR_CONTINUE(xf.data.size() < 4);
  1183. xf.data.write[3] = data[0];
  1184. } else if (at.component == "X" || at.component == "Y" || at.component == "Z") {
  1185. int cn2 = at.component[0] - 'X';
  1186. ERR_CONTINUE(cn2 >= xf.data.size());
  1187. ERR_CONTINUE(data.size() > 1);
  1188. xf.data.write[cn2] = data[0];
  1189. } else if (data.size() == xf.data.size()) {
  1190. xf.data = data;
  1191. } else {
  1192. ERR_CONTINUE_MSG(data.size() != xf.data.size(), "Component " + at.component + " has datasize " + itos(data.size()) + ", xfdatasize " + itos(xf.data.size()) + ".");
  1193. }
  1194. }
  1195. Transform xform = cn->compute_transform(collada);
  1196. xform = collada.fix_transform(xform) * cn->post_transform;
  1197. if (nm.bone >= 0) {
  1198. //make bone transform relative to rest (in case of skeleton)
  1199. Skeleton *sk = Object::cast_to<Skeleton>(nm.node);
  1200. if (sk) {
  1201. xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform;
  1202. } else {
  1203. ERR_PRINT("Collada: Invalid skeleton");
  1204. }
  1205. }
  1206. Vector3 s = xform.basis.get_scale();
  1207. 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);
  1208. Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat();
  1209. Vector3 l = xform.origin;
  1210. animation->transform_track_insert_key(track, snapshots[i], l, q, s);
  1211. }
  1212. if (nm.bone >= 0) {
  1213. if (found_anim)
  1214. bones_with_animation[E->get()] = true;
  1215. }
  1216. if (found_anim)
  1217. tracks_found = true;
  1218. else {
  1219. animation->remove_track(track);
  1220. }
  1221. }
  1222. if (p_make_tracks_in_all_bones) {
  1223. //some bones may lack animation, but since we don't store pose as a property, we must add keyframes!
  1224. for (Map<String, bool>::Element *E = bones_with_animation.front(); E; E = E->next()) {
  1225. if (E->get())
  1226. continue;
  1227. NodeMap &nm = node_map[E->key()];
  1228. String path = scene->get_path_to(nm.node);
  1229. ERR_CONTINUE(nm.bone < 0);
  1230. Skeleton *sk = static_cast<Skeleton *>(nm.node);
  1231. String name = sk->get_bone_name(nm.bone);
  1232. path = path + ":" + name;
  1233. Collada::Node *cn = collada.state.scene_map[E->key()];
  1234. if (cn->ignore_anim) {
  1235. WARN_PRINTS("Collada: Ignoring animation on node: " + path);
  1236. continue;
  1237. }
  1238. animation->add_track(Animation::TYPE_TRANSFORM);
  1239. int track = animation->get_track_count() - 1;
  1240. animation->track_set_path(track, path);
  1241. animation->track_set_imported(track, true); //helps merging later
  1242. Transform xform = cn->compute_transform(collada);
  1243. xform = collada.fix_transform(xform) * cn->post_transform;
  1244. xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform;
  1245. Vector3 s = xform.basis.get_scale();
  1246. 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);
  1247. Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat();
  1248. Vector3 l = xform.origin;
  1249. animation->transform_track_insert_key(track, 0, l, q, s);
  1250. tracks_found = true;
  1251. }
  1252. }
  1253. if (p_import_value_tracks) {
  1254. for (int i = 0; i < valid_animated_properties.size(); i++) {
  1255. int ti = valid_animated_properties[i];
  1256. if (p_clip == -1) {
  1257. if (track_filter.has(ti))
  1258. continue;
  1259. } else {
  1260. if (!track_filter.has(ti))
  1261. continue;
  1262. }
  1263. const Collada::AnimationTrack &at = collada.state.animation_tracks[ti];
  1264. // take snapshots
  1265. if (!collada.state.scene_map.has(at.target))
  1266. continue;
  1267. NodeMap &nm = node_map[at.target];
  1268. String path = scene->get_path_to(nm.node);
  1269. animation->add_track(Animation::TYPE_VALUE);
  1270. int track = animation->get_track_count() - 1;
  1271. path = path + ":" + at.param;
  1272. animation->track_set_path(track, path);
  1273. animation->track_set_imported(track, true); //helps merging later
  1274. for (int j = 0; j < at.keys.size(); j++) {
  1275. float time = at.keys[j].time;
  1276. Variant value;
  1277. Vector<float> data = at.keys[j].data;
  1278. if (data.size() == 1) {
  1279. //push a float
  1280. value = data[0];
  1281. } else if (data.size() == 16) {
  1282. //matrix
  1283. WARN_PRINT("Collada: Value keys for matrices not supported.");
  1284. } else {
  1285. WARN_PRINTS("Collada: Unexpected amount of value keys: " + itos(data.size()));
  1286. }
  1287. animation->track_insert_key(track, time, value);
  1288. }
  1289. tracks_found = true;
  1290. }
  1291. }
  1292. if (tracks_found) {
  1293. animations.push_back(animation);
  1294. }
  1295. }
  1296. /*********************************************************************************/
  1297. /*************************************** SCENE ***********************************/
  1298. /*********************************************************************************/
  1299. uint32_t EditorSceneImporterCollada::get_import_flags() const {
  1300. return IMPORT_SCENE | IMPORT_ANIMATION;
  1301. }
  1302. void EditorSceneImporterCollada::get_extensions(List<String> *r_extensions) const {
  1303. r_extensions->push_back("dae");
  1304. }
  1305. Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
  1306. ColladaImport state;
  1307. uint32_t flags = Collada::IMPORT_FLAG_SCENE;
  1308. if (p_flags & IMPORT_ANIMATION)
  1309. flags |= Collada::IMPORT_FLAG_ANIMATION;
  1310. state.use_mesh_builtin_materials = !(p_flags & IMPORT_MATERIALS_IN_INSTANCES);
  1311. state.bake_fps = p_bake_fps;
  1312. Error err = state.load(p_path, flags, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, p_flags & EditorSceneImporter::IMPORT_USE_COMPRESSION);
  1313. ERR_FAIL_COND_V_MSG(err != OK, NULL, "Cannot load scene from file '" + p_path + "'.");
  1314. if (state.missing_textures.size()) {
  1315. /*
  1316. for(int i=0;i<state.missing_textures.size();i++) {
  1317. EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
  1318. }
  1319. */
  1320. if (r_missing_deps) {
  1321. for (int i = 0; i < state.missing_textures.size(); i++) {
  1322. //EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
  1323. r_missing_deps->push_back(state.missing_textures[i]);
  1324. }
  1325. }
  1326. }
  1327. if (p_flags & IMPORT_ANIMATION) {
  1328. state.create_animations(p_flags & IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
  1329. AnimationPlayer *ap = memnew(AnimationPlayer);
  1330. for (int i = 0; i < state.animations.size(); i++) {
  1331. String name;
  1332. if (state.animations[i]->get_name() == "")
  1333. name = "default";
  1334. else
  1335. name = state.animations[i]->get_name();
  1336. if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
  1337. if (name.begins_with("loop") || name.ends_with("loop") || name.begins_with("cycle") || name.ends_with("cycle")) {
  1338. state.animations.write[i]->set_loop(true);
  1339. }
  1340. }
  1341. ap->add_animation(name, state.animations[i]);
  1342. }
  1343. state.scene->add_child(ap);
  1344. ap->set_owner(state.scene);
  1345. }
  1346. return state.scene;
  1347. }
  1348. Ref<Animation> EditorSceneImporterCollada::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
  1349. ColladaImport state;
  1350. state.use_mesh_builtin_materials = false;
  1351. Error err = state.load(p_path, Collada::IMPORT_FLAG_ANIMATION, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS);
  1352. ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load animation from file '" + p_path + "'.");
  1353. state.create_animations(p_flags & EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
  1354. if (state.scene)
  1355. memdelete(state.scene);
  1356. if (state.animations.size() == 0)
  1357. return Ref<Animation>();
  1358. Ref<Animation> anim = state.animations[0];
  1359. String base = p_path.get_basename().to_lower();
  1360. if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
  1361. if (base.begins_with("loop") || base.ends_with("loop") || base.begins_with("cycle") || base.ends_with("cycle")) {
  1362. anim->set_loop(true);
  1363. }
  1364. }
  1365. return anim;
  1366. }
  1367. EditorSceneImporterCollada::EditorSceneImporterCollada() {
  1368. }