roamwright 0.61.24 → 0.61.26
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/README.md +2 -1
- package/examples/title-backdrop.html +52 -0
- package/examples/title-backdrop.js +160 -0
- package/geometry/crater-profile.js +27 -0
- package/geometry/ground-drape.js +220 -0
- package/geometry/plant-tuft.js +68 -1
- package/geometry/room-halls.js +244 -0
- package/geometry/ruins-crater.js +2 -5
- package/geometry/ruins-rubble.js +28 -0
- package/geometry/scatter-instances.js +103 -0
- package/index.js +49 -5
- package/package.json +1 -1
- package/rendering/ground-drape.js +267 -0
- package/rendering/paved-ground.js +7 -1
- package/rendering/scatter-instances.js +151 -1
- package/rendering/scene-flash.js +105 -0
- package/rendering/title-backdrop-sprites.js +342 -0
- package/rendering/title-backdrop.js +212 -0
- package/runtime/article-page.js +128 -0
- package/runtime/articles.js +177 -0
- package/runtime/hud-room.js +103 -0
- package/runtime/journey.js +60 -0
- package/runtime/lazy-sets.js +69 -0
- package/runtime/site-pages.js +343 -0
- package/tools/ios-sim.sh +5 -1
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ npm resolves the Three.js peer in a new project. Roamwright targets Three.js **0
|
|
|
30
30
|
|
|
31
31
|
## Examples
|
|
32
32
|
|
|
33
|
-
Browse all twenty-
|
|
33
|
+
Browse all twenty-two examples on the [live showcase](https://enestaylan.github.io/roamwright/).
|
|
34
34
|
|
|
35
35
|
| Live example | What the example shows | Source |
|
|
36
36
|
| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
|
|
@@ -55,6 +55,7 @@ Browse all twenty-one examples on the [live showcase](https://enestaylan.github.
|
|
|
55
55
|
| [Pacing and measuring](https://enestaylan.github.io/roamwright/examples/pacing.html?seed=42) | Device-aware presets, ground render order, frame-pacing watch and a live performance box over a coastal valley. | [pacing.js](examples/pacing.js) |
|
|
56
56
|
| [World map and wayfinding](https://enestaylan.github.io/roamwright/examples/world-map.html?seed=42) | Shaded atlas relief, baked terrain sheets, collision-free lettering, interactive pan/zoom/tap and a live compass strip. | [world-map.js](examples/world-map.js) |
|
|
57
57
|
| [Scenes](https://enestaylan.github.io/roamwright/examples/scenes.html?seed=42) | Melee duels with blade trails, flying bolts, energy beams, props, instanced scene markers and automated camera cuts. | [scenes.js](examples/scenes.js) |
|
|
58
|
+
| [Title backdrop](https://enestaylan.github.io/roamwright/examples/title-backdrop.html?seed=42) | Procedural 2D parallax mountain ranges, snow lines, sunlit cumulus banks and freeze-on-load canvas handoff. | [title-backdrop.js](examples/title-backdrop.js) |
|
|
58
59
|
|
|
59
60
|
### Run locally
|
|
60
61
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<meta name="theme-color" content="#152c2c" />
|
|
7
|
+
<title>Title backdrop · Roamwright</title>
|
|
8
|
+
<link rel="stylesheet" href="./showcase.css" />
|
|
9
|
+
<script type="importmap">
|
|
10
|
+
{
|
|
11
|
+
"imports": {
|
|
12
|
+
"three": "../node_modules/three/build/three.module.js",
|
|
13
|
+
"three/addons/": "../node_modules/three/examples/jsm/",
|
|
14
|
+
"roamwright": "../index.js",
|
|
15
|
+
"roamwright/": "../"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
</script>
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
<canvas
|
|
22
|
+
tabindex="0"
|
|
23
|
+
aria-label="Layered 2D parallax title backdrop with procedural ridge-noise mountain ranges, drifting cumulus clouds and sunlit sky"
|
|
24
|
+
></canvas>
|
|
25
|
+
<header>
|
|
26
|
+
<a class="back" href="../index.html"><span aria-hidden="true">←</span> Roamwright</a>
|
|
27
|
+
<div class="meta">
|
|
28
|
+
<a class="source" href="https://github.com/enestaylan/roamwright/blob/main/examples/title-backdrop.js">Source</a>
|
|
29
|
+
<form method="get">
|
|
30
|
+
<label for="seed">Seed</label>
|
|
31
|
+
<input id="seed" name="seed" type="number" step="1" min="-2147483648" max="2147483647" value="42" required />
|
|
32
|
+
<button type="submit" title="Generate another mountain skyline" aria-label="Generate another mountain skyline">→</button>
|
|
33
|
+
</form>
|
|
34
|
+
</div>
|
|
35
|
+
</header>
|
|
36
|
+
<footer>
|
|
37
|
+
<div class="caption">
|
|
38
|
+
<h1>Title backdrop</h1>
|
|
39
|
+
<p>Procedural 2D parallax skyline: multi-octave ridge noise, snow-capped peaks, sunlit cumulus banks and freeze-on-load handoff.</p>
|
|
40
|
+
</div>
|
|
41
|
+
<div class="tools">
|
|
42
|
+
<div class="views" role="group" aria-label="Sky mood">
|
|
43
|
+
<button type="button" data-mood="morning" aria-pressed="true">Alpine morning</button>
|
|
44
|
+
<button type="button" data-mood="dusk" aria-pressed="false">Amber dusk</button>
|
|
45
|
+
<button type="button" data-mood="highland" aria-pressed="false">Misty highlands</button>
|
|
46
|
+
</div>
|
|
47
|
+
<button type="button" id="freeze-btn" aria-pressed="false">Freeze</button>
|
|
48
|
+
</div>
|
|
49
|
+
</footer>
|
|
50
|
+
<script type="module" src="./title-backdrop.js"></script>
|
|
51
|
+
</body>
|
|
52
|
+
</html>
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { createTitleBackdrop } from 'roamwright';
|
|
2
|
+
import { readSeed } from './stage.js';
|
|
3
|
+
|
|
4
|
+
const seed = readSeed(42);
|
|
5
|
+
const canvas = document.querySelector('canvas');
|
|
6
|
+
const freezeBtn = document.getElementById('freeze-btn');
|
|
7
|
+
const moodButtons = [...document.querySelectorAll('[data-mood]')];
|
|
8
|
+
|
|
9
|
+
const MOODS = {
|
|
10
|
+
morning: {
|
|
11
|
+
sky: {
|
|
12
|
+
stops: [
|
|
13
|
+
[0, '#6e97be'],
|
|
14
|
+
[0.4, '#9ebbd4'],
|
|
15
|
+
[0.68, '#e3dac8'],
|
|
16
|
+
[0.82, '#f5e1be'],
|
|
17
|
+
[1, '#edd5ad'],
|
|
18
|
+
],
|
|
19
|
+
sun: {
|
|
20
|
+
x: 0.32,
|
|
21
|
+
y: 0.64,
|
|
22
|
+
radius: 0.52,
|
|
23
|
+
stops: [
|
|
24
|
+
[0, 'rgba(255,247,222,0.95)'],
|
|
25
|
+
[0.1, 'rgba(255,232,190,0.52)'],
|
|
26
|
+
[0.42, 'rgba(255,220,170,0.16)'],
|
|
27
|
+
[1, 'rgba(255,220,170,0)'],
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
ranges: [
|
|
32
|
+
{ rock: '#91a5bc', haze: '#dfe4e5', snowLine: 0.34, height: 0.44, top: 0.46 },
|
|
33
|
+
{ rock: '#6f849c', haze: '#d7dede', snowLine: 0.16, height: 0.34, top: 0.58 },
|
|
34
|
+
{ rock: '#4f6478', haze: '#96a8b4', snowLine: 0, height: 0.28, top: 0.72 },
|
|
35
|
+
],
|
|
36
|
+
cloudCount: 8,
|
|
37
|
+
shade: '#e2dcd3',
|
|
38
|
+
veil: null,
|
|
39
|
+
},
|
|
40
|
+
dusk: {
|
|
41
|
+
sky: {
|
|
42
|
+
stops: [
|
|
43
|
+
[0, '#384b6e'],
|
|
44
|
+
[0.36, '#6d6988'],
|
|
45
|
+
[0.66, '#c78973'],
|
|
46
|
+
[0.82, '#e8ae7d'],
|
|
47
|
+
[1, '#f2c58f'],
|
|
48
|
+
],
|
|
49
|
+
sun: {
|
|
50
|
+
x: 0.68,
|
|
51
|
+
y: 0.66,
|
|
52
|
+
radius: 0.5,
|
|
53
|
+
stops: [
|
|
54
|
+
[0, 'rgba(255,238,196,0.95)'],
|
|
55
|
+
[0.12, 'rgba(248,186,120,0.58)'],
|
|
56
|
+
[0.45, 'rgba(220,132,96,0.18)'],
|
|
57
|
+
[1, 'rgba(220,132,96,0)'],
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
ranges: [
|
|
62
|
+
{
|
|
63
|
+
rock: '#6c5d75',
|
|
64
|
+
haze: '#d9a487',
|
|
65
|
+
snowLine: 0.28,
|
|
66
|
+
height: 0.44,
|
|
67
|
+
top: 0.46,
|
|
68
|
+
litStops: [
|
|
69
|
+
[0, 'rgba(255,204,152,0.36)'],
|
|
70
|
+
[1, 'rgba(255,204,152,0)'],
|
|
71
|
+
],
|
|
72
|
+
shadeStops: [
|
|
73
|
+
[0, 'rgba(38,34,62,0.36)'],
|
|
74
|
+
[1, 'rgba(38,34,62,0)'],
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
rock: '#4b455e',
|
|
79
|
+
haze: '#c48f7b',
|
|
80
|
+
snowLine: 0.1,
|
|
81
|
+
height: 0.34,
|
|
82
|
+
top: 0.58,
|
|
83
|
+
litStops: [
|
|
84
|
+
[0, 'rgba(255,192,138,0.3)'],
|
|
85
|
+
[1, 'rgba(255,192,138,0)'],
|
|
86
|
+
],
|
|
87
|
+
shadeStops: [
|
|
88
|
+
[0, 'rgba(30,28,50,0.4)'],
|
|
89
|
+
[1, 'rgba(30,28,50,0)'],
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
{ rock: '#333447', haze: '#68525b', snowLine: 0, height: 0.28, top: 0.72 },
|
|
93
|
+
],
|
|
94
|
+
cloudCount: 7,
|
|
95
|
+
shade: null,
|
|
96
|
+
veil: null,
|
|
97
|
+
},
|
|
98
|
+
highland: {
|
|
99
|
+
sky: {
|
|
100
|
+
stops: [
|
|
101
|
+
[0, '#728796'],
|
|
102
|
+
[0.42, '#99adb8'],
|
|
103
|
+
[0.72, '#cfd8da'],
|
|
104
|
+
[1, '#dde3e1'],
|
|
105
|
+
],
|
|
106
|
+
sun: {
|
|
107
|
+
x: 0.48,
|
|
108
|
+
y: 0.56,
|
|
109
|
+
radius: 0.48,
|
|
110
|
+
stops: [
|
|
111
|
+
[0, 'rgba(246,249,248,0.85)'],
|
|
112
|
+
[0.18, 'rgba(226,235,236,0.4)'],
|
|
113
|
+
[1, 'rgba(226,235,236,0)'],
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
ranges: [
|
|
118
|
+
{ rock: '#7d8f98', haze: '#d5dddd', snowLine: 0.22, height: 0.46, top: 0.44 },
|
|
119
|
+
{ rock: '#5c7276', haze: '#c6d2d0', snowLine: 0.08, height: 0.36, top: 0.56 },
|
|
120
|
+
{ rock: '#3f5756', haze: '#849894', snowLine: 0, height: 0.3, top: 0.7 },
|
|
121
|
+
],
|
|
122
|
+
cloudCount: 10,
|
|
123
|
+
shade: '#dde2e0',
|
|
124
|
+
veil: null,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
let active = null;
|
|
129
|
+
let currentMood = 'morning';
|
|
130
|
+
let frozen = false;
|
|
131
|
+
|
|
132
|
+
function startMood(name) {
|
|
133
|
+
currentMood = name;
|
|
134
|
+
active?.stop();
|
|
135
|
+
frozen = false;
|
|
136
|
+
freezeBtn.setAttribute('aria-pressed', 'false');
|
|
137
|
+
freezeBtn.textContent = 'Freeze';
|
|
138
|
+
for (const btn of moodButtons) btn.setAttribute('aria-pressed', String(btn.dataset.mood === name));
|
|
139
|
+
active = createTitleBackdrop(canvas, {
|
|
140
|
+
seed,
|
|
141
|
+
...MOODS[name],
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
for (const btn of moodButtons) {
|
|
146
|
+
btn.addEventListener('click', () => startMood(btn.dataset.mood));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
freezeBtn.addEventListener('click', () => {
|
|
150
|
+
if (!frozen) {
|
|
151
|
+
frozen = true;
|
|
152
|
+
freezeBtn.setAttribute('aria-pressed', 'true');
|
|
153
|
+
freezeBtn.textContent = 'Resume';
|
|
154
|
+
active?.freeze();
|
|
155
|
+
} else {
|
|
156
|
+
startMood(currentMood);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
startMood('morning');
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure impact-crater height-profile function: ragged bowl depression with a thrown-subsoil lip.
|
|
3
|
+
* Zero Three.js dependencies so terrain-height workers can import it without pulling Three.js.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createNoise } from '../math/noise.js';
|
|
8
|
+
|
|
9
|
+
let fallbackVnoise = null;
|
|
10
|
+
const defaultVnoise = () => (fallbackVnoise ??= createNoise().vnoise);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Compute the height offset (in metres) of an impact crater's ragged bowl and thrown-earth lip at
|
|
14
|
+
* local offset `(dx, dz)` from the crater centre. Negative inside the bowl (down to `-crater.deep`),
|
|
15
|
+
* rising cleanly along the rim near radius `crater.r * 1.06`.
|
|
16
|
+
*
|
|
17
|
+
* @param {{ x: number, z: number, r: number, deep: number }} crater - Crater centre (`x, z`), radius `r` and depth `deep` in metres.
|
|
18
|
+
* @param {number} dx - Offset east from crater centre in metres.
|
|
19
|
+
* @param {number} dz - Offset south from crater centre in metres.
|
|
20
|
+
* @param {(x: number, z: number, salt?: number) => number} [vnoise] - Seeded value noise in `[0, 1)`.
|
|
21
|
+
* @returns {number} Vertical height displacement in metres.
|
|
22
|
+
*/
|
|
23
|
+
export function craterDip(crater, dx, dz, vnoise = defaultVnoise()) {
|
|
24
|
+
const d = Math.hypot(dx, dz) || 1e-3;
|
|
25
|
+
const t = d / (crater.r * (0.86 + 0.28 * vnoise((dx / d) * 1.6 + crater.x, (dz / d) * 1.6 + crater.z, 381)));
|
|
26
|
+
return (t < 1 ? -crater.deep * (1 - t * t) : 0) + crater.deep * 0.42 * Math.exp(-(((t - 1.06) / 0.3) ** 2));
|
|
27
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ground-drape patch geometry and loose surface debris scatter: draped elliptical discs conforming
|
|
3
|
+
* to any height field with feathered ragged edges, trodden rim banking, per-vertex surface look
|
|
4
|
+
* attributes (`aLook`), scattered cobbles/pebbles and droppings, and faceted pebble geometry.
|
|
5
|
+
* Worker-safe.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as THREE from 'three';
|
|
10
|
+
import { createNoise } from '../math/noise.js';
|
|
11
|
+
import { mulberry32 } from '../math/random.js';
|
|
12
|
+
import { TAU, clamp, sstep } from '../math/scalar.js';
|
|
13
|
+
|
|
14
|
+
let fallbackFbm = null;
|
|
15
|
+
const defaultFbm = () => (fallbackFbm ??= createNoise().fbm);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Surface kind codes written into the `aLook.x` vertex attribute for `rendering/ground-drape.js`.
|
|
19
|
+
*/
|
|
20
|
+
export const GROUND_PATCH_KINDS = Object.freeze({
|
|
21
|
+
trodden: 0,
|
|
22
|
+
cracked: 1,
|
|
23
|
+
stain: 2,
|
|
24
|
+
litter: 3,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Default feathering, mottling, rim-banking, rutting and loose-stone scatter settings for
|
|
29
|
+
* {@link drapeGroundPatch} and {@link scatterPatchBits}.
|
|
30
|
+
*/
|
|
31
|
+
export const GROUND_PATCH_DEFAULTS = Object.freeze({
|
|
32
|
+
/** Fraction of patch radius out to which the patch remains full thickness. */
|
|
33
|
+
fadeFrom: 0.6,
|
|
34
|
+
/** Rim-fade noise modulation amplitude so patch edges break up organically. */
|
|
35
|
+
raggedFade: 0.25,
|
|
36
|
+
/** Vertex-colour mottling amplitude. */
|
|
37
|
+
mottle: 0.2,
|
|
38
|
+
/** Spatial frequency (cycles/m) of vertex-colour mottling noise. */
|
|
39
|
+
mottleGrain: 0.16,
|
|
40
|
+
/** Fractional radius where a trodden patch's kicked-up bank peaks. */
|
|
41
|
+
bankPeak: 0.66,
|
|
42
|
+
/** Fractional radius half-width of the trodden rim bank. */
|
|
43
|
+
bankWidth: 0.42,
|
|
44
|
+
/** Bank height in metres per metre of the patch's lesser radius. */
|
|
45
|
+
bankRise: 0.009,
|
|
46
|
+
/** Maximum rim-bank height in metres. */
|
|
47
|
+
bankMost: 0.22,
|
|
48
|
+
/** Minimum length-to-width ratio above which a trodden patch carries wheel ruts. */
|
|
49
|
+
rutsFrom: 1.8,
|
|
50
|
+
/** Square metres of trodden ground per scattered loose stone. */
|
|
51
|
+
stoneArea: 7,
|
|
52
|
+
/** Square metres of trodden ground per scattered beast dropping cluster. */
|
|
53
|
+
dungArea: 45,
|
|
54
|
+
/** Maximum scattered stones per patch (droppings cap at one quarter of this). */
|
|
55
|
+
bitsMost: 1400,
|
|
56
|
+
/** Stones generated between async `breathe()` yields. */
|
|
57
|
+
stonesABreath: 600,
|
|
58
|
+
/** Default sRGB stone colours for scattered pebbles and cobbles. */
|
|
59
|
+
stoneHues: Object.freeze([0x9a8c76, 0x857a68, 0xb0a28a, 0x6f6557, 0xa89478]),
|
|
60
|
+
/** Default sRGB colours for scattered animal droppings. */
|
|
61
|
+
dungHues: Object.freeze([0x4a3a26, 0x3e3424, 0x55462c, 0x4b4430]),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Create an empty ground-patch vertex/index/bit accumulator consumed by {@link drapeGroundPatch} and
|
|
66
|
+
* meshed by `rendering/ground-drape.js`.
|
|
67
|
+
*
|
|
68
|
+
* @returns {{ pos: number[], colours: number[], looks: number[], index: number[], bits: number[] }}
|
|
69
|
+
*/
|
|
70
|
+
export function createGroundPatchData() {
|
|
71
|
+
return {
|
|
72
|
+
pos: [],
|
|
73
|
+
colours: [],
|
|
74
|
+
looks: [],
|
|
75
|
+
index: [],
|
|
76
|
+
bits: [],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Build a faceted, noise-displaced pebble `BufferGeometry` with unit white vertex colours so
|
|
82
|
+
* instanced stones inherit per-instance colours cleanly on vertex-coloured rock materials.
|
|
83
|
+
*
|
|
84
|
+
* @param {(x: number, z: number, octaves?: number, salt?: number) => number} [fbm] - Fractal noise in `[0, 1)`.
|
|
85
|
+
* @returns {THREE.BufferGeometry}
|
|
86
|
+
*/
|
|
87
|
+
export function buildDrapePebbleGeometry(fbm = defaultFbm()) {
|
|
88
|
+
const g = new THREE.IcosahedronGeometry(0.5, 0);
|
|
89
|
+
const a = g.attributes.position.array;
|
|
90
|
+
for (let i = 0; i < a.length; i += 3) {
|
|
91
|
+
const l = Math.hypot(a[i], a[i + 1], a[i + 2]);
|
|
92
|
+
const r = (0.5 * (0.7 + 0.6 * fbm((a[i] / l) * 1.9 + 7, (a[i + 2] / l) * 1.9 + (a[i + 1] / l) * 1.4, 2, 311))) / l;
|
|
93
|
+
a[i] *= r;
|
|
94
|
+
a[i + 1] *= r * 0.7;
|
|
95
|
+
a[i + 2] *= r;
|
|
96
|
+
}
|
|
97
|
+
g.computeVertexNormals();
|
|
98
|
+
g.setAttribute('color', new THREE.Float32BufferAttribute(new Float32Array(a.length).fill(1), 3));
|
|
99
|
+
return g;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Scatter kicked-loose stones and beast droppings onto a trodden ground patch around `(x, z)` in
|
|
104
|
+
* local frame `at`, appending packed 8-number tuples `[wx, wy, wz, size, height, turn, tilt, hex]`
|
|
105
|
+
* to `patches.bits`. Optionally yields via `breathe` every `cfg.stonesABreath` stones.
|
|
106
|
+
*
|
|
107
|
+
* @param {{ bits: number[] }} patches - Target patch data accumulator.
|
|
108
|
+
* @param {{ x0: number, z0: number, Y: (x: number, z: number) => number }} at - Local origin and ground height sampler.
|
|
109
|
+
* @param {() => number} rng - Dedicated repeatable PRNG stream in `[0, 1)`.
|
|
110
|
+
* @param {number} x - Patch centre local X in metres.
|
|
111
|
+
* @param {number} z - Patch centre local Z in metres.
|
|
112
|
+
* @param {number} rx - Patch semi-axis along X in metres.
|
|
113
|
+
* @param {number} rz - Patch semi-axis along Z in metres.
|
|
114
|
+
* @param {(() => Promise<void>) | null} [breathe=null] - Optional async frame-yield callback.
|
|
115
|
+
* @param {typeof GROUND_PATCH_DEFAULTS} [cfg=GROUND_PATCH_DEFAULTS] - Scatter tuning parameters.
|
|
116
|
+
*/
|
|
117
|
+
export async function scatterPatchBits(patches, at, rng, x, z, rx, rz, breathe = null, cfg = GROUND_PATCH_DEFAULTS) {
|
|
118
|
+
const area = Math.PI * rx * rz;
|
|
119
|
+
const stones = Math.min(cfg.bitsMost, Math.round(area / cfg.stoneArea));
|
|
120
|
+
const dung = Math.min(cfg.bitsMost / 4, Math.round(area / cfg.dungArea));
|
|
121
|
+
const put = (px, pz, s, h, sink, col) =>
|
|
122
|
+
patches.bits.push(at.x0 + px, at.Y(px, pz) - sink, at.z0 + pz, s, h, rng() * TAU, (rng() - 0.5) * 0.5, col);
|
|
123
|
+
const inside = () => {
|
|
124
|
+
const a = rng() * TAU;
|
|
125
|
+
const d = Math.sqrt(rng()) * 0.85;
|
|
126
|
+
return [x + Math.cos(a) * rx * d, z + Math.sin(a) * rz * d];
|
|
127
|
+
};
|
|
128
|
+
for (let i = 0; i < stones; i++) {
|
|
129
|
+
const [px, pz] = inside();
|
|
130
|
+
const s = rng() < 0.12 ? 0.22 + rng() * 0.25 : 0.06 + rng() * 0.12;
|
|
131
|
+
put(px, pz, s, s, -s * 0.08, cfg.stoneHues[(rng() * cfg.stoneHues.length) | 0]);
|
|
132
|
+
if (breathe && i % cfg.stonesABreath === cfg.stonesABreath - 1) await breathe();
|
|
133
|
+
}
|
|
134
|
+
for (let i = 0; i < dung; i++) {
|
|
135
|
+
const [px, pz] = inside();
|
|
136
|
+
const col = cfg.dungHues[(rng() * cfg.dungHues.length) | 0];
|
|
137
|
+
if (rng() < 0.6) {
|
|
138
|
+
const balls = 3 + ((rng() * 4) | 0);
|
|
139
|
+
for (let k = 0; k < balls; k++) {
|
|
140
|
+
put(px + (rng() - 0.5) * 0.22, pz + (rng() - 0.5) * 0.22, 0.07 + rng() * 0.03, 0.06, -0.012, col);
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
put(px, pz, 0.22 + rng() * 0.12, 0.05, -0.006, col);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Append one draped elliptical ground patch conforming to `at.Y(x, z)` into `patches`, with a
|
|
150
|
+
* ragged feathered rim, mottled linear vertex colours, per-vertex `aLook` surface parameters, and
|
|
151
|
+
* optional scattered loose stones and droppings for large `'trodden'` patches.
|
|
152
|
+
*
|
|
153
|
+
* @param {{ pos: number[], colours: number[], looks: number[], index: number[], bits: number[] }} patches - Accumulator from {@link createGroundPatchData}.
|
|
154
|
+
* @param {{ x0: number, z0: number, Y: (x: number, z: number) => number }} at - Local origin and ground height sampler.
|
|
155
|
+
* @param {() => number} rng - Repeatable PRNG in `[0, 1)`.
|
|
156
|
+
* @param {number} x - Patch centre local X in metres.
|
|
157
|
+
* @param {number} z - Patch centre local Z in metres.
|
|
158
|
+
* @param {number} rx - Patch semi-axis along X in metres.
|
|
159
|
+
* @param {number} rz - Patch semi-axis along Z in metres.
|
|
160
|
+
* @param {readonly [number, number, number]} col - Base linear RGB colour `[r, g, b]`.
|
|
161
|
+
* @param {object} [options]
|
|
162
|
+
* @param {number} [options.lift=0.04] - Vertical lift above the terrain surface in metres.
|
|
163
|
+
* @param {number} [options.rag=0.12] - Rim radial irregularity share.
|
|
164
|
+
* @param {'trodden' | 'cracked' | 'stain' | 'litter'} [options.kind='trodden'] - Patch surface kind.
|
|
165
|
+
* @param {(() => Promise<void>) | null} [options.breathe=null] - Optional async frame-yield callback.
|
|
166
|
+
* @param {(x: number, z: number, octaves?: number, salt?: number) => number} [options.fbm] - Fractal noise in `[0, 1)`.
|
|
167
|
+
* @param {Partial<typeof GROUND_PATCH_DEFAULTS>} [options.shape] - Overrides to {@link GROUND_PATCH_DEFAULTS}.
|
|
168
|
+
*/
|
|
169
|
+
export async function drapeGroundPatch(patches, at, rng, x, z, rx, rz, col, options = {}) {
|
|
170
|
+
const { lift = 0.04, rag = 0.12, kind = 'trodden', breathe = null, fbm = defaultFbm() } = options;
|
|
171
|
+
const cfg = options.shape ? { ...GROUND_PATCH_DEFAULTS, ...options.shape } : GROUND_PATCH_DEFAULTS;
|
|
172
|
+
const trodden = kind === 'trodden';
|
|
173
|
+
const rings = Math.max(trodden ? 8 : 5, Math.ceil(Math.max(rx, rz) / 3.5));
|
|
174
|
+
const segs = Math.max(30, Math.ceil((Math.max(rx, rz) * TAU) / 5));
|
|
175
|
+
const { pos, colours, looks, index } = patches;
|
|
176
|
+
const first = pos.length / 3;
|
|
177
|
+
const edge = Array.from({ length: segs }, () => 1 + (rng() - 0.5) * 2 * rag);
|
|
178
|
+
const seed = Math.floor(rng() * 1e4);
|
|
179
|
+
// Dedicated stream so internal debris scattering never shifts the caller's layout RNG.
|
|
180
|
+
const own = mulberry32(seed + 7);
|
|
181
|
+
const long = Math.max(rx, rz) / Math.min(rx, rz);
|
|
182
|
+
const ruts = trodden ? clamp((long - cfg.rutsFrom) / 1.5, 0, 1) : 0;
|
|
183
|
+
const bearing = long > cfg.rutsFrom ? (rx >= rz ? 0 : Math.PI / 2) : own() * Math.PI;
|
|
184
|
+
const bank = trodden ? Math.min(cfg.bankMost, cfg.bankRise * Math.min(rx, rz)) : 0;
|
|
185
|
+
const code = GROUND_PATCH_KINDS[kind];
|
|
186
|
+
const vertex = (px, pz, y, out) => {
|
|
187
|
+
const wx = at.x0 + px;
|
|
188
|
+
const wz = at.z0 + pz;
|
|
189
|
+
const grain = 2 * fbm(wx * cfg.mottleGrain, wz * cfg.mottleGrain, 3, seed) - 1;
|
|
190
|
+
const mottle = 1 + cfg.mottle * grain;
|
|
191
|
+
const thick = out >= 1 ? 0 : sstep(1, cfg.fadeFrom, out + cfg.raggedFade * grain);
|
|
192
|
+
const rise = bank * Math.max(0, 1 - ((out - cfg.bankPeak) / cfg.bankWidth) ** 2) ** 2;
|
|
193
|
+
pos.push(wx, y + rise, wz);
|
|
194
|
+
colours.push(col[0] * mottle, col[1] * mottle, col[2] * mottle, thick);
|
|
195
|
+
looks.push(code, Math.min(out, 1), ruts, bearing);
|
|
196
|
+
};
|
|
197
|
+
vertex(x, z, at.Y(x, z) + lift, 0);
|
|
198
|
+
for (let r = 1; r <= rings; r++) {
|
|
199
|
+
for (let k = 0; k < segs; k++) {
|
|
200
|
+
const a = (k / segs) * TAU;
|
|
201
|
+
const d = (r / rings) * (r === rings ? edge[k] : 1);
|
|
202
|
+
const px = x + Math.cos(a) * rx * d;
|
|
203
|
+
const pz = z + Math.sin(a) * rz * d;
|
|
204
|
+
vertex(px, pz, at.Y(px, pz) + lift * (r === rings ? 0.3 : 1), r / rings);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (let k = 0; k < segs; k++) {
|
|
208
|
+
const n = (k + 1) % segs;
|
|
209
|
+
index.push(first, first + 1 + n, first + 1 + k);
|
|
210
|
+
for (let r = 1; r < rings; r++) {
|
|
211
|
+
const a = first + 1 + (r - 1) * segs;
|
|
212
|
+
const b = a + segs;
|
|
213
|
+
index.push(a + k, a + n, b + k, a + n, b + n, b + k);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (trodden && Math.min(rx, rz) > 3) {
|
|
217
|
+
if (breathe) await breathe();
|
|
218
|
+
await scatterPatchBits(patches, at, own, x, z, rx, rz, breathe, cfg);
|
|
219
|
+
}
|
|
220
|
+
}
|
package/geometry/plant-tuft.js
CHANGED
|
@@ -6,8 +6,12 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import * as THREE from 'three';
|
|
9
|
+
import { createNoise } from '../math/noise.js';
|
|
9
10
|
import { mulberry32 } from '../math/random.js';
|
|
10
|
-
import { TAU } from '../math/scalar.js';
|
|
11
|
+
import { TAU, sstep } from '../math/scalar.js';
|
|
12
|
+
|
|
13
|
+
let fallbackVnoise = null;
|
|
14
|
+
const defaultVnoise = () => (fallbackVnoise ??= createNoise().vnoise);
|
|
11
15
|
|
|
12
16
|
/**
|
|
13
17
|
* Default tuft blade, stem and petal slot counts for {@link tuftGeometry}.
|
|
@@ -136,3 +140,66 @@ export function tuftGeometry(options = {}) {
|
|
|
136
140
|
g.setIndex(index);
|
|
137
141
|
return g;
|
|
138
142
|
}
|
|
143
|
+
|
|
144
|
+
/** Default spatial frequency (cycles per metre) for wildflower drift noise in {@link createFlowerDrift}. */
|
|
145
|
+
export const DEFAULT_DRIFT_SCALE = 1 / 30;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Default drift parameters (`seed`, noise threshold `from`, peak tuft `share`) per built-in flower
|
|
149
|
+
* or seed-head species in {@link FLOWER_KINDS}.
|
|
150
|
+
*/
|
|
151
|
+
export const DEFAULT_FLOWER_DRIFTS = Object.freeze({
|
|
152
|
+
poppy: Object.freeze({ seed: 41, from: 0.56, share: 0.7 }),
|
|
153
|
+
cornflower: Object.freeze({ seed: 42, from: 0.64, share: 0.3 }),
|
|
154
|
+
daisy: Object.freeze({ seed: 43, from: 0.6, share: 0.6 }),
|
|
155
|
+
buttercup: Object.freeze({ seed: 44, from: 0.6, share: 0.6 }),
|
|
156
|
+
yarrow: Object.freeze({ seed: 46, from: 0.6, share: 0.7 }),
|
|
157
|
+
tansy: Object.freeze({ seed: 47, from: 0.64, share: 0.6 }),
|
|
158
|
+
seeds: Object.freeze({ seed: 45, from: 0.5, share: 0.5 }),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Create a noise-driven wildflower drift sampler that evaluates per-species drift density at
|
|
163
|
+
* `(x, z)` and packs a chosen species and head count (`kind * 4 + heads`, or `0` for plain grass)
|
|
164
|
+
* into the `aFlower` attribute read by `rendering/plant-tuft.js`. Zero per-call allocation.
|
|
165
|
+
*
|
|
166
|
+
* @param {object} [options]
|
|
167
|
+
* @param {(x: number, z: number, salt?: number) => number} [options.vnoise] - Seeded value noise in `[0, 1)`.
|
|
168
|
+
* @param {Record<string, { seed: number, from: number, share: number }>} [options.drifts=DEFAULT_FLOWER_DRIFTS] - Drift table by species name.
|
|
169
|
+
* @param {number} [options.scale=DEFAULT_DRIFT_SCALE] - Noise coordinate scale in cycles per metre.
|
|
170
|
+
* @param {Record<string, number>} [options.kinds=FLOWER_KINDS] - Mapping from species name to 1-based shader flower index.
|
|
171
|
+
* @param {number} [options.ramp=0.2] - Smoothstep noise width from `from` to full drift `share`.
|
|
172
|
+
* @param {number} [options.maxHeads=3] - Maximum flower heads encoded per tuft (`1..maxHeads`).
|
|
173
|
+
* @returns {{
|
|
174
|
+
* driftOf: (kind: string, x: number, z: number) => number,
|
|
175
|
+
* pickFlower: (candidates: readonly string[] | null | undefined, x: number, z: number, pick: number) => number,
|
|
176
|
+
* }}
|
|
177
|
+
*/
|
|
178
|
+
export function createFlowerDrift(options = {}) {
|
|
179
|
+
const {
|
|
180
|
+
vnoise = defaultVnoise(),
|
|
181
|
+
drifts = DEFAULT_FLOWER_DRIFTS,
|
|
182
|
+
scale = DEFAULT_DRIFT_SCALE,
|
|
183
|
+
kinds = FLOWER_KINDS,
|
|
184
|
+
ramp = 0.2,
|
|
185
|
+
maxHeads = 3,
|
|
186
|
+
} = options;
|
|
187
|
+
|
|
188
|
+
function driftOf(kind, x, z) {
|
|
189
|
+
const d = drifts[kind];
|
|
190
|
+
return d ? d.share * sstep(d.from, d.from + ramp, vnoise(x * scale, z * scale, d.seed)) : 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function pickFlower(candidates, x, z, pick) {
|
|
194
|
+
if (!candidates) return 0;
|
|
195
|
+
let below = 0;
|
|
196
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
197
|
+
const kind = candidates[i];
|
|
198
|
+
below += driftOf(kind, x, z);
|
|
199
|
+
if (pick < below) return kinds[kind] * 4 + 1 + (Math.floor(pick * 1e4) % maxHeads);
|
|
200
|
+
}
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { driftOf, pickFlower };
|
|
205
|
+
}
|