Skip to content

Commit 09917ea

Browse files
committed
only load materials once
1 parent 00c1f89 commit 09917ea

File tree

1 file changed

+3
-38
lines changed

1 file changed

+3
-38
lines changed

crates/bevy_gltf/src/loader.rs

+3-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use bevy_asset::{AssetIoError, AssetLoader, AssetPath, LoadContext, LoadedAsset};
2+
use bevy_asset::{AssetIoError, AssetLoader, AssetPath, Handle, LoadContext, LoadedAsset};
33
use bevy_ecs::{bevy_utils::BoxedFuture, World, WorldBuilderSource};
44
use bevy_math::Mat4;
55
use bevy_pbr::prelude::{PbrBundle, StandardMaterial};
@@ -83,38 +83,7 @@ async fn load_gltf<'a, 'b>(
8383
let mut materials = vec![];
8484
let mut named_materials = HashMap::new();
8585
for material in gltf.materials() {
86-
let material_label = material_label(&material);
87-
let pbr = material.pbr_metallic_roughness();
88-
let mut dependencies = Vec::new();
89-
let texture_handle = if let Some(info) = pbr.base_color_texture() {
90-
match info.texture().source().source() {
91-
gltf::image::Source::View { .. } => {
92-
let label = texture_label(&info.texture());
93-
let path = AssetPath::new_ref(load_context.path(), Some(&label));
94-
Some(load_context.get_handle(path))
95-
}
96-
gltf::image::Source::Uri { uri, .. } => {
97-
let parent = load_context.path().parent().unwrap();
98-
let image_path = parent.join(uri);
99-
let asset_path = AssetPath::new(image_path, None);
100-
let handle = load_context.get_handle(asset_path.clone());
101-
dependencies.push(asset_path);
102-
Some(handle)
103-
}
104-
}
105-
} else {
106-
None
107-
};
108-
let color = pbr.base_color_factor();
109-
let handle = load_context.set_labeled_asset(
110-
&material_label,
111-
LoadedAsset::new(StandardMaterial {
112-
albedo: Color::rgba(color[0], color[1], color[2], color[3]),
113-
albedo_texture: texture_handle,
114-
..Default::default()
115-
})
116-
.with_dependencies(dependencies),
117-
);
86+
let handle = load_material(&material, load_context);
11887
if let Some(name) = material.name() {
11988
named_materials.insert(name.to_string(), handle.clone());
12089
}
@@ -252,10 +221,6 @@ async fn load_gltf<'a, 'b>(
252221
}
253222
}
254223

255-
for material in gltf.materials() {
256-
load_material(&material, load_context);
257-
}
258-
259224
let mut scenes = vec![];
260225
let mut named_scenes = HashMap::new();
261226
for scene in gltf.scenes() {
@@ -303,7 +268,7 @@ async fn load_gltf<'a, 'b>(
303268
Ok(())
304269
}
305270

306-
fn load_material(material: &Material, load_context: &mut LoadContext) {
271+
fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle<StandardMaterial> {
307272
let material_label = material_label(&material);
308273
let pbr = material.pbr_metallic_roughness();
309274
let mut dependencies = Vec::new();

0 commit comments

Comments
 (0)