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