# graphics3d/subsurfacescatteringeffect (vanilla.js 0.4.0)

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

Screen-space subsurface scattering. (separable, depth-aware horizontal/vertical blur of the diffuse irradiance buffer)

## SubsurfaceScatteringEffect extends Object

Screen-space subsurface scattering. (separable, depth-aware horizontal/vertical blur of the diffuse irradiance buffer)
- Builds per-channel kernels from the skin diffusion profile (sum of Gaussians), and projects the scattering width (world units) onto the screen according to depth.
- render() input is the irradiance texture holding the scattering mask in alpha and the depth texture of the same pass. The result is getResultTexture().

Example: Scatter an irradiance texture with a skin profile

```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): Constructor.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
- `computeKernel()` (method, since 0.3.0): Computes the kernel. (integrates the skin diffusion profile over the offset interval areas, center sample at index 0)
- `static buildKernel(falloffColor, strengthColor)` (static, since 0.4.0): Builds the kernel. (WebGL-independent, rgb weight + w offset per sample)
  - `falloffColor` number[]: Per-channel falloff. ([r, g, b], larger values spread that channel farther)
  - `strengthColor` number[]: Per-channel scattering strength. ([r, g, b], 0 keeps only the center sample, 1 uses the kernel weights as is)
  - returns Float32Array: Array holding rgb weights and a w offset for each of the 17 samples. (length 68, index 0 is the center sample)

  Example: Inspect kernel values without 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): Resizes. (based on the irradiance buffer resolution, 2 full-resolution float targets)
  - `width` number: Irradiance buffer width. (pixels)
  - `height` number: Irradiance buffer height. (pixels)
- `render(irradianceTexture, depthTexture, fieldOfViewRadian, aspectRatio, nearDistance, farDistance)` (method, since 0.3.0): Renders the scattering. (horizontal → ping, vertical → pong, result is getResultTexture())
  - Side effects: depth/blend disabled, TEXTURE0 active, framebuffer binding changed.
  - `irradianceTexture` WebGLTexture: Irradiance texture holding the scattering mask in alpha.
  - `depthTexture` WebGLTexture: Depth texture of the same pass.
  - `fieldOfViewRadian` number: Vertical field of view.
  - `aspectRatio` number: Aspect ratio. (width / height)
  - `nearDistance` number: Camera near distance. (used to linearize depth)
  - `farDistance` number: Camera far distance. (used to linearize depth)
- `getResultTexture()` (method, since 0.3.0): Returns the result texture. (after render())
  - returns WebGLTexture | null: Scattering result texture. (pong target, RGBA16F, alpha is the scattering mask)
- `setScatterWidth(scatterWidth)` (method, since 0.3.0): Sets the scattering width. (world units, full kernel radius)
  - `scatterWidth` number: Scatter width. (world units, the whole kernel radius, default 0.2)
- `getScatterWidth()` (method, since 0.3.0): Returns the scattering width.
  - returns number: Scatter width. (world units)
- `setProfile(falloffColor, strengthColor)` (method, since 0.3.0): Sets the diffusion profile. (per-channel falloff / strength, skin default (1, 0.37, 0.3) / (0.48, 0.41, 0.28))
  - `falloffColor` number[]: Per-channel falloff. ([r, g, b], the first three values are copied, skin default [1, 0.37, 0.3])
  - `strengthColor` number[]: Per-channel scattering strength. ([r, g, b], the first three values are copied, skin default [0.48, 0.41, 0.28])
- `getFalloffColor()` (method, since 0.3.0): Returns the falloff color.
  - returns number[]: Per-channel falloff. ([r, g, b])
- `getStrengthColor()` (method, since 0.3.0): Returns the strength color.
  - returns number[]: Per-channel scattering strength. ([r, g, b])
- `destroy()` (method, since 0.3.0): Destroy. (Releases GL resources)
- `getWebGL2RenderingContext()` (method, since 0.3.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getScatterPass()` (method, since 0.3.0): Returns the scattering pass.
  - returns FullscreenPass: Separable scattering blur pass.
- `getPingRenderTarget()` (method, since 0.3.0): Returns the ping render target. (horizontal blur result)
  - returns RenderTarget: Horizontal blur result render target.
- `getPongRenderTarget()` (method, since 0.3.0): Returns the pong render target. (holds the final result)
  - returns RenderTarget: Vertical blur result render target. (final result)
