topotopo 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (336) hide show
  1. package/NOTICE +10 -10
  2. package/README.md +460 -117
  3. package/dist/cli.js +323 -146
  4. package/dist/config.d.ts +48 -0
  5. package/dist/config.js +170 -0
  6. package/dist/core/bake.d.ts +14 -3
  7. package/dist/core/bake.js +153 -4
  8. package/dist/core/bevel.d.ts +3 -1
  9. package/dist/core/bevel.js +76 -3
  10. package/dist/core/decimate.d.ts +65 -0
  11. package/dist/core/decimate.js +409 -3
  12. package/dist/core/erosion.d.ts +37 -0
  13. package/dist/core/erosion.js +41 -1
  14. package/dist/core/geometry.d.ts +5 -1
  15. package/dist/core/geometry.js +203 -3
  16. package/dist/core/heightfield.d.ts +27 -4
  17. package/dist/core/heightfield.js +54 -16
  18. package/dist/core/intrinsics.d.ts +7 -0
  19. package/dist/core/intrinsics.js +94 -0
  20. package/dist/core/manifold.d.ts +12 -1
  21. package/dist/core/manifold.js +78 -6
  22. package/dist/core/materials.d.ts +7 -0
  23. package/dist/core/materials.js +45 -0
  24. package/dist/core/noise.d.ts +9 -0
  25. package/dist/core/noise.js +10 -1
  26. package/dist/core/polymerge.d.ts +21 -0
  27. package/dist/core/polymerge.js +327 -0
  28. package/dist/core/repair.d.ts +2 -0
  29. package/dist/core/repair.js +2 -0
  30. package/dist/core/report.d.ts +70 -0
  31. package/dist/core/report.js +275 -0
  32. package/dist/core/scene.js +2 -1
  33. package/dist/core/sha256.d.ts +4 -0
  34. package/dist/core/sha256.js +69 -0
  35. package/dist/core/shapes.d.ts +3 -0
  36. package/dist/core/shapes.js +34 -0
  37. package/dist/core/snippet-lib.d.ts +14 -1
  38. package/dist/core/snippet-lib.js +39 -13
  39. package/dist/core/tangents.d.ts +2 -0
  40. package/dist/core/tangents.js +5 -1
  41. package/dist/core/texnoise.d.ts +6 -1
  42. package/dist/core/texnoise.js +6 -2
  43. package/dist/core/texsdf.d.ts +131 -0
  44. package/dist/core/texsdf.js +637 -0
  45. package/dist/core/texture.d.ts +163 -9
  46. package/dist/core/texture.js +844 -20
  47. package/dist/core/topology.d.ts +10 -0
  48. package/dist/core/topology.js +168 -0
  49. package/dist/core/unwrap.d.ts +72 -0
  50. package/dist/core/unwrap.js +800 -0
  51. package/dist/core/usercode.d.ts +27 -0
  52. package/dist/core/usercode.js +87 -0
  53. package/dist/core/uv.d.ts +4 -18
  54. package/dist/core/uv.js +35 -188
  55. package/dist/core/uvislands.d.ts +62 -0
  56. package/dist/core/uvislands.js +574 -0
  57. package/dist/core/uvoverlap.d.ts +58 -0
  58. package/dist/core/uvoverlap.js +220 -0
  59. package/dist/core/uvpack.d.ts +64 -0
  60. package/dist/core/uvpack.js +628 -0
  61. package/dist/core/uvreport.d.ts +61 -0
  62. package/dist/core/uvreport.js +207 -143
  63. package/dist/core/wrangle.d.ts +6 -1
  64. package/dist/core/wrangle.js +27 -16
  65. package/dist/core/xatlas.d.ts +16 -1
  66. package/dist/core/xatlas.js +66 -62
  67. package/dist/engines.d.ts +63 -0
  68. package/dist/engines.js +180 -0
  69. package/dist/explain.d.ts +4 -3
  70. package/dist/explain.js +129 -12
  71. package/dist/graph/engine.d.ts +92 -29
  72. package/dist/graph/engine.js +298 -148
  73. package/dist/graph/expr.d.ts +6 -3
  74. package/dist/graph/expr.js +18 -8
  75. package/dist/graph/outputs.d.ts +23 -0
  76. package/dist/graph/outputs.js +41 -0
  77. package/dist/graph/packages.d.ts +72 -0
  78. package/dist/graph/packages.js +261 -0
  79. package/dist/graph/parallel.d.ts +3 -1
  80. package/dist/graph/parallel.js +4 -1
  81. package/dist/graph/params.d.ts +6 -0
  82. package/dist/graph/params.js +34 -7
  83. package/dist/graph/scope.d.ts +55 -0
  84. package/dist/graph/scope.js +14 -0
  85. package/dist/graph/types.d.ts +75 -12
  86. package/dist/graph/types.js +4 -2
  87. package/dist/graph/vars.d.ts +16 -0
  88. package/dist/graph/vars.js +80 -0
  89. package/dist/host/artifacts.d.ts +10 -1
  90. package/dist/host/artifacts.js +37 -6
  91. package/dist/host/cook.d.ts +241 -0
  92. package/dist/host/cook.js +844 -0
  93. package/dist/host/export-targets.d.ts +42 -5
  94. package/dist/host/export-targets.js +72 -13
  95. package/dist/host/godot-commands.d.ts +4 -0
  96. package/dist/host/godot-commands.js +21 -0
  97. package/dist/host/godot.d.ts +183 -0
  98. package/dist/host/godot.js +916 -0
  99. package/dist/host/node.d.ts +12 -7
  100. package/dist/host/node.js +27 -9
  101. package/dist/host/pool.js +51 -2
  102. package/dist/host/sidecars.d.ts +8 -0
  103. package/dist/host/sidecars.js +50 -0
  104. package/dist/host/types.d.ts +19 -6
  105. package/dist/host/version.d.ts +1 -0
  106. package/dist/host/version.js +3 -0
  107. package/dist/host/worker.js +5 -2
  108. package/dist/index.d.ts +2 -2
  109. package/dist/index.js +1 -1
  110. package/dist/inspect.d.ts +80 -2
  111. package/dist/inspect.js +265 -30
  112. package/dist/io/common.d.ts +25 -5
  113. package/dist/io/export.js +2 -1
  114. package/dist/io/files.d.ts +23 -4
  115. package/dist/io/files.js +60 -8
  116. package/dist/io/gltf.js +134 -47
  117. package/dist/io/heightmap.d.ts +3 -0
  118. package/dist/io/heightmap.js +1 -0
  119. package/dist/io/import.js +10 -0
  120. package/dist/io/inspect-scene.d.ts +55 -0
  121. package/dist/io/inspect-scene.js +155 -0
  122. package/dist/io/kinds.d.ts +81 -0
  123. package/dist/io/kinds.js +163 -0
  124. package/dist/io/points.d.ts +2 -1
  125. package/dist/io/points.js +3 -1
  126. package/dist/io/sidecars.d.ts +24 -0
  127. package/dist/io/sidecars.js +57 -0
  128. package/dist/io/source.d.ts +65 -0
  129. package/dist/io/source.js +112 -0
  130. package/dist/io/targets.d.ts +47 -3
  131. package/dist/io/targets.js +148 -13
  132. package/dist/io/textures.js +21 -1
  133. package/dist/io/unreal.js +5 -1
  134. package/dist/mcp/docs.d.ts +10 -1
  135. package/dist/mcp/docs.js +786 -143
  136. package/dist/mcp/inputs.d.ts +12 -5
  137. package/dist/mcp/inputs.js +50 -13
  138. package/dist/mcp/server.js +56 -27
  139. package/dist/mcp/tools.d.ts +14 -7
  140. package/dist/mcp/tools.js +196 -103
  141. package/dist/mcp/verify.d.ts +2 -2
  142. package/dist/mcp/verify.js +1 -1
  143. package/dist/mcp/workspace.d.ts +15 -0
  144. package/dist/mcp/workspace.js +349 -0
  145. package/dist/nodes/attributes.js +198 -1
  146. package/dist/nodes/bake.js +151 -47
  147. package/dist/nodes/beam.d.ts +1 -0
  148. package/dist/nodes/beam.js +201 -0
  149. package/dist/nodes/bevel.js +51 -3
  150. package/dist/nodes/cloth.js +2 -0
  151. package/dist/nodes/code.js +21 -9
  152. package/dist/nodes/curves.js +433 -93
  153. package/dist/nodes/flow.js +53 -33
  154. package/dist/nodes/generate.js +12 -38
  155. package/dist/nodes/heightfield.js +248 -77
  156. package/dist/nodes/imageops.js +16 -19
  157. package/dist/nodes/index.d.ts +1 -0
  158. package/dist/nodes/index.js +1 -0
  159. package/dist/nodes/material.js +89 -24
  160. package/dist/nodes/measure.js +43 -17
  161. package/dist/nodes/particles.js +2 -0
  162. package/dist/nodes/pcg.js +6 -5
  163. package/dist/nodes/procedural.js +4 -3
  164. package/dist/nodes/remesh.js +71 -15
  165. package/dist/nodes/scene.js +4 -2
  166. package/dist/nodes/sdf.js +10 -5
  167. package/dist/nodes/solid.js +20 -5
  168. package/dist/nodes/splines.d.ts +15 -1
  169. package/dist/nodes/splines.js +112 -105
  170. package/dist/nodes/texture.d.ts +21 -6
  171. package/dist/nodes/texture.js +1318 -185
  172. package/dist/nodes/topology.d.ts +7 -0
  173. package/dist/nodes/topology.js +133 -6
  174. package/dist/nodes/transform.js +6 -2
  175. package/dist/nodes/uv.js +192 -174
  176. package/dist/nodes/wasmcall.js +5 -1
  177. package/dist/open.d.ts +19 -0
  178. package/dist/open.js +12 -0
  179. package/dist/package.d.ts +288 -0
  180. package/dist/package.js +772 -0
  181. package/dist/packages/check.d.ts +32 -0
  182. package/dist/packages/check.js +169 -0
  183. package/dist/packages/commands.d.ts +57 -0
  184. package/dist/packages/commands.js +537 -0
  185. package/dist/packages/registry.d.ts +796 -0
  186. package/dist/packages/registry.js +2998 -0
  187. package/dist/packages/update.d.ts +124 -0
  188. package/dist/packages/update.js +476 -0
  189. package/dist/render/options.d.ts +2 -0
  190. package/dist/render/options.js +44 -5
  191. package/dist/render/preview.d.ts +15 -4
  192. package/dist/render/preview.js +6 -4
  193. package/dist/render/raster.d.ts +66 -5
  194. package/dist/render/raster.js +506 -138
  195. package/dist/render/texview.d.ts +6 -0
  196. package/dist/render/texview.js +27 -0
  197. package/dist/scan.d.ts +61 -0
  198. package/dist/scan.js +228 -0
  199. package/dist/session.d.ts +311 -0
  200. package/dist/session.js +849 -0
  201. package/dist/viewer/app/api.js +48 -6
  202. package/dist/viewer/app/cooking.js +78 -0
  203. package/dist/viewer/app/embed.js +27 -7
  204. package/dist/viewer/app/export-popover.js +118 -16
  205. package/dist/viewer/app/grid.js +5 -1
  206. package/dist/viewer/app/guides.js +122 -0
  207. package/dist/viewer/app/keymap.js +6 -3
  208. package/dist/viewer/app/layers.js +4 -4
  209. package/dist/viewer/app/main.js +185 -52
  210. package/dist/viewer/app/markers.js +60 -0
  211. package/dist/viewer/app/outliner.js +72 -27
  212. package/dist/viewer/app/output-info.js +45 -0
  213. package/dist/viewer/app/outputs.css +147 -8
  214. package/dist/viewer/app/outputs.js +168 -54
  215. package/dist/viewer/app/packages.js +614 -0
  216. package/dist/viewer/app/props.js +176 -52
  217. package/dist/viewer/app/scene.js +30 -5
  218. package/dist/viewer/app/select.js +4 -3
  219. package/dist/viewer/app/status.js +12 -15
  220. package/dist/viewer/app/style.css +65 -7
  221. package/dist/viewer/app/vars.js +47 -0
  222. package/dist/viewer/app/welcome.js +72 -0
  223. package/dist/viewer/graph-file.d.ts +62 -0
  224. package/dist/viewer/graph-file.js +175 -0
  225. package/dist/viewer/index.html +23 -10
  226. package/dist/viewer/json-edit.d.ts +13 -0
  227. package/dist/viewer/json-edit.js +274 -0
  228. package/dist/viewer/outputs.d.ts +49 -5
  229. package/dist/viewer/outputs.js +264 -56
  230. package/dist/viewer/packages.d.ts +121 -0
  231. package/dist/viewer/packages.js +584 -0
  232. package/dist/viewer/server.d.ts +14 -9
  233. package/dist/viewer/server.js +266 -28
  234. package/dist/web/app/web.css +419 -0
  235. package/dist/web/app/web.js +188 -0
  236. package/dist/{gallery → web}/bake.d.ts +4 -4
  237. package/dist/{gallery → web}/bake.js +24 -15
  238. package/dist/web/folder-page.d.ts +101 -0
  239. package/dist/web/folder-page.js +146 -0
  240. package/dist/web/folders.d.ts +280 -0
  241. package/dist/web/folders.js +970 -0
  242. package/dist/web/library.d.ts +91 -0
  243. package/dist/web/library.js +228 -0
  244. package/dist/web/markdown.d.ts +45 -0
  245. package/dist/web/markdown.js +677 -0
  246. package/dist/web/pages.d.ts +258 -0
  247. package/dist/web/pages.js +301 -0
  248. package/dist/web/server.d.ts +24 -0
  249. package/dist/web/server.js +1068 -0
  250. package/dist/workspace.d.ts +88 -118
  251. package/dist/workspace.js +358 -460
  252. package/integrations/godot/addons/topotopo/LICENSE +202 -0
  253. package/integrations/godot/addons/topotopo/README.md +57 -0
  254. package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
  255. package/integrations/godot/addons/topotopo/dock.gd +266 -0
  256. package/integrations/godot/addons/topotopo/dock.tscn +72 -0
  257. package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
  258. package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
  259. package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
  260. package/integrations/godot/addons/topotopo/inspector.gd +38 -0
  261. package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
  262. package/integrations/godot/addons/topotopo/package.gd +23 -0
  263. package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
  264. package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
  265. package/integrations/godot/addons/topotopo/plugin.gd +970 -0
  266. package/integrations/godot/addons/topotopo/record.gd +435 -0
  267. package/integrations/godot/addons/topotopo/runner.gd +458 -0
  268. package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
  269. package/integrations/godot/addons/topotopo/watcher.gd +199 -0
  270. package/kernels/pkg/README.md +2 -1
  271. package/kernels/pkg-simd/README.md +2 -1
  272. package/library/architecture/versions/1.0/architecture.topo +12 -0
  273. package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
  274. package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
  275. package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
  276. package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
  277. package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
  278. package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
  279. package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
  280. package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
  281. package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
  282. package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
  283. package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
  284. package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
  285. package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
  286. package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
  287. package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
  288. package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
  289. package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
  290. package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
  291. package/library/architecture/versions/1.0/version.json +1119 -0
  292. package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
  293. package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
  294. package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
  295. package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
  296. package/library/nature/versions/1.0/graphs/lineup.json +24 -0
  297. package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
  298. package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
  299. package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
  300. package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
  301. package/library/nature/versions/1.0/nature.topo +12 -0
  302. package/library/nature/versions/1.0/version.json +534 -0
  303. package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
  304. package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
  305. package/library/props/versions/1.0/graphs/lineup.json +18 -0
  306. package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
  307. package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
  308. package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
  309. package/library/props/versions/1.0/props.topo +12 -0
  310. package/library/props/versions/1.0/version.json +336 -0
  311. package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
  312. package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
  313. package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
  314. package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
  315. package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
  316. package/library/street/versions/1.0/graphs/lineup.json +22 -0
  317. package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
  318. package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
  319. package/library/street/versions/1.0/street.topo +12 -0
  320. package/library/street/versions/1.0/version.json +421 -0
  321. package/package.json +17 -8
  322. package/dist/gallery/app/gallery.css +0 -100
  323. package/dist/gallery/app/gallery.js +0 -80
  324. package/dist/gallery/cli.d.ts +0 -4
  325. package/dist/gallery/cli.js +0 -157
  326. package/dist/gallery/library.d.ts +0 -124
  327. package/dist/gallery/library.js +0 -366
  328. package/dist/gallery/pages.d.ts +0 -94
  329. package/dist/gallery/pages.js +0 -148
  330. package/dist/gallery/server.d.ts +0 -21
  331. package/dist/gallery/server.js +0 -391
  332. package/dist/graph/migrate.d.ts +0 -23
  333. package/dist/graph/migrate.js +0 -58
  334. package/dist/mcp/library.d.ts +0 -12
  335. package/dist/mcp/library.js +0 -159
  336. package/shelf/index.json +0 -190
