@heyramzi/ui 2.9.0 → 2.10.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 (312) hide show
  1. package/dist/agents/helpers.js +52 -0
  2. package/dist/agents/index.js +3 -0
  3. package/dist/agents/page-state.js +65 -0
  4. package/dist/agents/types.js +1 -0
  5. package/dist/agents/vendor/color.js +149 -0
  6. package/dist/agents/vendor/draw.js +548 -0
  7. package/dist/agents/vendor/engine.js +632 -0
  8. package/dist/agents/vendor/mesh.js +727 -0
  9. package/dist/agents/vendor/plastic.js +3273 -0
  10. package/dist/agents/vendor/presets.js +171 -0
  11. package/dist/agents/vendor/shapes.js +24 -0
  12. package/dist/agents/vendor/ticker.js +56 -0
  13. package/dist/agents/vendor/types.js +0 -0
  14. package/dist/agents/vendor/wear.js +1525 -0
  15. package/dist/ai/ai-button/index.js +2 -0
  16. package/dist/ai/confirmation/confirmation-context.svelte.js +12 -0
  17. package/dist/ai/confirmation/index.js +9 -0
  18. package/dist/ai/generation-progress/index.js +2 -0
  19. package/dist/ai/index.js +11 -0
  20. package/dist/ai/loading-state/index.js +3 -0
  21. package/dist/ai/prompt-input/index.js +6 -0
  22. package/dist/ai/reasoning/index.js +4 -0
  23. package/dist/ai/reasoning/reasoning-context.svelte.js +12 -0
  24. package/dist/ai/response/index.js +3 -0
  25. package/dist/ai/suggestion/index.js +3 -0
  26. package/dist/ai/thinking-dots/index.js +3 -0
  27. package/dist/ai/typing-dots/index.js +2 -0
  28. package/dist/ai/voice-waveform/index.js +3 -0
  29. package/dist/animations/text-swap.js +30 -0
  30. package/dist/brand/index.js +65 -0
  31. package/dist/canvas/card/resize.js +10 -0
  32. package/dist/canvas/edges/edge-path.js +284 -0
  33. package/dist/canvas/editor/context-menu.js +42 -0
  34. package/dist/canvas/editor/dispatch.svelte.js +157 -0
  35. package/dist/canvas/editor/editor-context.js +8 -0
  36. package/dist/canvas/editor/editor-mode.js +0 -0
  37. package/dist/canvas/editor/frame.js +87 -0
  38. package/dist/canvas/editor/hand-context.js +8 -0
  39. package/dist/canvas/editor/history-core.js +221 -0
  40. package/dist/canvas/editor/keymap.js +193 -0
  41. package/dist/canvas/editor/layout-actions.js +170 -0
  42. package/dist/canvas/editor/lock.js +65 -0
  43. package/dist/canvas/editor/rebase.js +279 -0
  44. package/dist/canvas/editor/style.js +174 -0
  45. package/dist/canvas/editor/text-field-context.js +8 -0
  46. package/dist/canvas/editor/z-order.js +82 -0
  47. package/dist/canvas/headless/alignment.js +20 -0
  48. package/dist/canvas/headless/break-apart-find.js +324 -0
  49. package/dist/canvas/headless/break-apart-pieces.js +74 -0
  50. package/dist/canvas/headless/break-apart-plan.js +378 -0
  51. package/dist/canvas/headless/break-apart-style.js +93 -0
  52. package/dist/canvas/headless/break-apart.js +206 -0
  53. package/dist/canvas/headless/canvas-cleanup.js +70 -0
  54. package/dist/canvas/headless/edge-anchors.js +134 -0
  55. package/dist/canvas/headless/edge-memo.js +28 -0
  56. package/dist/canvas/headless/edge-serialization.js +31 -0
  57. package/dist/canvas/headless/figure-geometry.js +331 -0
  58. package/dist/canvas/headless/fit-viewport.js +82 -0
  59. package/dist/canvas/headless/grid.js +96 -0
  60. package/dist/canvas/headless/icon-node.js +45 -0
  61. package/dist/canvas/headless/index.js +1 -0
  62. package/dist/canvas/headless/linkify.js +25 -0
  63. package/dist/canvas/headless/pen-marks.js +115 -0
  64. package/dist/canvas/headless/sketch.js +250 -0
  65. package/dist/canvas/headless/snap-guides.js +100 -0
  66. package/dist/canvas/headless/svg-figure.js +137 -0
  67. package/dist/canvas/headless/table-layout.js +161 -0
  68. package/dist/canvas/index.js +25 -0
  69. package/dist/canvas/lod/lod-mode.svelte.js +22 -0
  70. package/dist/canvas/lod/render-all.svelte.js +45 -0
  71. package/dist/canvas/ops/arrange.js +83 -0
  72. package/dist/canvas/ops/ops.js +318 -0
  73. package/dist/canvas/pen/colors.js +41 -0
  74. package/dist/canvas/pen/eraser.js +35 -0
  75. package/dist/canvas/pen/geometry.js +143 -0
  76. package/dist/canvas/pen/index.js +9 -0
  77. package/dist/canvas/pen/ops.js +15 -0
  78. package/dist/canvas/pen/pen-session.js +171 -0
  79. package/dist/canvas/pen/plugin.js +129 -0
  80. package/dist/canvas/pen/shapes.js +80 -0
  81. package/dist/canvas/pen/stroke-path.js +37 -0
  82. package/dist/canvas/pen/types.js +30 -0
  83. package/dist/canvas/pen/ui.js +20 -0
  84. package/dist/canvas/plugin.js +51 -0
  85. package/dist/canvas/screen/glyphs.js +24 -0
  86. package/dist/canvas/screen/labels.js +24 -0
  87. package/dist/canvas/screen/parts.js +140 -0
  88. package/dist/canvas/screen/plugin.js +71 -0
  89. package/dist/canvas/screen/types.js +44 -0
  90. package/dist/canvas/screen/ui.js +13 -0
  91. package/dist/canvas/test-utils.js +18 -0
  92. package/dist/canvas/types.js +0 -0
  93. package/dist/canvas/ui/forward-wheel.js +27 -0
  94. package/dist/canvas/ui/icons.js +133 -0
  95. package/dist/canvas/ui/place-panel.js +57 -0
  96. package/dist/canvas/ui/shortcut-tip.js +18 -0
  97. package/dist/canvas/ui/tools.js +411 -0
  98. package/dist/charts/dashboard/chart.js +1 -0
  99. package/dist/charts/dashboard/data-table.js +7 -0
  100. package/dist/charts/dashboard/index.js +13 -0
  101. package/dist/charts/dashboard/windows.js +5 -0
  102. package/dist/charts/edge-fade.js +7 -0
  103. package/dist/charts/hover-band.js +13 -0
  104. package/dist/charts/index.js +19 -0
  105. package/dist/charts/motion.js +15 -0
  106. package/dist/charts/primitives/chart-utils.js +22 -0
  107. package/dist/charts/primitives/index.js +4 -0
  108. package/dist/components/copy/clipboard.js +38 -0
  109. package/dist/components/copy/copied.svelte.js +26 -0
  110. package/dist/components/copy/index.js +3 -0
  111. package/dist/components/data-table/data-table.js +55 -0
  112. package/dist/components/data-table/index.js +7 -0
  113. package/dist/components/docs/index.js +3 -0
  114. package/dist/components/docs/scroll-spy.svelte.js +71 -0
  115. package/dist/components/field-row/index.js +1 -0
  116. package/dist/components/hold-to-confirm/index.js +1 -0
  117. package/dist/components/icon/index.js +1 -0
  118. package/dist/components/language-flag/index.js +1 -0
  119. package/dist/components/list-page/filter-section.js +4 -0
  120. package/dist/components/list-page/index.js +25 -0
  121. package/dist/components/list-page/list-table.js +5 -0
  122. package/dist/components/list-page/queue-rail.js +3 -0
  123. package/dist/components/list-page/section-host.js +8 -0
  124. package/dist/components/list-page/step.js +5 -0
  125. package/dist/components/list-page/types.js +0 -0
  126. package/dist/components/locale-switch/index.js +1 -0
  127. package/dist/components/mark/index.js +1 -0
  128. package/dist/components/open-in/index.js +1 -0
  129. package/dist/components/picker/index.js +3 -0
  130. package/dist/components/picker/trigger.js +33 -0
  131. package/dist/components/picker/types.js +0 -0
  132. package/dist/components/section-card/index.js +1 -0
  133. package/dist/components/section-header/index.js +2 -0
  134. package/dist/components/segmented/collapse.js +32 -0
  135. package/dist/components/segmented/index.js +8 -0
  136. package/dist/components/segmented/segmented.js +50 -0
  137. package/dist/components/star-rating-input/index.js +2 -0
  138. package/dist/components/svg/index.js +1 -0
  139. package/dist/components/table-view/index.js +1 -0
  140. package/dist/components/table-view/types.js +0 -0
  141. package/dist/components/tags-input/index.js +2 -0
  142. package/dist/components/tags-input/tags.js +90 -0
  143. package/dist/components/theme/index.js +3 -0
  144. package/dist/components/toolbar/index.js +3 -0
  145. package/dist/components/toolbar/toolbar-trigger.js +4 -0
  146. package/dist/editor/anchors.js +17 -0
  147. package/dist/editor/block-id.js +62 -0
  148. package/dist/editor/block-ops.js +105 -0
  149. package/dist/editor/doc-blocks.js +181 -0
  150. package/dist/editor/extensions.js +200 -0
  151. package/dist/editor/index.js +4 -0
  152. package/dist/editor/markdown-test-helpers.js +6 -0
  153. package/dist/editor/markdown.js +486 -0
  154. package/dist/editor/serializer.js +268 -0
  155. package/dist/editor/slash-items.js +104 -0
  156. package/dist/editor/youtube-url.js +27 -0
  157. package/dist/effects/animated-beam/index.js +1 -0
  158. package/dist/effects/animated-beam/path-calculator.svelte.js +42 -0
  159. package/dist/effects/animated-beam/types.js +0 -0
  160. package/dist/effects/border-beam/css.js +420 -0
  161. package/dist/effects/border-beam/presets.js +1792 -0
  162. package/dist/effects/border-beam/register-properties.js +39 -0
  163. package/dist/effects/border-beam/types.js +0 -0
  164. package/dist/effects/image-comparison/compare.js +4 -0
  165. package/dist/effects/image-reveal/index.js +1 -0
  166. package/dist/effects/image-reveal/types.js +0 -0
  167. package/dist/effects/magnetic/magnetic.js +54 -0
  168. package/dist/effects/reveal/index.js +4 -0
  169. package/dist/effects/reveal/observe.js +43 -0
  170. package/dist/effects/reveal/types.js +0 -0
  171. package/dist/effects/shader-gradient/color.js +34 -0
  172. package/dist/effects/shader-gradient/index.js +1 -0
  173. package/dist/effects/shader-gradient/renderer.js +320 -0
  174. package/dist/effects/shader-gradient/shaders.js +280 -0
  175. package/dist/effects/shader-gradient/types.js +0 -0
  176. package/dist/icons/apple-symbol-exports.js +126 -0
  177. package/dist/icons/sf-symbols.js +465 -0
  178. package/dist/icons/trades.js +34 -0
  179. package/dist/inbox/index.js +4 -0
  180. package/dist/inbox/logic.js +156 -0
  181. package/dist/inbox/types.js +46 -0
  182. package/dist/index.js +18 -0
  183. package/dist/lib/arrive.js +13 -0
  184. package/dist/lib/icon-types.js +0 -0
  185. package/dist/lib/initials.js +17 -0
  186. package/dist/lib/layout.js +1 -0
  187. package/dist/lib/panel.js +3 -0
  188. package/dist/lib/result.js +26 -0
  189. package/dist/lib/sliding-pill.js +45 -0
  190. package/dist/lib/theme-transition.js +53 -0
  191. package/dist/lib/tone.js +32 -0
  192. package/dist/lib/utils.js +5 -0
  193. package/dist/onboarding/define-tours.js +28 -0
  194. package/dist/onboarding/index.js +4 -0
  195. package/dist/onboarding/onboarding.svelte.js +15 -0
  196. package/dist/onboarding/pick.js +16 -0
  197. package/dist/onboarding/stores.js +50 -0
  198. package/dist/onboarding/types.js +1 -0
  199. package/dist/onboarding/wait.js +25 -0
  200. package/dist/panel/index.js +1 -0
  201. package/dist/panel/side-panel-types.js +0 -0
  202. package/dist/record/demo-data.js +512 -0
  203. package/dist/record/fields.js +148 -0
  204. package/dist/record/format.js +97 -0
  205. package/dist/record/gutter.js +1 -0
  206. package/dist/record/index.js +15 -0
  207. package/dist/record/kinds.js +110 -0
  208. package/dist/record/logic.js +255 -0
  209. package/dist/record/types.js +52 -0
  210. package/dist/shadcn/accordion/index.js +5 -0
  211. package/dist/shadcn/alert/index.js +5 -0
  212. package/dist/shadcn/alert-dialog/index.js +12 -0
  213. package/dist/shadcn/attachment/index.js +10 -0
  214. package/dist/shadcn/avatar/index.js +7 -0
  215. package/dist/shadcn/badge/index.js +2 -0
  216. package/dist/shadcn/breadcrumb/index.js +8 -0
  217. package/dist/shadcn/bubble/index.js +5 -0
  218. package/dist/shadcn/button/index.js +2 -0
  219. package/dist/shadcn/calendar/index.js +19 -0
  220. package/dist/shadcn/card/index.js +8 -0
  221. package/dist/shadcn/checkbox/index.js +2 -0
  222. package/dist/shadcn/code-block/index.js +9 -0
  223. package/dist/shadcn/collapsible/index.js +4 -0
  224. package/dist/shadcn/command/index.js +13 -0
  225. package/dist/shadcn/context-menu/index.js +17 -0
  226. package/dist/shadcn/dialog/index.js +11 -0
  227. package/dist/shadcn/dropdown-menu/index.js +19 -0
  228. package/dist/shadcn/dropdown-menu/menu-item.js +23 -0
  229. package/dist/shadcn/empty/index.js +7 -0
  230. package/dist/shadcn/field/index.js +12 -0
  231. package/dist/shadcn/hooks/highlighter.js +92 -0
  232. package/dist/shadcn/hooks/is-mobile.svelte.js +7 -0
  233. package/dist/shadcn/hooks/use-highlighted-code.svelte.js +65 -0
  234. package/dist/shadcn/hover-card/index.js +5 -0
  235. package/dist/shadcn/input/index.js +3 -0
  236. package/dist/shadcn/input-otp/index.js +5 -0
  237. package/dist/shadcn/kbd/index.js +3 -0
  238. package/dist/shadcn/label/index.js +2 -0
  239. package/dist/shadcn/marker/index.js +4 -0
  240. package/dist/shadcn/message/index.js +7 -0
  241. package/dist/shadcn/message-scroller/index.js +8 -0
  242. package/dist/shadcn/popover/index.js +9 -0
  243. package/dist/shadcn/progress/index.js +2 -0
  244. package/dist/shadcn/progress/tone.js +11 -0
  245. package/dist/shadcn/radio-group/index.js +3 -0
  246. package/dist/shadcn/scroll-area/index.js +3 -0
  247. package/dist/shadcn/select/index.js +12 -0
  248. package/dist/shadcn/separator/index.js +2 -0
  249. package/dist/shadcn/sheet/index.js +11 -0
  250. package/dist/shadcn/sidebar/constants.js +6 -0
  251. package/dist/shadcn/sidebar/context.svelte.js +38 -0
  252. package/dist/shadcn/sidebar/index.js +25 -0
  253. package/dist/shadcn/skeleton/index.js +2 -0
  254. package/dist/shadcn/slider/index.js +2 -0
  255. package/dist/shadcn/sonner/index.js +1 -0
  256. package/dist/shadcn/spinner/index.js +1 -0
  257. package/dist/shadcn/switch/index.js +2 -0
  258. package/dist/shadcn/table/index.js +9 -0
  259. package/dist/shadcn/tabs/index.js +5 -0
  260. package/dist/shadcn/textarea/index.js +2 -0
  261. package/dist/shadcn/toggle/index.js +3 -0
  262. package/dist/shadcn/toggle-group/index.js +3 -0
  263. package/dist/shadcn/tooltip/context.js +8 -0
  264. package/dist/shadcn/tooltip/index.js +6 -0
  265. package/dist/shadcn/trace/index.js +9 -0
  266. package/dist/slides/bridges.js +66 -0
  267. package/dist/slides/deck-controller.svelte.js +221 -0
  268. package/dist/slides/export/capture.js +81 -0
  269. package/dist/slides/export/ffmpeg.js +85 -0
  270. package/dist/slides/export/index.js +4 -0
  271. package/dist/slides/export/launch.js +8 -0
  272. package/dist/slides/export/pdf.js +78 -0
  273. package/dist/slides/export/types.js +0 -0
  274. package/dist/slides/index.js +7 -0
  275. package/dist/slides/labels.js +26 -0
  276. package/dist/slides/layout.js +47 -0
  277. package/dist/slides/model.js +9 -0
  278. package/dist/slides/navigation.js +89 -0
  279. package/dist/slides/types.js +6 -0
  280. package/dist/status/define-statuses.js +16 -0
  281. package/dist/status/index.js +4 -0
  282. package/dist/status/status-types.js +0 -0
  283. package/dist/test-setup.js +26 -0
  284. package/dist/thread/index.js +3 -0
  285. package/dist/thread/logic.js +94 -0
  286. package/dist/thread/types.js +23 -0
  287. package/dist/work/board-order.js +109 -0
  288. package/dist/work/dates.js +19 -0
  289. package/dist/work/detail/detail-types.js +20 -0
  290. package/dist/work/docs/edited.js +39 -0
  291. package/dist/work/docs/index.js +7 -0
  292. package/dist/work/docs/labels.js +38 -0
  293. package/dist/work/docs/menu.js +121 -0
  294. package/dist/work/docs/outline.js +39 -0
  295. package/dist/work/gantt/logic.js +260 -0
  296. package/dist/work/inbox/inbox-logic.js +94 -0
  297. package/dist/work/inbox/inbox-statuses.js +86 -0
  298. package/dist/work/inbox/inbox-types.js +0 -0
  299. package/dist/work/inbox/index.js +1 -0
  300. package/dist/work/index.js +21 -0
  301. package/dist/work/kanban-drag.js +25 -0
  302. package/dist/work/kanban-types.js +0 -0
  303. package/dist/work/presets/sales.js +35 -0
  304. package/dist/work/reorder.js +10 -0
  305. package/dist/work/task-types.js +0 -0
  306. package/dist/work/time/duration.js +60 -0
  307. package/dist/work/time/time-types.js +25 -0
  308. package/dist/work/use-kanban-dnd.svelte.js +128 -0
  309. package/package.json +45 -3
  310. package/src/components/toolbar/toolbar-trigger.ts +8 -2
  311. package/src/record/demo-data.ts +4 -4
  312. package/src/record/record-demo.svelte +2 -2
