# graphics3d/bloomeffect (vanilla-engine 0.4.0)

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

Bloom effect. (the original graphics3d/bloomeffect.js)
- Bright area extraction → half resolution Gaussian blur ping-pong. The render() result texture is used by adding it in the final composite pass.
- Shaders, pass order and defaults (threshold 0.5 ~ 0.95, iterations 2) are the same as the original.
- The exchanged buffers are RGBA8 by default, so bright values above 1 are clipped. With set_use_float_color(true) they are built as RGBA16F and HDR values are blurred as they are.
- Functions that use GL take gl, and creation and resizing return an error (Result) if GL objects cannot be made.

## BloomEffect (struct)

Bloom effect.

Example: Make bloom from a scene render target and bind the result to unit 1 of the composite pass

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

fn create_bloom(gl: &vanilla::GlContext) -> Result<BloomEffect, String> {
	let mut bloom_effect = BloomEffect::new(gl)?;
	bloom_effect.set_threshold(0.7, 0.95);
	Ok(bloom_effect)
}

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

- `pub fn new(gl: &GlContext) -> Result<BloomEffect, String>` (constructor, since 0.3.2): Creates. (2 passes + 2 2x2 render targets, sized with resize)
  - `gl` &GlContext: GL context. (taken out with Graphic::get_gl)
  - returns Result<BloomEffect, String>: New bloom effect. (threshold 0.5 ~ 0.95, iterations 2, RGBA8 buffers) Err(error text) if shader compilation or GL object creation fails.
- `pub fn resize(&mut self, gl: &GlContext, scene_width: i32, scene_height: i32) -> Result<(), String>` (method, since 0.3.2): Resize. (Based on the scene resolution, internally half resolution)
  - `gl` &GlContext: GL context.
  - `scene_width` i32: Scene width. (pixels, the buffers are half of it, at least 1)
  - `scene_height` i32: Scene height. (pixels, the buffers are half of it, at least 1)
  - returns Result<(), String>: Ok if the buffers were fitted. Err(GL error text) if GL objects cannot be made while rebuilding.
- `pub fn render(&self, gl: &GlContext, scene_color_texture: Option<GlTexture>)` (method, since 0.3.2): Renders bloom. (the result is get_result_texture() after the call)
  - Side effects: depth/blend disabled, TEXTURE0 active, the framebuffer binding changes.
  - `gl` &GlContext: GL context.
  - `scene_color_texture` Option<GlTexture>: Scene color texture to extract bright areas from. (usually get_color_texture(0) of the scene RenderTarget)
- `pub fn get_result_texture(&self) -> Option<GlTexture>` (method, since 0.3.2): Returns the result texture. (after render())
  - returns Option<GlTexture>: Texture after blurring. (color of the ping buffer, half the scene size) None after destroy.
- `pub fn set_threshold(&mut self, threshold_start: f64, threshold_end: f64)` (method, since 0.3.2): Sets the extraction threshold. (Adjust to the scene's brightness characteristics)
  - `threshold_start` f64: Brightness where extraction starts. (the largest of the three channels, darker than this is not extracted, default 0.5)
  - `threshold_end` f64: Brightness of full extraction. (brighter than this is kept as is, smoothstep in between, default 0.95)
- `pub fn set_iteration_count(&mut self, iteration_count: u32)` (method, since 0.3.2): Sets the blur iteration count.
  - `iteration_count` u32: Number of times to repeat a pair of horizontal and vertical blurs. (default 2, 0 only extracts bright areas)
- `pub fn set_use_float_color(&mut self, gl: &GlContext, use_float_color: bool) -> Result<(), String>` (method, since 0.4.0): Sets float color. (If true, rebuilds the ping-pong buffers as RGBA16F so bright values above 1 are not clipped, default false is RGBA8, unchanged if the same)
  - `gl` &GlContext: GL context.
  - `use_float_color` bool: RGBA16F if true, RGBA8 if false.
  - returns Result<(), String>: Ok if changed or already the same. Err(GL error text, the previous buffers are kept) if new buffers cannot be built.

  Example: Blur light above 1 in an HDR scene without clipping

  ```rust
  use vanilla::BloomEffect;
  
  fn create_hdr_bloom(gl: &vanilla::GlContext) -> Result<BloomEffect, String> {
  	let mut bloom_effect = BloomEffect::new(gl)?;
  	bloom_effect.set_use_float_color(gl, true)?;
  	bloom_effect.set_threshold(1.0, 1.5);
  	assert!(bloom_effect.get_use_float_color());
  	Ok(bloom_effect)
  }
  ```

- `pub fn get_use_float_color(&self) -> bool` (method, since 0.4.0): Returns whether float color is used.
  - returns bool: true if the buffers are RGBA16F, false if RGBA8.
- `pub fn get_iteration_count(&self) -> u32` (method, since 0.3.2): Returns the blur iteration count.
  - returns u32: Blur iteration count.
- `pub fn destroy(&mut self, gl: &GlContext)` (method, since 0.3.2): Destroy. (Releases GL resources)
  - `gl` &GlContext: GL context used at creation.
- `pub fn get_bright_pass(&self) -> &FullscreenPass` (method, since 0.3.2): Returns the bright area extraction pass.
  - returns &FullscreenPass: Bright area extraction pass. (uniforms sceneTexture, thresholdRange)
- `pub fn get_blur_pass(&self) -> &FullscreenPass` (method, since 0.3.2): Returns the blur pass.
  - returns &FullscreenPass: 9 tap Gaussian blur pass. (uniforms sourceTexture, blurDirection)
- `pub fn get_ping_render_target(&self) -> &RenderTarget` (method, since 0.3.2): Returns the ping render target. (holds the final result)
  - returns &RenderTarget: Ping render target. (the buffer receiving the bright areas and the vertical blur, owner of the result texture)
- `pub fn get_pong_render_target(&self) -> &RenderTarget` (method, since 0.3.2): Returns the pong render target.
  - returns &RenderTarget: Pong render target. (the buffer receiving the horizontal blur)
