@vfx-js/effects 1.4.4 → 1.5.0

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 (511) hide show
  1. package/README.md +4 -0
  2. package/THIRD_PARTY_NOTICES.md +61 -0
  3. package/lib/cjs/_bloom-common.d.ts +31 -0
  4. package/lib/cjs/_bloom-common.js +101 -0
  5. package/lib/cjs/_bloom-common.js.map +1 -0
  6. package/lib/cjs/_bubble-fill.d.ts +126 -0
  7. package/lib/cjs/_bubble-fill.js +1044 -0
  8. package/lib/cjs/_bubble-fill.js.map +1 -0
  9. package/lib/cjs/_bubble-kernel-source.d.ts +2 -0
  10. package/lib/cjs/_bubble-kernel-source.js +8 -0
  11. package/lib/cjs/_bubble-kernel-source.js.map +1 -0
  12. package/lib/cjs/_holo/data/kernels.d.ts +4 -0
  13. package/lib/cjs/_holo/data/kernels.js +10 -0
  14. package/lib/cjs/_holo/data/kernels.js.map +1 -0
  15. package/lib/cjs/_holo/data/looks.d.ts +2 -0
  16. package/lib/cjs/_holo/data/looks.js +595 -0
  17. package/lib/cjs/_holo/data/looks.js.map +1 -0
  18. package/lib/cjs/_holo/data/ltc.d.ts +2 -0
  19. package/lib/cjs/_holo/data/ltc.js +11 -0
  20. package/lib/cjs/_holo/data/ltc.js.map +1 -0
  21. package/lib/cjs/_holo/data/material-presets.d.ts +3 -0
  22. package/lib/cjs/_holo/data/material-presets.js +75 -0
  23. package/lib/cjs/_holo/data/material-presets.js.map +1 -0
  24. package/lib/cjs/_holo/data/not-ported.d.ts +5 -0
  25. package/lib/cjs/_holo/data/not-ported.js +190 -0
  26. package/lib/cjs/_holo/data/not-ported.js.map +1 -0
  27. package/lib/cjs/_holo/data/params.d.ts +4 -0
  28. package/lib/cjs/_holo/data/params.js +1641 -0
  29. package/lib/cjs/_holo/data/params.js.map +1 -0
  30. package/lib/cjs/_holo/data/starting-points.d.ts +199 -0
  31. package/lib/cjs/_holo/data/starting-points.js +193 -0
  32. package/lib/cjs/_holo/data/starting-points.js.map +1 -0
  33. package/lib/cjs/_holo/data/studio-presets.d.ts +2 -0
  34. package/lib/cjs/_holo/data/studio-presets.js +177 -0
  35. package/lib/cjs/_holo/data/studio-presets.js.map +1 -0
  36. package/lib/cjs/_holo/emboss-assets.d.ts +55 -0
  37. package/lib/cjs/_holo/emboss-assets.js +155 -0
  38. package/lib/cjs/_holo/emboss-assets.js.map +1 -0
  39. package/lib/cjs/_holo/glare.d.ts +15 -0
  40. package/lib/cjs/_holo/glare.js +125 -0
  41. package/lib/cjs/_holo/glare.js.map +1 -0
  42. package/lib/cjs/_holo/glsl/coat.d.ts +2 -0
  43. package/lib/cjs/_holo/glsl/coat.js +32 -0
  44. package/lib/cjs/_holo/glsl/coat.js.map +1 -0
  45. package/lib/cjs/_holo/glsl/common.d.ts +2 -0
  46. package/lib/cjs/_holo/glsl/common.js +141 -0
  47. package/lib/cjs/_holo/glsl/common.js.map +1 -0
  48. package/lib/cjs/_holo/glsl/emboss.d.ts +2 -0
  49. package/lib/cjs/_holo/glsl/emboss.js +246 -0
  50. package/lib/cjs/_holo/glsl/emboss.js.map +1 -0
  51. package/lib/cjs/_holo/glsl/env.d.ts +2 -0
  52. package/lib/cjs/_holo/glsl/env.js +52 -0
  53. package/lib/cjs/_holo/glsl/env.js.map +1 -0
  54. package/lib/cjs/_holo/glsl/glare-down.d.ts +2 -0
  55. package/lib/cjs/_holo/glsl/glare-down.js +65 -0
  56. package/lib/cjs/_holo/glsl/glare-down.js.map +1 -0
  57. package/lib/cjs/_holo/glsl/glare-up.d.ts +2 -0
  58. package/lib/cjs/_holo/glsl/glare-up.js +18 -0
  59. package/lib/cjs/_holo/glsl/glare-up.js.map +1 -0
  60. package/lib/cjs/_holo/glsl/glitter.d.ts +2 -0
  61. package/lib/cjs/_holo/glsl/glitter.js +325 -0
  62. package/lib/cjs/_holo/glsl/glitter.js.map +1 -0
  63. package/lib/cjs/_holo/glsl/grating.d.ts +2 -0
  64. package/lib/cjs/_holo/glsl/grating.js +240 -0
  65. package/lib/cjs/_holo/glsl/grating.js.map +1 -0
  66. package/lib/cjs/_holo/glsl/holo-emboss.d.ts +2 -0
  67. package/lib/cjs/_holo/glsl/holo-emboss.js +296 -0
  68. package/lib/cjs/_holo/glsl/holo-emboss.js.map +1 -0
  69. package/lib/cjs/_holo/glsl/holo-nebula.d.ts +2 -0
  70. package/lib/cjs/_holo/glsl/holo-nebula.js +186 -0
  71. package/lib/cjs/_holo/glsl/holo-nebula.js.map +1 -0
  72. package/lib/cjs/_holo/glsl/holo-prism.d.ts +2 -0
  73. package/lib/cjs/_holo/glsl/holo-prism.js +183 -0
  74. package/lib/cjs/_holo/glsl/holo-prism.js.map +1 -0
  75. package/lib/cjs/_holo/glsl/holo-shatter.d.ts +2 -0
  76. package/lib/cjs/_holo/glsl/holo-shatter.js +328 -0
  77. package/lib/cjs/_holo/glsl/holo-shatter.js.map +1 -0
  78. package/lib/cjs/_holo/glsl/holo-sparkle.d.ts +2 -0
  79. package/lib/cjs/_holo/glsl/holo-sparkle.js +235 -0
  80. package/lib/cjs/_holo/glsl/holo-sparkle.js.map +1 -0
  81. package/lib/cjs/_holo/glsl/holo-vortex.d.ts +2 -0
  82. package/lib/cjs/_holo/glsl/holo-vortex.js +201 -0
  83. package/lib/cjs/_holo/glsl/holo-vortex.js.map +1 -0
  84. package/lib/cjs/_holo/glsl/ibl.d.ts +2 -0
  85. package/lib/cjs/_holo/glsl/ibl.js +79 -0
  86. package/lib/cjs/_holo/glsl/ibl.js.map +1 -0
  87. package/lib/cjs/_holo/glsl/lights.d.ts +2 -0
  88. package/lib/cjs/_holo/glsl/lights.js +125 -0
  89. package/lib/cjs/_holo/glsl/lights.js.map +1 -0
  90. package/lib/cjs/_holo/glsl/ltc.d.ts +2 -0
  91. package/lib/cjs/_holo/glsl/ltc.js +66 -0
  92. package/lib/cjs/_holo/glsl/ltc.js.map +1 -0
  93. package/lib/cjs/_holo/glsl/output.d.ts +2 -0
  94. package/lib/cjs/_holo/glsl/output.js +113 -0
  95. package/lib/cjs/_holo/glsl/output.js.map +1 -0
  96. package/lib/cjs/_holo/glsl/paper.d.ts +2 -0
  97. package/lib/cjs/_holo/glsl/paper.js +165 -0
  98. package/lib/cjs/_holo/glsl/paper.js.map +1 -0
  99. package/lib/cjs/_holo/glsl/relief.d.ts +2 -0
  100. package/lib/cjs/_holo/glsl/relief.js +103 -0
  101. package/lib/cjs/_holo/glsl/relief.js.map +1 -0
  102. package/lib/cjs/_holo/glsl/sparkle.d.ts +2 -0
  103. package/lib/cjs/_holo/glsl/sparkle.js +279 -0
  104. package/lib/cjs/_holo/glsl/sparkle.js.map +1 -0
  105. package/lib/cjs/_holo/glsl/spectral.d.ts +2 -0
  106. package/lib/cjs/_holo/glsl/spectral.js +60 -0
  107. package/lib/cjs/_holo/glsl/spectral.js.map +1 -0
  108. package/lib/cjs/_holo/glsl/wear.d.ts +2 -0
  109. package/lib/cjs/_holo/glsl/wear.js +107 -0
  110. package/lib/cjs/_holo/glsl/wear.js.map +1 -0
  111. package/lib/cjs/_holo/ltc.d.ts +9 -0
  112. package/lib/cjs/_holo/ltc.js +56 -0
  113. package/lib/cjs/_holo/ltc.js.map +1 -0
  114. package/lib/cjs/_holo/materials/build.d.ts +32 -0
  115. package/lib/cjs/_holo/materials/build.js +36 -0
  116. package/lib/cjs/_holo/materials/build.js.map +1 -0
  117. package/lib/cjs/_holo/materials/emboss.d.ts +22 -0
  118. package/lib/cjs/_holo/materials/emboss.js +205 -0
  119. package/lib/cjs/_holo/materials/emboss.js.map +1 -0
  120. package/lib/cjs/_holo/materials/nebula.d.ts +4 -0
  121. package/lib/cjs/_holo/materials/nebula.js +105 -0
  122. package/lib/cjs/_holo/materials/nebula.js.map +1 -0
  123. package/lib/cjs/_holo/materials/prism.d.ts +2 -0
  124. package/lib/cjs/_holo/materials/prism.js +69 -0
  125. package/lib/cjs/_holo/materials/prism.js.map +1 -0
  126. package/lib/cjs/_holo/materials/registry.d.ts +11 -0
  127. package/lib/cjs/_holo/materials/registry.js +88 -0
  128. package/lib/cjs/_holo/materials/registry.js.map +1 -0
  129. package/lib/cjs/_holo/materials/shatter.d.ts +2 -0
  130. package/lib/cjs/_holo/materials/shatter.js +80 -0
  131. package/lib/cjs/_holo/materials/shatter.js.map +1 -0
  132. package/lib/cjs/_holo/materials/sparkle.d.ts +2 -0
  133. package/lib/cjs/_holo/materials/sparkle.js +108 -0
  134. package/lib/cjs/_holo/materials/sparkle.js.map +1 -0
  135. package/lib/cjs/_holo/materials/vortex.d.ts +2 -0
  136. package/lib/cjs/_holo/materials/vortex.js +85 -0
  137. package/lib/cjs/_holo/materials/vortex.js.map +1 -0
  138. package/lib/cjs/_holo/physics/emboss.d.ts +39 -0
  139. package/lib/cjs/_holo/physics/emboss.js +95 -0
  140. package/lib/cjs/_holo/physics/emboss.js.map +1 -0
  141. package/lib/cjs/_holo/physics/geometry.d.ts +109 -0
  142. package/lib/cjs/_holo/physics/geometry.js +252 -0
  143. package/lib/cjs/_holo/physics/geometry.js.map +1 -0
  144. package/lib/cjs/_holo/physics/grating.d.ts +86 -0
  145. package/lib/cjs/_holo/physics/grating.js +328 -0
  146. package/lib/cjs/_holo/physics/grating.js.map +1 -0
  147. package/lib/cjs/_holo/physics/sparkle.d.ts +143 -0
  148. package/lib/cjs/_holo/physics/sparkle.js +388 -0
  149. package/lib/cjs/_holo/physics/sparkle.js.map +1 -0
  150. package/lib/cjs/_holo/scene/env.d.ts +17 -0
  151. package/lib/cjs/_holo/scene/env.js +151 -0
  152. package/lib/cjs/_holo/scene/env.js.map +1 -0
  153. package/lib/cjs/_holo/scene/lights.d.ts +13 -0
  154. package/lib/cjs/_holo/scene/lights.js +211 -0
  155. package/lib/cjs/_holo/scene/lights.js.map +1 -0
  156. package/lib/cjs/_holo/scene/logo.d.ts +87 -0
  157. package/lib/cjs/_holo/scene/logo.js +655 -0
  158. package/lib/cjs/_holo/scene/logo.js.map +1 -0
  159. package/lib/cjs/_holo/scene/paper-maps.d.ts +26 -0
  160. package/lib/cjs/_holo/scene/paper-maps.js +391 -0
  161. package/lib/cjs/_holo/scene/paper-maps.js.map +1 -0
  162. package/lib/cjs/_holo/scene/pose.d.ts +31 -0
  163. package/lib/cjs/_holo/scene/pose.js +77 -0
  164. package/lib/cjs/_holo/scene/pose.js.map +1 -0
  165. package/lib/cjs/_holo/scene/relief.d.ts +24 -0
  166. package/lib/cjs/_holo/scene/relief.js +148 -0
  167. package/lib/cjs/_holo/scene/relief.js.map +1 -0
  168. package/lib/cjs/_holo/settings-types.d.ts +20 -0
  169. package/lib/cjs/_holo/settings-types.js +3 -0
  170. package/lib/cjs/_holo/settings-types.js.map +1 -0
  171. package/lib/cjs/_holo/settings.d.ts +83 -0
  172. package/lib/cjs/_holo/settings.js +375 -0
  173. package/lib/cjs/_holo/settings.js.map +1 -0
  174. package/lib/cjs/_holo/shader.d.ts +23 -0
  175. package/lib/cjs/_holo/shader.js +219 -0
  176. package/lib/cjs/_holo/shader.js.map +1 -0
  177. package/lib/cjs/_holo/spectral/atlas.d.ts +36 -0
  178. package/lib/cjs/_holo/spectral/atlas.js +114 -0
  179. package/lib/cjs/_holo/spectral/atlas.js.map +1 -0
  180. package/lib/cjs/_holo/spectral/cmf.d.ts +67 -0
  181. package/lib/cjs/_holo/spectral/cmf.js +244 -0
  182. package/lib/cjs/_holo/spectral/cmf.js.map +1 -0
  183. package/lib/cjs/_holo/spectral/float24.d.ts +8 -0
  184. package/lib/cjs/_holo/spectral/float24.js +70 -0
  185. package/lib/cjs/_holo/spectral/float24.js.map +1 -0
  186. package/lib/cjs/_holo/spectral/spd.d.ts +22 -0
  187. package/lib/cjs/_holo/spectral/spd.js +139 -0
  188. package/lib/cjs/_holo/spectral/spd.js.map +1 -0
  189. package/lib/cjs/_holo/spectral/tables.d.ts +62 -0
  190. package/lib/cjs/_holo/spectral/tables.js +386 -0
  191. package/lib/cjs/_holo/spectral/tables.js.map +1 -0
  192. package/lib/cjs/_holo/tier.d.ts +4 -0
  193. package/lib/cjs/_holo/tier.js +110 -0
  194. package/lib/cjs/_holo/tier.js.map +1 -0
  195. package/lib/cjs/_holo/types.d.ts +148 -0
  196. package/lib/cjs/_holo/types.js +14 -0
  197. package/lib/cjs/_holo/types.js.map +1 -0
  198. package/lib/cjs/_kernel-worker.d.ts +22 -0
  199. package/lib/cjs/_kernel-worker.js +174 -0
  200. package/lib/cjs/_kernel-worker.js.map +1 -0
  201. package/lib/cjs/_orbit-camera.d.ts +175 -0
  202. package/lib/cjs/_orbit-camera.js +366 -0
  203. package/lib/cjs/_orbit-camera.js.map +1 -0
  204. package/lib/cjs/_pad.d.ts +2 -0
  205. package/lib/cjs/_pad.js +11 -0
  206. package/lib/cjs/_pad.js.map +1 -1
  207. package/lib/cjs/_scene3d.d.ts +101 -0
  208. package/lib/cjs/_scene3d.js +794 -0
  209. package/lib/cjs/_scene3d.js.map +1 -0
  210. package/lib/cjs/_shape-kernel-source.d.ts +2 -0
  211. package/lib/cjs/_shape-kernel-source.js +8 -0
  212. package/lib/cjs/_shape-kernel-source.js.map +1 -0
  213. package/lib/cjs/_shape-worker.d.ts +8 -0
  214. package/lib/cjs/_shape-worker.js +27 -0
  215. package/lib/cjs/_shape-worker.js.map +1 -0
  216. package/lib/cjs/_silhouette.d.ts +88 -0
  217. package/lib/cjs/_silhouette.js +441 -0
  218. package/lib/cjs/_silhouette.js.map +1 -0
  219. package/lib/cjs/_simplex3.d.ts +2 -0
  220. package/lib/cjs/_simplex3.js +160 -0
  221. package/lib/cjs/_simplex3.js.map +1 -0
  222. package/lib/cjs/_source.d.ts +80 -0
  223. package/lib/cjs/_source.js +192 -0
  224. package/lib/cjs/_source.js.map +1 -0
  225. package/lib/cjs/_sphere-foam.d.ts +34 -0
  226. package/lib/cjs/_sphere-foam.js +135 -0
  227. package/lib/cjs/_sphere-foam.js.map +1 -0
  228. package/lib/cjs/_thin-film.d.ts +26 -0
  229. package/lib/cjs/_thin-film.js +146 -0
  230. package/lib/cjs/_thin-film.js.map +1 -0
  231. package/lib/cjs/bloom.js +40 -106
  232. package/lib/cjs/bloom.js.map +1 -1
  233. package/lib/cjs/bubble.d.ts +284 -0
  234. package/lib/cjs/bubble.js +1820 -0
  235. package/lib/cjs/bubble.js.map +1 -0
  236. package/lib/cjs/glass.d.ts +254 -0
  237. package/lib/cjs/glass.js +981 -0
  238. package/lib/cjs/glass.js.map +1 -0
  239. package/lib/cjs/holo-static.d.ts +3 -0
  240. package/lib/cjs/holo-static.js +29 -0
  241. package/lib/cjs/holo-static.js.map +1 -0
  242. package/lib/cjs/holo.d.ts +401 -0
  243. package/lib/cjs/holo.js +1613 -0
  244. package/lib/cjs/holo.js.map +1 -0
  245. package/lib/cjs/index.d.ts +18 -9
  246. package/lib/cjs/index.js +12 -1
  247. package/lib/cjs/index.js.map +1 -1
  248. package/lib/cjs/jpeg-glitch.js +32 -29
  249. package/lib/cjs/jpeg-glitch.js.map +1 -1
  250. package/lib/cjs/matrix.d.ts +1 -1
  251. package/lib/cjs/package.json +20 -5
  252. package/lib/cjs/swirl-particle.d.ts +257 -0
  253. package/lib/cjs/swirl-particle.js +2346 -0
  254. package/lib/cjs/swirl-particle.js.map +1 -0
  255. package/lib/cjs/voronoi.js +2 -75
  256. package/lib/cjs/voronoi.js.map +1 -1
  257. package/lib/esm/_bloom-common.d.ts +31 -0
  258. package/lib/esm/_bloom-common.js +95 -0
  259. package/lib/esm/_bloom-common.js.map +1 -0
  260. package/lib/esm/_bubble-fill.d.ts +126 -0
  261. package/lib/esm/_bubble-fill.js +1039 -0
  262. package/lib/esm/_bubble-fill.js.map +1 -0
  263. package/lib/esm/_bubble-kernel-source.d.ts +2 -0
  264. package/lib/esm/_bubble-kernel-source.js +5 -0
  265. package/lib/esm/_bubble-kernel-source.js.map +1 -0
  266. package/lib/esm/_holo/data/kernels.d.ts +4 -0
  267. package/lib/esm/_holo/data/kernels.js +7 -0
  268. package/lib/esm/_holo/data/kernels.js.map +1 -0
  269. package/lib/esm/_holo/data/looks.d.ts +2 -0
  270. package/lib/esm/_holo/data/looks.js +592 -0
  271. package/lib/esm/_holo/data/looks.js.map +1 -0
  272. package/lib/esm/_holo/data/ltc.d.ts +2 -0
  273. package/lib/esm/_holo/data/ltc.js +8 -0
  274. package/lib/esm/_holo/data/ltc.js.map +1 -0
  275. package/lib/esm/_holo/data/material-presets.d.ts +3 -0
  276. package/lib/esm/_holo/data/material-presets.js +72 -0
  277. package/lib/esm/_holo/data/material-presets.js.map +1 -0
  278. package/lib/esm/_holo/data/not-ported.d.ts +5 -0
  279. package/lib/esm/_holo/data/not-ported.js +187 -0
  280. package/lib/esm/_holo/data/not-ported.js.map +1 -0
  281. package/lib/esm/_holo/data/params.d.ts +4 -0
  282. package/lib/esm/_holo/data/params.js +1638 -0
  283. package/lib/esm/_holo/data/params.js.map +1 -0
  284. package/lib/esm/_holo/data/starting-points.d.ts +199 -0
  285. package/lib/esm/_holo/data/starting-points.js +190 -0
  286. package/lib/esm/_holo/data/starting-points.js.map +1 -0
  287. package/lib/esm/_holo/data/studio-presets.d.ts +2 -0
  288. package/lib/esm/_holo/data/studio-presets.js +174 -0
  289. package/lib/esm/_holo/data/studio-presets.js.map +1 -0
  290. package/lib/esm/_holo/emboss-assets.d.ts +55 -0
  291. package/lib/esm/_holo/emboss-assets.js +150 -0
  292. package/lib/esm/_holo/emboss-assets.js.map +1 -0
  293. package/lib/esm/_holo/glare.d.ts +15 -0
  294. package/lib/esm/_holo/glare.js +117 -0
  295. package/lib/esm/_holo/glare.js.map +1 -0
  296. package/lib/esm/_holo/glsl/coat.d.ts +2 -0
  297. package/lib/esm/_holo/glsl/coat.js +30 -0
  298. package/lib/esm/_holo/glsl/coat.js.map +1 -0
  299. package/lib/esm/_holo/glsl/common.d.ts +2 -0
  300. package/lib/esm/_holo/glsl/common.js +139 -0
  301. package/lib/esm/_holo/glsl/common.js.map +1 -0
  302. package/lib/esm/_holo/glsl/emboss.d.ts +2 -0
  303. package/lib/esm/_holo/glsl/emboss.js +244 -0
  304. package/lib/esm/_holo/glsl/emboss.js.map +1 -0
  305. package/lib/esm/_holo/glsl/env.d.ts +2 -0
  306. package/lib/esm/_holo/glsl/env.js +50 -0
  307. package/lib/esm/_holo/glsl/env.js.map +1 -0
  308. package/lib/esm/_holo/glsl/glare-down.d.ts +2 -0
  309. package/lib/esm/_holo/glsl/glare-down.js +63 -0
  310. package/lib/esm/_holo/glsl/glare-down.js.map +1 -0
  311. package/lib/esm/_holo/glsl/glare-up.d.ts +2 -0
  312. package/lib/esm/_holo/glsl/glare-up.js +16 -0
  313. package/lib/esm/_holo/glsl/glare-up.js.map +1 -0
  314. package/lib/esm/_holo/glsl/glitter.d.ts +2 -0
  315. package/lib/esm/_holo/glsl/glitter.js +323 -0
  316. package/lib/esm/_holo/glsl/glitter.js.map +1 -0
  317. package/lib/esm/_holo/glsl/grating.d.ts +2 -0
  318. package/lib/esm/_holo/glsl/grating.js +238 -0
  319. package/lib/esm/_holo/glsl/grating.js.map +1 -0
  320. package/lib/esm/_holo/glsl/holo-emboss.d.ts +2 -0
  321. package/lib/esm/_holo/glsl/holo-emboss.js +294 -0
  322. package/lib/esm/_holo/glsl/holo-emboss.js.map +1 -0
  323. package/lib/esm/_holo/glsl/holo-nebula.d.ts +2 -0
  324. package/lib/esm/_holo/glsl/holo-nebula.js +184 -0
  325. package/lib/esm/_holo/glsl/holo-nebula.js.map +1 -0
  326. package/lib/esm/_holo/glsl/holo-prism.d.ts +2 -0
  327. package/lib/esm/_holo/glsl/holo-prism.js +181 -0
  328. package/lib/esm/_holo/glsl/holo-prism.js.map +1 -0
  329. package/lib/esm/_holo/glsl/holo-shatter.d.ts +2 -0
  330. package/lib/esm/_holo/glsl/holo-shatter.js +326 -0
  331. package/lib/esm/_holo/glsl/holo-shatter.js.map +1 -0
  332. package/lib/esm/_holo/glsl/holo-sparkle.d.ts +2 -0
  333. package/lib/esm/_holo/glsl/holo-sparkle.js +233 -0
  334. package/lib/esm/_holo/glsl/holo-sparkle.js.map +1 -0
  335. package/lib/esm/_holo/glsl/holo-vortex.d.ts +2 -0
  336. package/lib/esm/_holo/glsl/holo-vortex.js +199 -0
  337. package/lib/esm/_holo/glsl/holo-vortex.js.map +1 -0
  338. package/lib/esm/_holo/glsl/ibl.d.ts +2 -0
  339. package/lib/esm/_holo/glsl/ibl.js +77 -0
  340. package/lib/esm/_holo/glsl/ibl.js.map +1 -0
  341. package/lib/esm/_holo/glsl/lights.d.ts +2 -0
  342. package/lib/esm/_holo/glsl/lights.js +123 -0
  343. package/lib/esm/_holo/glsl/lights.js.map +1 -0
  344. package/lib/esm/_holo/glsl/ltc.d.ts +2 -0
  345. package/lib/esm/_holo/glsl/ltc.js +64 -0
  346. package/lib/esm/_holo/glsl/ltc.js.map +1 -0
  347. package/lib/esm/_holo/glsl/output.d.ts +2 -0
  348. package/lib/esm/_holo/glsl/output.js +111 -0
  349. package/lib/esm/_holo/glsl/output.js.map +1 -0
  350. package/lib/esm/_holo/glsl/paper.d.ts +2 -0
  351. package/lib/esm/_holo/glsl/paper.js +163 -0
  352. package/lib/esm/_holo/glsl/paper.js.map +1 -0
  353. package/lib/esm/_holo/glsl/relief.d.ts +2 -0
  354. package/lib/esm/_holo/glsl/relief.js +101 -0
  355. package/lib/esm/_holo/glsl/relief.js.map +1 -0
  356. package/lib/esm/_holo/glsl/sparkle.d.ts +2 -0
  357. package/lib/esm/_holo/glsl/sparkle.js +277 -0
  358. package/lib/esm/_holo/glsl/sparkle.js.map +1 -0
  359. package/lib/esm/_holo/glsl/spectral.d.ts +2 -0
  360. package/lib/esm/_holo/glsl/spectral.js +58 -0
  361. package/lib/esm/_holo/glsl/spectral.js.map +1 -0
  362. package/lib/esm/_holo/glsl/wear.d.ts +2 -0
  363. package/lib/esm/_holo/glsl/wear.js +105 -0
  364. package/lib/esm/_holo/glsl/wear.js.map +1 -0
  365. package/lib/esm/_holo/ltc.d.ts +9 -0
  366. package/lib/esm/_holo/ltc.js +52 -0
  367. package/lib/esm/_holo/ltc.js.map +1 -0
  368. package/lib/esm/_holo/materials/build.d.ts +32 -0
  369. package/lib/esm/_holo/materials/build.js +30 -0
  370. package/lib/esm/_holo/materials/build.js.map +1 -0
  371. package/lib/esm/_holo/materials/emboss.d.ts +22 -0
  372. package/lib/esm/_holo/materials/emboss.js +198 -0
  373. package/lib/esm/_holo/materials/emboss.js.map +1 -0
  374. package/lib/esm/_holo/materials/nebula.d.ts +4 -0
  375. package/lib/esm/_holo/materials/nebula.js +98 -0
  376. package/lib/esm/_holo/materials/nebula.js.map +1 -0
  377. package/lib/esm/_holo/materials/prism.d.ts +2 -0
  378. package/lib/esm/_holo/materials/prism.js +63 -0
  379. package/lib/esm/_holo/materials/prism.js.map +1 -0
  380. package/lib/esm/_holo/materials/registry.d.ts +11 -0
  381. package/lib/esm/_holo/materials/registry.js +48 -0
  382. package/lib/esm/_holo/materials/registry.js.map +1 -0
  383. package/lib/esm/_holo/materials/shatter.d.ts +2 -0
  384. package/lib/esm/_holo/materials/shatter.js +74 -0
  385. package/lib/esm/_holo/materials/shatter.js.map +1 -0
  386. package/lib/esm/_holo/materials/sparkle.d.ts +2 -0
  387. package/lib/esm/_holo/materials/sparkle.js +102 -0
  388. package/lib/esm/_holo/materials/sparkle.js.map +1 -0
  389. package/lib/esm/_holo/materials/vortex.d.ts +2 -0
  390. package/lib/esm/_holo/materials/vortex.js +79 -0
  391. package/lib/esm/_holo/materials/vortex.js.map +1 -0
  392. package/lib/esm/_holo/physics/emboss.d.ts +39 -0
  393. package/lib/esm/_holo/physics/emboss.js +89 -0
  394. package/lib/esm/_holo/physics/emboss.js.map +1 -0
  395. package/lib/esm/_holo/physics/geometry.d.ts +109 -0
  396. package/lib/esm/_holo/physics/geometry.js +228 -0
  397. package/lib/esm/_holo/physics/geometry.js.map +1 -0
  398. package/lib/esm/_holo/physics/grating.d.ts +86 -0
  399. package/lib/esm/_holo/physics/grating.js +313 -0
  400. package/lib/esm/_holo/physics/grating.js.map +1 -0
  401. package/lib/esm/_holo/physics/sparkle.d.ts +143 -0
  402. package/lib/esm/_holo/physics/sparkle.js +360 -0
  403. package/lib/esm/_holo/physics/sparkle.js.map +1 -0
  404. package/lib/esm/_holo/scene/env.d.ts +17 -0
  405. package/lib/esm/_holo/scene/env.js +141 -0
  406. package/lib/esm/_holo/scene/env.js.map +1 -0
  407. package/lib/esm/_holo/scene/lights.d.ts +13 -0
  408. package/lib/esm/_holo/scene/lights.js +204 -0
  409. package/lib/esm/_holo/scene/lights.js.map +1 -0
  410. package/lib/esm/_holo/scene/logo.d.ts +87 -0
  411. package/lib/esm/_holo/scene/logo.js +645 -0
  412. package/lib/esm/_holo/scene/logo.js.map +1 -0
  413. package/lib/esm/_holo/scene/paper-maps.d.ts +26 -0
  414. package/lib/esm/_holo/scene/paper-maps.js +387 -0
  415. package/lib/esm/_holo/scene/paper-maps.js.map +1 -0
  416. package/lib/esm/_holo/scene/pose.d.ts +31 -0
  417. package/lib/esm/_holo/scene/pose.js +71 -0
  418. package/lib/esm/_holo/scene/pose.js.map +1 -0
  419. package/lib/esm/_holo/scene/relief.d.ts +24 -0
  420. package/lib/esm/_holo/scene/relief.js +142 -0
  421. package/lib/esm/_holo/scene/relief.js.map +1 -0
  422. package/lib/esm/_holo/settings-types.d.ts +20 -0
  423. package/lib/esm/_holo/settings-types.js +2 -0
  424. package/lib/esm/_holo/settings-types.js.map +1 -0
  425. package/lib/esm/_holo/settings.d.ts +83 -0
  426. package/lib/esm/_holo/settings.js +358 -0
  427. package/lib/esm/_holo/settings.js.map +1 -0
  428. package/lib/esm/_holo/shader.d.ts +23 -0
  429. package/lib/esm/_holo/shader.js +211 -0
  430. package/lib/esm/_holo/shader.js.map +1 -0
  431. package/lib/esm/_holo/spectral/atlas.d.ts +36 -0
  432. package/lib/esm/_holo/spectral/atlas.js +110 -0
  433. package/lib/esm/_holo/spectral/atlas.js.map +1 -0
  434. package/lib/esm/_holo/spectral/cmf.d.ts +67 -0
  435. package/lib/esm/_holo/spectral/cmf.js +230 -0
  436. package/lib/esm/_holo/spectral/cmf.js.map +1 -0
  437. package/lib/esm/_holo/spectral/float24.d.ts +8 -0
  438. package/lib/esm/_holo/spectral/float24.js +64 -0
  439. package/lib/esm/_holo/spectral/float24.js.map +1 -0
  440. package/lib/esm/_holo/spectral/spd.d.ts +22 -0
  441. package/lib/esm/_holo/spectral/spd.js +130 -0
  442. package/lib/esm/_holo/spectral/spd.js.map +1 -0
  443. package/lib/esm/_holo/spectral/tables.d.ts +62 -0
  444. package/lib/esm/_holo/spectral/tables.js +372 -0
  445. package/lib/esm/_holo/spectral/tables.js.map +1 -0
  446. package/lib/esm/_holo/tier.d.ts +4 -0
  447. package/lib/esm/_holo/tier.js +106 -0
  448. package/lib/esm/_holo/tier.js.map +1 -0
  449. package/lib/esm/_holo/types.d.ts +148 -0
  450. package/lib/esm/_holo/types.js +11 -0
  451. package/lib/esm/_holo/types.js.map +1 -0
  452. package/lib/esm/_kernel-worker.d.ts +22 -0
  453. package/lib/esm/_kernel-worker.js +169 -0
  454. package/lib/esm/_kernel-worker.js.map +1 -0
  455. package/lib/esm/_orbit-camera.d.ts +175 -0
  456. package/lib/esm/_orbit-camera.js +360 -0
  457. package/lib/esm/_orbit-camera.js.map +1 -0
  458. package/lib/esm/_pad.d.ts +2 -0
  459. package/lib/esm/_pad.js +10 -0
  460. package/lib/esm/_pad.js.map +1 -1
  461. package/lib/esm/_scene3d.d.ts +101 -0
  462. package/lib/esm/_scene3d.js +783 -0
  463. package/lib/esm/_scene3d.js.map +1 -0
  464. package/lib/esm/_shape-kernel-source.d.ts +2 -0
  465. package/lib/esm/_shape-kernel-source.js +5 -0
  466. package/lib/esm/_shape-kernel-source.js.map +1 -0
  467. package/lib/esm/_shape-worker.d.ts +8 -0
  468. package/lib/esm/_shape-worker.js +23 -0
  469. package/lib/esm/_shape-worker.js.map +1 -0
  470. package/lib/esm/_silhouette.d.ts +88 -0
  471. package/lib/esm/_silhouette.js +436 -0
  472. package/lib/esm/_silhouette.js.map +1 -0
  473. package/lib/esm/_simplex3.d.ts +2 -0
  474. package/lib/esm/_simplex3.js +157 -0
  475. package/lib/esm/_simplex3.js.map +1 -0
  476. package/lib/esm/_source.d.ts +80 -0
  477. package/lib/esm/_source.js +187 -0
  478. package/lib/esm/_source.js.map +1 -0
  479. package/lib/esm/_sphere-foam.d.ts +34 -0
  480. package/lib/esm/_sphere-foam.js +131 -0
  481. package/lib/esm/_sphere-foam.js.map +1 -0
  482. package/lib/esm/_thin-film.d.ts +26 -0
  483. package/lib/esm/_thin-film.js +140 -0
  484. package/lib/esm/_thin-film.js.map +1 -0
  485. package/lib/esm/bloom.js +40 -106
  486. package/lib/esm/bloom.js.map +1 -1
  487. package/lib/esm/bubble.d.ts +284 -0
  488. package/lib/esm/bubble.js +1816 -0
  489. package/lib/esm/bubble.js.map +1 -0
  490. package/lib/esm/glass.d.ts +254 -0
  491. package/lib/esm/glass.js +977 -0
  492. package/lib/esm/glass.js.map +1 -0
  493. package/lib/esm/holo-static.d.ts +3 -0
  494. package/lib/esm/holo-static.js +13 -0
  495. package/lib/esm/holo-static.js.map +1 -0
  496. package/lib/esm/holo.d.ts +401 -0
  497. package/lib/esm/holo.js +1606 -0
  498. package/lib/esm/holo.js.map +1 -0
  499. package/lib/esm/index.d.ts +18 -9
  500. package/lib/esm/index.js +7 -2
  501. package/lib/esm/index.js.map +1 -1
  502. package/lib/esm/jpeg-glitch.js +32 -29
  503. package/lib/esm/jpeg-glitch.js.map +1 -1
  504. package/lib/esm/matrix.d.ts +1 -1
  505. package/lib/esm/package.json +20 -5
  506. package/lib/esm/swirl-particle.d.ts +257 -0
  507. package/lib/esm/swirl-particle.js +2342 -0
  508. package/lib/esm/swirl-particle.js.map +1 -0
  509. package/lib/esm/voronoi.js +2 -75
  510. package/lib/esm/voronoi.js.map +1 -1
  511. package/package.json +38 -11
