roamwright 0.58.0 → 0.59.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/examples/land.html +2 -1
- package/examples/land.js +92 -2
- package/geometry/creased-sheet.js +20 -0
- package/geometry/drystone-wall.js +143 -0
- package/geometry/farm-boundaries.js +158 -0
- package/geometry/farm-life.js +183 -0
- package/geometry/field-survey.js +461 -0
- package/geometry/irrigated-fields.js +308 -0
- package/geometry/land-trails.js +108 -0
- package/geometry/prop-plough.js +40 -0
- package/geometry/prop-stook.js +47 -0
- package/geometry/road-ribbon.js +99 -0
- package/geometry/shore-reeds.js +105 -0
- package/geometry/strata-talus.js +71 -0
- package/geometry/stream-banks.js +164 -0
- package/index.js +20 -0
- package/motion/road-traffic.js +207 -0
- package/package.json +1 -1
- package/rendering/farm-cells.js +88 -0
- package/rendering/irrigated-field-surface.js +294 -0
- package/rendering/road-ribbons.js +37 -0
- package/rendering/strata-cliffs.js +333 -0
- package/rendering/strata-talus.js +58 -0
- package/rendering/stream-banks.js +76 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { lerp, sstep, TAU } from '../math/scalar.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reed beds along a shore, in metres: where the ground may lie for reeds round the water's level, the steepest slope they
|
|
5
|
+
* stand on, how sheltered and shallow the shore must be, the clumps' sizes and shades, and the like along a slow reach's line.
|
|
6
|
+
*/
|
|
7
|
+
export const SHORE_REEDS = Object.freeze({
|
|
8
|
+
/** How far under and over the water's level the ground may lie for reeds. */
|
|
9
|
+
depth: [-0.82, 0.94],
|
|
10
|
+
/** The steepest slope (rise over run) reeds stand on. */
|
|
11
|
+
slope: 0.62,
|
|
12
|
+
/** Metres from a road within which no reeds stand. */
|
|
13
|
+
road: 6,
|
|
14
|
+
/** How sheltered and shallow (0..1) a shore must be before reeds take it. */
|
|
15
|
+
shallow: [0.15, 0.78],
|
|
16
|
+
/** How far under the water's level the ground must lie for them. */
|
|
17
|
+
low: [-0.82, -0.31],
|
|
18
|
+
/** A clump's size as a share of its model, the least and the most. */
|
|
19
|
+
size: [0.7, 1.48],
|
|
20
|
+
/** A clump's shade, and how much it may gain. */
|
|
21
|
+
shade: [0.77, 0.35],
|
|
22
|
+
/** Beds are gathered into cells of 2^`cellShift` lattice points to a side (16 by default). */
|
|
23
|
+
cellShift: 4,
|
|
24
|
+
/** Along a slow reach's line: metres either side of it searched for reed ground, the spacing across it, the share planted in a
|
|
25
|
+
* bed, how far under and over the water's level the ground may lie, the steepest slope, how far a noise reading makes a bed,
|
|
26
|
+
* and that noise's span in metres and its seed. */
|
|
27
|
+
reach: { metres: 40, step: 1.1, dense: 0.95, depth: [-0.7, 0.45], slope: 0.45, beds: [0.28, 0.48], noise: { span: [17, 15], seed: 377 } },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Plan reed beds wherever a shore is shallow and gentle: a lattice of candidate places over the shore, and along a slow reach's
|
|
32
|
+
* line, on the shelves at its edge and wherever its bank meets the water gently, in beds with open water between. Pure
|
|
33
|
+
* arithmetic, for the page or a worker; the beds are left for a reed kit to mesh.
|
|
34
|
+
*
|
|
35
|
+
* @param {object} options
|
|
36
|
+
* @param {{ from: [number, number], to: [number, number], step: number }} options.lattice - The candidate places over the shore:
|
|
37
|
+
* lattice points `i` and `j` from `from` to `to` (both inclusive) at `step` metres, a place at `(i, j) * step`.
|
|
38
|
+
* @param {number} options.waterLevel - The water's level in metres.
|
|
39
|
+
* @param {(x: number, z: number) => number} options.ground - The terrain's height at a place, in metres.
|
|
40
|
+
* @param {(x: number, z: number) => number} options.slope - The ground's slope at a place (rise over run).
|
|
41
|
+
* @param {(x: number, z: number) => number} options.road - Metres from a place to the nearest road.
|
|
42
|
+
* @param {(x: number, z: number) => boolean} options.clear - Whether reeds may stand at a place at all: the caller keeps them out
|
|
43
|
+
* of its towns and off the far side of its waters.
|
|
44
|
+
* @param {(x: number, z: number, slope: number, out: number[]) => number[]} options.shore - Writes a place's shore profile into
|
|
45
|
+
* `out`; its second value is how sheltered and shallow the shore is there (0..1).
|
|
46
|
+
* @param {{ line: number[][] }} options.reach - The slow reach's line as `[x, z]` points in metres, searched either side.
|
|
47
|
+
* @param {(x: number, z: number, seed: number) => number} options.noise - Seeded value noise, which breaks the reach's reeds into beds.
|
|
48
|
+
* @param {Function} [options.random=Math.random] - A repeatable stream.
|
|
49
|
+
* @param {Partial<typeof SHORE_REEDS>} [options.shape] - Changes to {@link SHORE_REEDS}.
|
|
50
|
+
* @returns {Map<string, number[]>} By cell (`'i:j'`, its place in cells of the lattice), each a list of clumps
|
|
51
|
+
* `[x, y, z, size, turn, shade]`: where a clump stands in metres, its size as a share of its model, its turn in radians and its shade.
|
|
52
|
+
*/
|
|
53
|
+
export function planShoreReeds({ lattice, waterLevel, ground, slope, road, clear, shore, reach, noise, random = Math.random, shape = {} }) {
|
|
54
|
+
const {
|
|
55
|
+
depth,
|
|
56
|
+
slope: steepest,
|
|
57
|
+
road: roadClear,
|
|
58
|
+
shallow,
|
|
59
|
+
low,
|
|
60
|
+
size,
|
|
61
|
+
shade,
|
|
62
|
+
cellShift,
|
|
63
|
+
reach: { metres, step, dense, depth: reachDepth, slope: reachSlope, beds, noise: bedNoise },
|
|
64
|
+
} = { ...SHORE_REEDS, ...shape, reach: { ...SHORE_REEDS.reach, ...shape.reach } };
|
|
65
|
+
const rng = random,
|
|
66
|
+
bedsOut = new Map();
|
|
67
|
+
const [i0, j0] = lattice.from,
|
|
68
|
+
[i1, j1] = lattice.to,
|
|
69
|
+
step0 = lattice.step;
|
|
70
|
+
// A clump at (x, h, z) into its cell's bed.
|
|
71
|
+
const clump = (x, h, z, key) => {
|
|
72
|
+
let bed = bedsOut.get(key);
|
|
73
|
+
if (!bed) bedsOut.set(key, (bed = []));
|
|
74
|
+
bed.push([x, h - 0.05, z, lerp(size[0], size[1], rng()), rng() * TAU, shade[0] + rng() * shade[1]]);
|
|
75
|
+
};
|
|
76
|
+
const profile = [0, 0, 0];
|
|
77
|
+
for (let j = j0; j <= j1; j++)
|
|
78
|
+
for (let i = i0; i <= i1; i++) {
|
|
79
|
+
const x = (i + rng()) * step0,
|
|
80
|
+
z = (j + rng()) * step0,
|
|
81
|
+
h = ground(x, z),
|
|
82
|
+
steep = slope(x, z);
|
|
83
|
+
if (h < waterLevel + depth[0] || h > waterLevel + depth[1] || steep > steepest || road(x, z) < roadClear || !clear(x, z)) continue;
|
|
84
|
+
shore(x, z, steep, profile);
|
|
85
|
+
if (rng() > sstep(shallow[0], shallow[1], profile[1]) * sstep(low[0], low[1], h - waterLevel)) continue;
|
|
86
|
+
clump(x, h, z, (i >> cellShift) + ':' + (j >> cellShift));
|
|
87
|
+
}
|
|
88
|
+
// and along the slow reach, on the shelves at its edge and wherever its bank meets the water gently
|
|
89
|
+
const line = reach.line;
|
|
90
|
+
for (let p = 2; p < line.length - 2; p += 2) {
|
|
91
|
+
const [px, pz] = line[p],
|
|
92
|
+
dx = line[p + 1][0] - line[p - 1][0],
|
|
93
|
+
dz = line[p + 1][1] - line[p - 1][1],
|
|
94
|
+
len = Math.hypot(dx, dz) || 1;
|
|
95
|
+
for (let across = -metres; across <= metres; across += step) {
|
|
96
|
+
const x = px - (dz / len) * across + (rng() - 0.5) * step0,
|
|
97
|
+
z = pz + (dx / len) * across + (rng() - 0.5) * step0,
|
|
98
|
+
h = ground(x, z);
|
|
99
|
+
if (h < waterLevel + reachDepth[0] || h > waterLevel + reachDepth[1] || slope(x, z) > reachSlope || road(x, z) < roadClear) continue;
|
|
100
|
+
if (rng() > sstep(beds[0], beds[1], noise(x / bedNoise.span[0], z / bedNoise.span[1], bedNoise.seed)) * dense) continue;
|
|
101
|
+
clump(x, h, z, (Math.floor(x / step0) >> cellShift) + ':' + (Math.floor(z / step0) >> cellShift));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return bedsOut;
|
|
105
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { lerp, TAU } from '../math/scalar.js';
|
|
2
|
+
import { nearestGridIndex } from './grid-sample.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The stones of a talus, the broken stone fallen from cliffs and lying in fans at their feet: how far apart the places
|
|
6
|
+
* looked at are, the stones' sizes, and how the fans' cover (0..255, as a ground's bake marks it) makes a stone likely.
|
|
7
|
+
*/
|
|
8
|
+
export const TALUS_STONES = Object.freeze({
|
|
9
|
+
/** Metres between two places looked at, on a lattice over the land. */
|
|
10
|
+
step: 1.6,
|
|
11
|
+
/** Metres along a side of the cells the stones are gathered into, one pair of meshes to a cell. */
|
|
12
|
+
cell: 128,
|
|
13
|
+
/** Metres across a stone, the least and the most. */
|
|
14
|
+
stone: [0.14, 0.95],
|
|
15
|
+
/** Metres across a block the size of a cart, the least and the most. */
|
|
16
|
+
block: [1.1, 2.6],
|
|
17
|
+
/** Metres across from which a stone is a block. */
|
|
18
|
+
blockFrom: 1.1,
|
|
19
|
+
/** The share of the stones that are blocks. */
|
|
20
|
+
blocksShare: 0.025,
|
|
21
|
+
/** How likely the thickest cover makes a stone (its square is taken first). */
|
|
22
|
+
thick: 0.85,
|
|
23
|
+
/** The cover a lattice point needs for a stone to be considered there at all. */
|
|
24
|
+
least: 8,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Plan the stones of a talus over a land: mostly a hand to an arm across, with now and then a block the size of a cart, thickest
|
|
29
|
+
* close under the face and thinning down the fan. A lattice over the land is walked; where the ground's bake marks a fan at the
|
|
30
|
+
* lattice point, a stone may lie near it. Pure arithmetic, for the page or a worker.
|
|
31
|
+
*
|
|
32
|
+
* @param {object} options
|
|
33
|
+
* @param {number} options.size - The land's size in metres; it is centred on the origin.
|
|
34
|
+
* @param {{ x0: number, z0: number, spacing: number, stride: number }} options.grid - The ground's grid, as `grid-sample.js`
|
|
35
|
+
* reads it; the fans' cover has one value to its point.
|
|
36
|
+
* @param {ArrayLike<number>} options.cover - How far each point of the grid lies in a fan of fallen stone, 0..255.
|
|
37
|
+
* @param {(x: number, z: number) => number} options.ground - The terrain's height at a place, in metres.
|
|
38
|
+
* @param {(x: number, z: number, cell: number) => boolean} [options.clear] - Whether a stone may lie at a place, which lies in
|
|
39
|
+
* the grid's cell `cell`: the caller keeps them off its roads and rivers and off the rock itself.
|
|
40
|
+
* @param {Function} [options.random=Math.random] - A repeatable stream.
|
|
41
|
+
* @param {Partial<typeof TALUS_STONES>} [options.shape] - Changes to {@link TALUS_STONES}.
|
|
42
|
+
* @returns {Map<string, number[]>} By cell and kind (`'x:z:stone'`, `'x:z:block'`; x and z the cell's place in cells of the
|
|
43
|
+
* land), each a list of rows `[x, y, z, size, turn, tiltX, tiltZ, shade, cell]`: the stone's middle in metres, its size in
|
|
44
|
+
* metres, its turn and tilts in radians, its shade, and the ground's cell it lies in (for the caller's tint).
|
|
45
|
+
*/
|
|
46
|
+
export function planTalusStones({ size, grid, cover, ground, clear = () => true, random = Math.random, shape = {} }) {
|
|
47
|
+
const { step, cell, stone, block, blockFrom, blocksShare, thick, least } = { ...TALUS_STONES, ...shape };
|
|
48
|
+
const half = size / 2,
|
|
49
|
+
last = grid.stride - 1,
|
|
50
|
+
count = Math.floor(size / step),
|
|
51
|
+
cells = new Map();
|
|
52
|
+
for (let j = 0; j < count; j++)
|
|
53
|
+
for (let i = 0; i < count; i++) {
|
|
54
|
+
const gi = Math.floor((i * step) / grid.spacing),
|
|
55
|
+
gj = Math.floor((j * step) / grid.spacing);
|
|
56
|
+
if (gi > last || gj > last || cover[gj * grid.stride + gi] < least) continue;
|
|
57
|
+
const x = (i + random()) * step - half,
|
|
58
|
+
z = (j + random()) * step - half,
|
|
59
|
+
k = nearestGridIndex(grid, x, z),
|
|
60
|
+
t = cover[k] / 255;
|
|
61
|
+
if (random() > t * t * thick || !clear(x, z, k)) continue;
|
|
62
|
+
const isBlock = random() < blocksShare,
|
|
63
|
+
s = isBlock ? lerp(block[0], block[1], random()) : lerp(stone[0], stone[1], random() ** 2.2) * (0.7 + 0.5 * t);
|
|
64
|
+
const key = Math.floor(x / cell) + ':' + Math.floor(z / cell) + (s >= blockFrom ? ':block' : ':stone');
|
|
65
|
+
let list = cells.get(key);
|
|
66
|
+
if (!list) cells.set(key, (list = []));
|
|
67
|
+
const shade = 0.85 + random() * 0.3;
|
|
68
|
+
list.push([x, ground(x, z) - s * 0.18, z, s, random() * TAU, (random() - 0.5) * 0.8, (random() - 0.5) * 0.8, shade, k]);
|
|
69
|
+
}
|
|
70
|
+
return cells;
|
|
71
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { clamp, lerp, sstep, TAU } from '../math/scalar.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The look of a stream's banks, in metres: how steep a reach must be to be a torrent among boulders and how many boulders it
|
|
5
|
+
* carries, the stones along a quiet reach's margins, the shingle bars a slow outflow lays on the inside of its bends with
|
|
6
|
+
* driftwood stranded on them, and how far a stone's middle may stand over the bed.
|
|
7
|
+
*/
|
|
8
|
+
export const STREAM_BANKS = Object.freeze({
|
|
9
|
+
/** A bed's fall over `run` of its points: a torrent from `from`, whole at `to` (as white water is graded). */
|
|
10
|
+
steep: { from: 0.015, to: 0.32, reach: 3, run: 9 },
|
|
11
|
+
/** Boulders to a point of a torrent at its steepest, the share that stand in the water, and their sizes in metres. */
|
|
12
|
+
boulders: { each: 1.5, in: 0.55, small: 0.3, large: 1.7 },
|
|
13
|
+
/** Stones along a quiet reach: the chance of one at a point, and their sizes in metres. */
|
|
14
|
+
margin: { chance: 0.22, small: 0.16, large: 0.6 },
|
|
15
|
+
/** The signed curvature that puts a bar on a bend's inside, the points between bars on a straight reach (they alternate
|
|
16
|
+
* sides), a bar's length in points, its stones, and how often driftwood lies on it. */
|
|
17
|
+
bar: { bend: 0.004, every: 84, length: 30, stones: 60, logs: 0.6 },
|
|
18
|
+
/** How far out from a river's middle its water is looked for: the step and the longest reach in metres, and how far past a
|
|
19
|
+
* slow outflow's own width its water may lie where no bank rises. */
|
|
20
|
+
edge: { step: 0.5, reach: 24, open: 5 },
|
|
21
|
+
/** A stone lies on the bed: its middle no higher over the ground than this share of its size, however deep the water. */
|
|
22
|
+
afloat: 0.5,
|
|
23
|
+
/** The stones of a bank: `long` times as long as they are across, and `tall[0]` plus up to `tall[1]` times as tall. */
|
|
24
|
+
long: 1.25,
|
|
25
|
+
tall: [0.6, 0.3],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Plan where a stream meets its bed and banks. A steep reach is a torrent among boulders: blocks that break the surface, the
|
|
30
|
+
* largest at the edges where the water has undercut them, with stretches of quieter water between. A gentle reach has stones
|
|
31
|
+
* along its margins. A slow outflow (a river with no bed of its own, lying at the water's level) lays shingle on the inside of
|
|
32
|
+
* each bend, the stones sorted by the current, with driftwood stranded on the bars. Pure arithmetic, for the page or a worker.
|
|
33
|
+
*
|
|
34
|
+
* @param {object} options
|
|
35
|
+
* @param {{ fine: number[][], bed?: Float32Array, level?: Float32Array, w: number }[]} options.rivers - Each river's line as
|
|
36
|
+
* `[x, z]` points in metres, its bed's height and its water's level at each point (a stream's own), and its width; a river
|
|
37
|
+
* without a bed is an outflow at the water's level.
|
|
38
|
+
* @param {number} options.waterLevel - The water's level in metres, which an outflow lies at and a stream ends under.
|
|
39
|
+
* @param {(x: number, z: number) => number} options.ground - The terrain's height at a place, in metres.
|
|
40
|
+
* @param {Function} [options.random=Math.random] - A repeatable stream, drawn from for every river in turn.
|
|
41
|
+
* @param {Partial<typeof STREAM_BANKS>} [options.shape] - Changes to {@link STREAM_BANKS}.
|
|
42
|
+
* @returns {{ stones: number[], logs: number[] }[]} One entry for each river with a line, in their order. `stones`: rows of
|
|
43
|
+
* `[x, y, z, size, turn, tilt, wetness, shade, tall]` (its middle in metres, its size in metres, its turn and tilt in radians,
|
|
44
|
+
* how wet it is 0..1, its shade, and how tall it stands as a share of its size); `logs`: rows of
|
|
45
|
+
* `[x, y, z, length, turn, thick, shade]` (a bleached trunk lying along a bar). A river with no stones and no logs has empty rows.
|
|
46
|
+
*/
|
|
47
|
+
export function planStreamBanks({ rivers, waterLevel, ground, random = Math.random, shape = {} }) {
|
|
48
|
+
const {
|
|
49
|
+
steep: STEEP,
|
|
50
|
+
boulders: BOULDERS,
|
|
51
|
+
margin: MARGIN,
|
|
52
|
+
bar: BAR,
|
|
53
|
+
edge: EDGE,
|
|
54
|
+
afloat: AFLOAT,
|
|
55
|
+
tall: TALL,
|
|
56
|
+
} = {
|
|
57
|
+
...STREAM_BANKS,
|
|
58
|
+
...shape,
|
|
59
|
+
};
|
|
60
|
+
const rng = random;
|
|
61
|
+
const between = (a, b) => a + rng() * (b - a);
|
|
62
|
+
// How far from a river's middle the water reaches towards (nx, nz): where the ground first stands above its surface.
|
|
63
|
+
function waterEdge(x, z, nx, nz, level, reach) {
|
|
64
|
+
for (let d = EDGE.step; d < reach; d += EDGE.step) if (ground(x + nx * d, z + nz * d) > level) return d;
|
|
65
|
+
return reach;
|
|
66
|
+
}
|
|
67
|
+
// A bed's fall over `run` of its points, as white water is graded for a torrent.
|
|
68
|
+
const gradeAt = (bed, i) => {
|
|
69
|
+
const n = bed.length;
|
|
70
|
+
return sstep(STEEP.from, STEEP.to, (bed[Math.max(i - STEEP.reach, 0)] - bed[Math.min(i + STEEP.reach, n - 1)]) / STEEP.run);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// One river's stones into `stones` and its driftwood into `logs`.
|
|
74
|
+
function scatter(river, stones, logs) {
|
|
75
|
+
const pts = river.fine,
|
|
76
|
+
n = pts.length,
|
|
77
|
+
outflow = !river.bed,
|
|
78
|
+
half = river.w / 2 + EDGE.open;
|
|
79
|
+
let bar = 0, // points of the current bar still to lay
|
|
80
|
+
side = 1; // and the bank it lies against
|
|
81
|
+
for (let i = 2; i < n - 2; i++) {
|
|
82
|
+
const y = outflow ? waterLevel : river.level[i];
|
|
83
|
+
if (!outflow && y <= waterLevel + 0.05) continue; // a stream's last reach, under the water
|
|
84
|
+
const [x, z] = pts[i],
|
|
85
|
+
dx = pts[i + 1][0] - pts[i - 1][0],
|
|
86
|
+
dz = pts[i + 1][1] - pts[i - 1][1],
|
|
87
|
+
len = Math.hypot(dx, dz) || 1,
|
|
88
|
+
nx = -dz / len, // across the stream, to its left
|
|
89
|
+
nz = dx / len,
|
|
90
|
+
grade = outflow ? 0 : gradeAt(river.bed, i);
|
|
91
|
+
// how far the water reaches towards a bank: an outflow's over its whole reach, a stream's a little past its own width
|
|
92
|
+
const reachOf = bank => waterEdge(x, z, nx * bank, nz * bank, y, outflow ? EDGE.reach : half);
|
|
93
|
+
// one stone: on which bank, how far across as a share of the water's half width there, and how deep it sits
|
|
94
|
+
const at = (bank, across, size, wet, sink) => {
|
|
95
|
+
const reach = across * reachOf(bank),
|
|
96
|
+
px = x + nx * bank * reach,
|
|
97
|
+
pz = z + nz * bank * reach,
|
|
98
|
+
groundHere = ground(px, pz);
|
|
99
|
+
stones.push(
|
|
100
|
+
px,
|
|
101
|
+
Math.min(Math.max(y, groundHere), groundHere + size * AFLOAT) - size * sink,
|
|
102
|
+
pz,
|
|
103
|
+
size,
|
|
104
|
+
rng() * TAU,
|
|
105
|
+
between(-0.5, 0.5),
|
|
106
|
+
wet,
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
const eitherBank = () => (rng() < 0.5 ? -1 : 1);
|
|
110
|
+
// a torrent's boulders: in the water and wedged along its edges
|
|
111
|
+
for (let k = grade * BOULDERS.each + rng(); k >= 1; k--) {
|
|
112
|
+
const inStream = rng() < BOULDERS.in,
|
|
113
|
+
size = lerp(BOULDERS.small, BOULDERS.large, rng() ** (inStream ? 2.6 : 1.5));
|
|
114
|
+
at(eitherBank(), inStream ? between(0, 0.8) : between(0.82, 1.35), size, inStream ? 1 : 0.5, inStream ? 0.32 : 0.22);
|
|
115
|
+
}
|
|
116
|
+
// a quiet reach: stones along the margins
|
|
117
|
+
if (grade < 0.3 && rng() < MARGIN.chance) at(eitherBank(), between(0.92, 1.12), between(MARGIN.small, MARGIN.large), 0.7, 0.25);
|
|
118
|
+
if (!outflow) continue;
|
|
119
|
+
// the outflow's shingle bars
|
|
120
|
+
if (bar <= 0 && i % BAR.every === 0) {
|
|
121
|
+
// a new bar: on the inside of a bend, or on a straight reach the bank opposite the last one
|
|
122
|
+
const ax = x - pts[i - 2][0],
|
|
123
|
+
az = z - pts[i - 2][1],
|
|
124
|
+
bx = pts[i + 2][0] - x,
|
|
125
|
+
bz = pts[i + 2][1] - z,
|
|
126
|
+
bend = (ax * bz - az * bx) / ((Math.hypot(ax, az) * Math.hypot(bx, bz) || 1) * Math.hypot(ax, az)); // signed curvature
|
|
127
|
+
bar = BAR.length;
|
|
128
|
+
side = Math.abs(bend) > BAR.bend ? Math.sign(bend) : -side;
|
|
129
|
+
}
|
|
130
|
+
if (bar-- <= 0) continue;
|
|
131
|
+
const along = 1 - Math.abs(bar / BAR.length - 0.5) * 2; // a bar is fullest at its middle
|
|
132
|
+
for (let k = (BAR.stones / BAR.length) * (0.4 + along); k > rng(); k--) {
|
|
133
|
+
const across = between(1 - 0.5 * (0.3 + along), 1.04), // the bar reaches furthest into the water at its middle
|
|
134
|
+
size = lerp(0.1, 0.42, clamp((across - 0.5) / 0.54, 0, 1)) * between(0.6, 1.3); // and its stones grow finer towards the current
|
|
135
|
+
at(side, across, size, 0.75, 0.38);
|
|
136
|
+
}
|
|
137
|
+
if (bar === BAR.length >> 1 && rng() < BAR.logs) {
|
|
138
|
+
const reach = between(0.72, 0.95) * reachOf(side);
|
|
139
|
+
logs.push(
|
|
140
|
+
x + nx * side * reach,
|
|
141
|
+
y + 0.1,
|
|
142
|
+
z + nz * side * reach,
|
|
143
|
+
between(2.6, 5.2),
|
|
144
|
+
Math.atan2(dx, dz) + between(-0.5, 0.5),
|
|
145
|
+
between(0.12, 0.22),
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Every river in turn, from the one stream: each one's scatter, then the shade and height the mesher would have drawn for
|
|
152
|
+
// its stones, and the shade of its driftwood, in the order the mesher met them.
|
|
153
|
+
return rivers.map(river => {
|
|
154
|
+
const stones = [],
|
|
155
|
+
logs = [];
|
|
156
|
+
if (!river.fine) return { stones, logs };
|
|
157
|
+
scatter(river, stones, logs);
|
|
158
|
+
const rows = [];
|
|
159
|
+
for (let i = 0; i < stones.length; i += 7) rows.push(...stones.slice(i, i + 7), 0.8 + rng() * 0.4, TALL[0] + TALL[1] * rng());
|
|
160
|
+
const trunks = [];
|
|
161
|
+
for (let i = 0; i < logs.length; i += 6) trunks.push(...logs.slice(i, i + 6), 0.85 + rng() * 0.3);
|
|
162
|
+
return { stones: rows, logs: trunks };
|
|
163
|
+
});
|
|
164
|
+
}
|
package/index.js
CHANGED
|
@@ -37,6 +37,26 @@ export { createTownGround } from './geometry/land-town-ground.js';
|
|
|
37
37
|
export { createDryBasin } from './geometry/land-dry-basin.js';
|
|
38
38
|
export { landEdgeGrid } from './geometry/land-edge.js';
|
|
39
39
|
export { createSimpleWater } from './rendering/water-simple.js';
|
|
40
|
+
export { rockSheet } from './geometry/creased-sheet.js';
|
|
41
|
+
export { createStrataCliffs } from './rendering/strata-cliffs.js';
|
|
42
|
+
export { planTalusStones } from './geometry/strata-talus.js';
|
|
43
|
+
export { meshTalusStones } from './rendering/strata-talus.js';
|
|
44
|
+
export { planStreamBanks } from './geometry/stream-banks.js';
|
|
45
|
+
export { meshStreamBanks } from './rendering/stream-banks.js';
|
|
46
|
+
export { planShoreReeds } from './geometry/shore-reeds.js';
|
|
47
|
+
export { roadRibbon } from './geometry/road-ribbon.js';
|
|
48
|
+
export { layRoads } from './rendering/road-ribbons.js';
|
|
49
|
+
export { createRoadTraffic, ROAD_TRAFFIC } from './motion/road-traffic.js';
|
|
50
|
+
export { createFieldSurvey, FIELD_SURVEY, FIELD_USE } from './geometry/field-survey.js';
|
|
51
|
+
export { createLandTrails, LAND_TRAILS } from './geometry/land-trails.js';
|
|
52
|
+
export { createIrrigatedFields, IRRIGATED_FIELDS } from './geometry/irrigated-fields.js';
|
|
53
|
+
export { createIrrigatedFieldSurface, IRRIGATED_FIELD_KIND, IRRIGATED_FIELD_SURFACE } from './rendering/irrigated-field-surface.js';
|
|
54
|
+
export { DRYSTONE_WALL, FIELD_STONE, drystoneStone, planDrystoneWall } from './geometry/drystone-wall.js';
|
|
55
|
+
export { FARM_BOUNDARIES, planFieldBoundaries } from './geometry/farm-boundaries.js';
|
|
56
|
+
export { meshFarmCells, scatterFarmLots } from './rendering/farm-cells.js';
|
|
57
|
+
export { DEFAULT_FARM_HERDS, FARM_LIFE, herdPlaces, parcelPlaces, planFarmLife, ploughSite } from './geometry/farm-life.js';
|
|
58
|
+
export { STOOK, createStook } from './geometry/prop-stook.js';
|
|
59
|
+
export { PLOUGH, ploughGear } from './geometry/prop-plough.js';
|
|
40
60
|
|
|
41
61
|
// ── Settlements, houses, fortifications & interiors ──────────────────────
|
|
42
62
|
export { createHouseKit } from './geometry/house-kit.js';
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { spacedCurve } from '../math/curve.js';
|
|
2
|
+
import { along, wayThrough } from '../math/ways.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convoys on a road, in metres and seconds: the stretch at each end of a road a convoy keeps clear of (a gate and its walkway, a
|
|
6
|
+
* town's edge: it turns back there); the most a road's surface may rise from one sample to the next (a deck that rises more
|
|
7
|
+
* stands over it, and is passed under); the distance from the viewer within which a convoy's beasts and vehicles are drawn, and
|
|
8
|
+
* within which they are first made; how fast a convoy gains and loses its pace; how far it keeps behind the one ahead of it, and
|
|
9
|
+
* how much faster it closes for each metre beyond that; how long it rests at a road's end; where a wagon's two oxen walk and
|
|
10
|
+
* the ox in a cart's shafts; the height of a rider's seat; and the `home` that keeps a crowd figure from being drawn (a rider
|
|
11
|
+
* before there is a horse to sit).
|
|
12
|
+
*/
|
|
13
|
+
export const ROAD_TRAFFIC = Object.freeze({
|
|
14
|
+
trim: 45,
|
|
15
|
+
stepRise: 0.6,
|
|
16
|
+
sample: 1.5,
|
|
17
|
+
drawn: 650,
|
|
18
|
+
raise: 1100,
|
|
19
|
+
getGoing: 0.25,
|
|
20
|
+
keepBack: 10,
|
|
21
|
+
closing: 0.15,
|
|
22
|
+
rest: [50, 140],
|
|
23
|
+
team: { ahead: 3.5, apart: 0.62 },
|
|
24
|
+
shafts: 2.95,
|
|
25
|
+
saddle: 1.2,
|
|
26
|
+
unseen: -1,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Convoys that go along a road at their pace: the road as they follow it, where a member stands on it, how a convoy keeps back
|
|
31
|
+
* from the one ahead, and how it goes on, rests at the end and turns back. Each convoy is a plain record
|
|
32
|
+
* `{ road, pace, s, dir, v, rest, rank, travel, near, front, back }`: its mark `s` in metres along the road, its direction `dir`
|
|
33
|
+
* (+1 or -1), its speed `v` in metres a second, the seconds `rest` it has yet to rest at the road's end, its place `rank` among
|
|
34
|
+
* the day's travellers (0..1, first out in the morning to last), the metres `travel` it has gone (which turn its wheels), whether
|
|
35
|
+
* it is `near` enough to be seen, and `front` and `back` its reach in metres from its mark to its head and to its tail.
|
|
36
|
+
*
|
|
37
|
+
* @param {object} [options]
|
|
38
|
+
* @param {number} [options.trim=ROAD_TRAFFIC.trim] - Metres at each end of a road a convoy keeps clear of.
|
|
39
|
+
* @param {number} [options.stepRise=ROAD_TRAFFIC.stepRise] - Metres: the most a road's surface rises from one sample to the next.
|
|
40
|
+
* @param {number} [options.sample=ROAD_TRAFFIC.sample] - Metres between a road's samples.
|
|
41
|
+
* @param {number} [options.getGoing=ROAD_TRAFFIC.getGoing] - The share of its pace a convoy gains a second, and loses stopping.
|
|
42
|
+
* @param {number} [options.keepBack=ROAD_TRAFFIC.keepBack] - Metres a convoy keeps behind the one ahead of it going its way.
|
|
43
|
+
* @param {number} [options.closing=ROAD_TRAFFIC.closing] - Metres a second faster than the one ahead, for each metre of the gap
|
|
44
|
+
* beyond `keepBack`.
|
|
45
|
+
* @param {number[]} [options.rest=ROAD_TRAFFIC.rest] - The least and the most seconds a convoy rests at the end of its road
|
|
46
|
+
* before it turns back.
|
|
47
|
+
* @returns {{ road: Function, setOut: Function, traveller: Function, placeOn: Function, keepingBack: Function, goOn: Function }}
|
|
48
|
+
*/
|
|
49
|
+
export function createRoadTraffic({
|
|
50
|
+
trim = ROAD_TRAFFIC.trim,
|
|
51
|
+
stepRise = ROAD_TRAFFIC.stepRise,
|
|
52
|
+
sample = ROAD_TRAFFIC.sample,
|
|
53
|
+
getGoing = ROAD_TRAFFIC.getGoing,
|
|
54
|
+
keepBack = ROAD_TRAFFIC.keepBack,
|
|
55
|
+
closing = ROAD_TRAFFIC.closing,
|
|
56
|
+
rest = ROAD_TRAFFIC.rest,
|
|
57
|
+
} = {}) {
|
|
58
|
+
const at = { x: 0, z: 0, dx: 0, dz: 0, i: 0, t: 0 },
|
|
59
|
+
place = { x: 0, y: 0, z: 0, yaw: 0 }; // scratch: read `place` before the next call
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A road as convoys follow it: its middle sampled every `sample` metres, and its surface's height at each sample. The height
|
|
63
|
+
* follows the ground and decks laid along the road (a bridge), but not a deck standing over it (a gate's walkway): a step up
|
|
64
|
+
* too steep for any road is passed under.
|
|
65
|
+
*
|
|
66
|
+
* @param {number[][]} pts - The road's line as `[x, z]` points, metres.
|
|
67
|
+
* @param {(x: number, z: number) => number} ground - The ground's height at a place, metres.
|
|
68
|
+
* @returns {{ way: object, y: Float32Array }} The way (`ways.js`: its points, the metres gone at each, its length) and the
|
|
69
|
+
* surface's height at each of its points.
|
|
70
|
+
*/
|
|
71
|
+
function road(pts, ground) {
|
|
72
|
+
const fine = spacedCurve(pts, sample),
|
|
73
|
+
way = wayThrough(fine),
|
|
74
|
+
y = new Float32Array(fine.length);
|
|
75
|
+
for (let i = 0; i < fine.length; i++) {
|
|
76
|
+
const h = ground(fine[i][0], fine[i][1]);
|
|
77
|
+
y[i] = i && h - y[i - 1] > stepRise ? y[i - 1] : h;
|
|
78
|
+
}
|
|
79
|
+
return { way, y };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A convoy set out on its road: its mark spread along it (the `order`th of `count` convoys on the road, each a share of the
|
|
84
|
+
* way, set back from either end by `trim`), its direction, and its place among the day's travellers.
|
|
85
|
+
*
|
|
86
|
+
* @param {object} road - {@link createRoadTraffic}'s `road`.
|
|
87
|
+
* @param {number} pace - Its pace in metres a second.
|
|
88
|
+
* @param {object} where
|
|
89
|
+
* @param {number} where.order - Which of the road's convoys this is, from 0.
|
|
90
|
+
* @param {number} where.count - How many convoys the road has.
|
|
91
|
+
* @param {Function} where.random - A repeatable stream, drawn from four times.
|
|
92
|
+
* @returns {object} The convoy's record (`front` and `back` are the caller's to set from its members).
|
|
93
|
+
*/
|
|
94
|
+
function setOut(road, pace, { order, count, random }) {
|
|
95
|
+
const span = road.way.length - 2 * trim;
|
|
96
|
+
return {
|
|
97
|
+
road,
|
|
98
|
+
pace,
|
|
99
|
+
s: trim + ((order + 0.2 + random() * 0.6) / count) * span,
|
|
100
|
+
dir: random() < 0.5 ? 1 : -1,
|
|
101
|
+
v: 0,
|
|
102
|
+
rest: 0, // seconds it has yet to rest at the road's end
|
|
103
|
+
rank: random(), // its place among the day's travellers, first out in the morning to last
|
|
104
|
+
travel: random() * 100, // metres gone, which turn its wheels and set its beasts' strides
|
|
105
|
+
near: false,
|
|
106
|
+
front: 0,
|
|
107
|
+
back: 0,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* One of a convoy's people, walking or riding, as a crowd's figure it becomes: moved by its convoy, never a stander whose
|
|
113
|
+
* ground is looked up once. A rider is lifted to the saddle and kept from being drawn until there is a horse to sit (`home`).
|
|
114
|
+
*
|
|
115
|
+
* @param {'walker' | 'rider'} kind - What the traveller is.
|
|
116
|
+
* @param {number} pace - The convoy's pace in metres a second, which the walker keeps.
|
|
117
|
+
* @param {Function} random - A repeatable stream, drawn from twice.
|
|
118
|
+
* @returns {object} The figure's initial record, for a crowd kit (`crowd-figure.js`).
|
|
119
|
+
*/
|
|
120
|
+
function traveller(kind, pace, random) {
|
|
121
|
+
const rider = kind === 'rider';
|
|
122
|
+
return {
|
|
123
|
+
ax: 0,
|
|
124
|
+
az: 0,
|
|
125
|
+
bx: 0,
|
|
126
|
+
bz: 0,
|
|
127
|
+
sp: 1e-6, // not a stander whose ground is looked up once: these are moved
|
|
128
|
+
ph: random() * 100,
|
|
129
|
+
s: 0.93 + random() * 0.12,
|
|
130
|
+
walkSpeed: pace,
|
|
131
|
+
steady: true,
|
|
132
|
+
speed: 0,
|
|
133
|
+
travel: 0,
|
|
134
|
+
mounted: rider,
|
|
135
|
+
lift: rider ? ROAD_TRAFFIC.saddle : 0,
|
|
136
|
+
role: rider ? 'ride' : undefined,
|
|
137
|
+
home: rider ? ROAD_TRAFFIC.unseen : undefined,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Where a convoy's member stands: `ahead` metres before its mark and `side` metres right of the road's middle.
|
|
143
|
+
*
|
|
144
|
+
* @param {object} c - The convoy.
|
|
145
|
+
* @param {number} ahead - Metres before the convoy's mark (negative: behind it).
|
|
146
|
+
* @param {number} side - Metres right of the road's middle.
|
|
147
|
+
* @returns {{ x: number, y: number, z: number, yaw: number }} The member's place on the road's surface and the way it faces;
|
|
148
|
+
* a shared scratch, to be read before the next call.
|
|
149
|
+
*/
|
|
150
|
+
function placeOn(c, ahead, side) {
|
|
151
|
+
const { way, y } = c.road,
|
|
152
|
+
p = along(way, c.s + ahead * c.dir, at),
|
|
153
|
+
l = Math.hypot(p.dx, p.dz) || 1,
|
|
154
|
+
fx = (p.dx / l) * c.dir,
|
|
155
|
+
fz = (p.dz / l) * c.dir;
|
|
156
|
+
place.x = p.x - fz * side;
|
|
157
|
+
place.z = p.z + fx * side;
|
|
158
|
+
place.y = y[p.i] + (y[Math.min(p.i + 1, y.length - 1)] - y[p.i]) * p.t;
|
|
159
|
+
place.yaw = Math.atan2(fx, fz);
|
|
160
|
+
return place;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* How fast a convoy may go so as not to run into the one ahead of it on its road going its way.
|
|
165
|
+
*
|
|
166
|
+
* @param {object} c - The convoy.
|
|
167
|
+
* @param {object[]} convoys - Every convoy, to find the one ahead.
|
|
168
|
+
* @returns {number} Metres a second (Infinity if none is near).
|
|
169
|
+
*/
|
|
170
|
+
function keepingBack(c, convoys) {
|
|
171
|
+
let most = Infinity;
|
|
172
|
+
for (let i = 0; i < convoys.length; i++) {
|
|
173
|
+
const o = convoys[i];
|
|
174
|
+
if (o === c || o.road !== c.road || o.dir !== c.dir) continue;
|
|
175
|
+
const gap = (o.s + o.back * o.dir - (c.s + c.front * c.dir)) * c.dir;
|
|
176
|
+
if (gap > -1 && gap < 60) most = Math.min(most, Math.max(0, o.v + (gap - keepBack) * closing));
|
|
177
|
+
}
|
|
178
|
+
return most;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Moves a convoy on along its road at its pace while the hour has it travelling, easing into its pace and out of it, resting
|
|
183
|
+
* at a road's end before it turns back (only where nobody sees it do so, the whole convoy at once).
|
|
184
|
+
*
|
|
185
|
+
* @param {object} c - The convoy.
|
|
186
|
+
* @param {number} dt - The seconds gone by.
|
|
187
|
+
* @param {object[]} convoys - Every convoy, for {@link createRoadTraffic}'s `keepingBack`.
|
|
188
|
+
* @param {boolean} travelling - Whether the hour has this convoy on the road.
|
|
189
|
+
*/
|
|
190
|
+
function goOn(c, dt, convoys, travelling) {
|
|
191
|
+
const end = c.dir > 0 ? c.road.way.length - trim : trim,
|
|
192
|
+
left = (end - c.s) * c.dir;
|
|
193
|
+
if (left <= 0 && c.rest <= 0) c.rest = rest[0] + (rest[1] - rest[0]) * c.rank;
|
|
194
|
+
if (c.rest > 0) {
|
|
195
|
+
c.rest -= dt;
|
|
196
|
+
// it turns back only where nobody sees it do so, the whole convoy at once
|
|
197
|
+
if (c.rest <= 0 && c.near) c.rest = 1;
|
|
198
|
+
else if (c.rest <= 0) c.dir = -c.dir;
|
|
199
|
+
}
|
|
200
|
+
const goal = travelling && c.rest <= 0 ? Math.min(c.pace * Math.min(1, 0.3 + Math.max(0, left) / 6), keepingBack(c, convoys)) : 0;
|
|
201
|
+
c.v += Math.max(-getGoing * c.pace * dt, Math.min(getGoing * c.pace * dt, goal - c.v));
|
|
202
|
+
c.s += c.v * dt * c.dir;
|
|
203
|
+
c.travel += c.v * dt;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { road, setOut, traveller, placeOn, keepingBack, goOn };
|
|
207
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "roamwright",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.59.0",
|
|
4
4
|
"description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"threejs",
|