{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "plasma-orb",
  "title": "Plasma Orb",
  "author": "HorizonX <https://horizonx.so>",
  "description": "A glass sphere with a live plasma core, ray-traced per pixel in WebGL2. Has an AI agent `state` prop: idle, listening, thinking, speaking, error.",
  "files": [
    {
      "path": "registry/drops/plasma-orb/plasma-orb.tsx",
      "content": "'use client';\n\nimport { useEffect, useRef, type CSSProperties } from 'react';\nimport { buildPlasmaOrb, DEFAULT_PLASMA_ORB_CONFIG, type PlasmaOrbConfig, type PlasmaOrbHandle } from './plasma-orb.engine.js';\n\n/** What an AI agent is doing. Each state sets the orb's energy and swirl; the engine eases between them. */\nexport type PlasmaOrbState = 'idle' | 'listening' | 'thinking' | 'speaking' | 'error';\n\n// Same looks as the Assistant Pill on horizonx.so.\nconst LOOK: Record<PlasmaOrbState, { energy: number; swirl: number }> = {\n  idle: { energy: 0.38, swirl: 0 },\n  listening: { energy: 0.62, swirl: 0 },\n  thinking: { energy: 0.72, swirl: 1 },\n  speaking: { energy: 0.58, swirl: 0.25 },\n  error: { energy: 0.2, swirl: 0 }\n};\n\nexport interface PlasmaOrbProps {\n  /** Agent state. `energy` and `swirl` override it when set. */\n  state?: PlasmaOrbState;\n  /** Orb diameter as a share of the short side, 0.3 to 0.8. */\n  size?: number;\n  background?: string;\n  center?: string;\n  glass?: string;\n  /** Rim and glow colour. */\n  rim?: string;\n  plasma?: string;\n  /** Sparkle accent colour. */\n  accent?: string;\n  /** 0 to 1. */\n  energy?: number;\n  /** 0 to 1. */\n  swirl?: number;\n  /** Bloom strength, 0 to 1.2. */\n  glow?: number;\n  /** 0 to 2. */\n  speed?: number;\n  /** `balanced` halves the ray-march work for weaker GPUs. */\n  detail?: 'high' | 'balanced';\n  paused?: boolean;\n  /** Hover lifts the plasma and a press sends a pulse. */\n  interactive?: boolean;\n  className?: string;\n  style?: CSSProperties;\n}\n\nconst clamp = (value: number | undefined, min: number, max: number, fallback: number) =>\n  typeof value === 'number' && Number.isFinite(value) ? Math.min(max, Math.max(min, value)) : fallback;\n\nfunction toConfig(props: PlasmaOrbProps): Partial<PlasmaOrbConfig> {\n  const d = DEFAULT_PLASMA_ORB_CONFIG;\n  const look = props.state ? LOOK[props.state] : { energy: d.energy, swirl: d.swirl };\n  return {\n    size: clamp(props.size, 0.3, 0.8, d.size),\n    background: props.background ?? d.background,\n    center: props.center ?? d.center,\n    glass: props.glass ?? d.glass,\n    rim: props.rim ?? d.rim,\n    plasma: props.plasma ?? d.plasma,\n    accent: props.accent ?? d.accent,\n    energy: clamp(props.energy, 0, 1, look.energy),\n    swirl: clamp(props.swirl, 0, 1, look.swirl),\n    glow: clamp(props.glow, 0, 1.2, d.glow),\n    speed: clamp(props.speed, 0, 2, d.speed),\n    detail: props.detail ?? d.detail,\n    animate: !props.paused,\n    interactive: props.interactive ?? true\n  };\n}\n\nconst CSS = `\n.pob-viewport{position:relative;box-sizing:border-box;width:100%;height:100%;overflow:hidden;isolation:isolate}\n.pob-stage{position:absolute;inset:0;touch-action:manipulation}\n.pob-status{position:absolute;inset:0;display:grid;place-items:center;padding:24px;color:#b4b4bc;font:14px/1.5 system-ui,sans-serif;text-align:center;pointer-events:none}\n.pob-status:empty{display:none}\n`;\n\n/** A glass sphere with a live plasma core, ray-traced per pixel in WebGL2. Fills its parent. */\nexport function PlasmaOrb({ className, style, ...props }: PlasmaOrbProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const engine = useRef<PlasmaOrbHandle | null>(null);\n  const config = toConfig(props);\n  const latest = useRef(config);\n  const key = JSON.stringify(config);\n\n  // Declared before the mount effect so a remount always starts from the newest props.\n  useEffect(() => {\n    latest.current = config;\n    engine.current?.update(config);\n  }, [key]); // eslint-disable-line react-hooks/exhaustive-deps\n\n  useEffect(() => {\n    const handle = buildPlasmaOrb(root.current!, latest.current);\n    engine.current = handle;\n    return () => { handle(); engine.current = null; };\n  }, []);\n\n  return (\n    <>\n      <style href=\"hx-drops-plasma-orb\" precedence=\"default\">{CSS}</style>\n      <div\n        ref={root}\n        className={className ? `pob-viewport ${className}` : 'pob-viewport'}\n        style={{ background: config.background, ...style }}\n      >\n        <div className=\"pob-stage\" />\n        <span className=\"pob-status\" role=\"status\" />\n      </div>\n    </>\n  );\n}\n\nexport default PlasmaOrb;\n",
      "type": "registry:component"
    },
    {
      "path": "registry/drops/plasma-orb/plasma-orb.engine.js",
      "content": "// HorizonX Drops: this engine is copied unchanged from the HorizonX editor\n// (horizonx-frontend src/lib/tools/plasmaOrb.engine.js at 00b7cec).\n// Licensed under the HorizonX Drops License in this repository's LICENSE file.\n// Do not edit here: change it in horizonx-frontend and run scripts/sync-from-horizonx.mjs.\n// --- end of HorizonX Drops banner ---\n/**\n * Plasma Orb: a thick glass sphere ray-traced per pixel, with a volumetric\n * plasma core inside. Original HorizonX code, no libraries and no assets, so\n * the editor and the HTML/React/Svelte exports run the same file.\n *\n *   1. The outer glass reflects a dark studio and refracts the view ray into a\n *      0.1-thick shell (index 1.45), so the edge compresses into a bright band\n *      and the inside is magnified.\n *   2. Inside the shell the ray marches through emissive plasma: a white-hot\n *      core with radial fibres, and electric webs drawn on four wobbling\n *      shells (domain-warped noise from a 3D texture baked on the GPU).\n *   3. At the far inner wall a shorter march follows the reflected ray, so the\n *      plasma shows again as ghost reflections near the rim.\n *   4. A mip-chain bloom adds the halo; a per-channel exponential tone map\n *      lets the hottest light burn to white while the rest keeps its hue.\n */\n\nexport const DEFAULT_PLASMA_ORB_CONFIG = {\n  viewportWidth: 620,\n  viewportHeight: 620,\n  size: 0.55,\n  background: '#000000',\n  center: '#1a4fe6',\n  glass: '#4fa8ff',\n  rim: '#92dcff',\n  plasma: '#6fd0ff',\n  accent: '#ff8fc8',\n  energy: 0.4,\n  swirl: 0,\n  glow: 0.55,\n  speed: 1,\n  detail: 'high',\n  animate: true,\n  interactive: true\n};\n\n// March samples and the cap on the ray-traced buffer, per detail level.\nconst DETAIL = { high: { steps: 36, maxSide: 1100 }, balanced: { steps: 22, maxSide: 640 } };\n\nconst ORB_VERT = `#version 300 es\nin vec2 a; out vec2 vUv;\nvoid main(){ vUv = a * 0.5 + 0.5; gl_Position = vec4(a, 0.0, 1.0); }`;\n\n// Tileable Perlin noise, four independent fields in RGBA, baked once into a 3D texture.\nconst ORB_NOISE = `#version 300 es\nprecision highp float;\nuniform float uZ, uN, uP;\nout vec4 o;\nvec3 grad(vec3 i, float s){\ni = mod(i, uP);\nvec3 h = fract(sin(vec3(dot(i, vec3(127.1, 311.7, 74.7)), dot(i, vec3(269.5, 183.3, 246.1)), dot(i, vec3(113.5, 271.9, 124.6))) + s * 17.31) * 43758.5453);\nreturn normalize(h * 2.0 - 1.0 + 1e-5);\n}\nfloat pn(vec3 p, float s){\nvec3 i = floor(p), f = fract(p);\nvec3 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\nfloat n000 = dot(grad(i, s), f);\nfloat n100 = dot(grad(i + vec3(1, 0, 0), s), f - vec3(1, 0, 0));\nfloat n010 = dot(grad(i + vec3(0, 1, 0), s), f - vec3(0, 1, 0));\nfloat n110 = dot(grad(i + vec3(1, 1, 0), s), f - vec3(1, 1, 0));\nfloat n001 = dot(grad(i + vec3(0, 0, 1), s), f - vec3(0, 0, 1));\nfloat n101 = dot(grad(i + vec3(1, 0, 1), s), f - vec3(1, 0, 1));\nfloat n011 = dot(grad(i + vec3(0, 1, 1), s), f - vec3(0, 1, 1));\nfloat n111 = dot(grad(i + vec3(1, 1, 1), s), f - vec3(1, 1, 1));\nreturn mix(mix(mix(n000, n100, u.x), mix(n010, n110, u.x), u.y), mix(mix(n001, n101, u.x), mix(n011, n111, u.x), u.y), u.z);\n}\nvoid main(){\nvec3 p = vec3(gl_FragCoord.xy, uZ + 0.5) / uN * uP;\no = clamp(vec4(pn(p, 0.0), pn(p, 1.0), pn(p, 2.0), pn(p, 3.0)) * 0.8 + 0.5, 0.0, 1.0);\n}`;\n\nconst ORB_SCENE = `#version 300 es\nprecision highp float;\nprecision highp sampler3D;\nuniform sampler3D uNoise;\nuniform vec2 uRes;\nuniform float uTime, uLevel, uEnergy, uWarm, uSwirl, uSpin, uSteps, uEnc, uS;\nuniform float uDetail, uLineW, uCore;   // web scale, strand width and core weight; 1 = the full-size orb\nuniform vec3 uDeep, uBody, uGlow, uPlasma, uAccent, uBg;\nout vec4 o;\n\nconst float IOR = 1.45;\nconst float RI = 0.9;        // inner radius of the glass shell (outer radius is 1)\nconst float CAM = 4.0;       // camera distance\n\nfloat T;\nmat3 ROT;\nvec3 cDeep, cBody, cGlow, cPlasma, cAccent, cHot;\n\nvec4 nz(vec3 p){ return (texture(uNoise, p * (1.0 / 12.0)) - 0.5) * 2.5; }\nfloat hash12(vec2 p){ vec3 p3 = fract(vec3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }\nmat3 rotY(float a){ float c = cos(a), s = sin(a); return mat3(c, 0.0, -s, 0.0, 1.0, 0.0, s, 0.0, c); }\nmat3 rotX(float a){ float c = cos(a), s = sin(a); return mat3(1.0, 0.0, 0.0, 0.0, c, s, 0.0, -s, c); }\nvec3 tint(vec3 c, vec3 w){ return mix(c, w, uWarm); }\nvec3 sat(vec3 c, float k){ return max(mix(vec3(dot(c, vec3(0.299, 0.587, 0.114))), c, k), 0.0); }\n\n// Soft light filling the gas: the white-hot core, radial streaks and faint crackle.\nvec3 plasma(vec3 p){\nvec3 q = ROT * (p / RI);\nfloat r = length(q);\nvec3 dir = q / max(r, 1e-3);\nvec4 c = nz(q * 4.0 + vec3(T * 0.11, T * 0.07, 0.0) + 3.7);\nfloat fn = nz(dir * 11.0 * uDetail + vec3(0.0, 0.0, T * 0.12)).y;                          // radial streaks\nfloat fib = exp(-fn * fn * 160.0 / (uLineW * uLineW)) * exp(-r * 3.0) * smoothstep(0.03, 0.12, r);\nfloat haze = exp(-pow(r / 0.7, 2.0)) * (0.05 + 0.18 * exp(-abs(c.x) * 10.0));\nfloat core = exp(-r * r * 90.0) * 3.5 + exp(-r * r * 16.0) * 0.3;\nfloat drive = 1.0 + 0.9 * uEnergy + 1.8 * uLevel;\nreturn cPlasma * haze * drive * uCore\n     + mix(cPlasma, cHot, 0.35) * fib * 2.4 * drive\n     + cHot * core * uCore * (0.8 + 0.5 * uEnergy + 1.4 * uLevel);\n}\n\nvec3 march(vec3 ro, vec3 rd, float len, float n, float jit){\nfloat dt = len / n;\nvec3 acc = vec3(0.0);\nfor (int i = 0; i < 128; i++) {\n  if (float(i) >= n) break;\n  acc += plasma(ro + rd * (dt * (float(i) + jit)));\n}\nreturn acc * dt;\n}\n\n// Anti-aliased line where a noise field crosses zero, plus a soft glow around it.\nfloat line(float n, float w){ float fw = fwidth(n); return 1.0 - smoothstep(w, w + fw * 1.2 + 1e-4, abs(n)) + exp(-abs(n) * 9.0) * 0.12; }\n\n// The electric web: crisp lines drawn on a thin shell. dir is the point on the shell.\nfloat web(vec3 dir, float seed, float scale, float open){\nvec3 x = dir * scale * uDetail + seed;\nvec4 w = nz(x * 0.5 + vec3(0.0, T * 0.03, T * 0.02));\nx += w.xyz * 0.45;\nfloat n1 = nz(x + vec3(T * 0.05, 0.0, -T * 0.03)).x;\nfloat n2 = nz(x * 2.2 + vec3(0.0, -T * 0.07, T * 0.04) + 7.1).y;\nfloat n3 = nz(x * 5.0 + vec3(T * 0.1, T * 0.06, 0.0) + 3.7).z;\nfloat l1 = line(n1, 0.035 * uLineW), l2 = line(n2, 0.03 * uLineW), l3 = line(n3, 0.03 * uLineW);\nfloat lit = 0.35 + 1.3 * smoothstep(-0.3, 0.7, w.w);                             // some strands burn brighter\nfloat holes = smoothstep(-0.05, 0.3, nz(x * 0.8 + 11.3).w + 0.5 - open);\nreturn ((l1 + l2 * 0.7) * lit + l3 * 0.45 * (0.25 + l1 + l2) + 0.012) * holes;\n}\n\n// All shells crossed by the ray segment [ro, ro + rd * len]; front and back crossings both glow.\nvec3 shells(vec3 ro, vec3 rd, float len){\nvec3 acc = vec3(0.0);\nfor (int k = 0; k < 4; k++) {\n  float fk = float(k);\n  float R = 0.3 + 0.17 * fk + 0.045 * nz(rd * 2.3 + vec3(fk * 3.1, 0.0, T * 0.05)).x;   // wobble so no shell draws a perfect circle\n  float b = dot(ro, rd), h = b * b - (dot(ro, ro) - R * R);\n  if (h <= 0.0) continue;\n  float sq = sqrt(h);\n  float bright = (1.6 - 0.32 * fk) * (1.0 + 0.9 * uEnergy + 1.8 * uLevel);\n  mat3 rot = ROT * rotY(fk * 1.7 + T * 0.02 * fk);\n  for (int j = 0; j < 2; j++) {\n    float t = j == 0 ? -b - sq : -b + sq;\n    if (t < 0.0 || t > len) continue;\n    vec3 n = (ro + rd * t) / R;\n    float cv = abs(dot(rd, n));\n    float slant = min(1.0 / max(cv, 0.08), 2.5) * smoothstep(0.0, 0.3 + 0.25 * (1.0 - fk / 3.0), cv);      // brighter when slanted, but fade the shell's own outline\n    float e = web(rot * n, fk * 5.3, 1.6 + fk * 0.35, fk * 0.22) * bright * slant * 0.3 * (j == 0 ? 1.0 : 0.65);\n    acc += mix(cPlasma, cHot, clamp(0.55 - fk * 0.18, 0.0, 1.0)) * e;\n  }\n}\nreturn acc;\n}\n\n// A dark studio: a big softbox up-left, a thin strip light on the right, the orb's own glow below.\nvec3 env(vec3 d){\nvec3 c = vec3(0.0);\nvec3 L = normalize(vec3(-0.55, 0.72, 0.45));\nvec3 U = normalize(cross(L, vec3(0.0, 0.0, 1.0))), V = cross(U, L);\nfloat k = dot(d, L);\nvec2 sp = vec2(dot(d, U), dot(d, V)) / max(k, 1e-3);\nc += vec3(0.95, 0.97, 1.0) * step(0.0, k) * (1.0 - smoothstep(0.3, 0.38, abs(sp.x))) * (1.0 - smoothstep(0.14, 0.2, abs(sp.y))) * 1.4;\nvec3 L2 = normalize(vec3(0.92, -0.1, 0.38));\nvec3 V2 = normalize(cross(L2, vec3(0.0, 1.0, 0.0)));\nfloat k2 = dot(d, L2);\nvec2 sp2 = vec2(d.y, dot(d, V2)) / max(k2, 1e-3);\nc += mix(vec3(1.0), cGlow, 0.5) * step(0.0, k2) * (1.0 - smoothstep(0.5, 0.75, abs(sp2.x))) * (1.0 - smoothstep(0.025, 0.05, abs(sp2.y))) * 1.1;\nc += cGlow * smoothstep(0.0, -0.8, d.y) * 0.18;\nreturn c;\n}\n\nvoid main(){\nvec2 uv = (gl_FragCoord.xy * 2.0 - uRes) / uRes.y;\nfloat px = 2.0 / uRes.y;\nT = uTime;\nROT = rotY(T * 0.07 + uSpin * 2.0 + uSwirl * 0.0) * rotX(0.35 + 0.12 * sin(T * 0.13));\n\ncDeep   = tint(uDeep,   vec3(0.30, 0.12, 0.04));\ncBody   = tint(uBody,   vec3(0.62, 0.34, 0.14));\ncGlow   = tint(uGlow,   vec3(1.00, 0.62, 0.28));\ncPlasma = tint(sat(uPlasma, 1.6), vec3(1.00, 0.68, 0.32));\ncAccent = tint(uAccent, vec3(1.00, 0.85, 0.60));\ncHot    = tint(mix(uGlow, vec3(1.0), 0.85), vec3(1.00, 0.94, 0.84));\n\nfloat r2d = length(uv);\nfloat cov = clamp((uS - r2d) / px + 0.5, 0.0, 1.0);\nvec3 col = vec3(0.0);\nif (cov > 0.0) {\n  vec2 us = uv * min(1.0, (uS - px * 0.5) / max(r2d, 1e-5));     // keep edge pixels on the sphere\n  float tanA = 1.0 / sqrt(CAM * CAM - 1.0);\n  vec3 ro = vec3(0.0, 0.0, CAM);\n  vec3 rd = normalize(vec3(us * (tanA / uS), -1.0));\n  float b = dot(ro, rd), h = max(b * b - (dot(ro, ro) - 1.0), 0.0);\n  vec3 P1 = ro + rd * (-b - sqrt(h));\n  vec3 N1 = normalize(P1 + 0.03 * nz(P1 * 2.2 + vec3(0.0, T * 0.08, 0.0)).xyz);   // glass is not perfectly round\n  float cosi = clamp(-dot(rd, N1), 0.0, 1.0);\n  float F = 0.04 + 0.96 * pow(1.0 - cosi, 5.0);\n  vec3 refl = env(reflect(rd, N1));\n  vec3 T1 = refract(rd, N1, 1.0 / IOR);\n  float jit = hash12(gl_FragCoord.xy);\n\n  float ang = atan(P1.y, P1.x);\n  vec2 ring = vec2(cos(ang), sin(ang));\n  float caus = nz(vec3(ring * 1.4, T * 0.22 + 3.0)).x;                     // caustic patches travelling round the rim\n  float lit = 0.45 + 1.4 * smoothstep(-0.1, 0.7, caus);\n\n  float b2 = dot(P1, T1), h2 = b2 * b2 - (dot(P1, P1) - RI * RI);\n  float tIn = -b2 - sqrt(max(h2, 0.0));\n  vec3 inside = vec3(0.0);\n  if (h2 > 0.0 && tIn > 0.0) {\n    vec3 P2 = P1 + T1 * tIn;\n    vec3 T2 = refract(T1, P2 / RI, IOR);\n    if (dot(T2, T2) > 1e-4) {\n      float len = -2.0 * dot(P2, T2);\n      inside = march(P2, T2, len, uSteps, jit) + shells(P2, T2, len);\n      vec3 P3 = P2 + T2 * len;\n      vec3 n3 = P3 / RI;\n      vec3 R3 = reflect(T2, n3);\n      float F3 = 0.035 + 0.965 * pow(1.0 - clamp(dot(T2, n3), 0.0, 1.0), 5.0);\n      float len3 = -2.0 * dot(P3, R3);\n      inside += (march(P3, R3, len3, max(6.0, floor(uSteps * 0.3)), jit) + shells(P3, R3, len3)) * F3 * 2.6;\n      // Sparks: tiny points scattered in the gas, seen through the refraction.\n      vec2 g = (ROT * P2).xy * 22.0 * uDetail;\n      vec2 id = floor(g), f = fract(g) - 0.5;\n      float hs = hash12(id);\n      vec2 off = vec2(hash12(id + 7.3), hash12(id + 1.9)) - 0.5;\n      vec2 sd = f - off * 0.7;\n      float spark = step(0.86, hs) * exp(-dot(sd, sd) * 160.0) * (0.5 + 0.5 * sin(T * 3.0 + hs * 60.0));\n      inside += mix(cHot, cAccent, step(0.95, hs)) * spark * 1.4 * smoothstep(0.95, 0.4, length(P2.xy));\n    }\n  }\n  // Dark tinted glass behind the plasma, then a thin rim that the plasma light leaks into.\n  float e = clamp(r2d / uS, 0.0, 1.0);\n  inside = inside * mix(vec3(1.0), cBody, 0.25) + uBg * 0.7 + cDeep * 0.07 * (1.0 + uEnergy);\n  float rim = pow(e, 10.0) * 0.55 + pow(e, 40.0) * 1.6;\n  vec3 rimCol = mix(cBody, cGlow, 0.6) * rim * lit * (0.8 + 0.5 * uEnergy + 1.0 * uLevel);\n  col = refl * F + (1.0 - F) * (inside + rimCol);\n}\no = vec4(col * cov * uEnc, cov);\n}`;\n\nconst ORB_DOWN = `#version 300 es\nprecision highp float;\nuniform sampler2D uTex; uniform vec2 uTexel; uniform float uPre, uThresh;\nin vec2 vUv; out vec4 o;\nvec3 s(vec2 d){ return texture(uTex, vUv + d * uTexel).rgb; }\nvoid main(){\nvec3 c = s(vec2(0.0)) * 0.125\n  + (s(vec2(-2.0, 2.0)) + s(vec2(2.0, 2.0)) + s(vec2(-2.0, -2.0)) + s(vec2(2.0, -2.0))) * 0.03125\n  + (s(vec2(0.0, 2.0)) + s(vec2(-2.0, 0.0)) + s(vec2(2.0, 0.0)) + s(vec2(0.0, -2.0))) * 0.0625\n  + (s(vec2(-1.0, 1.0)) + s(vec2(1.0, 1.0)) + s(vec2(-1.0, -1.0)) + s(vec2(1.0, -1.0))) * 0.125;\nif (uPre > 0.5) {\n  float br = max(c.r, max(c.g, c.b));\n  float k = clamp(br - uThresh * 0.5, 0.0, uThresh); k = k * k / (2.0 * uThresh + 1e-5);\n  c *= max(k, br - uThresh) / max(br, 1e-5);\n}\no = vec4(c, 1.0);\n}`;\n\nconst ORB_UP = `#version 300 es\nprecision highp float;\nuniform sampler2D uTex; uniform vec2 uTexel;\nin vec2 vUv; out vec4 o;\nvec3 s(vec2 d){ return texture(uTex, vUv + d * uTexel).rgb; }\nvoid main(){\nvec3 c = s(vec2(0.0)) * 4.0\n  + (s(vec2(0.0, 1.0)) + s(vec2(-1.0, 0.0)) + s(vec2(1.0, 0.0)) + s(vec2(0.0, -1.0))) * 2.0\n  + (s(vec2(-1.0, 1.0)) + s(vec2(1.0, 1.0)) + s(vec2(-1.0, -1.0)) + s(vec2(1.0, -1.0)));\no = vec4(c / 16.0, 1.0);\n}`;\n\nconst ORB_COMPOSITE = `#version 300 es\nprecision highp float;\nuniform sampler2D uScene, uBloom; uniform float uBloomK, uExposure, uDec;\nin vec2 vUv; out vec4 o;\nfloat hash12(vec2 p){ vec3 p3 = fract(vec3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }\nvoid main(){\nvec4 sc = texture(uScene, vUv);\nfloat fade = 1.0 - smoothstep(0.62, 0.98, length(vUv * 2.0 - 1.0));  // halo dies out before the canvas edge, never a square\nvec3 hdr = (sc.rgb + texture(uBloom, vUv).rgb * uBloomK * fade) * uDec;\nvec3 m = 1.0 - exp(-hdr * uExposure);\nfloat lit = max(m.r, max(m.g, m.b));\nm += (hash12(gl_FragCoord.xy) - 0.5) / 255.0 * smoothstep(0.004, 0.03, lit);   // dither only where there is light\nm = max(m, 0.0);\no = vec4(m, clamp(max(sc.a, max(m.r, max(m.g, m.b))), 0.0, 1.0));\n}`;\n\nexport const hexToRgb = (hex) => {\n  const h = /^#[0-9a-f]{6}$/i.test(hex) ? hex : '#000000';\n  return [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16) / 255);\n};\n\n// Returns null when WebGL2 or the shaders are unavailable. Shared with the\n// Assistant Pill, which draws the same orb inside its button.\nexport function createRenderer(cv) {\n  const gl = cv.getContext('webgl2', { alpha: true, premultipliedAlpha: true, antialias: false, powerPreference: 'high-performance' });\n  if (!gl) return null;\n  let res = null, lost = false, adapt = 1, slowFor = 0, fastFor = 0;\n\n  function init() {\n    const half = !!gl.getExtension('EXT_color_buffer_float');\n    const FMT = half ? [gl.RGBA16F, gl.HALF_FLOAT] : [gl.RGBA8, gl.UNSIGNED_BYTE];\n    const compile = (type, src) => {\n      const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s);\n      if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s));\n      return s;\n    };\n    const vs = compile(gl.VERTEX_SHADER, ORB_VERT);\n    const program = (src) => {\n      const p = gl.createProgram();\n      gl.attachShader(p, vs); gl.attachShader(p, compile(gl.FRAGMENT_SHADER, src));\n      gl.bindAttribLocation(p, 0, 'a');\n      gl.linkProgram(p);\n      if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p));\n      const u = {};\n      const n = gl.getProgramParameter(p, gl.ACTIVE_UNIFORMS);\n      for (let i = 0; i < n; i++) { const name = gl.getActiveUniform(p, i).name; u[name] = gl.getUniformLocation(p, name); }\n      return { p, u };\n    };\n    const P = { noise: program(ORB_NOISE), scene: program(ORB_SCENE), down: program(ORB_DOWN), up: program(ORB_UP), comp: program(ORB_COMPOSITE) };\n\n    const vao = gl.createVertexArray(); gl.bindVertexArray(vao);\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\n    // Bake the noise volume on the GPU, one slice per draw.\n    const N = 96;\n    const noise = gl.createTexture();\n    gl.bindTexture(gl.TEXTURE_3D, noise);\n    gl.texImage3D(gl.TEXTURE_3D, 0, FMT[0], N, N, N, 0, gl.RGBA, FMT[1], null);\n    for (const [k, v] of [[gl.TEXTURE_MIN_FILTER, gl.LINEAR], [gl.TEXTURE_MAG_FILTER, gl.LINEAR], [gl.TEXTURE_WRAP_S, gl.REPEAT], [gl.TEXTURE_WRAP_T, gl.REPEAT], [gl.TEXTURE_WRAP_R, gl.REPEAT]]) gl.texParameteri(gl.TEXTURE_3D, k, v);\n    const fb = gl.createFramebuffer();\n    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);\n    gl.viewport(0, 0, N, N);\n    gl.useProgram(P.noise.p);\n    gl.uniform1f(P.noise.u.uN, N); gl.uniform1f(P.noise.u.uP, 12);\n    for (let z = 0; z < N; z++) {\n      gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, noise, 0, z);\n      gl.uniform1f(P.noise.u.uZ, z);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    }\n    gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n    res = { P, FMT, enc: half ? 1 : 0.125, targets: null, key: '', noise };\n  }\n\n  function target(w, h) {\n    const tex = gl.createTexture();\n    gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.texImage2D(gl.TEXTURE_2D, 0, res.FMT[0], w, h, 0, gl.RGBA, res.FMT[1], null);\n    for (const [k, v] of [[gl.TEXTURE_MIN_FILTER, gl.LINEAR], [gl.TEXTURE_MAG_FILTER, gl.LINEAR], [gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE], [gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE]]) gl.texParameteri(gl.TEXTURE_2D, k, v);\n    const fb = gl.createFramebuffer();\n    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);\n    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);\n    return { tex, fb, w, h };\n  }\n  function ensureTargets(sw, sh) {\n    const key = sw + 'x' + sh;\n    if (res.key === key) return;\n    if (res.targets) for (const t of [res.targets.scene, ...res.targets.mips]) { gl.deleteTexture(t.tex); gl.deleteFramebuffer(t.fb); }\n    const mips = [];\n    let w = sw, h = sh;\n    for (let i = 0; i < 6 && Math.min(w, h) > 8; i++) { w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); mips.push(target(w, h)); }\n    res.targets = { scene: target(sw, sh), mips };\n    res.key = key;\n  }\n  const pass = (prog, t) => { gl.useProgram(prog.p); gl.bindFramebuffer(gl.FRAMEBUFFER, t ? t.fb : null); gl.viewport(0, 0, t ? t.w : cv.width, t ? t.h : cv.height); };\n  const bindTex = (unit, tex, loc) => { gl.activeTexture(gl.TEXTURE0 + unit); gl.bindTexture(gl.TEXTURE_2D, tex); gl.uniform1i(loc, unit); };\n\n  const onLost = (e) => { e.preventDefault(); lost = true; res = null; };\n  const onRestored = () => { lost = false; try { init(); } catch { res = null; } };\n  cv.addEventListener('webglcontextlost', onLost);\n  cv.addEventListener('webglcontextrestored', onRestored);\n  try { init(); } catch { return null; }\n\n  return {\n    // s: { time, dt, level, energy, swirl, spin, colors, sphere, steps, maxSide, bloom }\n    // Optional: warm (0-1 tint), webScale, strand, core (1 each = the full-size orb).\n    draw(s) {\n      if (lost || !res) return false;\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const W = Math.max(2, Math.round(cv.clientWidth * dpr)), H = Math.max(2, Math.round(cv.clientHeight * dpr));\n      if (cv.width !== W || cv.height !== H) { cv.width = W; cv.height = H; }\n\n      // Keep frames smooth: shrink the ray-traced buffer when frames run long, grow it back when there is headroom.\n      if (s.dt) {\n        if (s.dt > 1 / 40) { slowFor += s.dt; fastFor = 0; } else if (s.dt < 1 / 55) { fastFor += s.dt; slowFor = 0; }\n        if (slowFor > 0.6 && adapt > 0.5) { adapt = Math.max(0.5, adapt * 0.85); slowFor = 0; }\n        if (fastFor > 3 && adapt < 1) { adapt = Math.min(1, adapt * 1.1); fastFor = 0; }\n      }\n      const scale = Math.min(1, s.maxSide / Math.max(W, H)) * adapt;\n      const sw = Math.max(16, Math.round(W * scale)), sh = Math.max(16, Math.round(H * scale));\n      ensureTargets(sw, sh);\n      const { P, targets } = res;\n      gl.disable(gl.BLEND);\n\n      pass(P.scene, targets.scene);\n      const u = P.scene.u;\n      gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_3D, res.noise); gl.uniform1i(u.uNoise, 0);\n      gl.uniform2f(u.uRes, sw, sh);\n      gl.uniform1f(u.uTime, s.time); gl.uniform1f(u.uLevel, s.level); gl.uniform1f(u.uEnergy, s.energy);\n      gl.uniform1f(u.uWarm, s.warm ?? 0); gl.uniform1f(u.uSwirl, s.swirl); gl.uniform1f(u.uSpin, s.spin);\n      gl.uniform1f(u.uDetail, s.webScale ?? 1); gl.uniform1f(u.uLineW, s.strand ?? 1); gl.uniform1f(u.uCore, s.core ?? 1);\n      gl.uniform1f(u.uSteps, s.steps); gl.uniform1f(u.uEnc, res.enc); gl.uniform1f(u.uS, s.sphere);\n      for (const k of ['Deep', 'Body', 'Glow', 'Plasma', 'Accent', 'Bg']) gl.uniform3fv(u['u' + k], s.colors[k]);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n\n      // Bloom: downsample chain with a soft threshold on the first step, then add back up.\n      let src = targets.scene;\n      targets.mips.forEach((t, i) => {\n        pass(P.down, t);\n        bindTex(0, src.tex, P.down.u.uTex);\n        gl.uniform2f(P.down.u.uTexel, 1 / src.w, 1 / src.h);\n        gl.uniform1f(P.down.u.uPre, i === 0 ? 1 : 0);\n        gl.uniform1f(P.down.u.uThresh, 0.55 * res.enc);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n        src = t;\n      });\n      gl.enable(gl.BLEND); gl.blendFunc(gl.ONE, gl.ONE);\n      for (let i = targets.mips.length - 2; i >= 0; i--) {\n        const from = targets.mips[i + 1];\n        pass(P.up, targets.mips[i]);\n        bindTex(0, from.tex, P.up.u.uTex);\n        gl.uniform2f(P.up.u.uTexel, 1 / from.w, 1 / from.h);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n      gl.disable(gl.BLEND);\n\n      pass(P.comp, null);\n      gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT);\n      bindTex(0, targets.scene.tex, P.comp.u.uScene);\n      bindTex(1, targets.mips[0].tex, P.comp.u.uBloom);\n      gl.uniform1f(P.comp.u.uBloomK, s.bloom * (1 + 0.6 * s.level));\n      gl.uniform1f(P.comp.u.uExposure, 1.0);\n      gl.uniform1f(P.comp.u.uDec, 1 / res.enc);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      return true;\n    },\n    dispose() {\n      cv.removeEventListener('webglcontextlost', onLost);\n      cv.removeEventListener('webglcontextrestored', onRestored);\n      gl.getExtension('WEBGL_lose_context')?.loseContext();\n      res = null;\n    }\n  };\n}\n\n/**\n * Mounts the orb into `root` (the element holding `.pob-stage` and\n * `.pob-status`). Returns an idempotent disposer with update(config); updates\n * keep the GPU context.\n */\nexport function buildPlasmaOrb(root, config) {\n  let disposed = false;\n  let values = { ...DEFAULT_PLASMA_ORB_CONFIG, ...config };\n  const stage = root.querySelector('.pob-stage');\n  const status = root.querySelector('.pob-status');\n  root.removeAttribute('data-ready');\n  root.removeAttribute('data-fallback');\n  if (status) status.textContent = '';\n\n  const canvas = document.createElement('canvas');\n  // Inline styles survive Svelte scoping. The canvas is square so the glow stays round on any stage.\n  Object.assign(canvas.style, { position: 'absolute', left: '50%', top: '50%', transform: 'translate(-50%, -50%)', display: 'block', background: 'transparent' });\n  canvas.setAttribute('role', 'img');\n  canvas.setAttribute('aria-label', 'Glass orb with a glowing plasma core.');\n  stage?.append(canvas);\n  const renderer = stage ? createRenderer(canvas) : null;\n\n  const fail = () => {\n    canvas.remove();\n    root.setAttribute('data-fallback', '');\n    if (status) status.textContent = 'The orb needs WebGL 2. Enable hardware acceleration and reload to see it.';\n  };\n  if (!renderer) {\n    fail();\n    const noop = () => { disposed = true; root.removeAttribute('data-fallback'); if (status) status.textContent = ''; };\n    noop.update = () => {};\n    return noop;\n  }\n\n  const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');\n  const listen = { passive: true };\n  const cur = { energy: values.energy, swirl: values.swirl, level: 0, pulse: 0, time: 5.7, spin: 0 };\n  let colors = {}, raf = 0, last = 0, hover = false, visible = true;\n\n  const paint = () => {\n    colors = {\n      Deep: hexToRgb(values.center), Body: hexToRgb(values.glass), Glow: hexToRgb(values.rim),\n      Plasma: hexToRgb(values.plasma), Accent: hexToRgb(values.accent), Bg: hexToRgb(values.background)\n    };\n  };\n  const fit = () => {\n    const side = Math.max(2, Math.min(stage.clientWidth, stage.clientHeight));\n    canvas.style.width = canvas.style.height = side + 'px';\n  };\n  const running = () => values.animate && values.speed > 0 && !reducedMotion.matches && visible && !document.hidden;\n\n  function frame(now) {\n    raf = 0;\n    const dt = last ? Math.min(0.05, (now - last) / 1000) : 1 / 60;\n    last = now;\n    const run = running();\n    const live = values.interactive && !reducedMotion.matches;\n\n    // Hovering lifts the plasma; a press sends a pulse that fades out.\n    cur.pulse *= Math.exp(-dt * 4);\n    const target = live ? Math.max(hover ? 0.28 : 0, cur.pulse) : 0;\n    cur.level += (target - cur.level) * (1 - Math.exp(-dt * (target > cur.level ? 14 : 4)));\n    const ease = 1 - Math.exp(-dt * 3.5);\n    cur.energy += (values.energy - cur.energy) * ease;\n    cur.swirl += (values.swirl - cur.swirl) * ease;\n    if (run) {\n      cur.time += dt * values.speed * (0.55 + cur.energy * 1.1 + cur.level * 1.5);\n      cur.spin += dt * values.speed * (0.04 + cur.swirl * 0.9);\n    }\n\n    const detail = DETAIL[values.detail] || DETAIL.high;\n    const drawn = renderer.draw({\n      time: cur.time, dt: run ? dt : 0, level: cur.level, energy: cur.energy, swirl: cur.swirl, spin: cur.spin,\n      colors, sphere: values.size, steps: detail.steps, maxSide: detail.maxSide, bloom: values.glow\n    });\n    if (drawn) root.setAttribute('data-ready', '');\n\n    const settled = Math.abs(values.energy - cur.energy) < 0.002 && Math.abs(values.swirl - cur.swirl) < 0.002 && Math.abs(target - cur.level) < 0.003;\n    if (run || !settled) raf = requestAnimationFrame(frame);\n    else last = 0;\n  }\n  const wake = () => { if (!disposed && !raf) raf = requestAnimationFrame(frame); };\n\n  const onEnter = () => { hover = true; wake(); };\n  const onLeave = () => { hover = false; wake(); };\n  const onDown = () => { if (values.interactive) { cur.pulse = 1; wake(); } };\n  stage.addEventListener('pointerenter', onEnter, listen);\n  stage.addEventListener('pointerleave', onLeave, listen);\n  stage.addEventListener('pointerdown', onDown, listen);\n  reducedMotion.addEventListener?.('change', wake);\n  document.addEventListener('visibilitychange', wake);\n  const resize = typeof ResizeObserver === 'function' ? new ResizeObserver(() => { fit(); wake(); }) : null;\n  resize?.observe(stage);\n  // Off-screen orbs stop drawing.\n  const seen = typeof IntersectionObserver === 'function'\n    ? new IntersectionObserver((entries) => { visible = entries.some((e) => e.isIntersecting); wake(); })\n    : null;\n  seen?.observe(stage);\n\n  paint(); fit(); wake();\n\n  const cleanup = () => {\n    if (disposed) return;\n    disposed = true;\n    if (raf) cancelAnimationFrame(raf);\n    raf = 0;\n    stage.removeEventListener('pointerenter', onEnter);\n    stage.removeEventListener('pointerleave', onLeave);\n    stage.removeEventListener('pointerdown', onDown);\n    reducedMotion.removeEventListener?.('change', wake);\n    document.removeEventListener('visibilitychange', wake);\n    resize?.disconnect();\n    seen?.disconnect();\n    renderer.dispose();\n    canvas.remove();\n    root.removeAttribute('data-ready');\n  };\n  cleanup.update = (next) => {\n    if (disposed) return;\n    values = { ...values, ...next };\n    paint(); fit(); wake();\n  };\n  return cleanup;\n}\n",
      "type": "registry:component"
    },
    {
      "path": "registry/drops/plasma-orb/plasma-orb.engine.d.ts",
      "content": "// Types for plasma-orb.engine.js (the engine itself is plain JavaScript).\nexport interface PlasmaOrbConfig {\n  viewportWidth: number;\n  viewportHeight: number;\n  size: number;\n  background: string;\n  center: string;\n  glass: string;\n  rim: string;\n  plasma: string;\n  accent: string;\n  energy: number;\n  swirl: number;\n  glow: number;\n  speed: number;\n  detail: 'high' | 'balanced';\n  animate: boolean;\n  interactive: boolean;\n}\n\nexport interface PlasmaOrbHandle {\n  (): void;\n  update(next: Partial<PlasmaOrbConfig>): void;\n}\n\nexport const DEFAULT_PLASMA_ORB_CONFIG: PlasmaOrbConfig;\nexport function buildPlasmaOrb(root: HTMLElement, config: Partial<PlasmaOrbConfig>): PlasmaOrbHandle;\nexport function hexToRgb(hex: string): [number, number, number];\nexport function createRenderer(canvas: HTMLCanvasElement): unknown;\n",
      "type": "registry:component"
    }
  ],
  "meta": {
    "license": "HorizonX Drops License 1.0",
    "page": "https://horizonx.so/tools/plasma-orb",
    "source": "https://horizonx.so"
  },
  "docs": "Plasma Orb fills its parent, so give the parent a width and height. The full editor, presets and exports are at https://horizonx.so/tools/plasma-orb",
  "categories": [
    "webgl",
    "orb"
  ],
  "type": "registry:component"
}