@@ -0,0 +1,2342 @@
1
+ import { createBloomPyramid, disposeBloomPyramid, GLSL_BLOOM_KNEE, upsampleBloomPyramid, } from "./_bloom-common.js";
2
+ import { parseHexColor } from "./_figma-common.js";
3
+ import { ORBIT_CAMERA_RANGE, OrbitCamera, } from "./_orbit-camera.js";
4
+ import { padOf, padOutputRect } from "./_pad.js";
5
+ import { clampDt, QUAD_VERTS, stateSizeFromCount } from "./_particle-common.js";
6
+ import { buildShapeAsync } from "./_shape-worker.js";
7
+ import { GLSL_SDF, MASK_LONG, SPAWN_SIZE, } from "./_silhouette.js";
8
+ import { SourceCapture, sourceChannelOf, } from "./_source.js";
9
+ // Frozen, so a preset can't be edited in place by accident.
10
+ function stops(list) {
11
+ return Object.freeze(list.map((s) => Object.freeze(s)));
12
+ }
13
+ /**
14
+ * Built-in particle gradients. Low offsets colour the shaded body, high
15
+ * offsets the fast, glinting and peeled-off particles. Frozen: copy the
16
+ * stops to edit them.
17
+ */
18
+ export const SWIRL_PARTICLE_PALETTES = Object.freeze({
19
+ // Deep navy → blue → violet-pink → near white.
20
+ nebula: stops([
21
+ { offset: 0, color: "#0e1554" },
22
+ { offset: 0.45, color: "#3a50d4" },
23
+ { offset: 0.8, color: "#c478e0" },
24
+ { offset: 1, color: "#f4e6ff" },
25
+ ]),
26
+ ember: stops([
27
+ { offset: 0, color: "#2a0603" },
28
+ { offset: 0.45, color: "#b0260c" },
29
+ { offset: 0.8, color: "#ff9a2e" },
30
+ { offset: 1, color: "#fff3cf" },
31
+ ]),
32
+ aurora: stops([
33
+ { offset: 0, color: "#03262a" },
34
+ { offset: 0.45, color: "#0d8f72" },
35
+ { offset: 0.8, color: "#5cf2b0" },
36
+ { offset: 1, color: "#e4fffb" },
37
+ ]),
38
+ mono: stops([
39
+ { offset: 0, color: "#141519" },
40
+ { offset: 0.45, color: "#4b4f58" },
41
+ { offset: 0.8, color: "#b4b8c0" },
42
+ { offset: 1, color: "#ffffff" },
43
+ ]),
44
+ sakura: stops([
45
+ { offset: 0, color: "#3b0d2f" },
46
+ { offset: 0.45, color: "#a8357a" },
47
+ { offset: 0.8, color: "#f5a3c7" },
48
+ { offset: 1, color: "#fff2f7" },
49
+ ]),
50
+ ocean: stops([
51
+ { offset: 0, color: "#021a35" },
52
+ { offset: 0.45, color: "#0a61a3" },
53
+ { offset: 0.8, color: "#2fc6da" },
54
+ { offset: 1, color: "#e9fcff" },
55
+ ]),
56
+ });
57
+ const DEFAULT_PARAMS = {
58
+ count: 65536,
59
+ depth: 0.22,
60
+ swirl: 1.0,
61
+ curl: 0.8,
62
+ curlScale: 2.2,
63
+ speed: 0.14,
64
+ attract: 10.0,
65
+ peel: 0.8,
66
+ life: 4.0,
67
+ intro: 1.5,
68
+ pointSize: 1.4,
69
+ streak: 0.3,
70
+ fireflyRatio: 0.01,
71
+ fireflyIntensity: 6.0,
72
+ fireflyColor: "#ffd2ec",
73
+ bloom: 0.9,
74
+ palette: "nebula",
75
+ sourceColor: 0,
76
+ sourceChannel: "alpha",
77
+ lightDirection: [-0.45, 0.75, 0.55],
78
+ lightColor: "#ffffff",
79
+ lightIntensity: 1,
80
+ ambient: 0.25,
81
+ occlusion: 0.7,
82
+ glint: 0.7,
83
+ shadow: false,
84
+ shadowOpacity: 0.35,
85
+ shadowSoftness: 14,
86
+ shadowDistance: 0.26,
87
+ shadowColor: "#1c2233",
88
+ yaw: 0,
89
+ pitch: 0,
90
+ orbitAnimation: 23,
91
+ orbitSpeed: 1,
92
+ fov: 35,
93
+ zoom: 1.0,
94
+ drag: false,
95
+ cameraOrigin: "element",
96
+ pointer: "none",
97
+ pointerRadius: 120,
98
+ pointerStrength: 1,
99
+ pad: 200,
100
+ antialias: false,
101
+ };
102
+ // Largest `count`: 2^20, a 1024² state texture.
103
+ const MAX_COUNT = 1 << 20;
104
+ const BLOOM_LEVELS = 6;
105
+ // How much of each coarser bloom level adds into the next finer one.
106
+ const BLOOM_SCATTER = 0.85;
107
+ // Brightness above which pixels bloom: only the HDR sparkles reach it.
108
+ const BLOOM_THRESHOLD = 1.0;
109
+ // Texels of the baked palette gradient.
110
+ const PALETTE_SIZE = 256;
111
+ // Longest simulation step. A frame's dt (capped at 0.1 s by clampDt) is
112
+ // split into ceil(dt / SIM_STEP) equal steps, so the flow keeps real time
113
+ // down to 10 fps and stays as stable as at 30 fps. Below 30 fps each frame
114
+ // then runs up to MAX_SUBSTEPS sim passes (and trail pushes), the price of
115
+ // not slowing down.
116
+ const SIM_STEP = 1 / 30;
117
+ // = clampDt's 0.1 s cap / SIM_STEP.
118
+ const MAX_SUBSTEPS = 3;
119
+ const STILL_STEPS = 90;
120
+ // Share of the surface that is side wall, for spawning: the caps keep
121
+ // their area, the wall grows with the depth.
122
+ function spawnSideShare(shape, halfDepth) {
123
+ if (!shape) {
124
+ return 0;
125
+ }
126
+ const side = shape.spawnSide * 2 * halfDepth;
127
+ const total = side + shape.spawnCap;
128
+ return total > 0 ? side / total : 0;
129
+ }
130
+ // Near / far planes relative to the camera distance.
131
+ const NEAR_K = 0.05 / 3.2;
132
+ const FAR_K = 1 + 6 / 3.2;
133
+ // Past positions kept per particle. The trail is TRAIL_SAMPLES segments:
134
+ // current position → newest sample → … → oldest sample, cut at `streak`.
135
+ const TRAIL_SAMPLES = 6;
136
+ // The history slots are tiled in one atlas render target.
137
+ const HIST_COLS = Math.ceil(Math.sqrt(TRAIL_SAMPLES));
138
+ const HIST_ROWS = Math.ceil(TRAIL_SAMPLES / HIST_COLS);
139
+ // The most particles a GPU can hold: the state is a power-of-two square,
140
+ // and the trail history tiles HIST_COLS × HIST_ROWS of it in one texture.
141
+ function maxCountFor(gl) {
142
+ const maxTex = gl.getParameter(gl.MAX_TEXTURE_SIZE);
143
+ const side = Math.floor(maxTex / Math.max(HIST_COLS, HIST_ROWS));
144
+ const width = 2 ** Math.floor(Math.log2(Math.max(side, 1)));
145
+ return Math.min(MAX_COUNT, width * width);
146
+ }
147
+ // Ago of a slot that holds no sample.
148
+ const HIST_NEVER = 1e9;
149
+ // Generations wrap at this, so they stay exact in a 16-bit float
150
+ // (integers up to 2048 are) and fit the trail's respawn check. The
151
+ // generation of a particle's first life after an `intro` is GEN_WRAP
152
+ // itself, which the wrap never reaches: that life is shortened (see
153
+ // lifeOf), and the next generation is 1.
154
+ const GEN_WRAP = 1024;
155
+ // The first life after an `intro` is uniform in [FIRST_LIFE_MIN, 1] ×
156
+ // the particle's life, close to the life a particle has left at a random
157
+ // moment of the steady flow. So the deaths, and the wisps peeling off,
158
+ // keep their steady rate after the intro instead of coming in waves.
159
+ // Kept above 0 so no life is 0 s long.
160
+ const FIRST_LIFE_MIN = 0.05;
161
+ // Shadow coverage levels, from 1/2 to 1/16 of the scene resolution. The
162
+ // blur runs on the coarsest level it still needs, so wide blurs stay cheap.
163
+ const SHADOW_LEVELS = 4;
164
+ // Largest blur sigma, in texels of a shadow level, before moving down one.
165
+ const SHADOW_MAX_SIGMA = 6;
166
+ // Time constant of the shadow coverage's running average, in seconds of
167
+ // simulation. The binary coverage has holes that move every frame; the
168
+ // average hides them, at the cost of the shadow trailing a fast move.
169
+ const SHADOW_TAU = 0.1;
170
+ // `pointer: "repel"`: seconds for the push to fade in / out.
171
+ const REPEL_FADE = 0.15;
172
+ // Time constant of the smoothed pointer that "repel" pushes from, seconds.
173
+ const REPEL_POS_TAU = 0.06;
174
+ // Push speed at the pointer, in radii (at the particle's depth) per
175
+ // second, at pointerStrength 1.
176
+ const REPEL_K = 4;
177
+ // ---------------------------------------------------------------------------
178
+ // GLSL
179
+ // ---------------------------------------------------------------------------
180
+ // Ashima / Stefan Gustavson 3D simplex noise with analytic gradient (MIT).
181
+ const GLSL_NOISE = `
182
+ vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
183
+ vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
184
+ vec4 permute(vec4 x) { return mod289(((x * 34.0) + 10.0) * x); }
185
+ vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
186
+
187
+ float snoiseGrad(vec3 v, out vec3 gradient) {
188
+ const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);
189
+ const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
190
+ vec3 i = floor(v + dot(v, C.yyy));
191
+ vec3 x0 = v - i + dot(i, C.xxx);
192
+ vec3 g = step(x0.yzx, x0.xyz);
193
+ vec3 l = 1.0 - g;
194
+ vec3 i1 = min(g.xyz, l.zxy);
195
+ vec3 i2 = max(g.xyz, l.zxy);
196
+ vec3 x1 = x0 - i1 + C.xxx;
197
+ vec3 x2 = x0 - i2 + C.yyy;
198
+ vec3 x3 = x0 - D.yyy;
199
+ i = mod289(i);
200
+ vec4 p = permute(permute(permute(
201
+ i.z + vec4(0.0, i1.z, i2.z, 1.0))
202
+ + i.y + vec4(0.0, i1.y, i2.y, 1.0))
203
+ + i.x + vec4(0.0, i1.x, i2.x, 1.0));
204
+ float n_ = 0.142857142857;
205
+ vec3 ns = n_ * D.wyz - D.xzx;
206
+ vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
207
+ vec4 x_ = floor(j * ns.z);
208
+ vec4 y_ = floor(j - 7.0 * x_);
209
+ vec4 x = x_ * ns.x + ns.yyyy;
210
+ vec4 y = y_ * ns.x + ns.yyyy;
211
+ vec4 h = 1.0 - abs(x) - abs(y);
212
+ vec4 b0 = vec4(x.xy, y.xy);
213
+ vec4 b1 = vec4(x.zw, y.zw);
214
+ vec4 s0 = floor(b0) * 2.0 + 1.0;
215
+ vec4 s1 = floor(b1) * 2.0 + 1.0;
216
+ vec4 sh = -step(h, vec4(0.0));
217
+ vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
218
+ vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
219
+ vec3 p0 = vec3(a0.xy, h.x);
220
+ vec3 p1 = vec3(a0.zw, h.y);
221
+ vec3 p2 = vec3(a1.xy, h.z);
222
+ vec3 p3 = vec3(a1.zw, h.w);
223
+ vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
224
+ p0 *= norm.x;
225
+ p1 *= norm.y;
226
+ p2 *= norm.z;
227
+ p3 *= norm.w;
228
+ vec4 m = max(0.5 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
229
+ vec4 m2 = m * m;
230
+ vec4 m4 = m2 * m2;
231
+ vec4 pdotx = vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3));
232
+ vec4 temp = m2 * m * pdotx;
233
+ gradient = -8.0 * (temp.x * x0 + temp.y * x1 + temp.z * x2 + temp.w * x3);
234
+ gradient += m4.x * p0 + m4.y * p1 + m4.z * p2 + m4.w * p3;
235
+ gradient *= 105.0;
236
+ return 105.0 * dot(m4, pdotx);
237
+ }
238
+ `;
239
+ const GLSL_HASH = `
240
+ uint pcg(uint v) {
241
+ uint s = v * 747796405u + 2891336453u;
242
+ uint w = ((s >> ((s >> 28u) + 4u)) ^ s) * 277803737u;
243
+ return (w >> 22u) ^ w;
244
+ }
245
+ float rnd(uint v) {
246
+ return float(pcg(v)) * (1.0 / 4294967296.0);
247
+ }
248
+ `;
249
+ // Spawn points are a pure function of (id, generation), so the colour
250
+ // pass can find where a particle was born without storing it.
251
+ const GLSL_SPAWN = `
252
+ uniform sampler2D spawnTex;
253
+ uniform int spawnSize;
254
+ // Share of births on the side wall, by area at the current depth.
255
+ uniform float spawnSideShare;
256
+
257
+ uint spawnKey(uint id, float gen) {
258
+ return pcg(id ^ (uint(gen) * 2654435769u));
259
+ }
260
+
261
+ // xy = world position, z = position along the depth / halfDepth (-1..1).
262
+ // The first half of the points are on the side wall, the second on the caps.
263
+ vec4 spawnPoint(uint key) {
264
+ int half_ = spawnSize * spawnSize / 2;
265
+ bool side = float(key & 0xffffu) < spawnSideShare * 65536.0;
266
+ int idx = int((key >> 16u) % uint(half_)) + (side ? 0 : half_);
267
+ return texelFetch(spawnTex, ivec2(idx % spawnSize, idx / spawnSize), 0);
268
+ }
269
+ `;
270
+ const FRAG_SIM = `#version 300 es
271
+ precision highp float;
272
+ precision highp int;
273
+ precision highp sampler2D;
274
+
275
+ // State: pos = xyz + age / life (0..1; < 0 while waiting to be born in
276
+ // an intro), vel = xyz + generation (wraps at GEN_WRAP). Both stay
277
+ // meaningful in 16-bit float targets.
278
+ uniform sampler2D posTex;
279
+ uniform sampler2D velTex;
280
+ uniform int stateWidth;
281
+ // Ids at or past \`count\` are not drawn and keep their state.
282
+ uniform int count;
283
+ // 1: respawn every particle. 2: the state grew; copy the first
284
+ // \`oldCount\` particles from the old, \`oldWidth\` wide state, respawn the rest.
285
+ uniform int reset;
286
+ // Seconds over which respawned particles are born; 0: all at once.
287
+ uniform float intro;
288
+ uniform sampler2D oldPosTex;
289
+ uniform sampler2D oldVelTex;
290
+ uniform int oldWidth;
291
+ uniform int oldCount;
292
+ // 1: the state targets are 16-bit floats.
293
+ uniform int halfState;
294
+ uniform int frameSeed;
295
+ uniform float lifeMean;
296
+ uniform float time;
297
+ uniform float dt;
298
+ uniform float swirl;
299
+ uniform float curl;
300
+ uniform float curlScale;
301
+ uniform float speed;
302
+ uniform float attract;
303
+ uniform float peel;
304
+ // Pointer repel: strength × fade (0 = off), the smoothed pointer and the
305
+ // radius in buffer px, and the camera that draws the particles.
306
+ uniform float repelForce;
307
+ uniform vec2 repelPx;
308
+ uniform float repelRadiusPx;
309
+ uniform mat3 viewRot;
310
+ uniform float camDist;
311
+ uniform float focalPx;
312
+ uniform vec2 eye;
313
+ uniform vec2 principalPx;
314
+
315
+ layout(location = 0) out vec4 outPos;
316
+ layout(location = 1) out vec4 outVel;
317
+
318
+ ${GLSL_HASH}
319
+ ${GLSL_NOISE}
320
+ ${GLSL_SDF}
321
+ ${GLSL_SPAWN}
322
+
323
+ float lifeOf(uint id, float gen) {
324
+ float life = lifeMean * mix(0.6, 1.4, rnd(id * 1664525u + uint(gen) * 1013904223u + 7u));
325
+ // The first life after an intro is cut short at random, so the
326
+ // particles born together die over a whole life, not together.
327
+ if (gen >= ${GEN_WRAP.toFixed(1)}) {
328
+ life *= mix(${FIRST_LIFE_MIN.toFixed(2)}, 1.0, rnd(id * 40503u + 23u));
329
+ }
330
+ return life;
331
+ }
332
+
333
+ // Round onto the 16-bit float grid, up or down at random. In 16-bit
334
+ // state targets, a step smaller than half a step of the grid would
335
+ // otherwise round away every frame: the particle would never die or
336
+ // move. This way the steps add up on average.
337
+ float roundHalf(float x, float r) {
338
+ float e = exp2(floor(log2(max(abs(x), 6.2e-5))) - 10.0);
339
+ return floor(x / e + r) * e;
340
+ }
341
+
342
+ vec3 spawn(uint id, float gen) {
343
+ uint key = spawnKey(id, gen);
344
+ vec4 s = spawnPoint(key);
345
+ vec3 p = vec3(s.xy, s.z * halfDepth);
346
+ // A thin fuzzy layer instead of an exact shell.
347
+ vec4 d = sdf3(p);
348
+ return p + d.yzw * (rnd(key + 11u) * 0.012 - d.x);
349
+ }
350
+
351
+ void main() {
352
+ ivec2 texel = ivec2(gl_FragCoord.xy);
353
+ int i = texel.y * stateWidth + texel.x;
354
+ uint id = uint(i);
355
+
356
+ if (reset == 2 && i < oldCount) {
357
+ ivec2 o = ivec2(i % oldWidth, i / oldWidth);
358
+ outPos = texelFetch(oldPosTex, o, 0);
359
+ outVel = texelFetch(oldVelTex, o, 0);
360
+ return;
361
+ }
362
+ if (reset != 0) {
363
+ if (intro > 0.0) {
364
+ // Born at a random moment in the next \`intro\` seconds: until
365
+ // then the age is negative and the particle waits, undrawn.
366
+ // The births speed up (the share born by time t is
367
+ // (t / intro)²): a few particles already show the whole
368
+ // shape, so a steady rate would look done a third of the way in.
369
+ float gen0 = ${GEN_WRAP.toFixed(1)};
370
+ float wait = sqrt(rnd(id * 7919u + 3u)) * intro;
371
+ outPos = vec4(spawn(id, gen0), -wait / lifeOf(id, gen0));
372
+ outVel = vec4(0.0, 0.0, 0.0, gen0);
373
+ return;
374
+ }
375
+ // Random ages, so the particles don't all respawn together.
376
+ outPos = vec4(spawn(id, 0.0), rnd(id * 7919u + 3u));
377
+ outVel = vec4(0.0);
378
+ return;
379
+ }
380
+
381
+ vec4 P = texelFetch(posTex, texel, 0);
382
+ vec4 V = texelFetch(velTex, texel, 0);
383
+ if (i >= count) {
384
+ outPos = P;
385
+ outVel = V;
386
+ return;
387
+ }
388
+
389
+ float gen = V.w;
390
+ float life = lifeOf(id, gen);
391
+ float ageN = P.w + dt / life;
392
+ if (halfState == 1) {
393
+ ageN = roundHalf(ageN, rnd(id * 2654435761u + uint(frameSeed) * 2246822519u + 29u));
394
+ }
395
+ if (ageN < 0.0) {
396
+ // Not born yet: hold on the spawn point, at rest. It grows in
397
+ // once the age passes 0 (see lifeScale). After a refresh() it
398
+ // flows onto the new shape from there, like the others.
399
+ outPos = vec4(P.xyz, ageN);
400
+ outVel = vec4(0.0, 0.0, 0.0, gen);
401
+ return;
402
+ }
403
+ if (ageN >= 1.0) {
404
+ gen = mod(gen + 1.0, ${GEN_WRAP.toFixed(1)});
405
+ outPos = vec4(spawn(id, gen), 0.0);
406
+ outVel = vec4(0.0, 0.0, 0.0, gen);
407
+ return;
408
+ }
409
+
410
+ vec3 p = P.xyz;
411
+ vec3 v = V.xyz;
412
+ vec4 s = sdf3(p);
413
+ vec3 n = s.yzw;
414
+ float d = s.x;
415
+
416
+ // How firmly this particle still holds on to the surface.
417
+ float peeler = smoothstep(0.5, 0.9, rnd(id * 31u + uint(gen) * 17u + 5u));
418
+ float hold = 1.0 - peel * peeler * smoothstep(0.2, 0.9, ageN);
419
+
420
+ // Tangential flow: cross(normal, A), with A a slow, low-frequency
421
+ // vector field around a drifting main axis.
422
+ vec3 ga;
423
+ snoiseGrad(p * 0.55 + vec3(0.0, 0.0, time * 0.05), ga);
424
+ vec3 axis = normalize(vec3(0.35 * sin(time * 0.11), 1.0, 0.45 * cos(time * 0.07)));
425
+ vec3 A = normalize(axis + ga * 0.35);
426
+ vec3 sw = cross(n, A);
427
+
428
+ // Divergence-free noise: cross of two noise gradients.
429
+ vec3 g1;
430
+ vec3 g2;
431
+ vec3 q = p * curlScale;
432
+ snoiseGrad(q + vec3(time * 0.09, 0.0, 0.0), g1);
433
+ snoiseGrad(q + vec3(31.4, 17.7, 9.2) - vec3(0.0, time * 0.08, 0.0), g2);
434
+ vec3 cu = cross(g1, g2) * 0.12;
435
+ // Attached particles stay tangent; peeled ones may lift off.
436
+ vec3 cuT = cu - n * dot(cu, n) * hold;
437
+
438
+ float near = exp(-max(d, 0.0) * 5.0);
439
+ vec3 target = speed * (sw * swirl * mix(0.5, 1.0, near) + cuT * curl)
440
+ - n * d * attract * hold
441
+ + n * speed * 0.8 * (1.0 - hold);
442
+
443
+ // Repel: measured on screen, with the camera that draws the particle,
444
+ // pushed in the view plane. The push is in radii (at the particle's
445
+ // depth) per second, so the hole is the same on any element size.
446
+ if (repelForce > 0.0) {
447
+ vec3 qv = viewRot * p;
448
+ float depth = camDist - qv.z;
449
+ if (depth > 1e-3) {
450
+ vec2 dd = principalPx + (qv.xy - eye) * focalPx / depth - repelPx;
451
+ float r = length(dd) / repelRadiusPx;
452
+ if (r < 1.0) {
453
+ float f = 1.0 - r * r;
454
+ f *= f;
455
+ float a = rnd(id * 97u + 1u) * 6.2831853;
456
+ vec2 dir = r > 1e-3 ? dd / (r * repelRadiusPx) : vec2(cos(a), sin(a));
457
+ float radiusW = repelRadiusPx * depth / focalPx;
458
+ // Inverse rotation: view plane → world.
459
+ target += (vec3(dir, 0.0) * viewRot) * (repelForce * ${REPEL_K.toFixed(1)} * radiusW * f);
460
+ }
461
+ }
462
+ }
463
+
464
+ v = mix(v, target, 1.0 - exp(-dt * 5.0));
465
+ p += v * dt;
466
+ if (halfState == 1) {
467
+ // As for the age: small moves would round away, and slow particles freeze.
468
+ uint k = id * 2654435761u + uint(frameSeed) * 2246822519u;
469
+ p = vec3(
470
+ roundHalf(p.x, rnd(k + 31u)),
471
+ roundHalf(p.y, rnd(k + 37u)),
472
+ roundHalf(p.z, rnd(k + 41u))
473
+ );
474
+ }
475
+
476
+ outPos = vec4(p, ageN);
477
+ outVel = vec4(v, gen);
478
+ }
479
+ `;
480
+ // Per particle, once a frame: the lit colour the body and every segment
481
+ // of its trail share, and how far the particle is off the surface (0..1).
482
+ // Lighting here, not per vertex, saves the SDF lookups of every corner of
483
+ // every segment.
484
+ const FRAG_ATTR = `#version 300 es
485
+ precision highp float;
486
+ precision highp int;
487
+ precision highp sampler2D;
488
+
489
+ uniform sampler2D posTex;
490
+ uniform sampler2D velTex;
491
+ uniform sampler2D paletteTex;
492
+ uniform sampler2D src;
493
+ uniform vec4 srcRectUv;
494
+ uniform vec2 shapeHalf;
495
+ uniform float sourceColor;
496
+ uniform float time;
497
+ uniform float speed;
498
+ uniform int stateWidth;
499
+ uniform int count;
500
+ uniform mat3 viewRot;
501
+ uniform vec3 lightDir;
502
+ uniform vec3 lightColor;
503
+ uniform float lightIntensity;
504
+ uniform float ambient;
505
+ uniform float occlusion;
506
+ uniform float glintAmount;
507
+
508
+ // rgb = the lit colour, a = how far the particle is off the surface.
509
+ out vec4 outA;
510
+
511
+ ${GLSL_HASH}
512
+ ${GLSL_NOISE}
513
+ ${GLSL_SDF}
514
+ ${GLSL_SPAWN}
515
+
516
+ vec3 ramp(float t) {
517
+ float u = (clamp(t, 0.0, 1.0) * ${(PALETTE_SIZE - 1).toFixed(1)} + 0.5) / ${PALETTE_SIZE.toFixed(1)};
518
+ return textureLod(paletteTex, vec2(u, 0.5), 0.0).rgb;
519
+ }
520
+
521
+ // Straight colour of the source where this particle was born.
522
+ vec3 spawnColor(uint id, float gen) {
523
+ vec2 xy = spawnPoint(spawnKey(id, gen)).xy;
524
+ // Side-wall points sit on the contour; step inside to skip the AA edge.
525
+ vec3 s = sd2(xy);
526
+ xy -= s.yz * max(s.x + 0.01, 0.0);
527
+ vec2 uv = clamp(xy / (2.0 * shapeHalf) + 0.5, 0.0, 1.0);
528
+ vec4 c = textureLod(src, srcRectUv.xy + uv * srcRectUv.zw, 0.0);
529
+ return c.a > 1e-3 ? c.rgb / c.a : vec3(0.0);
530
+ }
531
+
532
+ void main() {
533
+ ivec2 texel = ivec2(gl_FragCoord.xy);
534
+ int i = texel.y * stateWidth + texel.x;
535
+ if (i >= count) {
536
+ outA = vec4(0.0);
537
+ return;
538
+ }
539
+ uint id = uint(i);
540
+ vec4 P = texelFetch(posTex, texel, 0);
541
+ vec4 V = texelFetch(velTex, texel, 0);
542
+ vec3 p = P.xyz;
543
+ vec4 s = sdf3(p);
544
+ float off = clamp(s.x / 0.08, 0.0, 1.0);
545
+ float r = rnd(id * 2246822519u + 13u);
546
+
547
+ // Colour attribute: slow noise over the surface + speed + per particle.
548
+ vec3 gn;
549
+ float nz = snoiseGrad(p * 1.6 + vec3(0.0, time * 0.05, 0.0), gn);
550
+ float spd = length(V.xyz) / max(speed, 1e-4);
551
+ float t = 0.3 + 0.35 * nz + 0.3 * clamp(spd - 0.5, 0.0, 1.5) + 0.3 * (r - 0.5);
552
+ t += 0.4 * off;
553
+ vec3 base = ramp(t);
554
+ vec3 highlight = ramp(1.0);
555
+ if (sourceColor > 0.0) {
556
+ // Carried from the spawn point like paint; t keeps some texture.
557
+ vec3 sc = spawnColor(id, V.w);
558
+ base = mix(base, sc * mix(0.85, 1.15, clamp(t, 0.0, 1.0)), sourceColor);
559
+ highlight = mix(highlight, mix(sc, vec3(1.0), 0.5), sourceColor);
560
+ }
561
+
562
+ // Lit once per particle, from the SDF normal at its head, in view
563
+ // space; every segment of its trail takes this colour.
564
+ vec3 nv = viewRot * s.yzw;
565
+ float ndl = dot(nv, lightDir);
566
+ float wrap = 0.5 + 0.5 * ndl;
567
+ // Cheap occlusion: how far the field is clear along the normal.
568
+ float aoDist = sdf3(p + s.yzw * 0.07).x;
569
+ float ao = mix(1.0 - occlusion, 1.0, clamp(aoDist / 0.07, 0.0, 1.0));
570
+ vec3 col = base * (ambient + 0.9 * lightIntensity * wrap * lightColor) * ao;
571
+ // Some fibres catch the light.
572
+ float glint = pow(max(ndl, 0.0), 3.0) * step(0.65, fract(r * 7.13));
573
+ col += highlight * lightColor * glint * glintAmount * ao;
574
+ // Peeled-off wisps get lighter.
575
+ col = mix(col, highlight * 0.85, off * 0.5);
576
+ outA = vec4(col, off);
577
+ }
578
+ `;
579
+ // - body: instanced trails. Each instance is TRAIL_SAMPLES quads, one per
580
+ // segment of the particle's recent path, so the fibres curve. A quad
581
+ // runs half its width past both ends of its segment, so consecutive
582
+ // segments overlap at bends.
583
+ // - shadow: the head segment of each body particle, flattened onto the
584
+ // page along the light, into the shadow coverage target.
585
+ // - firefly: points.
586
+ function makeParticleVert(pass) {
587
+ return `#version 300 es
588
+ precision highp float;
589
+ precision highp int;
590
+ precision highp sampler2D;
591
+ #define ${pass.toUpperCase()}
592
+ #define TRAIL_SAMPLES ${TRAIL_SAMPLES}
593
+ #define HIST_COLS ${HIST_COLS}
594
+
595
+ uniform int idOffset;
596
+ // Instances from here on are spare room in the geometry: drawn as nothing.
597
+ uniform int idEnd;
598
+ #ifdef FIREFLY
599
+ uniform float fireflyIntensity;
600
+ uniform vec3 fireflyColor;
601
+ uniform float time;
602
+ #else
603
+ in vec2 position;
604
+ uniform sampler2D velTex;
605
+ uniform float streak;
606
+ uniform sampler2D histTex;
607
+ // Newest first: slot in the history atlas, and seconds since it was written.
608
+ uniform float histSlot[TRAIL_SAMPLES];
609
+ uniform float histAgo[TRAIL_SAMPLES];
610
+ uniform sampler2D attrA;
611
+ #endif
612
+ #ifdef BODY
613
+ in float seg;
614
+ #endif
615
+ #ifdef SHADOW
616
+ // Towards the light, z > 0; the page is at view z = -shadowDepth
617
+ // (shadowDistance behind the front face, so shadowDepth can be negative).
618
+ uniform vec3 shadowLight;
619
+ uniform float shadowDepth;
620
+ #endif
621
+
622
+ uniform sampler2D posTex;
623
+ uniform int stateWidth;
624
+ uniform mat3 viewRot;
625
+ uniform float camDist;
626
+ uniform float camNear;
627
+ uniform float camFar;
628
+ uniform float focalPx;
629
+ // The eye in view space is (eye, camDist); its axis meets the buffer at
630
+ // principalPx (see _orbit-camera.ts).
631
+ uniform vec2 eye;
632
+ uniform vec2 principalPx;
633
+ uniform vec2 bufferPx;
634
+ uniform float pointPx;
635
+
636
+ out vec3 vColor;
637
+ out vec2 vLocal;
638
+ out vec2 vShape;
639
+
640
+ ${GLSL_HASH}
641
+
642
+ void cull() {
643
+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
644
+ gl_PointSize = 0.0;
645
+ vColor = vec3(0.0);
646
+ vLocal = vec2(0.0);
647
+ vShape = vec2(0.0);
648
+ }
649
+
650
+ // Camera space: xy in buffer px, z = distance from the camera.
651
+ vec3 project(vec3 p) {
652
+ vec3 q = viewRot * p;
653
+ #ifdef SHADOW
654
+ // Flatten onto the page along the light. Points behind the page
655
+ // (the page cuts the shape when shadowDistance is under depth) lie
656
+ // on it.
657
+ q -= shadowLight * (max(q.z + shadowDepth, 0.0) / shadowLight.z);
658
+ #endif
659
+ float depth = camDist - q.z;
660
+ return vec3(principalPx + (q.xy - eye) * focalPx / max(depth, 1e-3), depth);
661
+ }
662
+
663
+ #ifndef FIREFLY
664
+ vec4 histAt(int k, ivec2 texel) {
665
+ int slot = int(histSlot[k]);
666
+ ivec2 o = ivec2(slot % HIST_COLS, slot / HIST_COLS) * stateWidth;
667
+ return texelFetch(histTex, o + texel, 0);
668
+ }
669
+ #endif
670
+
671
+ void main() {
672
+ int i = gl_InstanceID + idOffset;
673
+ if (i >= idEnd) {
674
+ cull();
675
+ return;
676
+ }
677
+ ivec2 texel = ivec2(i % stateWidth, i / stateWidth);
678
+ uint id = uint(i);
679
+ vec4 P = texelFetch(posTex, texel, 0);
680
+ vec3 p = P.xyz;
681
+ float ageN = P.w;
682
+
683
+ // Grow in, shrink out: opaque particles can't fade.
684
+ float lifeScale = smoothstep(0.0, 0.08, ageN) * (1.0 - smoothstep(0.85, 1.0, ageN));
685
+ vec3 c = project(p);
686
+ if (lifeScale <= 0.01 || c.z < camNear) {
687
+ cull();
688
+ return;
689
+ }
690
+ float persp = camDist / c.z;
691
+ float r = rnd(id * 2246822519u + 13u);
692
+
693
+ #ifdef FIREFLY
694
+ float zLin = (c.z - camNear) / (camFar - camNear);
695
+ float flick = pow(0.5 + 0.5 * sin(time * (3.0 + r * 5.0) + r * 40.0), 6.0);
696
+ // Nudge towards the camera so the fibres don't bury every sparkle.
697
+ zLin -= 0.03 / (camFar - camNear);
698
+ gl_Position = vec4(c.xy / bufferPx * 2.0 - 1.0, zLin * 2.0 - 1.0, 1.0);
699
+ gl_PointSize = pointPx * persp * (1.4 + 1.6 * flick) * lifeScale;
700
+ vColor = fireflyColor * fireflyIntensity * (0.25 + 1.75 * flick);
701
+ vLocal = vec2(0.0);
702
+ vShape = vec2(0.0);
703
+ #else
704
+ vec4 A = texelFetch(attrA, texel, 0);
705
+ float off = A.w;
706
+ float w = max(pointPx * persp * lifeScale * mix(1.0, 0.6, off), 1.0);
707
+ #ifdef BODY
708
+ int sg = int(seg + 0.5);
709
+ #else
710
+ int sg = 0;
711
+ #endif
712
+
713
+ // Walk the trail from the head to segment sg. Point j + 1 is history
714
+ // sample j. A sample from another generation (written before the
715
+ // particle's last respawn, or never written: -1) ends the trail, so a
716
+ // respawn never draws a line back. The trail is cut at \`streak\`
717
+ // seconds and, like a speed cap, at 14 widths on screen per 0.3 s of
718
+ // streak (the default), never fewer than 14.
719
+ float gen = texelFetch(velTex, texel, 0).w;
720
+ float capLen = w * 14.0 * max(streak / 0.3, 1.0);
721
+ float used = 0.0;
722
+ vec3 pa = p;
723
+ vec3 ca = c;
724
+ float agoA = 0.0;
725
+ vec3 cb = c;
726
+ bool reached = false;
727
+ for (int j = 0; j < TRAIL_SAMPLES; j++) {
728
+ vec4 h = histAt(j, texel);
729
+ float ago = histAgo[j];
730
+ // ago == agoA: the newest sample was written this frame.
731
+ bool valid = agoA < streak && used < capLen && ago >= agoA
732
+ && abs(h.w - gen) < 0.5;
733
+ vec3 cn = ca;
734
+ vec3 pn = pa;
735
+ if (valid) {
736
+ float f = ago > agoA ? min((streak - agoA) / (ago - agoA), 1.0) : 1.0;
737
+ pn = mix(pa, h.xyz, f);
738
+ cn = project(pn);
739
+ float l = length(cn.xy - ca.xy);
740
+ float room = capLen - used;
741
+ if (l > room) {
742
+ float k = room / l;
743
+ cn = mix(ca, cn, k);
744
+ pn = mix(pa, pn, k);
745
+ l = room;
746
+ }
747
+ used += l;
748
+ }
749
+ if (j == sg) {
750
+ cb = cn;
751
+ // The head segment always draws, as a dot if need be.
752
+ reached = valid || sg == 0;
753
+ break;
754
+ }
755
+ if (!valid) {
756
+ break;
757
+ }
758
+ pa = pn;
759
+ ca = cn;
760
+ agoA = ago;
761
+ }
762
+ if (!reached) {
763
+ cull();
764
+ return;
765
+ }
766
+
767
+ // Quad from the segment's start (ca, towards the head) to its end,
768
+ // ws wide and ws / 2 longer at each end. The body draws it whole, the
769
+ // shadow cuts a capsule out of it.
770
+ float ws = w * mix(1.0, 0.55, clamp(agoA / max(streak, 1e-4), 0.0, 1.0));
771
+ #ifdef SHADOW
772
+ // The coverage target is half-res: twice as wide (at least one of its
773
+ // texels), so the dense body reads as solid rather than speckled.
774
+ ws = max(ws * 2.0, 2.0);
775
+ #endif
776
+ vec2 d = ca.xy - cb.xy;
777
+ float len = length(d);
778
+ vec2 ax = len > 1e-3 ? d / len : vec2(1.0, 0.0);
779
+ vec2 pe = vec2(-ax.y, ax.x);
780
+ vec2 mid = ca.xy - ax * len * 0.5;
781
+ vec2 local = position * vec2(len + ws, ws);
782
+ vec2 px = mid + ax * local.x + pe * local.y;
783
+ float zA = clamp((ca.z - camNear) / (camFar - camNear), 0.0, 1.0);
784
+ float zB = clamp((cb.z - camNear) / (camFar - camNear), 0.0, 1.0);
785
+ float zLin = mix(zB, zA, position.x + 0.5);
786
+ gl_Position = vec4(px / bufferPx * 2.0 - 1.0, zLin * 2.0 - 1.0, 1.0);
787
+ vLocal = local;
788
+ vShape = vec2(len * 0.5, ws * 0.5);
789
+
790
+ #ifdef BODY
791
+ vColor = A.rgb;
792
+ #else
793
+ vColor = vec3(1.0);
794
+ #endif
795
+ #endif
796
+ }
797
+ `;
798
+ }
799
+ const VERT_BODY = makeParticleVert("body");
800
+ const VERT_SHADOW = makeParticleVert("shadow");
801
+ const VERT_FIREFLY = makeParticleVert("firefly");
802
+ const FRAG_BODY = `#version 300 es
803
+ precision highp float;
804
+ in vec3 vColor;
805
+ in vec2 vLocal;
806
+ in vec2 vShape;
807
+ out vec4 outColor;
808
+
809
+ // No discard: the quad's sides are the fibre's edges, so multisampling
810
+ // (antialias) smooths them. Round caps would be invisible at fibre
811
+ // widths anyway.
812
+ void main() {
813
+ // Rounded shading across the width (vShape.y = half width).
814
+ float r = vLocal.y / max(vShape.y, 1e-3);
815
+ outColor = vec4(vColor * (1.0 - 0.3 * r * r), 1.0);
816
+ }
817
+ `;
818
+ // Shadow coverage: 1 inside the flattened capsule.
819
+ const FRAG_SHADOW_COVER = `#version 300 es
820
+ precision highp float;
821
+ in vec2 vLocal;
822
+ in vec2 vShape;
823
+ out vec4 outColor;
824
+
825
+ void main() {
826
+ vec2 q = vec2(max(abs(vLocal.x) - vShape.x, 0.0), vLocal.y);
827
+ if (length(q) > max(vShape.y, 1e-3)) discard;
828
+ outColor = vec4(1.0);
829
+ }
830
+ `;
831
+ // Running average of the coverage: hist = mix(hist, cur, amount).
832
+ const FRAG_SHADOW_ACCUM = `#version 300 es
833
+ precision highp float;
834
+ in vec2 uv;
835
+ uniform sampler2D curTex;
836
+ uniform sampler2D histTex;
837
+ uniform float amount;
838
+ out vec4 outColor;
839
+
840
+ void main() {
841
+ float c = texture(curTex, uv).r;
842
+ // amount 1 restarts the average; skip whatever the history holds.
843
+ float v = amount >= 1.0 ? c : mix(texture(histTex, uv).r, c, amount);
844
+ outColor = vec4(v, 0.0, 0.0, 1.0);
845
+ }
846
+ `;
847
+ // One direction of a separable gaussian on the coverage (red channel).
848
+ const FRAG_SHADOW_BLUR = `#version 300 es
849
+ precision highp float;
850
+ in vec2 uv;
851
+ uniform sampler2D srcTex;
852
+ uniform vec2 dir;
853
+ uniform float sigma;
854
+ uniform int radius;
855
+ out vec4 outColor;
856
+
857
+ void main() {
858
+ float k = -0.5 / (sigma * sigma);
859
+ float sum = texture(srcTex, uv).r;
860
+ float wsum = 1.0;
861
+ for (int i = 1; i <= 24; i++) {
862
+ if (i > radius) break;
863
+ float w = exp(float(i * i) * k);
864
+ sum += (texture(srcTex, uv + dir * float(i)).r
865
+ + texture(srcTex, uv - dir * float(i)).r) * w;
866
+ wsum += 2.0 * w;
867
+ }
868
+ outColor = vec4(sum / wsum, 0.0, 0.0, 1.0);
869
+ }
870
+ `;
871
+ // Trail history: copy the position texture into one slot of the atlas.
872
+ const VERT_HIST = `#version 300 es
873
+ in vec2 position;
874
+ uniform vec4 slotRect;
875
+ void main() {
876
+ gl_Position = vec4(mix(slotRect.xy, slotRect.zw, position + 0.5), 0.0, 1.0);
877
+ }
878
+ `;
879
+ // A sample is the position and the generation it was written in.
880
+ const FRAG_HIST_PUSH = `#version 300 es
881
+ precision highp float;
882
+ precision highp sampler2D;
883
+ uniform sampler2D posTex;
884
+ uniform sampler2D velTex;
885
+ uniform vec2 slotOrigin;
886
+ out vec4 outColor;
887
+ void main() {
888
+ ivec2 texel = ivec2(gl_FragCoord.xy - slotOrigin);
889
+ outColor = vec4(texelFetch(posTex, texel, 0).xyz, texelFetch(velTex, texel, 0).w);
890
+ }
891
+ `;
892
+ // A generation no particle has, so every sample reads as stale.
893
+ const FRAG_HIST_CLEAR = `#version 300 es
894
+ precision highp float;
895
+ out vec4 outColor;
896
+ void main() {
897
+ outColor = vec4(0.0, 0.0, 0.0, -1.0);
898
+ }
899
+ `;
900
+ const FRAG_FIREFLY = `#version 300 es
901
+ precision highp float;
902
+ in vec3 vColor;
903
+ out vec4 outColor;
904
+
905
+ void main() {
906
+ vec2 c = gl_PointCoord * 2.0 - 1.0;
907
+ float r2 = dot(c, c);
908
+ // A hot core with a small 4-point star.
909
+ float core = exp(-r2 * 10.0);
910
+ float star = exp(-abs(c.x) * 14.0) * exp(-abs(c.y) * 2.5)
911
+ + exp(-abs(c.y) * 14.0) * exp(-abs(c.x) * 2.5);
912
+ float a = core + star * 0.45;
913
+ if (a < 0.003) discard;
914
+ outColor = vec4(vColor * a, 0.0);
915
+ }
916
+ `;
917
+ const FRAG_BLOOM_PREFILTER = `#version 300 es
918
+ precision highp float;
919
+ in vec2 uv;
920
+ uniform sampler2D scene;
921
+ uniform vec2 texel;
922
+ out vec4 outColor;
923
+
924
+ const float threshold = ${BLOOM_THRESHOLD.toFixed(1)};
925
+ ${GLSL_BLOOM_KNEE}
926
+ void main() {
927
+ vec3 c = texture(scene, uv + texel * vec2(-0.5, -0.5)).rgb
928
+ + texture(scene, uv + texel * vec2(0.5, -0.5)).rgb
929
+ + texture(scene, uv + texel * vec2(-0.5, 0.5)).rgb
930
+ + texture(scene, uv + texel * vec2(0.5, 0.5)).rgb;
931
+ c *= 0.25;
932
+ float br = max(c.r, max(c.g, c.b));
933
+ outColor = vec4(c * bloomKnee(br, threshold, 0.5), 1.0);
934
+ }
935
+ `;
936
+ // 5-tap downsample of the bloom and the shadow chains.
937
+ const FRAG_BLOOM_DOWN = `#version 300 es
938
+ precision highp float;
939
+ in vec2 uv;
940
+ uniform sampler2D srcTex;
941
+ uniform vec2 texel;
942
+ out vec4 outColor;
943
+
944
+ void main() {
945
+ vec3 c = texture(srcTex, uv).rgb * 0.5;
946
+ c += texture(srcTex, uv + texel * vec2(-1.0, -1.0)).rgb * 0.125;
947
+ c += texture(srcTex, uv + texel * vec2(1.0, -1.0)).rgb * 0.125;
948
+ c += texture(srcTex, uv + texel * vec2(-1.0, 1.0)).rgb * 0.125;
949
+ c += texture(srcTex, uv + texel * vec2(1.0, 1.0)).rgb * 0.125;
950
+ outColor = vec4(c, 1.0);
951
+ }
952
+ `;
953
+ // Premultiplied particles + bloom over transparency, over the shadow.
954
+ // Alpha covers the bloom and the shadow too, so both composite over any
955
+ // page background.
956
+ const FRAG_COMPOSITE = `#version 300 es
957
+ precision highp float;
958
+ in vec2 uv;
959
+ uniform sampler2D scene;
960
+ uniform sampler2D bloomTex;
961
+ uniform sampler2D shadowTex;
962
+ uniform int hasScene;
963
+ uniform float bloom;
964
+ // shadowOpacity, or 0 with the shadow off.
965
+ uniform float shadowAmount;
966
+ uniform vec3 shadowColor;
967
+ out vec4 outColor;
968
+
969
+ vec3 softClip(vec3 x) {
970
+ vec3 k = 0.8 + 0.2 * (1.0 - exp(-(x - 0.8) / 0.2));
971
+ return mix(x, k, step(0.8, x));
972
+ }
973
+
974
+ void main() {
975
+ if (hasScene == 0) {
976
+ outColor = vec4(0.0);
977
+ return;
978
+ }
979
+ vec4 s = texture(scene, uv);
980
+ vec3 col = s.rgb + texture(bloomTex, uv).rgb * bloom;
981
+ col = softClip(max(col, 0.0));
982
+ float a = clamp(max(s.a, max(col.r, max(col.g, col.b))), 0.0, 1.0);
983
+ if (shadowAmount > 0.0) {
984
+ float sh = shadowAmount * clamp(texture(shadowTex, uv).r, 0.0, 1.0);
985
+ col += shadowColor * sh * (1.0 - a);
986
+ a += sh * (1.0 - a);
987
+ }
988
+ // Noise dither against banding in the faint glow, on the alpha too:
989
+ // the glow composites as a translucent colour, so where its wide tail
990
+ // crosses an 8-bit alpha step it would draw a contour on a flat page.
991
+ // The colour scales with the dithered alpha (premultiplied, hue kept)
992
+ // and gets its own noise. Pixels with nothing drawn stay exactly 0.
993
+ if (a <= 0.0) {
994
+ outColor = vec4(0.0);
995
+ return;
996
+ }
997
+ vec2 fc = gl_FragCoord.xy;
998
+ float n1 = fract(sin(dot(fc, vec2(12.9898, 78.233))) * 43758.5453) - 0.5;
999
+ float n2 = fract(sin(dot(fc, vec2(39.3468, 11.135))) * 24634.6345) - 0.5;
1000
+ // Slightly under ±0.5 levels, so a vanishing tail can't round up to
1001
+ // specks across the whole output rect.
1002
+ float ad = clamp(a + n1 * 0.98 / 255.0, 0.0, 1.0);
1003
+ col = clamp(col * (ad / a) + n2 / 255.0, 0.0, ad);
1004
+ outColor = vec4(col, ad);
1005
+ }
1006
+ `;
1007
+ // [min, max] of every numeric param. A finite value is clamped into it,
1008
+ // anything else (NaN, ±Infinity, not a number) falls back to the default.
1009
+ const NUM_RANGE = {
1010
+ count: [1, MAX_COUNT],
1011
+ depth: [0.01, 4],
1012
+ swirl: [-10, 10],
1013
+ curl: [-10, 10],
1014
+ curlScale: [0.01, 50],
1015
+ speed: [0, 10],
1016
+ attract: [0, 100],
1017
+ peel: [0, 1],
1018
+ life: [0.1, 120],
1019
+ intro: [0, 120],
1020
+ pointSize: [0, 64],
1021
+ streak: [0, 10],
1022
+ fireflyRatio: [0, 1],
1023
+ fireflyIntensity: [0, 1000],
1024
+ bloom: [0, 100],
1025
+ sourceColor: [0, 1],
1026
+ lightIntensity: [0, 100],
1027
+ ambient: [0, 1],
1028
+ occlusion: [0, 1],
1029
+ glint: [0, 100],
1030
+ shadowOpacity: [0, 1],
1031
+ shadowSoftness: [0, 500],
1032
+ shadowDistance: [0, 100],
1033
+ ...ORBIT_CAMERA_RANGE,
1034
+ pointerRadius: [0, 10000],
1035
+ };
1036
+ const NUM_KEYS = Object.keys(NUM_RANGE);
1037
+ function clamp(x, lo, hi) {
1038
+ return Math.min(Math.max(x, lo), hi);
1039
+ }
1040
+ const HEX_RE = /^#?(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
1041
+ // Parsed colours by string, so a frame doesn't parse them again.
1042
+ const rgbCache = new Map();
1043
+ // RGB in [0, 1] of a CSS hex colour, or of `fallback` when it isn't one.
1044
+ // The result is shared: don't mutate it.
1045
+ function rgbOf(hex, fallback) {
1046
+ const key = typeof hex === "string" && HEX_RE.test(hex) ? hex : fallback;
1047
+ let rgb = rgbCache.get(key);
1048
+ if (!rgb) {
1049
+ if (rgbCache.size >= 64) {
1050
+ rgbCache.clear();
1051
+ }
1052
+ const [r, g, b] = parseHexColor(key);
1053
+ rgb = [r, g, b];
1054
+ rgbCache.set(key, rgb);
1055
+ }
1056
+ return rgb;
1057
+ }
1058
+ // Unit vector towards the light, into `out`. Falls back to the default
1059
+ // for a zero or broken direction.
1060
+ function lightUnit(dir, out) {
1061
+ const d = Array.isArray(dir) ? dir : [];
1062
+ const [x, y, z] = [Number(d[0]), Number(d[1]), Number(d[2])];
1063
+ const len = Math.hypot(x, y, z);
1064
+ if (!Number.isFinite(len) || len < 1e-6) {
1065
+ lightUnit(DEFAULT_PARAMS.lightDirection, out);
1066
+ return;
1067
+ }
1068
+ out[0] = x / len;
1069
+ out[1] = y / len;
1070
+ out[2] = z / len;
1071
+ }
1072
+ // The one place params are checked: once a frame, into `out`.
1073
+ function resolveParams(p, out) {
1074
+ const r = out ?? { light: [0, 0, 1] };
1075
+ for (const k of NUM_KEYS) {
1076
+ const v = p[k];
1077
+ const [lo, hi] = NUM_RANGE[k];
1078
+ r[k] =
1079
+ typeof v === "number" && Number.isFinite(v)
1080
+ ? clamp(v, lo, hi)
1081
+ : DEFAULT_PARAMS[k];
1082
+ }
1083
+ r.count = Math.floor(r.count);
1084
+ r.pad = padOf(p.pad, DEFAULT_PARAMS.pad);
1085
+ r.shadow = !!p.shadow;
1086
+ r.drag = !!p.drag;
1087
+ r.cameraOrigin = p.cameraOrigin === "viewport" ? "viewport" : "element";
1088
+ r.antialias = !!p.antialias;
1089
+ r.pointer =
1090
+ p.pointer === "repel" || p.pointer === "tilt" ? p.pointer : "none";
1091
+ lightUnit(p.lightDirection, r.light);
1092
+ r.lightColor = rgbOf(p.lightColor, DEFAULT_PARAMS.lightColor);
1093
+ r.fireflyColor = rgbOf(p.fireflyColor, DEFAULT_PARAMS.fireflyColor);
1094
+ r.shadowColor = rgbOf(p.shadowColor, DEFAULT_PARAMS.shadowColor);
1095
+ return r;
1096
+ }
1097
+ function resolvePalette(palette) {
1098
+ const stops = (typeof palette === "string"
1099
+ ? SWIRL_PARTICLE_PALETTES[palette]
1100
+ : Array.isArray(palette)
1101
+ ? palette.filter((s) => s && typeof s === "object")
1102
+ : null);
1103
+ return stops && stops.length > 0 ? stops : SWIRL_PARTICLE_PALETTES.nebula;
1104
+ }
1105
+ // Bake the stops into PALETTE_SIZE RGBA8 texels, like a CSS gradient:
1106
+ // sorted by offset, flat beyond the ends, linear in between.
1107
+ function bakePalette(palette) {
1108
+ const stops = resolvePalette(palette)
1109
+ .map((s) => ({
1110
+ offset: Number.isFinite(s.offset) ? clamp(s.offset, 0, 1) : 0,
1111
+ rgb: rgbOf(s.color, "#000000"),
1112
+ }))
1113
+ .sort((a, b) => a.offset - b.offset);
1114
+ const out = new Uint8Array(PALETTE_SIZE * 4);
1115
+ let k = 0;
1116
+ for (let i = 0; i < PALETTE_SIZE; i++) {
1117
+ const t = i / (PALETTE_SIZE - 1);
1118
+ while (k < stops.length - 1 && stops[k + 1].offset < t) {
1119
+ k++;
1120
+ }
1121
+ const a = stops[k];
1122
+ const b = stops[Math.min(k + 1, stops.length - 1)];
1123
+ let f = 0;
1124
+ if (t <= a.offset) {
1125
+ f = 0;
1126
+ }
1127
+ else if (t >= b.offset) {
1128
+ f = 1;
1129
+ }
1130
+ else {
1131
+ f = (t - a.offset) / Math.max(b.offset - a.offset, 1e-6);
1132
+ }
1133
+ for (let c = 0; c < 3; c++) {
1134
+ const v = a.rgb[c] + (b.rgb[c] - a.rgb[c]) * f;
1135
+ out[i * 4 + c] = Math.round(v * 255);
1136
+ }
1137
+ out[i * 4 + 3] = 255;
1138
+ }
1139
+ return out;
1140
+ }
1141
+ // TRAIL_SAMPLES quads, one per trail segment: corner + segment index.
1142
+ // Room for `n` instances, rounded up to one of 8 steps per power of two
1143
+ // (at most 12.5% spare).
1144
+ function instanceBucket(n) {
1145
+ if (n <= 0) {
1146
+ return 0;
1147
+ }
1148
+ const step = 2 ** Math.max(Math.floor(Math.log2(n)) - 3, 0);
1149
+ return Math.ceil(n / step) * step;
1150
+ }
1151
+ // `segments` quads per instance, one per segment of the trail from the
1152
+ // head; 1 draws only the head, for `streak` 0.
1153
+ function trailGeometry(instanceCount, segments = TRAIL_SAMPLES) {
1154
+ const corners = [-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5];
1155
+ const position = new Float32Array(segments * 8);
1156
+ const seg = new Float32Array(segments * 4);
1157
+ const indices = new Uint16Array(segments * 6);
1158
+ for (let s = 0; s < segments; s++) {
1159
+ position.set(corners, s * 8);
1160
+ seg.fill(s, s * 4, s * 4 + 4);
1161
+ const v = s * 4;
1162
+ indices.set([v, v + 1, v + 2, v, v + 2, v + 3], s * 6);
1163
+ }
1164
+ return {
1165
+ attributes: {
1166
+ position: { data: position, itemSize: 2 },
1167
+ seg: { data: seg, itemSize: 1 },
1168
+ },
1169
+ indices,
1170
+ instanceCount,
1171
+ };
1172
+ }
1173
+ function smoothstep(e0, e1, x) {
1174
+ const t = clamp((x - e0) / (e1 - e0), 0, 1);
1175
+ return t * t * (3 - 2 * t);
1176
+ }
1177
+ const NO_LAYERS = { bloom: null, shadow: null, shadowAmount: 0 };
1178
+ /**
1179
+ * Particles that flow over the surface of the element's silhouette,
1180
+ * extruded into 3D, and peel off it in curling wisps. Fibres are shaded
1181
+ * from the shape's SDF, coloured by a gradient {@link SwirlParticleParams.palette}
1182
+ * (or the source colours, with `sourceColor`), with HDR sparkles and bloom.
1183
+ *
1184
+ * ```ts
1185
+ * vfx.add(img, { effect: new SwirlParticleEffect({ palette: "ember" }) });
1186
+ * ```
1187
+ *
1188
+ * The silhouette is the element's alpha (or the channel
1189
+ * {@link SwirlParticleParams.sourceChannel | sourceChannel} picks), read
1190
+ * back when the effect starts and again when the element's aspect ratio
1191
+ * changes. Call
1192
+ * {@link SwirlParticleEffect.refresh | refresh()} after its content
1193
+ * changes. An opaque photo becomes an extruded slab. The particles are
1194
+ * born over the first {@link SwirlParticleParams.intro | intro} seconds
1195
+ * (1.5 by default). The output is premultiplied over transparency, so set
1196
+ * the page background yourself. The camera
1197
+ * ({@link OrbitCameraParams}) is the one {@link GlassEffect} has, with
1198
+ * angles in degrees; `pointer: "tilt"` turns it towards the pointer. With
1199
+ * `cameraOrigin: "viewport"`, the default `pad` of 200 can clip an
1200
+ * element near the edge of the viewport.
1201
+ *
1202
+ * All params, `count` and `fireflyRatio` included, are read every frame,
1203
+ * so you can mutate `params` live. Numbers that are not finite fall back
1204
+ * to their defaults. Set `drag: true` to orbit the camera by dragging
1205
+ * the element, `shadow: true` to cast a drop shadow onto the page
1206
+ * behind, and `antialias: true` to smooth the fibres' edges.
1207
+ */
1208
+ export class SwirlParticleEffect {
1209
+ params;
1210
+ #still;
1211
+ // Whether float targets are 16-bit: the core falls back to RGBA16F
1212
+ // without OES_texture_float_linear.
1213
+ #halfState = false;
1214
+ // This frame's params, checked (see resolveParams).
1215
+ #r;
1216
+ // The most particles the GPU's textures can hold (see maxCountFor).
1217
+ #maxCount = MAX_COUNT;
1218
+ #warnedCount = false;
1219
+ // The params resolved, with `count` held to what the GPU can hold.
1220
+ #resolve() {
1221
+ const r = resolveParams(this.params, this.#r);
1222
+ if (r.count > this.#maxCount) {
1223
+ if (!this.#warnedCount) {
1224
+ this.#warnedCount = true;
1225
+ console.warn(`[VFX-JS] SwirlParticleEffect: count ${r.count} is more than this GPU can hold; using ${this.#maxCount}.`);
1226
+ }
1227
+ r.count = this.#maxCount;
1228
+ }
1229
+ return r;
1230
+ }
1231
+ // Reads the silhouette back: when the effect starts, on refresh(),
1232
+ // and when the element's aspect ratio changes.
1233
+ #source = new SourceCapture({
1234
+ longSide: MASK_LONG,
1235
+ onRead: (r, isCurrent) => this.#onRead(r, isCurrent),
1236
+ });
1237
+ // `sourceChannel` of the last shape read; a change reads it again.
1238
+ #channel = null;
1239
+ #pending = null;
1240
+ #shape = null;
1241
+ #needsReset = true;
1242
+ #simTime = 0;
1243
+ // Sim steps so far, a seed for the age rounding.
1244
+ #steps = 0;
1245
+ #stillDone = false;
1246
+ // Count the still frame was simulated with.
1247
+ #stillCount = 0;
1248
+ // Inputs of the drawn still frame; a change draws it again.
1249
+ #stillKey = [];
1250
+ // Particle state, stateWidth² particles. Grows with `count`.
1251
+ #stateWidth = 0;
1252
+ #pos = null;
1253
+ #vel = null;
1254
+ #scene = null;
1255
+ // Samples #scene was created with: 4 with `antialias`.
1256
+ #sceneSamples = 1;
1257
+ #bloom = null;
1258
+ // Scene size the bloom pyramid was made for.
1259
+ #bloomBase = [0, 0];
1260
+ #shadow = [];
1261
+ #shadowBase = [0, 0];
1262
+ // Running average of the level 0 coverage (persistent: ping-pong).
1263
+ #shadowHist = null;
1264
+ // False restarts the average on the next shadow frame.
1265
+ #shadowHistValid = false;
1266
+ #layers = NO_LAYERS;
1267
+ // Seconds the simulation advanced this frame.
1268
+ #frameDt = 0;
1269
+ // Camera of the last drawn frame; "repel" pushes in its view.
1270
+ #cam = null;
1271
+ // Per particle colour, written once a frame (see FRAG_ATTR).
1272
+ #attrA = null;
1273
+ // Trail history: TRAIL_SAMPLES copies of the position texture, tiled in
1274
+ // one atlas, written round-robin. #histTime holds the sim time each
1275
+ // slot was written (-Infinity: never).
1276
+ #hist = null;
1277
+ #histTime = new Array(TRAIL_SAMPLES).fill(-Infinity);
1278
+ #histHead = TRAIL_SAMPLES - 1;
1279
+ #lastPush = -Infinity;
1280
+ #gl = null;
1281
+ #sdfRaw = null;
1282
+ #spawnRaw = null;
1283
+ #paletteRaw = null;
1284
+ #sdfTex = null;
1285
+ #spawnTex = null;
1286
+ #paletteTex = null;
1287
+ // The palette last baked: the value, and for stops, a flat copy of
1288
+ // their offsets and colours to catch edits in place.
1289
+ #paletteSrc = null;
1290
+ #paletteSnap = [];
1291
+ #paletteVersion = 0;
1292
+ // Draw geometry for the current body / firefly split (fireflies take
1293
+ // the first ids), rebuilt when it changes.
1294
+ #trails = null;
1295
+ #heads = null;
1296
+ // The body with only its head segment, for `streak` 0.
1297
+ #dots = null;
1298
+ #fireflies = null;
1299
+ #nBody = -1;
1300
+ #nFire = -1;
1301
+ // Instances the geometry has room for.
1302
+ #bodyCap = -1;
1303
+ #fireCap = -1;
1304
+ #slotQuad = {
1305
+ attributes: { position: { data: QUAD_VERTS, itemSize: 2 } },
1306
+ };
1307
+ #unsubRestored = null;
1308
+ // The camera: drags, the "tilt" and whether the pointer is in the
1309
+ // page, with window listeners while `drag` is on or `pointer` is not
1310
+ // "none".
1311
+ #camera = new OrbitCamera(DEFAULT_PARAMS);
1312
+ // Smoothed repel pointer, element-local physical px, and the fade.
1313
+ #repelPos = [0, 0];
1314
+ #repelAmount = 0;
1315
+ constructor(initial = {}, opts = {}) {
1316
+ this.params = { ...DEFAULT_PARAMS, ...initial };
1317
+ this.#still = opts.still ?? false;
1318
+ this.#r = resolveParams(this.params);
1319
+ }
1320
+ setParams(updates) {
1321
+ Object.assign(this.params, updates);
1322
+ }
1323
+ init(ctx) {
1324
+ this.#gl = ctx.gl;
1325
+ this.#halfState = !ctx.gl.getExtension("OES_texture_float_linear");
1326
+ this.#maxCount = maxCountFor(ctx.gl);
1327
+ const r = this.#resolve();
1328
+ this.#allocState(ctx, stateSizeFromCount(r.count), false);
1329
+ this.#createScene(ctx);
1330
+ this.#unsubRestored = ctx.onContextRestored(() => {
1331
+ // Raw textures are gone; upload them again. A readback in
1332
+ // flight was rejected; start over if it was the first.
1333
+ this.#source.contextRestored();
1334
+ this.#gl = ctx.gl;
1335
+ this.#halfState = !ctx.gl.getExtension("OES_texture_float_linear");
1336
+ this.#sdfRaw = null;
1337
+ this.#spawnRaw = null;
1338
+ this.#paletteRaw = null;
1339
+ this.#sdfTex = null;
1340
+ this.#spawnTex = null;
1341
+ this.#paletteTex = null;
1342
+ this.#paletteSrc = null;
1343
+ this.#pending = this.#pending ?? this.#shape;
1344
+ // The state and trail history targets come back empty:
1345
+ // respawn and mark every history slot stale.
1346
+ this.#needsReset = true;
1347
+ this.#stillDone = false;
1348
+ this.#shadowHistValid = false;
1349
+ });
1350
+ }
1351
+ outputRect(dims) {
1352
+ return padOutputRect(padOf(this.params.pad, DEFAULT_PARAMS.pad), dims);
1353
+ }
1354
+ // The particle buffer: HDR colour + depth, multisampled with
1355
+ // `antialias`.
1356
+ #createScene(ctx) {
1357
+ const samples = this.#r.antialias ? 4 : 1;
1358
+ this.#scene?.dispose();
1359
+ const scene = ctx.createRenderTarget({
1360
+ float: true,
1361
+ depth: true,
1362
+ samples,
1363
+ });
1364
+ this.#scene = scene;
1365
+ this.#sceneSamples = samples;
1366
+ return scene;
1367
+ }
1368
+ // (Re)allocate the per-particle targets, stateWidth² particles. With
1369
+ // `migrate`, the particles that exist carry over and the new ones
1370
+ // spawn; the trails regrow. Needs `render()` for the migration.
1371
+ #allocState(ctx, width, migrate) {
1372
+ const oldPos = this.#pos;
1373
+ const oldVel = this.#vel;
1374
+ const oldWidth = this.#stateWidth;
1375
+ const stateOpts = {
1376
+ size: [width, width],
1377
+ float: true,
1378
+ persistent: true,
1379
+ filter: "nearest",
1380
+ };
1381
+ const pos = ctx.createRenderTarget(stateOpts);
1382
+ const vel = ctx.createRenderTarget(stateOpts);
1383
+ this.#stateWidth = width;
1384
+ if (migrate &&
1385
+ oldPos &&
1386
+ oldVel &&
1387
+ !this.#needsReset &&
1388
+ this.#sdfTex &&
1389
+ this.#spawnTex) {
1390
+ ctx.draw({
1391
+ frag: FRAG_SIM,
1392
+ uniforms: {
1393
+ ...this.#simUniforms(ctx),
1394
+ posTex: oldPos,
1395
+ velTex: oldVel,
1396
+ oldPosTex: oldPos,
1397
+ oldVelTex: oldVel,
1398
+ oldWidth,
1399
+ oldCount: oldWidth * oldWidth,
1400
+ reset: 2,
1401
+ dt: 0,
1402
+ },
1403
+ target: [pos, vel],
1404
+ });
1405
+ }
1406
+ else {
1407
+ this.#needsReset = true;
1408
+ }
1409
+ oldPos?.dispose();
1410
+ oldVel?.dispose();
1411
+ this.#attrA?.dispose();
1412
+ this.#hist?.dispose();
1413
+ this.#pos = pos;
1414
+ this.#vel = vel;
1415
+ const attrOpts = { ...stateOpts, persistent: false };
1416
+ this.#attrA = ctx.createRenderTarget(attrOpts);
1417
+ this.#hist = ctx.createRenderTarget({
1418
+ ...attrOpts,
1419
+ size: [width * HIST_COLS, width * HIST_ROWS],
1420
+ });
1421
+ // The new atlas reads as generation 0 everywhere; mark it stale.
1422
+ if (!this.#needsReset) {
1423
+ this.#resetHistory(ctx);
1424
+ }
1425
+ }
1426
+ render(ctx) {
1427
+ if (!this.#scene || !this.#pos || !this.#vel) {
1428
+ return;
1429
+ }
1430
+ const r = this.#resolve();
1431
+ // `antialias` changed: a new buffer with the other sample count.
1432
+ const scene = (r.antialias ? 4 : 1) === this.#sceneSamples
1433
+ ? this.#scene
1434
+ : this.#createScene(ctx);
1435
+ if (this.#source.phase === "idle" &&
1436
+ this.#channel !== null &&
1437
+ sourceChannelOf(this.params.sourceChannel) !== this.#channel) {
1438
+ this.#source.refresh();
1439
+ }
1440
+ // The aspect watch waits while a shape read back waits to upload.
1441
+ if (this.#source.phase !== "idle" || !this.#pending) {
1442
+ this.#source.update(ctx);
1443
+ }
1444
+ if (this.#pending && this.#upload(ctx, this.#pending)) {
1445
+ // The first shape spawns every particle. A new one takes over
1446
+ // without a reset: the particles flow onto it and respawn on
1447
+ // it. A still frame is simulated again from scratch.
1448
+ if (!this.#shape || this.#still) {
1449
+ this.#needsReset = true;
1450
+ }
1451
+ this.#shape = this.#pending;
1452
+ this.#pending = null;
1453
+ }
1454
+ this.#updatePalette(ctx);
1455
+ const ready = !!(this.#shape &&
1456
+ this.#sdfTex &&
1457
+ this.#spawnTex &&
1458
+ this.#paletteTex);
1459
+ if (!ready) {
1460
+ this.#layers = NO_LAYERS;
1461
+ }
1462
+ else {
1463
+ // `count` grew past the state: reallocate it.
1464
+ if (r.count > this.#stateWidth ** 2) {
1465
+ this.#allocState(ctx, stateSizeFromCount(r.count), true);
1466
+ }
1467
+ this.#syncGeometry(ctx);
1468
+ if (this.#still && r.count !== this.#stillCount) {
1469
+ this.#needsReset = true;
1470
+ }
1471
+ // A still frame that's done is drawn again only when what it
1472
+ // shows changed.
1473
+ const keyChanged = this.#still && this.#updateStillKey(ctx, scene);
1474
+ if (!this.#still ||
1475
+ !this.#stillDone ||
1476
+ this.#needsReset ||
1477
+ keyChanged) {
1478
+ // Capped like the sim, so a stall doesn't teleport the
1479
+ // particles; the orbit animation and fireflies follow the
1480
+ // sim time.
1481
+ const dt = clampDt(ctx.deltaTime) || 0;
1482
+ this.#updateRepel(ctx, dt);
1483
+ this.#simulate(ctx, dt);
1484
+ this.#cam = this.#updateCamera(ctx, dt);
1485
+ const shadow = this.#drawParticles(ctx, scene, this.#cam);
1486
+ this.#layers = {
1487
+ bloom: this.#drawBloom(ctx, scene),
1488
+ shadow: shadow ? shadow.tex : null,
1489
+ shadowAmount: shadow ? shadow.amount : 0,
1490
+ };
1491
+ }
1492
+ }
1493
+ const layers = this.#layers;
1494
+ ctx.draw({
1495
+ frag: FRAG_COMPOSITE,
1496
+ uniforms: {
1497
+ scene,
1498
+ bloomTex: layers.bloom ?? scene,
1499
+ shadowTex: layers.shadow ?? scene,
1500
+ hasScene: ready ? 1 : 0,
1501
+ bloom: layers.bloom ? r.bloom : 0,
1502
+ shadowAmount: layers.shadow ? layers.shadowAmount : 0,
1503
+ shadowColor: r.shadowColor,
1504
+ },
1505
+ target: ctx.target,
1506
+ });
1507
+ }
1508
+ // Writes what the still frame depends on into #stillKey; true when
1509
+ // something changed.
1510
+ #updateStillKey(ctx, scene) {
1511
+ const r = this.#r;
1512
+ const key = this.#stillKey;
1513
+ let n = 0;
1514
+ let changed = false;
1515
+ const put = (v) => {
1516
+ if (key[n] !== v) {
1517
+ key[n] = v;
1518
+ changed = true;
1519
+ }
1520
+ n++;
1521
+ };
1522
+ for (const k of NUM_KEYS) {
1523
+ put(r[k]);
1524
+ }
1525
+ put(r.pad);
1526
+ put(r.shadow);
1527
+ put(r.light[0]);
1528
+ put(r.light[1]);
1529
+ put(r.light[2]);
1530
+ put(r.lightColor);
1531
+ put(r.fireflyColor);
1532
+ put(r.shadowColor);
1533
+ put(this.#paletteVersion);
1534
+ put(this.#stateWidth);
1535
+ put(scene.width);
1536
+ put(scene.height);
1537
+ put(this.#sceneSamples);
1538
+ // The camera frames the element in the output rect: with
1539
+ // "fullscreen" the scene keeps its size when the element doesn't.
1540
+ const dims = ctx.dims;
1541
+ const [ox, oy] = padOutputRect(r.pad, dims);
1542
+ put(dims.elementPixel[0]);
1543
+ put(dims.elementPixel[1]);
1544
+ put(ox);
1545
+ put(oy);
1546
+ // "viewport": the camera follows the element's place in it.
1547
+ put(r.cameraOrigin);
1548
+ if (r.cameraOrigin === "viewport") {
1549
+ put(dims.canvasRect[0] + dims.canvasRect[2] / 2);
1550
+ put(dims.canvasRect[1] + dims.canvasRect[3] / 2);
1551
+ }
1552
+ return changed;
1553
+ }
1554
+ /**
1555
+ * Capture the element's silhouette again, for when its content
1556
+ * changed: an image's `src` swapped, a text edit. For an `<img>`,
1557
+ * call it after `vfx.update(img)` has reloaded the image.
1558
+ *
1559
+ * The particles keep flowing over the current shape until the new
1560
+ * one has been read back (a few frames later), then flow onto it, and
1561
+ * respawn on it. A change of the element's aspect ratio is
1562
+ * recaptured automatically once its size settles; a change of size
1563
+ * alone needs none.
1564
+ *
1565
+ * Safe to call at any time and any number of times; a call while a
1566
+ * capture is pending restarts it, so the latest content wins.
1567
+ */
1568
+ refresh() {
1569
+ this.#source.refresh();
1570
+ }
1571
+ // Build the shape from the element read back. False when the
1572
+ // silhouette is empty, to try again later.
1573
+ // Build the shape from the element read back, in the worker. False
1574
+ // when the silhouette is empty, to try again later.
1575
+ async #onRead(r, isCurrent) {
1576
+ const channel = sourceChannelOf(this.params.sourceChannel);
1577
+ const built = await buildShapeAsync({
1578
+ pixels: r.pixels,
1579
+ mw: r.width,
1580
+ mh: r.height,
1581
+ channel,
1582
+ aspect: r.aspect,
1583
+ smoothRadius: 1,
1584
+ smoothPasses: 1,
1585
+ spawn: true,
1586
+ });
1587
+ if (!built) {
1588
+ return false;
1589
+ }
1590
+ if (!isCurrent()) {
1591
+ return true; // dropped by the capture anyway
1592
+ }
1593
+ this.#pending = built;
1594
+ this.#channel = channel;
1595
+ return true;
1596
+ }
1597
+ // Upload the SDF and spawn textures, replacing the current ones.
1598
+ // False (and the current ones kept) when the GPU can't make them; the
1599
+ // caller tries again next frame.
1600
+ #upload(ctx, shape) {
1601
+ const gl = ctx.gl;
1602
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
1603
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
1604
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
1605
+ // Half float so it can be filtered linearly everywhere.
1606
+ const sdf = gl.createTexture();
1607
+ const spawn = gl.createTexture();
1608
+ if (!sdf || !spawn) {
1609
+ gl.deleteTexture(sdf);
1610
+ gl.deleteTexture(spawn);
1611
+ ctx.resetState();
1612
+ return false;
1613
+ }
1614
+ gl.bindTexture(gl.TEXTURE_2D, sdf);
1615
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, shape.sdfW, shape.sdfH, 0, gl.RGBA, gl.FLOAT, shape.sdf);
1616
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
1617
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1618
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1619
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1620
+ gl.bindTexture(gl.TEXTURE_2D, spawn);
1621
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, SPAWN_SIZE, SPAWN_SIZE, 0, gl.RGBA, gl.FLOAT, shape.spawn);
1622
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
1623
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
1624
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1625
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1626
+ gl.bindTexture(gl.TEXTURE_2D, null);
1627
+ ctx.resetState();
1628
+ this.#deleteShapeTextures();
1629
+ this.#sdfRaw = sdf;
1630
+ this.#spawnRaw = spawn;
1631
+ this.#sdfTex = ctx.wrapTexture(sdf, {
1632
+ size: [shape.sdfW, shape.sdfH],
1633
+ filter: "linear",
1634
+ wrap: "clamp",
1635
+ });
1636
+ this.#spawnTex = ctx.wrapTexture(spawn, {
1637
+ size: [SPAWN_SIZE, SPAWN_SIZE],
1638
+ filter: "nearest",
1639
+ wrap: "clamp",
1640
+ });
1641
+ return true;
1642
+ }
1643
+ #deleteShapeTextures() {
1644
+ if (this.#gl) {
1645
+ for (const tex of [this.#sdfRaw, this.#spawnRaw]) {
1646
+ if (tex) {
1647
+ this.#gl.deleteTexture(tex);
1648
+ }
1649
+ }
1650
+ }
1651
+ this.#sdfRaw = null;
1652
+ this.#spawnRaw = null;
1653
+ this.#sdfTex = null;
1654
+ this.#spawnTex = null;
1655
+ }
1656
+ // Whether the palette is the one last baked, stops edited in place
1657
+ // included. No allocation when it is.
1658
+ #paletteUnchanged(palette) {
1659
+ if (palette !== this.#paletteSrc) {
1660
+ return false;
1661
+ }
1662
+ if (!Array.isArray(palette)) {
1663
+ return true;
1664
+ }
1665
+ const snap = this.#paletteSnap;
1666
+ if (snap.length !== palette.length * 2) {
1667
+ return false;
1668
+ }
1669
+ for (let i = 0; i < palette.length; i++) {
1670
+ const s = palette[i];
1671
+ if (snap[i * 2] !== s?.offset || snap[i * 2 + 1] !== s?.color) {
1672
+ return false;
1673
+ }
1674
+ }
1675
+ return true;
1676
+ }
1677
+ // Re-bake the gradient texture when the palette changes.
1678
+ #updatePalette(ctx) {
1679
+ const palette = this.params.palette;
1680
+ if (this.#paletteTex && this.#paletteUnchanged(palette)) {
1681
+ return;
1682
+ }
1683
+ const gl = ctx.gl;
1684
+ const texels = bakePalette(palette);
1685
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
1686
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
1687
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
1688
+ let tex = this.#paletteRaw;
1689
+ if (tex) {
1690
+ gl.bindTexture(gl.TEXTURE_2D, tex);
1691
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, PALETTE_SIZE, 1, gl.RGBA, gl.UNSIGNED_BYTE, texels);
1692
+ }
1693
+ else {
1694
+ tex = gl.createTexture();
1695
+ gl.bindTexture(gl.TEXTURE_2D, tex);
1696
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, PALETTE_SIZE, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, texels);
1697
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
1698
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1699
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1700
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1701
+ }
1702
+ gl.bindTexture(gl.TEXTURE_2D, null);
1703
+ ctx.resetState();
1704
+ if (!tex) {
1705
+ return;
1706
+ }
1707
+ if (!this.#paletteTex) {
1708
+ this.#paletteRaw = tex;
1709
+ this.#paletteTex = ctx.wrapTexture(tex, {
1710
+ size: [PALETTE_SIZE, 1],
1711
+ filter: "linear",
1712
+ wrap: "clamp",
1713
+ });
1714
+ }
1715
+ this.#paletteSrc = palette;
1716
+ this.#paletteSnap = Array.isArray(palette)
1717
+ ? palette.flatMap((s) => [s?.offset, s?.color])
1718
+ : [];
1719
+ this.#paletteVersion++;
1720
+ }
1721
+ // Body and firefly geometry for this frame's count and ratio.
1722
+ // Instance counts are fixed when a geometry is compiled, so a new
1723
+ // split gets new (small) geometries.
1724
+ #syncGeometry(ctx) {
1725
+ const r = this.#r;
1726
+ const nFire = Math.min(r.count, Math.round(r.count * r.fireflyRatio));
1727
+ const nBody = r.count - nFire;
1728
+ this.#nBody = nBody;
1729
+ this.#nFire = nFire;
1730
+ // The geometry is made for a bucket of counts, so animating `count`
1731
+ // or `fireflyRatio` does not rebuild it every frame.
1732
+ const bodyCap = instanceBucket(nBody);
1733
+ if (bodyCap !== this.#bodyCap) {
1734
+ if (this.#trails) {
1735
+ ctx.releaseGeometry(this.#trails);
1736
+ }
1737
+ if (this.#dots) {
1738
+ ctx.releaseGeometry(this.#dots);
1739
+ }
1740
+ if (this.#heads) {
1741
+ ctx.releaseGeometry(this.#heads);
1742
+ }
1743
+ this.#trails = trailGeometry(bodyCap);
1744
+ this.#dots = trailGeometry(bodyCap, 1);
1745
+ this.#heads = {
1746
+ attributes: { position: { data: QUAD_VERTS, itemSize: 2 } },
1747
+ instanceCount: bodyCap,
1748
+ };
1749
+ this.#bodyCap = bodyCap;
1750
+ }
1751
+ const fireCap = instanceBucket(nFire);
1752
+ if (fireCap !== this.#fireCap) {
1753
+ if (this.#fireflies) {
1754
+ ctx.releaseGeometry(this.#fireflies);
1755
+ }
1756
+ // One point per instance; the id is gl_InstanceID.
1757
+ this.#fireflies = {
1758
+ mode: "points",
1759
+ attributes: {},
1760
+ drawRange: { start: 0, count: 1 },
1761
+ instanceCount: fireCap,
1762
+ };
1763
+ this.#fireCap = fireCap;
1764
+ }
1765
+ }
1766
+ #shapeUniforms() {
1767
+ const halfDepth = this.#r.depth / 2;
1768
+ const uniforms = {
1769
+ sdfRect: this.#shape?.sdfRect ?? [0, 0, 1, 1],
1770
+ halfDepth,
1771
+ rounding: Math.min(0.025, halfDepth * 0.5),
1772
+ stateWidth: this.#stateWidth,
1773
+ spawnSize: SPAWN_SIZE,
1774
+ spawnSideShare: spawnSideShare(this.#shape, halfDepth),
1775
+ };
1776
+ if (this.#sdfTex && this.#spawnTex) {
1777
+ uniforms.sdfTex = this.#sdfTex;
1778
+ uniforms.spawnTex = this.#spawnTex;
1779
+ }
1780
+ return uniforms;
1781
+ }
1782
+ // Once a frame: ease the repel force towards what the pointer asks
1783
+ // for, over the frame's dt (the one the sim advances by).
1784
+ // Frame-rate independent.
1785
+ #updateRepel(ctx, dt) {
1786
+ const r = this.#r;
1787
+ const repel = !this.#still && r.pointer === "repel" && this.#camera.pointerIn;
1788
+ if (repel) {
1789
+ const [mx, my] = ctx.mouse;
1790
+ if (this.#repelAmount <= 0) {
1791
+ // Appear under the pointer, not sweep in from where it left.
1792
+ this.#repelPos = [mx, my];
1793
+ }
1794
+ else {
1795
+ const k = 1 - Math.exp(-dt / REPEL_POS_TAU);
1796
+ this.#repelPos[0] += (mx - this.#repelPos[0]) * k;
1797
+ this.#repelPos[1] += (my - this.#repelPos[1]) * k;
1798
+ }
1799
+ }
1800
+ const step = dt / REPEL_FADE;
1801
+ this.#repelAmount = repel
1802
+ ? Math.min(this.#repelAmount + step, 1)
1803
+ : Math.max(this.#repelAmount - step, 0);
1804
+ }
1805
+ // The camera at the current sim time; eases the "tilt" over `dt`.
1806
+ // Also keeps the window listeners in step with `drag` and `pointer`.
1807
+ #updateCamera(ctx, dt) {
1808
+ const r = this.#r;
1809
+ return this.#camera.update(r, {
1810
+ time: this.#simTime,
1811
+ dt,
1812
+ dims: ctx.dims,
1813
+ outputRect: padOutputRect(r.pad, ctx.dims),
1814
+ mouse: ctx.mouse,
1815
+ mouseViewport: ctx.mouseViewport,
1816
+ pixelRatio: ctx.pixelRatio,
1817
+ live: !this.#still,
1818
+ tilt: r.pointer === "tilt",
1819
+ trackPointer: r.pointer !== "none",
1820
+ });
1821
+ }
1822
+ // Uniforms of every FRAG_SIM draw but the state and the step.
1823
+ #simUniforms(ctx) {
1824
+ const r = this.#r;
1825
+ const spawn = this.#spawnTex;
1826
+ return {
1827
+ ...this.#shapeUniforms(),
1828
+ count: r.count,
1829
+ lifeMean: r.life,
1830
+ // A still frame is simulated from scratch: no intro.
1831
+ intro: this.#still ? 0 : r.intro,
1832
+ swirl: r.swirl,
1833
+ curl: r.curl,
1834
+ curlScale: r.curlScale,
1835
+ speed: r.speed,
1836
+ attract: r.attract,
1837
+ peel: r.peel,
1838
+ time: this.#simTime,
1839
+ halfState: this.#halfState ? 1 : 0,
1840
+ frameSeed: this.#steps,
1841
+ // Only read when the state grows; bound to something that is
1842
+ // never the target.
1843
+ ...(spawn ? { oldPosTex: spawn, oldVelTex: spawn } : {}),
1844
+ oldWidth: 1,
1845
+ oldCount: 0,
1846
+ ...this.#repelUniforms(ctx),
1847
+ };
1848
+ }
1849
+ // Advance the simulation by `frameDt` seconds (already clamped), in
1850
+ // steps of at most SIM_STEP.
1851
+ #simulate(ctx, frameDt) {
1852
+ const pos = this.#pos;
1853
+ const vel = this.#vel;
1854
+ if (!pos || !vel || !this.#sdfTex || !this.#spawnTex) {
1855
+ return;
1856
+ }
1857
+ let steps;
1858
+ let dt;
1859
+ if (this.#still) {
1860
+ this.#frameDt = 0;
1861
+ if (this.#stillDone && !this.#needsReset) {
1862
+ return;
1863
+ }
1864
+ steps = STILL_STEPS;
1865
+ dt = 1 / 60;
1866
+ }
1867
+ else {
1868
+ this.#frameDt = frameDt;
1869
+ // At least one pass, as a frame with dt 0 always ran one.
1870
+ steps = clamp(Math.ceil(frameDt / SIM_STEP), 1, MAX_SUBSTEPS);
1871
+ dt = frameDt / steps;
1872
+ }
1873
+ if (this.#needsReset && this.#still) {
1874
+ // Each still starts from the same clock, so it shows the same
1875
+ // view and flow. The orbit camera follows the clock back.
1876
+ this.#simTime = 0;
1877
+ this.#steps = 0;
1878
+ }
1879
+ const base = this.#simUniforms(ctx);
1880
+ if (this.#needsReset) {
1881
+ ctx.draw({
1882
+ frag: FRAG_SIM,
1883
+ uniforms: {
1884
+ ...base,
1885
+ posTex: pos,
1886
+ velTex: vel,
1887
+ reset: 1,
1888
+ dt: 0,
1889
+ },
1890
+ target: [pos, vel],
1891
+ });
1892
+ this.#resetHistory(ctx);
1893
+ this.#needsReset = false;
1894
+ this.#shadowHistValid = false;
1895
+ }
1896
+ // The trail history is pushed per step: it is spaced by sim time,
1897
+ // so trails look the same whatever the frame rate.
1898
+ for (let i = 0; i < steps; i++) {
1899
+ this.#simTime += dt;
1900
+ this.#steps = (this.#steps + 1) % 0x100000;
1901
+ ctx.draw({
1902
+ frag: FRAG_SIM,
1903
+ uniforms: {
1904
+ ...base,
1905
+ posTex: pos,
1906
+ velTex: vel,
1907
+ reset: 0,
1908
+ time: this.#simTime,
1909
+ frameSeed: this.#steps,
1910
+ dt,
1911
+ },
1912
+ target: [pos, vel],
1913
+ });
1914
+ this.#pushHistory(ctx);
1915
+ }
1916
+ this.#stillDone = true;
1917
+ this.#stillCount = this.#r.count;
1918
+ }
1919
+ // Sim uniforms for "repel": the pointer in buffer px and the camera
1920
+ // of the last drawn frame, which is where the user sees the particles.
1921
+ #repelUniforms(ctx) {
1922
+ const r = this.#r;
1923
+ const cam = this.#cam ?? this.#updateCamera(ctx, 0);
1924
+ return {
1925
+ repelForce: r.pointerRadius > 0 ? this.#repelAmount * r.pointerStrength : 0,
1926
+ repelPx: [
1927
+ this.#repelPos[0] - cam.origin[0],
1928
+ this.#repelPos[1] - cam.origin[1],
1929
+ ],
1930
+ repelRadiusPx: Math.max(r.pointerRadius * ctx.pixelRatio, 1),
1931
+ viewRot: cam.viewRot,
1932
+ camDist: cam.camDist,
1933
+ focalPx: cam.focalPx,
1934
+ eye: [cam.eye[0], cam.eye[1]],
1935
+ principalPx: cam.principalPx,
1936
+ };
1937
+ }
1938
+ // Mark every history slot stale: a generation no particle has.
1939
+ #resetHistory(ctx) {
1940
+ if (this.#hist) {
1941
+ ctx.draw({ frag: FRAG_HIST_CLEAR, target: this.#hist });
1942
+ }
1943
+ this.#histTime.fill(-Infinity);
1944
+ this.#histHead = TRAIL_SAMPLES - 1;
1945
+ this.#lastPush = -Infinity;
1946
+ }
1947
+ // Copy the positions into the next history slot once the sim has run
1948
+ // streak / (TRAIL_SAMPLES - 1) seconds since the last copy. Then the
1949
+ // history always spans at least `streak`, and the trail can be cut
1950
+ // at exactly `streak` seconds, frame-rate independent.
1951
+ #pushHistory(ctx) {
1952
+ const hist = this.#hist;
1953
+ const pos = this.#pos;
1954
+ const vel = this.#vel;
1955
+ const streak = this.#r.streak;
1956
+ if (!hist || !pos || !vel || !(streak > 0)) {
1957
+ return;
1958
+ }
1959
+ const interval = streak / (TRAIL_SAMPLES - 1);
1960
+ if (this.#simTime - this.#lastPush < interval) {
1961
+ return;
1962
+ }
1963
+ const slot = (this.#histHead + 1) % TRAIL_SAMPLES;
1964
+ const col = slot % HIST_COLS;
1965
+ const row = Math.floor(slot / HIST_COLS);
1966
+ const ndcX = (2 * col) / HIST_COLS - 1;
1967
+ const ndcY = (2 * row) / HIST_ROWS - 1;
1968
+ const sw = this.#stateWidth;
1969
+ ctx.draw({
1970
+ vert: VERT_HIST,
1971
+ frag: FRAG_HIST_PUSH,
1972
+ geometry: this.#slotQuad,
1973
+ uniforms: {
1974
+ posTex: pos,
1975
+ velTex: vel,
1976
+ slotRect: [
1977
+ ndcX,
1978
+ ndcY,
1979
+ ndcX + 2 / HIST_COLS,
1980
+ ndcY + 2 / HIST_ROWS,
1981
+ ],
1982
+ slotOrigin: [col * sw, row * sw],
1983
+ },
1984
+ target: hist,
1985
+ });
1986
+ this.#histHead = slot;
1987
+ this.#histTime[slot] = this.#simTime;
1988
+ this.#lastPush = this.#simTime;
1989
+ }
1990
+ // Uniforms that let a vertex walk the history, newest sample first.
1991
+ #historyUniforms() {
1992
+ const histSlot = [];
1993
+ const histAgo = [];
1994
+ for (let j = 0; j < TRAIL_SAMPLES; j++) {
1995
+ const slot = (this.#histHead - j + TRAIL_SAMPLES * 2) % TRAIL_SAMPLES;
1996
+ const written = this.#histTime[slot];
1997
+ histSlot.push(slot);
1998
+ histAgo.push(Number.isFinite(written) ? this.#simTime - written : HIST_NEVER);
1999
+ }
2000
+ return { histSlot, histAgo };
2001
+ }
2002
+ // Draws the particles into `scene`, and the shadow coverage when the
2003
+ // shadow is on. Returns the blurred coverage and its opacity.
2004
+ #drawParticles(ctx, scene, cam) {
2005
+ const pos = this.#pos;
2006
+ const vel = this.#vel;
2007
+ const shape = this.#shape;
2008
+ const attrA = this.#attrA;
2009
+ const hist = this.#hist;
2010
+ const trails = this.#trails;
2011
+ const heads = this.#heads;
2012
+ const fireflies = this.#fireflies;
2013
+ if (!pos ||
2014
+ !vel ||
2015
+ !shape ||
2016
+ !this.#paletteTex ||
2017
+ !attrA ||
2018
+ !hist ||
2019
+ !trails ||
2020
+ !heads ||
2021
+ !fireflies) {
2022
+ return null;
2023
+ }
2024
+ const r = this.#r;
2025
+ const camDist = cam.camDist;
2026
+ const uniforms = {
2027
+ ...this.#shapeUniforms(),
2028
+ posTex: pos,
2029
+ viewRot: cam.viewRot,
2030
+ camDist,
2031
+ camNear: camDist * NEAR_K,
2032
+ camFar: camDist * FAR_K,
2033
+ focalPx: cam.focalPx,
2034
+ eye: [cam.eye[0], cam.eye[1]],
2035
+ principalPx: cam.principalPx,
2036
+ bufferPx: [scene.width, scene.height],
2037
+ pointPx: r.pointSize * ctx.pixelRatio,
2038
+ };
2039
+ ctx.clear(scene);
2040
+ if (this.#nBody > 0) {
2041
+ // Colour per particle, shared by all segments of its trail.
2042
+ ctx.draw({
2043
+ frag: FRAG_ATTR,
2044
+ uniforms: {
2045
+ ...uniforms,
2046
+ velTex: vel,
2047
+ count: r.count,
2048
+ time: this.#simTime,
2049
+ speed: r.speed,
2050
+ paletteTex: this.#paletteTex,
2051
+ src: ctx.src,
2052
+ shapeHalf: shape.shapeHalf,
2053
+ sourceColor: r.sourceColor,
2054
+ lightDir: r.light,
2055
+ lightColor: r.lightColor,
2056
+ lightIntensity: r.lightIntensity,
2057
+ ambient: r.ambient,
2058
+ occlusion: r.occlusion,
2059
+ glintAmount: r.glint,
2060
+ },
2061
+ target: attrA,
2062
+ });
2063
+ }
2064
+ const trail = {
2065
+ ...uniforms,
2066
+ ...this.#historyUniforms(),
2067
+ velTex: vel,
2068
+ histTex: hist,
2069
+ attrA,
2070
+ idOffset: this.#nFire,
2071
+ idEnd: r.count,
2072
+ streak: r.streak,
2073
+ };
2074
+ if (this.#nBody > 0) {
2075
+ ctx.draw({
2076
+ vert: VERT_BODY,
2077
+ frag: FRAG_BODY,
2078
+ // Without a streak only the head segment draws.
2079
+ geometry: r.streak > 0 ? trails : (this.#dots ?? trails),
2080
+ uniforms: trail,
2081
+ target: scene,
2082
+ depthTest: true,
2083
+ blend: "none",
2084
+ });
2085
+ }
2086
+ if (this.#nFire > 0) {
2087
+ ctx.draw({
2088
+ vert: VERT_FIREFLY,
2089
+ frag: FRAG_FIREFLY,
2090
+ geometry: fireflies,
2091
+ uniforms: {
2092
+ ...uniforms,
2093
+ idOffset: 0,
2094
+ idEnd: this.#nFire,
2095
+ time: this.#simTime,
2096
+ fireflyIntensity: r.fireflyIntensity,
2097
+ fireflyColor: r.fireflyColor,
2098
+ },
2099
+ target: scene,
2100
+ depthTest: true,
2101
+ depthWrite: false,
2102
+ blend: "additive",
2103
+ });
2104
+ }
2105
+ // A light from behind the page casts no shadow; fade it out as
2106
+ // the light grazes the page.
2107
+ const light = r.light;
2108
+ const amount = r.shadowOpacity * smoothstep(0, 0.25, light[2]);
2109
+ if (!r.shadow || !(amount > 0) || this.#nBody === 0) {
2110
+ // Free the targets; the average starts afresh when the
2111
+ // shadow comes back.
2112
+ this.#disposeShadow();
2113
+ return null;
2114
+ }
2115
+ const tex = this.#drawShadow(ctx, scene, heads, {
2116
+ ...trail,
2117
+ shadowLight: [light[0], light[1], Math.max(light[2], 0.02)],
2118
+ shadowDepth: r.shadowDistance - r.depth / 2,
2119
+ });
2120
+ return tex ? { tex, amount } : null;
2121
+ }
2122
+ // Level `i` of the shadow chain, allocated on first use.
2123
+ #shadowLevel(ctx, i) {
2124
+ const levels = this.#shadow;
2125
+ while (levels.length <= i) {
2126
+ const k = 2 ** levels.length;
2127
+ const opts = {
2128
+ size: [
2129
+ Math.max(1, Math.floor(this.#shadowBase[0] / k)),
2130
+ Math.max(1, Math.floor(this.#shadowBase[1] / k)),
2131
+ ],
2132
+ float: true,
2133
+ filter: "linear",
2134
+ };
2135
+ levels.push({
2136
+ a: ctx.createRenderTarget(opts),
2137
+ b: ctx.createRenderTarget(opts),
2138
+ });
2139
+ }
2140
+ return levels[i];
2141
+ }
2142
+ // Planar projected shadow: the head capsule of each body particle,
2143
+ // flattened onto the page along the light, into a 1/2-res coverage
2144
+ // target. The coverage is averaged over the last frames, then
2145
+ // blurred on the coarsest level that holds the blur. Float targets
2146
+ // keep the soft falloff free of banding.
2147
+ #drawShadow(ctx, scene, heads, uniforms) {
2148
+ const bw = Math.max(1, Math.floor(scene.width / 2));
2149
+ const bh = Math.max(1, Math.floor(scene.height / 2));
2150
+ if (bw !== this.#shadowBase[0] || bh !== this.#shadowBase[1]) {
2151
+ this.#disposeShadow();
2152
+ this.#shadowBase = [bw, bh];
2153
+ }
2154
+ if (!this.#shadowHist) {
2155
+ this.#shadowHist = ctx.createRenderTarget({
2156
+ size: [bw, bh],
2157
+ float: true,
2158
+ persistent: true,
2159
+ filter: "linear",
2160
+ });
2161
+ }
2162
+ const hist = this.#shadowHist;
2163
+ const cover = this.#shadowLevel(ctx, 0).a;
2164
+ // The vertex shader works in scene px; NDC maps them onto this
2165
+ // smaller target as is.
2166
+ ctx.clear(cover);
2167
+ ctx.draw({
2168
+ vert: VERT_SHADOW,
2169
+ frag: FRAG_SHADOW_COVER,
2170
+ geometry: heads,
2171
+ uniforms,
2172
+ target: cover,
2173
+ blend: "none",
2174
+ });
2175
+ // Average out the holes that move from frame to frame. Frame-rate
2176
+ // independent; a still frame has nothing to average.
2177
+ const amount = this.#shadowHistValid && !this.#still
2178
+ ? 1 - Math.exp(-this.#frameDt / SHADOW_TAU)
2179
+ : 1;
2180
+ ctx.draw({
2181
+ frag: FRAG_SHADOW_ACCUM,
2182
+ uniforms: { curTex: cover, histTex: hist, amount },
2183
+ target: hist,
2184
+ });
2185
+ this.#shadowHistValid = true;
2186
+ // Blur sigma in scene px, then in texels of the chosen level.
2187
+ const sigmaPx = this.#r.shadowSoftness * ctx.pixelRatio;
2188
+ let level = 0;
2189
+ while (level < SHADOW_LEVELS - 1 &&
2190
+ sigmaPx / 2 ** (level + 1) > SHADOW_MAX_SIGMA) {
2191
+ level++;
2192
+ }
2193
+ // Level 0 of the chain is the average; level 0's `a` only holds
2194
+ // this frame's coverage.
2195
+ const levelSrc = (i) => i === 0 ? hist : this.#shadowLevel(ctx, i).a;
2196
+ for (let i = 1; i <= level; i++) {
2197
+ const src = levelSrc(i - 1);
2198
+ ctx.draw({
2199
+ frag: FRAG_BLOOM_DOWN,
2200
+ uniforms: {
2201
+ srcTex: src,
2202
+ texel: [1 / src.width, 1 / src.height],
2203
+ },
2204
+ target: this.#shadowLevel(ctx, i).a,
2205
+ });
2206
+ }
2207
+ const src = levelSrc(level);
2208
+ const sigma = sigmaPx / 2 ** (level + 1);
2209
+ if (sigma < 0.3) {
2210
+ return src;
2211
+ }
2212
+ const { a, b } = this.#shadowLevel(ctx, level);
2213
+ const radius = Math.min(Math.ceil(sigma * 3), 24);
2214
+ ctx.draw({
2215
+ frag: FRAG_SHADOW_BLUR,
2216
+ uniforms: { srcTex: src, dir: [1 / src.width, 0], sigma, radius },
2217
+ target: b,
2218
+ });
2219
+ ctx.draw({
2220
+ frag: FRAG_SHADOW_BLUR,
2221
+ uniforms: { srcTex: b, dir: [0, 1 / b.height], sigma, radius },
2222
+ target: a,
2223
+ });
2224
+ return a;
2225
+ }
2226
+ #disposeShadow() {
2227
+ for (const l of this.#shadow) {
2228
+ l.a.dispose();
2229
+ l.b.dispose();
2230
+ }
2231
+ this.#shadowHist?.dispose();
2232
+ this.#shadowHist = null;
2233
+ this.#shadowHistValid = false;
2234
+ this.#shadow = [];
2235
+ this.#shadowBase = [0, 0];
2236
+ }
2237
+ #disposeBloom() {
2238
+ if (this.#bloom) {
2239
+ disposeBloomPyramid(this.#bloom);
2240
+ }
2241
+ this.#bloom = null;
2242
+ this.#bloomBase = [0, 0];
2243
+ }
2244
+ // The bloom of the sparkles: prefilter, a downsample chain, then
2245
+ // upsample and add back. Null, and no targets kept, at `bloom` 0.
2246
+ // It has to happen here, not in a chained BloomEffect: the composite
2247
+ // soft-clips the HDR sparkles, so a later effect never sees them.
2248
+ #drawBloom(ctx, scene) {
2249
+ if (!(this.#r.bloom > 0)) {
2250
+ this.#disposeBloom();
2251
+ return null;
2252
+ }
2253
+ if (!this.#bloom ||
2254
+ scene.width !== this.#bloomBase[0] ||
2255
+ scene.height !== this.#bloomBase[1]) {
2256
+ this.#disposeBloom();
2257
+ this.#bloom = createBloomPyramid(ctx, scene.width, scene.height, BLOOM_LEVELS);
2258
+ this.#bloomBase = [scene.width, scene.height];
2259
+ }
2260
+ const down = this.#bloom.down;
2261
+ ctx.draw({
2262
+ frag: FRAG_BLOOM_PREFILTER,
2263
+ uniforms: {
2264
+ scene,
2265
+ texel: [1 / scene.width, 1 / scene.height],
2266
+ },
2267
+ target: down[0],
2268
+ });
2269
+ for (let i = 1; i < down.length; i++) {
2270
+ const src = down[i - 1];
2271
+ ctx.draw({
2272
+ frag: FRAG_BLOOM_DOWN,
2273
+ uniforms: {
2274
+ srcTex: src,
2275
+ texel: [1 / src.width, 1 / src.height],
2276
+ },
2277
+ target: down[i],
2278
+ });
2279
+ }
2280
+ return upsampleBloomPyramid(ctx, this.#bloom, (_i, small) => ({
2281
+ texelSize: [1 / small.width, 1 / small.height],
2282
+ weightLarge: 1,
2283
+ weightSmall: BLOOM_SCATTER,
2284
+ }));
2285
+ }
2286
+ dispose() {
2287
+ this.#camera.dispose();
2288
+ this.#unsubRestored?.();
2289
+ this.#unsubRestored = null;
2290
+ this.#deleteShapeTextures();
2291
+ if (this.#gl && this.#paletteRaw) {
2292
+ this.#gl.deleteTexture(this.#paletteRaw);
2293
+ }
2294
+ this.#paletteRaw = null;
2295
+ this.#paletteTex = null;
2296
+ this.#gl = null;
2297
+ this.#pos?.dispose();
2298
+ this.#vel?.dispose();
2299
+ this.#attrA?.dispose();
2300
+ this.#hist?.dispose();
2301
+ this.#scene?.dispose();
2302
+ this.#disposeShadow();
2303
+ this.#disposeBloom();
2304
+ this.#pos = null;
2305
+ this.#vel = null;
2306
+ this.#attrA = null;
2307
+ this.#hist = null;
2308
+ this.#scene = null;
2309
+ this.#stateWidth = 0;
2310
+ // The core frees the compiled geometry with the chain.
2311
+ this.#trails = null;
2312
+ this.#dots = null;
2313
+ this.#heads = null;
2314
+ this.#fireflies = null;
2315
+ this.#nBody = -1;
2316
+ this.#nFire = -1;
2317
+ this.#bodyCap = -1;
2318
+ this.#fireCap = -1;
2319
+ // Start over like a new instance if it is added again.
2320
+ this.#source.reset();
2321
+ this.#channel = null;
2322
+ this.#pending = null;
2323
+ this.#shape = null;
2324
+ this.#needsReset = true;
2325
+ this.#simTime = 0;
2326
+ this.#steps = 0;
2327
+ this.#stillDone = false;
2328
+ this.#stillCount = 0;
2329
+ this.#stillKey = [];
2330
+ this.#layers = NO_LAYERS;
2331
+ this.#frameDt = 0;
2332
+ this.#cam = null;
2333
+ this.#histTime.fill(-Infinity);
2334
+ this.#histHead = TRAIL_SAMPLES - 1;
2335
+ this.#lastPush = -Infinity;
2336
+ this.#paletteSrc = null;
2337
+ this.#paletteSnap = [];
2338
+ this.#repelPos = [0, 0];
2339
+ this.#repelAmount = 0;
2340
+ }
2341
+ }
2342
+ //# sourceMappingURL=swirl-particle.js.map