roamwright 0.61.17 → 0.61.19
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 +1 -2
- package/examples/land-features.js +130 -9
- package/examples/land.html +2 -1
- package/examples/land.js +17 -2
- package/geometry/farm-props.js +59 -0
- package/geometry/field-map.js +147 -0
- package/geometry/instances.js +40 -0
- package/geometry/plant-tuft.js +138 -0
- package/geometry/ruins-breach.js +329 -0
- package/geometry/ruins-crater.js +269 -0
- package/geometry/ruins-masonry.js +323 -0
- package/geometry/ruins-walls.js +272 -0
- package/geometry/strata-mesa.js +114 -0
- package/geometry/strata-rocks.js +73 -2
- package/index.js +42 -1
- package/package.json +1 -1
- package/rendering/contact-shadows.js +56 -0
- package/rendering/crowd-lifecycle.js +80 -0
- package/rendering/farm-plough.js +145 -0
- package/rendering/plant-tuft.js +190 -0
- package/rendering/ruins-fittings.js +199 -0
- package/rendering/ruins-mound.js +103 -0
- package/examples/ruins.html +0 -60
- package/examples/ruins.js +0 -327
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dressed masonry and architectural fittings geometry: coursed ashlar walls with optional ragged
|
|
3
|
+
* ruined tops and open doorways, square flagstone paving, fluted standing or fallen column drums
|
|
4
|
+
* built via `rockSheet`, brass oil-lamp stands, and iron bracketed wall lanterns. Worker-safe.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { dim } from '../math/color.js';
|
|
10
|
+
import { createNoise } from '../math/noise.js';
|
|
11
|
+
import { createHash } from '../math/random.js';
|
|
12
|
+
import { TAU, sstep } from '../math/scalar.js';
|
|
13
|
+
import { rockSheet } from './creased-sheet.js';
|
|
14
|
+
import { C as defaultColor, TRS as defaultTransform, createUnitShapes, stick } from './primitives.js';
|
|
15
|
+
|
|
16
|
+
let fallbackShapes = null;
|
|
17
|
+
const defaultShapes = () => (fallbackShapes ??= createUnitShapes());
|
|
18
|
+
let fallbackNoise = null;
|
|
19
|
+
const defaultNoise = () => (fallbackNoise ??= createNoise().vnoise);
|
|
20
|
+
let fallbackHash = null;
|
|
21
|
+
const defaultHash = () => (fallbackHash ??= createHash(42));
|
|
22
|
+
|
|
23
|
+
/** Default dressed ashlar course height in metres. */
|
|
24
|
+
export const ASHLAR_COURSE = 0.42;
|
|
25
|
+
|
|
26
|
+
/** Default linear RGB colours for iron lantern fittings and brass lamp stands. */
|
|
27
|
+
export const MASONRY_METAL = Object.freeze({
|
|
28
|
+
iron: Object.freeze([0.05, 0.048, 0.05]),
|
|
29
|
+
brass: Object.freeze([0.62, 0.46, 0.16]),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/** Default fluting and axial profile settings for {@link buildColumnDrum}. */
|
|
33
|
+
export const COLUMN_DRUM = Object.freeze({
|
|
34
|
+
flutes: 20,
|
|
35
|
+
fluteDepth: 0.07,
|
|
36
|
+
along: Object.freeze([0, 0.025, 0.07, 0.2, 0.35, 0.5, 0.65, 0.8, 0.93, 0.975, 1]),
|
|
37
|
+
end: Object.freeze([1, 0.8, 0.45, 0.12, 0]),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const _drum = new THREE.Matrix4();
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Build an axis-aligned wall of coursed dressed ashlar blocks from `(x0, z0)` to `(x1, z1)` over a
|
|
44
|
+
* darker recessed core so masonry joints read cleanly. Supports ragged ruined tops and open gaps.
|
|
45
|
+
*
|
|
46
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} stone - Destination stone geometry builder.
|
|
47
|
+
* @param {() => number} [random=Math.random] - Repeatable PRNG in `[0, 1)`.
|
|
48
|
+
* @param {object} options
|
|
49
|
+
* @param {number} options.x0 - Start X in metres.
|
|
50
|
+
* @param {number} options.z0 - Start Z in metres.
|
|
51
|
+
* @param {number} options.x1 - End X in metres.
|
|
52
|
+
* @param {number} options.z1 - End Z in metres.
|
|
53
|
+
* @param {number} [options.y=0] - Wall base elevation in metres.
|
|
54
|
+
* @param {number} [options.height=3] - Standing wall height in metres.
|
|
55
|
+
* @param {number} [options.thickness=0.6] - Wall thickness in metres.
|
|
56
|
+
* @param {number} [options.colorHex=0xb9a686] - Stone sRGB hex colour.
|
|
57
|
+
* @param {number} [options.ragged=0] - Ruin wear amplitude (`0` intact, `0..1` ragged crest).
|
|
58
|
+
* @param {[number, number, number] | null} [options.gap=null] - Optional open doorway `[from, to, height]` along the wall in metres.
|
|
59
|
+
* @param {number} [options.seed=1] - Crest wear hash seed.
|
|
60
|
+
* @param {number} [options.course=ASHLAR_COURSE] - Course height in metres.
|
|
61
|
+
* @param {(x: number, z: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
|
|
62
|
+
* @param {Function} [options.color] - Linear RGB colour helper.
|
|
63
|
+
*/
|
|
64
|
+
export function buildAshlarWall(stone, random = Math.random, options) {
|
|
65
|
+
const {
|
|
66
|
+
x0,
|
|
67
|
+
z0,
|
|
68
|
+
x1,
|
|
69
|
+
z1,
|
|
70
|
+
y = 0,
|
|
71
|
+
height = 3,
|
|
72
|
+
thickness = 0.6,
|
|
73
|
+
colorHex = 0xb9a686,
|
|
74
|
+
ragged = 0,
|
|
75
|
+
gap = null,
|
|
76
|
+
seed = 1,
|
|
77
|
+
course = ASHLAR_COURSE,
|
|
78
|
+
hash = defaultHash(),
|
|
79
|
+
color = defaultColor,
|
|
80
|
+
} = options;
|
|
81
|
+
const along = Math.abs(x1 - x0) > Math.abs(z1 - z0);
|
|
82
|
+
const len = along ? Math.abs(x1 - x0) : Math.abs(z1 - z0);
|
|
83
|
+
const ax = Math.min(x0, x1);
|
|
84
|
+
const az = Math.min(z0, z1);
|
|
85
|
+
const top = u =>
|
|
86
|
+
height - (ragged ? ragged * height * (0.5 + 0.5 * Math.sin(u * 1.7 + seed * 2.1)) * (0.4 + 0.6 * hash((u * 2) | 0, seed, 71)) : 0);
|
|
87
|
+
const open = (u, v) => gap && u > gap[0] && u < gap[1] && v < gap[2];
|
|
88
|
+
const place = (u, v, lu, hv, thick, c) => {
|
|
89
|
+
const cu = u + lu / 2;
|
|
90
|
+
if (along) stone.box(ax + cu, y + v, z0, lu, hv, thick, c);
|
|
91
|
+
else stone.box(x0, y + v, az + cu, thick, hv, lu, c);
|
|
92
|
+
};
|
|
93
|
+
for (let row = 0, v = 0; v < height; row++, v += course) {
|
|
94
|
+
let u = row % 2 ? -0.45 : 0;
|
|
95
|
+
while (u < len) {
|
|
96
|
+
const lu = 0.7 + random() * 0.55;
|
|
97
|
+
const a = Math.max(0, u);
|
|
98
|
+
const b = Math.min(len, u + lu);
|
|
99
|
+
u += lu;
|
|
100
|
+
if (b - a < 0.12 || v + course * 0.5 > top((a + b) / 2) || open((a + b) / 2, v + course / 2)) continue;
|
|
101
|
+
place(a + 0.012, v + 0.012, b - a - 0.024, course - 0.024, thickness, color(colorHex, 0.07, random));
|
|
102
|
+
place(a, v, b - a, course, thickness - 0.07, dim(color(colorHex), 0.5));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Lay a rectangular floor of individually shaded, slightly tilted square flagstones over
|
|
109
|
+
* `(x0, z0)..(x1, z1)` with their top faces at elevation `y`.
|
|
110
|
+
*
|
|
111
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} stone - Destination stone geometry builder.
|
|
112
|
+
* @param {object} options
|
|
113
|
+
* @param {number} options.x0 - Minimum X in metres.
|
|
114
|
+
* @param {number} options.z0 - Minimum Z in metres.
|
|
115
|
+
* @param {number} options.x1 - Maximum X in metres.
|
|
116
|
+
* @param {number} options.z1 - Maximum Z in metres.
|
|
117
|
+
* @param {number} [options.y=0] - Top surface elevation in metres.
|
|
118
|
+
* @param {number} [options.size=1] - Nominal flagstone side length in metres.
|
|
119
|
+
* @param {number} [options.colorHex=0xb9a686] - Flagstone sRGB hex colour.
|
|
120
|
+
* @param {number} [options.seed=1] - Hash seed for shade and tilt variation.
|
|
121
|
+
* @param {(x: number, z: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
|
|
122
|
+
* @param {Function} [options.color] - Linear RGB colour helper.
|
|
123
|
+
*/
|
|
124
|
+
export function buildFlagstones(stone, options) {
|
|
125
|
+
const { x0, z0, x1, z1, y = 0, size = 1, colorHex = 0xb9a686, seed = 1, hash = defaultHash(), color = defaultColor } = options;
|
|
126
|
+
for (let x = x0; x < x1 - 0.01; x += size) {
|
|
127
|
+
for (let z = z0; z < z1 - 0.01; z += size) {
|
|
128
|
+
const w = Math.min(size, x1 - x);
|
|
129
|
+
const d = Math.min(size, z1 - z);
|
|
130
|
+
const k = 0.84 + 0.18 * hash(x * 7 + seed, z * 7, 31);
|
|
131
|
+
stone.box(x + w / 2, y - 0.12, z + d / 2, w - 0.016, 0.12, d - 0.016, dim(color(colorHex), k), (hash(x, z, seed + 32) - 0.5) * 0.015);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Build a fluted stone column drum (lying on its side) or broken standing column stump using a
|
|
138
|
+
* creased `rockSheet` mesh with chipped ends, fluted hollows and rising damp shading.
|
|
139
|
+
*
|
|
140
|
+
* @param {Pick<import('./builder.js').GB, 'raw'>} wall - Destination dressed-stone geometry builder.
|
|
141
|
+
* @param {() => number} [random=Math.random] - Repeatable PRNG in `[0, 1)`.
|
|
142
|
+
* @param {object} options
|
|
143
|
+
* @param {number} [options.x=0] - Column X position in metres.
|
|
144
|
+
* @param {number} [options.y=0] - Resting ground elevation in metres.
|
|
145
|
+
* @param {number} [options.z=0] - Column Z position in metres.
|
|
146
|
+
* @param {number} [options.radius=0.4] - Column radius in metres.
|
|
147
|
+
* @param {number} [options.length=2] - Drum or stump length in metres.
|
|
148
|
+
* @param {number} [options.colorHex=0xb8a888] - Stone sRGB hex colour.
|
|
149
|
+
* @param {boolean} [options.lying=true] - True for a fallen horizontal drum, false for a broken standing stump.
|
|
150
|
+
* @param {number} [options.yaw=0] - Orientation around Y in radians.
|
|
151
|
+
* @param {(x: number, z: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
|
|
152
|
+
* @param {(x: number, z: number, salt?: number) => number} [options.noise] - Seeded value noise in `[0, 1)`.
|
|
153
|
+
* @param {Function} [options.color] - Linear RGB colour helper.
|
|
154
|
+
* @param {Function} [options.transform] - Transform matrix helper (`TRS`).
|
|
155
|
+
* @param {Partial<typeof COLUMN_DRUM>} [options.shape] - Overrides to {@link COLUMN_DRUM}.
|
|
156
|
+
*/
|
|
157
|
+
export function buildColumnDrum(wall, random = Math.random, options = {}) {
|
|
158
|
+
const {
|
|
159
|
+
x = 0,
|
|
160
|
+
y = 0,
|
|
161
|
+
z = 0,
|
|
162
|
+
radius: r = 0.4,
|
|
163
|
+
length: len = 2,
|
|
164
|
+
colorHex = 0xb8a888,
|
|
165
|
+
lying = true,
|
|
166
|
+
yaw = 0,
|
|
167
|
+
hash = defaultHash(),
|
|
168
|
+
noise = defaultNoise(),
|
|
169
|
+
color = defaultColor,
|
|
170
|
+
transform = defaultTransform,
|
|
171
|
+
} = options;
|
|
172
|
+
const cfg = options.shape ? { ...COLUMN_DRUM, ...options.shape } : COLUMN_DRUM;
|
|
173
|
+
const around = cfg.flutes * 3;
|
|
174
|
+
const key = random();
|
|
175
|
+
const c = dim(color(colorHex), 0.8 + 0.1 * key);
|
|
176
|
+
const seed = Math.floor(key * 997);
|
|
177
|
+
const e = _drum.copy(transform(x, y + (lying ? r : 0), z, 1, 1, 1, yaw, lying ? Math.PI / 2 : 0)).elements;
|
|
178
|
+
const place = (px, py, pz) => [
|
|
179
|
+
e[0] * px + e[4] * py + e[8] * pz + e[12],
|
|
180
|
+
e[1] * px + e[5] * py + e[9] * pz + e[13],
|
|
181
|
+
e[2] * px + e[6] * py + e[10] * pz + e[14],
|
|
182
|
+
];
|
|
183
|
+
const slope = lying ? 0 : 0.2 * len * (0.4 + 0.6 * hash(seed, 2, 731));
|
|
184
|
+
const facing = TAU * hash(seed, 3, 732);
|
|
185
|
+
const topAt = (ca, sa, f) => len - slope * 0.5 * (1 + f * (ca * Math.cos(facing) + sa * Math.sin(facing)));
|
|
186
|
+
const radiusAt = (a, along, end) => {
|
|
187
|
+
const t = (a / TAU) * cfg.flutes;
|
|
188
|
+
const hollow = Math.sin(Math.PI * (t - Math.floor(t)));
|
|
189
|
+
const chip = 0.14 * sstep(0.62, 0.85, noise(Math.cos(a) * 3 + seed, Math.sin(a) * 3 + along * 4, 733)) * sstep(0.25, 0, end);
|
|
190
|
+
return r * (1 - cfg.fluteDepth * hollow) * (1 - chip) * (end < 0.004 ? 0.95 : 1);
|
|
191
|
+
};
|
|
192
|
+
const tone = (shade, low) => {
|
|
193
|
+
const k = shade * (lying ? 1 : 1 - 0.22 * sstep(0.5, 0, low));
|
|
194
|
+
return [c[0] * k, c[1] * k, c[2] * k];
|
|
195
|
+
};
|
|
196
|
+
const rows = [];
|
|
197
|
+
const tones = [];
|
|
198
|
+
const ring = (fn, tn) => {
|
|
199
|
+
const row = [];
|
|
200
|
+
const shades = [];
|
|
201
|
+
for (let i = 0; i < around; i++) {
|
|
202
|
+
const a = (i / around) * TAU;
|
|
203
|
+
const p = fn(a, Math.cos(a), Math.sin(a));
|
|
204
|
+
row.push(place(p[0], p[1], p[2]));
|
|
205
|
+
shades.push(tn(a));
|
|
206
|
+
}
|
|
207
|
+
rows.push(row);
|
|
208
|
+
tones.push(shades);
|
|
209
|
+
};
|
|
210
|
+
const grain = (a, along) => 0.9 + 0.16 * noise(Math.cos(a) * 2 + seed + along * 1.3, Math.sin(a) * 2, 734);
|
|
211
|
+
if (lying) {
|
|
212
|
+
for (const f of [...cfg.end].reverse()) {
|
|
213
|
+
ring(
|
|
214
|
+
(a, ca, sa) => [ca * radiusAt(a, 0, 0) * f, 0.01 * (1 - f), sa * radiusAt(a, 0, 0) * f],
|
|
215
|
+
a => tone(grain(a, 0) * 0.92, 1),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const share of cfg.along) {
|
|
220
|
+
ring(
|
|
221
|
+
(a, ca, sa) => {
|
|
222
|
+
const top = topAt(ca, sa, 1);
|
|
223
|
+
const along = share * top;
|
|
224
|
+
const rr = radiusAt(a, along, Math.min(along, top - along));
|
|
225
|
+
return [ca * rr, along, sa * rr];
|
|
226
|
+
},
|
|
227
|
+
a => {
|
|
228
|
+
const t = (a / TAU) * cfg.flutes;
|
|
229
|
+
const hollow = Math.sin(Math.PI * (t - Math.floor(t)));
|
|
230
|
+
return tone(grain(a, share * len) * (1 - 0.12 * hollow), share * len);
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
for (const f of cfg.end.slice(1)) {
|
|
235
|
+
ring(
|
|
236
|
+
(a, ca, sa) => {
|
|
237
|
+
const rr = radiusAt(a, len, 0) * f;
|
|
238
|
+
const rough = lying ? 0.004 : 0.05 * r * (noise(ca * f * 4 + seed, sa * f * 4, 735) - 0.5);
|
|
239
|
+
return [ca * rr, topAt(ca, sa, f) + rough * (1 - f * f), sa * rr];
|
|
240
|
+
},
|
|
241
|
+
a => tone(grain(a, len) * (lying ? 0.92 : 1.04), len),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
rockSheet(wall, rows, tones, { wrap: true, inside: true });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Emit the brass stand and oil-lamp vessel geometry into `kit.trim` and `kit.cloth`, returning the
|
|
249
|
+
* local `[x, y, z]` wick position for a flame.
|
|
250
|
+
*
|
|
251
|
+
* @param {{ trim: import('./builder.js').GB, cloth: import('./builder.js').GB }} kit - Destination geometry builders.
|
|
252
|
+
* @param {object} [options]
|
|
253
|
+
* @param {number} [options.x=0] - Base X in metres.
|
|
254
|
+
* @param {number} [options.y=0] - Base elevation in metres.
|
|
255
|
+
* @param {number} [options.z=0] - Base Z in metres.
|
|
256
|
+
* @param {number} [options.height=1.25] - Stand height in metres.
|
|
257
|
+
* @param {readonly [number, number, number]} [options.metal=MASONRY_METAL.brass] - Linear RGB metal colour.
|
|
258
|
+
* @param {object} [options.shapes] - Unit shapes (`createUnitShapes()`).
|
|
259
|
+
* @param {Function} [options.transform] - Transform matrix helper (`TRS`).
|
|
260
|
+
* @returns {[number, number, number]} Wick position `[x, y, z]` in metres.
|
|
261
|
+
*/
|
|
262
|
+
export function buildLampStandGeometry(kit, options = {}) {
|
|
263
|
+
const {
|
|
264
|
+
x = 0,
|
|
265
|
+
y = 0,
|
|
266
|
+
z = 0,
|
|
267
|
+
height: h = 1.25,
|
|
268
|
+
metal = MASONRY_METAL.brass,
|
|
269
|
+
shapes = defaultShapes(),
|
|
270
|
+
transform = defaultTransform,
|
|
271
|
+
} = options;
|
|
272
|
+
kit.trim.add(shapes.cyl16, dim(metal, 0.7), transform(x, y, z, 0.3, 0.03, 0.3));
|
|
273
|
+
kit.trim.add(shapes.cyl6, dim(metal, 0.8), transform(x, y, z, 0.035, h, 0.035));
|
|
274
|
+
kit.trim.add(shapes.cyl16, metal, transform(x, y + h, z, 0.2, 0.02, 0.2));
|
|
275
|
+
kit.cloth.add(shapes.sph, metal, transform(x, y + h + 0.01, z, 0.17, 0.09, 0.12));
|
|
276
|
+
kit.cloth.add(shapes.cone6, metal, transform(x + 0.1, y + h + 0.04, z, 0.04, 0.09, 0.04, 0, 0, -1.2));
|
|
277
|
+
return [x + 0.16, y + h + 0.13, z];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Emit an iron wall bracket and six-bar lantern cage into `kit.trim`, returning the lantern centre
|
|
282
|
+
* `[lx, y, lz]` in local metres.
|
|
283
|
+
*
|
|
284
|
+
* @param {{ trim: import('./builder.js').GB }} kit - Destination geometry builder container.
|
|
285
|
+
* @param {object} [options]
|
|
286
|
+
* @param {number} [options.x=0] - Wall anchor X in metres.
|
|
287
|
+
* @param {number} [options.y=2.5] - Bracket height in metres.
|
|
288
|
+
* @param {number} [options.z=0] - Wall anchor Z in metres.
|
|
289
|
+
* @param {number} [options.out=0] - Outward bearing in radians (`0` east, clockwise through south).
|
|
290
|
+
* @param {readonly [number, number, number]} [options.metal=MASONRY_METAL.iron] - Linear RGB metal colour.
|
|
291
|
+
* @param {object} [options.shapes] - Unit shapes (`createUnitShapes()`).
|
|
292
|
+
* @param {Function} [options.transform] - Transform matrix helper (`TRS`).
|
|
293
|
+
* @param {Function} [options.stickFn] - Tapered rod helper (`stick`).
|
|
294
|
+
* @returns {{ x: number, y: number, z: number }} Lantern centre position in metres.
|
|
295
|
+
*/
|
|
296
|
+
export function buildWallLanternGeometry(kit, options = {}) {
|
|
297
|
+
const {
|
|
298
|
+
x = 0,
|
|
299
|
+
y = 2.5,
|
|
300
|
+
z = 0,
|
|
301
|
+
out = 0,
|
|
302
|
+
metal = MASONRY_METAL.iron,
|
|
303
|
+
shapes = defaultShapes(),
|
|
304
|
+
transform = defaultTransform,
|
|
305
|
+
stickFn = stick,
|
|
306
|
+
} = options;
|
|
307
|
+
const dx = Math.cos(out);
|
|
308
|
+
const dz = Math.sin(out);
|
|
309
|
+
const lx = x + dx * 0.55;
|
|
310
|
+
const lz = z + dz * 0.55;
|
|
311
|
+
stickFn(kit.trim, [x, y + 0.34, z], [lx, y + 0.34, lz], 0.016, metal);
|
|
312
|
+
stickFn(kit.trim, [x, y - 0.1, z], [x + dx * 0.4, y + 0.34, z + dz * 0.4], 0.012, metal);
|
|
313
|
+
stickFn(kit.trim, [lx, y + 0.34, lz], [lx, y + 0.22, lz], 0.008, metal);
|
|
314
|
+
kit.trim.add(shapes.cone6, metal, transform(lx, y + 0.14, lz, 0.2, 0.09, 0.2));
|
|
315
|
+
kit.trim.add(shapes.cyl6, metal, transform(lx, y - 0.16, lz, 0.17, 0.025, 0.17));
|
|
316
|
+
for (let k = 0; k < 6; k++) {
|
|
317
|
+
const a = (k / 6) * TAU;
|
|
318
|
+
const bx = lx + Math.cos(a) * 0.075;
|
|
319
|
+
const bz = lz + Math.sin(a) * 0.075;
|
|
320
|
+
stickFn(kit.trim, [bx, y - 0.14, bz], [bx, y + 0.14, bz], 0.006, metal);
|
|
321
|
+
}
|
|
322
|
+
return { x: lx, y, z: lz };
|
|
323
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coursed ruined stone walls laid in running bond with weathered open joints, stepped broken tops,
|
|
3
|
+
* tumbled fallen blocks at the foot, and optional surrounding rubble heaps and box colliders.
|
|
4
|
+
* Pure geometry assembly into a `GB` builder; worker-safe.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createNoise } from '../math/noise.js';
|
|
9
|
+
import { clamp } from '../math/scalar.js';
|
|
10
|
+
import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
|
|
11
|
+
import { buildRubbleHeap } from './ruins-rubble.js';
|
|
12
|
+
|
|
13
|
+
let fallbackRock = null;
|
|
14
|
+
const defaultRock = () => (fallbackRock ??= createUnitShapes().rock);
|
|
15
|
+
let fallbackFbm = null;
|
|
16
|
+
const defaultFbm = () => (fallbackFbm ??= createNoise().fbm);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Default masonry course height, block lengths, weathering and wear parameters (metres) for
|
|
20
|
+
* {@link coursedWall}, {@link tumbleBlock} and {@link buildRuinWall}.
|
|
21
|
+
*/
|
|
22
|
+
export const RUIN_WALL = Object.freeze({
|
|
23
|
+
/** Height of one masonry course in metres. */
|
|
24
|
+
course: 0.42,
|
|
25
|
+
/** Shortest and longest stone block lengths in metres. */
|
|
26
|
+
block: Object.freeze([0.7, 1.35]),
|
|
27
|
+
/** Open weathered joint width in metres. */
|
|
28
|
+
joint: 0.03,
|
|
29
|
+
/** Footing depth sunk below ground in metres. */
|
|
30
|
+
footing: 0.3,
|
|
31
|
+
/** Number of lowest courses darkened by rising damp. */
|
|
32
|
+
dampCourses: 2,
|
|
33
|
+
/** Share of blocks darkened by lichen and run-off staining. */
|
|
34
|
+
stained: 0.12,
|
|
35
|
+
/** Spatial frequency per metre of the ruined crest wear profile. */
|
|
36
|
+
wearGrain: 0.2,
|
|
37
|
+
/** End-zone length in metres where wall corners stand higher. */
|
|
38
|
+
corner: 4,
|
|
39
|
+
/** Default stone colour in sRGB hex. */
|
|
40
|
+
hue: 0xb39c76,
|
|
41
|
+
/** Default share of fallen courses and strewn foot rubble. */
|
|
42
|
+
fallen: 0.6,
|
|
43
|
+
/** Default share of crest blocks shifted loose. */
|
|
44
|
+
loose: 0.35,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function blockColour(hue, rng, course, top, cfg, color) {
|
|
48
|
+
const col = color(hue, 0.1, rng);
|
|
49
|
+
const k = (course < cfg.dampCourses ? 0.8 + 0.08 * course : 1) * (top ? 1.07 : 1) * (rng() < cfg.stained ? 0.78 : 1);
|
|
50
|
+
return [col[0] * k, col[1] * k, col[2] * k];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Emit one (or occasionally two broken halves of a) masonry block tumbled off course `course` onto
|
|
55
|
+
* the ground beside a wall.
|
|
56
|
+
*
|
|
57
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} builder - Destination stone geometry builder.
|
|
58
|
+
* @param {() => number} [random=Math.random] - Repeatable PRNG in `[0, 1)`.
|
|
59
|
+
* @param {object} options
|
|
60
|
+
* @param {number} options.x - Wall-axis position X in local metres.
|
|
61
|
+
* @param {number} options.z - Wall-axis position Z in local metres.
|
|
62
|
+
* @param {[number, number]} options.across - Unit vector perpendicular to the wall `[ax, az]`.
|
|
63
|
+
* @param {number} options.length - Fallen block length along the wall in metres.
|
|
64
|
+
* @param {number} options.thickness - Wall thickness in metres.
|
|
65
|
+
* @param {number} options.course - Zero-based course index from which the block fell.
|
|
66
|
+
* @param {number} [options.hue=RUIN_WALL.hue] - Stone sRGB hex colour.
|
|
67
|
+
* @param {number} [options.turn=0] - Wall yaw bearing in radians.
|
|
68
|
+
* @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
|
|
69
|
+
* @param {Function} [options.color] - Linear RGB colour helper.
|
|
70
|
+
* @param {Partial<typeof RUIN_WALL>} [options.shape] - Overrides to {@link RUIN_WALL}.
|
|
71
|
+
*/
|
|
72
|
+
export function tumbleBlock(builder, random = Math.random, options) {
|
|
73
|
+
const { x, z, across, length, thickness, course, hue = RUIN_WALL.hue, turn = 0, ground = () => 0, color = defaultColor } = options;
|
|
74
|
+
const cfg = options.shape ? { ...RUIN_WALL, ...options.shape } : RUIN_WALL;
|
|
75
|
+
const pieces = random() < 0.3 ? 2 : 1;
|
|
76
|
+
for (let i = 0; i < pieces; i++) {
|
|
77
|
+
const side = random() < 0.5 ? -1 : 1;
|
|
78
|
+
const out = thickness * 0.6 + 0.3 + random() * (0.4 + course * cfg.course * 0.6);
|
|
79
|
+
const px = x + across[0] * side * out + (random() - 0.5) * 0.8;
|
|
80
|
+
const pz = z + across[1] * side * out + (random() - 0.5) * 0.8;
|
|
81
|
+
const size = length / pieces;
|
|
82
|
+
builder.box(
|
|
83
|
+
px,
|
|
84
|
+
ground(px, pz) - 0.12,
|
|
85
|
+
pz,
|
|
86
|
+
thickness * (0.9 + random() * 0.15),
|
|
87
|
+
cfg.course * 0.95,
|
|
88
|
+
size * (0.85 + random() * 0.15),
|
|
89
|
+
blockColour(hue, random, 1 + (random() < 0.5), false, cfg, color),
|
|
90
|
+
turn + (random() - 0.5) * 1.4,
|
|
91
|
+
(random() - 0.5) * 0.5,
|
|
92
|
+
(random() - 0.5) * 0.4,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Build a running-bond coursed stone wall from `(ax, az)` to `(bx, bz)` following `ground(x, z)`,
|
|
99
|
+
* stepping its courses to `heightAt(u)` (`u` in `[0, 1]`), shifting loose crest blocks and tumbling
|
|
100
|
+
* missing upper blocks beside the wall.
|
|
101
|
+
*
|
|
102
|
+
* @param {Pick<import('./builder.js').GB, 'box'>} builder - Destination stone geometry builder.
|
|
103
|
+
* @param {() => number} [random=Math.random] - Repeatable PRNG in `[0, 1)`.
|
|
104
|
+
* @param {object} options
|
|
105
|
+
* @param {number} options.ax - Start X in local metres.
|
|
106
|
+
* @param {number} options.az - Start Z in local metres.
|
|
107
|
+
* @param {number} options.bx - End X in local metres.
|
|
108
|
+
* @param {number} options.bz - End Z in local metres.
|
|
109
|
+
* @param {number} [options.thickness=0.6] - Wall thickness in metres.
|
|
110
|
+
* @param {(u: number) => number} [options.heightAt] - Standing height above ground at normalized position `u`.
|
|
111
|
+
* @param {number} [options.hue=RUIN_WALL.hue] - Stone sRGB hex colour.
|
|
112
|
+
* @param {number} [options.fallen=0.36] - Probability a missing upper block lies tumbled nearby.
|
|
113
|
+
* @param {number} [options.loose=RUIN_WALL.loose] - Probability a crest block sits shifted half off the wall.
|
|
114
|
+
* @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
|
|
115
|
+
* @param {Function} [options.color] - Linear RGB colour helper.
|
|
116
|
+
* @param {Partial<typeof RUIN_WALL>} [options.shape] - Overrides to {@link RUIN_WALL}.
|
|
117
|
+
*/
|
|
118
|
+
export function coursedWall(builder, random = Math.random, options) {
|
|
119
|
+
const {
|
|
120
|
+
ax,
|
|
121
|
+
az,
|
|
122
|
+
bx,
|
|
123
|
+
bz,
|
|
124
|
+
thickness = 0.6,
|
|
125
|
+
heightAt = () => 2,
|
|
126
|
+
hue = RUIN_WALL.hue,
|
|
127
|
+
fallen = 0.36,
|
|
128
|
+
loose = RUIN_WALL.loose,
|
|
129
|
+
ground = () => 0,
|
|
130
|
+
color = defaultColor,
|
|
131
|
+
} = options;
|
|
132
|
+
const cfg = options.shape ? { ...RUIN_WALL, ...options.shape } : RUIN_WALL;
|
|
133
|
+
const len = Math.hypot(bx - ax, bz - az);
|
|
134
|
+
const turn = Math.atan2(bx - ax, bz - az);
|
|
135
|
+
const along = [(bx - ax) / len, (bz - az) / len];
|
|
136
|
+
const across = [along[1], -along[0]];
|
|
137
|
+
const courses = Math.ceil((Math.max(heightAt(0), heightAt(0.5), heightAt(1)) + cfg.footing) / cfg.course) + 2;
|
|
138
|
+
for (let k = 0; k < courses; k++) {
|
|
139
|
+
let s = k % 2 ? -random() * cfg.block[0] : 0;
|
|
140
|
+
while (s < len) {
|
|
141
|
+
const e = Math.min(len, s + cfg.block[0] + random() * (cfg.block[1] - cfg.block[0]));
|
|
142
|
+
const from = Math.max(0, s);
|
|
143
|
+
s = e;
|
|
144
|
+
if (e - from < 0.25) continue;
|
|
145
|
+
const mid = (from + e) / 2;
|
|
146
|
+
const x = ax + along[0] * mid;
|
|
147
|
+
const z = az + along[1] * mid;
|
|
148
|
+
const stands = heightAt(mid / len) + cfg.footing + (random() - 0.5) * 0.3;
|
|
149
|
+
const base = ground(x, z) - cfg.footing;
|
|
150
|
+
if (k > 0 && (k + 1) * cfg.course > stands) {
|
|
151
|
+
if (random() < fallen) {
|
|
152
|
+
tumbleBlock(builder, random, {
|
|
153
|
+
x,
|
|
154
|
+
z,
|
|
155
|
+
across,
|
|
156
|
+
length: e - from,
|
|
157
|
+
thickness,
|
|
158
|
+
course: k,
|
|
159
|
+
hue,
|
|
160
|
+
turn,
|
|
161
|
+
ground,
|
|
162
|
+
color,
|
|
163
|
+
shape: cfg,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const top = (k + 2) * cfg.course > stands;
|
|
169
|
+
const shifted = top && random() < loose;
|
|
170
|
+
const depth = (shifted ? 0.55 : 1) * thickness * (0.94 + random() * 0.08);
|
|
171
|
+
const off = (shifted ? (random() < 0.5 ? -1 : 1) * thickness * 0.22 : 0) + (random() - 0.5) * 0.04;
|
|
172
|
+
builder.box(
|
|
173
|
+
x + across[0] * off,
|
|
174
|
+
base + k * cfg.course,
|
|
175
|
+
z + across[1] * off,
|
|
176
|
+
depth,
|
|
177
|
+
cfg.course - cfg.joint * (0.6 + random() * 0.8),
|
|
178
|
+
e - from - cfg.joint,
|
|
179
|
+
blockColour(hue, random, k, top, cfg, color),
|
|
180
|
+
turn + (random() - 0.5) * (shifted ? 0.2 : 0.025),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Build a breached, unevenly weathered coursed stone ruin wall from `(ax, az)` to `(bx, bz)`,
|
|
188
|
+
* tallest near its corners, strewn with foot rubble via {@link buildRubbleHeap}, and optionally
|
|
189
|
+
* appending standing-stretch box colliders into `solid`.
|
|
190
|
+
*
|
|
191
|
+
* @param {Pick<import('./builder.js').GB, 'box' | 'add'>} builder - Destination stone geometry builder.
|
|
192
|
+
* @param {() => number} [random=Math.random] - Repeatable PRNG in `[0, 1)`.
|
|
193
|
+
* @param {object} options
|
|
194
|
+
* @param {number} options.ax - Start X in local metres.
|
|
195
|
+
* @param {number} options.az - Start Z in local metres.
|
|
196
|
+
* @param {number} options.bx - End X in local metres.
|
|
197
|
+
* @param {number} options.bz - End Z in local metres.
|
|
198
|
+
* @param {number} [options.tall=2.5] - Peak wall height in metres.
|
|
199
|
+
* @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
|
|
200
|
+
* @param {number} [options.originX=0] - World X offset added when pushing `solid` box colliders.
|
|
201
|
+
* @param {number} [options.originZ=0] - World Z offset added when pushing `solid` box colliders.
|
|
202
|
+
* @param {Array<object> | null} [options.solid=null] - Optional array receiving `{ x, z, hx, hz, rot }` colliders.
|
|
203
|
+
* @param {number} [options.hue=RUIN_WALL.hue] - Stone sRGB hex colour.
|
|
204
|
+
* @param {number} [options.fallen=RUIN_WALL.fallen] - Fallen-block and foot-rubble density share.
|
|
205
|
+
* @param {(x: number, z: number, octaves: number, salt: number) => number} [options.fbm] - Fractal noise for crest wear.
|
|
206
|
+
* @param {import('three').BufferGeometry} [options.rock] - Unit broken stone geometry for foot rubble.
|
|
207
|
+
* @param {Function} [options.color] - Linear RGB colour helper.
|
|
208
|
+
* @param {Function} [options.transform] - Transform matrix helper (`TRS`).
|
|
209
|
+
* @param {Partial<typeof RUIN_WALL>} [options.shape] - Overrides to {@link RUIN_WALL}.
|
|
210
|
+
*/
|
|
211
|
+
export function buildRuinWall(builder, random = Math.random, options) {
|
|
212
|
+
const {
|
|
213
|
+
ax,
|
|
214
|
+
az,
|
|
215
|
+
bx,
|
|
216
|
+
bz,
|
|
217
|
+
tall = 2.5,
|
|
218
|
+
ground = () => 0,
|
|
219
|
+
originX = 0,
|
|
220
|
+
originZ = 0,
|
|
221
|
+
solid = null,
|
|
222
|
+
hue = RUIN_WALL.hue,
|
|
223
|
+
fallen = RUIN_WALL.fallen,
|
|
224
|
+
fbm = defaultFbm(),
|
|
225
|
+
rock = defaultRock(),
|
|
226
|
+
color = defaultColor,
|
|
227
|
+
transform = defaultTransform,
|
|
228
|
+
} = options;
|
|
229
|
+
const cfg = options.shape ? { ...RUIN_WALL, ...options.shape } : RUIN_WALL;
|
|
230
|
+
const len = Math.hypot(bx - ax, bz - az);
|
|
231
|
+
const turn = Math.atan2(bx - ax, bz - az);
|
|
232
|
+
const thick = 0.55 + random() * 0.2;
|
|
233
|
+
const seed = Math.floor(random() * 1e4);
|
|
234
|
+
const heightAt = u => {
|
|
235
|
+
const end = Math.max(0, 1 - (Math.min(u, 1 - u) * len) / cfg.corner);
|
|
236
|
+
const wear = fbm(u * len * cfg.wearGrain, seed * 0.37, 3, seed);
|
|
237
|
+
return tall * clamp(1.5 * (wear - 0.28) + 0.55 * end, 0.04, 1);
|
|
238
|
+
};
|
|
239
|
+
coursedWall(builder, random, {
|
|
240
|
+
ax,
|
|
241
|
+
az,
|
|
242
|
+
bx,
|
|
243
|
+
bz,
|
|
244
|
+
thickness: thick,
|
|
245
|
+
heightAt,
|
|
246
|
+
hue,
|
|
247
|
+
fallen: fallen * 0.6,
|
|
248
|
+
ground,
|
|
249
|
+
color,
|
|
250
|
+
shape: cfg,
|
|
251
|
+
});
|
|
252
|
+
const n = Math.max(2, Math.round(len / 1.5));
|
|
253
|
+
for (let i = 0; i < n; i++) {
|
|
254
|
+
const u = (i + 0.5) / n;
|
|
255
|
+
const x = ax + (bx - ax) * u;
|
|
256
|
+
const z = az + (bz - az) * u;
|
|
257
|
+
if (heightAt(u) > 0.9 && solid) solid.push({ x: originX + x, z: originZ + z, hx: thick / 2, hz: len / n / 2, rot: turn });
|
|
258
|
+
if (random() < fallen * 0.6) {
|
|
259
|
+
buildRubbleHeap(builder, random, {
|
|
260
|
+
x: x + (random() - 0.5) * 2.4,
|
|
261
|
+
z: z + (random() - 0.5) * 2.4,
|
|
262
|
+
radius: 0.9,
|
|
263
|
+
count: 3,
|
|
264
|
+
ground,
|
|
265
|
+
hue,
|
|
266
|
+
rock,
|
|
267
|
+
color,
|
|
268
|
+
transform,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|