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
@@ -1,23 +1,25 @@
1
1
  import { Image, MAIN_LAYER } from '../core/image.js';
2
- import { averagedVertexNormals, bakeSamples, curvatureEdges, dilate, edgeGrid, idColor, interpolate, normalize3, resolve, triangleTangents, uvIslands, vertexPositions, vertexValues, } from '../core/bake.js';
2
+ import { averagedVertexNormals, bakeSamples, curvatureEdges, dilate, edgeGrid, idColor, interpolate, interpolateCorners, normalize3, resolve, uvIslands, vertexPositions, vertexValues, } from '../core/bake.js';
3
+ import { cornerTangents, loadMikkTSpace, mikkTSpaceReady, shadingFrame } from '../core/tangents.js';
3
4
  import { RayScene, bboxDiagonal } from '../core/analysis.js';
4
5
  import { pointNormals, resolveVertexNormals, triangulate } from '../core/topology.js';
5
6
  import { defineNode } from '../graph/registry.js';
6
7
  import { NodeError } from '../graph/types.js';
7
8
  import { andThen } from '../graph/parallel.js';
9
+ import { partsOf, primWhere } from '../core/report.js';
8
10
  import { P, mask } from './util.js';
9
- import { imageGeo, nameParam, resolutionParam, size2 } from './texture.js';
11
+ import { NAMED_BY_NODE, imageGeo, nameParam, outName, resolutionParam, size2 } from './texture.js';
10
12
  /** Most samples a bake may rasterise (output texels × supersample²): 4096², about 1 GB of per-sample arrays at full coverage. */
11
13
  const MAX_SAMPLES = 4096 * 4096;
12
14
  // ---------------------------------------------------------------- shared params
