diff options
Diffstat (limited to 'modules/gltf/gltf_document.cpp')
-rw-r--r-- | modules/gltf/gltf_document.cpp | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 580bc006f5..cce0740121 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -57,8 +57,12 @@ #include "core/version_hash.gen.h" #include "drivers/png/png_driver_common.h" #include "editor/import/resource_importer_scene.h" +#ifdef MODULE_CSG_ENABLED #include "modules/csg/csg_shape.h" +#endif // MODULE_CSG_ENABLED +#ifdef MODULE_GRIDMAP_ENABLED #include "modules/gridmap/grid_map.h" +#endif // MODULE_GRIDMAP_ENABLED #include "modules/regex/regex.h" #include "scene/2d/node_2d.h" #include "scene/3d/bone_attachment_3d.h" @@ -805,7 +809,9 @@ Error GLTFDocument::_encode_buffer_views(Ref<GLTFState> state) { } Error GLTFDocument::_parse_buffer_views(Ref<GLTFState> state) { - ERR_FAIL_COND_V(!state->json.has("bufferViews"), ERR_FILE_CORRUPT); + if (!state->json.has("bufferViews")) { + return OK; + } const Array &buffers = state->json["bufferViews"]; for (GLTFBufferViewIndex i = 0; i < buffers.size(); i++) { const Dictionary &d = buffers[i]; @@ -942,7 +948,9 @@ GLTFDocument::GLTFType GLTFDocument::_get_type_from_str(const String &p_string) } Error GLTFDocument::_parse_accessors(Ref<GLTFState> state) { - ERR_FAIL_COND_V(!state->json.has("accessors"), ERR_FILE_CORRUPT); + if (!state->json.has("accessors")) { + return OK; + } const Array &accessors = state->json["accessors"]; for (GLTFAccessorIndex i = 0; i < accessors.size(); i++) { const Dictionary &d = accessors[i]; @@ -5097,7 +5105,6 @@ Node3D *GLTFDocument::_generate_spatial(Ref<GLTFState> state, Node *scene_parent } void GLTFDocument::_convert_scene_node(Ref<GLTFState> state, Node *p_current, Node *p_root, const GLTFNodeIndex p_gltf_parent, const GLTFNodeIndex p_gltf_root) { bool retflag = true; - Node3D *spatial = cast_to<Node3D>(p_current); _check_visibility(p_current, retflag); if (retflag) { return; @@ -5106,9 +5113,11 @@ void GLTFDocument::_convert_scene_node(Ref<GLTFState> state, Node *p_current, No gltf_node.instance(); gltf_node->set_name(_gen_unique_name(state, p_current->get_name())); if (cast_to<Node3D>(p_current)) { + Node3D *spatial = cast_to<Node3D>(p_current); _convert_spatial(state, spatial, gltf_node); } if (cast_to<MeshInstance3D>(p_current)) { + Node3D *spatial = cast_to<Node3D>(p_current); _convert_mesh_to_gltf(p_current, state, spatial, gltf_node); } else if (cast_to<BoneAttachment3D>(p_current)) { _convert_bone_attachment_to_gltf(p_current, state, gltf_node, retflag); @@ -5120,18 +5129,22 @@ void GLTFDocument::_convert_scene_node(Ref<GLTFState> state, Node *p_current, No return; } else if (cast_to<MultiMeshInstance3D>(p_current)) { _convert_mult_mesh_instance_to_gltf(p_current, p_gltf_parent, p_gltf_root, gltf_node, state, p_root); +#ifdef MODULE_CSG_ENABLED } else if (cast_to<CSGShape3D>(p_current)) { if (p_current->get_parent() && cast_to<CSGShape3D>(p_current)->is_root_shape()) { _convert_csg_shape_to_gltf(p_current, p_gltf_parent, gltf_node, state); } +#endif // MODULE_CSG_ENABLED +#ifdef MODULE_GRIDMAP_ENABLED } else if (cast_to<GridMap>(p_current)) { _convert_grid_map_to_gltf(p_current, p_gltf_parent, p_gltf_root, gltf_node, state, p_root); +#endif // MODULE_GRIDMAP_ENABLED } else if (cast_to<Camera3D>(p_current)) { Camera3D *camera = Object::cast_to<Camera3D>(p_current); - _convert_camera_to_gltf(camera, state, spatial, gltf_node); + _convert_camera_to_gltf(camera, state, camera, gltf_node); } else if (cast_to<Light3D>(p_current)) { Light3D *light = Object::cast_to<Light3D>(p_current); - _convert_light_to_gltf(light, state, spatial, gltf_node); + _convert_light_to_gltf(light, state, light, gltf_node); } else if (cast_to<AnimationPlayer>(p_current)) { AnimationPlayer *animation_player = Object::cast_to<AnimationPlayer>(p_current); _convert_animation_player_to_gltf(animation_player, state, p_gltf_parent, p_gltf_root, gltf_node, p_current, p_root); @@ -5150,6 +5163,7 @@ void GLTFDocument::_convert_scene_node(Ref<GLTFState> state, Node *p_current, No } } +#ifdef MODULE_CSG_ENABLED void GLTFDocument::_convert_csg_shape_to_gltf(Node *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> gltf_node, Ref<GLTFState> state) { CSGShape3D *csg = Object::cast_to<CSGShape3D>(p_current); csg->call("_update_shape"); @@ -5176,6 +5190,7 @@ void GLTFDocument::_convert_csg_shape_to_gltf(Node *p_current, GLTFNodeIndex p_g gltf_node->xform = csg->get_meshes()[0]; gltf_node->set_name(_gen_unique_name(state, csg->get_name())); } +#endif // MODULE_CSG_ENABLED void GLTFDocument::_create_gltf_node(Ref<GLTFState> state, Node *p_scene_parent, GLTFNodeIndex current_node_i, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_gltf_node, Ref<GLTFNode> gltf_node) { @@ -5225,6 +5240,7 @@ void GLTFDocument::_convert_light_to_gltf(Light3D *light, Ref<GLTFState> state, } } +#ifdef MODULE_GRIDMAP_ENABLED void GLTFDocument::_convert_grid_map_to_gltf(Node *p_scene_parent, const GLTFNodeIndex &p_parent_node_index, const GLTFNodeIndex &p_root_node_index, Ref<GLTFNode> gltf_node, Ref<GLTFState> state, Node *p_root_node) { GridMap *grid_map = Object::cast_to<GridMap>(p_scene_parent); ERR_FAIL_COND(!grid_map); @@ -5256,6 +5272,7 @@ void GLTFDocument::_convert_grid_map_to_gltf(Node *p_scene_parent, const GLTFNod new_gltf_node->set_name(_gen_unique_name(state, grid_map->get_mesh_library()->get_item_name(cell))); } } +#endif // MODULE_GRIDMAP_ENABLED void GLTFDocument::_convert_mult_mesh_instance_to_gltf(Node *p_scene_parent, const GLTFNodeIndex &p_parent_node_index, const GLTFNodeIndex &p_root_node_index, Ref<GLTFNode> gltf_node, Ref<GLTFState> state, Node *p_root_node) { MultiMeshInstance3D *multi_mesh_instance = Object::cast_to<MultiMeshInstance3D>(p_scene_parent); @@ -5271,8 +5288,7 @@ void GLTFDocument::_convert_mult_mesh_instance_to_gltf(Node *p_scene_parent, con transform.origin = Vector3(xform_2d.get_origin().x, 0, xform_2d.get_origin().y); real_t rotation = xform_2d.get_rotation(); - Quat quat; - quat.set_axis_angle(Vector3(0, 1, 0), rotation); + Quat quat(Vector3(0, 1, 0), rotation); Size2 scale = xform_2d.get_scale(); transform.basis.set_quat_scale(quat, Vector3(scale.x, 0, scale.y)); @@ -6024,14 +6040,12 @@ GLTFAnimation::Track GLTFDocument::_convert_animation_track(Ref<GLTFState> state p_track.rotation_track.interpolation = gltf_interpolation; for (int32_t key_i = 0; key_i < key_count; key_i++) { - Quat rotation; Vector3 rotation_degrees = p_animation->track_get_key_value(p_track_i, key_i); Vector3 rotation_radian; rotation_radian.x = Math::deg2rad(rotation_degrees.x); rotation_radian.y = Math::deg2rad(rotation_degrees.y); rotation_radian.z = Math::deg2rad(rotation_degrees.z); - rotation.set_euler(rotation_radian); - p_track.rotation_track.values.write[key_i] = rotation; + p_track.rotation_track.values.write[key_i] = Quat(rotation_radian); } } else if (path.find(":scale") != -1) { p_track.scale_track.times = times; @@ -6283,18 +6297,22 @@ void GLTFDocument::_convert_animation(Ref<GLTFState> state, AnimationPlayer *ap, } } } else if (String(orig_track_path).find(":") == -1) { - const Node *node = ap->get_parent()->get_node_or_null(orig_track_path); - for (Map<GLTFNodeIndex, Node *>::Element *scene_node_i = state->scene_nodes.front(); scene_node_i; scene_node_i = scene_node_i->next()) { - if (scene_node_i->get() == node) { - GLTFNodeIndex node_index = scene_node_i->key(); - Map<int, GLTFAnimation::Track>::Element *node_track_i = gltf_animation->get_tracks().find(node_index); - GLTFAnimation::Track track; - if (node_track_i) { - track = node_track_i->get(); + ERR_CONTINUE(!ap->get_parent()); + for (int32_t node_i = 0; node_i < ap->get_parent()->get_child_count(); node_i++) { + const Node *child = ap->get_parent()->get_child(node_i); + const Node *node = child->get_node_or_null(orig_track_path); + for (Map<GLTFNodeIndex, Node *>::Element *scene_node_i = state->scene_nodes.front(); scene_node_i; scene_node_i = scene_node_i->next()) { + if (scene_node_i->get() == node) { + GLTFNodeIndex node_index = scene_node_i->key(); + Map<int, GLTFAnimation::Track>::Element *node_track_i = gltf_animation->get_tracks().find(node_index); + GLTFAnimation::Track track; + if (node_track_i) { + track = node_track_i->get(); + } + track = _convert_animation_track(state, track, animation, Transform(), track_i, node_index); + gltf_animation->get_tracks().insert(node_index, track); + break; } - track = _convert_animation_track(state, track, animation, Transform(), track_i, node_index); - gltf_animation->get_tracks().insert(node_index, track); - break; } } } |