{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"wet-asphalt-at-night-webgl-background-with-puddles-that-mirror-coloured-shop-signs-and-rain-rings","type":"registry:block","title":"Wet Asphalt at Night WebGL Background with Puddles That Mirror Coloured Shop Signs and Rain Rings","description":"Night street background in WebGL: a shader draws rain-soaked asphalt whose puddles mirror coloured shop signs and rain rings. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/wet-asphalt-at-night-webgl-background-with-puddles-that-mirror-coloured-shop-signs-and-rain-rings.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything about the street. Colours are red, green, blue from 0 to 1.\nconst street = {\n  speed: 1, // how fast time passes for the rain and the walk; 0 freezes the street\n  water: 0.5, // how much of the road lies under puddles; 0.35 is nearly drained, 0.62 flooded\n  rain: 1, // strength of the rings where drops land; 0 leaves the puddles as still mirrors\n  stone: 1, // height of the stone in the road surface; 0 is fresh, smooth tarmac\n  glow: 1, // brightness of the signs across the street\n  patch: [25, 8], // which stretch of road you stand on; any two numbers give a new set of puddles\n  paint: 1, // 1 keeps the worn white line, 0 removes it\n  eye: 1.35, // height of the viewer's eye above the road\n  drift: 0.7, // how far the viewer wanders left and right\n  quality: 0.8, // share of the device pixels that are drawn\n  maxPixels: 950000, // the canvas never draws more pixels than this\n  signs: [\n    [1.0, 0.1, 0.55], // magenta, the tall sign on the left\n    [0.1, 0.85, 1.0], // cyan, the long sign in the middle\n    [1.0, 0.6, 0.12], // amber, the ring\n    [1.0, 0.16, 0.12], // red, the small square\n    [0.3, 1.0, 0.5], // green, the thin sign on the right\n  ],\n};\n\nconst VERTEX = `\nattribute vec2 position;\nvoid main() { gl_Position = vec4(position, 0.0, 1.0); }`;\n\nconst FRAGMENT = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uEye;\nuniform float uHeight;\nuniform float uWater;\nuniform float uRain;\nuniform float uStone;\nuniform float uGlow;\nuniform float uPaint;\nuniform vec2 uPatch;\nuniform vec3 uSignA;\nuniform vec3 uSignB;\nuniform vec3 uSignC;\nuniform vec3 uSignD;\nuniform vec3 uSignE;\n\nconst float WALL = 14.0; // how far away the shop fronts are\n\nfloat hash(vec2 p) {\n  vec3 q = fract(vec3(p.xyx) * 0.1031);\n  q += dot(q, q.yzx + 33.33);\n  return fract((q.x + q.y) * q.z);\n}\nvec2 hash2(vec2 p) { return vec2(hash(p), hash(p.yx + 19.19)); }\nfloat noise(vec2 p) {\n  vec2 i = floor(p);\n  vec2 f = fract(p);\n  f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);\n}\n\n// The stone rolled into the tar, and the slow dips in the road where water collects.\nfloat stones(vec2 x) { return 0.5 * noise(x * 58.0) + 0.38 * noise(x * 121.0 + 3.1) + 0.12 * noise(x * 23.0 + 8.0); }\nfloat lie(vec2 x) {\n  x += uPatch;\n  return 0.62 * noise(x * 0.55 + 2.7) + 0.38 * noise(x * 1.35 + 9.4);\n}\n\n// The ring thrown out by one raindrop in each square of a loose grid.\nvec2 rings(vec2 x, float seed) {\n  vec2 g = x * 1.7 + seed * 5.3;\n  vec2 id = floor(g);\n  vec2 f = fract(g) - 0.5;\n  vec2 away = f - (hash2(id + seed) - 0.5) * 0.3;\n  float d = length(away);\n  float age = fract(uTime * 0.6 + hash(id + seed * 3.0));\n  float gap = (d - age * 0.4) * 16.0;\n  float wave = sin(gap * 4.4) * exp(-gap * gap) * (1.0 - age) * (1.0 - age);\n  return away / max(d, 0.001) * wave;\n}\n\nfloat box(vec2 d, vec2 h) {\n  vec2 a = abs(d) - h;\n  return length(max(a, 0.0)) + min(max(a.x, a.y), 0.0);\n}\n// A lit panel and a bent glass tube, each with the haze that hangs round it in damp air.\nfloat panel(float sd, float soft) { return smoothstep(soft, -soft, sd) + 0.3 * exp(-max(sd, 0.0) / (soft * 2.0 + 0.3)); }\nfloat tube(float sd, float soft) {\n  float d = abs(sd) - 0.06;\n  return 1.25 * smoothstep(soft, -soft, d) + 0.34 * exp(-max(d, 0.0) / (soft * 2.0 + 0.3));\n}\n\n// What a ray leaving the road sees across the street: tube signs on a dark frontage and a\n// few lit windows above. rough smears every shape, far more up and down than side to side,\n// which is what pulls a sign into a long streak on a wet road.\nvec3 across(vec3 from, vec3 dir, float rough) {\n  float s = (WALL - from.z) / max(dir.z, 0.02);\n  vec2 q = from.xy + dir.xy * s;\n  vec2 k = vec2(1.0, 1.0 / (1.0 + rough * 16.0)); // squeeze the wall, measure, and the blur comes out tall\n  float soft = 0.035 + rough * 0.42;\n  float thin = 1.0 / (1.0 + rough * 2.2);\n  float buzz = 0.8 + 0.2 * step(0.12, noise(vec2(uTime * 9.0, 3.0)));\n  vec2 d;\n\n  // Magenta: a tall framed sign with a column of short strokes inside it.\n  d = q - vec2(-5.2, 5.8);\n  float a = tube(box(d * k, vec2(0.62, 2.9) * k) - 0.12, soft);\n  a += panel(box(vec2(d.x, mod(d.y + 0.45, 0.9) - 0.45) * k, vec2(0.3, 0.13) * k), soft) * step(abs(d.y), 2.5);\n  vec3 col = uSignA * buzz * a;\n  // Cyan: a long frame with a row of upright strokes.\n  d = q - vec2(0.8, 3.2);\n  float c = tube(box(d * k, vec2(2.7, 0.62) * k) - 0.12, soft);\n  c += panel(box(vec2(mod(d.x + 0.3, 0.6) - 0.3, d.y) * k, vec2(0.1, 0.3) * k), soft) * step(abs(d.x), 2.3);\n  col += uSignB * c;\n  // Amber: a ring with a dot in it.\n  d = (q - vec2(5.6, 6.6)) * k;\n  col += uSignC * (tube(length(d) - 1.2 * mix(1.0, k.y, 0.5), soft) + panel(length(d) - 0.3 * k.y, soft));\n  // Red: one round lamp. Green: a thin upright.\n  d = (q - vec2(-1.7, 8.4)) * k;\n  col += uSignD * 1.1 * (tube(length(d) - 0.62 * mix(1.0, k.y, 0.5), soft) + panel(length(d) - 0.2 * k.y, soft));\n  col += uSignE * panel(box((q - vec2(9.3, 4.6)) * k, vec2(0.2, 2.3) * k), soft);\n  // More of the same further along and higher up.\n  col += uSignB * 0.8 * tube(box((q - vec2(-10.2, 3.4)) * k, vec2(1.8, 0.5) * k) - 0.1, soft);\n  col += uSignA * 0.8 * tube(box((q - vec2(2.8, 11.0)) * k, vec2(1.7, 0.4) * k) - 0.1, soft);\n  col += uSignD * 0.9 * tube(box((q - vec2(7.4, 15.0)) * k, vec2(0.5, 3.2) * k) - 0.1, soft);\n  col += uSignC * 0.8 * panel(box((q - vec2(-8.0, 12.0)) * k, vec2(0.22, 2.6) * k), soft);\n  col += uSignE * 0.7 * tube(length((q - vec2(-2.6, 17.5)) * k) - 1.0 * mix(1.0, k.y, 0.5), soft);\n  col += vec3(1.0, 0.8, 0.55) * 1.5 * panel(length((q - vec2(-3.6, 12.8)) * k) - 0.25 * k.y, soft); // a street lamp\n  col *= thin * uGlow * 5.0;\n\n  // The sky over the roofs carries the town's glow.\n  col += vec3(0.17, 0.11, 0.24) * (0.3 + 0.7 * smoothstep(0.0, 26.0, q.y));\n  return col;\n}\n\n// Light arriving at the road from one sign, on a surface facing n.\nvec3 wash(vec3 g, vec3 n, vec3 at, vec3 tint, float power) {\n  vec3 to = at - g;\n  float far = dot(to, to);\n  return tint * power * max(dot(n, to / sqrt(far)), 0.0) / (1.0 + far * 0.03);\n}\n\nvoid main() {\n  // An upright screen gets a wider lens and looks further down, so it still takes in several signs.\n  float tall = step(uRes.x, uRes.y);\n  vec2 uv = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y * (1.0 + 0.3 * tall);\n  vec3 rd = normalize(vec3(uv.x, uv.y, 1.08));\n  float tilt = 0.56 + 0.1 * tall + uEye.y;\n  rd = vec3(rd.x, rd.y * cos(tilt) - rd.z * sin(tilt), rd.y * sin(tilt) + rd.z * cos(tilt));\n  vec3 ro = vec3(uEye.x, uHeight, 0.0);\n  float t = ro.y / max(-rd.y, 0.02);\n  vec3 g = ro + rd * t;\n  vec2 x = g.xz;\n  float near = 1.0 / (1.0 + t * 0.22); // fine detail is let go with distance so it does not shimmer\n\n  float e = 0.003;\n  float h = stones(x);\n  vec2 slope = vec2(stones(x + vec2(e, 0.0)) - h, stones(x + vec2(0.0, e)) - h) / e;\n  float low = lie(x);\n  float depth = uWater - low - (h - 0.5) * 0.07 * uStone * near;\n  float pool = smoothstep(0.0, 0.028, depth);\n  float damp = smoothstep(-0.12, 0.0, depth);\n\n  vec2 ripple = (rings(x, 1.0) + rings(x * 1.31 + 7.0, 2.0)) * 0.05 * uRain;\n  ripple += (vec2(noise(x * 5.0 + uTime * 0.4), noise(x * 5.0 - uTime * 0.33 + 4.0)) - 0.5) * 0.008;\n  vec3 nRoad = normalize(vec3(-slope.x * 0.009 * uStone * near * near, 1.0, -slope.y * 0.009 * uStone * near * near));\n  vec3 nPool = normalize(vec3(-ripple.x, 1.0, -ripple.y));\n  vec3 n = normalize(mix(nRoad, nPool, pool));\n\n  // The road itself: black binder, paler stone showing through where tyres have worn it.\n  float grit = smoothstep(0.52, 0.74, h);\n  vec3 tar = mix(vec3(0.02, 0.021, 0.025), vec3(0.075, 0.076, 0.08), grit * near);\n  // A worn white line running away up the street.\n  float side = x.x + 0.9 - x.y * 0.16;\n  float worn = smoothstep(0.38, 0.62, noise(x * 11.0) * 0.5 + noise(x * 2.2 + 5.0) * 0.5 + (0.5 - h) * 0.7);\n  float line = uPaint * smoothstep(0.085, 0.07, abs(side)) * worn;\n  tar = mix(tar, vec3(0.36, 0.36, 0.34) * (0.7 + 0.4 * h), line * 0.85);\n  tar *= mix(1.0, 0.6, damp); // wet stone is darker\n  tar *= mix(1.0, 0.3, pool); // and darker again seen through water\n\n  vec3 light = wash(g, n, vec3(-5.2, 5.6, WALL), uSignA, 6.0);\n  light += wash(g, n, vec3(0.6, 3.1, WALL), uSignB, 6.5);\n  light += wash(g, n, vec3(5.4, 6.4, WALL), uSignC, 6.0);\n  light += wash(g, n, vec3(9.2, 4.4, WALL), uSignE, 3.5);\n  light += wash(g, n, vec3(-3.4, 12.6, 5.0), vec3(1.0, 0.8, 0.55), 7.0);\n  vec3 col = tar * (light * uGlow + vec3(0.05, 0.05, 0.07));\n\n  // The film of water on top mirrors the far side of the street.\n  vec3 r = reflect(rd, n);\n  float facing = max(dot(-rd, n), 0.0);\n  float fres = 0.05 + 0.95 * pow(1.0 - facing, 3.0);\n  float rough = mix(0.34 + 0.3 * noise(x * 3.0) + 0.25 * (1.0 - damp), 0.0, pool);\n  vec3 seen = across(g, r, rough);\n  col += seen * fres * mix(0.34, 1.0, pool) * (0.5 + 0.5 * damp);\n\n  // Haze in the distance, vignette, grain.\n  col += vec3(0.05, 0.02, 0.06) * smoothstep(3.5, 9.0, t) * 0.35;\n  vec2 c = gl_FragCoord.xy / uRes - 0.5;\n  col *= 1.0 - 0.7 * dot(c, c);\n  col = 1.0 - exp(-col * 2.3);\n  col += (hash(gl_FragCoord.xy + fract(uTime) * 31.0) - 0.5) * 0.018;\n  gl_FragColor = vec4(pow(max(col, 0.0), vec3(0.8)), 1.0);\n}`;\n\nconst mix = (a: number, b: number, t: number) => a + (b - a) * t;\n\n/** One fragment shader fills the block. `onLive` reports when a frame has been painted. */\nfunction StreetCanvas({ onLive }: { onLive: (live: boolean) => void }) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current;\n    const gl = canvas?.getContext(\"webgl\", { antialias: false, alpha: false });\n    if (!canvas || !gl) return; // no WebGL: the CSS street underneath stays\n\n    const compile = (type: number, source: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, source);\n      gl.compileShader(s);\n      return s;\n    };\n    const program = gl.createProgram()!;\n    gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX));\n    gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));\n    gl.linkProgram(program);\n    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return;\n    gl.useProgram(program);\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    const position = gl.getAttribLocation(program, \"position\");\n    gl.enableVertexAttribArray(position);\n    gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);\n    const at = (name: string) => gl.getUniformLocation(program, name);\n    const u = { res: at(\"uRes\"), time: at(\"uTime\"), eye: at(\"uEye\") };\n    gl.uniform1f(at(\"uHeight\"), street.eye);\n    gl.uniform1f(at(\"uWater\"), street.water);\n    gl.uniform1f(at(\"uRain\"), street.rain);\n    gl.uniform1f(at(\"uStone\"), street.stone);\n    gl.uniform1f(at(\"uGlow\"), street.glow);\n    gl.uniform1f(at(\"uPaint\"), street.paint);\n    gl.uniform2f(at(\"uPatch\"), street.patch[0], street.patch[1]);\n    [\"uSignA\", \"uSignB\", \"uSignC\", \"uSignD\", \"uSignE\"].forEach((name, i) => gl.uniform3fv(at(name), street.signs[i]));\n\n    const still = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const eye = { x: 0, y: 0, tx: 0, ty: 0, touched: -1e9 };\n    let raf = 0;\n    let visible = true;\n    let time = 12;\n    let before = 0;\n\n    const draw = () => {\n      gl.uniform1f(u.time, time);\n      gl.uniform2f(u.eye, eye.x * street.drift, eye.y * 0.05);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const resize = () => {\n      const w = canvas.clientWidth;\n      const h = canvas.clientHeight || 1;\n      const scale = Math.min(Math.min(Math.max(window.devicePixelRatio || 1, 1), 2) * street.quality, Math.sqrt(street.maxPixels / Math.max(1, w * h)));\n      canvas.width = Math.max(1, Math.round(w * scale));\n      canvas.height = Math.max(1, Math.round(h * scale));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      gl.uniform2f(u.res, canvas.width, canvas.height);\n      draw();\n      onLive(true);\n    };\n    const loop = (now: number) => {\n      raf = requestAnimationFrame(loop);\n      const dt = Math.min((now - (before || now)) / 1000, 0.05);\n      before = now;\n      time += dt * street.speed;\n      // Left alone, the viewer shifts their weight from foot to foot, and the mirrored signs slide.\n      if (now - eye.touched > 2600) {\n        eye.tx = Math.sin(time * 0.35);\n        eye.ty = 0.5 * Math.sin(time * 0.23 + 1);\n      }\n      const k = 1 - Math.exp(-dt * 2.6);\n      eye.x = mix(eye.x, eye.tx, k);\n      eye.y = mix(eye.y, eye.ty, k);\n      draw();\n    };\n    const start = () => {\n      cancelAnimationFrame(raf);\n      before = 0;\n      if (still) draw();\n      else if (visible && !document.hidden) raf = requestAnimationFrame(loop);\n    };\n    const onPointer = (e: PointerEvent) => {\n      const box = canvas.getBoundingClientRect();\n      const x = ((e.clientX - box.left) / box.width) * 2 - 1;\n      const y = 1 - ((e.clientY - box.top) / box.height) * 2;\n      if (Math.abs(x) > 1 || Math.abs(y) > 1) return;\n      eye.tx = x * 1.6;\n      eye.ty = -y;\n      eye.touched = performance.now();\n    };\n    const onLost = (e: Event) => {\n      e.preventDefault();\n      cancelAnimationFrame(raf);\n      onLive(false);\n    };\n\n    const sizeWatcher = new ResizeObserver(resize);\n    sizeWatcher.observe(canvas);\n    const viewWatcher = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      start();\n    });\n    viewWatcher.observe(canvas);\n    document.addEventListener(\"visibilitychange\", start);\n    if (!still) window.addEventListener(\"pointermove\", onPointer, { passive: true });\n    canvas.addEventListener(\"webglcontextlost\", onLost);\n    resize();\n    start();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      sizeWatcher.disconnect();\n      viewWatcher.disconnect();\n      document.removeEventListener(\"visibilitychange\", start);\n      window.removeEventListener(\"pointermove\", onPointer);\n      canvas.removeEventListener(\"webglcontextlost\", onLost);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    };\n  }, [onLive]);\n\n  return <canvas ref={ref} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />;\n}\n\n/** The street without a shader: blurred colour streaks on near-black. Shown first, and kept when WebGL is missing. */\nfunction StillStreet() {\n  return (\n    <div aria-hidden=\"true\" className=\"absolute inset-0 overflow-hidden bg-[#07070b]\">\n      <div className=\"absolute left-[14%] top-[-10%] h-[85%] w-[7%] bg-[linear-gradient(180deg,#ff1a8c_0%,rgba(255,26,140,0.35)_55%,rgba(255,26,140,0)_100%)] blur-2xl\" />\n      <div className=\"absolute left-[38%] top-[-10%] h-[60%] w-[26%] bg-[linear-gradient(180deg,#19d9ff_0%,rgba(25,217,255,0.3)_50%,rgba(25,217,255,0)_100%)] blur-3xl\" />\n      <div className=\"absolute left-[70%] top-[-10%] h-[95%] w-[9%] bg-[linear-gradient(180deg,#ff991f_0%,rgba(255,153,31,0.35)_60%,rgba(255,153,31,0)_100%)] blur-2xl\" />\n      <div className=\"absolute left-[88%] top-[-10%] h-[70%] w-[4%] bg-[linear-gradient(180deg,#4dff80_0%,rgba(77,255,128,0.3)_55%,rgba(77,255,128,0)_100%)] blur-2xl\" />\n      <div className=\"absolute inset-0 bg-[radial-gradient(120%_90%_at_50%_0%,rgba(7,7,11,0)_30%,rgba(7,7,11,0.85)_100%)]\" />\n    </div>\n  );\n}\n\nexport default function WetAsphaltAtNightWebglBackgroundWithPuddlesThatMirrorColouredShopSignsAndRainRings() {\n  const [live, setLive] = useState(false);\n\n  return (\n    <section className=\"relative isolate h-[620px] overflow-hidden bg-[#07070b] sm:h-[760px]\">\n      <StillStreet />\n      <div className={`pointer-events-none absolute inset-0 transition-opacity duration-700 motion-reduce:transition-none ${live ? \"opacity-100\" : \"opacity-0\"}`}>\n        <StreetCanvas onLive={setLive} />\n      </div>\n      <p className=\"absolute bottom-5 left-5 rounded-full bg-black/80 px-3.5 py-2 font-mono text-[11px] uppercase tracking-[0.2em] text-zinc-50 sm:bottom-7 sm:left-7\">\n        Wet asphalt <span className=\"hidden text-cyan-200 sm:inline\">/ after rain, signs across the road</span>\n      </p>\n    </section>\n  );\n}\n"}]}
