From f76430670c1b929917aaf87890cd5cc3018e60f7 Mon Sep 17 00:00:00 2001 From: RedMser Date: Fri, 31 Dec 2021 17:43:15 +0100 Subject: [PATCH] Fix glTF scene export crash on null normal texture Also removes a redundant get_texture call directly below the modified code block. Fixes #56379 (cherry picked from commit 0e36d5e782e9d5d69e38aa310174ab5c4b72174a) --- modules/gltf/gltf_document.cpp | 45 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 953211ecd15..7716faae795 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -3382,30 +3382,33 @@ Error GLTFDocument::_serialize_materials(Ref state) { tex.instance(); { Ref normal_texture = material->get_texture(SpatialMaterial::TEXTURE_NORMAL); - // Code for uncompressing RG normal maps - Ref img = normal_texture->get_data(); - Ref img_tex = img; - if (img_tex.is_valid()) { - img = img_tex->get_data(); - } - img->decompress(); - img->convert(Image::FORMAT_RGBA8); - img->lock(); - for (int32_t y = 0; y < img->get_height(); y++) { - for (int32_t x = 0; x < img->get_width(); x++) { - Color c = img->get_pixel(x, y); - Vector2 red_green = Vector2(c.r, c.g); - red_green = red_green * Vector2(2.0f, 2.0f) - Vector2(1.0f, 1.0f); - float blue = 1.0f - red_green.dot(red_green); - blue = MAX(0.0f, blue); - c.b = Math::sqrt(blue); - img->set_pixel(x, y, c); + if (normal_texture.is_valid()) { + // Code for uncompressing RG normal maps + Ref img = normal_texture->get_data(); + if (img.is_valid()) { + Ref img_tex = img; + if (img_tex.is_valid()) { + img = img_tex->get_data(); + } + img->decompress(); + img->convert(Image::FORMAT_RGBA8); + img->lock(); + for (int32_t y = 0; y < img->get_height(); y++) { + for (int32_t x = 0; x < img->get_width(); x++) { + Color c = img->get_pixel(x, y); + Vector2 red_green = Vector2(c.r, c.g); + red_green = red_green * Vector2(2.0f, 2.0f) - Vector2(1.0f, 1.0f); + float blue = 1.0f - red_green.dot(red_green); + blue = MAX(0.0f, blue); + c.b = Math::sqrt(blue); + img->set_pixel(x, y, c); + } + } + img->unlock(); + tex->create_from_image(img); } } - img->unlock(); - tex->create_from_image(img); } - Ref normal_texture = material->get_texture(SpatialMaterial::TEXTURE_NORMAL); GLTFTextureIndex gltf_texture_index = -1; if (tex.is_valid() && tex->get_data().is_valid()) { tex->set_name(material->get_name() + "_normal");