Skip to content

Commit 9c6415c

Browse files
remove Debug from material strings and add LoadContext to GltfExtensi… (#22844)
# Objective #22569 included some regressions. At least some of them are due to the debug formatting in the asset names. This results in asset names like: `"DefaultMaterial"#std`. ## Solution Remove the debug formatting. Additionally, enable the initialization of resources like the `StandardMaterial` built from a `GltfMaterial::default()` so that the asset loader will have access to them. Materials like this default material that don't exist will not call the `on_material` hook, and thus not be initialized to be referenced later. ## Testing multi_asset_sync and hot_reloading are fixed by this. <img width="3776" height="2096" alt="screenshot-2026-02-06-at-19 11 31@2x" src="https://github.com/user-attachments/assets/07a31bf9-4013-4e80-a285-9b078eb28875" /> <img width="3776" height="2096" alt="screenshot-2026-02-06-at-19 16 42@2x" src="https://github.com/user-attachments/assets/5f1c1848-9469-4108-9600-5115d71d7e4c" /> but anisotropy still looks wrong, so this is a fix for one issue and there appears to be more to do. <img width="3776" height="2096" alt="screenshot-2026-02-06-at-19 17 20@2x" src="https://github.com/user-attachments/assets/70bd1fe2-39e4-439d-a88e-e34b39aa06a8" />
1 parent b43966b commit 9c6415c

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

crates/bevy_gltf/src/loader/extensions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub trait GltfExtensionHandler: Send + Sync {
6969
unused,
7070
reason = "default trait implementations do not use the arguments because they are no-ops"
7171
)]
72-
fn on_root(&mut self, gltf: &gltf::Gltf) {}
72+
fn on_root(&mut self, load_context: &mut LoadContext<'_>, gltf: &gltf::Gltf) {}
7373

7474
#[cfg(feature = "bevy_animation")]
7575
#[expect(

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl GltfLoader {
249249
// Let extensions process the root data for the extension ids
250250
// they've subscribed to.
251251
for extension in extensions.iter_mut() {
252-
extension.on_root(&gltf);
252+
extension.on_root(load_context, &gltf);
253253
}
254254

255255
let file_name = load_context

crates/bevy_pbr/src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ mod atmosphere;
2828
mod cluster;
2929
mod components;
3030
pub mod contact_shadows;
31-
use bevy_gltf::extensions::{GltfExtensionHandler, GltfExtensionHandlers};
31+
use bevy_gltf::{
32+
extensions::{GltfExtensionHandler, GltfExtensionHandlers},
33+
GltfAssetLabel,
34+
};
3235
use bevy_render::sync_component::SyncComponent;
3336
pub use contact_shadows::{
3437
ContactShadows, ContactShadowsBuffer, ContactShadowsPlugin, ContactShadowsUniform,
@@ -453,6 +456,16 @@ impl GltfExtensionHandler for GltfExtensionHandlerPbr {
453456
fn dyn_clone(&self) -> Box<dyn GltfExtensionHandler> {
454457
Box::new((*self).clone())
455458
}
459+
fn on_root(&mut self, load_context: &mut LoadContext<'_>, _gltf: &gltf::Gltf) {
460+
// create the `StandardMaterial` for the glTF `DefaultMaterial` so
461+
// it can be accessed when meshes don't have materials.
462+
let std_label = format!("{}#std", GltfAssetLabel::DefaultMaterial);
463+
464+
load_context.add_labeled_asset(
465+
std_label,
466+
standard_material_from_gltf_material(&GltfMaterial::default()),
467+
);
468+
}
456469

457470
fn on_material(
458471
&mut self,
@@ -462,11 +475,12 @@ impl GltfExtensionHandler for GltfExtensionHandlerPbr {
462475
material_asset: &GltfMaterial,
463476
material_label: &str,
464477
) {
465-
let std_label = format!("{:?}#std", material_label);
478+
let std_label = format!("{}#std", material_label);
466479

467-
let _t = load_context.labeled_asset_scope::<_, ()>(std_label, |_load_context| {
468-
Ok(standard_material_from_gltf_material(material_asset))
469-
});
480+
load_context.add_labeled_asset(
481+
std_label,
482+
standard_material_from_gltf_material(material_asset),
483+
);
470484
}
471485

472486
fn on_spawn_mesh_and_material(
@@ -478,7 +492,7 @@ impl GltfExtensionHandler for GltfExtensionHandlerPbr {
478492
entity: &mut EntityWorldMut,
479493
material_label: &str,
480494
) {
481-
let std_label = format!("{:?}#std", material_label);
495+
let std_label = format!("{}#std", material_label);
482496
let handle = load_context.get_label_handle::<StandardMaterial>(std_label);
483497

484498
entity.insert(MeshMaterial3d(handle));

0 commit comments

Comments
 (0)