topotopo 0.3.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 (354) hide show
  1. package/NOTICE +40 -10
  2. package/README.md +475 -111
  3. package/dist/cli.js +328 -133
  4. package/dist/config.d.ts +48 -0
  5. package/dist/config.js +170 -0
  6. package/dist/core/analysis.d.ts +5 -1
  7. package/dist/core/analysis.js +8 -3
  8. package/dist/core/bake.d.ts +16 -8
  9. package/dist/core/bake.js +165 -29
  10. package/dist/core/bevel.d.ts +3 -1
  11. package/dist/core/bevel.js +238 -97
  12. package/dist/core/decimate.d.ts +65 -0
  13. package/dist/core/decimate.js +409 -3
  14. package/dist/core/erosion.d.ts +37 -0
  15. package/dist/core/erosion.js +41 -1
  16. package/dist/core/geometry.d.ts +5 -1
  17. package/dist/core/geometry.js +204 -4
  18. package/dist/core/heightfield.d.ts +27 -4
  19. package/dist/core/heightfield.js +54 -16
  20. package/dist/core/imageops.d.ts +6 -0
  21. package/dist/core/imageops.js +51 -0
  22. package/dist/core/intrinsics.d.ts +7 -0
  23. package/dist/core/intrinsics.js +94 -0
  24. package/dist/core/manifold.d.ts +12 -1
  25. package/dist/core/manifold.js +78 -6
  26. package/dist/core/materials.d.ts +11 -0
  27. package/dist/core/materials.js +56 -0
  28. package/dist/core/noise.d.ts +9 -0
  29. package/dist/core/noise.js +10 -1
  30. package/dist/core/packed.js +11 -0
  31. package/dist/core/particles.d.ts +1 -1
  32. package/dist/core/particles.js +1 -1
  33. package/dist/core/polymerge.d.ts +21 -0
  34. package/dist/core/polymerge.js +327 -0
  35. package/dist/core/rbd.js +13 -1
  36. package/dist/core/repair.d.ts +2 -0
  37. package/dist/core/repair.js +2 -0
  38. package/dist/core/report.d.ts +70 -0
  39. package/dist/core/report.js +275 -0
  40. package/dist/core/scatter.d.ts +1 -1
  41. package/dist/core/scatter.js +1 -1
  42. package/dist/core/scene.d.ts +20 -0
  43. package/dist/core/scene.js +72 -0
  44. package/dist/core/sha256.d.ts +4 -0
  45. package/dist/core/sha256.js +69 -0
  46. package/dist/core/shapes.d.ts +3 -0
  47. package/dist/core/shapes.js +34 -0
  48. package/dist/core/snippet-lib.d.ts +15 -2
  49. package/dist/core/snippet-lib.js +40 -14
  50. package/dist/core/solidify.d.ts +38 -0
  51. package/dist/core/solidify.js +356 -0
  52. package/dist/core/tangents.d.ts +54 -0
  53. package/dist/core/tangents.js +218 -0
  54. package/dist/core/texcoord.d.ts +4 -0
  55. package/dist/core/texcoord.js +14 -0
  56. package/dist/core/texnoise.d.ts +6 -1
  57. package/dist/core/texnoise.js +6 -2
  58. package/dist/core/texsdf.d.ts +131 -0
  59. package/dist/core/texsdf.js +637 -0
  60. package/dist/core/texture.d.ts +163 -9
  61. package/dist/core/texture.js +844 -20
  62. package/dist/core/topology.d.ts +32 -0
  63. package/dist/core/topology.js +238 -7
  64. package/dist/core/traceimage.d.ts +7 -0
  65. package/dist/core/traceimage.js +221 -0
  66. package/dist/core/tritri.d.ts +5 -0
  67. package/dist/core/tritri.js +120 -0
  68. package/dist/core/unwrap.d.ts +72 -0
  69. package/dist/core/unwrap.js +800 -0
  70. package/dist/core/usercode.d.ts +27 -0
  71. package/dist/core/usercode.js +87 -0
  72. package/dist/core/uv.d.ts +15 -16
  73. package/dist/core/uv.js +36 -189
  74. package/dist/core/uvislands.d.ts +62 -0
  75. package/dist/core/uvislands.js +574 -0
  76. package/dist/core/uvoverlap.d.ts +58 -0
  77. package/dist/core/uvoverlap.js +220 -0
  78. package/dist/core/uvpack.d.ts +64 -0
  79. package/dist/core/uvpack.js +628 -0
  80. package/dist/core/uvreport.d.ts +127 -0
  81. package/dist/core/uvreport.js +372 -0
  82. package/dist/core/wrangle.d.ts +6 -1
  83. package/dist/core/wrangle.js +27 -16
  84. package/dist/core/xatlas.d.ts +16 -1
  85. package/dist/core/xatlas.js +95 -61
  86. package/dist/engines.d.ts +63 -0
  87. package/dist/engines.js +180 -0
  88. package/dist/explain.d.ts +4 -3
  89. package/dist/explain.js +129 -12
  90. package/dist/graph/engine.d.ts +92 -29
  91. package/dist/graph/engine.js +298 -148
  92. package/dist/graph/expr.d.ts +6 -3
  93. package/dist/graph/expr.js +18 -8
  94. package/dist/graph/outputs.d.ts +23 -0
  95. package/dist/graph/outputs.js +41 -0
  96. package/dist/graph/packages.d.ts +72 -0
  97. package/dist/graph/packages.js +261 -0
  98. package/dist/graph/parallel.d.ts +3 -1
  99. package/dist/graph/parallel.js +4 -1
  100. package/dist/graph/params.d.ts +6 -0
  101. package/dist/graph/params.js +34 -7
  102. package/dist/graph/scope.d.ts +55 -0
  103. package/dist/graph/scope.js +14 -0
  104. package/dist/graph/types.d.ts +75 -12
  105. package/dist/graph/types.js +4 -2
  106. package/dist/graph/vars.d.ts +16 -0
  107. package/dist/graph/vars.js +80 -0
  108. package/dist/host/artifacts.d.ts +10 -1
  109. package/dist/host/artifacts.js +37 -6
  110. package/dist/host/cook.d.ts +241 -0
  111. package/dist/host/cook.js +844 -0
  112. package/dist/host/export-targets.d.ts +42 -5
  113. package/dist/host/export-targets.js +72 -13
  114. package/dist/host/godot-commands.d.ts +4 -0
  115. package/dist/host/godot-commands.js +21 -0
  116. package/dist/host/godot.d.ts +183 -0
  117. package/dist/host/godot.js +916 -0
  118. package/dist/host/node.d.ts +12 -7
  119. package/dist/host/node.js +27 -9
  120. package/dist/host/pool.js +51 -2
  121. package/dist/host/sidecars.d.ts +8 -0
  122. package/dist/host/sidecars.js +50 -0
  123. package/dist/host/types.d.ts +19 -6
  124. package/dist/host/version.d.ts +1 -0
  125. package/dist/host/version.js +3 -0
  126. package/dist/host/worker.js +5 -2
  127. package/dist/index.d.ts +5 -3
  128. package/dist/index.js +3 -2
  129. package/dist/inspect.d.ts +84 -1
  130. package/dist/inspect.js +271 -24
  131. package/dist/io/common.d.ts +33 -5
  132. package/dist/io/export.js +2 -1
  133. package/dist/io/files.d.ts +23 -4
  134. package/dist/io/files.js +60 -8
  135. package/dist/io/gltf.js +355 -51
  136. package/dist/io/heightmap.d.ts +3 -0
  137. package/dist/io/heightmap.js +1 -0
  138. package/dist/io/import.js +10 -0
  139. package/dist/io/inspect-scene.d.ts +55 -0
  140. package/dist/io/inspect-scene.js +155 -0
  141. package/dist/io/kinds.d.ts +81 -0
  142. package/dist/io/kinds.js +163 -0
  143. package/dist/io/points.d.ts +2 -1
  144. package/dist/io/points.js +3 -1
  145. package/dist/io/sidecars.d.ts +24 -0
  146. package/dist/io/sidecars.js +57 -0
  147. package/dist/io/source.d.ts +65 -0
  148. package/dist/io/source.js +112 -0
  149. package/dist/io/targets.d.ts +47 -3
  150. package/dist/io/targets.js +149 -14
  151. package/dist/io/textures.js +21 -1
  152. package/dist/io/unreal.js +5 -1
  153. package/dist/mcp/docs.d.ts +10 -1
  154. package/dist/mcp/docs.js +816 -119
  155. package/dist/mcp/inputs.d.ts +12 -5
  156. package/dist/mcp/inputs.js +50 -13
  157. package/dist/mcp/server.js +70 -24
  158. package/dist/mcp/tools.d.ts +14 -7
  159. package/dist/mcp/tools.js +207 -108
  160. package/dist/mcp/verify.d.ts +2 -2
  161. package/dist/mcp/verify.js +1 -1
  162. package/dist/mcp/workspace.d.ts +15 -0
  163. package/dist/mcp/workspace.js +349 -0
  164. package/dist/nodes/attributes.js +198 -1
  165. package/dist/nodes/bake.js +207 -83
  166. package/dist/nodes/beam.d.ts +1 -0
  167. package/dist/nodes/beam.js +201 -0
  168. package/dist/nodes/bevel.js +54 -5
  169. package/dist/nodes/cloth.js +2 -0
  170. package/dist/nodes/code.js +22 -10
  171. package/dist/nodes/curves.js +440 -93
  172. package/dist/nodes/flow.js +53 -33
  173. package/dist/nodes/generate.js +13 -39
  174. package/dist/nodes/heightfield.js +248 -77
  175. package/dist/nodes/imageops.d.ts +1 -0
  176. package/dist/nodes/imageops.js +203 -0
  177. package/dist/nodes/index.d.ts +4 -0
  178. package/dist/nodes/index.js +4 -0
  179. package/dist/nodes/material.js +91 -23
  180. package/dist/nodes/measure.js +45 -18
  181. package/dist/nodes/particles.js +2 -0
  182. package/dist/nodes/pcg.js +6 -5
  183. package/dist/nodes/procedural.js +4 -3
  184. package/dist/nodes/rbd.js +1 -1
  185. package/dist/nodes/remesh.js +71 -15
  186. package/dist/nodes/scene.d.ts +1 -0
  187. package/dist/nodes/scene.js +55 -0
  188. package/dist/nodes/sdf.js +10 -5
  189. package/dist/nodes/solid.js +20 -5
  190. package/dist/nodes/solidify.d.ts +1 -0
  191. package/dist/nodes/solidify.js +52 -0
  192. package/dist/nodes/splines.d.ts +15 -1
  193. package/dist/nodes/splines.js +112 -105
  194. package/dist/nodes/texture.d.ts +21 -6
  195. package/dist/nodes/texture.js +1318 -185
  196. package/dist/nodes/topology.d.ts +7 -0
  197. package/dist/nodes/topology.js +133 -6
  198. package/dist/nodes/transform.js +11 -3
  199. package/dist/nodes/util.d.ts +1 -1
  200. package/dist/nodes/uv.js +192 -174
  201. package/dist/nodes/wasmcall.js +6 -2
  202. package/dist/open.d.ts +19 -0
  203. package/dist/open.js +12 -0
  204. package/dist/package.d.ts +288 -0
  205. package/dist/package.js +772 -0
  206. package/dist/packages/check.d.ts +32 -0
  207. package/dist/packages/check.js +169 -0
  208. package/dist/packages/commands.d.ts +57 -0
  209. package/dist/packages/commands.js +537 -0
  210. package/dist/packages/registry.d.ts +796 -0
  211. package/dist/packages/registry.js +2998 -0
  212. package/dist/packages/update.d.ts +124 -0
  213. package/dist/packages/update.js +476 -0
  214. package/dist/render/options.d.ts +2 -0
  215. package/dist/render/options.js +44 -5
  216. package/dist/render/pbr.d.ts +28 -0
  217. package/dist/render/pbr.js +378 -0
  218. package/dist/render/preview.d.ts +69 -0
  219. package/dist/render/preview.js +26 -0
  220. package/dist/render/raster.d.ts +78 -7
  221. package/dist/render/raster.js +600 -145
  222. package/dist/render/texview.d.ts +7 -1
  223. package/dist/render/texview.js +34 -2
  224. package/dist/scan.d.ts +61 -0
  225. package/dist/scan.js +228 -0
  226. package/dist/session.d.ts +311 -0
  227. package/dist/session.js +849 -0
  228. package/dist/viewer/app/api.js +48 -6
  229. package/dist/viewer/app/camera.js +1 -1
  230. package/dist/viewer/app/cooking.js +78 -0
  231. package/dist/viewer/app/embed.js +100 -0
  232. package/dist/viewer/app/export-popover.js +118 -16
  233. package/dist/viewer/app/grid.js +6 -2
  234. package/dist/viewer/app/guides.js +123 -1
  235. package/dist/viewer/app/keymap.js +7 -4
  236. package/dist/viewer/app/layers.js +5 -5
  237. package/dist/viewer/app/main.js +186 -53
  238. package/dist/viewer/app/markers.js +61 -1
  239. package/dist/viewer/app/outliner.js +72 -27
  240. package/dist/viewer/app/output-info.js +45 -0
  241. package/dist/viewer/app/outputs.css +147 -8
  242. package/dist/viewer/app/outputs.js +168 -54
  243. package/dist/viewer/app/packages.js +614 -0
  244. package/dist/viewer/app/props.js +177 -53
  245. package/dist/viewer/app/scene.js +35 -6
  246. package/dist/viewer/app/select.js +5 -4
  247. package/dist/viewer/app/selection.js +1 -1
  248. package/dist/viewer/app/status.js +12 -15
  249. package/dist/viewer/app/style.css +66 -8
  250. package/dist/viewer/app/units.js +1 -1
  251. package/dist/viewer/app/vars.js +48 -1
  252. package/dist/viewer/app/welcome.js +72 -0
  253. package/dist/viewer/graph-file.d.ts +62 -0
  254. package/dist/viewer/graph-file.js +175 -0
  255. package/dist/viewer/index.html +24 -11
  256. package/dist/viewer/json-edit.d.ts +13 -0
  257. package/dist/viewer/json-edit.js +274 -0
  258. package/dist/viewer/outputs.d.ts +49 -5
  259. package/dist/viewer/outputs.js +264 -56
  260. package/dist/viewer/packages.d.ts +121 -0
  261. package/dist/viewer/packages.js +584 -0
  262. package/dist/viewer/server.d.ts +24 -7
  263. package/dist/viewer/server.js +289 -31
  264. package/dist/web/app/web.css +419 -0
  265. package/dist/web/app/web.js +188 -0
  266. package/dist/web/bake.d.ts +34 -0
  267. package/dist/web/bake.js +153 -0
  268. package/dist/web/folder-page.d.ts +101 -0
  269. package/dist/web/folder-page.js +146 -0
  270. package/dist/web/folders.d.ts +280 -0
  271. package/dist/web/folders.js +970 -0
  272. package/dist/web/library.d.ts +91 -0
  273. package/dist/web/library.js +228 -0
  274. package/dist/web/markdown.d.ts +45 -0
  275. package/dist/web/markdown.js +677 -0
  276. package/dist/web/pages.d.ts +258 -0
  277. package/dist/web/pages.js +301 -0
  278. package/dist/web/server.d.ts +24 -0
  279. package/dist/web/server.js +1068 -0
  280. package/dist/workspace.d.ts +88 -105
  281. package/dist/workspace.js +359 -375
  282. package/integrations/godot/addons/topotopo/LICENSE +202 -0
  283. package/integrations/godot/addons/topotopo/README.md +57 -0
  284. package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
  285. package/integrations/godot/addons/topotopo/dock.gd +266 -0
  286. package/integrations/godot/addons/topotopo/dock.tscn +72 -0
  287. package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
  288. package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
  289. package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
  290. package/integrations/godot/addons/topotopo/inspector.gd +38 -0
  291. package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
  292. package/integrations/godot/addons/topotopo/package.gd +23 -0
  293. package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
  294. package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
  295. package/integrations/godot/addons/topotopo/plugin.gd +970 -0
  296. package/integrations/godot/addons/topotopo/record.gd +435 -0
  297. package/integrations/godot/addons/topotopo/runner.gd +458 -0
  298. package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
  299. package/integrations/godot/addons/topotopo/watcher.gd +199 -0
  300. package/kernels/pkg/README.md +23 -29
  301. package/kernels/pkg-simd/README.md +23 -29
  302. package/library/architecture/versions/1.0/architecture.topo +12 -0
  303. package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
  304. package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
  305. package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
  306. package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
  307. package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
  308. package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
  309. package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
  310. package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
  311. package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
  312. package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
  313. package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
  314. package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
  315. package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
  316. package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
  317. package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
  318. package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
  319. package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
  320. package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
  321. package/library/architecture/versions/1.0/version.json +1119 -0
  322. package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
  323. package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
  324. package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
  325. package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
  326. package/library/nature/versions/1.0/graphs/lineup.json +24 -0
  327. package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
  328. package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
  329. package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
  330. package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
  331. package/library/nature/versions/1.0/nature.topo +12 -0
  332. package/library/nature/versions/1.0/version.json +534 -0
  333. package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
  334. package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
  335. package/library/props/versions/1.0/graphs/lineup.json +18 -0
  336. package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
  337. package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
  338. package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
  339. package/library/props/versions/1.0/props.topo +12 -0
  340. package/library/props/versions/1.0/version.json +336 -0
  341. package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
  342. package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
  343. package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
  344. package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
  345. package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
  346. package/library/street/versions/1.0/graphs/lineup.json +22 -0
  347. package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
  348. package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
  349. package/library/street/versions/1.0/street.topo +12 -0
  350. package/library/street/versions/1.0/version.json +421 -0
  351. package/package.json +23 -8
  352. package/dist/graph/migrate.d.ts +0 -23
  353. package/dist/graph/migrate.js +0 -58
  354. package/shelf/index.json +0 -190
