roamwright 0.2.0 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/CONTRIBUTING.md +3 -1
  2. package/README.md +19 -21
  3. package/geometry/bedding.js +141 -0
  4. package/geometry/builder-cells.js +42 -0
  5. package/geometry/builder-frame.js +47 -0
  6. package/geometry/builder.js +189 -177
  7. package/geometry/cell-mask.js +25 -0
  8. package/geometry/creased-sheet.js +34 -34
  9. package/geometry/ear.js +36 -36
  10. package/geometry/erosion.js +65 -42
  11. package/geometry/face-grid.js +65 -65
  12. package/geometry/foliage.js +47 -0
  13. package/geometry/hair-gathers.js +41 -41
  14. package/geometry/hair-locks.js +41 -41
  15. package/geometry/hair-patch.js +18 -18
  16. package/geometry/hair-shell.js +31 -31
  17. package/geometry/head-profile.js +15 -15
  18. package/geometry/head-shape.js +77 -77
  19. package/geometry/heightfield.js +27 -18
  20. package/geometry/index-parts.js +60 -0
  21. package/geometry/instance-visibility.js +41 -0
  22. package/geometry/instances.js +35 -12
  23. package/geometry/outline-grid.js +7 -7
  24. package/geometry/part-ranges.js +18 -0
  25. package/geometry/path-field.js +63 -33
  26. package/geometry/placement.js +52 -52
  27. package/geometry/point-buckets.js +50 -0
  28. package/geometry/primitives.js +63 -47
  29. package/geometry/sagging-tube.js +27 -27
  30. package/geometry/sampled-surface.js +109 -77
  31. package/geometry/scatter-grid.js +46 -6
  32. package/geometry/sculpt.js +93 -93
  33. package/geometry/shore-grid.js +77 -34
  34. package/geometry/strata-field.js +92 -92
  35. package/geometry/strata-tiles.js +91 -91
  36. package/geometry/stream.js +70 -48
  37. package/geometry/terrain-chunks.js +101 -92
  38. package/geometry/terrain-extension.js +6 -6
  39. package/index.js +5 -0
  40. package/math/cell-edge.js +16 -16
  41. package/math/cell-key.js +6 -0
  42. package/math/circle-clearances.js +19 -0
  43. package/math/cloth-textures.js +6 -6
  44. package/math/cloud-textures.js +20 -20
  45. package/math/color.js +14 -14
  46. package/math/curve.js +22 -22
  47. package/math/mountain-ranges.js +216 -165
  48. package/math/noise.js +101 -49
  49. package/math/particle-noise.js +28 -28
  50. package/math/periodic.js +5 -5
  51. package/math/random.js +24 -15
  52. package/math/rectangles.js +52 -0
  53. package/math/rock-texture.js +7 -7
  54. package/math/scalar.js +5 -5
  55. package/math/surface-textures.js +54 -54
  56. package/motion/arm-reach.js +24 -24
  57. package/motion/ballistic-arc.js +11 -0
  58. package/motion/biped-cycle.js +44 -0
  59. package/motion/biped-leg.js +37 -0
  60. package/motion/biped-stride.js +98 -122
  61. package/motion/contact-transition.js +33 -0
  62. package/motion/crowd-alarms.js +39 -39
  63. package/motion/crowd-routine.js +94 -65
  64. package/motion/daily-clock.js +9 -9
  65. package/motion/deck-surface.js +38 -0
  66. package/motion/ground-lookahead.js +29 -0
  67. package/motion/instance-sight.js +8 -8
  68. package/motion/player.js +36 -28
  69. package/motion/quadruped.js +155 -155
  70. package/motion/spaces.js +32 -32
  71. package/motion/weather.js +73 -24
  72. package/motion/wind.js +32 -13
  73. package/package.json +8 -4
  74. package/rendering/caster-motion.js +5 -5
  75. package/rendering/cell-lod.js +147 -82
  76. package/rendering/cloud-dome.js +28 -28
  77. package/rendering/cloud-field.js +2 -2
  78. package/rendering/compilation-schedule.js +68 -11
  79. package/rendering/contact-shadows.js +2 -2
  80. package/rendering/context.js +12 -0
  81. package/rendering/crowd-skinning.js +13 -13
  82. package/rendering/crust.js +18 -12
  83. package/rendering/daylight.js +3 -3
  84. package/rendering/detail-range.js +28 -0
  85. package/rendering/dispose-meshes.js +18 -0
  86. package/rendering/dof.js +9 -9
  87. package/rendering/eye-adapt.js +14 -14
  88. package/rendering/far-shadow.js +8 -8
  89. package/rendering/flocks.js +78 -78
  90. package/rendering/garment-material.js +17 -17
  91. package/rendering/glow-lines.js +13 -13
  92. package/rendering/hair-strands.js +20 -20
  93. package/rendering/height-probe.js +149 -19
  94. package/rendering/label-layout.js +46 -0
  95. package/rendering/label-range.js +38 -0
  96. package/rendering/light-budget.js +52 -52
  97. package/rendering/light-shafts.js +23 -23
  98. package/rendering/local-lights.js +15 -4
  99. package/rendering/material-lod.js +49 -49
  100. package/rendering/particle-emitters.js +90 -90
  101. package/rendering/particles.js +78 -67
  102. package/rendering/piece-batch.js +91 -26
  103. package/rendering/planar-reflection.js +38 -38
  104. package/rendering/post-shaders.js +17 -13
  105. package/rendering/post.js +71 -71
  106. package/rendering/rain-streaks.js +67 -0
  107. package/rendering/road-surface.js +11 -11
  108. package/rendering/sea-shaders.js +11 -11
  109. package/rendering/shader-warmup.js +29 -16
  110. package/rendering/shadow-alignment.js +4 -4
  111. package/rendering/shadow-depth.js +7 -7
  112. package/rendering/sky-occlusion.js +16 -16
  113. package/rendering/sky.js +16 -6
  114. package/rendering/stage-lights.js +6 -6
  115. package/rendering/static-point-shadow.js +3 -3
  116. package/rendering/still-casters.js +1 -1
  117. package/rendering/strata-material.js +10 -10
  118. package/rendering/stream-flow.js +10 -10
  119. package/rendering/taa.js +26 -26
  120. package/rendering/talus.js +15 -15
  121. package/rendering/terrain-rock.js +7 -7
  122. package/rendering/texture-fills.js +8 -8
  123. package/rendering/textures.js +28 -28
  124. package/rendering/wakes.js +24 -24
  125. package/rendering/water-material.js +36 -23
  126. package/rendering/world-matrices.js +38 -38
  127. package/runtime/diagnostic-log.js +2 -2
  128. package/runtime/first-use.js +33 -28
  129. package/runtime/frame-budget.js +49 -0
  130. package/runtime/frame-pacing.js +33 -0
  131. package/runtime/gpu-timing.js +13 -10
  132. package/runtime/grid-cache.js +1 -1
  133. package/runtime/grid-rows.js +44 -5
  134. package/runtime/hand-over.js +41 -23
  135. package/runtime/pool.js +58 -16
  136. package/runtime/quality-fit.js +1 -1
  137. package/runtime/search.js +22 -22
  138. package/runtime/spatial-audio.js +13 -13
  139. package/runtime/withdrawable-task.js +35 -0
  140. package/runtime/worker.js +7 -0
