{
 "id": "threejs-tsl-node-materials",
 "title": "Three.js Shading Language (TSL) Node Materials",
 "aliases": [
  "TSL",
  "Three.js TSL"
 ],
 "domain": "materials",
 "subdomain": "shading-languages",
 "stage": "material",
 "realtime": "realtime",
 "platforms": [
  "webgpu",
  "webgl2",
  "mobile-gpu"
 ],
 "year_introduced": 2024,
 "inputs": [
  "material-params",
  "texture",
  "normals"
 ],
 "outputs": [
  "hdr-image",
  "gbuffer"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "A JavaScript-native composable shader node graph in Three.js that compiles identically to WGSL (WebGPU) and GLSL (WebGL2), replacing brittle `onBeforeCompile` string patching.",
 "how_it_works": "Instead of injecting raw GLSL substrings into internal `ShaderChunk` hooks—which break across engine releases—TSL expresses shading math, triplanar sampling, custom lighting, post-processing passes, and GPU compute kernels as typed JavaScript node expressions (`Fn`, `uniform`, `texture`, `triplanarTexture`, `positionWorld`, `normalView`). Three.js's node builder analyzes the graph, deduplicates varyings and bindings, and emits optimized WGSL for `WebGPURenderer` with automatic GLSL fallback when running on WebGL2.",
 "when_to_use": "Use for any custom material, procedural surface, custom post-processing node, or GPU compute shader in modern Three.js (`r170+`) instead of `onBeforeCompile` string patching.",
 "cost": {
  "class": "cheap",
  "notes": "Zero runtime overhead over hand-written shaders once compiled; enables WebGPU compute passes."
 },
 "failure_modes": [
  "Requires importing from `three/webgpu` and `three/tsl` rather than legacy `WebGLRenderer`-only bundles",
  "API names evolved rapidly across r165–r187; pin your Three.js version",
  "Shader compilation still incurs first-frame pipeline creation cost unless pre-warmed"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language",
   "type": "spec",
   "title": "Three.js Shading Language (TSL) Specification",
   "authors": [
    "Three.js Authors"
   ],
   "year": 2024,
   "venue": "Three.js Documentation",
   "url": "https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language",
   "ids": {
    "url": "https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language"
   },
   "license": null,
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language",
     "checked_at": "2026-12-02T02:40:24.869Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language",
     "checked_at": "2026-12-02T02:40:24.869Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-12-02T02:40:24.869Z",
   "title_sim": 1,
   "__bib": "authors2024three"
  }
 ],
 "presets": [
  {
   "id": "threejs-tsl-node-materials-live",
   "title": "TSL Node Material — composable triplanar procedural color + clearcoat Fresnel node graph",
   "runtime": "glsl-fragment",
   "params": {
    "P_SCALE": 6,
    "P_COAT": 0.85
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_SCALE = 6.0; // P Triplanar Scale [2.0, 14.0, 0.5]\nconst float P_COAT = 0.85; // P Clearcoat Weight [0.0, 1.0, 0.05]\n\nfloat hash21(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }\nfloat noise(vec2 p) {\n  vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash21(i), hash21(i + vec2(1, 0)), f.x), mix(hash21(i + vec2(0, 1)), hash21(i + vec2(1, 1)), f.x), f.y);\n}\nfloat triplanar(vec3 p, vec3 n) {\n  vec3 w = pow(abs(n), vec3(4.0)); w /= (w.x + w.y + w.z + 1e-5);\n  return noise(p.yz * P_SCALE) * w.x + noise(p.xz * P_SCALE) * w.y + noise(p.xy * P_SCALE) * w.z;\n}\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.0, 2.4), rd = normalize(vec3(uv, -1.55));\n  float b = dot(ro, rd), c = dot(ro, ro) - 0.76 * 0.76;\n  float disc = b * b - c;\n  vec3 col = mix(vec3(0.04, 0.06, 0.11), vec3(0.11, 0.15, 0.24), uv.y + 0.5);\n  if (disc > 0.0) {\n    vec3 p = ro + rd * (-b - sqrt(disc));\n    float a = iTime * 0.45;\n    mat2 R = mat2(cos(a), -sin(a), sin(a), cos(a));\n    vec3 rp = p; rp.xz = R * rp.xz;\n    vec3 n = normalize(p), rn = normalize(rp);\n    float pat = triplanar(rp, rn);\n    vec3 alb = mix(vec3(0.16, 0.26, 0.42), vec3(0.86, 0.54, 0.26), smoothstep(0.28, 0.72, pat));\n    vec3 lDir = normalize(vec3(0.6, 0.75, 0.5));\n    float diff = max(dot(n, lDir), 0.0);\n    float coatSpec = pow(max(dot(n, normalize(lDir - rd)), 0.0), 96.0) * P_COAT;\n    float fres = pow(1.0 - max(dot(n, -rd), 0.0), 4.0) * P_COAT;\n    col = alb * (0.24 + 0.85 * diff) + vec3(1.15, 1.05, 0.95) * coatSpec + vec3(0.35, 0.62, 0.98) * fres * 0.55;\n  }\n  col = (col * (2.51 * col + 0.03)) / (col * (2.43 * col + 0.59) + 0.14);\n  fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-12-02",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "threejs-tsl-node-materials",
     "runtime": "glsl-fragment",
     "checked_at": "2026-12-02T02:47:21.702Z",
     "execution_fidelity": "faithful-pass",
     "benchmark_eligible": true,
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime∈{0.0,1.5,4.0}",
     "stats": {
      "mean": 0.5584,
      "std": 0.1608,
      "unique_colors": 513,
      "nan_pixels": 0,
      "animated": true,
      "anim_delta": 0.0839
     },
     "image": "/specimens/threejs-tsl-node-materials-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "disney-principled-brdf",
  "triplanar-mapping",
  "clearcoat-layer"
 ],
 "extends": [
  "disney-principled-brdf"
 ],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (frontier expansion, authority-verified)",
  "sources_verified_at": "2026-12-02T02:40:24.869Z"
 },
 "solves": [],
 "authority": "Authors, 2024",
 "url": "https://texelatlas.vercel.app/t/threejs-tsl-node-materials/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true
}