Parcourir la source

GLTF: Don't give up loading image if import fails

Aaron Franke il y a 1 an
Parent
commit
0b7354dfa9
2 fichiers modifiés avec 22 ajouts et 23 suppressions
  1. 2 2
      editor/editor_file_system.cpp
  2. 20 21
      modules/gltf/gltf_document.cpp

+ 2 - 2
editor/editor_file_system.cpp

@@ -2581,7 +2581,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
 		EditorFileSystemDirectory *fs = nullptr;
 		int cpos = -1;
 		bool found = _find_file(file, &fs, cpos);
-		ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, "Can't find file '" + file + "'.");
+		ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, vformat("Can't find file '%s' during group reimport.", file));
 
 		//update modified times, to avoid reimport
 		fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
@@ -2631,7 +2631,7 @@ Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<Strin
 	int cpos = -1;
 	if (p_update_file_system) {
 		bool found = _find_file(p_file, &fs, cpos);
-		ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, "Can't find file '" + p_file + "'.");
+		ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s' during file reimport.", p_file));
 	}
 
 	//try to obtain existing params

+ 20 - 21
modules/gltf/gltf_document.cpp

@@ -3502,13 +3502,12 @@ void GLTFDocument::_parse_image_save_image(Ref<GLTFState> p_state, const Vector<
 #ifdef TOOLS_ENABLED
 	if (Engine::get_singleton()->is_editor_hint() && handling == GLTFState::GLTFHandleBinary::HANDLE_BINARY_EXTRACT_TEXTURES) {
 		if (p_state->base_path.is_empty()) {
-			p_state->images.push_back(Ref<Texture2D>());
-			p_state->source_images.push_back(Ref<Image>());
-		} else if (p_image->get_name().is_empty()) {
-			WARN_PRINT(vformat("glTF: Image index '%d' couldn't be named. Skipping it.", p_index));
-			p_state->images.push_back(Ref<Texture2D>());
-			p_state->source_images.push_back(Ref<Image>());
+			WARN_PRINT("glTF: Couldn't extract image because the base path is empty. It will be loaded directly instead, uncompressed.");
 		} else {
+			if (p_image->get_name().is_empty()) {
+				WARN_PRINT(vformat("glTF: Image index '%d' did not have a name. It will be automatically given a name based on its index.", p_index));
+				p_image->set_name(itos(p_index));
+			}
 			bool must_import = true;
 			Vector<uint8_t> img_data = p_image->get_data();
 			Dictionary generator_parameters;
@@ -3559,14 +3558,11 @@ void GLTFDocument::_parse_image_save_image(Ref<GLTFState> p_state, const Vector<
 			if (saved_image.is_valid()) {
 				p_state->images.push_back(saved_image);
 				p_state->source_images.push_back(saved_image->get_image());
+				return;
 			} else {
-				WARN_PRINT(vformat("glTF: Image index '%d' couldn't be loaded with the name: %s. Skipping it.", p_index, p_image->get_name()));
-				// Placeholder to keep count.
-				p_state->images.push_back(Ref<Texture2D>());
-				p_state->source_images.push_back(Ref<Image>());
+				WARN_PRINT(vformat("glTF: Image index '%d' with the name '%s' couldn't be imported. It will be loaded directly instead, uncompressed.", p_index, p_image->get_name()));
 			}
 		}
-		return;
 	}
 #endif // TOOLS_ENABLED
 	if (handling == GLTFState::GLTFHandleBinary::HANDLE_BINARY_EMBED_AS_BASISU) {
@@ -3649,16 +3645,19 @@ Error GLTFDocument::_parse_images(Ref<GLTFState> p_state, const String &p_base_p
 				ERR_FAIL_COND_V(p_base_path.is_empty(), ERR_INVALID_PARAMETER);
 				uri = uri.uri_decode();
 				uri = p_base_path.path_join(uri).replace("\\", "/"); // Fix for Windows.
-				// 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.
-				// there could be a `.png` image which is actually JPEG), but there's no easy
-				// API for that in Godot, so we'd have to load as a buffer (i.e. embedded in
-				// the material), so we only do that only as fallback.
-				Ref<Texture2D> texture = ResourceLoader::load(uri);
-				if (texture.is_valid()) {
-					p_state->images.push_back(texture);
-					p_state->source_images.push_back(texture->get_image());
-					continue;
+				// If the image is in the .godot/imported directory, we can't use ResourceLoader.
+				if (!p_base_path.begins_with("res://.godot/imported")) {
+					// 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.
+					// there could be a `.png` image which is actually JPEG), but there's no easy
+					// API for that in Godot, so we'd have to load as a buffer (i.e. embedded in
+					// the material), so we only do that only as fallback.
+					Ref<Texture2D> texture = ResourceLoader::load(uri, "Texture2D");
+					if (texture.is_valid()) {
+						p_state->images.push_back(texture);
+						p_state->source_images.push_back(texture->get_image());
+						continue;
+					}
 				}
 				// mimeType is optional, but if we have it in the file extension, let's use it.
 				// If the mimeType does not match with the file extension, either it should be