editor_import_collada.cpp 56 KB

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