Don't panic when StandardMaterial normal_map hasn't loaded yet (#5307)

# Objective

[This unwrap()](de484c1e41/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.
This commit is contained in:
Christopher Biscardi 2022-07-16 21:50:19 +00:00
parent f531a94370
commit d4f8f88bb6

View file

@ -190,19 +190,17 @@ impl AsBindGroupShaderType<StandardMaterialUniform> 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())
.unwrap()
.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;