From f3c272471dc9d55c8ba9c0a7dd511dbba61ecb2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 27 Mar 2021 01:10:28 +0100 Subject: [PATCH] single threaded behavior for wasm --- crates/bevy_gltf/src/loader.rs | 74 +++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 8a876eb4fb7c35..205f168e20c129 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -213,6 +213,14 @@ async fn load_gltf<'a, 'b>( }) .collect(); + #[cfg(target_arch = "wasm32")] + for gltf_texture in gltf.textures() { + let (texture, label) = + load_texture(gltf_texture, &buffer_data, &linear_textures, &load_context).await?; + load_context.set_labeled_asset(&label, LoadedAsset::new(texture)); + } + + #[cfg(not(target_arch = "wasm32"))] load_context .task_pool() .scope(|scope| { @@ -221,41 +229,14 @@ async fn load_gltf<'a, 'b>( let load_context: &LoadContext = load_context; let buffer_data = &buffer_data; scope.spawn(async move { - let mut texture = match gltf_texture.source().source() { - gltf::image::Source::View { view, mime_type } => { - let start = view.offset() as usize; - let end = (view.offset() + view.length()) as usize; - let buffer = &buffer_data[view.buffer().index()][start..end]; - Texture::from_buffer(buffer, ImageType::MimeType(mime_type))? - } - gltf::image::Source::Uri { uri, mime_type } => { - let parent = load_context.path().parent().unwrap(); - let image_path = parent.join(uri); - let bytes = load_context.read_asset_bytes(image_path.clone()).await?; - Texture::from_buffer( - &bytes, - mime_type - .map(|mt| ImageType::MimeType(mt)) - .unwrap_or_else(|| { - ImageType::Extension( - image_path.extension().unwrap().to_str().unwrap(), - ) - }), - )? - } - }; - texture.sampler = texture_sampler(&gltf_texture); - if (linear_textures).contains(&gltf_texture.index()) { - texture.format = TextureFormat::Rgba8Unorm; - } - Result::<_, GltfError>::Ok((texture, texture_label(&gltf_texture))) + load_texture(gltf_texture, &buffer_data, &linear_textures, &load_context).await }); }); }) .into_iter() .filter_map(|result| result.ok()) .for_each(|(texture, label)| { - load_context.set_labeled_asset::(&label, LoadedAsset::new(texture)); + load_context.set_labeled_asset(&label, LoadedAsset::new(texture)); }); let mut scenes = vec![]; @@ -305,6 +286,41 @@ async fn load_gltf<'a, 'b>( Ok(()) } +async fn load_texture<'a>( + gltf_texture: gltf::Texture<'a>, + buffer_data: &Vec>, + linear_textures: &HashSet, + load_context: &LoadContext<'a>, +) -> Result<(Texture, String), GltfError> { + let mut texture = match gltf_texture.source().source() { + gltf::image::Source::View { view, mime_type } => { + let start = view.offset() as usize; + let end = (view.offset() + view.length()) as usize; + let buffer = &buffer_data[view.buffer().index()][start..end]; + Texture::from_buffer(buffer, ImageType::MimeType(mime_type))? + } + gltf::image::Source::Uri { uri, mime_type } => { + let parent = load_context.path().parent().unwrap(); + let image_path = parent.join(uri); + let bytes = load_context.read_asset_bytes(image_path.clone()).await?; + Texture::from_buffer( + &bytes, + mime_type + .map(|mt| ImageType::MimeType(mt)) + .unwrap_or_else(|| { + ImageType::Extension(image_path.extension().unwrap().to_str().unwrap()) + }), + )? + } + }; + texture.sampler = texture_sampler(&gltf_texture); + if (linear_textures).contains(&gltf_texture.index()) { + texture.format = TextureFormat::Rgba8Unorm; + } + + Ok((texture, texture_label(&gltf_texture))) +} + fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle { let material_label = material_label(&material);