roamwright 0.61.13 → 0.61.15
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 +24 -22
- package/examples/ruins.html +60 -0
- package/examples/ruins.js +326 -0
- package/examples/town-fittings.html +52 -0
- package/examples/town-fittings.js +464 -0
- package/geometry/harbour-kit.js +200 -0
- package/geometry/paved-road.js +443 -0
- package/geometry/plant-garden.js +92 -0
- package/geometry/ruins-hill.js +392 -0
- package/geometry/ruins-mound.js +484 -0
- package/geometry/ruins-rubble.js +161 -0
- package/geometry/storm-wall.js +98 -0
- package/geometry/strata-rocks.js +270 -0
- package/geometry/town-garden.js +343 -0
- package/index.js +37 -1
- package/package.json +1 -1
- package/rendering/lamp-maps.js +87 -0
- package/rendering/paved-ground.js +218 -0
- package/rendering/paved-road.js +353 -0
- package/rendering/plant-garden.js +51 -0
- package/rendering/storm-wall.js +238 -0
- package/rendering/street-lamps.js +101 -1
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paved stone highways: flagstone courses with raised kerbs following a terrain spline, far-distance
|
|
3
|
+
* continuous paving strips, roadside milestones, monumental spanning arches, and roadside cedar avenues
|
|
4
|
+
* (near/far tree geometry, layout and trunk radius). Pure geometry and layout builders; worker-safe.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { dim } from '../math/color.js';
|
|
10
|
+
import { spacedCurve } from '../math/curve.js';
|
|
11
|
+
import { TAU, clamp, lerp } from '../math/scalar.js';
|
|
12
|
+
import { GB } from './builder.js';
|
|
13
|
+
import { C as defaultColor, TRS as defaultTransform, stick } from './primitives.js';
|
|
14
|
+
import { sampleSurface } from './sampled-surface.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Default paving, kerb, milestone, monumental arch and far-strip parameters for paved roads.
|
|
18
|
+
* Lengths are in metres; colours are sRGB hex.
|
|
19
|
+
*/
|
|
20
|
+
export const PAVED_ROAD = Object.freeze({
|
|
21
|
+
/** Length of one paving flag along the road in metres. */
|
|
22
|
+
flag: 2.6,
|
|
23
|
+
/** Width of each raised roadside kerb in metres. */
|
|
24
|
+
kerb: 0.45,
|
|
25
|
+
/** Default lateral distance in metres from outer kerb edge to an avenue tree row. */
|
|
26
|
+
treeOff: 4.2,
|
|
27
|
+
/** Spacing in metres between roadside milestones. */
|
|
28
|
+
mile: 208,
|
|
29
|
+
/** Distance in metres short of the road's end where a spanning arch stands. */
|
|
30
|
+
archBack: 16,
|
|
31
|
+
/** Ashlar spandrel courses in a spanning arch. */
|
|
32
|
+
courses: 7,
|
|
33
|
+
/** Height in metres that flag tops and the far paving strip sit over the terrain. */
|
|
34
|
+
pavingTop: 0.05,
|
|
35
|
+
/** Mean brightness multiplier for the middle wheel-rutted flag course. */
|
|
36
|
+
middleTone: 0.9,
|
|
37
|
+
/** Mean brightness multiplier for the side flag courses. */
|
|
38
|
+
sideTone: 0.99,
|
|
39
|
+
/** Mean brightness multiplier for the raised kerb stones. */
|
|
40
|
+
kerbTone: 1.05,
|
|
41
|
+
/** Base worn limestone colour in sRGB hex. */
|
|
42
|
+
limestone: 0xc8bea4,
|
|
43
|
+
/** Base masonry colour for a spanning road arch in sRGB hex. */
|
|
44
|
+
archTone: 0xc9bd9f,
|
|
45
|
+
/** Sample spacing in metres along the road spline. */
|
|
46
|
+
spacing: 1.3,
|
|
47
|
+
/** Cross-section positions across the far paving strip as shares of half-width kerb to kerb. */
|
|
48
|
+
across: Object.freeze([-1, -0.5, -0.18, 0, 0.18, 0.5, 1]),
|
|
49
|
+
/** Brightness tone multiplier at each cross-section station of the far strip. */
|
|
50
|
+
toneAcross: Object.freeze([1.05, 0.99, 0.99, 0.9, 0.99, 0.99, 1.05]),
|
|
51
|
+
/** Width of each arch pier in metres. */
|
|
52
|
+
archPier: 3.4,
|
|
53
|
+
/** Impost springing height of a spanning arch in metres. */
|
|
54
|
+
archSpring: 6.2,
|
|
55
|
+
/** Number of wedge voussoirs across a spanning arch ring (odd so the middle is the keystone). */
|
|
56
|
+
archVoussoirs: 15,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Default proportions and LOD handover parameters for roadside avenue cedars.
|
|
61
|
+
* Dimensions are in metres; handover duration is in seconds.
|
|
62
|
+
*/
|
|
63
|
+
export const AVENUE_CEDAR = Object.freeze({
|
|
64
|
+
/** Nominal height of an avenue cedar in metres. */
|
|
65
|
+
height: 17,
|
|
66
|
+
/** Bole range (`foot` in metres below ground, `head` share of tree height, `width` radius multiplier). */
|
|
67
|
+
trunk: Object.freeze({ foot: -0.4, head: 0.62, width: 1.25 }),
|
|
68
|
+
/** Depth in metres each avenue cedar is sunk into the ground at its base. */
|
|
69
|
+
sink: 0.3,
|
|
70
|
+
/** Trees grouped into each near-LOD instanced stretch. */
|
|
71
|
+
stretch: 6,
|
|
72
|
+
/** Hysteresis band in metres preventing near/far flicker when standing at the LOD boundary. */
|
|
73
|
+
hold: 3,
|
|
74
|
+
/** Dissolve handover duration in seconds between near and far avenue tree meshes. */
|
|
75
|
+
handover: 0.8,
|
|
76
|
+
/** Camera jump distance in metres treated as a cut that snaps every tree to its target LOD. */
|
|
77
|
+
cut: 150,
|
|
78
|
+
/** Top radius of the unit bole cylinder before `trunk.width` scaling. */
|
|
79
|
+
boleRadiusTop: 0.3,
|
|
80
|
+
/** Bottom radius of the unit bole cylinder before `trunk.width` scaling. */
|
|
81
|
+
boleRadiusBottom: 0.5,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const BOLE = new THREE.CylinderGeometry(AVENUE_CEDAR.boleRadiusTop, AVENUE_CEDAR.boleRadiusBottom, 1, 7, 1, true).translate(0, 0.5, 0);
|
|
85
|
+
const SPRAY = new THREE.IcosahedronGeometry(0.5, 1);
|
|
86
|
+
const FAR_SPRAY = new THREE.IcosahedronGeometry(0.5, 0);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Build an arc-length parameterised road polyline over a terrain height sampler.
|
|
90
|
+
*
|
|
91
|
+
* @param {(x: number, z: number) => number} ground - Terrain height sampler `(x, z) => y` in metres.
|
|
92
|
+
* @param {number[][]} pts - Control points `[[x, z], ...]` in metres.
|
|
93
|
+
* @param {number} [spacing=1.3] - Spline subdivision step in metres.
|
|
94
|
+
* @returns {{ length: number, P: (s: number) => { x: number, z: number, y: number, dx: number, dz: number, yaw: number } }}
|
|
95
|
+
*/
|
|
96
|
+
export function roadLine(ground, pts, spacing = PAVED_ROAD.spacing) {
|
|
97
|
+
const line = spacedCurve(pts, spacing);
|
|
98
|
+
const n = line.length;
|
|
99
|
+
const step = Math.hypot(line[1][0] - line[0][0], line[1][1] - line[0][1]);
|
|
100
|
+
return {
|
|
101
|
+
length: (n - 1) * step,
|
|
102
|
+
P(s) {
|
|
103
|
+
const at = Math.min(n - 1.001, Math.max(0, s / step));
|
|
104
|
+
const i = Math.floor(at);
|
|
105
|
+
const t = at - i;
|
|
106
|
+
const x = line[i][0] + (line[i + 1][0] - line[i][0]) * t;
|
|
107
|
+
const z = line[i][1] + (line[i + 1][1] - line[i][1]) * t;
|
|
108
|
+
const dx = (line[i + 1][0] - line[i][0]) / step;
|
|
109
|
+
const dz = (line[i + 1][1] - line[i][1]) / step;
|
|
110
|
+
return { x, z, y: ground(x, z), dx, dz, yaw: Math.atan2(dx, dz) };
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// One flag or kerb block laid along `line` from `s0` to `s1`, following the road's longitudinal grade.
|
|
116
|
+
function laidBlock(builder, line, s0, s1, off, w, h, over, col) {
|
|
117
|
+
const a = line.P(s0);
|
|
118
|
+
const b = line.P(s1);
|
|
119
|
+
const m = line.P((s0 + s1) / 2);
|
|
120
|
+
const len = s1 - s0;
|
|
121
|
+
builder.box(m.x + m.dz * off, (a.y + b.y) / 2 + over - h, m.z - m.dx * off, w, h, len - 0.035, col, m.yaw, -Math.atan2(b.y - a.y, len));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Append three courses of worn stone flagstones (a wheel-rutted middle course flanked by offset side
|
|
126
|
+
* courses) and raised kerbs along a road line into a geometry builder.
|
|
127
|
+
*
|
|
128
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} builder - Destination geometry builder.
|
|
129
|
+
* @param {() => number} random - Repeatable random stream in `[0, 1)`.
|
|
130
|
+
* @param {ReturnType<typeof roadLine>} line - Sampled road line.
|
|
131
|
+
* @param {number} width - Travelled width between kerbs in metres.
|
|
132
|
+
* @param {object} [options]
|
|
133
|
+
* @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
|
|
134
|
+
* @param {Partial<typeof PAVED_ROAD>} [options.shape] - Overrides to {@link PAVED_ROAD}.
|
|
135
|
+
*/
|
|
136
|
+
export function buildRoadPaving(builder, random, line, width, options = {}) {
|
|
137
|
+
const color = options.color ?? defaultColor;
|
|
138
|
+
const cfg = options.shape ? { ...PAVED_ROAD, ...options.shape } : PAVED_ROAD;
|
|
139
|
+
const stone = () => color(cfg.limestone, 0.05, random);
|
|
140
|
+
const side = (width - 3.9) / 2;
|
|
141
|
+
for (let s = 0; s < line.length - cfg.flag; s += cfg.flag) {
|
|
142
|
+
laidBlock(builder, line, s, s + cfg.flag, 0, 3.86, 0.3, cfg.pavingTop, dim(stone(), cfg.middleTone - 0.06 + random() * 0.12));
|
|
143
|
+
for (const sg of [-1, 1]) {
|
|
144
|
+
const s0 = Math.max(0, s - cfg.flag / 2);
|
|
145
|
+
laidBlock(
|
|
146
|
+
builder,
|
|
147
|
+
line,
|
|
148
|
+
s0,
|
|
149
|
+
s + cfg.flag / 2,
|
|
150
|
+
sg * (1.95 + side / 2),
|
|
151
|
+
side - 0.04,
|
|
152
|
+
0.3,
|
|
153
|
+
0.06,
|
|
154
|
+
dim(stone(), cfg.sideTone - 0.07 + random() * 0.14),
|
|
155
|
+
);
|
|
156
|
+
laidBlock(
|
|
157
|
+
builder,
|
|
158
|
+
line,
|
|
159
|
+
s,
|
|
160
|
+
s + cfg.flag,
|
|
161
|
+
sg * (width / 2 + cfg.kerb / 2),
|
|
162
|
+
cfg.kerb,
|
|
163
|
+
0.5,
|
|
164
|
+
0.26,
|
|
165
|
+
dim(stone(), cfg.kerbTone - 0.05 + random() * 0.1),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Build the distant continuous paving strip flush with flag tops, tinted across its width with each
|
|
173
|
+
* course's mean tone so the road stays crisp from afar without depth fighting.
|
|
174
|
+
*
|
|
175
|
+
* @param {ReturnType<typeof roadLine>} line - Sampled road line.
|
|
176
|
+
* @param {number} width - Travelled width between kerbs in metres.
|
|
177
|
+
* @param {object} [options]
|
|
178
|
+
* @param {typeof GB} [options.Builder] - Geometry builder class.
|
|
179
|
+
* @param {(hex: number) => number[]} [options.color] - Linear RGB colour helper.
|
|
180
|
+
* @param {Partial<typeof PAVED_ROAD>} [options.shape] - Overrides to {@link PAVED_ROAD}.
|
|
181
|
+
* @returns {import('./builder.js').GB} Populated geometry builder for the far strip.
|
|
182
|
+
*/
|
|
183
|
+
export function buildFarRoadPaving(line, width, options = {}) {
|
|
184
|
+
const Builder = options.Builder ?? GB;
|
|
185
|
+
const color = options.color ?? defaultColor;
|
|
186
|
+
const cfg = options.shape ? { ...PAVED_ROAD, ...options.shape } : PAVED_ROAD;
|
|
187
|
+
const gb = new Builder();
|
|
188
|
+
const stone = color(cfg.limestone);
|
|
189
|
+
const half = width / 2 + cfg.kerb;
|
|
190
|
+
const m = cfg.across.length;
|
|
191
|
+
const first = gb.count / 3;
|
|
192
|
+
const corners = [];
|
|
193
|
+
let rows = 0;
|
|
194
|
+
for (let s = 0; s < line.length + cfg.flag / 2; s += cfg.flag, rows++) {
|
|
195
|
+
const p = line.P(Math.min(s, line.length));
|
|
196
|
+
for (let j = 0; j < m; j++) {
|
|
197
|
+
const off = cfg.across[j] * half;
|
|
198
|
+
const k = cfg.toneAcross[j];
|
|
199
|
+
gb.raw(p.x + p.dz * off, p.y + cfg.pavingTop, p.z - p.dx * off, 0, 1, 0, stone[0] * k, stone[1] * k, stone[2] * k);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
for (let i = 0; i < rows - 1; i++)
|
|
203
|
+
for (let j = 0; j < m - 1; j++) {
|
|
204
|
+
const v = i * m + j;
|
|
205
|
+
corners.push(v, v + m, v + 1, v + m, v + m + 1, v + 1);
|
|
206
|
+
}
|
|
207
|
+
gb.triangles(first, corners);
|
|
208
|
+
return gb;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Append capped stone milestones at regular intervals along the side of a paved road.
|
|
213
|
+
*
|
|
214
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} builder - Destination geometry builder.
|
|
215
|
+
* @param {() => number} random - Repeatable random stream in `[0, 1)`.
|
|
216
|
+
* @param {ReturnType<typeof roadLine>} line - Sampled road line.
|
|
217
|
+
* @param {number} width - Travelled width between kerbs in metres.
|
|
218
|
+
* @param {object} [options]
|
|
219
|
+
* @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
|
|
220
|
+
* @param {Partial<typeof PAVED_ROAD>} [options.shape] - Overrides to {@link PAVED_ROAD}.
|
|
221
|
+
*/
|
|
222
|
+
export function buildRoadMilestones(builder, random, line, width, options = {}) {
|
|
223
|
+
const color = options.color ?? defaultColor;
|
|
224
|
+
const cfg = options.shape ? { ...PAVED_ROAD, ...options.shape } : PAVED_ROAD;
|
|
225
|
+
const stone = () => color(cfg.limestone, 0.05, random);
|
|
226
|
+
for (let s = cfg.mile / 2; s < line.length - 30; s += cfg.mile) {
|
|
227
|
+
const p = line.P(s);
|
|
228
|
+
const x = p.x + p.dz * (width / 2 + 1.5);
|
|
229
|
+
const z = p.z - p.dx * (width / 2 + 1.5);
|
|
230
|
+
builder.box(x, p.y - 0.4, z, 0.75, 2.1, 0.5, stone(), p.yaw);
|
|
231
|
+
builder.box(x, p.y + 1.7, z, 0.95, 0.22, 0.7, dim(stone(), 1.1), p.yaw);
|
|
232
|
+
builder.box(x, p.y - 0.3, z, 1.3, 0.5, 1, dim(stone(), 0.85), p.yaw);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Append a monumental masonry arch spanning the head of a paved road: stepped plinths, piers,
|
|
238
|
+
* imposts, coursed ashlar spandrels, wedge voussoirs with a proud keystone, cornice, attic and blank tablets.
|
|
239
|
+
*
|
|
240
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} builder - Destination geometry builder.
|
|
241
|
+
* @param {() => number} random - Repeatable random stream in `[0, 1)`.
|
|
242
|
+
* @param {ReturnType<typeof roadLine>} line - Sampled road line.
|
|
243
|
+
* @param {number} width - Travelled width between kerbs in metres.
|
|
244
|
+
* @param {object} [options]
|
|
245
|
+
* @param {(x: number, z: number, hx: number, hz: number, rot: number) => void} [options.addCollider] - Optional collider callback for the two arch piers.
|
|
246
|
+
* @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
|
|
247
|
+
* @param {Partial<typeof PAVED_ROAD>} [options.shape] - Overrides to {@link PAVED_ROAD}.
|
|
248
|
+
*/
|
|
249
|
+
export function buildRoadArch(builder, random, line, width, options = {}) {
|
|
250
|
+
const color = options.color ?? defaultColor;
|
|
251
|
+
const addCollider = options.addCollider ?? (() => {});
|
|
252
|
+
const cfg = options.shape ? { ...PAVED_ROAD, ...options.shape } : PAVED_ROAD;
|
|
253
|
+
const p = line.P(line.length - cfg.archBack);
|
|
254
|
+
const stone = () => color(cfg.archTone, 0.05, random);
|
|
255
|
+
const span = width + 1.6;
|
|
256
|
+
const pier = cfg.archPier;
|
|
257
|
+
const spring = cfg.archSpring;
|
|
258
|
+
const r = span / 2;
|
|
259
|
+
const at = (across, along) => [p.x + p.dz * across + p.dx * along, p.z - p.dx * across + p.dz * along];
|
|
260
|
+
for (const sg of [-1, 1]) {
|
|
261
|
+
const [x, z] = at(sg * (r + pier / 2), 0);
|
|
262
|
+
builder.box(x, p.y - 1.5, z, pier + 0.9, 2.3, 4.6, dim(stone(), 0.88), p.yaw);
|
|
263
|
+
builder.box(x, p.y + 0.8, z, pier, spring - 0.8, 3.8, stone(), p.yaw);
|
|
264
|
+
builder.box(x, p.y + spring, z, pier + 0.5, 0.5, 4.3, dim(stone(), 1.08), p.yaw);
|
|
265
|
+
builder.box(x, p.y + spring + 0.5, z, pier, r + 1.6, 3.8, dim(stone(), 0.97), p.yaw);
|
|
266
|
+
addCollider(x, z, pier / 2, 2, -p.yaw);
|
|
267
|
+
for (let j = 0; j < cfg.courses; j++) {
|
|
268
|
+
const h = (j * (r + 1.6)) / cfg.courses;
|
|
269
|
+
const inner = Math.sqrt(Math.max(0, (r + 0.9) ** 2 - h * h));
|
|
270
|
+
const [sx, sz] = at((sg * (inner + r)) / 2, 0);
|
|
271
|
+
builder.box(
|
|
272
|
+
sx,
|
|
273
|
+
p.y + spring + 0.5 + h,
|
|
274
|
+
sz,
|
|
275
|
+
r - inner + 0.1,
|
|
276
|
+
(r + 1.6) / cfg.courses + 0.02,
|
|
277
|
+
3.7,
|
|
278
|
+
dim(stone(), 0.93 + 0.03 * (j % 2)),
|
|
279
|
+
p.yaw,
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const n = cfg.archVoussoirs;
|
|
284
|
+
for (let i = 0; i < n; i++) {
|
|
285
|
+
const a = (Math.PI * (i + 0.5)) / n;
|
|
286
|
+
const [x, z] = at(-Math.cos(a) * r, 0);
|
|
287
|
+
const key = i === (n - 1) / 2;
|
|
288
|
+
const tone = dim(stone(), key ? 1.1 : 0.9 + 0.12 * (i % 2));
|
|
289
|
+
builder.box(
|
|
290
|
+
x,
|
|
291
|
+
p.y + spring + 0.5 + Math.sin(a) * r,
|
|
292
|
+
z,
|
|
293
|
+
(Math.PI * (r + 0.55)) / n + 0.03,
|
|
294
|
+
key ? 1.45 : 1.1,
|
|
295
|
+
key ? 4.3 : 3.9,
|
|
296
|
+
tone,
|
|
297
|
+
p.yaw,
|
|
298
|
+
0,
|
|
299
|
+
Math.PI / 2 - a,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
const top = p.y + spring + 0.5 + r + 1.6;
|
|
303
|
+
const [cx, cz] = at(0, 0);
|
|
304
|
+
builder.box(cx, top, cz, span + 2 * pier + 0.8, 0.45, 4.5, dim(stone(), 1.08), p.yaw);
|
|
305
|
+
builder.box(cx, top + 0.45, cz, span + 2 * pier, 2.4, 3.6, stone(), p.yaw);
|
|
306
|
+
builder.box(cx, top + 2.85, cz, span + 2 * pier + 1, 0.5, 4.6, dim(stone(), 1.1), p.yaw);
|
|
307
|
+
for (const sg of [-1, 1]) {
|
|
308
|
+
const [tx, tz] = at(0, sg * 1.82);
|
|
309
|
+
builder.box(tx, top + 0.85, tz, span * 0.62, 1.6, 0.1, dim(stone(), 0.78), p.yaw);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Furrowed lower bole with root buttresses, sampled smooth for close inspection and coarsely for far LOD.
|
|
314
|
+
function cedarBole(builder, far, cfg, color) {
|
|
315
|
+
const greyBrown = color(0x82705e);
|
|
316
|
+
const furrow = (angle, v) => Math.cos(13 * angle + 0.9 * Math.sin(3 * angle + 8 * v) + 0.55 * Math.sin(5 * angle - 12 * v));
|
|
317
|
+
sampleSurface(
|
|
318
|
+
builder,
|
|
319
|
+
far ? 16 : 64,
|
|
320
|
+
far ? 3 : 18,
|
|
321
|
+
(u, v) => {
|
|
322
|
+
const angle = TAU * u;
|
|
323
|
+
const foot = Math.max(0, 1 - 4.5 * v) ** 2;
|
|
324
|
+
const roots = foot * (0.1 + 0.09 * Math.cos(5 * angle + 0.5));
|
|
325
|
+
const radius = cfg.trunk.width * lerp(cfg.boleRadiusBottom, cfg.boleRadiusTop, v) + roots + (far ? 0 : 0.016 * furrow(angle, v));
|
|
326
|
+
return [
|
|
327
|
+
0.1 * v + radius * Math.cos(angle),
|
|
328
|
+
lerp(cfg.trunk.foot, cfg.height * cfg.trunk.head, v),
|
|
329
|
+
0.05 * v + radius * Math.sin(angle),
|
|
330
|
+
];
|
|
331
|
+
},
|
|
332
|
+
(u, v) => dim(greyBrown, 0.86 + (far ? 0 : 0.2 * furrow(TAU * u, v)) + 0.07 * Math.sin(5 * TAU * u + v * 4)),
|
|
333
|
+
{ blend: true },
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Build a tall roadside avenue cedar geometry (near or far LOD from the same RNG stream so every
|
|
339
|
+
* bough aligns during LOD dissolve handover).
|
|
340
|
+
*
|
|
341
|
+
* @param {() => number} random - Repeatable random stream in `[0, 1)`.
|
|
342
|
+
* @param {boolean} [far=false] - When true, emits a coarser bole and one needle spray per bough.
|
|
343
|
+
* @param {object} [options]
|
|
344
|
+
* @param {typeof GB} [options.Builder] - Geometry builder class.
|
|
345
|
+
* @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
|
|
346
|
+
* @param {Function} [options.transform] - Matrix4 transform helper (`TRS`).
|
|
347
|
+
* @param {Partial<typeof AVENUE_CEDAR>} [options.shape] - Overrides to {@link AVENUE_CEDAR}.
|
|
348
|
+
* @returns {import('three').BufferGeometry}
|
|
349
|
+
*/
|
|
350
|
+
export function buildAvenueCedar(random, far = false, options = {}) {
|
|
351
|
+
const Builder = options.Builder ?? GB;
|
|
352
|
+
const color = options.color ?? defaultColor;
|
|
353
|
+
const transform = options.transform ?? defaultTransform;
|
|
354
|
+
const cfg = options.shape ? { ...AVENUE_CEDAR, ...options.shape } : AVENUE_CEDAR;
|
|
355
|
+
const spray = far ? FAR_SPRAY : SPRAY;
|
|
356
|
+
const b = new Builder();
|
|
357
|
+
const bark = color(0x54402f, 0.08, random);
|
|
358
|
+
const H = cfg.height;
|
|
359
|
+
cedarBole(b, far, cfg, color);
|
|
360
|
+
stick(b, [0.1, H * 0.6, 0.05], [0, H - 0.6, 0], 0.72, dim(bark, 0.92), BOLE);
|
|
361
|
+
for (let tier = 0; tier < 8; tier++) {
|
|
362
|
+
const y = 5.4 + tier * 1.45 + random() * 0.3;
|
|
363
|
+
const reach = (5.6 - tier * 0.52) * (0.9 + random() * 0.2);
|
|
364
|
+
const limbs = tier < 6 ? 5 : 4;
|
|
365
|
+
for (let q = 0; q < limbs; q++) {
|
|
366
|
+
const a = (TAU * q) / limbs + tier * 0.9 + random() * 0.5;
|
|
367
|
+
const ca = Math.cos(a);
|
|
368
|
+
const sa = Math.sin(a);
|
|
369
|
+
const len = reach * (0.8 + random() * 0.3);
|
|
370
|
+
const droop = 0.3 + random() * 0.5;
|
|
371
|
+
if (!far) stick(b, [0, y, 0], [ca * len, y + 0.5 - droop, sa * len], 0.22, dim(bark, 0.85), BOLE);
|
|
372
|
+
const sprays = [0, 1, 2].map(k => {
|
|
373
|
+
const t = 0.42 + k * 0.29;
|
|
374
|
+
const w = len * (0.62 - k * 0.12);
|
|
375
|
+
return {
|
|
376
|
+
col: color(k === 2 ? 0x55703f : tier % 2 ? 0x3c5632 : 0x466238, 0.1, random),
|
|
377
|
+
place: [
|
|
378
|
+
ca * len * t,
|
|
379
|
+
y + 0.62 - droop * t + random() * 0.2,
|
|
380
|
+
sa * len * t,
|
|
381
|
+
w * 1.25,
|
|
382
|
+
0.42 + random() * 0.16,
|
|
383
|
+
w,
|
|
384
|
+
-a,
|
|
385
|
+
(random() - 0.5) * 0.14,
|
|
386
|
+
],
|
|
387
|
+
};
|
|
388
|
+
});
|
|
389
|
+
if (!far) for (const { col, place } of sprays) b.add(spray, col, transform(...place));
|
|
390
|
+
else {
|
|
391
|
+
const [p0, , p2] = sprays.map(item => item.place);
|
|
392
|
+
b.add(
|
|
393
|
+
spray,
|
|
394
|
+
sprays[1].col,
|
|
395
|
+
transform((p0[0] + p2[0]) / 2, (p0[1] + p2[1]) / 2, (p0[2] + p2[2]) / 2, len * 0.95, 0.5, len * 0.62, -a, p0[7]),
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
b.add(spray, color(0x4a663a, 0.08, random), transform(0, H - 0.5, 0, 3.2, 1.1, 3));
|
|
401
|
+
return b.build(true);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Compute the trunk radius in metres of an avenue cedar at `up` metres above the ground at its base,
|
|
406
|
+
* for staging props or figures flush against its bark.
|
|
407
|
+
*
|
|
408
|
+
* @param {{ tall: number, girth: number }} tree - Tree scale record produced by avenue placement.
|
|
409
|
+
* @param {number} up - Height above ground in metres.
|
|
410
|
+
* @param {Partial<typeof AVENUE_CEDAR>} [shape] - Overrides to {@link AVENUE_CEDAR}.
|
|
411
|
+
* @returns {number} Trunk radius in metres.
|
|
412
|
+
*/
|
|
413
|
+
export function avenueBoleRadius(tree, up, shape) {
|
|
414
|
+
const cfg = shape ? { ...AVENUE_CEDAR, ...shape } : AVENUE_CEDAR;
|
|
415
|
+
const along = clamp(((up + cfg.sink) / tree.tall - cfg.trunk.foot) / (cfg.height * cfg.trunk.head - cfg.trunk.foot), 0, 1);
|
|
416
|
+
return tree.girth * cfg.trunk.width * lerp(cfg.boleRadiusBottom, cfg.boleRadiusTop, along);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Plan alternating pairs of avenue tree placements along both sides of a paved road line.
|
|
421
|
+
*
|
|
422
|
+
* @param {ReturnType<typeof roadLine>} line - Sampled road line.
|
|
423
|
+
* @param {(x: number, z: number) => number} ground - Terrain height sampler `(x, z) => y` in metres.
|
|
424
|
+
* @param {number} offset - Lateral distance in metres from road centreline to each tree row.
|
|
425
|
+
* @param {number} spacing - Longitudinal spacing in metres between consecutive trees on the same side.
|
|
426
|
+
* @param {object} [options]
|
|
427
|
+
* @param {number} [options.start=34] - Starting distance along the road in metres.
|
|
428
|
+
* @param {number} [options.endMargin=28] - Margin before the end of the road left clear of trees in metres.
|
|
429
|
+
* @returns {Array<{ x: number, z: number, y: number, side: number, s: number }>}
|
|
430
|
+
*/
|
|
431
|
+
export function planRoadAvenue(line, ground, offset, spacing, options = {}) {
|
|
432
|
+
const start = options.start ?? 34;
|
|
433
|
+
const endMargin = options.endMargin ?? PAVED_ROAD.archBack + 12;
|
|
434
|
+
const trees = [];
|
|
435
|
+
for (let s = start; s < line.length - endMargin; s += spacing)
|
|
436
|
+
for (const side of [-1, 1]) {
|
|
437
|
+
const p = line.P(s + (side > 0 ? spacing * 0.5 : 0));
|
|
438
|
+
const x = p.x + p.dz * offset * side;
|
|
439
|
+
const z = p.z - p.dx * offset * side;
|
|
440
|
+
trees.push({ x, z, y: ground(x, z), side: -side, s });
|
|
441
|
+
}
|
|
442
|
+
return trees;
|
|
443
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Procedural multi-lobe garden plant shapes (shrubs, clipped hedges, field crops and blossom clusters) built from
|
|
3
|
+
* displaced foliage blobs with vertex ambient shading and leaf flags. Worker-safe.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { createNoise } from '../math/noise.js';
|
|
9
|
+
import { createHash } from '../math/random.js';
|
|
10
|
+
import { addShape, partsGeometry, shapeParts } from './foliage.js';
|
|
11
|
+
import { createPlantTints } from './plant-tints.js';
|
|
12
|
+
|
|
13
|
+
const BLOSSOMS = 6;
|
|
14
|
+
const DEFAULT_LOBE_DETAIL = 1; // icosahedron subdivided once: 80 triangles per lobe
|
|
15
|
+
const DEFAULT_BASE_COLOR = 0xffffff; // shaded in white so per-instance colours tint each plant
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Default unit-plant lobe tables standing on `y = 0`: each entry is `[x, y, z, width, height, depth]` in local metres.
|
|
19
|
+
*/
|
|
20
|
+
export const GARDEN_PLANT_LOBES = {
|
|
21
|
+
shrub: [
|
|
22
|
+
[0, 0.42, 0, 0.95, 0.82, 0.95],
|
|
23
|
+
[0.24, 0.3, 0.12, 0.62, 0.56, 0.6],
|
|
24
|
+
[-0.2, 0.3, -0.16, 0.64, 0.52, 0.6],
|
|
25
|
+
],
|
|
26
|
+
hedge: [
|
|
27
|
+
// Clipped hedge unit 1 m along z: two lobes across and three along so border segments merge cleanly.
|
|
28
|
+
...[-0.17, 0.17].flatMap(x => [-0.33, 0, 0.33].map(z => [x, 0.33, z, 0.52, 0.74, 0.56])),
|
|
29
|
+
],
|
|
30
|
+
crop: [
|
|
31
|
+
// Lighter two-lobe row-crop tuft for dense fields.
|
|
32
|
+
[0, 0.34, 0, 0.92, 0.7, 0.92],
|
|
33
|
+
[0.16, 0.24, -0.12, 0.6, 0.5, 0.6],
|
|
34
|
+
],
|
|
35
|
+
bloom: [
|
|
36
|
+
// Blossom clusters emerging from the top of a flowering shrub.
|
|
37
|
+
...Array.from({ length: BLOSSOMS }, (_, i) => {
|
|
38
|
+
const a = i * 2.4;
|
|
39
|
+
const r = 0.08 + 0.34 * Math.sqrt((i + 0.5) / BLOSSOMS);
|
|
40
|
+
return [Math.cos(a) * r, 0.86 - r * 0.62, Math.sin(a) * r, 0.3, 0.2, 0.3];
|
|
41
|
+
}),
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a cached geometry factory for multi-lobe garden plants (`shrub`, `hedge`, `crop`, `bloom`, or custom kinds).
|
|
47
|
+
* @param {object} [options] - Plant tint helpers and lobe overrides.
|
|
48
|
+
* @param {(seed: number, detail?: number) => THREE.BufferGeometry} [options.crownBlob] - Displaced crown lump helper (`createPlantTints().crownBlob`).
|
|
49
|
+
* @param {(hex: number) => (x: number, y: number, z: number, out: number[]) => void} [options.crownTint] - Foliage vertex tint helper (`createPlantTints().crownTint`).
|
|
50
|
+
* @param {Record<string, number[][]>} [options.lobes] - Map of plant kind name to `[x, y, z, w, h, d]` lobe lists.
|
|
51
|
+
* @param {number} [options.detail=1] - Icosahedron subdivision detail per lobe.
|
|
52
|
+
* @param {number} [options.color=0xffffff] - Base sRGB hex colour before per-instance tinting.
|
|
53
|
+
* @returns {{
|
|
54
|
+
* plantShape: (kind: string) => THREE.BufferGeometry,
|
|
55
|
+
* geometries: WeakSet<THREE.BufferGeometry>,
|
|
56
|
+
* }} Cached geometry accessor and shared-geometry membership set.
|
|
57
|
+
*/
|
|
58
|
+
export function createGardenPlantShapes(options = {}) {
|
|
59
|
+
let tintsFallback = null;
|
|
60
|
+
const getTints = () => {
|
|
61
|
+
if (!tintsFallback) {
|
|
62
|
+
const { fbm, vnoise } = createNoise(0);
|
|
63
|
+
tintsFallback = createPlantTints({ fbm, vnoise, hash: createHash(0) });
|
|
64
|
+
}
|
|
65
|
+
return tintsFallback;
|
|
66
|
+
};
|
|
67
|
+
const crownBlob = options.crownBlob ?? getTints().crownBlob;
|
|
68
|
+
const crownTint = options.crownTint ?? getTints().crownTint;
|
|
69
|
+
const lobes = options.lobes ?? GARDEN_PLANT_LOBES;
|
|
70
|
+
const detail = options.detail ?? DEFAULT_LOBE_DETAIL;
|
|
71
|
+
const baseColor = options.color ?? DEFAULT_BASE_COLOR;
|
|
72
|
+
|
|
73
|
+
const shapes = {};
|
|
74
|
+
const geometries = new WeakSet();
|
|
75
|
+
|
|
76
|
+
function plantShape(kind) {
|
|
77
|
+
if (shapes[kind]) return shapes[kind];
|
|
78
|
+
const spec = lobes[kind];
|
|
79
|
+
if (!spec) throw new Error(`Unknown garden plant shape: ${kind}`);
|
|
80
|
+
const parts = shapeParts();
|
|
81
|
+
const m = new THREE.Matrix4();
|
|
82
|
+
spec.forEach(([x, y, z, w, h, d], i) => {
|
|
83
|
+
m.makeScale(w, h, d).setPosition(x, y, z);
|
|
84
|
+
addShape(parts, crownBlob(i * 3.7 + kind.length, detail), m, crownTint(baseColor), 1);
|
|
85
|
+
});
|
|
86
|
+
const geometry = partsGeometry(parts);
|
|
87
|
+
geometries.add(geometry);
|
|
88
|
+
return (shapes[kind] = geometry);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { plantShape, geometries };
|
|
92
|
+
}
|