@@ -0,0 +1,3273 @@
1
+ import { parseColor } from "./color.js";
2
+ export const PAD = 3;
3
+ export const SPAN = 100 + 2 * PAD;
4
+ const M = 64;
5
+ export const MATCAP_SIZE = M;
6
+ const MM = M * M;
7
+ const CONIC_STOPS = 24;
8
+ const INF = 1e12;
9
+ const EL = 48 * Math.PI / 180;
10
+ const E_XY = Math.cos(EL), E_Z = Math.sin(EL);
11
+ function edt1d(f, n, d, s, v, z) {
12
+ let k = 0;
13
+ v[0] = 0;
14
+ z[0] = -1e30;
15
+ z[1] = 1e30;
16
+ for(let q = 1; q < n; q++){
17
+ let x = 0;
18
+ for(;;){
19
+ const vk = v[k];
20
+ x = (f[q] + q * q - f[vk] - vk * vk) / (2 * (q - vk));
21
+ if (x > z[k]) break;
22
+ k--;
23
+ }
24
+ k++;
25
+ v[k] = q;
26
+ z[k] = x;
27
+ z[k + 1] = 1e30;
28
+ }
29
+ k = 0;
30
+ for(let q = 0; q < n; q++){
31
+ while(z[k + 1] < q)k++;
32
+ const vk = v[k];
33
+ d[q] = (q - vk) * (q - vk) + f[vk];
34
+ s[q] = vk;
35
+ }
36
+ }
37
+ function edt2d(mask, site, N, out, near) {
38
+ const f = new Float32Array(N), d = new Float32Array(N), s = new Int32Array(N), v = new Int32Array(N), z = new Float32Array(N + 1);
39
+ const g = new Float32Array(N * N), row = new Int32Array(N * N);
40
+ for(let x = 0; x < N; x++){
41
+ for(let y = 0; y < N; y++)f[y] = mask[y * N + x] === site ? 0 : INF;
42
+ edt1d(f, N, d, s, v, z);
43
+ for(let y = 0; y < N; y++){
44
+ g[y * N + x] = d[y];
45
+ row[y * N + x] = s[y];
46
+ }
47
+ }
48
+ for(let y = 0; y < N; y++){
49
+ const o = y * N;
50
+ for(let x = 0; x < N; x++)f[x] = g[o + x];
51
+ edt1d(f, N, d, s, v, z);
52
+ for(let x = 0; x < N; x++){
53
+ out[o + x] = d[x];
54
+ if (near) near[o + x] = row[o + s[x]] * N + s[x];
55
+ }
56
+ }
57
+ }
58
+ function blur5(a, N, tmp) {
59
+ for(let y = 0; y < N; y++){
60
+ const o = y * N;
61
+ for(let x = 0; x < N; x++){
62
+ const x0 = x < 2 ? 0 : x - 2, x1 = x < 1 ? 0 : x - 1, x3 = x > N - 2 ? N - 1 : x + 1, x4 = x > N - 3 ? N - 1 : x + 2;
63
+ tmp[o + x] = (a[o + x0] + 4 * a[o + x1] + 6 * a[o + x] + 4 * a[o + x3] + a[o + x4]) * 0.0625;
64
+ }
65
+ }
66
+ for(let x = 0; x < N; x++){
67
+ for(let y = 0; y < N; y++){
68
+ const y0 = y < 2 ? 0 : y - 2, y1 = y < 1 ? 0 : y - 1, y3 = y > N - 2 ? N - 1 : y + 1, y4 = y > N - 3 ? N - 1 : y + 2;
69
+ a[y * N + x] = (tmp[y0 * N + x] + 4 * tmp[y1 * N + x] + 6 * tmp[y * N + x] + 4 * tmp[y3 * N + x] + tmp[y4 * N + x]) * 0.0625;
70
+ }
71
+ }
72
+ }
73
+ function poisson(cov, N, u) {
74
+ const levels = [];
75
+ for(let n = N, f = 1; f <= 4 && n % 2 === 0 || f === 1; n >>= 1, f <<= 1){
76
+ const mask = new Uint8Array(n * n);
77
+ for(let y = 0; y < n; y++){
78
+ for(let x = 0; x < n; x++){
79
+ let sum = 0;
80
+ for(let j = 0; j < f; j++)for(let i = 0; i < f; i++)sum += cov[(y * f + j) * N + x * f + i];
81
+ mask[y * n + x] = sum >= 128 * f * f ? 1 : 0;
82
+ }
83
+ }
84
+ levels.push({
85
+ n,
86
+ mask,
87
+ phi: new Float32Array(n * n)
88
+ });
89
+ if (f === 4) break;
90
+ }
91
+ const sweep = (L, s, iters, om)=>{
92
+ const { n, mask, phi } = L, s2 = s * s;
93
+ for(let it = 0; it < iters; it++){
94
+ for(let y = 1; y < n - 1; y++){
95
+ const o = y * n;
96
+ for(let x = 1; x < n - 1; x++){
97
+ const i = o + x;
98
+ if (!mask[i]) continue;
99
+ const v = (phi[i - 1] + phi[i + 1] + phi[i - n] + phi[i + n] + s2) * 0.25;
100
+ phi[i] += om * (v - phi[i]);
101
+ }
102
+ }
103
+ }
104
+ };
105
+ for(let l = levels.length - 1; l >= 0; l--){
106
+ const L = levels[l], f = 1 << l;
107
+ if (l < levels.length - 1) {
108
+ const C = levels[l + 1], n = L.n, cn = C.n;
109
+ for(let y = 0; y < n; y++){
110
+ const fy = Math.min(cn - 1, Math.max(0, (y + 0.5) / 2 - 0.5)), y0 = fy | 0, y1 = Math.min(cn - 1, y0 + 1), ty = fy - y0;
111
+ for(let x = 0; x < n; x++){
112
+ const i = y * n + x;
113
+ if (!L.mask[i]) continue;
114
+ const fx = Math.min(cn - 1, Math.max(0, (x + 0.5) / 2 - 0.5)), x0 = fx | 0, x1 = Math.min(cn - 1, x0 + 1), tx = fx - x0;
115
+ L.phi[i] = (C.phi[y0 * cn + x0] * (1 - tx) + C.phi[y0 * cn + x1] * tx) * (1 - ty) + (C.phi[y1 * cn + x0] * (1 - tx) + C.phi[y1 * cn + x1] * tx) * ty;
116
+ }
117
+ }
118
+ }
119
+ const om = Math.min(1.9, 2 / (1 + Math.sin(Math.PI / L.n)) - 0.05);
120
+ sweep(L, u * f, 4, 1);
121
+ sweep(L, u * f, l === 2 ? 100 : l === 1 ? 30 : 16, om);
122
+ sweep(L, u * f, 8, 1);
123
+ }
124
+ return levels[0].phi;
125
+ }
126
+ const DX = [
127
+ 1,
128
+ 1,
129
+ 0,
130
+ -1,
131
+ -1,
132
+ -1,
133
+ 0,
134
+ 1
135
+ ], DY = [
136
+ 0,
137
+ 1,
138
+ 1,
139
+ 1,
140
+ 0,
141
+ -1,
142
+ -1,
143
+ -1
144
+ ];
145
+ const DL = [
146
+ 1,
147
+ Math.SQRT2,
148
+ 1,
149
+ Math.SQRT2,
150
+ 1,
151
+ Math.SQRT2,
152
+ 1,
153
+ Math.SQRT2
154
+ ];
155
+ export function buildForm(cov, N, halfDepth) {
156
+ const u = SPAN / N, NN = N * N;
157
+ const mask = new Uint8Array(NN);
158
+ for(let i = 0; i < NN; i++)mask[i] = cov[i] >= 128 ? 1 : 0;
159
+ const dIn = new Float32Array(NN), dOut = new Float32Array(NN), nearIn = new Int32Array(NN);
160
+ edt2d(mask, 0, N, dIn, null);
161
+ edt2d(mask, 1, N, dOut, nearIn);
162
+ const sd = new Float32Array(NN), tmp = new Float32Array(NN);
163
+ for(let i = 0; i < NN; i++){
164
+ const a = cov[i] / 255;
165
+ sd[i] = u * (a > 0 && a < 1 ? a - 0.5 : mask[i] ? Math.sqrt(dIn[i]) - 0.5 : 0.5 - Math.sqrt(dOut[i]));
166
+ }
167
+ blur5(sd, N, tmp);
168
+ blur5(sd, N, tmp);
169
+ const phi = poisson(cov, N, u);
170
+ let phiMax = 0;
171
+ for(let i = 0; i < NN; i++)if (phi[i] > phiMax) phiMax = phi[i];
172
+ const rIn = 2 * Math.sqrt(phiMax);
173
+ const hMax = Math.min(1.5 * halfDepth + 0.32 * rIn, 1.4 * rIn);
174
+ const kh = phiMax > 0 ? hMax / Math.sqrt(phiMax) : 0;
175
+ const h = new Float32Array(NN);
176
+ for(let i = 0; i < NN; i++)h[i] = phi[i] > 0 ? kh * Math.sqrt(phi[i]) : 0;
177
+ blur5(h, N, tmp);
178
+ const form = {
179
+ N,
180
+ i00: new Uint16Array(NN),
181
+ wx: new Uint8Array(NN),
182
+ wy: new Uint8Array(NN),
183
+ ao: new Uint8Array(NN),
184
+ sd,
185
+ h
186
+ };
187
+ const { i00, wx, wy, ao } = form;
188
+ const STEPS = N <= 64 ? [
189
+ 1,
190
+ 2,
191
+ 3,
192
+ 5,
193
+ 8
194
+ ] : N <= 96 ? [
195
+ 1,
196
+ 2,
197
+ 4,
198
+ 7,
199
+ 11
200
+ ] : [
201
+ 1,
202
+ 2,
203
+ 4,
204
+ 7,
205
+ 11,
206
+ 15
207
+ ];
208
+ const halo = 64;
209
+ const last = N - 1;
210
+ for(let y = 0; y < N; y++){
211
+ for(let x = 0; x < N; x++){
212
+ const i = y * N + x;
213
+ const src = mask[i] ? i : dOut[i] <= halo ? nearIn[i] : -1;
214
+ if (src < 0) continue;
215
+ const sx = src % N, sy = (src - sx) / N;
216
+ const xl = sx > 0 ? sx - 1 : 0, xr = sx < last ? sx + 1 : last, yu = sy > 0 ? sy - 1 : 0, yd = sy < last ? sy + 1 : last;
217
+ let nx = -(h[sy * N + xr] - h[sy * N + xl]) / (2 * u);
218
+ let ny = -(h[yd * N + sx] - h[yu * N + sx]) / (2 * u);
219
+ let nz = 1;
220
+ let len = Math.sqrt(nx * nx + ny * ny + 1);
221
+ nx /= len;
222
+ ny /= len;
223
+ nz /= len;
224
+ const dd = Math.max(0, sd[src]);
225
+ if (dd < 2) {
226
+ let gx = sd[sy * N + xr] - sd[sy * N + xl], gy = sd[yd * N + sx] - sd[yu * N + sx];
227
+ const gl = Math.hypot(gx, gy) || 1;
228
+ gx /= gl;
229
+ gy /= gl;
230
+ const w = 0.7 * (1 - dd / 2);
231
+ nx += w * (-gx - nx);
232
+ ny += w * (-gy - ny);
233
+ nz += w * (0 - nz);
234
+ len = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;
235
+ nx /= len;
236
+ ny /= len;
237
+ nz /= len;
238
+ }
239
+ const h0 = h[src];
240
+ let occ = 0;
241
+ for(let d = 0; d < 8; d++){
242
+ let m = 0;
243
+ for(let k = 0; k < STEPS.length; k++){
244
+ const r = STEPS[k];
245
+ let qx = sx + DX[d] * r, qy = sy + DY[d] * r;
246
+ if (qx < 0) qx = 0;
247
+ else if (qx > last) qx = last;
248
+ if (qy < 0) qy = 0;
249
+ else if (qy > last) qy = last;
250
+ const t = (h[qy * N + qx] - h0) / (r * u * DL[d]);
251
+ if (t > m) m = t;
252
+ }
253
+ occ += m / Math.sqrt(1 + m * m);
254
+ }
255
+ const e = 1 - Math.min(1, dd / 3);
256
+ const edge = 1 - 0.2 * e * e;
257
+ const aoLin = Math.pow(1 - 0.9 * occ / 8, 1.5) * edge;
258
+ ao[i] = Math.max(1, Math.round(255 * Math.pow(aoLin, 1 / 2.2)));
259
+ const fx = (nx * 0.5 + 0.5) * (M - 1), fy = (ny * 0.5 + 0.5) * (M - 1);
260
+ const cx = Math.min(M - 2, Math.max(0, fx | 0)), cy = Math.min(M - 2, Math.max(0, fy | 0));
261
+ i00[i] = cy * M + cx;
262
+ wx[i] = Math.round(255 * Math.min(1, Math.max(0, fx - cx)));
263
+ wy[i] = Math.round(255 * Math.min(1, Math.max(0, fy - cy)));
264
+ }
265
+ }
266
+ return form;
267
+ }
268
+ function makeCanvas(n) {
269
+ if (typeof OffscreenCanvas === "function") return new OffscreenCanvas(n, n);
270
+ if (typeof document !== "undefined") {
271
+ const c = document.createElement("canvas");
272
+ c.width = c.height = n;
273
+ return c;
274
+ }
275
+ return null;
276
+ }
277
+ function ctx2d(c, readBack) {
278
+ return c.getContext("2d", readBack ? {
279
+ willReadFrequently: true
280
+ } : undefined);
281
+ }
282
+ export function rasterize(path, N) {
283
+ const c = makeCanvas(N);
284
+ const g = c && ctx2d(c, true);
285
+ if (!g) return null;
286
+ const u = SPAN / N;
287
+ g.setTransform(1 / u, 0, 0, 1 / u, PAD / u, PAD / u);
288
+ g.fillStyle = "#fff";
289
+ g.fill(path);
290
+ const px = g.getImageData(0, 0, N, N).data;
291
+ const cov = new Uint8ClampedArray(N * N);
292
+ for(let i = 0; i < N * N; i++)cov[i] = px[i * 4 + 3];
293
+ return cov;
294
+ }
295
+ const forms = new Map();
296
+ const pending = new Set();
297
+ const pathIds = new WeakMap();
298
+ let nextPathId = 0;
299
+ function pathId(p) {
300
+ let id = pathIds.get(p);
301
+ if (!id) pathIds.set(p, id = `p${nextPathId++}`);
302
+ return id;
303
+ }
304
+ const queue = [];
305
+ let scheduled = false;
306
+ const FORCED_MS = 6;
307
+ const clock = ()=>typeof performance !== "undefined" ? performance.now() : Date.now();
308
+ const HURRY_MS = 20;
309
+ let hurrying = 0;
310
+ function pump(deadline) {
311
+ scheduled = false;
312
+ const t0 = clock();
313
+ let fn = queue.shift();
314
+ while(fn){
315
+ fn();
316
+ const room = hurrying > 0 ? HURRY_MS - (clock() - t0) : deadline && !deadline.didTimeout ? deadline.timeRemaining() - 6 : FORCED_MS - (clock() - t0);
317
+ if (room <= 0) break;
318
+ fn = queue.shift();
319
+ }
320
+ if (queue.length) schedule();
321
+ }
322
+ function schedule() {
323
+ if (scheduled) return;
324
+ scheduled = true;
325
+ if (hurrying > 0) {
326
+ setTimeout(()=>pump(), 0);
327
+ return;
328
+ }
329
+ const ric = globalThis.requestIdleCallback;
330
+ if (ric) ric(pump, {
331
+ timeout: 60
332
+ });
333
+ else setTimeout(()=>pump(), 24);
334
+ }
335
+ export function hurry(on) {
336
+ hurrying = Math.max(0, hurrying + (on ? 1 : -1));
337
+ if (on && queue.length) setTimeout(()=>pump(), 0);
338
+ }
339
+ export function bakesPending() {
340
+ return pending.size > 0 || furPending.size > 0;
341
+ }
342
+ function idle(fn) {
343
+ queue.push(fn);
344
+ schedule();
345
+ }
346
+ const landed = new Set();
347
+ let missed = false;
348
+ let landing = false;
349
+ function land() {
350
+ if (landing || !landed.size) return;
351
+ landing = true;
352
+ setTimeout(()=>{
353
+ landing = false;
354
+ for (const fn of [
355
+ ...landed
356
+ ])fn();
357
+ }, 0);
358
+ }
359
+ export function onLanded(fn) {
360
+ landed.add(fn);
361
+ return ()=>{
362
+ landed.delete(fn);
363
+ };
364
+ }
365
+ export function takeMissed() {
366
+ const m = missed;
367
+ missed = false;
368
+ return m;
369
+ }
370
+ function formFor(key, path, N, halfDepth, sync) {
371
+ const id = `${key}|${N}|${Math.round(halfDepth)}`;
372
+ const hit = forms.get(id);
373
+ if (hit) return hit;
374
+ const build = ()=>{
375
+ pending.delete(id);
376
+ if (forms.has(id)) return;
377
+ const cov = rasterize(path, N);
378
+ if (!cov) return;
379
+ if (forms.size >= 48) forms.clear();
380
+ forms.set(id, buildForm(cov, N, halfDepth));
381
+ land();
382
+ };
383
+ if (sync) {
384
+ build();
385
+ return forms.get(id) ?? null;
386
+ }
387
+ if (!pending.has(id)) {
388
+ pending.add(id);
389
+ idle(build);
390
+ }
391
+ missed = true;
392
+ return null;
393
+ }
394
+ export function warmPlastic(key, path, devicePx = 192, depth = 0.65, fabric = false, style = FUR_STOCK, light = 295, lights) {
395
+ const form = formFor(key, path, tierFor(devicePx), 15 * depth, true);
396
+ if (fabric && form) {
397
+ const a = light * Math.PI / 180;
398
+ furReady(form, key, 15 * depth, Math.ceil(SPAN * devicePx / 100), false, Math.sin(a), -Math.cos(a), style, lights ?? stockLights(Math.sin(a), -Math.cos(a)));
399
+ }
400
+ }
401
+ export function tierFor(devicePx) {
402
+ return devicePx <= 100 ? 64 : devicePx <= 224 ? 96 : 128;
403
+ }
404
+ const reliefs = new WeakMap();
405
+ export function reliefFor(form, K, round) {
406
+ const q = Math.max(0.05, Math.min(1, round));
407
+ const key = `${K}|${q.toFixed(2)}`;
408
+ let byKey = reliefs.get(form);
409
+ if (!byKey) reliefs.set(form, byKey = new Map());
410
+ const hit = byKey.get(key);
411
+ if (hit) return hit;
412
+ const { N, h, sd } = form;
413
+ let top = 0;
414
+ for(let i = 0; i < N * N; i++)if (h[i] > top) top = h[i];
415
+ const u = SPAN / N;
416
+ const z = new Float32Array(K);
417
+ const scale = new Float32Array(K), cx = new Float32Array(K), cy = new Float32Array(K), inset = new Float32Array(K);
418
+ inset[0] = 1.2;
419
+ const measure = (inside)=>{
420
+ let n = 0, sx = 0, sy = 0;
421
+ for(let j = 0, i = 0; j < N; j++)for(let x = 0; x < N; x++, i++)if (inside(i)) n += 1, sx += x, sy += j;
422
+ return n ? [
423
+ n,
424
+ (sx / n + 0.5) * u - PAD,
425
+ (sy / n + 0.5) * u - PAD
426
+ ] : [
427
+ 0,
428
+ 50,
429
+ 50
430
+ ];
431
+ };
432
+ const [a0, x0, y0] = measure((i)=>sd[i] >= 0);
433
+ scale[0] = 1;
434
+ cx[0] = x0;
435
+ cy[0] = y0;
436
+ for(let k = 1; k < K; k++){
437
+ const s = k / K;
438
+ z[k] = top * s;
439
+ const tau = top * Math.pow(s, 1 / q);
440
+ const [ak, xk, yk] = measure((i)=>sd[i] >= 0 && h[i] >= tau);
441
+ scale[k] = Math.min(scale[k - 1], a0 ? Math.sqrt(ak / a0) : 1);
442
+ cx[k] = xk;
443
+ cy[k] = yk;
444
+ const band = top / (2 * K);
445
+ let n = 0, sum = 0;
446
+ for(let i = 0; i < N * N; i++){
447
+ if (sd[i] > 0 && Math.abs(h[i] - tau) < band) {
448
+ sum += sd[i];
449
+ n++;
450
+ }
451
+ }
452
+ inset[k] = Math.max(inset[k - 1], n ? sum / n : inset[k - 1]);
453
+ }
454
+ const r = {
455
+ top,
456
+ z,
457
+ q,
458
+ scale,
459
+ cx,
460
+ cy,
461
+ inset
462
+ };
463
+ if (byKey.size > 6) byKey.clear();
464
+ byKey.set(key, r);
465
+ return r;
466
+ }
467
+ export function surfaceAt(key, halfDepth, x, y, round = 1) {
468
+ const prefix = `${key}|`, suffix = `|${Math.round(halfDepth)}`;
469
+ let form = null;
470
+ for (const [id, f] of forms)if (id.startsWith(prefix) && id.endsWith(suffix)) form = f;
471
+ if (!form) return null;
472
+ const { N, h } = form;
473
+ const u = SPAN / N;
474
+ const fx = Math.min(N - 1, Math.max(0, (x + PAD) / u - 0.5)), fy = Math.min(N - 1, Math.max(0, (y + PAD) / u - 0.5));
475
+ const x0 = Math.min(N - 2, fx | 0), y0 = Math.min(N - 2, fy | 0), tx = fx - x0, ty = fy - y0, i = y0 * N + x0;
476
+ const v = (h[i] * (1 - tx) + h[i + 1] * tx) * (1 - ty) + (h[i + N] * (1 - tx) + h[i + N + 1] * tx) * ty;
477
+ let top = 0;
478
+ for(let k = 0; k < N * N; k++)if (h[k] > top) top = h[k];
479
+ if (top <= 0) return 0;
480
+ const q = Math.max(0.05, Math.min(1, round));
481
+ return top * Math.pow(Math.max(0, v) / top, q);
482
+ }
483
+ const toLin = (v)=>v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
484
+ const linCache = new Map();
485
+ function linearColor(color) {
486
+ let c = linCache.get(color);
487
+ if (!c) {
488
+ let rgb = parseColor(color);
489
+ if (!rgb) {
490
+ const cv = makeCanvas(1);
491
+ const g = cv && ctx2d(cv, true);
492
+ if (g) {
493
+ g.fillStyle = color;
494
+ g.fillRect(0, 0, 1, 1);
495
+ const p = g.getImageData(0, 0, 1, 1).data;
496
+ rgb = [
497
+ p[0],
498
+ p[1],
499
+ p[2]
500
+ ];
501
+ } else rgb = [
502
+ 128,
503
+ 128,
504
+ 128
505
+ ];
506
+ }
507
+ c = [
508
+ toLin(rgb[0] / 255),
509
+ toLin(rgb[1] / 255),
510
+ toLin(rgb[2] / 255)
511
+ ];
512
+ if (linCache.size > 200) linCache.clear();
513
+ linCache.set(color, c);
514
+ }
515
+ return c;
516
+ }
517
+ const TONE_N = 2048, TONE_MAX = 2.5, TONE_SCALE = TONE_N / TONE_MAX;
518
+ const toneLut = new Float32Array(TONE_N);
519
+ for(let i = 0; i < TONE_N; i++){
520
+ const v = (i + 0.5) / TONE_SCALE;
521
+ const y = v <= 0.75 ? v : 0.75 + 0.25 * (1 - Math.exp(-(v - 0.75) / 0.25));
522
+ toneLut[i] = 255 * (y <= 0.0031308 ? 12.92 * y : 1.055 * Math.pow(y, 1 / 2.4) - 0.055);
523
+ }
524
+ const tone = (v)=>toneLut[v <= 0 ? 0 : v >= TONE_MAX ? TONE_N - 1 : v * TONE_SCALE | 0];
525
+ const srgb = (v)=>v <= 0 ? 0 : v >= 1 ? 255 : 255 * (v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055);
526
+ const POW_N = 1024;
527
+ const powLuts = new Map();
528
+ function powLut(e) {
529
+ let t = powLuts.get(e);
530
+ if (!t) {
531
+ t = new Float32Array(POW_N + 1);
532
+ for(let i = 0; i <= POW_N; i++)t[i] = Math.pow(i / POW_N, e);
533
+ if (powLuts.size > 16) powLuts.clear();
534
+ powLuts.set(e, t);
535
+ }
536
+ return t;
537
+ }
538
+ function vivify(c, v, cap = Infinity) {
539
+ if (!(v > 0)) return;
540
+ const y = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
541
+ const mn = Math.min(c[0], c[1], c[2]), mx = Math.max(c[0], c[1], c[2]);
542
+ let k = 1 + 3 * v;
543
+ if (mn < y) k = Math.min(k, y / (y - mn));
544
+ if (mx > y) k = Math.min(k, Math.max(1, (cap - y) / (mx - y)));
545
+ c[0] = y + (c[0] - y) * k;
546
+ c[1] = y + (c[1] - y) * k;
547
+ c[2] = y + (c[2] - y) * k;
548
+ }
549
+ const ENV = [
550
+ 0.92,
551
+ 0.96,
552
+ 1.0
553
+ ];
554
+ const WARM = [
555
+ 1,
556
+ 0.98,
557
+ 0.95
558
+ ];
559
+ const smooth = (a, b, v)=>{
560
+ const t = v <= a ? 0 : v >= b ? 1 : (v - a) / (b - a);
561
+ return t * t * (3 - 2 * t);
562
+ };
563
+ const soft = (w, s, x)=>1 - smooth(w - s, w + s, x);
564
+ export function buildMatcap(out, c, f, p) {
565
+ const { L, V, H, U, W, A, B } = f;
566
+ const vivid = p.vivid ?? 0, px = [
567
+ 0,
568
+ 0,
569
+ 0
570
+ ];
571
+ const mx = Math.max(c[0], c[1], c[2], 0.05);
572
+ const tint = [
573
+ c[0] / mx,
574
+ c[1] / mx,
575
+ c[2] / mx
576
+ ];
577
+ const amb = Math.max(0.03, 0.3 - 0.15 * p.shadow);
578
+ const wrap = 0.15 + 0.14 * p.spread;
579
+ const kd = 0.85;
580
+ const e1 = Math.min(90, Math.round(110 / Math.pow(p.spread, 1.3))), e2 = Math.max(2, Math.round(8 / p.spread));
581
+ const lut1 = powLut(e1), lut2 = powLut(e2);
582
+ const ks1 = 0.45 * p.highlight, ks2 = 0.1 * p.highlight, winK = 0.11 * p.highlight, rimK = 0.3 * p.rim;
583
+ const ambT = [
584
+ amb * tint[0],
585
+ amb * tint[1],
586
+ amb * tint[2]
587
+ ];
588
+ for(let j = 0; j < M; j++){
589
+ for(let i = 0; i < M; i++){
590
+ let nx = i / (M - 1) * 2 - 1, ny = j / (M - 1) * 2 - 1;
591
+ let r2 = nx * nx + ny * ny;
592
+ if (r2 > 1.14) continue;
593
+ if (r2 > 1) {
594
+ const s = 1 / Math.sqrt(r2);
595
+ nx *= s;
596
+ ny *= s;
597
+ r2 = 1;
598
+ }
599
+ const nz = Math.sqrt(1 - r2);
600
+ const nl = nx * L[0] + ny * L[1] + nz * L[2];
601
+ const nv = Math.max(0, nx * V[0] + ny * V[1] + nz * V[2]);
602
+ const nh = Math.max(0, nx * H[0] + ny * H[1] + nz * H[2]);
603
+ const dif = Math.min(1, Math.max(0, (nl + wrap) / (1 + wrap)));
604
+ const q = 1 - nv, q2 = q * q, f3 = q2 * q, f5 = f3 * q2;
605
+ const ni = nh * POW_N | 0;
606
+ const spec = (ks1 * lut1[ni] + ks2 * lut2[ni]) * (1 + 3 * f5);
607
+ const rx = 2 * nv * nx - V[0], ry = 2 * nv * ny - V[1], rz = 2 * nv * nz - V[2];
608
+ const sky = 0.45 + 0.55 * smooth(-0.4, 0.6, rx * U[0] + ry * U[1] + rz * U[2]);
609
+ const rw = rx * W[0] + ry * W[1] + rz * W[2];
610
+ let win = 0;
611
+ if (rw > 0.5) {
612
+ const ra = (rx * A[0] + ry * A[1] + rz * A[2]) / rw, rb = (rx * B[0] + ry * B[1] + rz * B[2]) / rw;
613
+ win = soft(0.34, 0.12, Math.abs(ra)) * soft(0.12, 0.06, Math.abs(rb));
614
+ }
615
+ const env = rimK * f3 * sky + winK * win;
616
+ const k = (j * M + i) * 3;
617
+ px[0] = c[0] * (ambT[0] + kd * dif);
618
+ px[1] = c[1] * (ambT[1] + kd * dif);
619
+ px[2] = c[2] * (ambT[2] + kd * dif);
620
+ vivify(px, vivid);
621
+ out[k] = tone(px[0] + spec * WARM[0] + env * ENV[0]);
622
+ out[k + 1] = tone(px[1] + spec * WARM[1] + env * ENV[1]);
623
+ out[k + 2] = tone(px[2] + spec * WARM[2] + env * ENV[2]);
624
+ }
625
+ }
626
+ }
627
+ function sampleMatcap(mc, nx, ny, out) {
628
+ const fx = (nx * 0.5 + 0.5) * (M - 1), fy = (ny * 0.5 + 0.5) * (M - 1);
629
+ const cx = Math.min(M - 2, Math.max(0, fx | 0)), cy = Math.min(M - 2, Math.max(0, fy | 0));
630
+ const x = fx - cx, y = fy - cy, b = (cy * M + cx) * 3, R = M * 3;
631
+ const w00 = (1 - x) * (1 - y), w10 = x * (1 - y), w01 = (1 - x) * y, w11 = x * y;
632
+ for(let ch = 0; ch < 3; ch++)out[ch] = mc[b + ch] * w00 + mc[b + 3 + ch] * w10 + mc[b + R + ch] * w01 + mc[b + R + 3 + ch] * w11;
633
+ }
634
+ const lifts = new WeakMap();
635
+ export function edgeLift(form) {
636
+ let l = lifts.get(form);
637
+ if (l) return l;
638
+ const { N, sd, ao } = form;
639
+ l = {
640
+ ao: new Uint8Array(N * N),
641
+ k: new Float32Array(N * N)
642
+ };
643
+ for(let i = 0; i < N * N; i++){
644
+ if (ao[i] === 0) continue;
645
+ const dd = Math.max(0, sd[i]);
646
+ const e = 1 - Math.min(1, dd / 3);
647
+ const edge = 1 - 0.2 * e * e;
648
+ const lin = Math.min(1, Math.pow(ao[i] / 255, 2.2) / edge);
649
+ l.ao[i] = Math.max(1, Math.round(255 * Math.pow(lin, 1 / 2.2)));
650
+ l.k[i] = 1;
651
+ }
652
+ lifts.set(form, l);
653
+ return l;
654
+ }
655
+ const FRONT_FADE = 5;
656
+ export function shadeTexels(form, mc, px, aoMul, lift) {
657
+ const { N, i00, wx, wy, ao, sd } = form;
658
+ const R = M * 3;
659
+ for(let i = 0, k = 0; i < N * N; i++, k += 4){
660
+ const a = ao[i];
661
+ if (a === 0) {
662
+ px[k + 3] = 0;
663
+ continue;
664
+ }
665
+ let m = lift ? aoMul[lift.ao[i]] * lift.k[i] : aoMul[a];
666
+ const b = i00[i] * 3, x = wx[i] * (1 / 255), y = wy[i] * (1 / 255);
667
+ if (lift) {
668
+ const g = (mc[b + 1] * (1 - x) + mc[b + 4] * x) * (1 - y) + (mc[b + R + 1] * (1 - x) + mc[b + R + 4] * x) * y;
669
+ m = 1 - (1 - m) * (1.1 - 0.75 * Math.min(1, g / 235));
670
+ }
671
+ const w00 = (1 - x) * (1 - y) * m, w10 = x * (1 - y) * m, w01 = (1 - x) * y * m, w11 = x * y * m;
672
+ px[k] = mc[b] * w00 + mc[b + 3] * w10 + mc[b + R] * w01 + mc[b + R + 3] * w11;
673
+ px[k + 1] = mc[b + 1] * w00 + mc[b + 4] * w10 + mc[b + R + 1] * w01 + mc[b + R + 4] * w11;
674
+ px[k + 2] = mc[b + 2] * w00 + mc[b + 5] * w10 + mc[b + R + 2] * w01 + mc[b + R + 5] * w11;
675
+ const t = sd[i] / FRONT_FADE;
676
+ px[k + 3] = t >= 1 ? 255 : t <= 0 ? 0 : Math.round(255 * t * t * (3 - 2 * t));
677
+ }
678
+ }
679
+ const softs = new WeakMap();
680
+ function softMask(form, R) {
681
+ let byR = softs.get(form);
682
+ if (!byR) softs.set(form, byR = new Map());
683
+ if (byR.has(R)) return byR.get(R);
684
+ const c = makeCanvas(R), g = c && ctx2d(c, false);
685
+ if (!c || !g) {
686
+ byR.set(R, null);
687
+ return null;
688
+ }
689
+ const { N, sd } = form;
690
+ const img = new ImageData(R, R), px = R / SPAN;
691
+ for(let y = 0; y < R; y++){
692
+ for(let x = 0; x < R; x++){
693
+ const d = bilerp(sd, N, (x + 0.5) / px / SPAN * N - 0.5, (y + 0.5) / px / SPAN * N - 0.5);
694
+ const t = (d + 0.2) / 0.6;
695
+ const k = (y * R + x) * 4;
696
+ img.data[k] = img.data[k + 1] = img.data[k + 2] = 255;
697
+ img.data[k + 3] = t >= 1 ? 255 : t <= 0 ? 0 : Math.round(255 * t * t * (3 - 2 * t));
698
+ }
699
+ }
700
+ g.putImageData(img, 0, 0);
701
+ byR.set(R, c);
702
+ return c;
703
+ }
704
+ const rims = new WeakMap();
705
+ function rimOf(form) {
706
+ let r = rims.get(form);
707
+ if (r) return r;
708
+ const { N, sd } = form;
709
+ const u = SPAN / N;
710
+ const pts = [];
711
+ for(let j = 0, i = 0; j < N; j++)for(let x = 0; x < N; x++, i++)if (sd[i] >= 0 && sd[i] < 2 * u) pts.push((x + 0.5) * u - PAD, (j + 0.5) * u - PAD, sd[i]);
712
+ r = Float32Array.from(pts);
713
+ rims.set(form, r);
714
+ return r;
715
+ }
716
+ const rings = new WeakMap();
717
+ function ringOf(form) {
718
+ let r = rings.get(form);
719
+ if (r) return r;
720
+ const rim = rimOf(form);
721
+ const all = [];
722
+ let mx = 0, my = 0;
723
+ for(let i = 0; i < rim.length; i += 3)mx += rim[i], my += rim[i + 1];
724
+ mx /= rim.length / 3 || 1;
725
+ my /= rim.length / 3 || 1;
726
+ for(let i = 0; i < rim.length; i += 3)all.push([
727
+ rim[i],
728
+ rim[i + 1],
729
+ Math.atan2(rim[i + 1] - my, rim[i] - mx),
730
+ rim[i + 2]
731
+ ]);
732
+ all.sort((p, q)=>p[2] - q[2]);
733
+ const step = Math.max(1, Math.floor(all.length / 96));
734
+ const pts = all.filter((_, i)=>i % step === 0);
735
+ r = {
736
+ x: Float32Array.from(pts, (p)=>p[0]),
737
+ y: Float32Array.from(pts, (p)=>p[1]),
738
+ d: Float32Array.from(pts, (p)=>p[3])
739
+ };
740
+ rings.set(form, r);
741
+ return r;
742
+ }
743
+ function ringOrder(n, a, k) {
744
+ const start = Math.round((a + Math.PI) / (2 * Math.PI) * n);
745
+ const off = k & 1 ? k + 1 >> 1 : -(k >> 1);
746
+ return ((start + off) % n + n) % n;
747
+ }
748
+ function reach(rim, ex, ey) {
749
+ let m = -INF;
750
+ for(let i = 0; i < rim.length; i += 3){
751
+ const v = rim[i] * ex + rim[i + 1] * ey + rim[i + 2];
752
+ if (v > m) m = v;
753
+ }
754
+ return m;
755
+ }
756
+ const furs = new Map();
757
+ const furPending = new Set();
758
+ const lightBin = (lx, ly)=>Math.round(Math.atan2(ly, lx) / (Math.PI / 12));
759
+ const furTier = (capPx)=>capPx <= 200 ? 192 : capPx <= 360 ? 320 : 512;
760
+ export const FUR_STOCK = {
761
+ length: 1,
762
+ density: 1.6,
763
+ fuzz: 0.9,
764
+ clumps: 0.4,
765
+ curl: 0.7,
766
+ gravity: 0.9
767
+ };
768
+ const styleId = (s)=>`${s.length.toFixed(2)},${s.density.toFixed(2)},${s.fuzz.toFixed(2)},${s.clumps.toFixed(2)},${s.curl.toFixed(2)},${(s.gravity ?? FUR_STOCK.gravity).toFixed(2)}`;
769
+ const furKey = (key, halfDepth, R, bin, style, lights)=>`${key}|${Math.round(halfDepth)}|${R}|${bin}|${styleId(style)}|${lightsId(lights)}`;
770
+ const queuedFor = new Map();
771
+ const dropped = new Set();
772
+ const FUR_MID = 128;
773
+ const LOCK_COVER = 3.2;
774
+ const LOCK_DENSE = 1 / 2.56;
775
+ const LOCK_TAPER = 1 / (2 / 3 * Math.sqrt(1 / 3));
776
+ const LOCK_PART = 0.6;
777
+ const PILE_AMB = 0.07;
778
+ const PILE_FILL = 0.14;
779
+ const PILE_WRAP = 0.3;
780
+ const PILE_SHADOW = 0.6;
781
+ const PILE_AO = 0.35;
782
+ const PILE_ROOT = 0.3;
783
+ const PILE_KK = 0.3;
784
+ const PILE_GAIN = 1.26;
785
+ const PILE_LIFT = 1.6;
786
+ const PILE_BIAS = 0.045;
787
+ const PILE_STAND = 0.5;
788
+ const PILE_COS = Math.cos(PILE_STAND), PILE_SIN = Math.sin(PILE_STAND);
789
+ const PILE_CREASE = 0.22;
790
+ const PILE_H = 0.5;
791
+ const GAP_REF = 0.36;
792
+ const PILE_LIT = 1.1;
793
+ const TIP_TOP = 1.625;
794
+ const STRAND_TAPER = 0.5;
795
+ const STRAND_RISE = 0.9;
796
+ const STRAND_SHORT = 0.7;
797
+ const STRAND_CAST_H = 0.15;
798
+ const STRAND_CAST = 1.3;
799
+ const FILM_M = 20;
800
+ const FILM_SPAN = SPAN + 2 * FILM_M;
801
+ const HALO_A = 0.8;
802
+ const HALO_STREAK = 0.3;
803
+ const HALO_HAIR = 0.8;
804
+ const HALO_LIFT = 0.7;
805
+ const HALO_SHADE = 0.5;
806
+ const HALO_FIT = 2.5;
807
+ const HALO_CORE = 1.2;
808
+ const HALO_INSET = 3;
809
+ const HALO_VEIL = 0.8;
810
+ const HALO_BLEND = 0.3;
811
+ const HALO_EASE = 0.1;
812
+ const KEY_EL = 32 * Math.PI / 180;
813
+ const BACK_SOFT = 1;
814
+ function fabricKey(U, V, front = KEY_EL) {
815
+ const c = Math.cos(front), s = Math.sin(front);
816
+ return norm3([
817
+ U[0] * c + V[0] * s,
818
+ U[1] * c + V[1] * s,
819
+ U[2] * c + V[2] * s
820
+ ]);
821
+ }
822
+ function fabricBack(K, V) {
823
+ return norm3([
824
+ 0.94 * K[0] - 0.34 * V[0],
825
+ 0.94 * K[1] - 0.34 * V[1],
826
+ 0.94 * K[2] - 0.34 * V[2]
827
+ ]);
828
+ }
829
+ export function backFromKey(lx, ly) {
830
+ const x = -0.5 * lx, y = -0.5 * ly - 0.87;
831
+ const l = Math.hypot(x, y) || 1;
832
+ return [
833
+ x / l,
834
+ y / l
835
+ ];
836
+ }
837
+ export const stockLights = (lx, ly)=>{
838
+ const [bx, by] = backFromKey(lx, ly);
839
+ return {
840
+ bx,
841
+ by,
842
+ front: KEY_EL
843
+ };
844
+ };
845
+ const lightsId = (s)=>`${Math.round(Math.atan2(s.by, s.bx) / (Math.PI / 36))}|${Math.round(s.front / (Math.PI / 90))}`;
846
+ function hash2(x, y) {
847
+ let h = x * 374761393 + y * 668265263 | 0;
848
+ h = Math.imul(h ^ h >>> 13, 1274126177);
849
+ return ((h ^ h >>> 16) >>> 0) / 4294967296;
850
+ }
851
+ function bilerp(f, N, x, y) {
852
+ const last = N - 1;
853
+ const fx = Math.min(last, Math.max(0, x)), fy = Math.min(last, Math.max(0, y));
854
+ const x0 = Math.min(last - 1, fx | 0), y0 = Math.min(last - 1, fy | 0);
855
+ const tx = fx - x0, ty = fy - y0, i = y0 * N + x0;
856
+ return (f[i] * (1 - tx) + f[i + 1] * tx) * (1 - ty) + (f[i + N] * (1 - tx) + f[i + N + 1] * tx) * ty;
857
+ }
858
+ function furReady(form, key, halfDepth, capPx, sync, lx, ly, style = FUR_STOCK, lights = stockLights(lx, ly)) {
859
+ const R = furTier(capPx), bin = lightBin(lx, ly);
860
+ const id = furKey(key, halfDepth, R, bin, style, lights);
861
+ const hit = furs.get(id);
862
+ if (hit) return hit;
863
+ if (sync) return furFor(form, key, halfDepth, capPx, lx, ly, style, lights);
864
+ const prefix = `${key}|${Math.round(halfDepth)}|`;
865
+ if (R > 192 && !hurrying && !furPending.has(id)) {
866
+ let any = false;
867
+ for (const k of furs.keys())if (k.startsWith(prefix)) any = true;
868
+ for (const k of furPending)if (k.startsWith(prefix)) any = true;
869
+ if (!any) furReady(form, key, halfDepth, 1, false, lx, ly, style, lights);
870
+ }
871
+ if (!furPending.has(id)) {
872
+ furPending.add(id);
873
+ dropped.delete(id);
874
+ const shape = `${key}|${Math.round(halfDepth)}|${R}|${bin}`;
875
+ const line = queuedFor.get(shape) ?? [];
876
+ line.push(id);
877
+ while(line.length > 2){
878
+ const old = line.shift();
879
+ dropped.add(old);
880
+ furPending.delete(old);
881
+ }
882
+ queuedFor.set(shape, line);
883
+ const job = furJob(form, id, R, lx, ly, style, lights);
884
+ if (job) for (const step of job.steps)idle(()=>dropped.has(id) ? undefined : step());
885
+ }
886
+ missed = true;
887
+ for (const [k, f] of furs)if (k.startsWith(prefix)) return f;
888
+ return null;
889
+ }
890
+ function rng(seed) {
891
+ let a = seed >>> 0;
892
+ return ()=>{
893
+ a = a + 0x6d2b79f5 | 0;
894
+ let t = Math.imul(a ^ a >>> 15, 1 | a);
895
+ t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
896
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
897
+ };
898
+ }
899
+ function valueNoise(scale, seed) {
900
+ return (x, y)=>{
901
+ const fx = x / scale, fy = y / scale;
902
+ const x0 = Math.floor(fx), y0 = Math.floor(fy);
903
+ const tx = fx - x0, ty = fy - y0;
904
+ const sx = tx * tx * (3 - 2 * tx), sy = ty * ty * (3 - 2 * ty);
905
+ const h = (i, j)=>hash2(i + seed * 131, j - seed * 71);
906
+ const a = h(x0, y0), b = h(x0 + 1, y0), c = h(x0, y0 + 1), d = h(x0 + 1, y0 + 1);
907
+ return (a + (b - a) * sx) * (1 - sy) + (c + (d - c) * sx) * sy;
908
+ };
909
+ }
910
+ function hash3(x, y, z) {
911
+ let h = x * 374761393 + y * 668265263 + z * 1274126177 | 0;
912
+ h = Math.imul(h ^ h >>> 13, 1274126177);
913
+ return ((h ^ h >>> 16) >>> 0) / 4294967296;
914
+ }
915
+ function valueNoise3(scale, seed) {
916
+ return (x, y, z)=>{
917
+ const fx = x / scale, fy = y / scale, fz = z / scale;
918
+ const x0 = Math.floor(fx), y0 = Math.floor(fy), z0 = Math.floor(fz);
919
+ const tx = fx - x0, ty = fy - y0, tz = fz - z0;
920
+ const sx = tx * tx * (3 - 2 * tx), sy = ty * ty * (3 - 2 * ty), sz = tz * tz * (3 - 2 * tz);
921
+ const h = (i, j, k)=>hash3(i + seed * 131, j - seed * 71, k + seed * 17);
922
+ const lerp = (a, b, t)=>a + (b - a) * t;
923
+ const c00 = lerp(h(x0, y0, z0), h(x0 + 1, y0, z0), sx), c10 = lerp(h(x0, y0 + 1, z0), h(x0 + 1, y0 + 1, z0), sx);
924
+ const c01 = lerp(h(x0, y0, z0 + 1), h(x0 + 1, y0, z0 + 1), sx), c11 = lerp(h(x0, y0 + 1, z0 + 1), h(x0 + 1, y0 + 1, z0 + 1), sx);
925
+ return lerp(lerp(c00, c10, sy), lerp(c01, c11, sy), sz);
926
+ };
927
+ }
928
+ export function furFor(form, key = "custom", halfDepth = 9.75, capPx = 320, lx = -1, ly = 0, style = FUR_STOCK, lights = stockLights(lx, ly)) {
929
+ const R = furTier(capPx);
930
+ const id = furKey(key, halfDepth, R, lightBin(lx, ly), style, lights);
931
+ const hit = furs.get(id);
932
+ if (hit) return hit;
933
+ const job = furJob(form, id, R, lx, ly, style, lights);
934
+ if (job) for (const step of job.steps)step();
935
+ return furs.get(id) ?? {
936
+ R: 0,
937
+ mask: null,
938
+ haze: null,
939
+ cut: null,
940
+ core: null,
941
+ dark: null,
942
+ light: null,
943
+ glint: null,
944
+ back: null,
945
+ backSoft: null
946
+ };
947
+ }
948
+ function furJob(form, id, R, lx, ly, style = FUR_STOCK, lights = stockLights(lx, ly)) {
949
+ if (furs.has(id)) return null;
950
+ const file = (f)=>{
951
+ if (furs.size >= 24) furs.delete(furs.keys().next().value);
952
+ furs.set(id, f);
953
+ furPending.delete(id);
954
+ land();
955
+ };
956
+ const { N } = form;
957
+ const px = R / SPAN;
958
+ const at = (f, x, y)=>bilerp(f, N, (x + PAD) / SPAN * N - 0.5, (y + PAD) / SPAN * N - 0.5);
959
+ const sdAt = (x, y)=>at(form.sd, x, y);
960
+ const O = PAD + FILM_M, Rb = Math.round(R * FILM_SPAN / SPAN);
961
+ const fur = {
962
+ R,
963
+ mask: null,
964
+ haze: null,
965
+ cut: null,
966
+ core: null,
967
+ dark: null,
968
+ light: null,
969
+ glint: null,
970
+ back: null,
971
+ backSoft: null
972
+ };
973
+ const dc = makeCanvas(Rb), lc = makeCanvas(Rb), mc = makeCanvas(R), gc = makeCanvas(Rb), bc = makeCanvas(Rb), sc2 = makeCanvas(Rb);
974
+ const dg = dc && ctx2d(dc, false), lg = lc && ctx2d(lc, false), mg = mc && ctx2d(mc, false);
975
+ const gg = gc && ctx2d(gc, false), bg = bc && ctx2d(bc, false), sg2 = sc2 && ctx2d(sc2, false);
976
+ if (!dc || !lc || !mc || !gc || !bc || !sc2 || !dg || !lg || !mg || !gg || !bg || !sg2) {
977
+ file(fur);
978
+ return null;
979
+ }
980
+ const rand = rng(N * 7919 + 17);
981
+ const lean = valueNoise(3.4, 21), tone = valueNoise(22, 9);
982
+ const swirl = valueNoise(13, 57);
983
+ const { length: kLen, density: kDen, fuzz, clumps, curl } = style;
984
+ const gravity = style.gravity ?? FUR_STOCK.gravity;
985
+ const ll = Math.hypot(lx, ly) || 1;
986
+ const Lx = lx / ll, Ly = ly / ll;
987
+ const Uf = [
988
+ Lx,
989
+ Ly,
990
+ 0
991
+ ], Vf = [
992
+ 0,
993
+ 0,
994
+ 1
995
+ ], Df = [
996
+ 0,
997
+ 1,
998
+ 0
999
+ ];
1000
+ const K3 = fabricKey(Uf, Vf, lights.front), B3 = fabricBack([
1001
+ lights.bx,
1002
+ lights.by,
1003
+ 0
1004
+ ], Vf), H3 = norm3([
1005
+ K3[0],
1006
+ K3[1],
1007
+ K3[2] + 1
1008
+ ]);
1009
+ const F3 = norm3([
1010
+ -0.55 * Uf[0] + 0.85 * Vf[0] + 0.15 * Df[0],
1011
+ -0.55 * Uf[1] + 0.85 * Vf[1] + 0.15 * Df[1],
1012
+ -0.55 * Uf[2] + 0.85 * Vf[2] + 0.15 * Df[2]
1013
+ ]);
1014
+ let crownY = 12;
1015
+ for(let y = -PAD; y < 60; y += 0.5)if (sdAt(50, y) > 0.5) {
1016
+ crownY = y + 1;
1017
+ break;
1018
+ }
1019
+ crownY -= 18;
1020
+ const crownX = 50;
1021
+ const outward = (x, y)=>{
1022
+ const e = 0.8;
1023
+ const ox = sdAt(x - e, y) - sdAt(x + e, y), oy = sdAt(x, y - e) - sdAt(x, y + e);
1024
+ const ol = Math.hypot(ox, oy) || 1;
1025
+ return [
1026
+ ox / ol,
1027
+ oy / ol
1028
+ ];
1029
+ };
1030
+ const flow = (x, y)=>{
1031
+ let rx = x - crownX, ry = y - crownY;
1032
+ const rl = Math.hypot(rx, ry) || 1;
1033
+ rx /= rl;
1034
+ ry /= rl;
1035
+ const fall = gravity * (1 - 0.6 * Math.exp(-rl / 8));
1036
+ let dx = rx * (1 - 0.8 * fall), dy = ry * (1 - 0.8 * fall) + fall;
1037
+ const d0 = sdAt(x, y);
1038
+ if (d0 < 5) {
1039
+ const [ox, oy] = outward(x, y);
1040
+ const up = Math.max(0, -oy);
1041
+ const ex = ox * (1 - gravity * up), ey = oy * (1 - gravity * up) + 1.2 * gravity;
1042
+ const w = Math.max(0, Math.min(1, 1 - d0 / 5)) * 0.75;
1043
+ dx = dx * (1 - w) + ex * w * 1.6;
1044
+ dy = dy * (1 - w) + ey * w * 1.6;
1045
+ }
1046
+ const a = (lean(x, y) - 0.5) * (0.5 + 1.6 * curl) + (swirl(x, y) - 0.5) * (0.6 + curl);
1047
+ const ca = Math.cos(a), sa = Math.sin(a);
1048
+ const qx = dx * ca - dy * sa, qy = dx * sa + dy * ca;
1049
+ const m = Math.hypot(qx, qy) || 1;
1050
+ return [
1051
+ qx / m,
1052
+ qy / m
1053
+ ];
1054
+ };
1055
+ const steps = [];
1056
+ const rows = (n, chunk, fn)=>{
1057
+ for(let y0 = 0; y0 < n; y0 += chunk)steps.push(()=>fn(y0, Math.min(n, y0 + chunk)));
1058
+ };
1059
+ const hAt = (x, y)=>at(form.h, x, y);
1060
+ const slopeAt = (x, y)=>{
1061
+ const e = 0.6;
1062
+ const gx = (hAt(x + e, y) - hAt(x - e, y)) / (2 * e), gy = (hAt(x, y + e) - hAt(x, y - e)) / (2 * e);
1063
+ const m = Math.hypot(gx, gy);
1064
+ return [
1065
+ m > 1e-6 ? -gx / m : 0,
1066
+ m > 1e-6 ? -gy / m : 0,
1067
+ m / Math.sqrt(1 + m * m)
1068
+ ];
1069
+ };
1070
+ const G = 112, gu = FILM_SPAN / G;
1071
+ const flowX = new Float32Array(G * G), flowY = new Float32Array(G * G), tiltG = new Float32Array(G * G);
1072
+ const nXg = new Float32Array(G * G), nYg = new Float32Array(G * G), nZg = new Float32Array(G * G);
1073
+ const onG = new Float32Array(G * G);
1074
+ const pfX = new Float32Array(G * G), pfY = new Float32Array(G * G), foreG = new Float32Array(G * G);
1075
+ const hG = new Float32Array(G * G), creaseG = new Float32Array(G * G);
1076
+ const toneG = new Float32Array(G * G);
1077
+ rows(G, 28, (j0, j1)=>{
1078
+ for(let j = j0, i = j0 * G; j < j1; j++){
1079
+ for(let x = 0; x < G; x++, i++){
1080
+ const X = (x + 0.5) * gu - O, Y = (j + 0.5) * gu - O;
1081
+ let [fx, fy] = flow(X, Y);
1082
+ const [dx, dy, tiltS] = slopeAt(X, Y);
1083
+ const w = tiltS * (0.25 + 0.5 * (1 - gravity)) * (1 - gravity * Math.max(0, -dy));
1084
+ fx = fx * (1 - w) + dx * w;
1085
+ fy = fy * (1 - w) + (dy + 0.35 * gravity) * w;
1086
+ const fl = Math.hypot(fx, fy) || 1;
1087
+ fx /= fl;
1088
+ fy /= fl;
1089
+ flowX[i] = fx;
1090
+ flowY[i] = fy;
1091
+ tiltG[i] = tiltS;
1092
+ let nx = dx * tiltS, ny = dy * tiltS, nz = Math.sqrt(Math.max(0, 1 - tiltS * tiltS));
1093
+ const d0 = sdAt(X, Y);
1094
+ if (d0 < 2) {
1095
+ const [ox, oy] = outward(X, Y);
1096
+ const w = d0 >= 0 ? 0.7 * (1 - d0 / 2) : Math.min(1, 0.7 - 0.2 * d0);
1097
+ nx += w * (ox - nx);
1098
+ ny += w * (oy - ny);
1099
+ nz -= w * nz;
1100
+ const nl = Math.hypot(nx, ny, nz) || 1;
1101
+ nx /= nl;
1102
+ ny /= nl;
1103
+ nz /= nl;
1104
+ }
1105
+ nXg[i] = nx;
1106
+ nYg[i] = ny;
1107
+ nZg[i] = nz;
1108
+ onG[i] = smooth(-3.5, -1.5, d0);
1109
+ toneG[i] = tone(X, Y);
1110
+ hG[i] = d0 > 0 ? hAt(X, Y) : 0;
1111
+ const s = Math.sqrt(nx * nx + ny * ny), gx = s > 1e-4 ? nx / s : 0, gy = s > 1e-4 ? ny / s : 0;
1112
+ const a = fx * gx + fy * gy, k = (1 - Math.max(0, nz)) * a;
1113
+ let qx = PILE_COS * (fx - k * gx) + PILE_SIN * s * gx, qy = PILE_COS * (fy - k * gy) + PILE_SIN * s * gy;
1114
+ const att = smooth(-0.5, 1.5, d0);
1115
+ qx = fx + (qx - fx) * att;
1116
+ qy = fy + (qy - fy) * att;
1117
+ const ql = Math.sqrt(qx * qx + qy * qy) || 1;
1118
+ pfX[i] = qx / ql;
1119
+ pfY[i] = qy / ql;
1120
+ foreG[i] = 1 + (Math.min(1, ql) - 1) * att;
1121
+ }
1122
+ }
1123
+ });
1124
+ steps.push(()=>{
1125
+ const tmp = new Float32Array(G * G), avg = Float32Array.from(hG);
1126
+ for(let pass = 0; pass < 3; pass++){
1127
+ for(let y = 0, i = 0; y < G; y++)for(let x = 0; x < G; x++, i++){
1128
+ let acc = 0;
1129
+ for(let t = -2; t <= 2; t++)acc += avg[y * G + Math.min(G - 1, Math.max(0, x + t))];
1130
+ tmp[i] = acc / 5;
1131
+ }
1132
+ for(let y = 0, i = 0; y < G; y++)for(let x = 0; x < G; x++, i++){
1133
+ let acc = 0;
1134
+ for(let t = -2; t <= 2; t++)acc += tmp[Math.min(G - 1, Math.max(0, y + t)) * G + x];
1135
+ avg[i] = acc / 5;
1136
+ }
1137
+ }
1138
+ for(let j = 0, i = 0; j < G; j++)for(let x = 0; x < G; x++, i++){
1139
+ const inside = smooth(0.2, 2, sdAt((x + 0.5) * gu - O, (j + 0.5) * gu - O));
1140
+ const valley = smooth(0.3, 3.5, avg[i] - hG[i]) * inside;
1141
+ const under = smooth(0.15, 0.85, nYg[i]) * (1 - nZg[i]);
1142
+ creaseG[i] = Math.min(1, valley + 0.45 * under) * onG[i];
1143
+ }
1144
+ });
1145
+ const gridAt = (f, X, Y)=>bilerp(f, G, (X + O) / gu - 0.5, (Y + O) / gu - 0.5);
1146
+ const dirAt = (X, Y)=>{
1147
+ const ux = gridAt(pfX, X, Y), uy = gridAt(pfY, X, Y);
1148
+ const ul = Math.sqrt(ux * ux + uy * uy);
1149
+ return ul > 1e-4 ? [
1150
+ ux / ul,
1151
+ uy / ul
1152
+ ] : [
1153
+ 0,
1154
+ 1
1155
+ ];
1156
+ };
1157
+ const freeDirAt = (X, Y)=>{
1158
+ const ux = gridAt(flowX, X, Y), uy = gridAt(flowY, X, Y);
1159
+ const ul = Math.sqrt(ux * ux + uy * uy);
1160
+ return ul > 1e-4 ? [
1161
+ ux / ul,
1162
+ uy / ul
1163
+ ] : [
1164
+ 0,
1165
+ 1
1166
+ ];
1167
+ };
1168
+ const Rl = Rb >> 1, pl = Rl / FILM_SPAN;
1169
+ const lock = new Float32Array(Rl * Rl), lockLit = new Float32Array(Rl * Rl);
1170
+ const lockAt = (f, X, Y)=>bilerp(f, Rl, (X + O) * pl - 0.5, (Y + O) * pl - 0.5);
1171
+ const bump = valueNoise(0.7 + 0.35 * kLen, 61), swell = valueNoise(2.2 + 0.6 * kLen, 67);
1172
+ const tuftK = (0.2 + 0.5 * fuzz) * Math.min(1.8, kLen);
1173
+ const tuftAt = (X, Y)=>{
1174
+ const v = 0.3 * Math.max(-1.6, Math.min(1.6, lockAt(lock, X, Y))) / 1.6 + 0.25 * (bump(X, Y) - 0.5) * 2.4 + 0.45 * (swell(X, Y) - 0.5) * 2.4;
1175
+ return tuftK * Math.max(-1, Math.min(1, v));
1176
+ };
1177
+ const lockW = 0.8 + 0.45 * kLen;
1178
+ const lockWd = lockW * (0.65 + 0.4 * clumps), lockLd = 1.5 + 1.6 * kLen;
1179
+ const hMax = 0.32 * lockWd * (0.15 + 0.85 * clumps);
1180
+ const pileH = new Float32Array(Rb * Rb).fill(-0.25 * hMax), own = new Int32Array(Rb * Rb).fill(-1);
1181
+ const LP = 16;
1182
+ const cell = Math.sqrt(0.6 * lockWd * lockLd / LOCK_COVER / LOCK_DENSE);
1183
+ const gn = Math.ceil(FILM_SPAN / cell);
1184
+ const locks = new Float32Array(gn * gn * LP);
1185
+ let nLocks = 0;
1186
+ const stand0 = 0.45 - 0.2 * gravity;
1187
+ const tuftN = valueNoise3(1.6 * lockLd, 79), sizeN = valueNoise(9, 73);
1188
+ const tuftH = 1.5 * hMax * (0.25 + clumps);
1189
+ steps.push(()=>{
1190
+ for(let gy = 0; gy < gn; gy++)for(let gx = 0; gx < gn; gx++){
1191
+ const X = (gx + rand()) * cell - O, Y = (gy + rand()) * cell - O;
1192
+ const r1 = rand(), r2 = rand(), r3 = rand(), r4 = rand(), r5 = rand(), r6 = rand(), r7 = rand();
1193
+ let nx = gridAt(nXg, X, Y), ny = gridAt(nYg, X, Y), nz = gridAt(nZg, X, Y);
1194
+ const nl = Math.hypot(nx, ny, nz) || 1;
1195
+ nx /= nl;
1196
+ ny /= nl;
1197
+ nz /= nl;
1198
+ const [fx, fy] = freeDirAt(X, Y);
1199
+ const ja = (r1 - 0.5) * (0.2 + 0.45 * curl), cj = Math.cos(ja), sj = Math.sin(ja);
1200
+ const ux = fx * cj - fy * sj, uy = fx * sj + fy * cj;
1201
+ const a = ux * nx + uy * ny;
1202
+ let tx = ux - a * nx, ty = uy - a * ny, tz = -a * nz;
1203
+ let tl = Math.hypot(tx, ty, tz);
1204
+ if (tl < 0.05) {
1205
+ tx = nz * nx;
1206
+ ty = nz * ny;
1207
+ tz = nz * nz - 1;
1208
+ tl = Math.hypot(tx, ty, tz) || 1;
1209
+ }
1210
+ tx /= tl;
1211
+ ty /= tl;
1212
+ tz /= tl;
1213
+ let bx = ny * tz - nz * ty, by = nz * tx - nx * tz, bz = nx * ty - ny * tx;
1214
+ const st = stand0 + 0.2 * r2, cs = Math.cos(st), sn = Math.sin(st);
1215
+ const grow = 0.8 + 0.45 * sizeN(X, Y);
1216
+ const Wd = lockWd * grow * (0.8 + 0.4 * r3), Ld = lockLd * grow * (0.75 + 0.5 * r4);
1217
+ let ax = (tx * cs + nx * sn) * Ld * px, ay = (ty * cs + ny * sn) * Ld * px;
1218
+ let La = Math.hypot(ax, ay);
1219
+ if (La < 1.5) {
1220
+ ax = ux;
1221
+ ay = uy;
1222
+ La = 1.5;
1223
+ } else {
1224
+ ax /= La;
1225
+ ay /= La;
1226
+ }
1227
+ let across = -bx * ay + by * ax;
1228
+ if (across < 0) {
1229
+ bx = -bx;
1230
+ by = -by;
1231
+ bz = -bz;
1232
+ across = -across;
1233
+ }
1234
+ const Wa = Math.max(0.6, 0.5 * Wd * px * Math.max(0.15, across));
1235
+ const seen = La / (Ld * px) * (Wa / (0.5 * Wd * px));
1236
+ const tn = tuftN(X, Y, hAt(X, Y));
1237
+ if (r5 > LOCK_DENSE / Math.max(LOCK_DENSE, seen) * (1 - LOCK_PART * clumps * (1 - smooth(0.25, 0.65, tn)))) continue;
1238
+ const b = nLocks++ * LP;
1239
+ locks[b] = (X + O) * px;
1240
+ locks[b + 1] = (Y + O) * px;
1241
+ locks[b + 2] = ax;
1242
+ locks[b + 3] = ay;
1243
+ locks[b + 4] = La;
1244
+ locks[b + 5] = Wa;
1245
+ locks[b + 6] = 0.1 * hMax * r6 + tuftH * tn;
1246
+ locks[b + 7] = (r7 - 0.5) * 1.2 * curl;
1247
+ locks[b + 8] = tx;
1248
+ locks[b + 9] = ty;
1249
+ locks[b + 10] = tz;
1250
+ locks[b + 11] = bx;
1251
+ locks[b + 12] = by;
1252
+ locks[b + 13] = bz;
1253
+ locks[b + 14] = Ld;
1254
+ locks[b + 15] = Wd;
1255
+ }
1256
+ });
1257
+ const lockAlong = (s)=>(s < 0.25 ? 4 * s : 1) * (1 - 0.5 * s);
1258
+ const SPLAT_PARTS = 6;
1259
+ for(let part = 0; part < SPLAT_PARTS; part++)steps.push(()=>{
1260
+ const k0 = Math.floor(nLocks * part / SPLAT_PARTS), k1 = Math.floor(nLocks * (part + 1) / SPLAT_PARTS);
1261
+ for(let k = k0; k < k1; k++){
1262
+ const b = k * LP;
1263
+ const ox = locks[b], oy = locks[b + 1], ax = locks[b + 2], ay = locks[b + 3], La = locks[b + 4], Wa = locks[b + 5], z0 = locks[b + 6], bend = locks[b + 7];
1264
+ const ext = Wa * (1 + Math.abs(bend)) + 1, ex = ox + ax * La, ey = oy + ay * La;
1265
+ const xa = Math.max(0, Math.floor(Math.min(ox, ex) - ext)), xb = Math.min(Rb - 1, Math.ceil(Math.max(ox, ex) + ext));
1266
+ const ya = Math.max(0, Math.floor(Math.min(oy, ey) - ext)), yb = Math.min(Rb - 1, Math.ceil(Math.max(oy, ey) + ext));
1267
+ const iLa = 1 / La, iWa = 1 / Wa;
1268
+ for(let y = ya; y <= yb; y++){
1269
+ const dy = y + 0.5 - oy;
1270
+ for(let x = xa, i = y * Rb + xa; x <= xb; x++, i++){
1271
+ const dx = x + 0.5 - ox;
1272
+ const s = (dx * ax + dy * ay) * iLa;
1273
+ if (s <= 0 || s >= 1) continue;
1274
+ const w = LOCK_TAPER * Math.sqrt(s) * (1 - s);
1275
+ const v = (dy * ax - dx * ay) * iWa - bend * s * s;
1276
+ if (v >= w || v <= -w) continue;
1277
+ const q = v / w;
1278
+ const h = z0 + hMax * lockAlong(s) * (1 - q * q);
1279
+ if (h > pileH[i]) {
1280
+ pileH[i] = h;
1281
+ own[i] = k;
1282
+ }
1283
+ }
1284
+ }
1285
+ }
1286
+ });
1287
+ const pileB = new Float32Array(Rb * Rb);
1288
+ const rB = Math.max(1, Math.round(0.3 * lockWd * px));
1289
+ const line = new Float32Array(Rb);
1290
+ const box = (src, dst, r, k, stride)=>{
1291
+ const step = stride === 1 ? Rb : 1, inv = 1 / (2 * r + 1);
1292
+ const at = (t)=>line[t < 0 ? 0 : t >= Rb ? Rb - 1 : t];
1293
+ const o = k * step;
1294
+ for(let t = 0; t < Rb; t++)line[t] = src[o + t * stride];
1295
+ let acc = 0;
1296
+ for(let t = -r; t <= r; t++)acc += at(t);
1297
+ for(let t = 0; t < Rb; t++){
1298
+ dst[o + t * stride] = acc * inv;
1299
+ acc += at(t + r + 1) - at(t - r);
1300
+ }
1301
+ };
1302
+ steps.push(()=>{
1303
+ for(let y = 0; y < Rb; y++)box(pileH, pileB, rB, y, 1);
1304
+ for(let y = 0; y < Rb; y++)box(pileB, pileB, rB, y, 1);
1305
+ });
1306
+ steps.push(()=>{
1307
+ for(let x = 0; x < Rb; x++)box(pileB, pileB, rB, x, Rb);
1308
+ for(let x = 0; x < Rb; x++)box(pileB, pileB, rB, x, Rb);
1309
+ });
1310
+ steps.push(()=>{
1311
+ for(let y = 0; y < Rb; y++)box(pileH, pileH, 1, y, 1);
1312
+ for(let x = 0; x < Rb; x++)box(pileH, pileH, 1, x, Rb);
1313
+ });
1314
+ const darkImg = new ImageData(Rb, Rb), lightImg = new ImageData(Rb, Rb);
1315
+ const glintImg = new ImageData(Rb, Rb), backImg = new ImageData(Rb, Rb), softImg = new ImageData(Rb, Rb);
1316
+ const white = new Float32Array(Rb * Rb);
1317
+ const fib = new Float32Array(11);
1318
+ const fibreLight = (X, Y, ux, uy, stand)=>{
1319
+ const fx = Math.min(G - 1, Math.max(0, (X + O) / gu - 0.5)), fy = Math.min(G - 1, Math.max(0, (Y + O) / gu - 0.5));
1320
+ const x0 = Math.min(G - 2, fx | 0), y0 = Math.min(G - 2, fy | 0);
1321
+ const u = fx - x0, v = fy - y0, j = y0 * G + x0;
1322
+ const w00 = (1 - u) * (1 - v), w10 = u * (1 - v), w01 = (1 - u) * v, w11 = u * v;
1323
+ const read = (f)=>f[j] * w00 + f[j + 1] * w10 + f[j + G] * w01 + f[j + G + 1] * w11;
1324
+ let nx = read(nXg), ny = read(nYg), nz = read(nZg);
1325
+ const nn = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;
1326
+ nx /= nn;
1327
+ ny /= nn;
1328
+ nz /= nn;
1329
+ const nK = nx * K3[0] + ny * K3[1] + nz * K3[2];
1330
+ fib[4] = nx;
1331
+ fib[5] = ny;
1332
+ fib[6] = nz;
1333
+ fib[7] = nK;
1334
+ fib[8] = read(creaseG);
1335
+ const on = read(onG);
1336
+ fib[9] = on;
1337
+ fib[10] = read(toneG);
1338
+ if (on <= 0) {
1339
+ fib[0] = fib[1] = fib[3] = 0;
1340
+ fib[2] = 0.7;
1341
+ return;
1342
+ }
1343
+ const key = smooth(-0.2, 0.5, nK);
1344
+ const along = ux * nx + uy * ny;
1345
+ let tx = ux - along * nx, ty = uy - along * ny, tz = -along * nz;
1346
+ const tl = Math.sqrt(tx * tx + ty * ty + tz * tz);
1347
+ if (tl > 1e-3) {
1348
+ tx /= tl;
1349
+ ty /= tl;
1350
+ tz /= tl;
1351
+ } else tx = 0, ty = 0, tz = 1;
1352
+ const cs = Math.cos(stand), sn = Math.sin(stand);
1353
+ tx = tx * cs + nx * sn;
1354
+ ty = ty * cs + ny * sn;
1355
+ tz = tz * cs + nz * sn;
1356
+ const tH = tx * H3[0] + ty * H3[1] + tz * H3[2], nH = nx * H3[0] + ny * H3[1] + nz * H3[2];
1357
+ const a1 = tH - 0.12 * nH, a2 = tH + 0.2 * nH;
1358
+ const b1 = Math.max(0, 1 - a1 * a1), b2 = b1 * b1, b4 = b2 * b2, b8 = b4 * b4, b16 = b8 * b8;
1359
+ const c1 = Math.max(0, 1 - a2 * a2), c2 = c1 * c1;
1360
+ fib[0] = key * (0.85 * b16 * b16 * b4 + 0.15 * c2 * c2);
1361
+ fib[0] *= (0.4 + 0.6 * (1 - smooth(0.35, 0.85, nK))) * on;
1362
+ const side = Math.sqrt(Math.max(0, 1 - tz * tz));
1363
+ fib[1] = smooth(-0.3, 0.6, nx * B3[0] + ny * B3[1] + nz * B3[2]) * Math.pow(1 - nz, 1.6) * (0.4 + 0.6 * side) * (1 - 0.6 * Math.max(0, ny)) * on;
1364
+ fib[3] = smooth(-0.5, 0.75, nx * B3[0] + ny * B3[1] + nz * B3[2]) * Math.pow(1 - nz, 0.85) * (1 - 0.6 * Math.max(0, ny)) * on;
1365
+ fib[2] = key * on + 0.7 * (1 - on);
1366
+ };
1367
+ steps.push(()=>{
1368
+ for(let y = 0, i = 0; y < Rb; y++)for(let x = 0; x < Rb; x++, i++){
1369
+ const h = hash2(x + 911, y + 37);
1370
+ white[i] = h * h * h;
1371
+ }
1372
+ });
1373
+ const W_MEAN = 0.25, W_SD = Math.sqrt(1 / 7 - 1 / 16);
1374
+ const strandHalf = Math.max(1, Math.round((0.35 + 0.4 * kLen) * px));
1375
+ const strandW = [], strandMean = new Float32Array(2 * strandHalf + 1), strandSd = new Float32Array(2 * strandHalf + 1);
1376
+ for(let L = 0; L <= 2 * strandHalf; L++){
1377
+ const w = new Float32Array(L + 1);
1378
+ let m1 = 0, v2 = 0;
1379
+ for(let t = 0; t <= L; t++){
1380
+ const tau = L ? t / L : 0, p = tau <= STRAND_SHORT ? 1 : (1 - tau) / (1 - STRAND_SHORT);
1381
+ w[t] = 1 - STRAND_TAPER * tau;
1382
+ m1 += w[t] * p * W_MEAN;
1383
+ v2 += w[t] * w[t] * (p / 7 - W_MEAN * W_MEAN * p * p);
1384
+ }
1385
+ strandW.push(w);
1386
+ strandMean[L] = m1;
1387
+ strandSd[L] = Math.sqrt(Math.max(1e-6, v2));
1388
+ }
1389
+ const strandK = 0.12 * (R >= 512 ? 1 : R >= 320 ? 0.75 : 0.4) * Math.min(1.4, 0.55 + 0.35 * kDen);
1390
+ const reach = Math.max(1, 1.5 * lockWd * px), NSH = Math.max(1, Math.min(5, Math.round(reach)));
1391
+ const shX = new Int32Array(NSH), shY = new Int32Array(NSH), shD = new Float32Array(NSH);
1392
+ for(let t = 0; t < NSH; t++){
1393
+ const d = (t + 1) / NSH * reach;
1394
+ shX[t] = Math.round(Lx * d);
1395
+ shY[t] = Math.round(Ly * d);
1396
+ shD[t] = Math.hypot(shX[t], shY[t]) / px;
1397
+ }
1398
+ const difLut = powLut(1.35), gamLut = powLut(1 / 2.2);
1399
+ const keyOn = (nk)=>{
1400
+ const q = (nk + PILE_WRAP) / (1 + PILE_WRAP);
1401
+ return q <= 0 ? 0 : q >= 1 ? 1 : difLut[q * POW_N | 0];
1402
+ };
1403
+ const depthK = R >= 512 ? 1 : R >= 320 ? 0.8 : 0.55;
1404
+ const seen = new Float32Array(4);
1405
+ const lockSeen = (x, y, X, Y, k)=>{
1406
+ if (k < 0) {
1407
+ const [ux, uy] = dirAt(X, Y);
1408
+ seen[0] = ux;
1409
+ seen[1] = uy;
1410
+ seen[2] = 0.5;
1411
+ seen[3] = 0;
1412
+ return;
1413
+ }
1414
+ const b = k * LP;
1415
+ const ax = locks[b + 2], ay = locks[b + 3], La = locks[b + 4], Wa = locks[b + 5], bend = locks[b + 7];
1416
+ const dx = x + 0.5 - locks[b], dy = y + 0.5 - locks[b + 1];
1417
+ const s = Math.min(0.995, Math.max(0.005, (dx * ax + dy * ay) / La));
1418
+ const ss = Math.sqrt(s), w = LOCK_TAPER * ss * (1 - s), dw = LOCK_TAPER * (0.5 * (1 - s) / ss - ss);
1419
+ const q = Math.max(-1, Math.min(1, ((dy * ax - dx * ay) / Wa - bend * s * s) / w));
1420
+ const sl = Math.max(-1.2, Math.min(1.2, q * dw + 2 * bend * s));
1421
+ const ux = La * ax - sl * Wa * ay, uy = La * ay + sl * Wa * ax;
1422
+ const ul = Math.sqrt(ux * ux + uy * uy) || 1;
1423
+ seen[0] = ux / ul;
1424
+ seen[1] = uy / ul;
1425
+ seen[2] = s;
1426
+ seen[3] = lockAlong(s) * (1 - q * q);
1427
+ };
1428
+ const strandBuf = new Float32Array(Rb * Rb);
1429
+ rows(Rb, 32, (y0, y1)=>{
1430
+ for(let y = y0; y < y1; y++){
1431
+ const Y = (y + 0.5) / px - O;
1432
+ for(let x = 0, i = y * Rb; x < Rb; x++, i++){
1433
+ const X = (x + 0.5) / px - O;
1434
+ lockSeen(x, y, X, Y, own[i]);
1435
+ const ux = seen[0], uy = seen[1];
1436
+ const gx = Math.min(G - 1, Math.max(0, (X + O) / gu - 0.5)), gy = Math.min(G - 1, Math.max(0, (Y + O) / gu - 0.5));
1437
+ const gx0 = Math.min(G - 2, gx | 0), gy0 = Math.min(G - 2, gy | 0), gtx = gx - gx0, gty = gy - gy0, gj = gy0 * G + gx0;
1438
+ const tilt = (tiltG[gj] * (1 - gtx) + tiltG[gj + 1] * gtx) * (1 - gty) + (tiltG[gj + G] * (1 - gtx) + tiltG[gj + G + 1] * gtx) * gty;
1439
+ const fore = (foreG[gj] * (1 - gtx) + foreG[gj + 1] * gtx) * (1 - gty) + (foreG[gj + G] * (1 - gtx) + foreG[gj + G + 1] * gtx) * gty;
1440
+ const L = 2 * Math.max(1, Math.min(strandHalf, Math.round(strandHalf * (0.5 + 0.5 * tilt) * fore / PILE_COS)));
1441
+ const sw = strandW[L];
1442
+ let acc = 0, accR = 0;
1443
+ for(let t = 0; t <= L; t++){
1444
+ const sx = Math.round(x - ux * t), sy = Math.round(y - uy * t);
1445
+ if (sx < 0 || sy < 0 || sx >= Rb || sy >= Rb) {
1446
+ acc += strandMean[L] / (L + 1);
1447
+ continue;
1448
+ }
1449
+ const m = white[sy * Rb + sx], f = m * 53.7, Ls = L * (STRAND_SHORT + (1 - STRAND_SHORT) * (f - Math.floor(f)));
1450
+ if (t <= Ls) {
1451
+ acc += m * sw[t];
1452
+ accR += m * sw[t] * t / (Ls || 1);
1453
+ }
1454
+ }
1455
+ const strand = (acc - strandMean[L]) / strandSd[L];
1456
+ const along = acc > 1e-4 ? accR / acc : 0.5;
1457
+ strandBuf[i] = strand + STRAND_RISE * Math.min(2.5, Math.max(0, strand)) * (along - 0.45);
1458
+ }
1459
+ }
1460
+ });
1461
+ let lsum = 0, lsum2 = 0;
1462
+ rows(Rb, 16, (y0, y1)=>{
1463
+ const dd = darkImg.data, ld = lightImg.data, gd = glintImg.data, bd = backImg.data, sd2 = softImg.data;
1464
+ for(let y = y0; y < y1; y++){
1465
+ const Y = (y + 0.5) / px - O;
1466
+ for(let x = 0; x < Rb; x++){
1467
+ const X = (x + 0.5) / px - O;
1468
+ const i = y * Rb + x, k = own[i], b = k * LP;
1469
+ lockSeen(x, y, X, Y, k);
1470
+ const ux = seen[0], uy = seen[1], s = seen[2], hgt = seen[3];
1471
+ fibreLight(X, Y, ux, uy, 0.25 + 0.2 * hgt);
1472
+ const nx = fib[4], ny = fib[5], nz = fib[6], nKb = fib[7];
1473
+ const xl0 = x > 0 ? i - 1 : i, xr0 = x < Rb - 1 ? i + 1 : i, yu0 = y > 0 ? i - Rb : i, yd0 = y < Rb - 1 ? i + Rb : i;
1474
+ const gx = (pileH[xr0] - pileH[xl0]) * px / (xr0 - xl0 || 1), gy = (pileH[yd0] - pileH[yu0]) * px * Rb / (yd0 - yu0 || 1);
1475
+ const l1 = Math.sqrt(nx * nx + ny * ny);
1476
+ let t1x = 1, t1y = 0, t2x = 0, t2y = 1, t2z = 0;
1477
+ if (l1 > 1e-3) {
1478
+ t1x = -ny / l1;
1479
+ t1y = nx / l1;
1480
+ t2x = -nz * nx / l1;
1481
+ t2y = -nz * ny / l1;
1482
+ t2z = l1;
1483
+ }
1484
+ const g1 = Math.max(-1.5, Math.min(1.5, gx * t1x + gy * t1y)), g2 = Math.max(-1.5, Math.min(1.5, gx * t2x + gy * t2y));
1485
+ let mx = nx - g1 * t1x - g2 * t2x, my = ny - g1 * t1y - g2 * t2y, mz = nz - g2 * t2z;
1486
+ const ml = Math.sqrt(mx * mx + my * my + mz * mz) || 1;
1487
+ mx /= ml;
1488
+ my /= ml;
1489
+ mz /= ml;
1490
+ let kk = 1;
1491
+ if (k >= 0) {
1492
+ const tx = locks[b + 8], ty = locks[b + 9], tz = locks[b + 10];
1493
+ const fx = 0.9 * tx + 0.44 * nx, fy = 0.9 * ty + 0.44 * ny, fz = 0.9 * tz + 0.44 * nz;
1494
+ const fK = (fx * K3[0] + fy * K3[1] + fz * K3[2]) / (Math.sqrt(fx * fx + fy * fy + fz * fz) || 1);
1495
+ kk = 1 - PILE_KK + PILE_KK * Math.min(1.2, Math.sqrt(Math.max(0, 1 - fK * fK)) / 0.85);
1496
+ }
1497
+ const nKp = mx * K3[0] + my * K3[1] + mz * K3[2];
1498
+ const h0 = pileH[i];
1499
+ let sh = 1;
1500
+ if (nKb > -0.25) {
1501
+ const sinE = Math.min(0.97, Math.max(0.05, nKb)), tanE = sinE / Math.sqrt(1 - sinE * sinE);
1502
+ let occ = 0;
1503
+ for(let t = 0; t < NSH; t++){
1504
+ const sx = x + shX[t], sy = y + shY[t];
1505
+ if (sx < 0 || sy < 0 || sx >= Rb || sy >= Rb) break;
1506
+ const o = pileH[sy * Rb + sx] - h0 - shD[t] * tanE;
1507
+ if (o > occ) occ = o;
1508
+ }
1509
+ sh = 1 - PILE_SHADOW * smooth(0, 0.35 * hMax, occ);
1510
+ }
1511
+ let ao = 1 - PILE_AO * smooth(0, 0.6 * hMax, pileB[i] - h0);
1512
+ if (k >= 0) ao *= 1 - PILE_ROOT * (1 - smooth(0, 0.35, s));
1513
+ const fillP = PILE_AMB + PILE_FILL * Math.max(0, mx * F3[0] + my * F3[1] + mz * F3[2]);
1514
+ const fillB = PILE_AMB + PILE_FILL * Math.max(0, nx * F3[0] + ny * F3[1] + nz * F3[2]);
1515
+ let r = ao * (fillP + keyOn(nKp) * sh * kk) * PILE_GAIN / (fillB + keyOn(nKb));
1516
+ if (k >= 0) r *= (1 + 0.15 * s * s) * (1 + 0.08 * (hash2(k, 5) - 0.5));
1517
+ const crease = fib[8];
1518
+ r *= 1 - PILE_CREASE * crease * (0.6 + 0.4 * smooth(0, 0.6 * hMax, pileB[i] - h0));
1519
+ r = 1 + (r - 1) * depthK;
1520
+ const on = fib[9], lq = (fillB + keyOn(nKb)) / PILE_LIT;
1521
+ const lbs = (lq >= 1 ? 1 : gamLut[lq * POW_N | 0]) * on + 0.75 * (1 - on);
1522
+ let m0 = Math.sqrt(r > 2.4 ? 2.4 : r);
1523
+ if (m0 > 1) m0 = 1 + (m0 - 1) / (1 + 2.5 * lbs * (m0 - 1));
1524
+ const strand = strandBuf[i];
1525
+ let cast = 0;
1526
+ if (nKb > 0) {
1527
+ const kx = K3[0] - nKb * nx, ky = K3[1] - nKb * ny, kt = Math.sqrt(1 - nKb * nKb);
1528
+ const reachS = kt > 1e-3 ? STRAND_CAST_H * px * Math.min(3, kt / Math.max(0.2, nKb)) / kt : 0;
1529
+ const cx = Math.min(Rb - 1.001, Math.max(0, x + kx * reachS)), cy = Math.min(Rb - 1.001, Math.max(0, y + ky * reachS));
1530
+ const cx0 = cx | 0, cy0 = cy | 0, tx = cx - cx0, ty = cy - cy0, ci = cy0 * Rb + cx0;
1531
+ const caster = (strandBuf[ci] * (1 - tx) + strandBuf[ci + 1] * tx) * (1 - ty) + (strandBuf[ci + Rb] * (1 - tx) + strandBuf[ci + Rb + 1] * tx) * ty;
1532
+ cast = Math.max(0, caster - Math.max(0, strand)) * smooth(0, 0.3, nKb);
1533
+ }
1534
+ let m = m0 * (1 + strandK * (strand * (0.45 + 0.55 * fib[2]) * (0.75 + 0.5 * s) - STRAND_CAST * cast)) + 0.04 * (fib[10] - 0.5) + PILE_BIAS * depthK * on;
1535
+ const edgeD = Math.min(X + O, FILM_SPAN - O - X, Y + O, FILM_SPAN - O - Y);
1536
+ const fadeE = edgeD >= 8 ? 1 : edgeD <= 0 ? 0 : edgeD / 8 * (edgeD / 8) * (3 - 2 * edgeD / 8);
1537
+ m = 1 + (m - 1) * fadeE;
1538
+ const k4 = i * 4;
1539
+ if (m < 1) dd[k4 + 3] = Math.round(255 * Math.min(1, (1 - m) * lbs / Math.max(0.4, lbs - GAP_REF)));
1540
+ else {
1541
+ ld[k4] = ld[k4 + 1] = ld[k4 + 2] = 255;
1542
+ ld[k4 + 3] = Math.round(255 * Math.min(1, PILE_LIFT * (TIP_TOP - 1) * lbs / (TIP_TOP - lbs) * (m - 1) * (1 - 0.7 * crease)));
1543
+ }
1544
+ const lit = Math.max(-2, Math.min(2, 4 * (m0 - 1)));
1545
+ const xl = x >> 1, yl = y >> 1;
1546
+ if (xl < Rl && yl < Rl) {
1547
+ const j = yl * Rl + xl;
1548
+ lock[j] += 0.25 * h0;
1549
+ lockLit[j] += 0.25 * lit;
1550
+ }
1551
+ const tipK = smooth(-0.4, 1, lit) * Math.max(0, Math.min(1.4, 0.65 + 0.25 * strand));
1552
+ gd[k4] = gd[k4 + 1] = gd[k4 + 2] = bd[k4] = bd[k4 + 1] = bd[k4 + 2] = 255;
1553
+ gd[k4 + 3] = Math.round(255 * Math.min(1, fib[0] * tipK));
1554
+ bd[k4 + 3] = Math.round(255 * Math.min(1, fib[1] * Math.max(0, Math.min(1.6, 0.5 + 0.4 * strand))));
1555
+ const onPile = Math.max(0.55, Math.min(1.3, 0.85 + 0.15 * strand + 0.12 * lit));
1556
+ sd2[k4] = sd2[k4 + 1] = sd2[k4 + 2] = 255;
1557
+ sd2[k4 + 3] = Math.round(255 * Math.min(1, 0.85 * fib[3] * onPile));
1558
+ }
1559
+ }
1560
+ });
1561
+ steps.push(()=>{
1562
+ const n = Rl * Rl;
1563
+ for(let i = 0; i < n; i++){
1564
+ lsum += lock[i];
1565
+ lsum2 += lock[i] * lock[i];
1566
+ }
1567
+ const mean = lsum / n, sdv = Math.sqrt(Math.max(1e-9, lsum2 / n - mean * mean));
1568
+ for(let i = 0; i < n; i++)lock[i] = Math.max(-2.5, Math.min(2.5, (lock[i] - mean) / sdv));
1569
+ });
1570
+ steps.push(()=>{
1571
+ dg.putImageData(darkImg, 0, 0);
1572
+ lg.putImageData(lightImg, 0, 0);
1573
+ gg.putImageData(glintImg, 0, 0);
1574
+ sg2.putImageData(softImg, 0, 0);
1575
+ bg.putImageData(backImg, 0, 0);
1576
+ for (const g of [
1577
+ dg,
1578
+ lg,
1579
+ gg,
1580
+ bg
1581
+ ]){
1582
+ g.setTransform(1, 0, 0, 1, FILM_M * px, FILM_M * px);
1583
+ g.lineCap = "round";
1584
+ g.lineJoin = "round";
1585
+ }
1586
+ });
1587
+ const SHADES = 16;
1588
+ const area = FILM_SPAN * FILM_SPAN;
1589
+ const fine = R >= 512 ? 1 : R >= 320 ? 0.45 : 0;
1590
+ const kinds = [
1591
+ {
1592
+ count: 0.12,
1593
+ len: 1.2,
1594
+ vary: 1.0,
1595
+ endOn: 0.6,
1596
+ width: 0.09,
1597
+ spread: 30,
1598
+ alpha: 0.4,
1599
+ deep: 1,
1600
+ lift: 0.25,
1601
+ light: 0.46,
1602
+ dark: 0.34,
1603
+ bend: 0.93,
1604
+ clump: [
1605
+ 1,
1606
+ 0
1607
+ ],
1608
+ clumpW: 0,
1609
+ shadow: 0,
1610
+ shadowW: 1,
1611
+ over: 0,
1612
+ crowd: false,
1613
+ short: 0
1614
+ },
1615
+ {
1616
+ count: 0.4 * fine,
1617
+ len: 0.9,
1618
+ vary: 1.2,
1619
+ endOn: 0.35,
1620
+ width: 0.13,
1621
+ spread: 100,
1622
+ alpha: 0.26,
1623
+ deep: 0.2,
1624
+ lift: 0.18,
1625
+ light: 0.55,
1626
+ dark: 0.06,
1627
+ bend: 1.2,
1628
+ clump: [
1629
+ 1,
1630
+ Math.round(1 + 2 * clumps)
1631
+ ],
1632
+ clumpW: 0.08 + 0.07 * clumps,
1633
+ shadow: 0.25,
1634
+ shadowW: 2,
1635
+ over: 0.25,
1636
+ crowd: true,
1637
+ short: 1
1638
+ }
1639
+ ];
1640
+ const shadeOf = (v)=>Math.min(SHADES - 1, Math.max(0, Math.round((Math.max(-1, Math.min(1, v)) + 1) / 2 * (SHADES - 1))));
1641
+ const GLINTS = 6;
1642
+ const glintOf = (v)=>Math.min(GLINTS - 1, v * GLINTS | 0);
1643
+ const SEGS = 4;
1644
+ const hx = new Float32Array(SEGS + 1), hy = new Float32Array(SEGS + 1);
1645
+ for (const L of kinds){
1646
+ if (L.count <= 0) continue;
1647
+ const perRoot = L.clump[0] + 0.5 * L.clump[1];
1648
+ const parts = Math.max(1, Math.ceil(area * L.count * kDen * (L.crowd ? 2 : 1) * (0.5 + 0.5 * perRoot) / 9000));
1649
+ for(let part = 0; part < parts; part++)steps.push(()=>{
1650
+ const buckets = [], glints = [], backs = [], shades = [];
1651
+ for(let i = 0; i < SHADES; i++)buckets.push(new Path2D());
1652
+ for(let i = 0; i < GLINTS; i++)glints.push(new Path2D()), backs.push(new Path2D());
1653
+ for(let i = 0; i < 3; i++)shades.push(new Path2D());
1654
+ const over = new Path2D();
1655
+ const count = Math.round(area * L.count * kDen * (L.crowd ? 2 : 1) / parts);
1656
+ for(let n = 0; n < count; n++){
1657
+ const X = rand() * FILM_SPAN - O, Y = rand() * FILM_SPAN - O;
1658
+ const r0 = rand(), r1 = rand(), r2 = rand(), r3 = rand(), r4 = rand(), r5 = rand();
1659
+ if (r5 * 8 > Math.min(X + O, FILM_SPAN - O - X, Y + O, FILM_SPAN - O - Y)) continue;
1660
+ if (L.crowd && r5 > (sdAt(X, Y) > 0 ? 0.5 / Math.max(0.25, gridAt(nZg, X, Y)) : 0.5)) continue;
1661
+ const [fx, fy] = dirAt(X, Y);
1662
+ const tiltS = gridAt(tiltG, X, Y), vl = gridAt(foreG, X, Y) / PILE_COS;
1663
+ const ang = (r1 - 0.5) * (0.6 + 0.8 * curl) * (1 - 0.5 * tiltS);
1664
+ const ca = Math.cos(ang), sa = Math.sin(ang);
1665
+ const ux = fx * ca - fy * sa, uy = fx * sa + fy * ca;
1666
+ const longer = r2 < 0.1 ? 1.35 + 0.6 * r3 : 1;
1667
+ const len = (L.len + L.vary * r3 * r3) * px * kLen * longer * vl * (1 - L.endOn + 1.25 * L.endOn * tiltS);
1668
+ fibreLight(X, Y, ux, uy, 0.1 + 0.5 * hash2(n, 7 + part));
1669
+ let v = 0.22 * lockAt(lock, X, Y) + 0.3 * lockAt(lockLit, X, Y);
1670
+ const lit = r0 < L.light * (0.4 + 0.6 * fib[2]);
1671
+ if (lit) v += 0.2 + 0.28 * r4;
1672
+ else if (r0 < L.light + L.dark) v -= 0.2 + 0.28 * r4;
1673
+ v += L.lift;
1674
+ v += 0.45 * (fib[2] - 0.5) - 0.35 * fib[8];
1675
+ if (v > 0) v *= 0.3 + 0.7 * fib[2];
1676
+ const deepen = 0.15 * (1 - fib[2]);
1677
+ const x0 = (X + PAD) * px, y0 = (Y + PAD) * px;
1678
+ const b1 = (rand() - 0.5) * L.bend * curl * len, b2 = r4 < 0.3 ? -0.6 * b1 : b1 * (0.6 + 0.8 * rand());
1679
+ const hairs = L.clump[0] + (rand() * (L.clump[1] + 1) | 0);
1680
+ const wC = L.clumpW * px * Math.min(1.5, kLen);
1681
+ for(let k = 0; k < hairs; k++){
1682
+ const o = hairs > 1 ? rand() - 0.5 : 0, f = hairs > 1 ? 0.7 + 0.3 * rand() : 1, j = hairs > 1 ? (rand() - 0.5) * 0.3 : 0;
1683
+ const rx = x0 - uy * o * wC, ry = y0 + ux * o * wC;
1684
+ const lk = len * f, e1 = b1 + j * lk, e2 = b2 + j * lk - o * wC * 0.7;
1685
+ const c1x = rx + ux * lk * 0.33 - uy * e1, c1y = ry + uy * lk * 0.33 + ux * e1;
1686
+ const c2x = rx + ux * lk * 0.67 - uy * e2, c2y = ry + uy * lk * 0.67 + ux * e2;
1687
+ const x1 = rx + ux * lk - uy * (b2 - o * wC * 0.75), y1 = ry + uy * lk + ux * (b2 - o * wC * 0.75);
1688
+ const sg = lk < 4 ? 2 : lk < 10 ? 3 : SEGS;
1689
+ for(let s = 0; s <= sg; s++){
1690
+ const t = s / sg, q = 1 - t;
1691
+ const a = q * q * q, b = 3 * q * q * t, c = 3 * q * t * t, d = t * t * t;
1692
+ hx[s] = a * rx + b * c1x + c * c2x + d * x1;
1693
+ hy[s] = a * ry + b * c1y + c * c2y + d * y1;
1694
+ }
1695
+ const vh = v + (hairs > 1 ? (rand() - 0.5) * 0.25 : 0), rise = 0.3 + 0.15 * fib[2];
1696
+ for(let s = 0; s < sg; s++){
1697
+ const bk = buckets[shadeOf(vh - 0.3 - deepen * (1 - s / sg) + rise * (s + 0.5) / sg)];
1698
+ bk.moveTo(hx[s], hy[s]);
1699
+ bk.lineTo(hx[s + 1], hy[s + 1]);
1700
+ }
1701
+ if (lit && L.over > 0) {
1702
+ over.moveTo(hx[1], hy[1]);
1703
+ for(let s = 2; s <= sg; s++)over.lineTo(hx[s], hy[s]);
1704
+ }
1705
+ if (k === 0 && L.shadow > 0 && fib[2] > 0.25 && v > -0.2) {
1706
+ const kk = L.shadow * px * Math.min(1, kLen), sh = shades[Math.min(2, (fib[2] - 0.25) * 4 | 0)];
1707
+ for(let s = 1; s <= sg; s++){
1708
+ const t = s / sg, ox = -Lx * kk * t, oy = -Ly * kk * t + 0.3 * kk * t;
1709
+ if (s === 1) sh.moveTo(hx[0], hy[0]);
1710
+ sh.lineTo(hx[s] + ox, hy[s] + oy);
1711
+ }
1712
+ }
1713
+ const h = sg >> 1;
1714
+ if (fib[0] > 0.3) {
1715
+ const gb = glints[glintOf(fib[0])];
1716
+ gb.moveTo(hx[h], hy[h]);
1717
+ for(let s = h + 1; s <= sg; s++)gb.lineTo(hx[s], hy[s]);
1718
+ }
1719
+ if (fib[1] > 0.1) {
1720
+ const bb = backs[glintOf(fib[1])];
1721
+ bb.moveTo(hx[h], hy[h]);
1722
+ for(let s = h + 1; s <= sg; s++)bb.lineTo(hx[s], hy[s]);
1723
+ }
1724
+ }
1725
+ }
1726
+ const shortK = Math.min(1, kLen) ** L.short;
1727
+ const lw = Math.max(0.45, L.width * px * (0.5 + 0.5 * shortK));
1728
+ if (L.over > 0) {
1729
+ dg.globalCompositeOperation = "destination-out";
1730
+ dg.lineWidth = lw;
1731
+ dg.globalAlpha = L.over * shortK;
1732
+ dg.strokeStyle = "#000";
1733
+ dg.stroke(over);
1734
+ dg.globalCompositeOperation = "source-over";
1735
+ }
1736
+ if (L.shadow > 0) {
1737
+ dg.lineWidth = lw * L.shadowW;
1738
+ dg.strokeStyle = "#000";
1739
+ for(let i = 0; i < 3; i++){
1740
+ dg.globalAlpha = 0.03 * (i + 1) * shortK;
1741
+ dg.stroke(shades[i]);
1742
+ }
1743
+ }
1744
+ for(let i = 0; i < SHADES; i++){
1745
+ const v = FUR_MID + (i / (SHADES - 1) * 2 - 1) * L.spread;
1746
+ const g = v < FUR_MID ? dg : lg;
1747
+ g.lineWidth = lw;
1748
+ g.globalAlpha = L.alpha * shortK * (v < FUR_MID ? L.deep * (1 - v / FUR_MID) : (v - FUR_MID) / (255 - FUR_MID));
1749
+ g.strokeStyle = v < FUR_MID ? "#000" : "#fff";
1750
+ g.stroke(buckets[i]);
1751
+ }
1752
+ for (const g of [
1753
+ gg,
1754
+ bg
1755
+ ]){
1756
+ g.lineWidth = lw;
1757
+ g.strokeStyle = "#fff";
1758
+ }
1759
+ for(let i = 0; i < GLINTS; i++){
1760
+ gg.globalAlpha = 0.4 * (i + 0.5) / GLINTS;
1761
+ bg.globalAlpha = 0.8 * (i + 0.5) / GLINTS;
1762
+ gg.stroke(glints[i]);
1763
+ bg.stroke(backs[i]);
1764
+ }
1765
+ dg.globalAlpha = lg.globalAlpha = gg.globalAlpha = bg.globalAlpha = 1;
1766
+ });
1767
+ }
1768
+ const fringe = new Path2D();
1769
+ const fringeBack = [];
1770
+ for(let i = 0; i < GLINTS; i++)fringeBack.push(new Path2D());
1771
+ const cells = [];
1772
+ const cu = SPAN / N;
1773
+ let edgeTries = 0;
1774
+ const FRINGE_PARTS = 2;
1775
+ for(let part = 0; part < FRINGE_PARTS; part++)steps.push(()=>{
1776
+ if (part === 0) {
1777
+ for(let i = 0; i < N * N; i++)if (form.sd[i] > -1.6 && form.sd[i] < 2.2) cells.push(i);
1778
+ edgeTries = Math.round(cells.length * cu * cu * 24 * (0.3 + 1.4 * fuzz) * Math.sqrt(kDen) * (0.5 + 0.5 * Math.min(1, R / 512)) * (1 - 0.55 * fine));
1779
+ }
1780
+ const n1 = Math.floor(edgeTries * (part + 1) / FRINGE_PARTS);
1781
+ for(let n = Math.floor(edgeTries * part / FRINGE_PARTS); n < n1; n++){
1782
+ const c = cells[rand() * cells.length | 0];
1783
+ const X = (c % N + rand()) * cu - PAD, Y = ((c / N | 0) + rand()) * cu - PAD;
1784
+ const d0 = sdAt(X, Y);
1785
+ if (d0 < -1.2 || d0 > 1.8) continue;
1786
+ const d = d0 + tuftAt(X, Y);
1787
+ if (d < -0.2 || d > 1.2) continue;
1788
+ const [fx, fy] = flow(X, Y);
1789
+ const [ox, oy] = outward(X, Y);
1790
+ const ang = (rand() - 0.5) * 1.1;
1791
+ const ca = Math.cos(ang), sa = Math.sin(ang);
1792
+ let ux = fx * ca - fy * sa, uy = fx * sa + fy * ca;
1793
+ const r1 = rand();
1794
+ const hang = 1 + 0.5 * gravity * Math.max(0, oy) - 0.35 * gravity * Math.max(0, -oy);
1795
+ let fl = (0.3 + 1.5 * r1 * r1 * r1) * px * kLen * (0.5 + fuzz) * hang;
1796
+ const x0 = (X + PAD) * px, y0 = (Y + PAD) * px;
1797
+ let bend = (rand() - 0.5) * 1.17 * curl * fl;
1798
+ if (oy < -0.3 && rand() < 0.06 * (0.3 + fuzz)) {
1799
+ ux = ox * 0.8 + (rand() - 0.5) * 0.6;
1800
+ uy = oy * 0.8;
1801
+ fl *= 1.2;
1802
+ bend = (rand() < 0.5 ? -1 : 1) * (0.25 + 0.3 * gravity) * fl;
1803
+ }
1804
+ fringe.moveTo(x0, y0);
1805
+ const qx = x0 + ux * fl * 0.5 - uy * bend, qy = y0 + uy * fl * 0.5 + ux * bend, ex = x0 + ux * fl, ey = y0 + uy * fl + gravity * fl * 0.25;
1806
+ fringe.quadraticCurveTo(qx, qy, ex, ey);
1807
+ fibreLight(X, Y, ux, uy, 0.3);
1808
+ if (fib[1] > 0.08) {
1809
+ const fb = fringeBack[glintOf(fib[1])];
1810
+ fb.moveTo(x0, y0);
1811
+ fb.quadraticCurveTo(qx, qy, ex, ey);
1812
+ }
1813
+ }
1814
+ });
1815
+ const tufts = new Path2D(), tuftTips = [
1816
+ new Path2D(),
1817
+ new Path2D(),
1818
+ new Path2D()
1819
+ ];
1820
+ const tuftBack = [];
1821
+ for(let i = 0; i < GLINTS; i++)tuftBack.push(new Path2D());
1822
+ steps.push(()=>{
1823
+ const band = cells.length * cu * cu, around = band / 3.8;
1824
+ const count = Math.round(around / (0.45 + 0.3 * kLen) * (0.3 + 0.8 * fuzz) * Math.sqrt(kDen) * (1 + 0.6 * fine));
1825
+ for(let n = 0, tries = 0; n < count && tries < 6 * count; tries++){
1826
+ const c = cells[rand() * cells.length | 0];
1827
+ const X = (c % N + rand()) * cu - PAD, Y = ((c / N | 0) + rand()) * cu - PAD;
1828
+ const r0 = rand(), r1 = rand(), r2 = rand(), r3 = rand();
1829
+ const d0 = sdAt(X, Y);
1830
+ if (d0 < -0.5 || d0 > 1.6) continue;
1831
+ const d = d0 + tuftAt(X, Y);
1832
+ if (d < 0 || d > 0.9) continue;
1833
+ if (lockAt(lock, X, Y) < -0.4 + 0.8 * r3) continue;
1834
+ n++;
1835
+ const [fx, fy] = flow(X, Y);
1836
+ const [ox, oy] = outward(X, Y);
1837
+ const a = (r0 - 0.5) * 0.8 * (0.5 + curl), ca = Math.cos(a), sa = Math.sin(a);
1838
+ let sx = 0.75 * ox + 0.25 * fx, sy = 0.75 * oy + 0.25 * fy;
1839
+ let ex = 0.55 * fx + 0.45 * ox, ey = 0.55 * fy + 0.45 * oy + 0.35 * gravity;
1840
+ const sl = Math.hypot(sx, sy) || 1, el = Math.hypot(ex, ey) || 1;
1841
+ [sx, sy] = [
1842
+ (sx * ca - sy * sa) / sl,
1843
+ (sx * sa + sy * ca) / sl
1844
+ ];
1845
+ [ex, ey] = [
1846
+ (ex * ca - ey * sa) / el,
1847
+ (ex * sa + ey * ca) / el
1848
+ ];
1849
+ const hang = 1 + 0.5 * gravity * Math.max(0, oy) - 0.35 * gravity * Math.max(0, -oy);
1850
+ const Lt = (0.5 + 1.1 * r1 * r1) * px * kLen * (0.5 + fuzz) * hang;
1851
+ const wT = (0.3 + 0.35 * r2) * px * Math.min(1.5, kLen);
1852
+ const x0 = (X + PAD) * px, y0 = (Y + PAD) * px;
1853
+ const cx = x0 + sx * Lt * 0.5, cy = y0 + sy * Lt * 0.5;
1854
+ const tx = x0 + (sx * 0.4 + ex * 0.6) * Lt, ty = y0 + (sy * 0.4 + ey * 0.6) * Lt;
1855
+ const qx = -sy, qy = sx;
1856
+ fibreLight(X, Y, ex, ey, 0.4);
1857
+ const fb = fib[1] > 0.08 ? tuftBack[glintOf(fib[1])] : null;
1858
+ const tt = tuftTips[Math.min(2, fib[2] * 3 | 0)];
1859
+ const hairs = 4 + (rand() * 5 | 0);
1860
+ for(let k = 0; k < hairs; k++){
1861
+ const o = rand() - 0.5, f = 0.7 + 0.3 * rand(), w = (rand() - 0.5) * 0.35;
1862
+ const hx0 = x0 + qx * o * wT, hy0 = y0 + qy * o * wT;
1863
+ const hcx = cx + qx * o * wT * 0.6, hcy = cy + qy * o * wT * 0.6;
1864
+ const htx = hx0 + (tx + qx * (o * 0.25 + w) * wT - hx0) * f, hty = hy0 + (ty + qy * (o * 0.25 + w) * wT - hy0) * f;
1865
+ tufts.moveTo(hx0, hy0);
1866
+ tufts.quadraticCurveTo(hcx, hcy, htx, hty);
1867
+ const mx = 0.25 * hx0 + 0.5 * hcx + 0.25 * htx, my = 0.25 * hy0 + 0.5 * hcy + 0.25 * hty;
1868
+ tt.moveTo(mx, my);
1869
+ tt.quadraticCurveTo(0.5 * (hcx + htx), 0.5 * (hcy + hty), htx, hty);
1870
+ if (fb) {
1871
+ fb.moveTo(mx, my);
1872
+ fb.quadraticCurveTo(0.5 * (hcx + htx), 0.5 * (hcy + hty), htx, hty);
1873
+ }
1874
+ }
1875
+ }
1876
+ });
1877
+ steps.push(()=>{
1878
+ const tw = Math.max(0.4, 0.11 * px);
1879
+ dg.lineWidth = lg.lineWidth = bg.lineWidth = tw;
1880
+ dg.globalCompositeOperation = "destination-out";
1881
+ dg.globalAlpha = 0.6;
1882
+ dg.strokeStyle = "#000";
1883
+ lg.strokeStyle = "#fff";
1884
+ for(let i = 0; i < 3; i++){
1885
+ dg.stroke(tuftTips[i]);
1886
+ lg.globalAlpha = 0.08 + 0.11 * i;
1887
+ lg.stroke(tuftTips[i]);
1888
+ }
1889
+ dg.globalCompositeOperation = "source-over";
1890
+ bg.strokeStyle = "#fff";
1891
+ for(let i = 0; i < GLINTS; i++){
1892
+ bg.globalAlpha = (i + 0.5) / GLINTS;
1893
+ bg.stroke(tuftBack[i]);
1894
+ }
1895
+ dg.globalAlpha = lg.globalAlpha = bg.globalAlpha = 1;
1896
+ lg.lineWidth = dg.lineWidth = Math.max(0.35, 0.08 * px);
1897
+ lg.globalAlpha = 0.5 * ((140 - FUR_MID) / (255 - FUR_MID));
1898
+ lg.strokeStyle = "#fff";
1899
+ lg.stroke(fringe);
1900
+ lg.globalAlpha = 1;
1901
+ bg.lineWidth = lg.lineWidth;
1902
+ bg.strokeStyle = "#fff";
1903
+ for(let i = 0; i < GLINTS; i++){
1904
+ bg.globalAlpha = (i + 0.5) / GLINTS;
1905
+ bg.stroke(fringeBack[i]);
1906
+ }
1907
+ bg.globalAlpha = 1;
1908
+ dg.globalCompositeOperation = "destination-out";
1909
+ dg.globalAlpha = 0.7;
1910
+ dg.strokeStyle = "#000";
1911
+ dg.stroke(fringe);
1912
+ dg.globalAlpha = 1;
1913
+ dg.globalCompositeOperation = "source-over";
1914
+ });
1915
+ const mi = new ImageData(R, R), zi = new ImageData(R, R), ci = new ImageData(R, R);
1916
+ const hOut = 0.5 + 1.2 * fuzz, reachOut = hOut + tuftK;
1917
+ const haloA = HALO_A * (1 - 0.6 * fine), haloStreak = HALO_STREAK * (1 + 1.2 * fine);
1918
+ const hn = Math.max(1, Math.round(HALO_HAIR * kLen * px)), hs1 = hn + 1;
1919
+ const hsd = W_SD * Math.sqrt(1 + hn * (2 * hn + 1) / (3 * hs1)) / hs1;
1920
+ const off = Math.round(FILM_M * px);
1921
+ rows(R, 64, (y0, y1)=>{
1922
+ const md = mi.data, zd = zi.data, cd = ci.data;
1923
+ for(let y = y0; y < y1; y++){
1924
+ const Y = (y + 0.5) / px - PAD;
1925
+ for(let x = 0; x < R; x++){
1926
+ const X = (x + 0.5) / px - PAD;
1927
+ const k = (y * R + x) * 4;
1928
+ md[k] = md[k + 1] = md[k + 2] = zd[k] = zd[k + 1] = zd[k + 2] = 255;
1929
+ const d0 = sdAt(X, Y);
1930
+ if (d0 >= tuftK + HALO_CORE) {
1931
+ md[k + 3] = cd[k + 3] = 255;
1932
+ continue;
1933
+ }
1934
+ const d = d0 <= -reachOut ? d0 : d0 + tuftAt(X, Y);
1935
+ if (d >= 0) {
1936
+ md[k + 3] = 255;
1937
+ cd[k + 3] = Math.round(255 * smooth(0.25 * HALO_CORE, HALO_CORE, d));
1938
+ continue;
1939
+ }
1940
+ const z = Math.min(1, -d / (2 * hOut));
1941
+ const nB = gridAt(nXg, X, Y) * B3[0] + gridAt(nYg, X, Y) * B3[1];
1942
+ zd[k + 3] = Math.round(255 * HALO_LIFT * z * z * (3 - 2 * z) * (0.45 + 0.55 * smooth(-0.3, 0.6, nB)));
1943
+ if (d <= -hOut) continue;
1944
+ const o = 1 + d / hOut, f = o * o * (3 - 2 * o);
1945
+ const [ux, uy] = freeDirAt(X, Y);
1946
+ let acc = 0;
1947
+ for(let t = -hn; t <= hn; t++){
1948
+ const sx = Math.round(x + off + ux * t), sy = Math.round(y + off + uy * t);
1949
+ acc += (sx < 0 || sy < 0 || sx >= Rb || sy >= Rb ? W_MEAN : white[sy * Rb + sx]) * (1 - Math.abs(t) / hs1);
1950
+ }
1951
+ const strand = (acc / hs1 - W_MEAN) / hsd;
1952
+ md[k + 3] = Math.round(255 * Math.max(0, Math.min(1, f * (haloA + haloStreak * strand))));
1953
+ }
1954
+ }
1955
+ });
1956
+ steps.push(()=>{
1957
+ const zc = makeCanvas(R), zg = zc && ctx2d(zc, false);
1958
+ if (zc && zg) {
1959
+ zg.putImageData(zi, 0, 0);
1960
+ fur.haze = zc;
1961
+ }
1962
+ mg.putImageData(mi, 0, 0);
1963
+ mg.lineCap = "round";
1964
+ mg.lineWidth = Math.max(0.35, 0.08 * px);
1965
+ mg.strokeStyle = "rgba(255,255,255,0.45)";
1966
+ mg.stroke(fringe);
1967
+ mg.lineWidth = Math.max(0.4, 0.11 * px);
1968
+ mg.strokeStyle = "rgba(255,255,255,0.8)";
1969
+ mg.stroke(tufts);
1970
+ const oc = makeCanvas(R), og = oc && ctx2d(oc, false);
1971
+ if (oc && og) {
1972
+ og.putImageData(ci, 0, 0);
1973
+ fur.core = oc;
1974
+ }
1975
+ fur.dark = dc;
1976
+ fur.light = lc;
1977
+ fur.glint = gc;
1978
+ fur.back = bc;
1979
+ fur.backSoft = sc2;
1980
+ fur.mask = mc;
1981
+ file(fur);
1982
+ });
1983
+ return {
1984
+ steps
1985
+ };
1986
+ }
1987
+ function furCut(fur) {
1988
+ if (fur.cut || !fur.mask) return fur.cut;
1989
+ const R = fur.mask.width, cc = makeCanvas(R), cg = cc && ctx2d(cc, false);
1990
+ if (cc && cg) {
1991
+ cg.fillRect(0, 0, R, R);
1992
+ cg.globalCompositeOperation = "destination-out";
1993
+ cg.drawImage(fur.mask, 0, 0);
1994
+ fur.cut = cc;
1995
+ }
1996
+ return fur.cut;
1997
+ }
1998
+ export function buildFabricMatcap(out, c, f, p) {
1999
+ const { V, U, D } = f;
2000
+ const vivid = p.vivid ?? 0, px = [
2001
+ 0,
2002
+ 0,
2003
+ 0
2004
+ ];
2005
+ const L = fabricKey(U, V, p.front);
2006
+ const Lb = fabricBack(f.K, V);
2007
+ const amb = Math.max(0.03, 0.2 - 0.15 * p.shadow);
2008
+ const fillK = Math.max(0.04, 0.24 - 0.13 * p.shadow);
2009
+ const wrap = Math.min(0.7, 0.05 + 0.12 * p.spread);
2010
+ const kd = 1.3;
2011
+ const floorK = 0.34 * Math.min(1.6, p.shadow / 0.35);
2012
+ const Fl = norm3([
2013
+ -0.55 * U[0] + 0.85 * V[0] + 0.15 * D[0],
2014
+ -0.55 * U[1] + 0.85 * V[1] + 0.15 * D[1],
2015
+ -0.55 * U[2] + 0.85 * V[2] + 0.15 * D[2]
2016
+ ]);
2017
+ const satin = p.sheen ?? 0;
2018
+ const backSoft = Math.min(1, Math.max(0, p.backSoft ?? BACK_SOFT));
2019
+ const sheenK = 0.16 * p.highlight * satin, rimK = 1.15 * p.rim * satin, sssK = 0.2 + 0.25 * vivid;
2020
+ const mx = Math.max(c[0], c[1], c[2], 0.02);
2021
+ const cb = [
2022
+ c[0] / mx,
2023
+ c[1] / mx,
2024
+ c[2] / mx
2025
+ ];
2026
+ const rimIn = [
2027
+ cb[0],
2028
+ cb[1],
2029
+ cb[2]
2030
+ ];
2031
+ const wR = 0.45 * (1 - vivid), wS = 0.25 * (1 - vivid);
2032
+ const rimOut = [
2033
+ wR + (1 - wR) * cb[0],
2034
+ wR + (1 - wR) * cb[1],
2035
+ wR + (1 - wR) * cb[2]
2036
+ ];
2037
+ const sheenC = [
2038
+ wS + (1 - wS) * cb[0],
2039
+ wS + (1 - wS) * cb[1],
2040
+ wS + (1 - wS) * cb[2]
2041
+ ];
2042
+ const yOf = (v)=>0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2];
2043
+ const de = 1.8 + 2.2 * vivid;
2044
+ const d0 = [
2045
+ Math.pow(c[0], de),
2046
+ Math.pow(c[1], de),
2047
+ Math.pow(c[2], de)
2048
+ ];
2049
+ const dk = yOf(c) / Math.max(1e-4, yOf(d0));
2050
+ const deep = [
2051
+ d0[0] * dk,
2052
+ d0[1] * dk,
2053
+ d0[2] * dk
2054
+ ];
2055
+ const sc = [
2056
+ cb[0] * cb[0] * c[0],
2057
+ cb[1] * cb[1] * c[1],
2058
+ cb[2] * cb[2] * c[2]
2059
+ ];
2060
+ for(let j = 0; j < M; j++){
2061
+ for(let i = 0; i < M; i++){
2062
+ let nx = i / (M - 1) * 2 - 1, ny = j / (M - 1) * 2 - 1;
2063
+ let r2 = nx * nx + ny * ny;
2064
+ if (r2 > 1.14) continue;
2065
+ if (r2 > 1) {
2066
+ const s = 1 / Math.sqrt(r2);
2067
+ nx *= s;
2068
+ ny *= s;
2069
+ r2 = 1;
2070
+ }
2071
+ const nz = Math.sqrt(1 - r2);
2072
+ const nl = nx * L[0] + ny * L[1] + nz * L[2];
2073
+ const nv = Math.max(0, nx * V[0] + ny * V[1] + nz * V[2]);
2074
+ const nd = nx * D[0] + ny * D[1] + nz * D[2];
2075
+ const nb = nx * Lb[0] + ny * Lb[1] + nz * Lb[2];
2076
+ const q = 1 - nv;
2077
+ const w = wrap + (0.45 - wrap) * q * q;
2078
+ let dif = Math.min(1, Math.max(0, (nl + w) / (1 + w)));
2079
+ dif = Math.pow(dif, 1.35);
2080
+ const fill = fillK * Math.max(0, nx * Fl[0] + ny * Fl[1] + nz * Fl[2]);
2081
+ const floor = 1 - floorK * Math.max(0, nd) * (0.6 + 0.4 * q);
2082
+ const light = (amb + kd * dif + fill) * floor;
2083
+ const t = Math.min(1, Math.max(0, 1 - light));
2084
+ const sb = Math.max(0, 1 - (nl + 0.08) * (nl + 0.08) / 0.18);
2085
+ const sss = sssK * sb * sb * floor;
2086
+ const lit = Math.min(1, Math.max(0, (nl + 0.2) / 0.8));
2087
+ const sheen = sheenK * q * q * lit;
2088
+ const bl = smooth(-0.3, 0.6, nb);
2089
+ const rq = smooth(0.18 - 0.12 * backSoft, 0.62 + 0.18 * backSoft, q);
2090
+ const rim = rimK * rq * Math.sqrt(rq) * (0.3 + 0.7 * bl) * (1 - 0.7 * Math.max(0, nd));
2091
+ const hot = rq * rq * Math.sqrt(rq) * (1 - 0.5 * backSoft);
2092
+ const k = (j * M + i) * 3;
2093
+ let r = 0, g = 0, b = 0;
2094
+ for(let ch = 0; ch < 3; ch++){
2095
+ const base = c[ch] + (deep[ch] - c[ch]) * t * 0.9;
2096
+ const v = base * light + sss * sc[ch] + sheen * sheenC[ch] + rim * (rimIn[ch] + (rimOut[ch] - rimIn[ch]) * hot);
2097
+ if (ch === 0) r = v;
2098
+ else if (ch === 1) g = v;
2099
+ else b = v;
2100
+ }
2101
+ const m = Math.max(r, g, b);
2102
+ if (m > 0.8) {
2103
+ const kk = (0.8 + 0.2 * (1 - Math.exp(-(m - 0.8) / 0.2))) / m;
2104
+ r *= kk;
2105
+ g *= kk;
2106
+ b *= kk;
2107
+ }
2108
+ if (vivid > 0) {
2109
+ px[0] = r;
2110
+ px[1] = g;
2111
+ px[2] = b;
2112
+ vivify(px, vivid, 1);
2113
+ [r, g, b] = px;
2114
+ }
2115
+ out[k] = srgb(r);
2116
+ out[k + 1] = srgb(g);
2117
+ out[k + 2] = srgb(b);
2118
+ }
2119
+ }
2120
+ }
2121
+ const GAP_DEEP = 0.28, GAP_SCATTER = 0.42;
2122
+ const gapFilms = new WeakMap();
2123
+ function gapFilm(fur, color, vivid = 0) {
2124
+ const src = fur.dark;
2125
+ if (!src) return null;
2126
+ let byColor = gapFilms.get(fur);
2127
+ if (!byColor) gapFilms.set(fur, byColor = new Map());
2128
+ const vq = Math.round(Math.min(1, vivid) * 10) / 10;
2129
+ const key = `${color}|${vq}`;
2130
+ const hit = byColor.get(key);
2131
+ if (hit !== undefined) return hit;
2132
+ const c = makeCanvas(src.width), g = c && ctx2d(c, false);
2133
+ if (c && g) {
2134
+ const lin = linearColor(color);
2135
+ const e = [
2136
+ srgb(lin[0]) / 255,
2137
+ srgb(lin[1]) / 255,
2138
+ srgb(lin[2]) / 255
2139
+ ];
2140
+ const mx = Math.max(e[0], e[1], e[2], 1e-3);
2141
+ const lu = 0.2126 * e[0] + 0.7152 * e[1] + 0.0722 * e[2];
2142
+ const pale = Math.min(e[0], e[1], e[2]) / mx;
2143
+ const kd = GAP_DEEP + GAP_SCATTER * lu * lu * pale;
2144
+ const ch = (v)=>Math.round(255 * kd * v * Math.pow(v / mx, 3.2 + 3 * vq));
2145
+ const r = ch(e[0]), gr = ch(e[1]), b = ch(e[2]);
2146
+ const deep = (0.2126 * r + 0.7152 * gr + 0.0722 * b) / 255 / Math.max(1e-3, lu);
2147
+ g.globalAlpha = Math.min(1, Math.sqrt((1 - GAP_REF) / Math.max(0.05, 1 - deep)));
2148
+ g.drawImage(src, 0, 0);
2149
+ g.globalAlpha = 1;
2150
+ g.globalCompositeOperation = "source-in";
2151
+ g.fillStyle = `rgb(${r} ${gr} ${b})`;
2152
+ g.fillRect(0, 0, src.width, src.height);
2153
+ }
2154
+ if (byColor.size >= 4) byColor.clear();
2155
+ byColor.set(key, c && g ? c : null);
2156
+ return c && g ? c : null;
2157
+ }
2158
+ const TIP_TINT = 0.6;
2159
+ const tipFilms = new WeakMap();
2160
+ function tipFilm(fur, color, vivid) {
2161
+ const src = fur.light;
2162
+ if (!src) return null;
2163
+ const v = Math.round((TIP_TINT + (1 - TIP_TINT) * Math.min(1, Math.max(0, vivid))) * 10) / 10;
2164
+ const key = `${color}|${v}`;
2165
+ let byKey = tipFilms.get(fur);
2166
+ if (!byKey) tipFilms.set(fur, byKey = new Map());
2167
+ const hit = byKey.get(key);
2168
+ if (hit !== undefined) return hit;
2169
+ const c = makeCanvas(src.width), g = c && ctx2d(c, false);
2170
+ if (c && g) {
2171
+ g.drawImage(src, 0, 0);
2172
+ g.globalCompositeOperation = "source-in";
2173
+ const lin = linearColor(color);
2174
+ const e = [
2175
+ srgb(lin[0]),
2176
+ srgb(lin[1]),
2177
+ srgb(lin[2])
2178
+ ];
2179
+ const mx = Math.max(e[0], e[1], e[2], 1);
2180
+ const ch = (u)=>Math.round(255 + (255 * u / mx - 255) * Math.min(1, v));
2181
+ g.fillStyle = `rgb(${ch(e[0])} ${ch(e[1])} ${ch(e[2])})`;
2182
+ g.fillRect(0, 0, src.width, src.height);
2183
+ }
2184
+ if (byKey.size >= 6) byKey.clear();
2185
+ byKey.set(key, c && g ? c : null);
2186
+ return c && g ? c : null;
2187
+ }
2188
+ const sheenFilms = new WeakMap();
2189
+ function sheenFilm(fur, color, p) {
2190
+ const gl = fur.glint, bk = fur.back;
2191
+ if (!gl || !bk) return null;
2192
+ const hk = Math.min(1, 0.45 * p.highlight * (p.shine ?? 0)), rk = Math.min(1, 1.1 * p.rim);
2193
+ if (hk <= 0.01 && rk <= 0.01) return null;
2194
+ const vivid = Math.min(1, p.vivid ?? 0);
2195
+ const soft = Math.min(1, Math.max(0, Math.round((p.backSoft ?? BACK_SOFT) * 20) / 20));
2196
+ const key = `${color}|${hk.toFixed(2)}|${rk.toFixed(2)}|${vivid.toFixed(1)}|${soft}`;
2197
+ let byKey = sheenFilms.get(fur);
2198
+ if (!byKey) sheenFilms.set(fur, byKey = new Map());
2199
+ const hit = byKey.get(key);
2200
+ if (hit !== undefined) return hit;
2201
+ const W = gl.width;
2202
+ const c = makeCanvas(W), g = c && ctx2d(c, false);
2203
+ const t = makeCanvas(W), tg = t && ctx2d(t, false);
2204
+ if (c && g && t && tg) {
2205
+ const lin = linearColor(color);
2206
+ const e = [
2207
+ srgb(lin[0]),
2208
+ srgb(lin[1]),
2209
+ srgb(lin[2])
2210
+ ];
2211
+ const mx = Math.max(e[0], e[1], e[2], 1);
2212
+ const mix = (k)=>`rgb(${[
2213
+ 0,
2214
+ 1,
2215
+ 2
2216
+ ].map((i)=>Math.round(255 + (255 * e[i] / mx - 255) * k)).join(" ")})`;
2217
+ const layers = [
2218
+ [
2219
+ bk,
2220
+ mix(0.65 + 0.35 * vivid),
2221
+ rk * (1 - 0.85 * soft)
2222
+ ],
2223
+ [
2224
+ fur.backSoft,
2225
+ mix(0.7 + 0.3 * vivid),
2226
+ rk * soft
2227
+ ],
2228
+ [
2229
+ gl,
2230
+ mix(0.45 + 0.55 * vivid),
2231
+ hk
2232
+ ]
2233
+ ];
2234
+ for (const [src, fill, a] of layers){
2235
+ if (!src) continue;
2236
+ if (a <= 0.01) continue;
2237
+ tg.globalCompositeOperation = "copy";
2238
+ tg.drawImage(src, 0, 0);
2239
+ tg.globalCompositeOperation = "source-in";
2240
+ tg.fillStyle = fill;
2241
+ tg.fillRect(0, 0, W, W);
2242
+ g.globalAlpha = a;
2243
+ g.drawImage(t, 0, 0);
2244
+ }
2245
+ g.globalAlpha = 1;
2246
+ }
2247
+ if (byKey.size >= 6) byKey.clear();
2248
+ byKey.set(key, c && g ? c : null);
2249
+ return c && g ? c : null;
2250
+ }
2251
+ const norm3 = (v)=>{
2252
+ const l = Math.hypot(v[0], v[1], v[2]) || 1;
2253
+ return [
2254
+ v[0] / l,
2255
+ v[1] / l,
2256
+ v[2] / l
2257
+ ];
2258
+ };
2259
+ export function capFrame(r) {
2260
+ const cr = Math.cos(r.roll), sr = Math.sin(r.roll);
2261
+ const lx = cr * r.lx + sr * r.ly, ly = -sr * r.lx + cr * r.ly;
2262
+ const mirror = r.facing < 0 ? -1 : 1;
2263
+ const zf = mirror * Math.min(1, Math.abs(r.facing) / 0.16);
2264
+ const { cy, sy, cp, sp } = r;
2265
+ const local = (x, y, z, zk = zf)=>norm3([
2266
+ cy * x + sy * sp * y - sy * cp * z,
2267
+ cp * y + sp * z,
2268
+ zk * (sy * x - cy * sp * y + cy * cp * z)
2269
+ ]);
2270
+ const L = local(E_XY * lx, E_XY * ly, E_Z);
2271
+ const V = local(0, 0, 1, mirror);
2272
+ const H = norm3([
2273
+ L[0] + V[0],
2274
+ L[1] + V[1],
2275
+ L[2] + V[2]
2276
+ ]);
2277
+ const U = local(lx, ly, 0, mirror);
2278
+ const cw = Math.cos(80 * Math.PI / 180), sw = Math.sin(80 * Math.PI / 180);
2279
+ const wx = cw * lx - sw * ly, wy = sw * lx + cw * ly;
2280
+ const Ws = norm3([
2281
+ 0.55 * wx,
2282
+ 0.55 * wy,
2283
+ 0.83
2284
+ ]);
2285
+ const As = norm3([
2286
+ Ws[1],
2287
+ -Ws[0],
2288
+ 0
2289
+ ]);
2290
+ const Bs = [
2291
+ Ws[1] * As[2] - Ws[2] * As[1],
2292
+ Ws[2] * As[0] - Ws[0] * As[2],
2293
+ Ws[0] * As[1] - Ws[1] * As[0]
2294
+ ];
2295
+ const W = local(Ws[0], Ws[1], Ws[2], mirror), A = local(As[0], As[1], As[2], mirror), B = local(Bs[0], Bs[1], Bs[2], mirror);
2296
+ const D = local(sr, cr, 0, mirror);
2297
+ const [kx0, ky0] = r.bx !== undefined && r.by !== undefined ? [
2298
+ r.bx,
2299
+ r.by
2300
+ ] : backFromKey(r.lx, r.ly);
2301
+ const K = local(cr * kx0 + sr * ky0, -sr * kx0 + cr * ky0, 0, mirror);
2302
+ return {
2303
+ L,
2304
+ V,
2305
+ H,
2306
+ U,
2307
+ W,
2308
+ A,
2309
+ B,
2310
+ D,
2311
+ K
2312
+ };
2313
+ }
2314
+ const states = new WeakMap();
2315
+ function stateFor(ctx, outline) {
2316
+ const key = ctx.canvas ?? ctx;
2317
+ let byOutline = states.get(key);
2318
+ if (!byOutline) {
2319
+ byOutline = new Map();
2320
+ states.set(key, byOutline);
2321
+ }
2322
+ let s = byOutline.get(outline);
2323
+ if (!s) {
2324
+ s = {
2325
+ N: 0,
2326
+ img: null,
2327
+ mc: new Float32Array(MM * 3),
2328
+ mcPrev: new Float32Array(MM * 3),
2329
+ mcMix: new Float32Array(MM * 3),
2330
+ mixVersion: 0,
2331
+ blendT: 1,
2332
+ blendFrames: 1,
2333
+ sinceBuild: 0,
2334
+ L: null,
2335
+ V: null,
2336
+ lx: NaN,
2337
+ ly: NaN,
2338
+ base: "",
2339
+ shadow: NaN,
2340
+ highlight: NaN,
2341
+ spread: NaN,
2342
+ rim: NaN,
2343
+ vivid: NaN,
2344
+ front: NaN,
2345
+ sheen: NaN,
2346
+ backSoft: NaN,
2347
+ K: null,
2348
+ version: 0,
2349
+ imgVersion: -1,
2350
+ imgAoK: NaN,
2351
+ imgForm: null,
2352
+ aoK: -1,
2353
+ aoMul: new Float32Array(256),
2354
+ scratch: [
2355
+ null,
2356
+ null
2357
+ ],
2358
+ scratchIdx: 0,
2359
+ scratchN: 0,
2360
+ scratchStale: true,
2361
+ sprites: [
2362
+ null,
2363
+ null,
2364
+ null,
2365
+ null,
2366
+ null
2367
+ ],
2368
+ sliceG: [],
2369
+ sliceIns: [],
2370
+ sliceSpr: new Map(),
2371
+ sliceSprKey: "",
2372
+ sliceSprMade: new Set(),
2373
+ sliceKey: "",
2374
+ body: [
2375
+ null,
2376
+ null
2377
+ ],
2378
+ bodyIdx: 0,
2379
+ spriteVersion: -1,
2380
+ spritePx: 0,
2381
+ spriteFur: null,
2382
+ miss: [
2383
+ 0,
2384
+ 0
2385
+ ],
2386
+ missAt: [
2387
+ 0,
2388
+ 0
2389
+ ],
2390
+ turnAt: [
2391
+ 0,
2392
+ 0
2393
+ ],
2394
+ fade: null,
2395
+ lay: 1,
2396
+ layTmp: null,
2397
+ haloTmp: null
2398
+ };
2399
+ if (byOutline.size > 4) byOutline.clear();
2400
+ byOutline.set(outline, s);
2401
+ }
2402
+ return s;
2403
+ }
2404
+ const BIN = 1 / 48;
2405
+ const moved = (a, b)=>!b || Math.abs(a[0] - b[0]) >= BIN || Math.abs(a[1] - b[1]) >= BIN || Math.abs(a[2] - b[2]) >= BIN;
2406
+ export function mulAffine(A, B) {
2407
+ return [
2408
+ A[0] * B[0] + A[2] * B[1],
2409
+ A[1] * B[0] + A[3] * B[1],
2410
+ A[0] * B[2] + A[2] * B[3],
2411
+ A[1] * B[2] + A[3] * B[3],
2412
+ A[0] * B[4] + A[2] * B[5] + A[4],
2413
+ A[1] * B[4] + A[3] * B[5] + A[5]
2414
+ ];
2415
+ }
2416
+ function raysOf(img, form, c0) {
2417
+ const { N, sd } = form;
2418
+ const data = img.data;
2419
+ const toG = (v)=>(v + PAD) / SPAN * N - 0.5;
2420
+ const dOut = [], cOut = [];
2421
+ for(let s = 0; s < CONIC_STOPS; s++){
2422
+ const phi = s / CONIC_STOPS * Math.PI * 2, ux = Math.cos(phi), uy = Math.sin(phi);
2423
+ const ds = [], cs = [];
2424
+ for(let r = 0; r < 80; r += 0.5){
2425
+ const x = c0[0] + ux * r, y = c0[1] + uy * r;
2426
+ const d = bilerp(sd, N, toG(x), toG(y));
2427
+ if (d < 0) break;
2428
+ const gx = Math.round(toG(x)), gy = Math.round(toG(y));
2429
+ if (gx < 0 || gy < 0 || gx >= N || gy >= N) break;
2430
+ const k = (gy * N + gx) * 4;
2431
+ ds.push(d);
2432
+ cs.push(data[k], data[k + 1], data[k + 2]);
2433
+ }
2434
+ dOut.push(Float32Array.from(ds));
2435
+ cOut.push(Float32Array.from(cs));
2436
+ }
2437
+ return {
2438
+ d: dOut,
2439
+ c: cOut
2440
+ };
2441
+ }
2442
+ function sliceGradient(ctx, rays, inset, dark, c0, lxy, linear) {
2443
+ const at = (s)=>{
2444
+ const d = rays.d[s], c = rays.c[s];
2445
+ const ins = typeof inset === "number" ? inset : inset[s], k = 1 - (typeof dark === "number" ? dark : dark[s]);
2446
+ let j = 0;
2447
+ for(let i = 0; i < d.length; i++)if (d[i] >= ins) j = i;
2448
+ return c.length ? `rgb(${c[j * 3] * k | 0} ${c[j * 3 + 1] * k | 0} ${c[j * 3 + 2] * k | 0})` : "rgb(0 0 0)";
2449
+ };
2450
+ if (!linear && typeof ctx.createConicGradient === "function") {
2451
+ const g = ctx.createConicGradient(0, c0[0], c0[1]);
2452
+ for(let s = 0; s <= CONIC_STOPS; s++)g.addColorStop(s / CONIC_STOPS, at(s % CONIC_STOPS));
2453
+ return g;
2454
+ }
2455
+ const g = ctx.createLinearGradient(c0[0] + lxy[0] * 50, c0[1] + lxy[1] * 50, c0[0] - lxy[0] * 50, c0[1] - lxy[1] * 50);
2456
+ const ray = (a)=>Math.round((a % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) / (Math.PI * 2 / CONIC_STOPS)) % CONIC_STOPS;
2457
+ g.addColorStop(0, at(ray(Math.atan2(lxy[1], lxy[0]))));
2458
+ g.addColorStop(0.5, at(ray(Math.atan2(lxy[0], -lxy[1]))));
2459
+ g.addColorStop(1, at(ray(Math.atan2(-lxy[1], -lxy[0]))));
2460
+ return g;
2461
+ }
2462
+ function hazeColor(rays, base, rim, vivid = 0) {
2463
+ const m = /rgb\((\d+) (\d+) (\d+)\)/.exec(brightest(rays));
2464
+ const b = m ? [
2465
+ +m[1],
2466
+ +m[2],
2467
+ +m[3]
2468
+ ] : [
2469
+ 255,
2470
+ 255,
2471
+ 255
2472
+ ];
2473
+ const lin = linearColor(base);
2474
+ const e = [
2475
+ srgb(lin[0]),
2476
+ srgb(lin[1]),
2477
+ srgb(lin[2])
2478
+ ];
2479
+ const mx = Math.max(e[0], e[1], e[2], 1);
2480
+ const k = Math.min(0.85, 0.75 * rim);
2481
+ const pale = 0.45 + 0.55 * Math.min(1, vivid);
2482
+ const q = (i)=>Math.round(b[i] + (255 + (255 * e[i] / mx - 255) * pale - b[i]) * k);
2483
+ return `rgb(${q(0)} ${q(1)} ${q(2)})`;
2484
+ }
2485
+ function brightest(rays) {
2486
+ let best = -1, r = 0, g = 0, b = 0;
2487
+ for (const c of rays.c){
2488
+ for(let i = 0; i < c.length; i += 3){
2489
+ const l = 0.2126 * c[i] + 0.7152 * c[i + 1] + 0.0722 * c[i + 2];
2490
+ if (l > best) best = l, r = c[i], g = c[i + 1], b = c[i + 2];
2491
+ }
2492
+ }
2493
+ const q = (v)=>Math.min(255, Math.round(v / 4) * 4);
2494
+ return `rgb(${q(r)} ${q(g)} ${q(b)})`;
2495
+ }
2496
+ function stackReach(rel, K, ex, ey, dl1, vx, vy, h, at) {
2497
+ const v0 = vx * rel.cx[0] + vy * rel.cy[0], ve = (vx * ex + vy * ey) * dl1;
2498
+ let best = h, kb = 0, far = false;
2499
+ for(let k = 1; k < K; k++){
2500
+ const r = vx * rel.cx[k] + vy * rel.cy[k] + rel.scale[k] * (h - v0);
2501
+ if (r + rel.z[k] * ve > best) best = r + rel.z[k] * ve, kb = k, far = false;
2502
+ if (r - rel.z[k] * ve > best) best = r - rel.z[k] * ve, kb = k, far = true;
2503
+ }
2504
+ if (at) at.k = kb, at.far = far;
2505
+ return best;
2506
+ }
2507
+ function haloStretch(form, rel, K, ex, ey, dl1) {
2508
+ if (dl1 <= 1e-4) return [
2509
+ 1,
2510
+ 0,
2511
+ 0,
2512
+ 1,
2513
+ 0,
2514
+ 0
2515
+ ];
2516
+ const rim = rimOf(form);
2517
+ const hp = reach(rim, ex, ey), hn = reach(rim, -ex, -ey);
2518
+ const up = stackReach(rel, K, ex, ey, dl1, ex, ey, hp), un = stackReach(rel, K, ex, ey, dl1, -ex, -ey, hn);
2519
+ const s1 = (up + un) / Math.max(1, hp + hn) - 1, t = up - (s1 + 1) * hp;
2520
+ return [
2521
+ 1 + s1 * ex * ex,
2522
+ s1 * ex * ey,
2523
+ s1 * ex * ey,
2524
+ 1 + s1 * ey * ey,
2525
+ ex * t,
2526
+ ey * t
2527
+ ];
2528
+ }
2529
+ function haloRays(form, rel, K, ex, ey, dl1, S, backShade) {
2530
+ const rim = rimOf(form);
2531
+ const inset = new Float32Array(CONIC_STOPS), dark = new Float32Array(CONIC_STOPS);
2532
+ const s1 = S[0] + S[3] - 2, at = {
2533
+ k: 0,
2534
+ far: false
2535
+ };
2536
+ for(let s = 0; s < CONIC_STOPS; s++){
2537
+ const phi = s / CONIC_STOPS * Math.PI * 2, ux = Math.cos(phi), uy = Math.sin(phi);
2538
+ const ue = ux * ex + uy * ey, q = s1 / (1 + s1);
2539
+ let vx = ux - q * ue * ex, vy = uy - q * ue * ey;
2540
+ const vl = Math.hypot(vx, vy);
2541
+ vx /= vl;
2542
+ vy /= vl;
2543
+ stackReach(rel, K, ex, ey, dl1, vx, vy, reach(rim, vx, vy), at);
2544
+ inset[s] = Math.max(HALO_INSET, rel.inset[at.k]);
2545
+ dark[s] = at.far ? HALO_SHADE * backShade * (1 - Math.exp(-2.4 * (at.k / K))) : 0;
2546
+ }
2547
+ return {
2548
+ inset,
2549
+ dark
2550
+ };
2551
+ }
2552
+ const misses = new WeakMap();
2553
+ const MISS_N = 96, MISS_STEP = 0.1;
2554
+ function haloMiss(form, path, rel, K, S, dxs, dys, sync) {
2555
+ const key = `${K}|${rel.q}|${Math.round(dxs / MISS_STEP)}|${Math.round(dys / MISS_STEP)}`;
2556
+ let byKey = misses.get(form);
2557
+ if (!byKey) misses.set(form, byKey = new Map());
2558
+ const hit = byKey.get(key);
2559
+ if (hit || hit === null && !sync) return hit ?? null;
2560
+ if (byKey.size > 256) byKey.clear();
2561
+ if (!sync) {
2562
+ byKey.set(key, null);
2563
+ idle(()=>{
2564
+ byKey.set(key, measureMiss(form, path, rel, K, S, dxs, dys));
2565
+ });
2566
+ return null;
2567
+ }
2568
+ const r = measureMiss(form, path, rel, K, S, dxs, dys);
2569
+ byKey.set(key, r);
2570
+ return r;
2571
+ }
2572
+ function measureMiss(form, path, rel, K, S, dxs, dys) {
2573
+ const c = makeCanvas(MISS_N), g = c && ctx2d(c, true);
2574
+ if (!g) return [
2575
+ 0,
2576
+ 0
2577
+ ];
2578
+ const u = SPAN / MISS_N;
2579
+ const det = S[0] * S[3] - S[1] * S[2];
2580
+ const ia = S[3] / det, ib = -S[1] / det, ic = -S[2] / det, id = S[0] / det;
2581
+ const ie = -(ia * S[4] + ic * S[5]), jf = -(ib * S[4] + id * S[5]);
2582
+ g.fillStyle = "#fff";
2583
+ for(let k = 0; k < K; k++){
2584
+ for (const z of k ? [
2585
+ rel.z[k],
2586
+ -rel.z[k]
2587
+ ] : [
2588
+ 0
2589
+ ]){
2590
+ g.setTransform(1 / u, 0, 0, 1 / u, PAD / u, PAD / u);
2591
+ g.transform(ia, ib, ic, id, ie, jf);
2592
+ g.translate(z * dxs, z * dys);
2593
+ const sk = rel.scale[k];
2594
+ if (k) g.transform(sk, 0, 0, sk, rel.cx[k] - sk * rel.cx[0], rel.cy[k] - sk * rel.cy[0]);
2595
+ g.fill(path);
2596
+ }
2597
+ }
2598
+ const px = g.getImageData(0, 0, MISS_N, MISS_N).data;
2599
+ const on = new Uint8Array(MISS_N * MISS_N), dist = new Float32Array(MISS_N * MISS_N);
2600
+ for(let i = 0; i < MISS_N * MISS_N; i++)on[i] = px[i * 4 + 3] > 127 ? 1 : 0;
2601
+ edt2d(on, 1, MISS_N, dist, null);
2602
+ let cut = 0, over = 0;
2603
+ for(let y = 0, i = 0; y < MISS_N; y++){
2604
+ for(let x = 0; x < MISS_N; x++, i++){
2605
+ const d = bilerp(form.sd, form.N, (x + 0.5) / MISS_N * form.N - 0.5, (y + 0.5) / MISS_N * form.N - 0.5);
2606
+ if (on[i] && d < 0) cut = Math.max(cut, -d);
2607
+ if (!on[i] && d > 0) over = Math.max(over, Math.sqrt(dist[i]) * u);
2608
+ }
2609
+ }
2610
+ return [
2611
+ cut,
2612
+ over
2613
+ ];
2614
+ }
2615
+ const WEBKIT = typeof navigator !== "undefined" && /AppleWebKit\//.test(navigator.userAgent) && !/Chrome\/|Chromium\/|Edg\//.test(navigator.userAgent);
2616
+ const GECKO = typeof navigator !== "undefined" && /Firefox\//.test(navigator.userAgent);
2617
+ export function drawPlasticCap(ctx, cfg, rig, pal, union, mat) {
2618
+ const N = tierFor(rig.dev);
2619
+ const form = formFor(cfg.typeKey ?? pathId(cfg.path), cfg.path, N, rig.halfDepth, !!rig.still);
2620
+ if (!form) return false;
2621
+ const fabric = cfg.shading === "fabric";
2622
+ const capPx = Math.ceil(SPAN * rig.dev / 100);
2623
+ const lights = (()=>{
2624
+ const [bx, by] = rig.bx !== undefined && rig.by !== undefined ? [
2625
+ rig.bx,
2626
+ rig.by
2627
+ ] : backFromKey(rig.lx, rig.ly);
2628
+ return {
2629
+ bx,
2630
+ by,
2631
+ front: mat.front ?? KEY_EL
2632
+ };
2633
+ })();
2634
+ const fur = fabric ? furReady(form, cfg.typeKey ?? pathId(cfg.path), rig.halfDepth, capPx, !!rig.still && !cfg.furOnIdle, rig.lx, rig.ly, cfg.fur ?? FUR_STOCK, lights) : null;
2635
+ if (fabric && !fur) return false;
2636
+ const st = stateFor(ctx, (cfg.typeKey ?? pathId(cfg.path)) + (fabric ? "|fabric" : ""));
2637
+ const f = capFrame(rig);
2638
+ const lxy = (()=>{
2639
+ const l = Math.hypot(f.L[0], f.L[1]);
2640
+ return l < 0.05 ? [
2641
+ 0,
2642
+ -1
2643
+ ] : [
2644
+ f.L[0] / l,
2645
+ f.L[1] / l
2646
+ ];
2647
+ })();
2648
+ if (moved(f.L, st.L) || moved(f.V, st.V) || rig.lx !== st.lx || rig.ly !== st.ly || pal.base !== st.base || mat.shadow !== st.shadow || mat.highlight !== st.highlight || mat.spread !== st.spread || mat.rim !== st.rim || (mat.vivid ?? 0) !== st.vivid || (mat.front ?? KEY_EL) !== st.front || (mat.sheen ?? 0) !== st.sheen || (mat.backSoft ?? BACK_SOFT) !== st.backSoft || fabric && moved(f.K, st.K)) {
2649
+ if (st.version > 0) st.mcPrev.set(st.mcMix);
2650
+ if (fabric) buildFabricMatcap(st.mc, linearColor(pal.base), f, mat);
2651
+ else buildMatcap(st.mc, linearColor(pal.base), f, mat);
2652
+ if (st.version === 0 || rig.still) {
2653
+ st.mcMix.set(st.mc);
2654
+ st.blendT = 1;
2655
+ } else {
2656
+ st.blendFrames = Math.min(10, Math.max(1, st.sinceBuild));
2657
+ st.blendT = 0;
2658
+ }
2659
+ st.sinceBuild = 0;
2660
+ st.mixVersion++;
2661
+ st.L = f.L;
2662
+ st.V = f.V;
2663
+ st.lx = rig.lx;
2664
+ st.ly = rig.ly;
2665
+ st.base = pal.base;
2666
+ st.shadow = mat.shadow;
2667
+ st.highlight = mat.highlight;
2668
+ st.spread = mat.spread;
2669
+ st.rim = mat.rim;
2670
+ st.vivid = mat.vivid ?? 0;
2671
+ st.front = mat.front ?? KEY_EL;
2672
+ st.sheen = mat.sheen ?? 0;
2673
+ st.backSoft = mat.backSoft ?? BACK_SOFT;
2674
+ st.K = f.K;
2675
+ st.version++;
2676
+ }
2677
+ st.sinceBuild++;
2678
+ if (st.blendT < 1) {
2679
+ st.blendT = Math.min(1, st.blendT + 1 / st.blendFrames);
2680
+ const e = st.blendT >= 1 ? 1 : st.blendT * st.blendT * (3 - 2 * st.blendT);
2681
+ const a = st.mcPrev, b = st.mc, o = st.mcMix;
2682
+ for(let i = 0; i < MM * 3; i++)o[i] = a[i] + (b[i] - a[i]) * e;
2683
+ st.mixVersion++;
2684
+ }
2685
+ const aoK = fur ? Math.min(1.2, 0.3 + 0.8 * mat.shadow) : Math.min(1.3, 1.2 * mat.shadow);
2686
+ if (aoK !== st.aoK) {
2687
+ for(let a = 0; a < 256; a++)st.aoMul[a] = Math.max(0, 1 - aoK * (1 - a / 255));
2688
+ st.aoK = aoK;
2689
+ }
2690
+ if (!st.img || st.N !== N) {
2691
+ st.img = new ImageData(N, N);
2692
+ st.N = N;
2693
+ st.imgVersion = -1;
2694
+ }
2695
+ if (st.imgVersion !== st.mixVersion || st.imgAoK !== aoK || st.imgForm !== form) {
2696
+ shadeTexels(form, st.mcMix, st.img.data, st.aoMul, fur ? edgeLift(form) : undefined);
2697
+ st.imgVersion = st.mixVersion;
2698
+ st.imgAoK = aoK;
2699
+ st.imgForm = form;
2700
+ st.scratchStale = true;
2701
+ }
2702
+ if (st.scratchN !== N) {
2703
+ st.scratch = [
2704
+ null,
2705
+ null
2706
+ ];
2707
+ st.scratchN = N;
2708
+ st.scratchStale = true;
2709
+ }
2710
+ if (st.scratchStale) {
2711
+ st.scratchIdx ^= 1;
2712
+ let sc = st.scratch[st.scratchIdx];
2713
+ if (!sc) {
2714
+ const c = makeCanvas(N);
2715
+ const g = c && ctx2d(c, false);
2716
+ if (!c || !g) return false;
2717
+ sc = st.scratch[st.scratchIdx] = {
2718
+ c,
2719
+ g
2720
+ };
2721
+ }
2722
+ sc.g.putImageData(st.img, 0, 0);
2723
+ st.scratchStale = false;
2724
+ }
2725
+ const sc = st.scratch[st.scratchIdx];
2726
+ const capSrc = sc.c;
2727
+ const K = rig.dev <= 100 ? 14 : rig.dev <= 224 ? 20 : 26;
2728
+ const rel = reliefFor(form, K, rig.round ?? 1);
2729
+ const { cy, sy, cp, sp } = rig;
2730
+ const [ca, cb, cc, cd, ce, cf] = rig.ctm;
2731
+ const m1 = sy * sp;
2732
+ const ta = ca * cy + cc * m1, tb = cb * cy + cd * m1, tc = cc * cp, td = cd * cp;
2733
+ ctx.imageSmoothingEnabled = true;
2734
+ ctx.imageSmoothingQuality = "high";
2735
+ const near = rig.facing >= 0 ? 1 : -1;
2736
+ const dxs = sy / cy, dys = -sp / (cy * cp);
2737
+ const dl1 = Math.hypot(dxs, dys);
2738
+ let lead = 50;
2739
+ for(let k = 1; k < K; k++)lead = Math.max(lead, 50 * rel.scale[k] + rel.z[k] * dl1);
2740
+ const ex = dl1 > 1e-6 ? near * dxs / dl1 : 1, ey = dl1 > 1e-6 ? near * dys / dl1 : 0;
2741
+ const backShade = Math.min(0.7, 0.55 * Math.min(1.6, mat.shadow / 0.35));
2742
+ const haloS = fur ? haloStretch(form, rel, K, ex, ey, dl1) : null;
2743
+ const spx = Math.ceil(SPAN * rig.dev / 100);
2744
+ if (st.spritePx !== spx) {
2745
+ st.sprites = [
2746
+ null,
2747
+ null,
2748
+ null,
2749
+ null,
2750
+ null
2751
+ ];
2752
+ st.spritePx = spx;
2753
+ st.spriteVersion = -1;
2754
+ }
2755
+ if (st.spriteVersion !== st.version || st.spriteFur !== fur) {
2756
+ const k = spx / SPAN;
2757
+ const soft = fur ? softMask(form, fur.R) : null;
2758
+ const rays = raysOf(st.img, form, [
2759
+ rel.cx[0],
2760
+ rel.cy[0]
2761
+ ]);
2762
+ for(let i = 0; i < 4; i++){
2763
+ if (i === 2) continue;
2764
+ let spr = st.sprites[i];
2765
+ if (!spr) {
2766
+ const c = makeCanvas(spx);
2767
+ const g = c && ctx2d(c, false);
2768
+ if (!c || !g) return false;
2769
+ spr = st.sprites[i] = {
2770
+ c,
2771
+ g
2772
+ };
2773
+ }
2774
+ const g = spr.g;
2775
+ g.setTransform(1, 0, 0, 1, 0, 0);
2776
+ g.globalCompositeOperation = "source-over";
2777
+ g.clearRect(0, 0, spx, spx);
2778
+ g.imageSmoothingEnabled = true;
2779
+ g.imageSmoothingQuality = "high";
2780
+ const halo = i === 0 && fur && fur.mask ? fur : null;
2781
+ if (halo && halo.haze) {
2782
+ g.imageSmoothingQuality = "low";
2783
+ g.drawImage(halo.haze, 0, 0, spx, spx);
2784
+ g.imageSmoothingQuality = "high";
2785
+ g.globalCompositeOperation = "source-in";
2786
+ g.fillStyle = hazeColor(rays, pal.base, mat.rim, mat.vivid ?? 0);
2787
+ g.fillRect(0, 0, spx, spx);
2788
+ g.globalCompositeOperation = "destination-over";
2789
+ }
2790
+ g.setTransform(k, 0, 0, k, PAD * k, PAD * k);
2791
+ const edge = halo && haloS ? haloRays(form, rel, K, ex, ey, dl1, haloS, backShade) : null;
2792
+ g.fillStyle = i === 1 ? "#000" : sliceGradient(g, rays, edge ? edge.inset : fur ? Math.max(HALO_INSET, rel.inset[0]) : rel.inset[0], edge ? edge.dark : 0, [
2793
+ rel.cx[0],
2794
+ rel.cy[0]
2795
+ ], lxy, WEBKIT);
2796
+ if (fur && fur.mask) {
2797
+ g.fillRect(-PAD, -PAD, SPAN, SPAN);
2798
+ g.setTransform(1, 0, 0, 1, 0, 0);
2799
+ g.globalCompositeOperation = "destination-in";
2800
+ g.drawImage((i === 0 ? fur.mask : soft) ?? fur.mask, 0, 0, spx, spx);
2801
+ g.globalCompositeOperation = "source-over";
2802
+ } else g.fill(cfg.path);
2803
+ }
2804
+ if (fur && fur.core) {
2805
+ let band = st.sprites[4];
2806
+ if (!band) {
2807
+ const c = makeCanvas(spx);
2808
+ const bg = c && ctx2d(c, false);
2809
+ if (c && bg) band = st.sprites[4] = {
2810
+ c,
2811
+ g: bg
2812
+ };
2813
+ }
2814
+ if (band) {
2815
+ band.g.setTransform(1, 0, 0, 1, 0, 0);
2816
+ band.g.globalCompositeOperation = "copy";
2817
+ band.g.drawImage(st.sprites[0].c, 0, 0);
2818
+ band.g.globalCompositeOperation = "destination-out";
2819
+ band.g.drawImage(fur.core, 0, 0, spx, spx);
2820
+ band.g.globalCompositeOperation = "source-over";
2821
+ }
2822
+ }
2823
+ st.spriteVersion = st.version;
2824
+ st.spriteFur = fur;
2825
+ }
2826
+ const stretch = (lead + 50) / 100, shift = (lead - 50) / 2;
2827
+ const A = 1 + (stretch - 1) * ex * ex, B = (stretch - 1) * ex * ey, D = 1 + (stretch - 1) * ey * ey;
2828
+ const capM = mulAffine(mulAffine(rig.ctm, [
2829
+ cy,
2830
+ m1,
2831
+ 0,
2832
+ cp,
2833
+ 0,
2834
+ 0
2835
+ ]), [
2836
+ A,
2837
+ B,
2838
+ B,
2839
+ D,
2840
+ shift * ex - 50 * A - 50 * B,
2841
+ shift * ey - 50 * B - 50 * D
2842
+ ]);
2843
+ const film = fur?.dark && fur.light ? fur : null;
2844
+ let g = ctx, ox = 0, oy = 0, bw = 0, bh = 0;
2845
+ if (film) {
2846
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
2847
+ for (const z of [
2848
+ -rel.top,
2849
+ rel.top
2850
+ ]){
2851
+ const e = z * sy - 50 * cy, fo = -z * cy * sp - 50 * m1 - 50 * cp;
2852
+ const E = ca * e + cc * fo + ce, F = cb * e + cd * fo + cf;
2853
+ for (const x of [
2854
+ -PAD,
2855
+ 100 + PAD
2856
+ ])for (const y of [
2857
+ -PAD,
2858
+ 100 + PAD
2859
+ ]){
2860
+ const X = ta * x + tc * y + E, Y = tb * x + td * y + F;
2861
+ if (X < x0) x0 = X;
2862
+ if (X > x1) x1 = X;
2863
+ if (Y < y0) y0 = Y;
2864
+ if (Y > y1) y1 = Y;
2865
+ }
2866
+ }
2867
+ ox = Math.floor(x0) - 1;
2868
+ oy = Math.floor(y0) - 1;
2869
+ bw = Math.ceil(x1) + 1 - ox;
2870
+ bh = Math.ceil(y1) + 1 - oy;
2871
+ st.bodyIdx ^= 1;
2872
+ let b = st.body[st.bodyIdx];
2873
+ if (!b || b.w < bw || b.h < bh || b.w > 2 * bw + 64 || b.h > 2 * bh + 64) {
2874
+ const w = Math.ceil(bw / 64) * 64, h = Math.ceil(bh / 64) * 64;
2875
+ let c = null;
2876
+ if (typeof document !== "undefined") {
2877
+ c = document.createElement("canvas");
2878
+ c.width = w;
2879
+ c.height = h;
2880
+ } else if (typeof OffscreenCanvas !== "undefined") c = new OffscreenCanvas(w, h);
2881
+ const cg = c && ctx2d(c, false);
2882
+ b = c && cg ? st.body[st.bodyIdx] = {
2883
+ c,
2884
+ g: cg,
2885
+ w: c.width,
2886
+ h: c.height
2887
+ } : null;
2888
+ }
2889
+ if (b) {
2890
+ g = b.g;
2891
+ g.setTransform(1, 0, 0, 1, 0, 0);
2892
+ g.globalCompositeOperation = "source-over";
2893
+ g.globalAlpha = 1;
2894
+ g.clearRect(0, 0, bw, bh);
2895
+ g.imageSmoothingEnabled = true;
2896
+ g.imageSmoothingQuality = "high";
2897
+ } else ox = oy = 0;
2898
+ }
2899
+ const at = (z)=>{
2900
+ const e = z * sy - 50 * cy, fo = -z * cy * sp - 50 * m1 - 50 * cp;
2901
+ g.setTransform(ta, tb, tc, td, ca * e + cc * fo + ce - ox, cb * e + cd * fo + cf - oy);
2902
+ };
2903
+ const [fringed, black, , side] = st.sprites;
2904
+ const c0 = [
2905
+ rel.cx[0],
2906
+ rel.cy[0]
2907
+ ];
2908
+ const sliceKey = `${st.version}|${K}|${rel.q}`;
2909
+ if (st.sliceKey !== sliceKey) {
2910
+ const rays = raysOf(st.img, form, c0);
2911
+ st.sliceG = [];
2912
+ st.sliceIns = [];
2913
+ for(let k = 0; k < K; k++){
2914
+ const inset = fur ? Math.max(HALO_INSET, rel.inset[k]) : rel.inset[k];
2915
+ st.sliceIns.push(inset);
2916
+ st.sliceG.push(sliceGradient(ctx, rays, inset, 0, c0, lxy, WEBKIT));
2917
+ }
2918
+ st.sliceKey = sliceKey;
2919
+ }
2920
+ const useSpr = GECKO && typeof document !== "undefined";
2921
+ const sprKey = `${sliceKey}|${spx}`;
2922
+ if (useSpr && st.sliceSprKey !== sprKey) {
2923
+ st.sliceSprKey = sprKey;
2924
+ st.sliceSprMade = new Set();
2925
+ if (st.sliceSpr.size > 32) st.sliceSpr.clear();
2926
+ }
2927
+ const sprite = (k)=>{
2928
+ const ins = st.sliceIns[k];
2929
+ let sp = st.sliceSpr.get(ins);
2930
+ if (sp && st.sliceSprMade.has(ins)) return sp;
2931
+ if (!sp || sp.c.width !== spx) {
2932
+ const c = document.createElement("canvas");
2933
+ c.width = c.height = spx;
2934
+ const cg = c.getContext("2d");
2935
+ if (!cg) return undefined;
2936
+ sp = {
2937
+ c,
2938
+ g: cg
2939
+ };
2940
+ st.sliceSpr.set(ins, sp);
2941
+ }
2942
+ const kk = spx / SPAN;
2943
+ sp.g.setTransform(1, 0, 0, 1, 0, 0);
2944
+ sp.g.clearRect(0, 0, spx, spx);
2945
+ sp.g.setTransform(kk, 0, 0, kk, PAD * kk, PAD * kk);
2946
+ sp.g.fillStyle = st.sliceG[k];
2947
+ sp.g.fill(cfg.path);
2948
+ st.sliceSprMade.add(ins);
2949
+ return sp;
2950
+ };
2951
+ const slice = (k, z, dark)=>{
2952
+ at(z);
2953
+ const sk = rel.scale[k];
2954
+ if (k > 0) {
2955
+ g.transform(sk, 0, 0, sk, rel.cx[k] - sk * rel.cx[0], rel.cy[k] - sk * rel.cy[0]);
2956
+ const spr = useSpr ? sprite(k) : undefined;
2957
+ if (spr) g.drawImage(spr.c, -PAD, -PAD, SPAN, SPAN);
2958
+ else {
2959
+ g.fillStyle = st.sliceG[k];
2960
+ g.fill(cfg.path);
2961
+ }
2962
+ if (dark > 0.01) {
2963
+ g.globalAlpha = dark;
2964
+ g.fillStyle = "#000";
2965
+ g.fill(cfg.path);
2966
+ g.globalAlpha = 1;
2967
+ }
2968
+ return;
2969
+ }
2970
+ if (fur) g.drawImage(side.c, -PAD, -PAD, SPAN, SPAN);
2971
+ else {
2972
+ g.fillStyle = st.sliceG[0];
2973
+ g.fill(cfg.path);
2974
+ }
2975
+ if (fur && g === ctx && fringeA > 0.01) {
2976
+ g.globalAlpha = fringeA;
2977
+ g.drawImage(fringed.c, -PAD, -PAD, SPAN, SPAN);
2978
+ g.globalAlpha = 1;
2979
+ }
2980
+ if (dark > 0.01) {
2981
+ g.globalAlpha = dark;
2982
+ g.drawImage(black.c, -PAD, -PAD, SPAN, SPAN);
2983
+ g.globalAlpha = 1;
2984
+ }
2985
+ };
2986
+ const faceOn = Math.abs(rig.facing);
2987
+ const fringeA = faceOn >= 0.8 ? 1 : faceOn <= 0.45 ? 0 : (faceOn - 0.45) / 0.35;
2988
+ const ring = ringOf(form), rn = ring.x.length, sd = form.sd, NF = form.N;
2989
+ const hideBy = 1.5 / (Math.sqrt(Math.abs(ta * td - tb * tc)) || 1) + 0.4;
2990
+ const texel = SPAN / NF;
2991
+ const toG = (v)=>(v + PAD) / SPAN * NF - 0.5;
2992
+ const leanFar = Math.atan2(-near * dys, -near * dxs), leanNear = Math.atan2(near * dys, near * dxs);
2993
+ for(let k = K - 1; k >= 1; k--){
2994
+ const z = -near * rel.z[k], sk = rel.scale[k];
2995
+ if (fur) {
2996
+ const bx = rel.cx[k] - sk * rel.cx[0] + z * dxs, by = rel.cy[k] - sk * rel.cy[0] + z * dys;
2997
+ let hidden = Number.isFinite(bx) && Number.isFinite(by);
2998
+ for(let j = 0; hidden && j < rn; j++){
2999
+ const i = ringOrder(rn, leanFar, j);
3000
+ if (bilerp(sd, NF, toG(sk * ring.x[i] + bx), toG(sk * ring.y[i] + by)) < hideBy + sk * (ring.d[i] + texel)) hidden = false;
3001
+ }
3002
+ if (hidden) continue;
3003
+ }
3004
+ slice(k, z, fur ? 0 : backShade * (1 - Math.exp(-2.4 * (k / K))));
3005
+ }
3006
+ if (fur && dl1 > 1e-3) {
3007
+ const hn = reach(rimOf(form), -ex, -ey), span = 1.3 * rel.top * dl1;
3008
+ at(0);
3009
+ const ramp = g.createLinearGradient(-ex * hn, -ey * hn, -ex * (hn + span), -ey * (hn + span));
3010
+ for(let s = 0; s <= 8; s++){
3011
+ const t = s / 8;
3012
+ ramp.addColorStop(t, `rgba(0,0,0,${(backShade * (1 - Math.exp(-2.4 * t)) * t * t * (3 - 2 * t)).toFixed(3)})`);
3013
+ }
3014
+ g.globalCompositeOperation = "source-atop";
3015
+ g.fillStyle = ramp;
3016
+ g.fillRect(-PAD - 60, -PAD - 60, SPAN + 120, SPAN + 120);
3017
+ g.globalCompositeOperation = "source-over";
3018
+ }
3019
+ slice(0, 0, 0);
3020
+ const face = Math.abs(rig.facing);
3021
+ const frontA = face >= 0.72 ? 1 : face <= 0.34 ? 0 : ((face - 0.34) / 0.38) ** 1.5;
3022
+ const cdet = capM[0] * capM[3] - capM[1] * capM[2];
3023
+ const coverBy = FRONT_FADE + 1.5 / (Math.sqrt(Math.abs(cdet)) || 1);
3024
+ for(let k = 1; k < K; k++){
3025
+ const z = near * rel.z[k], sk = rel.scale[k];
3026
+ if (frontA >= 1 && Math.abs(cdet) > 1e-9) {
3027
+ const e = z * sy - 50 * cy, fo = -z * cy * sp - 50 * m1 - 50 * cp;
3028
+ const E = ca * e + cc * fo + ce - capM[4], F = cb * e + cd * fo + cf - capM[5];
3029
+ const ux = rel.cx[k] - sk * rel.cx[0], uy = rel.cy[k] - sk * rel.cy[0];
3030
+ let covered = true;
3031
+ for(let j = 0; covered && j < rn; j++){
3032
+ const i = ringOrder(rn, leanNear, j);
3033
+ const u = sk * ring.x[i] + ux, v = sk * ring.y[i] + uy;
3034
+ const X = ta * u + tc * v + E, Y = tb * u + td * v + F;
3035
+ const qx = (capM[3] * X - capM[2] * Y) / cdet, qy = (capM[0] * Y - capM[1] * X) / cdet;
3036
+ if (bilerp(sd, NF, toG(qx), toG(qy)) < coverBy + sk * (ring.d[i] + texel)) covered = false;
3037
+ }
3038
+ if (covered) continue;
3039
+ }
3040
+ slice(k, z, 0);
3041
+ }
3042
+ if (frontA > 0.01) {
3043
+ g.save();
3044
+ g.setTransform(capM[0], capM[1], capM[2], capM[3], capM[4] - ox, capM[5] - oy);
3045
+ g.globalAlpha = frontA;
3046
+ g.drawImage(capSrc, -PAD, -PAD, SPAN, SPAN);
3047
+ g.restore();
3048
+ }
3049
+ if (fur && fur.mask && haloS && g !== ctx) {
3050
+ const mx = near * dxs, my = near * dys;
3051
+ const stale = Math.abs(mx - st.missAt[0]) > 1.5 * MISS_STEP || Math.abs(my - st.missAt[1]) > 1.5 * MISS_STEP;
3052
+ const measured = dl1 > 1e-4 ? haloMiss(form, cfg.path, rel, K, haloS, mx, my, !!rig.still || stale) : [
3053
+ 0,
3054
+ 0
3055
+ ];
3056
+ if (measured) {
3057
+ st.miss = measured;
3058
+ st.missAt = [
3059
+ mx,
3060
+ my
3061
+ ];
3062
+ }
3063
+ const [cut, over] = st.miss;
3064
+ const miss = Math.max(cut, over);
3065
+ let back = smooth(HALO_FIT / 2, 2 * HALO_FIT, miss), gone = smooth(HALO_FIT / 2, HALO_FIT, over);
3066
+ const quick = Math.abs(mx - st.turnAt[0]) > MISS_STEP || Math.abs(my - st.turnAt[1]) > MISS_STEP;
3067
+ st.turnAt = [
3068
+ mx,
3069
+ my
3070
+ ];
3071
+ const settle = !rig.still && !quick;
3072
+ if (settle && st.fade) {
3073
+ const ease = (a, b)=>Math.abs(b - a) < 0.02 ? b : a + (b - a) * 0.2;
3074
+ back = ease(st.fade[0], back);
3075
+ gone = ease(st.fade[1], gone);
3076
+ }
3077
+ st.fade = [
3078
+ back,
3079
+ gone
3080
+ ];
3081
+ const band = st.sprites[4];
3082
+ g.imageSmoothingQuality = "low";
3083
+ at(0);
3084
+ g.transform(haloS[0], haloS[1], haloS[2], haloS[3], haloS[4], haloS[5]);
3085
+ const layTo = (1 - smooth(0, HALO_BLEND, back)) * (1 - gone);
3086
+ const lay = st.lay = !settle ? layTo : st.lay + Math.max(-HALO_EASE, Math.min(HALO_EASE, layTo - st.lay));
3087
+ let h = null;
3088
+ if (lay > 0 && lay < 1) {
3089
+ let t = st.layTmp;
3090
+ if (!t || t.c.width < bw || t.c.height < bh) {
3091
+ const n = Math.ceil(Math.max(bw, bh) / 64) * 64;
3092
+ let c = null;
3093
+ if (typeof document !== "undefined") {
3094
+ c = document.createElement("canvas");
3095
+ c.width = c.height = n;
3096
+ } else c = makeCanvas(n);
3097
+ const cg = c && ctx2d(c, false);
3098
+ t = st.layTmp = c && cg ? {
3099
+ c,
3100
+ g: cg
3101
+ } : null;
3102
+ }
3103
+ if (t) {
3104
+ h = t.g;
3105
+ h.setTransform(1, 0, 0, 1, 0, 0);
3106
+ h.globalAlpha = 1;
3107
+ h.globalCompositeOperation = "copy";
3108
+ h.drawImage(g.canvas, 0, 0, bw, bh, 0, 0, bw, bh);
3109
+ h.imageSmoothingQuality = "low";
3110
+ h.setTransform(g.getTransform());
3111
+ }
3112
+ }
3113
+ const veil = HALO_VEIL * Math.min(1, dl1 / 0.2);
3114
+ const laid = (d)=>{
3115
+ if (WEBKIT) {
3116
+ let t = st.haloTmp;
3117
+ if (!t || t.c.width < bw || t.c.height < bh) {
3118
+ const n = Math.ceil(Math.max(bw, bh) / 64) * 64;
3119
+ let c = null;
3120
+ if (typeof document !== "undefined") {
3121
+ c = document.createElement("canvas");
3122
+ c.width = c.height = n;
3123
+ } else c = makeCanvas(n);
3124
+ const cg = c && ctx2d(c, false);
3125
+ t = st.haloTmp = c && cg ? {
3126
+ c,
3127
+ g: cg
3128
+ } : null;
3129
+ }
3130
+ if (t) {
3131
+ const tg = t.g;
3132
+ tg.setTransform(1, 0, 0, 1, 0, 0);
3133
+ tg.globalCompositeOperation = "source-over";
3134
+ tg.globalAlpha = 1;
3135
+ tg.clearRect(0, 0, bw, bh);
3136
+ tg.imageSmoothingEnabled = true;
3137
+ tg.imageSmoothingQuality = "low";
3138
+ tg.setTransform(d.getTransform());
3139
+ tg.drawImage(fringed.c, -PAD, -PAD, SPAN, SPAN);
3140
+ d.save();
3141
+ d.setTransform(1, 0, 0, 1, 0, 0);
3142
+ d.globalCompositeOperation = "destination-atop";
3143
+ d.drawImage(t.c, 0, 0, bw, bh, 0, 0, bw, bh);
3144
+ d.restore();
3145
+ }
3146
+ } else {
3147
+ d.globalCompositeOperation = "destination-atop";
3148
+ d.drawImage(fringed.c, -PAD, -PAD, SPAN, SPAN);
3149
+ }
3150
+ if (band && veil > 0.01) {
3151
+ d.globalCompositeOperation = "source-atop";
3152
+ d.globalAlpha = veil;
3153
+ d.drawImage(band.c, -PAD, -PAD, SPAN, SPAN);
3154
+ d.globalAlpha = 1;
3155
+ }
3156
+ d.globalCompositeOperation = "source-over";
3157
+ };
3158
+ if (h) laid(h);
3159
+ if (lay >= 1 || !h && lay >= 0.5) laid(g);
3160
+ else {
3161
+ const cutAway = back < 1 ? furCut(fur) : null;
3162
+ if (cutAway) {
3163
+ const f = 1 + back * (miss + HALO_FIT) / 25, cx = rel.cx[0], cy = rel.cy[0];
3164
+ g.save();
3165
+ g.transform(f, 0, 0, f, cx * (1 - f), cy * (1 - f));
3166
+ g.globalCompositeOperation = "destination-out";
3167
+ g.drawImage(cutAway, -PAD, -PAD, SPAN, SPAN);
3168
+ g.restore();
3169
+ }
3170
+ if (gone < 1) {
3171
+ g.globalAlpha = 1 - gone;
3172
+ g.globalCompositeOperation = "destination-over";
3173
+ g.drawImage(fringed.c, -PAD, -PAD, SPAN, SPAN);
3174
+ g.globalAlpha = 1;
3175
+ }
3176
+ if (h) {
3177
+ g.setTransform(1, 0, 0, 1, 0, 0);
3178
+ g.globalCompositeOperation = "destination-in";
3179
+ g.globalAlpha = 1 - lay;
3180
+ g.fillStyle = "#000";
3181
+ g.fillRect(0, 0, bw, bh);
3182
+ g.globalCompositeOperation = "lighter";
3183
+ g.globalAlpha = lay;
3184
+ g.drawImage(h.canvas, 0, 0, bw, bh, 0, 0, bw, bh);
3185
+ g.globalAlpha = 1;
3186
+ }
3187
+ }
3188
+ g.globalCompositeOperation = "source-over";
3189
+ }
3190
+ if (g !== ctx && film) {
3191
+ g.globalCompositeOperation = "source-atop";
3192
+ g.imageSmoothingQuality = "low";
3193
+ const lin = linearColor(pal.base);
3194
+ const lum = Math.pow(0.2126 * lin[0] + 0.7152 * lin[1] + 0.0722 * lin[2], 1 / 2.2);
3195
+ const dark = gapFilm(film, pal.base, mat.vivid ?? 0) ?? film.dark, tips = tipFilm(film, pal.base, mat.vivid ?? 0) ?? film.light;
3196
+ const sheenA = rig.dev < 160 ? 0 : face >= 0.88 ? 1 : face <= 0.55 ? 0 : (face - 0.55) / 0.33;
3197
+ const sheen = sheenA > 0.01 ? sheenFilm(film, pal.base, mat) : null;
3198
+ const lift = near * PILE_H * (cfg.fur ?? FUR_STOCK).length;
3199
+ const capT = mulAffine(mulAffine(mulAffine(rig.ctm, [
3200
+ 1,
3201
+ 0,
3202
+ 0,
3203
+ 1,
3204
+ lift * sy,
3205
+ -lift * cy * sp
3206
+ ]), [
3207
+ cy,
3208
+ m1,
3209
+ 0,
3210
+ cp,
3211
+ 0,
3212
+ 0
3213
+ ]), [
3214
+ A,
3215
+ B,
3216
+ B,
3217
+ D,
3218
+ shift * ex - 50 * A - 50 * B,
3219
+ shift * ey - 50 * B - 50 * D
3220
+ ]);
3221
+ const lay = (M, a, withSheen, T = M)=>{
3222
+ g.setTransform(M[0], M[1], M[2], M[3], M[4] - ox, M[5] - oy);
3223
+ g.globalAlpha = a;
3224
+ g.drawImage(dark, -PAD - FILM_M, -PAD - FILM_M, FILM_SPAN, FILM_SPAN);
3225
+ if (T !== M) g.setTransform(T[0], T[1], T[2], T[3], T[4] - ox, T[5] - oy);
3226
+ g.globalAlpha = a * (0.25 + 0.75 * lum);
3227
+ g.drawImage(tips, -PAD - FILM_M, -PAD - FILM_M, FILM_SPAN, FILM_SPAN);
3228
+ if (withSheen && sheen) {
3229
+ g.globalAlpha = a * sheenA;
3230
+ g.drawImage(sheen, -PAD - FILM_M, -PAD - FILM_M, FILM_SPAN, FILM_SPAN);
3231
+ }
3232
+ g.globalAlpha = 1;
3233
+ };
3234
+ const sideU = face >= 0.62 ? 0 : face <= 0.36 ? 1 : (0.62 - face) / 0.26;
3235
+ if (sideU < 1) lay(capM, 1 - sideU, true, capT);
3236
+ if (sideU > 0) {
3237
+ const k = rig.dev / 100, cxs = ox + bw / 2, cys = oy + bh / 2;
3238
+ lay([
3239
+ k,
3240
+ 0,
3241
+ 0,
3242
+ k,
3243
+ cxs - 50 * k,
3244
+ cys - 50 * k
3245
+ ], sideU, false);
3246
+ }
3247
+ g.globalCompositeOperation = "source-over";
3248
+ ctx.save();
3249
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
3250
+ ctx.imageSmoothingEnabled = false;
3251
+ ctx.drawImage(g.canvas, 0, 0, bw, bh, ox, oy, bw, bh);
3252
+ ctx.restore();
3253
+ }
3254
+ ctx.setTransform(ca, cb, cc, cd, ce, cf);
3255
+ const hairA = face >= 0.95 ? 1 : face <= 0.8 ? 0 : (face - 0.8) / 0.15;
3256
+ if (!fur && rig.dev >= 256 && mat.rim > 0 && mat.highlight > 0 && hairA > 0.01) {
3257
+ ctx.save();
3258
+ if (union) ctx.clip(union);
3259
+ else ctx.globalCompositeOperation = "source-atop";
3260
+ ctx.setTransform(capM[0], capM[1], capM[2], capM[3], capM[4], capM[5]);
3261
+ const al = hairA * Math.min(0.5, 0.3 * mat.rim * Math.min(1.4, mat.highlight));
3262
+ const g = ctx.createLinearGradient(50 + lxy[0] * 50, 50 + lxy[1] * 50, 50 - lxy[0] * 50, 50 - lxy[1] * 50);
3263
+ g.addColorStop(0, `rgba(235,244,255,${al.toFixed(3)})`);
3264
+ g.addColorStop(0.45, `rgba(235,244,255,${(0.35 * al).toFixed(3)})`);
3265
+ g.addColorStop(0.75, "rgba(235,244,255,0)");
3266
+ ctx.strokeStyle = g;
3267
+ ctx.lineJoin = "round";
3268
+ ctx.lineWidth = 1.3;
3269
+ ctx.stroke(cfg.path);
3270
+ ctx.restore();
3271
+ }
3272
+ return true;
3273
+ }