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
package/math/noise.js
CHANGED
|
@@ -1,8 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seeded two-dimensional noise for terrain heights and seamless textures.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { createHash } from './random.js';
|
|
2
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Create a family of noise samplers that share a world seed. Use `fbm` for
|
|
10
|
+
* layered terrain, `ridged` for crests and `tfbm` for seamless textures.
|
|
11
|
+
* For a terrain feature spanning roughly 100 metres, divide world X/Z by 100
|
|
12
|
+
* before sampling the first octave. For a texture, pass unit UVs to `tfbm`;
|
|
13
|
+
* the opposite edges of the unit tile match.
|
|
14
|
+
* @example
|
|
15
|
+
* ```js
|
|
16
|
+
* import { createNoise } from 'roamwright';
|
|
17
|
+
*
|
|
18
|
+
* const { fbm, tfbm } = createNoise(42);
|
|
19
|
+
* const groundAt = (x, z) => 30 * fbm(x / 100, z / 100, 4, 7);
|
|
20
|
+
* const grainAt = (u, v) => tfbm(u, v, 8, 3, 11);
|
|
21
|
+
* ```
|
|
22
|
+
* @param worldSeed - Integer offset shared by the samplers. Each sample also accepts its own seed.
|
|
23
|
+
*/
|
|
3
24
|
export function createNoise(worldSeed = 0) {
|
|
4
25
|
const hash2 = createHash(worldSeed);
|
|
5
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Smooth value noise in [0, 1] between integer grid points.
|
|
28
|
+
* @param {number} x - Noise-grid X.
|
|
29
|
+
* @param {number} z - Noise-grid Z.
|
|
30
|
+
* @param {number} seed - Integer sample seed.
|
|
31
|
+
*/
|
|
6
32
|
function vnoise(x, z, seed) {
|
|
7
33
|
const ix = Math.floor(x),
|
|
8
34
|
iz = Math.floor(z),
|
|
@@ -10,71 +36,97 @@ export function createNoise(worldSeed = 0) {
|
|
|
10
36
|
fz = z - iz;
|
|
11
37
|
const ux = fx * fx * (3 - 2 * fx),
|
|
12
38
|
uz = fz * fz * (3 - 2 * fz);
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return
|
|
39
|
+
const nearLeft = hash2(ix, iz, seed),
|
|
40
|
+
nearRight = hash2(ix + 1, iz, seed),
|
|
41
|
+
farLeft = hash2(ix, iz + 1, seed),
|
|
42
|
+
farRight = hash2(ix + 1, iz + 1, seed);
|
|
43
|
+
return nearLeft + (nearRight - nearLeft) * ux + (farLeft - nearLeft) * uz + (nearLeft - nearRight - farLeft + farRight) * ux * uz;
|
|
18
44
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Average layers of value noise, halving the weight at each finer scale.
|
|
47
|
+
* @param {number} x - First-octave X.
|
|
48
|
+
* @param {number} z - First-octave Z.
|
|
49
|
+
* @param {number} octaves - Positive integer octave count.
|
|
50
|
+
* @param {number} seed - Integer sample seed.
|
|
51
|
+
*/
|
|
52
|
+
function fbm(x, z, octaves, seed) {
|
|
53
|
+
let weightedSum = 0,
|
|
54
|
+
amplitude = 0.5,
|
|
55
|
+
totalWeight = 0;
|
|
56
|
+
for (let octave = 0; octave < octaves; octave++) {
|
|
57
|
+
weightedSum += amplitude * vnoise(x, z, seed + octave * 17);
|
|
58
|
+
totalWeight += amplitude;
|
|
59
|
+
amplitude *= 0.5;
|
|
28
60
|
x = x * 2.03 + 11.7;
|
|
29
61
|
z = z * 2.03 - 5.3;
|
|
30
62
|
}
|
|
31
|
-
return
|
|
63
|
+
return weightedSum / totalWeight;
|
|
32
64
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Layer sharp crests where the underlying value noise crosses its midpoint.
|
|
67
|
+
* @param {number} x - First-octave X.
|
|
68
|
+
* @param {number} z - First-octave Z.
|
|
69
|
+
* @param {number} octaves - Positive integer octave count.
|
|
70
|
+
* @param {number} seed - Integer sample seed.
|
|
71
|
+
*/
|
|
72
|
+
function ridged(x, z, octaves, seed) {
|
|
73
|
+
let weightedSum = 0,
|
|
74
|
+
amplitude = 0.5,
|
|
75
|
+
totalWeight = 0;
|
|
76
|
+
for (let octave = 0; octave < octaves; octave++) {
|
|
77
|
+
const ridgeValue = 1 - Math.abs(vnoise(x, z, seed + octave * 31) * 2 - 1);
|
|
78
|
+
weightedSum += amplitude * ridgeValue * ridgeValue;
|
|
79
|
+
totalWeight += amplitude;
|
|
80
|
+
amplitude *= 0.5;
|
|
43
81
|
x = x * 2.1 + 3.1;
|
|
44
82
|
z = z * 2.1 + 8.9;
|
|
45
83
|
}
|
|
46
|
-
return
|
|
84
|
+
return weightedSum / totalWeight;
|
|
47
85
|
}
|
|
48
|
-
|
|
49
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Sample smooth noise that repeats after a whole number of lattice cells.
|
|
88
|
+
* @param {number} x - Horizontal lattice coordinate.
|
|
89
|
+
* @param {number} y - Vertical lattice coordinate.
|
|
90
|
+
* @param {number} period - Positive integer period along X.
|
|
91
|
+
* @param {number} seed - Integer sample seed.
|
|
92
|
+
* @param {number} [verticalPeriod] - Vertical period in cells. Defaults to per.
|
|
93
|
+
*/
|
|
94
|
+
function tnoise(x, y, period, seed, verticalPeriod = period) {
|
|
50
95
|
const ix = Math.floor(x),
|
|
51
96
|
iy = Math.floor(y),
|
|
52
97
|
fx = x - ix,
|
|
53
98
|
fy = y - iy;
|
|
54
99
|
const ux = fx * fx * (3 - 2 * fx),
|
|
55
100
|
uy = fy * fy * (3 - 2 * fy);
|
|
56
|
-
const x0 = ((ix %
|
|
57
|
-
x1 = (x0 + 1) %
|
|
58
|
-
y0 = ((iy %
|
|
59
|
-
y1 = (y0 + 1) %
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return
|
|
101
|
+
const x0 = ((ix % period) + period) % period,
|
|
102
|
+
x1 = (x0 + 1) % period,
|
|
103
|
+
y0 = ((iy % verticalPeriod) + verticalPeriod) % verticalPeriod,
|
|
104
|
+
y1 = (y0 + 1) % verticalPeriod;
|
|
105
|
+
const nearLeft = hash2(x0, y0, seed),
|
|
106
|
+
nearRight = hash2(x1, y0, seed),
|
|
107
|
+
farLeft = hash2(x0, y1, seed),
|
|
108
|
+
farRight = hash2(x1, y1, seed);
|
|
109
|
+
return nearLeft + (nearRight - nearLeft) * ux + (farLeft - nearLeft) * uy + (nearLeft - nearRight - farLeft + farRight) * ux * uy;
|
|
65
110
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Layer tileable noise in UV space. Adding one to u or v repeats the same sample.
|
|
113
|
+
* @param {number} u - Horizontal texture coordinate.
|
|
114
|
+
* @param {number} v - Vertical texture coordinate.
|
|
115
|
+
* @param {number} period - Positive integer first-octave cell count per tile.
|
|
116
|
+
* @param {number} octaves - Positive integer octave count.
|
|
117
|
+
* @param {number} seed - Integer sample seed.
|
|
118
|
+
*/
|
|
119
|
+
function tfbm(u, v, period, octaves, seed) {
|
|
120
|
+
let weightedSum = 0,
|
|
121
|
+
amplitude = 0.5,
|
|
122
|
+
totalWeight = 0;
|
|
123
|
+
for (let octave = 0; octave < octaves; octave++) {
|
|
124
|
+
weightedSum += amplitude * tnoise(u * period, v * period, period, seed + octave * 13);
|
|
125
|
+
totalWeight += amplitude;
|
|
126
|
+
amplitude *= 0.5;
|
|
127
|
+
period *= 2;
|
|
76
128
|
}
|
|
77
|
-
return
|
|
129
|
+
return weightedSum / totalWeight;
|
|
78
130
|
}
|
|
79
131
|
return { fbm, ridged, tfbm, tnoise, vnoise };
|
|
80
132
|
}
|
package/math/particle-noise.js
CHANGED
|
@@ -4,45 +4,45 @@ import { createNoise } from './noise.js';
|
|
|
4
4
|
// The effects' noise sheet, shared by every sprite. R: billowing lumps with creases between them; G, B: the
|
|
5
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
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:
|
|
7
|
+
export function particleNoise({ size: size = 256, ridgeSeed = 311, cloudSeed = 733, noise = createNoise() } = {}) {
|
|
8
8
|
const { tfbm, tnoise } = noise;
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
for (let y = 0; y <
|
|
12
|
-
for (let x = 0; x <
|
|
13
|
-
const u = (x + 0.5) /
|
|
14
|
-
v = (y + 0.5) /
|
|
9
|
+
const heightSamples = new Float32Array(size * size),
|
|
10
|
+
pixels = new Uint8Array(size * size * 4);
|
|
11
|
+
for (let y = 0; y < size; y++)
|
|
12
|
+
for (let x = 0; x < size; x++) {
|
|
13
|
+
const u = (x + 0.5) / size,
|
|
14
|
+
v = (y + 0.5) / size;
|
|
15
15
|
let h = 0,
|
|
16
|
-
|
|
16
|
+
amplitude = 0.5,
|
|
17
17
|
n = 0,
|
|
18
18
|
per = 3;
|
|
19
19
|
for (let o = 0; o < 4; o++) {
|
|
20
|
-
h +=
|
|
21
|
-
n +=
|
|
22
|
-
|
|
20
|
+
h += amplitude * Math.abs(tnoise(u * per, v * per, per, ridgeSeed + o * 17) * 2 - 1);
|
|
21
|
+
n += amplitude;
|
|
22
|
+
amplitude *= 0.5;
|
|
23
23
|
per *= 2;
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
heightSamples[y * size + x] = clamp((h / n) * 1.9, 0, 1);
|
|
26
26
|
}
|
|
27
|
-
for (let y = 0; y <
|
|
28
|
-
for (let x = 0; x <
|
|
29
|
-
const i = y *
|
|
27
|
+
for (let y = 0; y < size; y++)
|
|
28
|
+
for (let x = 0; x < size; x++) {
|
|
29
|
+
const i = y * size + x,
|
|
30
30
|
k = i * 4,
|
|
31
|
-
xm = (x +
|
|
32
|
-
xp = (x + 1) %
|
|
33
|
-
ym = (y +
|
|
34
|
-
yp = (y + 1) %
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
xm = (x + size - 1) % size,
|
|
32
|
+
xp = (x + 1) % size,
|
|
33
|
+
ym = (y + size - 1) % size,
|
|
34
|
+
yp = (y + 1) % size;
|
|
35
|
+
pixels[k] = heightSamples[i] * 255;
|
|
36
|
+
pixels[k + 1] = clamp(0.5 + (heightSamples[y * size + xp] - heightSamples[y * size + xm]) * 5, 0, 1) * 255;
|
|
37
|
+
pixels[k + 2] = clamp(0.5 + (heightSamples[yp * size + x] - heightSamples[ym * size + x]) * 5, 0, 1) * 255;
|
|
38
|
+
pixels[k + 3] = clamp((tfbm((x + 0.5) / size, (y + 0.5) / size, 5, 4, cloudSeed) - 0.5) * 1.7 + 0.5, 0, 1) * 255;
|
|
39
39
|
}
|
|
40
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
41
|
return (u, v, out) => {
|
|
42
|
-
const k = ((
|
|
43
|
-
out[0] =
|
|
44
|
-
out[1] =
|
|
45
|
-
out[2] =
|
|
46
|
-
out[3] =
|
|
42
|
+
const k = ((size - 1 - Math.floor(v * size)) * size + Math.floor(u * size)) * 4;
|
|
43
|
+
out[0] = pixels[k];
|
|
44
|
+
out[1] = pixels[k + 1];
|
|
45
|
+
out[2] = pixels[k + 2];
|
|
46
|
+
out[3] = pixels[k + 3];
|
|
47
47
|
};
|
|
48
48
|
}
|
package/math/periodic.js
CHANGED
|
@@ -5,16 +5,16 @@
|
|
|
5
5
|
* within that step.
|
|
6
6
|
*/
|
|
7
7
|
export function cycleDelta(from, to, period = 1) {
|
|
8
|
-
const
|
|
9
|
-
return
|
|
8
|
+
const delta = to - from;
|
|
9
|
+
return delta - Math.round(delta / period) * period;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
// Advance a normalized phase towards its target by at most step. Snap exactly on arrival.
|
|
13
13
|
export function approachPhase(from, to, step, onArrival) {
|
|
14
|
-
const
|
|
15
|
-
if (Math.abs(
|
|
14
|
+
const delta = cycleDelta(from, to);
|
|
15
|
+
if (Math.abs(delta) <= step) {
|
|
16
16
|
onArrival?.();
|
|
17
17
|
return to;
|
|
18
18
|
}
|
|
19
|
-
return (from + Math.sign(
|
|
19
|
+
return (from + Math.sign(delta) * step + 1) % 1;
|
|
20
20
|
}
|
package/math/random.js
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Repeatable random streams and coordinate hashes for procedural placement.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a repeatable stream of numbers in [0, 1). Save `next.state()` and pass
|
|
8
|
+
* it to a new generator to resume at the next draw, including in a worker.
|
|
9
|
+
* @param {number} state - Integer seed or a previously saved generator state.
|
|
10
|
+
*/
|
|
11
|
+
function mulberry32(state) {
|
|
4
12
|
const next = function () {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
return ((
|
|
13
|
+
state |= 0;
|
|
14
|
+
state = (state + 0x6d2b79f5) | 0;
|
|
15
|
+
let mixedState = Math.imul(state ^ (state >>> 15), 1 | state);
|
|
16
|
+
mixedState = (mixedState + Math.imul(mixedState ^ (mixedState >>> 7), 61 | mixedState)) ^ mixedState;
|
|
17
|
+
return ((mixedState ^ (mixedState >>> 14)) >>> 0) / 4294967296;
|
|
10
18
|
};
|
|
11
|
-
next.state = () =>
|
|
19
|
+
next.state = () => state;
|
|
12
20
|
return next;
|
|
13
21
|
}
|
|
14
|
-
|
|
15
|
-
const pickR = (
|
|
22
|
+
/** Pick one item using the supplied random generator. */
|
|
23
|
+
const pickR = (random, items) => items[(random() * items.length) | 0];
|
|
16
24
|
export { mulberry32, pickR };
|
|
17
25
|
|
|
26
|
+
/** Return a repeatable hash in [0, 1) for integer X/Z grid coordinates and a per-sample seed. */
|
|
18
27
|
export function createHash(worldSeed = 0) {
|
|
19
28
|
// A random number in 0..1 fixed by two integers and a seed.
|
|
20
29
|
function hash2(ix, iz, seed) {
|
|
21
|
-
let
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return (
|
|
30
|
+
let hash = (Math.imul(ix, 374761393) + Math.imul(iz, 668265263) + Math.imul(seed + worldSeed, 1442695041)) | 0;
|
|
31
|
+
hash = Math.imul(hash ^ (hash >>> 13), 1274126177);
|
|
32
|
+
hash ^= hash >>> 16;
|
|
33
|
+
return (hash >>> 0) / 4294967296;
|
|
25
34
|
}
|
|
26
35
|
return hash2;
|
|
27
36
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Distance in world units from a point to a yawed rectangle, zero inside.
|
|
2
|
+
* Supply the centre, half width and depth, yaw in radians, and the query point. */
|
|
3
|
+
export function distanceToRectangle(centerX, centerZ, halfWidth, halfDepth, rotation, pointX, pointZ) {
|
|
4
|
+
const dx = pointX - centerX,
|
|
5
|
+
dz = pointZ - centerZ,
|
|
6
|
+
cosRotation = Math.cos(rotation),
|
|
7
|
+
sinRotation = Math.sin(rotation);
|
|
8
|
+
return Math.hypot(Math.max(Math.abs(dx * cosRotation - dz * sinRotation) - halfWidth, 0), Math.max(Math.abs(dx * sinRotation + dz * cosRotation) - halfDepth, 0));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Four x-z perimeter corners, using full dimensions and a yaw in radians. */
|
|
12
|
+
export function rectangleCorners(centerX, centerZ, width, depth, rotation) {
|
|
13
|
+
const cosRotation = Math.cos(rotation),
|
|
14
|
+
sinRotation = Math.sin(rotation),
|
|
15
|
+
o = [];
|
|
16
|
+
for (const [u, v] of [
|
|
17
|
+
[1, 1],
|
|
18
|
+
[1, -1],
|
|
19
|
+
[-1, -1],
|
|
20
|
+
[-1, 1],
|
|
21
|
+
])
|
|
22
|
+
o.push([centerX + (u * width * cosRotation + v * depth * sinRotation) / 2, centerZ + (-u * width * sinRotation + v * depth * cosRotation) / 2]);
|
|
23
|
+
return o;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Strict overlap of two nondegenerate rectangles given four perimeter corners each. Touching edges have no area in common.
|
|
27
|
+
export function rectanglesOverlap(firstCorners, secondCorners) {
|
|
28
|
+
for (const [P1, P2] of [
|
|
29
|
+
[firstCorners, secondCorners],
|
|
30
|
+
[secondCorners, firstCorners],
|
|
31
|
+
])
|
|
32
|
+
for (let i = 0; i < 2; i++) {
|
|
33
|
+
const ax = P1[i + 1][0] - P1[i][0],
|
|
34
|
+
az = P1[i + 1][1] - P1[i][1];
|
|
35
|
+
let a0 = Infinity,
|
|
36
|
+
a1 = -Infinity,
|
|
37
|
+
b0 = Infinity,
|
|
38
|
+
b1 = -Infinity;
|
|
39
|
+
for (const p of P1) {
|
|
40
|
+
const t = p[0] * ax + p[1] * az;
|
|
41
|
+
a0 = Math.min(a0, t);
|
|
42
|
+
a1 = Math.max(a1, t);
|
|
43
|
+
}
|
|
44
|
+
for (const p of P2) {
|
|
45
|
+
const t = p[0] * ax + p[1] * az;
|
|
46
|
+
b0 = Math.min(b0, t);
|
|
47
|
+
b1 = Math.max(b1, t);
|
|
48
|
+
}
|
|
49
|
+
if (a1 <= b0 || b1 <= a0) return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
package/math/rock-texture.js
CHANGED
|
@@ -18,12 +18,12 @@ export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
|
|
|
18
18
|
const { tfbm } = createNoise(seed);
|
|
19
19
|
const jointed = fractures === 'joints';
|
|
20
20
|
if (!jointed && fractures !== 'cells') throw new TypeError('Unknown rock fracture pattern: ' + fractures);
|
|
21
|
-
return (u, v,
|
|
22
|
-
const
|
|
21
|
+
return (u, v, output) => {
|
|
22
|
+
const grainNoise = tfbm(u, v, 6, 6, 81),
|
|
23
23
|
w = tfbm(u, v, 4, 3, 82) - 0.5;
|
|
24
|
-
|
|
24
|
+
output[0] = clamp(255 * (grainNoise * 1.25 - 0.12), 0, 255);
|
|
25
25
|
if (jointed) {
|
|
26
|
-
const warp = w * 0.22 + (
|
|
26
|
+
const warp = w * 0.22 + (grainNoise - 0.5) * 0.08;
|
|
27
27
|
// Independently spaced vertical joints stop in competent beds; the oblique set only opens in patches.
|
|
28
28
|
const trace = u * 7 + 0.17 * Math.sin(v * Math.PI * 2) + warp;
|
|
29
29
|
const joint = (Math.round(trace) % 7 + 7) % 7;
|
|
@@ -38,7 +38,7 @@ export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
|
|
|
38
38
|
const seam = fractureLine(bedCoordinate, 6, hash2, 85, 0.065);
|
|
39
39
|
const open = tfbm(u, v, 4, 2, 86);
|
|
40
40
|
const crossOpen = tfbm(u, v, 3, 2, 87);
|
|
41
|
-
|
|
41
|
+
output[1] = 255 * Math.max(
|
|
42
42
|
steep * opening * sstep(0.37, 0.61, open),
|
|
43
43
|
cross * sstep(0.53, 0.76, crossOpen) * 0.64,
|
|
44
44
|
seam * sstep(0.36, 0.72, crossOpen) * 0.60,
|
|
@@ -46,11 +46,11 @@ export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
|
|
|
46
46
|
} else {
|
|
47
47
|
const e1 = cellEdge(hash2, u + w * 0.05, v - w * 0.03, 5, 83);
|
|
48
48
|
const e2 = cellEdge(hash2, u - w * 0.03, v + w * 0.05, 13, 84);
|
|
49
|
-
|
|
49
|
+
output[1] = 255 * Math.max(
|
|
50
50
|
sstep(0.014, 0.003, e1) * sstep(0.55, 0.66, tfbm(u, v, 5, 2, 86)),
|
|
51
51
|
0.5 * sstep(0.009, 0.002, e2) * sstep(0.66, 0.74, tfbm(u, v, 7, 2, 87)),
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
|
-
|
|
54
|
+
output[2] = 255 * tfbm(u, v, 4, 5, 85);
|
|
55
55
|
};
|
|
56
56
|
}
|
package/math/scalar.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const clamp = (
|
|
2
|
-
const lerp = (
|
|
3
|
-
|
|
4
|
-
const sstep = (
|
|
5
|
-
const t = clamp((x -
|
|
1
|
+
const clamp = (value, minimum, maximum) => (value < minimum ? minimum : value > maximum ? maximum : value);
|
|
2
|
+
const lerp = (start, end, fraction) => start + (end - start) * fraction;
|
|
3
|
+
/** Ease from 0 at start to 1 at end. Reverse the endpoints for a falling transition. */
|
|
4
|
+
const sstep = (start, end, x) => {
|
|
5
|
+
const t = clamp((x - start) / (end - start), 0, 1);
|
|
6
6
|
return t * t * (3 - 2 * t);
|
|
7
7
|
};
|
|
8
8
|
const TAU = Math.PI * 2;
|
package/math/surface-textures.js
CHANGED
|
@@ -10,28 +10,28 @@ import { clamp, sstep } from './scalar.js';
|
|
|
10
10
|
export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
11
11
|
// Coursed ashlar: courses of uneven height, blocks of uneven length, thin joints, and each block its own tone.
|
|
12
12
|
function ashlar({ seed = 611, courses = 10 } = {}) {
|
|
13
|
-
const
|
|
13
|
+
const random = mulberry32(seed),
|
|
14
14
|
N = courses,
|
|
15
15
|
rows = [];
|
|
16
16
|
let acc = 0;
|
|
17
17
|
for (let i = 0; i < N; i++) {
|
|
18
|
-
const h = 0.75 +
|
|
18
|
+
const h = 0.75 + random() * 0.6;
|
|
19
19
|
rows.push({ y0: acc, h });
|
|
20
20
|
acc += h;
|
|
21
21
|
}
|
|
22
22
|
for (const r of rows) {
|
|
23
23
|
r.y0 /= acc;
|
|
24
24
|
r.h /= acc;
|
|
25
|
-
const n = 3 + ((
|
|
25
|
+
const n = 3 + ((random() * 3) | 0),
|
|
26
26
|
cuts = [];
|
|
27
|
-
let
|
|
27
|
+
let cutPosition = random();
|
|
28
28
|
for (let k = 0; k < n; k++) {
|
|
29
|
-
cuts.push(
|
|
30
|
-
|
|
29
|
+
cuts.push(cutPosition % 1);
|
|
30
|
+
cutPosition += (0.7 + random() * 0.6) / n;
|
|
31
31
|
}
|
|
32
32
|
r.cuts = cuts.sort((p, q) => p - q);
|
|
33
|
-
r.tone = cuts.map(() => 0.86 +
|
|
34
|
-
r.warm = cuts.map(() =>
|
|
33
|
+
r.tone = cuts.map(() => 0.86 + random() * 0.26);
|
|
34
|
+
r.warm = cuts.map(() => random());
|
|
35
35
|
}
|
|
36
36
|
return (u, v, o) => {
|
|
37
37
|
let r = rows[N - 1];
|
|
@@ -40,29 +40,29 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
40
40
|
r = rows[i];
|
|
41
41
|
break;
|
|
42
42
|
}
|
|
43
|
-
const
|
|
44
|
-
m =
|
|
43
|
+
const cuts = r.cuts,
|
|
44
|
+
m = cuts.length;
|
|
45
45
|
let k = m - 1;
|
|
46
46
|
for (let i = 0; i < m; i++)
|
|
47
|
-
if (u <
|
|
47
|
+
if (u < cuts[i]) {
|
|
48
48
|
k = (i + m - 1) % m;
|
|
49
49
|
break;
|
|
50
50
|
}
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
w = (
|
|
54
|
-
bu = ((u -
|
|
51
|
+
const startCut = cuts[k],
|
|
52
|
+
endCut = cuts[(k + 1) % m],
|
|
53
|
+
w = (endCut - startCut + 1) % 1 || 1,
|
|
54
|
+
bu = ((u - startCut + 1) % 1) / w,
|
|
55
55
|
bv = (v - r.y0) / r.h;
|
|
56
56
|
const dj = Math.min(Math.min(bu, 1 - bu) * w, Math.min(bv, 1 - bv) * r.h); // distance to the nearest joint, in tile units
|
|
57
57
|
const joint = sstep(0.0, 0.0055, dj),
|
|
58
58
|
edge = sstep(0.0, 0.028, dj),
|
|
59
59
|
n = tfbm(u, v, 8, 4, 62),
|
|
60
60
|
pit = sstep(0.62, 0.8, tfbm(u, v, 32, 2, 65));
|
|
61
|
-
const
|
|
61
|
+
const brightness = (158 + n * 62) * r.tone[k] * (0.72 + 0.28 * joint) * (0.93 + 0.07 * edge) * (1 - 0.12 * pit),
|
|
62
62
|
wm = r.warm[k];
|
|
63
|
-
o[0] =
|
|
64
|
-
o[1] =
|
|
65
|
-
o[2] =
|
|
63
|
+
o[0] = brightness * (1 + 0.03 * wm);
|
|
64
|
+
o[1] = brightness * 0.985;
|
|
65
|
+
o[2] = brightness * (0.95 - 0.04 * wm);
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
|
|
@@ -88,11 +88,11 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
88
88
|
const tone = 0.8 + 0.35 * id,
|
|
89
89
|
stone = (150 + 60 * grain) * tone * (0.72 + 0.28 * Math.sqrt(rim)) * (1 - 0.12 * pit),
|
|
90
90
|
mortar = 84 + 26 * tfbm(u, v, 14, 3, 100) + 14 * tfbm(u, v, 60, 2, 103), // lying back, flat and sandy
|
|
91
|
-
|
|
91
|
+
brightness = mortar + (stone - mortar) * inStone,
|
|
92
92
|
warm = (hash2(big[0] + 7, big[1], 101) - 0.5) * 0.08 * inStone; // ironstone, limestone and grey field stone
|
|
93
|
-
o[0] = clamp(
|
|
94
|
-
o[1] = clamp(
|
|
95
|
-
o[2] = clamp(
|
|
93
|
+
o[0] = clamp(brightness * (1 + warm), 0, 255);
|
|
94
|
+
o[1] = clamp(brightness * 0.985, 0, 255);
|
|
95
|
+
o[2] = clamp(brightness * (0.95 - warm), 0, 255);
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
98
|
|
|
@@ -100,10 +100,10 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
100
100
|
const plaster = () => (u, v, o) => {
|
|
101
101
|
const n = tfbm(u, v, 5, 5, 63),
|
|
102
102
|
streak = tfbm(u * 3, v * 0.4, 4, 3, 64);
|
|
103
|
-
const
|
|
104
|
-
o[0] =
|
|
105
|
-
o[1] =
|
|
106
|
-
o[2] =
|
|
103
|
+
const brightness = clamp(170 + n * 60 + streak * 24 - 20 * sstep(0.9, 1.0, (v * 6) % 1), 0, 255);
|
|
104
|
+
o[0] = brightness;
|
|
105
|
+
o[1] = brightness * 0.99;
|
|
106
|
+
o[2] = brightness * 0.97;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
// Roof tiles in rows that break joint: each tile its own shade, its lower lip lit, a gap between neighbours.
|
|
@@ -116,20 +116,20 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
116
116
|
const shade = 0.72 + 0.28 * hash2(Math.floor(u * cols + (r % 2) * 0.5) % cols, r, 65);
|
|
117
117
|
const lip = 0.66 + 0.34 * sstep(0.0, 0.5, tv),
|
|
118
118
|
gap = 0.75 + 0.25 * sstep(0, 0.12, tu) * sstep(1, 0.88, tu);
|
|
119
|
-
const
|
|
120
|
-
o[0] =
|
|
121
|
-
o[1] =
|
|
122
|
-
o[2] =
|
|
119
|
+
const brightness = (150 + tfbm(u, v, 8, 3, 66) * 70) * shade * lip * gap;
|
|
120
|
+
o[0] = brightness;
|
|
121
|
+
o[1] = brightness * 0.98;
|
|
122
|
+
o[2] = brightness * 0.96;
|
|
123
123
|
};
|
|
124
124
|
|
|
125
125
|
// Bare rock: mottled grey with dark cracks.
|
|
126
126
|
const rock = () => (u, v, o) => {
|
|
127
127
|
const n = tfbm(u, v, 4, 6, 67),
|
|
128
128
|
crack = Math.abs(tfbm(u, v, 6, 4, 68) - 0.5) * 2;
|
|
129
|
-
const
|
|
130
|
-
o[0] =
|
|
131
|
-
o[1] =
|
|
132
|
-
o[2] =
|
|
129
|
+
const brightness = clamp((130 + n * 110) * (0.55 + 0.45 * sstep(0.0, 0.12, crack)), 0, 255);
|
|
130
|
+
o[0] = brightness;
|
|
131
|
+
o[1] = brightness;
|
|
132
|
+
o[2] = brightness * 0.98;
|
|
133
133
|
};
|
|
134
134
|
|
|
135
135
|
// Planks side by side: grain along each, a dark edge between them, each plank its own tone.
|
|
@@ -139,18 +139,18 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
139
139
|
const plank = Math.floor(u * count),
|
|
140
140
|
g = tfbm(u * 0.6 + plank * 0.13, v, 4, 5, 69),
|
|
141
141
|
edge = sstep(0, 0.06, (u * count) % 1);
|
|
142
|
-
const
|
|
143
|
-
o[0] =
|
|
144
|
-
o[1] =
|
|
145
|
-
o[2] =
|
|
142
|
+
const brightness = (130 + g * 90) * (0.82 + 0.18 * hash2(plank, 0, 70)) * (0.6 + 0.4 * edge);
|
|
143
|
+
o[0] = brightness * 1.05;
|
|
144
|
+
o[1] = brightness * 0.95;
|
|
145
|
+
o[2] = brightness * 0.8;
|
|
146
146
|
};
|
|
147
147
|
|
|
148
148
|
// Leaf clusters, as one grey value: bright clumps, dark gaps.
|
|
149
149
|
const foliage = () => (u, v, o) => {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const n = clamp(sstep(0.34, 0.62,
|
|
150
|
+
const coarseNoise = tfbm(u, v, 10, 3, 71),
|
|
151
|
+
mediumNoise = tnoise(u * 34, v * 34, 34, 72),
|
|
152
|
+
fineNoise = tnoise(u * 90, v * 90, 90, 73);
|
|
153
|
+
const n = clamp(sstep(0.34, 0.62, coarseNoise) * 0.62 + mediumNoise * 0.3 + fineNoise * 0.16, 0, 1) * 255;
|
|
154
154
|
o[0] = o[1] = o[2] = n;
|
|
155
155
|
};
|
|
156
156
|
|
|
@@ -161,10 +161,10 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
161
161
|
(u, v, o) => {
|
|
162
162
|
const n = tfbm(u, v, 6, 6, 3),
|
|
163
163
|
g = tnoise(u * 160, v * 160, 160, 9);
|
|
164
|
-
const
|
|
165
|
-
o[0] =
|
|
166
|
-
o[1] =
|
|
167
|
-
o[2] =
|
|
164
|
+
const brightness = clamp(168 + n * 72 + g * 30, 0, 255);
|
|
165
|
+
o[0] = brightness;
|
|
166
|
+
o[1] = brightness * 0.99;
|
|
167
|
+
o[2] = brightness * 0.96;
|
|
168
168
|
if (alpha) o[3] = 255 * alpha(u, v);
|
|
169
169
|
};
|
|
170
170
|
|
|
@@ -178,9 +178,9 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
178
178
|
|
|
179
179
|
// Sun-dried brick in mud mortar: courses that break joint, each brick its own tone, flecked with straw and worn at its edges.
|
|
180
180
|
function brick({ seed = 733, courses: COURSES = 13, perCourse: BRICKS = 4 } = {}) {
|
|
181
|
-
const
|
|
182
|
-
tone = Array.from({ length: COURSES * BRICKS * 2 }, () => 0.84 +
|
|
183
|
-
shift = Array.from({ length: COURSES }, () =>
|
|
181
|
+
const random = mulberry32(seed),
|
|
182
|
+
tone = Array.from({ length: COURSES * BRICKS * 2 }, () => 0.84 + random() * 0.3),
|
|
183
|
+
shift = Array.from({ length: COURSES }, () => random() * 0.18);
|
|
184
184
|
return (u, v, o) => {
|
|
185
185
|
const row = Math.min(COURSES - 1, Math.floor(v * COURSES)),
|
|
186
186
|
bv = v * COURSES - row,
|
|
@@ -193,10 +193,10 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
|
|
|
193
193
|
joint = sstep(0.06, 0.2, edge),
|
|
194
194
|
grain = tfbm(u, v, 24, 4, 742),
|
|
195
195
|
straw = sstep(0.8, 0.86, tfbm(u * 3, v * 0.6, 48, 2, 743));
|
|
196
|
-
const
|
|
197
|
-
o[0] =
|
|
198
|
-
o[1] =
|
|
199
|
-
o[2] =
|
|
196
|
+
const brightness = clamp((128 + grain * 70) * (0.74 + 0.26 * joint) * (joint > 0.5 ? tone[k] : 0.92) * (1 + straw * 0.12), 0, 255);
|
|
197
|
+
o[0] = brightness * 1.02;
|
|
198
|
+
o[1] = brightness * 0.97;
|
|
199
|
+
o[2] = brightness * 0.9;
|
|
200
200
|
};
|
|
201
201
|
}
|
|
202
202
|
|