# graphics3d/subsurfacescatteringeffect (vanilla.js 0.4.0)

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

화면 공간 서브서피스 스캐터링. (분리형, 확산 조도 버퍼를 깊이 인식 가로/세로 블러)

## SubsurfaceScatteringEffect extends Object

화면 공간 서브서피스 스캐터링. (분리형, 확산 조도 버퍼를 깊이 인식 가로/세로 블러)
- 피부 확산 프로파일(가우시안 합)로 채널별 커널을 만들고, 산란 폭(월드 단위)을 깊이에 맞춰 화면에 투영한다.
- render() 입력은 알파에 산란 마스크를 담은 조도 텍스처와 같은 패스의 깊이 텍스처. 결과는 getResultTexture().

예제: 조도 텍스처를 피부 프로파일로 흩기

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

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

const camera = new Camera3D();
camera.setPerspective(0.6, 0.05, 50);
camera.setAspectRatio(width / height);
const fieldOfView = camera.getFieldOfView();
const aspectRatio = camera.getAspectRatio();
const nearDistance = camera.getNearDistance();
const farDistance = camera.getFarDistance();

const subsurfaceScatteringEffect = new SubsurfaceScatteringEffect(webGL2RenderingContext);
subsurfaceScatteringEffect.resize(width, height);
subsurfaceScatteringEffect.setScatterWidth(0.012);
subsurfaceScatteringEffect.setProfile([1.0, 0.37, 0.3], [0.48, 0.41, 0.28]);
const irradianceTexture = irradianceRenderTarget.getColorTexture();
const depthTexture = irradianceRenderTarget.getDepthTexture();
subsurfaceScatteringEffect.render(irradianceTexture, depthTexture, fieldOfView, aspectRatio, nearDistance, farDistance);
const scatteredTexture = subsurfaceScatteringEffect.getResultTexture();
console.log(scatteredTexture !== null);
subsurfaceScatteringEffect.destroy();
irradianceRenderTarget.destroy();
```

- `constructor(webGL2RenderingContext)` (constructor, since 0.3.0): 생성.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 렌더링 컨텍스트.
- `computeKernel()` (method, since 0.3.0): 커널 계산. (피부 확산 프로파일을 오프셋 구간 넓이로 적분, 중앙 샘플을 0번에)
- `static buildKernel(falloffColor, strengthColor)` (static, since 0.4.0): 커널 만들기. (WebGL 무관, 표본마다 rgb 가중치 + w 오프셋)
  - `falloffColor` number[]: 채널마다의 폴오프. ([r, g, b], 클수록 그 채널이 멀리 퍼진다)
  - `strengthColor` number[]: 채널마다의 산란 강도. ([r, g, b], 0 이면 중앙 표본만, 1 이면 커널 가중치 그대로)
  - returns Float32Array: 표본 17 개마다 rgb 가중치와 w 오프셋을 담은 배열. (길이 68, 0 번이 중앙 표본)

  예제: GL 없이 커널 값 보기

  ```js
  import { SubsurfaceScatteringEffect } from "./build/vanilla.js";
  
  const kernel = SubsurfaceScatteringEffect.buildKernel([1.0, 0.37, 0.3], [0.48, 0.41, 0.28]);
  console.log(kernel.length, kernel[0], kernel[1], kernel[2], kernel[3]);
  ```

- `resize(width, height)` (method, since 0.3.0): 크기 변경. (조도 버퍼 해상도 기준, 전체 해상도 실수 타겟 2장)
  - `width` number: 조도 버퍼 가로 크기. (픽셀)
  - `height` number: 조도 버퍼 세로 크기. (픽셀)
- `render(irradianceTexture, depthTexture, fieldOfViewRadian, aspectRatio, nearDistance, farDistance)` (method, since 0.3.0): 산란 렌더링. (가로 → 핑, 세로 → 퐁, 결과는 getResultTexture())
  - 부수 효과: 깊이/블렌드 비활성, TEXTURE0 활성, 프레임버퍼 바인드가 변경된다.
  - `irradianceTexture` WebGLTexture: 알파에 산란 마스크를 담은 조도 텍스처.
  - `depthTexture` WebGLTexture: 같은 패스의 깊이 텍스처.
  - `fieldOfViewRadian` number: 세로 시야각.
  - `aspectRatio` number: 가로 세로 비. (너비 / 높이)
  - `nearDistance` number: 카메라의 가까운 거리. (깊이를 선형으로 풀 때 쓴다)
  - `farDistance` number: 카메라의 먼 거리. (깊이를 선형으로 풀 때 쓴다)
- `getResultTexture()` (method, since 0.3.0): 결과 텍스처 반환. (render() 이후)
  - returns WebGLTexture | null: 산란 결과 텍스처. (퐁 판, RGBA16F, 알파는 산란 마스크)
- `setScatterWidth(scatterWidth)` (method, since 0.3.0): 산란 폭 설정. (월드 단위, 커널 전체 반경)
  - `scatterWidth` number: 산란 폭. (월드 단위, 커널 전체 반경, 기본 0.2)
- `getScatterWidth()` (method, since 0.3.0): 산란 폭 반환.
  - returns number: 산란 폭. (월드 단위)
- `setProfile(falloffColor, strengthColor)` (method, since 0.3.0): 확산 프로파일 설정. (채널별 폴오프 / 강도, 피부 기본 (1, 0.37, 0.3) / (0.48, 0.41, 0.28))
  - `falloffColor` number[]: 채널마다의 폴오프. ([r, g, b], 앞 세 값을 복사한다, 피부 기본 [1, 0.37, 0.3])
  - `strengthColor` number[]: 채널마다의 산란 강도. ([r, g, b], 앞 세 값을 복사한다, 피부 기본 [0.48, 0.41, 0.28])
- `getFalloffColor()` (method, since 0.3.0): 폴오프 색 반환.
  - returns number[]: 채널마다의 폴오프. ([r, g, b])
- `getStrengthColor()` (method, since 0.3.0): 강도 색 반환.
  - returns number[]: 채널마다의 산란 강도. ([r, g, b])
- `destroy()` (method, since 0.3.0): 파괴. (GL 리소스 해제)
- `getWebGL2RenderingContext()` (method, since 0.3.0): 렌더링 컨텍스트 반환.
  - returns WebGL2RenderingContext: 렌더링 컨텍스트.
- `getScatterPass()` (method, since 0.3.0): 산란 패스 반환.
  - returns FullscreenPass: 분리형 산란 블러 패스.
- `getPingRenderTarget()` (method, since 0.3.0): 핑 렌더 타겟 반환. (가로 블러 결과)
  - returns RenderTarget: 가로 블러 결과 렌더 타겟.
- `getPongRenderTarget()` (method, since 0.3.0): 퐁 렌더 타겟 반환. (최종 결과 보관)
  - returns RenderTarget: 세로 블러 결과 렌더 타겟. (최종 결과)
