Skip to content

Commit 821fecd

Browse files
committed
GLTF: Fix crash reading texture sampler for non-existent texture
1 parent e67074d commit 821fecd

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

modules/gltf/gltf_document.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,6 +4353,7 @@ GLTFTextureIndex GLTFDocument::_set_texture(Ref<GLTFState> p_state, Ref<Texture2
43534353
}
43544354

43554355
Ref<Texture2D> GLTFDocument::_get_texture(Ref<GLTFState> p_state, const GLTFTextureIndex p_texture, int p_texture_types) {
4356+
ERR_FAIL_COND_V_MSG(p_state->textures.is_empty(), Ref<Texture2D>(), "glTF import: Tried to read texture at index " + itos(p_texture) + ", but this glTF file does not contain any textures.");
43564357
ERR_FAIL_INDEX_V(p_texture, p_state->textures.size(), Ref<Texture2D>());
43574358
const GLTFImageIndex image = p_state->textures[p_texture]->get_src_image();
43584359
ERR_FAIL_INDEX_V(image, p_state->images.size(), Ref<Texture2D>());
@@ -4392,7 +4393,8 @@ GLTFTextureSamplerIndex GLTFDocument::_set_sampler_for_mode(Ref<GLTFState> p_sta
43924393
}
43934394

43944395
Ref<GLTFTextureSampler> GLTFDocument::_get_sampler_for_texture(Ref<GLTFState> p_state, const GLTFTextureIndex p_texture) {
4395-
ERR_FAIL_INDEX_V(p_texture, p_state->textures.size(), Ref<Texture2D>());
4396+
ERR_FAIL_COND_V_MSG(p_state->textures.is_empty(), Ref<GLTFTextureSampler>(), "glTF import: Tried to read sampler for texture at index " + itos(p_texture) + ", but this glTF file does not contain any textures.");
4397+
ERR_FAIL_INDEX_V(p_texture, p_state->textures.size(), Ref<GLTFTextureSampler>());
43964398
const GLTFTextureSamplerIndex sampler = p_state->textures[p_texture]->get_sampler();
43974399

43984400
if (sampler == -1) {
@@ -4850,10 +4852,13 @@ Error GLTFDocument::_parse_materials(Ref<GLTFState> p_state) {
48504852
if (mr.has("baseColorTexture")) {
48514853
const Dictionary &bct = mr["baseColorTexture"];
48524854
if (bct.has("index")) {
4853-
Ref<GLTFTextureSampler> bct_sampler = _get_sampler_for_texture(p_state, bct["index"]);
4854-
material->set_texture_filter(bct_sampler->get_filter_mode());
4855-
material->set_flag(BaseMaterial3D::FLAG_USE_TEXTURE_REPEAT, bct_sampler->get_wrap_mode());
4856-
material->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, _get_texture(p_state, bct["index"], TEXTURE_TYPE_GENERIC));
4855+
const GLTFTextureIndex base_color_texture_index = bct["index"];
4856+
material->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, _get_texture(p_state, base_color_texture_index, TEXTURE_TYPE_GENERIC));
4857+
const Ref<GLTFTextureSampler> bct_sampler = _get_sampler_for_texture(p_state, base_color_texture_index);
4858+
if (bct_sampler.is_valid()) {
4859+
material->set_texture_filter(bct_sampler->get_filter_mode());
4860+
material->set_flag(BaseMaterial3D::FLAG_USE_TEXTURE_REPEAT, bct_sampler->get_wrap_mode());
4861+
}
48574862
}
48584863
if (!mr.has("baseColorFactor")) {
48594864
material->set_albedo(Color(1, 1, 1));

0 commit comments

Comments
 (0)