# graphics3d/bloomeffect (vanilla.js 0.4.0)

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

블룸 이펙트. (밝은 영역 추출 → 하프 해상도 가우시안 블러 핑퐁)

## BloomEffect extends Object

블룸 이펙트. (밝은 영역 추출 → 하프 해상도 가우시안 블러 핑퐁)
- render() 결과 텍스처를 최종 합성 패스에서 가산하는 방식으로 사용한다.
- 주고받는 판은 기본 RGBA8 이라 1 을 넘는 밝은 값이 잘린다. setUseFloatColor(true) 면 RGBA16F 로 지어 HDR 값을 그대로 흐린다.

예제: 씬 텍스처에서 블룸을 뽑아 화면에 더하기

```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): 생성.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 렌더링 컨텍스트.
- `resize(sceneWidth, sceneHeight)` (method, since 0.3.0): 크기 변경. (씬 해상도 기준, 내부는 하프 해상도)
  - `sceneWidth` number: 씬 가로 크기. (픽셀, 내부 판은 그 반이고 최소 1)
  - `sceneHeight` number: 씬 세로 크기. (픽셀, 내부 판은 그 반이고 최소 1)
- `render(sceneColorTexture)` (method, since 0.3.0): 블룸 렌더링. (호출 후 결과는 getResultTexture())
  - 부수 효과: 깊이/블렌드 비활성, TEXTURE0 활성, 프레임버퍼 바인드가 변경된다.
  - `sceneColorTexture` WebGLTexture: 밝은 영역을 뽑을 씬 컬러 텍스처.
- `getResultTexture()` (method, since 0.3.0): 결과 텍스처 반환. (render() 이후)
  - returns WebGLTexture | null: 흐린 결과 텍스처. (핑 판의 컬러 텍스처)
- `setThreshold(thresholdStart, thresholdEnd)` (method, since 0.3.0): 추출 임계값 설정. (씬 밝기 특성에 맞게 조정)
  - `thresholdStart` number: 이 밝기부터 뽑기 시작한다. (세 채널 가운데 가장 큰 값, 기본 0.5)
  - `thresholdEnd` number: 이 밝기 이상은 그대로 뽑는다. (사이는 smoothstep, 기본 0.95)
- `setIterationCount(iterationCount)` (method, since 0.3.0): 블러 반복 횟수 설정.
  - `iterationCount` number: 가로, 세로 블러 한 쌍을 되풀이할 횟수. (기본 2, 0 이면 뽑기만)
- `setUseFloatColor(useFloatColor)` (method, since 0.4.0): 실수 컬러 설정. (true 면 주고받는 판을 RGBA16F 로 다시 지어 1 을 넘는 밝은 값이 잘리지 않는다, 기본 false 는 RGBA8, 같으면 그대로)
  - `useFloatColor` boolean: 참이면 RGBA16F, 거짓이면 RGBA8.
- `getUseFloatColor()` (method, since 0.4.0): 실수 컬러 사용 여부 반환.
  - returns boolean: 주고받는 판이 RGBA16F 면 참.
- `getIterationCount()` (method, since 0.3.0): 블러 반복 횟수 반환.
  - returns number: 블러 반복 횟수.
- `destroy()` (method, since 0.3.0): 파괴. (GL 리소스 해제)
- `getWebGL2RenderingContext()` (method, since 0.3.0): 렌더링 컨텍스트 반환.
  - returns WebGL2RenderingContext: 렌더링 컨텍스트.
- `getBrightPass()` (method, since 0.3.0): 밝은 영역 추출 패스 반환.
  - returns FullscreenPass: 밝은 영역 추출 패스.
- `getBlurPass()` (method, since 0.3.0): 블러 패스 반환.
  - returns FullscreenPass: 가우시안 블러 패스.
- `getPingRenderTarget()` (method, since 0.3.0): 핑 렌더 타겟 반환. (최종 결과 보관)
  - returns RenderTarget: 핑 렌더 타겟. (결과를 담는다)
- `getPongRenderTarget()` (method, since 0.3.0): 퐁 렌더 타겟 반환.
  - returns RenderTarget: 퐁 렌더 타겟. (가로 블러 중간 결과)
