Skip to content

Commit 17e3efa

Browse files
authored
Fix mesh extraction for meshes without associated material. (#18631)
# Objective Fixes #17986 Fixes #18608 ## Solution Guard against situations where an extracted mesh does not have an associated material. The way that mesh is dependent on the material api (although decoupled) here is a bit unfortunate and we might consider ways in the future to support these material features without this indirect dependency.
1 parent 95b9117 commit 17e3efa

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

crates/bevy_pbr/src/meshlet/instance_manager.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{meshlet_mesh_manager::MeshletMeshManager, MeshletMesh, MeshletMesh3d};
22
use crate::{
3-
Material, MeshFlags, MeshTransforms, MeshUniform, NotShadowCaster, NotShadowReceiver,
4-
PreviousGlobalTransform, RenderMaterialBindings, RenderMaterialInstances,
5-
RenderMeshMaterialIds,
3+
Material, MaterialBindingId, MeshFlags, MeshTransforms, MeshUniform, NotShadowCaster,
4+
NotShadowReceiver, PreviousGlobalTransform, RenderMaterialBindings, RenderMaterialInstances,
5+
RenderMeshMaterialIds, StandardMaterial,
66
};
7-
use bevy_asset::{AssetEvent, AssetServer, Assets, UntypedAssetId};
7+
use bevy_asset::{AssetEvent, AssetId, AssetServer, Assets, UntypedAssetId};
88
use bevy_ecs::{
99
entity::{Entities, Entity, EntityHashMap},
1010
event::EventReader,
@@ -113,10 +113,16 @@ impl InstanceManager {
113113
};
114114

115115
let mesh_material = mesh_material_ids.mesh_material(instance);
116-
let mesh_material_binding_id = render_material_bindings
117-
.get(&mesh_material)
118-
.cloned()
119-
.unwrap_or_default();
116+
let mesh_material_binding_id =
117+
if mesh_material != AssetId::<StandardMaterial>::invalid().untyped() {
118+
render_material_bindings
119+
.get(&mesh_material)
120+
.cloned()
121+
.unwrap_or_default()
122+
} else {
123+
// Use a dummy binding ID if the mesh has no material
124+
MaterialBindingId::default()
125+
};
120126

121127
let mesh_uniform = MeshUniform::new(
122128
&transforms,

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,13 +1162,19 @@ impl RenderMeshInstanceGpuBuilder {
11621162
// yet loaded. In that case, add the mesh to
11631163
// `meshes_to_reextract_next_frame` and bail.
11641164
let mesh_material = mesh_material_ids.mesh_material(entity);
1165-
let mesh_material_binding_id = match render_material_bindings.get(&mesh_material) {
1166-
Some(binding_id) => *binding_id,
1167-
None => {
1168-
meshes_to_reextract_next_frame.insert(entity);
1169-
return None;
1170-
}
1171-
};
1165+
let mesh_material_binding_id =
1166+
if mesh_material != AssetId::<StandardMaterial>::invalid().untyped() {
1167+
match render_material_bindings.get(&mesh_material) {
1168+
Some(binding_id) => *binding_id,
1169+
None => {
1170+
meshes_to_reextract_next_frame.insert(entity);
1171+
return None;
1172+
}
1173+
}
1174+
} else {
1175+
// Use a dummy material binding ID.
1176+
MaterialBindingId::default()
1177+
};
11721178
self.shared.material_bindings_index = mesh_material_binding_id;
11731179

11741180
let lightmap_slot = match render_lightmaps.render_lightmaps.get(&entity) {

0 commit comments

Comments
 (0)