roamwright 0.4.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.
- package/CONTRIBUTING.md +3 -1
- package/README.md +11 -34
- package/geometry/bedding.js +141 -0
- package/geometry/builder-cells.js +42 -0
- package/geometry/builder-frame.js +47 -0
- package/geometry/builder.js +189 -177
- package/geometry/cell-mask.js +25 -0
- package/geometry/creased-sheet.js +34 -34
- package/geometry/ear.js +36 -36
- package/geometry/erosion.js +65 -42
- package/geometry/face-grid.js +65 -65
- package/geometry/foliage.js +10 -10
- package/geometry/hair-gathers.js +41 -41
- package/geometry/hair-locks.js +41 -41
- package/geometry/hair-patch.js +18 -18
- package/geometry/hair-shell.js +31 -31
- package/geometry/head-profile.js +15 -15
- package/geometry/head-shape.js +77 -77
- package/geometry/heightfield.js +27 -18
- package/geometry/index-parts.js +2 -2
- package/geometry/instance-visibility.js +12 -12
- package/geometry/instances.js +15 -15
- package/geometry/outline-grid.js +7 -7
- package/geometry/part-ranges.js +3 -3
- package/geometry/path-field.js +63 -33
- package/geometry/placement.js +52 -52
- package/geometry/point-buckets.js +50 -0
- package/geometry/primitives.js +63 -47
- package/geometry/sagging-tube.js +27 -27
- package/geometry/sampled-surface.js +109 -77
- package/geometry/scatter-grid.js +46 -6
- package/geometry/sculpt.js +93 -93
- package/geometry/shore-grid.js +77 -34
- package/geometry/strata-field.js +92 -92
- package/geometry/strata-tiles.js +91 -91
- package/geometry/stream.js +70 -48
- package/geometry/terrain-chunks.js +87 -82
- package/geometry/terrain-extension.js +6 -6
- package/index.js +5 -0
- package/math/cell-edge.js +16 -16
- package/math/cell-key.js +6 -0
- package/math/circle-clearances.js +19 -0
- package/math/cloth-textures.js +6 -6
- package/math/cloud-textures.js +20 -20
- package/math/color.js +14 -14
- package/math/curve.js +22 -22
- package/math/mountain-ranges.js +216 -165
- package/math/noise.js +101 -49
- package/math/particle-noise.js +28 -28
- package/math/periodic.js +5 -5
- package/math/random.js +24 -15
- package/math/rectangles.js +52 -0
- package/math/rock-texture.js +7 -7
- package/math/scalar.js +5 -5
- package/math/surface-textures.js +54 -54
- package/motion/arm-reach.js +24 -24
- package/motion/ballistic-arc.js +11 -0
- package/motion/biped-cycle.js +44 -0
- package/motion/biped-leg.js +37 -0
- package/motion/biped-stride.js +98 -122
- package/motion/contact-transition.js +33 -0
- package/motion/crowd-alarms.js +39 -39
- package/motion/crowd-routine.js +94 -65
- package/motion/daily-clock.js +9 -9
- package/motion/deck-surface.js +38 -0
- package/motion/ground-lookahead.js +5 -5
- package/motion/instance-sight.js +8 -8
- package/motion/player.js +36 -28
- package/motion/quadruped.js +155 -155
- package/motion/spaces.js +32 -32
- package/motion/weather.js +73 -24
- package/motion/wind.js +32 -13
- package/package.json +7 -3
- package/rendering/caster-motion.js +5 -5
- package/rendering/cell-lod.js +138 -82
- package/rendering/cloud-dome.js +28 -28
- package/rendering/cloud-field.js +2 -2
- package/rendering/compilation-schedule.js +68 -11
- package/rendering/contact-shadows.js +2 -2
- package/rendering/context.js +12 -0
- package/rendering/crowd-skinning.js +13 -13
- package/rendering/crust.js +18 -12
- package/rendering/daylight.js +3 -3
- package/rendering/detail-range.js +28 -0
- package/rendering/dispose-meshes.js +18 -0
- package/rendering/dof.js +9 -9
- package/rendering/eye-adapt.js +14 -14
- package/rendering/far-shadow.js +8 -8
- package/rendering/flocks.js +78 -78
- package/rendering/garment-material.js +17 -17
- package/rendering/glow-lines.js +13 -13
- package/rendering/hair-strands.js +20 -20
- package/rendering/height-probe.js +149 -19
- package/rendering/label-layout.js +46 -0
- package/rendering/label-range.js +38 -0
- package/rendering/light-budget.js +52 -52
- package/rendering/light-shafts.js +23 -23
- package/rendering/local-lights.js +15 -4
- package/rendering/material-lod.js +49 -49
- package/rendering/particle-emitters.js +90 -90
- package/rendering/particles.js +78 -67
- package/rendering/piece-batch.js +17 -6
- package/rendering/planar-reflection.js +38 -38
- package/rendering/post-shaders.js +17 -13
- package/rendering/post.js +71 -71
- package/rendering/rain-streaks.js +67 -0
- package/rendering/road-surface.js +11 -11
- package/rendering/sea-shaders.js +11 -11
- package/rendering/shader-warmup.js +29 -16
- package/rendering/shadow-alignment.js +4 -4
- package/rendering/shadow-depth.js +7 -7
- package/rendering/sky-occlusion.js +16 -16
- package/rendering/sky.js +16 -6
- package/rendering/stage-lights.js +6 -6
- package/rendering/static-point-shadow.js +3 -3
- package/rendering/still-casters.js +1 -1
- package/rendering/strata-material.js +10 -10
- package/rendering/stream-flow.js +10 -10
- package/rendering/taa.js +26 -26
- package/rendering/talus.js +15 -15
- package/rendering/terrain-rock.js +7 -7
- package/rendering/texture-fills.js +8 -8
- package/rendering/textures.js +28 -28
- package/rendering/wakes.js +24 -24
- package/rendering/water-material.js +36 -23
- package/rendering/world-matrices.js +38 -38
- package/runtime/diagnostic-log.js +2 -2
- package/runtime/first-use.js +33 -28
- package/runtime/frame-pacing.js +33 -0
- package/runtime/gpu-timing.js +7 -7
- package/runtime/grid-cache.js +1 -1
- package/runtime/grid-rows.js +44 -5
- package/runtime/hand-over.js +41 -23
- package/runtime/pool.js +58 -16
- package/runtime/quality-fit.js +1 -1
- package/runtime/search.js +22 -22
- package/runtime/spatial-audio.js +13 -13
- package/runtime/withdrawable-task.js +35 -0
- package/runtime/worker.js +7 -0
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render a heightfield in chunks and change each chunk’s mesh resolution as the camera moves.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import * as THREE from 'three';
|
|
2
7
|
import { SRGB2LIN } from '../math/color.js';
|
|
3
8
|
import { clamp } from '../math/scalar.js';
|
|
@@ -20,59 +25,59 @@ function buildChunkGeometry(land, ci, cj, lod) {
|
|
|
20
25
|
const step = 1 << lod,
|
|
21
26
|
n = cells / step,
|
|
22
27
|
m = n + 3;
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
const positions = new Float32Array(m * m * 3),
|
|
29
|
+
normals = new Float32Array(m * m * 3),
|
|
30
|
+
chunkColors = new Float32Array(m * m * 3),
|
|
26
31
|
uv = new Float32Array(m * m * 2);
|
|
27
32
|
let p = 0;
|
|
28
|
-
for (let
|
|
29
|
-
const jj = clamp(
|
|
30
|
-
sb =
|
|
31
|
-
for (let
|
|
32
|
-
const ii = clamp(
|
|
33
|
-
skirt = sb ||
|
|
33
|
+
for (let row = 0; row < m; row++) {
|
|
34
|
+
const jj = clamp(row - 1, 0, n),
|
|
35
|
+
sb = row === 0 || row === m - 1;
|
|
36
|
+
for (let column = 0; column < m; column++, p++) {
|
|
37
|
+
const ii = clamp(column - 1, 0, n),
|
|
38
|
+
skirt = sb || column === 0 || column === m - 1;
|
|
34
39
|
const gi = ci * cells + ii * step,
|
|
35
40
|
gj = cj * cells + jj * step,
|
|
36
41
|
k = gj * stride + gi;
|
|
37
42
|
const x = x0 + gi * spacing,
|
|
38
43
|
z = z0 + gj * spacing;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
positions[p * 3] = x;
|
|
45
|
+
positions[p * 3 + 1] = heights[k] - (skirt ? 5 + 5 * lod : 0) - (sunkAt ? sunkAt(k) : 0);
|
|
46
|
+
positions[p * 3 + 2] = z;
|
|
42
47
|
normalOf(land, k, _normal);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
normals[p * 3] = _normal[0];
|
|
49
|
+
normals[p * 3 + 1] = _normal[1];
|
|
50
|
+
normals[p * 3 + 2] = _normal[2];
|
|
51
|
+
chunkColors[p * 3] = SRGB2LIN[colors[k * 3]];
|
|
52
|
+
chunkColors[p * 3 + 1] = SRGB2LIN[colors[k * 3 + 1]];
|
|
53
|
+
chunkColors[p * 3 + 2] = SRGB2LIN[colors[k * 3 + 2]];
|
|
49
54
|
uv[p * 2] = x / 9;
|
|
50
55
|
uv[p * 2 + 1] = z / 9;
|
|
51
56
|
}
|
|
52
57
|
}
|
|
53
|
-
return chunkGeometry(
|
|
58
|
+
return chunkGeometry(positions, normals, chunkColors, uv, m, bedY);
|
|
54
59
|
}
|
|
55
60
|
// A chunk's geometry from its m x m points: two triangles to a cell, the deep lake bed's (below bedY) in a group of their own.
|
|
56
|
-
function chunkGeometry(
|
|
61
|
+
function chunkGeometry(positions, normals, chunkColors, uv, m, bedY) {
|
|
57
62
|
const Index = m * m > 65536 ? Uint32Array : Uint16Array;
|
|
58
|
-
const
|
|
63
|
+
const indices = new Index((m - 1) * (m - 1) * 6);
|
|
59
64
|
let q = 0,
|
|
60
|
-
e =
|
|
61
|
-
const deep = (i, j, k) =>
|
|
65
|
+
e = indices.length; // land from the front, deep lake bed (group 1, the plain bed material) from the back
|
|
66
|
+
const deep = (i, j, k) => positions[i * 3 + 1] < bedY && positions[j * 3 + 1] < bedY && positions[k * 3 + 1] < bedY;
|
|
62
67
|
const tri = (i, j, k) => {
|
|
63
68
|
if (deep(i, j, k)) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
indices[--e] = k;
|
|
70
|
+
indices[--e] = j;
|
|
71
|
+
indices[--e] = i;
|
|
67
72
|
} else {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
indices[q++] = i;
|
|
74
|
+
indices[q++] = j;
|
|
75
|
+
indices[q++] = k;
|
|
71
76
|
}
|
|
72
77
|
};
|
|
73
|
-
for (let
|
|
74
|
-
for (let
|
|
75
|
-
const i00 =
|
|
78
|
+
for (let row = 0; row < m - 1; row++)
|
|
79
|
+
for (let column = 0; column < m - 1; column++) {
|
|
80
|
+
const i00 = row * m + column,
|
|
76
81
|
i10 = i00 + 1,
|
|
77
82
|
i01 = i00 + m,
|
|
78
83
|
i11 = i01 + 1;
|
|
@@ -80,15 +85,15 @@ function chunkGeometry(pos, nor, col, uv, m, bedY) {
|
|
|
80
85
|
tri(i10, i01, i11);
|
|
81
86
|
}
|
|
82
87
|
const g = new THREE.BufferGeometry();
|
|
83
|
-
g.setAttribute('position', new THREE.BufferAttribute(
|
|
84
|
-
g.setAttribute('normal', new THREE.BufferAttribute(
|
|
85
|
-
g.setAttribute('color', new THREE.BufferAttribute(
|
|
88
|
+
g.setAttribute('position', new THREE.BufferAttribute(positions, 3));
|
|
89
|
+
g.setAttribute('normal', new THREE.BufferAttribute(normals, 3));
|
|
90
|
+
g.setAttribute('color', new THREE.BufferAttribute(chunkColors, 3));
|
|
86
91
|
g.setAttribute('uv', new THREE.BufferAttribute(uv, 2));
|
|
87
|
-
g.setIndex(new THREE.BufferAttribute(
|
|
92
|
+
g.setIndex(new THREE.BufferAttribute(indices, 1));
|
|
88
93
|
if (bedY !== undefined) {
|
|
89
94
|
// a chunk with no deep bed (or nothing but bed) has one group: an empty one would still be a draw call
|
|
90
95
|
if (q) g.addGroup(0, q, 0);
|
|
91
|
-
if (q <
|
|
96
|
+
if (q < indices.length) g.addGroup(q, indices.length - q, 1);
|
|
92
97
|
}
|
|
93
98
|
g.computeBoundingSphere();
|
|
94
99
|
g.computeBoundingBox();
|
|
@@ -110,10 +115,10 @@ function buildFineGeometry(land, ci, cj) {
|
|
|
110
115
|
rel = new Float32Array(r * r),
|
|
111
116
|
weights = [0, 0, 0, 0],
|
|
112
117
|
corner = [0, 0, 0, 0];
|
|
113
|
-
const place = (
|
|
114
|
-
// the grid cell and the place in it of fine point (
|
|
115
|
-
const fa = Math.min(Math.max(ci * cells * FINE +
|
|
116
|
-
fb = Math.min(Math.max(cj * cells * FINE +
|
|
118
|
+
const place = (column, row) => {
|
|
119
|
+
// the grid cell and the place in it of fine point (column, row) of this chunk, clamped to the land
|
|
120
|
+
const fa = Math.min(Math.max(ci * cells * FINE + column, 0), last * FINE),
|
|
121
|
+
fb = Math.min(Math.max(cj * cells * FINE + row, 0), last * FINE),
|
|
117
122
|
i = Math.min((fa / FINE) | 0, last - 1),
|
|
118
123
|
j = Math.min((fb / FINE) | 0, last - 1),
|
|
119
124
|
u = fa / FINE - i,
|
|
@@ -134,32 +139,32 @@ function buildFineGeometry(land, ci, cj) {
|
|
|
134
139
|
(1 - u) * (heights[k + stride] - heights[k + stride + 1]) +
|
|
135
140
|
(1 - v) * (heights[k + 1] - heights[k + stride + 1]);
|
|
136
141
|
};
|
|
137
|
-
for (let
|
|
138
|
-
for (let
|
|
139
|
-
const h = place(
|
|
140
|
-
x = x0 + (ci * cells * FINE +
|
|
141
|
-
z = z0 + (cj * cells * FINE +
|
|
142
|
-
base[
|
|
143
|
-
rel[
|
|
142
|
+
for (let row = 0; row < r; row++)
|
|
143
|
+
for (let column = 0; column < r; column++) {
|
|
144
|
+
const h = place(column - 1, row - 1),
|
|
145
|
+
x = x0 + (ci * cells * FINE + column - 1) * step,
|
|
146
|
+
z = z0 + (cj * cells * FINE + row - 1) * step;
|
|
147
|
+
base[row * r + column] = h;
|
|
148
|
+
rel[row * r + column] = relief(x, z, h);
|
|
144
149
|
}
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
150
|
+
const positions = new Float32Array(m * m * 3),
|
|
151
|
+
normals = new Float32Array(m * m * 3),
|
|
152
|
+
chunkColors = new Float32Array(m * m * 3),
|
|
148
153
|
uv = new Float32Array(m * m * 2);
|
|
149
154
|
let p = 0;
|
|
150
|
-
for (let
|
|
151
|
-
const jj = clamp(
|
|
152
|
-
sb =
|
|
153
|
-
for (let
|
|
154
|
-
const ii = clamp(
|
|
155
|
-
skirt = sb ||
|
|
155
|
+
for (let row = 0; row < m; row++) {
|
|
156
|
+
const jj = clamp(row - 1, 0, n),
|
|
157
|
+
sb = row === 0 || row === m - 1;
|
|
158
|
+
for (let column = 0; column < m; column++, p++) {
|
|
159
|
+
const ii = clamp(column - 1, 0, n),
|
|
160
|
+
skirt = sb || column === 0 || column === m - 1,
|
|
156
161
|
q = (jj + 1) * r + ii + 1,
|
|
157
162
|
x = x0 + (ci * cells * FINE + ii) * step,
|
|
158
163
|
z = z0 + (cj * cells * FINE + jj) * step;
|
|
159
164
|
place(ii, jj);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
165
|
+
positions[p * 3] = x;
|
|
166
|
+
positions[p * 3 + 1] = base[q] + rel[q] - (skirt ? 5 : 0);
|
|
167
|
+
positions[p * 3 + 2] = z;
|
|
163
168
|
// the grid's normal between its points, as the slope of a height field, plus the relief's slope
|
|
164
169
|
let nx = 0,
|
|
165
170
|
ny = 0,
|
|
@@ -179,17 +184,17 @@ function buildFineGeometry(land, ci, cj) {
|
|
|
179
184
|
const gx = -nx / ny + (rel[q + 1] - rel[q - 1]) / (2 * step),
|
|
180
185
|
gz = -nz / ny + (rel[q + r] - rel[q - r]) / (2 * step),
|
|
181
186
|
len = Math.hypot(gx, 1, gz);
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
187
|
+
normals[p * 3] = -gx / len;
|
|
188
|
+
normals[p * 3 + 1] = 1 / len;
|
|
189
|
+
normals[p * 3 + 2] = -gz / len;
|
|
190
|
+
chunkColors[p * 3] = cr;
|
|
191
|
+
chunkColors[p * 3 + 1] = cg;
|
|
192
|
+
chunkColors[p * 3 + 2] = cb;
|
|
188
193
|
uv[p * 2] = x / 9;
|
|
189
194
|
uv[p * 2 + 1] = z / 9;
|
|
190
195
|
}
|
|
191
196
|
}
|
|
192
|
-
return chunkGeometry(
|
|
197
|
+
return chunkGeometry(positions, normals, chunkColors, uv, m, bedY);
|
|
193
198
|
}
|
|
194
199
|
// Builds the ground at its coarsest or, with initialPosition {x, y, z}, at the levels seen from that position.
|
|
195
200
|
// The returned updater follows camera.position as the viewer moves. `materials` is the pair
|
|
@@ -246,8 +251,8 @@ export function terrainChunks(parent, land, materials, {
|
|
|
246
251
|
if (!initialPosition) show(chunk, 3);
|
|
247
252
|
chunks.push(chunk);
|
|
248
253
|
}
|
|
249
|
-
const boxOf =
|
|
250
|
-
overlaps = (
|
|
254
|
+
const boxOf = chunk => [chunk.cx - width / 2, chunk.cz - width / 2, chunk.cx + width / 2, chunk.cz + width / 2],
|
|
255
|
+
overlaps = (firstBounds, secondBounds) => firstBounds[0] < secondBounds[2] && secondBounds[0] < firstBounds[2] && firstBounds[1] < secondBounds[3] && secondBounds[1] < firstBounds[3],
|
|
251
256
|
nearBox = (box, range, position) =>
|
|
252
257
|
Math.hypot(
|
|
253
258
|
Math.max(box[0] - position.x, 0, position.x - box[2]),
|
|
@@ -256,14 +261,14 @@ export function terrainChunks(parent, land, materials, {
|
|
|
256
261
|
range * lodScale();
|
|
257
262
|
// the chunks under ground held at a level, found when it is first seen (the list may fill later)
|
|
258
263
|
const holdUnder = h => {
|
|
259
|
-
h.chunks = chunks.filter(
|
|
264
|
+
h.chunks = chunks.filter(chunk => overlaps(boxOf(chunk), h.box));
|
|
260
265
|
h.fine = false;
|
|
261
|
-
for (const
|
|
266
|
+
for (const chunk of h.chunks) (chunk.holds ??= []).push(h);
|
|
262
267
|
};
|
|
263
268
|
// the level a chunk wants, `want` by its distance, or finer where ground over it is held within range
|
|
264
|
-
const heldLevel = (
|
|
265
|
-
if (
|
|
266
|
-
for (let i = 0; i <
|
|
269
|
+
const heldLevel = (chunk, want, position) => {
|
|
270
|
+
if (chunk.holds)
|
|
271
|
+
for (let i = 0; i < chunk.holds.length; i++) if (nearBox(chunk.holds[i].box, chunk.holds[i].range, position)) want = Math.min(want, chunk.holds[i].level);
|
|
267
272
|
return want;
|
|
268
273
|
};
|
|
269
274
|
// whether every chunk under held ground stands at its level or finer
|
|
@@ -279,21 +284,21 @@ export function terrainChunks(parent, land, materials, {
|
|
|
279
284
|
let built = false;
|
|
280
285
|
const count = force ? chunks.length : checksPerFrame;
|
|
281
286
|
for (let n = 0; n < count; n++) {
|
|
282
|
-
const
|
|
287
|
+
const chunk = chunks[chunkCursor];
|
|
283
288
|
chunkCursor = (chunkCursor + 1) % chunks.length;
|
|
284
|
-
const dx = Math.max(Math.abs(position.x -
|
|
285
|
-
dz = Math.max(Math.abs(position.z -
|
|
286
|
-
const
|
|
289
|
+
const dx = Math.max(Math.abs(position.x - chunk.cx) - width * 0.5, 0),
|
|
290
|
+
dz = Math.max(Math.abs(position.z - chunk.cz) - width * 0.5, 0);
|
|
291
|
+
const viewDistance = Math.hypot(dx, dz, Math.max(0, position.y - altitudeStart) * altitudeWeight);
|
|
287
292
|
const fine = low && Math.hypot(dx, dz) < fineRange;
|
|
288
293
|
const want = fine
|
|
289
294
|
? -1
|
|
290
295
|
: heldLevel(
|
|
291
|
-
|
|
292
|
-
|
|
296
|
+
chunk,
|
|
297
|
+
viewDistance < distances[0] * lodScale() ? 0 : viewDistance < distances[1] * lodScale() ? 1 : viewDistance < distances[2] * lodScale() ? 2 : 3,
|
|
293
298
|
position,
|
|
294
299
|
);
|
|
295
|
-
if (want ===
|
|
296
|
-
show(
|
|
300
|
+
if (want === chunk.lod || (built && !force)) continue;
|
|
301
|
+
show(chunk, want);
|
|
297
302
|
built = true;
|
|
298
303
|
}
|
|
299
304
|
for (let i = 0; i < held.length; i++) held[i].fine = nearBox(held[i].box, held[i].range, position) && standsAt(held[i]);
|
|
@@ -15,22 +15,22 @@ export function terrainExtension({ centerX = 0, centerZ = 0, size, segments, rea
|
|
|
15
15
|
const boundary = new Uint32Array(perimeter);
|
|
16
16
|
for (let ring = 0; ring <= rows; ring++) {
|
|
17
17
|
const radius = half * growth ** ring;
|
|
18
|
-
for (let
|
|
19
|
-
const side = Math.floor(
|
|
20
|
-
const along =
|
|
18
|
+
for (let perimeterIndex = 0; perimeterIndex < perimeter; perimeterIndex++) {
|
|
19
|
+
const side = Math.floor(perimeterIndex / segments);
|
|
20
|
+
const along = perimeterIndex % segments;
|
|
21
21
|
let col = along, row = 0;
|
|
22
22
|
switch (side) {
|
|
23
23
|
case 1: col = segments; row = along; break;
|
|
24
24
|
case 2: col = segments - along; row = segments; break;
|
|
25
25
|
case 3: col = 0; row = segments - along; break;
|
|
26
26
|
}
|
|
27
|
-
if (ring === 0) boundary[
|
|
28
|
-
const vertex = ring * perimeter +
|
|
27
|
+
if (ring === 0) boundary[perimeterIndex] = row * (segments + 1) + col;
|
|
28
|
+
const vertex = ring * perimeter + perimeterIndex;
|
|
29
29
|
const axis = -radius + 2 * radius * along / segments;
|
|
30
30
|
positions[3 * vertex] = centerX + (side === 0 ? axis : side === 1 ? radius : side === 2 ? -axis : -radius);
|
|
31
31
|
positions[3 * vertex + 2] = centerZ + (side === 0 ? -radius : side === 1 ? axis : side === 2 ? radius : -axis);
|
|
32
32
|
if (ring === rows) continue;
|
|
33
|
-
const next = ring * perimeter + (
|
|
33
|
+
const next = ring * perimeter + (perimeterIndex + 1) % perimeter;
|
|
34
34
|
const offset = 6 * vertex;
|
|
35
35
|
indices[offset] = vertex;
|
|
36
36
|
indices[offset + 1] = next;
|
package/index.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core entry points for building geometry, rendering a world, moving through it and dispatching worker jobs. Import these names directly from `roamwright`.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
export { createRenderContext } from './rendering/context.js';
|
|
2
7
|
export { createNoise } from './math/noise.js';
|
|
3
8
|
export { mulberry32 } from './math/random.js';
|
package/math/cell-edge.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
// Distance (in texture units) from (u, v) to the nearest edge of a tileable pattern of `
|
|
1
|
+
// Distance (in texture units) from (u, v) to the nearest edge of a tileable pattern of `period` jittered cells per side; `site`, when
|
|
2
2
|
// given, is told which cell (u, v) lies in, wrapped to the tile.
|
|
3
|
-
export function cellEdge(hash2, u, v,
|
|
4
|
-
const gx = u *
|
|
5
|
-
gz = v *
|
|
3
|
+
export function cellEdge(hash2, u, v, period, seed, site) {
|
|
4
|
+
const gx = u * period,
|
|
5
|
+
gz = v * period,
|
|
6
6
|
ix = Math.floor(gx),
|
|
7
7
|
iz = Math.floor(gz);
|
|
8
8
|
let d1 = 9,
|
|
@@ -11,16 +11,16 @@ export function cellEdge(hash2, u, v, per, seed, site) {
|
|
|
11
11
|
az = 0,
|
|
12
12
|
bx = 0,
|
|
13
13
|
bz = 0;
|
|
14
|
-
for (let
|
|
15
|
-
for (let
|
|
16
|
-
const cx = ix +
|
|
17
|
-
cz = iz +
|
|
18
|
-
wx = ((cx %
|
|
19
|
-
wz = ((cz %
|
|
14
|
+
for (let offsetZ = -1; offsetZ <= 1; offsetZ++)
|
|
15
|
+
for (let offsetX = -1; offsetX <= 1; offsetX++) {
|
|
16
|
+
const cx = ix + offsetX,
|
|
17
|
+
cz = iz + offsetZ,
|
|
18
|
+
wx = ((cx % period) + period) % period,
|
|
19
|
+
wz = ((cz % period) + period) % period;
|
|
20
20
|
const sx = cx + 0.15 + 0.7 * hash2(wx, wz, seed),
|
|
21
21
|
sz = cz + 0.15 + 0.7 * hash2(wx, wz, seed + 1),
|
|
22
|
-
|
|
23
|
-
if (
|
|
22
|
+
distanceSquared = (sx - gx) ** 2 + (sz - gz) ** 2;
|
|
23
|
+
if (distanceSquared < d1) {
|
|
24
24
|
if (site) {
|
|
25
25
|
site[0] = wx;
|
|
26
26
|
site[1] = wz;
|
|
@@ -28,11 +28,11 @@ export function cellEdge(hash2, u, v, per, seed, site) {
|
|
|
28
28
|
d2 = d1;
|
|
29
29
|
bx = ax;
|
|
30
30
|
bz = az;
|
|
31
|
-
d1 =
|
|
31
|
+
d1 = distanceSquared;
|
|
32
32
|
ax = sx;
|
|
33
33
|
az = sz;
|
|
34
|
-
} else if (
|
|
35
|
-
d2 =
|
|
34
|
+
} else if (distanceSquared < d2) {
|
|
35
|
+
d2 = distanceSquared;
|
|
36
36
|
bx = sx;
|
|
37
37
|
bz = sz;
|
|
38
38
|
}
|
|
@@ -40,5 +40,5 @@ export function cellEdge(hash2, u, v, per, seed, site) {
|
|
|
40
40
|
const ex = bx - ax,
|
|
41
41
|
ez = bz - az,
|
|
42
42
|
l = Math.hypot(ex, ez) || 1;
|
|
43
|
-
return (((ax + bx) * 0.5 - gx) * ex + ((az + bz) * 0.5 - gz) * ez) / l /
|
|
43
|
+
return (((ax + bx) * 0.5 - gx) * ex + ((az + bz) * 0.5 - gz) * ez) / l / period;
|
|
44
44
|
}
|
package/math/cell-key.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Keep common cells as small numeric keys. A row outside the packed range uses
|
|
2
|
+
// a separate key so two distant places can never share a collider bucket.
|
|
3
|
+
export function cellKey(column, row) {
|
|
4
|
+
if (row >= -2048 && row < 2048 && Math.abs(column) < 2 ** 39) return (column + 2048) * 4096 + row + 2048;
|
|
5
|
+
return column + ',' + row;
|
|
6
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Largest radii that leave a fraction of each centre-to-centre distance clear.
|
|
2
|
+
// Positions are in the x-z plane. Calculate once when laying out a group.
|
|
3
|
+
// A lone centre has unlimited clearance. Coincident centres have none.
|
|
4
|
+
export function circleClearances(centres, gapFraction = 0) {
|
|
5
|
+
if (!(gapFraction >= 0 && gapFraction < 1)) throw new RangeError('gapFraction must be in [0, 1)');
|
|
6
|
+
const nearestSq = new Float64Array(centres.length).fill(Infinity);
|
|
7
|
+
for (let i = 0; i < centres.length; i++) {
|
|
8
|
+
const centre = centres[i];
|
|
9
|
+
for (let j = i + 1; j < centres.length; j++) {
|
|
10
|
+
const neighbour = centres[j];
|
|
11
|
+
const distanceSq = (centre.x - neighbour.x) ** 2 + (centre.z - neighbour.z) ** 2;
|
|
12
|
+
nearestSq[i] = Math.min(nearestSq[i], distanceSq);
|
|
13
|
+
nearestSq[j] = Math.min(nearestSq[j], distanceSq);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const share = (1 - gapFraction) / 2;
|
|
17
|
+
for (let i = 0; i < nearestSq.length; i++) nearestSq[i] = Math.sqrt(nearestSq[i]) * share;
|
|
18
|
+
return nearestSq;
|
|
19
|
+
}
|
package/math/cloth-textures.js
CHANGED
|
@@ -9,12 +9,12 @@ const FOLD_SLOPES = [110, 30];
|
|
|
9
9
|
const PIPES = { n: 46, half: [0.009, 0.024], wander: [0.005, 0.016], tall: [0.45, 1], lasts: [0.07, 0.2], foot: 2.3, flank: 0.5 },
|
|
10
10
|
CRUMPLE = { per: 6, octaves: 2, seed: 83, full: 1.6 }, // the crumple's noise: cells a tile, and how far from its middle value it is at its full height
|
|
11
11
|
STEP = 1 / 512; // how far apart a field is read to find its slope (tiles)
|
|
12
|
-
const around =
|
|
12
|
+
const around = offset => offset - Math.round(offset), // the nearest way round a tile
|
|
13
13
|
byte = slope => clamp(127.5 + 127.5 * slope, 0, 255);
|
|
14
14
|
// The long folds' fill: a pixel's bytes from its place in the tile.
|
|
15
15
|
function garmentFolds(foldSeed) {
|
|
16
16
|
const rand = mulberry32(foldSeed),
|
|
17
|
-
between = ([
|
|
17
|
+
between = ([minimum, maximum]) => minimum + (maximum - minimum) * rand(),
|
|
18
18
|
pipes = [];
|
|
19
19
|
for (let k = 0; k < PIPES.n; k++)
|
|
20
20
|
pipes.push({
|
|
@@ -65,10 +65,10 @@ const cloth = (threads, hash2, tfbm) => (u, v, o) => {
|
|
|
65
65
|
thread = Math.sin(Math.PI * along), // round across the thread on top, darker where it dips under
|
|
66
66
|
tone = over ? hash2(i, 0, 69) : hash2(j, 1, 69), // one thread spun a little thicker or paler than the next
|
|
67
67
|
wear = tfbm(u, v, 4, 4, 70);
|
|
68
|
-
const
|
|
69
|
-
o[0] =
|
|
70
|
-
o[1] =
|
|
71
|
-
o[2] =
|
|
68
|
+
const brightness = clamp(158 + wear * 56 + (thread - 0.64) * 20 + (tone - 0.5) * 12, 0, 255);
|
|
69
|
+
o[0] = brightness;
|
|
70
|
+
o[1] = brightness * 0.99;
|
|
71
|
+
o[2] = brightness * 0.975;
|
|
72
72
|
};
|
|
73
73
|
|
|
74
74
|
function createClothTextures({ seed = 0, foldSeed = seed, threads = 64 } = {}) {
|
package/math/cloud-textures.js
CHANGED
|
@@ -17,26 +17,26 @@ export function createCloudTextures({
|
|
|
17
17
|
ix = Math.floor(gx),
|
|
18
18
|
iz = Math.floor(gz);
|
|
19
19
|
let near = 9;
|
|
20
|
-
for (let
|
|
21
|
-
for (let
|
|
22
|
-
const cx = ix +
|
|
23
|
-
cz = iz +
|
|
20
|
+
for (let offsetZ = -1; offsetZ <= 1; offsetZ++)
|
|
21
|
+
for (let offsetX = -1; offsetX <= 1; offsetX++) {
|
|
22
|
+
const cx = ix + offsetX,
|
|
23
|
+
cz = iz + offsetZ,
|
|
24
24
|
wx = ((cx % per) + per) % per,
|
|
25
25
|
wz = ((cz % per) + per) % per,
|
|
26
|
-
|
|
27
|
-
if (
|
|
26
|
+
distanceSquared = (cx + 0.15 + 0.7 * hash2(wx, wz, seed) - gx) ** 2 + (cz + 0.15 + 0.7 * hash2(wx, wz, seed + 1) - gz) ** 2;
|
|
27
|
+
if (distanceSquared < near) near = distanceSquared;
|
|
28
28
|
}
|
|
29
29
|
return Math.max(0, 1 - near * 2.2);
|
|
30
30
|
}
|
|
31
31
|
// Rounded lumps with creases between them, as a cloud's top is made of: domes at three sizes, from `per` to a sheet.
|
|
32
32
|
function lumps(u, v, per, seed) {
|
|
33
33
|
let s = 0,
|
|
34
|
-
|
|
34
|
+
amplitude = 0.5,
|
|
35
35
|
n = 0;
|
|
36
36
|
for (let i = 0; i < 3; i++) {
|
|
37
|
-
s +=
|
|
38
|
-
n +=
|
|
39
|
-
|
|
37
|
+
s += amplitude * domes(u, v, per, seed + i * 13);
|
|
38
|
+
n += amplitude;
|
|
39
|
+
amplitude *= 0.5;
|
|
40
40
|
per *= 2;
|
|
41
41
|
}
|
|
42
42
|
return s / n;
|
|
@@ -44,11 +44,11 @@ export function createCloudTextures({
|
|
|
44
44
|
// Streaks four times as long as they are wide, at three scales.
|
|
45
45
|
function wisps(u, v) {
|
|
46
46
|
let s = 0,
|
|
47
|
-
|
|
47
|
+
amplitude = 0.5,
|
|
48
48
|
n = 0;
|
|
49
|
-
for (let i = 0, per = 3; i < 3; i++, per *= 2,
|
|
50
|
-
s +=
|
|
51
|
-
n +=
|
|
49
|
+
for (let i = 0, per = 3; i < 3; i++, per *= 2, amplitude *= 0.5) {
|
|
50
|
+
s += amplitude * tnoise(u * per, v * per * 4, per, 1259 + i * 13, per * 4);
|
|
51
|
+
n += amplitude;
|
|
52
52
|
}
|
|
53
53
|
return s / n;
|
|
54
54
|
}
|
|
@@ -88,12 +88,12 @@ export function createCloudTextures({
|
|
|
88
88
|
iy = Math.floor(y),
|
|
89
89
|
iz = Math.floor(z);
|
|
90
90
|
let near = 9;
|
|
91
|
-
for (let
|
|
92
|
-
for (let
|
|
93
|
-
for (let
|
|
94
|
-
const k = ((wrap(iz +
|
|
95
|
-
|
|
96
|
-
if (
|
|
91
|
+
for (let offsetZ = -1; offsetZ <= 1; offsetZ++)
|
|
92
|
+
for (let offsetY = -1; offsetY <= 1; offsetY++)
|
|
93
|
+
for (let offsetX = -1; offsetX <= 1; offsetX++) {
|
|
94
|
+
const k = ((wrap(iz + offsetZ) * per + wrap(iy + offsetY)) * per + wrap(ix + offsetX)) * 3,
|
|
95
|
+
distanceSquared = (ix + offsetX + middles[k] - x) ** 2 + (iy + offsetY + middles[k + 1] - y) ** 2 + (iz + offsetZ + middles[k + 2] - z) ** 2;
|
|
96
|
+
if (distanceSquared < near) near = distanceSquared;
|
|
97
97
|
}
|
|
98
98
|
return Math.max(0, 1 - near * 1.7);
|
|
99
99
|
};
|
package/math/color.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import { clamp } from './scalar.js';
|
|
2
2
|
|
|
3
|
-
const dim = (
|
|
3
|
+
const dim = (color, scale) => [color[0] * scale, color[1] * scale, color[2] * scale];
|
|
4
4
|
|
|
5
5
|
const SRGB2LIN = new Float32Array(256);
|
|
6
6
|
for (let i = 0; i < 256; i++) {
|
|
7
|
-
const
|
|
8
|
-
SRGB2LIN[i] =
|
|
7
|
+
const channel = i / 255;
|
|
8
|
+
SRGB2LIN[i] = channel < 0.04045 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
|
|
9
9
|
}
|
|
10
10
|
// Three.js ACES filmic fit (see licenses/three.txt and licenses/baking-lab.txt).
|
|
11
11
|
// Mutates a linear RGB color so fog applied after tone mapping can match the sky.
|
|
12
|
-
function acesToColor(
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let r2 = 0.59719 *
|
|
17
|
-
g2 = 0.076 *
|
|
18
|
-
b2 = 0.0284 *
|
|
12
|
+
function acesToColor(color, exposure) {
|
|
13
|
+
const red = (color.r * exposure) / 0.6,
|
|
14
|
+
green = (color.g * exposure) / 0.6,
|
|
15
|
+
blue = (color.b * exposure) / 0.6;
|
|
16
|
+
let r2 = 0.59719 * red + 0.35458 * green + 0.04823 * blue,
|
|
17
|
+
g2 = 0.076 * red + 0.90834 * green + 0.01566 * blue,
|
|
18
|
+
b2 = 0.0284 * red + 0.13383 * green + 0.83777 * blue;
|
|
19
19
|
const f = v => (v * (v + 0.0245786) - 0.000090537) / (v * (0.983729 * v + 0.432951) + 0.238081);
|
|
20
20
|
r2 = f(r2);
|
|
21
21
|
g2 = f(g2);
|
|
22
22
|
b2 = f(b2);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
23
|
+
color.r = clamp(1.60475 * r2 - 0.53108 * g2 - 0.07367 * b2, 0, 1);
|
|
24
|
+
color.g = clamp(-0.10208 * r2 + 1.10813 * g2 - 0.00605 * b2, 0, 1);
|
|
25
|
+
color.b = clamp(-0.00327 * r2 - 0.07276 * g2 + 1.07602 * b2, 0, 1);
|
|
26
|
+
return color;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export { acesToColor, dim, SRGB2LIN };
|
package/math/curve.js
CHANGED
|
@@ -17,12 +17,12 @@ function cubic(x0, x1, x2, x3, dt0, dt1, dt2, t) {
|
|
|
17
17
|
tt = t * t;
|
|
18
18
|
return x1 + t1 * t + c2 * tt + c3 * (tt * t);
|
|
19
19
|
}
|
|
20
|
-
const knot = (
|
|
20
|
+
const knot = (startPoint, endPoint) => Math.pow((startPoint[0] - endPoint[0]) * (startPoint[0] - endPoint[0]) + (startPoint[1] - endPoint[1]) * (startPoint[1] - endPoint[1]), 0.25);
|
|
21
21
|
const _before = [0, 0],
|
|
22
22
|
_after = [0, 0];
|
|
23
|
-
// The point of the curve through
|
|
24
|
-
function curvePoint(
|
|
25
|
-
const count =
|
|
23
|
+
// The point of the curve through the control points at t in 0..1, where t runs evenly from point to point.
|
|
24
|
+
function curvePoint(controlPoints, t) {
|
|
25
|
+
const count = controlPoints.length,
|
|
26
26
|
at = (count - 1) * t;
|
|
27
27
|
let i = Math.floor(at),
|
|
28
28
|
weight = at - i;
|
|
@@ -31,17 +31,17 @@ function curvePoint(pts, t) {
|
|
|
31
31
|
weight = 1;
|
|
32
32
|
}
|
|
33
33
|
// past either end the line is carried on by its last step
|
|
34
|
-
const first =
|
|
35
|
-
last =
|
|
36
|
-
_after[0] = last[0] -
|
|
37
|
-
_after[1] = last[1] -
|
|
38
|
-
_before[0] = first[0] -
|
|
39
|
-
_before[1] = first[1] -
|
|
34
|
+
const first = controlPoints[0],
|
|
35
|
+
last = controlPoints[count - 1];
|
|
36
|
+
_after[0] = last[0] - controlPoints[count - 2][0] + last[0];
|
|
37
|
+
_after[1] = last[1] - controlPoints[count - 2][1] + last[1];
|
|
38
|
+
_before[0] = first[0] - controlPoints[1][0] + first[0];
|
|
39
|
+
_before[1] = first[1] - controlPoints[1][1] + first[1];
|
|
40
40
|
const atEnd = i + 2 >= count,
|
|
41
|
-
p0 = i > 0 ?
|
|
42
|
-
p1 =
|
|
43
|
-
p2 =
|
|
44
|
-
p3 = atEnd ? _after :
|
|
41
|
+
p0 = i > 0 ? controlPoints[i - 1] : atEnd ? _after : _before,
|
|
42
|
+
p1 = controlPoints[i],
|
|
43
|
+
p2 = controlPoints[i + 1],
|
|
44
|
+
p3 = atEnd ? _after : controlPoints[i + 2];
|
|
45
45
|
let dt0 = knot(p0, p1),
|
|
46
46
|
dt1 = knot(p1, p2),
|
|
47
47
|
dt2 = knot(p2, p3);
|
|
@@ -51,12 +51,12 @@ function curvePoint(pts, t) {
|
|
|
51
51
|
return [cubic(p0[0], p1[0], p2[0], p3[0], dt0, dt1, dt2, weight), cubic(p0[1], p1[1], p2[1], p3[1], dt0, dt1, dt2, weight)];
|
|
52
52
|
}
|
|
53
53
|
// The curve's length from its start to each of ARC_STEPS + 1 points along it.
|
|
54
|
-
function arcLengths(
|
|
54
|
+
function arcLengths(controlPoints) {
|
|
55
55
|
const lengths = [0];
|
|
56
|
-
let last = curvePoint(
|
|
56
|
+
let last = curvePoint(controlPoints, 0),
|
|
57
57
|
sum = 0;
|
|
58
|
-
for (let
|
|
59
|
-
const point = curvePoint(
|
|
58
|
+
for (let stepIndex = 1; stepIndex <= ARC_STEPS; stepIndex++) {
|
|
59
|
+
const point = curvePoint(controlPoints, stepIndex / ARC_STEPS);
|
|
60
60
|
sum += Math.sqrt((point[0] - last[0]) * (point[0] - last[0]) + (point[1] - last[1]) * (point[1] - last[1]));
|
|
61
61
|
lengths.push(sum);
|
|
62
62
|
last = point;
|
|
@@ -81,12 +81,12 @@ function shareToT(lengths, u) {
|
|
|
81
81
|
if (lengths[high] === target) return high / ARC_STEPS;
|
|
82
82
|
return (high + (target - lengths[high]) / (lengths[high + 1] - lengths[high])) / ARC_STEPS;
|
|
83
83
|
}
|
|
84
|
-
// The line through
|
|
85
|
-
function spacedCurve(
|
|
86
|
-
const lengths = arcLengths(
|
|
84
|
+
// The line through the control points as a smooth curve of evenly spaced points, about `spacing` apart.
|
|
85
|
+
function spacedCurve(controlPoints, spacing) {
|
|
86
|
+
const lengths = arcLengths(controlPoints),
|
|
87
87
|
steps = Math.max(2, Math.round(lengths[ARC_STEPS] / spacing)),
|
|
88
88
|
points = [];
|
|
89
|
-
for (let
|
|
89
|
+
for (let stepIndex = 0; stepIndex <= steps; stepIndex++) points.push(curvePoint(controlPoints, shareToT(lengths, stepIndex / steps)));
|
|
90
90
|
return points;
|
|
91
91
|
}
|
|
92
92
|
|