Skip to content

Commit 3ab5c2c

Browse files
GltfExtensionHandler::on_texture: pass Texture references (#22131)
# Objective extension hooks for `on_texture` currently pass the extension data and not the gltf object. ## Solution `IoTaskPool::scope` preserves ordering if the futures are only spawned from the root scope, so we can use that to zip with the texture references for hooks
1 parent 5ac16eb commit 3ab5c2c

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,11 @@ pub trait GltfExtensionHandler: Send + Sync {
9999
}
100100

101101
/// Called when an individual texture is processed
102-
/// Unlike other hooks, this hook does not receive its glTF
103-
/// object due to internal constraints.
104102
#[expect(
105103
unused,
106104
reason = "default trait implementations do not use the arguments because they are no-ops"
107105
)]
108-
fn on_texture(
109-
&mut self,
110-
extension_data: Option<&serde_json::Map<String, serde_json::Value>>,
111-
texture: Handle<bevy_image::Image>,
112-
) {
113-
}
106+
fn on_texture(&mut self, gltf_texture: &gltf::Texture, texture: Handle<bevy_image::Image>) {}
114107

115108
/// Called when an individual material is processed
116109
#[expect(

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,7 @@ impl GltfLoader {
644644
image.process_loaded_texture(load_context, &mut texture_handles);
645645
// let extensions handle texture data
646646
for extension in extensions.iter_mut() {
647-
extension.on_texture(
648-
texture.extensions(),
649-
texture_handles.last().unwrap().clone(),
650-
);
647+
extension.on_texture(&texture, texture_handles.last().unwrap().clone());
651648
}
652649
}
653650
} else {
@@ -658,9 +655,8 @@ impl GltfLoader {
658655
let asset_path = load_context.path().clone();
659656
let linear_textures = &linear_textures;
660657
let buffer_data = &buffer_data;
661-
let extension_data = gltf_texture.extensions().map(ToOwned::to_owned);
662658
scope.spawn(async move {
663-
let result = load_image(
659+
load_image(
664660
gltf_texture,
665661
buffer_data,
666662
linear_textures,
@@ -669,23 +665,19 @@ impl GltfLoader {
669665
default_sampler,
670666
settings,
671667
)
672-
.await;
673-
(extension_data, result)
668+
.await
674669
});
675670
});
676671
})
677672
.into_iter()
678-
.for_each(|(extension_data, result)| match result {
673+
// order is preserved if the futures are only spawned from the root scope
674+
.zip(gltf.textures())
675+
.for_each(|(result, texture)| match result {
679676
Ok(image) => {
680677
image.process_loaded_texture(load_context, &mut texture_handles);
681678
// let extensions handle texture data
682-
// We do this differently here because of the IoTaskPool vs
683-
// gltf::Texture lifetimes
684679
for extension in extensions.iter_mut() {
685-
extension.on_texture(
686-
extension_data.as_ref(),
687-
texture_handles.last().unwrap().clone(),
688-
);
680+
extension.on_texture(&texture, texture_handles.last().unwrap().clone());
689681
}
690682
}
691683
Err(err) => {

0 commit comments

Comments
 (0)