# graphics3d/bloomeffect (vanilla-engine 0.4.0)

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

블룸 이펙트. (원본 graphics3d/bloomeffect.js)
- 밝은 영역 추출 → 하프 해상도 가우시안 블러 핑퐁. render() 결과 텍스처를 최종 합성 패스에서 가산하는 방식으로 쓴다.
- 셰이더와 패스 순서, 기본값(임계 0.5 ~ 0.95, 반복 2)은 원본과 같다.
- 주고받는 판은 기본 RGBA8 이라 1 을 넘는 밝은 값이 잘린다. set_use_float_color(true) 면 RGBA16F 로 지어 HDR 값을 그대로 흐린다.
- GL 을 쓰는 함수는 gl 을 받고, 생성과 크기 변경은 GL 객체를 못 만들면 오류(Result)를 돌려준다.

## BloomEffect (struct)

블룸 이펙트.

예제: 씬 렌더 타겟에서 블룸을 만들고 결과를 합성 패스의 1 번 유닛에 묶기

```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): 생성. (패스 2개 + 2x2 렌더 타겟 2장, resize 로 크기를 맞춘다)
  - `gl` &GlContext: GL 컨텍스트. (Graphic::get_gl 로 꺼낸다)
  - returns Result<BloomEffect, String>: 새 블룸 이펙트. (임계 0.5 ~ 0.95, 반복 2, RGBA8 판) 셰이더 컴파일이나 GL 객체 만들기에 실패하면 Err(오류 글).
- `pub fn resize(&mut self, gl: &GlContext, scene_width: i32, scene_height: i32) -> Result<(), String>` (method, since 0.3.2): 크기 변경. (씬 해상도 기준, 내부는 하프 해상도)
  - `gl` &GlContext: GL 컨텍스트.
  - `scene_width` i32: 씬 가로 크기. (픽셀, 판은 그 절반, 최소 1)
  - `scene_height` i32: 씬 세로 크기. (픽셀, 판은 그 절반, 최소 1)
  - returns Result<(), String>: 판을 맞췄으면 Ok. 다시 짓다 GL 객체를 못 만들면 Err(GL 의 오류 글).
- `pub fn render(&self, gl: &GlContext, scene_color_texture: Option<GlTexture>)` (method, since 0.3.2): 블룸 렌더링. (호출 후 결과는 get_result_texture())
  - 부수 효과: 깊이/블렌드 비활성, TEXTURE0 활성, 프레임버퍼 바인드가 변경된다.
  - `gl` &GlContext: GL 컨텍스트.
  - `scene_color_texture` Option<GlTexture>: 밝은 영역을 뽑을 씬 컬러 텍스처. (보통 씬 RenderTarget 의 get_color_texture(0))
- `pub fn get_result_texture(&self) -> Option<GlTexture>` (method, since 0.3.2): 결과 텍스처 반환. (render() 이후)
  - returns Option<GlTexture>: 블러를 마친 텍스처. (핑 판의 컬러, 씬의 절반 크기) destroy 뒤면 None.
- `pub fn set_threshold(&mut self, threshold_start: f64, threshold_end: f64)` (method, since 0.3.2): 추출 임계값 설정. (씬 밝기 특성에 맞게 조정)
  - `threshold_start` f64: 뽑기 시작 밝기. (세 채널 가운데 가장 큰 값, 이보다 어두우면 뽑지 않는다, 기본 0.5)
  - `threshold_end` f64: 다 뽑는 밝기. (이보다 밝으면 그대로, 사이는 smoothstep, 기본 0.95)
- `pub fn set_iteration_count(&mut self, iteration_count: u32)` (method, since 0.3.2): 블러 반복 횟수 설정.
  - `iteration_count` u32: 가로와 세로 블러 한 쌍을 되풀이할 횟수. (기본 2, 0 이면 밝은 영역 뽑기만)
- `pub fn set_use_float_color(&mut self, gl: &GlContext, use_float_color: bool) -> Result<(), String>` (method, since 0.4.0): 실수 컬러 설정. (true 면 주고받는 판을 RGBA16F 로 다시 지어 1 을 넘는 밝은 값이 잘리지 않는다, 기본 false 는 RGBA8, 같으면 그대로)
  - `gl` &GlContext: GL 컨텍스트.
  - `use_float_color` bool: true 면 RGBA16F, false 면 RGBA8.
  - returns Result<(), String>: 바꿨거나 이미 같으면 Ok. 새 판을 못 지으면 Err(GL 의 오류 글, 앞의 판은 그대로 둔다).

  예제: HDR 씬에서 1 을 넘는 빛을 잘리지 않게 흐리기

  ```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 bool: 판이 RGBA16F 면 true, RGBA8 이면 false.
- `pub fn get_iteration_count(&self) -> u32` (method, since 0.3.2): 블러 반복 횟수 반환.
  - returns u32: 블러 반복 횟수.
- `pub fn destroy(&mut self, gl: &GlContext)` (method, since 0.3.2): 파괴. (GL 리소스 해제)
  - `gl` &GlContext: 만들 때 쓴 GL 컨텍스트.
- `pub fn get_bright_pass(&self) -> &FullscreenPass` (method, since 0.3.2): 밝은 영역 추출 패스 반환.
  - returns &FullscreenPass: 밝은 영역 추출 패스. (유니폼 sceneTexture, thresholdRange)
- `pub fn get_blur_pass(&self) -> &FullscreenPass` (method, since 0.3.2): 블러 패스 반환.
  - returns &FullscreenPass: 9 탭 가우시안 블러 패스. (유니폼 sourceTexture, blurDirection)
- `pub fn get_ping_render_target(&self) -> &RenderTarget` (method, since 0.3.2): 핑 렌더 타겟 반환. (최종 결과 보관)
  - returns &RenderTarget: 핑 렌더 타겟. (밝은 영역과 세로 블러를 받는 판, 결과 텍스처의 주인)
- `pub fn get_pong_render_target(&self) -> &RenderTarget` (method, since 0.3.2): 퐁 렌더 타겟 반환.
  - returns &RenderTarget: 퐁 렌더 타겟. (가로 블러를 받는 판)
