roamwright 0.61.5 → 0.61.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/animals.js +6 -4
- package/examples/crowds.js +4 -3
- package/examples/effects.js +7 -6
- package/examples/figure-motion.js +7 -5
- package/examples/fliers.js +3 -4
- package/examples/fortification.js +4 -3
- package/examples/house-kit.js +9 -3
- package/examples/land-features.js +102 -0
- package/examples/land.html +2 -2
- package/examples/land.js +3 -73
- package/examples/pacing.js +2 -2
- package/examples/rooms.js +13 -8
- package/examples/ships.js +2 -2
- package/examples/small-world-land.js +5 -6
- package/examples/small-world-town.js +9 -3
- package/examples/soundscape.js +6 -6
- package/examples/towns.js +2 -2
- package/geometry/farm-crops.js +142 -0
- package/geometry/farm-props.js +107 -0
- package/geometry/fort-city-walls.js +10 -8
- package/geometry/land-trails.js +110 -0
- package/geometry/scatter-instances.js +103 -0
- package/geometry/stream-springs.js +236 -0
- package/geometry/timber-fence.js +117 -0
- package/index.js +10 -1
- package/package.json +1 -1
- package/rendering/land-trail-surface.js +91 -0
- package/rendering/particles.js +78 -1
- package/rendering/scatter-instances.js +246 -0
- package/rendering/stream-springs.js +66 -0
- package/examples/stage-kits.js +0 -172
package/examples/soundscape.js
CHANGED
|
@@ -8,14 +8,18 @@ import {
|
|
|
8
8
|
createHouseKitMaterials,
|
|
9
9
|
createHouseKitMeshes,
|
|
10
10
|
createOrbitCamera,
|
|
11
|
+
createScriptFoley,
|
|
11
12
|
createShipKit,
|
|
12
13
|
createShipMeshes,
|
|
13
14
|
createSimpleWater,
|
|
15
|
+
createSoundscape,
|
|
16
|
+
createStrideFoley,
|
|
14
17
|
figureActor,
|
|
15
18
|
GeometryBuilder,
|
|
16
19
|
geometryOf,
|
|
17
20
|
mulberry32,
|
|
18
21
|
rigHorse,
|
|
22
|
+
scatterTrees,
|
|
19
23
|
SHIP_PRESETS,
|
|
20
24
|
} from 'roamwright';
|
|
21
25
|
import { sstep } from 'roamwright/math/scalar.js';
|
|
@@ -23,12 +27,8 @@ import { C, TRS } from 'roamwright/geometry/primitives.js';
|
|
|
23
27
|
import { oneOf } from 'roamwright/geometry/ship-kit.js';
|
|
24
28
|
import { createProps } from 'roamwright/geometry/props.js';
|
|
25
29
|
import { createReeds } from 'roamwright/rendering/plant-reeds.js';
|
|
26
|
-
import { createWind } from '
|
|
27
|
-
import { createScriptFoley } from '../runtime/foley-script.js';
|
|
28
|
-
import { createStrideFoley } from '../runtime/foley-strides.js';
|
|
29
|
-
import { createSoundscape } from '../runtime/soundscape.js';
|
|
30
|
+
import { createWind } from 'roamwright/motion/wind.js';
|
|
30
31
|
import { initExample, makeRollingGround } from './stage.js';
|
|
31
|
-
import { scatterTrees } from './stage-kits.js';
|
|
32
32
|
|
|
33
33
|
const { canvas, renderer, camera, scene, worldGroup, seed, random, noise, hash, shapes } = initExample({
|
|
34
34
|
fallbackSeed: 42,
|
|
@@ -143,7 +143,7 @@ createHouseKitMeshes({ materials: createHouseKitMaterials({ doubleSide: true })
|
|
|
143
143
|
|
|
144
144
|
// Trees framing the lakeside meadow and swaying reeds along the water's edge.
|
|
145
145
|
scatterTrees({
|
|
146
|
-
worldGroup,
|
|
146
|
+
parent: worldGroup,
|
|
147
147
|
noise,
|
|
148
148
|
hash,
|
|
149
149
|
seed,
|
package/examples/towns.js
CHANGED
|
@@ -14,13 +14,13 @@ import {
|
|
|
14
14
|
GeometryBuilder,
|
|
15
15
|
layVillage,
|
|
16
16
|
mulberry32,
|
|
17
|
+
scatterTrees,
|
|
17
18
|
} from 'roamwright';
|
|
18
19
|
import { createPieceBatch, TIGHT } from 'roamwright/rendering/piece-batch.js';
|
|
19
20
|
import { C, TRS } from 'roamwright/geometry/primitives.js';
|
|
20
21
|
import { litShare } from 'roamwright/math/lit-share.js';
|
|
21
22
|
import { sstep } from 'roamwright/math/scalar.js';
|
|
22
23
|
import { initExample, makeRollingGround } from './stage.js';
|
|
23
|
-
import { scatterTrees } from './stage-kits.js';
|
|
24
24
|
|
|
25
25
|
const { canvas, renderer, camera, scene, worldGroup, seed, random, noise, hash, shapes } = initExample({
|
|
26
26
|
fallbackSeed: 7,
|
|
@@ -170,7 +170,7 @@ worldGroup.add(wallMesh);
|
|
|
170
170
|
|
|
171
171
|
// Trees framing the pastures around the village.
|
|
172
172
|
scatterTrees({
|
|
173
|
-
worldGroup,
|
|
173
|
+
parent: worldGroup,
|
|
174
174
|
noise,
|
|
175
175
|
hash,
|
|
176
176
|
seed,
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { IRRIGATED_FIELDS } from './irrigated-fields.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default ring grids and placement dimensions (in metres) for standing crop tufts over irrigated fields: three nested rings
|
|
5
|
+
* around the camera (close drilled plants stepping out to wider clumps), drilled row pitch, worked-ground threshold, bund
|
|
6
|
+
* footpath clearance, palm watering-basin clearance, tuft blade height normalization, maximum clump width, edge taper room,
|
|
7
|
+
* and grain-ear edge margin.
|
|
8
|
+
*/
|
|
9
|
+
export const FARM_CROPS = Object.freeze({
|
|
10
|
+
rings: Object.freeze([
|
|
11
|
+
Object.freeze({ cell: 0.45, half: 20, refill: 2 }),
|
|
12
|
+
Object.freeze({ cell: 0.9, half: 24, refill: 4 }),
|
|
13
|
+
Object.freeze({ cell: 1.6, half: 25, refill: 8 }),
|
|
14
|
+
]),
|
|
15
|
+
rows: 0.69,
|
|
16
|
+
worked: 0.5,
|
|
17
|
+
path: 0.14,
|
|
18
|
+
bundExtra: 0.1,
|
|
19
|
+
basinClear: 1.7,
|
|
20
|
+
sink: 0.03,
|
|
21
|
+
blade: 0.82,
|
|
22
|
+
widest: 1.3,
|
|
23
|
+
room: Object.freeze([0.3, 1.2]),
|
|
24
|
+
earRoom: 1.2,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Default crop stand profiles (density share, width multiplier, max height in metres, linear RGB colour, optional drilled row
|
|
29
|
+
* pitch in metres and grain-ear flag) for each irrigated parcel state (`crop`, `ripe`, `cut`, `old`, `garden`, `grove`,
|
|
30
|
+
* `verge`, `bund`). Callers supply their own table to match a world's ground palette and crops.
|
|
31
|
+
*/
|
|
32
|
+
export const DEFAULT_CROP_STANDS = Object.freeze({
|
|
33
|
+
crop: Object.freeze({ share: 1, wide: 1.1, high: 0.62, tone: Object.freeze([0.16, 0.24, 0.08]), rows: FARM_CROPS.rows }),
|
|
34
|
+
ripe: Object.freeze({ share: 1, wide: 1.1, high: 0.95, tone: Object.freeze([0.42, 0.31, 0.12]), ears: true, rows: FARM_CROPS.rows }),
|
|
35
|
+
cut: Object.freeze({ share: 0.6, wide: 0.6, high: 0.16, tone: Object.freeze([0.34, 0.26, 0.13]), rows: FARM_CROPS.rows }),
|
|
36
|
+
old: Object.freeze({ share: 0.4, wide: 0.8, high: 0.3, tone: Object.freeze([0.29, 0.22, 0.11]) }),
|
|
37
|
+
garden: Object.freeze({ share: 0.85, wide: 0.75, high: 0.34, tone: Object.freeze([0.14, 0.22, 0.09]) }),
|
|
38
|
+
grove: Object.freeze({ share: 0.2, wide: 0.7, high: 0.4, tone: Object.freeze([0.15, 0.21, 0.08]) }),
|
|
39
|
+
verge: Object.freeze({ share: 0.75, wide: 0.9, high: 0.85, tone: Object.freeze([0.15, 0.2, 0.06]) }),
|
|
40
|
+
bund: Object.freeze({ share: 0.45, wide: 0.6, high: 0.3, tone: Object.freeze([0.3, 0.25, 0.13]) }),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a zero-allocation crop-tuft placer (`cropAt`) and stand resolver (`standOf`) for irrigated parcels built with
|
|
45
|
+
* `createIrrigatedFields`, ready to pass directly into a scatter grid or `tuftPatch`.
|
|
46
|
+
*
|
|
47
|
+
* @param {object} options
|
|
48
|
+
* @param {(x: number, z: number) => object} options.fieldAt - Queries the irrigated field state at `(x, z)` metres.
|
|
49
|
+
* @param {() => number} options.gardenBed - Evaluates the garden bed value at the last queried field point (`< bedBare` is bare/furrow).
|
|
50
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height sampler at `(x, z)` in metres.
|
|
51
|
+
* @param {typeof DEFAULT_CROP_STANDS} [options.stands=DEFAULT_CROP_STANDS] - Per-state crop stand definitions and linear RGB tones.
|
|
52
|
+
* @param {number} [options.ears=0] - Encoded flower/ear attribute value written to `tuft.flower` on ripe stands clear of parcel edges.
|
|
53
|
+
* @param {Partial<typeof IRRIGATED_FIELDS>} [options.fieldShape=IRRIGATED_FIELDS] - Parcel geometry thresholds (`bank`, `bedBare`, `bund`, `palmPitch`, `verge`).
|
|
54
|
+
* @param {Partial<typeof FARM_CROPS>} [options.shape] - Overrides for {@link FARM_CROPS}.
|
|
55
|
+
* @returns {{
|
|
56
|
+
* cropAt: (x: number, z: number, h1: number, h2: number, ix: number, iz: number, tuft: object, cell: number) => boolean,
|
|
57
|
+
* standOf: (field: object) => object | null,
|
|
58
|
+
* rings: readonly { cell: number, half: number, refill: number }[]
|
|
59
|
+
* }}
|
|
60
|
+
*/
|
|
61
|
+
export function createCropPlacer({
|
|
62
|
+
fieldAt,
|
|
63
|
+
gardenBed,
|
|
64
|
+
ground,
|
|
65
|
+
stands = DEFAULT_CROP_STANDS,
|
|
66
|
+
ears = 0,
|
|
67
|
+
fieldShape = IRRIGATED_FIELDS,
|
|
68
|
+
shape = {},
|
|
69
|
+
}) {
|
|
70
|
+
const bank = fieldShape.bank ?? IRRIGATED_FIELDS.bank,
|
|
71
|
+
bedBare = fieldShape.bedBare ?? IRRIGATED_FIELDS.bedBare,
|
|
72
|
+
bund = fieldShape.bund ?? IRRIGATED_FIELDS.bund,
|
|
73
|
+
palmPitch = fieldShape.palmPitch ?? IRRIGATED_FIELDS.palmPitch,
|
|
74
|
+
verge = fieldShape.verge ?? IRRIGATED_FIELDS.verge;
|
|
75
|
+
const rings = shape.rings ?? FARM_CROPS.rings,
|
|
76
|
+
worked = shape.worked ?? FARM_CROPS.worked,
|
|
77
|
+
path = shape.path ?? FARM_CROPS.path,
|
|
78
|
+
bundClear = bund + (shape.bundExtra ?? FARM_CROPS.bundExtra),
|
|
79
|
+
basinClear = shape.basinClear ?? FARM_CROPS.basinClear,
|
|
80
|
+
basinClearSq = basinClear * basinClear,
|
|
81
|
+
sink = shape.sink ?? FARM_CROPS.sink,
|
|
82
|
+
blade = shape.blade ?? FARM_CROPS.blade,
|
|
83
|
+
widest = shape.widest ?? FARM_CROPS.widest,
|
|
84
|
+
room = shape.room ?? FARM_CROPS.room,
|
|
85
|
+
earRoom = shape.earRoom ?? FARM_CROPS.earRoom;
|
|
86
|
+
|
|
87
|
+
// What stands in a parcel worked as `field` says, at the point it was asked about: one of `stands`, or null.
|
|
88
|
+
function standOf(field) {
|
|
89
|
+
switch (field.use) {
|
|
90
|
+
case 'crop':
|
|
91
|
+
return stands.crop;
|
|
92
|
+
case 'ripe':
|
|
93
|
+
return field.work > 0 ? stands.cut : stands.ripe;
|
|
94
|
+
case 'stubble':
|
|
95
|
+
return stands.cut;
|
|
96
|
+
case 'tilled':
|
|
97
|
+
return field.work > 0 ? null : stands.old; // nothing on a turned furrow
|
|
98
|
+
case 'garden':
|
|
99
|
+
return gardenBed() < bedBare ? null : stands.garden; // nor in a bed's furrow, or a bed left bare
|
|
100
|
+
default: {
|
|
101
|
+
// A grove: keep each palm's watering basin clear.
|
|
102
|
+
const a = (Math.floor(field.a / palmPitch) + 0.5) * palmPitch - field.a,
|
|
103
|
+
b = (Math.floor(field.b / palmPitch) + 0.5) * palmPitch - field.b;
|
|
104
|
+
return a * a + b * b < basinClearSq ? null : stands.grove;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Decide whether a crop tuft stands in a cell at (x, z) of width `cell` metres and write its transform/colour into `tuft`.
|
|
110
|
+
function cropAt(x, z, h1, h2, ix, iz, tuft, cell) {
|
|
111
|
+
const field = fieldAt(x, z);
|
|
112
|
+
if (field.net < worked) return false;
|
|
113
|
+
let stand = stands.verge;
|
|
114
|
+
if (field.water < verge) {
|
|
115
|
+
if (field.water < bank) return false; // a canal's water and its dug bank
|
|
116
|
+
} else if (field.edge < bundClear || field.end < bundClear) {
|
|
117
|
+
if (Math.min(field.edge, field.end) < path) return false;
|
|
118
|
+
stand = stands.bund;
|
|
119
|
+
} else if (field.k < worked) return false;
|
|
120
|
+
else stand = standOf(field);
|
|
121
|
+
if (!stand || h2 > stand.share) return false;
|
|
122
|
+
if (stand.rows && cell < stand.rows) {
|
|
123
|
+
// Drilled grain rows where the grid is finer than the row pitch: step across the parcel to the nearest row.
|
|
124
|
+
const step = Math.round(field.a / stand.rows) * stand.rows - field.a;
|
|
125
|
+
tuft.x = x + step * field.axes[0];
|
|
126
|
+
tuft.z = z + step * field.axes[1];
|
|
127
|
+
}
|
|
128
|
+
const edgeRoom = stand === stands.verge || stand === stands.bund ? widest : room[0] + room[1] * Math.min(field.edge, field.end),
|
|
129
|
+
wide = Math.min(widest, edgeRoom, cell * stand.wide * (0.8 + h1 * 0.4)),
|
|
130
|
+
shade = 0.85 + h1 * 0.35;
|
|
131
|
+
tuft.y = ground(tuft.x, tuft.z) - sink;
|
|
132
|
+
tuft.wide = wide;
|
|
133
|
+
tuft.tall = (stand.high * (0.88 + h1 * 0.24)) / blade;
|
|
134
|
+
tuft.r = stand.tone[0] * shade;
|
|
135
|
+
tuft.g = stand.tone[1] * shade;
|
|
136
|
+
tuft.b = stand.tone[2] * shade;
|
|
137
|
+
tuft.flower = stand.ears && Math.min(field.edge, field.end) > earRoom ? ears : 0;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { cropAt, standOf, rings };
|
|
142
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { dim } from '../math/color.js';
|
|
2
|
+
import { createNoise } from '../math/noise.js';
|
|
3
|
+
import { TAU } from '../math/scalar.js';
|
|
4
|
+
import { C, TRS, box, createUnitShapes, stick } from './primitives.js';
|
|
5
|
+
import { sampleSurface } from './sampled-surface.js';
|
|
6
|
+
|
|
7
|
+
let fallbackRock = null;
|
|
8
|
+
const defaultRock = () => (fallbackRock ??= createUnitShapes().rock);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Default proportions, mesh resolution and straw colours (sRGB hex) for stacked farmyard haystacks built by
|
|
12
|
+
* {@link buildHaystacks}. Dimensions are in metres.
|
|
13
|
+
*/
|
|
14
|
+
export const FARM_HAYSTACK = Object.freeze({
|
|
15
|
+
count: 2,
|
|
16
|
+
spacing: 3.05,
|
|
17
|
+
offsetX: -1.1,
|
|
18
|
+
height: Object.freeze([1.75, 0.65]),
|
|
19
|
+
radius: Object.freeze([1.32, 0.2]),
|
|
20
|
+
strawColor: 0xa89255,
|
|
21
|
+
strawJitter: 0.12,
|
|
22
|
+
strandColors: Object.freeze([0xa59157, 0xc8ad6e]),
|
|
23
|
+
strandJitter: 0.13,
|
|
24
|
+
strands: 126,
|
|
25
|
+
columns: 29,
|
|
26
|
+
rows: 18,
|
|
27
|
+
noiseSeed: 370,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Default stone count, footprint radii, diameter range (in metres) and sRGB stone colours for field-cleared stone piles built
|
|
32
|
+
* by {@link buildStonePile}.
|
|
33
|
+
*/
|
|
34
|
+
export const FARM_STONE_PILE = Object.freeze({
|
|
35
|
+
count: 22,
|
|
36
|
+
radiusX: 1.4,
|
|
37
|
+
radiusZ: 1.05,
|
|
38
|
+
size: Object.freeze([0.3, 0.55]),
|
|
39
|
+
colors: Object.freeze([0x544935, 0x7d6e53]),
|
|
40
|
+
colorJitter: 0.24,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Build one or more tall farmyard haystacks (by default a pair side by side) with weathered, bulging straw bodies and loose
|
|
45
|
+
* projecting straw wisps into a geometry builder.
|
|
46
|
+
*
|
|
47
|
+
* @param {Pick<import('./builder.js').GB, 'raw' | 'add'>} builder - Destination geometry builder (e.g. a kit's cloth builder).
|
|
48
|
+
* @param {() => number} [random=Math.random] - Repeatable random stream in `[0, 1)`.
|
|
49
|
+
* @param {object} [options]
|
|
50
|
+
* @param {(x: number, z: number, seed: number) => number} [options.noise] - Seeded value noise (`createNoise().vnoise`).
|
|
51
|
+
* @param {import('three').BufferGeometry} [options.box] - Unit box geometry for loose straw wisps (defaults to `primitives.box`).
|
|
52
|
+
* @param {Partial<typeof FARM_HAYSTACK>} [options.shape] - Overrides for {@link FARM_HAYSTACK}.
|
|
53
|
+
*/
|
|
54
|
+
export function buildHaystacks(builder, random = Math.random, options = {}) {
|
|
55
|
+
const noise = options.noise ?? createNoise().vnoise,
|
|
56
|
+
strandBox = options.box ?? box,
|
|
57
|
+
cfg = options.shape ? { ...FARM_HAYSTACK, ...options.shape } : FARM_HAYSTACK,
|
|
58
|
+
{ count, spacing, offsetX, height, radius, strawColor, strawJitter, strandColors, strandJitter, strands, columns, rows, noiseSeed } =
|
|
59
|
+
cfg;
|
|
60
|
+
for (let i = 0; i < count; i++) {
|
|
61
|
+
const px = i * spacing + offsetX,
|
|
62
|
+
h = height[0] + random() * height[1],
|
|
63
|
+
wide = radius[0] + random() * radius[1];
|
|
64
|
+
const straw = C(strawColor, strawJitter, random);
|
|
65
|
+
const fibre = (u, v, offset = 0) => {
|
|
66
|
+
const a = u * TAU,
|
|
67
|
+
sn = Math.sin(a),
|
|
68
|
+
cs = Math.cos(a);
|
|
69
|
+
const profile = v < 0.45 ? 0.87 + 0.29 * Math.sin(v * 3.1) : 1.14 * ((1 - v) / 0.55) ** 0.73;
|
|
70
|
+
const r = wide * profile * (0.96 + 0.09 * noise(4 * cs + i * 9, 4 * sn + 7 * v, noiseSeed)) + offset;
|
|
71
|
+
return [px + cs * r - v * 0.21, h * v - 0.15 + 0.07 * sn * (1 - v), sn * r * 0.84 + v * 0.16];
|
|
72
|
+
};
|
|
73
|
+
sampleSurface(builder, columns, rows, fibre, (u, v) => dim(straw, (0.88 + random() * 0.22) * (0.73 + 0.29 * Math.sin(v * 1.9))));
|
|
74
|
+
for (let j = 0; j < strands; j++) {
|
|
75
|
+
const u = random(),
|
|
76
|
+
v = 0.04 + random() * 0.87,
|
|
77
|
+
strand = C(j % 3 ? strandColors[0] : strandColors[1], strandJitter, random);
|
|
78
|
+
stick(builder, fibre(u, v, 0.025), fibre(u + (random() - 0.56) * 0.044, v + 0.035, random() * 0.09), 0.011, strand, strandBox);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Build a low mound of field-cleared stones into a geometry builder, piling stones higher toward the middle and scattering
|
|
85
|
+
* them wider around the base.
|
|
86
|
+
*
|
|
87
|
+
* @param {Pick<import('./builder.js').GB, 'add'>} builder - Destination geometry builder (e.g. a kit's wall/stone builder).
|
|
88
|
+
* @param {() => number} [random=Math.random] - Repeatable random stream in `[0, 1)`.
|
|
89
|
+
* @param {object} [options]
|
|
90
|
+
* @param {import('three').BufferGeometry} [options.rock] - Unit broken stone geometry (defaults to `createUnitShapes().rock`).
|
|
91
|
+
* @param {Partial<typeof FARM_STONE_PILE>} [options.shape] - Overrides for {@link FARM_STONE_PILE}.
|
|
92
|
+
*/
|
|
93
|
+
export function buildStonePile(builder, random = Math.random, options = {}) {
|
|
94
|
+
const rock = options.rock ?? defaultRock(),
|
|
95
|
+
cfg = options.shape ? { ...FARM_STONE_PILE, ...options.shape } : FARM_STONE_PILE,
|
|
96
|
+
{ count, radiusX, radiusZ, size, colors, colorJitter } = cfg;
|
|
97
|
+
for (let i = 0; i < count; i++) {
|
|
98
|
+
const a = random() * TAU,
|
|
99
|
+
r = Math.sqrt(random()),
|
|
100
|
+
d = size[0] + random() * size[1];
|
|
101
|
+
builder.add(
|
|
102
|
+
rock,
|
|
103
|
+
C(i % 6 ? colors[0] : colors[1], colorJitter, random),
|
|
104
|
+
TRS(Math.cos(a) * r * radiusX, 0.05 + (1 - r) * 0.46, Math.sin(a) * r * radiusZ, d * 1.55, d * 0.7, d, a, random(), random()),
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -53,10 +53,11 @@ const SOOT = [0.05, 0.045, 0.04];
|
|
|
53
53
|
* @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
|
|
54
54
|
* @param {Function} [options.transform] - Transform matrix helper (`TRS`).
|
|
55
55
|
* @param {typeof GB} [options.Builder] - Geometry builder class.
|
|
56
|
-
* @param {
|
|
57
|
-
* @param {Function} [options.
|
|
58
|
-
* @param {Function} [options.
|
|
59
|
-
* @param {Function} [options.
|
|
56
|
+
* @param {object} [options.houseKit] - Optional house kit (`createHouseKit`) supplying `archOpening`, `archRing`, `facePut`, and `kitNew` defaults.
|
|
57
|
+
* @param {Function} [options.archOpening] - House kit `archOpening` function (overrides `options.houseKit.archOpening`).
|
|
58
|
+
* @param {Function} [options.archRing] - House kit `archRing` function (overrides `options.houseKit.archRing`).
|
|
59
|
+
* @param {Function} [options.facePut] - House kit `facePut` function (overrides `options.houseKit.facePut`).
|
|
60
|
+
* @param {Function} [options.kitNew] - House kit `kitNew` factory for city sub-builders (overrides `options.houseKit.kitNew`).
|
|
60
61
|
* @param {Function} [options.greatWalls] - Walkable great-walls builder (defaults to `createGreatWalls`).
|
|
61
62
|
* @param {Function} [options.greatGateMouth] - Walkable great-wall gate mouth dimensions helper.
|
|
62
63
|
* @returns {{
|
|
@@ -79,12 +80,13 @@ export function createCityFortification(options = {}) {
|
|
|
79
80
|
const color = options.color ?? defaultColor;
|
|
80
81
|
const transform = options.transform ?? defaultTransform;
|
|
81
82
|
const Builder = options.Builder ?? GB;
|
|
83
|
+
const houseKit = options.houseKit ?? null;
|
|
82
84
|
let houseKitFallback = null;
|
|
83
85
|
const fallbackKit = () => (houseKitFallback ??= createHouseKit({ shapes }));
|
|
84
|
-
const archOpening = options.archOpening ?? fallbackKit().archOpening;
|
|
85
|
-
const archRing = options.archRing ?? fallbackKit().archRing;
|
|
86
|
-
const facePut = options.facePut ?? fallbackKit().facePut;
|
|
87
|
-
const kitNew = options.kitNew ?? fallbackKit().kitNew;
|
|
86
|
+
const archOpening = options.archOpening ?? houseKit?.archOpening ?? fallbackKit().archOpening;
|
|
87
|
+
const archRing = options.archRing ?? houseKit?.archRing ?? fallbackKit().archRing;
|
|
88
|
+
const facePut = options.facePut ?? houseKit?.facePut ?? fallbackKit().facePut;
|
|
89
|
+
const kitNew = options.kitNew ?? houseKit?.kitNew ?? fallbackKit().kitNew;
|
|
88
90
|
const defaultGreat = options.greatWalls && options.greatGateMouth ? null : createGreatWalls({ markWall, color });
|
|
89
91
|
const greatWalls = options.greatWalls ?? defaultGreat.greatWalls;
|
|
90
92
|
const greatGateMouth = options.greatGateMouth ?? defaultGreat.greatGateMouth;
|
package/geometry/land-trails.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { spacedCurve } from '../math/curve.js';
|
|
2
|
+
import { clamp } from '../math/scalar.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* A plain's washes and trails, in metres: how far a herd's side trails swing away from the one they follow; the spacing of the
|
|
@@ -14,6 +15,110 @@ export const LAND_TRAILS = Object.freeze({
|
|
|
14
15
|
far: 99, // the distance every cell starts at, beyond any radius marked
|
|
15
16
|
});
|
|
16
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Defaults for rasterizing fine 2-channel RG distance fields of herd trails (R) and dry washes (G), in metres:
|
|
20
|
+
* texel spacing (`step`), encoded distance reach per channel (`reach`), endpoint tapering length (`head`), and
|
|
21
|
+
* clearance margins around roads, rivers and cleared sites.
|
|
22
|
+
*/
|
|
23
|
+
export const LAND_TRAIL_MARKS = Object.freeze({
|
|
24
|
+
step: 1.5,
|
|
25
|
+
reach: Object.freeze({ trail: 8, wash: 16 }),
|
|
26
|
+
head: Object.freeze({ trail: 30, wash: 70 }),
|
|
27
|
+
roadRoom: 9,
|
|
28
|
+
riverRoom: 15,
|
|
29
|
+
siteFade: 18,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Test whether a world-space XZ rectangle overlaps a marks bounding box `{ x0, z0, x1, z1 }`.
|
|
34
|
+
*
|
|
35
|
+
* @param {{ x0: number, z0: number, x1: number, z1: number }} bounds - Marks bounds in metres.
|
|
36
|
+
* @param {number} x0 - Query minimum X in metres.
|
|
37
|
+
* @param {number} z0 - Query minimum Z in metres.
|
|
38
|
+
* @param {number} x1 - Query maximum X in metres.
|
|
39
|
+
* @param {number} z1 - Query maximum Z in metres.
|
|
40
|
+
* @returns {boolean}
|
|
41
|
+
*/
|
|
42
|
+
export function boundsOverlap(bounds, x0, z0, x1, z1) {
|
|
43
|
+
return x1 > bounds.x0 && x0 < bounds.x1 && z1 > bounds.z0 && z0 < bounds.z1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Rasterize herd trails (R channel) and dry washes (G channel) into a fine 2-channel `Uint8Array` distance field over
|
|
48
|
+
* `bounds`, tapering each line near its ends and fading marks near roads, rivers and cleared sites. Pure arithmetic, safe for
|
|
49
|
+
* workers and Node tests.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} options
|
|
52
|
+
* @param {{ trails: number[][][], washes: number[][][] }} options.paths - Sampled polylines (`[x, z]` metres).
|
|
53
|
+
* @param {{ x0: number, z0: number, x1: number, z1: number }} options.bounds - World-space XZ extent in metres.
|
|
54
|
+
* @param {(x: number, z: number) => number} [options.road] - Metres to the nearest road.
|
|
55
|
+
* @param {(x: number, z: number) => number} [options.river] - Metres to the nearest river.
|
|
56
|
+
* @param {(x: number, z: number) => number} [options.siteDistance] - Metres to the nearest cleared site.
|
|
57
|
+
* @param {number} [options.siteRoom=LAND_TRAILS.siteRoom] - Inner clear radius around each site, in metres.
|
|
58
|
+
* @param {Partial<typeof LAND_TRAIL_MARKS>} [options.shape] - Overrides for {@link LAND_TRAIL_MARKS}.
|
|
59
|
+
* @returns {{ data: Uint8Array, width: number, height: number, rect: [number, number, number, number] }}
|
|
60
|
+
*/
|
|
61
|
+
export function rasterizeTrailMarks({ paths, bounds, road, river, siteDistance, siteRoom = LAND_TRAILS.siteRoom, shape = {} }) {
|
|
62
|
+
const step = shape.step ?? LAND_TRAIL_MARKS.step,
|
|
63
|
+
reach = shape.reach ? { ...LAND_TRAIL_MARKS.reach, ...shape.reach } : LAND_TRAIL_MARKS.reach,
|
|
64
|
+
head = shape.head ? { ...LAND_TRAIL_MARKS.head, ...shape.head } : LAND_TRAIL_MARKS.head,
|
|
65
|
+
roadRoom = shape.roadRoom ?? LAND_TRAIL_MARKS.roadRoom,
|
|
66
|
+
riverRoom = shape.riverRoom ?? LAND_TRAIL_MARKS.riverRoom,
|
|
67
|
+
siteFade = shape.siteFade ?? LAND_TRAIL_MARKS.siteFade;
|
|
68
|
+
const { x0, z0, x1, z1 } = bounds,
|
|
69
|
+
width = Math.ceil((x1 - x0) / step),
|
|
70
|
+
height = Math.ceil((z1 - z0) / step),
|
|
71
|
+
data = new Uint8Array(width * height * 2).fill(255);
|
|
72
|
+
// `ends`: 1 when the line thins out at its head only (a gully runs into the next), 2 at both ends.
|
|
73
|
+
const stamp = (path, lineReach, lineHead, ends, channel) => {
|
|
74
|
+
const arc = [0];
|
|
75
|
+
for (let s = 1; s < path.length; s++) arc.push(arc[s - 1] + Math.hypot(path[s][0] - path[s - 1][0], path[s][1] - path[s - 1][1]));
|
|
76
|
+
for (let s = 1; s < path.length; s++) {
|
|
77
|
+
const fromEnd = ends > 1 ? Math.min(arc[s], arc[arc.length - 1] - arc[s]) : arc[s],
|
|
78
|
+
thin = lineReach * Math.max(0, 1 - fromEnd / lineHead) * 0.6; // the line's own distance starts this far out, so it draws narrower
|
|
79
|
+
const [ax, az] = path[s - 1],
|
|
80
|
+
[bx, bz] = path[s],
|
|
81
|
+
dx = bx - ax,
|
|
82
|
+
dz = bz - az,
|
|
83
|
+
len2 = dx * dx + dz * dz || 1;
|
|
84
|
+
const i0 = Math.max(0, Math.floor((Math.min(ax, bx) - lineReach - x0) / step)),
|
|
85
|
+
i1 = Math.min(width - 1, Math.ceil((Math.max(ax, bx) + lineReach - x0) / step)),
|
|
86
|
+
j0 = Math.max(0, Math.floor((Math.min(az, bz) - lineReach - z0) / step)),
|
|
87
|
+
j1 = Math.min(height - 1, Math.ceil((Math.max(az, bz) + lineReach - z0) / step));
|
|
88
|
+
for (let j = j0; j <= j1; j++)
|
|
89
|
+
for (let i = i0; i <= i1; i++) {
|
|
90
|
+
const x = x0 + (i + 0.5) * step,
|
|
91
|
+
z = z0 + (j + 0.5) * step,
|
|
92
|
+
t = clamp(((x - ax) * dx + (z - az) * dz) / len2, 0, 1),
|
|
93
|
+
near = Math.round(((Math.hypot(x - ax - dx * t, z - az - dz * t) + thin) / lineReach) * 255),
|
|
94
|
+
k = (j * width + i) * 2 + channel;
|
|
95
|
+
if (near < data[k]) data[k] = near;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
for (const path of paths.trails) stamp(path, reach.trail, head.trail, 2, 0);
|
|
100
|
+
for (const path of paths.washes) stamp(path, reach.wash, head.wash, 1, 1);
|
|
101
|
+
// A mark fades out towards a road, running water and cleared sites.
|
|
102
|
+
const hasRoad = typeof road === 'function',
|
|
103
|
+
hasRiver = typeof river === 'function',
|
|
104
|
+
hasSites = typeof siteDistance === 'function';
|
|
105
|
+
if (hasRoad || hasRiver || hasSites)
|
|
106
|
+
for (let j = 0; j < height; j++)
|
|
107
|
+
for (let i = 0; i < width; i++) {
|
|
108
|
+
const k = (j * width + i) * 2;
|
|
109
|
+
if (data[k] === 255 && data[k + 1] === 255) continue;
|
|
110
|
+
const x = x0 + (i + 0.5) * step,
|
|
111
|
+
z = z0 + (j + 0.5) * step;
|
|
112
|
+
const crowded =
|
|
113
|
+
(hasRoad ? Math.max(0, roadRoom - road(x, z)) / roadRoom : 0) +
|
|
114
|
+
(hasRiver ? Math.max(0, riverRoom - river(x, z)) / riverRoom : 0) +
|
|
115
|
+
(hasSites ? clamp((siteRoom + siteFade - siteDistance(x, z)) / siteFade, 0, 1) : 0);
|
|
116
|
+
data[k] = Math.min(255, data[k] + crowded * 255);
|
|
117
|
+
data[k + 1] = Math.min(255, data[k + 1] + crowded * 255);
|
|
118
|
+
}
|
|
119
|
+
return { data, width, height, rect: [x0, z0, 1 / (width * step), 1 / (height * step)] };
|
|
120
|
+
}
|
|
121
|
+
|
|
17
122
|
/**
|
|
18
123
|
* Seasonal washes and the trails of herds over a plain. The washes converge on the outflow; several livestock tracks fan out
|
|
19
124
|
* inland from shallow approaches, wearing the grass before the soil itself. Each is kept as the metres from every cell of the
|
|
@@ -94,6 +199,10 @@ export function createLandTrails({ noise, stamp, shape = {} }) {
|
|
|
94
199
|
washD = trackD = cover = null;
|
|
95
200
|
}
|
|
96
201
|
|
|
202
|
+
function rasterizeMarks({ bounds, road, river, shape: markShape } = {}) {
|
|
203
|
+
return rasterizeTrailMarks({ paths, bounds, road, river, siteDistance, siteRoom: SITE_ROOM, shape: markShape });
|
|
204
|
+
}
|
|
205
|
+
|
|
97
206
|
return {
|
|
98
207
|
siteRoom: SITE_ROOM,
|
|
99
208
|
paths,
|
|
@@ -101,6 +210,7 @@ export function createLandTrails({ noise, stamp, shape = {} }) {
|
|
|
101
210
|
forget,
|
|
102
211
|
siteDistance,
|
|
103
212
|
clear,
|
|
213
|
+
rasterizeMarks,
|
|
104
214
|
washDistance: () => washD,
|
|
105
215
|
trailDistance: () => trackD,
|
|
106
216
|
cover: () => cover,
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-safe instance placement planner: samples `(x, z)` points over a ground sampler and fills compact column-major
|
|
3
|
+
* 4×4 transform buffers per variant bucket with zero Three.js dependency or per-instance object allocation.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mulberry32 } from '../math/random.js';
|
|
8
|
+
import { TAU } from '../math/scalar.js';
|
|
9
|
+
|
|
10
|
+
const DEFAULT_SPREAD_METRES = 40;
|
|
11
|
+
const MATRIX_STRIDE = 16;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Samples candidate `(x, z)` placements across `attempts` iterations, distributes accepted points round-robin into
|
|
15
|
+
* `buckets` variant pools (`i % buckets`), grounds each at `groundAt(x, z) - sink`, applies a random Y-axis yaw and
|
|
16
|
+
* uniform scale `scaleRange[0] + random() * scaleRange[1]`, and writes column-major 4×4 transform matrices into compact
|
|
17
|
+
* `Float32Array` buffers ready for `THREE.InstancedMesh` or structured transfer from a worker.
|
|
18
|
+
*
|
|
19
|
+
* @param {object} [options] - Placement parameters.
|
|
20
|
+
* @param {number} [options.attempts=90] - Number of candidate samples to draw.
|
|
21
|
+
* @param {number} [options.maxPerBucket=32] - Maximum accepted instances per bucket.
|
|
22
|
+
* @param {number} [options.buckets=1] - Number of variant buckets (round-robin by attempt index `i % buckets`).
|
|
23
|
+
* @param {(attempt: number) => [number, number] | null | undefined} [options.sample] - Returns `[x, z]` in metres or a falsy value to reject the candidate. Defaults to uniform random points in a 40 m square.
|
|
24
|
+
* @param {(x: number, z: number) => number} [options.groundAt] - Terrain elevation sampler `(x, z) => y` in metres (defaults to flat `0`).
|
|
25
|
+
* @param {() => number} [options.random] - Deterministic PRNG in `[0, 1)` (defaults to `mulberry32(1)`).
|
|
26
|
+
* @param {[number, number]} [options.scaleRange=[1, 0]] - Uniform scale `[minScale, scaleSpan]`: each instance receives `scaleRange[0] + random() * scaleRange[1]`.
|
|
27
|
+
* @param {number} [options.sink=0] - Vertical sink below `groundAt(x, z)` in metres so trunks and roots seat cleanly on slopes.
|
|
28
|
+
* @param {boolean} [options.stopWhenFull=false] - When `true`, stops sampling once every bucket reaches `maxPerBucket`. Leave `false` when callers rely on a fixed `attempts` draw count against a shared PRNG.
|
|
29
|
+
* @returns {{ buckets: Array<{ count: number, matrices: Float32Array }>, total: number }} Per-bucket counts and tightly sized 4×4 transform buffers, plus total placed count.
|
|
30
|
+
*/
|
|
31
|
+
export function planScatterInstances({
|
|
32
|
+
attempts = 90,
|
|
33
|
+
maxPerBucket = 32,
|
|
34
|
+
buckets = 1,
|
|
35
|
+
sample,
|
|
36
|
+
groundAt = () => 0,
|
|
37
|
+
random = mulberry32(1),
|
|
38
|
+
scaleRange = [1, 0],
|
|
39
|
+
sink = 0,
|
|
40
|
+
stopWhenFull = false,
|
|
41
|
+
} = {}) {
|
|
42
|
+
if (!Number.isInteger(buckets) || buckets < 1) throw new RangeError('buckets must be a positive integer');
|
|
43
|
+
if (!Number.isInteger(maxPerBucket) || maxPerBucket < 0) throw new RangeError('maxPerBucket must be a non-negative integer');
|
|
44
|
+
if (!Number.isInteger(attempts) || attempts < 0) throw new RangeError('attempts must be a non-negative integer');
|
|
45
|
+
|
|
46
|
+
const samplePoint = sample ?? (() => [(random() - 0.5) * DEFAULT_SPREAD_METRES, (random() - 0.5) * DEFAULT_SPREAD_METRES]);
|
|
47
|
+
const buffers = Array.from({ length: buckets }, () => new Float32Array(maxPerBucket * MATRIX_STRIDE));
|
|
48
|
+
const counts = new Int32Array(buckets);
|
|
49
|
+
const maxTotal = buckets * maxPerBucket;
|
|
50
|
+
const minScale = scaleRange[0];
|
|
51
|
+
const scaleSpan = scaleRange[1];
|
|
52
|
+
let total = 0;
|
|
53
|
+
|
|
54
|
+
for (let i = 0; i < attempts; i++) {
|
|
55
|
+
if (stopWhenFull && total >= maxTotal) break;
|
|
56
|
+
const point = samplePoint(i);
|
|
57
|
+
if (!point) continue;
|
|
58
|
+
const bucketIndex = i % buckets;
|
|
59
|
+
const count = counts[bucketIndex];
|
|
60
|
+
if (count >= maxPerBucket) continue;
|
|
61
|
+
|
|
62
|
+
const tx = point[0];
|
|
63
|
+
const tz = point[1];
|
|
64
|
+
const ty = groundAt(tx, tz) - sink;
|
|
65
|
+
const yaw = random() * TAU;
|
|
66
|
+
const scale = minScale + random() * scaleSpan;
|
|
67
|
+
const cos = Math.cos(yaw);
|
|
68
|
+
const sin = Math.sin(yaw);
|
|
69
|
+
|
|
70
|
+
const matrices = buffers[bucketIndex];
|
|
71
|
+
const offset = count * MATRIX_STRIDE;
|
|
72
|
+
matrices[offset] = cos * scale;
|
|
73
|
+
matrices[offset + 1] = 0;
|
|
74
|
+
matrices[offset + 2] = -sin * scale;
|
|
75
|
+
matrices[offset + 3] = 0;
|
|
76
|
+
matrices[offset + 4] = 0;
|
|
77
|
+
matrices[offset + 5] = scale;
|
|
78
|
+
matrices[offset + 6] = 0;
|
|
79
|
+
matrices[offset + 7] = 0;
|
|
80
|
+
matrices[offset + 8] = sin * scale;
|
|
81
|
+
matrices[offset + 9] = 0;
|
|
82
|
+
matrices[offset + 10] = cos * scale;
|
|
83
|
+
matrices[offset + 11] = 0;
|
|
84
|
+
matrices[offset + 12] = tx;
|
|
85
|
+
matrices[offset + 13] = ty;
|
|
86
|
+
matrices[offset + 14] = tz;
|
|
87
|
+
matrices[offset + 15] = 1;
|
|
88
|
+
|
|
89
|
+
counts[bucketIndex] = count + 1;
|
|
90
|
+
total++;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
buckets: buffers.map((buffer, index) => {
|
|
95
|
+
const count = counts[index];
|
|
96
|
+
return {
|
|
97
|
+
count,
|
|
98
|
+
matrices: count === maxPerBucket ? buffer : buffer.slice(0, count * MATRIX_STRIDE),
|
|
99
|
+
};
|
|
100
|
+
}),
|
|
101
|
+
total,
|
|
102
|
+
};
|
|
103
|
+
}
|