topotopo 0.4.0 → 0.5.1
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 +327 -147
- 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 +198 -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 +57 -5
- package/dist/render/preview.d.ts +17 -4
- package/dist/render/preview.js +6 -4
- package/dist/render/raster.d.ts +74 -5
- package/dist/render/raster.js +527 -143
- 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 +13 -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
package/dist/core/decimate.js
CHANGED
|
@@ -12,6 +12,11 @@ import { MeshoptSimplifier } from 'meshoptimizer';
|
|
|
12
12
|
import { GeoBuilder } from './geometry.js';
|
|
13
13
|
import { triangulate } from './topology.js';
|
|
14
14
|
import { primRegions, vertexAttribsDiffer } from './remesh.js';
|
|
15
|
+
import { readUV } from './uv.js';
|
|
16
|
+
import { trianglesUV, uvOverlapPairs, uvPairText, uvTriangleAreas } from './uvoverlap.js';
|
|
17
|
+
import { chartRoots } from './uvreport.js';
|
|
18
|
+
import { partsOf, primWhere } from './report.js';
|
|
19
|
+
import { TriBVH, closestScratch } from './spatial.js';
|
|
15
20
|
export class DecimateError extends Error {
|
|
16
21
|
}
|
|
17
22
|
/**
|
|
@@ -53,6 +58,36 @@ function steerColumns(g, attrs, maxFloats) {
|
|
|
53
58
|
}
|
|
54
59
|
return { cols, total, used, skipped };
|
|
55
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Connected components of a triangle index list over `n` render vertices, or, with `toPoint`, over the points they map
|
|
63
|
+
* to (`np` points).
|
|
64
|
+
*/
|
|
65
|
+
function islandCount(idx, n, toPoint, np = 0) {
|
|
66
|
+
const m = toPoint ? np : n, parent = new Int32Array(m);
|
|
67
|
+
for (let i = 0; i < m; i++)
|
|
68
|
+
parent[i] = i;
|
|
69
|
+
const find = (x) => { while (parent[x] !== x) {
|
|
70
|
+
parent[x] = parent[parent[x]];
|
|
71
|
+
x = parent[x];
|
|
72
|
+
} return x; };
|
|
73
|
+
const id = (r) => (toPoint ? toPoint[r] : r);
|
|
74
|
+
for (let t = 0; t < idx.length; t += 3) {
|
|
75
|
+
const a = find(id(idx[t])), b = find(id(idx[t + 1])), c = find(id(idx[t + 2]));
|
|
76
|
+
if (a !== b)
|
|
77
|
+
parent[a] = b;
|
|
78
|
+
const b2 = find(b);
|
|
79
|
+
if (find(c) !== b2)
|
|
80
|
+
parent[find(c)] = b2;
|
|
81
|
+
}
|
|
82
|
+
const used = new Uint8Array(m);
|
|
83
|
+
for (let t = 0; t < idx.length; t++)
|
|
84
|
+
used[id(idx[t])] = 1;
|
|
85
|
+
let count = 0;
|
|
86
|
+
for (let i = 0; i < m; i++)
|
|
87
|
+
if (used[i] && find(i) === i)
|
|
88
|
+
count++;
|
|
89
|
+
return count;
|
|
90
|
+
}
|
|
56
91
|
/** Decimate the (selected) polygons of g to triangles with meshoptimizer; other prims pass through. */
|
|
57
92
|
export function decimateMesh(g, o) {
|
|
58
93
|
const sel = o.group;
|
|
@@ -129,10 +164,14 @@ export function decimateMesh(g, o) {
|
|
|
129
164
|
lock[r] = lockPt[rvPt[r]];
|
|
130
165
|
}
|
|
131
166
|
const target = Math.min(nt, o.targetCount > 0 ? Math.floor(o.targetCount) : Math.floor(nt * Math.max(0, Math.min(1, o.ratio)))) * 3;
|
|
167
|
+
// meshoptimizer prunes components of the render-vertex mesh: where seams or hard normals split a connected piece
|
|
168
|
+
// into islands, it would remove parts of surfaces (a sphere with hard normals vanished whole), so prune only when the
|
|
169
|
+
// render vertices keep every piece in one island
|
|
170
|
+
const prune = o.prune && islandCount(idx, R) === islandCount(idx, R, rvPt, np);
|
|
132
171
|
const scale = MeshoptSimplifier.getScale(pos, 3) || 1;
|
|
133
172
|
const steer = steerColumns(g, o.attributes, 32);
|
|
134
173
|
let out = idx, err = 0;
|
|
135
|
-
if (target < nt * 3 ||
|
|
174
|
+
if (target < nt * 3 || prune) {
|
|
136
175
|
if (o.sloppy) {
|
|
137
176
|
const relMax = o.errorAbsolute ? o.maxError / scale : o.maxError;
|
|
138
177
|
[out, err] = MeshoptSimplifier.simplifySloppy(idx, pos, 3, lock, target, relMax);
|
|
@@ -143,7 +182,7 @@ export function decimateMesh(g, o) {
|
|
|
143
182
|
const flags = [];
|
|
144
183
|
if (o.lockBorder)
|
|
145
184
|
flags.push('LockBorder');
|
|
146
|
-
if (
|
|
185
|
+
if (prune)
|
|
147
186
|
flags.push('Prune');
|
|
148
187
|
if (o.sparse)
|
|
149
188
|
flags.push('Sparse');
|
|
@@ -195,6 +234,51 @@ export function decimateMesh(g, o) {
|
|
|
195
234
|
seenTri.delete(key);
|
|
196
235
|
}
|
|
197
236
|
}
|
|
237
|
+
// prune by points where seams kept meshoptimizer from it: a piece (connected through its points) no larger than the
|
|
238
|
+
// error the reduction reached goes, as meshoptimizer's prune does
|
|
239
|
+
if (o.prune && !prune && !o.sloppy && target < nt * 3 && errorAbs > 0) {
|
|
240
|
+
const parent = new Int32Array(np);
|
|
241
|
+
for (let p = 0; p < np; p++)
|
|
242
|
+
parent[p] = p;
|
|
243
|
+
const find = (x) => { while (parent[x] !== x) {
|
|
244
|
+
parent[x] = parent[parent[x]];
|
|
245
|
+
x = parent[x];
|
|
246
|
+
} return x; };
|
|
247
|
+
for (let i = 0; i < nOut; i++) {
|
|
248
|
+
if (!keepTri[i])
|
|
249
|
+
continue;
|
|
250
|
+
const a = find(rvPt[out[i * 3]]), b = find(rvPt[out[i * 3 + 1]]), c = find(rvPt[out[i * 3 + 2]]);
|
|
251
|
+
if (a !== b)
|
|
252
|
+
parent[a] = b;
|
|
253
|
+
const r = find(b);
|
|
254
|
+
if (find(c) !== r)
|
|
255
|
+
parent[find(c)] = r;
|
|
256
|
+
}
|
|
257
|
+
const box = new Map();
|
|
258
|
+
for (let i = 0; i < nOut * 3; i++) {
|
|
259
|
+
if (!keepTri[(i / 3) | 0])
|
|
260
|
+
continue;
|
|
261
|
+
const p = rvPt[out[i]], r = find(p), q = p * 3;
|
|
262
|
+
const bb = box.get(r);
|
|
263
|
+
if (!bb)
|
|
264
|
+
box.set(r, [P[q], P[q + 1], P[q + 2], P[q], P[q + 1], P[q + 2]]);
|
|
265
|
+
else
|
|
266
|
+
for (let c = 0; c < 3; c++) {
|
|
267
|
+
if (P[q + c] < bb[c])
|
|
268
|
+
bb[c] = P[q + c];
|
|
269
|
+
if (P[q + c] > bb[c + 3])
|
|
270
|
+
bb[c + 3] = P[q + c];
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const small = new Set();
|
|
274
|
+
for (const [r, bb] of box)
|
|
275
|
+
if (Math.hypot(bb[3] - bb[0], bb[4] - bb[1], bb[5] - bb[2]) <= errorAbs)
|
|
276
|
+
small.add(r);
|
|
277
|
+
if (small.size && small.size < box.size)
|
|
278
|
+
for (let i = 0; i < nOut; i++)
|
|
279
|
+
if (keepTri[i] && small.has(find(rvPt[out[i * 3]])))
|
|
280
|
+
keepTri[i] = 0;
|
|
281
|
+
}
|
|
198
282
|
// rebuild: points in input order, passthrough prims, then the decimated triangles
|
|
199
283
|
const used = new Uint8Array(np);
|
|
200
284
|
for (let i = 0; i < out.length; i++)
|
|
@@ -231,7 +315,89 @@ export function decimateMesh(g, o) {
|
|
|
231
315
|
const prim = p1 === p2 ? p1 : p0;
|
|
232
316
|
b.addPrim([map[rvPt[r0]], map[rvPt[r1]], map[rvPt[r2]]], true, prim, [v0, v1, v2]);
|
|
233
317
|
}
|
|
234
|
-
|
|
318
|
+
const geo = b.build(), srcPoint = new Int32Array(geo.numPoints);
|
|
319
|
+
for (let p = 0; p < np; p++)
|
|
320
|
+
if (map[p] >= 0)
|
|
321
|
+
srcPoint[map[p]] = p;
|
|
322
|
+
return { geo, error, errorAbs, trisIn: nt, trisOut, used: steer.used, skipped: steer.skipped, srcPoint };
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* What keeps the polygons of g from reducing freely (decimateMesh keeps every discontinuity of a render vertex): per
|
|
326
|
+
* cause, how many points hold one. Causes: a vertex attribute whose corners differ at a point ('uv seams', 'hard
|
|
327
|
+
* normals (vertex N)', 'vertex <name>'), a string prim attribute or prim group that changes across it ('prim "<name>"
|
|
328
|
+
* borders', 'group "<name>" borders') and 'open borders' (edges used by one polygon). Largest first.
|
|
329
|
+
*/
|
|
330
|
+
export function reductionLocks(g) {
|
|
331
|
+
const vp = g.vertexPoint, np = g.numPoints;
|
|
332
|
+
const vAttrs = [...g.vertices.map].filter(([, a]) => !a.isArray);
|
|
333
|
+
const strs = [...g.prims.map].filter(([, a]) => a.type === 'string' && !a.isArray);
|
|
334
|
+
const grps = [...g.primGroups];
|
|
335
|
+
const vprim = g.vertexPrims();
|
|
336
|
+
// corners of every point (closed polygons only)
|
|
337
|
+
const start = new Int32Array(np + 1);
|
|
338
|
+
for (let pr = 0; pr < g.numPrims; pr++)
|
|
339
|
+
if (g.primClosed[pr] && g.primSize(pr) >= 3)
|
|
340
|
+
for (let v = g.primStart[pr]; v < g.primStart[pr + 1]; v++)
|
|
341
|
+
start[vp[v] + 1]++;
|
|
342
|
+
for (let p = 0; p < np; p++)
|
|
343
|
+
start[p + 1] += start[p];
|
|
344
|
+
const corners = new Int32Array(start[np]), fill = start.slice(0, np);
|
|
345
|
+
for (let pr = 0; pr < g.numPrims; pr++)
|
|
346
|
+
if (g.primClosed[pr] && g.primSize(pr) >= 3)
|
|
347
|
+
for (let v = g.primStart[pr]; v < g.primStart[pr + 1]; v++)
|
|
348
|
+
corners[fill[vp[v]]++] = v;
|
|
349
|
+
const counts = new Map();
|
|
350
|
+
const bump = (k) => counts.set(k, (counts.get(k) ?? 0) + 1);
|
|
351
|
+
for (let p = 0; p < np; p++) {
|
|
352
|
+
const s = start[p], e = start[p + 1];
|
|
353
|
+
if (e - s < 2)
|
|
354
|
+
continue;
|
|
355
|
+
const v0 = corners[s], p0 = vprim[v0];
|
|
356
|
+
for (const [n, a] of vAttrs) {
|
|
357
|
+
for (let k = s + 1; k < e; k++)
|
|
358
|
+
if (vertexAttribsDiffer([a], v0, corners[k])) {
|
|
359
|
+
bump(n === 'uv' ? 'uv seams' : n === 'N' ? 'hard normals (vertex N)' : `vertex ${n}`);
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
for (const [n, a] of strs) {
|
|
364
|
+
const d = a.data;
|
|
365
|
+
for (let k = s + 1; k < e; k++)
|
|
366
|
+
if (d[vprim[corners[k]]] !== d[p0]) {
|
|
367
|
+
bump(`prim "${n}" borders`);
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
for (const [n, m] of grps) {
|
|
372
|
+
for (let k = s + 1; k < e; k++)
|
|
373
|
+
if (m[vprim[corners[k]]] !== m[p0]) {
|
|
374
|
+
bump(`group "${n}" borders`);
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
// open borders: edges used by one polygon
|
|
380
|
+
const edges = new Map();
|
|
381
|
+
for (let pr = 0; pr < g.numPrims; pr++) {
|
|
382
|
+
if (!g.primClosed[pr] || g.primSize(pr) < 3)
|
|
383
|
+
continue;
|
|
384
|
+
const s = g.primStart[pr], e = g.primStart[pr + 1];
|
|
385
|
+
for (let v = s; v < e; v++) {
|
|
386
|
+
const a = vp[v], b = vp[v + 1 < e ? v + 1 : s];
|
|
387
|
+
const key = a < b ? `${a},${b}` : `${b},${a}`;
|
|
388
|
+
edges.set(key, (edges.get(key) ?? 0) + 1);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const borderPts = new Set();
|
|
392
|
+
for (const [k, n] of edges)
|
|
393
|
+
if (n === 1) {
|
|
394
|
+
const [a, b] = k.split(',').map(Number);
|
|
395
|
+
borderPts.add(a);
|
|
396
|
+
borderPts.add(b);
|
|
397
|
+
}
|
|
398
|
+
if (borderPts.size)
|
|
399
|
+
counts.set('open borders', borderPts.size);
|
|
400
|
+
return [...counts].map(([cause, points]) => ({ cause, points })).sort((a, b) => b.points - a.points || (a.cause < b.cause ? -1 : 1));
|
|
235
401
|
}
|
|
236
402
|
/** Thin a point cloud with meshoptimizer's simplifyPoints; kept points keep all attributes. */
|
|
237
403
|
export function decimatePoints(g, o) {
|
|
@@ -267,3 +433,243 @@ export function decimatePoints(g, o) {
|
|
|
267
433
|
keep[i] = 1;
|
|
268
434
|
return { geo: g.subsetPoints(keep), kept: keepIdx.length, used };
|
|
269
435
|
}
|
|
436
|
+
/**
|
|
437
|
+
* Where the uv of g overlaps itself or flips: overlapping triangle pairs (exact, core/uvoverlap.ts) and triangles
|
|
438
|
+
* whose uv winding is opposite to the majority of their chart (charts: triangles joined by uv-continuous edges, so a
|
|
439
|
+
* mirrored island as a whole is not flipped). null without uv.
|
|
440
|
+
*/
|
|
441
|
+
export function uvFolds(g) {
|
|
442
|
+
const uv = readUV(g);
|
|
443
|
+
if (!uv)
|
|
444
|
+
return null;
|
|
445
|
+
const { tris, triPrim } = triangulate(g), nt = triPrim.length;
|
|
446
|
+
const U = trianglesUV(uv, tris), { ok, signed } = uvTriangleAreas(U, nt);
|
|
447
|
+
const root = chartRoots(g.vertexPoint, tris, U, nt), sum = new Map();
|
|
448
|
+
for (let t = 0; t < nt; t++)
|
|
449
|
+
if (ok[t])
|
|
450
|
+
sum.set(root(t), (sum.get(root(t)) ?? 0) + Math.sign(signed[t]));
|
|
451
|
+
const bad = new Uint8Array(nt), valid = [];
|
|
452
|
+
let flipped = 0, pairs = 0, big = 0, largest = null;
|
|
453
|
+
for (let t = 0; t < nt; t++) {
|
|
454
|
+
if (!ok[t])
|
|
455
|
+
continue;
|
|
456
|
+
valid.push(t);
|
|
457
|
+
if (Math.sign(signed[t]) * (sum.get(root(t)) ?? 0) < 0) {
|
|
458
|
+
bad[t] = 1;
|
|
459
|
+
flipped++;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
uvOverlapPairs(U, valid, signed, (a, b, area) => { pairs++; bad[a] = 1; bad[b] = 1; if (area > big) {
|
|
463
|
+
big = area;
|
|
464
|
+
largest = [a, b];
|
|
465
|
+
} });
|
|
466
|
+
return { pairs, flipped, bad, largest, tris, triPrim, uv };
|
|
467
|
+
}
|
|
468
|
+
/** Steps a reduction that folds the uv is split into, and the lock rings tried (in turn) on a step that folds. */
|
|
469
|
+
const KEEP_UV_STEPS = 8;
|
|
470
|
+
const KEEP_UV_RINGS = [1, 1, 2, 2, 3, 4];
|
|
471
|
+
/** Points of g's prims around each point: CSR start / prim lists. */
|
|
472
|
+
function pointPrims(g) {
|
|
473
|
+
const np = g.numPoints, vp = g.vertexPoint, start = new Int32Array(np + 1);
|
|
474
|
+
for (let v = 0; v < g.numVertices; v++)
|
|
475
|
+
start[vp[v] + 1]++;
|
|
476
|
+
for (let p = 0; p < np; p++)
|
|
477
|
+
start[p + 1] += start[p];
|
|
478
|
+
const fill = start.slice(0, np), prims = new Int32Array(g.numVertices), vprim = g.vertexPrims();
|
|
479
|
+
for (let v = 0; v < g.numVertices; v++)
|
|
480
|
+
prims[fill[vp[v]]++] = vprim[v];
|
|
481
|
+
return { start, prims };
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Lock the points of `cur` that res removed within `rings` rings (through shared prims) of the input points of res's
|
|
485
|
+
* folded triangles. Returns how many points were newly locked.
|
|
486
|
+
*/
|
|
487
|
+
function lockAroundFolds(cur, res, f, rings, lock) {
|
|
488
|
+
const adj = pointPrims(cur), vp = cur.vertexPoint, seen = new Uint8Array(cur.numPoints);
|
|
489
|
+
let front = [], added = 0;
|
|
490
|
+
// only points the step removed are locked: a kept point did not make the fold, and locking it would only hold
|
|
491
|
+
// the next run back
|
|
492
|
+
const alive = new Uint8Array(cur.numPoints);
|
|
493
|
+
for (let i = 0; i < res.srcPoint.length; i++)
|
|
494
|
+
alive[res.srcPoint[i]] = 1;
|
|
495
|
+
const take = (p) => { if (!lock[p] && !alive[p]) {
|
|
496
|
+
lock[p] = 1;
|
|
497
|
+
added++;
|
|
498
|
+
} };
|
|
499
|
+
for (let t = 0; t < f.bad.length; t++) {
|
|
500
|
+
if (!f.bad[t])
|
|
501
|
+
continue;
|
|
502
|
+
for (let k = 0; k < 3; k++) {
|
|
503
|
+
const p = res.srcPoint[res.geo.vertexPoint[f.tris[t * 3 + k]]];
|
|
504
|
+
if (!seen[p]) {
|
|
505
|
+
seen[p] = 1;
|
|
506
|
+
front.push(p);
|
|
507
|
+
take(p);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
for (let d = 1; d <= rings; d++) {
|
|
512
|
+
const next = [];
|
|
513
|
+
for (const p of front)
|
|
514
|
+
for (let i = adj.start[p]; i < adj.start[p + 1]; i++) {
|
|
515
|
+
const pr = adj.prims[i];
|
|
516
|
+
for (let v = cur.primStart[pr]; v < cur.primStart[pr + 1]; v++) {
|
|
517
|
+
const q = vp[v];
|
|
518
|
+
if (seen[q])
|
|
519
|
+
continue;
|
|
520
|
+
seen[q] = 1;
|
|
521
|
+
next.push(q);
|
|
522
|
+
take(q);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
front = next;
|
|
526
|
+
}
|
|
527
|
+
return added;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* How far a reduction of g strays from it: `to(m, srcOf)` is the largest distance (scene units) from a point of g's
|
|
531
|
+
* polygons to the surface of m (srcOf: the g point of each m point), over the pieces of g (connected through shared
|
|
532
|
+
* points) that kept a point in m; a piece pruned away whole is left out, as meshoptimizer's prune allows. `scale` is
|
|
533
|
+
* meshoptimizer's mesh extent, the unit of relative errors.
|
|
534
|
+
*/
|
|
535
|
+
function deviationFrom(g) {
|
|
536
|
+
const np = g.numPoints, vp = g.vertexPoint, P = g.P, parent = new Int32Array(np), inPoly = new Uint8Array(np);
|
|
537
|
+
for (let p = 0; p < np; p++)
|
|
538
|
+
parent[p] = p;
|
|
539
|
+
const find = (x) => { while (parent[x] !== x) {
|
|
540
|
+
parent[x] = parent[parent[x]];
|
|
541
|
+
x = parent[x];
|
|
542
|
+
} return x; };
|
|
543
|
+
for (let pr = 0; pr < g.numPrims; pr++) {
|
|
544
|
+
if (!g.primClosed[pr] || g.primSize(pr) < 3)
|
|
545
|
+
continue;
|
|
546
|
+
for (let v = g.primStart[pr]; v < g.primStart[pr + 1]; v++) {
|
|
547
|
+
inPoly[vp[v]] = 1;
|
|
548
|
+
const a = find(vp[v]), b = find(vp[g.primStart[pr]]);
|
|
549
|
+
if (a !== b)
|
|
550
|
+
parent[a] = b;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
const pts = [];
|
|
554
|
+
for (let p = 0; p < np; p++)
|
|
555
|
+
if (inPoly[p])
|
|
556
|
+
pts.push(p);
|
|
557
|
+
const pos = new Float32Array(pts.length * 3);
|
|
558
|
+
pts.forEach((p, i) => { pos[i * 3] = P[p * 3]; pos[i * 3 + 1] = P[p * 3 + 1]; pos[i * 3 + 2] = P[p * 3 + 2]; });
|
|
559
|
+
const scale = (pts.length ? MeshoptSimplifier.getScale(pos, 3) : 0) || 1;
|
|
560
|
+
return {
|
|
561
|
+
scale,
|
|
562
|
+
to: (m, srcOf) => {
|
|
563
|
+
const alive = new Uint8Array(np);
|
|
564
|
+
for (let i = 0; i < srcOf.length; i++)
|
|
565
|
+
alive[find(srcOf[i])] = 1;
|
|
566
|
+
const bvh = new TriBVH(m), s = closestScratch();
|
|
567
|
+
let worst = 0;
|
|
568
|
+
for (const p of pts)
|
|
569
|
+
if (alive[find(p)] && bvh.closestInto(P[p * 3], P[p * 3 + 1], P[p * 3 + 2], Infinity, s) >= 0 && s.d2 > worst)
|
|
570
|
+
worst = s.d2;
|
|
571
|
+
return Math.sqrt(worst);
|
|
572
|
+
},
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* decimateMesh that keeps a clean uv atlas clean. meshoptimizer rejects flips in 3D only, and a kept corner keeps its
|
|
577
|
+
* uv, so a collapse can fold the uv around it (most of all where the uv map bends: bevels, curved chart borders).
|
|
578
|
+
* When the input uv has no overlaps and the plain decimation folds it (uvFolds), the reduction runs again in up to
|
|
579
|
+
* KEEP_UV_STEPS steps, the triangle count falling by the same factor each step. A step that folds the uv runs again
|
|
580
|
+
* with the points it removed around its folds locked, the rings growing (KEEP_UV_RINGS); a step that still folds is
|
|
581
|
+
* dropped (its locks stay for the next try on the same mesh; a step taken starts the next one with only the caller's
|
|
582
|
+
* locks, since the danger moves with the mesh). Every step taken is checked, so the result has no folds the input did
|
|
583
|
+
* not have; the price is a triangle count above the target where the uv held the reduction (kept.plainTris says what
|
|
584
|
+
* a plain reduction reached). keep false: the plain reduction, unchecked.
|
|
585
|
+
*/
|
|
586
|
+
export function decimateKeepUV(g, o, keep = true) {
|
|
587
|
+
const plain = decimateMesh(g, o);
|
|
588
|
+
const kept = { checked: false, steps: 1, dropped: 0, runs: 1, locked: 0, plainTris: plain.trisOut, plainFolds: null, plainGeo: null };
|
|
589
|
+
const before = !keep || o.sloppy || o.group || plain.trisOut >= plain.trisIn ? null : uvFolds(g);
|
|
590
|
+
if (!before || before.pairs > 0)
|
|
591
|
+
return { ...plain, uv: kept };
|
|
592
|
+
kept.checked = true;
|
|
593
|
+
const clean = (f) => f.pairs === 0 && f.flipped <= before.flipped;
|
|
594
|
+
const plainFolds = uvFolds(plain.geo);
|
|
595
|
+
if (clean(plainFolds))
|
|
596
|
+
return { ...plain, uv: kept };
|
|
597
|
+
kept.plainFolds = plainFolds;
|
|
598
|
+
kept.plainGeo = plain.geo;
|
|
599
|
+
kept.steps = 0;
|
|
600
|
+
const target = Math.max(1, o.targetCount > 0 ? Math.floor(o.targetCount) : Math.floor(plain.trisIn * Math.max(0, Math.min(1, o.ratio))));
|
|
601
|
+
const userLock = (srcPts) => Uint8Array.from(srcPts, (p) => (o.lockPoints && o.lockPoints[p] ? 1 : 0));
|
|
602
|
+
let srcOf = Int32Array.from({ length: g.numPoints }, (_, i) => i), cur = g, curLock = userLock(srcOf);
|
|
603
|
+
let curTris = plain.trisIn, err = 0, errAbs = 0, last = null;
|
|
604
|
+
// max_error holds for the whole reduction: each step may use all of it, and a step taken must keep every input point
|
|
605
|
+
// within it of the output surface (measured, since step errors do not simply add up)
|
|
606
|
+
const dev = deviationFrom(g), limit = o.errorAbsolute ? o.maxError : o.maxError * dev.scale;
|
|
607
|
+
for (let step = 1; step <= KEEP_UV_STEPS && curTris > target; step++) {
|
|
608
|
+
const left = KEEP_UV_STEPS - step + 1;
|
|
609
|
+
const stepTarget = step === KEEP_UV_STEPS ? target : Math.max(target, Math.round(curTris * Math.pow(target / curTris, 1 / left)));
|
|
610
|
+
const so = { ...o, ratio: 1, targetCount: stepTarget, group: null };
|
|
611
|
+
const lock = curLock.slice();
|
|
612
|
+
let res = null;
|
|
613
|
+
for (let attempt = 0;; attempt++) {
|
|
614
|
+
const r = decimateMesh(cur, { ...so, lockPoints: lock });
|
|
615
|
+
kept.runs++;
|
|
616
|
+
const f = uvFolds(r.geo);
|
|
617
|
+
if (clean(f)) {
|
|
618
|
+
res = r;
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
if (attempt >= KEEP_UV_RINGS.length)
|
|
622
|
+
break;
|
|
623
|
+
kept.locked += lockAroundFolds(cur, r, f, KEEP_UV_RINGS[attempt], lock);
|
|
624
|
+
}
|
|
625
|
+
if (!res) {
|
|
626
|
+
kept.dropped++;
|
|
627
|
+
curLock = lock;
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
const nextSrc = Int32Array.from(res.srcPoint, (p) => srcOf[p]), d = dev.to(res.geo, nextSrc);
|
|
631
|
+
if (d > limit * (1 + 1e-9))
|
|
632
|
+
break;
|
|
633
|
+
// the step's output is the next step's input, with its input points and the caller's locks
|
|
634
|
+
srcOf = nextSrc;
|
|
635
|
+
curLock = userLock(srcOf);
|
|
636
|
+
errAbs = d;
|
|
637
|
+
err = d / dev.scale;
|
|
638
|
+
const progress = res.trisOut < curTris;
|
|
639
|
+
cur = res.geo;
|
|
640
|
+
curTris = res.trisOut;
|
|
641
|
+
last = res;
|
|
642
|
+
kept.steps++;
|
|
643
|
+
if (!progress)
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
// no step taken (each one folded the uv): the input, triangulated
|
|
647
|
+
if (!last) {
|
|
648
|
+
const none = decimateMesh(g, { ...o, ratio: 1, targetCount: 0, prune: false });
|
|
649
|
+
kept.runs++;
|
|
650
|
+
return { ...none, uv: kept };
|
|
651
|
+
}
|
|
652
|
+
return { ...last, error: err, errorAbs: errAbs, trisIn: plain.trisIn, srcPoint: srcOf, uv: kept };
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Where and why decimateKeepUV held the reduction back, in words (the parts a plain reduction folds and its largest
|
|
656
|
+
* fold), or '' when it did not: the plain reduction kept the uv clean, or the steps reached about the same count.
|
|
657
|
+
*/
|
|
658
|
+
export function uvKeptText(r) {
|
|
659
|
+
const k = r.uv, f = k.plainFolds, pg = k.plainGeo;
|
|
660
|
+
if (!f || !pg || r.trisOut <= k.plainTris * 1.02 + 2)
|
|
661
|
+
return '';
|
|
662
|
+
const badPrims = [];
|
|
663
|
+
let firstBad = -1;
|
|
664
|
+
for (let t = 0; t < f.bad.length; t++)
|
|
665
|
+
if (f.bad[t]) {
|
|
666
|
+
badPrims.push(f.triPrim[t]);
|
|
667
|
+
if (firstBad < 0)
|
|
668
|
+
firstBad = t;
|
|
669
|
+
}
|
|
670
|
+
const parts = partsOf(pg, badPrims);
|
|
671
|
+
const where = f.largest ? uvPairText(pg, f.uv, f.tris, f.triPrim, f.largest[0], f.largest[1]) : firstBad >= 0 ? primWhere(pg, f.triPrim[firstBad]) : '';
|
|
672
|
+
const n = (x) => x.toLocaleString('en-US');
|
|
673
|
+
return `a plain reduction to ${n(k.plainTris)} triangles folds the uv (${n(f.pairs)} overlapping triangle pairs, ${n(f.flipped)} flipped triangles${parts ? `; parts ${parts}` : ''}; largest: ${where}), ` +
|
|
674
|
+
`so it went in ${k.steps} steps that keep the uv clean${k.dropped ? ` (${k.dropped} more folded and were dropped)` : ''}, to ${n(r.trisOut)} triangles`;
|
|
675
|
+
}
|
package/dist/core/erosion.d.ts
CHANGED
|
@@ -51,6 +51,43 @@ export interface ErosionParams {
|
|
|
51
51
|
boundary: 'closed' | 'open';
|
|
52
52
|
}
|
|
53
53
|
export declare const DEFAULT_EROSION: ErosionParams;
|
|
54
|
+
/**
|
|
55
|
+
* Droplet erosion settings in world units (heightfield_erode's params): rain per square metre, lengths in metres, and
|
|
56
|
+
* per-step settings defined for a droplet step of erosionRadius / 3 metres. dropletParams turns them into the
|
|
57
|
+
* per-sample ErosionParams the kernels take for one cell size.
|
|
58
|
+
*/
|
|
59
|
+
export interface DropletSettings extends Omit<ErosionParams, 'rainDensity' | 'erosionRadius' | 'maxLifetime'> {
|
|
60
|
+
/** droplets per square metre per iteration */
|
|
61
|
+
rainDensity: number;
|
|
62
|
+
/** erosion brush radius in metres; a third of it is the droplet step the per-step settings refer to */
|
|
63
|
+
erosionRadius: number;
|
|
64
|
+
/** the farthest a droplet travels, in metres */
|
|
65
|
+
maxDistance: number;
|
|
66
|
+
}
|
|
67
|
+
/** The kernel limits dropletParams applied, each with the value it used (in samples). */
|
|
68
|
+
export interface DropletLimits {
|
|
69
|
+
radius?: number;
|
|
70
|
+
steps?: number;
|
|
71
|
+
density?: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The per-sample ErosionParams that erode like `s` on a field with cells of `cell` metres, so the same settings give
|
|
75
|
+
* the same erosion in metres at any spacing (within what the coarser grid can resolve).
|
|
76
|
+
*
|
|
77
|
+
* The kernels move a droplet one cell per step and count in samples. The settings are defined for steps of
|
|
78
|
+
* step = erosionRadius / 3 metres (the default radius of three cells runs them as given), and a cell of `cell` metres
|
|
79
|
+
* is r = cell / step of those steps: the per-step fractions compound (f → 1 − (1 − f)^r, inertia i → i^r), the speed²
|
|
80
|
+
* gained on a slope grows with the drop (gravity × r), the sediment a droplet carries keeps its volume in metres
|
|
81
|
+
* (sediment capacity is a volume in cells³: ÷ r³), and a thermal pass moves the same height per iteration (rate ÷ r,
|
|
82
|
+
* at most 1). Counts become samples: the brush erosionRadius / cell samples (1..64), maxDistance / cell steps
|
|
83
|
+
* (1..4096) and rainDensity × cell² droplets per sample (at most 1); `limited` reports the limits that applied.
|
|
84
|
+
* Ratios within 1e-9 of a whole number are that number, so the defaults at 0.1 m cells give the kernel defaults bit
|
|
85
|
+
* for bit.
|
|
86
|
+
*/
|
|
87
|
+
export declare function dropletParams(s: DropletSettings, cell: number): {
|
|
88
|
+
params: ErosionParams;
|
|
89
|
+
limited: DropletLimits;
|
|
90
|
+
};
|
|
54
91
|
/** A fresh field for erosion with zeroed accumulators (heights are copied). */
|
|
55
92
|
export declare function makeErosionField(width: number, height: number, cellSize: number, h: ArrayLike<number>, hardness?: Float32Array | null, freeze?: Float32Array | null): ErosionField;
|
|
56
93
|
/** One erosion iteration: a rain pass of droplets, then one thermal pass. Updates the field in place. */
|
package/dist/core/erosion.js
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
// One iteration = erodeStep(field, params, rng): a rain pass of rain_density × samples droplets, then one thermal
|
|
12
12
|
// pass. It reads and writes plain typed arrays only, so a native/WASM kernel can replace it: same arrays in, the
|
|
13
13
|
// same arrays updated in place, the same parameter meanings (see ErosionField / ErosionParams). The rng is only
|
|
14
|
-
// used to draw droplet start positions and the tile order.
|
|
14
|
+
// used to draw droplet start positions and the tile order. ErosionParams count in samples (a droplet moves one cell
|
|
15
|
+
// per step); dropletParams converts heightfield_erode's settings in metres to them for a field's cell size.
|
|
15
16
|
//
|
|
16
17
|
// Performance: the droplet pass is bound by memory latency, so droplets run tile by tile (32 × 32 samples, tiles
|
|
17
18
|
// in a random order per pass) and the net change is recorded once per pass instead of per write. The thermal pass
|
|
@@ -22,6 +23,45 @@ export const DEFAULT_EROSION = {
|
|
|
22
23
|
inertia: 0.1, gravity: 4, minSlope: 0.01, erosionRadius: 3, maxLifetime: 30, talusAngle: 40, thermalRate: 0.1,
|
|
23
24
|
boundary: 'closed',
|
|
24
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* The per-sample ErosionParams that erode like `s` on a field with cells of `cell` metres, so the same settings give
|
|
28
|
+
* the same erosion in metres at any spacing (within what the coarser grid can resolve).
|
|
29
|
+
*
|
|
30
|
+
* The kernels move a droplet one cell per step and count in samples. The settings are defined for steps of
|
|
31
|
+
* step = erosionRadius / 3 metres (the default radius of three cells runs them as given), and a cell of `cell` metres
|
|
32
|
+
* is r = cell / step of those steps: the per-step fractions compound (f → 1 − (1 − f)^r, inertia i → i^r), the speed²
|
|
33
|
+
* gained on a slope grows with the drop (gravity × r), the sediment a droplet carries keeps its volume in metres
|
|
34
|
+
* (sediment capacity is a volume in cells³: ÷ r³), and a thermal pass moves the same height per iteration (rate ÷ r,
|
|
35
|
+
* at most 1). Counts become samples: the brush erosionRadius / cell samples (1..64), maxDistance / cell steps
|
|
36
|
+
* (1..4096) and rainDensity × cell² droplets per sample (at most 1); `limited` reports the limits that applied.
|
|
37
|
+
* Ratios within 1e-9 of a whole number are that number, so the defaults at 0.1 m cells give the kernel defaults bit
|
|
38
|
+
* for bit.
|
|
39
|
+
*/
|
|
40
|
+
export function dropletParams(s, cell) {
|
|
41
|
+
const snap = (x) => (Math.abs(x - Math.round(x)) < 1e-9 ? Math.round(x) : x);
|
|
42
|
+
const unit = (x) => Math.min(1, Math.max(0, x));
|
|
43
|
+
const r = snap(cell / (s.erosionRadius / 3));
|
|
44
|
+
const perStep = (f) => (r === 1 ? f : 1 - Math.pow(1 - unit(f), r));
|
|
45
|
+
const limited = {};
|
|
46
|
+
let radius = Math.max(1, snap(s.erosionRadius / cell));
|
|
47
|
+
if (radius > 64)
|
|
48
|
+
radius = limited.radius = 64;
|
|
49
|
+
let steps = Math.max(1, Math.round(s.maxDistance / cell));
|
|
50
|
+
if (steps > 4096)
|
|
51
|
+
steps = limited.steps = 4096;
|
|
52
|
+
let density = s.rainDensity * cell * cell;
|
|
53
|
+
if (density > 1)
|
|
54
|
+
density = limited.density = 1;
|
|
55
|
+
const params = {
|
|
56
|
+
rainDensity: density, rainAmount: s.rainAmount,
|
|
57
|
+
erosionRate: perStep(s.erosionRate), depositionRate: perStep(s.depositionRate), evaporation: perStep(s.evaporation),
|
|
58
|
+
sedimentCapacity: Math.min(1e6, s.sedimentCapacity / (r * r * r)),
|
|
59
|
+
inertia: r === 1 ? s.inertia : Math.pow(unit(s.inertia), r),
|
|
60
|
+
gravity: Math.min(1e6, s.gravity * r), minSlope: s.minSlope, erosionRadius: radius, maxLifetime: steps,
|
|
61
|
+
talusAngle: s.talusAngle, thermalRate: Math.min(1, s.thermalRate / r), boundary: s.boundary,
|
|
62
|
+
};
|
|
63
|
+
return { params, limited };
|
|
64
|
+
}
|
|
25
65
|
/** A fresh field for erosion with zeroed accumulators (heights are copied). */
|
|
26
66
|
export function makeErosionField(width, height, cellSize, h, hardness = null, freeze = null) {
|
|
27
67
|
const n = width * height;
|
package/dist/core/geometry.d.ts
CHANGED
|
@@ -195,6 +195,10 @@ export interface ImageVolume {
|
|
|
195
195
|
export type Volume = SdfVolume | GridVolume | HeightfieldVolume | ImageVolume;
|
|
196
196
|
/** Volumes for a new geometry: heightfields and images get their own layer map (the arrays stay shared), SDFs are immutable. */
|
|
197
197
|
export declare function cloneVolumes(vs: Volume[]): Volume[];
|
|
198
|
+
/** Two images with the same name, size and layers, every layer equal value for value (shared arrays compare at once). */
|
|
199
|
+
export declare function sameImage(a: ImageVolume, b: ImageVolume): boolean;
|
|
200
|
+
/** Two images of the same size and layers, every layer equal value for value, whatever their names. */
|
|
201
|
+
export declare function samePixels(a: ImageVolume, b: ImageVolume): boolean;
|
|
198
202
|
export interface GeoInit {
|
|
199
203
|
positions?: ArrayLike<number>;
|
|
200
204
|
prims?: ArrayLike<number>[];
|
|
@@ -331,7 +335,7 @@ export declare class Geometry {
|
|
|
331
335
|
* Concatenate geometries. Attributes are unioned; missing values take attribute defaults.
|
|
332
336
|
* Display attributes (Cd, Alpha, N, uv) that live on different classes in different inputs
|
|
333
337
|
* are promoted to vertex attributes first, so every input keeps its look (a point Cd from one
|
|
334
|
-
* input never masks a prim Cd from another).
|
|
338
|
+
* input never masks a prim Cd from another). An image that several inputs carry (same name and pixels) is kept once.
|
|
335
339
|
*/
|
|
336
340
|
static merge(geos: Geometry[]): Geometry;
|
|
337
341
|
}
|