roamwright 0.61.5 → 0.61.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/animals.js +6 -4
- package/examples/crowds.js +4 -3
- package/examples/effects.js +7 -6
- package/examples/figure-motion.js +7 -5
- package/examples/fliers.js +3 -4
- package/examples/fortification.js +4 -3
- package/examples/house-kit.js +9 -3
- package/examples/land-features.js +102 -0
- package/examples/land.html +2 -2
- package/examples/land.js +3 -73
- package/examples/pacing.js +2 -2
- package/examples/rooms.js +13 -8
- package/examples/ships.js +2 -2
- package/examples/small-world-land.js +5 -6
- package/examples/small-world-town.js +9 -3
- package/examples/soundscape.js +6 -6
- package/examples/towns.js +2 -2
- package/geometry/farm-crops.js +142 -0
- package/geometry/farm-props.js +107 -0
- package/geometry/fort-city-walls.js +10 -8
- package/geometry/land-trails.js +110 -0
- package/geometry/scatter-instances.js +103 -0
- package/geometry/stream-springs.js +236 -0
- package/geometry/timber-fence.js +117 -0
- package/index.js +10 -1
- package/package.json +1 -1
- package/rendering/land-trail-surface.js +91 -0
- package/rendering/particles.js +78 -1
- package/rendering/scatter-instances.js +246 -0
- package/rendering/stream-springs.js +66 -0
- package/examples/stage-kits.js +0 -172
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { createNoise } from '../math/noise.js';
|
|
2
|
+
import { lerp, sstep, TAU } from '../math/scalar.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The layout of a stream's springhead and its soaked surround, in metres and turns: which point downstream sets the spring's
|
|
6
|
+
* bearing, rock fineness thresholds, how deeply loose stones sit in the earth, what hill slope behind the pool forms a rock
|
|
7
|
+
* cleft rather than a ring of boulders, and the reach and spacing of the wet-ground sheet.
|
|
8
|
+
*/
|
|
9
|
+
export const SPRING_HEAD = Object.freeze({
|
|
10
|
+
/** Point index along `river.fine` that sets the downstream direction of the spring's local frame. */
|
|
11
|
+
aim: 6,
|
|
12
|
+
/** Rocks smaller than this diameter in metres use the coarsest rock fineness (1). */
|
|
13
|
+
stoneUnder: 0.45,
|
|
14
|
+
/** Rocks larger than this diameter in metres use the finest rock fineness (3); intermediate rocks use 2. */
|
|
15
|
+
block: 2.4,
|
|
16
|
+
/** Vertical centre offset of a loose rock above the terrain, as a share of its height. */
|
|
17
|
+
settled: 0.1,
|
|
18
|
+
/** Hillside slope measurement behind the spring (`back` to `far` metres upstream); steeper than `slope` builds a cleft. */
|
|
19
|
+
steep: Object.freeze({ back: 10, far: 24, slope: 0.3 }),
|
|
20
|
+
/** Boulders ringing a gentle spring's head: arc span in turns from downstream, radial reach and size range in metres. */
|
|
21
|
+
ring: Object.freeze({ from: 0.2, to: 0.8, near: 2.7, far: 4.4, small: 0.9, large: 2.3 }),
|
|
22
|
+
/** Soaked-ground strip: upstream/downstream reach, tail fade, half-width, column step, head/tail width and lift in metres. */
|
|
23
|
+
wet: Object.freeze({ behind: 9, along: 46, fade: 18, half: 11.2, step: 0.8, head: 8, tail: 5.6, lift: 0.06 }),
|
|
24
|
+
/** Value-noise seeds for the wet patch's ragged edge and local soil shade. */
|
|
25
|
+
wetNoise: Object.freeze([907, 911]),
|
|
26
|
+
/** Default spring eye offset along the stream as a share of the stream's full width. */
|
|
27
|
+
eyeRatio: 0.43,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Default linear RGB colours for springhead rocks and soaked earth: dry stone in sun, wet stone at the waterline, the dark
|
|
32
|
+
* interior of a rock cleft's throat, and soaked mud around the pool.
|
|
33
|
+
*/
|
|
34
|
+
export const SPRING_COLOURS = Object.freeze({
|
|
35
|
+
dry: Object.freeze([0.36, 0.34, 0.3]),
|
|
36
|
+
wet: Object.freeze([0.17, 0.17, 0.16]),
|
|
37
|
+
dark: Object.freeze([0.012, 0.012, 0.011]),
|
|
38
|
+
mud: Object.freeze([0.075, 0.064, 0.048]),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Compute a spring's local coordinate frame from its stream polyline and surrounding terrain: `at(along, across)` maps metres
|
|
43
|
+
* downstream and to the left into world `[x, z]`, `eye` is the pool centre distance downstream, `turn` is the downstream yaw in
|
|
44
|
+
* radians, and `steep` indicates whether the slope behind the spring rises steeply enough to form a rock cleft.
|
|
45
|
+
*
|
|
46
|
+
* @param {{ fine: number[][], w: number }} river - Stream polyline (`[x, z]` points in metres) and stream width in metres.
|
|
47
|
+
* @param {object} options
|
|
48
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height sampler in metres.
|
|
49
|
+
* @param {number | ((width: number) => number)} [options.eye] - Pool centre distance downstream in metres, or a function of stream width.
|
|
50
|
+
* @param {Partial<typeof SPRING_HEAD>} [options.shape] - Overrides for {@link SPRING_HEAD}.
|
|
51
|
+
* @returns {{ at: (along: number, across: number) => [number, number], eye: number, turn: number, steep: boolean }}
|
|
52
|
+
*/
|
|
53
|
+
export function springFrame(river, { ground, eye, shape = {} }) {
|
|
54
|
+
const aim = shape.aim ?? SPRING_HEAD.aim,
|
|
55
|
+
eyeRatio = shape.eyeRatio ?? SPRING_HEAD.eyeRatio,
|
|
56
|
+
steep = shape.steep ? { ...SPRING_HEAD.steep, ...shape.steep } : SPRING_HEAD.steep;
|
|
57
|
+
const [x, z] = river.fine[0],
|
|
58
|
+
[ax, az] = river.fine[aim],
|
|
59
|
+
length = Math.hypot(ax - x, az - z),
|
|
60
|
+
dx = (ax - x) / length,
|
|
61
|
+
dz = (az - z) / length,
|
|
62
|
+
eyeOffset = typeof eye === 'function' ? eye(river.w) : (eye ?? river.w * eyeRatio),
|
|
63
|
+
at = (along, across) => [x + dx * along - dz * across, z + dz * along + dx * across],
|
|
64
|
+
behind = (ground(...at(-steep.far, 0)) - ground(...at(-steep.back, 0))) / (steep.far - steep.back);
|
|
65
|
+
return { at, eye: eyeOffset, turn: Math.atan2(dx, dz), steep: behind > steep.slope };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Plan the rock placements around one springhead: either a hillside cleft (two leaning slabs over a dark throat, chockstone,
|
|
70
|
+
* capstone and flanking boulders) or a boulder-ringed hollow, followed by wet stones in the pool and stones strewn along the
|
|
71
|
+
* wet rill. Pure arithmetic, safe for workers and Node tests.
|
|
72
|
+
*
|
|
73
|
+
* @param {ReturnType<typeof springFrame>} frame - The spring's local frame from {@link springFrame}.
|
|
74
|
+
* @param {object} options
|
|
75
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height sampler in metres.
|
|
76
|
+
* @param {() => number} [options.random=Math.random] - Repeatable random stream in `[0, 1)`.
|
|
77
|
+
* @param {Partial<typeof SPRING_HEAD>} [options.shape] - Overrides for {@link SPRING_HEAD}.
|
|
78
|
+
* @param {Partial<typeof SPRING_COLOURS>} [options.colours] - Overrides for {@link SPRING_COLOURS}.
|
|
79
|
+
* @returns {number[]} Flat rows of 13 numbers per rock: `[x, y, z, sx, sy, sz, turn, leanX, leanZ, fineness, r, g, b]`.
|
|
80
|
+
*/
|
|
81
|
+
export function planSpringRocks(frame, { ground, random = Math.random, shape = {}, colours = {} }) {
|
|
82
|
+
const stoneUnder = shape.stoneUnder ?? SPRING_HEAD.stoneUnder,
|
|
83
|
+
block = shape.block ?? SPRING_HEAD.block,
|
|
84
|
+
settled = shape.settled ?? SPRING_HEAD.settled,
|
|
85
|
+
ring = shape.ring ? { ...SPRING_HEAD.ring, ...shape.ring } : SPRING_HEAD.ring,
|
|
86
|
+
dry = colours.dry ?? SPRING_COLOURS.dry,
|
|
87
|
+
wetTone = colours.wet ?? SPRING_COLOURS.wet,
|
|
88
|
+
dark = colours.dark ?? SPRING_COLOURS.dark;
|
|
89
|
+
const fineness = size => (size < stoneUnder ? 1 : size < block ? 2 : 3);
|
|
90
|
+
const rr = (a, b) => a + random() * (b - a);
|
|
91
|
+
const rocks = [];
|
|
92
|
+
// One rock in the spring's frame: default lean and turn draw before shape and shade to preserve seeded stream order.
|
|
93
|
+
const rock = (along, across, size, wet, blockShape = null, lean = [rr(-0.4, 0.4), rr(-0.4, 0.4)], lift = 0, turn = random() * TAU) => {
|
|
94
|
+
const [px, pz] = frame.at(along, across),
|
|
95
|
+
[sx, sy, sz] = blockShape ?? [1.2, rr(0.6, 0.95), rr(0.8, 1.1)],
|
|
96
|
+
shade = rr(0.82, 1.18),
|
|
97
|
+
r = wet < 0 ? dark[0] : lerp(dry[0], wetTone[0], wet) * shade,
|
|
98
|
+
g = wet < 0 ? dark[1] : lerp(dry[1], wetTone[1], wet) * shade,
|
|
99
|
+
b = wet < 0 ? dark[2] : lerp(dry[2], wetTone[2], wet) * shade,
|
|
100
|
+
y = ground(px, pz) + (blockShape ? lift : size * sy * settled);
|
|
101
|
+
rocks.push(px, y, pz, size * sx, size * sy, size * sz, turn, lean[0], lean[1], fineness(size * Math.max(sx, sy, sz)), r, g, b);
|
|
102
|
+
};
|
|
103
|
+
if (frame.steep) {
|
|
104
|
+
// A cleft: two slabs leaning together over a dark throat, a chockstone wedged between them and a capstone across their heads.
|
|
105
|
+
rock(-1.3, 1.75, 1, 0.35, [2.6, 4.9, 3.4], [-0.14, -0.19], 1.5, frame.turn + 0.12);
|
|
106
|
+
rock(-1.2, -1.8, 1, 0.3, [2.4, 4.4, 3.1], [-0.1, 0.21], 1.3, frame.turn - 0.1);
|
|
107
|
+
rock(-2.5, 0, 1, -1, [1.7, 3.6, 1.6], [0, 0], 1.1, frame.turn);
|
|
108
|
+
rock(-1.5, 0.05, 1.25, 0.2, [1.1, 0.9, 1.2], [0.3, 0.5], 2.9);
|
|
109
|
+
rock(-1.9, 0.1, 1, 0.1, [5.0, 1.5, 3.6], [-0.1, 0.07], 4.3, frame.turn + 0.2);
|
|
110
|
+
for (const side of [-1, 1]) {
|
|
111
|
+
rock(-0.4 + rr(-0.5, 0.5), side * rr(3.4, 4.2), rr(1.4, 2.0), 0.25);
|
|
112
|
+
rock(-2.2 + rr(-0.6, 0.6), side * rr(3.6, 4.8), rr(1.0, 1.7), 0.1);
|
|
113
|
+
rock(0.8 + rr(-0.4, 0.4), side * rr(2.6, 3.2), rr(0.7, 1.1), 0.6);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
// A hollow: boulders ringing the pool's head, the largest at its back, and a second rank behind them.
|
|
117
|
+
const stand = 9;
|
|
118
|
+
for (let i = 0; i < stand; i++) {
|
|
119
|
+
const round = lerp(ring.from, ring.to, (i + rr(0.2, 0.8)) / stand),
|
|
120
|
+
back = 1 - Math.abs(round - 0.5) / 0.3,
|
|
121
|
+
reach = rr(ring.near, ring.far) - back * 0.4;
|
|
122
|
+
rock(
|
|
123
|
+
frame.eye + Math.cos(round * TAU) * reach,
|
|
124
|
+
Math.sin(round * TAU) * reach,
|
|
125
|
+
lerp(ring.small, ring.large, back * rr(0.55, 1)),
|
|
126
|
+
0.45,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
for (let i = 0; i < 6; i++) {
|
|
130
|
+
const round = rr(0.27, 0.73),
|
|
131
|
+
reach = rr(5.0, 7.2);
|
|
132
|
+
rock(frame.eye + Math.cos(round * TAU) * reach, Math.sin(round * TAU) * reach, rr(0.55, 1.3), 0.1);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// Stones in the pool and its outlet, wet and half sunk; then stones strewn over the wet ground and along the rill.
|
|
136
|
+
for (let i = 0; i < 9; i++) rock(frame.eye + rr(-1.8, 5), rr(-1.5, 1.5), rr(0.2, 0.55), 1);
|
|
137
|
+
for (let i = 0; i < 54; i++) {
|
|
138
|
+
const along = rr(-5, 30),
|
|
139
|
+
spread = lerp(6.5, 4.2, sstep(0, 20, along)),
|
|
140
|
+
across = rr(-1, 1) * spread;
|
|
141
|
+
rock(along, across, lerp(0.1, 0.5, random() ** 2.2), sstep(spread, 1, Math.abs(across)) * 0.8);
|
|
142
|
+
}
|
|
143
|
+
return rocks;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Build the vertex arrays of a spring's soaked-ground sheet: a strip draped over the terrain round the pool and down both
|
|
148
|
+
* sides of the rill, with RGBA vertex colours whose alpha thins out along a noise-ragged margin. Pure typed-array builder.
|
|
149
|
+
*
|
|
150
|
+
* @param {{ fine: number[][] }} river - Stream polyline (`[x, z]` points in metres).
|
|
151
|
+
* @param {ReturnType<typeof springFrame>} frame - The spring's local frame from {@link springFrame}.
|
|
152
|
+
* @param {object} options
|
|
153
|
+
* @param {(x: number, z: number) => number} options.drawnGround - Drawn triangle height of the terrain mesh in metres.
|
|
154
|
+
* @param {(x: number, z: number, seed: number) => number} [options.noise] - Seeded value noise (`createNoise().vnoise`).
|
|
155
|
+
* @param {Partial<typeof SPRING_HEAD>} [options.shape] - Overrides for {@link SPRING_HEAD}.
|
|
156
|
+
* @param {Partial<typeof SPRING_COLOURS>} [options.colours] - Overrides for {@link SPRING_COLOURS}.
|
|
157
|
+
* @returns {{ position: Float32Array, normal: Float32Array, color: Float32Array, index: number[] }}
|
|
158
|
+
*/
|
|
159
|
+
export function springWetGround(river, frame, { drawnGround, noise = createNoise().vnoise, shape = {}, colours = {} }) {
|
|
160
|
+
const wet = shape.wet ? { ...SPRING_HEAD.wet, ...shape.wet } : SPRING_HEAD.wet,
|
|
161
|
+
wetNoise = shape.wetNoise ?? SPRING_HEAD.wetNoise,
|
|
162
|
+
mud = colours.mud ?? SPRING_COLOURS.mud,
|
|
163
|
+
pts = river.fine,
|
|
164
|
+
rows = [];
|
|
165
|
+
for (let along = -wet.behind; along < 0; along += 1.5) {
|
|
166
|
+
const [x, z] = frame.at(along, 0),
|
|
167
|
+
[lx, lz] = frame.at(along, 1);
|
|
168
|
+
rows.push({ along, x, z, nx: lx - x, nz: lz - z });
|
|
169
|
+
}
|
|
170
|
+
for (let i = 0, along = 0; along < wet.along && i < pts.length - 1; i++) {
|
|
171
|
+
const a = pts[Math.max(0, i - 1)],
|
|
172
|
+
b = pts[i + 1],
|
|
173
|
+
length = Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
174
|
+
rows.push({ along, x: pts[i][0], z: pts[i][1], nx: -(b[1] - a[1]) / length, nz: (b[0] - a[0]) / length });
|
|
175
|
+
along += Math.hypot(pts[i + 1][0] - pts[i][0], pts[i + 1][1] - pts[i][1]);
|
|
176
|
+
}
|
|
177
|
+
const columns = Math.round((wet.half * 2) / wet.step) + 1,
|
|
178
|
+
position = new Float32Array(rows.length * columns * 3),
|
|
179
|
+
normal = new Float32Array(rows.length * columns * 3),
|
|
180
|
+
color = new Float32Array(rows.length * columns * 4),
|
|
181
|
+
index = [];
|
|
182
|
+
rows.forEach((row, i) => {
|
|
183
|
+
const reach =
|
|
184
|
+
row.along < 0 ? wet.head * Math.sqrt(1 - (row.along / wet.behind) ** 2) : lerp(wet.head, wet.tail, sstep(0, 16, row.along)),
|
|
185
|
+
ending = sstep(wet.along, wet.along - wet.fade, row.along);
|
|
186
|
+
for (let j = 0; j < columns; j++) {
|
|
187
|
+
const across = j * wet.step - wet.half,
|
|
188
|
+
x = row.x + row.nx * across,
|
|
189
|
+
z = row.z + row.nz * across,
|
|
190
|
+
v = i * columns + j,
|
|
191
|
+
ragged = reach * (0.7 + 0.6 * noise(x / 2.7, z / 2.7, wetNoise[0])),
|
|
192
|
+
shade = 0.75 + 0.5 * noise(x / 1.3, z / 1.3, wetNoise[1]);
|
|
193
|
+
position.set([x, drawnGround(x, z) + wet.lift, z], v * 3);
|
|
194
|
+
normal.set([0, 1, 0], v * 3);
|
|
195
|
+
color.set([mud[0] * shade, mud[1] * shade, mud[2] * shade, 0.94 * ending * sstep(ragged, ragged * 0.45, Math.abs(across))], v * 4);
|
|
196
|
+
if (i < rows.length - 1 && j < columns - 1) index.push(v, v + 1, v + columns, v + 1, v + columns + 1, v + columns);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
return { position, normal, color, index };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Plan the springheads and soaked ground for every rising stream in `rivers` (entries carrying a `level` profile). Pure
|
|
204
|
+
* arithmetic, suitable for the page or a worker.
|
|
205
|
+
*
|
|
206
|
+
* @param {object} options
|
|
207
|
+
* @param {{ fine: number[][], w: number, level?: Float32Array | number[] }[]} options.rivers - Streams to inspect; rivers without `level` are skipped.
|
|
208
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height sampler in metres, for rocks and slope checks.
|
|
209
|
+
* @param {(x: number, z: number) => number} [options.drawnGround=options.ground] - Drawn mesh triangle height in metres, for the wet-ground sheet.
|
|
210
|
+
* @param {number | ((width: number) => number)} [options.eye] - Pool centre distance downstream in metres, or a function of stream width.
|
|
211
|
+
* @param {(x: number, z: number, seed: number) => number} [options.noise] - Seeded value noise (`createNoise().vnoise`).
|
|
212
|
+
* @param {() => number} [options.random=Math.random] - Repeatable random stream shared across springs in order.
|
|
213
|
+
* @param {Partial<typeof SPRING_HEAD>} [options.shape] - Overrides for {@link SPRING_HEAD}.
|
|
214
|
+
* @param {Partial<typeof SPRING_COLOURS>} [options.colours] - Overrides for {@link SPRING_COLOURS}.
|
|
215
|
+
* @returns {{ frame: ReturnType<typeof springFrame>, wetGround: ReturnType<typeof springWetGround>, rocks: number[] }[]}
|
|
216
|
+
*/
|
|
217
|
+
export function planSprings({
|
|
218
|
+
rivers,
|
|
219
|
+
ground,
|
|
220
|
+
drawnGround = ground,
|
|
221
|
+
eye,
|
|
222
|
+
noise = createNoise().vnoise,
|
|
223
|
+
random = Math.random,
|
|
224
|
+
shape = {},
|
|
225
|
+
colours = {},
|
|
226
|
+
}) {
|
|
227
|
+
const plans = [];
|
|
228
|
+
for (const river of rivers) {
|
|
229
|
+
if (!river.level) continue;
|
|
230
|
+
const frame = springFrame(river, { ground, eye, shape }),
|
|
231
|
+
wetGround = springWetGround(river, frame, { drawnGround, noise, shape, colours }),
|
|
232
|
+
rocks = planSpringRocks(frame, { ground, random, shape, colours });
|
|
233
|
+
plans.push({ frame, wetGround, rocks });
|
|
234
|
+
}
|
|
235
|
+
return plans;
|
|
236
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timber post-and-rail paddock fence builder: constructs circular post-and-rail enclosures grounded on arbitrary
|
|
3
|
+
* terrain heights into a `GeometryBuilder` (worker-safe) or directly as a static `THREE.Mesh`.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { mulberry32 } from '../math/random.js';
|
|
9
|
+
import { TAU } from '../math/scalar.js';
|
|
10
|
+
import { GB } from './builder.js';
|
|
11
|
+
import { C } from './primitives.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Default dimensions, vertical rail offsets (metres), timber sRGB colours, colour jitter and material roughness
|
|
15
|
+
* for post-and-rail timber fences.
|
|
16
|
+
*/
|
|
17
|
+
export const DEFAULT_FENCE_STYLE = Object.freeze({
|
|
18
|
+
postWidth: 0.15,
|
|
19
|
+
postHeight: 1.28,
|
|
20
|
+
postSink: 0.1,
|
|
21
|
+
railWidth: 0.07,
|
|
22
|
+
railHeight: 0.1,
|
|
23
|
+
railHeights: Object.freeze([0.48, 0.92]),
|
|
24
|
+
postColor: 0x6d563f,
|
|
25
|
+
postJitter: 0.1,
|
|
26
|
+
railColor: 0x7e654b,
|
|
27
|
+
railJitter: 0.08,
|
|
28
|
+
roughness: 0.92,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Appends a circular timber post-and-rail fence grounded on `groundAt(x, z)` into a `GeometryBuilder`. Worker-safe:
|
|
33
|
+
* creates no Three.js meshes or materials.
|
|
34
|
+
*
|
|
35
|
+
* @param {GB} builder - Geometry builder receiving the posts and rails.
|
|
36
|
+
* @param {object} [options] - Ring placement, dimensions and timber tint options.
|
|
37
|
+
* @param {(x: number, z: number) => number} [options.groundAt] - Terrain elevation sampler `(x, z) => y` in metres (defaults to flat `0`).
|
|
38
|
+
* @param {number} [options.cx=0] - Ring centre X coordinate in metres.
|
|
39
|
+
* @param {number} [options.cz=0] - Ring centre Z coordinate in metres.
|
|
40
|
+
* @param {number} [options.radius=16.5] - Ring radius in metres.
|
|
41
|
+
* @param {number} [options.posts=28] - Number of perimeter posts (and rail spans) around the ring.
|
|
42
|
+
* @param {() => number} [options.random] - Deterministic PRNG in `[0, 1)` for subtle timber tint jitter (defaults to `mulberry32(1)`).
|
|
43
|
+
* @param {number} [options.postHeight=1.28] - Total post height in metres (including buried footing `postSink`).
|
|
44
|
+
* @param {number} [options.postWidth=0.15] - Post square cross-section width in metres.
|
|
45
|
+
* @param {number} [options.postSink=0.1] - Depth each post foot sinks below `groundAt(x, z)` in metres.
|
|
46
|
+
* @param {readonly number[]} [options.railHeights=[0.48, 0.92]] - Rail elevations above the midpoint ground height in metres.
|
|
47
|
+
* @param {number} [options.railWidth=0.07] - Rail thickness in metres.
|
|
48
|
+
* @param {number} [options.railHeight=0.1] - Rail vertical height in metres.
|
|
49
|
+
* @param {number} [options.postColor=0x6d563f] - Base sRGB hex colour for posts.
|
|
50
|
+
* @param {number} [options.postJitter=0.1] - Timber colour variation amplitude for posts.
|
|
51
|
+
* @param {number} [options.railColor=0x7e654b] - Base sRGB hex colour for rails.
|
|
52
|
+
* @param {number} [options.railJitter=0.08] - Timber colour variation amplitude for rails.
|
|
53
|
+
* @returns {GB} The supplied `builder` for chaining.
|
|
54
|
+
*/
|
|
55
|
+
export function addFenceRing(
|
|
56
|
+
builder,
|
|
57
|
+
{
|
|
58
|
+
groundAt = () => 0,
|
|
59
|
+
cx = 0,
|
|
60
|
+
cz = 0,
|
|
61
|
+
radius = 16.5,
|
|
62
|
+
posts = 28,
|
|
63
|
+
random = mulberry32(1),
|
|
64
|
+
postHeight = DEFAULT_FENCE_STYLE.postHeight,
|
|
65
|
+
postWidth = DEFAULT_FENCE_STYLE.postWidth,
|
|
66
|
+
postSink = DEFAULT_FENCE_STYLE.postSink,
|
|
67
|
+
railHeights = DEFAULT_FENCE_STYLE.railHeights,
|
|
68
|
+
railWidth = DEFAULT_FENCE_STYLE.railWidth,
|
|
69
|
+
railHeight = DEFAULT_FENCE_STYLE.railHeight,
|
|
70
|
+
postColor = DEFAULT_FENCE_STYLE.postColor,
|
|
71
|
+
postJitter = DEFAULT_FENCE_STYLE.postJitter,
|
|
72
|
+
railColor = DEFAULT_FENCE_STYLE.railColor,
|
|
73
|
+
railJitter = DEFAULT_FENCE_STYLE.railJitter,
|
|
74
|
+
} = {},
|
|
75
|
+
) {
|
|
76
|
+
if (!Number.isInteger(posts) || posts < 3) throw new RangeError('posts must be an integer >= 3');
|
|
77
|
+
if (!(radius > 0)) throw new RangeError('radius must be positive');
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < posts; i++) {
|
|
80
|
+
const a0 = (i / posts) * TAU;
|
|
81
|
+
const a1 = ((i + 1) / posts) * TAU;
|
|
82
|
+
const x0 = cx + Math.cos(a0) * radius;
|
|
83
|
+
const z0 = cz + Math.sin(a0) * radius;
|
|
84
|
+
const y0 = groundAt(x0, z0);
|
|
85
|
+
const x1 = cx + Math.cos(a1) * radius;
|
|
86
|
+
const z1 = cz + Math.sin(a1) * radius;
|
|
87
|
+
const y1 = groundAt(x1, z1);
|
|
88
|
+
|
|
89
|
+
builder.box(x0, y0 - postSink, z0, postWidth, postHeight, postWidth, C(postColor, postJitter, random));
|
|
90
|
+
|
|
91
|
+
const mx = (x0 + x1) / 2;
|
|
92
|
+
const mz = (z0 + z1) / 2;
|
|
93
|
+
const my = (y0 + y1) / 2;
|
|
94
|
+
const span = Math.hypot(x1 - x0, z1 - z0);
|
|
95
|
+
const yaw = Math.atan2(x1 - x0, z1 - z0);
|
|
96
|
+
for (let r = 0; r < railHeights.length; r++) {
|
|
97
|
+
builder.box(mx, my + railHeights[r], mz, railWidth, railHeight, span, C(railColor, railJitter, random), yaw);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return builder;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Builds a circular timber post-and-rail paddock fence grounded on `groundAt(x, z)` and returns the resulting static
|
|
105
|
+
* `THREE.Mesh`, optionally attaching it to `parent`.
|
|
106
|
+
*
|
|
107
|
+
* @param {object} [options] - All {@link addFenceRing} options plus optional `parent`, `material` and `roughness`.
|
|
108
|
+
* @param {THREE.Object3D | null} [options.parent=null] - Optional scene or group to attach the fence mesh to.
|
|
109
|
+
* @param {THREE.Material | null} [options.material=null] - Optional timber material (defaults to vertex-coloured `MeshStandardMaterial`).
|
|
110
|
+
* @param {number} [options.roughness=0.92] - Material roughness when `material` is omitted.
|
|
111
|
+
* @returns {THREE.Mesh} The built fence mesh.
|
|
112
|
+
*/
|
|
113
|
+
export function buildFenceRing({ parent = null, material = null, roughness = DEFAULT_FENCE_STYLE.roughness, ...ringOptions } = {}) {
|
|
114
|
+
const fence = addFenceRing(new GB(), ringOptions);
|
|
115
|
+
const mat = material ?? new THREE.MeshStandardMaterial({ vertexColors: true, roughness });
|
|
116
|
+
return fence.mesh(mat, parent);
|
|
117
|
+
}
|
package/index.js
CHANGED
|
@@ -52,21 +52,29 @@ export { planTalusStones } from './geometry/strata-talus.js';
|
|
|
52
52
|
export { meshTalusStones } from './rendering/strata-talus.js';
|
|
53
53
|
export { planStreamBanks } from './geometry/stream-banks.js';
|
|
54
54
|
export { meshStreamBanks } from './rendering/stream-banks.js';
|
|
55
|
+
export { SPRING_COLOURS, SPRING_HEAD, planSpringRocks, planSprings, springFrame, springWetGround } from './geometry/stream-springs.js';
|
|
56
|
+
export { meshSprings } from './rendering/stream-springs.js';
|
|
55
57
|
export { planShoreReeds } from './geometry/shore-reeds.js';
|
|
56
58
|
export { roadRibbon } from './geometry/road-ribbon.js';
|
|
57
59
|
export { layRoads } from './rendering/road-ribbons.js';
|
|
58
60
|
export { createRoadTraffic, ROAD_TRAFFIC } from './motion/road-traffic.js';
|
|
59
61
|
export { createFieldSurvey, FIELD_SURVEY, FIELD_USE } from './geometry/field-survey.js';
|
|
60
|
-
export { createLandTrails,
|
|
62
|
+
export { LAND_TRAILS, LAND_TRAIL_MARKS, boundsOverlap, createLandTrails, rasterizeTrailMarks } from './geometry/land-trails.js';
|
|
63
|
+
export { createTrailMarks, trailMarksGLSL } from './rendering/land-trail-surface.js';
|
|
61
64
|
export { createIrrigatedFields, IRRIGATED_FIELDS } from './geometry/irrigated-fields.js';
|
|
65
|
+
export { DEFAULT_CROP_STANDS, FARM_CROPS, createCropPlacer } from './geometry/farm-crops.js';
|
|
62
66
|
export { createIrrigatedFieldSurface, IRRIGATED_FIELD_KIND, IRRIGATED_FIELD_SURFACE } from './rendering/irrigated-field-surface.js';
|
|
63
67
|
export { DRYSTONE_WALL, FIELD_STONE, drystoneStone, planDrystoneWall } from './geometry/drystone-wall.js';
|
|
68
|
+
export { DEFAULT_FENCE_STYLE, addFenceRing, buildFenceRing } from './geometry/timber-fence.js';
|
|
69
|
+
export { planScatterInstances } from './geometry/scatter-instances.js';
|
|
70
|
+
export { scatterInstances, scatterTrees, scatterTussocks } from './rendering/scatter-instances.js';
|
|
64
71
|
export { FARM_BOUNDARIES, planFieldBoundaries } from './geometry/farm-boundaries.js';
|
|
65
72
|
export { meshFarmCells, scatterFarmLots } from './rendering/farm-cells.js';
|
|
66
73
|
export { DEFAULT_LIVESTOCK_CASTS, createLivestockCache } from './rendering/farm-livestock.js';
|
|
67
74
|
export { DEFAULT_FARM_HERDS, FARM_LIFE, herdPlaces, parcelPlaces, planFarmLife, ploughSite } from './geometry/farm-life.js';
|
|
68
75
|
export { STOOK, createStook } from './geometry/prop-stook.js';
|
|
69
76
|
export { PLOUGH, ploughGear } from './geometry/prop-plough.js';
|
|
77
|
+
export { FARM_HAYSTACK, FARM_STONE_PILE, buildHaystacks, buildStonePile } from './geometry/farm-props.js';
|
|
70
78
|
|
|
71
79
|
// ── Settlements, houses, fortifications & interiors ──────────────────────
|
|
72
80
|
export { createHouseKit } from './geometry/house-kit.js';
|
|
@@ -186,6 +194,7 @@ export { createCrowdUpdater, registerCrowd } from './rendering/crowd-update.js';
|
|
|
186
194
|
export { createCrowdSystem } from './rendering/crowd-system.js';
|
|
187
195
|
|
|
188
196
|
// ── Effects, audio, pacing & runtime ─────────────────────────────────────
|
|
197
|
+
export { DEFAULT_PARTICLE_LOOK, createParticleNoiseTexture, createParticleSystem } from './rendering/particles.js';
|
|
189
198
|
export { createFireLightWatch } from './rendering/fire-light-watch.js';
|
|
190
199
|
export { createEffectsFrame, syncEffectsUniforms } from './rendering/effects-frame.js';
|
|
191
200
|
export { createRippleRingMaterial, createWaterRises, fishGeometry } from './rendering/effects-water-rises.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "roamwright",
|
|
3
|
-
"version": "0.61.
|
|
3
|
+
"version": "0.61.7",
|
|
4
4
|
"description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"threejs",
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { LAND_TRAIL_MARKS, boundsOverlap, rasterizeTrailMarks } from '../geometry/land-trails.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build the GLSL fragment chunk that draws dry stream beds (gravel, silt bars, cut banks and close-up cobbles from channel G)
|
|
6
|
+
* and braided herd trails (parallel worn ruts fading cleanly with pixel footprint from channel R).
|
|
7
|
+
*
|
|
8
|
+
* Expects uniforms `uPlain` (RG distance field `sampler2D`) and `uPlainR` (`vec4(x0, z0, 1/width, 1/height)`), world position
|
|
9
|
+
* `vWP`, world normal `vWN`, baked mesh vertex colour `vColor`, detail grain samples `d2`, `d3`, `fleck`, `grit` (0.73..1),
|
|
10
|
+
* view distance `vd`, and pixel footprints `groundX`, `groundY`. Updates `diffuseColor.rgb` and surface relief `gTH`.
|
|
11
|
+
* Contains no screen-space derivatives so both branches can be skipped cleanly off the trails.
|
|
12
|
+
*
|
|
13
|
+
* @param {object} [options]
|
|
14
|
+
* @param {{ trail?: number, wash?: number }} [options.reach] - Encoded distance reach per channel in metres (defaults to `LAND_TRAIL_MARKS.reach`).
|
|
15
|
+
* @returns {string} GLSL fragment snippet.
|
|
16
|
+
*/
|
|
17
|
+
export function trailMarksGLSL({ reach } = {}) {
|
|
18
|
+
const trail = reach?.trail ?? LAND_TRAIL_MARKS.reach.trail,
|
|
19
|
+
wash = reach?.wash ?? LAND_TRAIL_MARKS.reach.wash;
|
|
20
|
+
return `/* glsl */
|
|
21
|
+
vec2 pu = (vWP.xz - uPlainR.xy) * uPlainR.zw;
|
|
22
|
+
if (min(pu.x, pu.y) > 0.0 && max(pu.x, pu.y) < 1.0) {
|
|
23
|
+
vec2 mark = textureLod(uPlain, pu, 0.0).rg;
|
|
24
|
+
float level = smoothstep(0.55, 0.8, vWN.y);
|
|
25
|
+
vec3 baked = max(vColor.rgb, vec3(0.02));
|
|
26
|
+
if (mark.g < 0.995) { // a dry stream bed
|
|
27
|
+
float reach = smoothstep(0.76, 0.94, d3.r), swing = smoothstep(0.74, 0.96, d2.g); // from the grain samples the shader already holds: no further reads
|
|
28
|
+
float from = mark.g * ${wash.toFixed(1)} + (swing - 0.5) * 2.4, width = 2.0 + 3.8 * reach; // metres from the bed's middle, and its half width, which comes and goes
|
|
29
|
+
float bed = (1.0 - smoothstep(width - 0.6, width + 0.25, from)) * level;
|
|
30
|
+
float lip = (1.0 - smoothstep(0.0, 0.7, abs(from - width - 0.15))) * level; // the cut bank: a lip of bare earth and its shade
|
|
31
|
+
float stones = smoothstep(0.8, 0.93, fleck * 0.55 + grit * 0.45);
|
|
32
|
+
float bar = smoothstep(0.35, 0.75, reach * 0.5 + swing * 0.5 + (from / width - 0.5) * 0.5); // silt laid down in bars
|
|
33
|
+
vec3 gravel = mix(vec3(0.33, 0.275, 0.19), vec3(0.7, 0.63, 0.49), stones) * (0.8 + 0.3 * smoothstep(0.74, 0.96, d2.g));
|
|
34
|
+
vec3 silt = vec3(0.62, 0.52, 0.35) * (0.82 + 0.3 * smoothstep(0.74, 0.96, fleck));
|
|
35
|
+
// near the eye the gravel is cobbles: rounded stones a hand or two across, some pale, some dark, thinner on the silt
|
|
36
|
+
float close = 1.0 - smoothstep(18.0, 55.0, vd);
|
|
37
|
+
vec2 q = vWP.xz * 3.1 + (d3.rg - 0.86) * 6.0, cell = floor(q);
|
|
38
|
+
float pick = fract(sin(dot(cell, vec2(127.1, 311.7))) * 43758.5453), pick2 = fract(pick * 57.3), pick3 = fract(pick * 131.7);
|
|
39
|
+
vec2 in_ = fract(q) - 0.5 - (vec2(pick2, pick3) - 0.5) * 0.44; // each stone lies where it fell in its square, and is its own size and shape
|
|
40
|
+
float girth = (0.5 + 0.45 * pick) * 0.27;
|
|
41
|
+
float cobble = smoothstep(girth, girth * 0.6, length(in_ * vec2(0.8 + pick2 * 0.5, 1.2 - pick3 * 0.4))) * step(0.3 + 0.45 * bar, pick3) * close;
|
|
42
|
+
vec3 floor_ = mix(gravel, silt, bar);
|
|
43
|
+
floor_ = mix(floor_ * (1.0 - 0.25 * close), vec3(0.3, 0.27, 0.23) + 0.5 * fract(pick * 7.3) * vec3(0.95, 0.9, 0.8), cobble);
|
|
44
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, floor_ / baked, bed * 0.94);
|
|
45
|
+
diffuseColor.rgb *= 1.0 - 0.4 * lip;
|
|
46
|
+
gTH = mix(gTH, 0.3 + stones * (1.0 - bar) * 0.22 + 0.12 * fleck + cobble * (0.45 - dot(in_, in_) * 2.0), bed);
|
|
47
|
+
}
|
|
48
|
+
if (mark.r < 0.995) { // herd trails: a path down the middle and others worn beside it, a pace apart, where the herd spreads
|
|
49
|
+
float spread = smoothstep(0.76, 0.94, d3.r), wander = smoothstep(0.76, 0.94, d3.g) - 0.5;
|
|
50
|
+
float mid = mark.r * ${trail.toFixed(1)}, from = mid + wander * 1.1 * smoothstep(0.6, 2.2, mid), lane = from / 1.3;
|
|
51
|
+
float lanes = (1.0 - smoothstep(0.8, 2.2 + 3.2 * spread, mid)) * level;
|
|
52
|
+
float seen = 1.0 - smoothstep(0.12, 0.4, (length(groundX) + length(groundY)) / 1.3);
|
|
53
|
+
float rut = max(1.0 - smoothstep(0.5, 0.85, mid), 1.0 - smoothstep(0.13, 0.27, abs(fract(lane + 0.5) - 0.5))) * lanes;
|
|
54
|
+
vec3 dust = vec3(0.27, 0.2, 0.125) * (0.8 + 0.4 * smoothstep(0.74, 0.96, fleck));
|
|
55
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, dust / baked, mix(0.3 * lanes, rut, seen) * 0.8);
|
|
56
|
+
gTH -= rut * seen * 0.2;
|
|
57
|
+
}
|
|
58
|
+
}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Create a Three.js uniform holder (`tex`, `rect`), bounds overlap test (`reaches`), `DataTexture` baker (`bake`), and
|
|
63
|
+
* matching GLSL fragment chunk (`glsl`) for fine herd trails and dry stream beds over a region of terrain.
|
|
64
|
+
*
|
|
65
|
+
* @param {object} options
|
|
66
|
+
* @param {{ x0: number, z0: number, x1: number, z1: number }} options.bounds - World-space XZ extent in metres.
|
|
67
|
+
* @param {Partial<typeof LAND_TRAIL_MARKS>} [options.shape] - Overrides for `LAND_TRAIL_MARKS`.
|
|
68
|
+
* @returns {{
|
|
69
|
+
* uniforms: { tex: { value: THREE.DataTexture | null }, rect: { value: THREE.Vector4 } },
|
|
70
|
+
* reaches: (x0: number, z0: number, x1: number, z1: number) => boolean,
|
|
71
|
+
* bake: (inputs: { paths: { trails: number[][][], washes: number[][][] }, road?: Function, river?: Function, siteDistance?: Function, siteRoom?: number }) => THREE.DataTexture,
|
|
72
|
+
* glsl: string
|
|
73
|
+
* }}
|
|
74
|
+
*/
|
|
75
|
+
export function createTrailMarks({ bounds, shape = {} }) {
|
|
76
|
+
const uniforms = { tex: { value: null }, rect: { value: new THREE.Vector4() } },
|
|
77
|
+
reaches = (x0, z0, x1, z1) => boundsOverlap(bounds, x0, z0, x1, z1),
|
|
78
|
+
glsl = trailMarksGLSL({ reach: shape.reach });
|
|
79
|
+
|
|
80
|
+
function bake({ paths, road, river, siteDistance, siteRoom }) {
|
|
81
|
+
const { data, width, height, rect } = rasterizeTrailMarks({ paths, bounds, road, river, siteDistance, siteRoom, shape });
|
|
82
|
+
const tex = new THREE.DataTexture(data, width, height, THREE.RGFormat);
|
|
83
|
+
tex.minFilter = tex.magFilter = THREE.LinearFilter;
|
|
84
|
+
tex.needsUpdate = true;
|
|
85
|
+
uniforms.tex.value = tex;
|
|
86
|
+
uniforms.rect.value.set(rect[0], rect[1], rect[2], rect[3]);
|
|
87
|
+
return tex;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { uniforms, reaches, bake, glsl };
|
|
91
|
+
}
|
package/rendering/particles.js
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
+
import { createNoise } from '../math/noise.js';
|
|
3
|
+
import { particleNoise } from '../math/particle-noise.js';
|
|
2
4
|
import { ATMOSPHERE_GLSL } from './atmosphere.js';
|
|
3
5
|
import { glslFloat, glslVec3 } from './glsl-literal.js';
|
|
4
6
|
|
|
7
|
+
const DEFAULT_PARTICLE_NOISE_SIZE = 128;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates the seamless 4-channel procedural noise `DataTexture` consumed by `createParticleSystem` and its ember
|
|
11
|
+
* shaders: `R` carries billowing ridge height, `G`/`B` carry its horizontal/vertical slope for relief shading, and `A`
|
|
12
|
+
* carries soft cloud turbulence that frays smoke edges and drives flame flicker.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} [options] - Noise sheet options.
|
|
15
|
+
* @param {{ tfbm: Function, tnoise: Function }} [options.noise] - Seeded noise (`createNoise()` by default).
|
|
16
|
+
* @param {number} [options.size=128] - Square texture width and height in pixels (positive integer).
|
|
17
|
+
* @param {number} [options.ridgeSeed=311] - Seed for the billowing ridge channel.
|
|
18
|
+
* @param {number} [options.cloudSeed=733] - Seed for the alpha turbulence channel.
|
|
19
|
+
* @returns {THREE.DataTexture} Seamless repeating RGBA `DataTexture` with mipmaps enabled.
|
|
20
|
+
*/
|
|
21
|
+
export function createParticleNoiseTexture({
|
|
22
|
+
noise = createNoise(),
|
|
23
|
+
size = DEFAULT_PARTICLE_NOISE_SIZE,
|
|
24
|
+
ridgeSeed = 311,
|
|
25
|
+
cloudSeed = 733,
|
|
26
|
+
} = {}) {
|
|
27
|
+
if (!Number.isInteger(size) || size < 1) throw new RangeError('size must be a positive integer');
|
|
28
|
+
const sheetAt = particleNoise({ size, ridgeSeed, cloudSeed, noise });
|
|
29
|
+
const sheet = new Uint8Array(size * size * 4);
|
|
30
|
+
const texel = [0, 0, 0, 0];
|
|
31
|
+
for (let y = 0; y < size; y++) {
|
|
32
|
+
for (let x = 0; x < size; x++) {
|
|
33
|
+
sheetAt((x + 0.5) / size, (size - 1 - y + 0.5) / size, texel);
|
|
34
|
+
const offset = (y * size + x) * 4;
|
|
35
|
+
sheet[offset] = texel[0];
|
|
36
|
+
sheet[offset + 1] = texel[1];
|
|
37
|
+
sheet[offset + 2] = texel[2];
|
|
38
|
+
sheet[offset + 3] = texel[3];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const texture = new THREE.DataTexture(sheet, size, size);
|
|
42
|
+
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
|
|
43
|
+
texture.magFilter = THREE.LinearFilter;
|
|
44
|
+
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
45
|
+
texture.generateMipmaps = true;
|
|
46
|
+
texture.needsUpdate = true;
|
|
47
|
+
return texture;
|
|
48
|
+
}
|
|
49
|
+
|
|
5
50
|
/**
|
|
6
51
|
* Default visual and motion parameters for `createParticleSystem`:
|
|
7
52
|
* `wind` (`[wx, wz]` horizontal drift in m/s), `hazeNear` (`{ offset, start, end }` distance fade in metres),
|
|
@@ -14,12 +59,38 @@ export const DEFAULT_PARTICLE_LOOK = Object.freeze({
|
|
|
14
59
|
fireColors: { tip: [1.0, 0.5, 0.22], core: [1.0, 0.8, 0.4], underglow: [1.0, 0.35, 0.08] },
|
|
15
60
|
});
|
|
16
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Creates a GPU-animated instanced particle and ember material system (alpha smoke/dust/shadow pool and additive
|
|
64
|
+
* glow/flame/ember pool). When `noiseTexture` is omitted, a seamless noise `DataTexture` is generated on first pool
|
|
65
|
+
* creation via {@link createParticleNoiseTexture}.
|
|
66
|
+
*
|
|
67
|
+
* @param {object} options - Scene, clock, depth, noise and visual look bindings.
|
|
68
|
+
* @param {THREE.Object3D} options.scene - Scene or group receiving the particle pool meshes.
|
|
69
|
+
* @param {{ value: number }} options.pointScale - Uniform holding viewport height-derived sprite scale.
|
|
70
|
+
* @param {{ uDepth: { value: THREE.Texture | null }, uDepthPx: { value: THREE.Vector2 }, uSoft: { value: number } }} options.sceneDepth - Soft-particle depth uniforms.
|
|
71
|
+
* @param {() => number} options.elapsed - Current simulation time in seconds.
|
|
72
|
+
* @param {THREE.Texture | (() => THREE.Texture)} [options.noiseTexture] - 4-channel particle noise texture or lazy factory; defaults to `createParticleNoiseTexture({ noise, size: noiseSize })`.
|
|
73
|
+
* @param {{ tfbm: Function, tnoise: Function }} [options.noise] - Seeded noise used when creating the default noise texture.
|
|
74
|
+
* @param {number} [options.noiseSize=128] - Resolution in pixels of the default noise texture when `noiseTexture` is omitted.
|
|
75
|
+
* @param {(particle: object, out: THREE.Vector2) => boolean} [options.verticalBounds] - Optional enclosure sampler writing `[floorY, ceilingY]` in metres.
|
|
76
|
+
* @param {() => number} [options.random=Math.random] - PRNG for per-particle seed attributes.
|
|
77
|
+
* @param {number} [options.layer=0] - Render layer assigned to particle pool meshes.
|
|
78
|
+
* @param {number} [options.alphaOrder=0] - `renderOrder` for alpha-blended pools.
|
|
79
|
+
* @param {number} [options.additiveOrder=1] - `renderOrder` for additive-blended pools.
|
|
80
|
+
* @param {[number, number]} [options.wind] - Horizontal wind drift `[wx, wz]` in m/s.
|
|
81
|
+
* @param {{ offset?: number, start?: number, end?: number }} [options.hazeNear] - Near-distance haze fade in metres.
|
|
82
|
+
* @param {{ glow?: number[], core?: number[] }} [options.emberColors] - Linear RGB colours for ember shaders.
|
|
83
|
+
* @param {{ tip?: number[], core?: number[], underglow?: number[] }} [options.fireColors] - Linear RGB colours for flame shaders.
|
|
84
|
+
* @returns {{ emberMaterial: Function, fxU: object, makeParticles: Function, pFlush: Function, pSpawn: Function, spawn: object }}
|
|
85
|
+
*/
|
|
17
86
|
export function createParticleSystem({
|
|
18
87
|
scene,
|
|
19
88
|
pointScale,
|
|
20
89
|
sceneDepth,
|
|
21
90
|
elapsed,
|
|
22
91
|
noiseTexture,
|
|
92
|
+
noise,
|
|
93
|
+
noiseSize = DEFAULT_PARTICLE_NOISE_SIZE,
|
|
23
94
|
// Optional sampler at emission. Write absolute floor and ceiling heights (metres) to out.x/y
|
|
24
95
|
// and return true for an enclosed particle. Returning false leaves its flight unrestricted.
|
|
25
96
|
verticalBounds,
|
|
@@ -32,6 +103,12 @@ export function createParticleSystem({
|
|
|
32
103
|
emberColors = DEFAULT_PARTICLE_LOOK.emberColors,
|
|
33
104
|
fireColors = DEFAULT_PARTICLE_LOOK.fireColors,
|
|
34
105
|
}) {
|
|
106
|
+
const resolveNoiseTexture =
|
|
107
|
+
typeof noiseTexture === 'function'
|
|
108
|
+
? noiseTexture
|
|
109
|
+
: noiseTexture
|
|
110
|
+
? () => noiseTexture
|
|
111
|
+
: () => createParticleNoiseTexture({ noise, size: noiseSize });
|
|
35
112
|
const haze = { ...DEFAULT_PARTICLE_LOOK.hazeNear, ...hazeNear };
|
|
36
113
|
const embers = { ...DEFAULT_PARTICLE_LOOK.emberColors, ...emberColors };
|
|
37
114
|
const fire = { ...DEFAULT_PARTICLE_LOOK.fireColors, ...fireColors };
|
|
@@ -102,7 +179,7 @@ export function createParticleSystem({
|
|
|
102
179
|
// flame tongues that stand upright and lick upward (kind 1) and embers (kind 2).
|
|
103
180
|
function makeParticles(capacity, additive) {
|
|
104
181
|
if (!Number.isInteger(capacity) || capacity < 1) throw new RangeError('Particle capacity must be a positive integer');
|
|
105
|
-
if (!fxU.uNoise.value) fxU.uNoise.value =
|
|
182
|
+
if (!fxU.uNoise.value) fxU.uNoise.value = resolveNoiseTexture();
|
|
106
183
|
const particleGeometry = new THREE.InstancedBufferGeometry(),
|
|
107
184
|
quadGeometry = new THREE.PlaneGeometry(1, 1);
|
|
108
185
|
particleGeometry.setIndex(quadGeometry.index);
|