Godot Engine – Multi-platform 2D and 3D game engine
GLTF: Fix core parse errors not failing import
Aaron Franke797b08eb7b90
diff --git a/modules/gltf/doc_classes/GLTFPhysicsShape.xml b/modules/gltf/doc_classes/GLTFPhysicsShape.xmlindex 14c90c3c9b7..da34344bd1a 100644--- a/modules/gltf/doc_classes/GLTFPhysicsShape.xml+++ b/modules/gltf/doc_classes/GLTFPhysicsShape.xml@@ -59,20 +59,20 @@The height of the shape, in meters. This is only used when the shape type is [code]"capsule"[/code] or [code]"cylinder"[/code]. This value should not be negative, and for [code]"capsule"[/code] it should be at least twice the radius.</member><member name="importer_mesh" type="ImporterMesh" setter="set_importer_mesh" getter="get_importer_mesh">- The [ImporterMesh] resource of the shape. This is only used when the shape type is [code]"hull"[/code] (convex hull) or [code]"trimesh"[/code] (concave trimesh).+ The [ImporterMesh] resource of the shape. This is only used when the shape type is [code]"convex"[/code] (convex hull) or [code]"trimesh"[/code] (concave trimesh).</member><member name="is_trigger" type="bool" setter="set_is_trigger" getter="get_is_trigger" default="false">If [code]true[/code], indicates that this shape is a trigger. For Godot, this means that the shape should be a child of an [Area3D] node.This is the only variable not used in the [method to_node] method, it's intended to be used alongside when deciding where to add the generated node as a child.</member><member name="mesh_index" type="int" setter="set_mesh_index" getter="get_mesh_index" default="-1">- The index of the shape's mesh in the glTF file. This is only used when the shape type is [code]"hull"[/code] (convex hull) or [code]"trimesh"[/code] (concave trimesh).+ The index of the shape's mesh in the glTF file. This is only used when the shape type is [code]"convex"[/code] (convex hull) or [code]"trimesh"[/code] (concave trimesh).</member><member name="radius" type="float" setter="set_radius" getter="get_radius" default="0.5">The radius of the shape, in meters. This is only used when the shape type is [code]"capsule"[/code], [code]"cylinder"[/code], or [code]"sphere"[/code]. This value should not be negative.</member><member name="shape_type" type="String" setter="set_shape_type" getter="get_shape_type" default="""">- The type of shape this shape represents. Valid values are [code]"box"[/code], [code]"capsule"[/code], [code]"cylinder"[/code], [code]"sphere"[/code], [code]"hull"[/code], and [code]"trimesh"[/code].+ The type of shape this shape represents. Valid values are [code]"box"[/code], [code]"capsule"[/code], [code]"cylinder"[/code], [code]"sphere"[/code], [code]"convex"[/code], and [code]"trimesh"[/code].</member><member name="size" type="Vector3" setter="set_size" getter="get_size" default="Vector3(1, 1, 1)">The size of the shape, in meters. This is only used when the shape type is [code]"box"[/code], and it represents the [code]"diameter"[/code] of the box. This value should not be negative.diff --git a/modules/gltf/extensions/gltf_light.cpp b/modules/gltf/extensions/gltf_light.cppindex 025efa775b3..149513d837c 100644--- a/modules/gltf/extensions/gltf_light.cpp+++ b/modules/gltf/extensions/gltf_light.cpp@@ -212,11 +212,17 @@ Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary &p_dictionary) {light->range = p_dictionary["range"];}if (type == "spot") {- const Dictionary &spot = p_dictionary["spot"];- light->inner_cone_angle = spot["innerConeAngle"];- light->outer_cone_angle = spot["outerConeAngle"];- if (light->inner_cone_angle >= light->outer_cone_angle) {- ERR_PRINT("Error parsing glTF light: The inner angle must be smaller than the outer angle.");+ if (p_dictionary.has("spot")) {+ const Dictionary &spot = p_dictionary["spot"];+ if (spot.has("innerConeAngle")) {+ light->inner_cone_angle = spot["innerConeAngle"];+ }+ if (spot.has("outerConeAngle")) {+ light->outer_cone_angle = spot["outerConeAngle"];+ }+ if (light->inner_cone_angle >= light->outer_cone_angle) {+ WARN_PRINT("Error parsing glTF light: The inner angle must be smaller than the outer angle.");+ }}} else if (type != "point" && type != "directional") {ERR_PRINT("Error parsing glTF light: Light type '" + type + "' is unknown.");diff --git a/modules/gltf/extensions/physics/gltf_document_extension_physics.cpp b/modules/gltf/extensions/physics/gltf_document_extension_physics.cppindex 2be5478d2d5..a87f7a869fb 100644--- a/modules/gltf/extensions/physics/gltf_document_extension_physics.cpp+++ b/modules/gltf/extensions/physics/gltf_document_extension_physics.cpp@@ -54,7 +54,9 @@ Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, conif (state_shape_dicts.size() > 0) {Array state_shapes;for (int i = 0; i < state_shape_dicts.size(); i++) {- state_shapes.push_back(GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]));+ Ref<GLTFPhysicsShape> shape = GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]);+ ERR_FAIL_COND_V(shape.is_null(), ERR_PARSE_ERROR);+ state_shapes.push_back(shape);}p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_shapes);}@@ -67,7 +69,9 @@ Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, conif (state_collider_dicts.size() > 0) {Array state_colliders;for (int i = 0; i < state_collider_dicts.size(); i++) {- state_colliders.push_back(GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]));+ Ref<GLTFPhysicsShape> shape = GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]);+ ERR_FAIL_COND_V(shape.is_null(), ERR_PARSE_ERROR);+ state_colliders.push_back(shape);}p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_colliders);}@@ -97,7 +101,9 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_stateERR_FAIL_INDEX_V_MSG(node_collider_index, state_colliders.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the collider index " + itos(node_collider_index) + " is not in the state colliders (size: " + itos(state_colliders.size()) + ").");p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), state_colliders[node_collider_index]);} else {- p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(node_collider_ext));+ Ref<GLTFPhysicsShape> shape = GLTFPhysicsShape::from_dictionary(node_collider_ext);+ ERR_FAIL_COND_V(shape.is_null(), ERR_PARSE_ERROR);+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), shape);}}#endif // DISABLE_DEPRECATED@@ -142,7 +148,9 @@ Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state}}if (physics_body_ext.has("motion") || physics_body_ext.has("type")) {- p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(physics_body_ext));+ Ref<GLTFPhysicsBody> body = GLTFPhysicsBody::from_dictionary(physics_body_ext);+ ERR_FAIL_COND_V(body.is_null(), ERR_PARSE_ERROR);+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), body);}}return OK;diff --git a/modules/gltf/extensions/physics/gltf_physics_shape.cpp b/modules/gltf/extensions/physics/gltf_physics_shape.cppindex d39f7515991..92689296726 100644--- a/modules/gltf/extensions/physics/gltf_physics_shape.cpp+++ b/modules/gltf/extensions/physics/gltf_physics_shape.cpp@@ -270,17 +270,24 @@ Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_dictionary(const Dictionary &p_dictERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFPhysicsShape>(), "Failed to parse GLTFPhysicsShape, missing required field 'type'.");Ref<GLTFPhysicsShape> gltf_shape;gltf_shape.instantiate();- String shape_type = p_dictionary["type"];- if (shape_type == "hull") {+ String declared_shape_type = p_dictionary["type"];+ String shape_type;+ // Compatibility aliases for different iterations of the physics extensions.+ // Khronos glTF 2.1: convexMesh and mesh. OMI_physics_shape: convex and trimesh. OMI_collider: hull and trimesh.+ if (declared_shape_type == "hull" || declared_shape_type == "convexMesh") {shape_type = "convex";+ } else if (declared_shape_type == "mesh") {+ shape_type = "trimesh";+ } else {+ shape_type = declared_shape_type;}gltf_shape->shape_type = shape_type;if (shape_type != "box" && shape_type != "capsule" && shape_type != "cylinder" && shape_type != "sphere" && shape_type != "convex" && shape_type != "trimesh") {ERR_PRINT("GLTFPhysicsShape: Error parsing unknown shape type '" + shape_type + "'. Only box, capsule, cylinder, sphere, convex, and trimesh are supported.");}Dictionary properties;- if (p_dictionary.has(shape_type)) {- properties = p_dictionary[shape_type];+ if (p_dictionary.has(declared_shape_type)) {+ properties = p_dictionary[declared_shape_type];} else {properties = p_dictionary;}@@ -298,9 +305,12 @@ Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_dictionary(const Dictionary &p_dictERR_PRINT("GLTFPhysicsShape: Error parsing the size, it must have exactly 3 numbers.");}}+#ifndef DISABLE_DEPRECATED+ // "isTrigger" is only present in the deprecated OMI_collider extension.if (properties.has("isTrigger")) {gltf_shape->set_is_trigger(properties["isTrigger"]);}+#endif // DISABLE_DEPRECATEDif (properties.has("mesh")) {gltf_shape->set_mesh_index(properties["mesh"]);}diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cppindex 7d1e448f532..3da92ade770 100644--- a/modules/gltf/gltf_document.cpp+++ b/modules/gltf/gltf_document.cpp@@ -853,6 +853,7 @@ Error GLTFDocument::_parse_buffer_views(Ref<GLTFState> p_state) {ERR_FAIL_COND_V(!dict.has("buffer"), ERR_PARSE_ERROR);ERR_FAIL_COND_V(!dict.has("byteLength"), ERR_PARSE_ERROR);Ref<GLTFBufferView> buffer_view = GLTFBufferView::from_dictionary(dict);+ ERR_FAIL_COND_V(buffer_view.is_null(), ERR_PARSE_ERROR);p_state->buffer_views.push_back(buffer_view);}@@ -890,6 +891,7 @@ Error GLTFDocument::_parse_accessors(Ref<GLTFState> p_state) {ERR_FAIL_COND_V(!dict.has("count"), ERR_PARSE_ERROR);ERR_FAIL_COND_V(!dict.has("type"), ERR_PARSE_ERROR);Ref<GLTFAccessor> accessor = GLTFAccessor::from_dictionary(dict);+ ERR_FAIL_COND_V(accessor.is_null(), ERR_PARSE_ERROR);p_state->accessors.push_back(accessor);}@@ -3578,9 +3580,7 @@ Error GLTFDocument::_parse_lights(Ref<GLTFState> p_state) {for (GLTFLightIndex light_i = 0; light_i < lights.size(); light_i++) {Ref<GLTFLight> light = GLTFLight::from_dictionary(lights[light_i]);- if (light.is_null()) {- return Error::ERR_PARSE_ERROR;- }+ ERR_FAIL_COND_V(light.is_null(), ERR_PARSE_ERROR);p_state->lights.push_back(light);}@@ -3597,7 +3597,9 @@ Error GLTFDocument::_parse_cameras(Ref<GLTFState> p_state) {const Array cameras = p_state->json["cameras"];for (GLTFCameraIndex i = 0; i < cameras.size(); i++) {- p_state->cameras.push_back(GLTFCamera::from_dictionary(cameras[i]));+ Ref<GLTFCamera> camera = GLTFCamera::from_dictionary(cameras[i]);+ ERR_FAIL_COND_V(camera.is_null(), ERR_PARSE_ERROR);+ p_state->cameras.push_back(camera);}print_verbose("glTF: Total cameras: " + itos(p_state->cameras.size()));diff --git a/modules/gltf/structures/gltf_accessor.cpp b/modules/gltf/structures/gltf_accessor.cppindex b6931035484..155bbaccbff 100644--- a/modules/gltf/structures/gltf_accessor.cpp+++ b/modules/gltf/structures/gltf_accessor.cpp@@ -1708,8 +1708,15 @@ GLTFAccessorIndex GLTFAccessor::encode_new_sparse_accessor_from_vec3s(const Ref<Ref<GLTFAccessor> GLTFAccessor::from_dictionary(const Dictionary &p_dict) {// See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.schema.json+ ERR_FAIL_COND_V_MSG(!p_dict.has("componentType"), Ref<GLTFAccessor>(), "Failed to parse glTF accessor, missing required field 'componentType'.");+ ERR_FAIL_COND_V_MSG(!p_dict.has("count"), Ref<GLTFAccessor>(), "Failed to parse glTF accessor, missing required field 'count'.");+ ERR_FAIL_COND_V_MSG(!p_dict.has("type"), Ref<GLTFAccessor>(), "Failed to parse glTF accessor, missing required field 'type'.");Ref<GLTFAccessor> accessor;accessor.instantiate();+ if (p_dict.has("count")) {+ accessor->count = p_dict["count"];+ }+ ERR_FAIL_COND_V_MSG(accessor->count <= 0, Ref<GLTFAccessor>(), "glTF import: Invalid accessor count " + itos(accessor->count) + " for accessor. Accessor count must be greater than 0.");if (p_dict.has("bufferView")) {// bufferView is optional. If not present, the accessor is considered to be zero-initialized.accessor->buffer_view = p_dict["bufferView"];@@ -1720,12 +1727,6 @@ Ref<GLTFAccessor> GLTFAccessor::from_dictionary(const Dictionary &p_dict) {if (p_dict.has("componentType")) {accessor->component_type = (GLTFAccessor::GLTFComponentType)(int32_t)p_dict["componentType"];}- if (p_dict.has("count")) {- accessor->count = p_dict["count"];- }- if (accessor->count <= 0) {- ERR_PRINT("glTF import: Invalid accessor count " + itos(accessor->count) + " for accessor. Accessor count must be greater than 0.");- }if (p_dict.has("max")) {accessor->max = p_dict["max"];}@@ -1738,20 +1739,20 @@ Ref<GLTFAccessor> GLTFAccessor::from_dictionary(const Dictionary &p_dict) {if (p_dict.has("sparse")) {// See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.sparse.schema.jsonconst Dictionary &sparse_dict = p_dict["sparse"];- ERR_FAIL_COND_V(!sparse_dict.has("count"), accessor);+ ERR_FAIL_COND_V(!sparse_dict.has("count"), Ref<GLTFAccessor>());accessor->sparse_count = sparse_dict["count"];- ERR_FAIL_COND_V(!sparse_dict.has("indices"), accessor);+ ERR_FAIL_COND_V(!sparse_dict.has("indices"), Ref<GLTFAccessor>());const Dictionary &sparse_indices_dict = sparse_dict["indices"];- ERR_FAIL_COND_V(!sparse_indices_dict.has("bufferView"), accessor);+ ERR_FAIL_COND_V(!sparse_indices_dict.has("bufferView"), Ref<GLTFAccessor>());accessor->sparse_indices_buffer_view = sparse_indices_dict["bufferView"];- ERR_FAIL_COND_V(!sparse_indices_dict.has("componentType"), accessor);+ ERR_FAIL_COND_V(!sparse_indices_dict.has("componentType"), Ref<GLTFAccessor>());accessor->sparse_indices_component_type = (GLTFAccessor::GLTFComponentType)(int32_t)sparse_indices_dict["componentType"];if (sparse_indices_dict.has("byteOffset")) {accessor->sparse_indices_byte_offset = sparse_indices_dict["byteOffset"];}- ERR_FAIL_COND_V(!sparse_dict.has("values"), accessor);+ ERR_FAIL_COND_V(!sparse_dict.has("values"), Ref<GLTFAccessor>());const Dictionary &sparse_values_dict = sparse_dict["values"];- ERR_FAIL_COND_V(!sparse_values_dict.has("bufferView"), accessor);+ ERR_FAIL_COND_V(!sparse_values_dict.has("bufferView"), Ref<GLTFAccessor>());accessor->sparse_values_buffer_view = sparse_values_dict["bufferView"];if (sparse_values_dict.has("byteOffset")) {accessor->sparse_values_byte_offset = sparse_values_dict["byteOffset"];diff --git a/modules/gltf/structures/gltf_buffer_view.cpp b/modules/gltf/structures/gltf_buffer_view.cppindex 443d534ec9f..f0de5f69036 100644--- a/modules/gltf/structures/gltf_buffer_view.cpp+++ b/modules/gltf/structures/gltf_buffer_view.cpp@@ -186,6 +186,8 @@ GLTFBufferViewIndex GLTFBufferView::write_new_buffer_view_into_state(const Ref<GRef<GLTFBufferView> GLTFBufferView::from_dictionary(const Dictionary &p_dict) {// See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/bufferView.schema.json+ ERR_FAIL_COND_V_MSG(!p_dict.has("buffer"), Ref<GLTFBufferView>(), "Failed to parse glTF buffer view, missing required field 'buffer'.");+ ERR_FAIL_COND_V_MSG(!p_dict.has("byteLength"), Ref<GLTFBufferView>(), "Failed to parse glTF buffer view, missing required field 'byteLength'.");Ref<GLTFBufferView> buffer_view;buffer_view.instantiate();if (p_dict.has("buffer")) {@@ -198,10 +200,9 @@ Ref<GLTFBufferView> GLTFBufferView::from_dictionary(const Dictionary &p_dict) {buffer_view->set_byte_offset(p_dict["byteOffset"]);}if (p_dict.has("byteStride")) {- buffer_view->byte_stride = p_dict["byteStride"];- if (buffer_view->byte_stride < 4 || buffer_view->byte_stride > 252 || buffer_view->byte_stride % 4 != 0) {- ERR_PRINT("glTF import: Invalid byte stride " + itos(buffer_view->byte_stride) + " for buffer view. If defined, byte stride must be a multiple of 4 and between 4 and 252.");- }+ const int64_t declared_byte_stride = p_dict["byteStride"];+ ERR_FAIL_COND_V_MSG(declared_byte_stride < 4 || declared_byte_stride > 252 || declared_byte_stride % 4 != 0, Ref<GLTFBufferView>(), "glTF import: Invalid byte stride " + itos(declared_byte_stride) + " for buffer view. If defined, byte stride must be a multiple of 4 and between 4 and 252.");+ buffer_view->byte_stride = declared_byte_stride;}if (p_dict.has("target")) {const int target = p_dict["target"];