{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dot-terrain","type":"registry:ui","title":"Dot Terrain","description":"A perspective field of glowing dots rolling toward you in terrain-like waves; the camera leans with the cursor. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/dot-terrain.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface DotTerrainProps {\n  /** Dot color in the valleys. */\n  lowColor?: string;\n  /** Dot color on the crests. */\n  highColor?: string;\n  /** Travel and wave speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Wave height. */\n  amplitude?: number;\n  /** Dot size multiplier. */\n  dotSize?: number;\n  /** Grid density multiplier; higher packs the dots tighter. */\n  density?: number;\n  className?: string;\n}\n\nconst DEPTH = 34;\nconst HALF_WIDTH = 40;\n\nconst VERT = `\nattribute vec2 aGrid;\nuniform float uTime;\nuniform float uTravel;\nuniform float uAspect;\nuniform float uAmp;\nuniform float uSize;\nuniform float uDpr;\nuniform float uHeight;\nuniform vec2 uMouse;\nuniform vec3 uLow;\nuniform vec3 uHigh;\nvarying vec3 vCol;\nvarying float vAlpha;\n\nfloat terrain(vec2 p, float t) {\n  return sin(p.x * 0.35 + t * 0.6) * 0.6\n       + sin(p.y * 0.28 - t * 0.8 + p.x * 0.12) * 0.8\n       + sin((p.x - p.y) * 0.55 + t * 0.5) * 0.3\n       + sin(length(p - vec2(0.0, 20.0)) * 0.6 - t * 1.1) * 0.35;\n}\n\nvoid main() {\n  float depth = ${DEPTH}.0;\n  float z = mod(aGrid.y - uTravel, depth);\n  float y = terrain(vec2(aGrid.x, z + uTravel), uTime) * uAmp;\n\n  vec3 cam = vec3((uMouse.x - 0.5) * 3.0, 3.2 + (uMouse.y - 0.5) * 1.2, -1.5);\n  vec3 r = vec3(aGrid.x, y, z) - cam;\n  float cp = cos(0.3), sp = sin(0.3);\n  r = vec3(r.x, r.y * cp + r.z * sp, r.z * cp - r.y * sp);\n\n  float fov = 1.5;\n  gl_Position = vec4(r.x * fov / uAspect, r.y * fov, 0.0, r.z);\n  float size = uSize * uDpr * uHeight * 0.03 / r.z;\n  gl_PointSize = clamp(size, 1.0, 32.0);\n\n  float hn = clamp(y / (uAmp * 3.6 + 0.001) + 0.5, 0.0, 1.0);\n  vCol = mix(uLow, uHigh, smoothstep(0.25, 0.8, hn));\n  float fog = smoothstep(depth, depth * 0.35, z) * smoothstep(0.0, 3.0, z);\n  // Sub-pixel dots fade instead of shimmering.\n  vAlpha = fog * (0.45 + 0.55 * smoothstep(0.2, 0.85, hn)) * min(size, 1.0);\n}\n`;\n\nconst FRAG = `\nprecision mediump float;\nvarying vec3 vCol;\nvarying float vAlpha;\nvoid main() {\n  float d = length(gl_PointCoord - 0.5);\n  float a = smoothstep(0.5, 0.2, d) * vAlpha;\n  gl_FragColor = vec4(vCol * a, a);\n}\n`;\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\nfunction program(gl: WebGLRenderingContext) {\n  const prog = gl.createProgram();\n  if (!prog) return null;\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    if (!sh) return null;\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, \"aGrid\");\n  gl.linkProgram(prog);\n  return gl.getProgramParameter(prog, gl.LINK_STATUS) ? prog : null;\n}\n\nexport function DotTerrain({\n  lowColor = \"#5b4bff\",\n  highColor = \"#5eead4\",\n  speed = 1,\n  amplitude = 1,\n  dotSize = 1,\n  density = 1,\n  className,\n}: DotTerrainProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ lowColor, highColor, speed, amplitude, dotSize, density });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { lowColor, highColor, speed, amplitude, dotSize, density };\n    redraw.current?.();\n  }, [lowColor, highColor, speed, amplitude, dotSize, density]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, powerPreference: \"low-power\" });\n    const prog = gl && program(gl);\n    if (!gl || !prog) {\n      canvas.remove();\n      return;\n    }\n    gl.useProgram(prog);\n    gl.enable(gl.BLEND);\n    gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);\n    gl.clearColor(0, 0, 0, 0);\n    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n    gl.enableVertexAttribArray(0);\n    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n    const u = (name: string) => gl.getUniformLocation(prog, name);\n    const loc = {\n      time: u(\"uTime\"),\n      travel: u(\"uTravel\"),\n      aspect: u(\"uAspect\"),\n      amp: u(\"uAmp\"),\n      size: u(\"uSize\"),\n      dpr: u(\"uDpr\"),\n      height: u(\"uHeight\"),\n      mouse: u(\"uMouse\"),\n      low: u(\"uLow\"),\n      high: u(\"uHigh\"),\n    };\n\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const pointer = { x: 0.5, y: 0.5, tx: 0.5, ty: 0.5 };\n    let raf = 0;\n    let visible = true;\n    let time = 2;\n    let last = 0;\n    let dpr = 1;\n    let count = 0;\n    let builtDensity = -1;\n\n    // Rebuilds the flat grid of (x, z) points when the density changes.\n    const build = (d: number) => {\n      const step = 0.22 / Math.min(Math.max(d, 0.3), 3);\n      const cols = Math.floor((HALF_WIDTH * 2) / step);\n      const rows = Math.floor(DEPTH / step);\n      const data = new Float32Array(cols * rows * 2);\n      let i = 0;\n      for (let r = 0; r < rows; r++)\n        for (let c = 0; c < cols; c++) {\n          data[i++] = -HALF_WIDTH + c * step;\n          data[i++] = r * step;\n        }\n      gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);\n      count = cols * rows;\n      builtDensity = d;\n    };\n\n    const draw = () => {\n      const o = opts.current;\n      if (o.density !== builtDensity) build(o.density);\n      gl.clear(gl.COLOR_BUFFER_BIT);\n      gl.uniform1f(loc.time, time);\n      gl.uniform1f(loc.travel, time * 1.4);\n      gl.uniform1f(loc.aspect, canvas.width / canvas.height);\n      gl.uniform1f(loc.amp, o.amplitude);\n      gl.uniform1f(loc.size, o.dotSize);\n      gl.uniform1f(loc.dpr, dpr);\n      gl.uniform1f(loc.height, canvas.height / dpr);\n      gl.uniform2f(loc.mouse, pointer.x, pointer.y);\n      gl.uniform3fv(loc.low, rgb(o.lowColor));\n      gl.uniform3fv(loc.high, rgb(o.highColor));\n      gl.drawArrays(gl.POINTS, 0, count);\n    };\n    const loop = (now: number) => {\n      time += (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      pointer.x += (pointer.tx - pointer.x) * 0.04;\n      pointer.y += (pointer.ty - pointer.y) * 0.04;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      draw();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      pointer.tx = (e.clientX - r.left) / r.width;\n      pointer.ty = 1 - (e.clientY - r.top) / r.height;\n    };\n    const onLeave = () => {\n      pointer.tx = 0.5;\n      pointer.ty = 0.5;\n    };\n\n    const target = el.parentElement ?? el;\n    target.addEventListener(\"pointermove\", onMove, { passive: true });\n    target.addEventListener(\"pointerleave\", onLeave);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      target.removeEventListener(\"pointermove\", onMove);\n      target.removeEventListener(\"pointerleave\", onLeave);\n      redraw.current = null;\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\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"}]}