editor_import_collada.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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. /*
  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_j = 0; p_j < p.count; p_j++) {
  542. int amount;
  543. if (p.polygons.size()) {
  544. ERR_FAIL_INDEX_V(p_j, p.polygons.size(), ERR_INVALID_DATA);
  545. amount = p.polygons[p_j];
  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 *ng2 = 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 (ng2->controller) {
  831. String ngsource = ng2->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 = ng2->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 meshid2 = names[i];
  883. if (collada.state.mesh_data_map.has(meshid2)) {
  884. Ref<ArrayMesh> mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  885. const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2];
  886. mesh->set_name(meshdata.name);
  887. Error err = _create_mesh_surfaces(false, mesh, ng2->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 = ng2->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, ng2->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 (ng2->material_map.has(matname)) {
  930. String target = ng2->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 err2 = _create_scene_skeletons(vs.root_nodes[i]);
  968. if (err2 != OK) {
  969. memdelete(scene);
  970. ERR_FAIL_COND_V(err2, err2);
  971. }
  972. }
  973. for (int i = 0; i < vs.root_nodes.size(); i++) {
  974. Error err2 = _create_scene(vs.root_nodes[i], scene);
  975. if (err2 != OK) {
  976. memdelete(scene);
  977. ERR_FAIL_COND_V(err2, err2);
  978. }
  979. Error err3 = _create_resources(vs.root_nodes[i], p_use_compression);
  980. if (err3 != OK) {
  981. memdelete(scene);
  982. ERR_FAIL_COND_V(err3, err3);
  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. Vector<float> data = at.get_value_at_time(snapshots[i]);
  1191. ERR_CONTINUE(data.empty());
  1192. Collada::Node::XForm &xf = cn->xform_list.write[xform_idx];
  1193. if (at.component == "ANGLE") {
  1194. ERR_CONTINUE(data.size() != 1);
  1195. ERR_CONTINUE(xf.op != Collada::Node::XForm::OP_ROTATE);
  1196. ERR_CONTINUE(xf.data.size() < 4);
  1197. xf.data.write[3] = data[0];
  1198. } else if (at.component == "X" || at.component == "Y" || at.component == "Z") {
  1199. int cn2 = at.component[0] - 'X';
  1200. ERR_CONTINUE(cn2 >= xf.data.size());
  1201. ERR_CONTINUE(data.size() > 1);
  1202. xf.data.write[cn2] = data[0];
  1203. } else if (data.size() == xf.data.size()) {
  1204. xf.data = data;
  1205. } else {
  1206. ERR_EXPLAIN("Component " + at.component + " has datasize " + itos(data.size()) + ", xfdatasize " + itos(xf.data.size()));
  1207. ERR_CONTINUE(data.size() != xf.data.size());
  1208. }
  1209. }
  1210. Transform xform = cn->compute_transform(collada);
  1211. xform = collada.fix_transform(xform) * cn->post_transform;
  1212. if (nm.bone >= 0) {
  1213. //make bone transform relative to rest (in case of skeleton)
  1214. Skeleton *sk = Object::cast_to<Skeleton>(nm.node);
  1215. if (sk) {
  1216. xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform;
  1217. } else {
  1218. ERR_PRINT("Collada: Invalid skeleton");
  1219. }
  1220. }
  1221. Vector3 s = xform.basis.get_scale();
  1222. 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);
  1223. Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat();
  1224. Vector3 l = xform.origin;
  1225. animation->transform_track_insert_key(track, snapshots[i], l, q, s);
  1226. }
  1227. if (nm.bone >= 0) {
  1228. if (found_anim)
  1229. bones_with_animation[E->get()] = true;
  1230. }
  1231. if (found_anim)
  1232. tracks_found = true;
  1233. else {
  1234. animation->remove_track(track);
  1235. }
  1236. }
  1237. if (p_make_tracks_in_all_bones) {
  1238. //some bones may lack animation, but since we don't store pose as a property, we must add keyframes!
  1239. for (Map<String, bool>::Element *E = bones_with_animation.front(); E; E = E->next()) {
  1240. if (E->get())
  1241. continue;
  1242. NodeMap &nm = node_map[E->key()];
  1243. String path = scene->get_path_to(nm.node);
  1244. ERR_CONTINUE(nm.bone < 0);
  1245. Skeleton *sk = static_cast<Skeleton *>(nm.node);
  1246. String name = sk->get_bone_name(nm.bone);
  1247. path = path + ":" + name;
  1248. Collada::Node *cn = collada.state.scene_map[E->key()];
  1249. if (cn->ignore_anim) {
  1250. WARN_PRINTS("Collada: Ignoring animation on node: " + path);
  1251. continue;
  1252. }
  1253. animation->add_track(Animation::TYPE_TRANSFORM);
  1254. int track = animation->get_track_count() - 1;
  1255. animation->track_set_path(track, path);
  1256. animation->track_set_imported(track, true); //helps merging later
  1257. Transform xform = cn->compute_transform(collada);
  1258. xform = collada.fix_transform(xform) * cn->post_transform;
  1259. xform = sk->get_bone_rest(nm.bone).affine_inverse() * xform;
  1260. Vector3 s = xform.basis.get_scale();
  1261. 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);
  1262. Quat q = singular_matrix ? Quat() : xform.basis.get_rotation_quat();
  1263. Vector3 l = xform.origin;
  1264. animation->transform_track_insert_key(track, 0, l, q, s);
  1265. tracks_found = true;
  1266. }
  1267. }
  1268. if (p_import_value_tracks) {
  1269. for (int i = 0; i < valid_animated_properties.size(); i++) {
  1270. int ti = valid_animated_properties[i];
  1271. if (p_clip == -1) {
  1272. if (track_filter.has(ti))
  1273. continue;
  1274. } else {
  1275. if (!track_filter.has(ti))
  1276. continue;
  1277. }
  1278. const Collada::AnimationTrack &at = collada.state.animation_tracks[ti];
  1279. // take snapshots
  1280. if (!collada.state.scene_map.has(at.target))
  1281. continue;
  1282. NodeMap &nm = node_map[at.target];
  1283. String path = scene->get_path_to(nm.node);
  1284. animation->add_track(Animation::TYPE_VALUE);
  1285. int track = animation->get_track_count() - 1;
  1286. path = path + ":" + at.param;
  1287. animation->track_set_path(track, path);
  1288. animation->track_set_imported(track, true); //helps merging later
  1289. for (int j = 0; j < at.keys.size(); j++) {
  1290. float time = at.keys[j].time;
  1291. Variant value;
  1292. Vector<float> data = at.keys[j].data;
  1293. if (data.size() == 1) {
  1294. //push a float
  1295. value = data[0];
  1296. } else if (data.size() == 16) {
  1297. //matrix
  1298. WARN_PRINT("Collada: Value keys for matrices not supported.");
  1299. } else {
  1300. WARN_PRINTS("Collada: Unexpected amount of value keys: " + itos(data.size()));
  1301. }
  1302. animation->track_insert_key(track, time, value);
  1303. }
  1304. tracks_found = true;
  1305. }
  1306. }
  1307. if (tracks_found) {
  1308. animations.push_back(animation);
  1309. }
  1310. }
  1311. /*********************************************************************************/
  1312. /*************************************** SCENE ***********************************/
  1313. /*********************************************************************************/
  1314. uint32_t EditorSceneImporterCollada::get_import_flags() const {
  1315. return IMPORT_SCENE | IMPORT_ANIMATION;
  1316. }
  1317. void EditorSceneImporterCollada::get_extensions(List<String> *r_extensions) const {
  1318. r_extensions->push_back("dae");
  1319. }
  1320. Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
  1321. ColladaImport state;
  1322. uint32_t flags = Collada::IMPORT_FLAG_SCENE;
  1323. if (p_flags & IMPORT_ANIMATION)
  1324. flags |= Collada::IMPORT_FLAG_ANIMATION;
  1325. state.use_mesh_builtin_materials = !(p_flags & IMPORT_MATERIALS_IN_INSTANCES);
  1326. state.bake_fps = p_bake_fps;
  1327. Error err = state.load(p_path, flags, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, p_flags & EditorSceneImporter::IMPORT_USE_COMPRESSION);
  1328. ERR_FAIL_COND_V(err != OK, NULL);
  1329. if (state.missing_textures.size()) {
  1330. /*
  1331. for(int i=0;i<state.missing_textures.size();i++) {
  1332. EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
  1333. }
  1334. */
  1335. if (r_missing_deps) {
  1336. for (int i = 0; i < state.missing_textures.size(); i++) {
  1337. //EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
  1338. r_missing_deps->push_back(state.missing_textures[i]);
  1339. }
  1340. }
  1341. }
  1342. if (p_flags & IMPORT_ANIMATION) {
  1343. state.create_animations(p_flags & IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
  1344. AnimationPlayer *ap = memnew(AnimationPlayer);
  1345. for (int i = 0; i < state.animations.size(); i++) {
  1346. String name;
  1347. if (state.animations[i]->get_name() == "")
  1348. name = "default";
  1349. else
  1350. name = state.animations[i]->get_name();
  1351. if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
  1352. if (name.begins_with("loop") || name.ends_with("loop") || name.begins_with("cycle") || name.ends_with("cycle")) {
  1353. state.animations.write[i]->set_loop(true);
  1354. }
  1355. }
  1356. ap->add_animation(name, state.animations[i]);
  1357. }
  1358. state.scene->add_child(ap);
  1359. ap->set_owner(state.scene);
  1360. }
  1361. return state.scene;
  1362. }
  1363. Ref<Animation> EditorSceneImporterCollada::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
  1364. ColladaImport state;
  1365. state.use_mesh_builtin_materials = false;
  1366. Error err = state.load(p_path, Collada::IMPORT_FLAG_ANIMATION, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS);
  1367. ERR_FAIL_COND_V(err != OK, RES());
  1368. state.create_animations(p_flags & EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
  1369. if (state.scene)
  1370. memdelete(state.scene);
  1371. if (state.animations.size() == 0)
  1372. return Ref<Animation>();
  1373. Ref<Animation> anim = state.animations[0];
  1374. String base = p_path.get_basename().to_lower();
  1375. if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
  1376. if (base.begins_with("loop") || base.ends_with("loop") || base.begins_with("cycle") || base.ends_with("cycle")) {
  1377. anim->set_loop(true);
  1378. }
  1379. }
  1380. return anim;
  1381. }
  1382. EditorSceneImporterCollada::EditorSceneImporterCollada() {
  1383. }