diff --git a/scene/resources/compressed_texture.cpp b/scene/resources/compressed_texture.cpp index a641a2924c7..558bce21515 100644 --- a/scene/resources/compressed_texture.cpp +++ b/scene/resources/compressed_texture.cpp @@ -310,8 +310,6 @@ Ref CompressedTexture2D::load_image_from_file(Ref f, int p_si Vector> mipmap_images; uint64_t total_size = 0; - bool first = true; - for (uint32_t i = 0; i < mipmaps + 1; i++) { uint32_t size = f->get_32(); @@ -340,14 +338,17 @@ Ref CompressedTexture2D::load_image_from_file(Ref f, int p_si if (img.is_null() || img->is_empty()) { ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref()); } + // If the image is compressed and its format doesn't match the desired format, return an empty reference. + // This is done to avoid recompressing the image on load. + ERR_FAIL_COND_V(img->is_compressed() && format != img->get_format(), Ref()); - if (first) { - //format will actually be the format of the first image, - //as it may have changed on compression - format = img->get_format(); - first = false; - } else if (img->get_format() != format) { - img->convert(format); //all needs to be the same format + // The format will actually be the format of the header, + // as it may have changed on compression. + if (format != img->get_format()) { + // Convert the image to the desired format. + // Note: We are not decompressing the image here, just changing its format. + // It's important that all images in the texture array share the same format for correct rendering. + img->convert(format); } total_size += img->get_data().size();