agent-manifesto-kit 1.8.1 → 1.8.2

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.
Files changed (38) hide show
  1. package/README.md +18 -0
  2. package/collection/skills/concept-asset-builder/SKILL.md +124 -0
  3. package/collection/skills/concept-asset-builder/agents/openai.yaml +4 -0
  4. package/collection/skills/concept-asset-builder/assets/templates/agent_brief.md +56 -0
  5. package/collection/skills/concept-asset-builder/assets/templates/rubric.json +50 -0
  6. package/collection/skills/concept-asset-builder/assets/templates/rubric_example_vending.json +14 -0
  7. package/collection/skills/concept-asset-builder/assets/templates/scene_style_example.md +30 -0
  8. package/collection/skills/concept-asset-builder/assets/templates/wear_description_example.md +50 -0
  9. package/collection/skills/concept-asset-builder/assets/templates/zones.json +10 -0
  10. package/collection/skills/concept-asset-builder/assets/templates/zones_example_vending.json +69 -0
  11. package/collection/skills/concept-asset-builder/references/examples/postbox_blender.py +1112 -0
  12. package/collection/skills/concept-asset-builder/references/examples/postbox_wear_three.js +742 -0
  13. package/collection/skills/concept-asset-builder/references/examples/vending_materials_blender.py +487 -0
  14. package/collection/skills/concept-asset-builder/references/examples/vending_wear_three.js +798 -0
  15. package/collection/skills/concept-asset-builder/references/field-lessons.md +36 -0
  16. package/collection/skills/concept-asset-builder/references/wear-language.md +36 -0
  17. package/collection/skills/concept-asset-builder/scripts/blender/bake_export.py +174 -0
  18. package/collection/skills/concept-asset-builder/scripts/blender/render_views.py +100 -0
  19. package/collection/skills/concept-asset-builder/scripts/blender/studio.py +73 -0
  20. package/collection/skills/concept-asset-builder/scripts/capture.mjs +44 -0
  21. package/collection/skills/concept-asset-builder/scripts/codex_review.sh +88 -0
  22. package/collection/skills/concept-asset-builder/scripts/compare_to.py +24 -0
  23. package/collection/skills/concept-asset-builder/scripts/composite_godot.py +13 -0
  24. package/collection/skills/concept-asset-builder/scripts/crop.py +9 -0
  25. package/collection/skills/concept-asset-builder/scripts/gen_texture.sh +18 -0
  26. package/collection/skills/concept-asset-builder/scripts/godot/capture.gd +122 -0
  27. package/collection/skills/concept-asset-builder/scripts/godot/project.godot +18 -0
  28. package/collection/skills/concept-asset-builder/scripts/init_workspace.sh +17 -0
  29. package/collection/skills/concept-asset-builder/scripts/measure_silhouette.py +40 -0
  30. package/collection/skills/concept-asset-builder/scripts/metrics.py +144 -0
  31. package/collection/skills/concept-asset-builder/scripts/sheet.py +19 -0
  32. package/collection/skills/concept-asset-builder/scripts/test_tools.py +121 -0
  33. package/collection/skills/concept-asset-builder/scripts/threejs/index.html +23 -0
  34. package/collection/skills/concept-asset-builder/scripts/threejs/model.js +9 -0
  35. package/collection/skills/concept-asset-builder/scripts/threejs/studio.js +178 -0
  36. package/collection/skills/concept-asset-builder/scripts/wear_stats.py +130 -0
  37. package/collection/skills/concept-scene-builder/SKILL.md +16 -3
  38. package/package.json +1 -1
