roamwright 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CONTRIBUTING.md +39 -8
  2. package/README.md +53 -24
  3. package/examples/viaduct-camera.js +40 -0
  4. package/examples/viaduct-landscape.js +81 -0
  5. package/examples/viaduct.css +75 -0
  6. package/examples/viaduct.html +47 -0
  7. package/examples/viaduct.js +332 -0
  8. package/geometry/creased-sheet.js +127 -0
  9. package/geometry/ear.js +97 -0
  10. package/geometry/erosion.js +106 -0
  11. package/geometry/face-grid.js +143 -0
  12. package/geometry/garment-tags.js +11 -0
  13. package/geometry/hair-gathers.js +174 -0
  14. package/geometry/hair-locks.js +156 -0
  15. package/geometry/hair-patch.js +42 -0
  16. package/geometry/hair-shell.js +186 -0
  17. package/geometry/hair-tags.js +6 -0
  18. package/geometry/head-profile.js +138 -0
  19. package/geometry/head-shape.js +499 -0
  20. package/geometry/heightfield.js +68 -0
  21. package/geometry/outline-grid.js +46 -0
  22. package/geometry/path-field.js +65 -0
  23. package/geometry/sagging-tube.js +58 -0
  24. package/geometry/sampled-surface.js +101 -0
  25. package/geometry/scatter-grid.js +32 -0
  26. package/geometry/sculpt.js +263 -0
  27. package/geometry/shore-grid.js +67 -0
  28. package/geometry/strata-field.js +339 -0
  29. package/geometry/strata-tiles.js +215 -0
  30. package/geometry/stream.js +94 -0
  31. package/geometry/terrain-chunks.js +300 -0
  32. package/geometry/terrain-extension.js +44 -0
  33. package/math/cell-edge.js +44 -0
  34. package/math/cloth-textures.js +83 -0
  35. package/math/cloud-textures.js +112 -0
  36. package/math/color.js +29 -0
  37. package/math/mountain-ranges.js +463 -0
  38. package/math/particle-noise.js +48 -0
  39. package/math/periodic.js +20 -0
  40. package/math/rock-texture.js +56 -0
  41. package/math/surface-textures.js +241 -0
  42. package/motion/arm-reach.js +74 -0
  43. package/motion/biped-stride.js +276 -0
  44. package/motion/crowd-alarms.js +127 -0
  45. package/motion/crowd-routine.js +183 -0
  46. package/motion/daily-clock.js +59 -0
  47. package/motion/instance-sight.js +84 -0
  48. package/motion/quadruped.js +332 -0
  49. package/motion/weather.js +59 -0
  50. package/motion/wind.js +27 -0
  51. package/package.json +6 -4
  52. package/rendering/caster-motion.js +79 -0
  53. package/rendering/cell-lod.js +175 -0
  54. package/rendering/cloud-dome.js +371 -0
  55. package/rendering/cloud-field.js +83 -0
  56. package/rendering/compilation-schedule.js +34 -0
  57. package/rendering/contact-shadows.js +79 -0
  58. package/rendering/crowd-skinning.js +133 -0
  59. package/rendering/crust.js +28 -0
  60. package/rendering/daylight.js +50 -0
  61. package/rendering/far-shadow.js +186 -0
  62. package/rendering/flocks.js +323 -0
  63. package/rendering/garment-material.js +110 -0
  64. package/rendering/glow-lines.js +74 -0
  65. package/rendering/glow-shell.js +90 -0
  66. package/rendering/hair-strands.js +67 -0
  67. package/rendering/height-haze.js +129 -0
  68. package/rendering/height-probe.js +91 -0
  69. package/rendering/interior-mapping.js +19 -0
  70. package/rendering/light-budget.js +222 -0
  71. package/rendering/light-points.js +51 -0
  72. package/rendering/light-shafts.js +131 -0
  73. package/rendering/local-lights.js +28 -0
  74. package/rendering/low-light.js +13 -0
  75. package/rendering/material-lod.js +110 -0
  76. package/rendering/particle-emitters.js +223 -0
  77. package/rendering/particles.js +297 -0
  78. package/rendering/planar-reflection.js +175 -0
  79. package/rendering/post-shaders.js +21 -1
  80. package/rendering/post.js +4 -0
  81. package/rendering/rain-rings.js +22 -0
  82. package/rendering/road-surface.js +63 -0
  83. package/rendering/sea-shaders.js +230 -0
  84. package/rendering/shader-warmup.js +197 -0
  85. package/rendering/shadow-alignment.js +28 -0
  86. package/rendering/shadow-depth.js +129 -0
  87. package/rendering/sky-occlusion.js +187 -0
  88. package/rendering/sky.js +169 -0
  89. package/rendering/stage-lights.js +209 -0
  90. package/rendering/static-point-shadow.js +119 -0
  91. package/rendering/strata-material.js +93 -0
  92. package/rendering/stream-flow.js +36 -0
  93. package/rendering/surface-light.js +22 -0
  94. package/rendering/surface-projection.js +69 -0
  95. package/rendering/talus.js +26 -0
  96. package/rendering/terrain-rock.js +48 -0
  97. package/rendering/texture-fills.js +68 -0
  98. package/rendering/viewport.js +9 -0
  99. package/rendering/wakes.js +99 -0
  100. package/rendering/water-material.js +288 -0
  101. package/runtime/device-quality.js +27 -0
  102. package/runtime/diagnostic-log.js +80 -0
  103. package/runtime/events.js +14 -0
  104. package/runtime/first-use.js +3 -1
  105. package/runtime/frame-yield.js +14 -0
  106. package/runtime/grid-cache.js +88 -0
  107. package/runtime/grid-rows.js +9 -0
  108. package/runtime/module-preload.js +15 -0
  109. package/runtime/search.js +69 -0
  110. package/runtime/spatial-audio.js +57 -0
  111. package/scripts/serve.py +26 -0
