# graphics3d/ambientocclusioneffect (vanilla.js 0.4.0)

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

Screen-space ambient occlusion. (depth + normal buffer → hemisphere sample occlusion → depth-aware blur)

## AmbientOcclusionEffect extends Object

Screen-space ambient occlusion. (depth + normal buffer → hemisphere sample occlusion → depth-aware blur)
- render() input is the main pass depth texture and world normal texture (alpha = coverage), and the camera view rotation / projection parameters.
  The result is the visibility (1 = no occlusion) in the r channel of getResultTexture().

Example: Compute occlusion from depth and normal textures

```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): Constructor.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
- `computeKernel()` (method, since 0.3.0): Computes the sample kernel. (evenly within the hemisphere, denser toward the center)
- `static buildSampleKernel()` (static, since 0.4.0): Builds the sample kernel. (WebGL-independent, golden-angle spiral, even heights, size 0.15 ~ 1 squared distribution)
  - returns Float32Array: x, y, z per sample.
- `resize(width, height)` (method, since 0.3.0): Resizes. (main pass resolution, the downscale factor is applied internally)
  - `width` number: Main pass width. (pixels, the internal targets are multiplied by the scale, floored and at least 1)
  - `height` number: Main pass height. (pixels, the internal targets are multiplied by the scale, floored and at least 1)
- `render(depthTexture, normalTexture, viewMatrixElements, fieldOfViewRadian, aspectRatio, nearDistance, farDistance)` (method, since 0.3.0): Renders the occlusion. (occlusion → horizontal blur → vertical blur, result is getResultTexture())
  - Side effects: depth/blend disabled, TEXTURE0 active, framebuffer binding changed.
  - `depthTexture` WebGLTexture: Main pass depth texture.
  - `normalTexture` WebGLTexture: World normal texture. (alpha = coverage)
  - `viewMatrixElements` Float32Array: View matrix elements. (column-major 4x4)
  - `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(), r visibility)
  - returns WebGLTexture | null: Visibility texture. (r is visibility 0 ~ 1 where 1 means no occlusion, g is linear depth)
- `setParameters(radius, intensity, bias)` (method, since 0.3.0): Sets the parameters. (sample radius (world) / strength / depth bias)
  - `radius` number: Sample radius. (view-space world units, default 0.03)
  - `intensity` number: Occlusion intensity multiplier. (default 1)
  - `bias` number: Depth comparison bias. (world units, prevents self-occlusion, default 0.002)
- `setScale(scale)` (method, since 0.3.0): Sets the internal resolution scale. (call before resize)
  - `scale` number: Internal resolution scale. (multiplied by the main pass size, default 0.5, takes effect from the next resize)
- `destroy()` (method, since 0.3.0): Destroy. (Releases GL resources)
- `getWebGL2RenderingContext()` (method, since 0.3.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getOcclusionPass()` (method, since 0.3.0): Returns the occlusion pass.
  - returns FullscreenPass: Occlusion pass.
- `getBlurPass()` (method, since 0.3.0): Returns the blur pass.
  - returns FullscreenPass: Depth-aware blur pass.
- `getOcclusionRenderTarget()` (method, since 0.3.0): Returns the occlusion render target.
  - returns RenderTarget: Occlusion result render target. (RGBA16F)
- `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)
- `getRadius()` (method, since 0.3.0): Returns the sample radius.
  - returns number: Sample radius. (view-space world units)
- `getIntensity()` (method, since 0.3.0): Returns the strength.
  - returns number: Occlusion intensity multiplier.
- `getBias()` (method, since 0.3.0): Returns the depth bias.
  - returns number: Depth comparison bias. (world units)
- `getScale()` (method, since 0.3.0): Returns the internal resolution scale.
  - returns number: Internal resolution scale.
