Fix glTF scene export crash on null normal texture

Also removes a redundant get_texture call directly below
the modified code block.

Fixes #56379
This commit is contained in:
RedMser 2021-12-31 17:43:15 +01:00
parent 91b97dac03
commit 0e36d5e782

View file

@ -3402,29 +3402,32 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
tex.instantiate(); tex.instantiate();
{ {
Ref<Texture2D> normal_texture = material->get_texture(BaseMaterial3D::TEXTURE_NORMAL); Ref<Texture2D> normal_texture = material->get_texture(BaseMaterial3D::TEXTURE_NORMAL);
// Code for uncompressing RG normal maps if (normal_texture.is_valid()) {
Ref<Image> img = normal_texture->get_image(); // Code for uncompressing RG normal maps
Ref<ImageTexture> img_tex = img; Ref<Image> img = normal_texture->get_image();
if (img_tex.is_valid()) { if (img.is_valid()) {
img = img_tex->get_image(); Ref<ImageTexture> img_tex = img;
} if (img_tex.is_valid()) {
img->decompress(); img = img_tex->get_image();
img->convert(Image::FORMAT_RGBA8); }
img->convert_ra_rgba8_to_rg(); img->decompress();
for (int32_t y = 0; y < img->get_height(); y++) { img->convert(Image::FORMAT_RGBA8);
for (int32_t x = 0; x < img->get_width(); x++) { img->convert_ra_rgba8_to_rg();
Color c = img->get_pixel(x, y); for (int32_t y = 0; y < img->get_height(); y++) {
Vector2 red_green = Vector2(c.r, c.g); for (int32_t x = 0; x < img->get_width(); x++) {
red_green = red_green * Vector2(2.0f, 2.0f) - Vector2(1.0f, 1.0f); Color c = img->get_pixel(x, y);
float blue = 1.0f - red_green.dot(red_green); Vector2 red_green = Vector2(c.r, c.g);
blue = MAX(0.0f, blue); red_green = red_green * Vector2(2.0f, 2.0f) - Vector2(1.0f, 1.0f);
c.b = Math::sqrt(blue); float blue = 1.0f - red_green.dot(red_green);
img->set_pixel(x, y, c); blue = MAX(0.0f, blue);
c.b = Math::sqrt(blue);
img->set_pixel(x, y, c);
}
}
tex->create_from_image(img);
} }
} }
tex->create_from_image(img);
} }
Ref<Texture2D> normal_texture = material->get_texture(BaseMaterial3D::TEXTURE_NORMAL);
GLTFTextureIndex gltf_texture_index = -1; GLTFTextureIndex gltf_texture_index = -1;
if (tex.is_valid() && tex->get_image().is_valid()) { if (tex.is_valid() && tex->get_image().is_valid()) {
tex->set_name(material->get_name() + "_normal"); tex->set_name(material->get_name() + "_normal");