# graphics3d/ambientocclusioneffect (vanilla.js 0.4.0)

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

화면 공간 앰비언트 오클루전. (깊이 + 노멀 버퍼 → 반구 표본 차폐 → 깊이 인식 블러)

## AmbientOcclusionEffect extends Object

화면 공간 앰비언트 오클루전. (깊이 + 노멀 버퍼 → 반구 표본 차폐 → 깊이 인식 블러)
- render() 입력은 본 패스의 깊이 텍스처와 월드 노멀 텍스처(알파 = 커버리지), 카메라의 뷰 회전 / 투영 파라미터.
  결과는 getResultTexture() 의 r 채널 가시율(1 = 차폐 없음).

예제: 깊이와 노멀 텍스처로 차폐 구하기

```js
import { AmbientOcclusionEffect, RenderTarget, Camera3D, Vector3 } from "./build/vanilla.js";

const width = 640;
const height = 360;
const canvas = document.createElement("canvas");
const webGL2RenderingContext = canvas.getContext("webgl2");
const geometryRenderTarget = new RenderTarget(webGL2RenderingContext, width, height, true, { useDepthTexture: true });
geometryRenderTarget.bind();
webGL2RenderingContext.clearColor(0.0, 1.0, 0.0, 1.0);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);

const camera = new Camera3D();
camera.setPerspective(0.8, 0.1, 100);
camera.setAspectRatio(width / height);
const eyePosition = Vector3.create(0, 2, 6);
const targetPosition = Vector3.create(0, 0, 0);
camera.lookAt(eyePosition, targetPosition);
const viewMatrix = camera.getViewMatrix();
const viewMatrixElements = viewMatrix.getElements();
const fieldOfView = camera.getFieldOfView();
const aspectRatio = camera.getAspectRatio();
const nearDistance = camera.getNearDistance();
const farDistance = camera.getFarDistance();

const ambientOcclusionEffect = new AmbientOcclusionEffect(webGL2RenderingContext);
ambientOcclusionEffect.setScale(0.5);
ambientOcclusionEffect.resize(width, height);
ambientOcclusionEffect.setParameters(0.5, 1.2, 0.01);
const depthTexture = geometryRenderTarget.getDepthTexture();
const normalTexture = geometryRenderTarget.getColorTexture();
ambientOcclusionEffect.render(depthTexture, normalTexture, viewMatrixElements, fieldOfView, aspectRatio, nearDistance, farDistance);
const visibilityTexture = ambientOcclusionEffect.getResultTexture();
console.log(visibilityTexture !== null);
ambientOcclusionEffect.destroy();
geometryRenderTarget.destroy();
```

- `constructor(webGL2RenderingContext)` (constructor, since 0.3.0): 생성.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 렌더링 컨텍스트.
- `computeKernel()` (method, since 0.3.0): 표본 커널 계산. (반구 안에 고르게, 중심 쪽에 더 촘촘히)
- `static buildSampleKernel()` (static, since 0.4.0): 표본 커널 만들기. (WebGL 무관, 황금각 나선, 높이 고르게, 크기 0.15 ~ 1 제곱 분포)
  - returns Float32Array: 표본마다 x, y, z.
- `resize(width, height)` (method, since 0.3.0): 크기 변경. (본 패스 해상도, 내부는 축소 배율 적용)
  - `width` number: 본 패스 가로 크기. (픽셀, 내부 판은 배율을 곱해 내리고 최소 1)
  - `height` number: 본 패스 세로 크기. (픽셀, 내부 판은 배율을 곱해 내리고 최소 1)
- `render(depthTexture, normalTexture, viewMatrixElements, fieldOfViewRadian, aspectRatio, nearDistance, farDistance)` (method, since 0.3.0): 차폐 렌더링. (차폐 → 가로 블러 → 세로 블러, 결과는 getResultTexture())
  - 부수 효과: 깊이/블렌드 비활성, TEXTURE0 활성, 프레임버퍼 바인드가 변경된다.
  - `depthTexture` WebGLTexture: 본 패스 깊이 텍스처.
  - `normalTexture` WebGLTexture: 월드 노멀 텍스처. (알파 = 커버리지)
  - `viewMatrixElements` Float32Array: 뷰 행렬 원소. (열 우선 4x4)
  - `fieldOfViewRadian` number: 세로 시야각.
  - `aspectRatio` number: 가로 세로 비. (너비 / 높이)
  - `nearDistance` number: 카메라의 가까운 거리. (깊이를 선형으로 풀 때 쓴다)
  - `farDistance` number: 카메라의 먼 거리. (깊이를 선형으로 풀 때 쓴다)
- `getResultTexture()` (method, since 0.3.0): 결과 텍스처 반환. (render() 이후, r 가시율)
  - returns WebGLTexture | null: 가시율 텍스처. (r 이 가시율 0 ~ 1 이고 1 이면 차폐 없음, g 는 선형 깊이)
- `setParameters(radius, intensity, bias)` (method, since 0.3.0): 파라미터 설정. (표본 반경(월드) / 강도 / 깊이 바이어스)
  - `radius` number: 표본 반경. (뷰 공간 월드 단위, 기본 0.03)
  - `intensity` number: 차폐 세기 배율. (기본 1)
  - `bias` number: 깊이 비교 바이어스. (월드 단위, 제 면 차폐를 막는다, 기본 0.002)
- `setScale(scale)` (method, since 0.3.0): 내부 해상도 배율 설정. (resize 전에 호출)
  - `scale` number: 내부 해상도 배율. (본 패스 크기에 곱한다, 기본 0.5, 다음 resize 부터 쓴다)
- `destroy()` (method, since 0.3.0): 파괴. (GL 리소스 해제)
- `getWebGL2RenderingContext()` (method, since 0.3.0): 렌더링 컨텍스트 반환.
  - returns WebGL2RenderingContext: 렌더링 컨텍스트.
- `getOcclusionPass()` (method, since 0.3.0): 차폐 패스 반환.
  - returns FullscreenPass: 차폐 패스.
- `getBlurPass()` (method, since 0.3.0): 블러 패스 반환.
  - returns FullscreenPass: 깊이 인식 블러 패스.
- `getOcclusionRenderTarget()` (method, since 0.3.0): 차폐 렌더 타겟 반환.
  - returns RenderTarget: 차폐 결과 렌더 타겟. (RGBA16F)
- `getPingRenderTarget()` (method, since 0.3.0): 핑 렌더 타겟 반환. (가로 블러 결과)
  - returns RenderTarget: 가로 블러 결과 렌더 타겟.
- `getPongRenderTarget()` (method, since 0.3.0): 퐁 렌더 타겟 반환. (최종 결과 보관)
  - returns RenderTarget: 세로 블러 결과 렌더 타겟. (최종 결과)
- `getRadius()` (method, since 0.3.0): 표본 반경 반환.
  - returns number: 표본 반경. (뷰 공간 월드 단위)
- `getIntensity()` (method, since 0.3.0): 강도 반환.
  - returns number: 차폐 세기 배율.
- `getBias()` (method, since 0.3.0): 깊이 바이어스 반환.
  - returns number: 깊이 비교 바이어스. (월드 단위)
- `getScale()` (method, since 0.3.0): 내부 해상도 배율 반환.
  - returns number: 내부 해상도 배율.
