@arraypress/effects-astro 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (314) hide show
  1. package/AGENTS.md +85 -0
  2. package/COMPONENTS.md +1264 -0
  3. package/LICENSE +139 -0
  4. package/README.md +197 -2
  5. package/package.json +60 -4
  6. package/src/components/AsciiRain.astro +36 -0
  7. package/src/components/BorderSweep.astro +39 -0
  8. package/src/components/Caustics.astro +39 -0
  9. package/src/components/ColorPanels.astro +62 -0
  10. package/src/components/Cursor.astro +97 -0
  11. package/src/components/DotGrid.astro +34 -0
  12. package/src/components/DotSurface.astro +35 -0
  13. package/src/components/EffectCanvas.astro +152 -0
  14. package/src/components/FitText.astro +35 -0
  15. package/src/components/FlutedGlass.astro +36 -0
  16. package/src/components/Glass.astro +69 -0
  17. package/src/components/GlassGlyph.astro +38 -0
  18. package/src/components/GlowField.astro +177 -0
  19. package/src/components/Grain.astro +98 -0
  20. package/src/components/HaloRing.astro +38 -0
  21. package/src/components/HighlightSweep.astro +74 -0
  22. package/src/components/ImageRipple.astro +56 -0
  23. package/src/components/Ink.astro +37 -0
  24. package/src/components/InkField.astro +45 -0
  25. package/src/components/IsoCity.astro +54 -0
  26. package/src/components/LightField.astro +40 -0
  27. package/src/components/LiquidMetal.astro +41 -0
  28. package/src/components/Magnetic.astro +52 -0
  29. package/src/components/Marquee.astro +216 -0
  30. package/src/components/MosaicReveal.astro +196 -0
  31. package/src/components/NebulaLogo.astro +38 -0
  32. package/src/components/NeonSwarm.astro +38 -0
  33. package/src/components/Ocean.astro +40 -0
  34. package/src/components/OutlineWordmark.astro +222 -0
  35. package/src/components/PaperBackground.astro +40 -0
  36. package/src/components/PaperShader.astro +55 -0
  37. package/src/components/Parallax.astro +49 -0
  38. package/src/components/ParticleLogo.astro +37 -0
  39. package/src/components/PixelGlitchText.astro +70 -0
  40. package/src/components/PointCloud.astro +37 -0
  41. package/src/components/ProductStack.astro +298 -0
  42. package/src/components/PulseMark.astro +189 -0
  43. package/src/components/RadiantGlass.astro +38 -0
  44. package/src/components/RainOnGlass.astro +71 -0
  45. package/src/components/Reveal.astro +115 -0
  46. package/src/components/ScrambleText.astro +52 -0
  47. package/src/components/ScrollReveal.astro +38 -0
  48. package/src/components/ShaderBackground.astro +55 -0
  49. package/src/components/SignalLines.astro +41 -0
  50. package/src/components/SilkRibbon.astro +37 -0
  51. package/src/components/SpotlightText.astro +131 -0
  52. package/src/components/Tilt.astro +79 -0
  53. package/src/components/Topography.astro +38 -0
  54. package/src/components/Typewriter.astro +66 -0
  55. package/src/components/VariableType.astro +59 -0
  56. package/src/components/VoxelLogo.astro +46 -0
  57. package/src/components/WaveBars.astro +41 -0
  58. package/src/components/WorldRoutes.astro +41 -0
  59. package/src/components/style.js +19 -0
  60. package/src/index.d.ts +143 -0
  61. package/src/index.js +105 -0
  62. package/src/runtime/ascii-rain.d.ts +53 -0
  63. package/src/runtime/ascii-rain.js +355 -0
  64. package/src/runtime/audio-meter.d.ts +65 -0
  65. package/src/runtime/audio-meter.js +282 -0
  66. package/src/runtime/audio.d.ts +26 -0
  67. package/src/runtime/audio.js +453 -0
  68. package/src/runtime/blur.d.ts +2 -0
  69. package/src/runtime/blur.js +56 -0
  70. package/src/runtime/canvas2d.d.ts +23 -0
  71. package/src/runtime/canvas2d.js +105 -0
  72. package/src/runtime/caustics.d.ts +40 -0
  73. package/src/runtime/caustics.js +286 -0
  74. package/src/runtime/color.d.ts +33 -0
  75. package/src/runtime/color.js +213 -0
  76. package/src/runtime/curl.d.ts +5 -0
  77. package/src/runtime/curl.js +81 -0
  78. package/src/runtime/cursor.d.ts +9 -0
  79. package/src/runtime/cursor.js +80 -0
  80. package/src/runtime/device.d.ts +14 -0
  81. package/src/runtime/device.js +73 -0
  82. package/src/runtime/dot-grid.d.ts +27 -0
  83. package/src/runtime/dot-grid.js +245 -0
  84. package/src/runtime/dot-surface.d.ts +46 -0
  85. package/src/runtime/dot-surface.js +429 -0
  86. package/src/runtime/feedback.d.ts +18 -0
  87. package/src/runtime/feedback.js +131 -0
  88. package/src/runtime/fit-text.d.ts +5 -0
  89. package/src/runtime/fit-text.js +60 -0
  90. package/src/runtime/fluid.d.ts +55 -0
  91. package/src/runtime/fluid.js +504 -0
  92. package/src/runtime/fluted-glass.d.ts +27 -0
  93. package/src/runtime/fluted-glass.js +157 -0
  94. package/src/runtime/gl.d.ts +19 -0
  95. package/src/runtime/gl.js +104 -0
  96. package/src/runtime/glass-glyph.d.ts +49 -0
  97. package/src/runtime/glass-glyph.js +355 -0
  98. package/src/runtime/glass-sheen.d.ts +2 -0
  99. package/src/runtime/glass-sheen.js +30 -0
  100. package/src/runtime/glsl.d.ts +17 -0
  101. package/src/runtime/glsl.js +78 -0
  102. package/src/runtime/glyphs.d.ts +22 -0
  103. package/src/runtime/glyphs.js +107 -0
  104. package/src/runtime/halo-ring.d.ts +47 -0
  105. package/src/runtime/halo-ring.js +334 -0
  106. package/src/runtime/height-field.d.ts +50 -0
  107. package/src/runtime/height-field.js +176 -0
  108. package/src/runtime/image-ripple.d.ts +25 -0
  109. package/src/runtime/image-ripple.js +353 -0
  110. package/src/runtime/image.d.ts +11 -0
  111. package/src/runtime/image.js +81 -0
  112. package/src/runtime/in-view.d.ts +6 -0
  113. package/src/runtime/in-view.js +36 -0
  114. package/src/runtime/ink-field/shaders.js +258 -0
  115. package/src/runtime/ink-field/sim.js +582 -0
  116. package/src/runtime/ink-field.d.ts +39 -0
  117. package/src/runtime/ink-field.js +311 -0
  118. package/src/runtime/ink.d.ts +31 -0
  119. package/src/runtime/ink.js +109 -0
  120. package/src/runtime/instancing.d.ts +12 -0
  121. package/src/runtime/instancing.js +37 -0
  122. package/src/runtime/iso-camera.d.ts +35 -0
  123. package/src/runtime/iso-camera.js +85 -0
  124. package/src/runtime/iso-city/figures.d.ts +30 -0
  125. package/src/runtime/iso-city/figures.js +136 -0
  126. package/src/runtime/iso-city/look.d.ts +34 -0
  127. package/src/runtime/iso-city/look.js +105 -0
  128. package/src/runtime/iso-city/plan.d.ts +59 -0
  129. package/src/runtime/iso-city/plan.js +463 -0
  130. package/src/runtime/iso-city/roads.d.ts +64 -0
  131. package/src/runtime/iso-city/roads.js +320 -0
  132. package/src/runtime/iso-city/scene.d.ts +27 -0
  133. package/src/runtime/iso-city/scene.js +432 -0
  134. package/src/runtime/iso-city/shaders.d.ts +6 -0
  135. package/src/runtime/iso-city/shaders.js +233 -0
  136. package/src/runtime/iso-city/shadows.d.ts +13 -0
  137. package/src/runtime/iso-city/shadows.js +148 -0
  138. package/src/runtime/iso-city/traffic.d.ts +80 -0
  139. package/src/runtime/iso-city/traffic.js +457 -0
  140. package/src/runtime/iso-city.d.ts +37 -0
  141. package/src/runtime/iso-city.js +458 -0
  142. package/src/runtime/lens-map.d.ts +11 -0
  143. package/src/runtime/lens-map.js +146 -0
  144. package/src/runtime/light-field.d.ts +29 -0
  145. package/src/runtime/light-field.js +292 -0
  146. package/src/runtime/liquid-glass.d.ts +48 -0
  147. package/src/runtime/liquid-glass.js +423 -0
  148. package/src/runtime/liquid-metal/beads.js +217 -0
  149. package/src/runtime/liquid-metal/field.js +88 -0
  150. package/src/runtime/liquid-metal/look.js +64 -0
  151. package/src/runtime/liquid-metal/shaders.js +255 -0
  152. package/src/runtime/liquid-metal/wave.js +53 -0
  153. package/src/runtime/liquid-metal.d.ts +46 -0
  154. package/src/runtime/liquid-metal.js +366 -0
  155. package/src/runtime/loop.d.ts +7 -0
  156. package/src/runtime/loop.js +112 -0
  157. package/src/runtime/magnetic.d.ts +9 -0
  158. package/src/runtime/magnetic.js +46 -0
  159. package/src/runtime/marquee.d.ts +2 -0
  160. package/src/runtime/marquee.js +27 -0
  161. package/src/runtime/mesh.d.ts +30 -0
  162. package/src/runtime/mesh.js +217 -0
  163. package/src/runtime/mosaic-reveal.d.ts +34 -0
  164. package/src/runtime/mosaic-reveal.js +504 -0
  165. package/src/runtime/motion.d.ts +2 -0
  166. package/src/runtime/motion.js +21 -0
  167. package/src/runtime/mount.d.ts +13 -0
  168. package/src/runtime/mount.js +120 -0
  169. package/src/runtime/nebula-logo.d.ts +31 -0
  170. package/src/runtime/nebula-logo.js +282 -0
  171. package/src/runtime/neon-swarm.d.ts +31 -0
  172. package/src/runtime/neon-swarm.js +258 -0
  173. package/src/runtime/ocean/presets.d.ts +54 -0
  174. package/src/runtime/ocean/presets.js +184 -0
  175. package/src/runtime/ocean/shader.d.ts +14 -0
  176. package/src/runtime/ocean/shader.js +389 -0
  177. package/src/runtime/ocean/view.d.ts +24 -0
  178. package/src/runtime/ocean/view.js +98 -0
  179. package/src/runtime/ocean/waves.d.ts +29 -0
  180. package/src/runtime/ocean/waves.js +119 -0
  181. package/src/runtime/ocean.d.ts +31 -0
  182. package/src/runtime/ocean.js +173 -0
  183. package/src/runtime/palettes.d.ts +37 -0
  184. package/src/runtime/palettes.js +75 -0
  185. package/src/runtime/paper-look.d.ts +42 -0
  186. package/src/runtime/paper-look.js +91 -0
  187. package/src/runtime/paper-looks.d.ts +17 -0
  188. package/src/runtime/paper-looks.js +21 -0
  189. package/src/runtime/paper.d.ts +37 -0
  190. package/src/runtime/paper.js +303 -0
  191. package/src/runtime/particle-logo.d.ts +31 -0
  192. package/src/runtime/particle-logo.js +196 -0
  193. package/src/runtime/ping-pong.d.ts +43 -0
  194. package/src/runtime/ping-pong.js +175 -0
  195. package/src/runtime/pixel-glitch.d.ts +17 -0
  196. package/src/runtime/pixel-glitch.js +287 -0
  197. package/src/runtime/point-cloud.d.ts +33 -0
  198. package/src/runtime/point-cloud.js +287 -0
  199. package/src/runtime/pointer.d.ts +27 -0
  200. package/src/runtime/pointer.js +128 -0
  201. package/src/runtime/points.d.ts +25 -0
  202. package/src/runtime/points.js +130 -0
  203. package/src/runtime/pools.d.ts +2 -0
  204. package/src/runtime/pools.js +25 -0
  205. package/src/runtime/product-stack.d.ts +24 -0
  206. package/src/runtime/product-stack.js +210 -0
  207. package/src/runtime/radiant-glass.d.ts +29 -0
  208. package/src/runtime/radiant-glass.js +282 -0
  209. package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
  210. package/src/runtime/rain-on-glass/backdrop.js +185 -0
  211. package/src/runtime/rain-on-glass/shader.d.ts +2 -0
  212. package/src/runtime/rain-on-glass/shader.js +168 -0
  213. package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
  214. package/src/runtime/rain-on-glass/wipe.js +65 -0
  215. package/src/runtime/rain-on-glass.d.ts +27 -0
  216. package/src/runtime/rain-on-glass.js +210 -0
  217. package/src/runtime/ramp.d.ts +2 -0
  218. package/src/runtime/ramp.js +30 -0
  219. package/src/runtime/random.d.ts +2 -0
  220. package/src/runtime/random.js +22 -0
  221. package/src/runtime/scroll-reveal.d.ts +22 -0
  222. package/src/runtime/scroll-reveal.js +73 -0
  223. package/src/runtime/shader-background.d.ts +42 -0
  224. package/src/runtime/shader-background.js +261 -0
  225. package/src/runtime/shader-backgrounds/band-lens.js +72 -0
  226. package/src/runtime/shader-backgrounds/common.js +53 -0
  227. package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
  228. package/src/runtime/shader-backgrounds/grid.js +33 -0
  229. package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
  230. package/src/runtime/shader-backgrounds/silk.js +57 -0
  231. package/src/runtime/shader-backgrounds/waves.js +31 -0
  232. package/src/runtime/shape.d.ts +42 -0
  233. package/src/runtime/shape.js +313 -0
  234. package/src/runtime/silk-ribbon.d.ts +23 -0
  235. package/src/runtime/silk-ribbon.js +206 -0
  236. package/src/runtime/slideshow.d.ts +27 -0
  237. package/src/runtime/slideshow.js +234 -0
  238. package/src/runtime/spotlight.d.ts +9 -0
  239. package/src/runtime/spotlight.js +97 -0
  240. package/src/runtime/stack-poses.d.ts +53 -0
  241. package/src/runtime/stack-poses.js +273 -0
  242. package/src/runtime/stage.d.ts +47 -0
  243. package/src/runtime/stage.js +187 -0
  244. package/src/runtime/swarm.d.ts +49 -0
  245. package/src/runtime/swarm.js +199 -0
  246. package/src/runtime/text-motion.d.ts +41 -0
  247. package/src/runtime/text-motion.js +165 -0
  248. package/src/runtime/text.d.ts +11 -0
  249. package/src/runtime/text.js +90 -0
  250. package/src/runtime/tilt.d.ts +19 -0
  251. package/src/runtime/tilt.js +116 -0
  252. package/src/runtime/topography.d.ts +71 -0
  253. package/src/runtime/topography.js +650 -0
  254. package/src/runtime/trackers.d.ts +21 -0
  255. package/src/runtime/trackers.js +76 -0
  256. package/src/runtime/variable-type.d.ts +43 -0
  257. package/src/runtime/variable-type.js +358 -0
  258. package/src/runtime/voxel-logo.d.ts +31 -0
  259. package/src/runtime/voxel-logo.js +237 -0
  260. package/src/runtime/waves.d.ts +70 -0
  261. package/src/runtime/waves.js +799 -0
  262. package/src/runtime/world-labels.d.ts +29 -0
  263. package/src/runtime/world-labels.js +59 -0
  264. package/src/runtime/world-land.d.ts +8 -0
  265. package/src/runtime/world-land.js +105 -0
  266. package/src/runtime/world-map.d.ts +69 -0
  267. package/src/runtime/world-map.js +308 -0
  268. package/src/runtime/world-plan.d.ts +101 -0
  269. package/src/runtime/world-plan.js +333 -0
  270. package/src/runtime/world-routes.d.ts +75 -0
  271. package/src/runtime/world-routes.js +673 -0
  272. package/src/styles/border-sweep.css +82 -0
  273. package/src/styles/glass.css +104 -0
  274. package/src/styles/motion.css +218 -0
  275. package/src/styles/reveal.css +51 -0
  276. package/src/vendor/paper/LICENSE +201 -0
  277. package/src/vendor/paper/NOTICE +5 -0
  278. package/src/vendor/paper/README.md +8 -0
  279. package/src/vendor/paper/get-shader-color-from-string.js +100 -0
  280. package/src/vendor/paper/get-shader-noise-texture.js +17 -0
  281. package/src/vendor/paper/presets.js +3678 -0
  282. package/src/vendor/paper/shader-sizing.js +37 -0
  283. package/src/vendor/paper/shader-utils.js +130 -0
  284. package/src/vendor/paper/shaders/color-panels.js +223 -0
  285. package/src/vendor/paper/shaders/dithering.js +270 -0
  286. package/src/vendor/paper/shaders/dot-grid.js +109 -0
  287. package/src/vendor/paper/shaders/dot-orbit.js +122 -0
  288. package/src/vendor/paper/shaders/fluted-glass.js +360 -0
  289. package/src/vendor/paper/shaders/gem-smoke.js +572 -0
  290. package/src/vendor/paper/shaders/god-rays.js +132 -0
  291. package/src/vendor/paper/shaders/grain-gradient.js +300 -0
  292. package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
  293. package/src/vendor/paper/shaders/halftone-dots.js +320 -0
  294. package/src/vendor/paper/shaders/heatmap.js +383 -0
  295. package/src/vendor/paper/shaders/image-dithering.js +198 -0
  296. package/src/vendor/paper/shaders/lens-distortion.js +275 -0
  297. package/src/vendor/paper/shaders/liquid-metal.js +632 -0
  298. package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
  299. package/src/vendor/paper/shaders/metaballs.js +110 -0
  300. package/src/vendor/paper/shaders/neuro-noise.js +76 -0
  301. package/src/vendor/paper/shaders/paper-texture.js +526 -0
  302. package/src/vendor/paper/shaders/perlin-noise.js +166 -0
  303. package/src/vendor/paper/shaders/pulsing-border.js +238 -0
  304. package/src/vendor/paper/shaders/simplex-noise.js +91 -0
  305. package/src/vendor/paper/shaders/smoke-ring.js +132 -0
  306. package/src/vendor/paper/shaders/spiral.js +82 -0
  307. package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
  308. package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
  309. package/src/vendor/paper/shaders/swirl.js +103 -0
  310. package/src/vendor/paper/shaders/voronoi.js +138 -0
  311. package/src/vendor/paper/shaders/warp.js +129 -0
  312. package/src/vendor/paper/shaders/water.js +121 -0
  313. package/src/vendor/paper/shaders/waves.js +65 -0
  314. package/src/vendor/paper/vertex-shader.js +159 -0
