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
@@ -4,29 +4,44 @@
4
4
  // Opt-in overlays answer specific questions (help topic "render"): crisp 1 px edge overlays
5
5
  // (all / hard / boundary / edge groups), a section view and a persp clip plane with filled caps,
6
6
  // vector glyphs (N, up, orient, v), point/prim numbers, group/piece/attribute labels, legends,
7
- // flat/matcap/AO shading and a self-describing annotation footer. With none of them set the
8
- // output is exactly what it always was.
7
+ // flat/matcap/AO shading and a self-describing annotation footer. A heightfield drawn at fewer samples than it has
8
+ // (above heightfieldRes quads per side) gets a second footer line saying so.
9
+ // Framing: every 3D view fits the bounds (the 8 bbox corners projected into the view, with a small margin), so
10
+ // nothing is cropped; zoom is relative to that fit. Views can be W×H (size [w, h]), have a transparent background,
11
+ // a reference image (underlay) behind them, and scene empties (sockets) drawn as labelled dots.
9
12
  import { readMaterials, primMaterials } from '../core/materials.js';
10
13
  import { Geometry } from '../core/geometry.js';
11
14
  import { buildEdges, connectivity, primNormals, resolveVertexNormals, triangulate } from '../core/topology.js';
12
15
  import { halfEdges } from '../core/halfedge.js';
13
16
  import { drawText, textWidth } from './font.js';
14
17
  import { readUV, polygonSides, uvSeamPairs } from '../core/uv.js';
18
+ import { chartKinds, chartRoots, primLods } from '../core/uvreport.js';
15
19
  import { expandForRender } from '../core/packed.js';
16
20
  import { qNormalize, qRotate } from '../core/math.js';
17
21
  import { PLANE_UV, sliceTriangles, windingGrid } from './section.js';
22
+ import { openEdgesText, partsOf, primWhere, solidReport } from '../core/report.js';
18
23
  import { LabelPlacer, ambientOcclusion, dotOv, drawLabel, drawLegend, lineOv } from './overlay.js';
19
- import { withHeightfieldPreview } from '../core/heightfield.js';
24
+ import { heightfieldPreviewStep, isHeightfield, layerStats, withHeightfieldPreview } from '../core/heightfield.js';
20
25
  import { withSdfGridPreview } from '../core/sdfgrid.js';
21
- import { drawTextureView, textureSampler } from './texview.js';
26
+ import { drawTextureView, textureSampler, withMaterialPreview } from './texview.js';
27
+ import { EMPTY_ATTRIB } from '../core/scene.js';
22
28
  import { pbrScene, preparePBR } from './pbr.js';
29
+ import { decodePNG } from '../io/png-decode.js';
30
+ /**
31
+ * Named three-quarter views: persp views at these azimuths (degrees; 0 = the camera on +Z looking at the front, 90 =
32
+ * on +X looking at the right side), at the elevation of the persp view.
33
+ */
34
+ export const NAMED_VIEWS = { front_left: -45, front_right: 45, back_left: -135, back_right: 135 };
35
+ export const UNDERLAY_FITS = ['bounds', 'width', 'height', 'view'];
36
+ /** The most quads per side a heightfield is drawn with by default (RenderOptions.heightfieldRes). */
37
+ export const HEIGHTFIELD_RENDER_RES = 256;
23
38
  /** View presets accepted by parseViews (MCP render "views", CLI --views). */
24
39
  export const VIEW_PRESETS = {
25
40
  quad: ['persp', 'front', 'right', 'top'],
26
41
  quad_uv: ['persp', 'uv'],
27
42
  turntable: ['persp@0', 'persp@90', 'persp@180', 'persp@270'],
28
43
  };
29
- export const BASE_VIEWS = ['persp', 'front', 'back', 'left', 'right', 'top', 'bottom', 'uv', 'section', 'texture'];
44
+ export const BASE_VIEWS = ['persp', 'front', 'back', 'left', 'right', 'top', 'bottom', ...Object.keys(NAMED_VIEWS), 'uv', 'section', 'texture'];
30
45
  /** Views from a preset name, a comma/space separated list or an array (presets expand inside lists). Throws on unknown names. */
