{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "clouds-shader-background",
  "type": "registry:block",
  "title": "Clouds Shader Background",
  "description": "A self-contained WebGL cloudscape built from the supplied Shadertoy-style noise shader.",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/components/clouds-shader-background-source.ts",
      "type": "registry:lib",
      "target": "@ui/clouds-shader-background-source.ts",
      "content": "export const cloudsFragmentShader = String.raw`#version 300 es\nprecision highp float;\n\nlayout(location = 0) out vec4 outColor;\n\nuniform vec3 iResolution;\nuniform float iTime;\nuniform float iTimeDelta;\nuniform float iFrameRate;\nuniform int iFrame;\nuniform float iChannelTime[4];\nuniform vec3 iChannelResolution[4];\nuniform vec4 iMouse;\nuniform sampler2D iChannel0;\nuniform sampler2D iChannel1;\nuniform sampler2D iChannel2;\nuniform sampler2D iChannel3;\nuniform vec4 iDate;\nuniform float iSampleRate;\n\nconst float cloudscale = 1.1;\nconst float speed = 0.03;\nconst float clouddark = 0.5;\nconst float cloudlight = 0.3;\nconst float cloudcover = 0.2;\nconst float cloudalpha = 8.0;\nconst float skytint = 0.5;\nconst vec3 skycolour1 = vec3(0.2, 0.4, 0.6);\nconst vec3 skycolour2 = vec3(0.4, 0.7, 1.0);\n\nconst mat2 m = mat2(1.6, 1.2, -1.2, 1.6);\n\nvec2 hash(vec2 p) {\n  p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));\n  return -1.0 + 2.0 * fract(sin(p) * 43758.5453123);\n}\n\nfloat noise(in vec2 p) {\n  const float K1 = 0.366025404;\n  const float K2 = 0.211324865;\n  vec2 i = floor(p + (p.x + p.y) * K1);\n  vec2 a = p - i + (i.x + i.y) * K2;\n  vec2 o = (a.x > a.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);\n  vec2 b = a - o + K2;\n  vec2 c = a - 1.0 + 2.0 * K2;\n  vec3 h = max(0.5 - vec3(dot(a, a), dot(b, b), dot(c, c)), 0.0);\n  vec3 n = h * h * h * h * vec3(dot(a, hash(i)), dot(b, hash(i + o)), dot(c, hash(i + 1.0)));\n  return dot(n, vec3(70.0));\n}\n\nfloat fbm(vec2 n) {\n  float total = 0.0;\n  float amplitude = 0.1;\n  for (int i = 0; i < 7; i++) {\n    total += noise(n) * amplitude;\n    n = m * n;\n    amplitude *= 0.4;\n  }\n  return total;\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 p = fragCoord.xy / iResolution.xy;\n  vec2 uv = p * vec2(iResolution.x / iResolution.y, 1.0);\n  float time = iTime * speed;\n  float q = fbm(uv * cloudscale * 0.5);\n\n  float r = 0.0;\n  uv *= cloudscale;\n  uv -= q - time;\n  float weight = 0.8;\n  for (int i = 0; i < 8; i++) {\n    r += abs(weight * noise(uv));\n    uv = m * uv + time;\n    weight *= 0.7;\n  }\n\n  float f = 0.0;\n  uv = p * vec2(iResolution.x / iResolution.y, 1.0);\n  uv *= cloudscale;\n  uv -= q - time;\n  weight = 0.7;\n  for (int i = 0; i < 8; i++) {\n    f += weight * noise(uv);\n    uv = m * uv + time;\n    weight *= 0.6;\n  }\n\n  f *= r + f;\n\n  float c = 0.0;\n  time = iTime * speed * 2.0;\n  uv = p * vec2(iResolution.x / iResolution.y, 1.0);\n  uv *= cloudscale * 2.0;\n  uv -= q - time;\n  weight = 0.4;\n  for (int i = 0; i < 7; i++) {\n    c += weight * noise(uv);\n    uv = m * uv + time;\n    weight *= 0.6;\n  }\n\n  float c1 = 0.0;\n  time = iTime * speed * 3.0;\n  uv = p * vec2(iResolution.x / iResolution.y, 1.0);\n  uv *= cloudscale * 3.0;\n  uv -= q - time;\n  weight = 0.4;\n  for (int i = 0; i < 7; i++) {\n    c1 += abs(weight * noise(uv));\n    uv = m * uv + time;\n    weight *= 0.6;\n  }\n\n  c += c1;\n\n  vec3 skycolour = mix(skycolour2, skycolour1, p.y);\n  vec3 cloudcolour = vec3(1.1, 1.1, 0.9) * clamp(clouddark + cloudlight * c, 0.0, 1.0);\n  f = cloudcover + cloudalpha * f * r;\n\n  vec3 result = mix(skycolour, clamp(skytint * skycolour + cloudcolour, 0.0, 1.0), clamp(f + c, 0.0, 1.0));\n  fragColor = vec4(result, 1.0);\n}\n\nvoid main() {\n  vec4 color;\n  mainImage(color, gl_FragCoord.xy);\n  outColor = color;\n}`\n"
    },
    {
      "path": "registry/components/clouds-shader-background.tsx",
      "type": "registry:component",
      "target": "@ui/clouds-shader-background.tsx",
      "content": "\"use client\"\n\nimport { useEffect, useRef } from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\nimport { cloudsFragmentShader } from \"./clouds-shader-background-source\"\n\nconst vertexShader = `#version 300 es\nin vec2 a_position;\nvoid main() { gl_Position = vec4(a_position, 0.0, 1.0); }`\n\nexport type CloudsShaderBackgroundProps = {\n  className?: string\n  animate?: boolean\n}\n\nexport function CloudsShaderBackground({ className, animate = true }: CloudsShaderBackgroundProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null)\n\n  useEffect(() => {\n    const canvas = canvasRef.current\n    const gl = canvas?.getContext(\"webgl2\", { alpha: false, antialias: false })\n    if (!canvas || !gl) return\n    const shouldAnimate = animate && !window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches\n\n    const compile = (type: number, source: string) => {\n      const shader = gl.createShader(type)\n      if (!shader) throw new Error(\"Unable to create shader.\")\n      gl.shaderSource(shader, source)\n      gl.compileShader(shader)\n      if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n        const message = gl.getShaderInfoLog(shader) ?? \"Unknown shader compile error.\"\n        gl.deleteShader(shader)\n        throw new Error(message)\n      }\n      return shader\n    }\n\n    let program: WebGLProgram\n    try {\n      program = gl.createProgram()!\n      gl.attachShader(program, compile(gl.VERTEX_SHADER, vertexShader))\n      gl.attachShader(program, compile(gl.FRAGMENT_SHADER, cloudsFragmentShader))\n      gl.linkProgram(program)\n      if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(program) ?? \"Unable to link shader program.\")\n      gl.useProgram(program)\n    } catch (error) {\n      console.error(\"Clouds shader failed to compile.\", error)\n      return\n    }\n\n    const buffer = gl.createBuffer()\n    gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW)\n    const position = gl.getAttribLocation(program, \"a_position\")\n    gl.enableVertexAttribArray(position)\n    gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0)\n\n    const uniform = (name: string) => gl.getUniformLocation(program, name)\n    const resolution = uniform(\"iResolution\")\n    const time = uniform(\"iTime\")\n    const timeDelta = uniform(\"iTimeDelta\")\n    const frameRate = uniform(\"iFrameRate\")\n    const frame = uniform(\"iFrame\")\n    const channelTime = uniform(\"iChannelTime\")\n    const channelResolution = uniform(\"iChannelResolution\")\n    const mouse = uniform(\"iMouse\")\n    const date = uniform(\"iDate\")\n    const sampleRate = uniform(\"iSampleRate\")\n    const pointer = { x: 0, y: 0, clickX: 0, clickY: 0 }\n    let pointerDown = false\n    let width = 1\n    let height = 1\n    let frameNumber = 0\n    let lastTime = 0\n    let animationFrame = 0\n\n    const resize = () => {\n      const ratio = Math.min(window.devicePixelRatio || 1, 2)\n      width = Math.max(1, Math.floor(canvas.clientWidth * ratio))\n      height = Math.max(1, Math.floor(canvas.clientHeight * ratio))\n      if (canvas.width !== width || canvas.height !== height) {\n        canvas.width = width\n        canvas.height = height\n        gl.viewport(0, 0, width, height)\n      }\n    }\n\n    const updatePointer = (event: PointerEvent) => {\n      const rect = canvas.getBoundingClientRect()\n      pointer.x = (event.clientX - rect.left) / rect.width * width\n      pointer.y = (rect.bottom - event.clientY) / rect.height * height\n    }\n    const onPointerDown = (event: PointerEvent) => {\n      updatePointer(event)\n      pointer.clickX = pointer.x\n      pointer.clickY = pointer.y\n      pointerDown = true\n    }\n    const onPointerMove = (event: PointerEvent) => updatePointer(event)\n    const onPointerUp = () => { pointerDown = false }\n    const setDate = (now: Date) => gl.uniform4f(date, now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds())\n\n    const render = (now: number) => {\n      resize()\n      const seconds = now * 0.001\n      const delta = lastTime ? seconds - lastTime : 0\n      lastTime = seconds\n      gl.uniform3f(resolution, width, height, 1)\n      gl.uniform1f(time, shouldAnimate ? seconds : 0)\n      gl.uniform1f(timeDelta, delta)\n      gl.uniform1f(frameRate, delta > 0 ? 1 / delta : 60)\n      gl.uniform1i(frame, frameNumber)\n      gl.uniform1fv(channelTime, new Float32Array([seconds, seconds, seconds, seconds]))\n      gl.uniform3fv(channelResolution, new Float32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))\n      gl.uniform4f(mouse, pointerDown ? pointer.x : 0, pointerDown ? pointer.y : 0, pointer.clickX, pointer.clickY)\n      setDate(new Date())\n      gl.uniform1f(sampleRate, 44100)\n      gl.drawArrays(gl.TRIANGLES, 0, 3)\n      frameNumber += 1\n      if (shouldAnimate) animationFrame = window.requestAnimationFrame(render)\n    }\n\n    canvas.addEventListener(\"pointerdown\", onPointerDown)\n    canvas.addEventListener(\"pointermove\", onPointerMove)\n    window.addEventListener(\"pointerup\", onPointerUp)\n    animationFrame = window.requestAnimationFrame(render)\n    return () => {\n      window.cancelAnimationFrame(animationFrame)\n      canvas.removeEventListener(\"pointerdown\", onPointerDown)\n      canvas.removeEventListener(\"pointermove\", onPointerMove)\n      window.removeEventListener(\"pointerup\", onPointerUp)\n      if (buffer) gl.deleteBuffer(buffer)\n      gl.deleteProgram(program)\n    }\n  }, [animate])\n\n  return <div className={cn(\"relative aspect-video overflow-hidden rounded-lg bg-sky-400\", className)}><canvas ref={canvasRef} aria-label=\"Animated cloud shader background\" className=\"absolute inset-0 h-full w-full touch-none\" /></div>\n}\n"
    }
  ]
}
