| 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 | |
| 31 | #include "editor_import_collada.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "editor/import/3d/collada.h" |
| 35 | #include "scene/3d/camera_3d.h" |
| 36 | #include "scene/3d/importer_mesh_instance_3d.h" |
| 37 | #include "scene/3d/light_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/3d/importer_mesh.h" |
| 43 | #include "scene/resources/animation.h" |
| 44 | #include "scene/resources/surface_tool.h" |
| 45 | |
| 46 | struct ColladaImport { |
| 47 | Collada collada; |
| 48 | Node3D *scene = nullptr; |
| 49 | |
| 50 | Vector<Ref<Animation>> animations; |
| 51 | |
| 52 | struct NodeMap { |
| 53 | //String path; |
| 54 | Node3D *node = nullptr; |
| 55 | int bone = -1; |
| 56 | List<int> anim_tracks; |
| 57 | }; |
| 58 | |
| 59 | bool found_ambient = false; |
| 60 | Color ambient; |
| 61 | bool found_directional = false; |
| 62 | bool force_make_tangents = false; |
| 63 | bool apply_mesh_xform_to_vertices = true; |
| 64 | bool use_mesh_builtin_materials = false; |
| 65 | float bake_fps = 30; |
| 66 | |
| 67 | HashMap<String, NodeMap> node_map; //map from collada node to engine node |
| 68 | HashMap<String, String> node_name_map; //map from collada node to engine node |
| 69 | HashMap<String, Ref<ImporterMesh>> mesh_cache; |
| 70 | HashMap<String, Ref<Curve3D>> curve_cache; |
| 71 | HashMap<String, Ref<Material>> material_cache; |
| 72 | HashMap<Collada::Node *, Skeleton3D *> skeleton_map; |
| 73 | |
| 74 | HashMap<Skeleton3D *, HashMap<String, int>> skeleton_bone_map; |
| 75 | |
| 76 | HashSet<String> valid_animated_nodes; |
| 77 | Vector<int> valid_animated_properties; |
| 78 | HashMap<String, bool> bones_with_animation; |
| 79 | |
| 80 | HashSet<String> mesh_unique_names; |
| 81 | HashSet<String> material_unique_names; |
| 82 | |
| 83 | Error _populate_skeleton(Skeleton3D *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent); |
| 84 | Error _create_scene_skeletons(Collada::Node *p_node); |
| 85 | Error _create_scene(Collada::Node *p_node, Node3D *p_parent); |
| 86 | Error _create_resources(Collada::Node *p_node, bool p_use_compression); |
| 87 | Error _create_material(const String &p_target); |
| 88 | 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, const Vector<Ref<ImporterMesh>> &p_morph_meshes = Vector<Ref<ImporterMesh>>(), bool p_use_compression = false, bool p_use_mesh_material = false); |
| 89 | Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false, bool p_use_compression = false); |
| 90 | void _fix_param_animation_tracks(); |
| 91 | void create_animation(int p_clip, bool p_import_value_tracks); |
| 92 | void create_animations(bool p_import_value_tracks); |
| 93 | |
| 94 | HashSet<String> tracks_in_clips; |
| 95 | Vector<String> missing_textures; |
| 96 | |
| 97 | void _pre_process_lights(Collada::Node *p_node); |
| 98 | }; |
| 99 | |
| 100 | Error ColladaImport::_populate_skeleton(Skeleton3D *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent) { |
| 101 | if (p_node->type != Collada::Node::TYPE_JOINT) { |
| 102 | return OK; |
| 103 | } |
| 104 | |
| 105 | Collada::NodeJoint *joint = static_cast<Collada::NodeJoint *>(p_node); |
| 106 | |
| 107 | p_skeleton->add_bone(p_node->name); |
| 108 | if (p_parent >= 0) { |
| 109 | p_skeleton->set_bone_parent(r_bone, p_parent); |
| 110 | } |
| 111 | |
| 112 | NodeMap nm; |
| 113 | nm.node = p_skeleton; |
| 114 | nm.bone = r_bone; |
| 115 | node_map[p_node->id] = nm; |
| 116 | node_name_map[p_node->name] = p_node->id; |
| 117 | |
| 118 | skeleton_bone_map[p_skeleton][joint->sid] = r_bone; |
| 119 | |
| 120 | { |
| 121 | Transform3D xform = joint->compute_transform(collada); |
| 122 | xform = collada.fix_transform(xform) * joint->post_transform; |
| 123 | |
| 124 | p_skeleton->set_bone_pose_position(r_bone, xform.origin); |
| 125 | p_skeleton->set_bone_pose_rotation(r_bone, xform.basis.get_rotation_quaternion()); |
| 126 | p_skeleton->set_bone_pose_scale(r_bone, xform.basis.get_scale()); |
| 127 | } |
| 128 | |
| 129 | if (collada.state.bone_rest_map.has(joint->sid)) { |
| 130 | p_skeleton->set_bone_rest(r_bone, collada.fix_transform(collada.state.bone_rest_map[joint->sid])); |
| 131 | //should map this bone to something for animation? |
| 132 | } else { |
| 133 | WARN_PRINT("Collada: Joint has no rest."); |
| 134 | } |
| 135 | |
| 136 | int id = r_bone++; |
| 137 | for (int i = 0; i < p_node->children.size(); i++) { |
| 138 | Error err = _populate_skeleton(p_skeleton, p_node->children[i], r_bone, id); |
| 139 | if (err) { |
| 140 | return err; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return OK; |
| 145 | } |
| 146 | |
| 147 | void ColladaImport::_pre_process_lights(Collada::Node *p_node) { |
| 148 | if (p_node->type == Collada::Node::TYPE_LIGHT) { |
| 149 | Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node); |
| 150 | if (collada.state.light_data_map.has(light->light)) { |
| 151 | Collada::LightData &ld = collada.state.light_data_map[light->light]; |
| 152 | if (ld.mode == Collada::LightData::MODE_AMBIENT) { |
| 153 | found_ambient = true; |
| 154 | ambient = ld.color; |
| 155 | } |
| 156 | if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) { |
| 157 | found_directional = true; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | for (int i = 0; i < p_node->children.size(); i++) { |
| 163 | _pre_process_lights(p_node->children[i]); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | Error ColladaImport::_create_scene_skeletons(Collada::Node *p_node) { |
| 168 | if (p_node->type == Collada::Node::TYPE_SKELETON) { |
| 169 | Skeleton3D *sk = memnew(Skeleton3D); |
| 170 | int bone = 0; |
| 171 | for (int i = 0; i < p_node->children.size(); i++) { |
| 172 | _populate_skeleton(sk, p_node->children[i], bone, -1); |
| 173 | } |
| 174 | sk->localize_rests(); //after creating skeleton, rests must be localized...! |
| 175 | skeleton_map[p_node] = sk; |
| 176 | } |
| 177 | |
| 178 | for (int i = 0; i < p_node->children.size(); i++) { |
| 179 | Error err = _create_scene_skeletons(p_node->children[i]); |
| 180 | if (err) { |
| 181 | return err; |
| 182 | } |
| 183 | } |
| 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | Error ColladaImport::_create_scene(Collada::Node *p_node, Node3D *p_parent) { |
| 188 | Node3D *node = nullptr; |
| 189 | |
| 190 | switch (p_node->type) { |
| 191 | case Collada::Node::TYPE_NODE: { |
| 192 | node = memnew(Node3D); |
| 193 | } break; |
| 194 | case Collada::Node::TYPE_JOINT: { |
| 195 | return OK; // do nothing |
| 196 | } break; |
| 197 | case Collada::Node::TYPE_LIGHT: { |
| 198 | //node = memnew( Light) |
| 199 | Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node); |
| 200 | if (collada.state.light_data_map.has(light->light)) { |
| 201 | Collada::LightData &ld = collada.state.light_data_map[light->light]; |
| 202 | |
| 203 | if (ld.mode == Collada::LightData::MODE_AMBIENT) { |
| 204 | if (found_directional) { |
| 205 | return OK; //do nothing not needed |
| 206 | } |
| 207 | |
| 208 | if (!bool(GLOBAL_GET("collada/use_ambient"))) { |
| 209 | return OK; |
| 210 | } |
| 211 | //well, it's an ambient light.. |
| 212 | Light3D *l = memnew(DirectionalLight3D); |
| 213 | //l->set_color(Light::COLOR_AMBIENT,ld.color); |
| 214 | //l->set_color(Light::COLOR_DIFFUSE,Color(0,0,0)); |
| 215 | //l->set_color(Light::COLOR_SPECULAR,Color(0,0,0)); |
| 216 | node = l; |
| 217 | |
| 218 | } else if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) { |
| 219 | //well, it's an ambient light.. |
| 220 | Light3D *l = memnew(DirectionalLight3D); |
| 221 | /* |
| 222 | if (found_ambient) //use it here |
| 223 | l->set_color(Light::COLOR_AMBIENT,ambient); |
| 224 | |
| 225 | l->set_color(Light::COLOR_DIFFUSE,ld.color); |
| 226 | l->set_color(Light::COLOR_SPECULAR,Color(1,1,1)); |
| 227 | */ |
| 228 | node = l; |
| 229 | } else { |
| 230 | Light3D *l; |
| 231 | |
| 232 | if (ld.mode == Collada::LightData::MODE_OMNI) { |
| 233 | l = memnew(OmniLight3D); |
| 234 | } else { |
| 235 | l = memnew(SpotLight3D); |
| 236 | //l->set_parameter(Light::PARAM_SPOT_ANGLE,ld.spot_angle); |
| 237 | //l->set_parameter(Light::PARAM_SPOT_ATTENUATION,ld.spot_exp); |
| 238 | } |
| 239 | |
| 240 | // |
| 241 | //l->set_color(Light::COLOR_DIFFUSE,ld.color); |
| 242 | //l->set_color(Light::COLOR_SPECULAR,Color(1,1,1)); |
| 243 | //l->approximate_opengl_attenuation(ld.constant_att,ld.linear_att,ld.quad_att); |
| 244 | node = l; |
| 245 | } |
| 246 | |
| 247 | } else { |
| 248 | node = memnew(Node3D); |
| 249 | } |
| 250 | } break; |
| 251 | case Collada::Node::TYPE_CAMERA: { |
| 252 | Collada::NodeCamera *cam = static_cast<Collada::NodeCamera *>(p_node); |
| 253 | Camera3D *camera = memnew(Camera3D); |
| 254 | |
| 255 | if (collada.state.camera_data_map.has(cam->camera)) { |
| 256 | const Collada::CameraData &cd = collada.state.camera_data_map[cam->camera]; |
| 257 | |
| 258 | switch (cd.mode) { |
| 259 | case Collada::CameraData::MODE_ORTHOGONAL: { |
| 260 | if (cd.orthogonal.y_mag) { |
| 261 | camera->set_keep_aspect_mode(Camera3D::KEEP_HEIGHT); |
| 262 | camera->set_orthogonal(cd.orthogonal.y_mag * 2.0, cd.z_near, cd.z_far); |
| 263 | |
| 264 | } else if (!cd.orthogonal.y_mag && cd.orthogonal.x_mag) { |
| 265 | camera->set_keep_aspect_mode(Camera3D::KEEP_WIDTH); |
| 266 | camera->set_orthogonal(cd.orthogonal.x_mag * 2.0, cd.z_near, cd.z_far); |
| 267 | } |
| 268 | |
| 269 | } break; |
| 270 | case Collada::CameraData::MODE_PERSPECTIVE: { |
| 271 | if (cd.perspective.y_fov) { |
| 272 | camera->set_perspective(cd.perspective.y_fov, cd.z_near, cd.z_far); |
| 273 | |
| 274 | } else if (!cd.perspective.y_fov && cd.perspective.x_fov) { |
| 275 | camera->set_perspective(cd.perspective.x_fov / cd.aspect, cd.z_near, cd.z_far); |
| 276 | } |
| 277 | |
| 278 | } break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | node = camera; |
| 283 | |
| 284 | } break; |
| 285 | case Collada::Node::TYPE_GEOMETRY: { |
| 286 | Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node); |
| 287 | |
| 288 | if (collada.state.curve_data_map.has(ng->source)) { |
| 289 | node = memnew(Path3D); |
| 290 | } else { |
| 291 | //mesh since nothing else |
| 292 | node = memnew(ImporterMeshInstance3D); |
| 293 | //Object::cast_to<ImporterMeshInstance3D>(node)->set_flag(GeometryInstance3D::FLAG_USE_BAKED_LIGHT, true); |
| 294 | } |
| 295 | } break; |
| 296 | case Collada::Node::TYPE_SKELETON: { |
| 297 | ERR_FAIL_COND_V(!skeleton_map.has(p_node), ERR_CANT_CREATE); |
| 298 | Skeleton3D *sk = skeleton_map[p_node]; |
| 299 | node = sk; |
| 300 | } break; |
| 301 | } |
| 302 | |
| 303 | if (!p_node->name.is_empty()) { |
| 304 | node->set_name(p_node->name); |
| 305 | } |
| 306 | NodeMap nm; |
| 307 | nm.node = node; |
| 308 | node_map[p_node->id] = nm; |
| 309 | node_name_map[node->get_name()] = p_node->id; |
| 310 | Transform3D xf = p_node->default_transform; |
| 311 | |
| 312 | xf = collada.fix_transform(xf) * p_node->post_transform; |
| 313 | node->set_transform(xf); |
| 314 | p_parent->add_child(node, true); |
| 315 | node->set_owner(scene); |
| 316 | |
| 317 | if (!p_node->empty_draw_type.is_empty()) { |
| 318 | node->set_meta("empty_draw_type", Variant(p_node->empty_draw_type)); |
| 319 | } |
| 320 | |
| 321 | for (int i = 0; i < p_node->children.size(); i++) { |
| 322 | Error err = _create_scene(p_node->children[i], node); |
| 323 | if (err) { |
| 324 | return err; |
| 325 | } |
| 326 | } |
| 327 | return OK; |
| 328 | } |
| 329 | |
| 330 | Error ColladaImport::_create_material(const String &p_target) { |
| 331 | ERR_FAIL_COND_V(material_cache.has(p_target), ERR_ALREADY_EXISTS); |
| 332 | ERR_FAIL_COND_V(!collada.state.material_map.has(p_target), ERR_INVALID_PARAMETER); |
| 333 | Collada::Material &src_mat = collada.state.material_map[p_target]; |
| 334 | ERR_FAIL_COND_V(!collada.state.effect_map.has(src_mat.instance_effect), ERR_INVALID_PARAMETER); |
| 335 | Collada::Effect &effect = collada.state.effect_map[src_mat.instance_effect]; |
| 336 | |
| 337 | Ref<StandardMaterial3D> material = memnew(StandardMaterial3D); |
| 338 | |
| 339 | String base_name; |
| 340 | if (!src_mat.name.is_empty()) { |
| 341 | base_name = src_mat.name; |
| 342 | } else if (!effect.name.is_empty()) { |
| 343 | base_name = effect.name; |
| 344 | } else { |
| 345 | base_name = "Material"; |
| 346 | } |
| 347 | |
| 348 | String name = base_name; |
| 349 | int counter = 2; |
| 350 | while (material_unique_names.has(name)) { |
| 351 | name = base_name + itos(counter++); |
| 352 | } |
| 353 | |
| 354 | material_unique_names.insert(name); |
| 355 | |
| 356 | material->set_name(name); |
| 357 | |
| 358 | // DIFFUSE |
| 359 | |
| 360 | if (!effect.diffuse.texture.is_empty()) { |
| 361 | String texfile = effect.get_texture_path(effect.diffuse.texture, collada); |
| 362 | if (!texfile.is_empty()) { |
| 363 | if (texfile.begins_with("/")) { |
| 364 | texfile = texfile.replace_first("/", "res://"); |
| 365 | } |
| 366 | Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D"); |
| 367 | if (texture.is_valid()) { |
| 368 | material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture); |
| 369 | material->set_albedo(Color(1, 1, 1, 1)); |
| 370 | //material->set_parameter(StandardMaterial3D::PARAM_DIFFUSE,Color(1,1,1,1)); |
| 371 | } else { |
| 372 | missing_textures.push_back(texfile.get_file()); |
| 373 | } |
| 374 | } |
| 375 | } else { |
| 376 | material->set_albedo(effect.diffuse.color); |
| 377 | } |
| 378 | |
| 379 | // SPECULAR |
| 380 | |
| 381 | if (!effect.specular.texture.is_empty()) { |
| 382 | String texfile = effect.get_texture_path(effect.specular.texture, collada); |
| 383 | if (!texfile.is_empty()) { |
| 384 | if (texfile.begins_with("/")) { |
| 385 | texfile = texfile.replace_first("/", "res://"); |
| 386 | } |
| 387 | |
| 388 | Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D"); |
| 389 | if (texture.is_valid()) { |
| 390 | material->set_texture(StandardMaterial3D::TEXTURE_METALLIC, texture); |
| 391 | material->set_specular(1.0); |
| 392 | |
| 393 | //material->set_texture(StandardMaterial3D::PARAM_SPECULAR,texture); |
| 394 | //material->set_parameter(StandardMaterial3D::PARAM_SPECULAR,Color(1,1,1,1)); |
| 395 | } else { |
| 396 | missing_textures.push_back(texfile.get_file()); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | } else { |
| 401 | material->set_metallic(effect.specular.color.get_v()); |
| 402 | } |
| 403 | |
| 404 | // EMISSION |
| 405 | |
| 406 | if (!effect.emission.texture.is_empty()) { |
| 407 | String texfile = effect.get_texture_path(effect.emission.texture, collada); |
| 408 | if (!texfile.is_empty()) { |
| 409 | if (texfile.begins_with("/")) { |
| 410 | texfile = texfile.replace_first("/", "res://"); |
| 411 | } |
| 412 | |
| 413 | Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D"); |
| 414 | if (texture.is_valid()) { |
| 415 | material->set_feature(StandardMaterial3D::FEATURE_EMISSION, true); |
| 416 | material->set_texture(StandardMaterial3D::TEXTURE_EMISSION, texture); |
| 417 | material->set_emission(Color(1, 1, 1, 1)); |
| 418 | |
| 419 | //material->set_parameter(StandardMaterial3D::PARAM_EMISSION,Color(1,1,1,1)); |
| 420 | } else { |
| 421 | missing_textures.push_back(texfile.get_file()); |
| 422 | } |
| 423 | } |
| 424 | } else { |
| 425 | if (effect.emission.color != Color()) { |
| 426 | material->set_feature(StandardMaterial3D::FEATURE_EMISSION, true); |
| 427 | material->set_emission(effect.emission.color); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // NORMAL |
| 432 | |
| 433 | if (!effect.bump.texture.is_empty()) { |
| 434 | String texfile = effect.get_texture_path(effect.bump.texture, collada); |
| 435 | if (!texfile.is_empty()) { |
| 436 | if (texfile.begins_with("/")) { |
| 437 | texfile = texfile.replace_first("/", "res://"); |
| 438 | } |
| 439 | |
| 440 | Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D"); |
| 441 | if (texture.is_valid()) { |
| 442 | material->set_feature(StandardMaterial3D::FEATURE_NORMAL_MAPPING, true); |
| 443 | material->set_texture(StandardMaterial3D::TEXTURE_NORMAL, texture); |
| 444 | //material->set_emission(Color(1,1,1,1)); |
| 445 | |
| 446 | //material->set_texture(StandardMaterial3D::PARAM_NORMAL,texture); |
| 447 | } else { |
| 448 | //missing_textures.push_back(texfile.get_file()); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | float roughness = (effect.shininess - 1.0) / 510; |
| 454 | material->set_roughness(roughness); |
| 455 | |
| 456 | if (effect.double_sided) { |
| 457 | material->set_cull_mode(StandardMaterial3D::CULL_DISABLED); |
| 458 | } |
| 459 | if (effect.unshaded) { |
| 460 | material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 461 | } |
| 462 | |
| 463 | material_cache[p_target] = material; |
| 464 | return OK; |
| 465 | } |
| 466 | |
| 467 | 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, const Vector<Ref<ImporterMesh>> &p_morph_meshes, bool p_use_compression, bool p_use_mesh_material) { |
| 468 | bool local_xform_mirror = p_local_xform.basis.determinant() < 0; |
| 469 | |
| 470 | if (p_morph_data) { |
| 471 | //add morph target |
| 472 | ERR_FAIL_COND_V(!p_morph_data->targets.has("MORPH_TARGET"), ERR_INVALID_DATA); |
| 473 | String mt = p_morph_data->targets["MORPH_TARGET"]; |
| 474 | ERR_FAIL_COND_V(!p_morph_data->sources.has(mt), ERR_INVALID_DATA); |
| 475 | int morph_targets = p_morph_data->sources[mt].sarray.size(); |
| 476 | for (int i = 0; i < morph_targets; i++) { |
| 477 | String target = p_morph_data->sources[mt].sarray[i]; |
| 478 | ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(target), ERR_INVALID_DATA); |
| 479 | String name = collada.state.mesh_data_map[target].name; |
| 480 | |
| 481 | p_mesh->add_blend_shape(name); |
| 482 | } |
| 483 | if (p_morph_data->mode == "RELATIVE") { |
| 484 | p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_RELATIVE); |
| 485 | } else if (p_morph_data->mode == "NORMALIZED") { |
| 486 | p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | int surface = 0; |
| 491 | for (int p_i = 0; p_i < meshdata.primitives.size(); p_i++) { |
| 492 | const Collada::MeshData::Primitives &p = meshdata.primitives[p_i]; |
| 493 | |
| 494 | /* VERTEX SOURCE */ |
| 495 | ERR_FAIL_COND_V(!p.sources.has("VERTEX"), ERR_INVALID_DATA); |
| 496 | |
| 497 | String vertex_src_id = p.sources["VERTEX"].source; |
| 498 | int vertex_ofs = p.sources["VERTEX"].offset; |
| 499 | |
| 500 | ERR_FAIL_COND_V(!meshdata.vertices.has(vertex_src_id), ERR_INVALID_DATA); |
| 501 | |
| 502 | ERR_FAIL_COND_V(!meshdata.vertices[vertex_src_id].sources.has("POSITION"), ERR_INVALID_DATA); |
| 503 | String position_src_id = meshdata.vertices[vertex_src_id].sources["POSITION"]; |
| 504 | |
| 505 | ERR_FAIL_COND_V(!meshdata.sources.has(position_src_id), ERR_INVALID_DATA); |
| 506 | |
| 507 | const Collada::MeshData::Source *vertex_src = &meshdata.sources[position_src_id]; |
| 508 | |
| 509 | /* NORMAL SOURCE */ |
| 510 | |
| 511 | const Collada::MeshData::Source *normal_src = nullptr; |
| 512 | int normal_ofs = 0; |
| 513 | |
| 514 | { |
| 515 | String normal_source_id = ""; |
| 516 | |
| 517 | if (p.sources.has("NORMAL")) { |
| 518 | normal_source_id = p.sources["NORMAL"].source; |
| 519 | normal_ofs = p.sources["NORMAL"].offset; |
| 520 | } else if (meshdata.vertices[vertex_src_id].sources.has("NORMAL")) { |
| 521 | normal_source_id = meshdata.vertices[vertex_src_id].sources["NORMAL"]; |
| 522 | normal_ofs = vertex_ofs; |
| 523 | } |
| 524 | |
| 525 | if (!normal_source_id.is_empty()) { |
| 526 | ERR_FAIL_COND_V(!meshdata.sources.has(normal_source_id), ERR_INVALID_DATA); |
| 527 | normal_src = &meshdata.sources[normal_source_id]; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | const Collada::MeshData::Source *binormal_src = nullptr; |
| 532 | int binormal_ofs = 0; |
| 533 | |
| 534 | { |
| 535 | String binormal_source_id = ""; |
| 536 | |
| 537 | if (p.sources.has("TEXBINORMAL")) { |
| 538 | binormal_source_id = p.sources["TEXBINORMAL"].source; |
| 539 | binormal_ofs = p.sources["TEXBINORMAL"].offset; |
| 540 | } else if (meshdata.vertices[vertex_src_id].sources.has("TEXBINORMAL")) { |
| 541 | binormal_source_id = meshdata.vertices[vertex_src_id].sources["TEXBINORMAL"]; |
| 542 | binormal_ofs = vertex_ofs; |
| 543 | } |
| 544 | |
| 545 | if (!binormal_source_id.is_empty()) { |
| 546 | ERR_FAIL_COND_V(!meshdata.sources.has(binormal_source_id), ERR_INVALID_DATA); |
| 547 | binormal_src = &meshdata.sources[binormal_source_id]; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | const Collada::MeshData::Source *tangent_src = nullptr; |
| 552 | int tangent_ofs = 0; |
| 553 | |
| 554 | { |
| 555 | String tangent_source_id = ""; |
| 556 | |
| 557 | if (p.sources.has("TEXTANGENT")) { |
| 558 | tangent_source_id = p.sources["TEXTANGENT"].source; |
| 559 | tangent_ofs = p.sources["TEXTANGENT"].offset; |
| 560 | } else if (meshdata.vertices[vertex_src_id].sources.has("TEXTANGENT")) { |
| 561 | tangent_source_id = meshdata.vertices[vertex_src_id].sources["TEXTANGENT"]; |
| 562 | tangent_ofs = vertex_ofs; |
| 563 | } |
| 564 | |
| 565 | if (!tangent_source_id.is_empty()) { |
| 566 | ERR_FAIL_COND_V(!meshdata.sources.has(tangent_source_id), ERR_INVALID_DATA); |
| 567 | tangent_src = &meshdata.sources[tangent_source_id]; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | const Collada::MeshData::Source *uv_src = nullptr; |
| 572 | int uv_ofs = 0; |
| 573 | |
| 574 | { |
| 575 | String uv_source_id = ""; |
| 576 | |
| 577 | if (p.sources.has("TEXCOORD0")) { |
| 578 | uv_source_id = p.sources["TEXCOORD0"].source; |
| 579 | uv_ofs = p.sources["TEXCOORD0"].offset; |
| 580 | } else if (meshdata.vertices[vertex_src_id].sources.has("TEXCOORD0")) { |
| 581 | uv_source_id = meshdata.vertices[vertex_src_id].sources["TEXCOORD0"]; |
| 582 | uv_ofs = vertex_ofs; |
| 583 | } |
| 584 | |
| 585 | if (!uv_source_id.is_empty()) { |
| 586 | ERR_FAIL_COND_V(!meshdata.sources.has(uv_source_id), ERR_INVALID_DATA); |
| 587 | uv_src = &meshdata.sources[uv_source_id]; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | const Collada::MeshData::Source *uv2_src = nullptr; |
| 592 | int uv2_ofs = 0; |
| 593 | |
| 594 | { |
| 595 | String uv2_source_id = ""; |
| 596 | |
| 597 | if (p.sources.has("TEXCOORD1")) { |
| 598 | uv2_source_id = p.sources["TEXCOORD1"].source; |
| 599 | uv2_ofs = p.sources["TEXCOORD1"].offset; |
| 600 | } else if (meshdata.vertices[vertex_src_id].sources.has("TEXCOORD1")) { |
| 601 | uv2_source_id = meshdata.vertices[vertex_src_id].sources["TEXCOORD1"]; |
| 602 | uv2_ofs = vertex_ofs; |
| 603 | } |
| 604 | |
| 605 | if (!uv2_source_id.is_empty()) { |
| 606 | ERR_FAIL_COND_V(!meshdata.sources.has(uv2_source_id), ERR_INVALID_DATA); |
| 607 | uv2_src = &meshdata.sources[uv2_source_id]; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | const Collada::MeshData::Source *color_src = nullptr; |
| 612 | int color_ofs = 0; |
| 613 | |
| 614 | { |
| 615 | String color_source_id = ""; |
| 616 | |
| 617 | if (p.sources.has("COLOR")) { |
| 618 | color_source_id = p.sources["COLOR"].source; |
| 619 | color_ofs = p.sources["COLOR"].offset; |
| 620 | } else if (meshdata.vertices[vertex_src_id].sources.has("COLOR")) { |
| 621 | color_source_id = meshdata.vertices[vertex_src_id].sources["COLOR"]; |
| 622 | color_ofs = vertex_ofs; |
| 623 | } |
| 624 | |
| 625 | if (!color_source_id.is_empty()) { |
| 626 | ERR_FAIL_COND_V(!meshdata.sources.has(color_source_id), ERR_INVALID_DATA); |
| 627 | color_src = &meshdata.sources[color_source_id]; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | //find largest source.. |
| 632 | |
| 633 | /************************/ |
| 634 | /* ADD WEIGHTS IF EXIST */ |
| 635 | /************************/ |
| 636 | |
| 637 | HashMap<int, Vector<Collada::Vertex::Weight>> pre_weights; |
| 638 | |
| 639 | bool has_weights = false; |
| 640 | |
| 641 | if (p_skin_controller) { |
| 642 | const Collada::SkinControllerData::Source *weight_src = nullptr; |
| 643 | int weight_ofs = 0; |
| 644 | |
| 645 | if (p_skin_controller->weights.sources.has("WEIGHT")) { |
| 646 | String weight_id = p_skin_controller->weights.sources["WEIGHT"].source; |
| 647 | weight_ofs = p_skin_controller->weights.sources["WEIGHT"].offset; |
| 648 | if (p_skin_controller->sources.has(weight_id)) { |
| 649 | weight_src = &p_skin_controller->sources[weight_id]; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | int joint_ofs = 0; |
| 654 | |
| 655 | if (p_skin_controller->weights.sources.has("JOINT")) { |
| 656 | joint_ofs = p_skin_controller->weights.sources["JOINT"].offset; |
| 657 | } |
| 658 | |
| 659 | //should be OK, given this was pre-checked. |
| 660 | |
| 661 | int index_ofs = 0; |
| 662 | int wstride = p_skin_controller->weights.sources.size(); |
| 663 | for (int w_i = 0; w_i < p_skin_controller->weights.sets.size(); w_i++) { |
| 664 | int amount = p_skin_controller->weights.sets[w_i]; |
| 665 | |
| 666 | Vector<Collada::Vertex::Weight> weights; |
| 667 | |
| 668 | for (int a_i = 0; a_i < amount; a_i++) { |
| 669 | Collada::Vertex::Weight w; |
| 670 | |
| 671 | int read_from = index_ofs + a_i * wstride; |
| 672 | ERR_FAIL_INDEX_V(read_from + wstride - 1, p_skin_controller->weights.indices.size(), ERR_INVALID_DATA); |
| 673 | int weight_index = p_skin_controller->weights.indices[read_from + weight_ofs]; |
| 674 | ERR_FAIL_INDEX_V(weight_index, weight_src->array.size(), ERR_INVALID_DATA); |
| 675 | |
| 676 | w.weight = weight_src->array[weight_index]; |
| 677 | |
| 678 | int bone_index = p_skin_controller->weights.indices[read_from + joint_ofs]; |
| 679 | if (bone_index == -1) { |
| 680 | continue; //ignore this weight (refers to bind shape) |
| 681 | } |
| 682 | ERR_FAIL_INDEX_V(bone_index, bone_remap.size(), ERR_INVALID_DATA); |
| 683 | |
| 684 | w.bone_idx = bone_remap[bone_index]; |
| 685 | |
| 686 | weights.push_back(w); |
| 687 | } |
| 688 | |
| 689 | /* FIX WEIGHTS */ |
| 690 | |
| 691 | weights.sort(); |
| 692 | |
| 693 | if (weights.size() > 4) { |
| 694 | //cap to 4 and make weights add up 1 |
| 695 | weights.resize(4); |
| 696 | } |
| 697 | |
| 698 | //make sure weights always add up to 1 |
| 699 | float total = 0; |
| 700 | for (int i = 0; i < weights.size(); i++) { |
| 701 | total += weights[i].weight; |
| 702 | } |
| 703 | if (total) { |
| 704 | for (int i = 0; i < weights.size(); i++) { |
| 705 | weights.write[i].weight /= total; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (weights.is_empty() || total == 0) { //if nothing, add a weight to bone 0 |
| 710 | //no weights assigned |
| 711 | Collada::Vertex::Weight w; |
| 712 | w.bone_idx = 0; |
| 713 | w.weight = 1.0; |
| 714 | weights.clear(); |
| 715 | weights.push_back(w); |
| 716 | } |
| 717 | |
| 718 | pre_weights[w_i] = weights; |
| 719 | |
| 720 | index_ofs += wstride * amount; |
| 721 | } |
| 722 | |
| 723 | //vertices need to be localized |
| 724 | has_weights = true; |
| 725 | } |
| 726 | |
| 727 | RBSet<Collada::Vertex> vertex_set; //vertex set will be the vertices |
| 728 | List<int> indices_list; //indices will be the indices |
| 729 | |
| 730 | /**************************/ |
| 731 | /* CREATE PRIMITIVE ARRAY */ |
| 732 | /**************************/ |
| 733 | |
| 734 | // The way collada uses indices is more optimal, and friendlier with 3D modeling software, |
| 735 | // because it can index everything, not only vertices (similar to how the WII works). |
| 736 | // This is, however, more incompatible with standard video cards, so arrays must be converted. |
| 737 | // Must convert to GL/DX format. |
| 738 | |
| 739 | int _prim_ofs = 0; |
| 740 | int vertidx = 0; |
| 741 | for (int p_j = 0; p_j < p.count; p_j++) { |
| 742 | int amount; |
| 743 | if (p.polygons.size()) { |
| 744 | ERR_FAIL_INDEX_V(p_j, p.polygons.size(), ERR_INVALID_DATA); |
| 745 | amount = p.polygons[p_j]; |
| 746 | } else { |
| 747 | amount = 3; //triangles; |
| 748 | } |
| 749 | |
| 750 | //COLLADA_PRINT("amount: "+itos(amount)); |
| 751 | |
| 752 | int prev2[2] = { 0, 0 }; |
| 753 | |
| 754 | for (int j = 0; j < amount; j++) { |
| 755 | int src = _prim_ofs; |
| 756 | //_prim_ofs+=p.sources.size() |
| 757 | |
| 758 | ERR_FAIL_INDEX_V(src, p.indices.size(), ERR_INVALID_DATA); |
| 759 | |
| 760 | Collada::Vertex vertex; |
| 761 | if (!p_optimize) { |
| 762 | vertex.uid = vertidx++; |
| 763 | } |
| 764 | |
| 765 | int vertex_index = p.indices[src + vertex_ofs]; //used for index field (later used by controllers) |
| 766 | int vertex_pos = (vertex_src->stride ? vertex_src->stride : 3) * vertex_index; |
| 767 | ERR_FAIL_INDEX_V(vertex_pos + 0, vertex_src->array.size(), ERR_INVALID_DATA); |
| 768 | ERR_FAIL_INDEX_V(vertex_pos + 2, vertex_src->array.size(), ERR_INVALID_DATA); |
| 769 | vertex.vertex = Vector3(vertex_src->array[vertex_pos + 0], vertex_src->array[vertex_pos + 1], vertex_src->array[vertex_pos + 2]); |
| 770 | |
| 771 | if (pre_weights.has(vertex_index)) { |
| 772 | vertex.weights = pre_weights[vertex_index]; |
| 773 | } |
| 774 | |
| 775 | if (normal_src) { |
| 776 | int normal_pos = (normal_src->stride ? normal_src->stride : 3) * p.indices[src + normal_ofs]; |
| 777 | ERR_FAIL_INDEX_V(normal_pos + 0, normal_src->array.size(), ERR_INVALID_DATA); |
| 778 | ERR_FAIL_INDEX_V(normal_pos + 2, normal_src->array.size(), ERR_INVALID_DATA); |
| 779 | vertex.normal = Vector3(normal_src->array[normal_pos + 0], normal_src->array[normal_pos + 1], normal_src->array[normal_pos + 2]); |
| 780 | |
| 781 | if (tangent_src && binormal_src) { |
| 782 | int binormal_pos = (binormal_src->stride ? binormal_src->stride : 3) * p.indices[src + binormal_ofs]; |
| 783 | ERR_FAIL_INDEX_V(binormal_pos + 0, binormal_src->array.size(), ERR_INVALID_DATA); |
| 784 | ERR_FAIL_INDEX_V(binormal_pos + 2, binormal_src->array.size(), ERR_INVALID_DATA); |
| 785 | Vector3 binormal = Vector3(binormal_src->array[binormal_pos + 0], binormal_src->array[binormal_pos + 1], binormal_src->array[binormal_pos + 2]); |
| 786 | |
| 787 | int tangent_pos = (tangent_src->stride ? tangent_src->stride : 3) * p.indices[src + tangent_ofs]; |
| 788 | ERR_FAIL_INDEX_V(tangent_pos + 0, tangent_src->array.size(), ERR_INVALID_DATA); |
| 789 | ERR_FAIL_INDEX_V(tangent_pos + 2, tangent_src->array.size(), ERR_INVALID_DATA); |
| 790 | Vector3 tangent = Vector3(tangent_src->array[tangent_pos + 0], tangent_src->array[tangent_pos + 1], tangent_src->array[tangent_pos + 2]); |
| 791 | |
| 792 | vertex.tangent.normal = tangent; |
| 793 | vertex.tangent.d = vertex.normal.cross(tangent).dot(binormal) > 0 ? 1 : -1; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | if (uv_src) { |
| 798 | int uv_pos = (uv_src->stride ? uv_src->stride : 2) * p.indices[src + uv_ofs]; |
| 799 | ERR_FAIL_INDEX_V(uv_pos + 0, uv_src->array.size(), ERR_INVALID_DATA); |
| 800 | ERR_FAIL_INDEX_V(uv_pos + 1, uv_src->array.size(), ERR_INVALID_DATA); |
| 801 | vertex.uv = Vector3(uv_src->array[uv_pos + 0], 1.0 - uv_src->array[uv_pos + 1], 0); |
| 802 | } |
| 803 | |
| 804 | if (uv2_src) { |
| 805 | int uv2_pos = (uv2_src->stride ? uv2_src->stride : 2) * p.indices[src + uv2_ofs]; |
| 806 | ERR_FAIL_INDEX_V(uv2_pos + 0, uv2_src->array.size(), ERR_INVALID_DATA); |
| 807 | ERR_FAIL_INDEX_V(uv2_pos + 1, uv2_src->array.size(), ERR_INVALID_DATA); |
| 808 | vertex.uv2 = Vector3(uv2_src->array[uv2_pos + 0], 1.0 - uv2_src->array[uv2_pos + 1], 0); |
| 809 | } |
| 810 | |
| 811 | if (color_src) { |
| 812 | int color_pos = (color_src->stride ? color_src->stride : 3) * p.indices[src + color_ofs]; // colors are RGB in collada.. |
| 813 | ERR_FAIL_INDEX_V(color_pos + 0, color_src->array.size(), ERR_INVALID_DATA); |
| 814 | ERR_FAIL_INDEX_V(color_pos + ((color_src->stride > 3) ? 3 : 2), color_src->array.size(), ERR_INVALID_DATA); |
| 815 | 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); |
| 816 | } |
| 817 | |
| 818 | #ifndef NO_UP_AXIS_SWAP |
| 819 | if (collada.state.up_axis == Vector3::AXIS_Z) { |
| 820 | Vector3 bn = vertex.normal.cross(vertex.tangent.normal) * vertex.tangent.d; |
| 821 | |
| 822 | SWAP(vertex.vertex.z, vertex.vertex.y); |
| 823 | vertex.vertex.z = -vertex.vertex.z; |
| 824 | SWAP(vertex.normal.z, vertex.normal.y); |
| 825 | vertex.normal.z = -vertex.normal.z; |
| 826 | SWAP(vertex.tangent.normal.z, vertex.tangent.normal.y); |
| 827 | vertex.tangent.normal.z = -vertex.tangent.normal.z; |
| 828 | SWAP(bn.z, bn.y); |
| 829 | bn.z = -bn.z; |
| 830 | |
| 831 | vertex.tangent.d = vertex.normal.cross(vertex.tangent.normal).dot(bn) > 0 ? 1 : -1; |
| 832 | } |
| 833 | |
| 834 | #endif |
| 835 | |
| 836 | vertex.fix_unit_scale(collada); |
| 837 | int index = 0; |
| 838 | //COLLADA_PRINT("vertex: "+vertex.vertex); |
| 839 | |
| 840 | if (vertex_set.has(vertex)) { |
| 841 | index = vertex_set.find(vertex)->get().idx; |
| 842 | } else { |
| 843 | index = vertex_set.size(); |
| 844 | vertex.idx = index; |
| 845 | vertex_set.insert(vertex); |
| 846 | } |
| 847 | |
| 848 | //build triangles if needed |
| 849 | if (j == 0) { |
| 850 | prev2[0] = index; |
| 851 | } |
| 852 | |
| 853 | if (j >= 2) { |
| 854 | //insert indices in reverse order (collada uses CCW as frontface) |
| 855 | if (local_xform_mirror) { |
| 856 | indices_list.push_back(prev2[0]); |
| 857 | indices_list.push_back(prev2[1]); |
| 858 | indices_list.push_back(index); |
| 859 | |
| 860 | } else { |
| 861 | indices_list.push_back(prev2[0]); |
| 862 | indices_list.push_back(index); |
| 863 | indices_list.push_back(prev2[1]); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | prev2[1] = index; |
| 868 | _prim_ofs += p.vertex_size; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | Vector<Collada::Vertex> vertex_array; //there we go, vertex array |
| 873 | |
| 874 | vertex_array.resize(vertex_set.size()); |
| 875 | for (const Collada::Vertex &F : vertex_set) { |
| 876 | vertex_array.write[F.idx] = F; |
| 877 | } |
| 878 | |
| 879 | if (has_weights) { |
| 880 | //if skeleton, localize |
| 881 | Transform3D local_xform = p_local_xform; |
| 882 | for (int i = 0; i < vertex_array.size(); i++) { |
| 883 | vertex_array.write[i].vertex = local_xform.xform(vertex_array[i].vertex); |
| 884 | vertex_array.write[i].normal = local_xform.basis.xform(vertex_array[i].normal).normalized(); |
| 885 | vertex_array.write[i].tangent.normal = local_xform.basis.xform(vertex_array[i].tangent.normal).normalized(); |
| 886 | if (local_xform_mirror) { |
| 887 | //i shouldn't do this? wtf? |
| 888 | //vertex_array[i].normal*=-1.0; |
| 889 | //vertex_array[i].tangent.normal*=-1.0; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | /*****************/ |
| 895 | /* MAKE SURFACES */ |
| 896 | /*****************/ |
| 897 | |
| 898 | { |
| 899 | Ref<StandardMaterial3D> material; |
| 900 | |
| 901 | { |
| 902 | if (p_material_map.has(p.material)) { |
| 903 | String target = p_material_map[p.material].target; |
| 904 | |
| 905 | if (!material_cache.has(target)) { |
| 906 | Error err = _create_material(target); |
| 907 | if (!err) { |
| 908 | material = material_cache[target]; |
| 909 | } |
| 910 | } else { |
| 911 | material = material_cache[target]; |
| 912 | } |
| 913 | |
| 914 | } else if (!p.material.is_empty()) { |
| 915 | WARN_PRINT("Collada: Unreferenced material in geometry instance: " + p.material); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | uint64_t mesh_flags = 0; |
| 920 | |
| 921 | if (p_use_compression) { |
| 922 | mesh_flags = RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES; |
| 923 | } |
| 924 | |
| 925 | // We can't generate tangents without UVs, so create dummy tangents. |
| 926 | bool generate_dummy_tangents = (!binormal_src || !tangent_src) && !uv_src && force_make_tangents; |
| 927 | |
| 928 | Ref<SurfaceTool> surftool; |
| 929 | surftool.instantiate(); |
| 930 | surftool->begin(Mesh::PRIMITIVE_TRIANGLES); |
| 931 | |
| 932 | for (int k = 0; k < vertex_array.size(); k++) { |
| 933 | if (normal_src) { |
| 934 | surftool->set_normal(vertex_array[k].normal); |
| 935 | if (binormal_src && tangent_src) { |
| 936 | surftool->set_tangent(vertex_array[k].tangent); |
| 937 | } else if (generate_dummy_tangents) { |
| 938 | Vector3 tan = Vector3(vertex_array[k].normal.z, -vertex_array[k].normal.x, vertex_array[k].normal.y).cross(vertex_array[k].normal.normalized()).normalized(); |
| 939 | surftool->set_tangent(Plane(tan.x, tan.y, tan.z, 1.0)); |
| 940 | } |
| 941 | } else { |
| 942 | // No normals, use a dummy tangent since normals will be generated. |
| 943 | if (generate_dummy_tangents) { |
| 944 | surftool->set_tangent(Plane(1.0, 0.0, 0.0, 1.0)); |
| 945 | } |
| 946 | } |
| 947 | if (uv_src) { |
| 948 | surftool->set_uv(Vector2(vertex_array[k].uv.x, vertex_array[k].uv.y)); |
| 949 | } |
| 950 | if (uv2_src) { |
| 951 | surftool->set_uv2(Vector2(vertex_array[k].uv2.x, vertex_array[k].uv2.y)); |
| 952 | } |
| 953 | if (color_src) { |
| 954 | surftool->set_color(vertex_array[k].color); |
| 955 | } |
| 956 | |
| 957 | if (has_weights) { |
| 958 | Vector<float> weights; |
| 959 | Vector<int> bones; |
| 960 | weights.resize(RS::ARRAY_WEIGHTS_SIZE); |
| 961 | bones.resize(RS::ARRAY_WEIGHTS_SIZE); |
| 962 | //float sum=0.0; |
| 963 | for (int l = 0; l < RS::ARRAY_WEIGHTS_SIZE; l++) { |
| 964 | if (l < vertex_array[k].weights.size()) { |
| 965 | weights.write[l] = vertex_array[k].weights[l].weight; |
| 966 | bones.write[l] = vertex_array[k].weights[l].bone_idx; |
| 967 | //sum += vertex_array[k].weights[l].weight; |
| 968 | } else { |
| 969 | weights.write[l] = 0; |
| 970 | bones.write[l] = 0; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | surftool->set_bones(bones); |
| 975 | surftool->set_weights(weights); |
| 976 | } |
| 977 | |
| 978 | surftool->add_vertex(vertex_array[k].vertex); |
| 979 | } |
| 980 | |
| 981 | for (int &E : indices_list) { |
| 982 | surftool->add_index(E); |
| 983 | } |
| 984 | |
| 985 | if (!normal_src) { |
| 986 | // Should always have normals. |
| 987 | surftool->generate_normals(); |
| 988 | } |
| 989 | |
| 990 | bool generate_tangents = (!binormal_src || !tangent_src) && uv_src && force_make_tangents; |
| 991 | |
| 992 | if (generate_tangents) { |
| 993 | surftool->generate_tangents(); |
| 994 | } |
| 995 | |
| 996 | // Disable compression if all z equals 0 (the mesh is 2D). |
| 997 | bool is_mesh_2d = true; |
| 998 | for (int k = 0; k < vertex_array.size(); k++) { |
| 999 | if (!Math::is_zero_approx(vertex_array[k].vertex.z)) { |
| 1000 | is_mesh_2d = false; |
| 1001 | break; |
| 1002 | } |
| 1003 | }; |
| 1004 | |
| 1005 | if (p_mesh->get_blend_shape_count() != 0 || p_skin_controller || is_mesh_2d) { |
| 1006 | // Can't compress if attributes missing or if using vertex weights. |
| 1007 | mesh_flags &= ~RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES; |
| 1008 | } |
| 1009 | |
| 1010 | //////////////////////////// |
| 1011 | // FINALLY CREATE SUFRACE // |
| 1012 | //////////////////////////// |
| 1013 | |
| 1014 | Array d = surftool->commit_to_arrays(); |
| 1015 | d.resize(RS::ARRAY_MAX); |
| 1016 | |
| 1017 | if (mesh_flags & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES && (generate_dummy_tangents || generate_tangents)) { |
| 1018 | // Compression is enabled, so let's validate that the normals and tangents are correct. |
| 1019 | Vector<Vector3> normals = d[Mesh::ARRAY_NORMAL]; |
| 1020 | Vector<float> tangents = d[Mesh::ARRAY_TANGENT]; |
| 1021 | for (int vert = 0; vert < normals.size(); vert++) { |
| 1022 | Vector3 tan = Vector3(tangents[vert * 4 + 0], tangents[vert * 4 + 1], tangents[vert * 4 + 2]); |
| 1023 | if (std::abs(tan.dot(normals[vert])) > 0.0001) { |
| 1024 | // Tangent is not perpendicular to the normal, so we can't use compression. |
| 1025 | mesh_flags &= ~RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | Array mr; |
| 1031 | |
| 1032 | //////////////////////////// |
| 1033 | // THEN THE MORPH TARGETS // |
| 1034 | //////////////////////////// |
| 1035 | |
| 1036 | for (int mi = 0; mi < p_morph_meshes.size(); mi++) { |
| 1037 | Array a = p_morph_meshes[mi]->get_surface_arrays(surface); |
| 1038 | //add valid weight and bone arrays if they exist, TODO check if they are unique to shape (generally not) |
| 1039 | |
| 1040 | // Enforce blend shape mask array format |
| 1041 | for (int mj = 0; mj < Mesh::ARRAY_MAX; mj++) { |
| 1042 | if (!(Mesh::ARRAY_FORMAT_BLEND_SHAPE_MASK & (1ULL << mj))) { |
| 1043 | a[mj] = Variant(); |
| 1044 | } |
| 1045 | } |
| 1046 | mr.push_back(a); |
| 1047 | } |
| 1048 | |
| 1049 | String surface_name; |
| 1050 | Ref<Material> mat; |
| 1051 | if (material.is_valid()) { |
| 1052 | if (p_use_mesh_material) { |
| 1053 | mat = material; |
| 1054 | } |
| 1055 | surface_name = material->get_name(); |
| 1056 | } |
| 1057 | p_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, d, mr, Dictionary(), mat, surface_name, mesh_flags); |
| 1058 | } |
| 1059 | |
| 1060 | /*****************/ |
| 1061 | /* FIND MATERIAL */ |
| 1062 | /*****************/ |
| 1063 | |
| 1064 | surface++; |
| 1065 | } |
| 1066 | |
| 1067 | return OK; |
| 1068 | } |
| 1069 | |
| 1070 | Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compression) { |
| 1071 | if (p_node->type == Collada::Node::TYPE_GEOMETRY && node_map.has(p_node->id)) { |
| 1072 | Node3D *node = node_map[p_node->id].node; |
| 1073 | Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node); |
| 1074 | |
| 1075 | if (Object::cast_to<Path3D>(node)) { |
| 1076 | Path3D *path = Object::cast_to<Path3D>(node); |
| 1077 | |
| 1078 | if (curve_cache.has(ng->source)) { |
| 1079 | path->set_curve(curve_cache[ng->source]); |
| 1080 | } else { |
| 1081 | Ref<Curve3D> c = memnew(Curve3D); |
| 1082 | |
| 1083 | const Collada::CurveData &cd = collada.state.curve_data_map[ng->source]; |
| 1084 | |
| 1085 | ERR_FAIL_COND_V(!cd.control_vertices.has("POSITION"), ERR_INVALID_DATA); |
| 1086 | ERR_FAIL_COND_V(!cd.control_vertices.has("IN_TANGENT"), ERR_INVALID_DATA); |
| 1087 | ERR_FAIL_COND_V(!cd.control_vertices.has("OUT_TANGENT"), ERR_INVALID_DATA); |
| 1088 | ERR_FAIL_COND_V(!cd.control_vertices.has("INTERPOLATION"), ERR_INVALID_DATA); |
| 1089 | |
| 1090 | ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["POSITION"]), ERR_INVALID_DATA); |
| 1091 | const Collada::CurveData::Source &vertices = cd.sources[cd.control_vertices["POSITION"]]; |
| 1092 | ERR_FAIL_COND_V(vertices.stride != 3, ERR_INVALID_DATA); |
| 1093 | |
| 1094 | ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["IN_TANGENT"]), ERR_INVALID_DATA); |
| 1095 | const Collada::CurveData::Source &in_tangents = cd.sources[cd.control_vertices["IN_TANGENT"]]; |
| 1096 | ERR_FAIL_COND_V(in_tangents.stride != 3, ERR_INVALID_DATA); |
| 1097 | |
| 1098 | ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["OUT_TANGENT"]), ERR_INVALID_DATA); |
| 1099 | const Collada::CurveData::Source &out_tangents = cd.sources[cd.control_vertices["OUT_TANGENT"]]; |
| 1100 | ERR_FAIL_COND_V(out_tangents.stride != 3, ERR_INVALID_DATA); |
| 1101 | |
| 1102 | ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["INTERPOLATION"]), ERR_INVALID_DATA); |
| 1103 | const Collada::CurveData::Source &interps = cd.sources[cd.control_vertices["INTERPOLATION"]]; |
| 1104 | ERR_FAIL_COND_V(interps.stride != 1, ERR_INVALID_DATA); |
| 1105 | |
| 1106 | const Collada::CurveData::Source *tilts = nullptr; |
| 1107 | if (cd.control_vertices.has("TILT") && cd.sources.has(cd.control_vertices["TILT"])) { |
| 1108 | tilts = &cd.sources[cd.control_vertices["TILT"]]; |
| 1109 | } |
| 1110 | |
| 1111 | int pc = vertices.array.size() / 3; |
| 1112 | for (int i = 0; i < pc; i++) { |
| 1113 | Vector3 pos(vertices.array[i * 3 + 0], vertices.array[i * 3 + 1], vertices.array[i * 3 + 2]); |
| 1114 | Vector3 in(in_tangents.array[i * 3 + 0], in_tangents.array[i * 3 + 1], in_tangents.array[i * 3 + 2]); |
| 1115 | Vector3 out(out_tangents.array[i * 3 + 0], out_tangents.array[i * 3 + 1], out_tangents.array[i * 3 + 2]); |
| 1116 | |
| 1117 | #ifndef NO_UP_AXIS_SWAP |
| 1118 | if (collada.state.up_axis == Vector3::AXIS_Z) { |
| 1119 | SWAP(pos.y, pos.z); |
| 1120 | pos.z = -pos.z; |
| 1121 | SWAP(in.y, in.z); |
| 1122 | in.z = -in.z; |
| 1123 | SWAP(out.y, out.z); |
| 1124 | out.z = -out.z; |
| 1125 | } |
| 1126 | #endif |
| 1127 | pos *= collada.state.unit_scale; |
| 1128 | in *= collada.state.unit_scale; |
| 1129 | out *= collada.state.unit_scale; |
| 1130 | |
| 1131 | c->add_point(pos, in - pos, out - pos); |
| 1132 | if (tilts) { |
| 1133 | c->set_point_tilt(i, tilts->array[i]); |
| 1134 | } |
| 1135 | } |
| 1136 | if (cd.closed && pc > 1) { |
| 1137 | Vector3 pos = c->get_point_position(0); |
| 1138 | Vector3 in = c->get_point_in(0); |
| 1139 | Vector3 out = c->get_point_out(0); |
| 1140 | c->add_point(pos, in, out, -1); |
| 1141 | } |
| 1142 | |
| 1143 | curve_cache[ng->source] = c; |
| 1144 | path->set_curve(c); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | if (Object::cast_to<ImporterMeshInstance3D>(node)) { |
| 1149 | Collada::NodeGeometry *ng2 = static_cast<Collada::NodeGeometry *>(p_node); |
| 1150 | |
| 1151 | ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(node); |
| 1152 | |
| 1153 | ERR_FAIL_NULL_V(mi, ERR_BUG); |
| 1154 | |
| 1155 | Collada::SkinControllerData *skin = nullptr; |
| 1156 | Collada::MorphControllerData *morph = nullptr; |
| 1157 | String meshid; |
| 1158 | Transform3D apply_xform; |
| 1159 | Vector<int> bone_remap; |
| 1160 | Vector<Ref<ImporterMesh>> morphs; |
| 1161 | |
| 1162 | if (ng2->controller) { |
| 1163 | String ngsource = ng2->source; |
| 1164 | |
| 1165 | if (collada.state.skin_controller_data_map.has(ngsource)) { |
| 1166 | ERR_FAIL_COND_V(!collada.state.skin_controller_data_map.has(ngsource), ERR_INVALID_DATA); |
| 1167 | skin = &collada.state.skin_controller_data_map[ngsource]; |
| 1168 | |
| 1169 | Vector<String> skeletons = ng2->skeletons; |
| 1170 | |
| 1171 | ERR_FAIL_COND_V(skeletons.is_empty(), ERR_INVALID_DATA); |
| 1172 | |
| 1173 | const String &skname = skeletons[0]; |
| 1174 | ERR_FAIL_COND_V(!node_map.has(skname), ERR_INVALID_DATA); |
| 1175 | NodeMap nmsk = node_map[skname]; |
| 1176 | Skeleton3D *sk = Object::cast_to<Skeleton3D>(nmsk.node); |
| 1177 | ERR_FAIL_NULL_V(sk, ERR_INVALID_DATA); |
| 1178 | ERR_FAIL_COND_V(!skeleton_bone_map.has(sk), ERR_INVALID_DATA); |
| 1179 | HashMap<String, int> &bone_remap_map = skeleton_bone_map[sk]; |
| 1180 | |
| 1181 | meshid = skin->base; |
| 1182 | |
| 1183 | if (collada.state.morph_controller_data_map.has(meshid)) { |
| 1184 | //it's a morph!! |
| 1185 | morph = &collada.state.morph_controller_data_map[meshid]; |
| 1186 | ngsource = meshid; |
| 1187 | meshid = morph->mesh; |
| 1188 | } else { |
| 1189 | ngsource = ""; |
| 1190 | } |
| 1191 | |
| 1192 | if (apply_mesh_xform_to_vertices) { |
| 1193 | apply_xform = collada.fix_transform(p_node->default_transform); |
| 1194 | node->set_transform(Transform3D()); |
| 1195 | } else { |
| 1196 | apply_xform = Transform3D(); |
| 1197 | } |
| 1198 | |
| 1199 | ERR_FAIL_COND_V(!skin->weights.sources.has("JOINT"), ERR_INVALID_DATA); |
| 1200 | |
| 1201 | String joint_id = skin->weights.sources["JOINT"].source; |
| 1202 | ERR_FAIL_COND_V(!skin->sources.has(joint_id), ERR_INVALID_DATA); |
| 1203 | |
| 1204 | Collada::SkinControllerData::Source *joint_src = &skin->sources[joint_id]; |
| 1205 | |
| 1206 | bone_remap.resize(joint_src->sarray.size()); |
| 1207 | |
| 1208 | for (int i = 0; i < bone_remap.size(); i++) { |
| 1209 | String str = joint_src->sarray[i]; |
| 1210 | ERR_FAIL_COND_V(!bone_remap_map.has(str), ERR_INVALID_DATA); |
| 1211 | bone_remap.write[i] = bone_remap_map[str]; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if (collada.state.morph_controller_data_map.has(ngsource)) { |
| 1216 | //it's a morph!! |
| 1217 | morph = &collada.state.morph_controller_data_map[ngsource]; |
| 1218 | meshid = morph->mesh; |
| 1219 | |
| 1220 | if (morph->targets.has("MORPH_TARGET")) { |
| 1221 | String target = morph->targets["MORPH_TARGET"]; |
| 1222 | bool valid = false; |
| 1223 | if (morph->sources.has(target)) { |
| 1224 | valid = true; |
| 1225 | Vector<String> names = morph->sources[target].sarray; |
| 1226 | for (int i = 0; i < names.size(); i++) { |
| 1227 | const String &meshid2 = names[i]; |
| 1228 | if (collada.state.mesh_data_map.has(meshid2)) { |
| 1229 | Ref<ImporterMesh> mesh = Ref<ImporterMesh>(memnew(ImporterMesh)); |
| 1230 | const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2]; |
| 1231 | mesh->set_name(meshdata.name); |
| 1232 | Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, nullptr, Vector<Ref<ImporterMesh>>(), false); |
| 1233 | ERR_FAIL_COND_V(err, err); |
| 1234 | |
| 1235 | morphs.push_back(mesh); |
| 1236 | } else { |
| 1237 | valid = false; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | if (!valid) { |
| 1243 | morphs.clear(); |
| 1244 | } |
| 1245 | ngsource = ""; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | ERR_FAIL_COND_V_MSG(!ngsource.is_empty(), ERR_INVALID_DATA, "Controller instance source '" + ngsource + "' is neither skin or morph!"); |
| 1250 | |
| 1251 | } else { |
| 1252 | meshid = ng2->source; |
| 1253 | } |
| 1254 | |
| 1255 | Ref<ImporterMesh> mesh; |
| 1256 | if (mesh_cache.has(meshid)) { |
| 1257 | mesh = mesh_cache[meshid]; |
| 1258 | } else { |
| 1259 | if (collada.state.mesh_data_map.has(meshid)) { |
| 1260 | //bleh, must ignore invalid |
| 1261 | |
| 1262 | ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA); |
| 1263 | mesh.instantiate(); |
| 1264 | const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; |
| 1265 | String name = meshdata.name; |
| 1266 | if (name.is_empty()) { |
| 1267 | name = "Mesh"; |
| 1268 | } |
| 1269 | int counter = 2; |
| 1270 | while (mesh_unique_names.has(name)) { |
| 1271 | name = meshdata.name; |
| 1272 | if (name.is_empty()) { |
| 1273 | name = "Mesh"; |
| 1274 | } |
| 1275 | name += itos(counter++); |
| 1276 | } |
| 1277 | |
| 1278 | mesh_unique_names.insert(name); |
| 1279 | |
| 1280 | mesh->set_name(name); |
| 1281 | Error err = _create_mesh_surfaces(morphs.is_empty(), mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, p_use_compression, use_mesh_builtin_materials); |
| 1282 | ERR_FAIL_COND_V_MSG(err, err, "Cannot create mesh surface."); |
| 1283 | |
| 1284 | mesh_cache[meshid] = mesh; |
| 1285 | } else { |
| 1286 | WARN_PRINT("Collada: Will not import geometry: " + meshid); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | if (mesh.is_valid()) { |
| 1291 | mi->set_mesh(mesh); |
| 1292 | if (!use_mesh_builtin_materials) { |
| 1293 | const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid]; |
| 1294 | |
| 1295 | for (int i = 0; i < meshdata.primitives.size(); i++) { |
| 1296 | String matname = meshdata.primitives[i].material; |
| 1297 | |
| 1298 | if (ng2->material_map.has(matname)) { |
| 1299 | String target = ng2->material_map[matname].target; |
| 1300 | |
| 1301 | Ref<Material> material; |
| 1302 | if (!material_cache.has(target)) { |
| 1303 | Error err = _create_material(target); |
| 1304 | if (!err) { |
| 1305 | material = material_cache[target]; |
| 1306 | } |
| 1307 | } else { |
| 1308 | material = material_cache[target]; |
| 1309 | } |
| 1310 | |
| 1311 | mi->set_surface_material(i, material); |
| 1312 | } else if (!matname.is_empty()) { |
| 1313 | WARN_PRINT("Collada: Unreferenced material in geometry instance: " + matname); |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | for (int i = 0; i < p_node->children.size(); i++) { |
| 1322 | Error err = _create_resources(p_node->children[i], p_use_compression); |
| 1323 | if (err) { |
| 1324 | return err; |
| 1325 | } |
| 1326 | } |
| 1327 | return OK; |
| 1328 | } |
| 1329 | |
| 1330 | Error ColladaImport::load(const String &p_path, int p_flags, bool p_force_make_tangents, bool p_use_compression) { |
| 1331 | Error err = collada.load(p_path, p_flags); |
| 1332 | ERR_FAIL_COND_V_MSG(err, err, "Cannot load file '" + p_path + "'."); |
| 1333 | |
| 1334 | force_make_tangents = p_force_make_tangents; |
| 1335 | ERR_FAIL_COND_V(!collada.state.visual_scene_map.has(collada.state.root_visual_scene), ERR_INVALID_DATA); |
| 1336 | Collada::VisualScene &vs = collada.state.visual_scene_map[collada.state.root_visual_scene]; |
| 1337 | |
| 1338 | scene = memnew(Node3D); // root |
| 1339 | |
| 1340 | //determine what's going on with the lights |
| 1341 | for (int i = 0; i < vs.root_nodes.size(); i++) { |
| 1342 | _pre_process_lights(vs.root_nodes[i]); |
| 1343 | } |
| 1344 | //import scene |
| 1345 | |
| 1346 | for (int i = 0; i < vs.root_nodes.size(); i++) { |
| 1347 | Error err2 = _create_scene_skeletons(vs.root_nodes[i]); |
| 1348 | if (err2 != OK) { |
| 1349 | memdelete(scene); |
| 1350 | ERR_FAIL_COND_V(err2, err2); |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | for (int i = 0; i < vs.root_nodes.size(); i++) { |
| 1355 | Error err2 = _create_scene(vs.root_nodes[i], scene); |
| 1356 | if (err2 != OK) { |
| 1357 | memdelete(scene); |
| 1358 | ERR_FAIL_COND_V(err2, err2); |
| 1359 | } |
| 1360 | |
| 1361 | Error err3 = _create_resources(vs.root_nodes[i], p_use_compression); |
| 1362 | if (err3 != OK) { |
| 1363 | memdelete(scene); |
| 1364 | ERR_FAIL_COND_V(err3, err3); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | //optatively, set unit scale in the root |
| 1369 | scene->set_transform(collada.get_root_transform()); |
| 1370 | |
| 1371 | return OK; |
| 1372 | } |
| 1373 | |
| 1374 | void ColladaImport::_fix_param_animation_tracks() { |
| 1375 | for (KeyValue<String, Collada::Node *> &E : collada.state.scene_map) { |
| 1376 | Collada::Node *n = E.value; |
| 1377 | switch (n->type) { |
| 1378 | case Collada::Node::TYPE_NODE: { |
| 1379 | // ? do nothing |
| 1380 | } break; |
| 1381 | case Collada::Node::TYPE_JOINT: { |
| 1382 | } break; |
| 1383 | case Collada::Node::TYPE_SKELETON: { |
| 1384 | } break; |
| 1385 | case Collada::Node::TYPE_LIGHT: { |
| 1386 | } break; |
| 1387 | case Collada::Node::TYPE_CAMERA: { |
| 1388 | } break; |
| 1389 | case Collada::Node::TYPE_GEOMETRY: { |
| 1390 | Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(n); |
| 1391 | // test source(s) |
| 1392 | String source = ng->source; |
| 1393 | |
| 1394 | while (!source.is_empty()) { |
| 1395 | if (collada.state.skin_controller_data_map.has(source)) { |
| 1396 | const Collada::SkinControllerData &skin = collada.state.skin_controller_data_map[source]; |
| 1397 | |
| 1398 | //nothing to animate here i think |
| 1399 | |
| 1400 | source = skin.base; |
| 1401 | } else if (collada.state.morph_controller_data_map.has(source)) { |
| 1402 | const Collada::MorphControllerData &morph = collada.state.morph_controller_data_map[source]; |
| 1403 | |
| 1404 | if (morph.targets.has("MORPH_WEIGHT") && morph.targets.has("MORPH_TARGET")) { |
| 1405 | String weights = morph.targets["MORPH_WEIGHT"]; |
| 1406 | String targets = morph.targets["MORPH_TARGET"]; |
| 1407 | //fails here |
| 1408 | |
| 1409 | if (morph.sources.has(targets) && morph.sources.has(weights)) { |
| 1410 | const Collada::MorphControllerData::Source &weight_src = morph.sources[weights]; |
| 1411 | const Collada::MorphControllerData::Source &target_src = morph.sources[targets]; |
| 1412 | |
| 1413 | ERR_FAIL_COND(weight_src.array.size() != target_src.sarray.size()); |
| 1414 | |
| 1415 | for (int i = 0; i < weight_src.array.size(); i++) { |
| 1416 | String track_name = weights + "(" + itos(i) + ")"; |
| 1417 | String mesh_name = target_src.sarray[i]; |
| 1418 | if (collada.state.mesh_name_map.has(mesh_name) && collada.state.referenced_tracks.has(track_name)) { |
| 1419 | const Vector<int> &rt = collada.state.referenced_tracks[track_name]; |
| 1420 | |
| 1421 | for (int rti = 0; rti < rt.size(); rti++) { |
| 1422 | Collada::AnimationTrack *at = &collada.state.animation_tracks.write[rt[rti]]; |
| 1423 | |
| 1424 | at->target = E.key; |
| 1425 | at->param = "morph/" + collada.state.mesh_name_map[mesh_name]; |
| 1426 | at->property = true; |
| 1427 | //at->param |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | source = morph.mesh; |
| 1434 | } else { |
| 1435 | source = ""; // for now nothing else supported |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | } break; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | void ColladaImport::create_animations(bool p_import_value_tracks) { |
| 1445 | _fix_param_animation_tracks(); |
| 1446 | for (int i = 0; i < collada.state.animation_clips.size(); i++) { |
| 1447 | for (int j = 0; j < collada.state.animation_clips[i].tracks.size(); j++) { |
| 1448 | tracks_in_clips.insert(collada.state.animation_clips[i].tracks[j]); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | for (int i = 0; i < collada.state.animation_tracks.size(); i++) { |
| 1453 | const Collada::AnimationTrack &at = collada.state.animation_tracks[i]; |
| 1454 | |
| 1455 | String node; |
| 1456 | |
| 1457 | if (!node_map.has(at.target)) { |
| 1458 | if (node_name_map.has(at.target)) { |
| 1459 | node = node_name_map[at.target]; |
| 1460 | } else { |
| 1461 | WARN_PRINT("Collada: Couldn't find node: " + at.target); |
| 1462 | continue; |
| 1463 | } |
| 1464 | } else { |
| 1465 | node = at.target; |
| 1466 | } |
| 1467 | |
| 1468 | if (at.property) { |
| 1469 | valid_animated_properties.push_back(i); |
| 1470 | |
| 1471 | } else { |
| 1472 | node_map[node].anim_tracks.push_back(i); |
| 1473 | valid_animated_nodes.insert(node); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | create_animation(-1, p_import_value_tracks); |
| 1478 | for (int i = 0; i < collada.state.animation_clips.size(); i++) { |
| 1479 | create_animation(i, p_import_value_tracks); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | void ColladaImport::create_animation(int p_clip, bool p_import_value_tracks) { |
| 1484 | Ref<Animation> animation = Ref<Animation>(memnew(Animation)); |
| 1485 | |
| 1486 | if (p_clip == -1) { |
| 1487 | animation->set_name("default"); |
| 1488 | } else { |
| 1489 | animation->set_name(collada.state.animation_clips[p_clip].name); |
| 1490 | } |
| 1491 | |
| 1492 | for (const KeyValue<String, NodeMap> &E : node_map) { |
| 1493 | if (E.value.bone < 0) { |
| 1494 | continue; |
| 1495 | } |
| 1496 | bones_with_animation[E.key] = false; |
| 1497 | } |
| 1498 | //store and validate tracks |
| 1499 | |
| 1500 | if (p_clip == -1) { |
| 1501 | //main anim |
| 1502 | } |
| 1503 | |
| 1504 | HashSet<int> track_filter; |
| 1505 | |
| 1506 | if (p_clip == -1) { |
| 1507 | for (int i = 0; i < collada.state.animation_clips.size(); i++) { |
| 1508 | int tc = collada.state.animation_clips[i].tracks.size(); |
| 1509 | for (int j = 0; j < tc; j++) { |
| 1510 | String n = collada.state.animation_clips[i].tracks[j]; |
| 1511 | if (collada.state.by_id_tracks.has(n)) { |
| 1512 | const Vector<int> &ti = collada.state.by_id_tracks[n]; |
| 1513 | for (int k = 0; k < ti.size(); k++) { |
| 1514 | track_filter.insert(ti[k]); |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | } else { |
| 1520 | int tc = collada.state.animation_clips[p_clip].tracks.size(); |
| 1521 | for (int j = 0; j < tc; j++) { |
| 1522 | String n = collada.state.animation_clips[p_clip].tracks[j]; |
| 1523 | if (collada.state.by_id_tracks.has(n)) { |
| 1524 | const Vector<int> &ti = collada.state.by_id_tracks[n]; |
| 1525 | for (int k = 0; k < ti.size(); k++) { |
| 1526 | track_filter.insert(ti[k]); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | //animation->set_loop(true); |
| 1533 | //create animation tracks |
| 1534 | |
| 1535 | Vector<real_t> base_snapshots; |
| 1536 | |
| 1537 | float f = 0; |
| 1538 | float snapshot_interval = 1.0 / bake_fps; //should be customizable somewhere... |
| 1539 | |
| 1540 | float anim_length = collada.state.animation_length; |
| 1541 | if (p_clip >= 0 && collada.state.animation_clips[p_clip].end) { |
| 1542 | anim_length = collada.state.animation_clips[p_clip].end; |
| 1543 | } |
| 1544 | |
| 1545 | while (f < anim_length) { |
| 1546 | base_snapshots.push_back(f); |
| 1547 | |
| 1548 | f += snapshot_interval; |
| 1549 | |
| 1550 | if (f >= anim_length) { |
| 1551 | base_snapshots.push_back(anim_length); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | animation->set_length(anim_length); |
| 1556 | animation->set_step(snapshot_interval); |
| 1557 | |
| 1558 | bool tracks_found = false; |
| 1559 | |
| 1560 | for (const String &E : valid_animated_nodes) { |
| 1561 | // take snapshots |
| 1562 | |
| 1563 | if (!collada.state.scene_map.has(E)) { |
| 1564 | continue; |
| 1565 | } |
| 1566 | |
| 1567 | NodeMap &nm = node_map[E]; |
| 1568 | String path = String(scene->get_path_to(nm.node)); |
| 1569 | |
| 1570 | if (nm.bone >= 0) { |
| 1571 | Skeleton3D *sk = static_cast<Skeleton3D *>(nm.node); |
| 1572 | String name = sk->get_bone_name(nm.bone); |
| 1573 | path = path + ":" + name; |
| 1574 | } |
| 1575 | |
| 1576 | bool found_anim = false; |
| 1577 | |
| 1578 | Collada::Node *cn = collada.state.scene_map[E]; |
| 1579 | if (cn->ignore_anim) { |
| 1580 | continue; |
| 1581 | } |
| 1582 | |
| 1583 | bool has_position = false; |
| 1584 | bool has_rotation = false; |
| 1585 | bool has_scale = false; |
| 1586 | |
| 1587 | for (int i = 0; i < cn->xform_list.size(); i++) { |
| 1588 | switch (cn->xform_list[i].op) { |
| 1589 | case Collada::Node::XForm::OP_ROTATE: { |
| 1590 | has_rotation = true; |
| 1591 | } break; |
| 1592 | case Collada::Node::XForm::OP_SCALE: { |
| 1593 | has_scale = true; |
| 1594 | } break; |
| 1595 | case Collada::Node::XForm::OP_TRANSLATE: { |
| 1596 | has_position = true; |
| 1597 | } break; |
| 1598 | case Collada::Node::XForm::OP_MATRIX: { |
| 1599 | has_position = true; |
| 1600 | has_rotation = true; |
| 1601 | has_scale = true; |
| 1602 | } break; |
| 1603 | case Collada::Node::XForm::OP_VISIBILITY: { |
| 1604 | } break; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | int base_track = animation->get_track_count(); |
| 1609 | int position_idx = -1; |
| 1610 | if (has_position) { |
| 1611 | position_idx = animation->get_track_count(); |
| 1612 | animation->add_track(Animation::TYPE_POSITION_3D); |
| 1613 | animation->track_set_path(position_idx, path); |
| 1614 | animation->track_set_imported(position_idx, true); //helps merging later |
| 1615 | } |
| 1616 | |
| 1617 | int rotation_idx = -1; |
| 1618 | if (has_rotation) { |
| 1619 | rotation_idx = animation->get_track_count(); |
| 1620 | animation->add_track(Animation::TYPE_ROTATION_3D); |
| 1621 | animation->track_set_path(rotation_idx, path); |
| 1622 | animation->track_set_imported(rotation_idx, true); //helps merging later |
| 1623 | } |
| 1624 | |
| 1625 | int scale_idx = -1; |
| 1626 | if (has_scale) { |
| 1627 | scale_idx = animation->get_track_count(); |
| 1628 | animation->add_track(Animation::TYPE_SCALE_3D); |
| 1629 | animation->track_set_path(scale_idx, path); |
| 1630 | animation->track_set_imported(scale_idx, true); //helps merging later |
| 1631 | } |
| 1632 | |
| 1633 | Vector<real_t> snapshots = base_snapshots; |
| 1634 | |
| 1635 | if (nm.anim_tracks.size() == 1) { |
| 1636 | //use snapshot keys from anim track instead, because this was most likely exported baked |
| 1637 | const Collada::AnimationTrack &at = collada.state.animation_tracks[nm.anim_tracks.front()->get()]; |
| 1638 | snapshots.clear(); |
| 1639 | for (int i = 0; i < at.keys.size(); i++) { |
| 1640 | snapshots.push_back(at.keys[i].time); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | for (int i = 0; i < snapshots.size(); i++) { |
| 1645 | for (const int track_id : nm.anim_tracks) { |
| 1646 | //apply tracks |
| 1647 | |
| 1648 | if (p_clip == -1) { |
| 1649 | if (track_filter.has(track_id)) { |
| 1650 | continue; |
| 1651 | } |
| 1652 | } else { |
| 1653 | if (!track_filter.has(track_id)) { |
| 1654 | continue; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | found_anim = true; |
| 1659 | |
| 1660 | const Collada::AnimationTrack &at = collada.state.animation_tracks[track_id]; |
| 1661 | |
| 1662 | int xform_idx = -1; |
| 1663 | for (int j = 0; j < cn->xform_list.size(); j++) { |
| 1664 | if (cn->xform_list[j].id == at.param) { |
| 1665 | xform_idx = j; |
| 1666 | break; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | if (xform_idx == -1) { |
| 1671 | WARN_PRINT("Collada: Couldn't find matching node " + at.target + " xform for track " + at.param + "."); |
| 1672 | continue; |
| 1673 | } |
| 1674 | |
| 1675 | Vector<float> data = at.get_value_at_time(snapshots[i]); |
| 1676 | ERR_CONTINUE(data.is_empty()); |
| 1677 | |
| 1678 | Collada::Node::XForm &xf = cn->xform_list.write[xform_idx]; |
| 1679 | |
| 1680 | if (at.component == "ANGLE") { |
| 1681 | ERR_CONTINUE(data.size() != 1); |
| 1682 | ERR_CONTINUE(xf.op != Collada::Node::XForm::OP_ROTATE); |
| 1683 | ERR_CONTINUE(xf.data.size() < 4); |
| 1684 | xf.data.write[3] = data[0]; |
| 1685 | } else if (at.component == "X" || at.component == "Y" || at.component == "Z") { |
| 1686 | int cn2 = at.component[0] - 'X'; |
| 1687 | ERR_CONTINUE(cn2 >= xf.data.size()); |
| 1688 | ERR_CONTINUE(data.size() > 1); |
| 1689 | xf.data.write[cn2] = data[0]; |
| 1690 | } else if (data.size() == xf.data.size()) { |
| 1691 | xf.data = data; |
| 1692 | } else { |
| 1693 | ERR_CONTINUE_MSG(data.size() != xf.data.size(), "Component " + at.component + " has datasize " + itos(data.size()) + ", xfdatasize " + itos(xf.data.size()) + "."); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | Transform3D xform = cn->compute_transform(collada); |
| 1698 | xform = collada.fix_transform(xform) * cn->post_transform; |
| 1699 | |
| 1700 | Vector3 s = xform.basis.get_scale(); |
| 1701 | bool singular_matrix = Math::is_zero_approx(s.x) || Math::is_zero_approx(s.y) || Math::is_zero_approx(s.z); |
| 1702 | Quaternion q = singular_matrix ? Quaternion() : xform.basis.get_rotation_quaternion(); |
| 1703 | Vector3 l = xform.origin; |
| 1704 | |
| 1705 | if (position_idx >= 0) { |
| 1706 | animation->position_track_insert_key(position_idx, snapshots[i], l); |
| 1707 | } |
| 1708 | if (rotation_idx >= 0) { |
| 1709 | animation->rotation_track_insert_key(rotation_idx, snapshots[i], q); |
| 1710 | } |
| 1711 | if (scale_idx >= 0) { |
| 1712 | animation->scale_track_insert_key(scale_idx, snapshots[i], s); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | if (nm.bone >= 0) { |
| 1717 | if (found_anim) { |
| 1718 | bones_with_animation[E] = true; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | if (found_anim) { |
| 1723 | tracks_found = true; |
| 1724 | } else { |
| 1725 | if (position_idx >= 0) { |
| 1726 | animation->remove_track(base_track); |
| 1727 | } |
| 1728 | if (rotation_idx >= 0) { |
| 1729 | animation->remove_track(base_track); |
| 1730 | } |
| 1731 | if (scale_idx >= 0) { |
| 1732 | animation->remove_track(base_track); |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | if (p_import_value_tracks) { |
| 1738 | for (int i = 0; i < valid_animated_properties.size(); i++) { |
| 1739 | int ti = valid_animated_properties[i]; |
| 1740 | |
| 1741 | if (p_clip == -1) { |
| 1742 | if (track_filter.has(ti)) { |
| 1743 | continue; |
| 1744 | } |
| 1745 | } else { |
| 1746 | if (!track_filter.has(ti)) { |
| 1747 | continue; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | const Collada::AnimationTrack &at = collada.state.animation_tracks[ti]; |
| 1752 | |
| 1753 | // take snapshots |
| 1754 | if (!collada.state.scene_map.has(at.target)) { |
| 1755 | continue; |
| 1756 | } |
| 1757 | |
| 1758 | NodeMap &nm = node_map[at.target]; |
| 1759 | String path = String(scene->get_path_to(nm.node)); |
| 1760 | |
| 1761 | animation->add_track(Animation::TYPE_BLEND_SHAPE); |
| 1762 | int track = animation->get_track_count() - 1; |
| 1763 | |
| 1764 | path = path + ":" + at.param; |
| 1765 | animation->track_set_path(track, path); |
| 1766 | animation->track_set_imported(track, true); //helps merging later |
| 1767 | |
| 1768 | for (int j = 0; j < at.keys.size(); j++) { |
| 1769 | float time = at.keys[j].time; |
| 1770 | Variant value; |
| 1771 | Vector<float> data = at.keys[j].data; |
| 1772 | if (data.size() == 1) { |
| 1773 | //push a float |
| 1774 | value = data[0]; |
| 1775 | |
| 1776 | } else if (data.size() == 16) { |
| 1777 | //matrix |
| 1778 | WARN_PRINT("Collada: Value keys for matrices not supported."); |
| 1779 | } else { |
| 1780 | WARN_PRINT("Collada: Unexpected amount of value keys: " + itos(data.size())); |
| 1781 | } |
| 1782 | |
| 1783 | animation->blend_shape_track_insert_key(track, time, value); |
| 1784 | } |
| 1785 | |
| 1786 | tracks_found = true; |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | if (tracks_found) { |
| 1791 | animations.push_back(animation); |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /*********************************************************************************/ |
| 1796 | /*************************************** SCENE ***********************************/ |
| 1797 | /*********************************************************************************/ |
| 1798 | |
| 1799 | void EditorSceneFormatImporterCollada::get_extensions(List<String> *r_extensions) const { |
| 1800 | r_extensions->push_back("dae"); |
| 1801 | } |
| 1802 | |
| 1803 | 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) { |
| 1804 | if (r_err) { |
| 1805 | *r_err = OK; |
| 1806 | } |
| 1807 | ColladaImport state; |
| 1808 | uint32_t flags = Collada::IMPORT_FLAG_SCENE; |
| 1809 | if (p_flags & IMPORT_ANIMATION) { |
| 1810 | flags |= Collada::IMPORT_FLAG_ANIMATION; |
| 1811 | } |
| 1812 | |
| 1813 | state.use_mesh_builtin_materials = true; |
| 1814 | state.bake_fps = (float)p_options["animation/fps"]; |
| 1815 | |
| 1816 | Error err = state.load(p_path, flags, p_flags & EditorSceneFormatImporter::IMPORT_GENERATE_TANGENT_ARRAYS, !bool(p_flags & EditorSceneFormatImporter::IMPORT_FORCE_DISABLE_MESH_COMPRESSION)); |
| 1817 | |
| 1818 | if (r_err) { |
| 1819 | *r_err = err; |
| 1820 | } |
| 1821 | |
| 1822 | ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Cannot load scene from file '" + p_path + "'."); |
| 1823 | |
| 1824 | if (state.missing_textures.size()) { |
| 1825 | if (r_missing_deps) { |
| 1826 | for (int i = 0; i < state.missing_textures.size(); i++) { |
| 1827 | r_missing_deps->push_back(state.missing_textures[i]); |
| 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | if (p_flags & IMPORT_ANIMATION) { |
| 1833 | state.create_animations(true); |
| 1834 | AnimationPlayer *ap = memnew(AnimationPlayer); |
| 1835 | for (int i = 0; i < state.animations.size(); i++) { |
| 1836 | String name; |
| 1837 | if (state.animations[i]->get_name().is_empty()) { |
| 1838 | name = "default"; |
| 1839 | } else { |
| 1840 | name = state.animations[i]->get_name(); |
| 1841 | } |
| 1842 | |
| 1843 | Ref<AnimationLibrary> library; |
| 1844 | if (!ap->has_animation_library("")) { |
| 1845 | library.instantiate(); |
| 1846 | ap->add_animation_library("", library); |
| 1847 | } else { |
| 1848 | library = ap->get_animation_library(""); |
| 1849 | } |
| 1850 | library->add_animation(name, state.animations[i]); |
| 1851 | } |
| 1852 | state.scene->add_child(ap, true); |
| 1853 | ap->set_owner(state.scene); |
| 1854 | } |
| 1855 | |
| 1856 | return state.scene; |
| 1857 | } |