Skip to content

Commit

Permalink
Fix Texture3D import not working
Browse files Browse the repository at this point in the history
Fix Texture3D import not working when texture has fully opaque and (partly or fully) transparent slices.

For better style make cases of detect_alpha check against the exact enums rather than the enum 0.
  • Loading branch information
fire committed Dec 10, 2024
1 parent aa8d9b8 commit 1e342cf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion drivers/png/png_driver_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Error image_to_png(const Ref<Image> &p_image, Vector<uint8_t> &p_buffer) {
png_img.format = PNG_FORMAT_RGBA;
break;
default:
if (source_image->detect_alpha()) {
if (source_image->detect_alpha() != Image::ALPHA_NONE) {
source_image->convert(Image::FORMAT_RGBA8);
png_img.format = PNG_FORMAT_RGBA;
} else {
Expand Down
3 changes: 2 additions & 1 deletion editor/import/resource_importer_layered_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Error ResourceImporterLayeredTexture::import(ResourceUID::ID p_source_id, const
//if using video ram, optimize
if (channel_pack == 0) {
//remove alpha if not needed, so compression is more efficient
if (image->get_format() == Image::FORMAT_RGBA8 && !image->detect_alpha()) {
if (image->get_format() == Image::FORMAT_RGBA8 && image->detect_alpha() == Image::ALPHA_NONE) {
image->convert(Image::FORMAT_RGB8);
}
} else if (image->get_format() < Image::FORMAT_RGBA8) {
Expand Down Expand Up @@ -487,6 +487,7 @@ ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {
void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source_file, Ref<LayeredTextureImport> r_texture_import) {
String extension = get_save_extension();
ERR_FAIL_NULL(r_texture_import->csource);

if (r_texture_import->compress_mode != COMPRESS_VRAM_COMPRESSED) {
// Import normally.
_save_tex(*r_texture_import->slices, r_texture_import->save_path + "." + extension, r_texture_import->compress_mode, r_texture_import->lossy, Image::COMPRESS_S3TC /* IGNORED */, *r_texture_import->csource, r_texture_import->used_channels, r_texture_import->mipmaps, false);
Expand Down
2 changes: 1 addition & 1 deletion modules/webp/webp_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Vector<uint8_t> _webp_packer(const Ref<Image> &p_image, float p_quality, bool p_
Error error = img->decompress();
ERR_FAIL_COND_V_MSG(error != OK, Vector<uint8_t>(), "Couldn't decompress image.");
}
if (img->detect_alpha()) {
if (img->detect_alpha() != Image::ALPHA_NONE) {
img->convert(Image::FORMAT_RGBA8);
} else {
img->convert(Image::FORMAT_RGB8);
Expand Down
22 changes: 11 additions & 11 deletions scene/resources/compressed_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,6 @@ Ref<Image> CompressedTexture2D::load_image_from_file(Ref<FileAccess> f, int p_si
Vector<Ref<Image>> 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();

Expand All @@ -339,18 +337,20 @@ Ref<Image> CompressedTexture2D::load_image_from_file(Ref<FileAccess> f, int p_si
} else if (data_format == DATA_FORMAT_WEBP && Image::webp_unpacker) {
img = Image::webp_unpacker(pv);
}

if (img.is_null() || img->is_empty()) {
ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
}

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
// 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<Image>());

// The format will actually be the format of the header,
// as it may have changed on compression.
if (!img->is_compressed() && 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();
Expand Down

0 comments on commit 1e342cf

Please sign in to comment.