{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "coalesce",
  "type": "registry:ui",
  "title": "Coalesce",
  "description": "A metaball field: drifting cores fuse and split through smooth iso-surface blending, shaded as one mercury-chrome liquid with a circling glint and a silvery rim, the pointer riding along as an extra core. Original OGL fragment shader, offscreen-paused and reduced-motion-safe; the pointer core idles on its own when untouched.",
  "dependencies": [
    "ogl"
  ],
  "registryDependencies": [
    "https://crucibleui.com/r/ogl-canvas.json",
    "https://crucibleui.com/r/use-visibility-pause.json",
    "https://crucibleui.com/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/default/backgrounds/coalesce/coalesce.tsx",
      "type": "registry:ui",
      "target": "components/crucible/coalesce.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { OglCanvas } from \"@/lib/ogl-canvas\";\n\nexport interface CoalesceProps {\n  /** Number of drifting cores, clamped 1–10. The pointer adds one more. Default 5. */\n  count?: number;\n  /**\n   * Chrome pair `[shadow, highlight]`: the dark reflection tone and the\n   * bright sky tone the surface mirrors. Any hex strings. Defaults to\n   * mercury — silvery monochrome.\n   */\n  colors?: [string, string];\n  /** Drift speed multiplier. 1 = default. */\n  speed?: number;\n  /** Base core radius as a fraction of the container's height. Default 0.16. */\n  size?: number;\n  /** Overall brightness of the surface shading. 1 = default. */\n  intensity?: number;\n  /** Freeze the animation, holding the current frame. */\n  paused?: boolean;\n  /**\n   * Idle autoplay: after ~1.5s with no pointer present, a seeded ghost drifts\n   * slowly across the field and carries the extra pointer core, so thumbnails\n   * and heroes never look still; a real pointer takes over instantly. Never runs\n   * under reduced motion. Default true.\n   */\n  idle?: boolean;\n  /** Extra classes for the wrapper. Positioned `absolute inset-0 -z-10`. */\n  className?: string;\n}\n\nconst DEFAULT_COLORS: [string, string] = [\n  \"#262a31\", // mercury shadow\n  \"#e9edf3\", // mercury highlight\n];\n\n/** Hex → [r,g,b] in 0–1. Accepts #rgb / #rrggbb. */\nfunction hexToRgb(hex: string): [number, number, number] {\n  let h = hex.replace(\"#\", \"\").trim();\n  if (h.length === 3) {\n    h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];\n  }\n  const int = parseInt(h, 16);\n  if (Number.isNaN(int) || h.length !== 6) return [0.9, 0.92, 0.95];\n  return [((int >> 16) & 255) / 255, ((int >> 8) & 255) / 255, (int & 255) / 255];\n}\n\n/**\n * Metaball fragment shader. Classic sum-of-inverse-square fields thresholded\n * with smoothstep into one liquid silhouette; the analytic field gradient\n * doubles as a height-field normal for mirror-chrome shading (sharp fake\n * horizon, circling key light, rim on the blend edge). The pointer carries\n * one extra core. GLSL ES 1.00, constant loop bounds, grain-dithered.\n */\nconst FRAGMENT = /* glsl */ `\nprecision highp float;\n\nvarying vec2 vUv;\n\nuniform float uTime;\nuniform vec2 uResolution;\nuniform vec2 uPointer;\nuniform float uPointerActive;\n\nuniform vec3 uColorA; // shadow tone\nuniform vec3 uColorB; // highlight tone\nuniform float uCount;\nuniform float uSpeed;\nuniform float uSize;\nuniform float uIntensity;\n\nconst int MAX_N = 10;\n\nfloat hash11(float n) {\n  return fract(sin(n * 127.1 + 311.7) * 43758.5453);\n}\n\nfloat hash21(vec2 p) {\n  p = fract(p * vec2(217.13, 373.41));\n  p += dot(p, p + 31.73);\n  return fract(p.x * p.y);\n}\n\nvoid main() {\n  float aspect = uResolution.x / max(uResolution.y, 1.0);\n  vec2 p = vec2((vUv.x - 0.5) * aspect, vUv.y - 0.5);\n  float t = uTime * uSpeed;\n  float size = max(uSize, 0.02);\n  int count = int(clamp(uCount, 1.0, float(MAX_N)));\n\n  float f = 0.0;\n  vec2 grad = vec2(0.0);\n\n  // Sum of inverse-square fields, accumulating the analytic gradient\n  // alongside for shading. Constant loop bound with an early break.\n  for (int i = 0; i < MAX_N; i++) {\n    if (i >= count) break;\n    float fi = float(i);\n    float h1 = hash11(fi + 1.0);\n    float h2 = hash11(fi + 17.0);\n    float h3 = hash11(fi + 43.0);\n    float h4 = hash11(fi + 71.0);\n\n    // Each core drifts a slow seeded lissajous orbit around its anchor.\n    vec2 anchor = vec2((h1 - 0.5) * 0.8 * max(aspect, 1.0), (h2 - 0.5) * 0.5);\n    vec2 amp = vec2(0.14 + 0.22 * h4, 0.10 + 0.18 * h3);\n    vec2 c = anchor + amp * vec2(\n      sin(t * (0.28 + 0.5 * h3) + h1 * 6.2832),\n      cos(t * (0.22 + 0.45 * h4) + h2 * 6.2832)\n    );\n\n    float r = size * (0.55 + 0.7 * h3);\n    vec2 d = p - c;\n    float d2 = dot(d, d) + 1e-5;\n    float r2 = r * r;\n    f += r2 / d2;\n    grad -= 2.0 * r2 * d / (d2 * d2);\n  }\n\n  // The pointer carries one extra core; the harness lerps uPointer, so it\n  // trails with liquid lag and merges into the ambient field.\n  vec2 pc = vec2((uPointer.x - 0.5) * aspect, uPointer.y - 0.5);\n  vec2 pd = p - pc;\n  // Shrinks away when the pointer leaves instead of parking at center.\n  float pr = size * 0.8 * uPointerActive;\n  float pd2 = dot(pd, pd) + 1e-5;\n  f += pr * pr / pd2;\n  grad -= 2.0 * pr * pr * pd / (pd2 * pd2);\n\n  // Iso-surface: a crisp silhouette with a soft anti-aliased shoulder.\n  float mask = smoothstep(1.0, 1.16, f);\n\n  // Height-field normal: dividing the gradient by f^2 flattens the domes\n  // over the cores and keeps the tilt strongest at the blend edge. At fusion\n  // necks the raw slope spikes and, run through the sharp horizon term below,\n  // used to trace hard sawtooth contours across the interior. Soft-limiting the\n  // slope magnitude compresses those extreme neck values while leaving gentle\n  // dome slopes untouched, so the mirror reads as smooth liquid, not terraced.\n  vec2 gh = grad / (f * f);\n  float ghLen = length(gh);\n  gh /= 1.0 + ghLen * 0.6;\n  vec3 n = normalize(vec3(-gh * size * 0.85, 1.0));\n\n  // Mirror-chrome environment: bright sky above a soft dark horizon (a slightly\n  // wider band than a razor edge — enough to read as a horizon, gentle enough\n  // that rapid normal swings never quantize into hard steps).\n  float envY = n.y + 0.28 * n.x;\n  vec3 col = mix(uColorA, uColorB, smoothstep(-0.45, 0.6, envY));\n  float horizon = exp(-abs(envY - 0.02) * 5.5);\n  col = mix(col, uColorA * 0.45, horizon * 0.5);\n\n  // One slowly circling key light for the moving specular glint.\n  vec3 l = normalize(vec3(cos(t * 0.25) * 0.55, 0.7, 0.5));\n  float spec = pow(max(dot(n, l), 0.0), 48.0);\n  col += vec3(0.95, 0.97, 1.0) * spec * 0.9;\n\n  // Silvery rim hugging the blend edge, where necks form and snap.\n  float rim = smoothstep(1.0, 1.45, f) * (1.0 - smoothstep(1.45, 2.4, f));\n  col += uColorB * rim * 0.28;\n\n  col *= uIntensity;\n\n  // Film-grain dithering kills banding in both color and the alpha edge.\n  float grain = hash21(gl_FragCoord.xy + fract(t) * 61.0) - 0.5;\n  col += grain * (2.0 / 255.0);\n  float alpha = clamp(mask + grain * (1.5 / 255.0) * mask, 0.0, 1.0);\n\n  gl_FragColor = vec4(max(col, 0.0), alpha);\n}\n`;\n\n/**\n * Coalesce — a metaball field. Several blobby cores drift on slow seeded\n * orbits and fuse where they meet, rendered as one mercury-chrome liquid: a\n * mirror environment with a sharp horizon, a circling specular glint, and a\n * silvery rim on the blend edge. The pointer carries one extra core with\n * liquid lag. Transparent outside the surface, so it layers over any stage.\n * Built on the `ogl-canvas` harness: GPU-light, offscreen-paused,\n * reduced-motion-safe. Drop it inside any `relative` container; it fills\n * and sits behind content.\n */\nexport function Coalesce({\n  count = 5,\n  colors = DEFAULT_COLORS,\n  speed = 1,\n  size = 0.16,\n  intensity = 1,\n  paused = false,\n  idle = true,\n  className,\n}: CoalesceProps) {\n  const uniforms = React.useMemo(\n    () => ({\n      uColorA: { value: hexToRgb(colors[0]) },\n      uColorB: { value: hexToRgb(colors[1]) },\n      uCount: { value: count },\n      uSpeed: { value: speed },\n      uSize: { value: size },\n      uIntensity: { value: intensity },\n    }),\n    [colors, count, speed, size, intensity]\n  );\n\n  return (\n    <OglCanvas\n      aria-hidden\n      data-crucible=\"coalesce\"\n      className={cn(\"absolute inset-0 -z-10\", className)}\n      fragment={FRAGMENT}\n      uniforms={uniforms}\n      reducedMotionTime={8}\n      paused={paused}\n      ghost={idle}\n    />\n  );\n}\n"
    }
  ]
}