@@ -0,0 +1,637 @@
1
+ // Distance fields and regions for the texture nodes (src/nodes/texture.ts): an exact Euclidean distance transform
2
+ // that can wrap at the borders, signed distances from a grey mask to about a tenth of a pixel, SDF shapes, offsets
3
+ // and booleans, bevel profiles, mask growth with value propagation, and flood fill of the tiles of a mask.
4
+ // Conventions as in core/texture.ts (row-major, row 0 at the top, pixel centres at +0.5). Signed distances are
5
+ // negative inside. Functions taking a texel size work in metres (texel = metres per pixel), the others in pixels.
6
+ // "wrap" means the image tiles: distances are measured across the opposite border (to the nearest periodic copy),
7
+ // so a tileable mask gives a tileable field and regions crossing a border are one region.
8
+ import { hash32 } from './random.js';
9
+ const clamp01 = (x) => (x > 0 ? (x < 1 ? x : 1) : 0);
10
+ const smooth = (e0, e1, x) => {
11
+ if (e1 <= e0)
12
+ return x < e0 ? 0 : 1;
13
+ const t = clamp01((x - e0) / (e1 - e0));
14
+ return t * t * (3 - 2 * t);
15
+ };
16
+ const wrapI = (i, n) => ((i % n) + n) % n;
17
+ /** The shortest periodic difference (|result| ≤ n / 2). */
18
+ const wrapD = (d, n) => d - n * Math.round(d / n);
19
+ // ---------------------------------------------------------------- distance transform
20
+ /** 1D squared distance transform of f (Felzenszwalb & Huttenlocher); Infinity = no site. arg = the nearest site. */
21
+ function edt1d(f, n, d, arg, v, z) {
22
+ let k = -1;
23
+ for (let q = 0; q < n; q++) {
24
+ const fq = f[q];
25
+ if (fq === Infinity)
26
+ continue;
27
+ if (k < 0) {
28
+ k = 0;
29
+ v[0] = q;
30
+ z[0] = -Infinity;
31
+ z[1] = Infinity;
32
+ continue;
33
+ }
34
+ let s;
35
+ for (;;) {
36
+ const p = v[k];
37
+ s = (fq + q * q - (f[p] + p * p)) / (2 * (q - p));
38
+ if (s <= z[k])
39
+ k--;
40
+ else
41
+ break; // z[0] = -Infinity: k never drops below 0
42
+ }
43
+ k++;
44
+ v[k] = q;
45
+ z[k] = s;
46
+ z[k + 1] = Infinity;
47
+ }
48
+ if (k < 0) {
49
+ d.fill(Infinity, 0, n);
50
+ arg.fill(-1, 0, n);
51
+ return;
52
+ }
53
+ let j = 0;
54
+ for (let q = 0; q < n; q++) {
55
+ while (z[j + 1] < q)
56
+ j++;
57
+ const p = v[j], dq = q - p;
58
+ d[q] = dq * dq + f[p];
59
+ arg[q] = p;
60
+ }
61
+ }
62
+ /**
63
+ * Exact Euclidean distance transform of the sites (non-zero entries): d2 = squared distance in pixels to the nearest
64
+ * site, nearest = its index (-1 when there is none). With wrap the image is periodic: each 1D pass runs over three
65
+ * copies of its row or column and keeps the middle one, so the distance is to the nearest periodic copy of a site.
66
+ */
67
+ export function edt(sites, w, h, wrap) {
68
+ const m = Math.max(w, h) * (wrap ? 3 : 1);
69
+ const f = new Float64Array(m), d = new Float64Array(m), arg = new Int32Array(m), v = new Int32Array(m), z = new Float64Array(m + 1);
70
+ const d1 = new Float64Array(w * h), col = new Int32Array(w * h);
71
+ const pass = (n, get, put) => {
72
+ const len = wrap ? 3 * n : n;
73
+ for (let q = 0; q < len; q++)
74
+ f[q] = get(q % n);
75
+ edt1d(f, len, d, arg, v, z);
76
+ const o = wrap ? n : 0;
77
+ for (let q = 0; q < n; q++)
78
+ put(q, d[q + o], arg[q + o] < 0 ? -1 : arg[q + o] % n);
79
+ };
80
+ for (let j = 0; j < h; j++) {
81
+ const row = j * w;
82
+ pass(w, (q) => (sites[row + q] ? 0 : Infinity), (q, dist, a) => { d1[row + q] = dist; col[row + q] = a; });
83
+ }
84
+ const d2 = new Float64Array(w * h), nearest = new Int32Array(w * h);
85
+ for (let i = 0; i < w; i++)
86
+ pass(h, (q) => d1[q * w + i], (q, dist, a) => { d2[q * w + i] = dist; nearest[q * w + i] = a < 0 ? -1 : a * w + col[a * w + i]; });
87
+ return { d2, nearest };
88
+ }
89
+ /**
90
+ * Signed distance in pixels (negative inside, where value ≥ threshold) to the iso-line value = threshold of a grey
91
+ * layer. The line crosses each pair of 4-neighbours on opposite sides at the linearly interpolated point (exact for
92
+ * the one-pixel ramps of antialiased edges and for wider soft ramps); every pixel takes the distance to the nearest
93
+ * crossing, found around the nearest crossing pixel of an exact distance transform. Sub-pixel accurate, so a field
94
+ * measured in metres is the same at any resolution. Without any crossing every pixel is ±(width + height).
95
+ */
96
+ export function signedDistance(a, w, h, threshold, wrap) {
97
+ const n = w * h, inside = new Uint8Array(n);
98
+ for (let k = 0; k < n; k++)
99
+ inside[k] = a[k] >= threshold ? 1 : 0;
100
+ // crossings: up to four per pixel, as offsets from its centre
101
+ const ox = new Float32Array(n * 4), oy = new Float32Array(n * 4), cnt = new Uint8Array(n);
102
+ const DX = [1, -1, 0, 0], DY = [0, 0, 1, -1];
103
+ let any = false;
104
+ for (let j = 0; j < h; j++)
105
+ for (let i = 0; i < w; i++) {
106
+ const k = j * w + i;
107
+ let c = 0;
108
+ for (let e = 0; e < 4; e++) {
109
+ let ni = i + DX[e], nj = j + DY[e];
110
+ if (ni < 0 || nj < 0 || ni >= w || nj >= h) {
111
+ if (!wrap)
112
+ continue;
113
+ ni = wrapI(ni, w);
114
+ nj = wrapI(nj, h);
115
+ }
116
+ const m = nj * w + ni;
117
+ if (inside[m] === inside[k])
118
+ continue;
119
+ const t = clamp01((threshold - a[k]) / (a[m] - a[k]));
120
+ ox[k * 4 + c] = DX[e] * t;
121
+ oy[k * 4 + c] = DY[e] * t;
122
+ c++;
123
+ }
124
+ cnt[k] = c;
125
+ if (c)
126
+ any = true;
127
+ }
128
+ const out = new Float32Array(n);
129
+ if (!any) {
130
+ const far = w + h;
131
+ for (let k = 0; k < n; k++)
132
+ out[k] = inside[k] ? -far : far;
133
+ return out;
134
+ }
135
+ // first guess: the nearest crossing around the nearest crossing pixel; vx, vy = the vector to it
136
+ const { nearest } = edt(cnt, w, h, wrap);
137
+ const vx = new Float32Array(n), vy = new Float32Array(n), vd = new Float64Array(n);
138
+ for (let j = 0; j < h; j++)
139
+ for (let i = 0; i < w; i++) {
140
+ const k = j * w + i, s = nearest[k], si = s % w, sj = (s - si) / w;
141
+ let best = Infinity, bx = 0, by = 0;
142
+ for (let dj = -1; dj <= 1; dj++)
143
+ for (let di = -1; di <= 1; di++) {
144
+ const qi = si + di, qj = sj + dj;
145
+ if (!wrap && (qi < 0 || qj < 0 || qi >= w || qj >= h))
146
+ continue;
147
+ const q = wrapI(qj, h) * w + wrapI(qi, w);
148
+ for (let c = 0; c < cnt[q]; c++) {
149
+ let ex = qi + ox[q * 4 + c] - i, ey = qj + oy[q * 4 + c] - j;
150
+ if (wrap) {
151
+ ex = wrapD(ex, w);
152
+ ey = wrapD(ey, h);
153
+ }
154
+ const dd = ex * ex + ey * ey;
155
+ if (dd < best) {
156
+ best = dd;
157
+ bx = ex;
158
+ by = ey;
159
+ }
160
+ }
161
+ }
162
+ vx[k] = bx;
163
+ vy[k] = by;
164
+ vd[k] = best;
165
+ }
166
+ // pixels on a curved outline have many crossing pixels at nearly the same distance, and the transform's pick may
167
+ // not hold the nearest crossing: a forward and a backward sweep let every pixel take a neighbour's crossing when
168
+ // it is nearer (vector propagation), which brings the error to the crossings' own (about a tenth of a pixel)
169
+ const relax = (i, j, k) => {
170
+ for (let dj = -1; dj <= 1; dj++)
171
+ for (let di = -1; di <= 1; di++) {
172
+ if (!di && !dj)
173
+ continue;
174
+ const qi = i + di, qj = j + dj;
175
+ if (!wrap && (qi < 0 || qj < 0 || qi >= w || qj >= h))
176
+ continue;
177
+ const q = wrapI(qj, h) * w + wrapI(qi, w);
178
+ const ex = vx[q] + di, ey = vy[q] + dj, dd = ex * ex + ey * ey;
179
+ if (dd < vd[k] - 1e-9) {
180
+ vd[k] = dd;
181
+ vx[k] = ex;
182
+ vy[k] = ey;
183
+ }
184
+ }
185
+ };
186
+ for (let j = 0; j < h; j++)
187
+ for (let i = 0; i < w; i++)
188
+ relax(i, j, j * w + i);
189
+ for (let j = h - 1; j >= 0; j--)
190
+ for (let i = w - 1; i >= 0; i--)
191
+ relax(i, j, j * w + i);
192
+ for (let k = 0; k < n; k++)
193
+ out[k] = inside[k] ? -Math.sqrt(vd[k]) : Math.sqrt(vd[k]);
194
+ return out;
195
+ }
196
+ /** An SDF (metres) as a one-texel antialiased coverage, so signedDistance() can measure it again. */
197
+ function coverageOf(d, texel) {
198
+ const a = new Float32Array(d.length);
199
+ for (let k = 0; k < d.length; k++)
200
+ a[k] = clamp01(0.5 - d[k] / texel);
201
+ return a;
202
+ }
203
+ /** Re-measure an SDF in metres (after booleans or offsets that leave it inexact): the distance to its zero line again. */
204
+ export function redistance(d, w, h, texel, wrap) {
205
+ const s = signedDistance(coverageOf(d, texel), w, h, 0.5, wrap);
206
+ for (let k = 0; k < s.length; k++)
207
+ s[k] *= texel;
208
+ return s;
209
+ }
210
+ // ---------------------------------------------------------------- shapes
211
+ export const SDF_SHAPES = ['disc', 'box', 'polygon', 'star', 'capsule', 'cross'];
212
+ /** Signed distance from (x, y) to a closed polygon (flat [x0, y0, x1, y1, …]): negative inside. */
213
+ function polygonSD(poly, x, y) {
214
+ let d = Infinity, inside = false;
215
+ const n = poly.length / 2;
216
+ for (let a = 0, b = n - 1; a < n; b = a++) {
217
+ const ax = poly[a * 2], ay = poly[a * 2 + 1], bx = poly[b * 2], by = poly[b * 2 + 1];
218
+ const ex = bx - ax, ey = by - ay, wx = x - ax, wy = y - ay;
219
+ const t = clamp01((wx * ex + wy * ey) / (ex * ex + ey * ey || 1));
220
+ const qx = wx - ex * t, qy = wy - ey * t;
221
+ d = Math.min(d, qx * qx + qy * qy);
222
+ if ((ay > y) !== (by > y) && x < ((bx - ax) * (y - ay)) / (by - ay) + ax)
223
+ inside = !inside;
224
+ }
225
+ return inside ? -Math.sqrt(d) : Math.sqrt(d);
226
+ }
227
+ /** Signed distance to an axis-aligned box of half extents (hx, hy) with corners rounded by c (metres). */
228
+ function boxSD(x, y, hx, hy, c) {
229
+ const r = Math.max(0, Math.min(c, hx, hy));
230
+ const qx = Math.abs(x) - (hx - r), qy = Math.abs(y) - (hy - r);
231
+ return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - r;
232
+ }
233
+ /** The shape's distance function in its own frame (metres, centred, unrotated) and the radius that bounds it. */
234
+ function shapeFn(o) {
235
+ const r = Math.max(1e-9, o.radius), c = Math.max(0, o.corner);
236
+ const ex = Math.max(1e-9, o.extent[0]), ey = Math.max(1e-9, o.extent[1]);
237
+ const n = Math.max(3, Math.round(o.sides));
238
+ switch (o.shape) {
239
+ case 'box': return { f: (x, y) => boxSD(x, y, ex / 2, ey / 2, c), bound: Math.hypot(ex, ey) / 2 };
240
+ case 'capsule': {
241
+ const rad = ey / 2, a = Math.max(0, ex / 2 - rad);
242
+ return { f: (x, y) => Math.hypot(Math.max(Math.abs(x) - a, 0), y) - rad, bound: Math.max(ex / 2, rad) };
243
+ }
244
+ case 'cross': return { f: (x, y) => Math.min(boxSD(x, y, ex / 2, ey / 2, c), boxSD(x, y, ey / 2, ex / 2, c)), bound: Math.hypot(ex, ey) / 2 };
245
+ case 'polygon':
246
+ case 'star': {
247
+ const star = o.shape === 'star';
248
+ const k = star ? 2 * n : n;
249
+ // rounding a polygon: shrink it by the corner radius (along its inradius), then grow its distance back
250
+ const cr = star ? 0 : Math.min(c, r * Math.cos(Math.PI / n));
251
+ const rr = star ? r : r - cr / Math.cos(Math.PI / n);
252
+ const poly = [];
253
+ for (let i = 0; i < k; i++) {
254
+ const ang = Math.PI / 2 + (i * 2 * Math.PI) / k;
255
+ const ri = star && i & 1 ? rr * Math.max(0.01, Math.min(1, o.inner)) : rr;
256
+ poly.push(ri * Math.cos(ang), ri * Math.sin(ang));
257
+ }
258
+ if (rr <= 1e-9)
259
+ return { f: (x, y) => Math.hypot(x, y) - cr, bound: r };
260
+ return { f: (x, y) => polygonSD(poly, x, y) - cr, bound: r };
261
+ }
262
+ default: return { f: (x, y) => Math.hypot(x, y) - r, bound: r };
263
+ }
264
+ }
265
+ /**
266
+ * The cells around a pixel that its shape can reach, along x and y (the shape's bound over a cell, and one more):
267
+ * every pixel measures (2kx + 1) × (2ky + 1) instances.
268
+ */
269
+ export function shapeReach(o, w, h) {
270
+ const nx = Math.max(1, Math.round(o.tile[0])), ny = Math.max(1, Math.round(o.tile[1]));
271
+ const W = Math.max(1e-9, o.size), H = (W * h) / w, { bound } = shapeFn(o);
272
+ return { kx: Math.ceil(bound / (W / nx)) + 1, ky: Math.ceil(bound / (H / ny)) + 1 };
273
+ }
274
+ /**
275
+ * Signed distance in metres to a shape, or a tile[0] × tile[1] grid of them, on a w × h image whose width covers
276
+ * `size` metres (y up, the image height covers size · h / w). Each pixel takes the nearest instance among the cells
277
+ * around it (as many as the shape's size reaches), so repeats that overlap their cells union correctly; with wrap
278
+ * the grid continues past the borders and the field tiles.
279
+ */
280
+ export function sdfShape(o, w, h) {
281
+ const out = new Float32Array(w * h);
282
+ const nx = Math.max(1, Math.round(o.tile[0])), ny = Math.max(1, Math.round(o.tile[1]));
283
+ const W = Math.max(1e-9, o.size), H = (W * h) / w;
284
+ const cw = W / nx, chh = H / ny;
285
+ const { f, bound } = shapeFn(o);
286
+ const rot = (o.rotation * Math.PI) / 180, cr = Math.cos(-rot), sr = Math.sin(-rot);
287
+ const { kx, ky } = shapeReach(o, w, h);
288
+ for (let j = 0; j < h; j++) {
289
+ const y = (1 - (j + 0.5) / h) * H;
290
+ const cj0 = Math.round(y / chh - o.center[1]);
291
+ for (let i = 0; i < w; i++) {
292
+ const x = ((i + 0.5) / w) * W;
293
+ const ci0 = Math.round(x / cw - o.center[0]);
294
+ let best = Infinity;
295
+ for (let cj = cj0 - ky; cj <= cj0 + ky; cj++) {
296
+ if (!o.wrap && (cj < 0 || cj >= ny))
297
+ continue;
298
+ for (let ci = ci0 - kx; ci <= ci0 + kx; ci++) {
299
+ if (!o.wrap && (ci < 0 || ci >= nx))
300
+ continue;
301
+ const lx = x - (ci + o.center[0]) * cw, ly = y - (cj + o.center[1]) * chh;
302
+ const d = f(lx * cr - ly * sr, lx * sr + ly * cr);
303
+ if (d < best)
304
+ best = d;
305
+ }
306
+ }
307
+ out[j * w + i] = best;
308
+ }
309
+ }
310
+ return out;
311
+ }
312
+ // ---------------------------------------------------------------- operations on fields
313
+ export const SDF_OPS = ['offset', 'round', 'fillet', 'stroke', 'invert', 'redistance'];
314
+ /**
315
+ * An operation on an SDF in metres:
316
+ * offset grow by amount (negative shrinks): d - amount
317
+ * round round the convex corners by amount: shrink, re-measure, grow back
318
+ * fillet round the concave corners by amount: grow, re-measure, shrink back
319
+ * stroke a band amount wide centred on the outline: |d| - amount / 2
320
+ * invert inside out: -d
321
+ * redistance the distance to the zero line measured again (after smooth booleans or a morph)
322
+ */
323
+ export function sdfOp(d, w, h, mode, amount, texel, wrap) {
324
+ const n = d.length;
325
+ const map = (fn) => { const o = new Float32Array(n); for (let k = 0; k < n; k++)
326
+ o[k] = fn(d[k]); return o; };
327
+ switch (mode) {
328
+ case 'round':
329
+ case 'fillet': {
330
+ const r = Math.max(0, amount), sgn = mode === 'round' ? 1 : -1;
331
+ if (!r)
332
+ return d.slice();
333
+ const re = redistance(map((x) => x + sgn * r), w, h, texel, wrap);
334
+ for (let k = 0; k < n; k++)
335
+ re[k] -= sgn * r;
336
+ return re;
337
+ }
338
+ case 'stroke': {
339
+ const hw = Math.max(0, amount) / 2;
340
+ return map((x) => Math.abs(x) - hw);
341
+ }
342
+ case 'invert': return map((x) => -x);
343
+ case 'redistance': return redistance(d, w, h, texel, wrap);
344
+ default: return map((x) => x - amount);
345
+ }
346
+ }
347
+ export const SDF_BOOLEANS = ['union', 'subtract', 'intersect', 'xor', 'morph'];
348
+ /** Polynomial smooth minimum with a blend width of k (0 = min). */
349
+ const smin = (a, b, k) => {
350
+ if (!(k > 0))
351
+ return a < b ? a : b;
352
+ const hh = Math.max(k - Math.abs(a - b), 0) / k;
353
+ return Math.min(a, b) - (hh * hh * k) / 4;
354
+ };
355
+ /** Combine two SDFs (same size, metres): union, subtract (a minus b), intersect, xor, or morph (a → b by t); k = blend width. */
356
+ export function sdfBoolean(a, b, mode, k, t) {
357
+ const out = new Float32Array(a.length);
358
+ for (let i = 0; i < a.length; i++) {
359
+ const x = a[i], y = b[i];
360
+ let v;
361
+ switch (mode) {
362
+ case 'subtract':
363
+ v = -smin(-x, y, k);
364
+ break;
365
+ case 'intersect':
366
+ v = -smin(-x, -y, k);
367
+ break;
368
+ case 'xor':
369
+ v = Math.max(smin(x, y, k), smin(-x, -y, k));
370
+ break; // outside the union or inside the intersection
371
+ case 'morph':
372
+ v = x + (y - x) * t;
373
+ break;
374
+ default: v = smin(x, y, k);
375
+ }
376
+ out[i] = v;
377
+ }
378
+ return out;
379
+ }
380
+ export const BEVEL_PROFILES = ['linear', 'smooth', 'round', 'ramp'];
381
+ /** A bevel profile: t = 0 at the outline … 1 at the full bevel width → height 0 … 1. */
382
+ export function profileFn(name, ramp) {
383
+ switch (name) {
384
+ case 'smooth': return (t) => t * t * (3 - 2 * t);
385
+ case 'round': return (t) => Math.sqrt(Math.max(0, 1 - (1 - t) * (1 - t)));
386
+ case 'ramp': return ramp ?? ((t) => t);
387
+ default: return (t) => t;
388
+ }
389
+ }
390
+ export const SDF_RENDER = ['fill', 'outline', 'bevel', 'distance'];
391
+ /**
392
+ * A grey image from an SDF in metres:
393
+ * fill 1 inside, 0 outside; the edge is antialiased over a texel, or a smooth ramp `softness` metres wide
394
+ * outline a line `width` metres wide centred on the outline (softness as for fill)
395
+ * bevel 0 at the outline rising through the profile to 1 at `width` metres inside
396
+ * distance the field encoded for export: 0.5 on the outline, 1 at `width` metres inside, 0 at `width` outside
397
+ */
398
+ export function sdfToMask(d, mode, width, softness, texel, profile) {
399
+ const out = new Float32Array(d.length);
400
+ const s = Math.max(0, softness);
401
+ const edge = s > texel ? (e) => smooth(-s / 2, s / 2, e) : (e) => clamp01(0.5 + e / texel);
402
+ const wd = Math.max(1e-12, width);
403
+ for (let k = 0; k < d.length; k++) {
404
+ const x = d[k];
405
+ switch (mode) {
406
+ case 'outline':
407
+ out[k] = edge(width / 2 - Math.abs(x));
408
+ break;
409
+ case 'bevel':
410
+ out[k] = x < 0 ? clamp01(profile(clamp01(-x / wd))) : 0;
411
+ break;
412
+ case 'distance':
413
+ out[k] = clamp01(0.5 - x / (2 * wd));
414
+ break;
415
+ default: out[k] = edge(-x);
416
+ }
417
+ }
418
+ return out;
419
+ }
420
+ export const EDGE_MODES = ['sobel', 'outline', 'inner', 'outer'];
421
+ /**
422
+ * A line along an outline from its SDF in metres, `width` metres wide: outline = centred on it, inner = just inside,
423
+ * outer = just outside. roundness 0 = a flat band antialiased over a texel, 1 = a rounded ridge (a quarter circle up
424
+ * from each side of the band, a height for tex_normal_from_height).
425
+ */
426
+ export function edgeBand(d, mode, width, roundness, texel) {
427
+ const out = new Float32Array(d.length);
428
+ const hw = Math.max(0, width) / 2, c = mode === 'inner' ? -hw : mode === 'outer' ? hw : 0, rd = clamp01(roundness);
429
+ for (let k = 0; k < d.length; k++) {
430
+ const e = hw - Math.abs(d[k] - c); // inside the band: distance to its nearer side
431
+ const flat = clamp01(0.5 + e / texel);
432
+ const t = hw > 0 ? clamp01(e / hw) : 0, ridge = Math.sqrt(Math.max(0, 1 - (1 - t) * (1 - t)));
433
+ out[k] = flat + (Math.min(flat, ridge) - flat) * rd;
434
+ }
435
+ return out;
436
+ }
437
+ /**
438
+ * A bevel height from an SDF in metres: distance > 0 bevels inward (0 on the outline, 1 at `distance` inside),
439
+ * distance < 0 outward (1 inside, falling to 0 at |distance| outside).
440
+ */
441
+ export function bevelHeight(d, distance, profile, texel) {
442
+ const out = new Float32Array(d.length);
443
+ if (Math.abs(distance) < 1e-12)
444
+ return sdfToMask(d, 'fill', 0, 0, texel, profile);
445
+ const D = Math.abs(distance);
446
+ for (let k = 0; k < d.length; k++) {
447
+ const x = d[k];
448
+ out[k] = distance > 0 ? (x < 0 ? clamp01(profile(clamp01(-x / D))) : 0) : (x <= 0 ? 1 : clamp01(profile(1 - clamp01(x / D))));
449
+ }
450
+ return out;
451
+ }
452
+ /**
453
+ * Values of src (ch 1 or 4) carried out from the inside pixels to every pixel within maxPx pixels (each takes the
454
+ * value of its nearest inside pixel; inside pixels keep their own); farther pixels are 0.
455
+ */
456
+ export function propagate(src, ch, inside, w, h, maxPx, wrap) {
457
+ const out = new Float32Array(src.length);
458
+ const { d2, nearest } = edt(inside, w, h, wrap);
459
+ const lim = maxPx * maxPx;
460
+ for (let k = 0; k < w * h; k++) {
461
+ const s = nearest[k];
462
+ if (s < 0 || d2[k] > lim)
463
+ continue;
464
+ for (let c = 0; c < ch; c++)
465
+ out[k * ch + c] = src[s * ch + c];
466
+ }
467
+ return out;
468
+ }
469
+ // ---------------------------------------------------------------- flood fill
470
+ export const FLOOD_OUTPUTS = ['random', 'color', 'gradient', 'bbox_size', 'position'];
471
+ /**
472
+ * Flood fill: the tiles of a mask (connected pixels with value ≥ threshold; with wrap a tile crossing a border is one
473
+ * tile) and a value per tile in C:
474
+ * random a random grey 0..1 color a random RGB colour (alpha 1 on the tiles, 0 in the gaps)
475
+ * gradient 0 → 1 across the tile along angle (± angleRandom per tile)
476
+ * bbox_size the longer side of the tile's bounding box in metres
477
+ * position the tile's centre as (u, v, 0, 1)
478
+ * id = the random grey of each tile (0 in the gaps). Random values hash the tile's centre (area-weighted with the
479
+ * antialiased edge pixels, snapped to a 1/128 grid of the image whose cells are centred where the centres of regular
480
+ * patterns fall), so they do not depend on the resolution or on the order tiles are found in.
481
+ */
482
+ export function floodFill(a, w, h, o) {
483
+ const n = w * h, th = o.threshold, wrap = o.wrap;
484
+ const label = new Int32Array(n).fill(-1);
485
+ const ux = new Int32Array(n), uy = new Int32Array(n), queue = new Int32Array(n);
486
+ const touched = new Int32Array(n).fill(-1);
487
+ const nb = o.connectivity === 8 ? [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [-1, 1], [1, -1], [-1, -1]] : [[1, 0], [-1, 0], [0, 1], [0, -1]];
488
+ const tiles = [];
489
+ const order = new Int32Array(n); // tile pixels, tile after tile
490
+ let filled = 0;
491
+ for (let s = 0; s < n; s++) {
492
+ if (label[s] >= 0 || !(a[s] >= th))
493
+ continue;
494
+ const t = tiles.length;
495
+ const T = { x0: Infinity, x1: -Infinity, y0: Infinity, y1: -Infinity, sw: 0, sx: 0, sy: 0, start: filled, count: 0 };
496
+ tiles.push(T);
497
+ let head = 0, tail = 0;
498
+ queue[tail++] = s;
499
+ label[s] = t;
500
+ ux[s] = s % w;
501
+ uy[s] = (s - ux[s]) / w;
502
+ while (head < tail) {
503
+ const p = queue[head++], px = ux[p], py = uy[p];
504
+ order[filled++] = p;
505
+ T.count++;
506
+ if (px < T.x0)
507
+ T.x0 = px;
508
+ if (px > T.x1)
509
+ T.x1 = px;
510
+ if (py < T.y0)
511
+ T.y0 = py;
512
+ if (py > T.y1)
513
+ T.y1 = py;
514
+ const wp = clamp01(a[p]);
515
+ T.sw += wp;
516
+ T.sx += wp * (px + 0.5);
517
+ T.sy += wp * (py + 0.5);
518
+ for (const [dx, dy] of nb) {
519
+ const qx = px + dx, qy = py + dy;
520
+ if (!wrap && (qx < 0 || qy < 0 || qx >= w || qy >= h))
521
+ continue;
522
+ const q = wrapI(qy, h) * w + wrapI(qx, w);
523
+ if (a[q] >= th) {
524
+ if (label[q] < 0) {
525
+ label[q] = t;
526
+ ux[q] = qx;
527
+ uy[q] = qy;
528
+ queue[tail++] = q;
529
+ }
530
+ }
531
+ else if (a[q] > 0 && touched[q] !== t && (dx === 0 || dy === 0)) {
532
+ // an antialiased edge pixel outside the tile: its coverage belongs to the tile's area
533
+ touched[q] = t;
534
+ const wq = clamp01(a[q]);
535
+ T.sw += wq;
536
+ T.sx += wq * (qx + 0.5);
537
+ T.sy += wq * (qy + 0.5);
538
+ }
539
+ }
540
+ }
541
+ }
542
+ const nt = tiles.length;
543
+ const key = new Uint32Array(nt), cu = new Float64Array(nt), cv = new Float64Array(nt);
544
+ const seedH = hash32(Math.floor(o.seed * 7919) + 0x2545f491);
545
+ for (let t = 0; t < nt; t++) {
546
+ const T = tiles[t];
547
+ let x = T.sx / (T.sw || 1), y = T.sy / (T.sw || 1);
548
+ // a tile that wraps all the way around has no centre along that axis
549
+ if (wrap && T.x1 - T.x0 + 1 >= w)
550
+ x = 0;
551
+ if (wrap && T.y1 - T.y0 + 1 >= h)
552
+ y = 0;
553
+ let u = x / w, v = y / h;
554
+ if (wrap) {
555
+ u -= Math.floor(u);
556
+ v -= Math.floor(v);
557
+ }
558
+ cu[t] = u;
559
+ cv[t] = 1 - v;
560
+ // grid cells centred on multiples of 1/128 (where the centres of regular patterns fall), the index wrapped (a
561
+ // centre on the border reads u 0 or 1)
562
+ const qx = wrapI(Math.floor(u * 128 + 0.5), 128), qy = wrapI(Math.floor(v * 128 + 0.5), 128);
563
+ key[t] = hash32(Math.imul(qx, 0x27d4eb2d) ^ Math.imul(qy, 0x165667b1) ^ seedH);
564
+ }
565
+ // tiles whose centres fall in one cell of the grid (tiles closer than 1/128 of the image, a ring around a disc) are
566
+ // told apart by their order in it: by centre (rounded, so the order holds at any resolution), then by area
567
+ const cells = new Map();
568
+ for (let t = 0; t < nt; t++) {
569
+ const l = cells.get(key[t]);
570
+ if (l)
571
+ l.push(t);
572
+ else
573
+ cells.set(key[t], [t]);
574
+ }
575
+ const fine = (x) => Math.round(x * 2048);
576
+ for (const l of cells.values()) {
577
+ if (l.length < 2)
578
+ continue;
579
+ l.sort((a, b) => fine(cv[a]) - fine(cv[b]) || fine(cu[a]) - fine(cu[b]) || tiles[a].sw - tiles[b].sw);
580
+ l.forEach((t, k) => { key[t] = hash32(key[t] ^ Math.imul(k + 1, 0x85ebca6b)); });
581
+ }
582
+ const R = (t, k) => hash32(key[t] ^ Math.imul(k + 1, 0x9e3779b9)) / 4294967296;
583
+ const id = new Float32Array(n);
584
+ const ch = o.output === 'color' || o.output === 'position' ? 4 : 1;
585
+ const C = new Float32Array(n * ch);
586
+ for (let t = 0; t < nt; t++) {
587
+ const T = tiles[t], r = R(t, 0);
588
+ let dx = 0, dy = 0, pmin = Infinity, pmax = -Infinity;
589
+ // a tile that wraps all the way round an axis is read at its pixels' own positions along it: its gradient jumps at
590
+ // the border (a full-width band cannot ramp and tile), not in the middle where the fill met itself
591
+ const fx = wrap && T.x1 - T.x0 + 1 >= w, fy = wrap && T.y1 - T.y0 + 1 >= h;
592
+ const X = (p) => (fx ? p % w : ux[p]), Y = (p) => (fy ? Math.floor(p / w) : uy[p]);
593
+ if (o.output === 'gradient') {
594
+ const ang = ((o.angle + (R(t, 4) * 2 - 1) * o.angleRandom) * Math.PI) / 180;
595
+ dx = Math.cos(ang);
596
+ dy = -Math.sin(ang); // v up = rows down
597
+ for (let q = T.start; q < T.start + T.count; q++) {
598
+ const p = order[q], pr = (X(p) + 0.5) * dx + (Y(p) + 0.5) * dy;
599
+ if (pr < pmin)
600
+ pmin = pr;
601
+ if (pr > pmax)
602
+ pmax = pr;
603
+ }
604
+ const pad = 0.5 * (Math.abs(dx) + Math.abs(dy)); // pixel centres to pixel edges
605
+ pmin -= pad;
606
+ pmax += pad;
607
+ }
608
+ const bbox = Math.max(T.x1 - T.x0 + 1, T.y1 - T.y0 + 1) * o.texel;
609
+ const c1 = R(t, 1), c2 = R(t, 2), c3 = R(t, 3);
610
+ for (let q = T.start; q < T.start + T.count; q++) {
611
+ const p = order[q];
612
+ id[p] = r;
613
+ switch (o.output) {
614
+ case 'color':
615
+ C[p * 4] = c1;
616
+ C[p * 4 + 1] = c2;
617
+ C[p * 4 + 2] = c3;
618
+ C[p * 4 + 3] = 1;
619
+ break;
620
+ case 'position':
621
+ C[p * 4] = cu[t];
622
+ C[p * 4 + 1] = cv[t];
623
+ C[p * 4 + 2] = 0;
624
+ C[p * 4 + 3] = 1;
625
+ break;
626
+ case 'gradient':
627
+ C[p] = clamp01(((X(p) + 0.5) * dx + (Y(p) + 0.5) * dy - pmin) / (pmax - pmin || 1));
628
+ break;
629
+ case 'bbox_size':
630
+ C[p] = bbox;
631
+ break;
632
+ default: C[p] = r;
633
+ }
634
+ }
635
+ }
636
+ return { C, ch, id, tiles: nt };
637
+ }