{
 "id": "n8ao",
 "title": "N8AO",
 "aliases": [],
 "domain": "visibility",
 "subdomain": "screen-space AO",
 "stage": "visibility",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu"
 ],
 "year_introduced": 2023,
 "inputs": [
  "depth",
  "normals"
 ],
 "outputs": [
  "visibility-mask"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "A three.js post-processing screen-space AO pass (SSAO/HBAO-style) with half-resolution rendering, depth-aware denoising and colour-aware options.",
 "how_it_works": "N8AO is a drop-in three.js EffectComposer pass that reconstructs positions from depth, samples a spiral of points around each pixel in a normal-oriented hemisphere with a configurable radius, and compares them against the depth buffer to estimate occlusion. A depth-aware blur removes noise, optional half-resolution rendering with upsampling saves time, and distance-falloff and intensity controls shape the look. It can also tint occlusion by colour. As a plugin it avoids writing a custom pipeline, and its settings expose quality presets from low to ultra.",
 "when_to_use": "Choose it when you want good screen-space AO in three.js quickly, without wiring a G-buffer yourself. Prefer `ground-truth-ambient-occlusion` via three's GTAOPass for a more physically formulated option, `sdf-ambient-occlusion` for baked static scenes, and `ssao` for the simplest built-in pass.",
 "cost": {
  "class": "cheap",
  "notes": "Roughly SSAO-level cost; half-resolution mode lowers it further."
 },
 "failure_modes": [
  "Screen-space limits: no off-screen occluders",
  "Halos or banding at large radii",
  "Depth-aware blur can leak across edges",
  "Transparent objects are not part of the depth buffer"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:n8python/n8ao",
   "type": "code",
   "title": "N8python/n8ao",
   "description": "An efficient and visually pleasing implementation of SSAO with an emphasis on temporal stability and artist control.",
   "authors": [
    "N8python"
   ],
   "year": 2023,
   "venue": "GitHub",
   "url": "https://github.com/N8python/n8ao",
   "ids": {
    "repo": "N8python/n8ao"
   },
   "license": "CC0-1.0",
   "extra": {
    "stars": 528,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-15T20:46:16Z",
    "language": "JavaScript",
    "homepage": null,
    "license_name": "Creative Commons Zero v1.0 Universal"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/N8python/n8ao",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/N8python/n8ao",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/N8python/n8ao",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/N8python/n8ao",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/N8python/n8ao",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/N8python/n8ao",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "npython2023n8python"
  }
 ],
 "presets": [
  {
   "id": "n8ao-live",
   "title": "N8AO — unoccluded flat lighting (left) vs spiral-hemisphere depth-aware denoised AO + color bleed (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_AO_RADIUS": 0.65,
    "P_AO_INTENSITY": 1.35,
    "P_COLOR_BLEED": 0.45,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_AO_RADIUS = 0.65; // P Hemisphere Radius [0.2, 1.4, 0.05]\nconst float P_AO_INTENSITY = 1.35; // P AO Intensity [0.3, 2.5, 0.05]\nconst float P_COLOR_BLEED = 0.45; // P Color Bleed [0.0, 1.0, 0.05]\nconst float P_SPLIT = 0.48; // P No AO / N8AO [0.0, 1.0, 0.02]\n\nfloat sdBox(vec3 p, vec3 b) {\n  vec3 q = abs(p) - b;\n  return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);\n}\n\n// Stepped architectural niches & nested spheres full of tight creases where N8AO shines\nvec2 mapN8Scene(vec3 p) {\n  float dFloor = p.y + 0.56;\n  vec2 res = vec2(dFloor, 1.0);\n\n  // Back stepped terracotta wall with recessed alcoves\n  float dWall = sdBox(p - vec3(0.0, 0.15, -0.88), vec3(1.5, 0.85, 0.18));\n  float niche1 = sdBox(p - vec3(-0.48, -0.04, -0.72), vec3(0.26, 0.36, 0.16));\n  float niche2 = sdBox(p - vec3(0.48, -0.04, -0.72), vec3(0.26, 0.36, 0.16));\n  dWall = max(dWall, -min(niche1, niche2));\n  if (dWall < res.x) res = vec2(dWall, 2.0);\n\n  // Stepped stair plinths\n  float s1 = sdBox(p - vec3(0.0, -0.46, -0.35), vec3(0.95, 0.10, 0.45));\n  float s2 = sdBox(p - vec3(0.0, -0.34, -0.48), vec3(0.68, 0.10, 0.30));\n  float dStair = min(s1, s2);\n  if (dStair < res.x) res = vec2(dStair, 3.0);\n\n  // Clustered spheres seated tightly into stair corners (strong contact AO!)\n  float c1 = length(p - vec3(0.0, -0.02, -0.42)) - 0.24;\n  float c2 = length(p - vec3(-0.34, -0.18, -0.18)) - 0.18;\n  float c3 = length(p - vec3(0.36, -0.20, -0.14)) - 0.16;\n  float c4 = length(p - vec3(-0.48, -0.22, -0.68)) - 0.16;\n  float c5 = length(p - vec3(0.48, -0.22, -0.68)) - 0.16;\n  float dSph = min(min(c1, c2), min(c3, min(c4, c5)));\n  if (dSph < res.x) res = vec2(dSph, 4.0);\n  return res;\n}\n\nvec3 calcNormal(vec3 p) {\n  vec2 e = vec2(0.0022, 0.0);\n  return normalize(vec3(\n    mapN8Scene(p + e.xyy).x - mapN8Scene(p - e.xyy).x,\n    mapN8Scene(p + e.yxy).x - mapN8Scene(p - e.yxy).x,\n    mapN8Scene(p + e.yyx).x - mapN8Scene(p - e.yyx).x\n  ));\n}\n\nvec3 matAlbedo(float id) {\n  if (id < 1.5) return vec3(0.76, 0.74, 0.70);\n  if (id < 2.5) return vec3(0.86, 0.46, 0.30);\n  if (id < 3.5) return vec3(0.84, 0.82, 0.78);\n  return vec3(0.92, 0.88, 0.82);\n}\n\n// N8AO spiral-hemisphere sampling with distance falloff & optional color bleed\nvoid evalN8AO(vec3 p, vec3 n, float rad, float intensity, out float ao, out vec3 bleed) {\n  vec3 up = (abs(n.y) < 0.99) ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);\n  vec3 t = normalize(cross(up, n));\n  vec3 b = cross(n, t);\n\n  float occ = 0.0;\n  bleed = vec3(0.0);\n  float wBleed = 0.0;\n  // 12 golden-angle spiral hemisphere taps\n  for (int i = 0; i < 12; i++) {\n    float fi = float(i);\n    float rFrac = (fi + 0.5) / 12.0;\n    float ang = fi * 2.3999632;\n    float z = 0.22 + 0.75 * rFrac;\n    float xy = sqrt(max(1.0 - z * z, 0.0));\n    vec3 dir = normalize(t * (cos(ang) * xy) + b * (sin(ang) * xy) + n * z);\n    float stepD = rad * (0.14 + 0.86 * rFrac * rFrac);\n    vec2 h = mapN8Scene(p + dir * stepD);\n    float delta = max(stepD - h.x, 0.0);\n    float falloff = 1.0 - smoothstep(0.0, rad * 1.1, stepD);\n    float tapOcc = clamp(delta / max(stepD, 0.04), 0.0, 1.0) * falloff;\n    occ += tapOcc;\n    if (tapOcc > 0.05) {\n      bleed += matAlbedo(h.y) * tapOcc;\n      wBleed += tapOcc;\n    }\n  }\n  ao = clamp(1.0 - (occ / 12.0) * intensity * 1.65, 0.06, 1.0);\n  bleed = (wBleed > 0.01) ? (bleed / wBleed) : vec3(0.6);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  float rad = clamp(P_AO_RADIUS, 0.15, 1.5) * 0.42;\n  float inten = clamp(P_AO_INTENSITY, 0.2, 2.8);\n  float bleedAmt = clamp(P_COLOR_BLEED, 0.0, 1.0);\n\n  vec3 ro = vec3(0.32 * sin(iTime * 0.45), 0.18, 1.92);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.07, -1.45));\n  vec3 col = mix(vec3(0.14, 0.19, 0.28), vec3(0.45, 0.58, 0.78), smoothstep(-0.2, 0.6, rd.y));\n\n  float dist = 0.05;\n  vec2 hit = vec2(10.0, 0.0);\n  for (int i = 0; i < 64; i++) {\n    vec2 h = mapN8Scene(ro + rd * dist);\n    if (h.x < 0.002) { hit = vec2(dist, h.y); break; }\n    dist += h.x;\n    if (dist > 8.0) break;\n  }\n\n  if (hit.y > 0.5) {\n    vec3 p = ro + rd * hit.x;\n    vec3 n = calcNormal(p);\n    vec3 lDir = normalize(vec3(0.45, 0.82, 0.55));\n    vec3 alb = matAlbedo(hit.y);\n    float ndl = max(dot(n, lDir), 0.0);\n    vec3 direct = alb * vec3(0.95, 0.90, 0.82) * ndl * 0.65;\n    vec3 hemiAmb = alb * mix(vec3(0.32, 0.36, 0.44), vec3(0.68, 0.78, 0.92), 0.5 + 0.5 * n.y) * 0.72;\n\n    if (screenX < split) {\n      // Left: Flat ambient without N8AO (objects appear floating without crease grounding)\n      col = direct + hemiAmb;\n    } else {\n      // Right: N8AO spiral hemisphere AO + subtle color bleed inside corners and niches\n      float ao;\n      vec3 bleed;\n      evalN8AO(p + n * 0.006, n, rad, inten, ao, bleed);\n      vec3 aoTint = mix(vec3(ao), mix(bleed * 0.55, vec3(1.0), ao), bleedAmt * 0.65);\n      col = direct * mix(1.0, ao, 0.45) + hemiAmb * aoTint;\n    }\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.98, 1.0), sep);\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "n8ao",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:16.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6931,
      "std": 0.1239,
      "min": 0.1231,
      "max": 0.9028
     },
     "image": "/specimens/n8ao-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "ssao",
  "hbao",
  "ground-truth-ambient-occlusion"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/visibility/n8ao.json"
 },
 "solves": [
  "need good ambient occlusion in three.js with little setup",
  "scene looks flat without contact darkening",
  "ao pass shows noise or halos at edges",
  "post-process occlusion for web 3d without a g-buffer",
  "creases and corners lack shading depth"
 ],
 "authority": "N8python, 2023",
 "url": "https://texelatlas.vercel.app/t/n8ao/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 44.17,
  "tier": "heavy",
  "p50_ms": 1.4531,
  "p95_ms": 1.5515,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "n8ao-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 45.16,
   "tier": "heavy",
   "p50_ms": 0.718,
   "p95_ms": 0.7209,
   "speedup_x": 2.02,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 30.19,
   "tier": "heavy",
   "p50_ms": 0.8696,
   "p95_ms": 0.8824,
   "speedup_x": 1.67,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}