13
- const BAKE_PARAMS = (name) => ({
15
+ const BAKE_PARAMS = () => ({
14
16
  resolution: resolutionParam(1024),
15
17
  supersample: { type: 'int', default: 1, min: 1, max: 4, desc: 'samples per texel along each axis (2 = 4 samples averaged: smoother edges, 4× the work)', aliases: ['ssaa', 'antialiasing', 'subsampling'] },
16
18
  padding: { type: 'int', default: 4, min: 0, max: 256, desc: 'dilate islands by this many texels (no background bleeding in mipmaps)', aliases: ['dilation', 'margin', 'edge_padding'] },
17
19
  uv_attrib: { type: 'string', default: 'uv', desc: 'vertex or point uv attribute to bake into', aliases: ['uv_name', 'uvattrib'] },
18
20
  group: P.group('prim', 'prims to bake ("" = all); the others leave their texels empty'),
19
21
  keep_mesh: { type: 'bool', default: false, desc: 'output the input mesh with the image added (off: only the image)', aliases: ['keep_input', 'keep'] },
20
- name: nameParam(name),
22
+ name: nameParam(),
21
23
  });
22
24
  const INPUTS = [
23
25
  { name: 'geo', desc: 'mesh to bake (needs uv without overlaps: uv_unwrap or uv_layout)' },
@@ -25,6 +27,12 @@ const INPUTS = [
25
27
  ];
26
28
  const cageParam = () => ({ type: 'float', default: 0, min: 0, desc: 'high-res input: search distance along the cage normals on each side (metres; 0 = auto: 2% of the bbox diagonal)', aliases: ['cage_distance', 'max_frontal_distance', 'ray_distance'] });
27
29
  const cageNormalsParam = () => ({ type: 'enum', options: ['averaged', 'mesh'], default: 'averaged', desc: 'high-res input: ray directions: averaged normals over every face at a position (no gaps at hard edges) or the mesh\'s own normals', aliases: ['ray_normals', 'average_normals'] });
30
+ /** The params of every bake that reads a high-res mesh on input 1. */
31
+ const HIGH_PARAMS = () => ({
32
+ cage: cageParam(), cage_normals: cageNormalsParam(),
33
+ cage_attrib: { type: 'string', default: 'cage', desc: 'high-res input: a float attribute of the mesh (prim, point or vertex) that sets the cage where it is above 0, e.g. per part with attrib_create {name:"cage", class:"prim", group:"@name=grip", value:0.03}', aliases: ['cage_attribute'] },
34
+ match_name: { type: 'bool', default: false, desc: 'high-res input: each piece projects only onto the high prims with the same prim "name" (parts sharing one atlas do not pick up their neighbours)', aliases: ['match_names', 'by_name'] },
35
+ });
28
36
  function readUVNamed(g, name) {
29
37
  const f = g.findAttrib(name || 'uv', ['vertex', 'point']);
30
38
  if (!f || f.attr.type !== 'float')
@@ -81,12 +89,13 @@ function setup(ctx) {
81
89
  function finish(b, vals, ch, background, extra = {}) {
82
90
  const { ctx, s } = b, p = ctx.p;
83
91
  const { data, coverage } = resolve(s, vals, ch);
84
- const name = String(p.name ?? '').trim() || ctx.type.replace(/^bake_/, '');
85
- const img = new Image(s.W, s.H, name);
92
+ const img = new Image(s.W, s.H, outName(ctx));
86
93
  const pad = Math.max(0, Math.round(p.padding));
87
94
  dilate(data, ch, s.W, s.H, coverage, pad, background);
88
95
  img.set(MAIN_LAYER, data);
89
- for (const [k, e] of Object.entries(extra)) {
96
+ // with a high-res mesh: where the projection found nothing, to see which parts need a larger cage
97
+ const layers = b.missed ? { ...extra, missed: { vals: b.missed, ch: 1, background: [0] } } : extra;
98
+ for (const [k, e] of Object.entries(layers)) {
90
99
  const r = resolve(s, e.vals, e.ch);
91
100
  dilate(r.data, e.ch, s.W, s.H, coverage, pad, e.background);
92
101
  img.set(k, r.data);
@@ -117,14 +126,84 @@ function highOf(ctx) {
117
126
  * when the kernel ran on this thread.
118
127
  */
119
128
  function project(b, high, cage, normals) {
129
+ const { ctx } = b, n = b.s.n;
120
130
  const scene = RayScene.of(high);
121
- const HN = normals ? pointNormalsOf(high) : new Float64Array(0);
122
- const R = rayDirs(b);
123
- const D = new Float64Array(b.s.n), NO = new Float64Array(normals ? b.s.n * 3 : 0);
124
- const run = b.ctx.parallelMap('bake_project', {
125
- count: b.s.n, args: { cage }, inputs: { P: b.P, N: R }, results: normals ? { D, NO } : { D }, shared: { ...scene.toArrays(), HN },
126
- });
127
- return andThen(run, () => ({ D, NO, scene, R }));
131
+ const R = rayDirs(b), C = cagesOf(b, cage);
132
+ const D = new Float64Array(n), NO = new Float64Array(normals ? n * 3 : 0);
133
+ const done = () => {
134
+ const missed = new Float32Array(n);
135
+ for (let i = 0; i < n; i++)
136
+ if (!(D[i] === D[i]))
137
+ missed[i] = 1;
138
+ b.missed = missed;
139
+ let count = 0;
140
+ for (let i = 0; i < n; i++)
141
+ count += missed[i];
142
+ ctx.stat('missed', count);
143
+ return { D, NO, scene, R };
144
+ };
145
+ // match_name: each named piece of the mesh projects onto the high prims of the same name only
146
+ const lowName = b.g.prims.get('name'), highName = high.prims.get('name');
147
+ if (!ctx.p.match_name || !lowName || lowName.type !== 'string' || !highName || highName.type !== 'string') {
148
+ if (ctx.p.match_name)
149
+ ctx.warn(`${ctx.type}: match_name needs a prim "name" on the mesh and on the high mesh; every high prim is matched`);
150
+ const run = ctx.parallelMap('bake_project', {
151
+ count: n, args: { cage }, inputs: { P: b.P, N: R, C }, results: normals ? { D, NO } : { D }, shared: { ...scene.toArrays(), HN: normals ? pointNormalsOf(high) : new Float64Array(0) },
152
+ });
153
+ return andThen(run, done);
154
+ }
155
+ const ln = lowName.data, hn = highName.data;
156
+ const bySample = new Map();
157
+ for (let i = 0; i < n; i++) {
158
+ const k = ln[b.triPrim[b.s.tri[i]]] ?? '';
159
+ const l = bySample.get(k);
160
+ if (l)
161
+ l.push(i);
162
+ else
163
+ bySample.set(k, [i]);
164
+ }
165
+ const lonely = [], runs = [];
166
+ for (const [nm, idx] of bySample) {
167
+ const sel = Uint8Array.from(hn, (x) => (x === nm ? 1 : 0));
168
+ if (!sel.some((x) => x)) {
169
+ lonely.push(nm || '(no name)');
170
+ for (const i of idx)
171
+ D[i] = NaN;
172
+ continue;
173
+ }
174
+ const sub = high.subsetPrims(sel), sc = RayScene.of(sub), m = idx.length;
175
+ const Ps = new Float64Array(m * 3), Ns = new Float64Array(m * 3), Cs = C ? new Float64Array(m) : undefined;
176
+ idx.forEach((i, k) => { for (let c = 0; c < 3; c++) {
177
+ Ps[k * 3 + c] = b.P[i * 3 + c];
178
+ Ns[k * 3 + c] = R[i * 3 + c];
179
+ } if (Cs)
180
+ Cs[k] = C[i]; });
181
+ const Ds = new Float64Array(m), NOs = new Float64Array(normals ? m * 3 : 0);
182
+ const run = ctx.parallelMap('bake_project', {
183
+ count: m, args: { cage }, inputs: { P: Ps, N: Ns, C: Cs }, results: normals ? { D: Ds, NO: NOs } : { D: Ds }, shared: { ...sc.toArrays(), HN: normals ? pointNormalsOf(sub) : new Float64Array(0) },
184
+ });
185
+ runs.push(andThen(run, () => idx.forEach((i, k) => { D[i] = Ds[k]; if (normals)
186
+ for (let c = 0; c < 3; c++)
187
+ NO[i * 3 + c] = NOs[k * 3 + c]; })));
188
+ }
189
+ if (lonely.length)
190
+ ctx.warn(`${ctx.type}: match_name: no high prims named ${lonely.slice(0, 8).map((x) => `"${x}"`).join(', ')}${lonely.length > 8 ? ' …' : ''}; those pieces bake as misses`);
191
+ return runs.some((r) => r instanceof Promise) ? Promise.all(runs).then(done) : done();
192
+ }
193
+ /**
194
+ * Per-sample cage distances where the mesh carries the cage_attrib attribute (prim, point or vertex; e.g. set per part
195
+ * with attrib_create {name: "cage", class: "prim", group}): its value where > 0, else the node's cage. Null without one.
196
+ */
197
+ function cagesOf(b, cage) {
198
+ const name = String(b.ctx.p.cage_attrib ?? '').trim();
199
+ const f = name ? b.g.findAttrib(name, ['vertex', 'point', 'prim', 'detail']) : null;
200
+ if (!f || f.attr.type === 'string' || f.attr.isArray)
201
+ return undefined;
202
+ const C = interpolate(b.s, b.tris, vertexValues(b.g, name, 1), 1);
203
+ for (let i = 0; i < C.length; i++)
204
+ if (!(C[i] > 0))
205
+ C[i] = cage;
206
+ return C;
128
207
  }
129
208
  /** Cage ray directions per sample: averaged normals (default; no gaps at hard edges) or the mesh's own normals. */
130
209
  function rayDirs(b) {
@@ -134,16 +213,50 @@ function rayDirs(b) {
134
213
  normalize3(R);
135
214
  return R;
136
215
  }
137
- /** Point normals of a mesh: its point N when present, else area-weighted face normals. */
216
+ /**
217
+ * Point normals of a mesh: its point N when present, else area-weighted face normals. Points whose N is zero or not
218
+ * finite take the computed normal: a merge of parts with and without N fills the missing ones with zeros, which would
219
+ * bake as grey.
220
+ */
138
221
  function pointNormalsOf(g) {
139
222
  const a = g.points.get('N');
140
- if (a && a.type === 'float' && a.size === 3)
141
- return a.data;
142
- return pointNormals(g);
223
+ if (!a || a.type !== 'float' || a.size !== 3)
224
+ return pointNormals(g);
225
+ const N = a.data;
226
+ let out = null, computed = null;
227
+ for (let i = 0; i < N.length; i += 3) {
228
+ const l = Math.hypot(N[i], N[i + 1], N[i + 2]);
229
+ if (l > 1e-12 && Number.isFinite(l))
230
+ continue;
231
+ out ??= N.slice();
232
+ computed ??= pointNormals(g);
233
+ out[i] = computed[i];
234
+ out[i + 1] = computed[i + 1];
235
+ out[i + 2] = computed[i + 2];
236
+ }
237
+ return out ?? N;
143
238
  }
144
239
  /** andThen() for a callback that may itself return a promise (a cook result); keeps synchronous runs synchronous. */
145
240
  const chain = (x, f) => (x instanceof Promise ? x.then(f) : f(x));
146
241
  const cageOf = (b) => (b.ctx.p.cage > 0 ? b.ctx.p.cage : 0.02 * b.diag);
242
+ /** Where the projection missed, for its warning: the parts with missed samples (counts) and the first one's place. */
243
+ function missWhere(b) {
244
+ const m = b.missed;
245
+ if (!m)
246
+ return '';
247
+ const prims = [];
248
+ let first = -1;
249
+ for (let i = 0; i < b.s.n; i++)
250
+ if (m[i]) {
251
+ if (first < 0)
252
+ first = i;
253
+ prims.push(b.triPrim[b.s.tri[i]]);
254
+ }
255
+ if (first < 0)
256
+ return '';
257
+ const parts = partsOf(b.g, prims);
258
+ return `; ${parts ? `samples by part: ${parts}; ` : ''}the first at ${primWhere(b.g, b.triPrim[b.s.tri[first]], b.P.subarray(first * 3, first * 3 + 3))}`;
259
+ }
147
260
  /** Samples moved onto the high mesh (P, N) where the projection hit; the low surface elsewhere. */
148
261
  function onHigh(b, high) {
149
262
  return andThen(project(b, high, cageOf(b), true), ({ D, NO, scene, R }) => {
@@ -161,14 +274,14 @@ function onHigh(b, high) {
161
274
  }
162
275
  }
163
276
  if (miss)
164
- b.ctx.warn(`${b.ctx.type}: ${miss} of ${b.s.n} samples found no high-res surface within the cage (${cageOf(b).toPrecision(3)} m); they use the low surface (raise cage)`);
277
+ b.ctx.warn(`${b.ctx.type}: ${miss} of ${b.s.n} samples found no high-res surface within the cage (${cageOf(b).toPrecision(3)} m${b.ctx.p.cage_attrib && b.g.findAttrib(b.ctx.p.cage_attrib, ['vertex', 'point', 'prim', 'detail']) ? `, or attribute "${b.ctx.p.cage_attrib}"` : ''}); they use the low surface${missWhere(b)}. Layer "missed" shows where: raise cage there (cage_attrib per part)`);
165
278
  return { P, N, scene, D, miss };
166
279
  });
167
280
  }
168
281
  function defineBake(def) {
169
- defineNode({ category: 'texture', inputs: INPUTS, ...def });
282
+ defineNode({ category: 'texture', inputs: INPUTS, usesNodeName: NAMED_BY_NODE, ...def });
170
283
  }
171
- const BAKE_DOC = 'Input 0 needs non-overlapping uv in 0..1 (uv_unwrap); layer "mask" holds the coverage. ';
284
+ const BAKE_DOC = 'Input 0 needs non-overlapping uv in 0..1 (uv_unwrap); layer "mask" holds the coverage. Bakes that read a high-res mesh on input 1 add layer "missed": 1 where the cage ray found no high surface (stat missed); cage_attrib sets the cage per part and match_name keeps each named piece to the high prims of its name. ';
172
285
  // ---------------------------------------------------------------- ray bakes
173
286
  function rayBake(ctx, mode) {
174
287
  const b = setup(ctx), p = ctx.p;
@@ -200,7 +313,7 @@ const RAY_PARAMS = {
200
313
  bias: { type: 'float', default: 0, min: 0, desc: 'ray start offset along the normal against self-hits (0 = auto: 1e-4 of the bbox diagonal)' },
201
314
  seed: P.seed(),
202
315
  invert: { type: 'bool', default: false, desc: 'write 1 - value' },
203
- cage: cageParam(), cage_normals: cageNormalsParam(),
316
+ ...HIGH_PARAMS(),
204
317
  };
205
318
  defineBake({
206
319
  type: 'bake_ao',
@@ -210,15 +323,15 @@ defineBake({
210
323
  'that hit within max_distance. Combine with bake_curvature for edge wear and dirt masks; pack into ORM with tex_channel.',
211
324
  aliases: ['ao_bake', 'bake_ambient_occlusion', 'bake_occlusion'],
212
325
  params: {
213
- ...BAKE_PARAMS('ao'),
326
+ ...BAKE_PARAMS(),
214
327
  ...RAY_PARAMS,
215
328
  max_distance: { type: 'float', default: 0, min: 0, desc: 'hits farther than this do not occlude (metres; 0 = unlimited)', aliases: ['max_occluder_distance', 'distance', 'radius'] },
216
329
  cone_angle: { type: 'float', default: 0, min: 0, max: 90, desc: 'half-angle of the ray cone in degrees (0 = auto: 90, the hemisphere)', aliases: ['spread', 'angle'] },
217
330
  },
218
- outputs: 'image <name> (default "ao"): C grey 0..1 (1 = open), mask',
331
+ outputs: 'image <name> (default: the node name): C grey 0..1 (1 = open), mask',
219
332
  example: {
220
333
  nodes: {
221
- crate: { type: 'crate' },
334
+ crate: { type: 'topotopo/props/crate' },
222
335
  uv: { type: 'uv_unwrap', in: ['crate'] },
223
336
  ao: { type: 'bake_ao', in: ['uv'], params: { resolution: 512, samples: 24 } },
224
337
  },
@@ -234,12 +347,12 @@ defineBake({
234
347
  '(translucency, subsurface masks).',
235
348
  aliases: ['thickness_bake', 'bake_thick'],
236
349
  params: {
237
- ...BAKE_PARAMS('thickness'),
350
+ ...BAKE_PARAMS(),
238
351
  ...RAY_PARAMS,
239
352
  max_distance: { type: 'float', default: 0, min: 0, desc: 'thickness that maps to 1 (metres; 0 = auto: 25% of the bbox diagonal)', aliases: ['distance', 'radius'] },
240
353
  cone_angle: { type: 'float', default: 0, min: 0, max: 90, desc: 'half-angle of the ray cone in degrees (0 = auto: 30)', aliases: ['spread', 'angle'] },
241
354
  },
242
- outputs: 'image <name> (default "thickness"): C grey 0..1, mask',
355
+ outputs: 'image <name> (default: the node name): C grey 0..1, mask',
243
356
  example: {
244
357
  nodes: {
245
358
  ring: { type: 'torus' },
@@ -258,17 +371,21 @@ defineBake({
258
371
  'edge carries its signed bend (angle / 90°, + convex), and a texel sums the bends of the edge length within `radius`, ' +
259
372
  'weighted (1 - d²/r²)² and normalised so a 90° edge through the texel reads 1. C = 0.5 + 0.5 × strength × value; ' +
260
373
  'layers "convex" and "concave" hold the two sides 0..1. Edges whose faces both turn away from a texel (the far side ' +
261
- 'of a thin plank) are ignored; points are matched by position, so unwelded boxes work. tex_histogram_scan or ' +
262
- 'tex_levels on "convex" make an edge-wear mask.',
374
+ 'of a thin plank) are ignored; points are matched by position, so unwelded boxes work. intersections (on by ' +
375
+ 'default) also finds the creases where separate parts cut into each other, as kitbashed meshes have them (a rivet ' +
376
+ 'or boss merged onto a plate, a box sunk into another): concave by the angle between the two faces, so dirt ' +
377
+ 'gathers there too (stat crossing_edges). tex_histogram_scan or tex_levels on "convex" make an edge-wear mask, ' +
378
+ 'on "concave" a dirt or cavity mask.',
263
379
  aliases: ['curvature_bake', 'bake_edges', 'edge_wear_bake'],
264
380
  params: {
265
- ...BAKE_PARAMS('curvature'),
381
+ ...BAKE_PARAMS(),
266
382
  radius: { type: 'float', default: 0, min: 0, desc: 'how far an edge reaches across the surface (metres; 0 = auto: 1% of the bbox diagonal)', aliases: ['width', 'edge_width', 'distance'] },
267
383
  strength: { type: 'float', default: 1, min: 0, desc: 'contrast multiplier', aliases: ['intensity', 'contrast'] },
268
384
  min_angle: { type: 'float', default: 0.5, min: 0, max: 179, desc: 'edges bending less than this (degrees) are ignored' },
269
- cage: cageParam(), cage_normals: cageNormalsParam(),
385
+ intersections: { type: 'bool', default: true, desc: 'also find the concave creases where separate parts cut into each other (kitbashed meshes)', aliases: ['crossings', 'intersection_creases'] },
386
+ ...HIGH_PARAMS(),
270
387
  },
271
- outputs: 'image <name> (default "curvature"): C grey (0.5 = flat), convex, concave, mask',
388
+ outputs: 'image <name> (default: the node name): C grey (0.5 = flat), convex, concave, mask',
272
389
  example: {
273
390
  nodes: {
274
391
  box: { type: 'box' },
@@ -283,11 +400,15 @@ defineBake({
283
400
  const high = highOf(ctx);
284
401
  const go = (P, N, src) => {
285
402
  const r = p.radius > 0 ? p.radius : 0.01 * b.diag;
286
- const edges = curvatureEdges(src, 1e-6 * (bboxDiagonal(src) || 1), p.min_angle);
403
+ const counts = { shared: 0, crossing: 0, capped: false };
404
+ const edges = curvatureEdges(src, 1e-6 * (bboxDiagonal(src) || 1), p.min_angle, !!p.intersections, counts);
405
+ if (counts.capped)
406
+ ctx.warn(`${ctx.type}: the search for crossing parts stopped early on this large mesh; some creases where parts cut into each other may be missing`);
287
407
  const grid = edgeGrid(edges, r);
288
408
  const args = { radius: r, facing: 0.25, origin: grid.origin, cell: grid.cell, dims: grid.dims };
289
409
  const V = new Float64Array(b.s.n);
290
410
  ctx.stat('edges', edges.count);
411
+ ctx.stat('crossing_edges', counts.crossing);
291
412
  const run = ctx.parallelMap('bake_curvature', {
292
413
  count: b.s.n, args, inputs: { P, N }, results: { V }, shared: { E: edges.E, D: edges.D, K: edges.K, FN: edges.FN, start: grid.start, items: grid.items },
293
414
  });
@@ -318,13 +439,13 @@ defineBake({
318
439
  aliases: ['position_bake', 'bake_world_position'],
319
440
  inputs: [INPUTS[0]],
320
441
  params: {
321
- ...BAKE_PARAMS('position'),
442
+ ...BAKE_PARAMS(),
322
443
  mode: { type: 'enum', options: ['bbox', 'uniform', 'world'], default: 'bbox', desc: 'how positions map to colour', aliases: ['normalize'] },
323
444
  },
324
- outputs: 'image <name> (default "position"): C RGBA, mask',
445
+ outputs: 'image <name> (default: the node name): C RGBA, mask',
325
446
  example: {
326
447
  nodes: {
327
- crate: { type: 'crate' },
448
+ crate: { type: 'topotopo/props/crate' },
328
449
  uv: { type: 'uv_unwrap', in: ['crate'] },
329
450
  pos: { type: 'bake_position', in: ['uv'], params: { resolution: 256 } },
330
451
  },
@@ -351,8 +472,8 @@ defineBake({
351
472
  summary: 'Bake world-space normals into uv space as RGB (0.5 + 0.5 n): the mesh\'s own, or the high-res input\'s along the cage.',
352
473
  description: 'Substance Painter "World Space Normal". ' + BAKE_DOC + 'Without input 1 the mesh\'s interpolated vertex normals (N, else computed with a 60° cusp); with a high-res input its normals where the cage rays hit it. Green-channel masks (tex_channel extract g) give "facing up" for dust and snow.',
353
474
  aliases: ['world_normal_bake', 'bake_worldspace_normal'],
354
- params: { ...BAKE_PARAMS('world_normal'), cage: cageParam(), cage_normals: cageNormalsParam() },
355
- outputs: 'image <name> (default "world_normal"): C RGBA, mask',
475
+ params: { ...BAKE_PARAMS(), ...HIGH_PARAMS() },
476
+ outputs: 'image <name> (default: the node name): C RGBA, mask',
356
477
  example: {
357
478
  nodes: {
358
479
  ball: { type: 'sphere', params: { rows: 8, cols: 16 } },
@@ -382,16 +503,18 @@ defineBake({
382
503
  type: 'bake_normal',
383
504
  summary: 'Bake a tangent-space normal map from a high-res mesh (input 1) onto the uv of the low mesh: OpenGL or DirectX.',
384
505
  description: 'Substance Painter / Marmoset "Normal". ' + BAKE_DOC + 'For each texel a ray from cage above the low surface along -N finds ' +
385
- 'the high-res surface (within cage below); its interpolated normal is written in the low mesh\'s tangent frame (N ' +
386
- 'interpolated, T from the uv per triangle, B by handedness) as 0.5 + 0.5 n. Misses and a missing input 1 give the ' +
387
- 'flat (0.5, 0.5, 1). glTF and Blender read OpenGL; Unreal DirectX (or flip green on import).',
506
+ 'the high-res surface (within cage below); its interpolated normal is written in the low mesh\'s MikkTSpace tangent ' +
507
+ 'frame as 0.5 + 0.5 n: the frame glTF export writes as TANGENT and Blender, Unreal and Unity compute, so the map ' +
508
+ 'decodes the same everywhere (N interpolated and normalised, T the interpolated MikkTSpace tangent made ' +
509
+ 'perpendicular to N, B = w · N × T). Misses and a missing input 1 give the flat (0.5, 0.5, 1). glTF and Blender ' +
510
+ 'read OpenGL; Unreal DirectX (or flip green on import).',
388
511
  aliases: ['normal_bake', 'bake_tangent_normal', 'bake_normal_map'],
389
512
  params: {
390
- ...BAKE_PARAMS('normal'),
391
- cage: cageParam(), cage_normals: cageNormalsParam(),
513
+ ...BAKE_PARAMS(),
514
+ ...HIGH_PARAMS(),
392
515
  format: { type: 'enum', options: ['opengl', 'directx'], default: 'opengl', desc: 'green channel convention', aliases: ['convention'] },
393
516
  },
394
- outputs: 'image <name> (default "normal"): C RGBA, mask',
517
+ outputs: 'image <name> (default: the node name): C RGBA, mask',
395
518
  example: {
396
519
  nodes: {
397
520
  low: { type: 'box', params: { divisions: [2, 2, 2] } },
@@ -415,36 +538,37 @@ defineBake({
415
538
  ctx.warn(`${ctx.type}: no high-res mesh on input 1: the map is flat (0.5, 0.5, 1)`);
416
539
  return finish(b, out, 4, [0.5, 0.5, 1, 1]);
417
540
  }
418
- const tan = triangleTangents(b.g, b.uv, b.tris);
419
541
  const dx = p.format === 'directx' ? -1 : 1;
420
- return andThen(onHigh(b, high), (h) => {
421
- for (let i = 0; i < n; i++) {
422
- if (!(h.D[i] === h.D[i]))
423
- continue;
424
- const t = b.s.tri[i];
425
- const nx = b.N[i * 3], ny = b.N[i * 3 + 1], nz = b.N[i * 3 + 2];
426
- let tx = tan.T[t * 3], ty = tan.T[t * 3 + 1], tz = tan.T[t * 3 + 2];
427
- const d = tx * nx + ty * ny + tz * nz;
428
- tx -= nx * d;
429
- ty -= ny * d;
430
- tz -= nz * d;
431
- const tl = Math.hypot(tx, ty, tz);
432
- if (!(tl > 1e-12))
433
- continue;
434
- tx /= tl;
435
- ty /= tl;
436
- tz /= tl;
437
- const sg = tan.sign[t];
438
- const bx = (ny * tz - nz * ty) * sg, by = (nz * tx - nx * tz) * sg, bz = (nx * ty - ny * tx) * sg;
439
- const hx = h.N[i * 3], hy = h.N[i * 3 + 1], hz = h.N[i * 3 + 2];
440
- const u = hx * tx + hy * ty + hz * tz, v = hx * bx + hy * by + hz * bz, w = hx * nx + hy * ny + hz * nz;
441
- const l = Math.hypot(u, v, w) || 1;
442
- out[i * 4] = 0.5 + (0.5 * u) / l;
443
- out[i * 4 + 1] = 0.5 + (0.5 * v * dx) / l;
444
- out[i * 4 + 2] = 0.5 + (0.5 * w) / l;
542
+ const bakeWith = () => {
543
+ // the same vertex normals (cusp 60°) and uv the exporter writes, so the frame matches the exported TANGENT
544
+ const { tangents, diag } = cornerTangents(b.g, b.tris, resolveVertexNormals(b.g, 60), b.uv);
545
+ if (diag.uvDegenerate || diag.uvNonFinite)
546
+ ctx.warn(`${ctx.type}: ${diag.uvDegenerate + diag.uvNonFinite} of ${diag.triangles} triangles have collapsed or non-finite uvs: no tangent frame there (flat texels)`);
547
+ const T = interpolateCorners(b.s, tangents, 4), f = new Float64Array(9);
548
+ const collapsed = new Uint8Array(diag.triangles);
549
+ for (let t = 0; t < diag.triangles; t++) {
550
+ const u0 = b.uv[b.tris[t * 3] * 2], v0 = b.uv[b.tris[t * 3] * 2 + 1];
551
+ const du1 = b.uv[b.tris[t * 3 + 1] * 2] - u0, dv1 = b.uv[b.tris[t * 3 + 1] * 2 + 1] - v0, du2 = b.uv[b.tris[t * 3 + 2] * 2] - u0, dv2 = b.uv[b.tris[t * 3 + 2] * 2 + 1] - v0;
552
+ if (!(Math.abs(du1 * dv2 - du2 * dv1) > 0))
553
+ collapsed[t] = 1;
445
554
  }
446
- return finish(b, out, 4, [0.5, 0.5, 1, 1]);
447
- });
555
+ return andThen(onHigh(b, high), (h) => {
556
+ for (let i = 0; i < n; i++) {
557
+ if (!(h.D[i] === h.D[i]) || collapsed[b.s.tri[i]])
558
+ continue;
559
+ if (!shadingFrame(b.N[i * 3], b.N[i * 3 + 1], b.N[i * 3 + 2], T[i * 4], T[i * 4 + 1], T[i * 4 + 2], T[i * 4 + 3], f))
560
+ continue;
561
+ const hx = h.N[i * 3], hy = h.N[i * 3 + 1], hz = h.N[i * 3 + 2];
562
+ const u = hx * f[0] + hy * f[1] + hz * f[2], v = hx * f[3] + hy * f[4] + hz * f[5], w = hx * f[6] + hy * f[7] + hz * f[8];
563
+ const l = Math.hypot(u, v, w) || 1;
564
+ out[i * 4] = 0.5 + (0.5 * u) / l;
565
+ out[i * 4 + 1] = 0.5 + (0.5 * v * dx) / l;
566
+ out[i * 4 + 2] = 0.5 + (0.5 * w) / l;
567
+ }
568
+ return finish(b, out, 4, [0.5, 0.5, 1, 1]);
569
+ });
570
+ };
571
+ return mikkTSpaceReady() ? bakeWith() : loadMikkTSpace().then(bakeWith);
448
572
  },
449
573
  });
450
574
  defineBake({
@@ -456,11 +580,11 @@ defineBake({
456
580
  'or use it as displacement.',
457
581
  aliases: ['height_bake', 'bake_displacement', 'displacement_bake'],
458
582
  params: {
459
- ...BAKE_PARAMS('height'),
460
- cage: cageParam(), cage_normals: cageNormalsParam(),
583
+ ...BAKE_PARAMS(),
584
+ ...HIGH_PARAMS(),
461
585
  mode: { type: 'enum', options: ['cage', 'normalize'], default: 'cage', desc: 'how distances map to 0..1', aliases: ['range'] },
462
586
  },
463
- outputs: 'image <name> (default "height"): C grey 0..1, distance (metres), mask',
587
+ outputs: 'image <name> (default: the node name): C grey 0..1, distance (metres), mask',
464
588
  example: {
465
589
  nodes: {
466
590
  low: { type: 'box', params: { divisions: [2, 2, 2] } },
@@ -492,7 +616,7 @@ defineBake({
492
616
  miss++;
493
617
  }
494
618
  if (miss)
495
- ctx.warn(`${ctx.type}: ${miss} of ${n} samples found no high-res surface within the cage (${cage.toPrecision(3)} m)`);
619
+ ctx.warn(`${ctx.type}: ${miss} of ${n} samples found no high-res surface within the cage (${cage.toPrecision(3)} m)${missWhere(b)}; layer "missed" shows where`);
496
620
  const span = hi > lo ? hi - lo : 1;
497
621
  for (let i = 0; i < n; i++) {
498
622
  const d = D[i];
@@ -514,11 +638,11 @@ defineBake({
514
638
  description: 'Substance Painter "ID" (by uv island). ' + BAKE_DOC + 'Islands are triangles connected through corners that share a point and a uv. C is a random colour per island (seed), layer "id" the island number 0..n-1 (the stat islands counts them).',
515
639
  aliases: ['island_id_bake', 'bake_id', 'id_bake', 'bake_uv_islands'],
516
640
  inputs: [INPUTS[0]],
517
- params: { ...BAKE_PARAMS('island_id'), seed: P.seed() },
518
- outputs: 'image <name> (default "island_id"): C RGBA, id (island number), mask',
641
+ params: { ...BAKE_PARAMS(), seed: P.seed() },
642
+ outputs: 'image <name> (default: the node name): C RGBA, id (island number), mask',
519
643
  example: {
520
644
  nodes: {
521
- crate: { type: 'crate' },
645
+ crate: { type: 'topotopo/props/crate' },
522
646
  uv: { type: 'uv_unwrap', in: ['crate'] },
523
647
  ids: { type: 'bake_island_id', in: ['uv'], params: { resolution: 256 } },
524
648
  },
@@ -561,12 +685,12 @@ defineBake({
561
685
  aliases: ['mask_bake', 'bake_group', 'bake_attribute', 'attribute_bake'],
562
686
  inputs: [INPUTS[0]],
563
687
  params: {
564
- ...BAKE_PARAMS('mask'),
688
+ ...BAKE_PARAMS(),
565
689
  mask_group: { type: 'group', default: '', groupClass: 'prim', desc: 'prims that read 1 (e.g. "planks", "@P.y>0.5")', aliases: ['from_group', 'mask_prims'] },
566
690
  attrib: { type: 'string', default: '', desc: 'numeric attribute to bake when mask_group is empty', aliases: ['attribute', 'from_attrib'] },
567
691
  component: { type: 'int', default: 0, min: -1, max: 15, desc: 'vector component of attrib (-1 = luminance of the first three)' },
568
692
  },
569
- outputs: 'image <name> (default "mask"): C grey, mask',
693
+ outputs: 'image <name> (default: the node name): C grey, mask',
570
694
  example: {
571
695
  nodes: {
572
696
  box: { type: 'box' },
@@ -607,11 +731,11 @@ defineBake({
607
731
  aliases: ['vertex_color_bake', 'bake_color', 'bake_cd', 'color_bake'],
608
732
  inputs: [INPUTS[0]],
609
733
  params: {
610
- ...BAKE_PARAMS('vertex_color'),
734
+ ...BAKE_PARAMS(),
611
735
  attrib: { type: 'string', default: 'Cd', desc: 'colour attribute', aliases: ['attribute', 'color_attrib'] },
612
736
  alpha_attrib: { type: 'string', default: 'Alpha', desc: 'alpha attribute (missing = 1)' },
613
737
  },
614
- outputs: 'image <name> (default "vertex_color"): C RGBA, mask',
738
+ outputs: 'image <name> (default: the node name): C RGBA, mask',
615
739
  example: {
616
740
  nodes: {
617
741
  ball: { type: 'sphere', params: { rows: 12, cols: 24 } },
@@ -0,0 +1 @@
1
+ export {};