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,798 @@
1
+ // Procedural weathering for the vending machine (pipeline threejs). Same language as the post box wear
2
+ // (postbox_wear_three.js): explicitly placed ragged flakes with satellite flecks and a thin rust rim, broken primer
3
+ // strips along geometric edges with a metal core, rust drip clusters (stain + strands, bell-curve lengths) from real
4
+ // sources, olive grime only at the very bottom.
5
+ //
6
+ // Mapping. The cabinet is a rounded box, so instead of the post box's cylinder the canvas is a BELT: s = arc length
7
+ // around the rounded-rect section (0 = front centre, +s towards +x, wraps at the back centre), v = surface distance
8
+ // from the cabinet bottom (= height on the walls, continues as arc length over the R 7 cm roof rounding). Front and
9
+ // back parts (frames, housings, louvres) use the same canvas through their (x, y) projection. The flat roof has its
10
+ // own small planar canvas. The shader computes the same (s, v) from the object-space position.
11
+ import * as THREE from 'three';
12
+
13
+ const TAU = Math.PI * 2;
14
+ export function mulberry32(seed) {
15
+ return () => {
16
+ seed |= 0; seed = (seed + 0x6d2b79f5) | 0;
17
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
18
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
19
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
20
+ };
21
+ }
22
+ const hexRGB = (hex) => { const n = parseInt(hex.slice(1), 16); return [(n >> 16) & 255, (n >> 8) & 255, n & 255]; };
23
+ const scaleRGB = (c, k) => c.map((v) => Math.max(0, Math.min(255, v * k)));
24
+ const mixRGB = (a, b, t) => a.map((v, i) => v + (b[i] - v) * t);
25
+ const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
26
+ const clamp01 = (x) => Math.min(1, Math.max(0, x));
27
+
28
+ function hash3(x, y, z, s) {
29
+ let h = Math.imul(x, 0x8da6b343) ^ Math.imul(y, 0xd8163841) ^ Math.imul(z, 0xcb1ab31f) ^ Math.imul(s, 0x27d4eb2d);
30
+ h = Math.imul(h ^ (h >>> 13), 0x5bd1e995);
31
+ h ^= h >>> 15;
32
+ return (h >>> 0) / 4294967296;
33
+ }
34
+ export function noise3(x, y, z, s) {
35
+ const xi = Math.floor(x), yi = Math.floor(y), zi = Math.floor(z);
36
+ const xf = x - xi, yf = y - yi, zf = z - zi;
37
+ const u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf), w = zf * zf * (3 - 2 * zf);
38
+ 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);
39
+ 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);
40
+ const x1 = a + (b - a) * u, x2 = c + (d - c) * u, x3 = e + (f - e) * u, x4 = g + (h - g) * u;
41
+ const y1 = x1 + (x2 - x1) * v, y2 = x3 + (x4 - x3) * v;
42
+ return y1 + (y2 - y1) * w;
43
+ }
44
+ /** Anisotropic fbm: per-axis scale (cycles/m: [horizontal, vertical]). */
45
+ export function fbmA(x, y, z, sx, sy, oct, gain, s) {
46
+ let sum = 0, amp = 1, norm = 0, f = 1;
47
+ for (let o = 0; o < oct; o++) {
48
+ sum += noise3(x * sx * f, y * sy * f, z * sx * f, s + o * 101) * amp;
49
+ norm += amp; amp *= gain; f *= 2.0;
50
+ }
51
+ return sum / norm;
52
+ }
53
+ function quantiles(sample, n, seed) {
54
+ const rnd = mulberry32(seed);
55
+ const a = Float64Array.from({ length: n }, () => sample(rnd)).sort();
56
+ return (q) => a[Math.min(n - 1, Math.max(0, Math.floor(q * n)))];
57
+ }
58
+
59
+ // ---------- belt parameters (shared with the shader) ----------
60
+
61
+ export function beltParams(spec) {
62
+ const c = spec.cabinet;
63
+ const A = c.width / 2, B = c.depth / 2, rc = c.vertical_edge_radius, R = c.top_edge_radius, Rb = c.bottom_edge_radius;
64
+ const Lf = A - rc, Lc = (Math.PI / 2) * rc, Ls = 2 * (B - rc);
65
+ const P = 4 * Lf + 2 * Ls + 4 * Lc;
66
+ const yr = c.y1 - R;
67
+ // door depth seen on the sides: the concept shows a 4-7 cm pale strip; with the R 3 cm front round a 4 cm inset
68
+ // leaves only 1 cm of flat strip, so the seam sits 5.5 cm behind the front (decisions log)
69
+ const zs = B - 0.055;
70
+ const bp = {
71
+ A, B, rc, R, Rb, y0: c.y0, y1: c.y1, yr, Lf, Lc, Ls, P, zs, gap: c.door_seam.gap,
72
+ V0: c.y0, V1: yr + (Math.PI / 2) * R, vApex: yr + (Math.PI / 4) * R,
73
+ };
74
+ bp.sSide = (sg, z) => sg * (Lf + Lc + (B - rc) - z); // side wall at depth z
75
+ bp.sBack = (x) => (x >= 0 ? 1 : -1) * (P / 2 - Math.abs(x)); // back wall at x
76
+ bp.sCornerF = Lf + Lc / 2; // front vertical edge apex (|s|)
77
+ bp.sCornerB = P / 2 - Lf - Lc / 2; // back vertical edge apex (|s|)
78
+ bp.sSeam = Lf + Lc + (B - rc - zs); // door seam on the side walls (|s|)
79
+ bp.sSideMid = Lf + Lc + Ls / 2;
80
+ return bp;
81
+ }
82
+
83
+ // ---------- canvas with wear layers ----------
84
+
85
+ export class PlaneCanvas {
86
+ constructor(W, H, s0, s1, v0, v1, seed, wrap = false) {
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, H, s0, s1, v0, v1, seed, wrap });
91
+ this.px = (s1 - s0) / W;
92
+ this.rnd = mulberry32(seed);
93
+ const N = W * H;
94
+ this.chip = new Uint8Array(N); // 1 primer, 2 bare metal (core), 3 rubbed bright steel (metal housings)
95
+ this.rust = new Float32Array(N);
96
+ this.rdark = new Float32Array(N);
97
+ this.mould = new Float32Array(N);
98
+ }
99
+ r(a, b) { return a + this.rnd() * (b - a); }
100
+ sAt(i) { return this.s0 + ((i + 0.5) / this.W) * (this.s1 - this.s0); }
101
+ vAt(j) { return this.v1 - ((j + 0.5) / this.H) * (this.v1 - this.v0); }
102
+
103
+ /** Visit texels with centres in [sA, sB] x [vA, vB] (s unwrapped when the canvas wraps). */
104
+ box(sA, sB, vA, vB, fn) {
105
+ const { W, H } = this;
106
+ const span = this.s1 - this.s0, vspan = this.v1 - this.v0;
107
+ let i0 = Math.floor(((sA - this.s0) / span) * W - 0.5), i1 = Math.ceil(((sB - this.s0) / span) * W - 0.5);
108
+ if (!this.wrap) { i0 = Math.max(0, i0); i1 = Math.min(W - 1, i1); } else i1 = Math.min(i1, i0 + W - 1);
109
+ const j0 = Math.max(0, Math.floor(((this.v1 - vB) / vspan) * H - 0.5));
110
+ const j1 = Math.min(H - 1, Math.ceil(((this.v1 - vA) / vspan) * H - 0.5));
111
+ for (let j = j0; j <= j1; j++) {
112
+ const v = this.vAt(j);
113
+ for (let ii = i0; ii <= i1; ii++) fn(j * W + (((ii % W) + W) % W), this.sAt(ii), v);
114
+ }
115
+ }
116
+ pass(fn) {
117
+ const { W, H } = this;
118
+ for (let j = 0; j < H; j++) {
119
+ const v = this.vAt(j);
120
+ for (let i = 0; i < W; i++) fn(j * W + i, this.sAt(i), v);
121
+ }
122
+ }
123
+
124
+ /**
125
+ * One paint flake down to the primer. w, h: size (m), rot: radians. Outline = ellipse with `star` radial spikes
126
+ * (pointy lobes like the concept's star-shaped flakes), domain-warped, torn by two noise octaves and a fine jag.
127
+ */
128
+ flake({ s, v, w, h, rot = 0, metal = false, clip = null, amp = 1, star = 0, cls = 1 }) {
129
+ const L = Math.max(w, h);
130
+ // outline = an irregular polygon in polar form: 5-9 corners at jittered angles; star flakes alternate long
131
+ // pointed corners and short re-entrant ones (torn paint), plain flakes are blunt irregular polygons
132
+ const nV = 5 + Math.floor(this.rnd() * 5);
133
+ const a0 = this.r(-Math.PI, Math.PI);
134
+ const verts = [];
135
+ for (let i = 0; i < nV; i++) {
136
+ const ang = a0 + (i + this.r(-0.28, 0.28)) * TAU / nV;
137
+ const outer = i % 2 === 0 || this.rnd() < 0.25;
138
+ const r = star > 0 ? (outer ? this.r(0.95, 1.0 + 0.95 * star) : this.r(0.4, 0.75)) : this.r(0.72, 1.12);
139
+ verts.push({ a: ang, r });
140
+ }
141
+ verts.sort((p, q) => p.a - q.a);
142
+ // straight polygon edges between the corners (ray-edge intersection), so star corners come out pointed
143
+ const P2 = verts.map((q) => [q.r * Math.cos(q.a), q.r * Math.sin(q.a)]);
144
+ const rOf = (th) => {
145
+ let t = th - verts[0].a; t = ((t % TAU) + TAU) % TAU;
146
+ let i = nV - 1;
147
+ for (let k = 1; k < nV; k++) if (t < verts[k].a - verts[0].a) { i = k - 1; break; }
148
+ const Pp = P2[i], Q = P2[(i + 1) % nV];
149
+ const dx = Math.cos(th), dy = Math.sin(th), ex = Q[0] - Pp[0], ey = Q[1] - Pp[1];
150
+ const den = dx * ey - dy * ex;
151
+ return Math.abs(den) < 1e-9 ? verts[i].r : (Pp[0] * ey - Pp[1] * ex) / den;
152
+ };
153
+ const rMax = Math.max(...verts.map((q) => q.r));
154
+ const ext = (0.7 * rMax + 0.3) * L;
155
+ const sd = Math.floor(this.rnd() * 1e6), ox = this.rnd() * 40, oy = this.rnd() * 40;
156
+ const f1 = 2.6 / L, f2 = 10 / L;
157
+ const c = Math.cos(rot), sn = Math.sin(rot);
158
+ this.box(s - ext, s + ext, v - ext, v + ext, (k, ss, vv) => {
159
+ const dx = ss - s, dz = vv - v;
160
+ const lx = dx * c + dz * sn, ly = -dx * sn + dz * c;
161
+ const wx = lx + 0.12 * L * (noise3(lx * f1 * 0.7 + oy, ly * f1 * 0.7, 11.5, sd + 4) - 0.5);
162
+ const wy = ly + 0.12 * L * (noise3(lx * f1 * 0.7, ly * f1 * 0.7 + ox, 13.5, sd + 5) - 0.5);
163
+ const ex = wx / (w / 2), ey = wy / (h / 2);
164
+ const qw = Math.hypot(ex, ey) / rOf(Math.atan2(ey, ex));
165
+ if (qw > 1.6) return;
166
+ const n1 = noise3(wx * f1 + ox, wy * f1 + oy, 0.5, sd);
167
+ const n2 = noise3(wx * f2 + ox, wy * f2 + oy, 7.5, sd + 2);
168
+ const n3 = noise3(lx * 480 + ox, ly * 480 + oy, 9.5, sd + 3);
169
+ const val = 1 - qw + amp * (0.3 * (n1 - 0.5) + 0.25 * (n2 - 0.5)) + 0.28 * (n3 - 0.5);
170
+ if (val <= 0 || (clip && !clip(ss, vv))) return;
171
+ this.chip[k] = metal && val > 0.55 ? 2 : Math.max(this.chip[k], cls);
172
+ });
173
+ return verts.filter((q) => q.r > 1.2).map((q) => ({ a: q.a + rot, r: (L / 2) * q.r }));
174
+ }
175
+
176
+ /** Flake + a few tiny flecks around it; big star flakes also get 1-2 hairline cracks from their spikes. */
177
+ flakeCluster(o) {
178
+ const spikes = this.flake(o);
179
+ const L = Math.max(o.w, o.h);
180
+ if (L < 0.016) return;
181
+ const n = Math.floor(this.rnd() * (L > 0.04 ? 5 : 3));
182
+ for (let i = 0; i < n; i++) {
183
+ const a = this.r(0, TAU), d = L * this.r(0.55, 1.0) * (o.star ? 1.25 : 1), s = this.r(0.0025, 0.006);
184
+ this.flake({ s: o.s + Math.cos(a) * d, v: o.v + Math.sin(a) * d, w: s, h: s * this.r(1, 1.7), clip: o.clip });
185
+ }
186
+ if (o.star && L > 0.035) {
187
+ for (const p of spikes.slice(0, 1 + Math.floor(this.rnd() * 2))) {
188
+ this.crack(o.s + Math.cos(p.a) * p.r * 0.8, o.v + Math.sin(p.a) * p.r * 0.8, p.a + this.r(-0.4, 0.4), this.r(0.012, 0.03), o.clip);
189
+ }
190
+ }
191
+ }
192
+
193
+ /** Hairline crack (thin dark rust line) wandering from (s, v) in direction a. */
194
+ crack(s, v, a, len, clip) {
195
+ const step = this.px * 0.7;
196
+ let x = s, y = v, ang = a;
197
+ for (let t = 0; t < len; t += step) {
198
+ ang += this.r(-0.25, 0.25);
199
+ x += Math.cos(ang) * step; y += Math.sin(ang) * step;
200
+ const al = 0.55 * (1 - t / len) + 0.1;
201
+ this.box(x, x, y, y, (k, ss, vv) => {
202
+ if (clip && !clip(ss, vv)) return;
203
+ if (al > this.rust[k]) { this.rust[k] = al; this.rdark[k] = Math.max(this.rdark[k], 0.6); }
204
+ });
205
+ }
206
+ }
207
+
208
+ /** One tapering rust strand from vTop down `len` metres (stops at floorV), with a bead at a natural end. */
209
+ strand(s0, vTop, len, w, floorV, alpha) {
210
+ const pxM = 1 / this.px;
211
+ const wob = this.r(0.0004, 0.0012), ph = this.r(0, TAU), fq = this.r(3, 7);
212
+ const vEnd = Math.max(vTop - len, floorV), cut = vEnd > vTop - len;
213
+ if (vEnd >= vTop) return;
214
+ const reach = w + wob + 2 / pxM;
215
+ const sd = Math.floor(this.rnd() * 1e6);
216
+ 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);
217
+ this.box(s0 - reach, s0 + reach, vEnd - (bead ? w : 0), vTop, (k, ss, vv) => {
218
+ const depth = vTop - vv, t = Math.min(1, depth / len);
219
+ const cx = Math.sin(t * fq + ph) * wob * Math.min(1, t * 3);
220
+ const dxm = ss - s0 - cx;
221
+ const half = (w / 2) * (1 - taper * t);
222
+ let cov;
223
+ if (vv >= vEnd) cov = clamp01((half - Math.abs(dxm)) * pxM + 0.5);
224
+ else { const rb = half * 1.3; cov = clamp01((rb - Math.hypot(dxm, vv - (vEnd + rb * 0.3))) * pxM + 0.5); }
225
+ if (cov <= 0) return;
226
+ const a = alpha * cov * (1 - fade * t ** 2) * (0.85 + 0.15 * noise3(depth * 300, 0.5, 0.5, sd));
227
+ if (a > this.rust[k]) this.rust[k] = a;
228
+ this.rdark[k] = Math.max(this.rdark[k], cov * Math.max(0, 0.55 - 1.4 * t));
229
+ });
230
+ }
231
+
232
+ /** Drip cluster: stain at the source (2.25x the strand span) + n strands with a bell profile of lengths. */
233
+ cluster({ s, v, span, stainH, maxLen, n, floorV = -Infinity, alpha = 0.95, w = [0.004, 0.0075] }) {
234
+ const hw = span * 1.125;
235
+ const sd = Math.floor(this.rnd() * 1e6);
236
+ this.box(s - 1.35 * hw, s + 1.35 * hw, Math.max(floorV, v - stainH * 1.35), v, (k, ss, vv) => {
237
+ const dx = ss - s, dd = v - vv;
238
+ const q = Math.hypot(dx / hw, dd / stainH);
239
+ const val = 1 - q + 0.55 * (noise3(dx * 160, dd * 160, 0.3, sd) - 0.5);
240
+ if (val <= 0) return;
241
+ const a = Math.min(1, val / 0.3) * 0.85 * (1 - 0.25 * dd / stainH);
242
+ if (a > this.rust[k]) this.rust[k] = a;
243
+ this.rdark[k] = Math.max(this.rdark[k], 0.8 * Math.min(1, val / 0.35));
244
+ });
245
+ const asym = this.r(-0.35, 0.35), sig = this.r(0.35, 0.6);
246
+ for (let i = 0; i < n; i++) {
247
+ const t = n === 1 ? asym : (i / (n - 1)) * 2 - 1;
248
+ if (n > 2 && this.rnd() < 0.15) continue;
249
+ const tx = t + this.r(-0.3, 0.3) * (2 / Math.max(2, n));
250
+ const bell = Math.exp(-((tx - asym) ** 2) / (2 * sig * sig));
251
+ const len = maxLen * (0.3 + 0.7 * bell) * this.r(0.85, 1.05);
252
+ this.strand(s + (tx * span) / 2, v - stainH * 0.4, len, this.r(w[0], w[1]), floorV, alpha);
253
+ }
254
+ }
255
+
256
+ /** Clusters along a source edge [sA, sB] at ~`spacing` metres (jittered, some skipped). */
257
+ dripLine({ v, sA, sB, spacing, keep = 0.85, n = [1, 4], len, longP = 0, longLen = [0.1, 0.13],
258
+ span = [0.01, 0.022], stainH = [0.006, 0.014], floor = () => -Infinity, skip = () => false, alpha = 0.95, w }) {
259
+ let p = sA + this.rnd() * spacing * 0.8;
260
+ while (p < sB) {
261
+ if (this.rnd() < keep && !skip(p)) {
262
+ const maxLen = this.rnd() < longP ? this.r(...longLen) : this.r(...len);
263
+ this.cluster({ s: p, v, span: this.r(...span), stainH: this.r(...stainH), maxLen,
264
+ n: n[0] + Math.floor(this.rnd() * (n[1] - n[0] + 1)), floorV: floor(p), alpha, ...(w ? { w } : {}) });
265
+ }
266
+ p += spacing * this.r(0.6, 1.5);
267
+ }
268
+ }
269
+
270
+ compose({ primer, rim, metal, steel, rust, mould, ucJitter = 0.12 }) {
271
+ const { W, H, chip } = this;
272
+ const img = this.g.createImageData(W, H), d = img.data;
273
+ const uc = hexRGB(primer[0]), uc2 = hexRGB(primer[1]), rc = hexRGB(rim), mc = hexRGB(metal), sc = hexRGB(steel);
274
+ const r0 = hexRGB(rust[0]), r1 = hexRGB(rust[1]), m0 = hexRGB(mould[0]), m1 = hexRGB(mould[1]);
275
+ const s = this.seed;
276
+ const wrap = this.wrap;
277
+ const near = (i, j, rad) => {
278
+ for (let dj = -rad; dj <= rad; dj++) {
279
+ const jj = j + dj;
280
+ if (jj < 0 || jj >= H) continue;
281
+ for (let di = -rad; di <= rad; di++) {
282
+ let ii = i + di;
283
+ if (wrap) ii = ((ii % W) + W) % W; else if (ii < 0 || ii >= W) continue;
284
+ const c = chip[jj * W + ii];
285
+ if (c === 1 || c === 2) return true;
286
+ }
287
+ }
288
+ return false;
289
+ };
290
+ for (let j = 0; j < H; j++) {
291
+ for (let i = 0; i < W; i++) {
292
+ const k = j * W + i;
293
+ let cr = 0, cg = 0, cb = 0, ca = 0;
294
+ const over = (col, a) => {
295
+ const oa = a + ca * (1 - a);
296
+ if (oa <= 0) return;
297
+ 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;
298
+ ca = oa;
299
+ };
300
+ if (this.mould[k] > 0) over(mixRGB(m0, m1, noise3(i * 0.09, j * 0.09, 0.5, s + 7)), this.mould[k]);
301
+ const ch = chip[k];
302
+ if (ch === 3) {
303
+ over(scaleRGB(sc, 0.9 + 0.2 * noise3(i * 0.3, j * 0.3, 1.5, s + 9)), 0.9);
304
+ } else if (ch) {
305
+ const kk = 1 - ucJitter / 2 + ucJitter * noise3(i * 0.08, j * 0.08, 0.5, s + 3);
306
+ const pale = smooth(0.4, 0.75, noise3(i * 0.05, j * 0.05, 2.5, s + 4));
307
+ over(ch === 2 ? mc : scaleRGB(mixRGB(uc, uc2, pale), kk), 1);
308
+ } else if (near(i, j, 1)) over(rc, 0.6);
309
+ if (this.rust[k] > 0.004) over(mixRGB(r0, r1, this.rdark[k]), this.rust[k]);
310
+ const o = k * 4;
311
+ d[o] = cr; d[o + 1] = cg; d[o + 2] = cb; d[o + 3] = Math.round(ca * 255);
312
+ }
313
+ }
314
+ this.g.putImageData(img, 0, 0);
315
+ }
316
+
317
+ texture(wrap) {
318
+ (window.__wearCanvases ??= []).push(this.c);
319
+ const t = new THREE.CanvasTexture(this.c);
320
+ t.colorSpace = THREE.SRGBColorSpace;
321
+ t.wrapS = wrap ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
322
+ t.wrapT = THREE.ClampToEdgeWrapping;
323
+ // no mipmaps: the belt wraps at the back centre and switches to the roof canvas; both would pick a tiny mip
324
+ t.generateMipmaps = false;
325
+ t.minFilter = THREE.LinearFilter;
326
+ return t;
327
+ }
328
+ }
329
+
330
+ // ---------- edge wear sampler (as the post box) ----------
331
+
332
+ function edgeWearSampler(seed) {
333
+ const gates = {};
334
+ const gate = (gf) => (gates[gf] ??= {
335
+ n: (x, y, z) => fbmA(x, y, z, gf, gf, 2, 0.5, seed + gf),
336
+ q: quantiles((r) => fbmA(r() * 3 - 1.5, r() * 2, r() * 2, gf, gf, 2, 0.5, seed + gf), 3000, seed + gf),
337
+ });
338
+ return (x, y, z, e, cov, gf = 18, metal = true, cls = 1) => {
339
+ if (e <= 0 || cov <= 0) return 0;
340
+ const G = gate(gf);
341
+ const thr = G.q(1 - cov);
342
+ const on = smooth(thr - 0.004, thr + 0.035, G.n(x, y, z));
343
+ if (on <= 0) return 0;
344
+ const rag = 0.6 * noise3(x * 230, y * 230, z * 230, seed + 5) + 0.4 * noise3(x * 620, y * 620, z * 620, seed + 6);
345
+ const width = on * (0.2 + 1.0 * rag);
346
+ if (e <= 1 - width) return 0;
347
+ return metal && width > 0.8 && e > 1 - width * 0.3 ? 2 : cls;
348
+ };
349
+ }
350
+
351
+ /** Signed distance to a rounded rect {cx, cy, hw, hh, rc} (negative inside). */
352
+ export function sdRR(x, y, r) {
353
+ const qx = Math.abs(x - r.cx) - (r.hw - r.rc), qy = Math.abs(y - r.cy) - (r.hh - r.rc);
354
+ return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - r.rc;
355
+ }
356
+
357
+ // ---------- size classes ----------
358
+ function sizeOf(wc, cls) {
359
+ if (cls === 's') return 0.004 + 0.013 * wc.rnd() ** 1.4;
360
+ if (cls === 'm') return wc.r(0.022, 0.048);
361
+ return wc.r(0.052, 0.09);
362
+ }
363
+ const classes = (s, m, l) => [...Array(s).fill('s'), ...Array(m).fill('m'), ...Array(l).fill('l')];
364
+ /** Flake dims for a class: vertical bias, star spikes for medium/large (the base ellipse is shrunk to keep the size). */
365
+ function dims(wc, cls, L) {
366
+ const star = cls === 'l' ? wc.r(0.85, 1.2) : cls === 'm' ? wc.r(0.5, 0.95) : (wc.rnd() < 0.55 ? wc.r(0.3, 0.7) : 0);
367
+ const base = L / (1 + 0.45 * star);
368
+ const asp = wc.r(1.1, 1.8);
369
+ return { w: base / asp, h: base, star };
370
+ }
371
+
372
+ // ---------- cabinet wear ----------
373
+
374
+ /**
375
+ * geo: part rectangles in metres (front: x/y; back: x/y on the back wall):
376
+ * win, winIn, info, flap, flapIn, vent, items [{rect, inner?}], backGap, louvres [{rect, slats:[y0,y1]...}], screws
377
+ */
378
+ export function cabinetWear(spec, bp, geo) {
379
+ const { Lf, Lc, Ls, P, V0, V1, yr, R, vApex, sCornerF, sCornerB, sSeam } = bp;
380
+ const W = 2048, H = Math.round(W * (V1 - V0) / P);
381
+ const wc = new PlaneCanvas(W, H, -P / 2, P / 2, V0, V1, 4242, true);
382
+
383
+ const frontS = Lf + Lc; // |s| <= frontS: front face + front corner rounds (door)
384
+ const backS = P / 2 - Lf - Lc; // |s| >= backS: back face + back corner rounds
385
+ const onFront = (s) => Math.abs(s) <= frontS;
386
+ const onBack = (s) => Math.abs(s) >= backS;
387
+ const backX = (s) => (s >= 0 ? 1 : -1) * (P / 2 - Math.abs(s)); // inverse of sBack on the back wall
388
+ const pad = (r, o) => ({ ...r, hw: r.hw + o, hh: r.hh + o, rc: r.rc + o });
389
+
390
+ // --- exclusions: openings and parts that are not painted cabinet ---
391
+ const inHole = (s, v) => onFront(s) && (sdRR(s, v, geo.winIn) < 0.001 || sdRR(s, v, geo.flapIn) < 0.001);
392
+ const inItem = (s, v) => onFront(s) && geo.items.some((it) => sdRR(s, v, it.rect) < 0.002);
393
+ const inVent = (s, v) => onFront(s) && sdRR(s, v, geo.vent) < 0.002;
394
+ const inLouvre = (s, v) => onBack(s) && geo.louvres.some((L) => sdRR(backX(s), v, L.rect) < 0.004);
395
+ const paintOK = (s, v) => !inHole(s, v) && !inItem(s, v) && !inVent(s, v) && !inLouvre(s, v);
396
+ const onInfo = (s, v) => onFront(s) && sdRR(s, v, geo.info) < 0.008;
397
+
398
+ // ================= chips =================
399
+ /** Place `cls` flakes with positions from pos() (retried until paintOK && extra). */
400
+ const place = (cls, pos, extra = () => true, clip = paintOK) => {
401
+ for (const c of cls) {
402
+ const L = sizeOf(wc, c);
403
+ const d = dims(wc, c, L);
404
+ let s, v, tries = 0;
405
+ do { [s, v] = pos(d); } while ((!paintOK(s, v) || !extra(s, v)) && ++tries < 60);
406
+ if (tries >= 60) continue;
407
+ wc.flakeCluster({ s, v, ...d, rot: wc.r(-0.3, 0.3), clip, metal: c === 'l' && wc.rnd() < 0.3 });
408
+ }
409
+ };
410
+ const sg = () => (wc.rnd() < 0.5 ? -1 : 1);
411
+ // FRONT (door). Edge share ~45%: along the vertical rounds and the door edges.
412
+ const frontPos = (zone, edgeShare = 0.45) => (d) => {
413
+ const v = wc.r(zone[0], zone[1]);
414
+ if (wc.rnd() < edgeShare) return [sg() * (sCornerF + wc.r(-0.03, 0.02)), v];
415
+ return [wc.r(-Lf + 0.01, Lf - 0.01), v];
416
+ };
417
+ place(classes(8, 3, 0), frontPos([0.06, 0.10], 0.35)); // kick (mostly under mould)
418
+ place(classes(14, 5, 1), frontPos([0.10, 0.33], 0.4));
419
+ place(classes(24, 12, 1), frontPos([0.33, 0.95], 0.4), (s, v) => !onInfo(s, v) || wc.rnd() < 0.15);
420
+ place(classes(16, 8, 1), frontPos([0.95, 1.72], 0.75));
421
+ place(classes(6, 1, 0), (d) => [wc.r(-Lf, Lf), wc.r(yr + 0.01, V1 - 0.004)]); // front roof round
422
+ // tiny flecks (3-8 mm) scattered over the middle of the door
423
+ for (let i = 0; i < 36; i++) {
424
+ let s0, v0, t = 0;
425
+ do { s0 = wc.r(-Lf, Lf); v0 = wc.r(0.33, 0.95); } while (!paintOK(s0, v0) && ++t < 30);
426
+ const L = wc.r(0.003, 0.008);
427
+ wc.flake({ s: s0, v: v0, w: L * wc.r(0.6, 1), h: L, rot: wc.r(-0.5, 0.5), clip: paintOK });
428
+ }
429
+ // chips around the button panel: medium, ragged (zone up to 10-15% "light")
430
+ const bp0 = { cx: 0.245, cy: 0.80, hw: 0.14, hh: 0.14 };
431
+ place(classes(5, 5, 0), () => [bp0.cx + wc.r(-bp0.hw, bp0.hw), bp0.cy + wc.r(-bp0.hh, bp0.hh)]);
432
+ // window frame: 3-6 small chips along the outer edge
433
+ for (let i = 0; i < 5; i++) {
434
+ const t = wc.rnd(), r = geo.win;
435
+ const [s, v] = t < 0.5 ? [r.cx + sg() * (r.hw - wc.r(0.002, 0.01)), r.cy + wc.r(-r.hh, r.hh)] : [r.cx + wc.r(-r.hw, r.hw), r.cy + sg() * (r.hh - wc.r(0.002, 0.01))];
436
+ wc.flake({ s, v, w: wc.r(0.004, 0.01), h: wc.r(0.006, 0.014), clip: (ss, vv) => sdRR(ss, vv, geo.win) < 0 && sdRR(ss, vv, geo.winIn) > 0.002 });
437
+ }
438
+ // info panel: 1-2 medium on its edge, a couple of small inside
439
+ for (const [x, y, L] of [[-0.3, 0.66, 0.03], [0.03, 0.85, 0.022], [-0.12, 0.7, 0.01], [-0.22, 0.81, 0.008]]) {
440
+ const d = dims(wc, L > 0.02 ? 'm' : 's', L);
441
+ wc.flakeCluster({ s: x, v: y, ...d, clip: paintOK });
442
+ }
443
+ // flap frame: small chips on its bottom edge and corners
444
+ for (let i = 0; i < 4; i++) {
445
+ const r = geo.flap;
446
+ wc.flake({ s: r.cx + wc.r(-r.hw, r.hw), v: r.cy - r.hh + wc.r(0.003, 0.012), w: wc.r(0.005, 0.012), h: wc.r(0.004, 0.009), clip: paintOK });
447
+ }
448
+ // hero flakes where the concept has them (front: x, y, size)
449
+ const heroF = [[-0.125, 0.55, 0.085], [0.385, 0.48, 0.075], [-0.03, 0.645, 0.06], [-0.19, 0.72, 0.04], [0.40, 1.30, 0.06],
450
+ [-0.39, 1.41, 0.05], [-0.41, 0.75, 0.055], [-0.27, 0.18, 0.055], [0.36, 1.02, 0.05], [-0.35, 0.42, 0.05], [0.1, 0.95, 0.035], [-0.2, 0.96, 0.03]];
451
+ for (const [x, y, L0] of heroF) {
452
+ const L = L0 * 1.2;
453
+ const d = dims(wc, L >= 0.05 ? 'l' : 'm', L);
454
+ wc.flakeCluster({ s: x, v: y, ...d, rot: wc.r(-0.3, 0.3), clip: paintOK, metal: L > 0.08 && wc.rnd() < 0.5 });
455
+ }
456
+
457
+ // SIDES: chips along the back edge, the front edge and the seam, 1-2 large flakes per side
458
+ for (const side of [1, -1]) {
459
+ const sidePos = (zone) => () => {
460
+ const v = wc.r(zone[0], zone[1]), u = wc.rnd();
461
+ let s;
462
+ if (u < 0.35) s = side * (sCornerB + wc.r(-0.035, 0.012)); // along the back vertical edge
463
+ else if (u < 0.65) s = side * (sSeam + wc.r(-0.035, 0.01)); // at the seam (body side) / door strip
464
+ else s = side * wc.r(sSeam, sCornerB);
465
+ return [s, v];
466
+ };
467
+ place(classes(5, 2, 0), sidePos([0.06, 0.10]));
468
+ place(classes(7, 3, 0), sidePos([0.10, 0.33]));
469
+ place(classes(7, 4, 0), sidePos([0.33, 0.95]));
470
+ place(classes(6, 4, 0), sidePos([0.95, yr]));
471
+ place(classes(5, 1, 0), () => [side * wc.r(frontS, backS), wc.r(yr + 0.005, V1 - 0.003)]);
472
+ }
473
+ // side heroes (z, y, L): from the left/right views - near the back edge high, near the seam mid, low
474
+ const heroS = [[-1, -0.26, 1.5, 0.034], [-1, 0.21, 1.42, 0.03], [-1, 0.2, 1.07, 0.07], [-1, -0.24, 0.61, 0.032], [-1, -0.17, 0.45, 0.06], [-1, 0.21, 0.35, 0.03],
475
+ [1, -0.25, 1.25, 0.07], [1, 0.2, 0.95, 0.032], [1, -0.22, 0.55, 0.03], [1, 0.19, 0.3, 0.06], [1, -0.05, 1.55, 0.025], [1, 0.21, 1.5, 0.028]];
476
+ for (const [side, z, y, L0] of heroS) {
477
+ const L = L0 >= 0.05 ? L0 * 1.3 : L0;
478
+ const d = dims(wc, L >= 0.05 ? 'l' : 'm', L);
479
+ wc.flakeCluster({ s: bp.sSide(side, z), v: y, ...d, rot: wc.r(-0.3, 0.3), clip: paintOK, metal: L > 0.07 && wc.rnd() < 0.5 });
480
+ }
481
+
482
+ // BACK: 3-5 big star flakes mid/upper, 20-40 small, heavy wear along both vertical edges
483
+ const backPos = (zone, edgeShare) => () => {
484
+ const v = wc.r(zone[0], zone[1]);
485
+ if (wc.rnd() < edgeShare) return [sg() * (sCornerB + wc.r(-0.015, 0.035)), v];
486
+ return [bp.sBack(wc.r(-Lf + 0.01, Lf - 0.01)), v];
487
+ };
488
+ place(classes(7, 2, 0), backPos([0.06, 0.10], 0.4));
489
+ place(classes(10, 5, 1), backPos([0.10, 0.33], 0.6));
490
+ place(classes(18, 10, 2), backPos([0.33, 0.95], 0.5));
491
+ place(classes(16, 8, 1), backPos([0.95, yr], 0.5));
492
+ place(classes(6, 1, 0), () => [sg() * wc.r(backS, P / 2), wc.r(yr + 0.005, V1 - 0.003)]);
493
+ // back heroes (object x, y, L)
494
+ const heroB = [[0.14, 1.36, 0.09], [-0.07, 0.9, 0.095], [0.30, 1.03, 0.05], [-0.215, 1.67, 0.06], [0.34, 0.52, 0.075], [-0.35, 1.23, 0.05],
495
+ [-0.06, 0.64, 0.035], [-0.36, 0.86, 0.05], [-0.36, 0.12, 0.05], [0.25, 0.72, 0.03], [-0.25, 1.45, 0.03]];
496
+ for (const [x, y, L0] of heroB) {
497
+ const L = L0 * 1.4;
498
+ const d = dims(wc, L >= 0.05 ? 'l' : 'm', L);
499
+ wc.flakeCluster({ s: bp.sBack(x), v: y, ...d, rot: wc.r(-0.3, 0.3), clip: paintOK, metal: L > 0.085 && wc.rnd() < 0.5 });
500
+ }
501
+
502
+ // ================= edge wear + rust grime + mould (per texel) =================
503
+ const edgeAt = edgeWearSampler(wc.seed + 500);
504
+ const grimeN = (x, y, s) => fbmA(x, y, 0.3, 45, 110, 3, 0.6, s);
505
+ const mouldN = (x, y) => fbmA(x, y, 0.7, 26, 30, 4, 0.55, 907);
506
+ const roofLo = yr - 0.004;
507
+ wc.pass((k, s, v) => {
508
+ const as = Math.abs(s);
509
+ let hit = 0;
510
+ const z = 0.37; // third noise coordinate (constant) for planar sampling
511
+ // vertical edges (apex of the R 3 cm rounds): front ~30%, back ~40% of the length in broken runs
512
+ const dF = Math.abs(as - sCornerF), dB = Math.abs(as - sCornerB);
513
+ if (dF < 0.008 && v < V1) hit = Math.max(hit, edgeAt(s, v, z, 1 - dF / 0.006, 0.22, 22));
514
+ if (dB < 0.009 && v < V1) hit = Math.max(hit, edgeAt(s, v, z + 3, 1 - dB / 0.007, 0.3, 22));
515
+ // roof rounding apex: broken light strip along the front and side top edges (and the back)
516
+ if (v > roofLo) {
517
+ const dv = Math.abs(v - vApex);
518
+ const cov = onFront(s) ? 0.6 : onBack(s) ? 0.5 : 0.45;
519
+ hit = Math.max(hit, edgeAt(s, v, z + 7, 1 - dv / 0.0065, cov + 0.1, 9));
520
+ // the roof-corner junctions are the most exposed points
521
+ if (dF < 0.03 || dB < 0.03) hit = Math.max(hit, edgeAt(s, v, z + 9, 1 - Math.hypot(Math.min(dF, dB), dv) / 0.01, 0.5, 30));
522
+ }
523
+ // seam lips (body side and door side of the 4 mm gap)
524
+ const dS = Math.abs(as - sSeam);
525
+ if (dS < 0.007 && v < yr) hit = Math.max(hit, edgeAt(s, v, z + 11, 1 - Math.max(0, dS - 0.002) / 0.004, 0.38, 12, false));
526
+ // bottom edge
527
+ if (v < V0 + 0.008) hit = Math.max(hit, edgeAt(s, v, z + 13, 1 - (v - V0) / 0.006, 0.3, 20));
528
+
529
+ if (onFront(s)) {
530
+ // window frame outer round, inner lip
531
+ const dw = sdRR(s, v, geo.win), di = sdRR(s, v, geo.winIn);
532
+ if (dw < 0 && dw > -0.006) hit = Math.max(hit, edgeAt(s, v, z + 17, 1 + dw / 0.005, 0.3, 22));
533
+ if (di > 0 && di < 0.004) hit = Math.max(hit, edgeAt(s, v, z + 19, 1 - di / 0.003, 0.12, 26, false));
534
+ // flap frame
535
+ const df = sdRR(s, v, geo.flap);
536
+ if (df < 0 && df > -0.006) hit = Math.max(hit, edgeAt(s, v, z + 23, 1 + df / 0.005, v < geo.flap.cy ? 0.45 : 0.25, 22));
537
+ // info panel contour bead (broken)
538
+ const dn = sdRR(s, v, geo.info);
539
+ // thin dark line of dirt in the joint outside the contour bead
540
+ if (dn > 0.0058 && dn < 0.0078) { const a = 0.45 * (1 - Math.abs(dn - 0.0068) / 0.001); if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = 1; } }
541
+ if (dn > -0.001 && dn < 0.007) hit = Math.max(hit, edgeAt(s, v, z + 29, 1 - Math.abs(dn - 0.003) / 0.0035, 0.3, 24, false));
542
+ // metal housings: rubbed to bright steel along their rims
543
+ for (const it of geo.items) {
544
+ const de = sdRR(s, v, it.rect);
545
+ if (de < 0 && de > -0.004) hit = Math.max(hit, edgeAt(s, v, z + 31, 1 + de / 0.0035, 0.35, 14, false, 3));
546
+ if (it.inner) {
547
+ const dI = sdRR(s, v, it.inner);
548
+ if (dI > 0 && dI < 0.003) hit = Math.max(hit, edgeAt(s, v, z + 37, 1 - dI / 0.0025, 0.25, 16, false, 3));
549
+ }
550
+ }
551
+ const dv = sdRR(s, v, geo.vent);
552
+ if (dv < 0 && dv > -0.003) hit = Math.max(hit, edgeAt(s, v, z + 41, 1 + dv / 0.003, 0.4, 30, false, 3));
553
+
554
+ // rust grime: along the top edge of the info contour, under every housing, under the flap frame lip
555
+ const n = grimeN(s, v, 905);
556
+ let a = 0;
557
+ const inf = geo.info;
558
+ if (Math.abs(s - inf.cx) < inf.hw - 0.01 && v < inf.cy + inf.hh + 0.004 && v > inf.cy + inf.hh - 0.006) a = 0.5 * smooth(0.42, 0.62, n);
559
+ for (const it of geo.items) {
560
+ const r = it.rect;
561
+ if (Math.abs(s - r.cx) < r.hw - 0.004 && v < r.cy - r.hh && v > r.cy - r.hh - 0.006) a = Math.max(a, 0.9 * smooth(0.22, 0.45, n) * (1 - (r.cy - r.hh - v) / 0.008));
562
+ }
563
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.75); }
564
+ }
565
+ if (onBack(s)) {
566
+ const x = backX(s);
567
+ // panel contour gap lips
568
+ const dg = Math.abs(sdRR(x, v, geo.backGap) + 0.0015);
569
+ if (dg < 0.005) hit = Math.max(hit, edgeAt(s, v, z + 43, 1 - Math.max(0, dg - 0.0015) / 0.003, 0.25, 22, false));
570
+ // rust seeping along the contour gap, patchy (heavier on the sides and the top)
571
+ if (dg < 0.006) {
572
+ const a = 0.75 * smooth(0.45, 0.7, grimeN(s, v, 917)) * (1 - dg / 0.006);
573
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.6); }
574
+ }
575
+ // louvre slats: top lips worn, rust grime under every slat
576
+ for (const L of geo.louvres) {
577
+ if (Math.abs(x - L.rect.cx) > L.rect.hw - 0.004) continue;
578
+ for (const [y0, y1] of L.slats) {
579
+ if (Math.abs(v - y1) < 0.003) hit = Math.max(hit, edgeAt(s, v, z + 47, 1 - Math.abs(v - y1) / 0.0025, 0.18, 14, false));
580
+ if (v < y0 && v > y0 - 0.006) {
581
+ const a = 0.7 * smooth(0.3, 0.55, grimeN(s, v, 911));
582
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.7); }
583
+ }
584
+ }
585
+ }
586
+ // rust grime along the top edge of the contour
587
+ const top = geo.backGap.cy + geo.backGap.hh;
588
+ if (Math.abs(x) < geo.backGap.hw - 0.01 && v < top - 0.002 && v > top - 0.009) {
589
+ const a = 0.75 * smooth(0.32, 0.58, grimeN(s, v, 913));
590
+ if (a > wc.rust[k]) { wc.rust[k] = a; wc.rdark[k] = Math.max(wc.rdark[k], 0.75); }
591
+ }
592
+ }
593
+ // olive grime / mould: only the lowest 5-8 cm of the cabinet, in patches with ragged tops
594
+ const h = v - V0;
595
+ if (h < 0.1) {
596
+ const m = mouldN(s, v) + 0.32 * (1 - h / 0.05) - 0.2 * smooth(0.03, 0.09, h) + (onFront(s) || onBack(s) ? 0 : 0.06);
597
+ wc.mould[k] = 0.8 * smooth(0.58, 0.68, m) * (1 - smooth(0.06, 0.095, h));
598
+ }
599
+ if (hit && !wc.chip[k]) wc.chip[k] = hit;
600
+ });
601
+
602
+ // ================= drips (on top) =================
603
+ const winB = geo.win.cy - geo.win.hh;
604
+ // under the window frame: 2-4 short (2-5 cm)
605
+ for (const x of [-0.27, -0.1, 0.12, 0.29]) {
606
+ if (wc.rnd() < 0.2) continue;
607
+ wc.cluster({ s: x + wc.r(-0.02, 0.02), v: winB, span: wc.r(0.008, 0.016), stainH: wc.r(0.005, 0.009), maxLen: wc.r(0.02, 0.05), n: 1 + Math.floor(wc.rnd() * 2) });
608
+ }
609
+ // info panel: 2-6 from under the top edge of the contour, running over the panel (3-8 cm)
610
+ const inf = geo.info, infTop = inf.cy + inf.hh;
611
+ for (const x of [-0.29, -0.2, -0.09, 0.0]) {
612
+ wc.cluster({ s: x + wc.r(-0.015, 0.015), v: infTop - 0.004, span: wc.r(0.008, 0.018), stainH: wc.r(0.006, 0.01), maxLen: wc.r(0.04, 0.1), n: 1 + Math.floor(wc.rnd() * 3) });
613
+ }
614
+ // and a few from its bottom edge onto the door
615
+ for (const x of [-0.25, -0.04]) wc.cluster({ s: x + wc.r(-0.02, 0.02), v: inf.cy - inf.hh - 0.006, span: wc.r(0.008, 0.014), stainH: 0.006, maxLen: wc.r(0.03, 0.07), n: 2 });
616
+ // under every metal element: 3-8 cm, 2-4 strands (rust ~7-8% of the panel zone)
617
+ for (const it of geo.items) {
618
+ const r = it.rect;
619
+ const nC = 2;
620
+ for (let c = 0; c < nC; c++) {
621
+ const x = r.cx + (nC === 1 ? wc.r(-0.3, 0.3) : (c ? 0.45 : -0.45) + wc.r(-0.15, 0.15)) * r.hw;
622
+ wc.cluster({ s: x, v: r.cy - r.hh - 0.001, span: wc.r(0.022, 0.04), stainH: wc.r(0.016, 0.026), maxLen: wc.r(0.04, 0.08), n: 1 + Math.floor(wc.rnd() * 5), alpha: 1, w: [0.005, 0.009] });
623
+ }
624
+ }
625
+ // under the flap frame: 3-6 clusters, 3-10 cm
626
+ // from the opening's bottom lip: over the light frame and on down the door (3-6 clusters, 3-10 cm)
627
+ const flB = geo.flapIn.cy - geo.flapIn.hh;
628
+ wc.dripLine({ v: flB - 0.001, sA: geo.flap.cx - geo.flap.hw + 0.03, sB: geo.flap.cx + geo.flap.hw - 0.02, spacing: 0.1, keep: 0.95, n: [1, 4], len: [0.04, 0.1], longP: 0.25, longLen: [0.1, 0.12], span: [0.012, 0.026], stainH: [0.006, 0.01] });
629
+ // under the small vent: 1-3, 2-5 cm
630
+ const vB = geo.vent.cy - geo.vent.hh;
631
+ for (const x of [geo.vent.cx - 0.02, geo.vent.cx + 0.022]) wc.cluster({ s: x, v: vB, span: wc.r(0.008, 0.014), stainH: 0.006, maxLen: wc.r(0.02, 0.05), n: 1 + Math.floor(wc.rnd() * 2) });
632
+
633
+ // sides: 1-3 per side from under the roof round and at the seam
634
+ for (const side of [1, -1]) {
635
+ wc.cluster({ s: side * (bp.sSideMid + wc.r(-0.15, 0.15)), v: yr + 0.002, span: 0.012, stainH: 0.008, maxLen: wc.r(0.04, 0.09), n: 2 });
636
+ wc.cluster({ s: side * (sSeam + 0.004), v: wc.r(1.1, 1.5), span: 0.008, stainH: 0.006, maxLen: wc.r(0.04, 0.08), n: 1, alpha: 0.85 });
637
+ wc.cluster({ s: side * (sCornerB - 0.02), v: wc.r(0.8, 1.2), span: 0.01, stainH: 0.006, maxLen: wc.r(0.04, 0.08), n: 2, alpha: 0.85 });
638
+ wc.cluster({ s: side * (sSeam - 0.012), v: wc.r(0.45, 0.8), span: 0.01, stainH: 0.006, maxLen: wc.r(0.04, 0.09), n: 2, alpha: 0.85 });
639
+ wc.cluster({ s: side * (bp.sSideMid + wc.r(-0.1, 0.1)), v: yr + 0.002, span: 0.014, stainH: 0.008, maxLen: wc.r(0.03, 0.07), n: 3 });
640
+ }
641
+ // back: 5-10 from under the top of the contour, under every louvre block
642
+ const bTop = geo.backGap.cy + geo.backGap.hh;
643
+ wc.dripLine({ v: bTop - 0.004, sA: bp.sBack(0.001) - 0.0, sB: P / 2, spacing: 0.075, keep: 0.9, n: [1, 4], len: [0.04, 0.11], longP: 0.35, longLen: [0.11, 0.16], span: [0.01, 0.024],
644
+ skip: (s) => P / 2 - s > geo.backGap.hw - 0.02 });
645
+ wc.dripLine({ v: bTop - 0.004, sA: -P / 2, sB: -(P / 2 - geo.backGap.hw + 0.03), spacing: 0.075, keep: 0.9, n: [1, 4], len: [0.04, 0.11], longP: 0.35, longLen: [0.11, 0.16], span: [0.01, 0.024] });
646
+ // down the contour sides: rust seeping from the gap (a few runs on each side)
647
+ for (const sx of [-1, 1]) for (let i = 0; i < 3; i++) {
648
+ wc.cluster({ s: bp.sBack(sx * (geo.backGap.hw - 0.004)), v: wc.r(0.5, 1.6), span: 0.008, stainH: 0.006, maxLen: wc.r(0.04, 0.1), n: 1 + Math.floor(wc.rnd() * 2), alpha: 0.8 });
649
+ }
650
+ for (const L of geo.louvres) {
651
+ const yB = L.rect.cy - L.rect.hh;
652
+ for (let c = 0; c < 2; c++) {
653
+ const x = L.rect.cx + (c ? wc.r(0.15, 0.75) : wc.r(-0.75, -0.15)) * L.rect.hw;
654
+ wc.cluster({ s: bp.sBack(x), v: yB, span: wc.r(0.028, 0.045), stainH: wc.r(0.012, 0.018), maxLen: wc.r(0.04, 0.09), n: 1 + Math.floor(wc.rnd() * 4) });
655
+ }
656
+ }
657
+ // back vertical edges: a couple of runs from the roof
658
+ for (const sgn of [1, -1]) wc.cluster({ s: sgn * (backS + wc.r(0.01, 0.03)), v: yr, span: 0.01, stainH: 0.007, maxLen: wc.r(0.04, 0.1), n: 2 });
659
+
660
+ wc.compose({
661
+ primer: ['#A9A598', '#C0BCAE'], rim: '#A26C3C', metal: '#8E8B83', steel: '#B9BAB4',
662
+ rust: ['#AE6E36', '#5C3218'], mould: ['#6F7046', '#585A36'],
663
+ });
664
+ return wc.texture(true);
665
+ }
666
+
667
+ /** Flat roof: a few small flakes and short rubbed spots, faint dust. */
668
+ export function roofWear(spec, bp) {
669
+ const { A, B, R } = bp;
670
+ const hx = A - R, hz = B - R;
671
+ const W = 512, H = Math.round(W * hz / hx);
672
+ const wc = new PlaneCanvas(W, H, -hx, hx, -hz, hz, 99, false);
673
+ for (const c of classes(10, 3, 0)) {
674
+ const L = sizeOf(wc, c), d = dims(wc, c, L);
675
+ const edge = wc.rnd() < 0.6;
676
+ const s = edge ? sg2(wc) * (hx - wc.r(0, 0.03)) : wc.r(-hx, hx);
677
+ const v = edge && wc.rnd() < 0.5 ? sg2(wc) * (hz - wc.r(0, 0.03)) : wc.r(-hz, hz);
678
+ wc.flakeCluster({ s, v, ...d });
679
+ }
680
+ wc.compose({ primer: ['#A9A598', '#C0BCAE'], rim: '#A26C3C', metal: '#8E8B83', steel: '#B9BAB4', rust: ['#A5622D', '#5C3218'], mould: ['#6F7046', '#585A36'] });
681
+ return wc.texture(false);
682
+ }
683
+ const sg2 = (wc) => (wc.rnd() < 0.5 ? -1 : 1);
684
+
685
+ // ---------- shader patch ----------
686
+
687
+ const GLSL_COMMON = /* glsl */`
688
+ varying vec3 vObjPos;
689
+ varying vec3 vObjN;
690
+ varying vec3 vZView;
691
+ uniform sampler2D beltMap;
692
+ uniform sampler2D roofMap;
693
+ uniform vec4 cabA; // A, B, rc, R
694
+ uniform vec4 cabB; // y1, y0, Rb, zs
695
+ uniform vec4 cabC; // Lf, Lc, Ls, P
696
+ uniform vec2 beltV; // V0, V1
697
+ uniform float usePaint;
698
+ uniform float useWear;
699
+ uniform vec3 paintFront;
700
+ uniform vec3 paintSide;
701
+ uniform vec3 paintTop;
702
+ uniform vec3 paintBack;
703
+ uniform vec3 paintFront2;
704
+ uniform float seamGap;
705
+ vec4 gWear = vec4(0.0);
706
+ float h3(vec3 p) { p = fract(p * 0.3183099 + 0.1); p *= 17.0; return fract(p.x * p.y * p.z * (p.x + p.y + p.z)); }
707
+ float vn3(vec3 x) {
708
+ vec3 i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f);
709
+ return mix(mix(mix(h3(i), h3(i + vec3(1, 0, 0)), f.x), mix(h3(i + vec3(0, 1, 0)), h3(i + vec3(1, 1, 0)), f.x), f.y),
710
+ mix(mix(h3(i + vec3(0, 0, 1)), h3(i + vec3(1, 0, 1)), f.x), mix(h3(i + vec3(0, 1, 1)), h3(i + vec3(1, 1, 1)), f.x), f.y), f.z);
711
+ }
712
+ vec2 beltUV(vec3 p) {
713
+ float A = cabA.x, B = cabA.y, rc = cabA.z, R = cabA.w, y1 = cabB.x, y0 = cabB.y, Rb = cabB.z;
714
+ float Lf = cabC.x, Lc = cabC.y, Ls = cabC.z, P = cabC.w;
715
+ float yr = y1 - R, v = p.y, d = 0.0;
716
+ if (p.y > yr) { float t = clamp((p.y - yr) / R, 0.0, 1.0); float ang = asin(t); v = yr + R * ang; d = R - R * cos(ang); }
717
+ else if (p.y < y0 + Rb) { float t = clamp((y0 + Rb - p.y) / Rb, 0.0, 1.0); d = Rb - Rb * sqrt(1.0 - t * t); }
718
+ float a = A - d, b = B - d;
719
+ float ax = abs(p.x), qx = ax - (a - rc), qz = abs(p.z) - (b - rc);
720
+ bool front = p.z >= 0.0;
721
+ float s;
722
+ if (qx <= 0.0) { float fx = ax / max(a - rc, 1e-4) * Lf; s = front ? fx : (P * 0.5 - fx); }
723
+ else if (qz <= 0.0) { s = Lf + Lc + ((b - rc) - p.z) / (2.0 * (b - rc)) * Ls; }
724
+ else { float t = atan(qx, qz) / 1.5707963; s = front ? Lf + t * Lc : Lf + Lc + Ls + (1.0 - t) * Lc; }
725
+ if (p.x < 0.0) s = -s;
726
+ return vec2(s / P + 0.5, (v - beltV.x) / (beltV.y - beltV.x));
727
+ }
728
+ `;
729
+
730
+ /**
731
+ * Patch a MeshStandardMaterial with the wear layers. opts.paint: two-tone door/body paint split at the seam plane
732
+ * (pale front door, darker sides/back), faint vertical streaks, the seam gap with rounded lips (normal tilt).
733
+ */
734
+ export function applyWear(material, belt, roof, bp, { paint = false, wear = true, front = '#A9C6C6', side = '#5E8C9A', top = '#86AEB4', back = '#86AEB4', front2 = null, chipRough = 0.86 } = {}) {
735
+ material.onBeforeCompile = (shader) => {
736
+ Object.assign(shader.uniforms, {
737
+ beltMap: { value: belt }, roofMap: { value: roof },
738
+ cabA: { value: new THREE.Vector4(bp.A, bp.B, bp.rc, bp.R) },
739
+ cabB: { value: new THREE.Vector4(bp.y1, bp.y0, bp.Rb, bp.zs) },
740
+ cabC: { value: new THREE.Vector4(bp.Lf, bp.Lc, bp.Ls, bp.P) },
741
+ beltV: { value: new THREE.Vector2(bp.V0, bp.V1) },
742
+ usePaint: { value: paint ? 1 : 0 }, useWear: { value: wear ? 1 : 0 },
743
+ paintFront: { value: new THREE.Color(front) }, paintSide: { value: new THREE.Color(side) }, paintTop: { value: new THREE.Color(top) }, paintBack: { value: new THREE.Color(back) }, paintFront2: { value: new THREE.Color(front2 ?? front) },
744
+ seamGap: { value: bp.gap },
745
+ });
746
+ shader.vertexShader = shader.vertexShader
747
+ .replace('#include <common>', '#include <common>\nvarying vec3 vObjPos;\nvarying vec3 vObjN;\nvarying vec3 vZView;')
748
+ .replace('#include <begin_vertex>', '#include <begin_vertex>\nvObjPos = position;\nvObjN = normal;\nvZView = normalize(normalMatrix * vec3(0.0, 0.0, 1.0));');
749
+ shader.fragmentShader = shader.fragmentShader
750
+ .replace('#include <common>', '#include <common>\n' + GLSL_COMMON)
751
+ .replace('#include <map_fragment>', `#include <map_fragment>
752
+ {
753
+ vec3 p = vObjPos;
754
+ if (usePaint > 0.5) {
755
+ // body: darker teal on the walls, the sun-faded mid tone on the roof; pale door in front of the seam
756
+ vec3 body = mix(paintSide, paintBack, smoothstep(0.3, 0.9, -vObjN.z));
757
+ body = mix(body, paintTop, smoothstep(0.35, 0.95, vObjN.y));
758
+ vec3 door = mix(paintFront, paintFront2, smoothstep(0.6, 1.0, p.y));
759
+ vec3 base = mix(body, door, smoothstep(cabB.w - 0.0006, cabB.w + 0.0006, p.z));
760
+ // faded paint: faint vertical streaks + soft blotches
761
+ float st = vn3(p * vec3(14.0, 1.1, 14.0)) * 0.6 + vn3(p * vec3(40.0, 3.0, 40.0)) * 0.4;
762
+ float bl = vn3(p * 4.0 + 7.3);
763
+ base *= 1.0 + 0.1 * (st - 0.5) + 0.06 * (bl - 0.5) + 0.05 * (vn3(p * 60.0) - 0.5);
764
+ diffuseColor.rgb = base * diffuseColor.rgb;
765
+ }
766
+ if (useWear > 0.5) {
767
+ bool roofTop = p.y > cabB.x - 0.0015 && vObjN.y > 0.9;
768
+ if (roofTop) {
769
+ vec2 h = vec2(cabA.x - cabA.w, cabA.y - cabA.w);
770
+ gWear = texture2D(roofMap, vec2((p.x + h.x) / (2.0 * h.x), (p.z + h.y) / (2.0 * h.y)));
771
+ } else gWear = texture2D(beltMap, beltUV(p));
772
+ diffuseColor.rgb = mix(diffuseColor.rgb, gWear.rgb, gWear.a);
773
+ }
774
+ if (usePaint > 0.5 && abs(vObjN.z) < 0.97) {
775
+ float dz = abs(p.z - cabB.w);
776
+ diffuseColor.rgb *= mix(0.18, 1.0, smoothstep(seamGap * 0.5 - 0.0003, seamGap * 0.5 + 0.0004, dz));
777
+ }
778
+ }`)
779
+ .replace('#include <roughnessmap_fragment>', `#include <roughnessmap_fragment>
780
+ roughnessFactor = mix(roughnessFactor, ${chipRough.toFixed(3)}, gWear.a);`)
781
+ .replace('#include <normal_fragment_maps>', `#include <normal_fragment_maps>
782
+ if (usePaint > 0.5 && abs(vObjN.z) < 0.97) {
783
+ // rounded lips of the door seam: the surface rolls into the gap on both sides
784
+ float dz = vObjPos.z - cabB.w, hg = seamGap * 0.5, lip = 0.0022;
785
+ vec3 t = vZView - normal * dot(normal, vZView);
786
+ float tl = length(t);
787
+ if (tl > 1e-3) {
788
+ t /= tl;
789
+ float door = smoothstep(hg + lip, hg, dz) * step(hg * 0.6, dz);
790
+ float body = smoothstep(-hg - lip, -hg, dz) * step(dz, -hg * 0.6);
791
+ normal = normalize(normal - t * 1.2 * door + t * 1.2 * body);
792
+ }
793
+ }`);
794
+ };
795
+ material.customProgramCacheKey = () => `vmwear-${material.uuid}`;
796
+ material.needsUpdate = true;
797
+ return material;
798
+ }