topotopo 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +40 -10
- package/README.md +475 -111
- package/dist/cli.js +328 -133
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/analysis.d.ts +5 -1
- package/dist/core/analysis.js +8 -3
- package/dist/core/bake.d.ts +16 -8
- package/dist/core/bake.js +165 -29
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +238 -97
- 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 +204 -4
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/imageops.d.ts +6 -0
- package/dist/core/imageops.js +51 -0
- 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 +11 -0
- package/dist/core/materials.js +56 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/packed.js +11 -0
- package/dist/core/particles.d.ts +1 -1
- package/dist/core/particles.js +1 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/rbd.js +13 -1
- 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/scatter.d.ts +1 -1
- package/dist/core/scatter.js +1 -1
- package/dist/core/scene.d.ts +20 -0
- package/dist/core/scene.js +72 -0
- 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 +15 -2
- package/dist/core/snippet-lib.js +40 -14
- package/dist/core/solidify.d.ts +38 -0
- package/dist/core/solidify.js +356 -0
- package/dist/core/tangents.d.ts +54 -0
- package/dist/core/tangents.js +218 -0
- package/dist/core/texcoord.d.ts +4 -0
- package/dist/core/texcoord.js +14 -0
- 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 +32 -0
- package/dist/core/topology.js +238 -7
- package/dist/core/traceimage.d.ts +7 -0
- package/dist/core/traceimage.js +221 -0
- package/dist/core/tritri.d.ts +5 -0
- package/dist/core/tritri.js +120 -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 +15 -16
- package/dist/core/uv.js +36 -189
- 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 +127 -0
- package/dist/core/uvreport.js +372 -0
- 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 +95 -61
- 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 +5 -3
- package/dist/index.js +3 -2
- package/dist/inspect.d.ts +84 -1
- package/dist/inspect.js +271 -24
- package/dist/io/common.d.ts +33 -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 +355 -51
- 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 +149 -14
- 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 +816 -119
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +70 -24
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +207 -108
- 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 +207 -83
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +54 -5
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +22 -10
- package/dist/nodes/curves.js +440 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +13 -39
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.d.ts +1 -0
- package/dist/nodes/imageops.js +203 -0
- package/dist/nodes/index.d.ts +4 -0
- package/dist/nodes/index.js +4 -0
- package/dist/nodes/material.js +91 -23
- package/dist/nodes/measure.js +45 -18
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/rbd.js +1 -1
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.d.ts +1 -0
- package/dist/nodes/scene.js +55 -0
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/solidify.d.ts +1 -0
- package/dist/nodes/solidify.js +52 -0
- 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 +11 -3
- package/dist/nodes/util.d.ts +1 -1
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +6 -2
- 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/pbr.d.ts +28 -0
- package/dist/render/pbr.js +378 -0
- package/dist/render/preview.d.ts +69 -0
- package/dist/render/preview.js +26 -0
- package/dist/render/raster.d.ts +78 -7
- package/dist/render/raster.js +600 -145
- package/dist/render/texview.d.ts +7 -1
- package/dist/render/texview.js +34 -2
- 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/camera.js +1 -1
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +100 -0
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +6 -2
- package/dist/viewer/app/guides.js +123 -1
- package/dist/viewer/app/keymap.js +7 -4
- package/dist/viewer/app/layers.js +5 -5
- package/dist/viewer/app/main.js +186 -53
- package/dist/viewer/app/markers.js +61 -1
- 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 +177 -53
- package/dist/viewer/app/scene.js +35 -6
- package/dist/viewer/app/select.js +5 -4
- package/dist/viewer/app/selection.js +1 -1
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +66 -8
- package/dist/viewer/app/units.js +1 -1
- package/dist/viewer/app/vars.js +48 -1
- 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 +24 -11
- 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 +24 -7
- package/dist/viewer/server.js +289 -31
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/web/bake.d.ts +34 -0
- package/dist/web/bake.js +153 -0
- 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 -105
- package/dist/workspace.js +359 -375
- 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 +23 -29
- package/kernels/pkg-simd/README.md +23 -29
- 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 +23 -8
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/shelf/index.json +0 -190
package/dist/core/texture.js
CHANGED
|
@@ -33,6 +33,7 @@ export function noiseRange(o, out, start, end) {
|
|
|
33
33
|
* edges: 0 on the cell borders rising to 1 at edgeWidth inside (cells as plates)
|
|
34
34
|
* id: a random value per cell (flat, for per-cell variation)
|
|
35
35
|
* crackle: F2 - F1 (bright ridges between cells), normalised to about 0..1
|
|
36
|
+
* crystal: a flat facet per cell: a random tilt (a planar gradient through the site) on a random level
|
|
36
37
|
*/
|
|
37
38
|
export function cellsRange(o, C, id, start, end) {
|
|
38
39
|
const w = o.width, h = o.height, aspect = h / w;
|
|
@@ -42,7 +43,7 @@ export function cellsRange(o, C, id, start, end) {
|
|
|
42
43
|
fy = Math.max(1, Math.round(fy));
|
|
43
44
|
}
|
|
44
45
|
const c = cellular2(o.seed * 131 + 7, o.tileable ? fx : 0, o.tileable ? fy : 0, o.jitter, o.metric);
|
|
45
|
-
const hit = { f1: 0, f2: 0, id: 0, cx: 0, cy: 0 };
|
|
46
|
+
const hit = { f1: 0, f2: 0, id: 0, cx: 0, cy: 0, ex: 0, ey: 0 };
|
|
46
47
|
const ew = Math.max(1e-6, o.edgeWidth);
|
|
47
48
|
for (let k = start; k < end; k++) {
|
|
48
49
|
const i = k % w, j = (k - i) / w;
|
|
@@ -60,6 +61,12 @@ export function cellsRange(o, C, id, start, end) {
|
|
|
60
61
|
case 'crackle':
|
|
61
62
|
v = clamp01((hit.f2 - hit.f1) / 0.7);
|
|
62
63
|
break;
|
|
64
|
+
case 'crystal': {
|
|
65
|
+
const a = (hash32(hit.id ^ 0x2f6b9a17) / 4294967296) * 2 * Math.PI;
|
|
66
|
+
// ex, ey run from the pixel to the site: the facet rises along (cos a, sin a) through the site
|
|
67
|
+
v = clamp01(0.5 + (r - 0.5) * 0.5 - 0.8 * (hit.ex * Math.cos(a) + hit.ey * Math.sin(a)));
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
63
70
|
default: v = clamp01(hit.f1 / 0.85);
|
|
64
71
|
}
|
|
65
72
|
C[k] = v;
|
|
@@ -403,6 +410,24 @@ export function histogramScanFn(position, contrast) {
|
|
|
403
410
|
const width = Math.max(1e-4, 1 - clamp01(contrast));
|
|
404
411
|
return (x) => clamp01((x - (1 - position)) / width + 0.5);
|
|
405
412
|
}
|
|
413
|
+
/**
|
|
414
|
+
* The value cut that leaves `coverage` (0..1) of the samples above it: the (1 - coverage) quantile, midway between the
|
|
415
|
+
* two sorted samples it separates. Coverage 0 and 1 put it a transition width (from contrast) beyond the range, so
|
|
416
|
+
* nothing or everything turns white. Equal samples straddling the cut read 0.5 (not white).
|
|
417
|
+
*/
|
|
418
|
+
export function coverageCut(a, coverage, contrast) {
|
|
419
|
+
const n = a.length;
|
|
420
|
+
if (!n)
|
|
421
|
+
return 0.5;
|
|
422
|
+
const s = Float32Array.from(a).sort();
|
|
423
|
+
const width = Math.max(1e-4, 1 - clamp01(contrast));
|
|
424
|
+
const black = n - Math.round(clamp01(coverage) * n);
|
|
425
|
+
if (black >= n)
|
|
426
|
+
return s[n - 1] + width;
|
|
427
|
+
if (black <= 0)
|
|
428
|
+
return s[0] - width;
|
|
429
|
+
return (s[black - 1] + s[black]) / 2;
|
|
430
|
+
}
|
|
406
431
|
// ---------------------------------------------------------------- neighbourhood filters
|
|
407
432
|
/**
|
|
408
433
|
* One box pass of half-width r along rows (horizontal) or columns, every channel, wrapping or clamping at borders:
|
|
@@ -493,13 +518,13 @@ function sobel(g, w, h, i, j, wrap) {
|
|
|
493
518
|
SB[1] = (p + 2 * q + s - a - 2 * b - c) / 8;
|
|
494
519
|
}
|
|
495
520
|
/**
|
|
496
|
-
* Tangent-space normal map (RGBA, 0.5 + 0.5 n) from a grey height
|
|
497
|
-
*
|
|
498
|
-
* unless directx.
|
|
521
|
+
* Tangent-space normal map (RGBA, 0.5 + 0.5 n) from a grey height in physical units: a value of 1 stands `depth`
|
|
522
|
+
* metres high and the image width spans `size` metres, so the slope is the Sobel height step per texel × depth /
|
|
523
|
+
* (size / w), the same at any resolution for the same relief. OpenGL convention (+Y up = +v) unless directx.
|
|
499
524
|
*/
|
|
500
|
-
export function normalFromHeight(g, w, h,
|
|
525
|
+
export function normalFromHeight(g, w, h, depth, size, directx, wrap) {
|
|
501
526
|
const out = new Float32Array(w * h * 4);
|
|
502
|
-
const k = (
|
|
527
|
+
const k = (depth * w) / Math.max(1e-12, size); // Sobel / 8 is the value step per texel; × depth / texel size = metres per metre
|
|
503
528
|
for (let j = 0; j < h; j++)
|
|
504
529
|
for (let i = 0; i < w; i++) {
|
|
505
530
|
sobel(g, w, h, i, j, wrap);
|
|
@@ -517,6 +542,71 @@ export function normalFromHeight(g, w, h, strength, directx, wrap) {
|
|
|
517
542
|
}
|
|
518
543
|
return out;
|
|
519
544
|
}
|
|
545
|
+
export const NORMAL_BLEND_MODES = ['rnm', 'udn', 'linear'];
|
|
546
|
+
/**
|
|
547
|
+
* A detail normal map over a base one (tangent space, RGBA holding 0.5 + 0.5 n; the base's alpha is kept). The
|
|
548
|
+
* detail's tilt is scaled by `strength` (its x and y; 0 = flat, 2 = twice as steep), then:
|
|
549
|
+
* rnm reoriented normal mapping (Barré-Brisebois and Hill, 2012): the detail is rotated onto the base normal, so
|
|
550
|
+
* both reliefs keep their full depth (a flat detail returns the base exactly)
|
|
551
|
+
* udn (base.xy + detail.xy, base.z) normalised: cheaper, flattens a strong detail on steep base slopes
|
|
552
|
+
* linear (base + detail) normalised: softens both (a flat detail halves the base's tilt)
|
|
553
|
+
* The mask (0..1 per pixel, or none = 1) mixes from the base to that result. Both maps must use the same green
|
|
554
|
+
* convention (OpenGL or DirectX): every mode treats x and y alike, so the result keeps it.
|
|
555
|
+
*/
|
|
556
|
+
export function blendNormals(A, B, M, mode, strength) {
|
|
557
|
+
const n = A.length / 4, out = new Float32Array(A.length);
|
|
558
|
+
for (let i = 0; i < n; i++) {
|
|
559
|
+
const o = i * 4;
|
|
560
|
+
let bx = A[o] * 2 - 1, by = A[o + 1] * 2 - 1, bz = A[o + 2] * 2 - 1;
|
|
561
|
+
const bl = Math.hypot(bx, by, bz) || 1;
|
|
562
|
+
bx /= bl;
|
|
563
|
+
by /= bl;
|
|
564
|
+
bz /= bl;
|
|
565
|
+
let dx = (B[o] * 2 - 1) * strength, dy = (B[o + 1] * 2 - 1) * strength, dz = Math.max(1e-4, B[o + 2] * 2 - 1);
|
|
566
|
+
const dl = Math.hypot(dx, dy, dz);
|
|
567
|
+
dx /= dl;
|
|
568
|
+
dy /= dl;
|
|
569
|
+
dz /= dl;
|
|
570
|
+
let rx, ry, rz;
|
|
571
|
+
if (mode === 'udn') {
|
|
572
|
+
rx = bx + dx;
|
|
573
|
+
ry = by + dy;
|
|
574
|
+
rz = bz;
|
|
575
|
+
}
|
|
576
|
+
else if (mode === 'linear') {
|
|
577
|
+
rx = bx + dx;
|
|
578
|
+
ry = by + dy;
|
|
579
|
+
rz = bz + dz;
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
// t = base + (0, 0, 1), u = detail × (-1, -1, 1), r = t (t · u) / t.z - u
|
|
583
|
+
const tx = bx, ty = by, tz = bz + 1, ux = -dx, uy = -dy, uz = dz;
|
|
584
|
+
const k = (tx * ux + ty * uy + tz * uz) / Math.max(1e-6, tz);
|
|
585
|
+
rx = tx * k - ux;
|
|
586
|
+
ry = ty * k - uy;
|
|
587
|
+
rz = tz * k - uz;
|
|
588
|
+
}
|
|
589
|
+
let rl = Math.hypot(rx, ry, rz) || 1;
|
|
590
|
+
rx /= rl;
|
|
591
|
+
ry /= rl;
|
|
592
|
+
rz /= rl;
|
|
593
|
+
const m = M ? Math.max(0, Math.min(1, M[i])) : 1;
|
|
594
|
+
if (m < 1) {
|
|
595
|
+
rx = bx + (rx - bx) * m;
|
|
596
|
+
ry = by + (ry - by) * m;
|
|
597
|
+
rz = bz + (rz - bz) * m;
|
|
598
|
+
rl = Math.hypot(rx, ry, rz) || 1;
|
|
599
|
+
rx /= rl;
|
|
600
|
+
ry /= rl;
|
|
601
|
+
rz /= rl;
|
|
602
|
+
}
|
|
603
|
+
out[o] = 0.5 + 0.5 * rx;
|
|
604
|
+
out[o + 1] = 0.5 + 0.5 * ry;
|
|
605
|
+
out[o + 2] = 0.5 + 0.5 * rz;
|
|
606
|
+
out[o + 3] = A[o + 3];
|
|
607
|
+
}
|
|
608
|
+
return out;
|
|
609
|
+
}
|
|
520
610
|
/** Edge detection: Sobel gradient magnitude of a grey layer × strength, clamped, grown by `width` pixels (max filter). */
|
|
521
611
|
export function edgeDetect(g, w, h, strength, width, wrap) {
|
|
522
612
|
let out = new Float32Array(w * h);
|
|
@@ -581,15 +671,36 @@ export function heightToAO(g, w, h, radius, depth, dirs, steps, wrap) {
|
|
|
581
671
|
}
|
|
582
672
|
return out;
|
|
583
673
|
}
|
|
584
|
-
export const WARP_MODES = ['directional', 'slope', 'vector'];
|
|
674
|
+
export const WARP_MODES = ['directional', 'slope', 'vector', 'nonuniform', 'multi'];
|
|
585
675
|
/**
|
|
586
676
|
* Warp a layer by a second image W (already the same size): each pixel samples the source at an offset (fractions of
|
|
587
677
|
* the image width): directional = intensity × W along `angle`; slope = along the gradient of W, the steepest slope
|
|
588
|
-
* moving intensity; vector = intensity × (R, G) × 2 − 1 of an RGBA W
|
|
678
|
+
* moving intensity; vector = intensity × (R, G) × 2 − 1 of an RGBA W; nonuniform = intensity × W along angle plus
|
|
679
|
+
* angleMap × 360° per pixel (a grey map the same size); multi = the mean of `directions` directional warps whose
|
|
680
|
+
* angles split the circle evenly from `angle` (a smeared, direction-free warp).
|
|
589
681
|
*/
|
|
590
|
-
export function warpLayer(a, ch, w, h, W, chW, mode, intensity, angle, wrap) {
|
|
682
|
+
export function warpLayer(a, ch, w, h, W, chW, mode, intensity, angle, wrap, angleMap = null, directions = 4) {
|
|
591
683
|
const out = new Float32Array(a.length);
|
|
592
684
|
const n = w * h, gW = toGrey(W, chW);
|
|
685
|
+
const tmp = new Float32Array(ch);
|
|
686
|
+
if (mode === 'multi') {
|
|
687
|
+
const D = Math.max(1, Math.round(directions));
|
|
688
|
+
const acc = new Float64Array(ch);
|
|
689
|
+
for (let j = 0; j < h; j++)
|
|
690
|
+
for (let i = 0; i < w; i++) {
|
|
691
|
+
const k = j * w + i, len = gW[k] * intensity * w;
|
|
692
|
+
acc.fill(0);
|
|
693
|
+
for (let d = 0; d < D; d++) {
|
|
694
|
+
const t = ((angle + (d * 360) / D) * Math.PI) / 180;
|
|
695
|
+
sampleBilinear(a, ch, w, h, i + 0.5 - len * Math.cos(t), j + 0.5 + len * Math.sin(t), tmp, wrap);
|
|
696
|
+
for (let c = 0; c < ch; c++)
|
|
697
|
+
acc[c] += tmp[c];
|
|
698
|
+
}
|
|
699
|
+
for (let c = 0; c < ch; c++)
|
|
700
|
+
out[k * ch + c] = acc[c] / D;
|
|
701
|
+
}
|
|
702
|
+
return out;
|
|
703
|
+
}
|
|
593
704
|
const ox = new Float32Array(n), oy = new Float32Array(n);
|
|
594
705
|
if (mode === 'slope') {
|
|
595
706
|
let mx = 0;
|
|
@@ -614,6 +725,13 @@ export function warpLayer(a, ch, w, h, W, chW, mode, intensity, angle, wrap) {
|
|
|
614
725
|
oy[i] = -(g * 2 - 1) * intensity * w;
|
|
615
726
|
}
|
|
616
727
|
}
|
|
728
|
+
else if (mode === 'nonuniform') {
|
|
729
|
+
for (let i = 0; i < n; i++) {
|
|
730
|
+
const t = ((angle + (angleMap ? angleMap[i] : 0) * 360) * Math.PI) / 180;
|
|
731
|
+
ox[i] = gW[i] * intensity * w * Math.cos(t);
|
|
732
|
+
oy[i] = -gW[i] * intensity * w * Math.sin(t);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
617
735
|
else {
|
|
618
736
|
const a2 = (angle * Math.PI) / 180, dx = Math.cos(a2), dy = -Math.sin(a2);
|
|
619
737
|
for (let i = 0; i < n; i++) {
|
|
@@ -621,7 +739,6 @@ export function warpLayer(a, ch, w, h, W, chW, mode, intensity, angle, wrap) {
|
|
|
621
739
|
oy[i] = gW[i] * intensity * w * dy;
|
|
622
740
|
}
|
|
623
741
|
}
|
|
624
|
-
const tmp = new Float32Array(ch);
|
|
625
742
|
for (let j = 0; j < h; j++)
|
|
626
743
|
for (let i = 0; i < w; i++) {
|
|
627
744
|
const k = j * w + i;
|
|
@@ -630,6 +747,26 @@ export function warpLayer(a, ch, w, h, W, chW, mode, intensity, angle, wrap) {
|
|
|
630
747
|
}
|
|
631
748
|
return out;
|
|
632
749
|
}
|
|
750
|
+
/** sampleBilinear of a grey layer, returning the value (the same arithmetic, without the output array). */
|
|
751
|
+
function bilinear1(a, w, h, fx, fy, wrap) {
|
|
752
|
+
const x = fx - 0.5, y = fy - 0.5, x0 = Math.floor(x), y0 = Math.floor(y), tx = x - x0, ty = y - y0;
|
|
753
|
+
let i0, i1, j0, j1;
|
|
754
|
+
if (wrap) {
|
|
755
|
+
i0 = ((x0 % w) + w) % w;
|
|
756
|
+
i1 = i0 + 1 === w ? 0 : i0 + 1;
|
|
757
|
+
j0 = ((y0 % h) + h) % h;
|
|
758
|
+
j1 = j0 + 1 === h ? 0 : j0 + 1;
|
|
759
|
+
}
|
|
760
|
+
else {
|
|
761
|
+
i0 = clampI(x0, w);
|
|
762
|
+
i1 = clampI(x0 + 1, w);
|
|
763
|
+
j0 = clampI(y0, h);
|
|
764
|
+
j1 = clampI(y0 + 1, h);
|
|
765
|
+
}
|
|
766
|
+
const r0 = j0 * w, r1 = j1 * w;
|
|
767
|
+
const t = a[r0 + i0] + (a[r0 + i1] - a[r0 + i0]) * tx, b = a[r1 + i0] + (a[r1 + i1] - a[r1 + i0]) * tx;
|
|
768
|
+
return t + (b - t) * ty;
|
|
769
|
+
}
|
|
633
770
|
/** Bilinear sample at pixel coordinates (centres at +0.5); clamp or wrap at borders. */
|
|
634
771
|
export function sampleBilinear(a, ch, w, h, fx, fy, out, wrap) {
|
|
635
772
|
const x = fx - 0.5, y = fy - 0.5, x0 = Math.floor(x), y0 = Math.floor(y), tx = x - x0, ty = y - y0;
|
|
@@ -708,12 +845,19 @@ export function curvesLayer(a, ch, lut) {
|
|
|
708
845
|
return k >= m ? lut[m] : lut[k] + (lut[k + 1] - lut[k]) * f;
|
|
709
846
|
});
|
|
710
847
|
}
|
|
848
|
+
export const TILE_SAMPLER_OUTPUTS = ['grey', 'id', 'color'];
|
|
711
849
|
/**
|
|
712
|
-
* Scatter
|
|
713
|
-
*
|
|
850
|
+
* Scatter grey stamps over a w × h grid of count cells: one instance per cell with random offset, rotation, scale and
|
|
851
|
+
* intensity (times the scale, rotation and intensity maps at its centre when given), kept with probability
|
|
852
|
+
* mask(cell centre) when a mask is given; with several stamps each instance picks one at random. Tiles seamlessly
|
|
853
|
+
* when wrap (the default). C is the combined stamps (grey), or, for output id / color, the random value / colour of
|
|
854
|
+
* the instance that is strongest at each pixel (colour with the combined stamps as alpha); mask = the combined stamps.
|
|
714
855
|
*/
|
|
715
|
-
export function tileSampler(
|
|
856
|
+
export function tileSampler(stamps, w, h, o, mask) {
|
|
716
857
|
const out = new Float32Array(w * h);
|
|
858
|
+
const wrap = o.wrap ?? true, output = o.output ?? 'grey';
|
|
859
|
+
const best = output === 'grey' ? null : new Float32Array(w * h);
|
|
860
|
+
const ids = best ? new Float32Array(w * h) : null, cols = output === 'color' ? new Float32Array(w * h * 3) : null;
|
|
717
861
|
const nx = Math.max(1, Math.round(o.count[0])), ny = Math.max(1, Math.round(o.count[1]));
|
|
718
862
|
const cw = w / nx, chh = h / ny;
|
|
719
863
|
const tmp = new Float32Array(1);
|
|
@@ -724,29 +868,709 @@ export function tileSampler(src, sw, sh, w, h, o, mask) {
|
|
|
724
868
|
let py = (cy + 0.5) * chh + (R(2) - 0.5) * o.jitter * chh;
|
|
725
869
|
px = ((px % w) + w) % w;
|
|
726
870
|
py = ((py % h) + h) % h;
|
|
727
|
-
|
|
871
|
+
const at = Math.min(h - 1, Math.floor(py)) * w + Math.min(w - 1, Math.floor(px));
|
|
872
|
+
if (mask && R(3) >= mask[at])
|
|
728
873
|
continue;
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
874
|
+
let sc = lerp(o.scale[0], o.scale[1], R(4));
|
|
875
|
+
if (o.scaleMap)
|
|
876
|
+
sc *= Math.max(0, o.scaleMap[at]);
|
|
877
|
+
if (!(sc > 0))
|
|
878
|
+
continue; // scaled to nothing: no instance
|
|
879
|
+
let rot = ((R(5) * 2 - 1) * o.rotation * Math.PI) / 180;
|
|
880
|
+
if (o.rotationMap)
|
|
881
|
+
rot += o.rotationMap[at] * 2 * Math.PI;
|
|
882
|
+
let inten = lerp(o.intensity[0], o.intensity[1], R(6));
|
|
883
|
+
if (o.intensityMap)
|
|
884
|
+
inten *= o.intensityMap[at];
|
|
885
|
+
const st = stamps.length > 1 ? stamps[Math.min(stamps.length - 1, Math.floor(R(7) * stamps.length))] : stamps[0];
|
|
732
886
|
const iw = Math.max(1, sc * cw), ih = Math.max(1, sc * chh); // instance footprint in pixels
|
|
733
887
|
const c = Math.cos(rot), s = Math.sin(rot);
|
|
734
888
|
const ext = Math.hypot(iw, ih) / 2 + 1;
|
|
889
|
+
const rid = best ? R(8) : 0, r1 = cols ? R(9) : 0, r2 = cols ? R(10) : 0, r3 = cols ? R(11) : 0;
|
|
735
890
|
for (let y = Math.floor(py - ext); y <= Math.ceil(py + ext); y++)
|
|
736
891
|
for (let x = Math.floor(px - ext); x <= Math.ceil(px + ext); x++) {
|
|
737
892
|
const dx = x + 0.5 - px, dy = y + 0.5 - py;
|
|
738
893
|
const lx = (dx * c + dy * s) / iw + 0.5, ly = (-dx * s + dy * c) / ih + 0.5;
|
|
739
894
|
if (lx < 0 || lx > 1 || ly < 0 || ly > 1)
|
|
740
895
|
continue;
|
|
741
|
-
sampleBilinear(
|
|
896
|
+
sampleBilinear(st.data, 1, st.w, st.h, lx * st.w, ly * st.h, tmp, false);
|
|
742
897
|
const v = tmp[0] * inten;
|
|
743
|
-
if (v > 0)
|
|
744
|
-
|
|
898
|
+
if (!(v > 0))
|
|
899
|
+
continue;
|
|
900
|
+
splat(out, w, h, x, y, v, wrap, o.add);
|
|
901
|
+
if (!best || (!wrap && (x < 0 || y < 0 || x >= w || y >= h)))
|
|
902
|
+
continue;
|
|
903
|
+
const k = wrapI(y, h) * w + wrapI(x, w);
|
|
904
|
+
if (v > best[k]) {
|
|
905
|
+
best[k] = v;
|
|
906
|
+
ids[k] = rid;
|
|
907
|
+
if (cols) {
|
|
908
|
+
cols[k * 3] = r1;
|
|
909
|
+
cols[k * 3 + 1] = r2;
|
|
910
|
+
cols[k * 3 + 2] = r3;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
745
913
|
}
|
|
746
914
|
}
|
|
747
915
|
if (o.add)
|
|
748
916
|
for (let k = 0; k < out.length; k++)
|
|
749
917
|
if (out[k] > 1)
|
|
750
918
|
out[k] = 1;
|
|
919
|
+
if (output === 'id')
|
|
920
|
+
return { C: ids, ch: 1, mask: out };
|
|
921
|
+
if (cols) {
|
|
922
|
+
const C = new Float32Array(w * h * 4);
|
|
923
|
+
for (let k = 0; k < w * h; k++) {
|
|
924
|
+
C[k * 4] = cols[k * 3];
|
|
925
|
+
C[k * 4 + 1] = cols[k * 3 + 1];
|
|
926
|
+
C[k * 4 + 2] = cols[k * 3 + 2];
|
|
927
|
+
C[k * 4 + 3] = clamp01(out[k]);
|
|
928
|
+
}
|
|
929
|
+
return { C, ch: 4, mask: out };
|
|
930
|
+
}
|
|
931
|
+
return { C: out, ch: 1, mask: out };
|
|
932
|
+
}
|
|
933
|
+
// ---------------------------------------------------------------- histogram
|
|
934
|
+
/** Substance Histogram Range: the values squeezed around their middle by range and centred on position: position + (x - 0.5) × range. */
|
|
935
|
+
export function histogramRangeFn(range, position) {
|
|
936
|
+
return (x) => clamp01(position + (x - 0.5) * range);
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Substance Histogram Select: 1 for values within range / 2 of position, 0 beyond. contrast 1 is a hard band; lower
|
|
940
|
+
* contrast softens both edges around the same half-way points (0 = a tent that peaks at position).
|
|
941
|
+
*/
|
|
942
|
+
export function histogramSelectFn(position, range, contrast) {
|
|
943
|
+
const half = Math.max(0, range) / 2, s = (1 - clamp01(contrast)) * half;
|
|
944
|
+
return (x) => {
|
|
945
|
+
const d = Math.abs(x - position);
|
|
946
|
+
return s > 1e-9 ? 1 - smooth(half - s, half + s, d) : d <= half ? 1 : 0;
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* The range of a layer's colour values (grey, or the RGB values of an RGBA layer; alpha never) with `clip` (0..0.5)
|
|
951
|
+
* of them cut off at each end (0 = min..max; a 65536-bin histogram finds the cuts). channel 0..2 reads one RGB
|
|
952
|
+
* channel, -1 all three together.
|
|
953
|
+
*/
|
|
954
|
+
export function valueRange(a, ch, clip, channel = -1) {
|
|
955
|
+
const take = (i) => ch === 1 || (channel < 0 ? (i & 3) !== 3 : (i & 3) === channel);
|
|
956
|
+
let lo = Infinity, hi = -Infinity, n = 0;
|
|
957
|
+
for (let i = 0; i < a.length; i++) {
|
|
958
|
+
if (!take(i))
|
|
959
|
+
continue;
|
|
960
|
+
const x = a[i];
|
|
961
|
+
if (!(x === x))
|
|
962
|
+
continue;
|
|
963
|
+
if (x < lo)
|
|
964
|
+
lo = x;
|
|
965
|
+
if (x > hi)
|
|
966
|
+
hi = x;
|
|
967
|
+
n++;
|
|
968
|
+
}
|
|
969
|
+
if (!n)
|
|
970
|
+
return [0, 1];
|
|
971
|
+
if (!(clip > 0) || !(hi > lo))
|
|
972
|
+
return [lo, hi];
|
|
973
|
+
const B = 65536, hist = new Uint32Array(B), k = (B - 1) / (hi - lo);
|
|
974
|
+
for (let i = 0; i < a.length; i++)
|
|
975
|
+
if (take(i) && a[i] === a[i])
|
|
976
|
+
hist[Math.floor((a[i] - lo) * k)]++;
|
|
977
|
+
const cut = Math.min(0.5, clip) * n;
|
|
978
|
+
let acc = 0, b0 = 0, b1 = B - 1;
|
|
979
|
+
for (let b = 0; b < B; b++) {
|
|
980
|
+
acc += hist[b];
|
|
981
|
+
if (acc > cut) {
|
|
982
|
+
b0 = b;
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
acc = 0;
|
|
987
|
+
for (let b = B - 1; b >= 0; b--) {
|
|
988
|
+
acc += hist[b];
|
|
989
|
+
if (acc > cut) {
|
|
990
|
+
b1 = b;
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return b1 > b0 ? [lo + b0 / k, lo + (b1 + 1) / k] : [lo + b0 / k, lo + (b0 + 1) / k];
|
|
995
|
+
}
|
|
996
|
+
export const QUANTIZE_MODES = ['round', 'floor'];
|
|
997
|
+
/** Quantize to `levels` evenly spaced values 0, 1/(n-1) … 1: round = the nearest level, floor = equal-width bands (posterize). */
|
|
998
|
+
export function quantizeFn(levels, mode) {
|
|
999
|
+
const n = Math.max(2, Math.round(levels));
|
|
1000
|
+
return mode === 'floor' ? (x) => Math.min(n - 1, Math.max(0, Math.floor(x * n))) / (n - 1) : (x) => Math.round(clamp01(x) * (n - 1)) / (n - 1);
|
|
1001
|
+
}
|
|
1002
|
+
// ---------------------------------------------------------------- directional and slope blur
|
|
1003
|
+
/**
|
|
1004
|
+
* Motion blur: every pixel averages samples along a line `lengthPx` pixels long at `angle` degrees (0 = +u, 90 = +v):
|
|
1005
|
+
* centred on the pixel when symmetric, else behind it (so a bright dot trails along the angle). One sample per pixel
|
|
1006
|
+
* of length (at most 1024), bilinear.
|
|
1007
|
+
*/
|
|
1008
|
+
export function directionalBlur(a, ch, w, h, lengthPx, angle, symmetric, wrap) {
|
|
1009
|
+
if (!(lengthPx > 0))
|
|
1010
|
+
return a.slice();
|
|
1011
|
+
const N = Math.min(1024, Math.max(2, Math.ceil(lengthPx) + 1));
|
|
1012
|
+
const t = (angle * Math.PI) / 180, dx = Math.cos(t), dy = -Math.sin(t);
|
|
1013
|
+
const offs = new Float64Array(N);
|
|
1014
|
+
for (let s = 0; s < N; s++)
|
|
1015
|
+
offs[s] = symmetric ? (s / (N - 1) - 0.5) * lengthPx : -(s / (N - 1)) * lengthPx;
|
|
1016
|
+
const out = new Float32Array(a.length), tmp = new Float32Array(ch), acc = new Float64Array(ch);
|
|
1017
|
+
const ox = offs.map((t) => dx * t), oy = offs.map((t) => dy * t);
|
|
1018
|
+
for (let j = 0; j < h; j++)
|
|
1019
|
+
for (let i = 0; i < w; i++) {
|
|
1020
|
+
if (ch === 1) {
|
|
1021
|
+
let sum = 0;
|
|
1022
|
+
for (let s = 0; s < N; s++)
|
|
1023
|
+
sum += bilinear1(a, w, h, i + 0.5 + ox[s], j + 0.5 + oy[s], wrap);
|
|
1024
|
+
out[j * w + i] = sum / N;
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
acc.fill(0);
|
|
1028
|
+
for (let s = 0; s < N; s++) {
|
|
1029
|
+
sampleBilinear(a, ch, w, h, i + 0.5 + ox[s], j + 0.5 + oy[s], tmp, wrap);
|
|
1030
|
+
for (let c = 0; c < ch; c++)
|
|
1031
|
+
acc[c] += tmp[c];
|
|
1032
|
+
}
|
|
1033
|
+
for (let c = 0; c < ch; c++)
|
|
1034
|
+
out[(j * w + i) * ch + c] = acc[c] / N;
|
|
1035
|
+
}
|
|
1036
|
+
return out;
|
|
1037
|
+
}
|
|
1038
|
+
export const SLOPE_BLUR_MODES = ['blur', 'min', 'max'];
|
|
1039
|
+
/**
|
|
1040
|
+
* Slope blur: every pixel follows the slope of a grey map (downhill, or uphill) for `distancePx` pixels in `samples`
|
|
1041
|
+
* steps, re-reading the slope at each step, and combines the source values met on the way: blur (mean), min or max.
|
|
1042
|
+
* Where the map is flatter than half its average slope the steps shrink in proportion, so flat areas stay put. The
|
|
1043
|
+
* walk is in image space and the average slope is the map's own, so the result is the same in metres at any
|
|
1044
|
+
* resolution.
|
|
1045
|
+
*/
|
|
1046
|
+
export function slopeBlur(a, ch, w, h, slope, distancePx, samples, mode, uphill, wrap) {
|
|
1047
|
+
const n = w * h, S = Math.max(1, Math.round(samples));
|
|
1048
|
+
const G = new Float32Array(n * 2);
|
|
1049
|
+
let sum = 0;
|
|
1050
|
+
for (let j = 0; j < h; j++)
|
|
1051
|
+
for (let i = 0; i < w; i++) {
|
|
1052
|
+
sobel(slope, w, h, i, j, wrap);
|
|
1053
|
+
G[(j * w + i) * 2] = SB[0];
|
|
1054
|
+
G[(j * w + i) * 2 + 1] = SB[1];
|
|
1055
|
+
sum += Math.hypot(SB[0], SB[1]);
|
|
1056
|
+
}
|
|
1057
|
+
const out = new Float32Array(a.length), tmp = new Float32Array(ch), g = new Float32Array(2), acc = new Float64Array(ch);
|
|
1058
|
+
const sgn = uphill ? 1 : -1, stepMax = distancePx / S, full = (0.5 * sum) / n; // slopes from half the mean take full steps
|
|
1059
|
+
for (let j = 0; j < h; j++)
|
|
1060
|
+
for (let i = 0; i < w; i++) {
|
|
1061
|
+
const k = j * w + i;
|
|
1062
|
+
for (let c = 0; c < ch; c++)
|
|
1063
|
+
acc[c] = a[k * ch + c];
|
|
1064
|
+
let x = i + 0.5, y = j + 0.5, m = 1;
|
|
1065
|
+
for (let s = 0; s < S && stepMax > 0; s++) {
|
|
1066
|
+
sampleBilinear(G, 2, w, h, x, y, g, wrap);
|
|
1067
|
+
const mag = Math.hypot(g[0], g[1]);
|
|
1068
|
+
if (mag < 1e-9 || !(full > 1e-12))
|
|
1069
|
+
break;
|
|
1070
|
+
const step = stepMax * Math.min(1, mag / full);
|
|
1071
|
+
x += (sgn * g[0] / mag) * step;
|
|
1072
|
+
y += (sgn * g[1] / mag) * step;
|
|
1073
|
+
sampleBilinear(a, ch, w, h, x, y, tmp, wrap);
|
|
1074
|
+
for (let c = 0; c < ch; c++)
|
|
1075
|
+
acc[c] = mode === 'min' ? Math.min(acc[c], tmp[c]) : mode === 'max' ? Math.max(acc[c], tmp[c]) : acc[c] + tmp[c];
|
|
1076
|
+
m++;
|
|
1077
|
+
}
|
|
1078
|
+
for (let c = 0; c < ch; c++)
|
|
1079
|
+
out[k * ch + c] = mode === 'min' || mode === 'max' ? acc[c] : acc[c] / m;
|
|
1080
|
+
if (ch === 4)
|
|
1081
|
+
out[k * 4 + 3] = a[k * 4 + 3];
|
|
1082
|
+
}
|
|
751
1083
|
return out;
|
|
752
1084
|
}
|
|
1085
|
+
// ---------------------------------------------------------------- curvature and occlusion
|
|
1086
|
+
/**
|
|
1087
|
+
* Curvature from a tangent-space normal map (RGBA 0.5 + 0.5 n): the mean curvature H = div(n.xy) / 2 in 1/metres
|
|
1088
|
+
* (texel = metres per pixel; 1/R on a sphere of radius R; positive on convex bumps). C = 0.5 + 0.5 · H · radius
|
|
1089
|
+
* clamped: bends of radius `radius` metres or tighter read full white (convex) or black (concave); convex and concave
|
|
1090
|
+
* hold the two sides 0..1 (as bake_curvature's layers).
|
|
1091
|
+
*/
|
|
1092
|
+
export function curvatureFromNormal(N, w, h, texel, radius, directx, wrap) {
|
|
1093
|
+
const n = w * h, nx = new Float32Array(n), ny = new Float32Array(n);
|
|
1094
|
+
for (let k = 0; k < n; k++) {
|
|
1095
|
+
const x = N[k * 4] * 2 - 1, y = (N[k * 4 + 1] * 2 - 1) * (directx ? -1 : 1), z = N[k * 4 + 2] * 2 - 1;
|
|
1096
|
+
const l = Math.hypot(x, y, z) || 1;
|
|
1097
|
+
nx[k] = x / l;
|
|
1098
|
+
ny[k] = y / l;
|
|
1099
|
+
}
|
|
1100
|
+
const C = new Float32Array(n), convex = new Float32Array(n), concave = new Float32Array(n);
|
|
1101
|
+
const fix = wrap ? wrapI : clampI;
|
|
1102
|
+
for (let j = 0; j < h; j++)
|
|
1103
|
+
for (let i = 0; i < w; i++) {
|
|
1104
|
+
const il = wrap ? i - 1 : Math.max(0, i - 1), ir = wrap ? i + 1 : Math.min(w - 1, i + 1);
|
|
1105
|
+
const ju = wrap ? j - 1 : Math.max(0, j - 1), jd = wrap ? j + 1 : Math.min(h - 1, j + 1);
|
|
1106
|
+
// x grows with i, y (up) grows against j
|
|
1107
|
+
const dnx = ir > il ? (nx[j * w + fix(ir, w)] - nx[j * w + fix(il, w)]) / ((ir - il) * texel) : 0;
|
|
1108
|
+
const dny = jd > ju ? (ny[fix(ju, h) * w + i] - ny[fix(jd, h) * w + i]) / ((jd - ju) * texel) : 0;
|
|
1109
|
+
const v = 0.5 * (dnx + dny) * radius, k = j * w + i;
|
|
1110
|
+
C[k] = clamp01(0.5 + 0.5 * v);
|
|
1111
|
+
convex[k] = clamp01(v);
|
|
1112
|
+
concave[k] = clamp01(-v);
|
|
1113
|
+
}
|
|
1114
|
+
return { C, convex, concave };
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Ambient occlusion of a height in metres, ground-truth style (GTAO, Jimenez et al. 2016, seen from straight above):
|
|
1118
|
+
* for each slice through the pixel the highest horizon on both sides is found from bilinear samples out to `radius`
|
|
1119
|
+
* metres (spaced closer near the pixel, fading out over the last 40 % of the radius), and the slice's
|
|
1120
|
+
* cosine-weighted visibility between the two horizons and the surface's own tangent plane is integrated in closed
|
|
1121
|
+
* form. The result is that visibility over the open value of the same tilted surface: 1 on any plane, darker in pits,
|
|
1122
|
+
* gaps and at the foot of walls. Sample positions are in metres, so the result is the same at any resolution.
|
|
1123
|
+
*/
|
|
1124
|
+
export function heightAO(g, w, h, o) {
|
|
1125
|
+
const out = new Float32Array(w * h);
|
|
1126
|
+
const D = Math.max(1, Math.round(o.directions)), S = Math.max(1, Math.round(o.steps));
|
|
1127
|
+
const rpx = Math.max(1e-6, o.radius / o.texel), z = o.depth; // heights in metres; distances in pixels × texel
|
|
1128
|
+
const dirs = [];
|
|
1129
|
+
for (let d = 0; d < D; d++) {
|
|
1130
|
+
const a = ((d + 0.5) * Math.PI) / D + 0.37;
|
|
1131
|
+
dirs.push([Math.cos(a), Math.sin(a)]);
|
|
1132
|
+
}
|
|
1133
|
+
// samples up to the radius, the last short of it (a sample at the radius has faded to nothing)
|
|
1134
|
+
const rs = Array.from({ length: S }, (_, s) => rpx * ((s + 1) / (S + 1)) ** 2);
|
|
1135
|
+
const fade = rs.map((r) => smooth(0.6, 1, r / rpx)); // samples count fully up to 60 % of the radius, then fade out
|
|
1136
|
+
const rms = rs.map((r) => r * o.texel);
|
|
1137
|
+
// samples that do not fade only need the steepest tangent; the angle is taken once (atan is monotonic)
|
|
1138
|
+
const offs = dirs.map(([dx, dy]) => rs.map((r) => [dx * r, -dy * r]));
|
|
1139
|
+
const slice = (h1, h2, gm) => 0.25 * (-Math.cos(2 * h1 - gm) + Math.cos(gm) + 2 * h1 * Math.sin(gm)) + 0.25 * (-Math.cos(2 * h2 - gm) + Math.cos(gm) + 2 * h2 * Math.sin(gm));
|
|
1140
|
+
const HALF = Math.PI / 2;
|
|
1141
|
+
for (let j = 0; j < h; j++)
|
|
1142
|
+
for (let i = 0; i < w; i++) {
|
|
1143
|
+
const h0 = g[j * w + i] * z;
|
|
1144
|
+
sobel(g, w, h, i, j, o.wrap);
|
|
1145
|
+
const sx = (SB[0] * z) / o.texel, sy = (-SB[1] * z) / o.texel; // dh/dx, dh/dy (y up), metres per metre
|
|
1146
|
+
let vis = 0, open = 0;
|
|
1147
|
+
for (let d = 0; d < D; d++) {
|
|
1148
|
+
const [dx, dy] = dirs[d], off = offs[d];
|
|
1149
|
+
const sd = sx * dx + sy * dy; // slope along the slice
|
|
1150
|
+
const gm = -Math.atan(sd); // the normal's angle from straight up, toward +dir
|
|
1151
|
+
const len = Math.sqrt(1 + sd * sd) / Math.sqrt(1 + sx * sx + sy * sy);
|
|
1152
|
+
let e1 = -HALF, e2 = -HALF, t1 = -Infinity, t2 = -Infinity;
|
|
1153
|
+
const px = i + 0.5, py = j + 0.5;
|
|
1154
|
+
for (let s = 0; s < S; s++) {
|
|
1155
|
+
const f = fade[s];
|
|
1156
|
+
if (f >= 1)
|
|
1157
|
+
continue;
|
|
1158
|
+
const ox = off[s][0], oy = off[s][1], rm = rms[s];
|
|
1159
|
+
const v2 = (bilinear1(g, w, h, px + ox, py + oy, o.wrap) * z - h0) / rm;
|
|
1160
|
+
const v1 = (bilinear1(g, w, h, px - ox, py - oy, o.wrap) * z - h0) / rm;
|
|
1161
|
+
if (f <= 0) {
|
|
1162
|
+
if (v2 > t2)
|
|
1163
|
+
t2 = v2;
|
|
1164
|
+
if (v1 > t1)
|
|
1165
|
+
t1 = v1;
|
|
1166
|
+
continue;
|
|
1167
|
+
}
|
|
1168
|
+
const a2 = Math.atan(v2) * (1 - f) - HALF * f, a1 = Math.atan(v1) * (1 - f) - HALF * f;
|
|
1169
|
+
if (a2 > e2)
|
|
1170
|
+
e2 = a2;
|
|
1171
|
+
if (a1 > e1)
|
|
1172
|
+
e1 = a1;
|
|
1173
|
+
}
|
|
1174
|
+
if (t2 > -Infinity)
|
|
1175
|
+
e2 = Math.max(e2, Math.atan(t2));
|
|
1176
|
+
if (t1 > -Infinity)
|
|
1177
|
+
e1 = Math.max(e1, Math.atan(t1));
|
|
1178
|
+
// horizon angles from straight up (h2 toward +dir, h1 toward -dir), never below the tangent plane
|
|
1179
|
+
const h2 = Math.min(HALF - e2, gm + HALF), h1 = Math.max(-(HALF - e1), gm - HALF);
|
|
1180
|
+
vis += len * slice(h1, h2, gm);
|
|
1181
|
+
open += len * slice(gm - HALF, gm + HALF, gm);
|
|
1182
|
+
}
|
|
1183
|
+
const ao = open > 0 ? vis / open : 1;
|
|
1184
|
+
out[j * w + i] = clamp01(1 - o.strength * (1 - clamp01(ao)));
|
|
1185
|
+
}
|
|
1186
|
+
return out;
|
|
1187
|
+
}
|
|
1188
|
+
// ---------------------------------------------------------------- make it tile
|
|
1189
|
+
/**
|
|
1190
|
+
* Make an image tile: first along u, then along v. Each pass blends the image with a copy shifted by half the image,
|
|
1191
|
+
* whose own border seam sits in the middle: the mask is 0 at the borders (the shifted copy, continuous across them)
|
|
1192
|
+
* and 1 from `blend` (a fraction of the image) inward (the original, covering the copy's seam). noiseAmount 0 is a
|
|
1193
|
+
* smooth cross-fade over the band; toward 1 the hand-over becomes a narrow edge that wanders with the noise (a patch
|
|
1194
|
+
* edge hides the blend better on photos). noiseU / noiseV: tileable 0..1 noise over (u, v), one per pass.
|
|
1195
|
+
*/
|
|
1196
|
+
export function makeTile(a, ch, w, h, blend, noiseU, noiseV, noiseAmount) {
|
|
1197
|
+
const b = Math.max(1e-4, Math.min(0.5, blend)), na = clamp01(noiseAmount);
|
|
1198
|
+
const mask = (e, nz) => {
|
|
1199
|
+
let s = (b / 2) * (1 - 0.85 * na), c = b / 2 + (nz - 0.5) * b * na;
|
|
1200
|
+
s = Math.min(s, 0.25);
|
|
1201
|
+
c = Math.max(s, Math.min(0.5 - s, c));
|
|
1202
|
+
return smooth(c - s, c + s, e);
|
|
1203
|
+
};
|
|
1204
|
+
const pass = (src, alongU, noise) => {
|
|
1205
|
+
const out = new Float32Array(src.length), tmp = new Float32Array(ch);
|
|
1206
|
+
for (let j = 0; j < h; j++)
|
|
1207
|
+
for (let i = 0; i < w; i++) {
|
|
1208
|
+
const u = (i + 0.5) / w, v = 1 - (j + 0.5) / h, k = (j * w + i) * ch;
|
|
1209
|
+
const e = 0.5 - Math.abs((alongU ? u : v) - 0.5);
|
|
1210
|
+
const m = mask(e, noise ? noise(u, v) : 0.5);
|
|
1211
|
+
sampleBilinear(src, ch, w, h, i + 0.5 + (alongU ? w / 2 : 0), j + 0.5 + (alongU ? 0 : h / 2), tmp, true);
|
|
1212
|
+
for (let c = 0; c < ch; c++)
|
|
1213
|
+
out[k + c] = tmp[c] + (src[k + c] - tmp[c]) * m;
|
|
1214
|
+
}
|
|
1215
|
+
return out;
|
|
1216
|
+
};
|
|
1217
|
+
return pass(pass(a, true, noiseU), false, noiseV);
|
|
1218
|
+
}
|
|
1219
|
+
// ---------------------------------------------------------------- normal maps
|
|
1220
|
+
/** Normal map vectors scaled back to unit length (after blurs, resizes, blends or painting); z below 0 is lifted to 0; alpha kept. */
|
|
1221
|
+
export function normalizeNormals(N) {
|
|
1222
|
+
const out = new Float32Array(N.length);
|
|
1223
|
+
for (let k = 0; k < N.length; k += 4) {
|
|
1224
|
+
const x = N[k] * 2 - 1, y = N[k + 1] * 2 - 1, z = Math.max(0, N[k + 2] * 2 - 1);
|
|
1225
|
+
const l = Math.hypot(x, y, z);
|
|
1226
|
+
if (l < 1e-9) {
|
|
1227
|
+
out[k] = 0.5;
|
|
1228
|
+
out[k + 1] = 0.5;
|
|
1229
|
+
out[k + 2] = 1;
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
out[k] = 0.5 + (0.5 * x) / l;
|
|
1233
|
+
out[k + 1] = 0.5 + (0.5 * y) / l;
|
|
1234
|
+
out[k + 2] = 0.5 + (0.5 * z) / l;
|
|
1235
|
+
}
|
|
1236
|
+
out[k + 3] = N[k + 3];
|
|
1237
|
+
}
|
|
1238
|
+
return out;
|
|
1239
|
+
}
|
|
1240
|
+
/** Flip the red (x) and / or green (y) channel of a normal map: green converts OpenGL ↔ DirectX; alpha kept. */
|
|
1241
|
+
export function flipNormals(N, flipX, flipY) {
|
|
1242
|
+
const out = N.slice();
|
|
1243
|
+
for (let k = 0; k < N.length; k += 4) {
|
|
1244
|
+
if (flipX)
|
|
1245
|
+
out[k] = 1 - N[k];
|
|
1246
|
+
if (flipY)
|
|
1247
|
+
out[k + 1] = 1 - N[k + 1];
|
|
1248
|
+
}
|
|
1249
|
+
return out;
|
|
1250
|
+
}
|
|
1251
|
+
/** In-place complex FFT of any length: iterative radix 2, or Bluestein's chirp z-transform for other lengths. */
|
|
1252
|
+
class FFT {
|
|
1253
|
+
n;
|
|
1254
|
+
cos;
|
|
1255
|
+
sin;
|
|
1256
|
+
rev = null;
|
|
1257
|
+
blue = null;
|
|
1258
|
+
constructor(n) {
|
|
1259
|
+
this.n = n;
|
|
1260
|
+
const pow2 = (n & (n - 1)) === 0;
|
|
1261
|
+
const m = pow2 ? n : 1 << Math.ceil(Math.log2(2 * n - 1));
|
|
1262
|
+
if (pow2) {
|
|
1263
|
+
this.cos = new Float64Array(n / 2);
|
|
1264
|
+
this.sin = new Float64Array(n / 2);
|
|
1265
|
+
for (let k = 0; k < n / 2; k++) {
|
|
1266
|
+
this.cos[k] = Math.cos((2 * Math.PI * k) / n);
|
|
1267
|
+
this.sin[k] = Math.sin((2 * Math.PI * k) / n);
|
|
1268
|
+
}
|
|
1269
|
+
this.rev = new Uint32Array(n);
|
|
1270
|
+
const bits = Math.round(Math.log2(n));
|
|
1271
|
+
for (let i = 0; i < n; i++) {
|
|
1272
|
+
let r = 0;
|
|
1273
|
+
for (let b = 0; b < bits; b++)
|
|
1274
|
+
r |= ((i >> b) & 1) << (bits - 1 - b);
|
|
1275
|
+
this.rev[i] = r;
|
|
1276
|
+
}
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
this.cos = new Float64Array(0);
|
|
1280
|
+
this.sin = new Float64Array(0);
|
|
1281
|
+
const sub = new FFT(m);
|
|
1282
|
+
const wr = new Float64Array(n), wi = new Float64Array(n);
|
|
1283
|
+
for (let k = 0; k < n; k++) {
|
|
1284
|
+
const t = (Math.PI * ((k * k) % (2 * n))) / n;
|
|
1285
|
+
wr[k] = Math.cos(t);
|
|
1286
|
+
wi[k] = -Math.sin(t);
|
|
1287
|
+
}
|
|
1288
|
+
const br = new Float64Array(m), bi = new Float64Array(m);
|
|
1289
|
+
br[0] = wr[0];
|
|
1290
|
+
bi[0] = -wi[0];
|
|
1291
|
+
for (let k = 1; k < n; k++) {
|
|
1292
|
+
br[k] = br[m - k] = wr[k];
|
|
1293
|
+
bi[k] = bi[m - k] = -wi[k];
|
|
1294
|
+
}
|
|
1295
|
+
sub.forward(br, bi);
|
|
1296
|
+
this.blue = { sub, m, wr, wi, br, bi, ar: new Float64Array(m), ai: new Float64Array(m) };
|
|
1297
|
+
}
|
|
1298
|
+
/** Forward transform X_k = Σ x_j e^(−2πi jk / n), unscaled. */
|
|
1299
|
+
forward(re, im) {
|
|
1300
|
+
const n = this.n;
|
|
1301
|
+
if (this.rev) {
|
|
1302
|
+
const rev = this.rev;
|
|
1303
|
+
for (let i = 0; i < n; i++) {
|
|
1304
|
+
const j = rev[i];
|
|
1305
|
+
if (j > i) {
|
|
1306
|
+
let t = re[i];
|
|
1307
|
+
re[i] = re[j];
|
|
1308
|
+
re[j] = t;
|
|
1309
|
+
t = im[i];
|
|
1310
|
+
im[i] = im[j];
|
|
1311
|
+
im[j] = t;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
for (let size = 2; size <= n; size <<= 1) {
|
|
1315
|
+
const half = size >> 1, step = n / size;
|
|
1316
|
+
for (let i = 0; i < n; i += size)
|
|
1317
|
+
for (let k = 0; k < half; k++) {
|
|
1318
|
+
const tr = this.cos[k * step], ti = -this.sin[k * step];
|
|
1319
|
+
const a = i + k, b = a + half;
|
|
1320
|
+
const xr = re[b] * tr - im[b] * ti, xi = re[b] * ti + im[b] * tr;
|
|
1321
|
+
re[b] = re[a] - xr;
|
|
1322
|
+
im[b] = im[a] - xi;
|
|
1323
|
+
re[a] += xr;
|
|
1324
|
+
im[a] += xi;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
const B = this.blue, { m, wr, wi, ar, ai } = B;
|
|
1330
|
+
ar.fill(0);
|
|
1331
|
+
ai.fill(0);
|
|
1332
|
+
for (let k = 0; k < n; k++) {
|
|
1333
|
+
ar[k] = re[k] * wr[k] - im[k] * wi[k];
|
|
1334
|
+
ai[k] = re[k] * wi[k] + im[k] * wr[k];
|
|
1335
|
+
}
|
|
1336
|
+
B.sub.forward(ar, ai);
|
|
1337
|
+
for (let k = 0; k < m; k++) {
|
|
1338
|
+
const xr = ar[k] * B.br[k] - ai[k] * B.bi[k], xi = ar[k] * B.bi[k] + ai[k] * B.br[k];
|
|
1339
|
+
ar[k] = xr;
|
|
1340
|
+
ai[k] = -xi;
|
|
1341
|
+
}
|
|
1342
|
+
B.sub.forward(ar, ai); // inverse by conjugation: conj(F(conj(x)))
|
|
1343
|
+
for (let k = 0; k < n; k++) {
|
|
1344
|
+
const cr = ar[k] / m, ci = -ai[k] / m;
|
|
1345
|
+
re[k] = cr * wr[k] - ci * wi[k];
|
|
1346
|
+
im[k] = cr * wi[k] + ci * wr[k];
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
/** Inverse transform, scaled by 1 / n. */
|
|
1350
|
+
inverse(re, im) {
|
|
1351
|
+
const n = this.n;
|
|
1352
|
+
for (let k = 0; k < n; k++)
|
|
1353
|
+
im[k] = -im[k];
|
|
1354
|
+
this.forward(re, im);
|
|
1355
|
+
for (let k = 0; k < n; k++) {
|
|
1356
|
+
re[k] /= n;
|
|
1357
|
+
im[k] = -im[k] / n;
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
/** DCT-II X_k = Σ x_j cos(πk(2j + 1) / 2n) of x into x, and its inverse, through one complex FFT of length n (Makhoul). */
|
|
1362
|
+
function dct(x, f, re, im, inverse) {
|
|
1363
|
+
const n = f.n;
|
|
1364
|
+
if (!inverse) {
|
|
1365
|
+
for (let j = 0; 2 * j < n; j++)
|
|
1366
|
+
re[j] = x[2 * j];
|
|
1367
|
+
for (let j = 0; 2 * j + 1 < n; j++)
|
|
1368
|
+
re[n - 1 - j] = x[2 * j + 1];
|
|
1369
|
+
im.fill(0, 0, n);
|
|
1370
|
+
f.forward(re, im);
|
|
1371
|
+
for (let k = 0; k < n; k++) {
|
|
1372
|
+
const t = (Math.PI * k) / (2 * n);
|
|
1373
|
+
x[k] = re[k] * Math.cos(t) + im[k] * Math.sin(t);
|
|
1374
|
+
}
|
|
1375
|
+
return;
|
|
1376
|
+
}
|
|
1377
|
+
// V_k = e^(iπk/2n) (X_k − i X_(n−k)), X_n = 0; v = IFFT(V); x_2j = v_j, x_2j+1 = v_(n−1−j): dct then idct is the identity
|
|
1378
|
+
for (let k = 0; k < n; k++) {
|
|
1379
|
+
const t = (Math.PI * k) / (2 * n), c = Math.cos(t), s = Math.sin(t);
|
|
1380
|
+
const a = x[k], b = k ? -x[n - k] : 0;
|
|
1381
|
+
re[k] = a * c - b * s;
|
|
1382
|
+
im[k] = a * s + b * c;
|
|
1383
|
+
}
|
|
1384
|
+
f.inverse(re, im);
|
|
1385
|
+
for (let j = 0; 2 * j < n; j++)
|
|
1386
|
+
x[2 * j] = re[j];
|
|
1387
|
+
for (let j = 0; 2 * j + 1 < n; j++)
|
|
1388
|
+
x[2 * j + 1] = re[n - 1 - j];
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Solve the 5-point Laplacian ∇²u = f on a w × h grid: periodic (wrap, by FFT) or with mirrored borders (zero normal
|
|
1392
|
+
* gradient, by DCT). The solution with mean 0.
|
|
1393
|
+
*/
|
|
1394
|
+
export function poisson(f, w, h, wrap) {
|
|
1395
|
+
const fw = new FFT(w), fh = new FFT(h);
|
|
1396
|
+
const m = Math.max(w, h), re = new Float64Array(m), im = new Float64Array(m), line = new Float64Array(m);
|
|
1397
|
+
const u = Float64Array.from(f);
|
|
1398
|
+
if (wrap) {
|
|
1399
|
+
const ui = new Float64Array(w * h);
|
|
1400
|
+
const rows = (inv) => {
|
|
1401
|
+
for (let j = 0; j < h; j++) {
|
|
1402
|
+
for (let i = 0; i < w; i++) {
|
|
1403
|
+
re[i] = u[j * w + i];
|
|
1404
|
+
im[i] = ui[j * w + i];
|
|
1405
|
+
}
|
|
1406
|
+
if (inv)
|
|
1407
|
+
fw.inverse(re, im);
|
|
1408
|
+
else
|
|
1409
|
+
fw.forward(re, im);
|
|
1410
|
+
for (let i = 0; i < w; i++) {
|
|
1411
|
+
u[j * w + i] = re[i];
|
|
1412
|
+
ui[j * w + i] = im[i];
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
};
|
|
1416
|
+
const cols = (inv) => {
|
|
1417
|
+
for (let i = 0; i < w; i++) {
|
|
1418
|
+
for (let j = 0; j < h; j++) {
|
|
1419
|
+
re[j] = u[j * w + i];
|
|
1420
|
+
im[j] = ui[j * w + i];
|
|
1421
|
+
}
|
|
1422
|
+
if (inv)
|
|
1423
|
+
fh.inverse(re, im);
|
|
1424
|
+
else
|
|
1425
|
+
fh.forward(re, im);
|
|
1426
|
+
for (let j = 0; j < h; j++) {
|
|
1427
|
+
u[j * w + i] = re[j];
|
|
1428
|
+
ui[j * w + i] = im[j];
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
};
|
|
1432
|
+
rows(false);
|
|
1433
|
+
cols(false);
|
|
1434
|
+
for (let l = 0; l < h; l++)
|
|
1435
|
+
for (let k = 0; k < w; k++) {
|
|
1436
|
+
const lam = 2 * Math.cos((2 * Math.PI * k) / w) - 2 + 2 * Math.cos((2 * Math.PI * l) / h) - 2;
|
|
1437
|
+
const q = l * w + k;
|
|
1438
|
+
if (Math.abs(lam) < 1e-12) {
|
|
1439
|
+
u[q] = 0;
|
|
1440
|
+
ui[q] = 0;
|
|
1441
|
+
}
|
|
1442
|
+
else {
|
|
1443
|
+
u[q] /= lam;
|
|
1444
|
+
ui[q] /= lam;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
cols(true);
|
|
1448
|
+
rows(true);
|
|
1449
|
+
return u;
|
|
1450
|
+
}
|
|
1451
|
+
const rows = (inv) => {
|
|
1452
|
+
for (let j = 0; j < h; j++) {
|
|
1453
|
+
for (let i = 0; i < w; i++)
|
|
1454
|
+
line[i] = u[j * w + i];
|
|
1455
|
+
dct(line, fw, re, im, inv);
|
|
1456
|
+
for (let i = 0; i < w; i++)
|
|
1457
|
+
u[j * w + i] = line[i];
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
const cols = (inv) => {
|
|
1461
|
+
for (let i = 0; i < w; i++) {
|
|
1462
|
+
for (let j = 0; j < h; j++)
|
|
1463
|
+
line[j] = u[j * w + i];
|
|
1464
|
+
dct(line, fh, re, im, inv);
|
|
1465
|
+
for (let j = 0; j < h; j++)
|
|
1466
|
+
u[j * w + i] = line[j];
|
|
1467
|
+
}
|
|
1468
|
+
};
|
|
1469
|
+
rows(false);
|
|
1470
|
+
cols(false);
|
|
1471
|
+
for (let l = 0; l < h; l++)
|
|
1472
|
+
for (let k = 0; k < w; k++) {
|
|
1473
|
+
const lam = 2 * Math.cos((Math.PI * k) / w) - 2 + 2 * Math.cos((Math.PI * l) / h) - 2;
|
|
1474
|
+
const q = l * w + k;
|
|
1475
|
+
u[q] = Math.abs(lam) < 1e-12 ? 0 : u[q] / lam;
|
|
1476
|
+
}
|
|
1477
|
+
cols(true);
|
|
1478
|
+
rows(true);
|
|
1479
|
+
return u;
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Height in metres from a tangent-space normal map (texel = metres per pixel): the slopes −n.xy / n.z are integrated
|
|
1483
|
+
* by solving a Poisson equation (least squares over every pair of neighbouring pixels), periodic when wrap, else with
|
|
1484
|
+
* mirrored borders. Mean height 0. Slopes are capped at about 87° (n.z ≥ 0.05).
|
|
1485
|
+
*/
|
|
1486
|
+
export function heightFromNormal(N, w, h, texel, directx, wrap) {
|
|
1487
|
+
const n = w * h, p = new Float64Array(n), q = new Float64Array(n);
|
|
1488
|
+
for (let k = 0; k < n; k++) {
|
|
1489
|
+
const x = N[k * 4] * 2 - 1, y = (N[k * 4 + 1] * 2 - 1) * (directx ? -1 : 1), z = Math.max(0.05, N[k * 4 + 2] * 2 - 1);
|
|
1490
|
+
p[k] = -x / z; // dh/dx (x along +i)
|
|
1491
|
+
q[k] = y / z; // dh/dy_row = -dh/dv (rows go down)
|
|
1492
|
+
}
|
|
1493
|
+
// height steps between neighbouring pixel centres (metres), and their divergence
|
|
1494
|
+
const div = new Float64Array(n);
|
|
1495
|
+
for (let j = 0; j < h; j++)
|
|
1496
|
+
for (let i = 0; i < w; i++) {
|
|
1497
|
+
const k = j * w + i;
|
|
1498
|
+
const stepX = (a) => (wrap || (a >= 0 && a < w - 1) ? ((p[j * w + wrapI(a, w)] + p[j * w + wrapI(a + 1, w)]) / 2) * texel : 0);
|
|
1499
|
+
const stepY = (b) => (wrap || (b >= 0 && b < h - 1) ? ((q[wrapI(b, h) * w + i] + q[wrapI(b + 1, h) * w + i]) / 2) * texel : 0);
|
|
1500
|
+
div[k] = stepX(i) - stepX(i - 1) + stepY(j) - stepY(j - 1);
|
|
1501
|
+
}
|
|
1502
|
+
return poisson(div, w, h, wrap);
|
|
1503
|
+
}
|
|
1504
|
+
// ---------------------------------------------------------------- grunge
|
|
1505
|
+
export const GRUNGE_STYLES = ['dirt', 'leaks', 'spots'];
|
|
1506
|
+
/**
|
|
1507
|
+
* Grunge masks (grey 0..1, white = grime) built from tileable noise in uv, so the same at any resolution:
|
|
1508
|
+
* dirt blotchy patches of domain-warped fbm with speckle
|
|
1509
|
+
* leaks vertical streaks running down from random sources (rain marks, rust runs)
|
|
1510
|
+
* spots scattered round spots of random size (rust pits, drops, stains); coverage grows them
|
|
1511
|
+
* coverage sets the fraction of the image above 0.5 (measured on the field, like tex_histogram_scan), contrast the edge.
|
|
1512
|
+
*/
|
|
1513
|
+
export function genGrunge(o, w, h) {
|
|
1514
|
+
const aspect = h / w, sc = Math.max(0.01, o.scale), det = clamp01(o.detail);
|
|
1515
|
+
const base = { noise: 'perlin', fractal: 'fbm', roughness: 0.55, lacunarity: 2, tileable: o.tileable, offset: [0, 0], aspect };
|
|
1516
|
+
const nz = (scale, octaves, k, extra = {}) => texNoise({ ...base, scale, octaves, seed: o.seed * 7 + k, ...extra });
|
|
1517
|
+
const fine = nz(sc * 8, 3, 5);
|
|
1518
|
+
let field;
|
|
1519
|
+
if (o.style === 'leaks') {
|
|
1520
|
+
// long vertical cells: few rows, many columns; a coarse noise picks where runs start and how strong they are
|
|
1521
|
+
const streak = nz(sc * 3, 4, 1, { aspect: aspect * 0.08 });
|
|
1522
|
+
const source = nz(sc * 0.75, 3, 2);
|
|
1523
|
+
const drip = nz(sc * 6, 2, 3, { aspect: aspect * 0.25 });
|
|
1524
|
+
field = (u, v) => {
|
|
1525
|
+
const s = streak(u, v), d = drip(u, v);
|
|
1526
|
+
return (s * 0.7 + d * 0.3) * (0.35 + 0.65 * source(u, v));
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
1529
|
+
else if (o.style === 'spots') {
|
|
1530
|
+
// one spot per lattice cell at a random point with a random size; the field falls from 1 at each site with the
|
|
1531
|
+
// distance relative to that spot's size, and the strongest of the surrounding sites wins (spots overlap their
|
|
1532
|
+
// cells), so a higher coverage grows every spot, the big ones most
|
|
1533
|
+
let fx = sc, fy = sc * aspect;
|
|
1534
|
+
if (o.tileable) {
|
|
1535
|
+
fx = Math.max(1, Math.round(fx));
|
|
1536
|
+
fy = Math.max(1, Math.round(fy));
|
|
1537
|
+
}
|
|
1538
|
+
const sh = hash32(Math.floor(o.seed * 7919) ^ 0x68e31da4);
|
|
1539
|
+
const rough = nz(sc * 4, 3, 4);
|
|
1540
|
+
field = (u, v) => {
|
|
1541
|
+
const x = u * fx, y = v * fy, xf = Math.floor(x), yf = Math.floor(y), k = 1.6 + 0.8 * rough(u, v);
|
|
1542
|
+
let best = -Infinity;
|
|
1543
|
+
for (let dy = -1; dy <= 1; dy++)
|
|
1544
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
1545
|
+
const cx = xf + dx, cy = yf + dy;
|
|
1546
|
+
const wx = o.tileable ? wrapI(cx, fx) : cx, wy = o.tileable ? wrapI(cy, fy) : cy;
|
|
1547
|
+
const hh = hash32(Math.imul(wx, 0x8da6b343) ^ Math.imul(wy, 0xd8163841) ^ sh);
|
|
1548
|
+
const px = cx + (hh & 0xffff) / 65536, py = cy + (hash32(hh ^ 0x9e3779b9) & 0xffff) / 65536;
|
|
1549
|
+
const r = hash32(hh ^ 0x3b9ac9fb) / 4294967296, rad = 0.06 + 0.3 * r * r; // cell units
|
|
1550
|
+
const f = 1 - Math.hypot(px - x, py - y) / (rad * k);
|
|
1551
|
+
if (f > best)
|
|
1552
|
+
best = f;
|
|
1553
|
+
}
|
|
1554
|
+
return best;
|
|
1555
|
+
};
|
|
1556
|
+
}
|
|
1557
|
+
else {
|
|
1558
|
+
const n1 = nz(sc, 6, 1), wx = nz(sc * 0.5, 3, 2), wy = nz(sc * 0.5, 3, 3);
|
|
1559
|
+
const k = 0.6 / sc; // warp by up to about half a feature
|
|
1560
|
+
field = (u, v) => n1(u + (wx(u, v) - 0.5) * k, v + (wy(u, v) - 0.5) * k);
|
|
1561
|
+
}
|
|
1562
|
+
const val = new Float32Array(w * h);
|
|
1563
|
+
for (let j = 0; j < h; j++)
|
|
1564
|
+
for (let i = 0; i < w; i++) {
|
|
1565
|
+
const u = (i + 0.5) / w, v = 1 - (j + 0.5) / h;
|
|
1566
|
+
val[j * w + i] = field(u, v) * (1 + det * 1.4 * (fine(u, v) - 0.5)); // breakup scales the field: empty stays empty
|
|
1567
|
+
}
|
|
1568
|
+
// stretch the field to its own range first (noise spans a narrow band), so contrast means the same for every style
|
|
1569
|
+
const [lo, hi] = valueRange(val, 1, 0.002), k = hi > lo ? 1 / (hi - lo) : 1;
|
|
1570
|
+
for (let i = 0; i < val.length; i++)
|
|
1571
|
+
val[i] = (val[i] - lo) * k;
|
|
1572
|
+
const scan = histogramScanFn(1 - coverageCut(val, o.coverage, o.contrast), o.contrast);
|
|
1573
|
+
for (let i = 0; i < val.length; i++)
|
|
1574
|
+
val[i] = scan(val[i]);
|
|
1575
|
+
return val;
|
|
1576
|
+
}
|