@@ -0,0 +1,282 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/radiant-glass
3
+ *
4
+ * A cluster of frosted glass tiles, lit from behind: big soft lights drift
5
+ * behind the glass, and each tile shows them heavily blurred and bent, so
6
+ * it glows from inside while the ground and the gaps between the tiles
7
+ * stay dark. Each tile has a thin rim, cool on one side and warm on the
8
+ * other, and a soft halo of its own light round it.
9
+ *
10
+ * One pass and no textures: the tiles are rounded-box distance fields,
11
+ * and the lights are gaussians, so frosting one is only widening it (a
12
+ * blurred gaussian is a wider gaussian). Each light is a long streak
13
+ * with a different colour along each side, as a lens splits white light;
14
+ * they mix in Oklab and run to the hot colour where they gather, and the
15
+ * result is dithered so the long gradients do not band.
16
+ *
17
+ * The lights drift on their own and shift with the pointer. Reduced
18
+ * motion gets a still frame.
19
+ *
20
+ * Opaque: the effect is the whole background of what it fills. Without
21
+ * WebGL it removes its canvas and the element's background shows.
22
+ */
23
+
24
+ import { createStage } from './stage.js';
25
+ import { parseColor, toOklab, GLSL_OKLAB } from './color.js';
26
+ import { canHover } from './device.js';
27
+ import { pointerIn } from './pointer.js';
28
+ import { watchColors } from './mount.js';
29
+ import { pickColors } from './palettes.js';
30
+ import { GLSL_NOISE } from './glsl.js';
31
+
32
+ /**
33
+ * @typedef {object} RadiantGlassConfig
34
+ * @property {string[]} [colors] The lights' colours, the last the hot colour where they gather (default amber, blue, white).
35
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`. Its colours light the glass; its glint is the hot colour.
36
+ * @property {string} [background='#060818'] The ground.
37
+ * @property {'cross' | 'diagonal' | 'single'} [layout='cross'] The tiles: four in a plus, three on a slant, or one large.
38
+ * @property {'diamond' | 'square' | 'circle'} [shape='diamond'] Each tile's shape: a rounded square on its corner, square on, or round.
39
+ * @property {'left' | 'right'} [side='left'] Which side the cluster sits on, leaving the other for the copy.
40
+ * @property {number} [size=1] The cluster's size; 1 fills the height.
41
+ * @property {number} [frost=1] How blurred the light through the glass is; 0 for clear.
42
+ * @property {number} [glow=1] How strong the halo round each tile is; 0 for none.
43
+ * @property {number} [rim=1] How bright the rim line is; 0 for none.
44
+ * @property {number} [speed=1] How fast the lights drift.
45
+ */
46
+
47
+ const FRAG = `
48
+ precision highp float;
49
+ uniform vec2 uRes;
50
+ uniform float uTime;
51
+ uniform vec2 uPtr;
52
+ uniform vec3 uGround;
53
+ uniform vec3 uLights[4];
54
+ uniform vec3 uHot;
55
+ uniform vec2 uCenter;
56
+ uniform float uFlip;
57
+ uniform float uUnit;
58
+ uniform vec3 uTiles[4];
59
+ uniform float uCount;
60
+ uniform float uAngle;
61
+ uniform float uRound;
62
+ uniform float uFrost;
63
+ uniform float uGlow;
64
+ uniform float uRim;
65
+ ${GLSL_OKLAB}
66
+ ${GLSL_NOISE}
67
+
68
+ // A rounded box's signed distance and its outward direction, in the box's frame.
69
+ vec3 roundBox(vec2 q, float b, float r) {
70
+ vec2 s = sign(q);
71
+ vec2 e = abs(q) - b + r;
72
+ vec2 out_ = max(e, 0.0);
73
+ float lo = length(out_);
74
+ vec2 n = lo > 0.0 ? out_ / lo : (e.x > e.y ? vec2(1.0, 0.0) : vec2(0.0, 1.0));
75
+ return vec3(lo + min(max(e.x, e.y), 0.0) - r, n * s);
76
+ }
77
+
78
+ // The lights at a point, widened by a blur: their weighted colour (Oklab) and their sum.
79
+ // Each is a long soft streak turning slowly; blurring one only widens it.
80
+ vec4 lights(vec2 x, float blur) {
81
+ vec3 lab = vec3(0.0);
82
+ float total = 0.0, sharp = 0.0;
83
+ float bb = blur * blur;
84
+ for (int i = 0; i < 5; i++) {
85
+ float k = float(i);
86
+ // The first, the broadest and strongest, keeps to the tile nearest the copy.
87
+ vec2 home = vec2(cos(k * 2.4) * uFlip, sin(k * 2.4)) * (0.2 + 0.1 * mod(k, 2.0));
88
+ vec2 drift = vec2(sin(uTime * (0.21 + k * 0.037) + k * 1.7), cos(uTime * (0.17 + k * 0.029) + k * 2.3)) * (i == 0 ? 0.1 : 0.2);
89
+ // Each leans its own way with the pointer, so the light shifts within the tiles rather than leaving them.
90
+ vec2 at = uCenter + (home + drift + uPtr * (mod(k, 2.0) - 0.5) * (0.8 + k * 0.2)) * uUnit;
91
+ float turn = k * 1.1 + uTime * (0.05 + k * 0.013);
92
+ vec2 d = mat2(cos(turn), sin(turn), -sin(turn), cos(turn)) * (x - at);
93
+ vec2 v = (i == 0 ? vec2(0.2, 0.12) : vec2(0.17 + 0.04 * mod(k, 3.0), 0.07 + 0.02 * mod(k, 2.0))) * uUnit;
94
+ v *= v;
95
+ vec2 w = v + bb;
96
+ float weight = exp(-0.5 * dot(d * d, 1.0 / w)) * sqrt(v.x * v.y / (w.x * w.y)) * (i == 0 ? 1.4 : 1.0);
97
+ // Two colours, one along each long side, as a lens splits white light;
98
+ // weighted to the strongest light more than its share, so two meet without going grey.
99
+ vec3 tint = mix(uLights[i - (i / 4) * 4], uLights[i - ((i + 1) / 4) * 4 + 1], smoothstep(-1.2, 1.2, d.y / sqrt(w.y)));
100
+ lab += tint * weight * weight;
101
+ sharp += weight * weight;
102
+ total += weight;
103
+ }
104
+ return vec4(lab / max(sharp, 1e-6), total);
105
+ }
106
+
107
+ // The lights as colour: their mix, running to the hot colour as they strengthen, at their strength.
108
+ vec3 shade(vec4 l, float gain) {
109
+ float s = l.a * gain;
110
+ vec3 lab = l.rgb;
111
+ // Richer where it is dim, so a fading light glows rather than greys or browns.
112
+ lab.yz *= 1.0 + 0.45 * (1.0 - smoothstep(0.1, 0.9, s));
113
+ lab = mix(lab, uHot, smoothstep(0.55, 1.5, s) * 0.9);
114
+ // A steep curve: a light holds its colour, then falls away to dark.
115
+ return fromOklab(lab) * (1.0 - exp(-pow(s, 1.6) * 2.6));
116
+ }
117
+
118
+ // Where the glass at a point of a tile looks: a touch magnified, and drawn
119
+ // in toward the cluster's middle, so every tile sees the lights.
120
+ vec2 view(vec2 x, vec2 c) { return uCenter + (c - uCenter) * 0.7 + (x - c) * 0.82; }
121
+
122
+ void main() {
123
+ vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;
124
+ float px = 1.0 / uRes.y;
125
+ float cs = cos(uAngle), sn = sin(uAngle);
126
+ mat2 toTile = mat2(cs, -sn, sn, cs);
127
+
128
+ // The nearest tile: its distance, outward direction and centre; and how far the next one is.
129
+ float d = 1e3, next = 1e3;
130
+ vec2 n = vec2(0.0);
131
+ vec2 c = uCenter;
132
+ for (int i = 0; i < 4; i++) {
133
+ if (float(i) >= uCount) break;
134
+ vec2 at = uCenter + uTiles[i].xy * uUnit;
135
+ float b = 0.194 * uUnit * uTiles[i].z;
136
+ vec3 t = roundBox(toTile * (p - at), b, b * uRound);
137
+ if (t.x < d) { next = d; d = t.x; n = t.yz; c = at; } else next = min(next, t.x);
138
+ }
139
+ n = n * toTile;
140
+
141
+ vec3 col = uGround;
142
+ // The halo: the light just inside the nearest edge, spread and fading away
143
+ // from it; none in the gaps, where another tile is about as near.
144
+ if (d > 0.0) {
145
+ vec2 edge = p - n * d;
146
+ vec3 halo = shade(lights(view(edge, c), 0.16 * uUnit), 1.0);
147
+ float open = smoothstep(0.0, 0.16 * uUnit, next - d);
148
+ col += halo * exp(-d / (0.055 * uUnit)) * 0.38 * open * uGlow;
149
+ }
150
+ // Through the glass: the lights behind, frosted, and bent outward near the
151
+ // edge, so the rim gathers light from beyond it. Only where there is glass.
152
+ if (d < px) {
153
+ float inner = max(-d, 0.0);
154
+ float bend = 1.0 - clamp(inner / (0.07 * uUnit), 0.0, 1.0);
155
+ bend *= bend;
156
+ vec2 look = view(p, c) + n * bend * 0.09 * uUnit;
157
+ vec3 glass = shade(lights(look, 0.04 * uFrost * uUnit), 1.0);
158
+ // A dark seam just inside the rim, where the glass turns away.
159
+ glass *= mix(0.4, 1.0, smoothstep(0.0, 0.009 * uUnit, inner));
160
+ glass += uGround;
161
+ // The rim: a hairline, cool on the edges facing away from the copy, warm on
162
+ // those facing it, and brighter where the light behind is.
163
+ float line = 1.0 - smoothstep(0.0, 1.4 * px, abs(d + 0.8 * px));
164
+ if (line > 0.0) {
165
+ float face = dot(n, normalize(vec2(uFlip, 0.45))) * 0.5 + 0.5;
166
+ vec3 rimCol = fromOklab(mix(uLights[1], uLights[0], smoothstep(0.2, 0.8, face)));
167
+ glass += (rimCol + shade(lights(p, 0.12 * uUnit), 1.0)) * 0.7 * line * uRim;
168
+ }
169
+ col = mix(col, glass, smoothstep(px, -px, d));
170
+ }
171
+
172
+ col += (hash(gl_FragCoord.xy + fract(uTime * 7.0)) - 0.5) * (1.5 / 255.0);
173
+ gl_FragColor = vec4(col, 1.0);
174
+ }
175
+ `;
176
+
177
+ /** The default lights: amber, blue, and white where they gather. */
178
+ const COLORS = ['#ffb27a', '#4a68ff', '#fbf7ff'];
179
+
180
+ /** Each layout's tiles: x, y in units of the cluster, and a size. */
181
+ const LAYOUTS = {
182
+ cross: [
183
+ [0.355, 0, 1],
184
+ [0, 0.355, 1],
185
+ [0, -0.355, 1],
186
+ [-0.355, 0, 1],
187
+ ],
188
+ diagonal: [
189
+ [-0.2, 0.34, 0.9],
190
+ [0.14, -0.02, 1.05],
191
+ [0.48, -0.38, 0.9],
192
+ ],
193
+ single: [[0.12, 0, 1.7]],
194
+ };
195
+
196
+ /** Each shape: its turn in radians, and its corners' radius as a share of its half size. */
197
+ const SHAPES = {
198
+ diamond: [Math.PI / 4, 0.38],
199
+ square: [0, 0.3],
200
+ circle: [0, 1],
201
+ };
202
+
203
+ /**
204
+ * Starts radiant glass on an element: an opaque canvas inside it, filling it.
205
+ *
206
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
207
+ * @param {RadiantGlassConfig} config The settings.
208
+ * @returns {() => void} Stops it.
209
+ */
210
+ export function radiantGlass(el, config) {
211
+ const colors = pickColors(config, COLORS, (p) => [...p.colors, p.glint]);
212
+ const start = performance.now();
213
+ const speed = config.speed ?? 1;
214
+ const ptr = { x: 0, y: 0 };
215
+ const tiles = LAYOUTS[config.layout ?? 'cross'] ?? LAYOUTS.cross;
216
+ const [angle, round] = SHAPES[config.shape ?? 'diamond'] ?? SHAPES.diamond;
217
+ // Everything mirrors when the cluster is on the right.
218
+ const flip = config.side === 'right' ? -1 : 1;
219
+
220
+ /**
221
+ * The lights and the hot colour as the shader takes them, in Oklab: up
222
+ * to four lights (repeated to fill), then the hot colour.
223
+ *
224
+ * @param {string[]} list The colours, the hot one last.
225
+ * @returns {{ lights: Float32Array, hot: Float32Array }} The uniforms' values.
226
+ */
227
+ const toUniforms = (list) => {
228
+ const lights = list.length > 1 ? list.slice(0, -1) : list;
229
+ return {
230
+ lights: new Float32Array([0, 1, 2, 3].flatMap((i) => toOklab(parseColor(lights[i % lights.length])))),
231
+ hot: new Float32Array(toOklab(parseColor(list[list.length - 1]))),
232
+ };
233
+ };
234
+ let lab = toUniforms(colors);
235
+
236
+ const { stage, stop } = createStage(el, {
237
+ fragment: FRAG,
238
+ alpha: false,
239
+ setup({ gl, uniform }) {
240
+ gl.uniform3fv(uniform('uGround'), parseColor(config.background ?? '#060818'));
241
+ gl.uniform3fv(uniform('uTiles'), new Float32Array([0, 1, 2, 3].flatMap((i) => (tiles[i] ? [tiles[i][0] * flip, tiles[i][1], tiles[i][2]] : [0, 0, 0]))));
242
+ gl.uniform1f(uniform('uCount'), tiles.length);
243
+ gl.uniform1f(uniform('uFlip'), flip);
244
+ gl.uniform1f(uniform('uAngle'), angle);
245
+ gl.uniform1f(uniform('uRound'), round);
246
+ gl.uniform1f(uniform('uFrost'), config.frost ?? 1);
247
+ gl.uniform1f(uniform('uGlow'), config.glow ?? 1);
248
+ gl.uniform1f(uniform('uRim'), config.rim ?? 1);
249
+ },
250
+ resize({ gl, uniform, canvas }) {
251
+ // The cluster fills the height on a wide screen and shrinks on a narrow one,
252
+ // its middle a little in from the edge so the outer tile is cropped.
253
+ const aspect = canvas.width / canvas.height;
254
+ const unit = (config.size ?? 1) * Math.min(1, Math.max(0.5, aspect / 1.68));
255
+ const x = 0.385 * unit - aspect / 2;
256
+ gl.uniform2f(uniform('uCenter'), x * flip, 0);
257
+ gl.uniform1f(uniform('uUnit'), unit);
258
+ },
259
+ uniforms({ gl, uniform, still }, now) {
260
+ // Started a little way in, where the lights sit well; a still frame stays there.
261
+ const t = 8 + (still ? 0 : ((now - start) / 1000) * speed);
262
+ const p = !still && canHover() ? pointerIn(el) : null;
263
+ const goal = p?.inside ? { x: (p.x - 0.5) * 0.3, y: (p.y - 0.5) * 0.3 } : { x: 0, y: 0 };
264
+ ptr.x += (goal.x - ptr.x) * 0.03;
265
+ ptr.y += (goal.y - ptr.y) * 0.03;
266
+ gl.uniform1f(uniform('uTime'), t);
267
+ gl.uniform2f(uniform('uPtr'), ptr.x, ptr.y);
268
+ gl.uniform3fv(uniform('uLights'), lab.lights);
269
+ gl.uniform3fv(uniform('uHot'), lab.hot);
270
+ },
271
+ });
272
+ if (!stage) return stop;
273
+
274
+ const unwatch = watchColors(el, (next) => {
275
+ lab = toUniforms(next.glint ? [...next.colors, next.glint] : next.colors);
276
+ if (stage.still) stage.redraw();
277
+ });
278
+ return () => {
279
+ stop();
280
+ unwatch();
281
+ };
282
+ }
@@ -0,0 +1,22 @@
1
+ /** A bright point in a picture. */
2
+ export interface Light {
3
+ /** Across the picture, 0–1 from the left. */
4
+ x: number;
5
+ /** Down the picture, 0–1 from the top. */
6
+ y: number;
7
+ /** How bright, 0–1. */
8
+ level: number;
9
+ /** Its colour around it, each channel 0–1, the brightest 1. */
10
+ rgb: [number, number, number];
11
+ }
12
+
13
+ /** The brightest local peaks in RGBA pixels, brightest first, at most `count` (28), over `threshold` (0.6), `spacing` (10) pixels apart. */
14
+ export function findLights(
15
+ data: Uint8ClampedArray,
16
+ width: number,
17
+ height: number,
18
+ options?: { count?: number; threshold?: number; spacing?: number },
19
+ ): Light[];
20
+
21
+ /** A small, blurred copy of a photo with bokeh discs over its lights (`colors` for them, or each light's own). Throws without a 2D canvas. */
22
+ export function backdrop(img: CanvasImageSource & { width: number; height: number }, options: { blur: number; bokeh: number; colors: string[] }): HTMLCanvasElement;
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/rain-on-glass/backdrop
3
+ *
4
+ * The photo as it looks through fogged glass, made once at load: a small
5
+ * blurred copy, with the photo's brightest lights opened out into bokeh
6
+ * discs the way a lens out of focus draws them.
7
+ */
8
+
9
+ import { boxBlur } from '../blur.js';
10
+ import { parseColor } from '../color.js';
11
+
12
+ /** Pixels across the copy the lights are found in, the copy that is blurred, and the copy the discs are drawn on. */
13
+ const FIND = 256;
14
+ const BLUR = 128;
15
+ const LARGE = 640;
16
+
17
+ /**
18
+ * @typedef {object} Light
19
+ * @property {number} x Across the picture, 0–1 from the left.
20
+ * @property {number} y Down the picture, 0–1 from the top.
21
+ * @property {number} level How bright, 0–1.
22
+ * @property {[number, number, number]} rgb Its colour around it, each 0–1, brightest channel 1.
23
+ */
24
+
25
+ /**
26
+ * Finds the brightest points in a picture: local peaks over `threshold`,
27
+ * brightest first, none closer than `spacing` to a brighter one.
28
+ *
29
+ * @param {Uint8ClampedArray} data The pixels, RGBA, row by row.
30
+ * @param {number} width Pixels across.
31
+ * @param {number} height Pixels down.
32
+ * @param {{ count?: number, threshold?: number, spacing?: number }} [options] At most `count` (default 22),
33
+ * at least `threshold` bright (0–1, default 0.6), at least `spacing` pixels apart (default 10).
34
+ * @returns {Light[]} The lights.
35
+ */
36
+ export function findLights(data, width, height, { count = 22, threshold = 0.6, spacing = 10 } = {}) {
37
+ const level = new Float32Array(width * height);
38
+ for (let i = 0; i < level.length; i++) {
39
+ level[i] = (data[i * 4] * 0.2126 + data[i * 4 + 1] * 0.7152 + data[i * 4 + 2] * 0.0722) / 255;
40
+ }
41
+ /** @type {number[]} */
42
+ const peaks = [];
43
+ for (let y = 1; y < height - 1; y++) {
44
+ for (let x = 1; x < width - 1; x++) {
45
+ const i = y * width + x;
46
+ const v = level[i];
47
+ if (v < threshold) continue;
48
+ let top = true;
49
+ for (let dy = -1; dy <= 1 && top; dy++) for (let dx = -1; dx <= 1; dx++) if (level[i + dy * width + dx] > v) top = false;
50
+ if (top) peaks.push(i);
51
+ }
52
+ }
53
+ peaks.sort((a, b) => level[b] - level[a]);
54
+ /** @type {Light[]} */
55
+ const lights = [];
56
+ for (const i of peaks) {
57
+ if (lights.length >= count) break;
58
+ const x = i % width;
59
+ const y = (i - x) / width;
60
+ if (lights.some((l) => Math.hypot(l.x * width - x, l.y * height - y) < spacing)) continue;
61
+ lights.push({ x: x / width, y: y / height, level: level[i], rgb: around(data, width, height, x, y) });
62
+ }
63
+ return lights;
64
+ }
65
+
66
+ /**
67
+ * The colour around a pixel, brightened until its strongest channel is
68
+ * full and pushed away from grey: a light's own hue, which its blown-out
69
+ * centre often loses.
70
+ *
71
+ * @param {Uint8ClampedArray} data The pixels, RGBA, row by row.
72
+ * @param {number} width Pixels across.
73
+ * @param {number} height Pixels down.
74
+ * @param {number} x The pixel's column.
75
+ * @param {number} y The pixel's row.
76
+ * @returns {[number, number, number]} The colour, each channel 0–1.
77
+ */
78
+ function around(data, width, height, x, y) {
79
+ const sum = [0, 0, 0];
80
+ for (let dy = -3; dy <= 3; dy++) {
81
+ for (let dx = -3; dx <= 3; dx++) {
82
+ const px = Math.min(width - 1, Math.max(0, x + dx));
83
+ const py = Math.min(height - 1, Math.max(0, y + dy));
84
+ for (let c = 0; c < 3; c++) sum[c] += data[(py * width + px) * 4 + c];
85
+ }
86
+ }
87
+ const top = Math.max(1, ...sum);
88
+ const rgb = sum.map((c) => c / top);
89
+ const mean = (rgb[0] + rgb[1] + rgb[2]) / 3;
90
+ const [r, g, b] = rgb.map((c) => Math.min(1, Math.max(0, mean + (c - mean) * 1.8)));
91
+ return [r, g, b];
92
+ }
93
+
94
+ /**
95
+ * A small, blurred copy of a photo with bokeh discs over its lights.
96
+ *
97
+ * @param {CanvasImageSource & { width: number, height: number }} img The photo.
98
+ * @param {{ blur: number, bokeh: number, colors: string[] }} options `blur` and `bokeh` 0–1;
99
+ * `colors` for the discs (empty: each light's own colour).
100
+ * @returns {HTMLCanvasElement} The copy, `LARGE` pixels across.
101
+ * @throws {Error} When the browser gives no 2D canvas.
102
+ */
103
+ export function backdrop(img, { blur, bokeh, colors }) {
104
+ const aspect = img.width / img.height;
105
+ // Scaled down once from the photo; the blurred copy is halved from that.
106
+ const find = canvas(FIND, Math.max(1, Math.round(FIND / aspect)));
107
+ const fc = context(find);
108
+ fc.drawImage(img, 0, 0, find.width, find.height);
109
+ const lights = bokeh > 0 ? findLights(fc.getImageData(0, 0, find.width, find.height).data, find.width, find.height) : [];
110
+ const small = canvas(BLUR, Math.max(1, Math.round(BLUR / aspect)));
111
+ const sc = context(small);
112
+ sc.drawImage(find, 0, 0, small.width, small.height);
113
+ const pixels = sc.getImageData(0, 0, small.width, small.height);
114
+ boxBlur(pixels.data, small.width, small.height, 0.5 + blur * 3.5);
115
+ sc.putImageData(pixels, 0, 0);
116
+
117
+ const out = canvas(LARGE, Math.max(1, Math.round(LARGE / aspect)));
118
+ const ctx = context(out);
119
+ ctx.imageSmoothingQuality = 'high';
120
+ ctx.drawImage(small, 0, 0, out.width, out.height);
121
+ ctx.globalCompositeOperation = 'screen';
122
+ const tints = colors.map(parseColor);
123
+ const size = out.width * (0.008 + blur * 0.022);
124
+ lights.forEach((light, i) => {
125
+ // A steady spread of sizes and strengths, the same on every load.
126
+ const jitter = fract(Math.sin(i * 12.9898 + 4.1) * 43758.5453);
127
+ const r = size * (0.6 + jitter * 0.9);
128
+ const [cr, cg, cb] = (tints.length ? tints[i % tints.length] : light.rgb).map((c) => Math.round(c * 255));
129
+ const a = bokeh * (0.5 + 0.6 * light.level) * (1.1 - jitter * 0.4);
130
+ /**
131
+ * The disc's colour at a share of its strength.
132
+ *
133
+ * @param {number} k The share, 0–1.
134
+ * @returns {string} A CSS colour.
135
+ */
136
+ const tone = (k) => `rgb(${cr} ${cg} ${cb} / ${Math.min(1, a * k)})`;
137
+ const x = light.x * out.width;
138
+ const y = light.y * out.height;
139
+ const disc = ctx.createRadialGradient(x, y, 0, x, y, r);
140
+ disc.addColorStop(0, tone(0.4));
141
+ disc.addColorStop(0.75, tone(0.55));
142
+ disc.addColorStop(0.92, tone(0.95));
143
+ disc.addColorStop(1, tone(0));
144
+ ctx.fillStyle = disc;
145
+ ctx.beginPath();
146
+ ctx.arc(x, y, r, 0, Math.PI * 2);
147
+ ctx.fill();
148
+ });
149
+ return out;
150
+ }
151
+
152
+ /**
153
+ * A detached canvas.
154
+ *
155
+ * @param {number} width Pixels across.
156
+ * @param {number} height Pixels down.
157
+ * @returns {HTMLCanvasElement} The canvas.
158
+ */
159
+ function canvas(width, height) {
160
+ const c = document.createElement('canvas');
161
+ c.width = width;
162
+ c.height = height;
163
+ return c;
164
+ }
165
+
166
+ /**
167
+ * A canvas's 2D context, read back often.
168
+ *
169
+ * @param {HTMLCanvasElement} c The canvas.
170
+ * @returns {CanvasRenderingContext2D} Its context.
171
+ * @throws {Error} When the browser gives none.
172
+ */
173
+ function context(c) {
174
+ const ctx = c.getContext('2d', { willReadFrequently: true });
175
+ if (!ctx) throw new Error('No 2D canvas');
176
+ return ctx;
177
+ }
178
+
179
+ /**
180
+ * The fractional part of a number.
181
+ *
182
+ * @param {number} n The number.
183
+ * @returns {number} It, less its floor.
184
+ */
185
+ const fract = (n) => n - Math.floor(n);
@@ -0,0 +1,2 @@
1
+ /** The rain on glass fragment shader: procedural beads and runners, each a lens onto the photo, over its soft, fogged copy. */
2
+ export const FRAG: string;
@@ -0,0 +1,168 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/rain-on-glass/shader
3
+ *
4
+ * The rain on glass fragment shader. Drops are procedural, one per cell of
5
+ * a few grids: small beads that swell and dry where they sit, and larger
6
+ * runners that slide down tall cells along wandering paths, clearing a
7
+ * trail that beads up and fogs over behind them. Each drop is a little
8
+ * fisheye lens, showing the photo sharp and upside down; between them the
9
+ * photo is the soft, bokeh'd copy under a veil of condensation.
10
+ */
11
+
12
+ import { GLSL_NOISE } from '../glsl.js';
13
+ import { GLSL_COVER } from '../image.js';
14
+
15
+ /** The fragment shader. */
16
+ export const FRAG = `
17
+ precision highp float;
18
+ uniform vec2 uRes;
19
+ uniform float uTime;
20
+ uniform float uUnit;
21
+ uniform vec2 uCover;
22
+ uniform sampler2D uPhoto;
23
+ uniform sampler2D uSoft;
24
+ uniform sampler2D uWipe;
25
+ uniform float uRain;
26
+ uniform float uFog;
27
+ uniform float uClearBias;
28
+ uniform vec3 uTint;
29
+ uniform float uShow;
30
+ ${GLSL_NOISE}
31
+ ${GLSL_COVER}
32
+
33
+ // The drop that covers this pixel most so far: how much, where in it the
34
+ // pixel sits (centre 0, rim 1), and its centre in glass units.
35
+ float lensCover;
36
+ vec2 lensAt;
37
+ vec2 lensCentre;
38
+
39
+ vec3 hash3(vec2 p) { return vec3(hash(p), hash(p + 19.19), hash(p + 47.31)); }
40
+
41
+ // A box coordinate (0–1, y up) to the photo's (cropped to cover, y down).
42
+ vec2 photoUv(vec2 uv) {
43
+ vec2 c = coverUv(uv, uCover);
44
+ return vec2(c.x, 1.0 - c.y);
45
+ }
46
+
47
+ // Offers a drop to this pixel; it is kept when it covers the pixel more
48
+ // than the last. squash < 1 draws its top out into a tail.
49
+ void offer(vec2 q, vec2 centre, float radius, float squash) {
50
+ if (radius < 1e-4) return;
51
+ vec2 o = (q - centre) / radius;
52
+ o.y *= o.y > 0.0 ? squash : 1.0;
53
+ float cover = clamp((1.0 - length(o)) * radius * uUnit / 1.5, 0.0, 1.0);
54
+ if (cover > lensCover) {
55
+ lensCover = cover;
56
+ lensAt = o;
57
+ lensCentre = centre;
58
+ }
59
+ }
60
+
61
+ // Beads: at most one in each cell, which swells in, sits and dries away on
62
+ // a clock of its own. keep shrinks them where the glass was just cleared.
63
+ void beads(vec2 q, float cell, float seed, float amount, float keep) {
64
+ vec2 id = floor(q / cell);
65
+ vec3 h = hash3(id + seed);
66
+ if (h.x > amount) return;
67
+ float life = fract(uTime * (0.015 + 0.03 * h.y) + h.z);
68
+ float grow = smoothstep(0.0, 0.2, life) * smoothstep(1.0, 0.85, life);
69
+ float radius = (0.08 + 0.2 * h.y * h.y) * cell * grow * keep;
70
+ vec2 centre = (id + 0.3 + 0.4 * hash3(id + seed + 7.0).xy) * cell;
71
+ offer(q, centre, radius, 0.9);
72
+ }
73
+
74
+ // A runner's x in its cell at height y (both 0–1): a lazy wander, the same
75
+ // for the drop and for the trail it leaves.
76
+ float path(float y, vec3 h) {
77
+ return 0.5 + 0.17 * sin(y * (4.0 + 5.0 * h.y) + h.z * 6.283) + 0.06 * sin(y * 17.0 + h.x * 40.0);
78
+ }
79
+
80
+ // Runners: in each tall cell, maybe a drop that slides down in fits and
81
+ // starts, leaving a clear trail that beads up and fogs over again behind
82
+ // it. Returns how clear the trail leaves the glass here, 0–1.
83
+ float runners(vec2 q, vec2 cell, float seed, float amount) {
84
+ float column = floor(q.x / cell.x);
85
+ float lift = hash(vec2(column, seed)) * cell.y * 9.0;
86
+ vec2 s = q + vec2(0.0, lift);
87
+ vec2 id = floor(s / cell);
88
+ vec2 at = fract(s / cell);
89
+ vec3 h = hash3(id + seed);
90
+ if (h.x > amount) return 0.0;
91
+
92
+ float run = fract(uTime * (0.04 + 0.05 * h.y) + h.z);
93
+ float k = 6.283 * (2.0 + floor(h.z * 3.0));
94
+ float fall = run - 0.9 * sin(k * run) / k;
95
+ float y = mix(0.92, 0.08, fall);
96
+ float alive = smoothstep(0.0, 0.05, run) * smoothstep(1.0, 0.9, run);
97
+ float radius = (0.14 + 0.1 * h.y) * cell.x;
98
+ vec2 drop = (id + vec2(path(y, h), y)) * cell;
99
+ offer(q, drop - vec2(0.0, lift), radius * alive, 0.6);
100
+
101
+ // The trail: as wide as most of the drop, fogging over as it ages and
102
+ // gone by the top of the cell.
103
+ float above = (at.y - y) * cell.y;
104
+ float across = abs(at.x - path(at.y, h)) * cell.x;
105
+ float trail = smoothstep(radius * 1.05, radius * 0.35, across) * smoothstep(-0.2 * radius, 0.8 * radius, above) * exp(-max(above, 0.0) / 0.28) * smoothstep(1.0, 0.7, at.y) * alive;
106
+
107
+ // The beads it leaves behind, one in each short stretch of the trail.
108
+ float gap = radius * 1.6;
109
+ float row = floor(s.y / gap);
110
+ float by = (row + 0.5) * gap / cell.y - id.y;
111
+ float hb = hash(vec2(row, id.x + seed * 3.7));
112
+ float since = (by - y) * cell.y;
113
+ float size = radius * (0.2 + 0.22 * hb) * step(hb, 0.7) * smoothstep(radius, radius * 2.5, since) * exp(-since / 0.35) * step(by, 0.98) * alive;
114
+ offer(q, vec2((id.x + path(by, h) + (hb - 0.5) * 0.08) * cell.x, (row + 0.5) * gap - lift), size, 1.0);
115
+ return trail;
116
+ }
117
+
118
+ void main() {
119
+ if (uShow <= 0.0) {
120
+ gl_FragColor = vec4(0.0);
121
+ return;
122
+ }
123
+ vec2 uv = gl_FragCoord.xy / uRes;
124
+ vec2 q = gl_FragCoord.xy / uUnit;
125
+ lensCover = 0.0;
126
+ lensAt = vec2(0.0);
127
+ lensCentre = vec2(0.0);
128
+
129
+ float trail = max(runners(q, vec2(0.13, 0.75), 1.0, uRain * 0.7), runners(q + vec2(0.04, 0.0), vec2(0.08, 0.45), 2.0, uRain * 0.5));
130
+ float wipe = texture2D(uWipe, uv).r;
131
+ float clear = clamp(max(trail, wipe), 0.0, 1.0);
132
+ float keep = 1.0 - clear;
133
+ beads(q, 0.075, 5.0, 0.05 + 0.3 * uRain, keep);
134
+ beads(q, 0.034, 3.0, 0.15 + 0.6 * uRain, keep);
135
+ beads(q, 0.014, 4.0, 0.3 + 0.55 * uRain, keep);
136
+
137
+ // Behind the glass: the soft copy, sharper where the glass is clear,
138
+ // under a mottled veil of condensation in the tint.
139
+ vec2 p = photoUv(uv);
140
+ vec3 soft = texture2D(uSoft, p).rgb;
141
+ vec3 sharp = texture2D(uPhoto, p, uClearBias).rgb;
142
+ vec3 col = mix(soft, sharp, clear * 0.8);
143
+ col = max(mix(vec3(dot(col, vec3(0.2126, 0.7152, 0.0722))), col, 1.25), 0.0);
144
+ float haze = uFog * keep * (0.75 + 0.5 * noise(q * 12.0));
145
+ col = mix(col, 1.0 - (1.0 - col) * (1.0 - uTint * 0.3), haze);
146
+ col = mix(col, vec3(dot(col, vec3(0.2126, 0.7152, 0.0722))), haze * 0.2);
147
+
148
+ // A drop: a fisheye lens, the photo sharp and upside down in it, dark
149
+ // at the rim, lit at the top and focusing a crescent at the bottom.
150
+ float rr = dot(lensAt, lensAt);
151
+ vec2 centre = lensCentre * uUnit / uRes;
152
+ vec2 look = centre - lensAt * (1.0 + 0.7 * rr) * vec2(uRes.y / uRes.x, 1.0) * 0.14;
153
+ vec3 seen = texture2D(uPhoto, photoUv(look)).rgb * 1.3 + 0.02;
154
+ float r = sqrt(rr);
155
+ float rim = smoothstep(0.72, 1.0, r);
156
+ seen = mix(seen, seen * 0.3, rim * 0.8);
157
+ float glint = smoothstep(0.24, 0.0, length(lensAt - vec2(-0.3, 0.45)));
158
+ float crescent = smoothstep(0.5, 0.8, r) * smoothstep(0.98, 0.8, r) * smoothstep(0.1, -0.8, lensAt.y);
159
+ seen += glint * glint * 0.6 + crescent * (0.06 + soft * 0.6 + uTint * 0.08);
160
+ col = mix(col, seen, lensCover);
161
+
162
+ // A soft vignette, and a little dither so the gradients do not band.
163
+ vec2 v = uv - 0.5;
164
+ col *= 1.0 - 0.7 * dot(v, v);
165
+ col += (hash(gl_FragCoord.xy + fract(uTime)) - 0.5) / 128.0;
166
+ gl_FragColor = vec4(clamp(col, 0.0, 1.0) * uShow, uShow);
167
+ }
168
+ `;
@@ -0,0 +1,16 @@
1
+ /** A coarse grid of where the pointer has wiped the fog off the glass. */
2
+ export interface Wipe {
3
+ /** Cells across. */
4
+ width: number;
5
+ /** Cells up. */
6
+ height: number;
7
+ /** Wipes a soft spot at x, y (0–1, y up); `radius` a share of the height. */
8
+ stamp(x: number, y: number, radius: number): void;
9
+ /** Lets the fog return for `dt` seconds; true when `bytes` changed (upload them), false while the glass stays all fogged. */
10
+ settle(dt: number): boolean;
11
+ /** Clearness per cell, 0–255, bottom row first: for a LUMINANCE texture. */
12
+ bytes: Uint8Array;
13
+ }
14
+
15
+ /** Makes a wipe grid of `width` × `height` cells, all fogged. */
16
+ export function createWipe(width: number, height: number): Wipe;