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,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stepped sedimentary cliff walls wrapping flat-topped mesas: stacked rock beds broken into
|
|
3
|
+
* vertical buttresses and clefts along geological joints, stepping back beneath dust-covered ledges.
|
|
4
|
+
* Pure geometry builder, worker-safe.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createNoise } from '../math/noise.js';
|
|
9
|
+
import { TAU, clamp, lerp } from '../math/scalar.js';
|
|
10
|
+
import { GB } from './builder.js';
|
|
11
|
+
import { C } from './primitives.js';
|
|
12
|
+
import { flatQuad, jointed } from './strata-rocks.js';
|
|
13
|
+
|
|
14
|
+
let fallbackNoise = null;
|
|
15
|
+
const defaultNoise = () => (fallbackNoise ??= createNoise().vnoise);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Default bedding fractions, horizontal vertex spacing (metres) and weathered sandstone sRGB tones
|
|
19
|
+
* for mesa cliff walls built by {@link buildMesaCliff}.
|
|
20
|
+
*/
|
|
21
|
+
export const MESA_CLIFF = Object.freeze({
|
|
22
|
+
/** Fractional wall heights where each rock bed ends, bottom to top. */
|
|
23
|
+
bedTops: Object.freeze([0.3, 0.41, 0.7, 0.82, 1]),
|
|
24
|
+
/** Target horizontal spacing between perimeter cliff vertices in metres. */
|
|
25
|
+
step: 2.3,
|
|
26
|
+
/** sRGB hex base tone for each rock bed, bottom to top. */
|
|
27
|
+
bedTones: Object.freeze([0x8f6548, 0xa57856, 0x86593f, 0xae825e, 0x94694a]),
|
|
28
|
+
/** sRGB hex dust-ledge tone between consecutive beds. */
|
|
29
|
+
ledgeTone: 0xa98462,
|
|
30
|
+
/** Radial foot fraction passed to `mesaEdge` when sampling the cliff plinth. */
|
|
31
|
+
cliffFoot: 1.0,
|
|
32
|
+
/** Radial brow fraction passed to `mesaEdge` when sampling the cap rim. */
|
|
33
|
+
cliffBrow: 0.72,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build the stepped, jointed sedimentary cliff wall around a flat-topped mesa resting on `ground`.
|
|
38
|
+
*
|
|
39
|
+
* @param {{ x: number, z: number, r: number }} mesa - Mesa centre `(x, z)` and nominal radius `r` in metres.
|
|
40
|
+
* @param {(x: number, z: number) => number} [ground] - Terrain height sampler `(x, z) => y` in metres.
|
|
41
|
+
* @param {object} [options]
|
|
42
|
+
* @param {(mesa: object, angle: number, fraction: number) => number} [options.mesaEdge] - Radial outline distance at `angle` (radians) and `fraction`.
|
|
43
|
+
* @param {number} [options.cliffFoot=MESA_CLIFF.cliffFoot] - Foot fraction forwarded to `mesaEdge`.
|
|
44
|
+
* @param {number} [options.cliffBrow=MESA_CLIFF.cliffBrow] - Brow fraction forwarded to `mesaEdge`.
|
|
45
|
+
* @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise in `[0, 1)`.
|
|
46
|
+
* @param {typeof GB} [options.Builder=GB] - Geometry builder class.
|
|
47
|
+
* @param {Partial<typeof MESA_CLIFF>} [options.shape] - Overrides to {@link MESA_CLIFF}.
|
|
48
|
+
* @returns {GB} Populated geometry builder containing the cliff wall and ledges.
|
|
49
|
+
*/
|
|
50
|
+
export function buildMesaCliff(mesa, ground = () => 0, options = {}) {
|
|
51
|
+
const cfg = options.shape ? { ...MESA_CLIFF, ...options.shape } : MESA_CLIFF;
|
|
52
|
+
const vnoise = options.noise ?? defaultNoise();
|
|
53
|
+
const Builder = options.Builder ?? GB;
|
|
54
|
+
const cliffFoot = options.cliffFoot ?? cfg.cliffFoot;
|
|
55
|
+
const cliffBrow = options.cliffBrow ?? cfg.cliffBrow;
|
|
56
|
+
const mesaEdge = options.mesaEdge ?? ((m, _angle, fraction) => m.r * fraction);
|
|
57
|
+
const bedTops = cfg.bedTops;
|
|
58
|
+
const bedTones = cfg.bedTones;
|
|
59
|
+
const beds = bedTops.length;
|
|
60
|
+
|
|
61
|
+
const builder = new Builder();
|
|
62
|
+
const round = Math.round(clamp((TAU * mesa.r) / cfg.step, 72, 280));
|
|
63
|
+
const columns = [];
|
|
64
|
+
for (let i = 0; i < round; i++) {
|
|
65
|
+
const angle = (i / round) * TAU;
|
|
66
|
+
const c = Math.cos(angle);
|
|
67
|
+
const s = Math.sin(angle);
|
|
68
|
+
const foot = mesaEdge(mesa, angle, cliffFoot + 0.1);
|
|
69
|
+
const brow = mesaEdge(mesa, angle, cliffFoot + 0.03);
|
|
70
|
+
const cap = mesaEdge(mesa, angle, cliffBrow - 0.05);
|
|
71
|
+
columns.push({
|
|
72
|
+
c,
|
|
73
|
+
s,
|
|
74
|
+
foot,
|
|
75
|
+
brow,
|
|
76
|
+
cap,
|
|
77
|
+
low: ground(mesa.x + c * (foot + 2), mesa.z + s * (foot + 2)) - 1.5,
|
|
78
|
+
high: ground(mesa.x + c * cap, mesa.z + s * cap) + 0.12,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const point = (i, k, top) => {
|
|
82
|
+
const col = columns[i % round];
|
|
83
|
+
const x = mesa.x + col.c * col.foot;
|
|
84
|
+
const z = mesa.z + col.s * col.foot;
|
|
85
|
+
// Buttresses share noise across beds so vertical ribs and clefts run up the whole cliff.
|
|
86
|
+
const buttress = 3.4 * (jointed(vnoise(x * 0.075, z * 0.075, 881), 5) - 0.25);
|
|
87
|
+
const broken = 1.1 * (jointed(vnoise(x * 0.17 + k * 7.1, z * 0.17, 882), 3) - 0.3);
|
|
88
|
+
const r = lerp(col.foot, col.brow, k / (beds - 1)) + buttress + broken - 0.3 * top;
|
|
89
|
+
const up = top ? bedTops[k] : k ? bedTops[k - 1] : 0;
|
|
90
|
+
return [mesa.x + col.c * r, lerp(col.low, col.high, up), mesa.z + col.s * r];
|
|
91
|
+
};
|
|
92
|
+
const ledge = C(cfg.ledgeTone);
|
|
93
|
+
for (let k = 0; k < beds; k++) {
|
|
94
|
+
const tone = C(bedTones[k]);
|
|
95
|
+
const dark = tone.map(v => v * 0.78);
|
|
96
|
+
const head = k < beds - 1 ? ledge : tone;
|
|
97
|
+
for (let i = 0; i < round; i++) {
|
|
98
|
+
const a = point(i, k, 0);
|
|
99
|
+
const b = point(i + 1, k, 0);
|
|
100
|
+
const c = point(i + 1, k, 1);
|
|
101
|
+
const d = point(i, k, 1);
|
|
102
|
+
const shade = 0.82 + 0.36 * vnoise(a[0] * 0.21, a[2] * 0.21 + k, 883);
|
|
103
|
+
const high = tone.map(v => v * shade);
|
|
104
|
+
const low = dark.map(v => v * shade);
|
|
105
|
+
flatQuad(builder, b, a, d, c, low, high);
|
|
106
|
+
const col = columns[i];
|
|
107
|
+
const next = columns[(i + 1) % round];
|
|
108
|
+
const e = k < beds - 1 ? point(i, k + 1, 0) : [mesa.x + col.c * col.cap, col.high, mesa.z + col.s * col.cap];
|
|
109
|
+
const f = k < beds - 1 ? point(i + 1, k + 1, 0) : [mesa.x + next.c * next.cap, next.high, mesa.z + next.s * next.cap];
|
|
110
|
+
flatQuad(builder, c, d, e, f, head, head);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return builder;
|
|
114
|
+
}
|
package/geometry/strata-rocks.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import * as THREE from 'three';
|
|
9
9
|
import { createNoise } from '../math/noise.js';
|
|
10
|
+
import { mulberry32 } from '../math/random.js';
|
|
10
11
|
import { TAU, lerp, sstep } from '../math/scalar.js';
|
|
11
12
|
import { GB } from './builder.js';
|
|
12
13
|
import { lump } from './primitives.js';
|
|
@@ -70,8 +71,10 @@ const _quat = new THREE.Quaternion();
|
|
|
70
71
|
const _euler = new THREE.Euler();
|
|
71
72
|
const _scale = new THREE.Vector3();
|
|
72
73
|
|
|
73
|
-
let
|
|
74
|
-
const
|
|
74
|
+
let fallbackNoiseObj = null;
|
|
75
|
+
const defaultNoiseObj = () => (fallbackNoiseObj ??= createNoise());
|
|
76
|
+
const defaultNoise = () => defaultNoiseObj().vnoise;
|
|
77
|
+
const defaultFbm = () => defaultNoiseObj().fbm;
|
|
75
78
|
|
|
76
79
|
/**
|
|
77
80
|
* Append a flat-shaded quad `(a, b, c, d)` (anticlockwise from front) into a geometry builder,
|
|
@@ -268,3 +271,71 @@ export function createRockShapes(options = {}) {
|
|
|
268
271
|
farOutcropShapes: () => outcropSeeds.map(seed => outcrop(seed, true)),
|
|
269
272
|
};
|
|
270
273
|
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Build a roughly half-metre radius crag rock cut by two intersecting joint planes with a flat
|
|
277
|
+
* underside, shaded smoothly over rounded faces.
|
|
278
|
+
*
|
|
279
|
+
* @param {number} fineness - Icosahedron subdivision level (`0` stone, `1` boulder, `2` slab).
|
|
280
|
+
* @param {object} [options]
|
|
281
|
+
* @param {(x: number, z: number, octaves: number, salt: number) => number} [options.fbm] - Seeded fractal noise (`createNoise().fbm`).
|
|
282
|
+
* @returns {import('three').BufferGeometry}
|
|
283
|
+
*/
|
|
284
|
+
export function cragRock(fineness, options = {}) {
|
|
285
|
+
const fbm = options.fbm ?? defaultFbm();
|
|
286
|
+
return lump(fineness, (dir, out) => {
|
|
287
|
+
const bulge = 0.74 + 0.5 * fbm(dir.x * 1.3 + 7, dir.z * 1.3 + dir.y * 1.1, 3, 733);
|
|
288
|
+
const joint = Math.min(
|
|
289
|
+
1,
|
|
290
|
+
0.86 / Math.max(Math.abs(dir.x * 0.8 + dir.z * 0.6), Math.abs(dir.z * 0.8 - dir.x * 0.6 + dir.y * 0.3), 0.01),
|
|
291
|
+
);
|
|
292
|
+
return out.set(dir.x * 0.5 * bulge * joint, Math.max(dir.y * 0.5 * bulge, -0.36), dir.z * 0.5 * bulge * joint);
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Build a metre-long weathered stone with a tilted fracture plane across its top, a flat underside,
|
|
298
|
+
* smooth normals over rounded parts and white vertex colours ready for per-instance tinting.
|
|
299
|
+
*
|
|
300
|
+
* @param {number} detail - Icosahedron subdivision level (`0`, `1` or `2`).
|
|
301
|
+
* @param {number} [seed=1] - Seed for fracture plane tilt and surface noise.
|
|
302
|
+
* @param {object} [options]
|
|
303
|
+
* @param {(x: number, z: number, salt: number) => number} [options.vnoise] - Seeded value noise (`createNoise().vnoise`).
|
|
304
|
+
* @returns {import('three').BufferGeometry}
|
|
305
|
+
*/
|
|
306
|
+
export function fallenStone(detail, seed = 1, options = {}) {
|
|
307
|
+
const vnoise = options.vnoise ?? options.noise ?? defaultNoise();
|
|
308
|
+
const rng = mulberry32(seed);
|
|
309
|
+
const cx = (rng() - 0.5) * 0.5;
|
|
310
|
+
const cz = (rng() - 0.5) * 0.5;
|
|
311
|
+
const g = lump(detail, (dir, out) => {
|
|
312
|
+
const x = dir.x * 0.5;
|
|
313
|
+
const y = dir.y * 0.5;
|
|
314
|
+
const z = dir.z * 0.5;
|
|
315
|
+
const r = 0.78 + 0.38 * vnoise(x * 3 + seed, z * 3 + y * 2, 611);
|
|
316
|
+
return out.set(x * r * 1.15, Math.max(Math.min(y * r, 0.28 + x * cx + z * cz), -0.3), z * r);
|
|
317
|
+
});
|
|
318
|
+
g.setAttribute('color', new THREE.Float32BufferAttribute(new Float32Array(g.attributes.position.count * 3).fill(1), 3));
|
|
319
|
+
return g;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Create a cached library of crag rocks and fallen stones bound to a world's noise functions.
|
|
324
|
+
*
|
|
325
|
+
* @param {object} [options]
|
|
326
|
+
* @param {(x: number, z: number, octaves: number, salt: number) => number} [options.fbm] - Seeded fractal noise (`createNoise().fbm`).
|
|
327
|
+
* @param {(x: number, z: number, salt: number) => number} [options.vnoise] - Seeded value noise (`createNoise().vnoise`).
|
|
328
|
+
* @returns {{
|
|
329
|
+
* crag: (fineness: number) => import('three').BufferGeometry,
|
|
330
|
+
* fallenStone: (detail: number, seed?: number) => import('three').BufferGeometry,
|
|
331
|
+
* }}
|
|
332
|
+
*/
|
|
333
|
+
export function createCragLibrary(options = {}) {
|
|
334
|
+
const fbm = options.fbm ?? defaultFbm();
|
|
335
|
+
const vnoise = options.vnoise ?? defaultNoise();
|
|
336
|
+
const crags = [];
|
|
337
|
+
return {
|
|
338
|
+
crag: fineness => (crags[fineness] ??= cragRock(fineness, { fbm })),
|
|
339
|
+
fallenStone: (detail, seed = 1) => fallenStone(detail, seed, { vnoise }),
|
|
340
|
+
};
|
|
341
|
+
}
|
package/index.js
CHANGED
|
@@ -57,14 +57,41 @@ export {
|
|
|
57
57
|
WORN_STONE_FOOT,
|
|
58
58
|
WORN_STONE_FORMS,
|
|
59
59
|
beddedOutcrop,
|
|
60
|
+
cragRock,
|
|
61
|
+
createCragLibrary,
|
|
60
62
|
createRockShapes,
|
|
63
|
+
fallenStone,
|
|
61
64
|
flatQuad,
|
|
62
65
|
jointed,
|
|
63
66
|
wornStone,
|
|
64
67
|
} from './geometry/strata-rocks.js';
|
|
68
|
+
export { MESA_CLIFF, buildMesaCliff } from './geometry/strata-mesa.js';
|
|
65
69
|
export { HILL_RUIN, buildHillRuin } from './geometry/ruins-hill.js';
|
|
66
70
|
export { MOUND_RUINS, buildMoundRuins, buildMoundSherds, buildMoundWalls, createMoundRuins } from './geometry/ruins-mound.js';
|
|
71
|
+
export { MOUND_RUINS_LOD, raiseMoundRuins, registerMoundRuins } from './rendering/ruins-mound.js';
|
|
67
72
|
export { RUBBLE_HEAP, RUBBLE_SCATTER, buildRubbleHeap, scatterRubble } from './geometry/ruins-rubble.js';
|
|
73
|
+
export { RUIN_WALL, buildRuinWall, coursedWall, tumbleBlock } from './geometry/ruins-walls.js';
|
|
74
|
+
export {
|
|
75
|
+
ASHLAR_COURSE,
|
|
76
|
+
COLUMN_DRUM,
|
|
77
|
+
MASONRY_METAL,
|
|
78
|
+
buildAshlarWall,
|
|
79
|
+
buildColumnDrum,
|
|
80
|
+
buildFlagstones,
|
|
81
|
+
buildLampStandGeometry,
|
|
82
|
+
buildWallLanternGeometry,
|
|
83
|
+
} from './geometry/ruins-masonry.js';
|
|
84
|
+
export {
|
|
85
|
+
LIGHT_POOL,
|
|
86
|
+
addLampStand,
|
|
87
|
+
addLightPool,
|
|
88
|
+
addStillPool,
|
|
89
|
+
addWallLantern,
|
|
90
|
+
createLightPoolTexture,
|
|
91
|
+
} from './rendering/ruins-fittings.js';
|
|
92
|
+
export { DEFAULT_SCORCH_PALETTE, addEmberSkin, buildBreachFan, buildRubbleMound } from './geometry/ruins-breach.js';
|
|
93
|
+
export { CRATER_PALETTE, buildCraterBowl, buildFallenBoulder } from './geometry/ruins-crater.js';
|
|
94
|
+
export { createShapeBatches } from './geometry/instances.js';
|
|
68
95
|
export { planStreamBanks } from './geometry/stream-banks.js';
|
|
69
96
|
export { meshStreamBanks } from './rendering/stream-banks.js';
|
|
70
97
|
export { SPRING_COLOURS, SPRING_HEAD, planSpringRocks, planSprings, springFrame, springWetGround } from './geometry/stream-springs.js';
|
|
@@ -89,6 +116,9 @@ export { createAvenueHandover, farRoadPavingMaterial, layPavedRoad, layRoadAvenu
|
|
|
89
116
|
export { PAVED_GROUND_LIMITS, createPavedGroundMaterial } from './rendering/paved-ground.js';
|
|
90
117
|
export { createRoadTraffic, ROAD_TRAFFIC } from './motion/road-traffic.js';
|
|
91
118
|
export { createFieldSurvey, FIELD_SURVEY, FIELD_USE } from './geometry/field-survey.js';
|
|
119
|
+
export { FIELD_BITS, NO_PATH_DISTANCE, createFieldCoverCache, packFieldBits, packSparsePlain } from './geometry/field-map.js';
|
|
120
|
+
export { DEFAULT_FLOWER_LOOKS, FLOWER_KINDS, TUFT_SHAPE, tuftGeometry } from './geometry/plant-tuft.js';
|
|
121
|
+
export { TUFT_SHADER, createGrassTuft, createTuftMaterial } from './rendering/plant-tuft.js';
|
|
92
122
|
export { LAND_TRAILS, LAND_TRAIL_MARKS, boundsOverlap, createLandTrails, rasterizeTrailMarks } from './geometry/land-trails.js';
|
|
93
123
|
export { createTrailMarks, trailMarksGLSL } from './rendering/land-trail-surface.js';
|
|
94
124
|
export { createIrrigatedFields, IRRIGATED_FIELDS } from './geometry/irrigated-fields.js';
|
|
@@ -101,10 +131,19 @@ export { scatterInstances, scatterTrees, scatterTussocks } from './rendering/sca
|
|
|
101
131
|
export { FARM_BOUNDARIES, planFieldBoundaries } from './geometry/farm-boundaries.js';
|
|
102
132
|
export { meshFarmCells, scatterFarmLots } from './rendering/farm-cells.js';
|
|
103
133
|
export { DEFAULT_LIVESTOCK_CASTS, createLivestockCache } from './rendering/farm-livestock.js';
|
|
134
|
+
export {
|
|
135
|
+
DEFAULT_OX_COATS,
|
|
136
|
+
DEFAULT_PLOUGHMAN_LOOK,
|
|
137
|
+
PLOUGHMAN_SEEN_DISTANCE,
|
|
138
|
+
PLOUGHMAN_STANCE,
|
|
139
|
+
buildPloughman,
|
|
140
|
+
createPloughTeam,
|
|
141
|
+
posePloughman,
|
|
142
|
+
} from './rendering/farm-plough.js';
|
|
104
143
|
export { DEFAULT_FARM_HERDS, FARM_LIFE, herdPlaces, parcelPlaces, planFarmLife, ploughSite } from './geometry/farm-life.js';
|
|
105
144
|
export { STOOK, createStook } from './geometry/prop-stook.js';
|
|
106
145
|
export { PLOUGH, ploughGear } from './geometry/prop-plough.js';
|
|
107
|
-
export { FARM_HAYSTACK, FARM_STONE_PILE, buildHaystacks, buildStonePile } from './geometry/farm-props.js';
|
|
146
|
+
export { FARM_BARN_DOORS, FARM_HAYSTACK, FARM_STONE_PILE, buildBarnDoors, buildHaystacks, buildStonePile } from './geometry/farm-props.js';
|
|
108
147
|
|
|
109
148
|
// ── Settlements, houses, fortifications & interiors ──────────────────────
|
|
110
149
|
export { createHouseKit } from './geometry/house-kit.js';
|
|
@@ -230,6 +269,8 @@ export {
|
|
|
230
269
|
createLookPicker,
|
|
231
270
|
} from './rendering/crowd-batches.js';
|
|
232
271
|
export { createCrowdUpdater, registerCrowd } from './rendering/crowd-update.js';
|
|
272
|
+
export { createCrowdLifecycle } from './rendering/crowd-lifecycle.js';
|
|
273
|
+
export { ContactShadows, createFootShadows } from './rendering/contact-shadows.js';
|
|
233
274
|
export { createCrowdSystem } from './rendering/crowd-system.js';
|
|
234
275
|
|
|
235
276
|
// ── Effects, audio, pacing & runtime ─────────────────────────────────────
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "roamwright",
|
|
3
|
-
"version": "0.61.
|
|
3
|
+
"version": "0.61.19",
|
|
4
4
|
"description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"threejs",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
+
import { sstep } from '../math/scalar.js';
|
|
2
3
|
|
|
3
4
|
// Soft contact shadows on horizontal receivers. Supply the height of the receiving
|
|
4
5
|
// surface in mesh-local coordinates. One quad per contact, in one draw.
|
|
@@ -80,3 +81,58 @@ export class ContactShadows {
|
|
|
80
81
|
this.mesh.material.dispose();
|
|
81
82
|
}
|
|
82
83
|
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create a distance-culled and smoothly faded contact-shadow collector for feet, hooves, paws and
|
|
87
|
+
* supine bodies, backed by a single {@link ContactShadows} draw call.
|
|
88
|
+
*
|
|
89
|
+
* @param {object} [options]
|
|
90
|
+
* @param {THREE.Object3D | null} [options.scene=null] - Optional scene or group receiving the shadow mesh.
|
|
91
|
+
* @param {number} [options.capacity=512] - Maximum contact quads per frame.
|
|
92
|
+
* @param {number} [options.opacity=0.42] - Peak contact darkening opacity.
|
|
93
|
+
* @param {number} [options.lift=0.03] - Vertical offset above the receiver surface in metres.
|
|
94
|
+
* @param {number} [options.reach=42] - Distance in metres beyond which contacts are omitted.
|
|
95
|
+
* @param {number} [options.fadeFrom=0.7] - Fraction of `reach` where distance fade begins.
|
|
96
|
+
* @param {number} [options.renderOrder=2] - Three.js `renderOrder` for the contact shadow mesh.
|
|
97
|
+
* @param {number | null} [options.layer=1] - Render layer index assigned to the mesh (`null` leaves default layer 0).
|
|
98
|
+
* @param {string} [options.name='Foot shade'] - Scene-graph name for the contact shadow mesh.
|
|
99
|
+
* @returns {{
|
|
100
|
+
* add: (x: number, y: number, z: number, size: number, distanceSquared: number, intensityScale?: number) => void,
|
|
101
|
+
* begin: () => void,
|
|
102
|
+
* end: () => void,
|
|
103
|
+
* batch: ContactShadows,
|
|
104
|
+
* mesh: THREE.Mesh,
|
|
105
|
+
* dispose: () => void,
|
|
106
|
+
* }}
|
|
107
|
+
*/
|
|
108
|
+
export function createFootShadows({
|
|
109
|
+
scene = null,
|
|
110
|
+
capacity = 512,
|
|
111
|
+
opacity = 0.42,
|
|
112
|
+
lift = 0.03,
|
|
113
|
+
reach = 42,
|
|
114
|
+
fadeFrom = 0.7,
|
|
115
|
+
renderOrder = 2,
|
|
116
|
+
layer = 1,
|
|
117
|
+
name = 'Foot shade',
|
|
118
|
+
} = {}) {
|
|
119
|
+
const batch = new ContactShadows({ capacity, opacity, lift });
|
|
120
|
+
batch.mesh.name = name;
|
|
121
|
+
batch.mesh.renderOrder = renderOrder;
|
|
122
|
+
if (layer !== null) batch.mesh.layers.set(layer);
|
|
123
|
+
if (scene) scene.add(batch.mesh);
|
|
124
|
+
const reachSq = reach * reach;
|
|
125
|
+
const fadeStart = reach * fadeFrom;
|
|
126
|
+
return {
|
|
127
|
+
add(x, y, z, size, distanceSquared, intensityScale = 1) {
|
|
128
|
+
if (distanceSquared >= reachSq) return;
|
|
129
|
+
const strength = (1 - sstep(fadeStart, reach, Math.sqrt(distanceSquared))) * intensityScale;
|
|
130
|
+
batch.add(x, y, z, size * 0.5, strength);
|
|
131
|
+
},
|
|
132
|
+
begin: () => batch.begin(),
|
|
133
|
+
end: () => batch.end(),
|
|
134
|
+
batch,
|
|
135
|
+
mesh: batch.mesh,
|
|
136
|
+
dispose: () => batch.dispose(),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crowd prepare-and-batch lifecycle around `createCrowdBatches`: registers crowd groups,
|
|
3
|
+
* caches shared distant/remote walk geometries, bakes their walk textures when the walker rig is
|
|
4
|
+
* ready, and rebuilds instanced batches whenever the registered crowd count changes.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { farCrowdFigure } from '../geometry/crowd-far.js';
|
|
9
|
+
import { registerCrowd } from './crowd-update.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a page-scoped crowd prepare-and-batch lifecycle bound to a crowd batch manager and list.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} config
|
|
15
|
+
* @param {ReturnType<import('./crowd-batches.js').createCrowdBatches>} config.batchManager - Crowd batch manager holding pose and walk textures.
|
|
16
|
+
* @param {object[]} [config.crowds=[]] - Mutable crowd registry populated by `addCrowd` and batched by `batchCrowds`.
|
|
17
|
+
* @param {object} [config.head] - Optional head profile passed to `farCrowdFigure('distant'|'remote', { head })`.
|
|
18
|
+
* @param {(spaceId: string, x: number, z: number) => number} [config.groundAt] - Ground elevation sampler by space id.
|
|
19
|
+
* @param {(x: number, y: number, salt?: number) => number} [config.hash] - Seeded 2D hash forwarded to `registerCrowd`.
|
|
20
|
+
* @param {string} [config.defaultSpace='world'] - Default space id for `addCrowd`.
|
|
21
|
+
* @returns {{
|
|
22
|
+
* addCrowd: (list: object[], tones: number[], cx: number, cz: number, range: number, kind: string, beasts?: object | null, space?: string) => object,
|
|
23
|
+
* prepareCrowds: () => import('three').BufferGeometry[],
|
|
24
|
+
* prepareWhenReady: (walkerOrdered?: () => Promise<unknown> | null | undefined) => Promise<import('three').BufferGeometry[]> | null,
|
|
25
|
+
* batchCrowds: () => object,
|
|
26
|
+
* clearFarFigures: () => void,
|
|
27
|
+
* }}
|
|
28
|
+
*/
|
|
29
|
+
export function createCrowdLifecycle({
|
|
30
|
+
batchManager,
|
|
31
|
+
crowds = [],
|
|
32
|
+
head = undefined,
|
|
33
|
+
groundAt = () => 0,
|
|
34
|
+
hash = undefined,
|
|
35
|
+
defaultSpace = 'world',
|
|
36
|
+
}) {
|
|
37
|
+
let farFigures = null;
|
|
38
|
+
|
|
39
|
+
function addCrowd(list, tones, cx, cz, range, kind, beasts = null, space = defaultSpace) {
|
|
40
|
+
return registerCrowd(crowds, list, tones, cx, cz, range, kind, {
|
|
41
|
+
beasts,
|
|
42
|
+
space,
|
|
43
|
+
groundAt,
|
|
44
|
+
...(hash ? { hash } : {}),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function prepareCrowds() {
|
|
49
|
+
const farOptions = head ? { head } : undefined;
|
|
50
|
+
farFigures ??= [farCrowdFigure('distant', farOptions), farCrowdFigure('remote', farOptions)];
|
|
51
|
+
batchManager.bakeWalksOnce(farFigures);
|
|
52
|
+
return farFigures;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function prepareWhenReady(walkerOrdered = () => batchManager.walkerOrdered?.()) {
|
|
56
|
+
const ordered = walkerOrdered();
|
|
57
|
+
if (ordered) return ordered.then(prepareCrowds);
|
|
58
|
+
prepareCrowds();
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function batchCrowds() {
|
|
63
|
+
if (batchManager.batches?.groupCount === crowds.length) return batchManager.batches;
|
|
64
|
+
prepareCrowds();
|
|
65
|
+
const built = batchManager.buildCrowdBatches(
|
|
66
|
+
farFigures.map(f => f.clone()),
|
|
67
|
+
crowds,
|
|
68
|
+
);
|
|
69
|
+
batchManager.setBatches(built);
|
|
70
|
+
return built;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function clearFarFigures() {
|
|
74
|
+
if (!farFigures) return;
|
|
75
|
+
for (const geo of farFigures) geo.dispose();
|
|
76
|
+
farFigures = null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { addCrowd, batchCrowds, clearFarFigures, prepareCrowds, prepareWhenReady };
|
|
80
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plough team and ploughman assembly: pairs two yoked oxen (`THREE.InstancedMesh`) with timber
|
|
3
|
+
* plough gear (`geometry/prop-plough.js`) and lazily sculpts and poses the ploughman behind the
|
|
4
|
+
* stilts when the camera first approaches within visibility range.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { GB } from '../geometry/builder.js';
|
|
10
|
+
import { TRS, createUnitShapes } from '../geometry/primitives.js';
|
|
11
|
+
import { ploughGear } from '../geometry/prop-plough.js';
|
|
12
|
+
|
|
13
|
+
const _coat = new THREE.Color();
|
|
14
|
+
|
|
15
|
+
/** Default sRGB hex coat tones for the left and right yoked oxen. */
|
|
16
|
+
export const DEFAULT_OX_COATS = Object.freeze([0x96684a, 0xc4a57e]);
|
|
17
|
+
|
|
18
|
+
/** Default camera distance in metres within which the ploughman figure is lazily built. */
|
|
19
|
+
export const PLOUGHMAN_SEEN_DISTANCE = 180;
|
|
20
|
+
|
|
21
|
+
/** Default human look descriptor for a farm ploughman. */
|
|
22
|
+
export const DEFAULT_PLOUGHMAN_LOOK = Object.freeze({
|
|
23
|
+
h: 0.98,
|
|
24
|
+
w: 1.08,
|
|
25
|
+
skin: 0xb48a64,
|
|
26
|
+
hair: 0x4a3a2a,
|
|
27
|
+
garb: 'tunic',
|
|
28
|
+
c1: 0x7c6a48,
|
|
29
|
+
c2: 0x4c4234,
|
|
30
|
+
hairStyle: 'cropped',
|
|
31
|
+
beard: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Mid-stride ploughman pose leaning into the plough stilts with both hands lowered onto the handles
|
|
36
|
+
* (positions in metres, joint rotations in radians).
|
|
37
|
+
*/
|
|
38
|
+
export const PLOUGHMAN_STANCE = Object.freeze({
|
|
39
|
+
z: -1.02,
|
|
40
|
+
rootX: 0.14,
|
|
41
|
+
armX: -0.28,
|
|
42
|
+
foreX: -0.8,
|
|
43
|
+
legLX: -0.42,
|
|
44
|
+
legRX: 0.3,
|
|
45
|
+
shinRX: 0.35,
|
|
46
|
+
headX: 0.12,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Apply the ploughman's mid-stride leaning pose to an assembled figure rig.
|
|
51
|
+
*
|
|
52
|
+
* @param {object} figure - Assembled figure containing `group`, `root`, `armL`, `armR`, `foreL`, `foreR`, `legL`, `legR`, `shinR`, `head`.
|
|
53
|
+
* @param {typeof PLOUGHMAN_STANCE} [stance=PLOUGHMAN_STANCE] - Joint angles and Z offset behind the plough.
|
|
54
|
+
* @returns {THREE.Object3D} The posed `figure.group`.
|
|
55
|
+
*/
|
|
56
|
+
export function posePloughman(figure, stance = PLOUGHMAN_STANCE) {
|
|
57
|
+
figure.group.position.set(0, 0, stance.z);
|
|
58
|
+
figure.root.rotation.x = stance.rootX;
|
|
59
|
+
figure.armL.rotation.x = figure.armR.rotation.x = stance.armX;
|
|
60
|
+
figure.foreL.rotation.x = figure.foreR.rotation.x = stance.foreX;
|
|
61
|
+
figure.legL.rotation.x = stance.legLX;
|
|
62
|
+
figure.legR.rotation.x = stance.legRX;
|
|
63
|
+
figure.shinR.rotation.x = stance.shinRX;
|
|
64
|
+
figure.head.rotation.x = stance.headX;
|
|
65
|
+
return figure.group;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Build and pose a ploughman figure using a caller-supplied figure builder.
|
|
70
|
+
*
|
|
71
|
+
* @param {object} options
|
|
72
|
+
* @param {(look: object, seed: number) => object} options.buildFigure - Figure builder returning a posed-rig handle.
|
|
73
|
+
* @param {object} [options.look=DEFAULT_PLOUGHMAN_LOOK] - Figure look descriptor.
|
|
74
|
+
* @param {number} [options.seed=3587] - Deterministic figure seed.
|
|
75
|
+
* @param {typeof PLOUGHMAN_STANCE} [options.stance=PLOUGHMAN_STANCE] - Joint pose overrides.
|
|
76
|
+
* @returns {THREE.Object3D}
|
|
77
|
+
*/
|
|
78
|
+
export function buildPloughman({ buildFigure, look = DEFAULT_PLOUGHMAN_LOOK, seed = 3587, stance = PLOUGHMAN_STANCE }) {
|
|
79
|
+
return posePloughman(buildFigure(look, seed), stance);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Assemble a yoked two-ox plough team facing `+z` from the returned group's origin, with timber
|
|
84
|
+
* plough gear and an optional lazy-built ploughman sculpted on the first near render.
|
|
85
|
+
*
|
|
86
|
+
* @param {object} options
|
|
87
|
+
* @param {THREE.BufferGeometry} options.oxen - Standing ox geometry facing `+z` on `y = 0`.
|
|
88
|
+
* @param {() => number} [options.random=Math.random] - Deterministic PRNG in `[0, 1)` for plough timber tinting.
|
|
89
|
+
* @param {THREE.Material} [options.oxMaterial] - Material for the instanced oxen pair.
|
|
90
|
+
* @param {THREE.Material} [options.woodMaterial] - Material for the timber plough gear.
|
|
91
|
+
* @param {THREE.Camera | null} [options.camera=null] - Active camera checked against `seenDistance` before building the ploughman.
|
|
92
|
+
* @param {((look: object, seed: number) => object) | null} [options.buildFigure=null] - Figure builder for the ploughman (omit for unmanned team).
|
|
93
|
+
* @param {readonly [number, number]} [options.oxCoats=DEFAULT_OX_COATS] - sRGB hex coat tones for the two oxen.
|
|
94
|
+
* @param {object} [options.ploughmanLook=DEFAULT_PLOUGHMAN_LOOK] - Look descriptor passed to `buildFigure`.
|
|
95
|
+
* @param {number} [options.ploughmanSeed=3587] - Seed passed to `buildFigure`.
|
|
96
|
+
* @param {typeof PLOUGHMAN_STANCE} [options.ploughmanStance=PLOUGHMAN_STANCE] - Stance angles for the ploughman.
|
|
97
|
+
* @param {number} [options.seenDistance=PLOUGHMAN_SEEN_DISTANCE] - Camera distance in metres at which the ploughman is built (`0` builds immediately).
|
|
98
|
+
* @param {object} [options.shapes] - Unit shapes passed to `ploughGear` (defaults to `createUnitShapes()`).
|
|
99
|
+
* @param {number | null} [options.layer=1] - Render layer index for gear and oxen (`null` leaves default layer 0).
|
|
100
|
+
* @returns {THREE.Group}
|
|
101
|
+
*/
|
|
102
|
+
export function createPloughTeam({
|
|
103
|
+
oxen,
|
|
104
|
+
random = Math.random,
|
|
105
|
+
oxMaterial = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.9 }),
|
|
106
|
+
woodMaterial = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.92 }),
|
|
107
|
+
camera = null,
|
|
108
|
+
buildFigure = null,
|
|
109
|
+
oxCoats = DEFAULT_OX_COATS,
|
|
110
|
+
ploughmanLook = DEFAULT_PLOUGHMAN_LOOK,
|
|
111
|
+
ploughmanSeed = 3587,
|
|
112
|
+
ploughmanStance = PLOUGHMAN_STANCE,
|
|
113
|
+
seenDistance = PLOUGHMAN_SEEN_DISTANCE,
|
|
114
|
+
shapes = createUnitShapes(),
|
|
115
|
+
layer = 1,
|
|
116
|
+
}) {
|
|
117
|
+
const team = new THREE.Group();
|
|
118
|
+
const builder = new GB();
|
|
119
|
+
const pair = new THREE.InstancedMesh(oxen, oxMaterial, 2);
|
|
120
|
+
[-1, 1].forEach((side, i) => {
|
|
121
|
+
pair.setMatrixAt(i, TRS(side * 0.62, 0, 3.3, 1.04, 1.04, 1.04, side * -0.03));
|
|
122
|
+
pair.setColorAt(i, _coat.setHex(oxCoats[i]));
|
|
123
|
+
});
|
|
124
|
+
pair.castShadow = pair.receiveShadow = true;
|
|
125
|
+
ploughGear(builder, random, shapes);
|
|
126
|
+
const gear = builder.mesh(woodMaterial, team);
|
|
127
|
+
if (layer !== null) {
|
|
128
|
+
gear.layers.set(layer);
|
|
129
|
+
pair.layers.set(layer);
|
|
130
|
+
}
|
|
131
|
+
team.add(pair);
|
|
132
|
+
if (buildFigure) {
|
|
133
|
+
const mount = () => team.add(buildPloughman({ buildFigure, look: ploughmanLook, seed: ploughmanSeed, stance: ploughmanStance }));
|
|
134
|
+
if (seenDistance <= 0 || !camera) {
|
|
135
|
+
mount();
|
|
136
|
+
} else {
|
|
137
|
+
gear.onBeforeRender = () => {
|
|
138
|
+
if (team.position.distanceTo(camera.position) > seenDistance) return;
|
|
139
|
+
gear.onBeforeRender = () => {};
|
|
140
|
+
mount();
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return team;
|
|
145
|
+
}
|