{
 "id": "toon-shading",
 "title": "Toon / cel shading with lighting ramps",
 "aliases": [
  "cel shading",
  "toon ramp",
  "Toon Shading",
  "Cartoon Shading"
 ],
 "domain": "npr",
 "subdomain": "lit shading ramps",
 "stage": "lighting",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": null,
 "inputs": [
  "normals",
  "material-params"
 ],
 "outputs": [
  "ldr-image"
 ],
 "style": "stylized",
 "dynamic": "dynamic",
 "summary": "Quantizes diffuse lighting into a few flat tonal bands, usually via a 1D ramp texture or step functions, so a 3D surface reads as hand-inked cartoon colour.",
 "how_it_works": "The shader computes a scalar lighting term, typically N·L (often remapped as half-Lambert so the terminator is softer), and uses it to look up a ramp instead of feeding it straight to the output. The ramp is either a small 1D texture an artist can paint, or a handful of `step`/`smoothstep` thresholds blending between a shadow colour, a base colour and a highlight colour. Choosing hue-shifted shadow colours (cool and saturated rather than just darker) is what gives the look its character. Specular is usually a hard-edged blob from thresholding N·H, and a rim term from 1 − N·V adds a graphic edge light. Anti-aliasing the thresholds with `fwidth` prevents shimmering on the band borders. Outlines are a separate pass (see the outline entries).",
 "when_to_use": "Choose it when you want a flat-colour animated or comic look that still responds to a light direction. Prefer `gooch-shading` when the goal is legible technical or CAD-style depiction with warm-to-cool tone rather than banding. Prefer `guilty-gear-xrd-shading` when you need a hand-authored, animator-controlled look where artists sculpt shadow shapes through vertex data rather than letting the light decide. Prefer `posterization` for a purely image-space version.",
 "cost": {
  "class": "cheap",
  "notes": "Qualitatively a few ALU ops per pixel plus one tiny ramp lookup; negligible next to any physically based shading pass."
 },
 "failure_modes": [
  "Band borders crawl and alias on curved surfaces unless thresholds are filtered with screen-space derivatives",
  "Low-poly normals make the terminator follow facets; smooth or bake custom normals",
  "Self-shadowing and cast shadows must be thresholded too, or soft shadow gradients break the flat look",
  "A single N·L ramp cannot give artist-controlled shadow shapes on faces; shadows look noisy on small features"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/280814.280950",
   "type": "paper",
   "title": "A non-photorealistic lighting model for automatic technical illustration",
   "authors": [
    "Amy Gooch",
    "Bruce Gooch",
    "Peter Shirley",
    "Elaine Cohen"
   ],
   "year": 1998,
   "venue": "Proceedings of the 25th annual conference on Computer graphics and interactive techniques  - SIGGRAPH '98",
   "url": "https://doi.org/10.1145/280814.280950",
   "ids": {
    "doi": "10.1145/280814.280950"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280950",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280950",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280950",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280950",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280950",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "gooch1998photorealistic"
  },
  {
   "role": "tutorial",
   "key": "url:https://en.wikipedia.org/wiki/Cel_shading",
   "type": "tutorial",
   "title": "Cel shading (outline section)",
   "authors": [
    "Wikipedia"
   ],
   "year": null,
   "venue": "en.wikipedia.org",
   "url": "https://en.wikipedia.org/wiki/Cel_shading",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261109223803/https://en.wikipedia.org/wiki/Cel_shading",
     "timestamp": "20261109223803"
    },
    "page_title": "Cel shading - Wikipedia",
    "host": "en.wikipedia.org",
    "title_confirmed_by_page": false,
    "title_unconfirmed": true
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Cel_shading → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Cel_shading → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.5714285714285714,
   "__bib": "wikipediashading"
  }
 ],
 "presets": [
  {
   "id": "toon-ramp-sdf-sphere",
   "title": "Three-band toon ramp on an SDF sphere",
   "runtime": "glsl-fragment",
   "params": {
    "bands": 3,
    "t1": 0.45,
    "t2": 0.75
   },
   "code": "// GLSL ES 3.0, Shadertoy-style: toon ramp on a raymarched SDF sphere\nconst vec3  LIGHT_DIR = vec3(0.6, 0.7, -0.4);\nconst vec3  SHADOW_COL = vec3(0.35, 0.18, 0.45);\nconst vec3  MID_COL    = vec3(0.95, 0.45, 0.30);\nconst vec3  LIT_COL    = vec3(1.00, 0.85, 0.55);\nconst float T1 = 0.45;   // shadow / mid threshold on half-Lambert\nconst float T2 = 0.75;   // mid / lit threshold\nconst float RIM_POWER = 3.0;\n\nfloat sdScene(vec3 p){\n  float s = length(p - vec3(0.0, 0.1, 0.0)) - 1.0;\n  float f = p.y + 1.0;\n  return min(s, f);\n}\nvec3 calcN(vec3 p){\n  vec2 e = vec2(0.001, 0.0);\n  return normalize(vec3(sdScene(p+e.xyy)-sdScene(p-e.xyy),\n                        sdScene(p+e.yxy)-sdScene(p-e.yxy),\n                        sdScene(p+e.yyx)-sdScene(p-e.yyx)));\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord){\n  vec2 uv = (fragCoord - 0.5*iResolution.xy) / iResolution.y;\n  vec3 ro = vec3(0.0, 0.3, -3.5);\n  vec3 rd = normalize(vec3(uv, 1.7));\n  float t = 0.0; bool hit = false;\n  for(int i=0;i<80;i++){\n    float d = sdScene(ro + rd*t);\n    if(d < 0.001){ hit = true; break; }\n    t += d;\n    if(t > 20.0) break;\n  }\n  vec3 bg = mix(vec3(0.97,0.90,0.78), vec3(0.62,0.78,0.92), uv.y + 0.5);\n  vec3 col = bg;\n  if(hit){\n    vec3 p = ro + rd*t;\n    vec3 n = calcN(p);\n    vec3 l = normalize(vec3(LIGHT_DIR.x*cos(iTime*0.3), LIGHT_DIR.y, LIGHT_DIR.z));\n    float ndl = dot(n, l) * 0.5 + 0.5;\n    float w = max(fwidth(ndl), 0.002);          // antialias the ramp steps\n    float b1 = smoothstep(T1 - w, T1 + w, ndl);\n    float b2 = smoothstep(T2 - w, T2 + w, ndl);\n    vec3 base = (p.y < -0.99) ? vec3(0.75,0.85,0.70) : MID_COL;\n    vec3 ramp = mix(SHADOW_COL, base, b1);\n    ramp = mix(ramp, LIT_COL, b2 * step(-0.99, p.y));\n    float rim = pow(1.0 - max(dot(n, -rd), 0.0), RIM_POWER);\n    ramp += step(0.6, rim) * 0.25 * step(-0.99, p.y);\n    col = ramp;\n  }\n  fragColor = vec4(col, 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "toon-shading",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6832,
      "std": 0.1853,
      "unique_colors": 76
     },
     "image": "/specimens/toon-ramp-sdf-sphere.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "gooch-shading",
  "guilty-gear-xrd-shading",
  "posterization",
  "inverted-hull-outline"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "inverted-hull-outline",
   "why": "Hull pass supplies the ink line; toon pass supplies the flat colour bands on the mesh itself."
  },
  {
   "id": "screen-space-edge-outline",
   "why": "Edge pass draws creases and silhouettes over the toon-shaded colour buffer."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/npr/toon-shading.json"
 },
 "solves": [
  "want flat cartoon colour that reacts to light",
  "lighting gradients look too realistic for my stylised game",
  "need banded cel-shaded look",
  "3d character should look hand-inked"
 ],
 "authority": "Gooch et al., 1998",
 "url": "https://texelatlas.vercel.app/t/toon-shading/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 8.88,
  "tier": "moderate",
  "p50_ms": 0.2921,
  "p95_ms": 0.3031,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "toon-ramp-sdf-sphere",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 6.72,
   "tier": "moderate",
   "p50_ms": 0.1068,
   "p95_ms": 0.109,
   "speedup_x": 2.74,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 5.01,
   "tier": "light",
   "p50_ms": 0.1443,
   "p95_ms": 0.1449,
   "speedup_x": 2.02,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}