Skip to content

Commit d4f8f88

Browse files
Don't panic when StandardMaterial normal_map hasn't loaded yet (#5307)
# Objective [This unwrap()](https://github.com/bevyengine/bevy/blob/de484c1e4147d01bf34c88a10797b75128a0d98a/crates/bevy_pbr/src/pbr_material.rs#L195) in pbr_material.rs will be hit if a StandardMaterial normal_map image has not finished loading, resulting in an error message that is hard to debug. ## Solution ~~This PR improves the error message including a potential indication of why the unwrap() could have panic'd by using expect() instead of unwrap().~~ This PR removes the panic by only proceeding if the image is found. --- ## Changelog Don't panic when StandardMaterial normal_map images have not finished loading.
1 parent f531a94 commit d4f8f88

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,17 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
190190
}
191191
let has_normal_map = self.normal_map_texture.is_some();
192192
if has_normal_map {
193-
match images
194-
.get(self.normal_map_texture.as_ref().unwrap())
195-
.unwrap()
196-
.texture_format
197-
{
198-
// All 2-component unorm formats
199-
TextureFormat::Rg8Unorm
200-
| TextureFormat::Rg16Unorm
201-
| TextureFormat::Bc5RgUnorm
202-
| TextureFormat::EacRg11Unorm => {
203-
flags |= StandardMaterialFlags::TWO_COMPONENT_NORMAL_MAP;
193+
if let Some(texture) = images.get(self.normal_map_texture.as_ref().unwrap()) {
194+
match texture.texture_format {
195+
// All 2-component unorm formats
196+
TextureFormat::Rg8Unorm
197+
| TextureFormat::Rg16Unorm
198+
| TextureFormat::Bc5RgUnorm
199+
| TextureFormat::EacRg11Unorm => {
200+
flags |= StandardMaterialFlags::TWO_COMPONENT_NORMAL_MAP;
201+
}
202+
_ => {}
204203
}
205-
_ => {}
206204
}
207205
if self.flip_normal_map_y {
208206
flags |= StandardMaterialFlags::FLIP_NORMAL_MAP_Y;

0 commit comments

Comments
 (0)