# graphics3d/bloomeffect (vanilla.js 0.4.0)

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

Bloom effect. (bright area extraction → half resolution Gaussian blur ping-pong)

## BloomEffect extends Object

Bloom effect. (bright area extraction → half resolution Gaussian blur ping-pong)
- Used by additively blending the render() result texture in the final composite pass.
- The ping-pong buffers are RGBA8 by default, so bright values above 1 are clipped. With setUseFloatColor(true) they are built as RGBA16F and HDR values are blurred as is.

Example: Extract bloom from a scene texture and add it to the screen

```js
import { BloomEffect, RenderTarget, FullscreenPass } from "./build/vanilla.js";

const width = 320;
const height = 180;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
document.body.appendChild(canvas);
const webGL2RenderingContext = canvas.getContext("webgl2");
const sceneRenderTarget = new RenderTarget(webGL2RenderingContext, width, height, true);
sceneRenderTarget.bind();
webGL2RenderingContext.clearColor(1.0, 0.9, 0.6, 1.0);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);

const bloomEffect = new BloomEffect(webGL2RenderingContext);
bloomEffect.resize(width, height);
bloomEffect.setThreshold(0.6, 0.9);
bloomEffect.setIterationCount(3);
const sceneColorTexture = sceneRenderTarget.getColorTexture();
bloomEffect.render(sceneColorTexture);
const bloomTexture = bloomEffect.getResultTexture();

const compositeShaderSource = `#version 300 es
precision highp float;
in vec2 fragmentTextureCoordinate;
uniform sampler2D sceneTexture;
uniform sampler2D bloomTexture;
out vec4 outputColor;
void main() {
	vec3 sceneColor = texture(sceneTexture, fragmentTextureCoordinate).rgb;
	vec3 bloomColor = texture(bloomTexture, fragmentTextureCoordinate).rgb;
	outputColor = vec4(sceneColor + bloomColor * 0.8, 1.0);
}
`;
const compositePass = new FullscreenPass(webGL2RenderingContext, compositeShaderSource);
webGL2RenderingContext.bindFramebuffer(webGL2RenderingContext.FRAMEBUFFER, null);
webGL2RenderingContext.viewport(0, 0, width, height);
compositePass.useProgram();
const sceneTextureLocation = compositePass.getUniformLocation("sceneTexture");
const bloomTextureLocation = compositePass.getUniformLocation("bloomTexture");
webGL2RenderingContext.uniform1i(sceneTextureLocation, 0);
webGL2RenderingContext.uniform1i(bloomTextureLocation, 1);
webGL2RenderingContext.activeTexture(webGL2RenderingContext.TEXTURE0);
webGL2RenderingContext.bindTexture(webGL2RenderingContext.TEXTURE_2D, sceneColorTexture);
webGL2RenderingContext.activeTexture(webGL2RenderingContext.TEXTURE1);
webGL2RenderingContext.bindTexture(webGL2RenderingContext.TEXTURE_2D, bloomTexture);
compositePass.draw();
webGL2RenderingContext.activeTexture(webGL2RenderingContext.TEXTURE0);
```

- `constructor(webGL2RenderingContext)` (constructor, since 0.3.0): Constructor.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
- `resize(sceneWidth, sceneHeight)` (method, since 0.3.0): Resize. (Based on the scene resolution, internally half resolution)
  - `sceneWidth` number: Scene width. (pixels, the internal targets are half of it and at least 1)
  - `sceneHeight` number: Scene height. (pixels, the internal targets are half of it and at least 1)
- `render(sceneColorTexture)` (method, since 0.3.0): Bloom rendering. (After the call, the result is getResultTexture())
  - Side effects: depth/blend disabled, TEXTURE0 active, framebuffer binding changed.
  - `sceneColorTexture` WebGLTexture: Scene color texture to extract bright areas from.
- `getResultTexture()` (method, since 0.3.0): Returns the result texture. (after render())
  - returns WebGLTexture | null: Blurred result texture. (the ping target's color texture)
- `setThreshold(thresholdStart, thresholdEnd)` (method, since 0.3.0): Sets the extraction threshold. (Adjust to the scene's brightness characteristics)
  - `thresholdStart` number: Extraction starts at this brightness. (the largest of the three channels, default 0.5)
  - `thresholdEnd` number: Brightness at or above this is extracted fully. (smoothstep in between, default 0.95)
- `setIterationCount(iterationCount)` (method, since 0.3.0): Sets the blur iteration count.
  - `iterationCount` number: Number of times to repeat a horizontal and vertical blur pair. (default 2, 0 only extracts)
- `setUseFloatColor(useFloatColor)` (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)
  - `useFloatColor` boolean: RGBA16F when true, RGBA8 when false.
- `getUseFloatColor()` (method, since 0.4.0): Returns whether float color is used.
  - returns boolean: True when the ping-pong targets are RGBA16F.
- `getIterationCount()` (method, since 0.3.0): Returns the blur iteration count.
  - returns number: Blur iteration count.
- `destroy()` (method, since 0.3.0): Destroy. (Releases GL resources)
- `getWebGL2RenderingContext()` (method, since 0.3.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getBrightPass()` (method, since 0.3.0): Returns the bright area extraction pass.
  - returns FullscreenPass: Bright area extraction pass.
- `getBlurPass()` (method, since 0.3.0): Returns the blur pass.
  - returns FullscreenPass: Gaussian blur pass.
- `getPingRenderTarget()` (method, since 0.3.0): Returns the ping render target. (holds the final result)
  - returns RenderTarget: Ping render target. (holds the result)
- `getPongRenderTarget()` (method, since 0.3.0): Returns the pong render target.
  - returns RenderTarget: Pong render target. (intermediate horizontal blur result)
