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.
- package/NOTICE +10 -10
- package/README.md +460 -117
- package/dist/cli.js +323 -146
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/bake.d.ts +14 -3
- package/dist/core/bake.js +153 -4
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +76 -3
- package/dist/core/decimate.d.ts +65 -0
- package/dist/core/decimate.js +409 -3
- package/dist/core/erosion.d.ts +37 -0
- package/dist/core/erosion.js +41 -1
- package/dist/core/geometry.d.ts +5 -1
- package/dist/core/geometry.js +203 -3
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/intrinsics.d.ts +7 -0
- package/dist/core/intrinsics.js +94 -0
- package/dist/core/manifold.d.ts +12 -1
- package/dist/core/manifold.js +78 -6
- package/dist/core/materials.d.ts +7 -0
- package/dist/core/materials.js +45 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/repair.d.ts +2 -0
- package/dist/core/repair.js +2 -0
- package/dist/core/report.d.ts +70 -0
- package/dist/core/report.js +275 -0
- package/dist/core/scene.js +2 -1
- package/dist/core/sha256.d.ts +4 -0
- package/dist/core/sha256.js +69 -0
- package/dist/core/shapes.d.ts +3 -0
- package/dist/core/shapes.js +34 -0
- package/dist/core/snippet-lib.d.ts +14 -1
- package/dist/core/snippet-lib.js +39 -13
- package/dist/core/tangents.d.ts +2 -0
- package/dist/core/tangents.js +5 -1
- package/dist/core/texnoise.d.ts +6 -1
- package/dist/core/texnoise.js +6 -2
- package/dist/core/texsdf.d.ts +131 -0
- package/dist/core/texsdf.js +637 -0
- package/dist/core/texture.d.ts +163 -9
- package/dist/core/texture.js +844 -20
- package/dist/core/topology.d.ts +10 -0
- package/dist/core/topology.js +168 -0
- package/dist/core/unwrap.d.ts +72 -0
- package/dist/core/unwrap.js +800 -0
- package/dist/core/usercode.d.ts +27 -0
- package/dist/core/usercode.js +87 -0
- package/dist/core/uv.d.ts +4 -18
- package/dist/core/uv.js +35 -188
- package/dist/core/uvislands.d.ts +62 -0
- package/dist/core/uvislands.js +574 -0
- package/dist/core/uvoverlap.d.ts +58 -0
- package/dist/core/uvoverlap.js +220 -0
- package/dist/core/uvpack.d.ts +64 -0
- package/dist/core/uvpack.js +628 -0
- package/dist/core/uvreport.d.ts +61 -0
- package/dist/core/uvreport.js +207 -143
- package/dist/core/wrangle.d.ts +6 -1
- package/dist/core/wrangle.js +27 -16
- package/dist/core/xatlas.d.ts +16 -1
- package/dist/core/xatlas.js +66 -62
- package/dist/engines.d.ts +63 -0
- package/dist/engines.js +180 -0
- package/dist/explain.d.ts +4 -3
- package/dist/explain.js +129 -12
- package/dist/graph/engine.d.ts +92 -29
- package/dist/graph/engine.js +298 -148
- package/dist/graph/expr.d.ts +6 -3
- package/dist/graph/expr.js +18 -8
- package/dist/graph/outputs.d.ts +23 -0
- package/dist/graph/outputs.js +41 -0
- package/dist/graph/packages.d.ts +72 -0
- package/dist/graph/packages.js +261 -0
- package/dist/graph/parallel.d.ts +3 -1
- package/dist/graph/parallel.js +4 -1
- package/dist/graph/params.d.ts +6 -0
- package/dist/graph/params.js +34 -7
- package/dist/graph/scope.d.ts +55 -0
- package/dist/graph/scope.js +14 -0
- package/dist/graph/types.d.ts +75 -12
- package/dist/graph/types.js +4 -2
- package/dist/graph/vars.d.ts +16 -0
- package/dist/graph/vars.js +80 -0
- package/dist/host/artifacts.d.ts +10 -1
- package/dist/host/artifacts.js +37 -6
- package/dist/host/cook.d.ts +241 -0
- package/dist/host/cook.js +844 -0
- package/dist/host/export-targets.d.ts +42 -5
- package/dist/host/export-targets.js +72 -13
- package/dist/host/godot-commands.d.ts +4 -0
- package/dist/host/godot-commands.js +21 -0
- package/dist/host/godot.d.ts +183 -0
- package/dist/host/godot.js +916 -0
- package/dist/host/node.d.ts +12 -7
- package/dist/host/node.js +27 -9
- package/dist/host/pool.js +51 -2
- package/dist/host/sidecars.d.ts +8 -0
- package/dist/host/sidecars.js +50 -0
- package/dist/host/types.d.ts +19 -6
- package/dist/host/version.d.ts +1 -0
- package/dist/host/version.js +3 -0
- package/dist/host/worker.js +5 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/inspect.d.ts +80 -2
- package/dist/inspect.js +265 -30
- package/dist/io/common.d.ts +25 -5
- package/dist/io/export.js +2 -1
- package/dist/io/files.d.ts +23 -4
- package/dist/io/files.js +60 -8
- package/dist/io/gltf.js +134 -47
- package/dist/io/heightmap.d.ts +3 -0
- package/dist/io/heightmap.js +1 -0
- package/dist/io/import.js +10 -0
- package/dist/io/inspect-scene.d.ts +55 -0
- package/dist/io/inspect-scene.js +155 -0
- package/dist/io/kinds.d.ts +81 -0
- package/dist/io/kinds.js +163 -0
- package/dist/io/points.d.ts +2 -1
- package/dist/io/points.js +3 -1
- package/dist/io/sidecars.d.ts +24 -0
- package/dist/io/sidecars.js +57 -0
- package/dist/io/source.d.ts +65 -0
- package/dist/io/source.js +112 -0
- package/dist/io/targets.d.ts +47 -3
- package/dist/io/targets.js +148 -13
- package/dist/io/textures.js +21 -1
- package/dist/io/unreal.js +5 -1
- package/dist/mcp/docs.d.ts +10 -1
- package/dist/mcp/docs.js +786 -143
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +56 -27
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +196 -103
- package/dist/mcp/verify.d.ts +2 -2
- package/dist/mcp/verify.js +1 -1
- package/dist/mcp/workspace.d.ts +15 -0
- package/dist/mcp/workspace.js +349 -0
- package/dist/nodes/attributes.js +198 -1
- package/dist/nodes/bake.js +151 -47
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +51 -3
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +21 -9
- package/dist/nodes/curves.js +433 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +12 -38
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.js +16 -19
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/material.js +89 -24
- package/dist/nodes/measure.js +43 -17
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.js +4 -2
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/splines.d.ts +15 -1
- package/dist/nodes/splines.js +112 -105
- package/dist/nodes/texture.d.ts +21 -6
- package/dist/nodes/texture.js +1318 -185
- package/dist/nodes/topology.d.ts +7 -0
- package/dist/nodes/topology.js +133 -6
- package/dist/nodes/transform.js +6 -2
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +5 -1
- package/dist/open.d.ts +19 -0
- package/dist/open.js +12 -0
- package/dist/package.d.ts +288 -0
- package/dist/package.js +772 -0
- package/dist/packages/check.d.ts +32 -0
- package/dist/packages/check.js +169 -0
- package/dist/packages/commands.d.ts +57 -0
- package/dist/packages/commands.js +537 -0
- package/dist/packages/registry.d.ts +796 -0
- package/dist/packages/registry.js +2998 -0
- package/dist/packages/update.d.ts +124 -0
- package/dist/packages/update.js +476 -0
- package/dist/render/options.d.ts +2 -0
- package/dist/render/options.js +44 -5
- package/dist/render/preview.d.ts +15 -4
- package/dist/render/preview.js +6 -4
- package/dist/render/raster.d.ts +66 -5
- package/dist/render/raster.js +506 -138
- package/dist/render/texview.d.ts +6 -0
- package/dist/render/texview.js +27 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +228 -0
- package/dist/session.d.ts +311 -0
- package/dist/session.js +849 -0
- package/dist/viewer/app/api.js +48 -6
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +27 -7
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +5 -1
- package/dist/viewer/app/guides.js +122 -0
- package/dist/viewer/app/keymap.js +6 -3
- package/dist/viewer/app/layers.js +4 -4
- package/dist/viewer/app/main.js +185 -52
- package/dist/viewer/app/markers.js +60 -0
- package/dist/viewer/app/outliner.js +72 -27
- package/dist/viewer/app/output-info.js +45 -0
- package/dist/viewer/app/outputs.css +147 -8
- package/dist/viewer/app/outputs.js +168 -54
- package/dist/viewer/app/packages.js +614 -0
- package/dist/viewer/app/props.js +176 -52
- package/dist/viewer/app/scene.js +30 -5
- package/dist/viewer/app/select.js +4 -3
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +65 -7
- package/dist/viewer/app/vars.js +47 -0
- package/dist/viewer/app/welcome.js +72 -0
- package/dist/viewer/graph-file.d.ts +62 -0
- package/dist/viewer/graph-file.js +175 -0
- package/dist/viewer/index.html +23 -10
- package/dist/viewer/json-edit.d.ts +13 -0
- package/dist/viewer/json-edit.js +274 -0
- package/dist/viewer/outputs.d.ts +49 -5
- package/dist/viewer/outputs.js +264 -56
- package/dist/viewer/packages.d.ts +121 -0
- package/dist/viewer/packages.js +584 -0
- package/dist/viewer/server.d.ts +14 -9
- package/dist/viewer/server.js +266 -28
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/{gallery → web}/bake.d.ts +4 -4
- package/dist/{gallery → web}/bake.js +24 -15
- package/dist/web/folder-page.d.ts +101 -0
- package/dist/web/folder-page.js +146 -0
- package/dist/web/folders.d.ts +280 -0
- package/dist/web/folders.js +970 -0
- package/dist/web/library.d.ts +91 -0
- package/dist/web/library.js +228 -0
- package/dist/web/markdown.d.ts +45 -0
- package/dist/web/markdown.js +677 -0
- package/dist/web/pages.d.ts +258 -0
- package/dist/web/pages.js +301 -0
- package/dist/web/server.d.ts +24 -0
- package/dist/web/server.js +1068 -0
- package/dist/workspace.d.ts +88 -118
- package/dist/workspace.js +358 -460
- package/integrations/godot/addons/topotopo/LICENSE +202 -0
- package/integrations/godot/addons/topotopo/README.md +57 -0
- package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
- package/integrations/godot/addons/topotopo/dock.gd +266 -0
- package/integrations/godot/addons/topotopo/dock.tscn +72 -0
- package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
- package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
- package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
- package/integrations/godot/addons/topotopo/inspector.gd +38 -0
- package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
- package/integrations/godot/addons/topotopo/package.gd +23 -0
- package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
- package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
- package/integrations/godot/addons/topotopo/plugin.gd +970 -0
- package/integrations/godot/addons/topotopo/record.gd +435 -0
- package/integrations/godot/addons/topotopo/runner.gd +458 -0
- package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
- package/integrations/godot/addons/topotopo/watcher.gd +199 -0
- package/kernels/pkg/README.md +2 -1
- package/kernels/pkg-simd/README.md +2 -1
- package/library/architecture/versions/1.0/architecture.topo +12 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
- package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
- package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
- package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
- package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
- package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
- package/library/architecture/versions/1.0/version.json +1119 -0
- package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
- package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
- package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
- package/library/nature/versions/1.0/graphs/lineup.json +24 -0
- package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
- package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
- package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
- package/library/nature/versions/1.0/nature.topo +12 -0
- package/library/nature/versions/1.0/version.json +534 -0
- package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
- package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
- package/library/props/versions/1.0/graphs/lineup.json +18 -0
- package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
- package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
- package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
- package/library/props/versions/1.0/props.topo +12 -0
- package/library/props/versions/1.0/version.json +336 -0
- package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
- package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
- package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
- package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
- package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
- package/library/street/versions/1.0/graphs/lineup.json +22 -0
- package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
- package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
- package/library/street/versions/1.0/street.topo +12 -0
- package/library/street/versions/1.0/version.json +421 -0
- package/package.json +17 -8
- package/dist/gallery/app/gallery.css +0 -100
- package/dist/gallery/app/gallery.js +0 -80
- package/dist/gallery/cli.d.ts +0 -4
- package/dist/gallery/cli.js +0 -157
- package/dist/gallery/library.d.ts +0 -124
- package/dist/gallery/library.js +0 -366
- package/dist/gallery/pages.d.ts +0 -94
- package/dist/gallery/pages.js +0 -148
- package/dist/gallery/server.d.ts +0 -21
- package/dist/gallery/server.js +0 -391
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/dist/mcp/library.d.ts +0 -12
- package/dist/mcp/library.js +0 -159
- package/shelf/index.json +0 -190
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
// UV island packing on a raster: islands never overlap and keep `padding` texels apart, by construction.
|
|
2
|
+
//
|
|
3
|
+
// Each island is rasterised conservatively (every cell its uv triangles touch) on a grid of G × G cells per tile,
|
|
4
|
+
// G = the texture resolution clamped to 256..2048. Per grid column an island keeps one run of cells, from its lowest to
|
|
5
|
+
// its highest occupied row (gaps inside a column are filled), so its shape is a column profile. The atlas keeps, per
|
|
6
|
+
// column, the sorted runs of the islands placed so far, each dilated by floor(padding / 2) cells; an island to place is
|
|
7
|
+
// dilated by ceil(padding / 2), so two islands are at least `padding` cells apart (Chebyshev) and an island keeps
|
|
8
|
+
// ceil(padding / 2) cells from the tile border. Runs closer than the smallest island could use are merged.
|
|
9
|
+
//
|
|
10
|
+
// Placement is bottom-left fill with holes: the lowest position, then the leftmost, at which the island's runs miss
|
|
11
|
+
// every run in the atlas. A candidate column is solved by a fixed-point search (raise the island past the first run it
|
|
12
|
+
// hits, column after column, until every column is clear); candidate columns are scanned at a quarter of the island's
|
|
13
|
+
// width, refined around the best by halving steps, then slid left. With turns, 0° and 90° are both tried and the lower
|
|
14
|
+
// top wins. The scale (cells per island unit) is the largest at which every island fits the tiles: from an area
|
|
15
|
+
// estimate, bracketed by packs that report their height, then bisected to 0.5 %. Other orderings are tried at the
|
|
16
|
+
// found scale and continue the search when they fit more.
|
|
17
|
+
//
|
|
18
|
+
// Islands outside the packed set (prims outside a group) are fixed obstacles: their cells are taken before packing.
|
|
19
|
+
// The uv written is exactly the rasterised geometry moved by whole cells, so no two islands share a cell and none
|
|
20
|
+
// overlap; callers still check the result exactly (core/uvoverlap.ts).
|
|
21
|
+
import { convexHull2D, minAreaRectAngle } from './uv.js';
|
|
22
|
+
import { rng } from './random.js';
|
|
23
|
+
/** Rasteriser margin: a cell an island touches within EPS counts as taken (conservative). */
|
|
24
|
+
const EPS = 1e-6;
|
|
25
|
+
/** Islands sit OFFSET cells off the cell grid, so an island edge never falls exactly on a cell border (which the margin would round out to a whole extra row or column). */
|
|
26
|
+
const OFFSET = 1 + 1e-4;
|
|
27
|
+
/** Add run [s, e] to a sorted flat run list (start, end pairs), merging runs fewer than gap + 1 free cells apart. */
|
|
28
|
+
function addRun(r, s, e, gap) {
|
|
29
|
+
const n = r.length;
|
|
30
|
+
let i = 0;
|
|
31
|
+
while (i < n && r[i + 1] < s - gap - 1)
|
|
32
|
+
i += 2;
|
|
33
|
+
if (i === n) {
|
|
34
|
+
r.push(s, e);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (r[i] > e + gap + 1) {
|
|
38
|
+
r.splice(i, 0, s, e);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const ns = r[i] < s ? r[i] : s;
|
|
42
|
+
let ne = e, j = i;
|
|
43
|
+
while (j < n && r[j] <= ne + gap + 1) {
|
|
44
|
+
if (r[j + 1] > ne)
|
|
45
|
+
ne = r[j + 1];
|
|
46
|
+
j += 2;
|
|
47
|
+
}
|
|
48
|
+
if (j - i === 2) {
|
|
49
|
+
r[i] = ns;
|
|
50
|
+
r[i + 1] = ne;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
r.splice(i, j - i, ns, ne);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Runs per column while a profile is built: a column with one run keeps it in lo/hi (the common case); a column with
|
|
57
|
+
* more keeps a sorted run list (hi = -2). Runs fewer than gap + 1 free cells apart merge.
|
|
58
|
+
*/
|
|
59
|
+
class RunCols {
|
|
60
|
+
w;
|
|
61
|
+
gap;
|
|
62
|
+
lo;
|
|
63
|
+
hi;
|
|
64
|
+
more = null;
|
|
65
|
+
constructor(w, gap) {
|
|
66
|
+
this.w = w;
|
|
67
|
+
this.gap = gap;
|
|
68
|
+
this.lo = new Int32Array(w);
|
|
69
|
+
this.hi = new Int32Array(w).fill(-1);
|
|
70
|
+
}
|
|
71
|
+
add(c, s, e) {
|
|
72
|
+
const h = this.hi[c], g = this.gap;
|
|
73
|
+
if (h === -1) {
|
|
74
|
+
this.lo[c] = s;
|
|
75
|
+
this.hi[c] = e;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (h >= 0) {
|
|
79
|
+
const l = this.lo[c];
|
|
80
|
+
if (s <= h + g + 1 && e >= l - g - 1) {
|
|
81
|
+
if (s < l)
|
|
82
|
+
this.lo[c] = s;
|
|
83
|
+
if (e > h)
|
|
84
|
+
this.hi[c] = e;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
(this.more ??= new Array(this.w))[c] = l < s ? [l, h, s, e] : [s, e, l, h];
|
|
88
|
+
this.hi[c] = -2;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
addRun(this.more[c], s, e, g);
|
|
92
|
+
}
|
|
93
|
+
profile() {
|
|
94
|
+
const w = this.w, lo = this.lo, hi = this.hi;
|
|
95
|
+
let n = 0, top = 0;
|
|
96
|
+
for (let c = 0; c < w; c++) {
|
|
97
|
+
if (hi[c] >= 0) {
|
|
98
|
+
n++;
|
|
99
|
+
if (hi[c] + 1 > top)
|
|
100
|
+
top = hi[c] + 1;
|
|
101
|
+
}
|
|
102
|
+
else if (hi[c] === -2) {
|
|
103
|
+
const r = this.more[c];
|
|
104
|
+
n += r.length / 2;
|
|
105
|
+
if (r[r.length - 1] + 1 > top)
|
|
106
|
+
top = r[r.length - 1] + 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const C = new Int32Array(n), L = new Int32Array(n), H = new Int32Array(n);
|
|
110
|
+
for (let c = 0, k = 0; c < w; c++) {
|
|
111
|
+
if (hi[c] >= 0) {
|
|
112
|
+
C[k] = c;
|
|
113
|
+
L[k] = lo[c];
|
|
114
|
+
H[k] = hi[c];
|
|
115
|
+
k++;
|
|
116
|
+
}
|
|
117
|
+
else if (hi[c] === -2) {
|
|
118
|
+
const r = this.more[c];
|
|
119
|
+
for (let i = 0; i < r.length; i += 2, k++) {
|
|
120
|
+
C[k] = c;
|
|
121
|
+
L[k] = r[i];
|
|
122
|
+
H[k] = r[i + 1];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return { w, top, n, c: C, lo: L, hi: H };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** Conservative raster of triangles as runs per column; px holds cell coordinates (2 per local vertex), all >= 0. */
|
|
130
|
+
function rasterise(px, tris, gap) {
|
|
131
|
+
let maxX = 0;
|
|
132
|
+
for (let i = 0; i < px.length; i += 2)
|
|
133
|
+
if (px[i] > maxX)
|
|
134
|
+
maxX = px[i];
|
|
135
|
+
const w = Math.floor(maxX + EPS) + 1;
|
|
136
|
+
const cols = new RunCols(w, gap);
|
|
137
|
+
for (let t = 0; t < tris.length; t += 3) {
|
|
138
|
+
const ax = px[tris[t] * 2], ay = px[tris[t] * 2 + 1], bx = px[tris[t + 1] * 2], by = px[tris[t + 1] * 2 + 1], cx = px[tris[t + 2] * 2], cy = px[tris[t + 2] * 2 + 1];
|
|
139
|
+
const c0 = Math.max(0, Math.floor(Math.min(ax, bx, cx) - EPS)), c1 = Math.min(w - 1, Math.floor(Math.max(ax, bx, cx) + EPS));
|
|
140
|
+
for (let c = c0; c <= c1; c++) {
|
|
141
|
+
const s0 = c - EPS, s1 = c + 1 + EPS;
|
|
142
|
+
let y0 = Infinity, y1 = -Infinity;
|
|
143
|
+
if (ax >= s0 && ax <= s1) {
|
|
144
|
+
if (ay < y0)
|
|
145
|
+
y0 = ay;
|
|
146
|
+
if (ay > y1)
|
|
147
|
+
y1 = ay;
|
|
148
|
+
}
|
|
149
|
+
if (bx >= s0 && bx <= s1) {
|
|
150
|
+
if (by < y0)
|
|
151
|
+
y0 = by;
|
|
152
|
+
if (by > y1)
|
|
153
|
+
y1 = by;
|
|
154
|
+
}
|
|
155
|
+
if (cx >= s0 && cx <= s1) {
|
|
156
|
+
if (cy < y0)
|
|
157
|
+
y0 = cy;
|
|
158
|
+
if (cy > y1)
|
|
159
|
+
y1 = cy;
|
|
160
|
+
}
|
|
161
|
+
// edges crossing the slab borders
|
|
162
|
+
for (let e = 0; e < 3; e++) {
|
|
163
|
+
const px0 = e === 0 ? ax : e === 1 ? bx : cx, py0 = e === 0 ? ay : e === 1 ? by : cy;
|
|
164
|
+
const px1 = e === 0 ? bx : e === 1 ? cx : ax, py1 = e === 0 ? by : e === 1 ? cy : ay;
|
|
165
|
+
for (let k = 0; k < 2; k++) {
|
|
166
|
+
const X = k ? s1 : s0;
|
|
167
|
+
if ((px0 - X) * (px1 - X) < 0) {
|
|
168
|
+
const y = py0 + ((X - px0) * (py1 - py0)) / (px1 - px0);
|
|
169
|
+
if (y < y0)
|
|
170
|
+
y0 = y;
|
|
171
|
+
if (y > y1)
|
|
172
|
+
y1 = y;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!(y0 <= y1))
|
|
177
|
+
continue;
|
|
178
|
+
cols.add(c, Math.max(0, Math.floor(y0 - EPS)), Math.floor(y1 + EPS));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return cols.profile();
|
|
182
|
+
}
|
|
183
|
+
/** Chebyshev dilation by d cells; the result's origin is d cells left of and below the input's. */
|
|
184
|
+
function dilate(p, d, gap) {
|
|
185
|
+
if (d <= 0)
|
|
186
|
+
return p;
|
|
187
|
+
const cols = new RunCols(p.w + 2 * d, gap);
|
|
188
|
+
for (let i = 0; i < p.n; i++)
|
|
189
|
+
for (let k = 0; k <= 2 * d; k++)
|
|
190
|
+
cols.add(p.c[i] + k, p.lo[i], p.hi[i] + 2 * d);
|
|
191
|
+
return cols.profile();
|
|
192
|
+
}
|
|
193
|
+
function cellsOf(p) {
|
|
194
|
+
let n = 0;
|
|
195
|
+
for (let i = 0; i < p.n; i++)
|
|
196
|
+
n += p.hi[i] - p.lo[i] + 1;
|
|
197
|
+
return n;
|
|
198
|
+
}
|
|
199
|
+
/** Per column the sorted, disjoint runs [start, end] of taken cells. */
|
|
200
|
+
class Atlas {
|
|
201
|
+
G;
|
|
202
|
+
mergeGap;
|
|
203
|
+
starts;
|
|
204
|
+
ends;
|
|
205
|
+
constructor(G, mergeGap, from) {
|
|
206
|
+
this.G = G;
|
|
207
|
+
this.mergeGap = mergeGap;
|
|
208
|
+
this.starts = from ? from.starts.map((a) => a.slice()) : Array.from({ length: G }, () => []);
|
|
209
|
+
this.ends = from ? from.ends.map((a) => a.slice()) : Array.from({ length: G }, () => []);
|
|
210
|
+
}
|
|
211
|
+
/** Take rows s..e of column c (runs closer than mergeGap free cells merge: no island fits between them). */
|
|
212
|
+
take(c, s, e) {
|
|
213
|
+
if (c < 0 || c >= this.G)
|
|
214
|
+
return;
|
|
215
|
+
const S = this.starts[c], E = this.ends[c], g = this.mergeGap;
|
|
216
|
+
let a = 0, b = E.length;
|
|
217
|
+
while (a < b) {
|
|
218
|
+
const m = (a + b) >> 1;
|
|
219
|
+
if (E[m] < s - g - 1)
|
|
220
|
+
a = m + 1;
|
|
221
|
+
else
|
|
222
|
+
b = m;
|
|
223
|
+
}
|
|
224
|
+
let z = a;
|
|
225
|
+
while (z < S.length && S[z] <= e + g + 1)
|
|
226
|
+
z++;
|
|
227
|
+
if (z > a) {
|
|
228
|
+
if (S[a] < s)
|
|
229
|
+
s = S[a];
|
|
230
|
+
if (E[z - 1] > e)
|
|
231
|
+
e = E[z - 1];
|
|
232
|
+
}
|
|
233
|
+
S.splice(a, z - a, s);
|
|
234
|
+
E.splice(a, z - a, e);
|
|
235
|
+
}
|
|
236
|
+
/** The lowest y (<= maxY) at which profile T, its column 0 at column X, misses every run; -1 when there is none. */
|
|
237
|
+
lowest(T, X, maxY) {
|
|
238
|
+
const n = T.n;
|
|
239
|
+
if (!n)
|
|
240
|
+
return 0;
|
|
241
|
+
let y = 0, ok = 0, i = 0;
|
|
242
|
+
while (ok < n) {
|
|
243
|
+
const S = this.starts[X + T.c[i]], E = this.ends[X + T.c[i]];
|
|
244
|
+
const need0 = y + T.lo[i];
|
|
245
|
+
let a = 0, b = E.length;
|
|
246
|
+
while (a < b) {
|
|
247
|
+
const m = (a + b) >> 1;
|
|
248
|
+
if (E[m] < need0)
|
|
249
|
+
a = m + 1;
|
|
250
|
+
else
|
|
251
|
+
b = m;
|
|
252
|
+
}
|
|
253
|
+
if (a < E.length && S[a] <= y + T.hi[i]) {
|
|
254
|
+
y = E[a] + 1 - T.lo[i];
|
|
255
|
+
if (y > maxY)
|
|
256
|
+
return -1;
|
|
257
|
+
ok = 0;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
ok++;
|
|
261
|
+
i = i + 1 === n ? 0 : i + 1;
|
|
262
|
+
}
|
|
263
|
+
return y;
|
|
264
|
+
}
|
|
265
|
+
/** Bottom-left position of T with y <= maxY: { x, y } or null. */
|
|
266
|
+
place(T, maxY) {
|
|
267
|
+
const maxX = this.G - T.w;
|
|
268
|
+
if (maxX < 0 || maxY < 0)
|
|
269
|
+
return null;
|
|
270
|
+
let bx = -1, by = maxY + 1;
|
|
271
|
+
const tryX = (x) => {
|
|
272
|
+
if (x < 0 || x > maxX || x === bx)
|
|
273
|
+
return;
|
|
274
|
+
const lim = bx < 0 || x < bx ? by : by - 1;
|
|
275
|
+
if (lim < 0)
|
|
276
|
+
return;
|
|
277
|
+
const y = this.lowest(T, x, Math.min(lim, maxY));
|
|
278
|
+
if (y >= 0 && (y < by || (y === by && x < bx))) {
|
|
279
|
+
by = y;
|
|
280
|
+
bx = x;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const st = Math.max(1, Math.floor(T.w / 4), Math.floor(this.G / 256));
|
|
284
|
+
for (let x = 0; x <= maxX; x += st)
|
|
285
|
+
tryX(x);
|
|
286
|
+
tryX(maxX);
|
|
287
|
+
if (bx < 0)
|
|
288
|
+
return null;
|
|
289
|
+
for (let step = st >> 1; step >= 1; step >>= 1) {
|
|
290
|
+
const c = bx;
|
|
291
|
+
tryX(c - step);
|
|
292
|
+
tryX(c + step);
|
|
293
|
+
}
|
|
294
|
+
// slide left at the same height (bisection: a leftward position that fits, not always the leftmost there is)
|
|
295
|
+
let l = Math.max(0, bx - st), r = bx;
|
|
296
|
+
if (l < r) {
|
|
297
|
+
const yl = this.lowest(T, l, by);
|
|
298
|
+
if (yl >= 0) {
|
|
299
|
+
bx = l;
|
|
300
|
+
by = yl;
|
|
301
|
+
}
|
|
302
|
+
else
|
|
303
|
+
while (r - l > 1) {
|
|
304
|
+
const m = (l + r) >> 1, y = this.lowest(T, m, by);
|
|
305
|
+
if (y >= 0) {
|
|
306
|
+
r = m;
|
|
307
|
+
bx = m;
|
|
308
|
+
by = y;
|
|
309
|
+
}
|
|
310
|
+
else
|
|
311
|
+
l = m;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return { x: bx, y: by };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/** Pack islands into the unit tile (or UDIM tiles); returns new per-vertex uv (other vertices keep theirs). */
|
|
318
|
+
export function packCharts(uv, islands, o) {
|
|
319
|
+
const out = uv.slice();
|
|
320
|
+
const res = Math.max(1, o.resolution || 1024);
|
|
321
|
+
const G = Math.max(256, Math.min(2048, Math.round(res)));
|
|
322
|
+
const padCells = Math.max(0, Math.ceil((Math.max(0, o.padding) * G) / res - 1e-9));
|
|
323
|
+
const a = Math.floor(padCells / 2), b = padCells - a;
|
|
324
|
+
// free gaps of 2b cells or fewer hold no island (every island to place is at least 1 + 2b cells tall): merged away
|
|
325
|
+
const gap = 2 * b;
|
|
326
|
+
const tiles = Math.max(1, Math.min(100, Math.floor(o.tiles || 1)));
|
|
327
|
+
const result = { uv: out, packed: false, grid: G, padCells, tilesUsed: 0, uvArea: 0, packs: 0, cells: { island: 0, padded: 0 }, cappedBy: -1, texelsPerUnit: 0 };
|
|
328
|
+
const nI = islands.length;
|
|
329
|
+
if (!nI)
|
|
330
|
+
return result;
|
|
331
|
+
const prep = prepare(uv, islands, o);
|
|
332
|
+
// obstacle runs (fixed uv triangles), per tile, dilated by a when taken
|
|
333
|
+
const base = [];
|
|
334
|
+
for (let ti = 0; ti < tiles; ti++) {
|
|
335
|
+
const A = new Atlas(G, gap);
|
|
336
|
+
if (o.obstacles && o.obstacles.length)
|
|
337
|
+
takeObstacles(A, o.obstacles, ti % 10, Math.floor(ti / 10), G, a, gap);
|
|
338
|
+
base.push(A);
|
|
339
|
+
}
|
|
340
|
+
const turnsOf = o.turns ? [0, 1] : [0];
|
|
341
|
+
const packAt = (s, order, hardTop) => {
|
|
342
|
+
const atlases = base.map((A) => new Atlas(G, gap, A));
|
|
343
|
+
const H = new Int32Array(tiles);
|
|
344
|
+
const r = { ok: false, H: 0, tile: new Int32Array(nI), x: new Int32Array(nI), y: new Int32Array(nI), turn: new Uint8Array(nI), tilesUsed: 0 };
|
|
345
|
+
for (const k of order) {
|
|
346
|
+
let best = null;
|
|
347
|
+
for (const turn of turnsOf) {
|
|
348
|
+
const loc = prep.local[k][turn];
|
|
349
|
+
if (!loc)
|
|
350
|
+
continue;
|
|
351
|
+
const px = new Float64Array(loc.length);
|
|
352
|
+
for (let i = 0; i < loc.length; i++)
|
|
353
|
+
px[i] = loc[i] * s + OFFSET;
|
|
354
|
+
const R = rasterise(px, prep.tris[k], gap);
|
|
355
|
+
const T = dilate(R, b, gap);
|
|
356
|
+
if (T.w > G)
|
|
357
|
+
continue;
|
|
358
|
+
for (let ti = 0; ti < tiles; ti++) {
|
|
359
|
+
const limit = ti === tiles - 1 ? hardTop : G;
|
|
360
|
+
const p = atlases[ti].place(T, limit - T.top);
|
|
361
|
+
if (!p)
|
|
362
|
+
continue;
|
|
363
|
+
const top = p.y + T.top;
|
|
364
|
+
if (!best || ti < best.ti || (ti === best.ti && top < best.top))
|
|
365
|
+
best = { ti, x: p.x, y: p.y, top, turn, S: dilate(R, a, gap) };
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (!best)
|
|
370
|
+
return r;
|
|
371
|
+
const A = atlases[best.ti], S = best.S, ox = best.x + b - a, oy = best.y + b - a;
|
|
372
|
+
for (let i = 0; i < S.n; i++)
|
|
373
|
+
A.take(ox + S.c[i], oy + S.lo[i], oy + S.hi[i]);
|
|
374
|
+
if (best.top > H[best.ti])
|
|
375
|
+
H[best.ti] = best.top;
|
|
376
|
+
r.tile[k] = best.ti;
|
|
377
|
+
r.x[k] = best.x;
|
|
378
|
+
r.y[k] = best.y;
|
|
379
|
+
r.turn[k] = best.turn;
|
|
380
|
+
}
|
|
381
|
+
r.H = H[tiles - 1];
|
|
382
|
+
r.ok = r.H <= G;
|
|
383
|
+
for (let ti = 0; ti < tiles; ti++)
|
|
384
|
+
if (H[ti] > 0)
|
|
385
|
+
r.tilesUsed = ti + 1;
|
|
386
|
+
return r;
|
|
387
|
+
};
|
|
388
|
+
// scale bounds: every island must fit a tile; start from the area
|
|
389
|
+
let maxDim = 0, area = 0, largest = -1;
|
|
390
|
+
for (let k = 0; k < nI; k++) {
|
|
391
|
+
if (Math.max(prep.w[k], prep.h[k]) > maxDim) {
|
|
392
|
+
maxDim = Math.max(prep.w[k], prep.h[k]);
|
|
393
|
+
largest = k;
|
|
394
|
+
}
|
|
395
|
+
area += prep.area[k];
|
|
396
|
+
}
|
|
397
|
+
const sFix = (o.texelsPerUnit ?? 0) > 0 ? (o.texelsPerUnit * G) / res : 0;
|
|
398
|
+
const sMax = (o.maxTexelsPerUnit ?? 0) > 0 ? (o.maxTexelsPerUnit * G) / res : Infinity;
|
|
399
|
+
const sCap = Math.min(sMax, maxDim > 0 ? (G - 2 * b - 3) / maxDim : sFix > 0 ? sFix : 1);
|
|
400
|
+
if (!(sCap > 0))
|
|
401
|
+
return result;
|
|
402
|
+
let s0 = area > 0 ? Math.sqrt((0.55 * tiles * G * G) / area) : sCap;
|
|
403
|
+
s0 = Math.min(sCap, s0);
|
|
404
|
+
const orders = orderings(prep, nI, Math.max(1, Math.min(64, Math.floor(o.iterations || 1))), o.seed ?? 0);
|
|
405
|
+
let bestS = 0, best = null;
|
|
406
|
+
const hardTop = Math.ceil(G * 1.6);
|
|
407
|
+
const search = (order, sStart, lo, hi, maxPacks) => {
|
|
408
|
+
let s = sStart;
|
|
409
|
+
for (let i = 0; i < maxPacks; i++) {
|
|
410
|
+
const r = packAt(s, order, lo > 0 && hi < Infinity ? G : hardTop);
|
|
411
|
+
result.packs++;
|
|
412
|
+
if (r.ok) {
|
|
413
|
+
lo = s;
|
|
414
|
+
if (s > bestS) {
|
|
415
|
+
bestS = s;
|
|
416
|
+
best = r;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
else
|
|
420
|
+
hi = s;
|
|
421
|
+
if (lo > 0 && hi < Infinity && hi / lo < 1.005)
|
|
422
|
+
break;
|
|
423
|
+
if (lo >= sCap)
|
|
424
|
+
break;
|
|
425
|
+
const full = r.H > 0 && r.tile.length ? r.H : 0;
|
|
426
|
+
if (hi === Infinity)
|
|
427
|
+
s = Math.min(sCap, s * Math.min(1.5, Math.max(1.01, full > 0 ? Math.sqrt(G / full) * 0.998 : 1.5)));
|
|
428
|
+
else if (lo === 0)
|
|
429
|
+
s = s * (r.H > 0 && r.H < hardTop ? Math.max(0.5, Math.min(0.98, Math.sqrt(G / r.H))) : 0.75);
|
|
430
|
+
else
|
|
431
|
+
s = Math.sqrt(lo * hi);
|
|
432
|
+
}
|
|
433
|
+
return { lo, hi };
|
|
434
|
+
};
|
|
435
|
+
if (sFix > 0) {
|
|
436
|
+
// a fixed density: one pack over the tiles; only when it does not fit does the density drop
|
|
437
|
+
const s = Math.min(sFix, sCap);
|
|
438
|
+
const r = packAt(s, orders[0], G);
|
|
439
|
+
result.packs++;
|
|
440
|
+
if (r.ok) {
|
|
441
|
+
bestS = s;
|
|
442
|
+
best = r;
|
|
443
|
+
}
|
|
444
|
+
else
|
|
445
|
+
search(orders[0], s * 0.97, 0, s, 24);
|
|
446
|
+
}
|
|
447
|
+
const first = sFix > 0 ? { lo: bestS, hi: Infinity } : search(orders[0], s0, 0, Infinity, 24);
|
|
448
|
+
// other orderings at the scale found: one that fits there too continues the search upwards
|
|
449
|
+
for (let i = 1; i < orders.length && bestS > 0 && bestS < sCap && !(sFix > 0); i++) {
|
|
450
|
+
const s = Math.min(sCap, bestS * 1.004);
|
|
451
|
+
const r = packAt(s, orders[i], G);
|
|
452
|
+
result.packs++;
|
|
453
|
+
if (!r.ok)
|
|
454
|
+
continue;
|
|
455
|
+
bestS = s;
|
|
456
|
+
best = r;
|
|
457
|
+
search(orders[i], Math.min(sCap, s * 1.01), s, first.hi > s * 1.01 ? first.hi : Infinity, 8);
|
|
458
|
+
}
|
|
459
|
+
if (!best || !(bestS > 0))
|
|
460
|
+
return result;
|
|
461
|
+
const P = best;
|
|
462
|
+
// write: uv = (local · s + OFFSET + b + cell offset) / G + tile, exactly the rasterised shape moved by whole cells
|
|
463
|
+
let cellsIsland = 0, cellsPadded = 0;
|
|
464
|
+
for (let k = 0; k < nI; k++) {
|
|
465
|
+
const isl = islands[k], loc = prep.local[k][P.turn[k]];
|
|
466
|
+
const px = new Float64Array(loc.length);
|
|
467
|
+
for (let i = 0; i < loc.length; i++)
|
|
468
|
+
px[i] = loc[i] * bestS + OFFSET;
|
|
469
|
+
const R = rasterise(px, prep.tris[k], gap);
|
|
470
|
+
cellsIsland += cellsOf(R);
|
|
471
|
+
cellsPadded += cellsOf(dilate(R, b, gap));
|
|
472
|
+
const tu = P.tile[k] % 10, tv = Math.floor(P.tile[k] / 10);
|
|
473
|
+
for (let i = 0; i < isl.verts.length; i++) {
|
|
474
|
+
const v = isl.verts[i];
|
|
475
|
+
out[v * 2] = (px[i * 2] + b + P.x[k]) / G + tu;
|
|
476
|
+
out[v * 2 + 1] = (px[i * 2 + 1] + b + P.y[k]) / G + tv;
|
|
477
|
+
}
|
|
478
|
+
result.uvArea += (prep.area[k] * bestS * bestS) / (G * G);
|
|
479
|
+
}
|
|
480
|
+
result.packed = true;
|
|
481
|
+
result.tilesUsed = P.tilesUsed;
|
|
482
|
+
if (bestS >= sCap * 0.999)
|
|
483
|
+
result.cappedBy = largest;
|
|
484
|
+
result.texelsPerUnit = (bestS * res) / G;
|
|
485
|
+
const tileCells = G * G * Math.max(1, P.tilesUsed);
|
|
486
|
+
result.cells = { island: cellsIsland / tileCells, padded: cellsPadded / tileCells };
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
/** Orient and scale each island into its own units; local coordinates for turn 0 and (with turns) turn 90°. */
|
|
490
|
+
function prepare(uv, islands, o) {
|
|
491
|
+
const nI = islands.length;
|
|
492
|
+
const prep = { local: [], tris: [], w: new Float64Array(nI), h: new Float64Array(nI), area: new Float64Array(nI) };
|
|
493
|
+
let densSum = 0, densN = 0;
|
|
494
|
+
for (const isl of islands)
|
|
495
|
+
if (isl.areaUV > 1e-18 && isl.area3 > 1e-18) {
|
|
496
|
+
densSum += Math.sqrt(isl.area3 / isl.areaUV);
|
|
497
|
+
densN++;
|
|
498
|
+
}
|
|
499
|
+
const meanDens = densN ? densSum / densN : 1;
|
|
500
|
+
const localOf = new Map();
|
|
501
|
+
for (let k = 0; k < nI; k++) {
|
|
502
|
+
const isl = islands[k], nv = isl.verts.length;
|
|
503
|
+
localOf.clear();
|
|
504
|
+
for (let i = 0; i < nv; i++)
|
|
505
|
+
localOf.set(isl.verts[i], i);
|
|
506
|
+
const tl = [];
|
|
507
|
+
for (let t = 0; t + 2 < isl.tris.length; t += 3) {
|
|
508
|
+
const p = localOf.get(isl.tris[t]), q = localOf.get(isl.tris[t + 1]), r = localOf.get(isl.tris[t + 2]);
|
|
509
|
+
if (p !== undefined && q !== undefined && r !== undefined)
|
|
510
|
+
tl.push(p, q, r);
|
|
511
|
+
}
|
|
512
|
+
if (!tl.length)
|
|
513
|
+
for (let i = 0; i < nv; i++)
|
|
514
|
+
tl.push(i, i, i);
|
|
515
|
+
prep.tris.push(Int32Array.from(tl));
|
|
516
|
+
const pts = new Float64Array(nv * 2);
|
|
517
|
+
for (let i = 0; i < nv; i++) {
|
|
518
|
+
const v = isl.verts[i], u = uv[v * 2], w = uv[v * 2 + 1];
|
|
519
|
+
pts[i * 2] = Number.isFinite(u) ? u : 0;
|
|
520
|
+
pts[i * 2 + 1] = Number.isFinite(w) ? w : 0;
|
|
521
|
+
}
|
|
522
|
+
const fixed = o.angles ? Number(o.angles[k]) : NaN;
|
|
523
|
+
const ang = Number.isFinite(fixed) ? fixed : o.orient === 'min_rect' && nv >= 2 ? minAreaRectAngle(convexHull2D(pts)) : 0;
|
|
524
|
+
let sc = o.scaleMode === 'uniform' ? (isl.areaUV > 1e-18 && isl.area3 > 1e-18 ? Math.sqrt(isl.area3 / isl.areaUV) : meanDens) : 1;
|
|
525
|
+
const wt = o.weights ? Number(o.weights[k]) : 1;
|
|
526
|
+
if (Number.isFinite(wt) && wt > 0)
|
|
527
|
+
sc *= wt;
|
|
528
|
+
const c = Math.cos(ang), s = Math.sin(ang);
|
|
529
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
530
|
+
const L = new Float64Array(nv * 2);
|
|
531
|
+
for (let i = 0; i < nv; i++) {
|
|
532
|
+
const x = (pts[i * 2] * c - pts[i * 2 + 1] * s) * sc, y = (pts[i * 2] * s + pts[i * 2 + 1] * c) * sc;
|
|
533
|
+
L[i * 2] = x;
|
|
534
|
+
L[i * 2 + 1] = y;
|
|
535
|
+
if (x < x0)
|
|
536
|
+
x0 = x;
|
|
537
|
+
if (x > x1)
|
|
538
|
+
x1 = x;
|
|
539
|
+
if (y < y0)
|
|
540
|
+
y0 = y;
|
|
541
|
+
if (y > y1)
|
|
542
|
+
y1 = y;
|
|
543
|
+
}
|
|
544
|
+
if (!(x0 <= x1)) {
|
|
545
|
+
x0 = x1 = y0 = y1 = 0;
|
|
546
|
+
}
|
|
547
|
+
for (let i = 0; i < nv; i++) {
|
|
548
|
+
L[i * 2] = Math.max(0, L[i * 2] - x0);
|
|
549
|
+
L[i * 2 + 1] = Math.max(0, L[i * 2 + 1] - y0);
|
|
550
|
+
}
|
|
551
|
+
const w = x1 - x0, h = y1 - y0;
|
|
552
|
+
let T = null;
|
|
553
|
+
if (o.turns) {
|
|
554
|
+
// 90° counter-clockwise: (x, y) -> (h - y, x); a rotation, so the uv winding is kept
|
|
555
|
+
T = new Float64Array(nv * 2);
|
|
556
|
+
for (let i = 0; i < nv; i++) {
|
|
557
|
+
T[i * 2] = Math.max(0, h - L[i * 2 + 1]);
|
|
558
|
+
T[i * 2 + 1] = L[i * 2];
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
prep.local.push([L, T]);
|
|
562
|
+
prep.w[k] = w;
|
|
563
|
+
prep.h[k] = h;
|
|
564
|
+
let ar = 0;
|
|
565
|
+
const tr = prep.tris[k];
|
|
566
|
+
for (let t = 0; t < tr.length; t += 3) {
|
|
567
|
+
const p = tr[t] * 2, q = tr[t + 1] * 2, r = tr[t + 2] * 2;
|
|
568
|
+
ar += Math.abs((L[q] - L[p]) * (L[r + 1] - L[p + 1]) - (L[r] - L[p]) * (L[q + 1] - L[p + 1])) / 2;
|
|
569
|
+
}
|
|
570
|
+
prep.area[k] = ar;
|
|
571
|
+
}
|
|
572
|
+
return prep;
|
|
573
|
+
}
|
|
574
|
+
/** Island orders, largest first: by longest side, height, bounding-box area, area, then shuffles of the first. */
|
|
575
|
+
function orderings(prep, nI, count, seed) {
|
|
576
|
+
const idx = () => Array.from({ length: nI }, (_, i) => i);
|
|
577
|
+
const { w, h } = prep;
|
|
578
|
+
const keys = [(k) => Math.max(w[k], h[k]), (k) => h[k], (k) => w[k] * h[k], (k) => prep.area[k]];
|
|
579
|
+
const out = [];
|
|
580
|
+
for (let i = 0; i < Math.min(count, keys.length); i++) {
|
|
581
|
+
const key = keys[i];
|
|
582
|
+
out.push(Int32Array.from(idx().sort((p, q) => key(q) - key(p) || p - q)));
|
|
583
|
+
}
|
|
584
|
+
const R = rng((seed | 0) * 7919 + 17);
|
|
585
|
+
while (out.length < count) {
|
|
586
|
+
const o = Array.from(out[0]);
|
|
587
|
+
for (let s = 0; s < nI; s++) {
|
|
588
|
+
const i = Math.floor(R() * nI), j = Math.min(nI - 1, i + 1 + Math.floor(R() * Math.max(1, nI * 0.05)));
|
|
589
|
+
[o[i], o[j]] = [o[j], o[i]];
|
|
590
|
+
}
|
|
591
|
+
out.push(Int32Array.from(o));
|
|
592
|
+
}
|
|
593
|
+
return out;
|
|
594
|
+
}
|
|
595
|
+
/** Take the cells of fixed uv triangles inside tile (tu, tv), dilated by a. */
|
|
596
|
+
function takeObstacles(A, tri, tu, tv, G, a, gap) {
|
|
597
|
+
const px = new Float64Array(6), one = Int32Array.from([0, 1, 2]);
|
|
598
|
+
for (let t = 0; t + 5 < tri.length; t += 6) {
|
|
599
|
+
let finite = true;
|
|
600
|
+
for (let k = 0; k < 3; k++) {
|
|
601
|
+
px[k * 2] = (tri[t + k * 2] - tu) * G;
|
|
602
|
+
px[k * 2 + 1] = (tri[t + k * 2 + 1] - tv) * G;
|
|
603
|
+
if (!Number.isFinite(px[k * 2]) || !Number.isFinite(px[k * 2 + 1]))
|
|
604
|
+
finite = false;
|
|
605
|
+
}
|
|
606
|
+
if (!finite)
|
|
607
|
+
continue;
|
|
608
|
+
const u0 = Math.min(px[0], px[2], px[4]), u1 = Math.max(px[0], px[2], px[4]), v0 = Math.min(px[1], px[3], px[5]), v1 = Math.max(px[1], px[3], px[5]);
|
|
609
|
+
if (u1 < -a - 1 || u0 > G + a + 1 || v1 < -a - 1 || v0 > G + a + 1)
|
|
610
|
+
continue;
|
|
611
|
+
if (u1 - u0 > 4 * G || v1 - v0 > 4 * G) {
|
|
612
|
+
// a huge (tiling) triangle: take its bounding box inside the tile
|
|
613
|
+
const c0 = Math.max(0, Math.floor(u0) - a), c1 = Math.min(G - 1, Math.floor(u1) + a);
|
|
614
|
+
for (let c = c0; c <= c1; c++)
|
|
615
|
+
A.take(c, Math.floor(v0) - a, Math.floor(v1) + a);
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
// rasterise from a whole-cell origin at the triangle's lower corner, then move the cells back to the tile
|
|
619
|
+
const ox = Math.floor(u0), oy = Math.floor(v0);
|
|
620
|
+
for (let k = 0; k < 3; k++) {
|
|
621
|
+
px[k * 2] -= ox;
|
|
622
|
+
px[k * 2 + 1] -= oy;
|
|
623
|
+
}
|
|
624
|
+
const R = dilate(rasterise(px, one, gap), a, gap);
|
|
625
|
+
for (let i = 0; i < R.n; i++)
|
|
626
|
+
A.take(R.c[i] - a + ox, R.lo[i] - a + oy, R.hi[i] - a + oy);
|
|
627
|
+
}
|
|
628
|
+
}
|