@@ -0,0 +1,65 @@
1
+ import { clamp } from '../math/scalar.js';
2
+
3
+ // The box round a line of [x, z] points: [x0, x1, z0, z1].
4
+ function pathBox(line) {
5
+ const box = [Infinity, -Infinity, Infinity, -Infinity];
6
+ for (const [x, z] of line) {
7
+ box[0] = Math.min(box[0], x);
8
+ box[1] = Math.max(box[1], x);
9
+ box[2] = Math.min(box[2], z);
10
+ box[3] = Math.max(box[3], z);
11
+ }
12
+ return box;
13
+ }
14
+
15
+ // Whether (x, z) lies within `reach` of the path ({ line, box }); on a hit, fill the caller’s result.
16
+ function nearestPathPoint(c, x, z, reach, near) {
17
+ const b = c.box;
18
+ if (x < b[0] - reach || x > b[1] + reach || z < b[2] - reach || z > b[3] + reach) return false;
19
+ let best = reach * reach,
20
+ at = -1;
21
+ for (let i = 0; i < c.line.length - 1; i++) {
22
+ const p = c.line[i],
23
+ q = c.line[i + 1],
24
+ dx = q[0] - p[0],
25
+ dz = q[1] - p[1],
26
+ px = x - p[0],
27
+ pz = z - p[1],
28
+ t = clamp((px * dx + pz * dz) / (dx * dx + dz * dz), 0, 1),
29
+ d = (px - dx * t) ** 2 + (pz - dz * t) ** 2;
30
+ if (d < best) {
31
+ best = d;
32
+ at = (i + t) / (c.line.length - 1);
33
+ near.side = dx * pz - dz * px < 0 ? -1 : 1;
34
+ }
35
+ }
36
+ if (at < 0) return false;
37
+ near.d = Math.sqrt(best);
38
+ near.t = at;
39
+ return true;
40
+ }
41
+
42
+ // Stamp samples onto an XZ grid. The radius bounds the square of cells visited around each sample.
43
+ function stampPath({ x0, z0, width, height, spacing }, pts, radius, grid, valueGrid, valueFn) {
44
+ if (!(spacing > 0) || !Number.isFinite(spacing) || !Number.isFinite(radius) || radius < 0) throw new RangeError('Expected positive spacing and a nonnegative radius');
45
+ const rc = Math.ceil(radius / spacing);
46
+ for (let s = 0; s < pts.length; s++) {
47
+ const px = pts[s][0],
48
+ pz = pts[s][1],
49
+ ci = Math.round((px - x0) / spacing),
50
+ cj = Math.round((pz - z0) / spacing);
51
+ const val = valueFn ? valueFn(pts.length > 1 ? s / (pts.length - 1) : 0) : 0;
52
+ for (let j = Math.max(0, cj - rc); j <= Math.min(height - 1, cj + rc); j++) {
53
+ for (let i = Math.max(0, ci - rc); i <= Math.min(width - 1, ci + rc); i++) {
54
+ const d = Math.hypot(i * spacing + x0 - px, j * spacing + z0 - pz),
55
+ k = j * width + i;
56
+ if (d < grid[k]) {
57
+ grid[k] = d;
58
+ if (valueGrid) valueGrid[k] = val;
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ export { nearestPathPoint, pathBox, stampPath };
@@ -0,0 +1,58 @@
1
+ import * as THREE from 'three';
2
+
3
+ /**
4
+ * `createSaggingTube({spanSegments,radialSegments,radius,slack,extraLength})` returns an allocation-free update for a
5
+ * tube hanging between world-space anchors. Pass a Vector3 array, its active count and caller-owned position and
6
+ * normal arrays. Allocate three floats per vertex for `((maxAnchors−1) × spanSegments + 1) × radialSegments` vertices.
7
+ * Slack multiplies each span’s length, then extraLength adds metres. The caller owns indices, materials and drawing.
8
+ */
9
+ export function createSaggingTube({ spanSegments, radialSegments, radius, slack = 1, extraLength = 0 }) {
10
+ if (!Number.isInteger(spanSegments) || spanSegments < 1 || !Number.isInteger(radialSegments) || radialSegments < 3) {
11
+ throw new RangeError('Expected at least one span segment and three radial segments');
12
+ }
13
+ if (![radius, slack, extraLength].every(Number.isFinite) || radius <= 0 || slack < 1 || extraLength < 0) {
14
+ throw new RangeError('Expected a positive radius, slack of at least one and nonnegative extra length');
15
+ }
16
+ const _p = new THREE.Vector3(),
17
+ _t = new THREE.Vector3(),
18
+ _n = new THREE.Vector3(),
19
+ _b = new THREE.Vector3(),
20
+ UP = new THREE.Vector3(0, 1, 0);
21
+ return function update(anchors, count, pos, nor) {
22
+ let ring = 0;
23
+ for (let s = 0; s < count - 1; s++) {
24
+ const from = anchors[s],
25
+ to = anchors[s + 1],
26
+ span = from.distanceTo(to),
27
+ length = span * slack + extraLength,
28
+ sag = Math.sqrt((3 * span * Math.max(0, length - span)) / 8); // a parabola of that length over that span
29
+ for (let j = s ? 1 : 0; j <= spanSegments; j++, ring++) {
30
+ const u = j / spanSegments;
31
+ _p.lerpVectors(from, to, u);
32
+ _p.y -= sag * 4 * u * (1 - u);
33
+ _t.subVectors(to, from);
34
+ _t.y -= sag * 4 * (1 - 2 * u);
35
+ _t.normalize();
36
+ _n.crossVectors(_t, UP);
37
+ if (_n.lengthSq() < 1e-6) _n.set(1, 0, 0);
38
+ _n.normalize();
39
+ _b.crossVectors(_n, _t);
40
+ for (let k = 0; k < radialSegments; k++) {
41
+ const a = (k / radialSegments) * Math.PI * 2,
42
+ c = Math.cos(a),
43
+ d = Math.sin(a),
44
+ o = (ring * radialSegments + k) * 3,
45
+ nx = _n.x * c + _b.x * d,
46
+ ny = _n.y * c + _b.y * d,
47
+ nz = _n.z * c + _b.z * d;
48
+ nor[o] = nx;
49
+ nor[o + 1] = ny;
50
+ nor[o + 2] = nz;
51
+ pos[o] = _p.x + nx * radius;
52
+ pos[o + 1] = _p.y + ny * radius;
53
+ pos[o + 2] = _p.z + nz * radius;
54
+ }
55
+ }
56
+ }
57
+ };
58
+ }
@@ -0,0 +1,101 @@
1
+ // A sheet sampled on a grid with smooth normals: fn(u, v) -> [x, y, z]. Sagging canvas, flags, earth banks and rubble mounds are all made with it.
2
+ // col is one colour, or col(u, v) for each cell of the grid; with o.blend, col(u, v) is read at each point of the grid instead and
3
+ // blended across the cells (ground whose colour changes smoothly, without a patchwork of cells).
4
+ const buffers = [];
5
+ // Callbacks may themselves sample another surface. Only inactive buffers are reused.
6
+ export function sampleSurface(gb, nu, nv, fn, col, o = {}) {
7
+ const scratch = buffers.pop() ?? { positions: [], normals: [], colors: [] };
8
+ try {
9
+ writeSurface(gb, nu, nv, fn, col, o, scratch);
10
+ } finally {
11
+ buffers.push(scratch);
12
+ }
13
+ }
14
+
15
+ function writeSurface(gb, nu, nv, fn, col, o, scratch) {
16
+ const w1 = nu + 1,
17
+ P_ = scratch.positions,
18
+ N_ = scratch.normals,
19
+ cf = typeof col === 'function',
20
+ back = o.back,
21
+ fl = o.flip ? -1 : 1,
22
+ blend = cf && o.blend,
23
+ C_ = scratch.colors;
24
+ for (let j = 0; j <= nv; j++)
25
+ for (let i = 0; i <= nu; i++) {
26
+ const p = fn(i / nu, j / nv),
27
+ q = (j * w1 + i) * 3;
28
+ P_[q] = p[0];
29
+ P_[q + 1] = p[1];
30
+ P_[q + 2] = p[2];
31
+ if (blend) {
32
+ const c = col(i / nu, j / nv);
33
+ C_[q] = c[0];
34
+ C_[q + 1] = c[1];
35
+ C_[q + 2] = c[2];
36
+ }
37
+ }
38
+ for (let j = 0; j <= nv; j++)
39
+ for (let i = 0; i <= nu; i++) {
40
+ const a = (j * w1 + Math.max(0, i - 1)) * 3,
41
+ b = (j * w1 + Math.min(nu, i + 1)) * 3,
42
+ c = (Math.max(0, j - 1) * w1 + i) * 3,
43
+ d = (Math.min(nv, j + 1) * w1 + i) * 3,
44
+ q = (j * w1 + i) * 3;
45
+ const tx = P_[b] - P_[a],
46
+ ty = P_[b + 1] - P_[a + 1],
47
+ tz = P_[b + 2] - P_[a + 2],
48
+ sx = P_[d] - P_[c],
49
+ sy = P_[d + 1] - P_[c + 1],
50
+ sz = P_[d + 2] - P_[c + 2];
51
+ const nx = sy * tz - sz * ty,
52
+ ny = sz * tx - sx * tz,
53
+ nz = sx * ty - sy * tx,
54
+ l = (Math.hypot(nx, ny, nz) || 1) * fl;
55
+ N_[q] = nx / l;
56
+ N_[q + 1] = ny / l;
57
+ N_[q + 2] = nz / l;
58
+ }
59
+ const V = (i, j, s, cc) => {
60
+ const q = (j * w1 + i) * 3;
61
+ if (cc) gb.raw(P_[q], P_[q + 1], P_[q + 2], N_[q] * s, N_[q + 1] * s, N_[q + 2] * s, cc[0], cc[1], cc[2]);
62
+ else gb.raw(P_[q], P_[q + 1], P_[q + 2], N_[q] * s, N_[q + 1] * s, N_[q + 2] * s, C_[q], C_[q + 1], C_[q + 2]);
63
+ };
64
+ for (let j = 0; j < nv; j++)
65
+ for (let i = 0; i < nu; i++) {
66
+ const cc = blend ? null : cf ? col((i + 0.5) / nu, (j + 0.5) / nv) : col,
67
+ bc = back === true ? cc : back;
68
+ if (fl > 0) {
69
+ V(i, j, 1, cc);
70
+ V(i, j + 1, 1, cc);
71
+ V(i + 1, j + 1, 1, cc);
72
+ V(i, j, 1, cc);
73
+ V(i + 1, j + 1, 1, cc);
74
+ V(i + 1, j, 1, cc);
75
+ } else {
76
+ V(i, j, 1, cc);
77
+ V(i + 1, j + 1, 1, cc);
78
+ V(i, j + 1, 1, cc);
79
+ V(i, j, 1, cc);
80
+ V(i + 1, j, 1, cc);
81
+ V(i + 1, j + 1, 1, cc);
82
+ }
83
+ if (back) {
84
+ if (fl > 0) {
85
+ V(i, j, -1, bc);
86
+ V(i + 1, j + 1, -1, bc);
87
+ V(i, j + 1, -1, bc);
88
+ V(i, j, -1, bc);
89
+ V(i + 1, j, -1, bc);
90
+ V(i + 1, j + 1, -1, bc);
91
+ } else {
92
+ V(i, j, -1, bc);
93
+ V(i, j + 1, -1, bc);
94
+ V(i + 1, j + 1, -1, bc);
95
+ V(i, j, -1, bc);
96
+ V(i + 1, j + 1, -1, bc);
97
+ V(i + 1, j, -1, bc);
98
+ }
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,32 @@
1
+ // Deterministic cell samples around a moving observer, from fine inner cover to coarse outer cover.
2
+ export function createScatterGrid({ rings, hash, seeds, fill, start, finish }) {
3
+ const grids = rings.map(ring => ({ ...ring, x: Infinity, z: Infinity, halfUsed: -1, count: 0, inner: 0, outer: 0 }));
4
+ const update = (x, z, density = 1) => {
5
+ let changed = false;
6
+ let inner = 0;
7
+ for (let level = 0; level < grids.length; level++) {
8
+ const grid = grids[level];
9
+ const half = Math.round(grid.half * Math.max(0, Math.min(1, density)));
10
+ grid.inner = inner;
11
+ grid.outer = Math.max(inner, half * grid.cell - grid.refill);
12
+ inner = grid.outer;
13
+ if (grid.halfUsed === half && Math.abs(x - grid.x) + Math.abs(z - grid.z) < grid.refill) continue;
14
+ const ix = Math.floor(x / grid.cell), iz = Math.floor(z / grid.cell);
15
+ grid.x = x;
16
+ grid.z = z;
17
+ grid.halfUsed = half;
18
+ grid.count = 0;
19
+ start?.(level);
20
+ for (let row = iz - half; row < iz + half; row++) {
21
+ for (let col = ix - half; col < ix + half; col++) {
22
+ const h1 = hash(col, row, seeds[0]), h2 = hash(col, row, seeds[1]);
23
+ if (fill(level, grid.count, (col + h1) * grid.cell, (row + h2) * grid.cell, h1, h2, col, row, grid.cell)) grid.count++;
24
+ }
25
+ }
26
+ changed = true;
27
+ }
28
+ if (changed) finish?.(grids);
29
+ return changed;
30
+ };
31
+ return update;
32
+ }
@@ -0,0 +1,263 @@
1
+ import * as THREE from 'three';
2
+ import { TAU, clamp } from '../math/scalar.js';
3
+
4
+ // Scopes mutable scratch space, LOD selection and cached geometry to a single consumer.
5
+ export function createSculptor({ vertexTag } = {}) {
6
+ const _lp = [],
7
+ _ln = [],
8
+ _lt = [],
9
+ _pt = [0, 0, 0];
10
+ function setLodLo(level) {
11
+ LOD_LO = level;
12
+ }
13
+ // Level 1 halves detail. Level 2 is for the smallest screen-space silhouettes.
14
+ let LOD_LO = 0;
15
+ // rings: [y, rx, rz, cx, cz, col, bone1, bone2, weight1]; a ring marked `fine` is left out of the far and light versions.
16
+ // o.fold(v, a) scales the radius (pleats); o.lay(p, a, ring) moves a ring's point p = [x, y, z] at angle a from where the level
17
+ // ring puts it (a hem that dips, a ring that slopes, a muscle's swell); o.tint(p, a, ring) is how bright the ring's colour is
18
+ // there (a crease's shade, at most 1); o.sk(y, a, ring) gives a point's bones and weight in place of its ring's; o.a0/o.a1
19
+ // give an open sheet; o.joined puts each point in once and joins the triangles through them by number (a sixth of the points,
20
+ // for a builder whose geometry is drawn by its numbers: a person's body). o.relief(y, a, ring) is what stands out of the surface
21
+ // at a ring's height y and angle a, in metres along its normal (cloth's folds): its slopes turn the normals as well, worked out
22
+ // from the relief itself and not from the points round about, so that folds only two or three rings or sides apart are lit as
23
+ // folds even on sparse meshes. The optional factory vertexTag callback classifies individual vertices.
24
+ const RELIEF_STEP = [0.002, 0.02], // how far apart the relief is read to find its slope: metres of height, radians round
25
+ _lq = [],
26
+ _lm = [];
27
+ function loft(gb, ox, oy, oz, rings, seg, o = {}) {
28
+ if (LOD_LO) {
29
+ seg = LOD_LO > 1 ? Math.max(5, Math.round(seg * 0.38)) : Math.max(5, (seg + 1) >> 1);
30
+ rings = rings.filter(r => !r.fine);
31
+ }
32
+ const nr = rings.length,
33
+ a0 = o.a0 ?? 0,
34
+ a1 = o.a1 ?? TAU,
35
+ closed = a1 - a0 > TAU - 1e-4,
36
+ ns = closed ? seg : seg + 1,
37
+ P_ = _lp,
38
+ N_ = _ln,
39
+ T_ = _lt,
40
+ { fold, lay, tint } = o,
41
+ up = rings[nr - 1][0] > rings[0][0];
42
+ for (let i = 0; i < nr; i++) {
43
+ const r = rings[i];
44
+ for (let j = 0; j < ns; j++) {
45
+ const a = a0 + ((a1 - a0) * j) / seg,
46
+ k = fold ? fold(i / (nr - 1), a) : 1,
47
+ q = (i * ns + j) * 3;
48
+ _pt[0] = r[3] + Math.cos(a) * r[1] * k;
49
+ _pt[1] = r[0];
50
+ _pt[2] = r[4] + Math.sin(a) * r[2] * k;
51
+ if (lay) lay(_pt, a, r);
52
+ if (tint) T_[i * ns + j] = tint(_pt, a, r);
53
+ P_[q] = ox + _pt[0];
54
+ P_[q + 1] = oy + _pt[1];
55
+ P_[q + 2] = oz + _pt[2];
56
+ }
57
+ }
58
+ const idx = (i, j) => (i * ns + (closed ? (j + ns) % ns : clamp(j, 0, ns - 1))) * 3;
59
+ for (let i = 0; i < nr; i++)
60
+ for (let j = 0; j < ns; j++) {
61
+ const A = idx(i, j - 1),
62
+ B_ = idx(i, j + 1),
63
+ C_ = idx(Math.max(0, i - 1), j),
64
+ D = idx(Math.min(nr - 1, i + 1), j),
65
+ q = (i * ns + j) * 3,
66
+ r = rings[i];
67
+ const tx = P_[B_] - P_[A],
68
+ ty = P_[B_ + 1] - P_[A + 1],
69
+ tz = P_[B_ + 2] - P_[A + 2],
70
+ ux = P_[D] - P_[C_],
71
+ uy = P_[D + 1] - P_[C_ + 1],
72
+ uz = P_[D + 2] - P_[C_ + 2];
73
+ const nx = ty * uz - tz * uy,
74
+ ny = tz * ux - tx * uz,
75
+ nz = tx * uy - ty * ux,
76
+ l = Math.hypot(nx, ny, nz) || 1,
77
+ s = nx * (P_[q] - ox - r[3]) + nz * (P_[q + 2] - oz - r[4]) < 0 ? -1 : 1;
78
+ N_[q] = (nx / l) * s;
79
+ N_[q + 1] = (ny / l) * s;
80
+ N_[q + 2] = (nz / l) * s;
81
+ }
82
+ if (o.relief) raise(rings, ns, closed, a0, (a1 - a0) / seg, idx, o.relief);
83
+ const tag = gb.tv,
84
+ sk = o.sk,
85
+ V = (i, j) => {
86
+ const q = idx(i, j),
87
+ r = rings[i],
88
+ c = r[5],
89
+ t = tint ? T_[q / 3] : 1,
90
+ a = a0 + ((a1 - a0) * j) / seg;
91
+ if (vertexTag) gb.tv = vertexTag(gb, r, a, o, tag);
92
+ const w = sk ? sk(r[0], a, r) : null;
93
+ gb.raw(
94
+ P_[q],
95
+ P_[q + 1],
96
+ P_[q + 2],
97
+ N_[q],
98
+ N_[q + 1],
99
+ N_[q + 2],
100
+ c[0] * t,
101
+ c[1] * t,
102
+ c[2] * t,
103
+ w ? w[0] : r[6] || 0,
104
+ w ? w[1] : (r[7] ?? (r[6] || 0)),
105
+ w ? w[2] : (r[8] ?? 1),
106
+ );
107
+ };
108
+ if (o.joined) {
109
+ // each point once, and the triangles through them by number
110
+ const first = gb.count / 3,
111
+ corners = [],
112
+ at = (i, j) => i * ns + (closed ? j % ns : j);
113
+ for (let i = 0; i < nr; i++) for (let j = 0; j < ns; j++) V(i, j);
114
+ for (let i = 0; i < nr - 1; i++)
115
+ for (let j = 0; j < seg; j++) {
116
+ if (up) corners.push(at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j), at(i + 1, j + 1), at(i, j + 1));
117
+ else corners.push(at(i, j), at(i + 1, j + 1), at(i + 1, j), at(i, j), at(i, j + 1), at(i + 1, j + 1));
118
+ }
119
+ gb.triangles(first, corners);
120
+ } else
121
+ for (let i = 0; i < nr - 1; i++)
122
+ for (let j = 0; j < seg; j++) {
123
+ if (up) {
124
+ V(i, j);
125
+ V(i + 1, j);
126
+ V(i + 1, j + 1);
127
+ V(i, j);
128
+ V(i + 1, j + 1);
129
+ V(i, j + 1);
130
+ } else {
131
+ V(i, j);
132
+ V(i + 1, j + 1);
133
+ V(i + 1, j);
134
+ V(i, j);
135
+ V(i, j + 1);
136
+ V(i + 1, j + 1);
137
+ }
138
+ }
139
+ if (vertexTag) gb.tv = tag;
140
+ }
141
+ // Moves a loft's points (_lp, with their normals _ln) out by its relief and turns each normal down the relief's slopes: along the
142
+ // ring (the slope for each radian, over the metres a radian is there) and from ring to ring (the slope for each metre of
143
+ // height, over the metres of surface a metre of height is there).
144
+ function raise(rings, ns, closed, a0, step, idx, relief) {
145
+ const nr = rings.length,
146
+ P_ = _lp,
147
+ N_ = _ln,
148
+ [dy, da] = RELIEF_STEP;
149
+ for (let i = 0; i < nr; i++) {
150
+ const r = rings[i],
151
+ y = r[0],
152
+ low = Math.max(0, i - 1),
153
+ high = Math.min(nr - 1, i + 1),
154
+ rise = rings[high][0] - rings[low][0];
155
+ for (let j = 0; j < ns; j++) {
156
+ const a = a0 + step * j,
157
+ q = (i * ns + j) * 3,
158
+ A = idx(i, j - 1),
159
+ B_ = idx(i, j + 1),
160
+ C_ = idx(low, j),
161
+ D = idx(high, j),
162
+ out = relief(y, a, r),
163
+ round = (relief(y, a + da, r) - relief(y, a - da, r)) / (2 * da),
164
+ up = (relief(y + dy, a, r) - relief(y - dy, a, r)) / (2 * dy),
165
+ tx = P_[B_] - P_[A],
166
+ ty = P_[B_ + 1] - P_[A + 1],
167
+ tz = P_[B_ + 2] - P_[A + 2],
168
+ ux = P_[D] - P_[C_],
169
+ uy = P_[D + 1] - P_[C_ + 1],
170
+ uz = P_[D + 2] - P_[C_ + 2],
171
+ turn = (closed ? 2 : Math.min(ns - 1, j + 1) - Math.max(0, j - 1)) * step, // radians between the two neighbours (one step at an open sheet's edge)
172
+ kt = (round * turn) / (tx * tx + ty * ty + tz * tz || 1),
173
+ ku = (up * rise) / (ux * ux + uy * uy + uz * uz || 1),
174
+ nx = N_[q] - kt * tx - ku * ux,
175
+ ny = N_[q + 1] - kt * ty - ku * uy,
176
+ nz = N_[q + 2] - kt * tz - ku * uz,
177
+ l = Math.hypot(nx, ny, nz) || 1;
178
+ _lq[q] = P_[q] + N_[q] * out;
179
+ _lq[q + 1] = P_[q + 1] + N_[q + 1] * out;
180
+ _lq[q + 2] = P_[q + 2] + N_[q + 2] * out;
181
+ _lm[q] = nx / l;
182
+ _lm[q + 1] = ny / l;
183
+ _lm[q + 2] = nz / l;
184
+ }
185
+ }
186
+ for (let k = nr * ns * 3 - 1; k >= 0; k--) {
187
+ P_[k] = _lq[k];
188
+ N_[k] = _lm[k];
189
+ }
190
+ }
191
+ const meshes = new Map();
192
+ // A sphere pushed into shape by fn(dir, outPos, channels). Cached by key; channels are a colour mix and a brightness.
193
+ function sculptMesh(key, detail, fn, warp = 0) {
194
+ if (LOD_LO > 1) {
195
+ key += ':light';
196
+ detail = Math.max(2, (detail + 1) >> 2);
197
+ } else if (LOD_LO) {
198
+ key += ':lo';
199
+ detail = Math.max(3, detail >> 1);
200
+ }
201
+ if (meshes.has(key)) return meshes.get(key);
202
+ const pa = new THREE.IcosahedronGeometry(1, detail).attributes.position.array,
203
+ n = pa.length / 3;
204
+ const P_ = new Float32Array(n * 3),
205
+ N_ = new Float32Array(n * 3),
206
+ A = new Float32Array(n),
207
+ Bc = new Float32Array(n),
208
+ D = new Float32Array(n * 3);
209
+ const d = new THREE.Vector3(),
210
+ t1 = new THREE.Vector3(),
211
+ t2 = new THREE.Vector3(),
212
+ p0 = new THREE.Vector3(),
213
+ p1 = new THREE.Vector3(),
214
+ p2 = new THREE.Vector3(),
215
+ ch = [0, 1],
216
+ c2 = [0, 1],
217
+ e = 0.02;
218
+ for (let i = 0; i < n; i++) {
219
+ d.set(pa[i * 3], pa[i * 3 + 1], pa[i * 3 + 2] + warp).normalize();
220
+ D[i * 3] = d.x;
221
+ D[i * 3 + 1] = d.y;
222
+ D[i * 3 + 2] = d.z;
223
+ fn(d, p0, ch);
224
+ t1.set(d.z, 0, -d.x);
225
+ if (t1.lengthSq() < 1e-4) t1.set(1, 0, 0);
226
+ t1.normalize();
227
+ t2.crossVectors(d, t1);
228
+ fn(t1.multiplyScalar(e).add(d).normalize(), p1, c2);
229
+ fn(t2.multiplyScalar(e).add(d).normalize(), p2, c2);
230
+ p1.sub(p0);
231
+ p2.sub(p0);
232
+ t1.crossVectors(p1, p2).normalize();
233
+ if (t1.dot(d) < 0) t1.negate();
234
+ P_[i * 3] = p0.x;
235
+ P_[i * 3 + 1] = p0.y;
236
+ P_[i * 3 + 2] = p0.z;
237
+ N_[i * 3] = t1.x;
238
+ N_[i * 3 + 1] = t1.y;
239
+ N_[i * 3 + 2] = t1.z;
240
+ A[i] = ch[0];
241
+ Bc[i] = ch[1];
242
+ }
243
+ const mesh = { P: P_, N: N_, A, B: Bc, D, n };
244
+ meshes.set(key, mesh);
245
+ return mesh;
246
+ }
247
+ return { loft, sculptMesh, setLodLo };
248
+ }
249
+
250
+ const _e1 = new THREE.Vector3(),
251
+ _e2 = new THREE.Vector3();
252
+ // One triangle of points [x, y, z, nx, ny, nz, own colour, own weight], wound so that it faces the way its normals do. A point
253
+ // without a colour or a weight of its own takes col and weight.
254
+ export function triangle(gb, p, q, r, col, bone, other = bone, weight = 1) {
255
+ _e1.set(q[0] - p[0], q[1] - p[1], q[2] - p[2]);
256
+ _e2.set(r[0] - p[0], r[1] - p[1], r[2] - p[2]);
257
+ _e1.cross(_e2);
258
+ const flip = _e1.x * (p[3] + q[3] + r[3]) + _e1.y * (p[4] + q[4] + r[4]) + _e1.z * (p[5] + q[5] + r[5]) < 0;
259
+ for (const v of flip ? [p, r, q] : [p, q, r]) {
260
+ const c = v[6] || col;
261
+ gb.raw(v[0], v[1], v[2], v[3], v[4], v[5], c[0], c[1], c[2], bone, other, v[7] ?? weight);
262
+ }
263
+ }
@@ -0,0 +1,67 @@
1
+ import { clamp, lerp } from '../math/scalar.js';
2
+
3
+ // The edges of a closed outline, as the distance to it is worked out from them.
4
+ export const outlineEdges = outline =>
5
+ outline.map(([x, z], i) => {
6
+ const [ex, ez] = outline[(i + 1) % outline.length],
7
+ dx = ex - x,
8
+ dz = ez - z;
9
+ return { x, z, dx, dz, ez, inv: 1 / (dx * dx + dz * dz || 1) };
10
+ });
11
+ // The signed distance from (x, z) to an outline's edges: positive inside it.
12
+ export function outlineDistance(edges, x, z) {
13
+ let inside = false,
14
+ best = Infinity;
15
+ for (const e of edges) {
16
+ const px = x - e.x,
17
+ pz = z - e.z;
18
+ if (e.z > z !== e.ez > z && px < (pz * e.dx) / e.dz) inside = !inside;
19
+ const t = clamp((px * e.dx + pz * e.dz) * e.inv, 0, 1);
20
+ best = Math.min(best, (px - e.dx * t) ** 2 + (pz - e.dz * t) ** 2);
21
+ }
22
+ return Math.sqrt(best) * (inside ? 1 : -1);
23
+ }
24
+
25
+ export function createShoreGrid() {
26
+ let grid = null, // the distances, n to a side
27
+ n = 0,
28
+ x0 = 0,
29
+ z0 = 0,
30
+ step = 0; // metres from one point of the grid to the next
31
+
32
+ // Lays the grid out empty over the square from (x0, z0): n points a side, `step` metres apart.
33
+ function layOutShore(cornerX, cornerZ, spacing, points) {
34
+ x0 = cornerX;
35
+ z0 = cornerZ;
36
+ step = spacing;
37
+ n = points;
38
+ grid = new Float32Array(n * n);
39
+ }
40
+ // The grid's distances: what a bake fills and a later visit reads back (core/kept.js).
41
+ const shoreGrid = () => grid;
42
+ // Fills the grid from the land's outlines, each a closed line of [x, z] points.
43
+ function bakeShore(outlines) {
44
+ const edges = outlines.map(outlineEdges);
45
+ for (let j = 0; j < n; j++)
46
+ for (let i = 0; i < n; i++) {
47
+ const x = x0 + i * step,
48
+ z = z0 + j * step;
49
+ let d = -Infinity;
50
+ for (const outline of edges) d = Math.max(d, outlineDistance(outline, x, z));
51
+ grid[j * n + i] = d;
52
+ }
53
+ }
54
+ // The signed distance from (x, z) to the shore, in metres: positive inland.
55
+ function shoreDistance(x, z) {
56
+ const u = clamp((x - x0) / step, 0, n - 1.001),
57
+ v = clamp((z - z0) / step, 0, n - 1.001),
58
+ i = Math.floor(u),
59
+ j = Math.floor(v),
60
+ a = u - i,
61
+ b = v - j,
62
+ k = j * n + i;
63
+ return lerp(lerp(grid[k], grid[k + 1], a), lerp(grid[k + n], grid[k + n + 1], a), b);
64
+ }
65
+
66
+ return { bakeShore, layOutShore, shoreDistance, shoreGrid };
67
+ }