@wildshard/sdk 0.2.12 → 0.2.13
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/lint/tile-bake-debt.json +26 -1
- package/package.json +3 -3
- package/scripts/preparation-preview.mjs +6 -1
- package/src/shards/_template/card/landscape.jpg +0 -0
- package/src/shards/_template/card/portrait.jpg +0 -0
- package/src/shards/_template/card/thumb.jpg +0 -0
- package/src/shards/_template/data/card.json +21 -14
- package/src/shards/_template/data/clientScripts.json +27 -27
- package/src/shards/_template/data/creatures.json +5 -5
- package/src/shards/_template/data/encounters.json +2 -2
- package/src/shards/_template/data/layout.json +56 -25
- package/src/shards/_template/data/look.json +450 -220
- package/src/shards/_template/data/quests.json +2 -2
- package/src/shards/_template/data/water.json +131 -4
- package/src/shards/_template/generators/landscape.ts +19 -7
- package/src/shards/_template/generators/product.ts +7 -5
- package/src/shards/_template/generators/templatePropsSource.ts +3 -1
- package/src/shards/_template/generators/terrain.ts +28 -4
- package/src/shards/_template/generators/weather.ts +128 -0
- package/src/shards/_template/generators/world.ts +274 -98
- package/src/shards/_template/hero.json +6 -6
- package/src/shards/_template/manifest.json +21 -14
- package/src/shards/_template/assets/f2db05998615ce1cb79ea96be32c168842eb6a2b34ec01feb86d36404603ee54 +0 -0
|
@@ -4,10 +4,137 @@
|
|
|
4
4
|
"kind": "pool",
|
|
5
5
|
"level": 0,
|
|
6
6
|
"shape": {
|
|
7
|
-
"kind": "
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
"kind": "polygon",
|
|
8
|
+
"points": [
|
|
9
|
+
[
|
|
10
|
+
-12.8,
|
|
11
|
+
3.2
|
|
12
|
+
],
|
|
13
|
+
[
|
|
14
|
+
-12.855,
|
|
15
|
+
3.69
|
|
16
|
+
],
|
|
17
|
+
[
|
|
18
|
+
-13.018,
|
|
19
|
+
4.155
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
-13.28,
|
|
23
|
+
4.572
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
-13.628,
|
|
27
|
+
4.92
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
-14.045,
|
|
31
|
+
5.182
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
-14.51,
|
|
35
|
+
5.345
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
-15.0,
|
|
39
|
+
5.4
|
|
40
|
+
],
|
|
41
|
+
[
|
|
42
|
+
-23.8,
|
|
43
|
+
5.4
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
-24.29,
|
|
47
|
+
5.345
|
|
48
|
+
],
|
|
49
|
+
[
|
|
50
|
+
-24.755,
|
|
51
|
+
5.182
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
-25.172,
|
|
55
|
+
4.92
|
|
56
|
+
],
|
|
57
|
+
[
|
|
58
|
+
-25.52,
|
|
59
|
+
4.572
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
-25.782,
|
|
63
|
+
4.155
|
|
64
|
+
],
|
|
65
|
+
[
|
|
66
|
+
-25.945,
|
|
67
|
+
3.69
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
-26.0,
|
|
71
|
+
3.2
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
-26.0,
|
|
75
|
+
-3.2
|
|
76
|
+
],
|
|
77
|
+
[
|
|
78
|
+
-25.945,
|
|
79
|
+
-3.69
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
-25.782,
|
|
83
|
+
-4.155
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
-25.52,
|
|
87
|
+
-4.572
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
-25.172,
|
|
91
|
+
-4.92
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
-24.755,
|
|
95
|
+
-5.182
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
-24.29,
|
|
99
|
+
-5.345
|
|
100
|
+
],
|
|
101
|
+
[
|
|
102
|
+
-23.8,
|
|
103
|
+
-5.4
|
|
104
|
+
],
|
|
105
|
+
[
|
|
106
|
+
-15.0,
|
|
107
|
+
-5.4
|
|
108
|
+
],
|
|
109
|
+
[
|
|
110
|
+
-14.51,
|
|
111
|
+
-5.345
|
|
112
|
+
],
|
|
113
|
+
[
|
|
114
|
+
-14.045,
|
|
115
|
+
-5.182
|
|
116
|
+
],
|
|
117
|
+
[
|
|
118
|
+
-13.628,
|
|
119
|
+
-4.92
|
|
120
|
+
],
|
|
121
|
+
[
|
|
122
|
+
-13.28,
|
|
123
|
+
-4.572
|
|
124
|
+
],
|
|
125
|
+
[
|
|
126
|
+
-13.018,
|
|
127
|
+
-4.155
|
|
128
|
+
],
|
|
129
|
+
[
|
|
130
|
+
-12.855,
|
|
131
|
+
-3.69
|
|
132
|
+
],
|
|
133
|
+
[
|
|
134
|
+
-12.8,
|
|
135
|
+
-3.2
|
|
136
|
+
]
|
|
137
|
+
]
|
|
11
138
|
}
|
|
12
139
|
}
|
|
13
140
|
]
|
|
@@ -30,19 +30,31 @@ function trailDistance(x: number, z: number): number {
|
|
|
30
30
|
return Math.sqrt(distance);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* into the pond cant use shift to surface". */
|
|
37
|
-
export const POND = {
|
|
38
|
-
|
|
33
|
+
/** TEMPLATES-CLEANROOM M2: the pond is the cleanroom's rounded rectangle (layout.POND: centre, half extents, corner radius;
|
|
34
|
+
* data/water.json is the same outline as a 32-point polygon). Its bowl shelves from −0.375 m at the water's edge down to −3 m
|
|
35
|
+
* at 37°, so the 257² lattice keeps a beach inside the edge (shallower than the swim's 1.05 m stand-up). An old cliff at the
|
|
36
|
+
* edge smeared into a pit wall too steep to climb: Jake, 2026-11-12, "glitched into the pond cant use shift to surface". */
|
|
37
|
+
export const POND = { ...layout.POND, bed: -3, edge: -0.375, shelf: 0.75 } as const;
|
|
38
|
+
/** Signed distance to the pond's edge in metres: negative in the water, positive on the bank. */
|
|
39
|
+
export function pondSDF(x: number, z: number): number {
|
|
40
|
+
const qx = Math.abs(x - POND.x) - POND.hx + POND.r, qz = Math.abs(z - POND.z) - POND.hz + POND.r;
|
|
41
|
+
return Math.hypot(Math.max(qx, 0), Math.max(qz, 0)) + Math.min(Math.max(qx, qz), 0) - POND.r;
|
|
42
|
+
}
|
|
43
|
+
/** The pond's floor at signed distance `d` from its edge. */
|
|
44
|
+
export const pondFloor = (d: number): number => Math.max(POND.bed, POND.edge + d * POND.shelf);
|
|
45
|
+
/** The point on the pond's edge in direction `a` (radians from +x) from its centre, `out` metres past it. */
|
|
46
|
+
export function pondEdge(a: number, out = 0): [number, number] {
|
|
47
|
+
let r = Math.min(POND.hx, POND.hz);
|
|
48
|
+
for (let k = 0; k < 40; k++) r -= (pondSDF(POND.x + Math.cos(a) * r, POND.z + Math.sin(a) * r) - out) * 0.8;
|
|
49
|
+
return [POND.x + Math.cos(a) * r, POND.z + Math.sin(a) * r];
|
|
50
|
+
}
|
|
39
51
|
|
|
40
52
|
/** The retained Template author height law: seeded yard, pool, trail bed, flat roads and entry ramps.
|
|
41
53
|
* No runtime field, registry, render service or captured height grid is read. */
|
|
42
54
|
export function templateHeightAt(seed = 357): (x: number, z: number) => number {
|
|
43
55
|
const noise = templateNoise(seed);
|
|
44
56
|
return (x, z) => {
|
|
45
|
-
let height = Math.min(noise(x * 0.015, z * 0.015) * 0.5, pondFloor(
|
|
57
|
+
let height = Math.min(noise(x * 0.015, z * 0.015) * 0.5, pondFloor(pondSDF(x, z)));
|
|
46
58
|
height -= smoothstep(9, 2.5, trailDistance(x, z)) * 0.35;
|
|
47
59
|
height = cellGround(x, z, height);
|
|
48
60
|
const half = ROAD_WIDTH / 2 + 3;
|
|
@@ -28,7 +28,6 @@ import idle from '../data/clientScripts.json' with { type: 'json' };
|
|
|
28
28
|
// Skin projections belong to this build-time declaration, with no runtime wrapper.
|
|
29
29
|
const TEMPLATE_SKIN_LOOKS = skins.map(entry => ({ id: entry.look, species: entry.species, recipe: 'platform.skin', material: null, parameters: { skin: entry.skin }, animation: { recipe: 'platform.clips', parameters: { attackSpan: 1 } } }));
|
|
30
30
|
const TEMPLATE_SKIN_FILES = skins.flatMap(entry => entry.files), TEMPLATE_SKIN_LIBRARY = skins.map(entry => entry.skin);
|
|
31
|
-
const LF110_LUT = { hash: 'f2db05998615ce1cb79ea96be32c168842eb6a2b34ec01feb86d36404603ee54', kind: 'binary', compressed: 143_748, decoded: 143_748, gpu: 143_748, triangles: 0, draws: 0, dependencies: [], critical: false } as const;
|
|
32
31
|
const TEMPLATE_SKIN_COST = TEMPLATE_SKIN_FILES.reduce((sum, file) => ({ resident: sum.resident + file.decoded + file.gpu, compressed: sum.compressed + file.compressed }), { resident: 0, compressed: 0 });
|
|
33
32
|
|
|
34
33
|
/** The particle anchors over braziers A, B and C, in `layout.BRAZIERS` order. */
|
|
@@ -42,9 +41,12 @@ const MH4_LIBRARY_RESIDENT = 60_000;
|
|
|
42
41
|
/** LF1-9: the rim braziers' flame panel and the Warden's tell ring and core panels (props.library). Measured on the build:
|
|
43
42
|
* 63 092 resident, 33 512 compressed (14 244 + 15 116 + 4 152); these leave a margin. */
|
|
44
43
|
const LF19_LIBRARY = { resident: 70_000, compressed: 36_000 } as const;
|
|
44
|
+
/** TEMPLATES-CLEANROOM M2: the switchable panels (moss gate, arena gate, pond stones) carry coursed masonry and weathered
|
|
45
|
+
* vertex colours now; their share of the resident library budget. */
|
|
46
|
+
const M2_MASONRY_RESIDENT = 300_000;
|
|
45
47
|
// The idle reserve retains three pairs of Wasm pages and six particle slots.
|
|
46
48
|
const TEMPLATE_ROWS = parseRows(rows);
|
|
47
|
-
/** LF1-5: every props GLB names its surface (templatePropsSource.ts). The stone
|
|
49
|
+
/** LF1-5: every props GLB names its surface (templatePropsSource.ts). The stone draws in the look's plain `pbr` (M2: no measure layer); the red
|
|
48
50
|
* mushroom caps draw in the look's emissive `mossgate.mushroom-cap` (LF1-2: it keeps its colour under the measure look). */
|
|
49
51
|
const TEMPLATE_PROP_MATERIALS = { [TEMPLATE_STONE]: { id: 'pbr' }, [TEMPLATE_MUSHROOM_CAP]: { id: TEMPLATE_MUSHROOM_CAP }, [TEMPLATE_LAMP]: { id: TEMPLATE_LAMP }, [TEMPLATE_FLAME]: { id: TEMPLATE_FLAME }, [TEMPLATE_TELL]: { id: TEMPLATE_TELL } };
|
|
50
52
|
/** Assemble the original authored product from this build's terrain rows. */
|
|
@@ -88,11 +90,11 @@ const TERRAIN_DRAW = { shadow: { cast: true, receive: true }, follow: 5 } as con
|
|
|
88
90
|
// Retain the old producer's JSON boundary, including signed-zero normalization.
|
|
89
91
|
// oxlint-disable-next-line unicorn/prefer-structured-clone -- JSON wire normalization, not cloning: structuredClone retains forbidden signed zero.
|
|
90
92
|
return parseShardfile(JSON.parse(JSON.stringify({ ...base, accent: 'sand', manifest: CARD,
|
|
91
|
-
budgets: { library: { resident: 100_000 + TEMPLATE_SKIN_COST.resident + CLIENT_IDLE_RESIDENT + 2_633_280 + MH3_LIBRARY_RESIDENT + MH4_LIBRARY_RESIDENT + LF19_LIBRARY.resident +
|
|
93
|
+
budgets: { library: { resident: 100_000 + TEMPLATE_SKIN_COST.resident + CLIENT_IDLE_RESIDENT + 2_633_280 + MH3_LIBRARY_RESIDENT + MH4_LIBRARY_RESIDENT + LF19_LIBRARY.resident + M2_MASONRY_RESIDENT, compressed: 100_000 + CLIENT_IDLE_BYTES + TEMPLATE_SKIN_COST.compressed + LF19_LIBRARY.compressed }, sim: { resident: 16_000_000, compressed: 1_000_000 - boss.compressed }, overlap: 0 },
|
|
92
94
|
serverBudget: { ...base.serverBudget, tickMicros: 5_000, memory: 16_000_000 },
|
|
93
95
|
look: TEMPLATE_LOOK, spawn: SPAWN, rows: { ...TEMPLATE_ROWS, looks: TEMPLATE_SKIN_LOOKS }, plumbing: TEMPLATE_PLUMBING, ui: TEMPLATE_UI,
|
|
94
|
-
terrain: { ...terrain.terrain, ...TERRAIN_DRAW }, tiles, files: [...terrain.files, ...props.files, ...TEMPLATE_SKIN_FILES,
|
|
95
|
-
library: [...props.library, ...TEMPLATE_SKIN_LIBRARY, CLIENT_IDLE_HASH
|
|
96
|
+
terrain: { ...terrain.terrain, ...TERRAIN_DRAW }, tiles, files: [...terrain.files, ...props.files, ...TEMPLATE_SKIN_FILES, script, itemScript, clientScript], edge: terrain.edge,
|
|
97
|
+
library: [...props.library, ...TEMPLATE_SKIN_LIBRARY, CLIENT_IDLE_HASH], far: props.far, props: { ...props.props, materials: TEMPLATE_PROP_MATERIALS }, items: ITEMS, targets: TEMPLATE_TARGETS, clientScripts,
|
|
96
98
|
critical: [...terrain.critical, door.hash, itemModule], water: TEMPLATE_WATER, creatures: CREATURES, encounters: ENCOUNTERS.encounters,
|
|
97
99
|
quests: TEMPLATE_QUESTS, directors: TEMPLATE_DIRECTORS, ledger: TEMPLATE_LEDGER, audio: TEMPLATE_AUDIO, state: hooks.state, hooks: hooks.hooks,
|
|
98
100
|
sim: { ...base.sim, scriptTickDivisor: 1, scripts: [door.hash, itemModule], bindings: [{ module: door.hash, entity: playerHandle, actorId: 'actor.player', kind: 'server' }] },
|
|
@@ -4,6 +4,7 @@ import { templateScatterRandom } from './landscapeNoise';
|
|
|
4
4
|
import { buildCellFill, cellCoarse, cellFar, CELL_PIECES } from './cell';
|
|
5
5
|
import { brazierFlame, buildTemplateWorld, hutLamp, jumpCoursePads, lanternModel, mushroomCaps, rimFlames, wardenTell, type TemplatePart } from './world';
|
|
6
6
|
import type { PropsBakeSource } from '@wildshard/sdk/bake/props';
|
|
7
|
+
import { plainWalls } from './weather';
|
|
7
8
|
|
|
8
9
|
/** LF1-5: the GLB material names `props.materials` binds (product.ts): the measure stone every merged shape draws in, and the
|
|
9
10
|
* red mushroom caps' emissive surface. */
|
|
@@ -25,7 +26,7 @@ export function templateProps(count = 20): { source: PropsBakeSource; original:
|
|
|
25
26
|
buildTemplateWorld({ root: original, scope, piece, rng: { stream: () => random }, heightAt: templateHeightAt() }, count);
|
|
26
27
|
const cell = buildCellFill({ root: original, scope, piece });
|
|
27
28
|
const shape = (id: string): Object3D => { const object = pieces.find(part => part.id === id)?.object; if (object === undefined) throw new Error(`Missing template shape ${id}`); return object; };
|
|
28
|
-
const staticRoot = new Group(); staticRoot.add(shape('template.hut').clone(), shape('template.ramp').clone(), shape('template.braziers').clone(), shape('template.dressing').clone(), shape('template.foliage').clone(), shape('template.mossgate-frame').clone(), shape('template.mossgate-vines').clone(), shape('template.arena-wall').clone(), shape('template.arena-dressing').clone(), shape('template.pond-stones-sunk').clone(), shape('template.pond-planting').clone());
|
|
29
|
+
const staticRoot = new Group(); staticRoot.add(shape('template.hut').clone(), shape('template.ramp').clone(), shape('template.braziers').clone(), shape('template.dressing').clone(), shape('template.foliage').clone(), shape('template.mossgate-frame').clone(), shape('template.mossgate-vines').clone(), shape('template.arena-wall').clone(), shape('template.arena-dressing').clone(), shape('template.pond-stones-sunk').clone(), shape('template.pond-planting').clone(), shape('template.yard-walls').clone(), shape('template.hut-interior').clone());
|
|
29
30
|
// G220's cell fill: drawn whole near; the coarse tiles and the far proxy keep its big shapes (no stripes, posts or cubes)
|
|
30
31
|
const cellIds = CELL_PIECES, coarse = new Group(), far = new Group();
|
|
31
32
|
for (const id of cellIds) { staticRoot.add(shape(id).clone()); coarse.add(cellCoarse(shape(id))); far.add(cellFar(shape(id))); }
|
|
@@ -35,6 +36,7 @@ export function templateProps(count = 20): { source: PropsBakeSource; original:
|
|
|
35
36
|
// opus-greybox: the legacy grey scatter cubes (`template.props`, 0.6 m measure boxes on the pond's banks and the yard) are
|
|
36
37
|
// not baked at all now (LF1-9 had dropped them from tile 0/3/4 only). The world still builds them, so its seeded stream
|
|
37
38
|
// and everything placed after them stay put; they had no collider, so no walk changes.
|
|
39
|
+
plainWalls(coarse); plainWalls(far); // TEMPLATES-CLEANROOM M2: coursed walls stay plain boxes at range
|
|
38
40
|
const door = shape('template.door');
|
|
39
41
|
// SF16 relocates the course into the cell; this fixture records its same three pads at a bounded practice location.
|
|
40
42
|
const course = jumpCoursePads(30, 90);
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { templateHeightAt } from './landscape';
|
|
1
|
+
import { pondSDF, templateHeightAt } from './landscape';
|
|
2
|
+
import { templateNoise } from './landscapeNoise';
|
|
2
3
|
import { bakeTerrain, type BakedTerrain } from '@wildshard/sdk/bake/terrain';
|
|
3
|
-
import { Color } from 'three';
|
|
4
|
+
import { Color, SRGBColorSpace } from 'three';
|
|
4
5
|
import layout from '../data/layout.json' with { type: 'json' };
|
|
5
6
|
|
|
6
7
|
/** Trusted build-time landscape: the legacy seed/noise/trail/pool, levelled under G220's roads and plazas (`cellGround`); no callback enters the shardfile. */
|
|
7
8
|
export function templateTerrain(): BakedTerrain & { sites: { door: [number, number, number]; braziers: [number, number, number][]; lamplighter: [number, number, number] } } {
|
|
8
9
|
const heightAt = templateHeightAt();
|
|
9
|
-
const
|
|
10
|
-
return { ...bakeTerrain({ heightAt, colourAt
|
|
10
|
+
const colourAt = mossFloor();
|
|
11
|
+
return { ...bakeTerrain({ heightAt, colourAt, family: 'pbr' }), sites: { door: [layout.HUT.x, heightAt(layout.HUT.x, layout.HUT.z) + 1.2, layout.HUT.doorZ], braziers: braziers(heightAt),
|
|
11
12
|
// MH-5: the Lamplighter's feet, on the ground (a talk's `at` is where its figure stands)
|
|
12
13
|
lamplighter: [layout.LAMPLIGHTER.x, heightAt(layout.LAMPLIGHTER.x, layout.LAMPLIGHTER.z), layout.LAMPLIGHTER.z] } };
|
|
13
14
|
}
|
|
@@ -15,3 +16,26 @@ export function templateTerrain(): BakedTerrain & { sites: { door: [number, numb
|
|
|
15
16
|
function braziers(heightAt: (x: number, z: number) => number): [number, number, number][] {
|
|
16
17
|
return layout.BRAZIERS.map(([x = 0, z = 0]): [number, number, number] => [x, heightAt(x, z) + 1.7, z]);
|
|
17
18
|
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* TEMPLATES-CLEANROOM M2: the cleanroom's moss floor as vertex colours (no texture): dark, mid and light olive moss mixed
|
|
22
|
+
* by a broad and a clumpy noise, a warm dirt ring round the pond. sRGB palette, returned linear.
|
|
23
|
+
*/
|
|
24
|
+
function mossFloor(): (x: number, z: number) => [number, number, number] {
|
|
25
|
+
const big = templateNoise(11), clump = templateNoise(12), drift = templateNoise(13);
|
|
26
|
+
const dark = [72, 90, 36], mid = [102, 120, 48], lite = [134, 146, 64], dirt = [120, 104, 80];
|
|
27
|
+
const clamp = (x: number): number => Math.min(1, Math.max(0, x));
|
|
28
|
+
const mix = (a: number[], b: number[], t: number): number[] => a.map((x, i) => x + ((b[i] ?? 0) - x) * t);
|
|
29
|
+
const c = new Color();
|
|
30
|
+
return (x, z) => {
|
|
31
|
+
const b = big(x * 0.06, z * 0.06) * 0.5 + 0.5, k = clump(x * 0.21, z * 0.21) * 0.5 + 0.5, w = drift(x * 0.018, z * 0.018) * 0.5 + 0.5;
|
|
32
|
+
let col = mix(dark, mid, clamp(b * 1.5 - 0.15));
|
|
33
|
+
col = mix(col, lite, clamp((k - 0.5) * 2.5));
|
|
34
|
+
const tint = [0.96 + (b - 0.5) * 0.3, 0.98 + (b - 0.5) * 0.22, 0.86 + (w - 0.5) * 0.3];
|
|
35
|
+
col = col.map((v, i) => v * (tint[i] ?? 1) * (0.9 + k * 0.15));
|
|
36
|
+
const shore = pondSDF(x, z); // the water's edge (data/water.json)
|
|
37
|
+
if (shore < 1.2) col = mix(col, dirt, clamp((1.2 - shore) / 1.6));
|
|
38
|
+
c.setRGB(clamp((col[0] ?? 0) / 255), clamp((col[1] ?? 0) / 255), clamp((col[2] ?? 0) / 255), SRGBColorSpace);
|
|
39
|
+
return [c.r, c.g, c.b];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { BoxGeometry, BufferGeometry, Color, Float32BufferAttribute, Matrix3, Mesh, MeshStandardMaterial, SphereGeometry, Vector3, type Object3D } from 'three';
|
|
2
|
+
import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
|
|
3
|
+
import { templateNoise } from './landscapeNoise';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* TEMPLATES-CLEANROOM M2: the cleanroom's weathering, baked as vertex colours (the plain PBR look reads them; no texture).
|
|
7
|
+
* Every mesh under `root` gets, in world space: a stone mottle (two noises), moss creeping over the tops of grey stone
|
|
8
|
+
* faces, darker courses every half metre on stone walls, a grain on wood, clumps on moss and leaves, and a contact shade
|
|
9
|
+
* near the ground. The colours are multipliers on the mesh's own colour, which the bake keeps (vertex colour × colour).
|
|
10
|
+
*/
|
|
11
|
+
export function weather(root: Object3D, heightAt: (x: number, z: number) => number): void {
|
|
12
|
+
const mottle = templateNoise(21), speck = templateNoise(22), creep = templateNoise(23), grain = templateNoise(24);
|
|
13
|
+
const seen = new Set<BufferGeometry>(), p = new Vector3(), n = new Vector3(), base = new Color(), hsl = { h: 0, s: 0, l: 0 };
|
|
14
|
+
const MOSS = new Color(0x546826);
|
|
15
|
+
root.updateMatrixWorld(true);
|
|
16
|
+
root.traverse((node) => {
|
|
17
|
+
if (!(node instanceof Mesh) || !(node.material instanceof MeshStandardMaterial) || !(node.geometry instanceof BufferGeometry)) return;
|
|
18
|
+
if (node.material.emissiveIntensity > 0 && node.material.emissive.getHex() !== 0) return;
|
|
19
|
+
if (node.material.transparent) return;
|
|
20
|
+
let geometry: BufferGeometry = node.geometry;
|
|
21
|
+
if (seen.has(geometry)) { geometry = geometry.clone(); node.geometry = geometry; }
|
|
22
|
+
seen.add(geometry);
|
|
23
|
+
if (!geometry.hasAttribute('normal')) geometry.computeVertexNormals();
|
|
24
|
+
base.copy(node.material.color); base.getHSL(hsl);
|
|
25
|
+
geometry.computeBoundingBox(); const tall = geometry.boundingBox === null ? 1 : (geometry.boundingBox.max.y - geometry.boundingBox.min.y) * node.matrixWorld.getMaxScaleOnAxis();
|
|
26
|
+
const kind = hsl.s < 0.16 ? 'stone' : hsl.h > 0.05 && hsl.h < 0.12 && hsl.l < 0.45 ? 'wood' : hsl.h > 0.17 && hsl.h < 0.42 ? 'green' : 'other';
|
|
27
|
+
const pos = geometry.getAttribute('position'), nor = geometry.getAttribute('normal'), old = geometry.hasAttribute('color') ? geometry.getAttribute('color') : null;
|
|
28
|
+
const out = new Float32Array(pos.count * 3), normalMatrix = new Matrix3().getNormalMatrix(node.matrixWorld);
|
|
29
|
+
for (let i = 0; i < pos.count; i++) {
|
|
30
|
+
p.fromBufferAttribute(pos, i).applyMatrix4(node.matrixWorld);
|
|
31
|
+
n.fromBufferAttribute(nor, i).applyMatrix3(normalMatrix).normalize();
|
|
32
|
+
const m = mottle(p.x * 0.55 + p.y * 0.3, p.z * 0.55 - p.y * 0.3) * 0.5 + 0.5, s = speck(p.x * 3.1 + p.y * 1.7, p.z * 3.1 + p.y * 2.3);
|
|
33
|
+
let r = 1, g = 1, b = 1;
|
|
34
|
+
if (kind === 'stone') {
|
|
35
|
+
const k = 0.8 + m * 0.34 + s * 0.06; r = k; g = k; b = k * 0.98;
|
|
36
|
+
const course = Math.abs(((p.y + 1000) % 0.5) - 0.25) > 0.22 && Math.abs(n.y) < 0.5 ? 0.82 : 1; r *= course; g *= course; b *= course;
|
|
37
|
+
const c = creep(p.x * 0.4, p.z * 0.4) * 0.5 + 0.5 + (n.y > 0.55 ? (tall < 0.4 ? -1 : 0.18) : n.y > -0.2 ? Math.max(0, 0.2 - (p.y - heightAt(p.x, p.z)) * 0.06) : -1);
|
|
38
|
+
const t = Math.min(1, Math.max(0, (c - 0.62) * 4));
|
|
39
|
+
if (t > 0) { const k2 = 0.8 + s * 0.2; r += (MOSS.r * k2 / Math.max(base.r, 0.02) - r) * t; g += (MOSS.g * k2 / Math.max(base.g, 0.02) - g) * t; b += (MOSS.b * k2 / Math.max(base.b, 0.02) - b) * t; }
|
|
40
|
+
} else if (kind === 'wood') {
|
|
41
|
+
const k = 0.82 + (grain(p.x * 0.6 + p.z * 0.6, p.y * 9) * 0.5 + 0.5) * 0.3; r = k; g = k; b = k;
|
|
42
|
+
} else if (kind === 'green') {
|
|
43
|
+
const k = 0.78 + m * 0.36 + s * 0.08; r = k * (0.96 + m * 0.08); g = k; b = k * (0.9 + m * 0.1);
|
|
44
|
+
} else {
|
|
45
|
+
const k = 0.9 + m * 0.18; r = k; g = k; b = k;
|
|
46
|
+
}
|
|
47
|
+
// contact shade: the last 0.6 m above the ground darkens a little (the cleanroom's AO)
|
|
48
|
+
const above = p.y - heightAt(p.x, p.z), shade = 0.72 + 0.28 * Math.min(1, Math.max(0, above / 0.6));
|
|
49
|
+
out[i * 3] = r * shade * (old?.getX(i) ?? 1); out[i * 3 + 1] = g * shade * (old?.getY(i) ?? 1); out[i * 3 + 2] = b * shade * (old?.getZ(i) ?? 1);
|
|
50
|
+
}
|
|
51
|
+
geometry.setAttribute('color', new Float32BufferAttribute(out, 3));
|
|
52
|
+
node.material.vertexColors = true;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A w × h × d box cut into roughly `step`-metre quads (at most 8 a side), so the baked weathering has vertices to sit on. */
|
|
57
|
+
export function surfaceBox(w: number, h: number, d: number, step = 1e9): BoxGeometry {
|
|
58
|
+
const seg = (x: number): number => Math.min(8, Math.max(1, Math.round(x / step)));
|
|
59
|
+
return new BoxGeometry(w, h, d, seg(w), seg(h), seg(d));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The cleanroom's tree crown: a smooth bumpy ball (broad lumps and ridged leaf clumps on an indexed sphere, so its normals
|
|
64
|
+
* stay smooth), lit from above with a darker underside, the light and dark baked as vertex-colour multipliers.
|
|
65
|
+
*/
|
|
66
|
+
export function lumpyCrown(r: number, seed: number): BufferGeometry {
|
|
67
|
+
const big = templateNoise(31 + seed), lumps = templateNoise(32 + seed), fine = templateNoise(33 + seed);
|
|
68
|
+
const g = new SphereGeometry(1, 28, 18), p = g.getAttribute('position'), c = new Float32Array(p.count * 3), v = new Vector3();
|
|
69
|
+
for (let i = 0; i < p.count; i++) {
|
|
70
|
+
v.fromBufferAttribute(p, i);
|
|
71
|
+
const b = big(v.x * 1.6 + 5, v.z * 1.6 + v.y * 1.3) * 0.5 + 0.5;
|
|
72
|
+
const l = 1 - Math.abs(lumps(v.x * 4.4 + 11, v.y * 4.4 + v.z * 3.3)); // ridged: rounded leaf clumps
|
|
73
|
+
const f = fine(v.x * 12 + 1, v.y * 12 + v.z * 10) * 0.5 + 0.5;
|
|
74
|
+
const pole = Math.abs(v.y) > 0.999 ? 0 : 1; // the seam's poles stay shut
|
|
75
|
+
v.multiplyScalar(r * (0.9 + pole * ((b - 0.5) * 0.24 + l * 0.12 + f * 0.03))); v.y *= 0.92;
|
|
76
|
+
p.setXYZ(i, v.x, v.y, v.z);
|
|
77
|
+
const sun = 0.42 + 0.58 * Math.min(1, Math.max(0, (v.y / r + 0.95) / 1.8)), leaf = 0.82 + l * 0.24 + (f - 0.5) * 0.18;
|
|
78
|
+
c[i * 3] = sun * leaf; c[i * 3 + 1] = sun * leaf; c[i * 3 + 2] = sun * leaf * 0.9;
|
|
79
|
+
}
|
|
80
|
+
g.setAttribute('color', new Float32BufferAttribute(c, 3)); g.deleteAttribute('uv'); g.computeVertexNormals();
|
|
81
|
+
return g;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The cleanroom's masonry: a w × h × d wall box built as staggered courses of half-metre blocks, each a little proud or
|
|
86
|
+
* shy of the face and tinted on its own, over a dark mortar core (one merged geometry; colours are multipliers).
|
|
87
|
+
*/
|
|
88
|
+
export function coursedBox(w: number, h: number, d: number, seed = 0): BufferGeometry {
|
|
89
|
+
const alongX = w >= d, len = alongX ? w : d, thick = alongX ? d : w;
|
|
90
|
+
const rows = Math.max(2, Math.round(h / 0.5)), rowH = h / rows, gap = 0.035;
|
|
91
|
+
let s = (seed * 9301 + 49297) % 233280; const rand = (): number => { s = (s * 9301 + 49297) % 233280; return s / 233280; };
|
|
92
|
+
const parts: BufferGeometry[] = [];
|
|
93
|
+
const add = (g: BufferGeometry, x: number, y: number, z: number, k: number): void => {
|
|
94
|
+
g.translate(x, y, z); g.deleteAttribute('uv');
|
|
95
|
+
const n = g.getAttribute('position').count, c = new Float32Array(n * 3);
|
|
96
|
+
for (let i = 0; i < n; i++) { c[i * 3] = k * 1.01; c[i * 3 + 1] = k; c[i * 3 + 2] = k * 0.97; }
|
|
97
|
+
g.setAttribute('color', new Float32BufferAttribute(c, 3)); parts.push(g);
|
|
98
|
+
};
|
|
99
|
+
// the mortar core, shy of both faces
|
|
100
|
+
const core = alongX ? new BoxGeometry(w - 0.02, h - 0.02, Math.max(0.05, d - 0.07)) : new BoxGeometry(Math.max(0.05, w - 0.07), h - 0.02, d - 0.02);
|
|
101
|
+
add(core, 0, 0, 0, 0.5);
|
|
102
|
+
for (let r = 0; r < rows; r++) {
|
|
103
|
+
let at = -len / 2 - (r % 2 === 1 ? 0.45 : 0) + 0.0001;
|
|
104
|
+
while (at < len / 2 - 0.05) {
|
|
105
|
+
const want = 0.75 + rand() * 0.55, a = Math.max(at, -len / 2), b = Math.min(at + want, len / 2);
|
|
106
|
+
at += want;
|
|
107
|
+
if (b - a < 0.12) continue;
|
|
108
|
+
const proud = (rand() - 0.5) * 0.05, bl = b - a - gap, bh = rowH - gap, bd = thick + proud;
|
|
109
|
+
const g = alongX ? new BoxGeometry(bl, bh, bd) : new BoxGeometry(bd, bh, bl);
|
|
110
|
+
const mid = (a + b) / 2, y = -h / 2 + rowH * (r + 0.5);
|
|
111
|
+
add(g, alongX ? mid : 0, y, alongX ? 0 : mid, 0.84 + rand() * 0.28);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const merged = mergeGeometries(parts); for (const p of parts) p.dispose();
|
|
115
|
+
if (merged === null) throw new Error('coursedBox: merge failed');
|
|
116
|
+
return merged;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The coarse tiles and the far proxy keep a coursed wall as its plain box (`userData.plainBox`), so their caps hold. */
|
|
120
|
+
export function plainWalls(root: Object3D): void {
|
|
121
|
+
root.traverse((node) => {
|
|
122
|
+
const size: unknown = node.userData.plainBox;
|
|
123
|
+
if (!(node instanceof Mesh) || !Array.isArray(size)) return;
|
|
124
|
+
const [w = 1, h = 1, d = 1] = size as number[], g = new BoxGeometry(w, h, d), n = g.getAttribute('position').count;
|
|
125
|
+
g.setAttribute('color', new Float32BufferAttribute(new Float32Array(n * 3).fill(0.85), 3));
|
|
126
|
+
node.geometry = g;
|
|
127
|
+
});
|
|
128
|
+
}
|