From 6785904a94fb96d1e24d1ee836cda38b3b25953b Mon Sep 17 00:00:00 2001 From: Christopher Biscardi Date: Wed, 13 Jul 2022 04:11:44 -0700 Subject: [PATCH 1/2] Improve StandardMaterial normal_map error message This unwrap() in pbr_material.rs will be hit if the normal_map image has not finished loading, resulting in an error message that is hard to debug. This PR improves the error message including a potential indication of why the unwrap() could have panic'd. --- crates/bevy_pbr/src/pbr_material.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index 3da72ab12a163..3868aac5ae524 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -192,7 +192,7 @@ impl AsBindGroupShaderType for StandardMaterial { if has_normal_map { match images .get(self.normal_map_texture.as_ref().unwrap()) - .unwrap() + .expect("expect normal map asset to have finished loading already") .texture_format { // All 2-component unorm formats From f311cbb575ce2b12a4ff314e047c9463bb34de5a Mon Sep 17 00:00:00 2001 From: Christopher Biscardi Date: Wed, 13 Jul 2022 08:11:10 -0700 Subject: [PATCH 2/2] dont panic if texture isnt loaded yet --- crates/bevy_pbr/src/pbr_material.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index 3868aac5ae524..a5439426e9fa9 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -190,19 +190,17 @@ impl AsBindGroupShaderType for StandardMaterial { } let has_normal_map = self.normal_map_texture.is_some(); if has_normal_map { - match images - .get(self.normal_map_texture.as_ref().unwrap()) - .expect("expect normal map asset to have finished loading already") - .texture_format - { - // All 2-component unorm formats - TextureFormat::Rg8Unorm - | TextureFormat::Rg16Unorm - | TextureFormat::Bc5RgUnorm - | TextureFormat::EacRg11Unorm => { - flags |= StandardMaterialFlags::TWO_COMPONENT_NORMAL_MAP; + if let Some(texture) = images.get(self.normal_map_texture.as_ref().unwrap()) { + match texture.texture_format { + // All 2-component unorm formats + TextureFormat::Rg8Unorm + | TextureFormat::Rg16Unorm + | TextureFormat::Bc5RgUnorm + | TextureFormat::EacRg11Unorm => { + flags |= StandardMaterialFlags::TWO_COMPONENT_NORMAL_MAP; + } + _ => {} } - _ => {} } if self.flip_normal_map_y { flags |= StandardMaterialFlags::FLIP_NORMAL_MAP_Y;