@@ -33,3 +33,30 @@ export declare function runUserCode<T>(host: Host, fn: () => T, timeoutMs: numbe
33
33
  * run of user code). null when reading it did not finish within timeoutMs.
34
34
  */
35
35
  export declare function describeGuarded(host: Host, e: any, timeoutMs: number, withStack?: boolean): Thrown | null;
36
+ /** JavaScript's reserved words (and the names strict code cannot bind): a var with one of these names cannot be read. */
37
+ export declare const RESERVED_WORDS: ReadonlySet<string>;
38
+ /**
39
+ * The standard globals of a JavaScript realm. Code sees them (the scripting realm has these, the snippet library and
40
+ * console); a var of the same name would hide one. test/engine.ts checks the list against the scripting realm.
41
+ */
42
+ export declare const JS_GLOBALS: ReadonlySet<string>;
43
+ /**
44
+ * The graph vars `code` reads by their bare name: the names of `vars` that appear in it as identifiers (anywhere, also
45
+ * in strings and comments: a superset of what it can read), sorted. Names no binding can carry (keywords, `vars`,
46
+ * names starting with "__") are left out: code cannot read them.
47
+ */
48
+ export declare function codeVarNames(code: string, vars: Record<string, any> | undefined): string[];
49
+ /** The vars of `names`, for a cache key (missing ones are left out). */
50
+ export declare function pickVars(vars: Record<string, any>, names: string[]): Record<string, any>;
51
+ /** Freeze a value and everything in it (typed arrays, which cannot be frozen, stay as they are). */
52
+ export declare function deepFreeze<T>(v: T): T;
53
+ /**
54
+ * What code gets for the vars `names`: frozen copies (writing a var fails as writing a constant does, and nothing the
55
+ * code does reaches the graph's own values or the next node).
56
+ */
57
+ export declare function codeVarValues(vars: Record<string, any>, names: string[]): Record<string, any>;
58
+ /**
59
+ * The declarations that bind the vars `names` from the object `values` and `vars` to the guard `guard` (source text
60
+ * for the function around the code): `const vars = __varsGuard, height = __vars["height"];`.
61
+ */
62
+ export declare function codeVarDecl(names: string[], values?: string, guard?: string): string;
@@ -83,3 +83,90 @@ export function describeGuarded(host, e, timeoutMs, withStack = false) {
83
83
  }