@@ -0,0 +1,742 @@
1
+ // Procedural weathering for pipeline A. Source of truth: brief/wear_description.md (+ spec.wear).
2
+ // Seeded canvases in cylindrical space (u = angle around Y, v = height over a range), mixed over the albedo
3
+ // in the shader by object-space position, so body, slot box, door, cap and base share one mapping.
4
+ //
5
+ // Every canvas is built from explicit layers (one value per texel), then composited once:
6
+ // chip - paint lost down to the pinkish-beige primer: explicitly PLACED flakes (counts and sizes per part and
7
+ // view follow the description: many small, some medium, 1-2 large per view, half of them pressed to an
8
+ // edge) whose outline is an ellipse broken by 3 octaves of noise (ragged, lobed, with satellite flecks),
9
+ // plus EDGE WEAR: broken bands of primer along geometric edges (analytic distance to facet edges, ring
10
+ // lips, hood ridges, frames), gated along the edge by low-frequency noise to a target share of the edge
11
+ // length, grey-brown bare metal in the core of the widest runs;
12
+ // rim - 1-2 mm rust-brown border around every chip (dilation of the chip mask);
13
+ // rust - drip clusters (base stain 2.25x the strand span at the source, 1-5 strands with bell-shaped lengths,
14
+ // dark #5A2712 at the source -> #964623 down the strand) and rust grime in grooves (collar, ring lip,
15
+ // door panel groove, under the slot lip);
16
+ // mould - olive patches, only on the lowest 5-8 cm of the body (and on the concrete base).
17
+ import * as THREE from 'three';
18
+
19
+ const TAU = Math.PI * 2;
20
+
21
+ function mulberry32(seed) {
22
+ return () => {
23
+ seed |= 0; seed = (seed + 0x6d2b79f5) | 0;
24
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
25
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
26
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
27
+ };
28
+ }
29
+
30
+ /** u (0..1) of an object-space point, same formula as the shader. */
31
+ export const uOf = (x, z) => Math.atan2(x, z) / TAU + 0.5;
32
+
33
+ const hexRGB = (hex) => { const n = parseInt(hex.slice(1), 16); return [(n >> 16) & 255, (n >> 8) & 255, n & 255]; };
34
+ const scaleRGB = (c, k) => c.map((v) => Math.max(0, Math.min(255, v * k)));
35
+ const mixRGB = (a, b, t) => a.map((v, i) => v + (b[i] - v) * t);
36
+ const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
37
+ const clamp01 = (x) => Math.min(1, Math.max(0, x));
38
+
39
+ // ---------- 3D value noise / fbm (seamless on the cylinder because it is sampled in 3D) ----------
40
+
41
+ function hash3(x, y, z, s) {
42
+ let h = Math.imul(x, 0x8da6b343) ^ Math.imul(y, 0xd8163841) ^ Math.imul(z, 0xcb1ab31f) ^ Math.imul(s, 0x27d4eb2d);
43
+ h = Math.imul(h ^ (h >>> 13), 0x5bd1e995);
44
+ h ^= h >>> 15;
45
+ return (h >>> 0) / 4294967296;
46
+ }
47
+ export function noise3(x, y, z, s) {
48
+ const xi = Math.floor(x), yi = Math.floor(y), zi = Math.floor(z);
49
+ const xf = x - xi, yf = y - yi, zf = z - zi;
50
+ const u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf), w = zf * zf * (3 - 2 * zf);
51
+ const a = hash3(xi, yi, zi, s), b = hash3(xi + 1, yi, zi, s), c = hash3(xi, yi + 1, zi, s), d = hash3(xi + 1, yi + 1, zi, s);
52
+ const e = hash3(xi, yi, zi + 1, s), f = hash3(xi + 1, yi, zi + 1, s), g = hash3(xi, yi + 1, zi + 1, s), h = hash3(xi + 1, yi + 1, zi + 1, s);
53
+ const x1 = a + (b - a) * u, x2 = c + (d - c) * u, x3 = e + (f - e) * u, x4 = g + (h - g) * u;
54
+ const y1 = x1 + (x2 - x1) * v, y2 = x3 + (x4 - x3) * v;
55
+ return y1 + (y2 - y1) * w;
56
+ }
57
+ export function fbm(x, y, z, freq, oct, s) {
58
+ let sum = 0, amp = 1, norm = 0;
59
+ for (let o = 0; o < oct; o++) {
60
+ sum += noise3(x * freq, y * freq, z * freq, s + o * 101) * amp;
61
+ norm += amp; amp *= 0.5; freq *= 2.03;
62
+ }
63
+ return sum / norm;
64
+ }
65
+
66
+ /** Anisotropic fbm: per-axis scale (cycles/m: [horizontal, vertical]), `oct` octaves, gain = roughness. */
67
+ export function fbmA(x, y, z, sx, sy, oct, gain, s) {
68
+ let sum = 0, amp = 1, norm = 0, f = 1;
69
+ for (let o = 0; o < oct; o++) {
70
+ sum += noise3(x * sx * f, y * sy * f, z * sx * f, s + o * 101) * amp;
71
+ norm += amp; amp *= gain; f *= 2.0;
72
+ }
73
+ return sum / norm;
74
+ }
75
+
76
+ /** Quantile function of a noise field (sampled once): q(0.7) = the value 70% of the field lies below. */
77
+ function quantiles(sample, n, seed) {
78
+ const rnd = mulberry32(seed);
79
+ const a = Float64Array.from({ length: n }, () => sample(rnd)).sort();
80
+ return (q) => a[Math.min(n - 1, Math.max(0, Math.floor(q * n)))];
81
+ }
82
+
83
+ // ---------- canvas with wear layers ----------
84
+
85
+ class WearCanvas {
86
+ constructor(w, h, y0, y1, radius, seed) {
87
+ this.c = document.createElement('canvas');
88
+ this.c.width = w; this.c.height = h;
89
+ this.g = this.c.getContext('2d', { willReadFrequently: true });
90
+ Object.assign(this, { W: w, H: h, y0, y1, radius, seed });
91
+ this.rnd = mulberry32(seed);
92
+ const N = w * h;
93
+ this.chip = new Uint8Array(N); // 1 = primer, 2 = bare metal
94
+ this.rust = new Float32Array(N); // rust alpha
95
+ this.rdark = new Float32Array(N); // 0 = #964623 .. 1 = #5A2712
96
+ this.mould = new Float32Array(N); // olive alpha
97
+ }
98
+ r(a, b) { return a + this.rnd() * (b - a); }
99
+ phiAt(i) { return ((i + 0.5) / this.W - 0.5) * TAU; }
100
+ yAt(j) { return this.y1 - ((j + 0.5) / this.H) * (this.y1 - this.y0); }
101
+
102
+ /** Visit texels with centres in [phiA, phiB] x [ya, yb]; phi is passed unwrapped (continuous across ±pi). */
103
+ box(phiA, phiB, ya, yb, fn) {
104
+ const { W, H } = this;
105
+ const i0 = Math.floor((phiA / TAU + 0.5) * W - 0.5), i1 = Math.min(i0 + W - 1, Math.ceil((phiB / TAU + 0.5) * W - 0.5));
106
+ const span = this.y1 - this.y0;
107
+ const j0 = Math.max(0, Math.floor(((this.y1 - yb) / span) * H - 0.5));
108
+ const j1 = Math.min(H - 1, Math.ceil(((this.y1 - ya) / span) * H - 0.5));
109
+ for (let j = j0; j <= j1; j++) {
110
+ const y = this.yAt(j);
111
+ for (let ii = i0; ii <= i1; ii++) fn(j * W + (((ii % W) + W) % W), this.phiAt(ii), y);
112
+ }
113
+ }
114
+
115
+ /** Visit every texel with its 3D position on a cylinder of the canvas radius (for seamless noise). */
116
+ pass(fn) {
117
+ const { W, H } = this, R = this.radius;
118
+ for (let j = 0; j < H; j++) {
119
+ const y = this.yAt(j);
120
+ for (let i = 0; i < W; i++) {
121
+ const phi = this.phiAt(i);
122
+ fn(j * W + i, phi, y, Math.sin(phi) * R, Math.cos(phi) * R);
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * One paint flake down to the primer. w, h: size in surface metres (h along the canvas vertical); rr: metres per
129
+ * radian at the flake (surface radius); sy: surface metres per metre of height (1 on walls, 1/sin(slope) on the
130
+ * pyramid). Outline = ellipse broken by lobe noise (~2.6 cells per flake), ragged noise and 2 mm jag.
131
+ */
132
+ flake({ phi, y, w, h, rot = 0, rr = this.radius, sy = 1, metal = false, clip = null, amp = 1 }) {
133
+ const L = Math.max(w, h), ext = 0.9 * L;
134
+ const sd = Math.floor(this.rnd() * 1e6), ox = this.rnd() * 40, oy = this.rnd() * 40;
135
+ const f1 = 2.6 / L, f2 = 10 / L;
136
+ const c = Math.cos(rot), s = Math.sin(rot);
137
+ this.box(phi - ext / rr, phi + ext / rr, y - ext / sy, y + ext / sy, (k, p, yy) => {
138
+ const dx = (p - phi) * rr, dz = (yy - y) * sy;
139
+ const lx = dx * c + dz * s, ly = -dx * s + dz * c;
140
+ const q = Math.hypot(lx / (w / 2), ly / (h / 2));
141
+ if (q > 1.8) return;
142
+ // domain warp bends the ellipse into an irregular flake before the threshold noise tears its outline
143
+ const wx = lx + 0.22 * L * (noise3(lx * f1 * 0.7 + oy, ly * f1 * 0.7, 11.5, sd + 4) - 0.5);
144
+ const wy = ly + 0.22 * L * (noise3(lx * f1 * 0.7, ly * f1 * 0.7 + ox, 13.5, sd + 5) - 0.5);
145
+ const qw = Math.hypot(wx / (w / 2), wy / (h / 2));
146
+ const n1 = 0.65 * noise3(wx * f1 + ox, wy * f1 + oy, 0.5, sd) + 0.35 * noise3(wx * f1 * 2.1 + oy, wy * f1 * 2.1 + ox, 3.5, sd + 1);
147
+ const n2 = noise3(wx * f2 + ox, wy * f2 + oy, 7.5, sd + 2);
148
+ const n3 = noise3(lx * 480 + ox, ly * 480 + oy, 9.5, sd + 3);
149
+ const v = 1 - qw + amp * (2.2 * (n1 - 0.5) + 1.2 * (n2 - 0.5)) + 0.35 * (n3 - 0.5);
150
+ if (v <= 0 || (clip && !clip(p, yy))) return;
151
+ this.chip[k] = metal && v > 0.55 ? 2 : Math.max(this.chip[k], 1);
152
+ });
153
+ }
154
+
155
+ /** Flake + a few tiny flecks around it (spalls next to a bigger chip). */
156
+ flakeCluster(o) {
157
+ this.flake(o);
158
+ const L = Math.max(o.w, o.h), rr = o.rr ?? this.radius, sy = o.sy ?? 1;
159
+ if (L < 0.022) return;
160
+ const n = Math.floor(this.rnd() * (L > 0.045 ? 4 : 2.5));
161
+ for (let i = 0; i < n; i++) {
162
+ const a = this.r(0, TAU), d = L * this.r(0.5, 0.85), s = this.r(0.002, 0.006);
163
+ this.flake({ phi: o.phi + (Math.cos(a) * d) / rr, y: o.y + (Math.sin(a) * d * (o.h / L)) / sy, w: s, h: s * this.r(1, 1.6), rr, sy, clip: o.clip });
164
+ }
165
+ }
166
+
167
+ /** One tapering rust strand from yTop down `len` metres (stops at floorY), with a bead at a natural end. */
168
+ strand(phi0, yTop, len, w, rr, floorY, alpha) {
169
+ const pxM = this.W / (TAU * rr);
170
+ const wob = this.r(0.0004, 0.0014), ph = this.r(0, TAU), fq = this.r(3, 7);
171
+ const yEnd = Math.max(yTop - len, floorY), cut = yEnd > yTop - len;
172
+ if (yEnd >= yTop) return;
173
+ const reach = w + wob + 2 / pxM;
174
+ const sd = Math.floor(this.rnd() * 1e6);
175
+ const bead = !cut && this.rnd() < 0.45, taper = bead ? 0.3 : this.r(0.4, 0.65), fade = bead ? 0.35 : this.r(0.5, 0.75);
176
+ this.box(phi0 - reach / rr, phi0 + reach / rr, yEnd - (bead ? w : 0), yTop, (k, p, yy) => {
177
+ const depth = yTop - yy, t = Math.min(1, depth / len);
178
+ const cx = Math.sin(t * fq + ph) * wob * Math.min(1, t * 3);
179
+ const dxm = (p - phi0) * rr - cx;
180
+ const half = (w / 2) * (1 - taper * t);
181
+ let cov;
182
+ if (yy >= yEnd) cov = clamp01((half - Math.abs(dxm)) * pxM + 0.5);
183
+ else {
184
+ const rb = half * 1.3;
185
+ cov = clamp01((rb - Math.hypot(dxm, yy - (yEnd + rb * 0.3))) * pxM + 0.5);
186
+ }
187
+ if (cov <= 0) return;
188
+ const a = alpha * cov * (1 - fade * t ** 2) * (0.85 + 0.15 * noise3(depth * 300, 0.5, 0.5, sd));
189
+ if (a > this.rust[k]) this.rust[k] = a;
190
+ this.rdark[k] = Math.max(this.rdark[k], cov * Math.max(0, 0.85 - 1.1 * t));
191
+ });
192
+ }
193
+
194
+ /** Drip cluster: base stain at the source (2.25x the strand span) + n strands with a bell profile of lengths. */
195
+ cluster({ phi, y, rr = this.radius, span, stainH, maxLen, n, floorY = -Infinity, alpha = 0.95, w = [0.0058, 0.0105] }) {
196
+ const hw = span * 1.125;
197
+ const sd = Math.floor(this.rnd() * 1e6);
198
+ this.box(phi - (1.35 * hw) / rr, phi + (1.35 * hw) / rr, Math.max(floorY, y - stainH * 1.35), y, (k, p, yy) => {
199
+ const dx = (p - phi) * rr, dd = y - yy;
200
+ const q = Math.hypot(dx / hw, dd / stainH);
201
+ const v = 1 - q + 0.55 * (noise3(dx * 160, dd * 160, 0.3, sd) - 0.5);
202
+ if (v <= 0) return;
203
+ const a = Math.min(1, v / 0.3) * 0.9 * (1 - 0.25 * dd / stainH);
204
+ if (a > this.rust[k]) this.rust[k] = a;
205
+ this.rdark[k] = Math.max(this.rdark[k], 0.95 * Math.min(1, v / 0.35));
206
+ });
207
+ const asym = this.r(-0.35, 0.35), sig = this.r(0.35, 0.6);
208
+ for (let i = 0; i < n; i++) {
209
+ const t = n === 1 ? asym : (i / (n - 1)) * 2 - 1;
210
+ const tx = t + this.r(-0.12, 0.12);
211
+ const bell = Math.exp(-((tx - asym) ** 2) / (2 * sig * sig));
212
+ const len = maxLen * (0.3 + 0.7 * bell) * this.r(0.85, 1.05);
213
+ this.strand(phi + (tx * span) / 2 / rr, y - stainH * 0.4, len, this.r(w[0], w[1]), rr, floorY, alpha);
214
+ }
215
+ }
216
+
217
+ /** Clusters along a source edge [phi0, phi1] at ~`spacing` metres (jittered, some skipped). */
218
+ dripLine({ y, phi0, phi1, rr = this.radius, spacing, keep = 0.85, n = [1, 5], len, longP = 0, longLen = [0.1, 0.14],
219
+ span = [0.012, 0.026], stainH = [0.01, 0.02], floor = () => -Infinity, skip = () => false, alpha = 0.95, w }) {
220
+ let p = phi0 + (this.rnd() * spacing) / rr;
221
+ while (p < phi1) {
222
+ if (this.rnd() < keep && !skip(p)) {
223
+ const maxLen = this.rnd() < longP ? this.r(...longLen) : this.r(...len);
224
+ this.cluster({
225
+ phi: p, y, rr, span: this.r(...span), stainH: this.r(...stainH), maxLen,
226
+ n: n[0] + Math.floor(this.rnd() * (n[1] - n[0] + 1)), floorY: floor(p), alpha, ...(w ? { w } : {}),
227
+ });
228
+ }
229
+ p += (spacing * this.r(0.6, 1.4)) / rr;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Composite the layers into the canvas (straight alpha over transparent): mould, primer chips with a rust-brown
235
+ * rim, then rust on top (drips run over chips too).
236
+ */
237
+ compose({ undercoat, rim, metal = '#6E625A', rust = ['#8A3F1E', '#4E220F'], mould = ['#6B6842', '#565A3A'], ucJitter = 0.14 }) {
238
+ const { W, H, chip } = this;
239
+ const img = this.g.createImageData(W, H), d = img.data;
240
+ const uc = hexRGB(undercoat), rc = hexRGB(rim), mc = hexRGB(metal), uc2 = mixRGB(uc, [222, 204, 186], 0.2);
241
+ const r0 = hexRGB(rust[0]), r1 = hexRGB(rust[1]), m0 = hexRGB(mould[0]), m1 = hexRGB(mould[1]);
242
+ const s = this.seed;
243
+ const near = (i, j, rad) => {
244
+ for (let dj = -rad; dj <= rad; dj++) {
245
+ const jj = j + dj;
246
+ if (jj < 0 || jj >= H) continue;
247
+ for (let di = -rad; di <= rad; di++) if (chip[jj * W + (((i + di) % W) + W) % W]) return true;
248
+ }
249
+ return false;
250
+ };
251
+ for (let j = 0; j < H; j++) {
252
+ for (let i = 0; i < W; i++) {
253
+ const k = j * W + i;
254
+ let cr = 0, cg = 0, cb = 0, ca = 0;
255
+ const over = (col, a) => {
256
+ const oa = a + ca * (1 - a);
257
+ if (oa <= 0) return;
258
+ cr = (col[0] * a + cr * ca * (1 - a)) / oa; cg = (col[1] * a + cg * ca * (1 - a)) / oa; cb = (col[2] * a + cb * ca * (1 - a)) / oa;
259
+ ca = oa;
260
+ };
261
+ if (this.mould[k] > 0) over(mixRGB(m0, m1, noise3(i * 0.09, j * 0.09, 0.5, s + 7)), this.mould[k]);
262
+ if (chip[k]) {
263
+ // primer is not one flat colour: brightness jitter + patches of paler, greyer primer
264
+ const kk = 1 - ucJitter / 2 + ucJitter * noise3(i * 0.08, j * 0.08, 0.5, s + 3);
265
+ const pale = smooth(0.4, 0.75, noise3(i * 0.05, j * 0.05, 2.5, s + 4));
266
+ over(chip[k] === 2 ? mc : scaleRGB(mixRGB(uc, uc2, pale), kk), 1);
267
+ } else if (near(i, j, 1)) over(rc, 0.92);
268
+ else if (near(i, j, 2)) over(rc, 0.5);
269
+ if (this.rust[k] > 0.004) over(mixRGB(r0, r1, this.rdark[k]), this.rust[k]);
270
+ const o = k * 4;
271
+ d[o] = cr; d[o + 1] = cg; d[o + 2] = cb; d[o + 3] = Math.round(ca * 255);
272
+ }
273
+ }
274
+ this.g.putImageData(img, 0, 0);
275
+ }
276
+
277
+ texture() {
278
+ (window.__wearCanvases ??= []).push(this.c); // debug: lets tools dump the generated canvases
279
+ const t = new THREE.CanvasTexture(this.c);
280
+ t.colorSpace = THREE.SRGBColorSpace;
281
+ t.wrapS = THREE.RepeatWrapping;
282
+ t.wrapT = THREE.ClampToEdgeWrapping;
283
+ // no mipmaps: the atan() seam at the back would otherwise pick a tiny mip and draw a line
284
+ t.generateMipmaps = false;
285
+ t.minFilter = THREE.LinearFilter;
286
+ return t;
287
+ }
288
+ }
289
+
290
+ // ---------- edge geometry helpers ----------
291
+
292
+ /** Angular distance (radians) from phi to the nearest edge of a polyLathe with a flat face at +Z. */
293
+ function facetEdgeDist(phi, sides) {
294
+ const step = TAU / sides;
295
+ const a = phi + step / 2; // edges at -step/2 + k*step
296
+ const m = ((a % step) + step) % step;
297
+ return Math.min(m, step - m);
298
+ }
299
+ /** phi of the k-th vertical edge of a polyLathe with a flat face at +Z. */
300
+ const edgePhi = (k, sides) => -Math.PI / sides + (k * TAU) / sides;
301
+
302
+ /**
303
+ * Distance (m) to the vertical front corners of a box that sticks out of the body: front plane z = zf, half width
304
+ * hw, back at zb. Positive on the front face (|x| < hw) and on the side faces; Infinity outside the box's phi range.
305
+ */
306
+ function boxCornerDist(phi, hw, zf, zb) {
307
+ const a = Math.abs(phi);
308
+ if (a > Math.atan2(hw, zb)) return Infinity;
309
+ const t = Math.tan(a);
310
+ return Math.max(hw - t * zf, t > 1e-6 ? zf - hw / t : -Infinity);
311
+ }
312
+
313
+ /**
314
+ * Edge-wear sampler: a low-frequency gate along the edge keeps `cov` of the edge length worn (broken runs),
315
+ * ragged band width, bare metal in the core of the widest runs. e: 1 at the edge -> 0 at the band's max width.
316
+ */
317
+ function edgeWearSampler(seed) {
318
+ const gates = {};
319
+ const gate = (gf) => (gates[gf] ??= {
320
+ n: (x, y, z) => fbmA(x, y, z, gf, gf, 2, 0.5, seed + gf),
321
+ q: quantiles((r) => fbmA(r() * 2 - 1, r() * 2, r() * 2 - 1, gf, gf, 2, 0.5, seed + gf), 3000, seed + gf),
322
+ });
323
+ return (x, y, z, e, cov, gf = 18, metal = true) => {
324
+ if (e <= 0 || cov <= 0) return 0;
325
+ const G = gate(gf);
326
+ const thr = G.q(1 - cov);
327
+ const on = smooth(thr - 0.004, thr + 0.035, G.n(x, y, z));
328
+ if (on <= 0) return 0;
329
+ const rag = 0.6 * noise3(x * 230, y * 230, z * 230, seed + 5) + 0.4 * noise3(x * 620, y * 620, z * 620, seed + 6);
330
+ const width = on * (0.2 + 1.0 * rag);
331
+ if (e <= 1 - width) return 0;
332
+ return metal && width > 0.8 && e > 1 - width * 0.3 ? 2 : 1;
333
+ };
334
+ }
335
+
336
+ // ---------- size distribution ----------
337
+
338
+ /** Chip size by class: s < 2 cm, m 2-5 cm, l >= 5 cm (largest side). */
339
+ function sizeOf(wc, cls) {
340
+ if (cls === 's') return wc.r(0.006, 0.019);
341
+ if (cls === 'm') return wc.r(0.021, 0.046);
342
+ return wc.r(0.052, 0.085);
343
+ }
344
+ /** Vertical flake (h/w 1.3-2), the description's dominant shape. */
345
+ function vflake(wc, L) { const asp = wc.r(1.3, 2.0); return { w: L / asp, h: L }; }
346
+ /** Flake lying along a horizontal lip. */
347
+ function hflake(wc, L) { const asp = wc.r(1.4, 2.4); return { w: L, h: L / asp }; }
348
+ const classes = (s, m, l) => [...Array(s).fill('s'), ...Array(m).fill('m'), ...Array(l).fill('l')];
349
+
350
+ // ---------- body + slot box + door ----------
351
+
352
+ /** Body + slot box + door wear. Returns {texture, y0, y1}. */
353
+ export function bodyWear(spec, geo) {
354
+ const B = spec.body, R = B.radius;
355
+ const y0 = B.y0, y1 = spec.cap.slab_y0;
356
+ const wc = new WearCanvas(1536, 1024, y0, y1, R, 1234);
357
+ const [mid, cLow, cHigh] = spec.rings;
358
+ const P = spec.palette;
359
+ const Rv = R / Math.cos(Math.PI / B.sides);
360
+ const g = geo;
361
+
362
+ // ---- where the front parts sit in canvas space ----
363
+ const hoodIn = (y) => Math.min(g.hoodMaxIn, Math.max(0, (y - g.hoodY0 - g.hoodFascia) / Math.tan(g.hoodSlope)));
364
+ const inSlot = (phi, y) => y >= g.slotBottom - 0.002 && y <= g.hoodY0 && Math.abs(phi) < Math.atan2(g.slotHalfW, g.slotBack);
365
+ const inHood = (phi, y) => y >= g.hoodY0 && y <= g.hoodTop && Math.abs(phi) < Math.atan2(g.hoodHalfW - hoodIn(y), g.hoodBack);
366
+ const inDoor = (phi, y) => y >= g.doorBottom - 0.002 && y <= g.doorTop + 0.002 && Math.abs(phi) < Math.atan2(g.doorHalfW, g.doorBack);
367
+ const inMark = (phi, y) => Math.abs(y - g.markY) < g.markH / 2 + 0.006 && Math.abs(Math.tan(phi) * R) < g.markW / 2 + 0.006 && Math.abs(phi) < 1;
368
+ const plateAt = (phi, y) => spec.side_plates.some((p) => Math.abs(y - p.center_y) < p.height / 2 + 0.01
369
+ && Math.abs(Math.abs(phi) - Math.PI / 2) < (p.width / 2 + 0.01) / R);
370
+ const onPart = (phi, y) => inSlot(phi, y) || inHood(phi, y) || inDoor(phi, y);
371
+ const bodyOnly = (phi, y) => !onPart(phi, y) && !inMark(phi, y);
372
+ // texels on the slot box / door front planes: x = tan(phi) * z
373
+ const xOn = (phi, z) => Math.tan(phi) * z;
374
+
375
+ // ================= chips =================
376
+ const place = (cls, zone, edgeShare, opts = {}) => {
377
+ for (const c of cls) {
378
+ const L = sizeOf(wc, c);
379
+ const { w, h } = vflake(wc, L);
380
+ let phi, y, tries = 0;
381
+ do {
382
+ y = wc.r(zone[0], zone[1]);
383
+ if (wc.rnd() < edgeShare) {
384
+ // pressed to a facet edge: the flake straddles or touches it
385
+ const k = Math.floor(wc.rnd() * B.sides);
386
+ phi = edgePhi(k, B.sides) + ((wc.rnd() < 0.5 ? -1 : 1) * wc.r(0.1, 0.55) * w) / R;
387
+ } else phi = wc.r(-Math.PI, Math.PI);
388
+ } while ((!bodyOnly(phi, y) || plateAt(phi, y) || (opts.avoid && opts.avoid(phi, y))) && ++tries < 50);
389
+ wc.flakeCluster({ phi, y, w, h, rot: wc.r(-0.25, 0.25), clip: bodyOnly });
390
+ }
391
+ };
392
+ // body upper (between the mid ring and the collar): ~65% small, ~28% medium, 1-2 large per view
393
+ place(classes(8, 2, 0), [mid.y1 + 0.012, cLow.y0 - 0.012], 0.3);
394
+ // body lower: mostly small, ~one medium per view
395
+ place(classes(8, 1, 0), [B.y0 + 0.035, mid.y0 - 0.012], 0.3);
396
+ // hero flakes where the concept has them (front: right of the mark / lower right / left; back: upper right pair)
397
+ const heroes = [
398
+ // [phi, y, w, h] - front view: +phi is the viewer's right; back view: phi in (-pi, -pi/2) is the viewer's right
399
+ [0.6, 0.835, 0.032, 0.058], [-0.66, 0.775, 0.018, 0.03], [0.62, 0.36, 0.034, 0.062],
400
+ [-2.66, 0.985, 0.042, 0.08], [-2.42, 0.895, 0.03, 0.05], [2.55, 0.55, 0.026, 0.045],
401
+ ];
402
+ for (const [phi, y, w, h] of heroes) wc.flakeCluster({ phi, y, w, h, rot: wc.r(-0.2, 0.2), clip: bodyOnly });
403
+
404
+ // mid ring: the most worn band - small/medium ragged chips on its upper and lower lips (1-4 cm, max ~5.5)
405
+ for (const c of classes(8, 3, 0)) {
406
+ const L = c === 's' ? wc.r(0.008, 0.018) : wc.r(0.02, wc.rnd() < 0.15 ? 0.05 : 0.034);
407
+ const { w, h } = hflake(wc, L);
408
+ const top = wc.rnd() < 0.5;
409
+ const y = top ? mid.y1 - wc.r(0.002, 0.008) : mid.y0 + wc.r(0.002, 0.008);
410
+ wc.flake({ phi: wc.r(-Math.PI, Math.PI), y, w, h: Math.min(h, 0.016), rr: mid.radius, rot: wc.r(-0.15, 0.15), clip: (p, yy) => !inDoor(p, yy) });
411
+ }
412
+ // collar: almost no chips (< 1%), only small ones on the lips
413
+ for (let i = 0; i < 7; i++) {
414
+ const L = wc.r(0.005, 0.014);
415
+ const lips = [cLow.y0 + 0.004, cLow.y1 - 0.003, cHigh.y0 + 0.003, cHigh.y1 - 0.003];
416
+ wc.flake({ phi: wc.r(-Math.PI, Math.PI), y: lips[Math.floor(wc.rnd() * 4)], ...hflake(wc, L), rr: cLow.radius });
417
+ }
418
+
419
+ // slot box: 7-11 small (0.8-2 cm) + 2-3 medium (to 3.6 cm), on the frame edges and corners
420
+ const slotFront = (p, yy) => inSlot(p, yy) || inHood(p, yy);
421
+ const slotSpots = () => {
422
+ const side = wc.rnd();
423
+ const hw = g.slotHalfW;
424
+ if (side < 0.35) return [(wc.rnd() < 0.5 ? -1 : 1) * (hw - wc.r(0.002, 0.012)), wc.r(g.slotBottom + 0.01, g.hoodY0 - 0.005)]; // side posts
425
+ if (side < 0.65) return [wc.r(-hw + 0.01, hw - 0.01), g.slotBottom + wc.r(0.002, 0.012)]; // bottom rail
426
+ if (side < 0.85) return [(wc.rnd() < 0.5 ? -1 : 1) * (hw - wc.r(0.003, 0.012)), g.slotBottom + wc.r(0.003, 0.014)]; // corners
427
+ return [wc.r(-hw + 0.01, hw - 0.01), wc.r(g.openingTop + 0.004, g.hoodY0 - 0.004)]; // above the opening
428
+ };
429
+ for (const c of [...classes(9, 0, 0), 'm']) {
430
+ const L = c === 's' ? wc.r(0.008, 0.015) : wc.r(0.021, 0.026);
431
+ const [x, y] = slotSpots();
432
+ wc.flakeCluster({ phi: Math.atan2(x, g.slotFront), y, ...vflake(wc, L), rr: g.slotFront, clip: slotFront });
433
+ }
434
+ // hood slopes: 1-3 small chips (edges are worn by the edge pass)
435
+ for (let i = 0; i < 2; i++) {
436
+ const y = wc.r(g.hoodY0 + g.hoodFascia + 0.008, g.hoodTop - 0.01);
437
+ const x = wc.r(-1, 1) * (g.hoodHalfW - hoodIn(y) - 0.015);
438
+ wc.flake({ phi: Math.atan2(x, g.hoodFront - hoodIn(y)), y, ...vflake(wc, wc.r(0.007, 0.015)), rr: g.hoodFront, sy: 2, clip: inHood });
439
+ }
440
+ // door: ~5 small + up to 5 medium (1-3 cm) on the outer frame and its corners, the panel stays almost clean
441
+ const onFrame = (p, yy) => inDoor(p, yy) && !(Math.abs(xOn(p, g.doorFront)) < g.doorInnerHalfW - 0.001 && yy > g.doorInnerBottom + 0.001 && yy < g.doorInnerTop - 0.001);
442
+ for (const c of [...classes(6, 0, 0), 'm']) {
443
+ const L = c === 's' ? wc.r(0.005, 0.012) : wc.r(0.02, 0.024);
444
+ const corner = wc.rnd() < 0.5;
445
+ const sx = wc.rnd() < 0.5 ? -1 : 1, sy = wc.rnd() < 0.5 ? -1 : 1;
446
+ const fw = g.doorHalfW - g.doorInnerHalfW;
447
+ let x, y;
448
+ if (corner) { x = sx * (g.doorHalfW - wc.r(0.002, fw * 0.7)); y = (sy > 0 ? g.doorTop : g.doorBottom) - sy * wc.r(0.002, fw * 0.7); }
449
+ else if (wc.rnd() < 0.5) { x = sx * (g.doorHalfW - wc.r(0.002, fw * 0.8)); y = wc.r(g.doorBottom + 0.02, g.doorTop - 0.02); }
450
+ else { x = wc.r(-g.doorHalfW + 0.02, g.doorHalfW - 0.02); y = (sy > 0 ? g.doorTop : g.doorBottom) - sy * wc.r(0.002, fw * 0.8); }
451
+ wc.flake({ phi: Math.atan2(x, g.doorFront), y, ...vflake(wc, L), rr: g.doorFront, clip: onFrame });
452
+ }
453
+
454
+ // ================= edge wear + rust grime + mould (per texel) =================
455
+ const edgeAt = edgeWearSampler(wc.seed + 500);
456
+ const grimeN = (x, y, z, s) => fbmA(x, y, z, 45, 110, 3, 0.6, s);
457
+ const ringLip = (y, rg) => Math.min(Math.abs(y - (rg.y0 + rg.bevel * 0.45)), Math.abs(y - (rg.y1 - rg.bevel * 0.45)));
458
+ wc.pass((k, phi, y, x, z) => {
459
+ const fe = facetEdgeDist(phi, B.sides) * Rv;
460
+ let hit = 0;
461
+ if (inHood(phi, y)) {
462
+ // hip hood: fascia bottom edge, fascia/slope crease, hip ridges - worn like edges (2-6 mm primer bands)
463
+ const d = hoodIn(y);
464
+ const ridge = boxCornerDist(phi, g.hoodHalfW - d, g.hoodFront - d, g.hoodBack);
465
+ const e = Math.max(1 - (y - g.hoodY0) / 0.004, 1 - Math.abs(y - g.hoodY0 - g.hoodFascia) / 0.004, 1 - ridge / 0.005);
466
+ hit = edgeAt(x, y, z, e, 0.42, 24);
467
+ } else if (inSlot(phi, y)) {
468
+ const e = Math.max(1 - boxCornerDist(phi, g.slotHalfW, g.slotFront, g.slotBack) / 0.005, 1 - (y - g.slotBottom) / 0.005);
469
+ hit = edgeAt(x, y, z, e, 0.22, 26);
470
+ // rust under the slot's bottom lip: a dark ragged band the lip drips from
471
+ const xs = Math.abs(xOn(phi, g.slotFront));
472
+ if (xs < g.openingHalfW + 0.006 && y < g.lipBottom + 0.003 && y > g.lipBottom - 0.01) {
473
+ const a = 0.95 * smooth(0.2, 0.42, grimeN(x, y, z, 901)) * (1 - 0.7 * (g.lipBottom + 0.003 - y) / 0.013);
474
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.7); }
475
+ }
476
+ } else if (inDoor(phi, y)) {
477
+ // door: weak edge wear on the outer frame only
478
+ const e = Math.max(1 - boxCornerDist(phi, g.doorHalfW, g.doorFront, g.doorBack) / 0.004,
479
+ 1 - Math.min(Math.abs(y - g.doorBottom), Math.abs(y - g.doorTop)) / 0.004);
480
+ hit = edgeAt(x, y, z, e, 0.15, 26, false);
481
+ // rust grime in the groove between the frame and the panel, heavier in the corners
482
+ const xd = Math.abs(xOn(phi, g.doorFront));
483
+ const ix = g.doorInnerHalfW - xd, iyb = y - g.doorInnerBottom, iyt = g.doorInnerTop - y;
484
+ if (ix > -0.003 && iyb > -0.003 && iyt > -0.003) {
485
+ const dIn = Math.min(Math.abs(ix), Math.abs(iyb), Math.abs(iyt));
486
+ const corner = Math.min(Math.hypot(ix, iyb), Math.hypot(ix, iyt));
487
+ const n = grimeN(x, y, z, 903);
488
+ let a = 0;
489
+ if (dIn < 0.0075) a = (0.55 + 0.45 * smooth(0.35, 0.65, fbmA(x, y, z, 30, 30, 2, 0.5, 904))) * smooth(0.18, 0.4, n) * (1 - dIn / 0.0075 * 0.4);
490
+ if (corner < 0.028 && ix > 0 && iyb > 0 && iyt > 0) a = Math.max(a, 0.95 * smooth(0, 0.6, 1 - corner / 0.028) * smooth(0.2, 0.45, n));
491
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.85); }
492
+ }
493
+ } else if (!inMark(phi, y)) {
494
+ // body facet edges: light wear, rare short (1-4 cm) breaks
495
+ hit = edgeAt(x, y, z, 1 - fe / 0.003, 0.05, 45);
496
+ // ring lips: mid ring strongest (30-50% of the lip length), collar lips weak
497
+ if (y > mid.y0 - 0.004 && y < mid.y1 + 0.004) {
498
+ hit = Math.max(hit, edgeAt(x, y, z, 1 - ringLip(y, mid) / 0.0045, 0.36, 24), edgeAt(x, y, z, 1 - fe / 0.005, 0.15, 30));
499
+ }
500
+ for (const rg of [cLow, cHigh]) {
501
+ if (y > rg.y0 - 0.003 && y < rg.y1 + 0.003) hit = Math.max(hit, edgeAt(x, y, z, 1 - ringLip(y, rg) / 0.004, 0.15, 24));
502
+ }
503
+ // rust grime: collar grooves and under the collar, mid ring lower lip (and a little on its top)
504
+ let a = 0;
505
+ const n = grimeN(x, y, z, 905);
506
+ if (y > cLow.y0 - 0.004 && y < cLow.y0 + 0.026) a = 0.97 * smooth(0.15, 0.38, n) * (1 - 0.75 * Math.abs(y - cLow.y0 - 0.006) / 0.02);
507
+ if (Math.abs(y - cHigh.y0) < 0.01) a = Math.max(a, 0.97 * smooth(0.15, 0.38, n) * (1 - 0.75 * Math.abs(y - cHigh.y0) / 0.011));
508
+ if (Math.abs(y - cHigh.y1 + 0.002) < 0.004) a = Math.max(a, 0.6 * smooth(0.3, 0.55, n));
509
+ if (y > mid.y0 - 0.003 && y < mid.y0 + 0.018) a = Math.max(a, 0.97 * smooth(0.15, 0.38, n) * (1 - 0.75 * Math.abs(y - mid.y0 - 0.005) / 0.017));
510
+ if (y > mid.y1 - 0.004 && y < mid.y1 + 0.006) a = Math.max(a, 0.55 * smooth(0.36, 0.58, n) * (1 - Math.abs(y - mid.y1 - 0.001) / 0.007));
511
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.75); }
512
+ // mould / olive grime: only the lowest 5-8 cm of the body, in patches
513
+ const h = y - B.y0;
514
+ if (h < 0.07) {
515
+ const m = fbmA(x, y, z, 34, 34, 4, 0.55, 907) + 0.16 * (1 - h / 0.05) - 0.15 * smooth(0.03, 0.06, h);
516
+ wc.mould[k] = 0.55 * smooth(0.56, 0.68, m) * (1 - smooth(0.035, 0.065, h));
517
+ }
518
+ }
519
+ if (hit && !wc.chip[k]) wc.chip[k] = hit;
520
+ });
521
+
522
+ // ================= drips (on top) =================
523
+ const slotPhi = Math.atan2(g.hoodHalfW + 0.004, g.hoodBack);
524
+ const doorPhi = Math.atan2(g.doorHalfW + 0.004, g.doorBack);
525
+ const floorBody = (p) => (Math.abs(p) < slotPhi ? g.hoodTop + 0.002 : -Infinity);
526
+ // under the collar: the main source - 2-4 clusters per facet, strands 3-10 cm, a few to 15 cm
527
+ wc.dripLine({ y: cLow.y0 + 0.002, phi0: -Math.PI, phi1: Math.PI, spacing: 0.075, keep: 0.95, n: [1, 5], len: [0.025, 0.1], longP: 0.14, longLen: [0.1, 0.145], span: [0.014, 0.036], stainH: [0.014, 0.026], floor: floorBody });
528
+ // under the cap slab: short 1-3 cm tails over the collar
529
+ wc.dripLine({ y: cHigh.y1, phi0: -Math.PI, phi1: Math.PI, rr: cHigh.radius, spacing: 0.09, keep: 0.75, n: [1, 3], len: [0.012, 0.03], span: [0.012, 0.03], stainH: [0.004, 0.008], alpha: 0.75 });
530
+ // under the mid ring: ~2 clusters per facet, 3-8 cm, a few to 10 cm
531
+ wc.dripLine({
532
+ y: mid.y0 + 0.002, phi0: -Math.PI, phi1: Math.PI, rr: R, spacing: 0.068, keep: 0.95, n: [1, 4], len: [0.03, 0.08], longP: 0.12, longLen: [0.08, 0.1], span: [0.014, 0.028], stainH: [0.012, 0.02],
533
+ floor: (p) => (Math.abs(p) < doorPhi ? g.doorTop + 0.002 : -Infinity),
534
+ });
535
+ // slot box: thin strands from under the opening's bottom lip down the box front (2-7 cm)
536
+ const lipPhi = Math.atan2(g.openingHalfW * 0.92, g.slotFront);
537
+ wc.dripLine({
538
+ y: g.lipBottom, phi0: -lipPhi, phi1: lipPhi, rr: g.slotFront, spacing: 0.034, keep: 1, n: [1, 3], len: [0.02, 0.07],
539
+ span: [0.008, 0.018], stainH: [0.006, 0.012], floor: () => g.slotBottom + 0.002,
540
+ });
541
+ // under the slot box onto the body (2-4 clusters, 3-9 cm), beside the mark
542
+ for (const x of [-0.095, -0.07, 0.068, 0.092]) {
543
+ if (wc.rnd() < 0.15) continue;
544
+ wc.cluster({ phi: Math.atan2(x + wc.r(-0.008, 0.008), R), y: g.slotBottom, span: wc.r(0.012, 0.022), stainH: wc.r(0.008, 0.014), maxLen: wc.r(0.03, 0.09), n: 1 + Math.floor(wc.rnd() * 3) });
545
+ }
546
+ // door: 2-4 strands from the panel's top edge, 1-2 from the knob, 2-4 under the door onto the body (4-13 cm)
547
+ for (const x of [-0.04, 0.035]) {
548
+ wc.cluster({ phi: Math.atan2(x + wc.r(-0.01, 0.01), g.doorPanelFront), y: g.doorInnerTop, rr: g.doorPanelFront, span: wc.r(0.01, 0.018), stainH: wc.r(0.006, 0.012), maxLen: wc.r(0.04, 0.08), n: 2, floorY: g.doorInnerBottom + 0.002 });
549
+ }
550
+ wc.cluster({ phi: wc.r(-0.01, 0.01), y: g.knobBottom, rr: g.doorPanelFront, span: 0.01, stainH: 0.006, maxLen: wc.r(0.035, 0.06), n: 2, floorY: g.doorInnerBottom + 0.002 });
551
+ // the concept's boldest drips: wide dark strands under the door
552
+ for (const [x, L] of [[-0.058, 0.11], [-0.01, 0.13], [0.04, 0.075]]) {
553
+ wc.cluster({ phi: Math.atan2(x + wc.r(-0.008, 0.008), R), y: g.doorBottom, span: wc.r(0.014, 0.022), stainH: wc.r(0.012, 0.018), maxLen: L, n: 1 + Math.floor(wc.rnd() * 2), alpha: 1, w: [0.008, 0.012] });
554
+ }
555
+
556
+ wc.compose({ undercoat: '#C99882', rim: P.chip_rim });
557
+ return { texture: wc.texture(), y0, y1 };
558
+ }
559
+
560
+ // ---------- cap ----------
561
+
562
+ /** Cap: pyramid ridges and slab edges worn in broken runs, flakes mostly along the ridges/bottom edge, no rust. */
563
+ export function capWear(spec) {
564
+ const c = spec.cap;
565
+ const y0 = c.slab_y0 - 0.005, y1 = c.knob_top;
566
+ const wc = new WearCanvas(1536, 640, y0, y1, c.slab_apothem, 99);
567
+ const Rv = c.slab_apothem / Math.cos(Math.PI / c.sides);
568
+ const pyrBase = c.slab_apothem - 0.014 - 0.004, pyrY0 = c.slab_y1 + 0.004;
569
+ const rise = c.pyramid_y1 - pyrY0, run = pyrBase - c.pyramid_top_apothem;
570
+ const sy = Math.hypot(rise, run) / rise; // slope metres per metre of height (~2.5)
571
+ const apAt = (y) => (y > pyrY0 ? Math.max(c.pyramid_top_apothem, pyrBase - (run * (y - pyrY0)) / rise) : c.slab_apothem);
572
+ const ridgeR = (y) => apAt(y) / Math.cos(Math.PI / c.sides);
573
+
574
+ // pyramid: 10-19 chips per view (~30 around), small 55-65%, medium ~35%, 0-2 large up to 7 cm;
575
+ // ~70% along the 8 ridges and the slopes' bottom edge, the rest on the slopes
576
+ for (const cls of classes(17, 8, 1)) {
577
+ const L = cls === 'l' ? wc.r(0.05, 0.06) : sizeOf(wc, cls);
578
+ const { w, h } = vflake(wc, L);
579
+ const u = wc.rnd();
580
+ let phi, y;
581
+ if (u < 0.55) {
582
+ y = wc.r(pyrY0 + 0.006, c.pyramid_y1 - 0.025);
583
+ phi = edgePhi(Math.floor(wc.rnd() * c.sides), c.sides) + (wc.r(-0.5, 0.5) * w) / apAt(y);
584
+ } else if (u < 0.68) {
585
+ y = pyrY0 + wc.r(0.002, 0.01);
586
+ phi = wc.r(-Math.PI, Math.PI);
587
+ } else {
588
+ y = wc.r(pyrY0 + 0.012, c.pyramid_y1 - 0.03);
589
+ phi = wc.r(-Math.PI, Math.PI);
590
+ }
591
+ wc.flakeCluster({ phi, y, w, h, rr: apAt(y), sy, rot: wc.r(-0.3, 0.3), clip: (p, yy) => yy > c.slab_y1 - 0.004 });
592
+ }
593
+ // slab: 5-13 chips per view (~20 around), half at the vertical corners; 1-3 rubbed strips per view along the
594
+ // top/bottom edges (5-15 cm x 1-2 cm)
595
+ const onSlab = (p, yy) => yy < c.slab_y1 + 0.003;
596
+ for (const cls of classes(10, 2, 0)) {
597
+ const L = sizeOf(wc, cls);
598
+ const { w, h } = vflake(wc, Math.min(L, 0.035));
599
+ const phi = wc.rnd() < 0.5 ? edgePhi(Math.floor(wc.rnd() * c.sides), c.sides) + (wc.r(-0.5, 0.5) * w) / c.slab_apothem : wc.r(-Math.PI, Math.PI);
600
+ wc.flakeCluster({ phi, y: wc.r(c.slab_y0 + 0.01, c.slab_y1 - 0.01), w, h, rr: c.slab_apothem, clip: onSlab });
601
+ }
602
+ for (let i = 0; i < 7; i++) {
603
+ const top = i % 3 !== 2;
604
+ const y = top ? c.slab_y1 - wc.r(0.003, 0.008) : c.slab_y0 + wc.r(0.004, 0.009);
605
+ wc.flake({ phi: -Math.PI + (i + wc.r(0.2, 0.8)) * TAU / 7, y, w: wc.r(0.05, 0.11), h: wc.r(0.008, 0.014), rr: c.slab_apothem, rot: wc.r(-0.04, 0.04), amp: 0.7, clip: onSlab });
606
+ }
607
+
608
+ // edge wear: ridges 40-60% of their length in 3-10 cm runs, slab corners the same, slab top/bottom edges
609
+ const edgeAt = edgeWearSampler(wc.seed + 500);
610
+ wc.pass((k, phi, y, x, z) => {
611
+ let hit = 0;
612
+ if (y > pyrY0) {
613
+ // gate along the slope length (y stretched by the slope factor) -> 3-10 cm runs on ~50% of the ridge
614
+ const d = facetEdgeDist(phi, c.sides) * ridgeR(y);
615
+ hit = edgeAt(x, pyrY0 + (y - pyrY0) * sy, z, 1 - d / 0.008, 0.55, 24);
616
+ } else {
617
+ const d = facetEdgeDist(phi, c.sides) * Rv;
618
+ if (y > c.slab_y0 && y < c.slab_y1) hit = edgeAt(x, y, z, 1 - d / 0.007, 0.38, 22);
619
+ }
620
+ hit = Math.max(hit, edgeAt(x, y, z, 1 - Math.abs(y - (c.slab_y1 - 0.001)) / 0.005, 0.26, 22),
621
+ edgeAt(x, y, z, 1 - Math.abs(y - (c.slab_y0 + 0.004)) / 0.005, 0.22, 20));
622
+ if (hit && !wc.chip[k]) wc.chip[k] = hit;
623
+ });
624
+ // 3-6 short (1-3 cm) rust tails per view from the slab's lower joint, irregular spacing and size
625
+ wc.dripLine({ y: c.slab_y0 + 0.016, phi0: -Math.PI, phi1: Math.PI, rr: c.slab_apothem, spacing: 0.11, keep: 0.75, n: [2, 3], len: [0.01, 0.024],
626
+ span: [0.012, 0.026], stainH: [0.004, 0.008], floor: () => c.slab_y0 - 0.004, alpha: 0.55, w: [0.004, 0.007] });
627
+ wc.compose({ undercoat: '#C99C84', rim: spec.palette.chip_rim });
628
+ return { texture: wc.texture(), y0, y1 };
629
+ }
630
+
631
+ // ---------- concrete base ----------
632
+
633
+ /** Concrete base: olive moss/dirt patches (15-25%, heavier low down), dark water stains, small edge chips. */
634
+ export function baseWear(spec) {
635
+ const b = spec.base;
636
+ const y0 = b.y0, y1 = b.chamfer_top_y;
637
+ const wc = new WearCanvas(1536, 384, y0, y1, b.apothem, 777);
638
+ const H = b.y1 - b.y0;
639
+ const Rv = b.apothem / Math.cos(Math.PI / b.sides);
640
+ const img = wc.g.createImageData(wc.W, wc.H), d = img.data;
641
+ const moss = [hexRGB('#726B4A'), hexRGB('#5C5A3E')], dirt = hexRGB('#5E5244'), water = hexRGB('#4A4238'), chipC = hexRGB('#ADA597'), pale = hexRGB('#C4A88C');
642
+ const mossN = (x, y, z) => fbmA(x, y, z, 11, 9, 5, 0.55, 771);
643
+ const mq = quantiles((r) => mossN(r() * 2 - 1, r() * 0.2, r() * 2 - 1), 3000, 772);
644
+ // water stains: a few vertical dark tracks running down from the ledge
645
+ const streaks = Array.from({ length: 16 }, () => ({ phi: wc.r(-Math.PI, Math.PI), w: wc.r(0.005, 0.03), len: wc.r(0.03, 0.15), a: wc.r(0.2, 0.5) }));
646
+ const edgeAt = edgeWearSampler(779);
647
+ wc.pass((k, phi, y, x, z) => {
648
+ let cr = 0, cg = 0, cb = 0, ca = 0;
649
+ const over = (col, a) => {
650
+ const oa = a + ca * (1 - a);
651
+ if (oa <= 0) return;
652
+ cr = (col[0] * a + cr * ca * (1 - a)) / oa; cg = (col[1] * a + cg * ca * (1 - a)) / oa; cb = (col[2] * a + cb * ca * (1 - a)) / oa;
653
+ ca = oa;
654
+ };
655
+ const h = y - y0;
656
+ // dark bottom band (splash), then moss patches: ~45% of the area near the ground, ~10% higher up -> ~20% overall
657
+ // weathered (sun-bleached, washed) concrete: light mottled wash, the concept base is lighter than the texture
658
+ const ledge = smooth(b.y1 - 0.008, b.y1 - 0.002, y); // the top ledge collects dirt instead of bleaching
659
+ over(pale, (0.07 + 0.11 * smooth(0.45, 0.65, fbmA(x, y, z, 7, 7, 3, 0.5, 775))) * (1 - ledge));
660
+ if (ledge > 0) over(dirt, 0.32 * ledge);
661
+ over(dirt, 0.22 * (1 - smooth(0, H * 0.12, h)));
662
+ const corner = 1 - smooth(0.01, 0.05, facetEdgeDist(phi, b.sides) * Rv);
663
+ const share = 0.03 + 0.5 * (1 - smooth(0, H * 0.28, h)) + 0.1 * corner * (1 - smooth(0, H * 0.6, h)) + (y > b.y1 - 0.004 ? 0.15 : 0);
664
+ const n = mossN(x, y, z);
665
+ const t = n - mq(1 - share);
666
+ if (t > -0.01) over(mixRGB(moss[0], moss[1], noise3(x * 60, y * 60, z * 60, 773)), 0.45 * smooth(-0.01, 0.04, t));
667
+ for (const s of streaks) {
668
+ const dx = Math.abs(((phi - s.phi + Math.PI) % TAU + TAU) % TAU - Math.PI) * b.apothem;
669
+ const dd = b.y1 - y;
670
+ if (dx < s.w / 2 && dd > 0 && dd < s.len) over(water, s.a * (1 - dx / (s.w / 2)) ** 0.5 * (1 - dd / s.len));
671
+ }
672
+ // small concrete chips on the vertical corners and the top edge: muted light concrete, not white
673
+ const fe = facetEdgeDist(phi, b.sides) * Rv;
674
+ const hit = Math.max(edgeAt(x, y, z, 1 - fe / 0.007, 0.25, 30, false), edgeAt(x, y, z, 1 - Math.abs(y - b.y1 + 0.003) / 0.005, 0.2, 30, false));
675
+ if (hit) over(chipC, 0.4);
676
+ const o = k * 4;
677
+ d[o] = cr; d[o + 1] = cg; d[o + 2] = cb; d[o + 3] = Math.round(ca * 255);
678
+ });
679
+ wc.g.putImageData(img, 0, 0);
680
+ return { texture: wc.texture(), y0, y1 };
681
+ }
682
+
683
+ /**
684
+ * Patch a MeshStandardMaterial: per-facet brightness variation (facetSides > 0) on the albedo, the baked chips
685
+ * (dechip) and green mould (demoss) of the generated paint albedo replaced by paint, then the wear canvas
686
+ * (rgb + coverage alpha) mixed on top.
687
+ */
688
+ export function applyWear(material, wear, { facetSides = 0, facetRange = [0.87, 1.14], dechip = false, demoss = false } = {}) {
689
+ material.onBeforeCompile = (shader) => {
690
+ shader.uniforms.wearMap = { value: wear.texture };
691
+ shader.uniforms.wearRange = { value: new THREE.Vector2(wear.y0, wear.y1) };
692
+ shader.uniforms.facetSides = { value: facetSides };
693
+ shader.uniforms.facetRange = { value: new THREE.Vector2(...facetRange) };
694
+ shader.uniforms.dechip = { value: dechip ? 1 : 0 };
695
+ shader.uniforms.demoss = { value: demoss ? 1 : 0 };
696
+ // median paint albedo of body_wrap_albedo (201,85,58 sRGB) × the material tint, in linear
697
+ shader.uniforms.paintRef = { value: new THREE.Color('#C9553A').multiply(material.color) };
698
+ shader.vertexShader = shader.vertexShader
699
+ .replace('#include <common>', '#include <common>\nvarying vec3 vWearPos;')
700
+ .replace('#include <begin_vertex>', '#include <begin_vertex>\nvWearPos = position;');
701
+ shader.fragmentShader = shader.fragmentShader
702
+ .replace('#include <common>', '#include <common>\nvarying vec3 vWearPos;\nuniform sampler2D wearMap;\nuniform vec2 wearRange;\nuniform float facetSides;\nuniform vec2 facetRange;\nuniform float dechip;\nuniform float demoss;\nuniform vec3 paintRef;')
703
+ .replace('#include <map_fragment>', `#include <map_fragment>
704
+ {
705
+ // the generated albedo already contains big cream "paint spot" chips: suppress them (low saturation,
706
+ // light texels -> paint), the procedural wear layer below is the only source of chips
707
+ if (dechip > 0.5) {
708
+ vec3 t = diffuseColor.rgb;
709
+ float mx = max(t.r, max(t.g, t.b));
710
+ float sat = (mx - min(t.r, min(t.g, t.b))) / max(mx, 1e-4);
711
+ diffuseColor.rgb = mix(t, paintRef, smoothstep(0.62, 0.42, sat) * 0.95);
712
+ }
713
+ // ... and a continuous band of green mould at its bottom (which box-projected parts also pick up):
714
+ // olive texels (green close to red) -> paint; mould is drawn only where the description wants it
715
+ if (demoss > 0.5) {
716
+ vec3 t = diffuseColor.rgb;
717
+ float gr = t.g / max(t.r, 1e-4);
718
+ diffuseColor.rgb = mix(t, paintRef * (0.85 + 0.3 * clamp(t.r / max(paintRef.r, 1e-4), 0.0, 1.0)), smoothstep(0.28, 0.5, gr));
719
+ }
720
+ float fa = atan(vWearPos.x, vWearPos.z);
721
+ if (facetSides > 0.5) {
722
+ // per-facet brightness, blended across the rounded corner instead of switching on a hard line
723
+ float step = 6.2831853 / facetSides;
724
+ float pf = (fa + step * 0.5) / step;
725
+ float i0 = floor(pf), fr = pf - i0;
726
+ float hA = fract(sin(mod(i0, facetSides) * 12.9898 + 4.1) * 43758.5453);
727
+ float hP = fract(sin(mod(i0 - 1.0, facetSides) * 12.9898 + 4.1) * 43758.5453);
728
+ float hN = fract(sin(mod(i0 + 1.0, facetSides) * 12.9898 + 4.1) * 43758.5453);
729
+ float h = mix(mix(hP, hA, 0.5), hA, smoothstep(0.0, 0.07, fr));
730
+ h = mix(h, mix(hA, hN, 0.5), smoothstep(0.93, 1.0, fr));
731
+ diffuseColor.rgb *= mix(facetRange.x, facetRange.y, h);
732
+ }
733
+ float wu = fa / 6.2831853 + 0.5;
734
+ float wv = (vWearPos.y - wearRange.x) / (wearRange.y - wearRange.x);
735
+ vec4 w = texture2D(wearMap, vec2(wu, wv));
736
+ diffuseColor.rgb = mix(diffuseColor.rgb, w.rgb, w.a);
737
+ }`);
738
+ };
739
+ material.customProgramCacheKey = () => `wear-${material.uuid}`;
740
+ material.needsUpdate = true;
741
+ return material;
742
+ }