{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"warp-highway","type":"registry:ui","title":"Warp Highway","description":"A night highway of light trails rushing toward you. Hold the pointer down to punch it into warp.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/warp-highway.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface WarpHighwayProps {\n  /** Oncoming light trails (left side). */\n  leftColor?: string;\n  /** Outgoing light trails (right side). */\n  rightColor?: string;\n  /** Roadside light posts and lane markings. */\n  sideColor?: string;\n  /** Cruise speed multiplier. */\n  speed?: number;\n  /** Speed multiplier while the pointer is held down. */\n  boost?: number;\n  /** Share of lanes carrying light trails, 0-1. */\n  density?: number;\n  className?: string;\n}\n\nconst FRAG = `\nprecision highp float;\nfloat sq(float v) { return v * v; }\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uMouse;\nuniform float uHover;\nuniform float uDown;\nuniform vec3 uLeft;\nuniform vec3 uRight;\nuniform vec3 uSide;\nuniform float uDensity;\n\nfloat hash(float n) { return fract(sin(n * 12.9898) * 43758.5453); }\n\n// light trails along a line in depth; z is world depth, returns brightness\nfloat trails(float z, float seed, float cell, float stretch) {\n  float c = floor(z / cell);\n  float f = fract(z / cell);\n  if (hash(c * 1.37 + seed * 17.1) > uDensity) return 0.0;\n  float start = hash(c + seed * 3.3) * 0.3;\n  float len = min((0.25 + 0.4 * hash(c * 2.1 + seed)) * stretch, 0.95 - start);\n  float x = (f - start) / len;\n  return smoothstep(0.0, 0.04, x) * smoothstep(1.0, 0.6, x) * mix(1.0, 0.3, clamp(x, 0.0, 1.0))\n    * (0.6 + 0.4 * hash(c + seed * 9.0));\n}\n\nvoid main() {\n  vec2 p = (gl_FragCoord.xy - uRes * vec2(0.5, 0.55)) / uRes.y;\n  p *= 1.0 + 0.4 * uDown;                        // field of view kicks out when boosting\n  p.x += (uMouse.x - 0.5) * 0.1 * uHover;        // steer a little with the cursor\n  float t = uTime;\n  float stretch = 1.0 + 0.8 * uDown;\n  vec3 col = vec3(0.0);\n\n  // sky glow at the vanishing point\n  col += mix(uLeft, uRight, smoothstep(-0.4, 0.4, p.x)) * 0.1 * exp(-length(p * vec2(0.5, 2.5)) * 3.0);\n\n  if (p.y < 0.0) {\n    float z = 0.3 / -p.y;\n    float x = p.x * z;\n    float fog = exp(-z * 0.02);\n    float px = z / uRes.y;  // world size of a pixel at this depth\n    col += vec3(0.012, 0.012, 0.024) * smoothstep(4.0, 3.2, abs(x)) * fog;\n    // four lanes each side: pairs of lights, left oncoming, right outgoing\n    for (int i = 0; i < 4; i++) {\n      float fi = float(i);\n      float lx = 0.45 + fi * 0.8;\n      for (int s = 0; s < 2; s++) {\n        float side = s == 0 ? -1.0 : 1.0;\n        float d = abs(x - side * lx);\n        float pair = exp(-sq((abs(d - 0.14)) / (0.03 + px * 1.5)));\n        float glow = exp(-d / 0.3) * 0.18;\n        float zz = s == 0 ? z + t * 70.0 : z - t * 30.0 + 400.0;\n        col += (s == 0 ? uLeft : uRight) * trails(zz, fi + side * 5.0, 22.0, stretch) * (pair + glow) * fog * 1.8;\n      }\n    }\n    // dashed center line and road edges\n    float dash = smoothstep(0.35, 0.45, fract((z + t * 45.0) / 8.0)) * smoothstep(0.95, 0.85, fract((z + t * 45.0) / 8.0));\n    col += uSide * 0.18 * exp(-sq(x / (0.02 + px))) * dash * fog;\n    col += uSide * 0.15 * exp(-sq((abs(x) - 3.6) / (0.02 + px))) * fog;\n  }\n\n  // roadside: lamps and light trails on invisible walls at |x| = 4.6\n  float ax = abs(p.x);\n  if (ax > 0.002) {\n    float wz = 4.6 / ax;\n    float wy = p.y * wz + 0.3;\n    float fogw = exp(-wz * 0.02);\n    float lampZ = fract((wz + t * 45.0) / 12.0);\n    float lamp = smoothstep(0.93, 0.95, lampZ) * smoothstep(1.0, 0.98, lampZ) * exp(-sq((wy - 1.4) / 0.05));\n    col += uSide * lamp * 1.5 * fogw;\n    float rail = exp(-sq((wy - 0.6) / 0.025));\n    col += mix(uLeft, uRight, step(0.0, p.x)) * rail * trails(wz + t * 60.0, sign(p.x) * 11.0, 16.0, stretch) * fogw;\n  }\n\n  col = 1.0 - exp(-col * 1.6);\n  gl_FragColor = vec4(col, 1.0);\n}\n`;\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\ntype Ptr = { x: number; y: number; hover: number; down: number };\ntype Uni = (name: string) => WebGLUniformLocation | null;\n\n/** Full-screen shader canvas inside `host`: DPR-capped resize, offscreen pause, reduced motion, eased pointer. */\nfunction runShader(host: HTMLElement, frag: string, speed: (p: Ptr) => number, update: (gl: WebGLRenderingContext, u: Uni) => 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 });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\n  for (const [type, src] of [\n    [gl.VERTEX_SHADER, VERT],\n    [gl.FRAGMENT_SHADER, frag],\n  ] as const) {\n    const sh = gl.createShader(type)!;\n    gl.shaderSource(sh, src);\n    gl.compileShader(sh);\n    if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(sh));\n    gl.attachShader(prog, sh);\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 u: Uni = (n) => {\n    if (!locs.has(n)) locs.set(n, gl.getUniformLocation(prog, n));\n    return locs.get(n) ?? null;\n  };\n  const zone = host.parentElement ?? host;\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: 0.5, y: 0.5, hover: 0, down: 0 };\n  const goal: Ptr = { ...ptr };\n  let raf = 0;\n  let last = 0;\n  let time = 0;\n\n  const draw = () => {\n    gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n    gl.uniform1f(u(\"uTime\"), time);\n    gl.uniform2f(u(\"uMouse\"), ptr.x, ptr.y);\n    gl.uniform1f(u(\"uHover\"), ptr.hover);\n    gl.uniform1f(u(\"uDown\"), ptr.down);\n    update(gl, u);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    const dt = Math.min((now - last) / 1000, 0.1);\n    last = now;\n    const k = 1 - Math.exp(-dt * 4);\n    for (const key of [\"x\", \"y\", \"hover\", \"down\"] as const) ptr[key] += (goal[key] - ptr[key]) * k;\n    time += dt * speed(ptr);\n    draw();\n    raf = requestAnimationFrame(loop);\n  };\n  const run = (on: boolean) => {\n    if (on && !raf && !reduced) {\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    } else if (!on && raf) {\n      cancelAnimationFrame(raf);\n      raf = 0;\n    }\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    draw();\n  };\n  const move = (e: PointerEvent) => {\n    const r = host.getBoundingClientRect();\n    goal.x = (e.clientX - r.left) / r.width;\n    goal.y = 1 - (e.clientY - r.top) / r.height;\n    goal.hover = 1;\n  };\n  const leave = () => {\n    goal.hover = 0;\n    goal.down = 0;\n  };\n  const press = () => {\n    goal.down = 1;\n  };\n  const release = () => {\n    goal.down = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  const io = new IntersectionObserver(([e]) => run(e.isIntersecting));\n  ro.observe(host);\n  io.observe(host);\n  zone.addEventListener(\"pointermove\", move);\n  zone.addEventListener(\"pointerleave\", leave);\n  zone.addEventListener(\"pointerdown\", press);\n  zone.addEventListener(\"pointerup\", release);\n  zone.addEventListener(\"pointercancel\", release);\n  return {\n    redraw: () => {\n      if (!raf) draw();\n    },\n    dispose: () => {\n      run(false);\n      ro.disconnect();\n      io.disconnect();\n      zone.removeEventListener(\"pointermove\", move);\n      zone.removeEventListener(\"pointerleave\", leave);\n      zone.removeEventListener(\"pointerdown\", press);\n      zone.removeEventListener(\"pointerup\", release);\n      zone.removeEventListener(\"pointercancel\", release);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nconst rgb = (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\nexport function WarpHighway({\n  leftColor = \"#ff2d78\",\n  rightColor = \"#3ee0ff\",\n  sideColor = \"#c9c3ff\",\n  speed = 1,\n  boost = 4,\n  density = 0.6,\n  className,\n}: WarpHighwayProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const opts = useRef({ leftColor, rightColor, sideColor, speed, boost, density });\n  const redraw = useRef<(() => void) | undefined>(undefined);\n\n  useEffect(() => {\n    opts.current = { leftColor, rightColor, sideColor, speed, boost, density };\n    redraw.current?.();\n  }, [leftColor, rightColor, sideColor, speed, boost, density]);\n\n  useEffect(() => {\n    const r = runShader(\n      ref.current!,\n      FRAG,\n      (ptr) => opts.current.speed * (1 + ptr.down * (opts.current.boost - 1)),\n      (gl, u) => {\n        const o = opts.current;\n        gl.uniform3fv(u(\"uLeft\"), rgb(o.leftColor));\n        gl.uniform3fv(u(\"uRight\"), rgb(o.rightColor));\n        gl.uniform3fv(u(\"uSide\"), rgb(o.sideColor));\n        gl.uniform1f(u(\"uDensity\"), o.density);\n      },\n    );\n    redraw.current = r?.redraw;\n    return () => r?.dispose();\n  }, []);\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}