84
84
  return box.t ?? null;
85
85
  }
86
+ // ---------------------------------------------------------------- graph vars in user code
87
+ //
88
+ // Code reads graph vars the way "=expr" params do: by their bare name (`height`, not `vars.height`). A node binds the
89
+ // vars its code names once per cook, as constants of a function around the code, so the code's own names (its locals,
90
+ // the node's per-element names such as @ptnum or a heightfield layer) come first. Only the vars the code names are
91
+ // bound, and only they key the node's cache (NodeDef.usesVars). `vars` itself is bound to a guard whose every use
92
+ // fails with what to write instead.
93
+ /** JavaScript's reserved words (and the names strict code cannot bind): a var with one of these names cannot be read. */
94
+ export const RESERVED_WORDS = new Set([
95
+ 'await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'else', 'enum',
96
+ 'export', 'extends', 'false', 'finally', 'for', 'function', 'if', 'implements', 'import', 'in', 'instanceof',
97
+ 'interface', 'let', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'static', 'super', 'switch',
98
+ 'this', 'throw', 'true', 'try', 'typeof', 'var', 'void', 'while', 'with', 'yield', 'arguments', 'eval',
99
+ ]);
100
+ /**
101
+ * The standard globals of a JavaScript realm. Code sees them (the scripting realm has these, the snippet library and
102
+ * console); a var of the same name would hide one. test/engine.ts checks the list against the scripting realm.
103
+ */
104
+ export const JS_GLOBALS = new Set([
105
+ 'globalThis', 'Infinity', 'NaN', 'undefined', 'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI',
106
+ 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape', 'unescape', 'AggregateError', 'Array', 'ArrayBuffer',
107
+ 'AsyncDisposableStack', 'Atomics', 'BigInt', 'BigInt64Array', 'BigUint64Array', 'Boolean', 'DataView', 'Date',
108
+ 'DisposableStack', 'Error', 'EvalError', 'FinalizationRegistry', 'Float16Array', 'Float32Array', 'Float64Array',
109
+ 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Intl', 'Iterator', 'JSON', 'Map', 'Math', 'Number', 'Object',
110
+ 'Promise', 'Proxy', 'RangeError', 'ReferenceError', 'Reflect', 'RegExp', 'Set', 'SharedArrayBuffer', 'String',
111
+ 'SuppressedError', 'Symbol', 'SyntaxError', 'TypeError', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array',
112
+ 'Uint32Array', 'URIError', 'WeakMap', 'WeakRef', 'WeakSet', 'WebAssembly', 'console',
113
+ ]);
114
+ // an identifier not preceded by ".", "@" (wrangle attributes), "#" (private names) or a backslash
115
+ const CODE_NAME = /(?<![\w$.@#\\])[A-Za-z_$][\w$]*/g;
116
+ const BINDABLE = /^[A-Za-z_$][\w$]*$/;
117
+ /**
118
+ * The graph vars `code` reads by their bare name: the names of `vars` that appear in it as identifiers (anywhere, also
119
+ * in strings and comments: a superset of what it can read), sorted. Names no binding can carry (keywords, `vars`,
120
+ * names starting with "__") are left out: code cannot read them.
121
+ */
122
+ export function codeVarNames(code, vars) {
123
+ if (!vars || typeof code !== 'string' || !code)
124
+ return [];
125
+ const out = new Set();
126
+ for (const m of code.matchAll(CODE_NAME)) {
127
+ const n = m[0];
128
+ if (Object.hasOwn(vars, n) && BINDABLE.test(n) && !RESERVED_WORDS.has(n) && n !== 'vars' && !n.startsWith('__'))
129
+ out.add(n);
130
+ }
131
+ return [...out].sort();
132
+ }
133
+ /** The vars of `names`, for a cache key (missing ones are left out). */
134
+ export function pickVars(vars, names) {
135
+ const out = {};
136
+ for (const n of names)
137
+ if (Object.hasOwn(vars, n))
138
+ out[n] = vars[n];
139
+ return out;
140
+ }
141
+ /** Freeze a value and everything in it (typed arrays, which cannot be frozen, stay as they are). */
142
+ export function deepFreeze(v) {
143
+ if (v === null || typeof v !== 'object' || ArrayBuffer.isView(v) || Object.isFrozen(v))
144
+ return v;
145
+ Object.freeze(v);
146
+ for (const k of Object.keys(v))
147
+ deepFreeze(v[k]);
148
+ return v;
149
+ }
150
+ /**
151
+ * What code gets for the vars `names`: frozen copies (writing a var fails as writing a constant does, and nothing the
152
+ * code does reaches the graph's own values or the next node).
153
+ */
154
+ export function codeVarValues(vars, names) {
155
+ const out = {};
156
+ for (const n of names) {
157
+ try {
158
+ out[n] = deepFreeze(structuredClone(vars[n]));
159
+ }
160
+ catch (e) {
161
+ throw new Error(`var "${n}" cannot be read by code: ${e?.message ?? e}`);
162
+ }
163
+ }
164
+ return Object.freeze(out);
165
+ }
166
+ /**
167
+ * The declarations that bind the vars `names` from the object `values` and `vars` to the guard `guard` (source text
168
+ * for the function around the code): `const vars = __varsGuard, height = __vars["height"];`.
169
+ */
170
+ export function codeVarDecl(names, values = '__vars', guard = '__varsGuard') {
171
+ return `const vars = ${guard}${names.map((n) => `, ${n} = ${values}[${JSON.stringify(n)}]`).join('')};`;
172
+ }
package/dist/core/uv.d.ts CHANGED
@@ -96,19 +96,6 @@ export declare function lscm(pos: Float64Array, tris: Int32Array, pins?: [number
96
96
  }): LscmResult;
97
97
  /** Approximate farthest-apart pair (double sweep) among candidate vertex indices (all when null). */
98
98
  export declare function farthestPair(pos: Float64Array, cand: ArrayLike<number> | null, n: number): [number, number];
99
- export interface LayoutOptions {
100
- /** rotate islands to their minimum-area bounding rectangle, and allow 90° turns when packing */
101
- rotate: boolean;
102
- /** uniform: equal texel density (UV area ∝ 3D area); keep: keep relative island scale */
103
- scaleMode: 'uniform' | 'keep';
104
- /** gap between islands (and half of it to the tile border) in UV units of one tile */
105
- padding: number;
106
- /** number of UDIM tiles to fill (1 = the 0..1 square) */
107
- udims: number;
108
- /** random orderings tried; the tightest packing wins */
109
- iterations: number;
110
- seed: number;
111
- }
112
99
  export interface Island {
113
100
  prims: number[];
114
101
  /** distinct vertices (corners) of the island */
@@ -125,11 +112,23 @@ export declare function findIslands(g: Geometry, uv: Float64Array, byAttr?: Int3
125
112
  export declare function convexHull2D(pts: ArrayLike<number>): number[];
126
113
  /** Angle (radians) that rotates a hull to its minimum-area bounding rectangle (rotating calipers over hull edges). */
127
114
  export declare function minAreaRectAngle(hull: number[]): number;
115
+ export interface Rect {
116
+ w: number;
117
+ h: number;
118
+ id: number;
119
+ }
120
+ export interface Placed {
121
+ x: number;
122
+ y: number;
123
+ rot: boolean;
124
+ bin: number;
125
+ }
128
126
  /**
129
- * Pack islands into the 0..1 tile (or UDIM tiles 1001.. going right then up, 10 per row). Returns new
130
- * per-vertex uv (vertices outside every island keep their input uv).
127
+ * Skyline bottom-left packing of rects into `bins` rectangles of W × H. Returns placements (by rect id) or null if they
128
+ * do not fit. The skyline is a list of segments [x0, x1) at height y that tile [0, W) exactly; a rect rests on the
129
+ * highest segment under its whole span, so no two rects overlap.
131
130
  */
132
- export declare function packIslands(uv: Float64Array, islands: Island[], o: LayoutOptions): Float64Array;
131
+ export declare function skylinePack(rects: Rect[], W: number, bins: number, allowRotate: boolean, H?: number): Placed[] | null;
133
132
  /** Scale each chart's uv uniformly into the unit square (keeping its aspect). */
134
133
  export declare function normaliseCharts(uv: Float64Array, charts: {
135
134
  verts: number[];
package/dist/core/uv.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { primArea, triangulate } from './topology.js';
2
- import { rng } from './random.js';
3
2
  // ---------------------------------------------------------------- uv io
4
3
  /** Per-vertex (u, v) from a vertex or point `uv` attribute, or null when there is none. */
5
4
  export function readUV(g) {
@@ -800,224 +799,72 @@ export function minAreaRectAngle(hull) {
800
799
  }
801
800
  return best;
802
801
  }
803
- /** Skyline bottom-left packing of rects into `bins` squares of side W. Returns placements or null if they don't fit. */
804
- function skylinePack(rects, W, bins, allowRotate) {
802
+ /**
803
+ * Skyline bottom-left packing of rects into `bins` rectangles of W × H. Returns placements (by rect id) or null if they
804
+ * do not fit. The skyline is a list of segments [x0, x1) at height y that tile [0, W) exactly; a rect rests on the
805
+ * highest segment under its whole span, so no two rects overlap.
806
+ */
807
+ export function skylinePack(rects, W, bins, allowRotate, H = W) {
805
808
  const out = new Array(rects.length);
806
809
  let bin = 0;
807
- let sky = [{ x: 0, y: 0, w: W }];
808
- const eps = W * 1e-9;
810
+ let sky = [{ x0: 0, x1: W, y: 0 }];
809
811
  for (const r of rects) {
810
- let placed = false;
811
- while (!placed) {
812
+ for (;;) {
812
813
  let best = null;
813
- for (const rot of allowRotate && Math.abs(r.w - r.h) > eps ? [false, true] : [false]) {
814
+ for (const rot of allowRotate && r.w !== r.h ? [false, true] : [false]) {
814
815
  const w = rot ? r.h : r.w, h = rot ? r.w : r.h;
815
- if (w > W + eps || h > W + eps)
816
+ if (!(w <= W && h <= H))
816
817
  continue;
817
818
  for (let i = 0; i < sky.length; i++) {
818
- const x = sky[i].x;
819
- if (x + w > W + eps)
819
+ const x = sky[i].x0;
820
+ if (x + w > W)
820
821
  break;
821
- let y = 0, rem = w, j = i;
822
- while (rem > eps && j < sky.length) {
823
- y = Math.max(y, sky[j].y);
824
- rem -= sky[j].w;
825
- j++;
826
- }
827
- if (y + h > W + eps)
828
- continue;
822
+ let y = 0;
823
+ for (let j = i; j < sky.length && sky[j].x0 < x + w; j++)
824
+ if (sky[j].y > y)
825
+ y = sky[j].y;
829
826
  const top = y + h;
830
- if (!best || top < best.top - eps || (Math.abs(top - best.top) <= eps && x < best.x))
831
- best = { i, y, top, x, rot, w, h };
827
+ if (top > H)
828
+ continue;
829
+ if (!best || top < best.top || (top === best.top && x < best.x))
830
+ best = { x, y, top, rot, w };
832
831
  }
833
832
  }
834
833
  if (!best) {
835
- bin++;
836
- if (bin >= bins)
834
+ if (++bin >= bins)
837
835
  return null;
838
- sky = [{ x: 0, y: 0, w: W }];
836
+ sky = [{ x0: 0, x1: W, y: 0 }];
839
837
  continue;
840
838
  }
841
839
  out[r.id] = { x: best.x, y: best.y, rot: best.rot, bin };
842
- // update the skyline: new segment [x, x+w) at height top
843
- const nx = best.x, nw = best.w, ny = best.top;
840
+ // the new segment [x, x + w) at the rect's top; the segments it covers are clipped exactly
841
+ const n0 = best.x, n1 = best.x + best.w;
844
842
  const next = [];
845
843
  for (const s of sky) {
846
- const s0 = s.x, s1 = s.x + s.w;
847
- if (s1 <= nx + eps || s0 >= nx + nw - eps) {
844
+ if (s.x1 <= n0 || s.x0 >= n1) {
848
845
  next.push(s);
849
846
  continue;
850
847
  }
851
- if (s0 < nx)
852
- next.push({ x: s0, y: s.y, w: nx - s0 });
853
- if (s1 > nx + nw)
854
- next.push({ x: nx + nw, y: s.y, w: s1 - nx - nw });
848
+ if (s.x0 < n0)
849
+ next.push({ x0: s.x0, x1: n0, y: s.y });
850
+ if (s.x1 > n1)
851
+ next.push({ x0: n1, x1: s.x1, y: s.y });
855
852
  }
856
- next.push({ x: nx, y: ny, w: nw });
857
- next.sort((a, b) => a.x - b.x);
853
+ next.push({ x0: n0, x1: n1, y: best.top });
854
+ next.sort((p, q) => p.x0 - q.x0);
858
855
  sky = [];
859
856
  for (const s of next) {
857
+ if (!(s.x1 > s.x0))
858
+ continue;
860
859
  const last = sky[sky.length - 1];
861
- if (last && Math.abs(last.y - s.y) <= eps)
862
- last.w += s.w;
860
+ if (last && last.y === s.y)
861
+ last.x1 = s.x1;
863
862
  else
864
863
  sky.push({ ...s });
865
864
  }
866
- placed = true;
867
- }
868
- }
869
- return out;
870
- }
871
- /**
872
- * Pack islands into the 0..1 tile (or UDIM tiles 1001.. going right then up, 10 per row). Returns new
873
- * per-vertex uv (vertices outside every island keep their input uv).
874
- */
875
- export function packIslands(uv, islands, o) {
876
- const out = uv.slice();
877
- const nI = islands.length;
878
- if (!nI)
879
- return out;
880
- // 1. orient and measure each island
881
- const ang = new Float64Array(nI), minx = new Float64Array(nI), miny = new Float64Array(nI), w = new Float64Array(nI), h = new Float64Array(nI), sc = new Float64Array(nI);
882
- let densSum = 0, densN = 0;
883
- for (let k = 0; k < nI; k++) {
884
- const isl = islands[k];
885
- const pts = new Float64Array(isl.verts.length * 2);
886
- isl.verts.forEach((v, i) => { pts[i * 2] = uv[v * 2]; pts[i * 2 + 1] = uv[v * 2 + 1]; });
887
- const a = o.rotate && pts.length >= 4 ? minAreaRectAngle(convexHull2D(pts)) : 0;
888
- const c = Math.cos(a), s = Math.sin(a);
889
- let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
890
- for (let i = 0; i < pts.length; i += 2) {
891
- const x = pts[i] * c - pts[i + 1] * s, y = pts[i] * s + pts[i + 1] * c;
892
- if (x < x0)
893
- x0 = x;
894
- if (x > x1)
895
- x1 = x;
896
- if (y < y0)
897
- y0 = y;
898
- if (y > y1)
899
- y1 = y;
900
- }
901
- if (!(x0 <= x1)) {
902
- x0 = x1 = y0 = y1 = 0;
903
- }
904
- ang[k] = a;
905
- minx[k] = x0;
906
- miny[k] = y0;
907
- w[k] = x1 - x0;
908
- h[k] = y1 - y0;
909
- if (isl.areaUV > 1e-18 && isl.area3 > 1e-18) {
910
- densSum += Math.sqrt(isl.area3 / isl.areaUV);
911
- densN++;
912
- }
913
- }
914
- const meanDens = densN ? densSum / densN : 1;
915
- for (let k = 0; k < nI; k++) {
916
- const isl = islands[k];
917
- if (o.scaleMode === 'uniform')
918
- sc[k] = isl.areaUV > 1e-18 && isl.area3 > 1e-18 ? Math.sqrt(isl.area3 / isl.areaUV) : meanDens;
919
- else
920
- sc[k] = 1;
921
- w[k] *= sc[k];
922
- h[k] *= sc[k];
923
- }
924
- // normalise scale so sizes are O(1) (keeps tolerances meaningful)
925
- let maxDim = 0;
926
- for (let k = 0; k < nI; k++)
927
- maxDim = Math.max(maxDim, w[k], h[k]);
928
- if (!(maxDim > 0))
929
- maxDim = 1;
930
- for (let k = 0; k < nI; k++) {
931
- sc[k] /= maxDim;
932
- w[k] /= maxDim;
933
- h[k] /= maxDim;
934
- }
935
- const bins = Math.max(1, Math.floor(o.udims));
936
- const pad = Math.max(0, o.padding);
937
- // 2. orderings
938
- const base = Array.from({ length: nI }, (_, i) => i).sort((a, b) => Math.max(h[b], w[b]) - Math.max(h[a], w[a]) || h[b] - h[a] || a - b);
939
- const R = rng(o.seed * 7919 + 17);
940
- let bestSide = Infinity, bestPlace = null;
941
- const iters = Math.max(1, Math.min(64, Math.floor(o.iterations)));
942
- let area = 0, maxSide = 0;
943
- for (let k = 0; k < nI; k++) {
944
- area += w[k] * h[k];
945
- maxSide = Math.max(maxSide, o.rotate ? Math.max(w[k], h[k]) : Math.max(w[k], h[k]));
946
- }
947
- for (let it = 0; it < iters; it++) {
948
- const order = base.slice();
949
- if (it > 0) {
950
- // perturb: random adjacent swaps weighted towards small displacements
951
- for (let s = 0; s < nI; s++) {
952
- const i = Math.floor(R() * nI), j = Math.min(nI - 1, i + 1 + Math.floor(R() * Math.max(1, nI * 0.1)));
953
- [order[i], order[j]] = [order[j], order[i]];
954
- }
955
- }
956
- const pack = (S) => {
957
- const inflate = pad * S;
958
- const rects = order.map((k, i) => ({ w: w[k] + inflate, h: h[k] + inflate, id: i }));
959
- if (o.rotate)
960
- for (const r of rects)
961
- if (r.h > r.w) {
962
- const t = r.w;
963
- r.w = r.h;
964
- r.h = t;
965
- r.pre = true;
966
- }
967
- const res = skylinePack(rects, S, bins, o.rotate);
968
- if (!res)
969
- return null;
970
- return res.map((pl, i) => ({ ...pl, rot: rects[i].pre ? !pl.rot : pl.rot }));
971
- };
972
- // bracket the side length, then bisect
973
- let lo = Math.max(maxSide, Math.sqrt(area / bins)) / (1 + pad * 2 + 1e-9);
974
- let hi = Math.max(lo * 1.05, 1e-9);
975
- let found = pack(hi);
976
- let guard = 0;
977
- while (!found && guard++ < 60) {
978
- lo = hi;
979
- hi *= 1.25;
980
- found = pack(hi);
981
- }
982
- if (!found)
983
- continue;
984
- for (let b = 0; b < 14 && hi - lo > hi * 2e-3; b++) {
985
- const mid = (lo + hi) / 2;
986
- const f = pack(mid);
987
- if (f) {
988
- hi = mid;
989
- found = f;
990
- }
991
- else
992
- lo = mid;
993
- }
994
- if (hi < bestSide) {
995
- bestSide = hi;
996
- bestPlace = order.map((_, i) => found[i]);
997
- bestPlace.order = order;
865
+ break;
998
866
  }
999
867
  }
1000
- if (!bestPlace)
1001
- return out;
1002
- const order = bestPlace.order;
1003
- const S = bestSide, inflate = pad * S;
1004
- order.forEach((k, i) => {
1005
- const pl = bestPlace[i];
1006
- const isl = islands[k];
1007
- const c = Math.cos(ang[k]), s = Math.sin(ang[k]);
1008
- const tu = pl.bin % 10, tv = Math.floor(pl.bin / 10);
1009
- for (const v of isl.verts) {
1010
- const x = uv[v * 2] * c - uv[v * 2 + 1] * s, y = uv[v * 2] * s + uv[v * 2 + 1] * c;
1011
- let px = (x - minx[k]) * sc[k], py = (y - miny[k]) * sc[k];
1012
- if (pl.rot) {
1013
- const t = px;
1014
- px = h[k] - py;
1015
- py = t;
1016
- }
1017
- out[v * 2] = (pl.x + inflate / 2 + px) / S + tu;
1018
- out[v * 2 + 1] = (pl.y + inflate / 2 + py) / S + tv;
1019
- }
1020
- });
1021
868
  return out;
1022
869
  }
1023
870
  /** Scale each chart's uv uniformly into the unit square (keeping its aspect). */
@@ -0,0 +1,62 @@
1
+ import type { Geometry } from './geometry.ts';
2
+ import type { PackResult } from './uvpack.ts';
3
+ /**
4
+ * Per island texel weight from a numeric prim attribute (its first component), the mean over the island's prims
5
+ * weighted by 3D area; values <= 0 or not finite count as 1, so an attribute set on a group (attrib_create with group)
6
+ * weights only those parts. null when the attribute is missing.
7
+ */
8
+ export declare function islandWeights(g: Geometry, labels: ArrayLike<number>, count: number, attrib: string): Float64Array | null;
9
+ /**
10
+ * Per island the rotation (radians, counter-clockwise, as the packer's angles) that turns world up (+Y) to +v; an island
11
+ * with no up direction (a horizontal face) turns +X to +u instead. Gradients of the world coordinates over the uv of
12
+ * each polygon (fanned into triangles), weighted by uv area. NaN for an island with no uv area.
13
+ */
14
+ export declare function worldAngles(g: Geometry, uv: ArrayLike<number>, labels: ArrayLike<number>, count: number): Float64Array;
15
+ export interface CutOptions {
16
+ /** uniform: islands are sized by 3D area (equal texel density); keep: by their uv area */
17
+ scaleMode: 'uniform' | 'keep';
18
+ /** per island texel weight (islandWeights), or null */
19
+ weights?: ArrayLike<number> | null;
20
+ /** tiles the islands are packed into */
21
+ tiles: number;
22
+ /** > 0: tile sides per packing unit the islands are packed at (a fixed texel density), instead of the one that fills the tiles */
23
+ tileScale?: number;
24
+ /** > 0: the most tile sides per packing unit (a texel density the packing may not exceed) */
25
+ maxTileScale?: number;
26
+ }
27
+ export interface CutResult {
28
+ /** per prim the new island (parts numbered after the islands they come from, in order) */
29
+ labels: Int32Array;
30
+ count: number;
31
+ /** islands cut, the parts they became, and a prim of the first island cut (-1 when none was) */
32
+ cut: number;
33
+ parts: number;
34
+ firstCut: number;
35
+ }
36
+ /**
37
+ * Whether packing a (islands cut into parts) beats b (the islands whole) by enough to pay for the seams: fewer tiles,
38
+ * or a texel density at least 2 % higher.
39
+ */
40
+ export declare function packsDenser(a: PackResult, b: PackResult): boolean;
41
+ /** Cut charts longer than the tile, and rings and arcs, into parts (see the file comment). labels: island per prim. */
42
+ export declare function cutLongCharts(g: Geometry, uv: ArrayLike<number>, labels: ArrayLike<number>, count: number, o: CutOptions): CutResult;
43
+ export interface MergeOptions {
44
+ /** texture size and the padding between charts, in its texels */
45
+ resolution: number;
46
+ padding: number;
47
+ }
48
+ export interface MergeResult {
49
+ labels: Int32Array;
50
+ count: number;
51
+ merged: number;
52
+ }
53
+ /**
54
+ * Join thin charts (bevel strips, narrow bands) onto a neighbouring chart, so fewer charts each lose a padding border
55
+ * in the atlas. A thin chart (uniform texel density: its narrow side, at the density that fills about three quarters of
56
+ * the tile, under THIN_PADDINGS paddings, and long) is turned, scaled and shifted (a least squares similarity) so the
57
+ * seam it shares with the neighbour lines up; it joins when the scale is within MERGE_SCALE of the two charts' density
58
+ * ratio, the seam lines up within MERGE_FIT of its width, and it keeps its winding and overlaps neither itself nor the
59
+ * neighbour. The seam's corners then take the neighbour's uv, so
60
+ * it is no longer a seam. Neighbours are tried by shared seam length. uv (per vertex) is changed in place.
61
+ */
62
+ export declare function mergeThinCharts(g: Geometry, uv: Float64Array, labels: ArrayLike<number>, count: number, o: MergeOptions): MergeResult;