# graphics3d/multilevelbloomeffect (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/graphics3d/multilevelbloomeffect/

Multi-level bloom effect. (the original graphics3d/multilevelbloomeffect.js)
- Extracts the bright part at half size, blurs it while halving the size, then adds it back to the level above while doubling the size. The first level (half the scene size) is the result. (get_result_texture)
- Extraction: from the average of four source samples, the part whose brightness (the largest of the three channels) exceeds the threshold. Within the knee width around the threshold it comes in smoothly. (set_threshold(threshold, knee))
- Downsampling is a dual filter mixing the center four and the four corners (one diagonal step), and upsampling is a nine-tap tent. The shaders match the original character for character.
- With use_float_color the targets are RGBA16F, so bright values above 1 are blurred as they are. (for HDR scenes, with RGBA8 they are clipped at 1)
- Usage: resize(gl, scene width, height) → render(gl, scene texture) → get_result_texture() is multiplied by the strength and added in the composite pass.
- Side effects: disables the depth test and blending, makes TEXTURE0 active, and changes the framebuffer binding. The blend function is restored to the previous one.

## MultiLevelBloomEffect (struct)

Multi-level bloom effect.

Example: Usage from the header comment: wire resize, render and get_result_texture into a composite pass

```rust
use vanilla::{MultiLevelBloomEffect, RenderTarget};

fn create_bloom(gl: &vanilla::GlContext) -> Result<MultiLevelBloomEffect, String> {
	let mut multi_level_bloom_effect = MultiLevelBloomEffect::new(gl, 5, true)?;
	multi_level_bloom_effect.set_threshold(1.0, 0.25);
	Ok(multi_level_bloom_effect)
}

fn render_bloom(gl: &vanilla::GlContext, multi_level_bloom_effect: &mut MultiLevelBloomEffect, scene_render_target: &RenderTarget) -> Result<(), String> {
	multi_level_bloom_effect.resize(gl, scene_render_target.get_width(), scene_render_target.get_height())?;
	multi_level_bloom_effect.render(gl, scene_render_target.get_color_texture(0));
	unsafe {
		gl.active_texture(glow::TEXTURE1);
		gl.bind_texture(glow::TEXTURE_2D, multi_level_bloom_effect.get_result_texture());
		gl.active_texture(glow::TEXTURE0);
	}
	Ok(())
}
```

- `pub fn new(gl: &GlContext, level_count: usize, use_float_color: bool) -> Result<MultiLevelBloomEffect, String>` (constructor, since 0.4.0): Constructor. (level count, whether to use float targets, the original defaults are 5 and true)
  - The targets are built at a scene size of 2 x 2, so give the scene size with resize before drawing.
  - `gl` &GlContext: GL context. (taken out with Graphic::get_gl)
  - `level_count` usize: Level count. (1 if 0, each level is half size)
  - `use_float_color` bool: Whether to build the targets as RGBA16F. (RGBA8 when false, so clipped at 1)
  - returns Result<MultiLevelBloomEffect, String>: New multi-level bloom. (threshold 1, knee 0.25) Err(error message) if shader compilation or target creation fails.
- `pub fn resize(&mut self, gl: &GlContext, scene_width: i32, scene_height: i32) -> Result<(), String>` (method, since 0.4.0): Resizes. (based on the scene resolution, unchanged if the same)
  - `gl` &GlContext: GL context.
  - `scene_width` i32: Scene width. (pixels, 1 if below 1, the first level is half of it)
  - `scene_height` i32: Scene height. (pixels, 1 if below 1, the first level is half of it)
  - returns Result<(), String>: Ok if the targets were fitted. Err(GL error message) if a target cannot be made while rebuilding.
- `pub fn set_level_count(&mut self, gl: &GlContext, level_count: usize) -> Result<(), String>` (method, since 0.4.0): Sets the level count. (1 or more, unchanged if the same)
  - `gl` &GlContext: GL context.
  - `level_count` usize: Level count. (1 if 0)
  - returns Result<(), String>: Ok if the targets were fitted. Err(GL error message) if a target cannot be made while rebuilding.
- `pub fn get_level_count(&self) -> usize` (method, since 0.4.0): Returns the level count.
  - returns usize: Level count. (1 or more)
- `pub fn get_use_float_color(&self) -> bool` (method, since 0.4.0): Returns whether float targets are used.
  - returns bool: true if the buffers are RGBA16F, false if RGBA8.
- `pub fn set_threshold(&mut self, threshold: f64, knee: f64)` (method, since 0.4.0): Sets the extraction threshold. (brightness, knee is the smooth width around the threshold, 0 means a hard cut)
  - `threshold` f64: Extraction threshold. (brightness measured as the largest of the three channels, 0 if below 0, default 1)
  - `knee` f64: Soft width around the threshold. (below 0 becomes 0, default 0.25)
- `pub fn render(&self, gl: &GlContext, scene_color_texture: Option<GlTexture>)` (method, since 0.4.0): Renders the bloom. (after the call, the result is get_result_texture())
  - Does nothing if there are no targets (after destroy).
  - `gl` &GlContext: GL context.
  - `scene_color_texture` Option<GlTexture>: Scene color texture. (its size must match the scene size given to resize so the extraction texel spacing is right)
- `pub fn get_result_texture(&self) -> Option<GlTexture>` (method, since 0.4.0): Returns the result texture. (after render(), the first level)
  - returns Option<GlTexture>: Color texture of the first level (half the scene size). None after destroy.
- `pub fn destroy(&mut self, gl: &GlContext)` (method, since 0.4.0): Destroy. (Releases GL resources)
  - `gl` &GlContext: GL context used at creation.
