# graphics3d/multilevelbloomeffect (vanilla.js 0.4.0)

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

Multi-level bloom effect. (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)

## MultiLevelBloomEffect extends Object

Multi-level bloom effect. (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)
- Each level is half the size of the previous level. The first level (half the scene size) is the result. (getResultTexture)
- 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. (setThreshold(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.
- With useFloatColor 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(scene width, height) → render(scene texture) → getResultTexture() 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.

Example: Extract multi-level bloom from an HDR scene texture

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

const width = 640;
const height = 360;
const canvas = document.createElement("canvas");
const webGL2RenderingContext = canvas.getContext("webgl2");
const sceneRenderTarget = new RenderTarget(webGL2RenderingContext, width, height, true, { useFloatColor: true });
sceneRenderTarget.bind();
webGL2RenderingContext.clearColor(2.0, 1.5, 0.5, 1.0);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);

const multiLevelBloomEffect = new MultiLevelBloomEffect(webGL2RenderingContext, 5, true);
multiLevelBloomEffect.resize(width, height);
multiLevelBloomEffect.setThreshold(1.0, 0.5);
const sceneColorTexture = sceneRenderTarget.getColorTexture();
multiLevelBloomEffect.render(sceneColorTexture);
const bloomTexture = multiLevelBloomEffect.getResultTexture();
const levelCount = multiLevelBloomEffect.getLevelCount();
console.log(levelCount, bloomTexture !== null);
multiLevelBloomEffect.destroy();
sceneRenderTarget.destroy();
```

- `constructor(webGL2RenderingContext, levelCount = 5, useFloatColor = true)` (constructor, since 0.4.0): Constructor. (level count, whether to use float targets)
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
  - `levelCount` number: Default 5.
  - `useFloatColor` boolean: Default true.
- `createLevels()` (method, since 0.4.0): Rebuilds the per-level targets. (with the scene size and level count)
- `destroyLevels()` (method, since 0.4.0): Deletes the per-level targets.
- `resize(sceneWidth, sceneHeight)` (method, since 0.4.0): Resizes. (based on the scene resolution, unchanged if the same)
  - `sceneWidth` number: Scene width. (pixels, below 1 becomes 1, the first level is half of it)
  - `sceneHeight` number: Scene height. (pixels, below 1 becomes 1, the first level is half of it)
- `setLevelCount(levelCount)` (method, since 0.4.0): Sets the level count. (1 or more, unchanged if the same)
  - `levelCount` number: Level count. (fractions are dropped, below 1 becomes 1)
- `getLevelCount()` (method, since 0.4.0): Returns the level count.
  - returns number: Level count.
- `getUseFloatColor()` (method, since 0.4.0): Returns whether float targets are used.
  - returns boolean: True when the per-level targets are RGBA16F.
- `setThreshold(threshold, knee)` (method, since 0.4.0): Sets the extraction threshold. (brightness, knee is the smooth width around the threshold, 0 means a hard cut)
  - `threshold` number: Extraction threshold brightness. (the largest of the three channels, below 0 becomes 0, default 1)
  - `knee` number: Soft width around the threshold. (below 0 becomes 0, default 0.25)
- `render(sceneColorTexture)` (method, since 0.4.0): Renders the bloom. (after the call, the result is getResultTexture())
  - `sceneColorTexture` WebGLTexture: Scene color texture to extract the bright part from.
- `getResultTexture()` (method, since 0.4.0): Returns the result texture. (after render(), the first level)
  - returns WebGLTexture | null: Color texture of the first level (half the scene size). (null when there are no levels)
- `destroy()` (method, since 0.4.0): Destroy. (Releases GL resources)
- `getWebGL2RenderingContext()` (method, since 0.4.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
