roamwright 0.61.25 → 0.61.27

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.
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Roamwright's public geometry builders and kits, grouped by subsystem. Re-exported whole from `roamwright`.
3
+ * @module
4
+ */
5
+
6
+ // ── Context, math & core geometry ────────────────────────────────────────
7
+ export { GB as GeometryBuilder, geometryOf } from './builder.js';
8
+ export { stillMesh } from './still-mesh.js';
9
+ export { createUnitShapes, createFigureUnitShapes } from './primitives.js';
10
+
11
+ // ── Terrain & land baking ────────────────────────────────────────────────
12
+ export { createLandSheet } from './land-sheet.js';
13
+ export { createLandWaters } from './land-waters.js';
14
+ export { createEarthworks } from './land-earthworks.js';
15
+ export { createDitchedGround } from './land-ditches.js';
16
+ export { createMoundFaces } from './land-mound-faces.js';
17
+ export { createTownGround } from './land-town-ground.js';
18
+ export { createDryBasin } from './land-dry-basin.js';
19
+ export { landEdgeGrid } from './land-edge.js';
20
+ export { rockSheet } from './creased-sheet.js';
21
+ export { planTalusStones } from './strata-talus.js';
22
+ export {
23
+ OUTCROP_SEEDS,
24
+ OUTCROP_SHAPE,
25
+ SCREE_STONE_FORMS,
26
+ WORN_STONE_FOOT,
27
+ WORN_STONE_FORMS,
28
+ beddedOutcrop,
29
+ cragRock,
30
+ createCragLibrary,
31
+ createRockShapes,
32
+ fallenStone,
33
+ flatQuad,
34
+ jointed,
35
+ wornStone,
36
+ } from './strata-rocks.js';
37
+ export { MESA_CLIFF, buildMesaCliff } from './strata-mesa.js';
38
+ export { HILL_RUIN, buildHillRuin } from './ruins-hill.js';
39
+ export { MOUND_RUINS, buildMoundRuins, buildMoundSherds, buildMoundWalls, createMoundRuins } from './ruins-mound.js';
40
+ export { RUBBLE_HEAP, RUBBLE_SCATTER, buildRubbleHeap, createRubbleStone, scatterRubble } from './ruins-rubble.js';
41
+ export { RUIN_WALL, buildRuinWall, coursedWall, tumbleBlock } from './ruins-walls.js';
42
+ export {
43
+ ASHLAR_COURSE,
44
+ COLUMN_DRUM,
45
+ MASONRY_METAL,
46
+ buildAshlarWall,
47
+ buildColumnDrum,
48
+ buildFlagstones,
49
+ buildLampStandGeometry,
50
+ buildWallLanternGeometry,
51
+ } from './ruins-masonry.js';
52
+ export { DEFAULT_SCORCH_PALETTE, addEmberSkin, buildBreachFan, buildRubbleMound } from './ruins-breach.js';
53
+ export { CRATER_PALETTE, buildCraterBowl, buildFallenBoulder, craterDip } from './ruins-crater.js';
54
+ export {
55
+ GROUND_PATCH_DEFAULTS,
56
+ GROUND_PATCH_KINDS,
57
+ buildDrapePebbleGeometry,
58
+ createGroundPatchData,
59
+ drapeGroundPatch,
60
+ scatterPatchBits,
61
+ } from './ground-drape.js';
62
+ export { createShapeBatches } from './instances.js';
63
+ export { planStreamBanks } from './stream-banks.js';
64
+ export { SPRING_COLOURS, SPRING_HEAD, planSpringRocks, planSprings, springFrame, springWetGround } from './stream-springs.js';
65
+ export { planShoreReeds } from './shore-reeds.js';
66
+ export { roadRibbon } from './road-ribbon.js';
67
+ export { bridgeSpanPath, bridgeSpanPaths, planBridgeFrame, planRoadBridges } from './road-bridges.js';
68
+ export {
69
+ AVENUE_CEDAR,
70
+ PAVED_ROAD,
71
+ avenueBoleRadius,
72
+ buildAvenueCedar,
73
+ buildFarRoadPaving,
74
+ buildRoadArch,
75
+ buildRoadMilestones,
76
+ buildRoadPaving,
77
+ planRoadAvenue,
78
+ roadLine,
79
+ } from './paved-road.js';
80
+ export { createFieldSurvey, FIELD_SURVEY, FIELD_USE } from './field-survey.js';
81
+ export { FIELD_BITS, NO_PATH_DISTANCE, createFieldCoverCache, packFieldBits, packSparsePlain } from './field-map.js';
82
+ export {
83
+ DEFAULT_DRIFT_SCALE,
84
+ DEFAULT_FLOWER_DRIFTS,
85
+ DEFAULT_FLOWER_LOOKS,
86
+ FLOWER_KINDS,
87
+ TUFT_SHAPE,
88
+ createFlowerDrift,
89
+ tuftGeometry,
90
+ } from './plant-tuft.js';
91
+ export { LAND_TRAILS, LAND_TRAIL_MARKS, boundsOverlap, createLandTrails, rasterizeTrailMarks } from './land-trails.js';
92
+ export { createIrrigatedFields, IRRIGATED_FIELDS } from './irrigated-fields.js';
93
+ export { DEFAULT_CROP_STANDS, FARM_CROPS, createCropPlacer } from './farm-crops.js';
94
+ export { DRYSTONE_WALL, FIELD_STONE, drystoneStone, planDrystoneWall } from './drystone-wall.js';
95
+ export { DEFAULT_FENCE_STYLE, addFenceRing, buildFenceRing } from './timber-fence.js';
96
+ export { TREE_GROVE, planScatterInstances, planTreeGrove } from './scatter-instances.js';
97
+ export { FARM_BOUNDARIES, planFieldBoundaries } from './farm-boundaries.js';
98
+ export { DEFAULT_FARM_HERDS, FARM_LIFE, herdPlaces, parcelPlaces, planFarmLife, ploughSite } from './farm-life.js';
99
+ export { STOOK, createStook } from './prop-stook.js';
100
+ export { PLOUGH, ploughGear } from './prop-plough.js';
101
+ export { FARM_BARN_DOORS, FARM_HAYSTACK, FARM_STONE_PILE, buildBarnDoors, buildHaystacks, buildStonePile } from './farm-props.js';
102
+
103
+ // ── Settlements, houses, fortifications & interiors ──────────────────────
104
+ export { createHouseKit } from './house-kit.js';
105
+ export { createPooledKit } from './house-kit-pool.js';
106
+ export { layVillage } from './village-layout.js';
107
+ export { TOWN_CELL, districtAt, layTown, rectFree, stampArea, stampDisc, stampLine, stampRect, townPlan } from './town-plan.js';
108
+ export { townLots } from './town-lots.js';
109
+ export { paintTown } from './town-ground.js';
110
+ export { createTownHouses } from './town-houses.js';
111
+ export { ARCADE_BAY, ARCADE_WALK, createTownWorks, townFacing } from './town-works.js';
112
+ export { createTownLights, distantTownLights } from './town-lights.js';
113
+ export { createCourtyardInn } from './town-courtyard-inn.js';
114
+ export { alongRim, createTownGarden } from './town-garden.js';
115
+ export { createGardenPlantShapes, createPlantList, GARDEN_PLANT_LOBES, GARDEN_PLANT_SEEN } from './plant-garden.js';
116
+ export { createWallRuns } from './wall-runs.js';
117
+ export { addBattlement, addMerlonRow, addPortcullis, addRadialMerlons, addRampDeck, addRoundTower, wallGateFrames } from './fort-parts.js';
118
+ export { createGreatWalls, greatGateMouth } from './fort-great-walls.js';
119
+ export { createCityFortification } from './fort-city-walls.js';
120
+ export { buildPortalGateway, portalGatewayLights } from './fort-portal.js';
121
+ export {
122
+ addCrenelRun,
123
+ addDressedSlit,
124
+ buildArchedKeepDoor,
125
+ buildFlaggedWallWalk,
126
+ buildFlaredRoundTower,
127
+ buildKeepCitadel,
128
+ buildKeepTowers,
129
+ } from './fort-keep.js';
130
+ export { buildRingCurtain, buildRingFortification, buildRingGate, buildRingTower } from './fort-ring-wall.js';
131
+ export { buildPolylineWall, buildWallCulverts } from './fort-polyline.js';
132
+ export { buildGateApron, buildGateCauseway } from './fort-gate-approach.js';
133
+ export { createSeawall } from './fort-seawall.js';
134
+ export { TERRACE_DEFAULTS, createTerraceFrame, planTerrace, wallAt } from './fort-terrace-plan.js';
135
+ export { createTerraceWall } from './fort-terrace-wall.js';
136
+ export { createTerraceGates } from './fort-terrace-gates.js';
137
+ export { createRoomKit } from './room-kit.js';
138
+ export { HALL_OLD_STONE_TONE, HALL_STONE_TONE, HALL_WALL_THICKNESS, createStoneHalls } from './room-halls.js';
139
+
140
+ // ── Vessels ──────────────────────────────────────────────────────────────
141
+ export { CRANE_EDGE, HARBOUR_WATERFRONT, craneX, createHarbourKit, quayDeck } from './harbour-kit.js';
142
+ export { createShipKit } from './ship-kit.js';
143
+ export { SHIP_PRESETS } from './ship-presets.js';
144
+
145
+ // ── Figures, animals & fliers ────────────────────────────────────────────
146
+ export { createFigureBuilder } from './figure-body.js';
147
+ export { catBodies, catRig } from './animal-cat-body.js';
148
+ export { FLY_LOOK, flyShape } from './flier-shapes.js';
149
+ export {
150
+ ANT_LOOK,
151
+ BEETLE_LOOK,
152
+ RAT_LOOK,
153
+ SNAKE_LOOK,
154
+ SPIDER_LOOK,
155
+ antGeometry,
156
+ beetleGeometry,
157
+ ratGeometry,
158
+ snakeGeometry,
159
+ spiderGeometry,
160
+ } from './crawl-shapes.js';
161
+ export { GIANT_SPIDER_GAIT, GIANT_SPIDER_LOOK, giantSpiderMoment, giantSpiderMoments } from './crawler-giant-spider.js';
162
+
163
+ // ── Crowds ───────────────────────────────────────────────────────────────
164
+ export { buildCrowdLook, createCrowdLookBuilder } from './crowd-look.js';
165
+ export { farCrowdFigure } from './crowd-far.js';
166
+
167
+ // ── Effects, audio, pacing & runtime ─────────────────────────────────────
168
+ export { STORM_WALL_DEFAULTS, stormWallArrays } from './storm-wall.js';
169
+
170
+ // ── Scene playback, melee, projectiles, beams, props & markers ───────────
171
+ export { buildArrowGeometry, buildKnifeGeometry, buildQuarrelGeometry } from './projectile-shapes.js';
@@ -6,8 +6,12 @@
6
6
  */
