roamwright 0.1.1 → 0.4.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/CONTRIBUTING.md +39 -8
- package/README.md +74 -24
- package/examples/viaduct-camera.js +40 -0
- package/examples/viaduct-landscape.js +81 -0
- package/examples/viaduct.css +75 -0
- package/examples/viaduct.html +47 -0
- package/examples/viaduct.js +332 -0
- package/geometry/creased-sheet.js +127 -0
- package/geometry/ear.js +97 -0
- package/geometry/erosion.js +106 -0
- package/geometry/face-grid.js +143 -0
- package/geometry/foliage.js +47 -0
- package/geometry/garment-tags.js +11 -0
- package/geometry/hair-gathers.js +174 -0
- package/geometry/hair-locks.js +156 -0
- package/geometry/hair-patch.js +42 -0
- package/geometry/hair-shell.js +186 -0
- package/geometry/hair-tags.js +6 -0
- package/geometry/head-profile.js +138 -0
- package/geometry/head-shape.js +499 -0
- package/geometry/heightfield.js +68 -0
- package/geometry/index-parts.js +60 -0
- package/geometry/instance-visibility.js +41 -0
- package/geometry/instances.js +23 -0
- package/geometry/outline-grid.js +46 -0
- package/geometry/part-ranges.js +18 -0
- package/geometry/path-field.js +65 -0
- package/geometry/sagging-tube.js +58 -0
- package/geometry/sampled-surface.js +101 -0
- package/geometry/scatter-grid.js +32 -0
- package/geometry/sculpt.js +263 -0
- package/geometry/shore-grid.js +67 -0
- package/geometry/strata-field.js +339 -0
- package/geometry/strata-tiles.js +215 -0
- package/geometry/stream.js +94 -0
- package/geometry/terrain-chunks.js +304 -0
- package/geometry/terrain-extension.js +44 -0
- package/math/cell-edge.js +44 -0
- package/math/cloth-textures.js +83 -0
- package/math/cloud-textures.js +112 -0
- package/math/color.js +29 -0
- package/math/mountain-ranges.js +463 -0
- package/math/particle-noise.js +48 -0
- package/math/periodic.js +20 -0
- package/math/rock-texture.js +56 -0
- package/math/surface-textures.js +241 -0
- package/motion/arm-reach.js +74 -0
- package/motion/biped-stride.js +276 -0
- package/motion/crowd-alarms.js +127 -0
- package/motion/crowd-routine.js +183 -0
- package/motion/daily-clock.js +59 -0
- package/motion/ground-lookahead.js +29 -0
- package/motion/instance-sight.js +84 -0
- package/motion/quadruped.js +332 -0
- package/motion/weather.js +59 -0
- package/motion/wind.js +27 -0
- package/package.json +6 -4
- package/rendering/caster-motion.js +79 -0
- package/rendering/cell-lod.js +184 -0
- package/rendering/cloud-dome.js +371 -0
- package/rendering/cloud-field.js +83 -0
- package/rendering/compilation-schedule.js +34 -0
- package/rendering/contact-shadows.js +79 -0
- package/rendering/crowd-skinning.js +133 -0
- package/rendering/crust.js +28 -0
- package/rendering/daylight.js +50 -0
- package/rendering/far-shadow.js +186 -0
- package/rendering/flocks.js +323 -0
- package/rendering/garment-material.js +110 -0
- package/rendering/glow-lines.js +74 -0
- package/rendering/glow-shell.js +90 -0
- package/rendering/hair-strands.js +67 -0
- package/rendering/height-haze.js +129 -0
- package/rendering/height-probe.js +91 -0
- package/rendering/interior-mapping.js +19 -0
- package/rendering/light-budget.js +222 -0
- package/rendering/light-points.js +51 -0
- package/rendering/light-shafts.js +131 -0
- package/rendering/local-lights.js +28 -0
- package/rendering/low-light.js +13 -0
- package/rendering/material-lod.js +110 -0
- package/rendering/particle-emitters.js +223 -0
- package/rendering/particles.js +297 -0
- package/rendering/piece-batch.js +74 -20
- package/rendering/planar-reflection.js +175 -0
- package/rendering/post-shaders.js +21 -1
- package/rendering/post.js +4 -0
- package/rendering/rain-rings.js +22 -0
- package/rendering/road-surface.js +63 -0
- package/rendering/sea-shaders.js +230 -0
- package/rendering/shader-warmup.js +197 -0
- package/rendering/shadow-alignment.js +28 -0
- package/rendering/shadow-depth.js +129 -0
- package/rendering/sky-occlusion.js +187 -0
- package/rendering/sky.js +169 -0
- package/rendering/stage-lights.js +209 -0
- package/rendering/static-point-shadow.js +119 -0
- package/rendering/strata-material.js +93 -0
- package/rendering/stream-flow.js +36 -0
- package/rendering/surface-light.js +22 -0
- package/rendering/surface-projection.js +69 -0
- package/rendering/talus.js +26 -0
- package/rendering/terrain-rock.js +48 -0
- package/rendering/texture-fills.js +68 -0
- package/rendering/viewport.js +9 -0
- package/rendering/wakes.js +99 -0
- package/rendering/water-material.js +288 -0
- package/runtime/device-quality.js +27 -0
- package/runtime/diagnostic-log.js +80 -0
- package/runtime/events.js +14 -0
- package/runtime/first-use.js +3 -1
- package/runtime/frame-budget.js +49 -0
- package/runtime/frame-yield.js +14 -0
- package/runtime/gpu-timing.js +6 -3
- package/runtime/grid-cache.js +88 -0
- package/runtime/grid-rows.js +9 -0
- package/runtime/module-preload.js +15 -0
- package/runtime/search.js +69 -0
- package/runtime/spatial-audio.js +57 -0
- package/scripts/serve.py +26 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
// Seeded height and scree fields for elliptical uplands, branching ranges and groups of hills.
|
|
2
|
+
import { clamp, lerp, sstep } from './scalar.js';
|
|
3
|
+
import { createNoise } from './noise.js';
|
|
4
|
+
import { createHash } from './random.js';
|
|
5
|
+
|
|
6
|
+
const RANGE_FROM = [0.3, 0.75], // how broken an upland must be to begin to be a range, and to be one wholly
|
|
7
|
+
FOOT = 1.15, // where a flank's rock slope meets its apron, in flank widths from the crest
|
|
8
|
+
APRON = 0.13, // the share of a crest's height its apron stands for: the scree and wash banked against its foot
|
|
9
|
+
REACH = 2.1, // flank widths from the crest at which nothing of a range is left
|
|
10
|
+
TAIL_REACH = 1.9, // and the same past a crest's end, in the lengths its end falls away over
|
|
11
|
+
NARROW = 0.4, // the share of its width a range keeps where its crest has fallen away
|
|
12
|
+
CREST_ROUND = 7, // metres a crest is rounded over, so its line does not saw across the ground's cells
|
|
13
|
+
PEAK_SPACING = 0.6, // peaks along a crest, in half-widths of the range ...
|
|
14
|
+
PEAK_SPAN = [60, 200], // ... but no nearer and no farther apart than this, in metres
|
|
15
|
+
PEAK_POINT = 1.4, // how pointed a peak is and how level a saddle (1: a straight line from one to the other)
|
|
16
|
+
PEAK_DROP = 0.26, // the most a lesser peak stands under the highest, as a share of it,
|
|
17
|
+
SADDLE_DROP = [0.05, 0.24], // and a saddle under the lower of its two peaks;
|
|
18
|
+
PASS = [0.12, 0.2], // the share of the saddles that are passes, and how much lower a pass lies
|
|
19
|
+
HELD = 0.6, // the share of its length beyond its width that an ellipse's crest holds its height over, where it is a range
|
|
20
|
+
LOP = 0.3, // how much narrower than its half-width a range's steeper flank may be, and the other wider
|
|
21
|
+
BOW = 0.3, // how far a flank's slope may be bowed out or hollowed
|
|
22
|
+
WANDER = 0.28, // how far a crest strays from its straight line, in half-widths
|
|
23
|
+
WARP = [0.5, 3.2], // how far a range's whole shape is pulled about, in half-widths, and over what length, in peak spacings
|
|
24
|
+
SHOULDER = 0.85, // the share of a crest's saddles that send a short spur of their own down one side
|
|
25
|
+
EMBAYED = 0.32, // how much nearer the crest a valley's floor meets the apron than a spur's toe does
|
|
26
|
+
CUT = 0.6, // the share of a flank's height the valleys between its spurs are cut down by
|
|
27
|
+
RIB_CUT = 0.42, // and the gullies in a spur's side
|
|
28
|
+
KNOB = 0.3, // how far a spur's own crest is notched between its knobs, as a share of the valleys' depth
|
|
29
|
+
RIB_SPAN = [36, 80], // metres from one such gully to the next: six cells of the ground at the least
|
|
30
|
+
CRAG = 0.42, // how far broken rock roughens a flank
|
|
31
|
+
NOTCH = 0.1, // and notches a crest between its peaks, as a share of its height
|
|
32
|
+
BEDS = { thick: 21, hold: 0.34, dip: [0.05, 0.03], warp: 9 }, // the rock's hard beds: metres from one to the next, how far they step a
|
|
33
|
+
// flank into cliffs and benches at the most, how they dip (metres to a metre east and south) and how far (metres) they are warped
|
|
34
|
+
SPUR_LEAST = 0.1, // the share of its height a crest must keep for a spur to leave it
|
|
35
|
+
SCARP = { face: 0.5, back: 1.3, rim: 0.55, cliff: [0.28, 0.48], level: 0.35, wander: 2.4 }, // an escarpment: its face's and its dip
|
|
36
|
+
// slope's widths as shares of the half-width, how far its rim stands off the middle toward its face, how far down the face its
|
|
37
|
+
// cliff reaches (in flank widths) and the share of the height it drops, how much of a range's peaks and saddles its rim keeps,
|
|
38
|
+
// and how much farther than a range's crest it strays, in bays and headlands
|
|
39
|
+
HILL = { base: 0.3, long: [0.2, 0.5], wide: [0.35, 0.6], high: [0.4, 1], turn: 1.1 }, // a field of hills: the swell they stand
|
|
40
|
+
// on as a share of its height; each hill's half-length as a share of the field's lesser radius, its half-width as a share of its
|
|
41
|
+
// length, its height as a share of the field's, and how far (radians) its crest may turn off the field's strike
|
|
42
|
+
SWELL_REACH = 3.2, // how far out a swell's foot runs, as its radius squared,
|
|
43
|
+
SWELL_WARP = 0.22, // and how far that radius is pulled about
|
|
44
|
+
SEG = 9; // numbers to a spur: its ends (u, v), the metres along it at each, its flanks' width at each, its ribs' phase
|
|
45
|
+
|
|
46
|
+
// The long axis of an upland given as an ellipse ({ x, z, rx, rz }), as [bearing, half-length, half-width]: the longer of its
|
|
47
|
+
// radii, or along `axis` (a bearing: 0 east, clockwise seen from above), with rx then its half-length and rz its half-width.
|
|
48
|
+
const ellipseAxis = m => (m.axis !== undefined ? [m.axis, m.rx, m.rz] : m.rx >= m.rz ? [0, m.rx, m.rz] : [Math.PI / 2, m.rz, m.rx]);
|
|
49
|
+
// The line of an upland's crest as its data gives it ([[x, z], ...]) and its half-width, for the map's drawing of it.
|
|
50
|
+
export function crestOf(m) {
|
|
51
|
+
if (m.crest) return { line: m.crest, wide: m.wide };
|
|
52
|
+
const [bearing, a, b] = ellipseAxis(m),
|
|
53
|
+
reach = 0.8 * a;
|
|
54
|
+
return { line: [-reach, reach].map(u => [m.x + u * Math.cos(bearing), m.z + u * Math.sin(bearing)]), wide: b };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Each field owns its layout and sampling scratch so separate terrains can be sampled independently.
|
|
58
|
+
export function createMountainRanges(seed = 0) {
|
|
59
|
+
const { fbm, ridged, vnoise } = createNoise(seed);
|
|
60
|
+
const hash2 = createHash(seed);
|
|
61
|
+
// What rangesAt found out about the place it worked out last, for the colours: how far it is scree and wash, the apron at a
|
|
62
|
+
// range's foot and the floors of its valleys.
|
|
63
|
+
const range = { scree: 0 };
|
|
64
|
+
let uplands = [], // the uplands laid out: { limbs, x0, x1, z0, z1 }
|
|
65
|
+
crag = -1, // the noise every swell is broken by at the place rangesAt is working out, once one has asked for it,
|
|
66
|
+
pull = 0; // and what pulls its radius about
|
|
67
|
+
|
|
68
|
+
// The swell of an ellipse ({ x, z, rx, rz, h, rough }) at (x, z), in metres: a rise that falls away smoothly all round, broken
|
|
69
|
+
// by noise the more it is rough.
|
|
70
|
+
function swellAt(m, x, z) {
|
|
71
|
+
const r0 = ((x - m.x) / m.rx) ** 2 + ((z - m.z) / m.rz) ** 2;
|
|
72
|
+
if (r0 >= SWELL_REACH + SWELL_WARP / 2) return 0;
|
|
73
|
+
if (crag < 0) {
|
|
74
|
+
// the noise is worked out only where a swell is near
|
|
75
|
+
crag = ridged(x * 0.0034, z * 0.0034, 5, 331);
|
|
76
|
+
pull = SWELL_WARP * (fbm(x * 0.0021, z * 0.0021, 2, 337) - 0.5);
|
|
77
|
+
}
|
|
78
|
+
const r = r0 + pull;
|
|
79
|
+
return r < SWELL_REACH ? m.h * Math.exp(-1.5 * Math.max(r, 0)) * lerp(1, 0.35 + 1.25 * crag, m.rough) : 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// The height of a limb's crest at u, as a share of its own: 1 along its length, falling away past each end.
|
|
83
|
+
function crestShare(L, u) {
|
|
84
|
+
const over = u < 0 ? -u / L.tail0 : u > L.len ? (u - L.len) / L.tail1 : 0;
|
|
85
|
+
return (over > 0 ? Math.exp(-1.5 * over ** L.fall) : 1) * lerp(L.k0, L.k1, clamp(u / (L.len || 1), 0, 1));
|
|
86
|
+
}
|
|
87
|
+
// How far a limb's crest stands off its straight line at u: it wanders, and an escarpment's rim stands toward its face.
|
|
88
|
+
function crestOffset(L, u) {
|
|
89
|
+
const stray = 2 * (vnoise(u / L.wave + 7.3, 0, L.seed) - L.strayAt);
|
|
90
|
+
return L.rim + L.wander * stray * (L.line ? sstep(0, L.step, u) * sstep(L.len, L.len - L.step, u) : 1); // the stretches of a line meet end to end
|
|
91
|
+
}
|
|
92
|
+
// How far a range leans to one side at u, -1..1: no range's two flanks are alike, and which is the steeper changes along it.
|
|
93
|
+
const leanAt = (L, u, seed) => 2 * vnoise(u / (4 * L.step) + 3.1, 0, L.seed + seed) - 1;
|
|
94
|
+
// The width of a limb's flank on `side` of its crest at u, where the crest stands at share E of its height: a range narrows as
|
|
95
|
+
// its crest falls, so its flanks keep their slope, and the flank it leans toward is the narrower and steeper.
|
|
96
|
+
const flankWidth = (L, E, side, u) =>
|
|
97
|
+
L.b * (NARROW + (1 - NARROW) * E) * (L.face ? (L.face === side ? SCARP.face : SCARP.back) : 1 - side * LOP * L.rough * leanAt(L, u, 9));
|
|
98
|
+
// How far a range's shape is pulled off its place at (x, z), along x (`turn` 0) or z (1), as a share of the most: at two scales.
|
|
99
|
+
const warpAt = (L, x, z, turn) => 2 * fbm(x * L.warpOver + 31.7 * turn, z * L.warpOver + 11.3 * turn, 2, L.seed + 3 + turn) - 1;
|
|
100
|
+
// How far a crest is notched at u, as a share of the most.
|
|
101
|
+
const notchAt = (L, u) => 2 * fbm(u / (0.7 * L.step), 0, 3, L.seed + 6) - 1;
|
|
102
|
+
|
|
103
|
+
// The peaks and saddles of a limb's crest, as [u, share of the height] in turn from a peak to a peak: a peak stands where the
|
|
104
|
+
// data puts the upland (the middle of an ellipse, the points of a line), the rest a spacing apart.
|
|
105
|
+
function crestKnots(L, spacing) {
|
|
106
|
+
const first = L.line ? 0 : L.len / 2,
|
|
107
|
+
step = L.line ? L.len / Math.max(1, Math.round(L.len / spacing)) : spacing,
|
|
108
|
+
last = L.line ? Math.round(L.len / step) : 0, // the peak at a line's far point
|
|
109
|
+
from = -Math.ceil((first + 1.3 * L.tail0) / step),
|
|
110
|
+
to = Math.ceil((L.len + 1.3 * L.tail1 - first) / step),
|
|
111
|
+
level = L.rough * (L.face ? SCARP.level : 1),
|
|
112
|
+
knots = [];
|
|
113
|
+
let before = 0;
|
|
114
|
+
for (let k = from; k <= to; k++) {
|
|
115
|
+
const given = k === 0 || (L.line && k === last),
|
|
116
|
+
u = first + (k + (given ? 0 : 0.6 * (hash2(k, 1, L.seed) - 0.5))) * step,
|
|
117
|
+
peak = given ? 1 : 1 - level * PEAK_DROP * Math.sqrt(hash2(k, 2, L.seed)); // a few stand near the highest, most well under it
|
|
118
|
+
if (k > from) {
|
|
119
|
+
const drop = lerp(SADDLE_DROP[0], SADDLE_DROP[1], hash2(k, 4, L.seed)) + (hash2(k, 5, L.seed) < PASS[0] ? PASS[1] : 0);
|
|
120
|
+
knots.push(lerp(knots[knots.length - 2], u, 0.35 + 0.3 * hash2(k, 3, L.seed)), Math.min(before, peak) - level * drop);
|
|
121
|
+
}
|
|
122
|
+
knots.push(u, peak);
|
|
123
|
+
before = peak;
|
|
124
|
+
}
|
|
125
|
+
L.step = step;
|
|
126
|
+
return Float64Array.from(knots);
|
|
127
|
+
}
|
|
128
|
+
// How high a limb's crest stands at u between its peaks and saddles, as a share: pointed at a peak, level across a saddle.
|
|
129
|
+
function knotShare(L, u) {
|
|
130
|
+
const K = L.knots,
|
|
131
|
+
n = K.length / 2;
|
|
132
|
+
if (u <= K[0]) return K[1];
|
|
133
|
+
if (u >= K[2 * n - 2]) return K[2 * n - 1];
|
|
134
|
+
let i = clamp(Math.floor(((u - K[0]) / L.step) * 2), 0, n - 2);
|
|
135
|
+
while (K[2 * i] > u) i--;
|
|
136
|
+
while (K[2 * i + 2] < u) i++;
|
|
137
|
+
const t = (u - K[2 * i]) / (K[2 * i + 2] - K[2 * i]),
|
|
138
|
+
a = K[2 * i + 1],
|
|
139
|
+
b = K[2 * i + 3];
|
|
140
|
+
return a >= b ? b + (a - b) * (1 - t) ** PEAK_POINT : a + (b - a) * t ** PEAK_POINT;
|
|
141
|
+
}
|
|
142
|
+
// The spurs that run down one side of a limb: one from each peak, with a branch or two that leave it lower down, and a short
|
|
143
|
+
// one from the shoulder of most saddles. A spur is a straight line: the range's warp bends it.
|
|
144
|
+
function limbSpurs(L, side) {
|
|
145
|
+
const out = [],
|
|
146
|
+
spur = (u0, v0, angle, long, tau, wide0, wide1, seed) =>
|
|
147
|
+
out.push(
|
|
148
|
+
u0,
|
|
149
|
+
v0,
|
|
150
|
+
u0 + long * Math.sin(angle),
|
|
151
|
+
v0 + side * long * Math.cos(angle),
|
|
152
|
+
tau,
|
|
153
|
+
tau + long,
|
|
154
|
+
wide0,
|
|
155
|
+
wide1,
|
|
156
|
+
hash2(seed, 9, L.seed),
|
|
157
|
+
);
|
|
158
|
+
for (let k = 0; k < L.knots.length; k += 2) {
|
|
159
|
+
const u = L.knots[k],
|
|
160
|
+
E = crestShare(L, u),
|
|
161
|
+
id = k + (side > 0 ? 1 : 0),
|
|
162
|
+
r = n => hash2(id, n, L.seed + 1),
|
|
163
|
+
peak = k % 4 === 0;
|
|
164
|
+
if (E < SPUR_LEAST || (!peak && r(0) > SHOULDER)) continue;
|
|
165
|
+
const buttress = L.face === side, // an escarpment's face is buttressed, not spurred
|
|
166
|
+
long = flankWidth(L, E, side, u) * FOOT * (buttress ? 0.75 : peak ? 0.45 + 0.7 * r(1) : 0.3 + 0.35 * r(1)),
|
|
167
|
+
splay = u < 0 ? -sstep(0, 1, -u / L.tail0) : u > L.len ? sstep(0, 1, (u - L.len) / L.tail1) : 0, // round a crest's end they fan out
|
|
168
|
+
lean = (r(2) - 0.5) * 1.1 + 0.9 * splay,
|
|
169
|
+
v = crestOffset(L, u),
|
|
170
|
+
girth = L.step * (0.8 + 0.4 * r(7));
|
|
171
|
+
spur(u, v, lean, long, 0, 0.42 * girth, 0.64 * girth, id);
|
|
172
|
+
if (buttress || !peak) continue;
|
|
173
|
+
for (let b = 0; b < 2; b++) {
|
|
174
|
+
const at = (b ? 0.6 : 0.38) + 0.14 * r(3 + b),
|
|
175
|
+
turn = lean + (b ? -1 : 1) * (r(5) < 0.5 ? -1 : 1) * (0.6 + 0.4 * r(6 + b));
|
|
176
|
+
spur(
|
|
177
|
+
u + at * long * Math.sin(lean),
|
|
178
|
+
v + side * at * long * Math.cos(lean),
|
|
179
|
+
turn,
|
|
180
|
+
(1 - at) * long * 0.85,
|
|
181
|
+
at * long,
|
|
182
|
+
0.3 * girth,
|
|
183
|
+
0.42 * girth,
|
|
184
|
+
id + 1000 * (b + 1),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// where a crest ends, a long spur runs out and away on either side of its fall, so that the end of a range is no cone
|
|
189
|
+
for (const [u, away, free] of [
|
|
190
|
+
[0, -1, L.free0],
|
|
191
|
+
[L.len, 1, L.free1],
|
|
192
|
+
]) {
|
|
193
|
+
if (!free) continue;
|
|
194
|
+
const id = 5000 + (away > 0 ? 2 : 0) + (side > 0 ? 1 : 0),
|
|
195
|
+
long = flankWidth(L, crestShare(L, u), side, u) * FOOT * (0.7 + 0.6 * hash2(id, 1, L.seed + 1)),
|
|
196
|
+
girth = L.step * (1 + 0.4 * hash2(id, 2, L.seed + 1));
|
|
197
|
+
spur(u, crestOffset(L, u), away * (0.6 + 0.7 * hash2(id, 3, L.seed + 1)), long, 0, 0.42 * girth, 0.64 * girth, id);
|
|
198
|
+
}
|
|
199
|
+
return binned(L, out);
|
|
200
|
+
}
|
|
201
|
+
// The spurs of `list` sorted into bins along a limb's crest, a peak spacing long each: every bin holds the spurs that reach into
|
|
202
|
+
// its stretch, so a place asks only the few that could lie near it.
|
|
203
|
+
function binned(L, list) {
|
|
204
|
+
const bins = Array.from({ length: Math.ceil((L.to - L.from) / L.step) }, () => []);
|
|
205
|
+
for (let i = 0; i < list.length; i += SEG) {
|
|
206
|
+
const reach = Math.max(list[i + 6], list[i + 7]),
|
|
207
|
+
first = Math.floor((Math.min(list[i], list[i + 2]) - reach - L.from) / L.step),
|
|
208
|
+
last = Math.floor((Math.max(list[i], list[i + 2]) + reach - L.from) / L.step);
|
|
209
|
+
for (let b = Math.max(first, 0); b <= Math.min(last, bins.length - 1); b++) bins[b].push(...list.slice(i, i + SEG));
|
|
210
|
+
}
|
|
211
|
+
return bins.map(bin => Float64Array.from(bin));
|
|
212
|
+
}
|
|
213
|
+
// A limb: a straight stretch of crest from (x, z) along the unit vector (ax, az), `len` metres long and falling away past its
|
|
214
|
+
// ends over tail0 and tail1; k0, k1: its height at each end, as shares of h; b: its half-width; face: the side of an
|
|
215
|
+
// escarpment's steep face (the sign of v), 0 for a range; line: whether it is a stretch of a line the data gave; free0, free1:
|
|
216
|
+
// whether the crest ends at that end of the limb, or another limb carries it on; swell: the ellipse it is the range of, whose
|
|
217
|
+
// swell it is where it is not broken enough to be one (none: a swell about its own crest).
|
|
218
|
+
function limb(spec, seed) {
|
|
219
|
+
const sharp = sstep(RANGE_FROM[0], RANGE_FROM[1], spec.rough),
|
|
220
|
+
L = {
|
|
221
|
+
...spec,
|
|
222
|
+
seed,
|
|
223
|
+
sharp,
|
|
224
|
+
fall: lerp(2, 3, sharp),
|
|
225
|
+
rim: spec.face * SCARP.rim * spec.b,
|
|
226
|
+
wander: WANDER * spec.b * spec.rough * (spec.face ? SCARP.wander : 1),
|
|
227
|
+
strayAt: 0.5,
|
|
228
|
+
};
|
|
229
|
+
L.knots = crestKnots(L, clamp(PEAK_SPACING * L.b, PEAK_SPAN[0], PEAK_SPAN[1]));
|
|
230
|
+
L.wave = 3 * L.step;
|
|
231
|
+
L.warp = WARP[0] * L.b * L.rough * sharp;
|
|
232
|
+
L.warpOver = 1 / (WARP[1] * L.step);
|
|
233
|
+
L.rib = clamp(0.3 * L.step, RIB_SPAN[0], RIB_SPAN[1]);
|
|
234
|
+
// the stretch of its frame outside which the range raises nothing, however it is pulled about (by twice its warp at the most)
|
|
235
|
+
L.from = -TAIL_REACH * L.tail0 - 2 * L.warp;
|
|
236
|
+
L.to = L.len + TAIL_REACH * L.tail1 + 2 * L.warp;
|
|
237
|
+
L.far = REACH * L.b * Math.max(1 + LOP, SCARP.back) + Math.abs(L.rim) + 2 * L.wander + 2 * L.warp;
|
|
238
|
+
// a summit stands where the data puts it, the middle of an ellipse or the points of a line: there the crest has not strayed
|
|
239
|
+
// (crestOffset), nor been pulled about or notched, which `pin` holds for each end of the limb [warp along x, along z, notch]
|
|
240
|
+
const mid = L.len / 2,
|
|
241
|
+
pinAt = u => [warpAt(L, L.x + u * L.ax, L.z + u * L.az, 0), warpAt(L, L.x + u * L.ax, L.z + u * L.az, 1), notchAt(L, u)];
|
|
242
|
+
L.pin = L.line ? [pinAt(0), pinAt(L.len)] : [pinAt(mid), pinAt(mid)];
|
|
243
|
+
if (!L.line) L.strayAt = vnoise(mid / L.wave + 7.3, 0, seed);
|
|
244
|
+
L.spurs = { 1: limbSpurs(L, 1), [-1]: limbSpurs(L, -1) };
|
|
245
|
+
return L;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const spurAt = { best: 0, next: 0, tau: 0, phase: 0, side: 0 }; // what spurNear found: the nearest spur's share and the next one's, how far along the nearest, its phase and which side of it
|
|
249
|
+
// How far (u, v) lies on the spurs of `list`: 1 on a spur's line, 0 a flank's width off it.
|
|
250
|
+
function spurNear(list, u, v) {
|
|
251
|
+
let best = 0,
|
|
252
|
+
next = 0;
|
|
253
|
+
for (let i = 0; i < list.length; i += SEG) {
|
|
254
|
+
const au = list[i],
|
|
255
|
+
av = list[i + 1],
|
|
256
|
+
eu = list[i + 2] - au,
|
|
257
|
+
ev = list[i + 3] - av,
|
|
258
|
+
t = clamp(((u - au) * eu + (v - av) * ev) / (eu * eu + ev * ev), 0, 1),
|
|
259
|
+
du = u - au - t * eu,
|
|
260
|
+
dv = v - av - t * ev,
|
|
261
|
+
share = 1 - Math.hypot(du, dv) / lerp(list[i + 6], list[i + 7], t);
|
|
262
|
+
if (share > best) {
|
|
263
|
+
next = best;
|
|
264
|
+
best = share;
|
|
265
|
+
spurAt.tau = lerp(list[i + 4], list[i + 5], t);
|
|
266
|
+
spurAt.phase = list[i + 8];
|
|
267
|
+
spurAt.side = eu * dv - ev * du > 0 ? 0.37 : 0; // the gullies of its two sides do not meet across it
|
|
268
|
+
} else if (share > next) next = share;
|
|
269
|
+
}
|
|
270
|
+
spurAt.best = best;
|
|
271
|
+
spurAt.next = next;
|
|
272
|
+
}
|
|
273
|
+
// A flank's rock slope at s flank widths from the crest, as a share of the crest's height: a slope down to its foot, bowed out
|
|
274
|
+
// into a shoulder (`bow` under 1) or hollowed under a steep head (over 1); an escarpment's face a cliff over a bank of talus,
|
|
275
|
+
// its back a dip slope.
|
|
276
|
+
function flankBody(s, scarp, bow) {
|
|
277
|
+
if (scarp > 0) {
|
|
278
|
+
const [reach, drop] = SCARP.cliff;
|
|
279
|
+
return s < reach ? 1 - drop * sstep(0.02, reach, s) : (1 - drop) * Math.max(0, 1 - (s - reach) / (FOOT - reach)) ** 1.25;
|
|
280
|
+
}
|
|
281
|
+
return Math.max(0, 1 - s / FOOT) ** (scarp < 0 ? 0.9 : bow);
|
|
282
|
+
}
|
|
283
|
+
// A flank of rock h metres high at (x, z), stepped by its beds `hold` of the way: a cliff where a hard bed crops out, a bench
|
|
284
|
+
// on the softer rock over it. The beds dip, so the steps do not follow the flank's own level lines; they are thick and thin by
|
|
285
|
+
// turns, and a bed that stands as a cliff on one spur is lost under scree on the next.
|
|
286
|
+
function bedded(h, x, z, hold) {
|
|
287
|
+
const level = (h + BEDS.dip[0] * x + BEDS.dip[1] * z + BEDS.warp * vnoise(x * 0.004, z * 0.004, 347)) / BEDS.thick,
|
|
288
|
+
uneven = level + 0.3 * Math.sin(level * 2.4),
|
|
289
|
+
f = uneven - Math.floor(uneven),
|
|
290
|
+
shown = sstep(0.3, 0.7, vnoise(x * 0.006, z * 0.006, 349) + 0.25 * Math.sin(level * 1.7));
|
|
291
|
+
return h + hold * shown * sstep(0, BEDS.thick, h) * BEDS.thick * (sstep(0.3, 0.8, f) - f);
|
|
292
|
+
}
|
|
293
|
+
// Metres a limb's range raises the ground at (x, z), and the swell about its crest where it has no ellipse's (`smooth`: how
|
|
294
|
+
// far it is that swell).
|
|
295
|
+
function rangeHeight(L, x, z, smooth) {
|
|
296
|
+
const px = x - L.x,
|
|
297
|
+
pz = z - L.z,
|
|
298
|
+
along = px * L.ax + pz * L.az;
|
|
299
|
+
if (along < L.from || along > L.to || Math.abs(pz * L.ax - px * L.az) > L.far) return 0; // before any noise is worked out
|
|
300
|
+
// a range's shape is pulled about, so that no crest, spur or foot is a ruled line; not at its summits (L.pin)
|
|
301
|
+
const [from, to] = L.pin,
|
|
302
|
+
held = clamp(along / (L.len || 1), 0, 1),
|
|
303
|
+
dx = px + L.warp * (warpAt(L, x, z, 0) - lerp(from[0], to[0], held)),
|
|
304
|
+
dz = pz + L.warp * (warpAt(L, x, z, 1) - lerp(from[1], to[1], held)),
|
|
305
|
+
u = dx * L.ax + dz * L.az;
|
|
306
|
+
if (u < -TAIL_REACH * L.tail0 || u > L.len + TAIL_REACH * L.tail1) return 0;
|
|
307
|
+
const off = dz * L.ax - dx * L.az - crestOffset(L, u),
|
|
308
|
+
side = off < 0 ? -1 : 1,
|
|
309
|
+
D = side * off,
|
|
310
|
+
E = crestShare(L, u),
|
|
311
|
+
crest = L.h * E * knotShare(L, u) * (1 + L.sharp * L.rough * NOTCH * (notchAt(L, u) - lerp(from[2], to[2], held))),
|
|
312
|
+
swell = smooth > 0 ? crest * Math.exp(-1.5 * (D / L.b) ** 2) : 0,
|
|
313
|
+
wide = flankWidth(L, E, side, u),
|
|
314
|
+
s = D / wide;
|
|
315
|
+
if (smooth >= 1 || s >= REACH) return swell * smooth;
|
|
316
|
+
spurNear(L.spurs[side][clamp(Math.floor((u - L.from) / L.step), 0, L.spurs[side].length - 1)], u, off);
|
|
317
|
+
const R = spurAt.best,
|
|
318
|
+
scarp = L.face ? (L.face === side ? 1 : -1) : 0,
|
|
319
|
+
own = sstep(0, 0.25, R - spurAt.next), // how far the place is one spur's alone: what is its own (its knobs, its gullies) fades out toward the next spur
|
|
320
|
+
knobs = 1 - KNOB * own * vnoise(spurAt.tau / L.step + 40 * spurAt.phase, 0, L.seed + 8),
|
|
321
|
+
floor = 1 - ((R * R) / (R + 0.15)) * 1.15 * knobs, // how far the place is a valley's floor: a spur's crest is a sharp line, the floor rounded
|
|
322
|
+
round = CREST_ROUND / wide,
|
|
323
|
+
bow = 1 + side * BOW * L.rough * leanAt(L, u, 10),
|
|
324
|
+
body = flankBody((Math.sqrt(s * s + round * round) - round) / (1 - EMBAYED * floor * sstep(0.1, 0.5, s)), scarp, bow),
|
|
325
|
+
deep = L.rough * CUT * sstep(0.02, 0.3, s) * (scarp < 0 ? 0.5 : 1),
|
|
326
|
+
sides = L.rough * RIB_CUT * own * (1 - R) * sstep(0.1, 0.35, s),
|
|
327
|
+
gully = spurAt.tau / L.rib + spurAt.phase + spurAt.side,
|
|
328
|
+
cut = (1 - deep * floor) * (1 - sides * Math.abs(2 * (gully - Math.floor(gully)) - 1)), // what the valleys and gullies leave of the flank
|
|
329
|
+
broken = body > 0 ? 1 + L.rough * CRAG * sstep(0, 0.12, s) * (ridged(x * 0.009, z * 0.009, 4, 331) - 0.45) : 1,
|
|
330
|
+
stone = bedded(crest * (1 - APRON) * body * cut * broken, x, z, BEDS.hold * L.rough),
|
|
331
|
+
// the apron is banked highest where a valley comes out onto it, in a fan
|
|
332
|
+
apron = crest * APRON * Math.exp(-1.2 * s * s) * (1 + 0.5 * L.rough * (floor - 0.5) * sstep(0.6, 1, s)),
|
|
333
|
+
lies = sstep(2, 1.3, s) * Math.max(sstep(0.75, 1.05, s), 0.7 * floor * floor * sstep(0.3, 0.8, s)),
|
|
334
|
+
loose = L.sharp * sstep(8, 30, L.h * E) * lies;
|
|
335
|
+
if (loose > range.scree) range.scree = loose;
|
|
336
|
+
return lerp(stone + apron, swell, smooth);
|
|
337
|
+
}
|
|
338
|
+
// Metres a limb raises the ground at (x, z): its range, and where it is not broken enough to be one wholly, its swell.
|
|
339
|
+
function limbHeight(L, x, z) {
|
|
340
|
+
if (!L.swell) return rangeHeight(L, x, z, 1 - L.sharp);
|
|
341
|
+
return (L.sharp < 1 ? (1 - L.sharp) * swellAt(L.swell, x, z) : 0) + (L.sharp > 0 ? L.sharp * rangeHeight(L, x, z, 0) : 0);
|
|
342
|
+
}
|
|
343
|
+
// The higher of two limbs of one upland, rounded where they meet.
|
|
344
|
+
function joined(a, b) {
|
|
345
|
+
const most = Math.max(a, b),
|
|
346
|
+
k = 0.25 * most,
|
|
347
|
+
near = k > 0 ? Math.max(k - Math.abs(a - b), 0) / k : 0;
|
|
348
|
+
return most + (near * near * k) / 4;
|
|
349
|
+
}
|
|
350
|
+
// Metres the uplands laid out raise the ground at (x, z); `range` then says what else was found there.
|
|
351
|
+
function rangesAt(x, z) {
|
|
352
|
+
let h = 0;
|
|
353
|
+
range.scree = 0;
|
|
354
|
+
crag = -1;
|
|
355
|
+
for (let i = 0; i < uplands.length; i++) {
|
|
356
|
+
const U = uplands[i];
|
|
357
|
+
if (x < U.x0 || x > U.x1 || z < U.z0 || z > U.z1) continue;
|
|
358
|
+
let top = 0;
|
|
359
|
+
for (let j = 0; j < U.limbs.length; j++) top = joined(top, limbHeight(U.limbs[j], x, z));
|
|
360
|
+
h += top;
|
|
361
|
+
}
|
|
362
|
+
return h;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// An upland's limbs with the box that holds all they raise.
|
|
366
|
+
function boxed(limbs) {
|
|
367
|
+
const box = { limbs, x0: Infinity, x1: -Infinity, z0: Infinity, z1: -Infinity },
|
|
368
|
+
hold = (x, z) => {
|
|
369
|
+
box.x0 = Math.min(box.x0, x);
|
|
370
|
+
box.x1 = Math.max(box.x1, x);
|
|
371
|
+
box.z0 = Math.min(box.z0, z);
|
|
372
|
+
box.z1 = Math.max(box.z1, z);
|
|
373
|
+
};
|
|
374
|
+
for (const L of limbs) {
|
|
375
|
+
for (const u of [L.from, L.to]) for (const v of [-L.far, L.far]) hold(L.x + u * L.ax - v * L.az, L.z + u * L.az + v * L.ax);
|
|
376
|
+
if (L.swell) {
|
|
377
|
+
const reach = Math.sqrt(SWELL_REACH + SWELL_WARP / 2);
|
|
378
|
+
hold(L.swell.x - reach * L.swell.rx, L.swell.z - reach * L.swell.rz);
|
|
379
|
+
hold(L.swell.x + reach * L.swell.rx, L.swell.z + reach * L.swell.rz);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return box;
|
|
383
|
+
}
|
|
384
|
+
// The side of a limb (the sign of v) that the bearing `face` looks toward; 0 where the upland is no escarpment.
|
|
385
|
+
const faceSide = (face, ax, az) => (face === undefined ? 0 : Math.cos(face) * -az + Math.sin(face) * ax < 0 ? -1 : 1);
|
|
386
|
+
// An upland given as an ellipse ({ x, z, rx, rz, h, rough }): one limb along its long axis. The more broken it is, the longer
|
|
387
|
+
// its crest holds its height before it falls away.
|
|
388
|
+
function ellipseLimb(m, seed) {
|
|
389
|
+
const [bearing, a, b] = ellipseAxis(m),
|
|
390
|
+
held = HELD * sstep(RANGE_FROM[0], RANGE_FROM[1], m.rough) * Math.max(a - 0.5 * b, 0.2 * a), // the half-length its crest holds its height over: little of a round one's
|
|
391
|
+
tail = a - 0.95 * held,
|
|
392
|
+
ax = Math.cos(bearing),
|
|
393
|
+
az = Math.sin(bearing),
|
|
394
|
+
crest = {
|
|
395
|
+
x: m.x - held * ax,
|
|
396
|
+
z: m.z - held * az,
|
|
397
|
+
ax,
|
|
398
|
+
az,
|
|
399
|
+
len: 2 * held,
|
|
400
|
+
tail0: tail,
|
|
401
|
+
tail1: tail,
|
|
402
|
+
k0: 1,
|
|
403
|
+
k1: 1,
|
|
404
|
+
free0: true,
|
|
405
|
+
free1: true,
|
|
406
|
+
};
|
|
407
|
+
return limb(
|
|
408
|
+
{ ...crest, b, h: m.h, rough: m.rough, face: faceSide(m.face, ax, az), line: false, swell: m.axis === undefined ? m : null },
|
|
409
|
+
seed,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
// An upland given as a line ({ crest: [[x, z, share of h], ...], wide, h, rough }): a limb to each stretch, each running on a
|
|
413
|
+
// little past the point where the next begins.
|
|
414
|
+
function lineLimbs(m, seed) {
|
|
415
|
+
const limbs = [],
|
|
416
|
+
tail = end => m.wide * (end ? 0.65 : 0.7);
|
|
417
|
+
for (let i = 0; i + 1 < m.crest.length; i++) {
|
|
418
|
+
const [x, z, k0] = m.crest[i],
|
|
419
|
+
[x1, z1, k1] = m.crest[i + 1],
|
|
420
|
+
len = Math.hypot(x1 - x, z1 - z),
|
|
421
|
+
ax = (x1 - x) / len,
|
|
422
|
+
az = (z1 - z) / len,
|
|
423
|
+
first = i === 0,
|
|
424
|
+
last = i + 2 === m.crest.length,
|
|
425
|
+
crest = { x, z, ax, az, len, tail0: tail(first), tail1: tail(last), k0, k1, free0: first, free1: last };
|
|
426
|
+
limbs.push(
|
|
427
|
+
limb({ ...crest, b: m.wide, h: m.h, rough: m.rough, face: faceSide(m.face, ax, az), line: true, swell: null }, seed + i * 3),
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
return limbs;
|
|
431
|
+
}
|
|
432
|
+
// A field of hills ({ x, z, rx, rz, h, rough, hills: how many }): small ranges scattered over its ellipse, their crests near one
|
|
433
|
+
// strike (`axis`, or the ellipse's long axis), on a low swell.
|
|
434
|
+
function hillField(m, seed) {
|
|
435
|
+
const [strike, , lesser] = ellipseAxis(m),
|
|
436
|
+
r = (i, n) => hash2(i, n, seed + 2),
|
|
437
|
+
out = [boxed([ellipseLimb({ ...m, h: m.h * HILL.base, rough: 0, axis: undefined }, seed)])];
|
|
438
|
+
for (let i = 0; i < m.hills; i++) {
|
|
439
|
+
const far = 0.85 * Math.sqrt((i + 0.5) / m.hills),
|
|
440
|
+
round = i * 2.39996 + r(i, 1), // the golden angle from one to the next, so they fill the ellipse evenly
|
|
441
|
+
long = lesser * lerp(HILL.long[0], HILL.long[1], r(i, 2)),
|
|
442
|
+
hill = {
|
|
443
|
+
x: m.x + m.rx * far * Math.cos(round),
|
|
444
|
+
z: m.z + m.rz * far * Math.sin(round),
|
|
445
|
+
rx: long,
|
|
446
|
+
rz: long * lerp(HILL.wide[0], HILL.wide[1], r(i, 3)),
|
|
447
|
+
h: m.h * lerp(HILL.high[0], HILL.high[1], r(i, 4)) * (1 - 0.4 * far * far),
|
|
448
|
+
rough: m.rough,
|
|
449
|
+
axis: strike + (r(i, 5) - 0.5) * HILL.turn,
|
|
450
|
+
};
|
|
451
|
+
out.push(boxed([ellipseLimb(hill, seed + 10 + i * 3)]));
|
|
452
|
+
}
|
|
453
|
+
return out;
|
|
454
|
+
}
|
|
455
|
+
// Lays out a land's uplands (its data's list), for rangesAt.
|
|
456
|
+
function layOutRanges(list) {
|
|
457
|
+
uplands = list.flatMap((m, i) => {
|
|
458
|
+
const seed = 400 + i * 41;
|
|
459
|
+
return m.hills ? hillField(m, seed) : [boxed(m.crest ? lineLimbs(m, seed) : [ellipseLimb(m, seed)])];
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return { layout: layOutRanges, heightAt: rangesAt, surface: range };
|
|
463
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { clamp } from './scalar.js';
|
|
2
|
+
import { createNoise } from './noise.js';
|
|
3
|
+
|
|
4
|
+
// The effects' noise sheet, shared by every sprite. R: billowing lumps with creases between them; G, B: the
|
|
5
|
+
// slope of R, so a sprite can be shaded as if it had relief; A: a softer cloud that frays edges and drives the flames. R's slope
|
|
6
|
+
// needs its neighbours, so the sheet is worked out whole first, in its own rows (lowest first), as whole bytes.
|
|
7
|
+
export function particleNoise({ size: N = 256, ridgeSeed = 311, cloudSeed = 733, noise = createNoise() } = {}) {
|
|
8
|
+
const { tfbm, tnoise } = noise;
|
|
9
|
+
const H = new Float32Array(N * N),
|
|
10
|
+
d = new Uint8Array(N * N * 4);
|
|
11
|
+
for (let y = 0; y < N; y++)
|
|
12
|
+
for (let x = 0; x < N; x++) {
|
|
13
|
+
const u = (x + 0.5) / N,
|
|
14
|
+
v = (y + 0.5) / N;
|
|
15
|
+
let h = 0,
|
|
16
|
+
a = 0.5,
|
|
17
|
+
n = 0,
|
|
18
|
+
per = 3;
|
|
19
|
+
for (let o = 0; o < 4; o++) {
|
|
20
|
+
h += a * Math.abs(tnoise(u * per, v * per, per, ridgeSeed + o * 17) * 2 - 1);
|
|
21
|
+
n += a;
|
|
22
|
+
a *= 0.5;
|
|
23
|
+
per *= 2;
|
|
24
|
+
}
|
|
25
|
+
H[y * N + x] = clamp((h / n) * 1.9, 0, 1);
|
|
26
|
+
}
|
|
27
|
+
for (let y = 0; y < N; y++)
|
|
28
|
+
for (let x = 0; x < N; x++) {
|
|
29
|
+
const i = y * N + x,
|
|
30
|
+
k = i * 4,
|
|
31
|
+
xm = (x + N - 1) % N,
|
|
32
|
+
xp = (x + 1) % N,
|
|
33
|
+
ym = (y + N - 1) % N,
|
|
34
|
+
yp = (y + 1) % N;
|
|
35
|
+
d[k] = H[i] * 255;
|
|
36
|
+
d[k + 1] = clamp(0.5 + (H[y * N + xp] - H[y * N + xm]) * 5, 0, 1) * 255;
|
|
37
|
+
d[k + 2] = clamp(0.5 + (H[yp * N + x] - H[ym * N + x]) * 5, 0, 1) * 255;
|
|
38
|
+
d[k + 3] = clamp((tfbm((x + 0.5) / N, (y + 0.5) / N, 5, 4, cloudSeed) - 0.5) * 1.7 + 0.5, 0, 1) * 255;
|
|
39
|
+
}
|
|
40
|
+
// fillPixels writes the row of v from the top down; the sheet's row y is read as the card's row y
|
|
41
|
+
return (u, v, out) => {
|
|
42
|
+
const k = ((N - 1 - Math.floor(v * N)) * N + Math.floor(u * N)) * 4;
|
|
43
|
+
out[0] = d[k];
|
|
44
|
+
out[1] = d[k + 1];
|
|
45
|
+
out[2] = d[k + 2];
|
|
46
|
+
out[3] = d[k + 3];
|
|
47
|
+
};
|
|
48
|
+
}
|
package/math/periodic.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `cycleDelta(from,to,period = 1)` returns the shortest signed displacement, with negative half turns.
|
|
3
|
+
* `approachPhase(from,to,step,onArrival?)` advances a normalized clock phase on the shorter arc. Supply phases from
|
|
4
|
+
* zero to one and a nonnegative maximum step in cycles. The optional callback runs when the remaining distance is
|
|
5
|
+
* within that step.
|
|
6
|
+
*/
|
|
7
|
+
export function cycleDelta(from, to, period = 1) {
|
|
8
|
+
const d = to - from;
|
|
9
|
+
return d - Math.round(d / period) * period;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Advance a normalized phase towards its target by at most step. Snap exactly on arrival.
|
|
13
|
+
export function approachPhase(from, to, step, onArrival) {
|
|
14
|
+
const d = cycleDelta(from, to);
|
|
15
|
+
if (Math.abs(d) <= step) {
|
|
16
|
+
onArrival?.();
|
|
17
|
+
return to;
|
|
18
|
+
}
|
|
19
|
+
return (from + Math.sign(d) * step + 1) % 1;
|
|
20
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createNoise } from './noise.js';
|
|
2
|
+
import { createHash } from './random.js';
|
|
3
|
+
import { cellEdge } from './cell-edge.js';
|
|
4
|
+
import { clamp, sstep } from './scalar.js';
|
|
5
|
+
|
|
6
|
+
// A family of roughly parallel traces. Whole cycles across U and V keep the tile seamless.
|
|
7
|
+
// The offset belongs to the joint along its entire length, so spacing varies without crossing its neighbours.
|
|
8
|
+
function fractureLine(coordinate, count, hash, salt, width) {
|
|
9
|
+
const id = Math.round(coordinate);
|
|
10
|
+
const wrapped = (id % count + count) % count;
|
|
11
|
+
const gap = Math.abs(coordinate - id - (hash(wrapped, 0, salt) - 0.5) * 0.63);
|
|
12
|
+
return 1 - sstep(width * 0.28, width, gap);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// R grain, G fractures, B weathering.
|
|
16
|
+
export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
|
|
17
|
+
const hash2 = createHash(seed);
|
|
18
|
+
const { tfbm } = createNoise(seed);
|
|
19
|
+
const jointed = fractures === 'joints';
|
|
20
|
+
if (!jointed && fractures !== 'cells') throw new TypeError('Unknown rock fracture pattern: ' + fractures);
|
|
21
|
+
return (u, v, o) => {
|
|
22
|
+
const n = tfbm(u, v, 6, 6, 81),
|
|
23
|
+
w = tfbm(u, v, 4, 3, 82) - 0.5;
|
|
24
|
+
o[0] = clamp(255 * (n * 1.25 - 0.12), 0, 255);
|
|
25
|
+
if (jointed) {
|
|
26
|
+
const warp = w * 0.22 + (n - 0.5) * 0.08;
|
|
27
|
+
// Independently spaced vertical joints stop in competent beds; the oblique set only opens in patches.
|
|
28
|
+
const trace = u * 7 + 0.17 * Math.sin(v * Math.PI * 2) + warp;
|
|
29
|
+
const joint = (Math.round(trace) % 7 + 7) % 7;
|
|
30
|
+
const bedCoordinate = v * 6 + 0.34 * Math.sin(u * Math.PI * 2) + w * 0.46;
|
|
31
|
+
const lower = Math.floor(bedCoordinate), bed = (lower % 6 + 6) % 6;
|
|
32
|
+
const fraction = bedCoordinate - lower;
|
|
33
|
+
const intact = hash2(joint, bed, 90);
|
|
34
|
+
const previous = hash2(joint, (bed + 5) % 6, 90);
|
|
35
|
+
const opening = sstep(0.22, 0.56, previous + (intact - previous) * sstep(0.0, 0.19, fraction));
|
|
36
|
+
const steep = fractureLine(trace, 7, hash2, 83, 0.055);
|
|
37
|
+
const cross = fractureLine(u * 3 - v * 3 - warp, 3, hash2, 84, 0.044);
|
|
38
|
+
const seam = fractureLine(bedCoordinate, 6, hash2, 85, 0.065);
|
|
39
|
+
const open = tfbm(u, v, 4, 2, 86);
|
|
40
|
+
const crossOpen = tfbm(u, v, 3, 2, 87);
|
|
41
|
+
o[1] = 255 * Math.max(
|
|
42
|
+
steep * opening * sstep(0.37, 0.61, open),
|
|
43
|
+
cross * sstep(0.53, 0.76, crossOpen) * 0.64,
|
|
44
|
+
seam * sstep(0.36, 0.72, crossOpen) * 0.60,
|
|
45
|
+
);
|
|
46
|
+
} else {
|
|
47
|
+
const e1 = cellEdge(hash2, u + w * 0.05, v - w * 0.03, 5, 83);
|
|
48
|
+
const e2 = cellEdge(hash2, u - w * 0.03, v + w * 0.05, 13, 84);
|
|
49
|
+
o[1] = 255 * Math.max(
|
|
50
|
+
sstep(0.014, 0.003, e1) * sstep(0.55, 0.66, tfbm(u, v, 5, 2, 86)),
|
|
51
|
+
0.5 * sstep(0.009, 0.002, e2) * sstep(0.66, 0.74, tfbm(u, v, 7, 2, 87)),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
o[2] = 255 * tfbm(u, v, 4, 5, 85);
|
|
55
|
+
};
|
|
56
|
+
}
|