{
 "id": "implicit-cloth-integration",
 "title": "Implicit cloth integration (Baraff-Witkin)",
 "aliases": [
  "Large steps in cloth simulation"
 ],
 "domain": "simulation",
 "subdomain": "cloth",
 "stage": "simulation",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "cuda",
  "desktop-gpu"
 ],
 "year_introduced": 1998,
 "inputs": [
  "mesh"
 ],
 "outputs": [
  "mesh"
 ],
 "style": "photoreal",
 "dynamic": "dynamic",
 "summary": "Cloth simulation with implicit Euler on a triangle mesh with stretch/shear/bend energies, solved by modified conjugate gradient so large time steps stay stable.",
 "how_it_works": "Cloth is a triangle mesh whose internal forces come from condition functions (stretch and shear in the UV plane, bend across edges). Rather than integrate explicitly, the method uses implicit Euler and linearises forces, so each step solves a sparse system `(M - dt df/dv - dt^2 df/dx) dv = dt (f0 + dt df/dx v0)`. The authors solve it with a conjugate gradient variant that enforces per-vertex constraints (pins, contact planes) through a filtering step. Damping forces are included in the Jacobian. Collision is handled with penalty or constraint-filtered response. The result: stable, relatively stiff cloth at time steps of the order of a frame.",
 "when_to_use": "Choose it when you want physically grounded stiff cloth on CPU with large steps, e.g. offline garments. Prefer `position-based-dynamics` / `xpbd` for game-style cloth, `vertex-block-descent` for fast stiff cloth on GPUs, and `verlet-integration-constraints` for flags and capes where accuracy does not matter.",
 "cost": {
  "class": "heavy",
  "notes": "A sparse linear solve per step; cost grows with mesh size and stiffness. Not frame-budget friendly at film resolution."
 },
 "failure_modes": [
  "Implicit Euler adds numerical damping: cloth looks heavy and loses wrinkles",
  "CG solves with few iterations give rubbery or uneven stretch",
  "Collision handling is the weak part: cloth interpenetrates or locks",
  "Tuning stretch vs bend stiffness ratios is unintuitive"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/280814.280821",
   "type": "paper",
   "title": "Large steps in cloth simulation",
   "authors": [
    "David Baraff",
    "Andrew Witkin"
   ],
   "year": 1998,
   "venue": "Proceedings of the 25th annual conference on Computer graphics and interactive techniques  - SIGGRAPH '98",
   "url": "https://doi.org/10.1145/280814.280821",
   "ids": {
    "doi": "10.1145/280814.280821"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280821",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280821",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280821",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280821",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/280814.280821",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "baraff1998large"
  },
  {
   "role": "primary",
   "key": "doi:10.1145/2508363.2508406",
   "type": "paper",
   "title": "Fast simulation of mass-spring systems",
   "authors": [
    "Tiantian Liu",
    "Adam W. Bargteil",
    "James F. O'Brien",
    "Ladislav Kavan"
   ],
   "year": 2013,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/2508363.2508406",
   "ids": {
    "doi": "10.1145/2508363.2508406"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2508363.2508406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2508363.2508406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2508363.2508406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2508363.2508406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2508363.2508406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "liu2013fast"
  }
 ],
 "presets": [
  {
   "id": "implicit-cloth-integration-live",
   "title": "Implicit cloth integration (Baraff-Witkin) — 3D billowing woven drape over collider & stretch/shear stress map",
   "runtime": "glsl-fragment",
   "params": {
    "P_WIND_FORCE": 1,
    "P_BEND_STIFF": 0.65,
    "P_STRESS_SPLIT": 0.38,
    "P_DRAPE_SPEED": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_WIND_FORCE = 1.0; // P Wind Force [0.1, 2.2, 0.05]\nconst float P_BEND_STIFF = 0.65; // P Bend Stiffness [0.15, 1.4, 0.04]\nconst float P_STRESS_SPLIT = 0.38; // P Stress Map Split [0.0, 1.0, 0.02]\nconst float P_DRAPE_SPEED = 1.0; // P Motion Speed [0.0, 2.5, 0.05]\n\n// Evaluate hanging cloth z-displacement (pinned at top corners y = +0.72, draped over sphere at origin)\nfloat clothSurfaceZ(vec2 xy, float t, float wind, float stiff, out float strainOut) {\n  // Pin factor: 0 at top corners (-0.65, 0.72) and (0.65, 0.72), 1 freely billowing below\n  float dPinL = length(xy - vec2(-0.62, 0.68));\n  float dPinR = length(xy - vec2(0.62, 0.68));\n  float freeMask = smoothstep(0.04, 0.36, min(dPinL, dPinR));\n\n  // Stable Baraff-Witkin implicit-damped billowing harmonics (high stiffness dampens fine ripples)\n  float damp = exp(-stiff * 0.65);\n  float w1 = sin(xy.x * 3.4 - t * 2.1) * cos(xy.y * 2.5 - t * 1.45) * 0.14 * wind;\n  float w2 = sin(xy.x * 6.8 + xy.y * 4.2 - t * 3.2) * 0.055 * wind * damp;\n  float w3 = cos(xy.x * 11.2 - xy.y * 8.4 + t * 4.1) * 0.018 * wind * damp * damp;\n  // Natural catenary drape sag between pins\n  float catenary = -0.14 * (1.0 - (xy.x * xy.x) * 1.8) * smoothstep(-0.7, 0.7, xy.y);\n\n  float z = (w1 + w2 + w3 + catenary) * freeMask;\n\n  // Collision bulge over orbiting obsidian sphere behind cloth\n  vec2 spXY = vec2(0.24 * sin(t * 0.65), -0.08 + 0.12 * cos(t * 0.85));\n  float rSp = length(xy - spXY);\n  float bulge = 0.28 * exp(-rSp * rSp * 7.5);\n  // Radial compression folds radiating around the collider bulge\n  float radialFolds = 0.024 * cos(atan(xy.y - spXY.y, xy.x - spXY.x) * 7.0 + t * 0.8) * exp(-rSp * 3.2) * smoothstep(0.06, 0.24, rSp);\n  z += bulge + radialFolds;\n\n  // Approximate warp/weft tensile strain (highest near pin anchors and over collider dome)\n  float pinTension = exp(-min(dPinL, dPinR) * 4.2);\n  float domeTension = exp(-rSp * rSp * 6.5);\n  strainOut = clamp(pinTension * 0.92 + domeTension * 0.78 + abs(w2) * 3.2, 0.0, 1.0);\n  return z;\n}\n\n// Signed distance to finite cloth sheet with thickness\nfloat mapCloth(vec3 p, float t, float wind, float stiff) {\n  float st;\n  vec2 clampedXY = clamp(p.xy, vec2(-0.68, -0.68), vec2(0.68, 0.68));\n  float zSurf = clothSurfaceZ(clampedXY, t, wind, stiff, st);\n  vec2 edgeD = abs(p.xy) - vec2(0.68, 0.68);\n  float outXY = length(max(edgeD, 0.0));\n  float dz = abs(p.z - zSurf) - 0.014;\n  return (outXY > 0.0) ? sqrt(outXY * outXY + max(dz, 0.0) * max(dz, 0.0)) : dz;\n}\n\nvec3 calcClothNormal(vec3 p, float t, float wind, float stiff) {\n  float e = 0.008;\n  float s0, sx, sy;\n  float z0 = clothSurfaceZ(p.xy, t, wind, stiff, s0);\n  float zx = clothSurfaceZ(p.xy + vec2(e, 0.0), t, wind, stiff, sx);\n  float zy = clothSurfaceZ(p.xy + vec2(0.0, e), t, wind, stiff, sy);\n  return normalize(vec3(-(zx - z0) / e, -(zy - z0) / e, 1.0));\n}\n\nvec3 stressPalette(float s) {\n  s = clamp(s, 0.0, 1.0);\n  vec3 c0 = vec3(0.08, 0.24, 0.56);\n  vec3 c1 = vec3(0.14, 0.84, 0.72);\n  vec3 c2 = vec3(0.98, 0.74, 0.18);\n  vec3 c3 = vec3(0.98, 0.22, 0.34);\n  if (s < 0.35) return mix(c0, c1, s / 0.35);\n  if (s < 0.70) return mix(c1, c2, (s - 0.35) / 0.35);\n  return mix(c2, c3, (s - 0.70) / 0.30);\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 tAnim = iTime * max(P_DRAPE_SPEED, 0.0);\n  float wind = clamp(P_WIND_FORCE, 0.05, 2.4);\n  float stiff = clamp(P_BEND_STIFF, 0.1, 1.6);\n  float split = clamp(P_STRESS_SPLIT, 0.0, 1.0);\n\n  vec3 ro = vec3(0.18 * sin(tAnim * 0.28), 0.04, 2.25);\n  vec3 rd = normalize(vec3(uv, -1.55));\n\n  vec3 col = mix(vec3(0.03, 0.04, 0.065), vec3(0.09, 0.115, 0.165), smoothstep(-0.55, 0.55, uv.y));\n\n  float t = 0.4;\n  bool hit = false;\n  for (int i = 0; i < 64; i++) {\n    float d = mapCloth(ro + rd * t, tAnim, wind, stiff);\n    if (d < 0.0025) { hit = true; break; }\n    t += d * 0.65;\n    if (t > 6.5) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    vec3 n = calcClothNormal(p, tAnim, wind, stiff);\n    if (dot(n, -rd) < 0.0) n = -n;\n\n    float strain;\n    clothSurfaceZ(p.xy, tAnim, wind, stiff, strain);\n\n    // Fine procedural twill weave + structural warp/weft spring mesh lines\n    vec2 wuv = p.xy * 26.0;\n    float twill = 0.5 + 0.5 * sin(wuv.x * 3.14159 + sin(wuv.y * 3.14159)) * cos(wuv.y * 3.14159);\n    vec2 springG = abs(fract(p.xy * 11.0 - 0.5) - 0.5);\n    float springWire = smoothstep(0.055, 0.012, min(springG.x, springG.y));\n\n    vec3 l1 = normalize(vec3(0.62, 0.78, 0.68));\n    vec3 l2 = normalize(vec3(-0.72, -0.25, 0.55));\n    float diff1 = max(dot(n, l1), 0.0);\n    float diff2 = max(dot(n, l2), 0.0);\n    // Charlie / velvet sheen grazing highlight characteristic of woven cloth\n    float sheen = pow(1.0 - max(dot(n, -rd), 0.0), 2.6) * 0.46;\n\n    // Rich crimson-burgundy woven cloth (right) vs Baraff-Witkin warp/weft strain & spring grid (left)\n    vec3 fabricAlb = mix(vec3(0.58, 0.14, 0.18), vec3(0.76, 0.24, 0.26), twill * 0.55);\n    // Gold embroidered border hem\n    float hem = smoothstep(0.61, 0.65, max(abs(p.x), abs(p.y)));\n    fabricAlb = mix(fabricAlb, vec3(0.88, 0.70, 0.28), hem * 0.85);\n    vec3 fabricCol = fabricAlb * (0.24 + 0.86 * diff1 + 0.24 * diff2) + vec3(0.95, 0.68, 0.62) * sheen;\n\n    vec3 stressCol = stressPalette(strain) * (0.34 + 0.76 * diff1) + vec3(0.35, 0.55, 0.85) * sheen * 0.5;\n    stressCol = mix(stressCol, vec3(0.95, 0.98, 1.0), springWire * 0.42);\n\n    col = (screenX < split) ? stressCol : fabricCol;\n\n    // Highlight pinned top corner anchor grommets\n    float dPin = min(length(p.xy - vec2(-0.62, 0.68)), length(p.xy - vec2(0.62, 0.68)));\n    col = mix(col, vec3(1.0, 0.88, 0.38), smoothstep(0.055, 0.03, dPin));\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\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": "implicit-cloth-integration",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:31.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.595,
      "std": 0.1402,
      "min": 0.2057,
      "max": 0.9014
     },
     "image": "/specimens/implicit-cloth-integration-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "position-based-dynamics",
  "xpbd",
  "vertex-block-descent",
  "verlet-integration-constraints"
 ],
 "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/simulation/implicit-cloth-integration.json"
 },
 "solves": [
  "cloth simulation explodes with large time steps",
  "stiff fabric needs stable integration",
  "garment simulation jitters or blows up at low frame rates",
  "want realistic cloth that stretches and bends properly"
 ],
 "authority": "Baraff & Witkin, 1998",
 "url": "https://texelatlas.vercel.app/t/implicit-cloth-integration/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 26.31,
  "tier": "heavy",
  "p50_ms": 0.826,
  "p95_ms": 0.8981,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "implicit-cloth-integration-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 24.37,
   "tier": "heavy",
   "p50_ms": 0.385,
   "p95_ms": 0.389,
   "speedup_x": 2.15,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 17.11,
   "tier": "moderate",
   "p50_ms": 0.4927,
   "p95_ms": 0.5048,
   "speedup_x": 1.68,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}