{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"magma-crust","type":"registry:ui","title":"Magma Crust","description":"Slowly drifting plates of dark crust split by white-hot, pulsing cracks of molten metal. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/magma-crust.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface MagmaCrustProps {\n  /** Color of the cooled, dark crust. */\n  crustColor?: string;\n  /** Deep glow of the molten flow. */\n  glowColor?: string;\n  /** Hottest, brightest cracks. */\n  hotColor?: string;\n  /** Animation speed multiplier. */\n  speed?: number;\n  /** Plate size; higher gives smaller plates. */\n  scale?: number;\n  /** Width of the glowing cracks. */\n  crackWidth?: number;\n  intensity?: number;\n  className?: string;\n}\n\nconst FRAG = `precision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec3 uCrust;\nuniform vec3 uGlow;\nuniform vec3 uHot;\nuniform float uScale;\nuniform float uCrack;\nuniform float uIntensity;\n\nvec2 hash2(vec2 p){\n  p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));\n  return fract(sin(p) * 43758.5453);\n}\nfloat noise(vec2 p){\n  vec2 i = floor(p), f = fract(p);\n  f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash2(i).x, hash2(i + vec2(1, 0)).x, f.x), mix(hash2(i + vec2(0, 1)).x, hash2(i + vec2(1, 1)).x, f.x), f.y);\n}\nfloat fbm(vec2 p){\n  float v = 0.0, a = 0.5;\n  for (int i = 0; i < 4; i++) { v += a * noise(p); p = p * 2.07 + 13.7; a *= 0.5; }\n  return v;\n}\nvec2 site(vec2 cell, float t){ return 0.5 + 0.4 * sin(t + 6.2831 * hash2(cell)); }\n\n// x: distance to the nearest plate center, y: distance to the nearest plate border.\nvec2 plates(vec2 x, float t){\n  vec2 n = floor(x), f = fract(x), mg = vec2(0.0), mr = vec2(0.0);\n  float md = 8.0;\n  for (int j = -1; j <= 1; j++)\n  for (int i = -1; i <= 1; i++) {\n    vec2 g = vec2(float(i), float(j));\n    vec2 r = g + site(n + g, t) - f;\n    float d = dot(r, r);\n    if (d < md) { md = d; mr = r; mg = g; }\n  }\n  float bd = 8.0;\n  for (int j = -2; j <= 2; j++)\n  for (int i = -2; i <= 2; i++) {\n    vec2 g = mg + vec2(float(i), float(j));\n    vec2 r = g + site(n + g, t) - f;\n    vec2 dr = r - mr;\n    if (dot(dr, dr) > 1e-5) bd = min(bd, dot(0.5 * (mr + r), normalize(dr)));\n  }\n  return vec2(sqrt(md), bd);\n}\n\nvoid main(){\n  float t = uTime;\n  vec2 p = gl_FragCoord.xy / uRes.y * uScale;\n  p += vec2(t * 0.05, t * 0.018);\n  p += (vec2(fbm(p * 0.45 + t * 0.03), fbm(p * 0.45 + 7.3 - t * 0.03)) - 0.5) * 1.3;\n\n  vec2 v = plates(p, t * 0.22);\n  float heat = fbm(p * 0.55 - t * 0.06);\n  float pulse = 0.85 + 0.15 * sin(t * 1.3 + heat * 9.0);\n  float hot = smoothstep(0.25, 0.66, heat);\n  float w = uCrack * mix(0.25, 1.5, hot);\n  float crack = 1.0 - smoothstep(0.0, w, v.y);\n  float bloom = exp(-v.y / (w * 2.5 + 0.001));\n  // Finer fissures breaking up the hotter plates.\n  vec2 v2 = plates(p * 2.6 + 11.0, t * 0.3);\n  float fine = (1.0 - smoothstep(0.0, uCrack * 0.3, v2.y)) * smoothstep(0.4, 0.75, heat);\n  float rock = fbm(p * 5.0 + 3.1);\n\n  float h = crack * mix(0.45, 1.15, hot) * pulse + bloom * mix(0.12, 0.4, hot) + fine * 0.55\n          + smoothstep(0.5, 0.85, heat) * (1.0 - v.x) * 0.3;\n  h *= uIntensity;\n\n  vec3 col = uCrust * (0.3 + 1.6 * rock * rock) * (0.5 + 0.5 * smoothstep(0.0, 0.3, v.y));\n  col = mix(col, uGlow * 0.45, smoothstep(0.08, 0.4, h));\n  col = mix(col, uGlow, smoothstep(0.3, 0.65, h));\n  col = mix(col, uHot, smoothstep(0.62, 0.95, h));\n  col = mix(col, vec3(1.0, 0.97, 0.9), smoothstep(0.98, 1.35, h));\n  col += (fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 255.0;\n  gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);\n}`;\n\nfunction hexToRgb(hex: string) {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\ntype SetUniform = (name: string, v: number | number[], size?: number) => void;\n\n/** Full-screen-triangle WebGL runner: sizes to its host, pauses offscreen, one static frame under reduced motion. */\nfunction mountShader(host: HTMLElement, frag: string, frame: (set: SetUniform, dt: number) => void) {\n  const canvas = document.createElement(\"canvas\");\n  canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n  const gl = canvas.getContext(\"webgl\", { antialias: false, premultipliedAlpha: true });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\n  const stages: [number, string][] = [\n    [gl.VERTEX_SHADER, \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\"],\n    [gl.FRAGMENT_SHADER, frag],\n  ];\n  for (const [type, src] of stages) {\n    const s = gl.createShader(type)!;\n    gl.shaderSource(s, src);\n    gl.compileShader(s);\n    if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(s));\n    gl.attachShader(prog, s);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {\n    gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    return null;\n  }\n  gl.useProgram(prog);\n  gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n  gl.enableVertexAttribArray(0);\n  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n  host.appendChild(canvas);\n\n  const locs = new Map<string, WebGLUniformLocation | null>();\n  const setters = [gl.uniform1fv, gl.uniform2fv, gl.uniform3fv, gl.uniform4fv];\n  const set: SetUniform = (name, v, size) => {\n    if (!locs.has(name)) locs.set(name, gl.getUniformLocation(prog, name));\n    const arr = typeof v === \"number\" ? [v] : v;\n    setters[(size ?? arr.length) - 1].call(gl, locs.get(name)!, arr);\n  };\n\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  let raf = 0;\n  let last = 0;\n  const render = (dt: number) => {\n    set(\"uRes\", [canvas.width, canvas.height]);\n    frame(set, dt);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    render(last ? Math.min((now - last) / 1000, 0.1) : 0);\n    last = now;\n    raf = requestAnimationFrame(loop);\n  };\n  const resize = () => {\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    canvas.width = Math.max(1, Math.round(host.clientWidth * dpr));\n    canvas.height = Math.max(1, Math.round(host.clientHeight * dpr));\n    gl.viewport(0, 0, canvas.width, canvas.height);\n    render(0);\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(host);\n  const io = new IntersectionObserver(([e]) => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    last = 0;\n    if (e.isIntersecting && !reduced) raf = requestAnimationFrame(loop);\n  });\n  io.observe(host);\n\n  return {\n    redraw: () => {\n      if (!raf) render(0);\n    },\n    destroy: () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nexport function MagmaCrust({\n  crustColor = \"#1c100c\",\n  glowColor = \"#ff4d12\",\n  hotColor = \"#ffc861\",\n  speed = 1,\n  scale = 3,\n  crackWidth = 0.08,\n  intensity = 1,\n  className,\n}: MagmaCrustProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ crust: hexToRgb(crustColor), glow: hexToRgb(glowColor), hot: hexToRgb(hotColor), speed, scale, crackWidth, intensity });\n  const redraw = useRef<() => void>(undefined);\n\n  useEffect(() => {\n    opts.current = { crust: hexToRgb(crustColor), glow: hexToRgb(glowColor), hot: hexToRgb(hotColor), speed, scale, crackWidth, intensity };\n    redraw.current?.();\n  }, [crustColor, glowColor, hotColor, speed, scale, crackWidth, intensity]);\n\n  useEffect(() => {\n    let t = 9;\n    const shader = mountShader(host.current!, FRAG, (set, dt) => {\n      const o = opts.current;\n      t += dt * o.speed;\n      set(\"uTime\", t);\n      set(\"uCrust\", o.crust);\n      set(\"uGlow\", o.glow);\n      set(\"uHot\", o.hot);\n      set(\"uScale\", Math.max(o.scale, 0.2));\n      set(\"uCrack\", Math.max(o.crackWidth, 0.005));\n      set(\"uIntensity\", o.intensity);\n    });\n    if (!shader) return;\n    redraw.current = shader.redraw;\n    return () => {\n      redraw.current = undefined;\n      shader.destroy();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}