From 601a9eb4f58ffc95ec9cc34fc5384fce2f5d3f91 Mon Sep 17 00:00:00 2001 From: pablitar Date: Mon, 22 Jul 2024 13:51:53 -0300 Subject: [PATCH] GLTF: Fixed external images getting embedded on import Added a map to keep track of external images during import, and used that map to instance the textures using the resource loader instead of creating a new texture from scratch --- modules/gltf/gltf_document.cpp | 25 +++++++++++++++++-------- modules/gltf/gltf_state.h | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 4f2d0db35956..f48ea0b19083 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -3106,6 +3106,7 @@ Error GLTFDocument::_parse_images(Ref p_state, const String &p_base_p Ref texture = ResourceLoader::load(uri); if (texture.is_valid()) { p_state->images.push_back(texture->get_data()); + p_state->external_images_paths.insert(i, uri); continue; } else if (mimetype == "image/png" || mimetype == "image/jpeg") { // Fallback to loading as byte array. @@ -3234,17 +3235,25 @@ Error GLTFDocument::_parse_textures(Ref p_state) { } p_state->textures.push_back(t); + Ref tex; + // Create and cache the texture used in the engine - Ref imgTex; - imgTex.instance(); - imgTex->create_from_image(p_state->images[t->get_src_image()]); + if (p_state->external_images_paths.has(t->get_src_image())) { + tex = ResourceLoader::load(p_state->external_images_paths[t->get_src_image()]); + } else { + Ref img_tex; + img_tex.instance(); + img_tex->create_from_image(p_state->images[t->get_src_image()]); - // Set texture filter and repeat based on sampler settings - const Ref sampler = _get_sampler_for_texture(p_state, i); - Texture::Flags flags = sampler->get_texture_flags(); - imgTex->set_flags(flags); + // Set texture filter and repeat based on sampler settings. Only supported for embedded textures + const Ref sampler = _get_sampler_for_texture(p_state, i); + Texture::Flags flags = sampler->get_texture_flags(); + img_tex->set_flags(flags); + + tex = img_tex; + } - p_state->texture_cache.insert(i, imgTex); + p_state->texture_cache.insert(i, tex); } return OK; diff --git a/modules/gltf/gltf_state.h b/modules/gltf/gltf_state.h index 41ce216384d3..d1ec5a2533c3 100644 --- a/modules/gltf/gltf_state.h +++ b/modules/gltf/gltf_state.h @@ -78,6 +78,7 @@ class GLTFState : public Resource { Vector> texture_samplers; Ref default_texture_sampler; Vector> images; + Map external_images_paths; Map> texture_cache; Vector extensions_used; Vector extensions_required;