@@ -11,20 +11,20 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
11
11
  if (region) return; // planned already
12
12
  const mn = new Float32Array(MASK_SIDE * MASK_SIDE);
13
13
  region = new Uint8Array(MASK_SIDE * MASK_SIDE);
14
- for (let b = 0; b < MASK_SIDE; b++)
15
- for (let a = 0; a < MASK_SIDE; a++) mn[b * MASK_SIDE + a] = cliffMask(a * levels[2] - half, b * levels[2] - half);
16
- for (let b = 0; b < MASK_SIDE - 1; b++)
17
- for (let a = 0; a < MASK_SIDE - 1; a++) {
18
- const k = b * MASK_SIDE + a;
14
+ for (let row = 0; row < MASK_SIDE; row++)
15
+ for (let column = 0; column < MASK_SIDE; column++) mn[row * MASK_SIDE + column] = cliffMask(column * levels[2] - half, row * levels[2] - half);
16
+ for (let row = 0; row < MASK_SIDE - 1; row++)
17
+ for (let column = 0; column < MASK_SIDE - 1; column++) {
18
+ const k = row * MASK_SIDE + column;
19
19
  if (Math.max(mn[k], mn[k + 1], mn[k + MASK_SIDE], mn[k + MASK_SIDE + 1]) > 0.004) region[k] = 1;
20
20
  }
21
21
  for (let tj = 0; tj < tilesSide; tj++)
22
22
  for (let ti = 0; ti < tilesSide; ti++) {
23
23
  const n = tileSize / levels[2];
24
24
  let any = false;
25
- for (let b = tj * n; b < (tj + 1) * n && b < MASK_SIDE - 1 && !any; b++)
26
- for (let a = ti * n; a < (ti + 1) * n && a < MASK_SIDE - 1; a++)
27
- if (region[b * MASK_SIDE + a]) {
25
+ for (let row = tj * n; row < (tj + 1) * n && row < MASK_SIDE - 1 && !any; row++)
26
+ for (let column = ti * n; column < (ti + 1) * n && column < MASK_SIDE - 1; column++)
27
+ if (region[row * MASK_SIDE + column]) {
28
28
  any = true;
29
29
  break;
30
30
  }
@@ -32,55 +32,55 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
32
32
  }
33
33
  }
34
34
  const inRegion = (x, z) => {
35
- const a = Math.floor((x + half) / levels[2]),
36
- b = Math.floor((z + half) / levels[2]);
37
- return a >= 0 && b >= 0 && a < MASK_SIDE - 1 && b < MASK_SIDE - 1 && region[b * MASK_SIDE + a] === 1;
35
+ const column = Math.floor((x + half) / levels[2]),
36
+ row = Math.floor((z + half) / levels[2]);
37
+ return column >= 0 && row >= 0 && column < MASK_SIDE - 1 && row < MASK_SIDE - 1 && region[row * MASK_SIDE + column] === 1;
38
38
  };
39
39
 
40
40
  // One level's node grid over a tile, with a ring of one node round it for the normals: positions for every node that a rock cell touches
41
41
  // (and their ring), normals for the cell corners.
42
42
  const pt = { p: [0, 0, 0] };
43
- function tileGrid(t, lv, keepAttrs) {
44
- const g = levels[lv],
43
+ function tileGrid(t, levelIndex, keepAttrs) {
44
+ const g = levels[levelIndex],
45
45
  n = tileSize / g,
46
46
  w = n + 3,
47
47
  used = new Uint8Array(w * w),
48
48
  corner = new Uint8Array(w * w);
49
- for (let b = 0; b < n; b++)
50
- for (let a = 0; a < n; a++) {
51
- if (!inRegion(t.x0 + (a + 0.5) * g, t.z0 + (b + 0.5) * g)) continue;
52
- for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) used[(b + j) * w + a + i] = 1;
53
- for (let j = 1; j < 3; j++) for (let i = 1; i < 3; i++) corner[(b + j) * w + a + i] = 1;
49
+ for (let row = 0; row < n; row++)
50
+ for (let column = 0; column < n; column++) {
51
+ if (!inRegion(t.x0 + (column + 0.5) * g, t.z0 + (row + 0.5) * g)) continue;
52
+ for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) used[(row + j) * w + column + i] = 1;
53
+ for (let j = 1; j < 3; j++) for (let i = 1; i < 3; i++) corner[(row + j) * w + column + i] = 1;
54
54
  }
55
- const P = new Float32Array(w * w * 3),
56
- N = new Float32Array(w * w * 3),
57
- A = keepAttrs ? new Float32Array(w * w * 6) : null;
58
- for (let b = 0; b < w; b++)
59
- for (let a = 0; a < w; a++) {
60
- const k = b * w + a;
55
+ const positions = new Float32Array(w * w * 3),
56
+ normals = new Float32Array(w * w * 3),
57
+ attributes = keepAttrs ? new Float32Array(w * w * 6) : null;
58
+ for (let row = 0; row < w; row++)
59
+ for (let column = 0; column < w; column++) {
60
+ const k = row * w + column;
61
61
  if (!used[k]) continue;
62
- cliffPoint(t.x0 + (a - 1) * g, t.z0 + (b - 1) * g, lv, pt);
63
- P[k * 3] = pt.p[0];
64
- P[k * 3 + 1] = pt.p[1];
65
- P[k * 3 + 2] = pt.p[2];
66
- if (A) {
67
- A[k * 6] = pt.bed;
68
- A[k * 6 + 1] = pt.hard;
69
- A[k * 6 + 2] = pt.cav;
70
- A[k * 6 + 3] = pt.m;
71
- A[k * 6 + 4] = pt.yy;
62
+ cliffPoint(t.x0 + (column - 1) * g, t.z0 + (row - 1) * g, levelIndex, pt);
63
+ positions[k * 3] = pt.p[0];
64
+ positions[k * 3 + 1] = pt.p[1];
65
+ positions[k * 3 + 2] = pt.p[2];
66
+ if (attributes) {
67
+ attributes[k * 6] = pt.bed;
68
+ attributes[k * 6 + 1] = pt.hard;
69
+ attributes[k * 6 + 2] = pt.cav;
70
+ attributes[k * 6 + 3] = pt.m;
71
+ attributes[k * 6 + 4] = pt.yy;
72
72
  }
73
73
  }
74
- for (let b = 1; b < w - 1; b++)
75
- for (let a = 1; a < w - 1; a++) {
76
- const k = b * w + a;
74
+ for (let row = 1; row < w - 1; row++)
75
+ for (let column = 1; column < w - 1; column++) {
76
+ const k = row * w + column;
77
77
  if (!corner[k]) continue;
78
- const ax = P[(k + 1) * 3] - P[(k - 1) * 3],
79
- ay = P[(k + 1) * 3 + 1] - P[(k - 1) * 3 + 1],
80
- az = P[(k + 1) * 3 + 2] - P[(k - 1) * 3 + 2];
81
- const bx = P[(k + w) * 3] - P[(k - w) * 3],
82
- by = P[(k + w) * 3 + 1] - P[(k - w) * 3 + 1],
83
- bz = P[(k + w) * 3 + 2] - P[(k - w) * 3 + 2];
78
+ const ax = positions[(k + 1) * 3] - positions[(k - 1) * 3],
79
+ ay = positions[(k + 1) * 3 + 1] - positions[(k - 1) * 3 + 1],
80
+ az = positions[(k + 1) * 3 + 2] - positions[(k - 1) * 3 + 2];
81
+ const bx = positions[(k + w) * 3] - positions[(k - w) * 3],
82
+ by = positions[(k + w) * 3 + 1] - positions[(k - w) * 3 + 1],
83
+ bz = positions[(k + w) * 3 + 2] - positions[(k - w) * 3 + 2];
84
84
  let nx = by * az - bz * ay,
85
85
  ny = bz * ax - bx * az,
86
86
  nz = bx * ay - by * ax;
@@ -88,38 +88,38 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
88
88
  nx /= l;
89
89
  ny /= l;
90
90
  nz /= l;
91
- N[k * 3] = nx;
92
- N[k * 3 + 1] = ny;
93
- N[k * 3 + 2] = nz;
91
+ normals[k * 3] = nx;
92
+ normals[k * 3 + 1] = ny;
93
+ normals[k * 3 + 2] = nz;
94
94
  }
95
- return { g, n, w, P, N, A, corner };
95
+ return { g, n, w, P: positions, N: normals, A: attributes, corner };
96
96
  }
97
97
  // A coarser grid's surface at a point of the tile, interpolated over its triangles as that level's mesh draws them.
98
- function sample(c, lx, lz, arr, out) {
99
- const fx = Math.min(lx / c.g, c.n - 1e-6),
100
- fz = Math.min(lz / c.g, c.n - 1e-6),
98
+ function sample(tile, lx, lz, arr, out) {
99
+ const fx = Math.min(lx / tile.g, tile.n - 1e-6),
100
+ fz = Math.min(lz / tile.g, tile.n - 1e-6),
101
101
  i = Math.floor(fx),
102
102
  j = Math.floor(fz),
103
103
  tx = fx - i,
104
104
  tz = fz - j;
105
- const k = (j + 1) * c.w + i + 1,
106
- a = k * 3,
107
- b = (k + 1) * 3,
108
- d = (k + c.w) * 3,
109
- e = (k + c.w + 1) * 3;
105
+ const k = (j + 1) * tile.w + i + 1,
106
+ nearLeft = k * 3,
107
+ nearRight = (k + 1) * 3,
108
+ farLeft = (k + tile.w) * 3,
109
+ e = (k + tile.w + 1) * 3;
110
110
  for (let q = 0; q < 3; q++)
111
111
  out[q] =
112
112
  tx + tz <= 1
113
- ? arr[a + q] + (arr[b + q] - arr[a + q]) * tx + (arr[d + q] - arr[a + q]) * tz
114
- : arr[e + q] + (arr[d + q] - arr[e + q]) * (1 - tx) + (arr[b + q] - arr[e + q]) * (1 - tz);
113
+ ? arr[nearLeft + q] + (arr[nearRight + q] - arr[nearLeft + q]) * tx + (arr[farLeft + q] - arr[nearLeft + q]) * tz
114
+ : arr[e + q] + (arr[farLeft + q] - arr[e + q]) * (1 - tx) + (arr[nearRight + q] - arr[e + q]) * (1 - tz);
115
115
  return out;
116
116
  }
117
117
 
118
118
  // The arrays of one level of a box of tiles, as a generator that yields now and then so the work can be spread over frames; it returns
119
119
  // { attributes: { name: [array, itemSize] }, index, bounds: [tile id, lowest y, highest y, ...] }.
120
- function* boxArrays(lv, list) {
121
- const pos = [],
122
- nor = [],
120
+ function* boxArrays(levelIndex, list) {
121
+ const positions = [],
122
+ normals = [],
123
123
  m1 = [],
124
124
  n1 = [],
125
125
  m2 = [],
@@ -128,30 +128,30 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
128
128
  rock = [],
129
129
  yy = [],
130
130
  par = [],
131
- idx = [],
131
+ indices = [],
132
132
  s3 = [0, 0, 0],
133
133
  c3 = [0, 0, 0],
134
134
  bounds = [];
135
135
  for (const t of list) {
136
- const f = tileGrid(t, lv, true);
136
+ const f = tileGrid(t, levelIndex, true);
137
137
  yield;
138
- const c1 = lv < 2 ? tileGrid(t, lv + 1, false) : null,
139
- c2 = lv < 1 ? tileGrid(t, 2, false) : null;
138
+ const c1 = levelIndex < 2 ? tileGrid(t, levelIndex + 1, false) : null,
139
+ c2 = levelIndex < 1 ? tileGrid(t, 2, false) : null;
140
140
  if (c1) yield;
141
141
  const map = new Int32Array(f.w * f.w).fill(-1),
142
- base = pos.length / 3;
142
+ base = positions.length / 3;
143
143
  let vi = base,
144
144
  y0 = 1e9,
145
145
  y1 = -1e9;
146
- for (let b = 1; b < f.w - 1; b++)
147
- for (let a = 1; a < f.w - 1; a++) {
148
- const k = b * f.w + a;
146
+ for (let row = 1; row < f.w - 1; row++)
147
+ for (let column = 1; column < f.w - 1; column++) {
148
+ const k = row * f.w + column;
149
149
  if (!f.corner[k]) continue;
150
150
  map[k] = vi++;
151
- pos.push(f.P[k * 3], f.P[k * 3 + 1], f.P[k * 3 + 2]);
152
- nor.push(f.N[k * 3], f.N[k * 3 + 1], f.N[k * 3 + 2]);
153
- const lx = (a - 1) * f.g,
154
- lz = (b - 1) * f.g,
151
+ positions.push(f.P[k * 3], f.P[k * 3 + 1], f.P[k * 3 + 2]);
152
+ normals.push(f.N[k * 3], f.N[k * 3 + 1], f.N[k * 3 + 2]);
153
+ const lx = (column - 1) * f.g,
154
+ lz = (row - 1) * f.g,
155
155
  x = t.x0 + lx,
156
156
  z = t.z0 + lz;
157
157
  if (c1) {
@@ -174,31 +174,31 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
174
174
  y1 = Math.max(y1, f.P[k * 3 + 1]);
175
175
  }
176
176
  bounds.push(t.id, y0, y1);
177
- for (let b = 1; b < f.w - 2; b++)
178
- for (let a = 1; a < f.w - 2; a++) {
179
- if (!inRegion(t.x0 + (a - 0.5) * f.g, t.z0 + (b - 0.5) * f.g)) continue;
180
- const i00 = map[b * f.w + a],
181
- i10 = map[b * f.w + a + 1],
182
- i01 = map[(b + 1) * f.w + a],
183
- i11 = map[(b + 1) * f.w + a + 1];
184
- idx.push(i00, i01, i10, i10, i01, i11);
177
+ for (let row = 1; row < f.w - 2; row++)
178
+ for (let column = 1; column < f.w - 2; column++) {
179
+ if (!inRegion(t.x0 + (column - 0.5) * f.g, t.z0 + (row - 0.5) * f.g)) continue;
180
+ const i00 = map[row * f.w + column],
181
+ i10 = map[row * f.w + column + 1],
182
+ i01 = map[(row + 1) * f.w + column],
183
+ i11 = map[(row + 1) * f.w + column + 1];
184
+ indices.push(i00, i01, i10, i10, i01, i11);
185
185
  }
186
186
  yield;
187
187
  }
188
- const F = (a, n) => [new Float32Array(a), n],
189
- attributes = { position: F(pos, 3), normal: F(nor, 3), color: F(col, 3), aRock: F(rock, 4), aYY: F(yy, 1), aParam: F(par, 3) };
190
- if (lv < 2) Object.assign(attributes, { aM1: F(m1, 3), aN1: F(n1, 3) });
191
- if (lv < 1) Object.assign(attributes, { aM2: F(m2, 3), aN2: F(n2, 3) });
192
- return { attributes, index: pos.length / 3 > 65535 ? new Uint32Array(idx) : new Uint16Array(idx), bounds };
188
+ const packAttribute = (values, n) => [new Float32Array(values), n],
189
+ attributes = { position: packAttribute(positions, 3), normal: packAttribute(normals, 3), color: packAttribute(col, 3), aRock: packAttribute(rock, 4), aYY: packAttribute(yy, 1), aParam: packAttribute(par, 3) };
190
+ if (levelIndex < 2) Object.assign(attributes, { aM1: packAttribute(m1, 3), aN1: packAttribute(n1, 3) });
191
+ if (levelIndex < 1) Object.assign(attributes, { aM2: packAttribute(m2, 3), aN2: packAttribute(n2, 3) });
192
+ return { attributes, index: positions.length / 3 > 65535 ? new Uint32Array(indices) : new Uint16Array(indices), bounds };
193
193
  }
194
- const boxKey = (t, lv) => Math.floor((t.x0 + half) / boxSizes[lv]) + ':' + Math.floor((t.z0 + half) / boxSizes[lv]);
195
- function tilesIn(lv, key) {
196
- return tiles.filter(t => boxKey(t, lv) === key);
194
+ const boxKey = (t, levelIndex) => Math.floor((t.x0 + half) / boxSizes[levelIndex]) + ':' + Math.floor((t.z0 + half) / boxSizes[levelIndex]);
195
+ function tilesIn(levelIndex, key) {
196
+ return tiles.filter(t => boxKey(t, levelIndex) === key);
197
197
  }
198
198
 
199
- // The arrays of the box `key` at level `lv`, all at once.
200
- function boxNow(lv, key) {
201
- const it = boxArrays(lv, tilesIn(lv, key));
199
+ // The arrays of the box `key` at level `levelIndex`, all at once.
200
+ function boxNow(levelIndex, key) {
201
+ const it = boxArrays(levelIndex, tilesIn(levelIndex, key));
202
202
  for (;;) {
203
203
  const step = it.next();
204
204
  if (step.done) return step.value;
@@ -1,13 +1,11 @@
1
+ /**
2
+ * Build river ribbons from a centreline, a water level and a sampled bed.
3
+ * @module
4
+ */
5
+
1
6
  import * as THREE from 'three';
2
7
  import { clamp, lerp, sstep } from '../math/scalar.js';
3
8
 
4
- // A stream's ribbon of water along pts, width metres wide, its surface at surface(t) and its bed's heights in bed (for how
5
- // fast it runs). It rises at a spring: a small pool, then a rill that widens into the stream over the first `grows` metres, so
6
- // the ribbon begins as a point, never as a square end. With `banks` ({ ground(x, z), reach, grows }) the ribbon is laid `reach`
7
- // metres wider than the stream on each side and knows how deep it lies over the ground, so its edge is where the bank rises
8
- // out of it; without, it is a strip of the stream's own width with soft sides. `fadeAt(x, z)`, if given, is how much of the
9
- // ribbon shows at a point of its line (0 to 1): where it runs on into still water drawn beside it (a lake's sheet), the two
10
- // cross over, so it has no square end there either.
11
9
  const DEEP = 8, // the depth the shader takes for water that knows no bed
12
10
  FAR = 99, // how far from a spring's eye water is that has none
13
11
  MURK = 7, // a stream's water hides its bed this many times sooner than the lake's
@@ -27,65 +25,89 @@ const MIDDLE = ACROSS.indexOf(0),
27
25
  function shutBeyondBanks(bank, first) {
28
26
  for (const step of [-1, 1]) {
29
27
  let dry = false;
30
- for (let k = MIDDLE + step; k >= 0 && k < ACROSS.length; k += step) {
31
- const depth = (first + k) * 4;
28
+ for (let crossIndex = MIDDLE + step; crossIndex >= 0 && crossIndex < ACROSS.length; crossIndex += step) {
29
+ const depth = (first + crossIndex) * 4;
32
30
  if (dry) bank[depth] = Math.min(bank[depth], DRY);
33
31
  else dry = bank[depth] < 0;
34
32
  }
35
33
  }
36
34
  }
37
- export function streamGeometry(pts, width, surface, bed, banks = null, fadeAt = null) {
38
- const n = pts.length,
35
+ /**
36
+ * Build an indexed ribbon from spring to mouth. The ribbon starts at a point
37
+ * and widens downstream. When terrain is supplied, the water shader clips the
38
+ * ribbon at the first raised bank on each side of a cross-section.
39
+ * @param {[number, number][]} points - At least two downstream-ordered [x, z] points in metres.
40
+ * @param {number} width - Full stream width in metres once the channel has widened.
41
+ * @param {(t: number) => number} surface - Water elevation in metres at `t = i / (pts.length - 1)`.
42
+ * Samples are spaced by point index. Unevenly spaced points get equal increments in t.
43
+ * @param {ArrayLike<number>} bed - One bed elevation per point. The local drop drives the flow shader.
44
+ * @param {{ground: (x: number, z: number) => number, reach: number, grows?: number} | null} [banks] -
45
+ * Terrain elevation sampler, extra reach on each bank in metres and optional downstream widening distance.
46
+ * @param {((x: number, z: number) => number) | null} [fadeAt] - Visible share (0–1) along the centreline,
47
+ * useful where the stream meets a lake.
48
+ * @returns An owned BufferGeometry with position, flow, current and bank attributes for the stream material.
49
+ * @example
50
+ * ```js
51
+ * import { streamGeometry } from 'roamwright/geometry/stream.js';
52
+ *
53
+ * const channel = [[0, 0], [1, 8], [3, 16]];
54
+ * const water = streamGeometry(channel, 4, t => 3 - t, [2.4, 1.8, 1.2]);
55
+ * // Pass water to a mesh using the stream material, then dispose it on removal.
56
+ * water.dispose();
57
+ * ```
58
+ */
59
+ export function streamGeometry(points, width, surface, bed, banks = null, fadeAt = null) {
60
+ const pointCount = points.length,
39
61
  across = ACROSS.length,
40
- pos = new Float32Array(n * across * 3),
41
- flow = new Float32Array(n * across * 3),
42
- current = new Float32Array(n * across * 2),
43
- bank = new Float32Array(n * across * 4),
44
- idx = [],
62
+ positions = new Float32Array(pointCount * across * 3),
63
+ flow = new Float32Array(pointCount * across * 3),
64
+ current = new Float32Array(pointCount * across * 2),
65
+ bank = new Float32Array(pointCount * across * 4),
66
+ indices = [],
45
67
  reach = width * 0.5 + (banks ? banks.reach : 0),
46
68
  grows = banks?.grows ?? SPRING.grows * width;
47
69
  let run = 0;
48
- for (let i = 0; i < n; i++) {
49
- const a = pts[Math.max(0, i - 1)],
50
- b = pts[Math.min(n - 1, i + 1)];
51
- let dx = b[0] - a[0],
52
- dz = b[1] - a[1];
53
- const l = Math.hypot(dx, dz) || 1;
54
- dx /= l;
55
- dz /= l;
56
- if (i > 0) run += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]);
57
- const y = surface(i / (n - 1)),
58
- grade = sstep(0.015, 0.32, (bed[Math.max(i - 3, 0)] - bed[Math.min(i + 3, n - 1)]) / 9),
70
+ for (let pointIndex = 0; pointIndex < pointCount; pointIndex++) {
71
+ const previousPoint = points[Math.max(0, pointIndex - 1)],
72
+ nextPoint = points[Math.min(pointCount - 1, pointIndex + 1)];
73
+ let dx = nextPoint[0] - previousPoint[0],
74
+ dz = nextPoint[1] - previousPoint[1];
75
+ const tangentLength = Math.hypot(dx, dz) || 1;
76
+ dx /= tangentLength;
77
+ dz /= tangentLength;
78
+ if (pointIndex > 0) run += Math.hypot(points[pointIndex][0] - points[pointIndex - 1][0], points[pointIndex][1] - points[pointIndex - 1][1]);
79
+ const y = surface(pointIndex / (pointCount - 1)),
80
+ grade = sstep(0.015, 0.32, (bed[Math.max(pointIndex - 3, 0)] - bed[Math.min(pointIndex + 3, pointCount - 1)]) / 9),
59
81
  share = headShare(run, width, grows),
60
- rising = sstep(0, SPRING.fade * width, run) * (fadeAt ? fadeAt(pts[i][0], pts[i][1]) : 1);
61
- for (let k = 0; k < across; k++) {
62
- const side = ACROSS[k],
82
+ rising = sstep(0, SPRING.fade * width, run) * (fadeAt ? fadeAt(points[pointIndex][0], points[pointIndex][1]) : 1);
83
+ for (let crossIndex = 0; crossIndex < across; crossIndex++) {
84
+ const side = ACROSS[crossIndex],
63
85
  // a strip with no banks to shape it wanders a little in width
64
86
  wander = banks
65
87
  ? 1
66
88
  : 0.89 + 0.09 * Math.sin(run * 0.23 + Math.sign(side) * 1.2) + 0.025 * Math.sin(run * 0.87 - Math.sign(side) * 1.4),
67
- off = side * reach * share * wander,
68
- x = pts[i][0] - dz * off,
69
- z = pts[i][1] + dx * off,
70
- v = i * across + k;
71
- pos.set([x, y, z], v * 3);
72
- flow.set([off / (width * 0.5), run, grade], v * 3);
73
- current.set([dx, dz], v * 2);
89
+ lateralOffset = side * reach * share * wander,
90
+ x = points[pointIndex][0] - dz * lateralOffset,
91
+ z = points[pointIndex][1] + dx * lateralOffset,
92
+ vertexIndex = pointIndex * across + crossIndex;
93
+ positions.set([x, y, z], vertexIndex * 3);
94
+ flow.set([lateralOffset / (width * 0.5), run, grade], vertexIndex * 3);
95
+ current.set([dx, dz], vertexIndex * 2);
74
96
  bank.set(
75
- [banks ? (y - banks.ground(x, z)) * MURK : DEEP, rising * sstep(1, 0.75, Math.abs(side)), run - springEye(width), off],
76
- v * 4,
97
+ [banks ? (y - banks.ground(x, z)) * MURK : DEEP, rising * sstep(1, 0.75, Math.abs(side)), run - springEye(width), lateralOffset],
98
+ vertexIndex * 4,
77
99
  );
78
- if (i < n - 1 && k < across - 1) idx.push(v, v + 1, v + across, v + 1, v + across + 1, v + across);
100
+ if (pointIndex < pointCount - 1 && crossIndex < across - 1) indices.push(vertexIndex, vertexIndex + 1, vertexIndex + across, vertexIndex + 1, vertexIndex + across + 1, vertexIndex + across);
79
101
  }
80
- if (banks) shutBeyondBanks(bank, i * across);
102
+ if (banks) shutBeyondBanks(bank, pointIndex * across);
81
103
  }
82
- const g = new THREE.BufferGeometry();
83
- g.setAttribute('position', new THREE.BufferAttribute(pos, 3));
84
- g.setAttribute('aFlow', new THREE.BufferAttribute(flow, 3));
85
- g.setAttribute('aCurrent', new THREE.BufferAttribute(current, 2));
86
- g.setAttribute('aBank', new THREE.BufferAttribute(bank, 4));
87
- g.setIndex(idx);
88
- return g;
104
+ const geometry = new THREE.BufferGeometry();
105
+ geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
106
+ geometry.setAttribute('aFlow', new THREE.BufferAttribute(flow, 3));
107
+ geometry.setAttribute('aCurrent', new THREE.BufferAttribute(current, 2));
108
+ geometry.setAttribute('aBank', new THREE.BufferAttribute(bank, 4));
109
+ geometry.setIndex(indices);
110
+ return geometry;
89
111
  }
90
112
 
91
113
  // How far below its spring a stream of this width has the eye of its pool, in metres.