Godot Engine – Multi-platform 2D and 3D game engine
GLTF: Don't unnecessarily pass around the path internally
Aaron Franke4cb666d507dd
diff --git a/modules/fbx/fbx_document.cpp b/modules/fbx/fbx_document.cppindex 167085f70e7..23fd4704e8a 100644--- a/modules/fbx/fbx_document.cpp+++ b/modules/fbx/fbx_document.cpp@@ -1046,7 +1046,7 @@ GLTFImageIndex FBXDocument::_parse_image_save_image(Ref<FBXState> p_state, constreturn p_state->images.size() - 1;}-Error FBXDocument::_parse_images(Ref<FBXState> p_state, const String &p_base_path) {+Error FBXDocument::_parse_images(Ref<FBXState> p_state) {ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);const ufbx_scene *fbx_scene = p_state->scene.get();@@ -1057,8 +1057,8 @@ Error FBXDocument::_parse_images(Ref<FBXState> p_state, const String &p_base_patif (path.is_absolute_path()) {path = path.get_file();}- if (!p_base_path.is_empty()) {- path = p_base_path.path_join(path);+ if (!p_state->base_path.is_empty()) {+ path = p_state->base_path.path_join(path);}path = path.simplify_path();Vector<uint8_t> data;@@ -2015,7 +2015,7 @@ void FBXDocument::_process_mesh_instances(Ref<FBXState> p_state, Node *p_scene_r}}-Error FBXDocument::_parse(Ref<FBXState> p_state, const String &p_path, Ref<FileAccess> p_file) {+Error FBXDocument::_parse(Ref<FBXState> p_state, Ref<FileAccess> p_file) {p_state->scene.reset();Error err = ERR_INVALID_DATA;@@ -2106,7 +2106,7 @@ Error FBXDocument::_parse(Ref<FBXState> p_state, const String &p_path, Ref<FileA}}- err = _parse_fbx_state(p_state, p_path);+ err = _parse_fbx_state(p_state);ERR_FAIL_COND_V(err != OK, err);return OK;@@ -2170,7 +2170,7 @@ Error FBXDocument::append_from_buffer(const PackedByteArray &p_bytes, const Strifile_access.instantiate();file_access->open_custom(p_bytes.ptr(), p_bytes.size());state->base_path = p_base_path.get_base_dir();- err = _parse(state, state->base_path, file_access);+ err = _parse(state, file_access);ERR_FAIL_COND_V(err != OK, err);for (Ref<GLTFDocumentExtension> ext : document_extensions) {ERR_CONTINUE(ext.is_null());@@ -2180,7 +2180,7 @@ Error FBXDocument::append_from_buffer(const PackedByteArray &p_bytes, const Strireturn OK;}-Error FBXDocument::_parse_fbx_state(Ref<FBXState> p_state, const String &p_search_path) {+Error FBXDocument::_parse_fbx_state(Ref<FBXState> p_state) {Error err;// Abort parsing if the scene is not loaded.@@ -2196,7 +2196,7 @@ Error FBXDocument::_parse_fbx_state(Ref<FBXState> p_state, const String &p_searcif (!p_state->discard_meshes_and_materials) {/* PARSE IMAGES */- err = _parse_images(p_state, p_search_path);+ err = _parse_images(p_state);ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR);@@ -2260,7 +2260,7 @@ Error FBXDocument::append_from_file(const String &p_path, Ref<GLTFState> p_stateif (p_state == Ref<FBXState>()) {p_state.instantiate();}- state->filename = p_path.get_file().get_basename();+ state->filename = p_path.get_file();state->use_named_skin_binds = p_flags & GLTFDocument::ImportFlags::IMPORT_FLAG_USE_NAMED_SKIN_BINDS;state->discard_meshes_and_materials = p_flags & GLTFDocument::ImportFlags::IMPORT_FLAG_DISCARD_MESHES_AND_MATERIALS;Error err;@@ -2272,7 +2272,7 @@ Error FBXDocument::append_from_file(const String &p_path, Ref<GLTFState> p_statebase_path = p_path.get_base_dir();}state->base_path = base_path;- err = _parse(p_state, base_path, file);+ err = _parse(p_state, file);ERR_FAIL_COND_V(err != OK, err);for (Ref<GLTFDocumentExtension> ext : document_extensions) {ERR_CONTINUE(ext.is_null());diff --git a/modules/fbx/fbx_document.h b/modules/fbx/fbx_document.hindex c2dcfd1e2f1..d8667dda724 100644--- a/modules/fbx/fbx_document.h+++ b/modules/fbx/fbx_document.h@@ -78,7 +78,7 @@ private:Error _parse_meshes(Ref<FBXState> p_state);Ref<Image> _parse_image_bytes_into_image(Ref<FBXState> p_state, const Vector<uint8_t> &p_bytes, const String &p_filename, int p_index);GLTFImageIndex _parse_image_save_image(Ref<FBXState> p_state, const Vector<uint8_t> &p_bytes, const String &p_file_extension, int p_index, Ref<Image> p_image);- Error _parse_images(Ref<FBXState> p_state, const String &p_base_path);+ Error _parse_images(Ref<FBXState> p_state);Error _parse_materials(Ref<FBXState> p_state);Error _parse_skins(Ref<FBXState> p_state);Error _parse_animations(Ref<FBXState> p_state);@@ -95,11 +95,11 @@ private:Error _parse_lights(Ref<FBXState> p_state);public:- Error _parse_fbx_state(Ref<FBXState> p_state, const String &p_search_path);+ Error _parse_fbx_state(Ref<FBXState> p_state);void _process_mesh_instances(Ref<FBXState> p_state, Node *p_scene_root);void _generate_scene_node(Ref<FBXState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);void _generate_skeleton_bone_node(Ref<FBXState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);void _import_animation(Ref<FBXState> p_state, AnimationPlayer *p_animation_player,const GLTFAnimationIndex p_index, const bool p_trimming, const bool p_remove_immutable_tracks);- Error _parse(Ref<FBXState> p_state, const String &p_path, Ref<FileAccess> p_file);+ Error _parse(Ref<FBXState> p_state, Ref<FileAccess> p_file);};diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cppindex 7d1e448f532..5a30ecfd821 100644--- a/modules/gltf/gltf_document.cpp+++ b/modules/gltf/gltf_document.cpp@@ -242,29 +242,6 @@ Error GLTFDocument::_serialize_scenes(Ref<GLTFState> p_state) {return OK;}-Error GLTFDocument::_parse_json(const String &p_path, Ref<GLTFState> p_state) {- Error err;- Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ, &err);- if (file.is_null()) {- return err;- }-- Vector<uint8_t> array;- array.resize(file->get_length());- file->get_buffer(array.ptrw(), array.size());- String text = String::utf8((const char *)array.ptr(), array.size());-- JSON json;- err = json.parse(text);- if (err != OK) {- _err_print_error("", p_path.utf8().get_data(), json.get_error_line(), json.get_error_message().utf8().get_data(), false, ERR_HANDLER_SCRIPT);- return err;- }- p_state->json = json.get_data();-- return OK;-}-Error GLTFDocument::_parse_glb(Ref<FileAccess> p_file, Ref<GLTFState> p_state) {ERR_FAIL_COND_V(p_file.is_null(), ERR_INVALID_PARAMETER);ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);@@ -701,7 +678,7 @@ static inline bool _all_buffers_empty(const Vector<Vector<uint8_t>> &p_buffers,return true;}-Error GLTFDocument::_encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path) {+Error GLTFDocument::_encode_buffer_glb(Ref<GLTFState> p_state) {print_verbose("glTF: Total buffers: " + itos(p_state->buffers.size()));if (p_state->buffers.is_empty() || _all_buffers_empty(p_state->buffers)) {@@ -726,16 +703,16 @@ Error GLTFDocument::_encode_buffer_glb(Ref<GLTFState> p_state, const String &p_p}continue;}- String filename = p_path.get_basename().get_file() + itos(i) + ".bin";- String path = p_path.get_base_dir() + "/" + filename;+ const String bin_filename = p_state->filename.get_basename() + itos(i) + ".bin";+ const String bin_path = p_state->base_path.path_join(bin_filename);Error err;- Ref<FileAccess> file = FileAccess::open(path, FileAccess::WRITE, &err);+ Ref<FileAccess> file = FileAccess::open(bin_path, FileAccess::WRITE, &err);if (file.is_null()) {return err;}file->create(FileAccess::ACCESS_RESOURCES);file->store_buffer(buffer_data.ptr(), buffer_data.size());- gltf_buffer["uri"] = filename;+ gltf_buffer["uri"] = bin_filename;gltf_buffer["byteLength"] = buffer_data.size();buffers.push_back(gltf_buffer);}@@ -744,7 +721,7 @@ Error GLTFDocument::_encode_buffer_glb(Ref<GLTFState> p_state, const String &p_preturn OK;}-Error GLTFDocument::_encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path) {+Error GLTFDocument::_encode_buffer_bins(Ref<GLTFState> p_state) {print_verbose("glTF: Total buffers: " + itos(p_state->buffers.size()));if (p_state->buffers.is_empty() || _all_buffers_empty(p_state->buffers)) {@@ -765,15 +742,15 @@ Error GLTFDocument::_encode_buffer_bins(Ref<GLTFState> p_state, const String &p_}continue;}- String filename = p_path.get_basename().get_file() + itos(i) + ".bin";- String path = p_path.get_base_dir() + "/" + filename;+ const String bin_filename = p_state->filename.get_basename() + itos(i) + ".bin";+ const String bin_path = p_state->base_path.path_join(bin_filename);Error err;- Ref<FileAccess> file = FileAccess::open(path, FileAccess::WRITE, &err);+ Ref<FileAccess> file = FileAccess::open(bin_path, FileAccess::WRITE, &err);if (file.is_null()) {return err;}file->store_buffer(buffer_data.ptr(), buffer_data.size());- gltf_buffer["uri"] = filename;+ gltf_buffer["uri"] = bin_filename;gltf_buffer["byteLength"] = buffer_data.size();buffers.push_back(gltf_buffer);}@@ -784,7 +761,7 @@ Error GLTFDocument::_encode_buffer_bins(Ref<GLTFState> p_state, const String &p_return OK;}-Error GLTFDocument::_parse_buffers(Ref<GLTFState> p_state, const String &p_base_path) {+Error GLTFDocument::_parse_buffers(Ref<GLTFState> p_state) {if (!p_state->json.has("buffers")) {return OK;}@@ -804,9 +781,9 @@ Error GLTFDocument::_parse_buffers(Ref<GLTFState> p_state, const String &p_base_}buffer_data = _parse_base64_uri(uri);} else { // Relative path to an external image file.- ERR_FAIL_COND_V(p_base_path.is_empty(), ERR_INVALID_PARAMETER);+ ERR_FAIL_COND_V(p_state->base_path.is_empty(), ERR_INVALID_PARAMETER);uri = uri.uri_file_decode();- uri = p_base_path.path_join(uri).replace_char('\\', '/'); // Fix for Windows.+ uri = p_state->base_path.path_join(uri).replace_char('\\', '/'); // Fix for Windows.ERR_FAIL_COND_V_MSG(!FileAccess::exists(uri), ERR_FILE_NOT_FOUND, "glTF: Binary file not found: " + uri);buffer_data = FileAccess::get_file_as_bytes(uri);ERR_FAIL_COND_V_MSG(buffer_data.is_empty(), ERR_PARSE_ERROR, "glTF: Couldn't load binary file as an array: " + uri);@@ -2135,6 +2112,8 @@ Dictionary GLTFDocument::_serialize_image(Ref<GLTFState> p_state, Ref<Image> p_i}image_dict["uri"] = relative_texture_dir.path_join(image_file_name).uri_encode();} else {+ // Else, such as if this is a `.glb`, a byte array in memory, or an+ // embedded `.gltf`, we need to serialize the image into a buffer view.GLTFBufferViewIndex bvi;Ref<GLTFBufferView> bv;@@ -2325,7 +2304,7 @@ void GLTFDocument::_parse_image_save_image(Ref<GLTFState> p_state, const Vector<p_state->source_images.push_back(p_image);}-Error GLTFDocument::_parse_images(Ref<GLTFState> p_state, const String &p_base_path) {+Error GLTFDocument::_parse_images(Ref<GLTFState> p_state) {ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);if (!p_state->json.has("images")) {return OK;@@ -2385,9 +2364,9 @@ Error GLTFDocument::_parse_images(Ref<GLTFState> p_state, const String &p_base_pmime_type = uri.substr(5, uri.find(";base64") - 5);}} else { // Relative path to an external image file.- ERR_FAIL_COND_V(p_base_path.is_empty(), ERR_INVALID_PARAMETER);+ ERR_FAIL_COND_V(p_state->base_path.is_empty(), ERR_INVALID_PARAMETER);uri = uri.uri_file_decode();- uri = p_base_path.path_join(uri).replace_char('\\', '/'); // Fix for Windows.+ uri = p_state->base_path.path_join(uri).replace_char('\\', '/'); // Fix for Windows.resource_uri = uri.simplify_path();// ResourceLoader will rely on the file extension to use the relevant loader.// The spec says that if mimeType is defined, it should take precedence (e.g.@@ -6707,7 +6686,7 @@ void GLTFDocument::_convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p}}-Error GLTFDocument::_parse(Ref<GLTFState> p_state, const String &p_path, Ref<FileAccess> p_file) {+Error GLTFDocument::_parse(Ref<GLTFState> p_state, Ref<FileAccess> p_file) {Error err;if (p_file.is_null()) {return FAILED;@@ -6747,7 +6726,7 @@ Error GLTFDocument::_parse(Ref<GLTFState> p_state, const String &p_path, Ref<Fil}}- err = _parse_gltf_state(p_state, p_path);+ err = _parse_gltf_state(p_state);ERR_FAIL_COND_V(err != OK, err);return OK;@@ -6810,12 +6789,13 @@ Error GLTFDocument::_serialize_asset_header(Ref<GLTFState> p_state) {return OK;}-Error GLTFDocument::_serialize_file(Ref<GLTFState> p_state, const String p_path) {+Error GLTFDocument::_serialize_file(Ref<GLTFState> p_state) {Error err = FAILED;- if (p_path.to_lower().ends_with("glb")) {- err = _encode_buffer_glb(p_state, p_path);+ if (p_state->filename.to_lower().ends_with("glb")) {+ err = _encode_buffer_glb(p_state);ERR_FAIL_COND_V(err != OK, err);- Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);+ const String gltf_path = p_state->base_path.path_join(p_state->filename);+ Ref<FileAccess> file = FileAccess::open(gltf_path, FileAccess::WRITE, &err);ERR_FAIL_COND_V(file.is_null(), FAILED);constexpr uint64_t header_size = 12;@@ -6838,7 +6818,7 @@ Error GLTFDocument::_serialize_file(Ref<GLTFState> p_state, const String p_path)// Check if the file length with the buffer is greater than glTF's maximum of 4 GiB.// If it is, we can't write the buffer into the file, but can write it separately.if (unlikely(file_length_with_buffer > (uint64_t)UINT32_MAX)) {- err = _encode_buffer_bins(p_state, p_path);+ err = _encode_buffer_bins(p_state);ERR_FAIL_COND_V(err != OK, err);// Since the buffer bins were re-encoded, we need to re-convert the JSON to string.json_string = JSON::stringify(p_state->json, "", true, true);@@ -6877,9 +6857,10 @@ Error GLTFDocument::_serialize_file(Ref<GLTFState> p_state, const String p_path)}}} else {- err = _encode_buffer_bins(p_state, p_path);+ err = _encode_buffer_bins(p_state);ERR_FAIL_COND_V(err != OK, err);- Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);+ const String gltf_path = p_state->base_path.path_join(p_state->filename);+ Ref<FileAccess> file = FileAccess::open(gltf_path, FileAccess::WRITE, &err);ERR_FAIL_COND_V(file.is_null(), FAILED);String json = JSON::stringify(p_state->json, "", true, true);@@ -7027,7 +7008,7 @@ HashSet<String> GLTFDocument::get_supported_gltf_extensions_hashset() {}PackedByteArray GLTFDocument::_serialize_glb_buffer(Ref<GLTFState> p_state, Error *r_err) {- Error err = _encode_buffer_glb(p_state, "");+ Error err = _encode_buffer_glb(p_state);if (r_err) {*r_err = err;}@@ -7142,11 +7123,11 @@ Error GLTFDocument::_parse_asset_header(Ref<GLTFState> p_state) {return OK;}-Error GLTFDocument::_parse_gltf_state(Ref<GLTFState> p_state, const String &p_search_path) {+Error GLTFDocument::_parse_gltf_state(Ref<GLTFState> p_state) {Error err;/* PARSE BUFFERS */- err = _parse_buffers(p_state, p_search_path);+ err = _parse_buffers(p_state);ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR);/* PARSE BUFFER VIEWS */@@ -7171,7 +7152,7 @@ Error GLTFDocument::_parse_gltf_state(Ref<GLTFState> p_state, const String &p_seif (!p_state->discard_meshes_and_materials) {/* PARSE IMAGES */- err = _parse_images(p_state, p_search_path);+ err = _parse_images(p_state);ERR_FAIL_COND_V(err != OK, ERR_PARSE_ERROR);@@ -7243,7 +7224,7 @@ Error GLTFDocument::write_to_filesystem(Ref<GLTFState> p_state, const String &p_p_state->set_base_path(p_path.get_base_dir());p_state->filename = p_path.get_file();RETURN_IF_ERROR(_serialize(p_state));- Error err = _serialize_file(p_state, p_path);+ Error err = _serialize_file(p_state);if (err != OK) {return Error::FAILED;}@@ -7359,7 +7340,7 @@ Error GLTFDocument::append_from_buffer(const PackedByteArray &p_bytes, const Strfile_access.instantiate();file_access->open_custom(p_bytes.ptr(), p_bytes.size());p_state->set_base_path(p_base_path.get_base_dir());- err = _parse(p_state, p_state->base_path, file_access);+ err = _parse(p_state, file_access);ERR_FAIL_COND_V(err != OK, err);for (Ref<GLTFDocumentExtension> ext : document_extensions) {ERR_CONTINUE(ext.is_null());@@ -7387,7 +7368,7 @@ Error GLTFDocument::append_from_file(const String &p_path, Ref<GLTFState> p_statbase_path = p_path.get_base_dir();}p_state->set_base_path(base_path);- err = _parse(p_state, base_path, file);+ err = _parse(p_state, file);ERR_FAIL_COND_V(err != OK, err);for (Ref<GLTFDocumentExtension> ext : document_extensions) {ERR_CONTINUE(ext.is_null());diff --git a/modules/gltf/gltf_document.h b/modules/gltf/gltf_document.hindex bba726f8b26..fb67d78656f 100644--- a/modules/gltf/gltf_document.h+++ b/modules/gltf/gltf_document.h@@ -139,10 +139,9 @@ private:StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);Ref<GLTFTextureSampler> _get_sampler_for_texture(Ref<GLTFState> p_state,const GLTFTextureIndex p_texture);- Error _parse_json(const String &p_path, Ref<GLTFState> p_state);Error _parse_glb(Ref<FileAccess> p_file, Ref<GLTFState> p_state);void _compute_node_heights(Ref<GLTFState> p_state);- Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);+ Error _parse_buffers(Ref<GLTFState> p_state);Error _parse_buffer_views(Ref<GLTFState> p_state);Error _parse_accessors(Ref<GLTFState> p_state);template <typename T>@@ -164,7 +163,7 @@ private:Error _serialize_lights(Ref<GLTFState> p_state);Ref<Image> _parse_image_bytes_into_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_mime_type, int p_index, String &r_file_extension);void _parse_image_save_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_resource_uri, const String &p_file_extension, int p_index, Ref<Image> p_image);- Error _parse_images(Ref<GLTFState> p_state, const String &p_base_path);+ Error _parse_images(Ref<GLTFState> p_state);Error _parse_textures(Ref<GLTFState> p_state);Error _parse_texture_samplers(Ref<GLTFState> p_state);Error _parse_materials(Ref<GLTFState> p_state);@@ -206,13 +205,13 @@ private:Error _serialize_nodes(Ref<GLTFState> p_state);Error _serialize_scenes(Ref<GLTFState> p_state);String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);- Error _encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path);- Error _encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path);+ Error _encode_buffer_bins(Ref<GLTFState> p_state);+ Error _encode_buffer_glb(Ref<GLTFState> p_state);PackedByteArray _serialize_glb_buffer(Ref<GLTFState> p_state, Error *r_err);Dictionary _serialize_texture_transform_uv1(const Ref<BaseMaterial3D> &p_material);Dictionary _serialize_texture_transform_uv2(const Ref<BaseMaterial3D> &p_material);Error _serialize_asset_header(Ref<GLTFState> p_state);- Error _serialize_file(Ref<GLTFState> p_state, const String p_path);+ Error _serialize_file(Ref<GLTFState> p_state);Error _serialize_gltf_extensions(Ref<GLTFState> p_state) const;public:@@ -241,7 +240,7 @@ public:virtual Error write_to_filesystem(Ref<GLTFState> p_state, const String &p_path);public:- Error _parse_gltf_state(Ref<GLTFState> p_state, const String &p_search_path);+ Error _parse_gltf_state(Ref<GLTFState> p_state);Error _parse_asset_header(Ref<GLTFState> p_state);Error _parse_gltf_extensions(Ref<GLTFState> p_state);void _process_mesh_instances(Ref<GLTFState> p_state, Node *p_scene_root);@@ -302,7 +301,7 @@ public:void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player, const String &p_animation_track_name);Error _serialize(Ref<GLTFState> p_state);- Error _parse(Ref<GLTFState> p_state, const String &p_path, Ref<FileAccess> p_file);+ Error _parse(Ref<GLTFState> p_state, Ref<FileAccess> p_file);};VARIANT_ENUM_CAST(GLTFDocument::RootNodeMode);