7
7
 
8
8
  import * as THREE from 'three';
9
+ import { createNoise } from '../math/noise.js';
9
10
  import { mulberry32 } from '../math/random.js';
10
- import { TAU } from '../math/scalar.js';
11
+ import { TAU, sstep } from '../math/scalar.js';
12
+
13
+ let fallbackVnoise = null;
14
+ const defaultVnoise = () => (fallbackVnoise ??= createNoise().vnoise);
11
15
 
12
16
  /**
13
17
  * Default tuft blade, stem and petal slot counts for {@link tuftGeometry}.
@@ -136,3 +140,66 @@ export function tuftGeometry(options = {}) {
136
140
  g.setIndex(index);
137
141
  return g;
138
142
  }
143
+
144
+ /** Default spatial frequency (cycles per metre) for wildflower drift noise in {@link createFlowerDrift}. */
145
+ export const DEFAULT_DRIFT_SCALE = 1 / 30;
146
+
147
+ /**
148
+ * Default drift parameters (`seed`, noise threshold `from`, peak tuft `share`) per built-in flower
149
+ * or seed-head species in {@link FLOWER_KINDS}.
150
+ */
151
+ export const DEFAULT_FLOWER_DRIFTS = Object.freeze({
152
+ poppy: Object.freeze({ seed: 41, from: 0.56, share: 0.7 }),
153
+ cornflower: Object.freeze({ seed: 42, from: 0.64, share: 0.3 }),
154
+ daisy: Object.freeze({ seed: 43, from: 0.6, share: 0.6 }),
155
+ buttercup: Object.freeze({ seed: 44, from: 0.6, share: 0.6 }),
156
+ yarrow: Object.freeze({ seed: 46, from: 0.6, share: 0.7 }),
157
+ tansy: Object.freeze({ seed: 47, from: 0.64, share: 0.6 }),
158
+ seeds: Object.freeze({ seed: 45, from: 0.5, share: 0.5 }),
159
+ });
160
+
161
+ /**
162
+ * Create a noise-driven wildflower drift sampler that evaluates per-species drift density at
163
+ * `(x, z)` and packs a chosen species and head count (`kind * 4 + heads`, or `0` for plain grass)
164
+ * into the `aFlower` attribute read by `rendering/plant-tuft.js`. Zero per-call allocation.
165
+ *
166
+ * @param {object} [options]
167
+ * @param {(x: number, z: number, salt?: number) => number} [options.vnoise] - Seeded value noise in `[0, 1)`.
168
+ * @param {Record<string, { seed: number, from: number, share: number }>} [options.drifts=DEFAULT_FLOWER_DRIFTS] - Drift table by species name.
169
+ * @param {number} [options.scale=DEFAULT_DRIFT_SCALE] - Noise coordinate scale in cycles per metre.
170
+ * @param {Record<string, number>} [options.kinds=FLOWER_KINDS] - Mapping from species name to 1-based shader flower index.
171
+ * @param {number} [options.ramp=0.2] - Smoothstep noise width from `from` to full drift `share`.
172
+ * @param {number} [options.maxHeads=3] - Maximum flower heads encoded per tuft (`1..maxHeads`).
173
+ * @returns {{
174
+ * driftOf: (kind: string, x: number, z: number) => number,
175
+ * pickFlower: (candidates: readonly string[] | null | undefined, x: number, z: number, pick: number) => number,
176
+ * }}
177
+ */
178
+ export function createFlowerDrift(options = {}) {
179
+ const {
180
+ vnoise = defaultVnoise(),
181
+ drifts = DEFAULT_FLOWER_DRIFTS,
182
+ scale = DEFAULT_DRIFT_SCALE,
183
+ kinds = FLOWER_KINDS,
184
+ ramp = 0.2,
185
+ maxHeads = 3,
186
+ } = options;
187
+
188
+ function driftOf(kind, x, z) {
189
+ const d = drifts[kind];
190
+ return d ? d.share * sstep(d.from, d.from + ramp, vnoise(x * scale, z * scale, d.seed)) : 0;
191
+ }
192
+
193
+ function pickFlower(candidates, x, z, pick) {
194
+ if (!candidates) return 0;
195
+ let below = 0;
196
+ for (let i = 0; i < candidates.length; i++) {
197
+ const kind = candidates[i];
198
+ below += driftOf(kind, x, z);
199
+ if (pick < below) return kinds[kind] * 4 + 1 + (Math.floor(pick * 1e4) % maxHeads);
200
+ }
201
+ return 0;
202
+ }
203
+
204
+ return { driftOf, pickFlower };
205
+ }
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Underground and vaulted stone-hall masonry builders: coursed stone walls with lintelled doorways
3
+ * and walk colliders, flagstone floors with optional cutouts, beam-and-slab flat ceilings, ribbed
4
+ * barrel vaults, eight-sided chambers, and corbelled stone roofs. Worker-safe.
5
+ * @module
6
+ */
7
+
8
+ import { dim } from '../math/color.js';
9
+ import { createHash } from '../math/random.js';
10
+ import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
11
+
12
+ let fallbackShapes = null;
13
+ const defaultShapes = () => (fallbackShapes ??= createUnitShapes());
14
+ let fallbackHash = null;
15
+ const defaultHash = () => (fallbackHash ??= createHash(42));
16
+
17
+ /** Default thickness of a coursed stone hall wall in metres. */
18
+ export const HALL_WALL_THICKNESS = 0.8;
19
+
20
+ /** Default pale dressed limestone sRGB hex colour for stone halls. */
21
+ export const HALL_STONE_TONE = 0xb9ad94;
22
+
23
+ /** Default darker stone sRGB hex colour for older underground chambers and corbelled roofs. */
24
+ export const HALL_OLD_STONE_TONE = 0x8a7c68;
25
+
26
+ /**
27
+ * Create a set of stone-hall masonry builders bound to a world's hash, unit shapes, transform
28
+ * helper and default wall thickness and tones.
29
+ *
30
+ * @param {object} [options]
31
+ * @param {(i: number, j: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
32
+ * @param {{ box: import('three').BufferGeometry }} [options.shapes] - Unit shapes providing `box` for vault voussoirs.
33
+ * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
34
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
35
+ * @param {number} [options.wallThickness=HALL_WALL_THICKNESS] - Wall thickness in metres.
36
+ * @param {number} [options.tone=HALL_STONE_TONE] - Default stone sRGB hex colour for walls, floors, slab ceilings and barrel vaults.
37
+ * @param {number} [options.oldTone=HALL_OLD_STONE_TONE] - Default darker stone sRGB hex colour for octagonal chambers and corbel roofs.
38
+ * @returns {{
39
+ * wall: (K: object, a: [number, number], b: [number, number], y: number, h: number, gaps?: Array<{ at: number, w: number, h: number }>, stoneTone?: number, course?: number) => void,
40
+ * flags: (K: object, x0: number, x1: number, z0: number, z1: number, y: number, size?: number, stoneTone?: number, skip?: ((x: number, z: number) => boolean) | null) => void,
41
+ * slabCeiling: (K: object, x0: number, x1: number, z0: number, z1: number, y: number, stoneTone?: number) => void,
42
+ * barrelVault: (K: object, x0: number, x1: number, z0: number, z1: number, y: number, rise: number, stoneTone?: number) => void,
43
+ * octagonWalls: (K: object, c: { x: number, z: number }, r: number, y: number, h: number, doors?: Record<number, { w: number, h: number }>, stoneTone?: number) => void,
44
+ * corbelRoof: (K: object, c: { x: number, z: number }, r: number, y: number, stoneTone?: number) => void,
45
+ * }}
46
+ */
47
+ export function createStoneHalls(options = {}) {
48
+ const {
49
+ hash = defaultHash(),
50
+ shapes = defaultShapes(),
51
+ color = defaultColor,
52
+ transform = defaultTransform,
53
+ wallThickness = HALL_WALL_THICKNESS,
54
+ tone: defaultTone = HALL_STONE_TONE,
55
+ oldTone: defaultOldTone = HALL_OLD_STONE_TONE,
56
+ } = options;
57
+
58
+ /**
59
+ * Build a wall of coursed stone blocks along the centre line from `a` to `b` (`[x, z]` in metres),
60
+ * standing on elevation `y` and `h` metres high. Doorways in `gaps` (`{ at, w, h }`) receive stone
61
+ * jambs and lintels and are carved out of the box colliders pushed to `K.solid`.
62
+ */
63
+ function wall(K, a, b, y, h, gaps = [], stoneTone = defaultTone, course = 0.52) {
64
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]),
65
+ ux = (b[0] - a[0]) / len,
66
+ uz = (b[1] - a[1]) / len,
67
+ ry = -Math.atan2(uz, ux),
68
+ rows = Math.max(1, Math.round(h / course)),
69
+ rise = h / rows,
70
+ seed = Math.round(a[0] * 31 + a[1] * 17 + b[0] * 7 + b[1] * 3),
71
+ stone = color(stoneTone),
72
+ lay = (s0, s1, y0, sy, t, k) =>
73
+ K.B.stone.box(a[0] + ux * ((s0 + s1) / 2), y0, a[1] + uz * ((s0 + s1) / 2), s1 - s0, sy, t, dim(stone, k), ry);
74
+ for (let i = 0; i < rows; i++) {
75
+ const y0 = y + i * rise;
76
+ let s = i % 2 ? -0.5 : 0;
77
+ while (s < len - 0.01) {
78
+ const s0 = Math.max(0, s),
79
+ gap = gaps.find(g => y0 - y < g.h - 0.02 && s + 0.85 > g.at - g.w / 2 + 0.01 && s0 < g.at + g.w / 2 - 0.01);
80
+ if (gap) {
81
+ if (s0 < gap.at - gap.w / 2 - 0.05) lay(s0, gap.at - gap.w / 2, y0, rise - 0.015, wallThickness, 0.86);
82
+ s = gap.at + gap.w / 2;
83
+ continue;
84
+ }
85
+ let e = Math.min(len, s + 0.85 + 0.5 * hash(i, Math.round(s * 10), seed));
86
+ const next = gaps.find(g => y0 - y < g.h - 0.02 && e > g.at - g.w / 2 && s0 < g.at - g.w / 2);
87
+ if (next) e = next.at - next.w / 2;
88
+ else if (len - e < 0.3) e = len;
89
+ const worn = hash(Math.round(s * 10), i, seed + 1);
90
+ lay(s0, e - 0.015, y0, rise - 0.015, wallThickness + (worn - 0.5) * 0.05, 0.8 + 0.22 * worn - (i === 0 ? 0.08 : 0));
91
+ s = e;
92
+ }
93
+ }
94
+ for (const g of gaps) {
95
+ for (const sg of [-1, 1]) {
96
+ lay(g.at + sg * (g.w / 2 + 0.11) - 0.11, g.at + sg * (g.w / 2 + 0.11) + 0.11, y, g.h, wallThickness + 0.1, 1.04);
97
+ }
98
+ lay(g.at - g.w / 2 - 0.4, g.at + g.w / 2 + 0.4, y + g.h, 0.4, wallThickness + 0.12, 1.08);
99
+ }
100
+ // Walk colliders cover the wall segments less its doorways.
101
+ if (K.solid) {
102
+ const ox = K.O?.x ?? 0,
103
+ oz = K.O?.z ?? 0,
104
+ edges = [0, ...gaps.flatMap(g => [g.at - g.w / 2, g.at + g.w / 2]).sort((p, q) => p - q), len];
105
+ for (let i = 0; i < edges.length; i += 2) {
106
+ const m = (edges[i] + edges[i + 1]) / 2;
107
+ if (edges[i + 1] - edges[i] > 0.05) {
108
+ K.solid.push({ x: ox + a[0] + ux * m, z: oz + a[1] + uz * m, hx: (edges[i + 1] - edges[i]) / 2, hz: wallThickness / 2, rot: ry });
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Lay a floor of square stone flags over `(x0, z0)..(x1, z1)` with their top surfaces at `y`.
116
+ * An optional `skip(x, z)` callback omits individual flags for drains, pits or stair wells.
117
+ */
118
+ function flags(K, x0, x1, z0, z1, y, size = 1, stoneTone = defaultTone, skip = null) {
119
+ const nx = Math.round((x1 - x0) / size),
120
+ nz = Math.round((z1 - z0) / size),
121
+ sx = (x1 - x0) / nx,
122
+ sz = (z1 - z0) / nz;
123
+ for (let i = 0; i < nx; i++) {
124
+ for (let j = 0; j < nz; j++) {
125
+ const x = x0 + (i + 0.5) * sx,
126
+ z = z0 + (j + 0.5) * sz,
127
+ k = hash(i + Math.round(x0), j + Math.round(z0), 771);
128
+ if (skip && skip(x, z)) continue;
129
+ K.B.stone.box(x, y - 0.3, z, sx - 0.02, 0.3 - (k < 0.12 ? 0.02 : 0), sz - 0.02, dim(color(stoneTone), 0.66 + 0.2 * k));
130
+ }
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Build a flat stone ceiling at elevation `y` over `(x0, z0)..(x1, z1)`: cross-beams spanning the
136
+ * narrower dimension carrying roof slabs laid above them.
137
+ */
138
+ function slabCeiling(K, x0, x1, z0, z1, y, stoneTone = defaultTone) {
139
+ const alongX = x1 - x0 > z1 - z0,
140
+ run = alongX ? x1 - x0 : z1 - z0,
141
+ n = Math.max(1, Math.round(run / 2.2));
142
+ for (let i = 0; i <= n; i++) {
143
+ const s = (i / n) * run;
144
+ if (alongX) K.B.stone.box(x0 + s, y - 0.34, (z0 + z1) / 2, 0.42, 0.34, z1 - z0 + 0.5, dim(color(stoneTone), 0.6));
145
+ else K.B.stone.box((x0 + x1) / 2, y - 0.34, z0 + s, x1 - x0 + 0.5, 0.34, 0.42, dim(color(stoneTone), 0.6));
146
+ }
147
+ for (let i = 0; i < n; i++) {
148
+ const s = ((i + 0.5) / n) * run,
149
+ k = 0.5 + 0.12 * hash(i, Math.round(x0 + z0), 772);
150
+ if (alongX) K.B.stone.box(x0 + s, y, (z0 + z1) / 2, run / n - 0.02, 0.4, z1 - z0 + 0.6, dim(color(stoneTone), k));
151
+ else K.B.stone.box((x0 + x1) / 2, y, z0 + s, x1 - x0 + 0.6, 0.4, run / n - 0.02, dim(color(stoneTone), k));
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Build a stone barrel vault running along `x` from `x0` to `x1` over the span `z0..z1`, springing
157
+ * at elevation `y` and rising by `rise` metres, with transverse ribs proud of the voussoirs.
158
+ */
159
+ function barrelVault(K, x0, x1, z0, z1, y, rise, stoneTone = defaultTone) {
160
+ const half = (z1 - z0) / 2,
161
+ zc = (z0 + z1) / 2,
162
+ N = 14,
163
+ point = f => [zc + half * Math.cos(f), y + rise * Math.sin(f)],
164
+ ribs = Math.round((x1 - x0) / 4.5),
165
+ span = (xa, xb, proud, k0) => {
166
+ for (let j = 0; j < N; j++) {
167
+ const [za, ya] = point((j / N) * Math.PI),
168
+ [zb, yb] = point(((j + 1) / N) * Math.PI),
169
+ lenZ = Math.hypot(zb - za, yb - ya),
170
+ fm = ((j + 0.5) / N) * Math.PI,
171
+ nz = Math.cos(fm) / half,
172
+ ny = Math.sin(fm) / rise,
173
+ n = Math.hypot(nz, ny),
174
+ tilt = Math.atan2(nz / n, ny / n);
175
+ K.B.stone.add(
176
+ shapes.box,
177
+ dim(color(stoneTone), k0 + 0.1 * hash(j, Math.round(xa * 3), 773)),
178
+ transform(
179
+ (xa + xb) / 2,
180
+ (ya + yb) / 2 - (proud * ny) / n,
181
+ (za + zb) / 2 - (proud * nz) / n,
182
+ xb - xa - 0.02,
183
+ 0.5,
184
+ lenZ + 0.06,
185
+ 0,
186
+ tilt,
187
+ ),
188
+ );
189
+ }
190
+ };
191
+ for (let i = 0; i < ribs; i++) span(x0 + (i / ribs) * (x1 - x0), x0 + ((i + 1) / ribs) * (x1 - x0), 0, 0.5);
192
+ for (let i = 0; i <= ribs; i++) {
193
+ const x = Math.min(x1 - 0.25, Math.max(x0 + 0.25, x0 + (i / ribs) * (x1 - x0)));
194
+ span(x - 0.25, x + 0.25, 0.22, 0.7);
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Build the eight coursed walls of an octagonal chamber centred at `c` (`{ x, z }`) with apothem
200
+ * `r` metres to the inner face. `doors` maps side index (`0` east, `2` south, `4` west, `6` north)
201
+ * to `{ w, h }`.
202
+ */
203
+ function octagonWalls(K, c, r, y, h, doors = {}, stoneTone = defaultOldTone) {
204
+ const mid = r + wallThickness / 2,
205
+ half = (r + wallThickness) * Math.tan(Math.PI / 8);
206
+ for (let k = 0; k < 8; k++) {
207
+ const t = (k * Math.PI) / 4,
208
+ cx = c.x + Math.cos(t) * mid,
209
+ cz = c.z + Math.sin(t) * mid,
210
+ dx = -Math.sin(t),
211
+ dz = Math.cos(t),
212
+ gap = doors[k];
213
+ wall(K, [cx - dx * half, cz - dz * half], [cx + dx * half, cz + dz * half], y, h, gap ? [{ at: half, ...gap }] : [], stoneTone, 0.6);
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Build a seven-ring stepped corbelled stone roof over an octagonal chamber centred at `c` with
219
+ * apothem `r` metres, springing at elevation `y` and closed by a top capstone.
220
+ */
221
+ function corbelRoof(K, c, r, y, stoneTone = defaultOldTone) {
222
+ const rings = 7;
223
+ for (let i = 0; i < rings; i++) {
224
+ const ri = r + 0.5 - i * (r / rings),
225
+ half = (ri + 0.6) * Math.tan(Math.PI / 8);
226
+ for (let k = 0; k < 8; k++) {
227
+ const t = (k * Math.PI) / 4;
228
+ K.B.stone.box(
229
+ c.x + Math.cos(t) * ri,
230
+ y + i * 0.46,
231
+ c.z + Math.sin(t) * ri,
232
+ 1.9,
233
+ 0.5,
234
+ 2 * half,
235
+ dim(color(stoneTone), 0.62 - i * 0.03 + 0.08 * hash(i, k, 774)),
236
+ -t,
237
+ );
238
+ }
239
+ }
240
+ K.B.stone.box(c.x, y + rings * 0.46, c.z, 2.6, 0.4, 2.6, dim(color(stoneTone), 0.4), Math.PI / 8);
241
+ }
242
+
243
+ return { wall, flags, slabCeiling, barrelVault, octagonWalls, corbelRoof };
244
+ }
@@ -8,6 +8,7 @@
8
8
  import { dim } from '../math/color.js';
9
9
  import { createNoise } from '../math/noise.js';
10
10
  import { TAU, lerp, sstep } from '../math/scalar.js';
11
+ import { craterDip as defaultCraterDip } from './crater-profile.js';
11
12
  import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
12
13
  import { sampleSurface } from './sampled-surface.js';
13
14
 
@@ -33,11 +34,7 @@ export const CRATER_PALETTE = Object.freeze({
33
34
 
34
35
  const mix3 = (a, b, k) => [lerp(a[0], b[0], k), lerp(a[1], b[1], k), lerp(a[2], b[2], k)];
35
36
 
36
- function defaultCraterDip(crater, dx, dz, vnoise) {
37
- const d = Math.hypot(dx, dz) || 1e-3;
38
- const t = d / (crater.r * (0.86 + 0.28 * vnoise((dx / d) * 1.6 + crater.x, (dz / d) * 1.6 + crater.z, 381)));
39
- return (t < 1 ? -crater.deep * (1 - t * t) : 0) + crater.deep * 0.42 * Math.exp(-(((t - 1.06) / 0.3) ** 2));
40
- }
37
+ export { defaultCraterDip as craterDip };
41
38
 
42
39
  /**
43
40
  * Build a high-resolution impact crater bowl and thrown-subsoil lip blended cleanly into surrounding
@@ -4,11 +4,15 @@
4
4
  * @module
5
5
  */
6
6
 
7
+ import * as THREE from 'three';
8
+ import { createHash } from '../math/random.js';
7
9
  import { TAU } from '../math/scalar.js';
8
10
  import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
9
11
 
10
12
  let fallbackRock = null;
11
13
  const defaultRock = () => (fallbackRock ??= createUnitShapes().rock);
14
+ let fallbackHash = null;
15
+ const defaultHash = () => (fallbackHash ??= createHash(42));
12
16
 
13
17
  /**
14
18
  * Default tone and weathering settings for heaped fallen masonry built by {@link buildRubbleHeap}.
@@ -38,6 +42,30 @@ export const RUBBLE_SCATTER = Object.freeze({
38
42
  dressed: 0.45,
39
43
  });
40
44
 
45
+ /**
46
+ * Build a cheap, angular faceted rubble-stone `BufferGeometry` by displacing an icosahedron's
47
+ * vertices with a seeded 2D hash and slightly lifting its base so scattered fragments seat cleanly
48
+ * on heaps and broken masonry.
49
+ *
50
+ * @param {object} [options]
51
+ * @param {(x: number, z: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
52
+ * @param {number} [options.radius=0.5] - Base icosahedron radius in metres.
53
+ * @param {number} [options.salt=23] - Hash salt.
54
+ * @returns {THREE.BufferGeometry}
55
+ */
56
+ export function createRubbleStone({ hash = defaultHash(), radius = 0.5, salt = 23 } = {}) {
57
+ const g = new THREE.IcosahedronGeometry(radius, 0);
58
+ const a = g.attributes.position.array;
59
+ for (let i = 0; i < a.length; i += 3) {
60
+ const k = 0.7 + 0.6 * hash(Math.round(a[i] * 9), Math.round(a[i + 2] * 9 + a[i + 1] * 5), salt);
61
+ a[i] *= k;
62
+ a[i + 1] = a[i + 1] * k * 0.8 + 0.3;
63
+ a[i + 2] *= k;
64
+ }
65
+ g.computeVertexNormals();
66
+ return g;
67
+ }
68
+
41
69
  function weatheredTone(hue, random, course, cfg, color) {
42
70
  const col = color(hue, cfg.jitter, random);
43
71
  const k = (course < cfg.dampCourses ? 0.8 + 0.08 * course : 1) * (random() < cfg.stained ? 0.78 : 1);
@@ -101,3 +101,106 @@ export function planScatterInstances({
101
101
  total,
102
102
  };
103
103
  }
104
+
105
+ /**
106
+ * Default grid tiling, LOD transition, scale, lean, tint and trunk collider settings for
107
+ * {@link planTreeGrove} and `scatterTreeGrove`.
108
+ */
109
+ export const TREE_GROVE = Object.freeze({
110
+ /** Grid square edge length in metres used to batch neighbouring trees into one LOD stand. */
111
+ squareSize: 80,
112
+ /** Distance in metres at which each tiled stand switches from near to far geometry. */
113
+ nearDistance: 340,
114
+ /** Vertical sink below the sampled ground surface in metres. */
115
+ sink: 0.15,
116
+ /** Uniform tree scale `[minScale, scaleSpan]`. */
117
+ scaleRange: Object.freeze([0.8, 0.55]),
118
+ /** Maximum trunk pitch lean span in radians (`[-leanSpan/2, +leanSpan/2]`). */
119
+ leanSpan: 0.06,
120
+ /** Greyscale tint multiplier `[minShade, shadeSpan]`. */
121
+ shadeRange: Object.freeze([0.82, 0.3]),
122
+ /** Trunk circle-collider radius per unit scale in metres. */
123
+ colliderRadius: 0.6,
124
+ /** Default number of tree variants built when no prebuilt models are supplied. */
125
+ variants: 3,
126
+ });
127
+
128
+ /**
129
+ * Plan a wood or grove of trees over a rectangle `[x - rx, x + rx] × [z - rz, z + rz]` in a local
130
+ * frame anchored at `origin`, skipping points where `clear(px, pz)` is truthy, binning accepted
131
+ * trees into `squareSize`-metre tiles and recording circle trunk colliders. Worker-safe.
132
+ *
133
+ * @param {object} [options]
134
+ * @param {{ x: number, z: number }} [options.origin={ x: 0, z: 0 }] - Local frame world origin in metres.
135
+ * @param {(x: number, z: number) => number} [options.ground] - Local ground elevation sampler `(px, pz) => y` in metres.
136
+ * @param {() => number} [options.random] - Deterministic PRNG in `[0, 1)`.
137
+ * @param {number} [options.x=0] - Grove centre local X in metres.
138
+ * @param {number} [options.z=0] - Grove centre local Z in metres.
139
+ * @param {number} [options.rx=40] - Grove half-width along X in metres.
140
+ * @param {number} [options.rz=40] - Grove half-depth along Z in metres.
141
+ * @param {number} [options.count=40] - Candidate tree positions to sample.
142
+ * @param {(px: number, pz: number) => boolean} [options.clear] - Exclusion predicate in local coordinates.
143
+ * @param {number} [options.squareSize=TREE_GROVE.squareSize] - Tile square edge length in metres.
144
+ * @param {number} [options.sink=TREE_GROVE.sink] - Vertical sink below `ground(px, pz)` in metres.
145
+ * @param {readonly [number, number]} [options.scaleRange=TREE_GROVE.scaleRange] - Scale `[min, span]`.
146
+ * @param {number} [options.leanSpan=TREE_GROVE.leanSpan] - Trunk lean span in radians.
147
+ * @param {readonly [number, number]} [options.shadeRange=TREE_GROVE.shadeRange] - Greyscale shade `[min, span]`.
148
+ * @param {number} [options.colliderRadius=TREE_GROVE.colliderRadius] - Trunk collider radius per unit scale in metres.
149
+ * @returns {{
150
+ * squares: Array<{ key: number, mid: [number, number, number], trees: Array<[number, number, number, number, number, number, number]> }>,
151
+ * solids: Array<{ x: number, z: number, r: number }>,
152
+ * total: number,
153
+ * }}
154
+ */
155
+ export function planTreeGrove({
156
+ origin = { x: 0, z: 0 },
157
+ ground = () => 0,
158
+ random = mulberry32(1),
159
+ x = 0,
160
+ z = 0,
161
+ rx = 40,
162
+ rz = 40,
163
+ count = 40,
164
+ clear = () => false,
165
+ squareSize = TREE_GROVE.squareSize,
166
+ sink = TREE_GROVE.sink,
167
+ scaleRange = TREE_GROVE.scaleRange,
168
+ leanSpan = TREE_GROVE.leanSpan,
169
+ shadeRange = TREE_GROVE.shadeRange,
170
+ colliderRadius = TREE_GROVE.colliderRadius,
171
+ } = {}) {
172
+ const buckets = new Map();
173
+ const solids = [];
174
+ let total = 0;
175
+ for (let i = 0; i < count; i++) {
176
+ const px = x + (random() * 2 - 1) * rx;
177
+ const pz = z + (random() * 2 - 1) * rz;
178
+ if (clear(px, pz)) continue;
179
+ const size = scaleRange[0] + random() * scaleRange[1];
180
+ const key = Math.floor(px / squareSize) * 1e4 + Math.floor(pz / squareSize);
181
+ let trees = buckets.get(key);
182
+ if (!trees) {
183
+ trees = [];
184
+ buckets.set(key, trees);
185
+ }
186
+ const wx = origin.x + px;
187
+ const wz = origin.z + pz;
188
+ trees.push([
189
+ wx,
190
+ ground(px, pz) - sink,
191
+ wz,
192
+ size,
193
+ random() * TAU,
194
+ (random() - 0.5) * leanSpan,
195
+ shadeRange[0] + random() * shadeRange[1],
196
+ ]);
197
+ solids.push({ x: wx, z: wz, r: colliderRadius * size });
198
+ total++;
199
+ }
200
+ const squares = [];
201
+ for (const [key, trees] of buckets) {
202
+ const mid = trees.reduce((m, t) => [m[0] + t[0] / trees.length, m[1] + t[1] / trees.length, m[2] + t[2] / trees.length], [0, 0, 0]);
203
+ squares.push({ key, mid, trees });
204
+ }
205
+ return { squares, solids, total };
206
+ }