editor_import_collada.cpp 58 KB

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