Skip to content

Commit

Permalink
single threaded behavior for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Mar 27, 2021
1 parent 1a27958 commit f3c2724
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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::<Texture>(&label, LoadedAsset::new(texture));
load_context.set_labeled_asset(&label, LoadedAsset::new(texture));
});

let mut scenes = vec![];
Expand Down Expand Up @@ -305,6 +286,41 @@ async fn load_gltf<'a, 'b>(
Ok(())
}

async fn load_texture<'a>(
gltf_texture: gltf::Texture<'a>,
buffer_data: &Vec<Vec<u8>>,
linear_textures: &HashSet<usize>,
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<StandardMaterial> {
let material_label = material_label(&material);

Expand Down

0 comments on commit f3c2724

Please sign in to comment.