31
46
  export function parseViews(spec) {
32
47
  if (spec == null)
@@ -50,7 +65,15 @@ const AXES = ['x', 'y', 'z'];
50
65
  const WIRE_MODES = ['none', 'all', 'hard', 'boundary', 'groups'];
51
66
  const GLYPH_MODES = ['none', 'N', 'up', 'orient', 'v', 'custom'];
52
67
  const SHADING_MODES = ['default', 'flat', 'matcap', 'ao', 'pbr'];
53
- const CUT_C = [0.93, 0.4, 0.3], CUT_FLIP_C = [0.25, 0.38, 0.9];
68
+ /** The automatic wireframe draws polygon edges up to this many polygons. */
69
+ export const AUTO_WIRE_MAX = 6000;
70
+ const CUT_C = [0.93, 0.4, 0.3], CUT_FLIP_C = [0.25, 0.38, 0.9], SOCKET_C = [0.35, 1, 0.72];
71
+ /** Margins of the fit: the bounds fill this fraction of a view's width and height (room for the label and gizmo). */
72
+ const FIT_W = 0.9, FIT_H = 0.84;
73
+ /** Whether a zoom above 1 may crop the bounds: only when the person framed the view (look_at or ortho_scale). */
74
+ const zoomFramed = (o) => !!o.target || !!(o.orthoScale && o.orthoScale > 0);
75
+ /** The zoom a view uses: within 0.05..20, held at 1 above 1 unless the view is framed by look_at / ortho_scale. */
76
+ const zoomOf = (o) => { const z = Math.max(0.05, Math.min(20, o.zoom ?? 1)); return z > 1 && !zoomFramed(o) ? 1 : z; };
54
77
  const ORTHO = {
55
78
  front: { f: [0, 0, -1], r: [1, 0, 0], u: [0, 1, 0], label: 'FRONT X> Y^' },
56
79
  back: { f: [0, 0, 1], r: [-1, 0, 0], u: [0, 1, 0], label: 'BACK -X> Y^' },
@@ -59,6 +82,9 @@ const ORTHO = {
59
82
  top: { f: [0, -1, 0], r: [1, 0, 0], u: [0, 0, -1], label: 'TOP X> -Z^' },
60
83
  bottom: { f: [0, 1, 0], r: [1, 0, 0], u: [0, 0, 1], label: 'BOTTOM X> Z^' },
61
84
  };
85
+ /** persp, persp@<azimuth> and the named three-quarter views are perspective views. */
86
+ export const isPerspView = (v) => v.startsWith('persp') || NAMED_VIEWS[v] !== undefined;
87
+ const glAxisC = [[0.95, 0.3, 0.3], [0.4, 0.9, 0.4], [0.4, 0.6, 1]];
62
88
  const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
63
89
  const norm3 = (a) => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };
64
90
  const cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
@@ -77,15 +103,72 @@ function hashStr(s) { let h = 2166136261; for (let i = 0; i < s.length; i++)
77
103
  h = Math.imul(h ^ s.charCodeAt(i), 16777619); return h >>> 0; }
78
104
  /** Section views look along the section axis from its + side (same framing as right / top / front). */
79
105
  const SECTION_CAM = [ORTHO.right, ORTHO.top, ORTHO.front];
80
- function makeCam(name, bmin, bmax, W, H, o, kind = name, azDeg, secAxis = 1) {
106
+ /**
107
+ * The points a view fits: the geometry's points (evenly subsampled above 200,000), or the bbox corners when there are
108
+ * none (volumes only). Fitting the points rather than the bbox keeps rotated content (a square turned 20°) as large as
109
+ * it can be; nothing outside the bbox exists, so the fit never crops.
110
+ */
111
+ function fitPoints(g, bmin, bmax) {
112
+ const np = g.numPoints, n = Math.min(np, 200_000);
113
+ // volumes drawn in 3D (SDFs as their bounds) count with their bbox corners
114
+ const corners = !np || g.volumes.some((v) => v.kind !== 'image') ? 8 : 0;
115
+ const out = new Float64Array((n + corners) * 3);
116
+ for (let k = 0; k < n; k++) {
117
+ const i = Math.floor((k * np) / n) * 3;
118
+ out[k * 3] = g.P[i];
119
+ out[k * 3 + 1] = g.P[i + 1];
120
+ out[k * 3 + 2] = g.P[i + 2];
121
+ }
122
+ for (let k = 0; k < corners; k++) {
123
+ const o = (n + k) * 3;
124
+ out[o] = k & 1 ? bmax[0] : bmin[0];
125
+ out[o + 1] = k & 2 ? bmax[1] : bmin[1];
126
+ out[o + 2] = k & 4 ? bmax[2] : bmin[2];
127
+ }
128
+ return out;
129
+ }
130
+ /**
131
+ * The camera of a view. Persp views stand where they always stood (the bounding sphere seen through a 32° lens, so the
132
+ * perspective does not change with the framing) and then fit the image of the geometry into the view: the focal length
133
+ * and the image centre are chosen so the projected points (fitPoints) fill FIT_W × FIT_H of it, centred (or around the
134
+ * look-at point when target is set). Ortho and section views fit the same way. zoom scales the fit.
135
+ */
136
+ function makeCam(name, bmin, bmax, W, H, o, kind = name, azDeg, secAxis, pts) {
81
137
  const c = o.target ?? [(bmin[0] + bmax[0]) / 2, (bmin[1] + bmax[1]) / 2, (bmin[2] + bmax[2]) / 2];
82
- const ext = [bmax[0] - bmin[0], bmax[1] - bmin[1], bmax[2] - bmin[2]];
83
- const R = Math.max(1e-6, Math.hypot(ext[0], ext[1], ext[2]) / 2);
84
- const zoom = Math.max(0.05, Math.min(20, o.zoom ?? 1));
138
+ let R = 1e-6;
139
+ for (let k = 0; k < 8; k++)
140
+ R = Math.max(R, Math.hypot((k & 1 ? bmax[0] : bmin[0]) - c[0], (k & 2 ? bmax[1] : bmin[1]) - c[1], (k & 4 ? bmax[2] : bmin[2]) - c[2]));
141
+ const zoom = zoomOf(o), centred = !!o.target;
142
+ /** half extents and centre of the fit from the image coordinates of the points (x along r, y along u; persp: over depth) */
143
+ const fit = (r, u, f, from) => {
144
+ let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
145
+ for (let i = 0; i < pts.length; i += 3) {
146
+ const dx = pts[i] - from[0], dy = pts[i + 1] - from[1], dz = pts[i + 2] - from[2];
147
+ const z = f ? Math.max(1e-9, dx * f[0] + dy * f[1] + dz * f[2]) : 1;
148
+ const x = (dx * r[0] + dy * r[1] + dz * r[2]) / z, y = (dx * u[0] + dy * u[1] + dz * u[2]) / z;
149
+ if (!Number.isFinite(x) || !Number.isFinite(y))
150
+ continue;
151
+ if (x < x0)
152
+ x0 = x;
153
+ if (x > x1)
154
+ x1 = x;
155
+ if (y < y0)
156
+ y0 = y;
157
+ if (y > y1)
158
+ y1 = y;
159
+ }
160
+ if (!(x1 >= x0)) {
161
+ x0 = x1 = y0 = y1 = 0;
162
+ }
163
+ // around the look-at point (the view looks from `from` through it, so its image is x = y = 0), else centred
164
+ return centred
165
+ ? { hx: Math.max(Math.abs(x0), Math.abs(x1)), hy: Math.max(Math.abs(y0), Math.abs(y1)), cx: 0, cy: 0 }
166
+ : { hx: (x1 - x0) / 2, hy: (y1 - y0) / 2, cx: (x0 + x1) / 2, cy: (y0 + y1) / 2 };
167
+ };
85
168
  if (kind === 'persp') {
86
169
  const az = ((azDeg ?? o.azimuth ?? 35) * Math.PI) / 180, el = ((o.elevation ?? 25) * Math.PI) / 180;
87
170
  const fov = (32 * Math.PI) / 180;
88
- const dist = (R / Math.sin(fov / 2)) * 1.05 / zoom;
171
+ const dist = (R / Math.sin(fov / 2)) * 1.05;
89
172
  const dir = [Math.cos(el) * Math.sin(az), Math.sin(el), Math.cos(el) * Math.cos(az)];
90
173
  const eye = [c[0] + dir[0] * dist, c[1] + dir[1] * dist, c[2] + dir[2] * dist];
91
174
  const f = norm3([c[0] - eye[0], c[1] - eye[1], c[2] - eye[2]]);
@@ -94,13 +177,19 @@ function makeCam(name, bmin, bmax, W, H, o, kind = name, azDeg, secAxis = 1) {
94
177
  r = [1, 0, 0];
95
178
  r = norm3(r);
96
179
  const u = cross3(r, f);
97
- return { name, persp: true, eye, f, r, u, c, focal: Math.min(W, H) / 2 / Math.tan(fov / 2), scale: 1 };
180
+ const q = fit(r, u, f, eye);
181
+ const lens = Math.min(W, H) / 2 / Math.tan(fov / 2);
182
+ const focal = Math.min(q.hx > 1e-9 ? (W / 2) * FIT_W / q.hx : Infinity, q.hy > 1e-9 ? (H / 2) * FIT_H / q.hy : Infinity, lens * 50) * zoom;
183
+ const fl = Number.isFinite(focal) && focal > 0 ? focal : lens * zoom;
184
+ return { name, persp: true, eye, f, r, u, c, focal: fl, scale: 1, ox: W / 2 - q.cx * fl, oy: H / 2 + q.cy * fl };
98
185
  }
99
186
  const v = kind === 'section' ? SECTION_CAM[secAxis] : ORTHO[name];
100
- const halfW = Math.abs(v.r[0]) * ext[0] / 2 + Math.abs(v.r[1]) * ext[1] / 2 + Math.abs(v.r[2]) * ext[2] / 2;
101
- const halfH = Math.abs(v.u[0]) * ext[0] / 2 + Math.abs(v.u[1]) * ext[1] / 2 + Math.abs(v.u[2]) * ext[2] / 2;
102
- const s = (o.orthoScale && o.orthoScale > 0 ? W / o.orthoScale : Math.min((W / 2) * 0.86 / Math.max(halfW, 1e-6), (H / 2) * 0.82 / Math.max(halfH, 1e-6))) * zoom;
103
- return { name, persp: false, eye: c, f: v.f, r: v.r, u: v.u, c, focal: 0, scale: s };
187
+ if (o.orthoScale && o.orthoScale > 0)
188
+ return { name, persp: false, eye: c, f: v.f, r: v.r, u: v.u, c, focal: 0, scale: (W / o.orthoScale) * zoom, ox: W / 2, oy: H / 2 };
189
+ const q = fit(v.r, v.u, null, c);
190
+ const s0 = Math.min((W / 2) * FIT_W / Math.max(q.hx, 1e-6), (H / 2) * FIT_H / Math.max(q.hy, 1e-6));
191
+ const s = (Number.isFinite(s0) && s0 > 0 ? s0 : 1) * zoom;
192
+ return { name, persp: false, eye: c, f: v.f, r: v.r, u: v.u, c, focal: 0, scale: s, ox: W / 2 - q.cx * s, oy: H / 2 + q.cy * s };
104
193
  }
105
194
  /**
106
195
  * Render with the asynchronous preparation shading 'pbr' needs (MikkTSpace, texture files through readFile, usually
@@ -109,8 +198,43 @@ function makeCam(name, bmin, bmax, W, H, o, kind = name, azDeg, secAxis = 1) {
109
198
  export async function renderGeometryAsync(g, o = {}, readFile) {
110
199
  if (o.shading === 'pbr' && !o.pbrTextures)
111
200
  o = { ...o, pbrTextures: await preparePBR(g, readFile) };
201
+ if (o.underlay && !o.underlayImage)
202
+ o = { ...o, underlayImage: await readUnderlay(o.underlay, readFile) };
112
203
  return renderGeometry(g, o);
113
204
  }
205
+ /** A reference image for the underlay option: a PNG read through readFile (errors say what to pass instead). */
206
+ export async function readUnderlay(p, readFile) {
207
+ if (!readFile)
208
+ throw new Error(`render: underlay "${p}": no file access in this render`);
209
+ let bytes;
210
+ try {
211
+ bytes = await readFile(p);
212
+ }
213
+ catch (e) {
214
+ throw new Error(`render: underlay "${p}" cannot be read (${e?.message ?? e})`);
215
+ }
216
+ if (!(bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47))
217
+ throw new Error(`render: underlay "${p}" is not a PNG (only PNG reference images are read; convert it to PNG)`);
218
+ let img;
219
+ try {
220
+ img = decodePNG(bytes);
221
+ }
222
+ catch (e) {
223
+ throw new Error(`render: underlay "${p}" is not a readable PNG (${e?.message ?? e})`);
224
+ }
225
+ // 8-bit RGBA whatever the PNG holds (grey, grey + alpha, RGB, RGBA; 16-bit samples scaled down)
226
+ const n = img.width * img.height, ch = img.channels, rgba = new Uint8Array(n * 4);
227
+ const max = img.maxValue;
228
+ for (let i = 0; i < n; i++) {
229
+ const s = (k) => Math.round((img.data[i * ch + k] / max) * 255);
230
+ const grey = ch <= 2;
231
+ rgba[i * 4] = s(0);
232
+ rgba[i * 4 + 1] = grey ? s(0) : s(1);
233
+ rgba[i * 4 + 2] = grey ? s(0) : s(2);
234
+ rgba[i * 4 + 3] = ch === 2 ? s(1) : ch === 4 ? s(3) : 255;
235
+ }
236
+ return { width: img.width, height: img.height, rgba };
237
+ }
114
238
  /** Render the geometry; returns RGBA pixels. Shading 'pbr' needs renderGeometryAsync first (or MikkTSpace loaded). */
115
239
  export function renderGeometry(g, o = {}) {
116
240
  // packed instances are drawn expanded (within a triangle budget), spline curves as polylines, see core/packed.ts
@@ -121,17 +245,49 @@ export function renderGeometry(g, o = {}) {
121
245
  r.notes.unshift(...x.notes);
122
246
  return r;
123
247
  }
124
- g = withHeightfieldPreview(g); // heightfields draw as their (decimated) grid mesh with layers as attributes
248
+ // heightfields draw as their grid mesh with layers as attributes, decimated to heightfieldRes quads per side
249
+ const hfRes = o.heightfieldRes ?? HEIGHTFIELD_RENDER_RES;
250
+ if (!(hfRes >= 1))
251
+ throw new Error(`heightfieldRes must be at least 1 (quads per side) or Infinity (every sample), got ${hfRes}`);
252
+ const hfShown = g.volumes.filter(isHeightfield).map((v) => ({ v, ...heightfieldPreviewStep(v, hfRes) })).filter((x) => x.step > 1);
253
+ const hfLayer = o.heightfieldLayer ?? 'mask', hfNotes = [];
254
+ let hfLegend = '';
255
+ if (o.heightfieldLayer !== undefined && hfLayer !== 'none') {
256
+ // a layer the person chose: said in the legend (with its range when it is not a 0..1 layer), or why it is not shown
257
+ if (!g.volumes.some(isHeightfield))
258
+ hfNotes.push('heightfield_layer: the geometry has no heightfield');
259
+ for (const v of g.volumes.filter(isHeightfield)) {
260
+ const a = v.layers.get(hfLayer), st = layerStats(a);
261
+ if (!st)
262
+ hfNotes.push(`heightfield_layer: heightfield "${v.name}" has no layer "${hfLayer}" (layers: ${[...v.layers.keys()].join(', ') || 'none'})`);
263
+ else if (!hfLegend)
264
+ hfLegend = `HEIGHTFIELD ${hfLayer.toUpperCase()} IN RED${hfLayer === 'mask' || (st.min >= 0 && st.max <= 1) ? '' : ` [${fmtNum(st.min)} .. ${fmtNum(st.max)}]`}`;
265
+ }
266
+ }
267
+ g = withHeightfieldPreview(g, hfRes, hfLayer);
125
268
  g = withSdfGridPreview(g); // sampled SDFs draw through the existing level-set mesher at preview resolution
269
+ // a material output: spheres wearing its materials, with their base colour maps unless textures is set
270
+ const matPreview = withMaterialPreview(g), materialOnly = matPreview !== g;
271
+ if (materialOnly) {
272
+ g = matPreview;
273
+ if (o.textures === undefined)
274
+ o = { ...o, textures: true };
275
+ }
126
276
  const vlist = typeof o.views === 'string' ? parseViews(o.views) : o.views;
127
277
  // images and nothing else to draw in 3D: the texture view (render/texview.ts)
128
278
  const onlyImages = !g.numPoints && g.volumes.length > 0 && g.volumes.every((v) => v.kind === 'image');
129
- const views = vlist?.length ? vlist : onlyImages ? ['texture'] : ['persp', 'front', 'right', 'top'];
279
+ const views = vlist?.length ? vlist : onlyImages ? ['texture'] : materialOnly ? ['persp'] : ['persp', 'front', 'right', 'top'];
130
280
  const ui = Math.max(1, Math.min(4, Math.round(o.scale ?? 1)));
131
- const tile = Math.max(128, Math.min(1600, Math.round(o.size ?? (views.length === 1 ? 800 : 512)))) * ui;
281
+ // tile size: n (square) or [width, height] pixels per view, before scale
282
+ const sz0 = o.size ?? (views.length === 1 ? 800 : 512);
283
+ const [sw, sh] = Array.isArray(sz0) ? sz0 : [sz0, sz0];
284
+ if (!(Number.isFinite(sw) && Number.isFinite(sh)))
285
+ throw new Error(`render: size expects pixels per view, n or [width, height], got ${JSON.stringify(o.size)}`);
286
+ const TW = Math.max(128, Math.min(1600, Math.round(sw))) * ui, TH = Math.max(128, Math.min(1600, Math.round(sh))) * ui;
132
287
  let ss = Math.max(1, Math.min(3, Math.round(o.ssaa ?? 2)));
133
288
  if (ui > 1)
134
- ss = Math.max(1, Math.min(ss, Math.floor(4096 / tile)));
289
+ ss = Math.max(1, Math.min(ss, Math.floor(4096 / Math.max(TW, TH))));
290
+ const transparent = !!o.transparent;
135
291
  const cols = views.length === 1 ? 1 : views.length <= 4 ? 2 : 3;
136
292
  const rows = Math.ceil(views.length / cols);
137
293
  // opt-in overlay options (validated: they arrive from MCP/CLI as free strings)
@@ -145,6 +301,16 @@ export function renderGeometry(g, o = {}) {
145
301
  if (o.shading && !SHADING_MODES.includes(o.shading))
146
302
  throw new Error(`unknown shading "${o.shading}" (use ${SHADING_MODES.join(', ')})`);
147
303
  const shadingMode = o.shading && o.shading !== 'default' ? o.shading : null;
304
+ // reference image behind the 3D views
305
+ const ulFit = o.underlayFit ?? 'bounds';
306
+ if (!UNDERLAY_FITS.includes(ulFit))
307
+ throw new Error(`unknown underlay_fit "${o.underlayFit}" (use ${UNDERLAY_FITS.join(', ')})`);
308
+ const ulOpacity = Math.max(0, Math.min(1, o.underlayOpacity ?? 0.5));
309
+ if (o.underlayOpacity !== undefined && !Number.isFinite(o.underlayOpacity))
310
+ throw new Error(`render: underlay_opacity expects 0..1, got ${o.underlayOpacity}`);
311
+ if (o.underlay && !o.underlayImage)
312
+ throw new Error('render: underlay needs the image read first (renderGeometryAsync reads it)');
313
+ const ul = o.underlayImage && !o.transparent ? o.underlayImage : null;
148
314
  const hasSection = views.includes('section');
149
315
  const secK = AXES.indexOf(o.sectionAxis ?? 'y');
150
316
  if (secK < 0)
@@ -152,38 +318,34 @@ export function renderGeometry(g, o = {}) {
152
318
  const clipK = o.clip ? AXES.indexOf(o.clip.axis) : -1;
153
319
  if (o.clip && clipK < 0)
154
320
  throw new Error(`unknown clip axis "${o.clip.axis}" (use x, y or z)`);
155
- const annotate = o.annotate ?? !!(wireMode || o.pointNumbers || o.primNumbers || hasSection || clipK >= 0 || glyphMode || o.labelGroups || o.labelPieces || o.labelAttrib || o.legend || shadingMode);
156
- const footerH = 22 * ui + (annotate ? 16 * ui : 0);
321
+ const annotate = o.annotate ?? !!(wireMode || o.pointNumbers || o.primNumbers || hasSection || clipK >= 0 || glyphMode || o.labelGroups || o.labelPieces || o.labelAttrib || o.legend || shadingMode || o.sockets);
322
+ // a heightfield drawn at fewer samples than it has: said on the second footer line (unless annotate is false)
323
+ const hfLine = hfShown.map((x) => `HEIGHTFIELD ${hfShown.length > 1 ? x.v.name.toUpperCase() + ' ' : ''}SHOWN AT ${x.shown[0]}×${x.shown[1]} OF ${x.v.width}×${x.v.height}`).join(' · ');
324
+ const hfFooter = !!hfLine && o.annotate !== false;
325
+ const footerH = 22 * ui + (annotate || hfFooter ? 16 * ui : 0);
157
326
  const viewLabels = o.viewLabels ?? true;
158
- const outW = cols * tile + (cols - 1) * 2, outH = rows * tile + (rows - 1) * 2 + footerH;
159
- const notes = [];
327
+ const outW = cols * TW + (cols - 1) * 2, outH = rows * TH + (rows - 1) * 2 + footerH;
328
+ const notes = hfShown.map((x) => `heightfield "${x.v.name}" shown at ${x.shown[0]}×${x.shown[1]} of ${x.v.width}×${x.v.height} samples (1 in ${x.step} per side); heightfield_res "full" (CLI --heightfield-res full) draws every sample`);
329
+ notes.push(...hfNotes);
330
+ if ((o.zoom ?? 1) > 1 && !zoomFramed(o))
331
+ notes.push(`zoom ${fmtNum(o.zoom)}: held at 1, the fit (the views fit the bounds; a zoom above 1 would crop them): add look_at [x, y, z] to zoom into a point`);
332
+ if (o.underlayImage && o.transparent)
333
+ notes.push('underlay: not drawn on a transparent background');
160
334
  // --- scene data shared by all views
161
335
  const Pd = g.P, np = g.numPoints;
162
336
  const bb = g.bbox(true);
163
337
  const bmin = bb.min, bmax = bb.max;
338
+ const fitPts = fitPoints(g, bmin, bmax);
164
339
  const { tris, triPrim } = triangulate(g);
165
340
  const nTri = tris.length / 3;
166
341
  if (nTri > 4e6)
167
342
  notes.push(`large mesh (${nTri} triangles) — render may be slow`);
168
343
  const vnorm = nTri ? resolveVertexNormals(g, 50) : new Float64Array(0);
169
344
  const nrmOf = (v, out) => { out[0] = vnorm[v * 3]; out[1] = vnorm[v * 3 + 1]; out[2] = vnorm[v * 3 + 2]; };
170
- // back faces are only suspicious on closed pieces; open sheets/cards are shaded two-sided
171
- const conn = nTri ? connectivity(g) : null;
172
- const closedPiece = new Uint8Array(conn ? conn.count : 0).fill(1);
173
- let openEdges = 0, nonManifoldEdges = 0;
174
- if (conn && nTri) {
175
- const E0 = buildEdges(g);
176
- for (let e = 0; e < E0.a.length; e++)
177
- if (E0.count[e] !== 2)
178
- closedPiece[conn.primClass[E0.firstPrim[e]]] = 0;
179
- if (annotate)
180
- for (let e = 0; e < E0.a.length; e++) {
181
- if (E0.count[e] === 1 && g.primClosed[E0.firstPrim[e]])
182
- openEdges++;
183
- else if (E0.count[e] > 2)
184
- nonManifoldEdges++;
185
- }
186
- }
345
+ // back faces are only suspicious on closed pieces (closed once the vertex splits of uv / normal seams are welded, as
346
+ // an imported mesh has them); open sheets / cards are shaded two-sided. The pieces are the welded ones (core/report.ts).
347
+ const solid = nTri ? solidReport(g, { tri: { tris, triPrim }, flips: false }) : null;
348
+ const pieceOf = solid ? solid.primPiece : new Int32Array(0), closedPiece = solid ? solid.pieceClosed : new Uint8Array(0);
187
349
  // --- colours per vertex (Cd, colour-by, highlight)
188
350
  const vprim = g.vertexPrims();
189
351
  const BASE = [0.78, 0.74, 0.68];
@@ -278,6 +440,12 @@ export function renderGeometry(g, o = {}) {
278
440
  return c[0] === 1 && c[1] === 1 && c[2] === 1 ? m.baseColor : c;
279
441
  };
280
442
  }
443
+ // the heightfield layer a person chose to see in red
444
+ if (hfLegend) {
445
+ legend = hfLegend;
446
+ if (o.legend)
447
+ cbLegend = [{ color: [0.9, 0.12, 0.1], text: hfLegend.replace(' IN RED', '').toLowerCase() }];
448
+ }
281
449
  }
282
450
  const hl = o.highlight;
283
451
  if (hl) {
@@ -297,19 +465,32 @@ export function renderGeometry(g, o = {}) {
297
465
  cbLegend.push({ color: HC, text: `${hl.label ?? 'GROUP'} (${n} ${hl.cls}s)` });
298
466
  }
299
467
  const colorByActive = !!o.colorBy || !!hl;
468
+ const pbrTint = colorByActive && o.shading === 'pbr';
300
469
  // isolated points + polylines
301
470
  const used = new Uint8Array(np);
302
471
  for (let v = 0; v < g.numVertices; v++)
303
472
  used[g.vertexPoint[v]] = 1;
473
+ // scene empties (sockets: isolated points marked scene_empty 1): drawn by the sockets overlay when asked, and not as
474
+ // loose points in pbr (beauty) renders
475
+ const emptyA = g.points.get(EMPTY_ATTRIB);
476
+ const isEmpty = emptyA && emptyA.type === 'int' && emptyA.size === 1 ? (i) => !used[i] && emptyA.data[i] !== 0 : () => false;
477
+ const hideEmpties = !!o.sockets || o.shading === 'pbr';
304
478
  let isolated = 0;
305
479
  for (let i = 0; i < np; i++)
306
- if (!used[i])
480
+ if (!used[i] && !(hideEmpties && isEmpty(i)))
307
481
  isolated++;
308
482
  const showAllPts = o.points === true;
309
483
  const drawPts = showAllPts || (o.points !== false && isolated > 0);
310
484
  const closedPrims = g.primClosed.reduce((s, c) => s + c, 0);
311
485
  // the classic auto wireframe; the new edge overlays (wireMode) replace it
312
- const wire = !wireMode && o.wireframe !== 'none' && (o.wireframe === true || (o.wireframe !== false && closedPrims > 0 && closedPrims <= 6000));
486
+ // auto (the default): on below 6,000 polygons, but never over a material look (pbr shading or texture maps), where the
487
+ // edges read as a defect of the material; when auto draws them the footer says so
488
+ const autoWire = o.wireframe === undefined || o.wireframe === 'auto';
489
+ const wire = !wireMode && o.wireframe !== 'none' && (o.wireframe === true
490
+ || (autoWire && closedPrims > 0 && closedPrims <= AUTO_WIRE_MAX && o.shading !== 'pbr' && !o.textures));
491
+ const autoEdgesShown = wire && autoWire && views.some((v) => v !== 'uv' && v !== 'texture');
492
+ if (autoEdgesShown)
493
+ notes.push('wireframe auto: polygon edges drawn (wireframe false hides them)');
313
494
  const edges = wire ? buildEdges(g) : null;
314
495
  // triangle-dominant meshes (boolean/remesh output): hide edges between coplanar faces so the
315
496
  // wireframe shows shape, not triangulation slivers
@@ -340,6 +521,8 @@ export function renderGeometry(g, o = {}) {
340
521
  out[i + 1] = 0.12;
341
522
  out[i + 2] = 0.13;
342
523
  }
524
+ // transparent: alpha per output pixel (0 between the views; the footer is opaque)
525
+ const outA = transparent ? new Float32Array(outW * outH) : null;
343
526
  // --- uv data (uv view, checker shading)
344
527
  const materialNames = primMaterials(g), materialTable = readMaterials(g);
345
528
  const hasMasks = Object.values(materialTable).some((m) => m.alphaMode === 'MASK');
@@ -398,8 +581,9 @@ export function renderGeometry(g, o = {}) {
398
581
  if (!Number.isFinite(ev))
399
582
  throw new Error(`render: exposure expects a number of stops, got ${o.exposure}`);
400
583
  pbrS = pbrScene(g, tris, triPrim, o.pbrTextures, ev, notes);
401
- if (o.colorBy || o.highlight || o.checker)
402
- notes.push('pbr: color_by, highlight and checker are not shown under shading "pbr"');
584
+ // color_by and highlight tint the shaded material (their colour, lit by its brightness); the checker is not shown
585
+ if (o.checker)
586
+ notes.push('pbr: checker is not shown under shading "pbr" (color_by and highlight tint the material)');
403
587
  ovDesc.push(`SHADING PBR STUDIO EV ${fmtNum(ev)} (${pbrS.summary.toUpperCase()})`);
404
588
  }
405
589
  else if (shadingMode)
@@ -414,7 +598,7 @@ export function renderGeometry(g, o = {}) {
414
598
  const key = `${k}:${c}`;
415
599
  let s = slices.get(key);
416
600
  if (!s) {
417
- s = nTri && conn ? sliceTriangles(Pd, g.vertexPoint, tris, (t) => conn.primClass[triPrim[t]], k, c) : { n: 0, seg: new Float64Array(0), piece: new Int32Array(0) };
601
+ s = nTri && solid ? sliceTriangles(Pd, g.vertexPoint, tris, (t) => pieceOf[triPrim[t]], k, c) : { n: 0, seg: new Float64Array(0), piece: new Int32Array(0) };
418
602
  slices.set(key, s);
419
603
  }
420
604
  return s;
@@ -627,7 +811,7 @@ export function renderGeometry(g, o = {}) {
627
811
  }
628
812
  }
629
813
  if (o.labelPieces && np) {
630
- const cn = conn ?? connectivity(g);
814
+ const cn = connectivity(g);
631
815
  const n = Math.min(cn.count, maxLabels);
632
816
  const idx = Array.from({ length: n }, () => []);
633
817
  for (let i = 0; i < np; i++)
@@ -655,8 +839,32 @@ export function renderGeometry(g, o = {}) {
655
839
  notes.push(`point numbers: first ${maxLabels} of ${np} points (max_labels)`);
656
840
  ovDesc.push('POINT NUMBERS');
657
841
  }
842
+ // sockets: a dot, the name and short orient axes per scene empty, drawn through surfaces (sockets often sit inside)
843
+ const sockets = [];
844
+ if (o.sockets) {
845
+ const ori = g.points.get('orient'), nameA = g.points.get('name');
846
+ const L = Math.max(1e-6, diag * 0.045);
847
+ for (let i = 0; i < np; i++) {
848
+ if (!isEmpty(i))
849
+ continue;
850
+ const q = ori && ori.type === 'float' && ori.size === 4 ? qNormalize(ori.get(i)) : [0, 0, 0, 1];
851
+ const axes = [];
852
+ for (let ax = 0; ax < 3; ax++) {
853
+ const d = qRotate(q, [ax === 0 ? 1 : 0, ax === 1 ? 1 : 0, ax === 2 ? 1 : 0]);
854
+ axes.push(d[0] * L, d[1] * L, d[2] * L);
855
+ }
856
+ sockets.push({ p: i, axes });
857
+ const nm = nameA && nameA.type === 'string' ? String(nameA.get(i) ?? '') : '';
858
+ wl.push({ x: Pd[i * 3], y: Pd[i * 3 + 1], z: Pd[i * 3 + 2], text: nm || `empty ${i}`, c: SOCKET_C, kind: 0 });
859
+ }
860
+ if (!sockets.length)
861
+ notes.push('sockets: the geometry has no scene empties (scene_null)');
862
+ else
863
+ legendItems.push({ color: SOCKET_C, text: `SOCKETS (${sockets.length})` });
864
+ ovDesc.push(`SOCKETS ${sockets.length}`);
865
+ }
658
866
  const bigLabels = wl.filter((l) => l.kind === 0).length;
659
- const nameFs = tile >= 400 * ui && bigLabels <= 40 ? 2 * ui : ui;
867
+ const nameFs = Math.min(TW, TH) >= 400 * ui && bigLabels <= 40 ? 2 * ui : ui;
660
868
  // glyphs: world segments (start = anchor) with a base and a tip colour
661
869
  const glSeg = [], glCol = [];
662
870
  const glPal = [];
@@ -755,37 +963,72 @@ export function renderGeometry(g, o = {}) {
755
963
  }
756
964
  if (o.legend && !cbLegend.length)
757
965
  notes.push('legend: nothing to explain (use with color_by or highlight)');
758
- const hasOverlay = oeA.length > 0 || dotPts.length > 0 || wl.length > 0 || glCol.length > 0 || legendItems.length > 0 || hasSection || clipK >= 0;
966
+ const hasOverlay = sockets.length > 0 || oeA.length > 0 || dotPts.length > 0 || wl.length > 0 || glCol.length > 0 || legendItems.length > 0 || hasSection || clipK >= 0;
759
967
  const legendView = views.findIndex((v) => v !== 'uv' && v !== 'texture');
760
- const backView = annotate ? views.findIndex((v) => v !== 'uv' && v !== 'texture' && v !== 'section' && !(clipK >= 0 && v.startsWith('persp'))) : -1;
968
+ const backView = annotate ? views.findIndex((v) => v !== 'uv' && v !== 'texture' && v !== 'section' && !(clipK >= 0 && isPerspView(v))) : -1;
761
969
  let backPix = 0, shadedPix = 0;
970
+ /** the prims whose back faces show (blue), by pixel count, in the view that counts them */
971
+ const backPrims = new Map();
762
972
  let labelsSkipped = 0;
763
973
  views.forEach((vname, vi) => {
764
- const W = tile * ss, H = tile * ss;
974
+ const W = TW * ss, H = TH * ss;
765
975
  const col = new Float32Array(W * H * 3);
766
- const tx = (vi % cols) * (tile + 2), ty = Math.floor(vi / cols) * (tile + 2);
767
- // downsample a finished tile into the output, draw the output-resolution overlays (post), label it
768
- const blit = (label, post) => {
976
+ const tx = (vi % cols) * (TW + 2), ty = Math.floor(vi / cols) * (TH + 2);
977
+ // downsample a finished tile into the output, draw the output-resolution overlays (post), label it. Transparent
978
+ // 3D views (zb given): alpha = the covered fraction of each pixel's samples and the colour of those samples alone;
979
+ // whatever post and the label draw is opaque
980
+ const blit = (label, post, zb) => {
769
981
  const inv = 1 / (ss * ss);
770
- for (let y = 0; y < tile; y++)
771
- for (let x = 0; x < tile; x++) {
772
- let r = 0, gg = 0, b = 0;
982
+ const cover = !!(outA && zb);
983
+ for (let y = 0; y < TH; y++)
984
+ for (let x = 0; x < TW; x++) {
985
+ let r = 0, gg = 0, b = 0, n = 0, cr = 0, cg = 0, cb = 0;
773
986
  for (let j = 0; j < ss; j++)
774
987
  for (let i = 0; i < ss; i++) {
775
- const k = ((y * ss + j) * W + x * ss + i) * 3;
988
+ const zi = (y * ss + j) * W + x * ss + i, k = zi * 3;
776
989
  r += col[k];
777
990
  gg += col[k + 1];
778
991
  b += col[k + 2];
992
+ if (cover && zb[zi] > -Infinity) {
993
+ n++;
994
+ cr += col[k];
995
+ cg += col[k + 1];
996
+ cb += col[k + 2];
997
+ }
779
998
  }
780
- const k = ((ty + y) * outW + tx + x) * 3;
781
- out[k] = r * inv;
782
- out[k + 1] = gg * inv;
783
- out[k + 2] = b * inv;
999
+ const o1 = (ty + y) * outW + tx + x, k = o1 * 3;
1000
+ if (!cover) {
1001
+ out[k] = r * inv;
1002
+ out[k + 1] = gg * inv;
1003
+ out[k + 2] = b * inv;
1004
+ if (outA)
1005
+ outA[o1] = 1;
1006
+ continue;
1007
+ }
1008
+ if (n) {
1009
+ out[k] = cr / n;
1010
+ out[k + 1] = cg / n;
1011
+ out[k + 2] = cb / n;
1012
+ }
1013
+ else {
1014
+ out[k] = r * inv;
1015
+ out[k + 1] = gg * inv;
1016
+ out[k + 2] = b * inv;
1017
+ }
1018
+ outA[o1] = n * inv;
784
1019
  }
1020
+ const snap = cover ? out.slice(ty * outW * 3, (ty + TH) * outW * 3) : null;
785
1021
  if (post)
786
1022
  post();
787
1023
  if (viewLabels)
788
1024
  drawText(out, outW, outH, tx + 6 * ui, ty + 6 * ui, label, [1, 1, 1], 2 * ui);
1025
+ if (snap)
1026
+ for (let y = 0; y < TH; y++)
1027
+ for (let x = 0; x < TW; x++) {
1028
+ const o1 = (ty + y) * outW + tx + x, k = o1 * 3, q = (y * outW + tx + x) * 3;
1029
+ if (out[k] !== snap[q] || out[k + 1] !== snap[q + 1] || out[k + 2] !== snap[q + 2])
1030
+ outA[o1] = 1;
1031
+ }
789
1032
  };
790
1033
  if (vname === 'uv') {
791
1034
  blit(drawUVView(g, col, W, H, ss * ui, uvd, tris, colorOf, colorByActive, notes));
@@ -795,8 +1038,8 @@ export function renderGeometry(g, o = {}) {
795
1038
  blit(drawTextureView(g, col, W, H, ss * ui, o.texture ?? '', notes));
796
1039
  return;
797
1040
  }
798
- const kind = vname.startsWith('persp') ? 'persp' : vname;
799
- const azDeg = vname.startsWith('persp@') ? Number(vname.slice(6)) : undefined;
1041
+ const kind = isPerspView(vname) ? 'persp' : vname;
1042
+ const azDeg = vname.startsWith('persp@') ? Number(vname.slice(6)) : NAMED_VIEWS[vname];
800
1043
  const zb = new Float32Array(W * H).fill(-Infinity);
801
1044
  for (let y = 0; y < H; y++) {
802
1045
  const t = y / H;
@@ -808,7 +1051,7 @@ export function renderGeometry(g, o = {}) {
808
1051
  col[i + 2] = b;
809
1052
  }
810
1053
  }
811
- const cam = makeCam(vname, bmin, bmax, W, H, o, kind, azDeg, secK);
1054
+ const cam = makeCam(vname, bmin, bmax, W, H, o, kind, azDeg, secK, fitPts);
812
1055
  if (pbrS)
813
1056
  pbrS.camera(cam.r, cam.u, cam.f);
814
1057
  // project points
@@ -819,14 +1062,14 @@ export function renderGeometry(g, o = {}) {
819
1062
  const Y = dx * cam.u[0] + dy * cam.u[1] + dz * cam.u[2];
820
1063
  const Z = dx * cam.f[0] + dy * cam.f[1] + dz * cam.f[2];
821
1064
  if (cam.persp) {
822
- outp[0] = W / 2 + (X / Z) * cam.focal;
823
- outp[1] = H / 2 - (Y / Z) * cam.focal;
1065
+ outp[0] = cam.ox + (X / Z) * cam.focal;
1066
+ outp[1] = cam.oy - (Y / Z) * cam.focal;
824
1067
  outp[2] = Z > 1e-6 ? 1 / Z : -Infinity;
825
1068
  outp[3] = Z;
826
1069
  }
827
1070
  else {
828
- outp[0] = W / 2 + X * cam.scale;
829
- outp[1] = H / 2 - Y * cam.scale;
1071
+ outp[0] = cam.ox + X * cam.scale;
1072
+ outp[1] = cam.oy - Y * cam.scale;
830
1073
  outp[2] = -Z;
831
1074
  outp[3] = Z;
832
1075
  }
@@ -872,8 +1115,29 @@ export function renderGeometry(g, o = {}) {
872
1115
  return !(cam.persp && (q0[3] < 1e-4 || q1[3] < 1e-4));
873
1116
  };
874
1117
  const keptPt = (i) => !clipOn || (Pd[i * 3 + cK] - cC) * cS <= 0;
1118
+ // reference image behind the view, placed by underlayFit against the rectangle the bounds cover in this view
1119
+ if (ul && kind !== 'section') {
1120
+ let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
1121
+ const q = [0, 0, 0, 0];
1122
+ for (let k = 0; k < 8; k++) {
1123
+ project(k & 1 ? bmax[0] : bmin[0], k & 2 ? bmax[1] : bmin[1], k & 4 ? bmax[2] : bmin[2], q);
1124
+ x0 = Math.min(x0, q[0]);
1125
+ x1 = Math.max(x1, q[0]);
1126
+ y0 = Math.min(y0, q[1]);
1127
+ y1 = Math.max(y1, q[1]);
1128
+ }
1129
+ if (ulFit === 'view' || !(x1 > x0) || !(y1 > y0)) {
1130
+ x0 = 0;
1131
+ x1 = W;
1132
+ y0 = 0;
1133
+ y1 = H;
1134
+ }
1135
+ const bw = x1 - x0, bh = y1 - y0, ia = ul.width / ul.height;
1136
+ const w = ulFit === 'width' ? bw : ulFit === 'height' ? bh * ia : Math.min(bw, bh * ia), h = w / ia;
1137
+ drawUnderlay(col, W, H, ul, (x0 + x1) / 2 - w / 2, (y0 + y1) / 2 - h / 2, w, h, ulOpacity);
1138
+ }
875
1139
  // ground grid (persp only), drawn first without depth
876
- if (kind === 'persp' && o.grid !== false) {
1140
+ if (kind === 'persp' && (o.grid ?? !transparent)) {
877
1141
  const step = Math.pow(10, Math.floor(Math.log10(sceneR * 0.5)));
878
1142
  const ext = Math.ceil((sceneR * 1.6) / step) * step;
879
1143
  const cx = Math.round(cam.c[0] / step) * step, czz = Math.round(cam.c[2] / step) * step;
@@ -902,7 +1166,7 @@ export function renderGeometry(g, o = {}) {
902
1166
  const tn = [0, 0, 0];
903
1167
  const chk = [0, 0, 0];
904
1168
  const pbrUV0 = [0, 0], pbrUV1 = [0, 0], pbrUV2 = [0, 0], pbrV = [0, 0, 0], pbrRGB = [0, 0, 0];
905
- const bf = vi === backView ? new Uint8Array(W * H) : null;
1169
+ const bf = vi === backView ? new Uint8Array(W * H) : null, bfPrim = bf ? new Int32Array(W * H) : null;
906
1170
  for (let t = 0; t < nTri; t++) {
907
1171
  const va = tris[t * 3], vb = tris[t * 3 + 1], vc = tris[t * 3 + 2];
908
1172
  const a = g.vertexPoint[va], b = g.vertexPoint[vb], c = g.vertexPoint[vc];
@@ -934,7 +1198,7 @@ export function renderGeometry(g, o = {}) {
934
1198
  const back = dot3(fnw, toCam) < 0;
935
1199
  const material = materialNames ? materialTable[materialNames[triPrim[t]]] : undefined;
936
1200
  const alphaMask = material?.alphaMode === 'MASK';
937
- const suspicious = back && !material?.doubleSided && conn !== null && closedPiece[conn.primClass[triPrim[t]]] === 1;
1201
+ const suspicious = back && !material?.doubleSided && solid !== null && closedPiece[pieceOf[triPrim[t]]] === 1;
938
1202
  if (flatN) {
939
1203
  const p = triPrim[t] * 3;
940
1204
  na[0] = nb[0] = nc[0] = flatN[p];
@@ -953,6 +1217,7 @@ export function renderGeometry(g, o = {}) {
953
1217
  toCamSpace(nb, cb);
954
1218
  toCamSpace(nc, cc);
955
1219
  let colA = colorOf(va), colB = colorOf(vb), colC = colorOf(vc);
1220
+ const tA = colA, tB = colB, tC = colC; // before the back-face tint: the pbr colour tint
956
1221
  if (suspicious) {
957
1222
  const tint = (q) => [q[0] * 0.35 + 0.12, q[1] * 0.35 + 0.22, q[2] * 0.35 + 0.6];
958
1223
  colA = tint(colA);
@@ -1012,8 +1277,10 @@ export function renderGeometry(g, o = {}) {
1012
1277
  continue;
1013
1278
  }
1014
1279
  zb[zi] = depth;
1015
- if (bf)
1280
+ if (bf) {
1016
1281
  bf[zi] = suspicious ? 1 : 0;
1282
+ bfPrim[zi] = triPrim[t];
1283
+ }
1017
1284
  if (pbrS) {
1018
1285
  let dudx = 0, dvdx = 0, dudy = 0, dvdy = 0;
1019
1286
  if (pbrS.hasUV) {
@@ -1048,6 +1315,12 @@ export function renderGeometry(g, o = {}) {
1048
1315
  }
1049
1316
  pbrS.shade(t, w0, w1, w2, dudx, dvdx, dudy, dvdy, pbrV, back, pbrRGB);
1050
1317
  const ci = zi * 3;
1318
+ if (pbrTint) {
1319
+ // the attribute colour lit by the material's brightness, over a third of the material itself
1320
+ const lum = Math.min(1.2, 0.12 + 1.5 * (0.2126 * pbrRGB[0] + 0.7152 * pbrRGB[1] + 0.0722 * pbrRGB[2]));
1321
+ for (let k = 0; k < 3; k++)
1322
+ pbrRGB[k] = pbrRGB[k] * 0.35 + (tA[k] * w0 + tB[k] * w1 + tC[k] * w2) * lum * 0.65;
1323
+ }
1051
1324
  if (suspicious) {
1052
1325
  pbrRGB[0] = pbrRGB[0] * 0.35 + 0.12;
1053
1326
  pbrRGB[1] = pbrRGB[1] * 0.35 + 0.22;
@@ -1127,12 +1400,14 @@ export function renderGeometry(g, o = {}) {
1127
1400
  if (zb[i] > -Infinity) {
1128
1401
  shadedPix++;
1129
1402
  backPix += bf[i];
1403
+ if (bf[i])
1404
+ backPrims.set(bfPrim[i], (backPrims.get(bfPrim[i]) ?? 0) + 1);
1130
1405
  }
1131
1406
  // screen-space ambient occlusion on the shaded surface (before lines and points, which stay crisp)
1132
1407
  if (shadingMode === 'ao' && nTri) {
1133
- const T = tile, lin = new Float32Array(T * T);
1134
- for (let y = 0; y < T; y++)
1135
- for (let x = 0; x < T; x++) {
1408
+ const lin = new Float32Array(TW * TH);
1409
+ for (let y = 0; y < TH; y++)
1410
+ for (let x = 0; x < TW; x++) {
1136
1411
  let m = -Infinity;
1137
1412
  for (let j = 0; j < ss; j++)
1138
1413
  for (let i = 0; i < ss; i++) {
@@ -1140,15 +1415,15 @@ export function renderGeometry(g, o = {}) {
1140
1415
  if (z > m)
1141
1416
  m = z;
1142
1417
  }
1143
- lin[y * T + x] = m === -Infinity ? Infinity : cam.persp ? 1 / m : -m;
1418
+ lin[y * TW + x] = m === -Infinity ? Infinity : cam.persp ? 1 / m : -m;
1144
1419
  }
1145
- const aoF = ambientOcclusion(lin, T, T, (o.aoRadius ?? 10) * ui, cam.persp ? (z) => (z * ss) / cam.focal : () => ss / cam.scale);
1420
+ const aoF = ambientOcclusion(lin, TW, TH, (o.aoRadius ?? 10) * ui, cam.persp ? (z) => (z * ss) / cam.focal : () => ss / cam.scale);
1146
1421
  for (let y = 0; y < H; y++)
1147
1422
  for (let x = 0; x < W; x++) {
1148
1423
  const zi = y * W + x;
1149
1424
  if (zb[zi] === -Infinity)
1150
1425
  continue;
1151
- const f = aoF[Math.floor(y / ss) * T + Math.floor(x / ss)];
1426
+ const f = aoF[Math.floor(y / ss) * TW + Math.floor(x / ss)];
1152
1427
  col[zi * 3] *= f;
1153
1428
  col[zi * 3 + 1] *= f;
1154
1429
  col[zi * 3 + 2] *= f;
@@ -1195,6 +1470,8 @@ export function renderGeometry(g, o = {}) {
1195
1470
  for (let i = 0; i < np; i++) {
1196
1471
  if (!showAllPts && used[i])
1197
1472
  continue;
1473
+ if (hideEmpties && isEmpty(i))
1474
+ continue;
1198
1475
  if (cam.persp && vz[i] < 1e-4)
1199
1476
  continue;
1200
1477
  if (clipOn && !keptPt(i))
@@ -1303,9 +1580,9 @@ export function renderGeometry(g, o = {}) {
1303
1580
  const grid = windingGrid(seg2, m, gx, gy);
1304
1581
  const e = cam.eye, f = cam.f, r = cam.r, u = cam.u;
1305
1582
  for (let py = 0; py < H; py++) {
1306
- const dy = (H / 2 - (py + 0.5)) / cam.focal;
1583
+ const dy = (cam.oy - (py + 0.5)) / cam.focal;
1307
1584
  for (let px = 0; px < W; px++) {
1308
- const dx = (px + 0.5 - W / 2) / cam.focal;
1585
+ const dx = (px + 0.5 - cam.ox) / cam.focal;
1309
1586
  const dk = f[cK] + r[cK] * dx + u[cK] * dy;
1310
1587
  const t = (cC - e[cK]) / dk;
1311
1588
  if (!(t > 1e-6))
@@ -1328,7 +1605,7 @@ export function renderGeometry(g, o = {}) {
1328
1605
  }
1329
1606
  }
1330
1607
  // axis gizmo
1331
- if (kind === 'persp' && viewLabels) {
1608
+ if (kind === 'persp' && viewLabels && !transparent) {
1332
1609
  const ox = 34 * ss * ui, oy = H - 34 * ss * ui, len = 22 * ss * ui;
1333
1610
  for (const [ax, colr, lab] of [[[1, 0, 0], [0.95, 0.3, 0.3], 'X'], [[0, 1, 0], [0.4, 0.9, 0.4], 'Y'], [[0, 0, 1], [0.4, 0.6, 1], 'Z']]) {
1334
1611
  const ex = ox + dot3(ax, cam.r) * len, ey = oy - dot3(ax, cam.u) * len;
@@ -1336,15 +1613,15 @@ export function renderGeometry(g, o = {}) {
1336
1613
  drawText(col, W, H, Math.round(ex + 2 * ss * ui), Math.round(ey - 4 * ss * ui), lab, colr, ss * ui, true);
1337
1614
  }
1338
1615
  }
1339
- const viewLabel = (kind === 'persp' ? `PERSP AZ ${Math.round(azDeg ?? o.azimuth ?? 35)} EL ${Math.round(o.elevation ?? 25)}`
1616
+ const viewLabel = (kind === 'persp' ? `${NAMED_VIEWS[vname] !== undefined ? vname.toUpperCase().replace('_', ' ') : 'PERSP'} AZ ${Math.round(azDeg ?? o.azimuth ?? 35)} EL ${Math.round(o.elevation ?? 25)}`
1340
1617
  : kind === 'section' ? `SECTION ${planeDesc(secK, secC)} ${SECTION_CAM[secK].label.split(' ')[1]}`
1341
1618
  : ORTHO[vname].label) + (checkerOn ? ' CHECKER' : '') + (clipOn && kind === 'persp' ? ` CLIP ${planeDesc(cK, cC)}` : '');
1342
1619
  // output-resolution overlays: crisp 1 px lines, glyphs, cut outlines, labels, legend
1343
1620
  const post = !hasOverlay ? undefined : () => {
1344
- const T = tile, inv = 1 / ss;
1345
- const zt = new Float32Array(T * T); // nearest depth per output pixel
1346
- for (let y = 0; y < T; y++)
1347
- for (let x = 0; x < T; x++) {
1621
+ const inv = 1 / ss;
1622
+ const zt = new Float32Array(TW * TH); // nearest depth per output pixel
1623
+ for (let y = 0; y < TH; y++)
1624
+ for (let x = 0; x < TW; x++) {
1348
1625
  let m = -Infinity;
1349
1626
  for (let j = 0; j < ss; j++)
1350
1627
  for (let i = 0; i < ss; i++) {
@@ -1352,17 +1629,17 @@ export function renderGeometry(g, o = {}) {
1352
1629
  if (z > m)
1353
1630
  m = z;
1354
1631
  }
1355
- zt[y * T + x] = m;
1632
+ zt[y * TW + x] = m;
1356
1633
  }
1357
- const tb = new Float32Array(T * T * 3);
1358
- for (let y = 0; y < T; y++)
1359
- tb.set(out.subarray(((ty + y) * outW + tx) * 3, ((ty + y) * outW + tx + T) * 3), y * T * 3);
1634
+ const tb = new Float32Array(TW * TH * 3);
1635
+ for (let y = 0; y < TH; y++)
1636
+ tb.set(out.subarray(((ty + y) * outW + tx) * 3, ((ty + y) * outW + tx + TW) * 3), y * TW * 3);
1360
1637
  const dtol = cam.persp ? { abs: 1e-7, rel: 0.006 } : { abs: sceneR * 0.006, rel: 0 };
1361
1638
  const hidden = (z, x, y) => {
1362
1639
  const xi = Math.floor(x), yi = Math.floor(y);
1363
- if (xi < 0 || yi < 0 || xi >= T || yi >= T)
1640
+ if (xi < 0 || yi < 0 || xi >= TW || yi >= TH)
1364
1641
  return true;
1365
- return z < zt[yi * T + xi] - 2 * (dtol.abs + Math.abs(z) * dtol.rel);
1642
+ return z < zt[yi * TW + xi] - 2 * (dtol.abs + Math.abs(z) * dtol.rel);
1366
1643
  };
1367
1644
  const dim = kind === 'section' ? 0.45 : 1;
1368
1645
  // edge overlays (wireframe modes, group borders)
@@ -1373,12 +1650,12 @@ export function renderGeometry(g, o = {}) {
1373
1650
  const st = styles[oeS[e]];
1374
1651
  if (clipOn) {
1375
1652
  if (clipProj(Pd[a * 3], Pd[a * 3 + 1], Pd[a * 3 + 2], Pd[b * 3], Pd[b * 3 + 1], Pd[b * 3 + 2]))
1376
- lineOv(tb, T, T, q0[0] * inv, q0[1] * inv, q0[2], q1[0] * inv, q1[1] * inv, q1[2], st.c, st.a * dim, ui, zt, dtol);
1653
+ lineOv(tb, TW, TH, q0[0] * inv, q0[1] * inv, q0[2], q1[0] * inv, q1[1] * inv, q1[2], st.c, st.a * dim, ui, zt, dtol);
1377
1654
  continue;
1378
1655
  }
1379
1656
  if (cam.persp && (vz[a] < 1e-4 || vz[b] < 1e-4))
1380
1657
  continue;
1381
- lineOv(tb, T, T, sx[a] * inv, sy[a] * inv, cz[a], sx[b] * inv, sy[b] * inv, cz[b], st.c, st.a * dim, ui, zt, dtol);
1658
+ lineOv(tb, TW, TH, sx[a] * inv, sy[a] * inv, cz[a], sx[b] * inv, sy[b] * inv, cz[b], st.c, st.a * dim, ui, zt, dtol);
1382
1659
  }
1383
1660
  // point-group members and glyphs (not in the section view, where they would float over the cut)
1384
1661
  const flat2d = kind === 'section';
@@ -1386,7 +1663,7 @@ export function renderGeometry(g, o = {}) {
1386
1663
  const i = dotPts[k];
1387
1664
  if (!keptPt(i) || (cam.persp && vz[i] < 1e-4) || hidden(cz[i], sx[i] * inv, sy[i] * inv))
1388
1665
  continue;
1389
- dotOv(tb, T, T, sx[i] * inv, sy[i] * inv, 2 * ui, styles[dotSt[k]].c);
1666
+ dotOv(tb, TW, TH, sx[i] * inv, sy[i] * inv, 2 * ui, styles[dotSt[k]].c);
1390
1667
  }
1391
1668
  // glyphs: anchored on visible surface points, parts behind the surface dimmed, brighter tip
1392
1669
  const m3 = [0, 0, 0, 0];
@@ -1400,9 +1677,23 @@ export function renderGeometry(g, o = {}) {
1400
1677
  const pal = glPal[glCol[k]];
1401
1678
  const mx = x0 + (x1 - x0) * 0.62, my = y0 + (y1 - y0) * 0.62, mz = z0 + (z1 - z0) * 0.62;
1402
1679
  if (clipProj(x0, y0, z0, mx, my, mz))
1403
- lineOv(tb, T, T, q0[0] * inv, q0[1] * inv, q0[2], q1[0] * inv, q1[1] * inv, q1[2], pal[0], 0.95 * dim, ui, zt, dtol, 0.3 * dim);
1680
+ lineOv(tb, TW, TH, q0[0] * inv, q0[1] * inv, q0[2], q1[0] * inv, q1[1] * inv, q1[2], pal[0], 0.95 * dim, ui, zt, dtol, 0.3 * dim);
1404
1681
  if (clipProj(mx, my, mz, x1, y1, z1))
1405
- lineOv(tb, T, T, q0[0] * inv, q0[1] * inv, q0[2], q1[0] * inv, q1[1] * inv, q1[2], pal[1], dim, ui, zt, dtol, 0.35 * dim);
1682
+ lineOv(tb, TW, TH, q0[0] * inv, q0[1] * inv, q0[2], q1[0] * inv, q1[1] * inv, q1[2], pal[1], dim, ui, zt, dtol, 0.35 * dim);
1683
+ }
1684
+ // sockets: orient axes and a dot, through surfaces (dimmer where hidden)
1685
+ for (const sk of (flat2d ? [] : sockets)) {
1686
+ const i = sk.p;
1687
+ if (!keptPt(i) || (cam.persp && vz[i] < 1e-4))
1688
+ continue;
1689
+ const x = sx[i] * inv, y = sy[i] * inv, hid = hidden(cz[i], x, y);
1690
+ for (let ax = 0; ax < 3; ax++) {
1691
+ project(Pd[i * 3] + sk.axes[ax * 3], Pd[i * 3 + 1] + sk.axes[ax * 3 + 1], Pd[i * 3 + 2] + sk.axes[ax * 3 + 2], m3);
1692
+ if (cam.persp && m3[3] < 1e-4)
1693
+ continue;
1694
+ lineOv(tb, TW, TH, x, y, 0, m3[0] * inv, m3[1] * inv, 0, glAxisC[ax], hid ? 0.55 : 1, ui, null, dtol);
1695
+ }
1696
+ dotOv(tb, TW, TH, x, y, 5 * ui, hid ? [SOCKET_C[0] * 0.6, SOCKET_C[1] * 0.6, SOCKET_C[2] * 0.6] : SOCKET_C);
1406
1697
  }
1407
1698
  // cut outlines: solid pieces dark, open sheets (no cap) yellow
1408
1699
  if (cut)
@@ -1413,16 +1704,16 @@ export function renderGeometry(g, o = {}) {
1413
1704
  if (cam.persp && (q0[3] < 1e-4 || q1[3] < 1e-4))
1414
1705
  continue;
1415
1706
  const solid = closedPiece[cut.piece[s]] === 1;
1416
- lineOv(tb, T, T, q0[0] * inv, q0[1] * inv, 0, q1[0] * inv, q1[1] * inv, 0, solid ? [0.1, 0.02, 0.02] : [1, 0.85, 0.3], solid ? 0.9 : 1, ui, null, dtol);
1707
+ lineOv(tb, TW, TH, q0[0] * inv, q0[1] * inv, 0, q1[0] * inv, q1[1] * inv, 0, solid ? [0.1, 0.02, 0.02] : [1, 0.85, 0.3], solid ? 0.9 : 1, ui, null, dtol);
1417
1708
  }
1418
1709
  // legend, then labels placed around it and around the view label / axis gizmo
1419
- const pl = new LabelPlacer(T, T);
1710
+ const pl = new LabelPlacer(TW, TH);
1420
1711
  const vlRight = 6 * ui + textWidth(viewLabel, 2 * ui) + 4 * ui;
1421
1712
  pl.reserve(0, 0, vlRight, 22 * ui);
1422
1713
  if (kind === 'persp')
1423
- pl.reserve(0, T - 64 * ui, 64 * ui, T);
1714
+ pl.reserve(0, TH - 64 * ui, 64 * ui, TH);
1424
1715
  if (vi === legendView && legendItems.length)
1425
- drawLegend(tb, T, T, legendItems, T >= 700 * ui ? 2 * ui : ui, pl, 6 * ui, [vlRight, 22 * ui]);
1716
+ drawLegend(tb, TW, TH, legendItems, Math.min(TW, TH) >= 700 * ui ? 2 * ui : ui, pl, 6 * ui, [vlRight, 22 * ui]);
1426
1717
  if (kind !== 'section') {
1427
1718
  const m4 = [0, 0, 0, 0];
1428
1719
  const clipped = (x, y, z) => clipOn && ((cK === 0 ? x : cK === 1 ? y : z) - cC) * cS > 0;
@@ -1463,22 +1754,23 @@ export function renderGeometry(g, o = {}) {
1463
1754
  if (lb.kind === 1 && hidden(m4[2], ax, ay))
1464
1755
  continue;
1465
1756
  }
1466
- if (ax < 0 || ay < 0 || ax >= T || ay >= T)
1757
+ if (ax < 0 || ay < 0 || ax >= TW || ay >= TH)
1467
1758
  continue;
1468
- drawLabel(tb, T, T, pl, ax, ay, lb.text, lb.c, lb.kind === 0 ? nameFs : ui);
1759
+ drawLabel(tb, TW, TH, pl, ax, ay, lb.text, lb.c, lb.kind === 0 ? nameFs : ui);
1469
1760
  }
1470
1761
  if (vi === legendView)
1471
1762
  labelsSkipped = pl.skipped;
1472
1763
  }
1473
- for (let y = 0; y < T; y++)
1474
- out.set(tb.subarray(y * T * 3, (y + 1) * T * 3), ((ty + y) * outW + tx) * 3);
1764
+ for (let y = 0; y < TH; y++)
1765
+ out.set(tb.subarray(y * TW * 3, (y + 1) * TW * 3), ((ty + y) * outW + tx) * 3);
1475
1766
  };
1476
- blit(viewLabel, post);
1767
+ blit(viewLabel, post, zb);
1477
1768
  });
1478
1769
  // footer
1479
1770
  const fy = outH - footerH + 4 * ui;
1480
1771
  const ext = [bmax[0] - bmin[0], bmax[1] - bmin[1], bmax[2] - bmin[2]];
1481
- const stats = `${np} PTS ${g.numPrims} PRIMS BBOX ${fmtNum(ext[0])}×${fmtNum(ext[1])}×${fmtNum(ext[2])} CENTER ${fmtNum((bmin[0] + bmax[0]) / 2)},${fmtNum((bmin[1] + bmax[1]) / 2)},${fmtNum((bmin[2] + bmax[2]) / 2)}`;
1772
+ // EDGES AUTO early on the line, so a long title cannot push it off the image
1773
+ const stats = `${np} PTS ${g.numPrims} PRIMS${autoEdgesShown ? ' EDGES AUTO' : ''} BBOX ${fmtNum(ext[0])}×${fmtNum(ext[1])}×${fmtNum(ext[2])} CENTER ${fmtNum((bmin[0] + bmax[0]) / 2)},${fmtNum((bmin[1] + bmax[1]) / 2)},${fmtNum((bmin[2] + bmax[2]) / 2)}`;
1482
1774
  drawText(out, outW, outH, 6 * ui, fy, (o.title ? o.title.toUpperCase() + ' ' : '') + stats, [0.85, 0.85, 0.85], 2 * ui, false);
1483
1775
  if (legend) {
1484
1776
  const lx = outW - textWidth(legend, 2 * ui) - 8 * ui;
@@ -1488,27 +1780,41 @@ export function renderGeometry(g, o = {}) {
1488
1780
  }
1489
1781
  if (labelsSkipped)
1490
1782
  notes.push(`labels: ${labelsSkipped} not drawn to avoid overlaps (raise size, zoom in, or lower max_labels)`);
1491
- if (annotate) {
1492
- // second line: active overlays, then up to three geometry notes
1783
+ if (annotate || hfFooter) {
1784
+ // second line: a decimated heightfield, then the active overlays and up to three geometry notes
1493
1785
  const gn = [];
1494
- if (np) {
1495
- const pieces = conn ? conn.count : connectivity(g).count;
1496
- gn.push(`${pieces} PIECE${pieces === 1 ? '' : 'S'}`);
1497
- }
1498
- if (closedPrims)
1499
- gn.push(openEdges ? `${openEdges} OPEN EDGES` : nonManifoldEdges ? `${nonManifoldEdges} NON-MANIFOLD EDGES` : 'NO OPEN EDGES');
1500
- const capAll = capCells + capFlipCells;
1501
- const bp = shadedPix ? (backPix / shadedPix) * 100 : 0;
1502
- if (capFlipCells && capAll) {
1503
- const pct = (capFlipCells / capAll) * 100;
1504
- gn.push(`CUT ${pct.toFixed(1)}% INSIDE-OUT`);
1505
- notes.push(`section: ${pct.toFixed(1)}% of the cut area is inside-out (blue): flipped normals`);
1786
+ if (annotate) {
1787
+ if (np) {
1788
+ const pieces = solid ? solid.pieces : connectivity(g).count;
1789
+ gn.push(`${pieces} PIECE${pieces === 1 ? '' : 'S'}`);
1790
+ }
1791
+ if (closedPrims && solid) {
1792
+ // open edges after welding seam splits (an imported mesh's uv / normal seams are not holes), with where
1793
+ const welded = solid.seamEdges ? ' (SEAM SPLITS WELDED)' : '';
1794
+ gn.push(solid.openEdges ? `${solid.openEdges} OPEN EDGES${welded}` : solid.nonManifoldEdges ? `${solid.nonManifoldEdges} NON-MANIFOLD EDGES` : `NO OPEN EDGES${welded}`);
1795
+ if (solid.openEdges)
1796
+ notes.push(`${openEdgesText(solid)}, first ${solid.firstOpen}`);
1797
+ if (solid.nonManifoldEdges)
1798
+ notes.push(`${solid.nonManifoldEdges} non-manifold edge(s), first ${solid.firstNonManifold}`);
1799
+ }
1800
+ const capAll = capCells + capFlipCells;
1801
+ const bp = shadedPix ? (backPix / shadedPix) * 100 : 0;
1802
+ if (capFlipCells && capAll) {
1803
+ const pct = (capFlipCells / capAll) * 100;
1804
+ gn.push(`CUT ${pct.toFixed(1)}% INSIDE-OUT`);
1805
+ notes.push(`section: ${pct.toFixed(1)}% of the cut area is inside-out (blue): flipped normals`);
1806
+ }
1807
+ else if (bp >= 0.05)
1808
+ gn.push(`BACK-FACING ${bp.toFixed(1)}% OF PIXELS`);
1809
+ if (bp >= 0.05) {
1810
+ // which parts, and where the most visible one is
1811
+ const top = [...backPrims].sort((x, y) => y[1] - x[1] || x[0] - y[0]);
1812
+ const parts = partsOf(g, top.map(([p]) => p));
1813
+ notes.push(`${bp.toFixed(1)}% of the shaded pixels in the ${views[backView]} view are back faces of closed pieces (blue): flipped normals? ${top.length ? `First ${primWhere(g, top[0][0])}${parts ? `; parts: ${parts} prims` : ''}; ` : ''}reverse those faces or clean fix_winding`);
1814
+ }
1506
1815
  }
1507
- else if (bp >= 0.05)
1508
- gn.push(`BACK-FACING ${bp.toFixed(1)}% OF PIXELS`);
1509
- if (bp >= 0.05)
1510
- notes.push(`${bp.toFixed(1)}% of the shaded pixels in the ${views[backView]} view are back faces of closed pieces (blue): flipped normals?`);
1511
- const line = [...ovDesc, ...gn.slice(0, 3)].join(' · ');
1816
+ const overlays = [...ovDesc, ...gn.slice(0, 3)].join(' · ');
1817
+ const line = [hfFooter ? hfLine : '', annotate ? overlays : ''].filter(Boolean).join(' · ');
1512
1818
  const maxW = outW - 12 * ui;
1513
1819
  let fs2 = 2 * ui, txt = line;
1514
1820
  if (textWidth(txt, fs2) > maxW)
@@ -1516,14 +1822,17 @@ export function renderGeometry(g, o = {}) {
1516
1822
  while (txt.length > 4 && textWidth(txt, fs2) > maxW)
1517
1823
  txt = txt.slice(0, -4) + '..';
1518
1824
  drawText(out, outW, outH, 6 * ui, fy + 18 * ui + (fs2 < 2 * ui ? 3 * ui : 0), txt, [0.62, 0.84, 1], fs2, false);
1519
- notes.push('overlays: ' + line.toLowerCase());
1825
+ if (annotate)
1826
+ notes.push('overlays: ' + overlays.toLowerCase());
1520
1827
  }
1521
1828
  const rgba = new Uint8Array(outW * outH * 4);
1829
+ if (outA)
1830
+ outA.fill(1, (outH - footerH) * outW); // the footer stays opaque
1522
1831
  for (let i = 0, j = 0; i < out.length; i += 3, j += 4) {
1523
1832
  rgba[j] = toSRGB(out[i]);
1524
1833
  rgba[j + 1] = toSRGB(out[i + 1]);
1525
1834
  rgba[j + 2] = toSRGB(out[i + 2]);
1526
- rgba[j + 3] = 255;
1835
+ rgba[j + 3] = outA ? toSRGB(outA[j >> 2]) : 255;
1527
1836
  }
1528
1837
  if (legend)
1529
1838
  notes.push(legend.toLowerCase());
@@ -1606,6 +1915,32 @@ function drawDisc(col, zb, W, H, x, y, z, r, c, tol) {
1606
1915
  col[i + 2] = c[2] * edge;
1607
1916
  }
1608
1917
  }
1918
+ /** Blend an image into col over the rectangle (x, y, w, h) in pixels, bilinear, by opacity × the image's alpha. */
1919
+ function drawUnderlay(col, W, H, img, x, y, w, h, opacity) {
1920
+ const xa = Math.max(0, Math.floor(x)), xb = Math.min(W, Math.ceil(x + w)), ya = Math.max(0, Math.floor(y)), yb = Math.min(H, Math.ceil(y + h));
1921
+ const d = img.rgba, iw = img.width, ih = img.height;
1922
+ for (let py = ya; py < yb; py++) {
1923
+ const v = ((py + 0.5 - y) / h) * ih - 0.5;
1924
+ if (v < -0.5 || v > ih - 0.5)
1925
+ continue;
1926
+ const j0 = Math.max(0, Math.min(ih - 1, Math.floor(v))), j1 = Math.min(ih - 1, j0 + 1), fv = Math.max(0, Math.min(1, v - j0));
1927
+ for (let px = xa; px < xb; px++) {
1928
+ const u = ((px + 0.5 - x) / w) * iw - 0.5;
1929
+ if (u < -0.5 || u > iw - 0.5)
1930
+ continue;
1931
+ const i0 = Math.max(0, Math.min(iw - 1, Math.floor(u))), i1 = Math.min(iw - 1, i0 + 1), fu = Math.max(0, Math.min(1, u - i0));
1932
+ const at = (i, j, c) => d[(j * iw + i) * 4 + c];
1933
+ const s = (c) => (at(i0, j0, c) * (1 - fu) + at(i1, j0, c) * fu) * (1 - fv) + (at(i0, j1, c) * (1 - fu) + at(i1, j1, c) * fu) * fv;
1934
+ const a = (s(3) / 255) * opacity;
1935
+ if (!(a > 0))
1936
+ continue;
1937
+ const k = (py * W + px) * 3;
1938
+ col[k] += (s(0) / 255 - col[k]) * a;
1939
+ col[k + 1] += (s(1) / 255 - col[k + 1]) * a;
1940
+ col[k + 2] += (s(2) / 255 - col[k + 2]) * a;
1941
+ }
1942
+ }
1943
+ }
1609
1944
  /** Procedural 8×8 uv checker; light cells are tinted by the position inside the tile (red = u, green = v). */
1610
1945
  function checkerColor(u, v, out) {
1611
1946
  if (!Number.isFinite(u) || !Number.isFinite(v)) {
@@ -1709,9 +2044,23 @@ function drawUVView(g, col, W, H, ss, uv, tris, colorOf, useColorOf, notes) {
1709
2044
  const chartOf = !useColorOf && chartA && chartA.type === 'int' && chartA.size === 1 ? chartA.data : null;
1710
2045
  const owner = new Int32Array(W * H).fill(-1);
1711
2046
  const cnt = new Uint8Array(W * H);
2047
+ // overlap kinds (core/uvreport.ts): between atlas charts (red), or expected: a tiling chart (one that runs across
2048
+ // tile borders) or prims of different LODs (amber)
2049
+ const nt = tris.length / 3, U = new Float64Array(nt * 6);
2050
+ for (let c = 0; c < nt * 3; c++) {
2051
+ U[c * 2] = uv[tris[c] * 2];
2052
+ U[c * 2 + 1] = uv[tris[c] * 2 + 1];
2053
+ }
2054
+ const kinds = chartKinds(U, nt, chartRoots(g.vertexPoint, tris, U, nt)), lods = primLods(g);
2055
+ const first = new Int32Array(W * H).fill(-1), firstKind = new Uint8Array(W * H), flag = new Uint8Array(W * H);
2056
+ let tilingTris = 0;
2057
+ for (let t = 0; t < nt; t++)
2058
+ if (kinds[t] === 2)
2059
+ tilingTris++;
2060
+ const pairFlag = (pa, ka, pb, kb) => (ka === 2 || kb === 2 || (lods && lods[pa] >= 0 && lods[pb] >= 0 && lods[pa] !== lods[pb]) ? 2 : 1);
1712
2061
  for (let t = 0; t < tris.length; t += 3) {
1713
2062
  const va = tris[t], vb = tris[t + 1], vc = tris[t + 2];
1714
- const prim = vprim[va];
2063
+ const prim = vprim[va], kind = kinds[t / 3];
1715
2064
  const ax = X(uv[va * 2]), ay = Y(uv[va * 2 + 1]), bx = X(uv[vb * 2]), by = Y(uv[vb * 2 + 1]), cx = X(uv[vc * 2]), cy = Y(uv[vc * 2 + 1]);
1716
2065
  if (![ax, ay, bx, by, cx, cy].every(Number.isFinite))
1717
2066
  continue;
@@ -1739,6 +2088,12 @@ function drawUVView(g, col, W, H, ss, uv, tris, colorOf, useColorOf, notes) {
1739
2088
  const zi = py * W + px;
1740
2089
  if (owner[zi] === prim)
1741
2090
  continue;
2091
+ if (first[zi] < 0) {
2092
+ first[zi] = prim;
2093
+ firstKind[zi] = kind;
2094
+ }
2095
+ else if (first[zi] !== prim)
2096
+ flag[zi] |= pairFlag(first[zi], firstKind[zi], prim, kind);
1742
2097
  owner[zi] = prim;
1743
2098
  if (cnt[zi] < 255)
1744
2099
  cnt[zi]++;
@@ -1749,17 +2104,26 @@ function drawUVView(g, col, W, H, ss, uv, tris, colorOf, useColorOf, notes) {
1749
2104
  }
1750
2105
  }
1751
2106
  }
1752
- let covered = 0, overlap = 0;
2107
+ let covered = 0, overlap = 0, expected = 0;
1753
2108
  for (let i = 0; i < cnt.length; i++) {
1754
2109
  if (!cnt[i])
1755
2110
  continue;
1756
2111
  covered++;
1757
2112
  if (cnt[i] < 2)
1758
2113
  continue;
1759
- overlap++;
1760
- col[i * 3] += (1 - col[i * 3]) * 0.8;
1761
- col[i * 3 + 1] += (0.12 - col[i * 3 + 1]) * 0.8;
1762
- col[i * 3 + 2] += (0.12 - col[i * 3 + 2]) * 0.8;
2114
+ if (flag[i] & 1) {
2115
+ overlap++;
2116
+ col[i * 3] += (1 - col[i * 3]) * 0.8;
2117
+ col[i * 3 + 1] += (0.12 - col[i * 3 + 1]) * 0.8;
2118
+ col[i * 3 + 2] += (0.12 - col[i * 3 + 2]) * 0.8;
2119
+ }
2120
+ else {
2121
+ // expected overlap (tiling, LODs): a light amber wash, not the red of a real overlap
2122
+ expected++;
2123
+ col[i * 3] += (0.95 - col[i * 3]) * 0.35;
2124
+ col[i * 3 + 1] += (0.7 - col[i * 3 + 1]) * 0.35;
2125
+ col[i * 3 + 2] += (0.25 - col[i * 3 + 2]) * 0.35;
2126
+ }
1763
2127
  }
1764
2128
  // polygon edges, then island borders (uv seams and open borders) on top
1765
2129
  if (g.numPrims <= 60000) {
@@ -1787,8 +2151,12 @@ function drawUVView(g, col, W, H, ss, uv, tris, colorOf, useColorOf, notes) {
1787
2151
  edge(S.bv[i], S.bw[i]);
1788
2152
  tileLabels();
1789
2153
  const tiles = span === 1 && u0 === 0 && v0 === 0 ? '0..1' : `${span}×${span} TILES FROM ${u0},${v0}`;
1790
- const ov = covered ? (overlap / covered) * 100 : 0;
2154
+ const ov = covered ? (overlap / covered) * 100 : 0, ex = covered ? (expected / covered) * 100 : 0;
1791
2155
  if (ov >= 0.05)
1792
- notes.push(`uv view: ${ov.toFixed(1)}% of the covered uv area overlaps (red)`);
1793
- return `UV ${tiles}${ov >= 0.05 ? ` OVERLAP ${ov.toFixed(1)}%` : ''}`;
2156
+ notes.push(`uv view: ${ov.toFixed(1)}% of the covered uv area overlaps (red; inspect detail "uv" names the parts and where)`);
2157
+ if (tilingTris)
2158
+ notes.push(`uv view: tiling uv: ${tilingTris} triangles lie in charts that run across tile borders (a repeating texture, not an atlas); their overlap is expected (amber)`);
2159
+ if (ex >= 0.05 && lods)
2160
+ notes.push('uv view: LODs share the uv space; their overlap is expected (amber)');
2161
+ return `UV ${tiles}${ov >= 0.05 ? ` OVERLAP ${ov.toFixed(1)}%` : ''}${tilingTris ? ' TILING' : ''}${ex >= 0.05 && !tilingTris ? ' LOD OVERLAP OK' : ''}`;
1794
2162
  }