diff --git a/Cargo.toml b/Cargo.toml index 0e1b5d9f33..7698dc2ec8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -274,7 +274,7 @@ path = "examples/2d/bloom_2d.rs" name = "2D Bloom" description = "Illustrates bloom post-processing in 2d" category = "2D Rendering" -wasm = false +wasm = true [[example]] name = "move_sprite" @@ -535,7 +535,7 @@ path = "examples/3d/bloom_3d.rs" name = "3D Bloom" description = "Illustrates bloom configuration using HDR and emissive materials" category = "3D Rendering" -wasm = false +wasm = true [[example]] name = "load_gltf" diff --git a/crates/bevy_core_pipeline/src/bloom/mod.rs b/crates/bevy_core_pipeline/src/bloom/mod.rs index 7157bc449e..3c41ca716e 100644 --- a/crates/bevy_core_pipeline/src/bloom/mod.rs +++ b/crates/bevy_core_pipeline/src/bloom/mod.rs @@ -302,11 +302,16 @@ impl ViewNode for BloomNode { #[derive(Component)] struct BloomTexture { // First mip is half the screen resolution, successive mips are half the previous + #[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))] texture: CachedTexture, + // WebGL does not support binding specific mip levels for sampling, fallback to separate textures instead + #[cfg(all(feature = "webgl", target_arch = "wasm32"))] + texture: Vec, mip_count: u32, } impl BloomTexture { + #[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))] fn view(&self, base_mip_level: u32) -> TextureView { self.texture.texture.create_view(&TextureViewDescriptor { base_mip_level, @@ -314,6 +319,16 @@ impl BloomTexture { ..Default::default() }) } + #[cfg(all(feature = "webgl", target_arch = "wasm32"))] + fn view(&self, base_mip_level: u32) -> TextureView { + self.texture[base_mip_level as usize] + .texture + .create_view(&TextureViewDescriptor { + base_mip_level: 0, + mip_level_count: Some(1u32), + ..Default::default() + }) + } } fn prepare_bloom_textures( @@ -347,10 +362,29 @@ fn prepare_bloom_textures( view_formats: &[], }; - commands.entity(entity).insert(BloomTexture { - texture: texture_cache.get(&render_device, texture_descriptor), - mip_count, - }); + #[cfg(any(not(feature = "webgl"), not(target_arch = "wasm32")))] + let texture = texture_cache.get(&render_device, texture_descriptor); + #[cfg(all(feature = "webgl", target_arch = "wasm32"))] + let texture: Vec = (0..mip_count) + .map(|mip| { + texture_cache.get( + &render_device, + TextureDescriptor { + size: Extent3d { + width: (texture_descriptor.size.width >> mip).max(1), + height: (texture_descriptor.size.height >> mip).max(1), + depth_or_array_layers: 1, + }, + mip_level_count: 1, + ..texture_descriptor.clone() + }, + ) + }) + .collect(); + + commands + .entity(entity) + .insert(BloomTexture { texture, mip_count }); } } }