@kruschdev/pascal-plugin-mit-assets 0.0.0-stage → 0.1.0

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 (78) hide show
  1. package/ASSETS.md +298 -0
  2. package/LICENSE +21 -0
  3. package/README.md +71 -2
  4. package/dist/agent-operations.d.ts +8 -0
  5. package/dist/agent-operations.js +305 -0
  6. package/dist/agent-tools.d.ts +7 -0
  7. package/dist/agent-tools.js +128 -0
  8. package/dist/audit.d.ts +78 -0
  9. package/dist/bin/pascal-mit-assets.js +5238 -0
  10. package/dist/bom.d.ts +123 -0
  11. package/dist/cad/appliance-mep-builders.d.ts +7 -0
  12. package/dist/cad/furniture-builders.d.ts +9 -0
  13. package/dist/cad/index.d.ts +48 -0
  14. package/dist/cad/index.js +23 -0
  15. package/dist/cad/kitchen-bath-builders.d.ts +9 -0
  16. package/dist/cad/outdoor-fenestration-builders.d.ts +8 -0
  17. package/dist/cad/primitives.d.ts +56 -0
  18. package/dist/cad/props-vehicles-builders.d.ts +8 -0
  19. package/dist/chunk-2qgndrap.js +5200 -0
  20. package/dist/chunk-6hzyh6qv.js +346 -0
  21. package/dist/chunk-f72r2bmp.js +84 -0
  22. package/dist/chunk-hqgm57dv.js +310 -0
  23. package/dist/chunk-kgwk6kan.js +150 -0
  24. package/dist/chunk-raafkn3w.js +286 -0
  25. package/dist/chunk-vzmjt9dz.js +4380 -0
  26. package/dist/chunk-xhkwv6t4.js +5080 -0
  27. package/dist/hdri/index.d.ts +1 -0
  28. package/dist/hdri/index.js +322 -0
  29. package/dist/hdri/sky.d.ts +26 -0
  30. package/dist/index.d.ts +30 -0
  31. package/dist/index.js +198 -0
  32. package/dist/manifest.d.ts +46 -0
  33. package/dist/materials/index.d.ts +10 -0
  34. package/dist/materials/index.js +1464 -0
  35. package/dist/materials/ktx2.d.ts +10 -0
  36. package/dist/materials/noise.d.ts +89 -0
  37. package/dist/materials/patterns.d.ts +15 -0
  38. package/dist/nodes/mit-item.d.ts +76 -0
  39. package/dist/render.d.ts +10 -0
  40. package/dist/render.js +24 -0
  41. package/dist/scripts/build-assets.js +17129 -0
  42. package/dist/sfx/index.d.ts +1 -0
  43. package/dist/sfx/index.js +1223 -0
  44. package/dist/sfx/synth.d.ts +25 -0
  45. package/dist/sku-crosswalk.d.ts +46 -0
  46. package/dist/ui/chat-card.d.ts +7 -0
  47. package/dist/ui/host-panel.d.ts +1 -0
  48. package/dist/ui/inspector-card.d.ts +4 -0
  49. package/dist/ui/schedule-view.d.ts +1 -0
  50. package/package.json +135 -4
  51. package/src/agent-operations.ts +373 -0
  52. package/src/agent-tools.ts +136 -0
  53. package/src/audit.ts +356 -0
  54. package/src/bom.ts +512 -0
  55. package/src/cad/appliance-mep-builders.ts +1336 -0
  56. package/src/cad/furniture-builders.ts +1622 -0
  57. package/src/cad/index.ts +993 -0
  58. package/src/cad/kitchen-bath-builders.ts +1189 -0
  59. package/src/cad/outdoor-fenestration-builders.ts +1499 -0
  60. package/src/cad/primitives.ts +286 -0
  61. package/src/cad/props-vehicles-builders.ts +1065 -0
  62. package/src/hdri/index.ts +5 -0
  63. package/src/hdri/sky.ts +481 -0
  64. package/src/index.ts +127 -0
  65. package/src/manifest.ts +5296 -0
  66. package/src/materials/index.ts +56 -0
  67. package/src/materials/ktx2.ts +138 -0
  68. package/src/materials/noise.ts +417 -0
  69. package/src/materials/patterns.ts +1507 -0
  70. package/src/nodes/mit-item.ts +163 -0
  71. package/src/render.ts +25 -0
  72. package/src/sfx/index.ts +6 -0
  73. package/src/sfx/synth.ts +1495 -0
  74. package/src/sku-crosswalk.ts +3868 -0
  75. package/src/ui/chat-card.tsx +93 -0
  76. package/src/ui/host-panel.tsx +330 -0
  77. package/src/ui/inspector-card.tsx +213 -0
  78. package/src/ui/schedule-view.tsx +188 -0
@@ -0,0 +1,1464 @@
1
+ // src/materials/noise.ts
2
+ function fnv1a32(input) {
3
+ let hash = 2166136261;
4
+ for (let i = 0;i < input.length; i++) {
5
+ hash ^= input.charCodeAt(i);
6
+ hash = Math.imul(hash, 16777619);
7
+ }
8
+ return hash >>> 0;
9
+ }
10
+ function mulberry32(seed) {
11
+ let state = seed >>> 0;
12
+ return () => {
13
+ state = state + 1831565813 >>> 0;
14
+ let t = Math.imul(state ^ state >>> 15, 1 | state);
15
+ t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
16
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
17
+ };
18
+ }
19
+ function createSeededRng(seed) {
20
+ const numeric = typeof seed === "string" ? fnv1a32(seed) : seed >>> 0;
21
+ return mulberry32(numeric);
22
+ }
23
+ function wrapInt(value, period) {
24
+ const p = period > 0 ? period : 1;
25
+ const r = value % p;
26
+ return r < 0 ? r + p : r;
27
+ }
28
+ function fract(x) {
29
+ return x - Math.floor(x);
30
+ }
31
+ function clamp(value, min, max) {
32
+ if (value < min)
33
+ return min;
34
+ if (value > max)
35
+ return max;
36
+ return value;
37
+ }
38
+ function clampByte(value) {
39
+ if (!(value > 0))
40
+ return 0;
41
+ if (value >= 255)
42
+ return 255;
43
+ return Math.round(value);
44
+ }
45
+ function lerp(a, b, t) {
46
+ return a + (b - a) * t;
47
+ }
48
+ function smoothstep(edge0, edge1, x) {
49
+ if (edge0 === edge1)
50
+ return x < edge0 ? 0 : 1;
51
+ const t = clamp((x - edge0) / (edge1 - edge0), 0, 1);
52
+ return t * t * (3 - 2 * t);
53
+ }
54
+ function quintic(t) {
55
+ return t * t * t * (t * (t * 6 - 15) + 10);
56
+ }
57
+ function hash2D(ix, iy, seed) {
58
+ let h = Math.imul(ix, 374761393) + Math.imul(iy, 668265263) + Math.imul(seed, 1442695041) | 0;
59
+ h = Math.imul(h ^ h >>> 13, 1274126177);
60
+ h = h ^ h >>> 16;
61
+ return (h >>> 0) / 4294967296;
62
+ }
63
+ function hash2DVec(ix, iy, seed) {
64
+ return [hash2D(ix, iy, seed), hash2D(ix + 1013, iy - 1619, (seed ^ 2654435769) >>> 0)];
65
+ }
66
+ function periodicValueNoise2D(u, v, periodX, periodY, seed) {
67
+ const px = Math.max(1, Math.round(periodX));
68
+ const py = Math.max(1, Math.round(periodY));
69
+ const sx = fract(u) * px;
70
+ const sy = fract(v) * py;
71
+ const ix0 = Math.floor(sx);
72
+ const iy0 = Math.floor(sy);
73
+ const fx = quintic(sx - ix0);
74
+ const fy = quintic(sy - iy0);
75
+ const x0 = wrapInt(ix0, px);
76
+ const x1 = (x0 + 1) % px;
77
+ const y0 = wrapInt(iy0, py);
78
+ const y1 = (y0 + 1) % py;
79
+ const v00 = hash2D(x0, y0, seed);
80
+ const v10 = hash2D(x1, y0, seed);
81
+ const v01 = hash2D(x0, y1, seed);
82
+ const v11 = hash2D(x1, y1, seed);
83
+ const nx0 = v00 + (v10 - v00) * fx;
84
+ const nx1 = v01 + (v11 - v01) * fx;
85
+ return nx0 + (nx1 - nx0) * fy;
86
+ }
87
+ function periodicFbm2D(u, v, basePeriodX, basePeriodY, octaves, seed, persistence = 0.5, lacunarity = 2) {
88
+ let px = Math.max(1, Math.round(basePeriodX));
89
+ let py = Math.max(1, Math.round(basePeriodY));
90
+ const lac = Math.max(1, Math.round(lacunarity));
91
+ const count = Math.max(1, Math.round(octaves));
92
+ let sum = 0;
93
+ let amp = 1;
94
+ let norm = 0;
95
+ let s = seed >>> 0;
96
+ for (let i = 0;i < count; i++) {
97
+ sum += periodicValueNoise2D(u, v, px, py, s) * amp;
98
+ norm += amp;
99
+ amp *= persistence;
100
+ px *= lac;
101
+ py *= lac;
102
+ s = Math.imul(s ^ 2246822507, 3266489909) + 668265263 >>> 0;
103
+ }
104
+ return norm > 0 ? sum / norm : 0.5;
105
+ }
106
+ function periodicRidgedFbm2D(u, v, basePeriodX, basePeriodY, octaves, seed, persistence = 0.5, lacunarity = 2) {
107
+ let px = Math.max(1, Math.round(basePeriodX));
108
+ let py = Math.max(1, Math.round(basePeriodY));
109
+ const lac = Math.max(1, Math.round(lacunarity));
110
+ const count = Math.max(1, Math.round(octaves));
111
+ let sum = 0;
112
+ let amp = 1;
113
+ let norm = 0;
114
+ let s = seed >>> 0;
115
+ for (let i = 0;i < count; i++) {
116
+ const n = periodicValueNoise2D(u, v, px, py, s);
117
+ const ridge = 1 - Math.abs(2 * n - 1);
118
+ sum += ridge * ridge * amp;
119
+ norm += amp;
120
+ amp *= persistence;
121
+ px *= lac;
122
+ py *= lac;
123
+ s = Math.imul(s ^ 2146121005, 2221713035) + 461845907 >>> 0;
124
+ }
125
+ return norm > 0 ? sum / norm : 0.5;
126
+ }
127
+ function periodicDomainWarpFbm2D(u, v, periodX, periodY, octaves, warpAmp, seed) {
128
+ const wu = periodicFbm2D(u, v, periodX, periodY, Math.max(2, octaves - 1), seed + 101 >>> 0);
129
+ const wv = periodicFbm2D(u, v, periodX, periodY, Math.max(2, octaves - 1), seed + 313 >>> 0);
130
+ const du = (wu - 0.5) * warpAmp;
131
+ const dv = (wv - 0.5) * warpAmp;
132
+ return periodicFbm2D(u + du, v + dv, periodX, periodY, octaves, seed + 719 >>> 0);
133
+ }
134
+ function periodicVoronoi2D(u, v, cellsX, cellsY, seed, jitter = 0.85) {
135
+ const cx = Math.max(1, Math.round(cellsX));
136
+ const cy = Math.max(1, Math.round(cellsY));
137
+ const j = clamp(jitter, 0, 1);
138
+ const px = fract(u) * cx;
139
+ const py = fract(v) * cy;
140
+ const ix = Math.floor(px);
141
+ const iy = Math.floor(py);
142
+ const fx = px - ix;
143
+ const fy = py - iy;
144
+ let f1Sq = 1e9;
145
+ let f2Sq = 1e9;
146
+ let bestX = 0;
147
+ let bestY = 0;
148
+ const seedY = (seed ^ 2654435769) >>> 0;
149
+ for (let oy = -1;oy <= 1; oy++) {
150
+ const ny = wrapInt(iy + oy, cy);
151
+ for (let ox = -1;ox <= 1; ox++) {
152
+ const nx = wrapInt(ix + ox, cx);
153
+ const rx = 0.5 + (hash2D(nx, ny, seed) - 0.5) * j;
154
+ const ry = 0.5 + (hash2D(nx + 1013, ny - 1619, seedY) - 0.5) * j;
155
+ const dx = ox + rx - fx;
156
+ const dy = oy + ry - fy;
157
+ const dSq = dx * dx + dy * dy;
158
+ if (dSq < f1Sq) {
159
+ f2Sq = f1Sq;
160
+ f1Sq = dSq;
161
+ bestX = nx;
162
+ bestY = ny;
163
+ } else if (dSq < f2Sq) {
164
+ f2Sq = dSq;
165
+ }
166
+ }
167
+ }
168
+ const f1 = Math.sqrt(f1Sq);
169
+ const f2 = Math.sqrt(f2Sq);
170
+ const cellId = hash2D(bestX, bestY, (seed ^ 1542469173) >>> 0);
171
+ return {
172
+ f1,
173
+ f2,
174
+ edgeDist: Math.max(0, f2 - f1),
175
+ cellId,
176
+ cellX: bestX,
177
+ cellY: bestY
178
+ };
179
+ }
180
+ function hexToRgb(hex) {
181
+ const clean = hex.trim().replace(/^#/, "");
182
+ if (/^[0-9a-fA-F]{3,4}$/.test(clean)) {
183
+ return [
184
+ Number.parseInt(clean[0] + clean[0], 16),
185
+ Number.parseInt(clean[1] + clean[1], 16),
186
+ Number.parseInt(clean[2] + clean[2], 16)
187
+ ];
188
+ }
189
+ if (/^[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/.test(clean)) {
190
+ return [
191
+ Number.parseInt(clean.slice(0, 2), 16),
192
+ Number.parseInt(clean.slice(2, 4), 16),
193
+ Number.parseInt(clean.slice(4, 6), 16)
194
+ ];
195
+ }
196
+ return [128, 128, 128];
197
+ }
198
+ function rgbToHsv(r, g, b) {
199
+ const rn = clamp(r / 255, 0, 1);
200
+ const gn = clamp(g / 255, 0, 1);
201
+ const bn = clamp(b / 255, 0, 1);
202
+ const max = Math.max(rn, gn, bn);
203
+ const min = Math.min(rn, gn, bn);
204
+ const d = max - min;
205
+ let h = 0;
206
+ if (d > 0.000001) {
207
+ if (max === rn) {
208
+ h = 60 * ((gn - bn) / d % 6);
209
+ } else if (max === gn) {
210
+ h = 60 * ((bn - rn) / d + 2);
211
+ } else {
212
+ h = 60 * ((rn - gn) / d + 4);
213
+ }
214
+ }
215
+ if (h < 0)
216
+ h += 360;
217
+ const s = max > 0.000001 ? d / max : 0;
218
+ return [h, s, max];
219
+ }
220
+ function hsvToRgb(h, s, v) {
221
+ const hh = (h % 360 + 360) % 360;
222
+ const ss = clamp(s, 0, 1);
223
+ const vv = clamp(v, 0, 1);
224
+ const c = vv * ss;
225
+ const x = c * (1 - Math.abs(hh / 60 % 2 - 1));
226
+ const m = vv - c;
227
+ let r1 = 0;
228
+ let g1 = 0;
229
+ let b1 = 0;
230
+ if (hh < 60) {
231
+ r1 = c;
232
+ g1 = x;
233
+ } else if (hh < 120) {
234
+ r1 = x;
235
+ g1 = c;
236
+ } else if (hh < 180) {
237
+ g1 = c;
238
+ b1 = x;
239
+ } else if (hh < 240) {
240
+ g1 = x;
241
+ b1 = c;
242
+ } else if (hh < 300) {
243
+ r1 = x;
244
+ b1 = c;
245
+ } else {
246
+ r1 = c;
247
+ b1 = x;
248
+ }
249
+ return [clampByte((r1 + m) * 255), clampByte((g1 + m) * 255), clampByte((b1 + m) * 255)];
250
+ }
251
+ function mixRgb(a, b, t) {
252
+ const k = clamp(t, 0, 1);
253
+ return [
254
+ clampByte(a[0] + (b[0] - a[0]) * k),
255
+ clampByte(a[1] + (b[1] - a[1]) * k),
256
+ clampByte(a[2] + (b[2] - a[2]) * k)
257
+ ];
258
+ }
259
+ function scaleRgb(rgb, factor) {
260
+ return [clampByte(rgb[0] * factor), clampByte(rgb[1] * factor), clampByte(rgb[2] * factor)];
261
+ }
262
+ function modulateRgb(rgb, mod) {
263
+ const [h, s, v] = rgbToHsv(rgb[0], rgb[1], rgb[2]);
264
+ const nextH = h + (mod.hueShift ?? 0);
265
+ const nextS = clamp(s * (mod.satScale ?? 1) + (mod.satOffset ?? 0), 0, 1);
266
+ const nextV = clamp(v * (mod.valScale ?? 1) + (mod.valOffset ?? 0), 0, 1);
267
+ return hsvToRgb(nextH, nextS, nextV);
268
+ }
269
+ // src/materials/patterns.ts
270
+ var TWO_PI = Math.PI * 2;
271
+ var PATTERN_CONFIG = {
272
+ "concrete-plate": { normalStrength: 2.8, aoCavityStrength: 1.8, globalHeightAo: 0.08 },
273
+ "concrete-polished": { normalStrength: 1.1, aoCavityStrength: 0.9, globalHeightAo: 0.04 },
274
+ "concrete-raw": { normalStrength: 3.6, aoCavityStrength: 2.2, globalHeightAo: 0.1 },
275
+ plaster: { normalStrength: 1.8, aoCavityStrength: 1.2, globalHeightAo: 0.06 },
276
+ drywall: { normalStrength: 1, aoCavityStrength: 0.9, globalHeightAo: 0.04 },
277
+ stucco: { normalStrength: 4.2, aoCavityStrength: 2.4, globalHeightAo: 0.1 },
278
+ cotton: { normalStrength: 2.4, aoCavityStrength: 1.8, globalHeightAo: 0.07 },
279
+ linen: { normalStrength: 2.9, aoCavityStrength: 2, globalHeightAo: 0.08 },
280
+ velvet: { normalStrength: 1.6, aoCavityStrength: 1.2, globalHeightAo: 0.06 },
281
+ suede: { normalStrength: 2, aoCavityStrength: 1.4, globalHeightAo: 0.06 },
282
+ wool: { normalStrength: 3.4, aoCavityStrength: 2.2, globalHeightAo: 0.09 },
283
+ boucle: { normalStrength: 4.4, aoCavityStrength: 2.8, globalHeightAo: 0.12 },
284
+ leather: { normalStrength: 3, aoCavityStrength: 2.2, globalHeightAo: 0.09 },
285
+ copper: { normalStrength: 1.7, aoCavityStrength: 1.1, globalHeightAo: 0.04 },
286
+ "polished-metal": { normalStrength: 0.9, aoCavityStrength: 0.7, globalHeightAo: 0.02 },
287
+ "brushed-metal": { normalStrength: 1.5, aoCavityStrength: 1, globalHeightAo: 0.03 },
288
+ "roof-shingles": { normalStrength: 4.5, aoCavityStrength: 2.5, globalHeightAo: 0.12 },
289
+ "roof-tiles": { normalStrength: 5, aoCavityStrength: 2.6, globalHeightAo: 0.14 },
290
+ "board-batten": { normalStrength: 4, aoCavityStrength: 2.2, globalHeightAo: 0.1 },
291
+ "lap-siding": { normalStrength: 4.2, aoCavityStrength: 2.4, globalHeightAo: 0.11 },
292
+ "wood-fine": { normalStrength: 1.9, aoCavityStrength: 1.4, globalHeightAo: 0.05 },
293
+ "wood-plank": { normalStrength: 3, aoCavityStrength: 2, globalHeightAo: 0.08 },
294
+ "wood-parquet": { normalStrength: 3.1, aoCavityStrength: 2.1, globalHeightAo: 0.08 },
295
+ "wood-chevron": { normalStrength: 3.1, aoCavityStrength: 2.1, globalHeightAo: 0.08 },
296
+ "wood-square-parquet": { normalStrength: 3.2, aoCavityStrength: 2.2, globalHeightAo: 0.08 },
297
+ brick: { normalStrength: 4.8, aoCavityStrength: 2.6, globalHeightAo: 0.12 },
298
+ "stone-wall": { normalStrength: 5.2, aoCavityStrength: 2.8, globalHeightAo: 0.14 },
299
+ terrazzo: { normalStrength: 1.5, aoCavityStrength: 1.1, globalHeightAo: 0.05 },
300
+ "marble-stone": { normalStrength: 1.4, aoCavityStrength: 1, globalHeightAo: 0.04 },
301
+ "mosaic-tile": { normalStrength: 3.6, aoCavityStrength: 2.2, globalHeightAo: 0.09 },
302
+ "pool-tile": { normalStrength: 3.4, aoCavityStrength: 2, globalHeightAo: 0.08 },
303
+ "checker-tile": { normalStrength: 2.8, aoCavityStrength: 1.8, globalHeightAo: 0.07 },
304
+ "garage-panel": { normalStrength: 3.6, aoCavityStrength: 2, globalHeightAo: 0.09 },
305
+ earth: { normalStrength: 4.6, aoCavityStrength: 2.6, globalHeightAo: 0.12 },
306
+ "terracotta-tile": { normalStrength: 3.8, aoCavityStrength: 2.4, globalHeightAo: 0.1 },
307
+ "ceramic-tile": { normalStrength: 2.8, aoCavityStrength: 1.9, globalHeightAo: 0.07 }
308
+ };
309
+ function setRgb(out, r, g, b) {
310
+ out.r = r;
311
+ out.g = g;
312
+ out.b = b;
313
+ }
314
+ function applyRgbTint(out, base, mul, add = 0) {
315
+ out.r = base[0] * mul + add;
316
+ out.g = base[1] * mul + add;
317
+ out.b = base[2] * mul + add;
318
+ }
319
+ function blendOutWithRgb(out, rgb, t) {
320
+ const k = clamp(t, 0, 1);
321
+ out.r = out.r + (rgb[0] - out.r) * k;
322
+ out.g = out.g + (rgb[1] - out.g) * k;
323
+ out.b = out.b + (rgb[2] - out.b) * k;
324
+ }
325
+ function evalWoodGrain(u, v, seed, alongPeriod, acrossPeriod, ringCount) {
326
+ const warp = (periodicFbm2D(u, v, alongPeriod, acrossPeriod, 3, seed, 0.55) - 0.5) * 0.9;
327
+ const fineWarp = (periodicValueNoise2D(u, v, alongPeriod * 2, acrossPeriod * 4, seed + 71 >>> 0) - 0.5) * 0.22;
328
+ const wave = 0.5 + 0.5 * Math.sin(TWO_PI * (v * ringCount + warp + fineWarp));
329
+ const ring = Math.pow(wave, 0.65) * 0.72 + Math.pow(wave, 3.2) * 0.28;
330
+ const pore = periodicFbm2D(u, v, alongPeriod * 2, acrossPeriod * 10, 2, seed + 193 >>> 0, 0.5);
331
+ const ray = periodicValueNoise2D(u, v, alongPeriod * 6, acrossPeriod * 2, seed + 311 >>> 0);
332
+ return { ring, pore, ray };
333
+ }
334
+ function shadeWoodBlock(out, ctx, along, across, plankSeed, bevelMask, grainRepeatAcross) {
335
+ const toneShift = (plankSeed - 0.5) * 0.26;
336
+ const warmShift = (hash2D(Math.floor(plankSeed * 1000), 17, ctx.seed) - 0.5) * 0.35;
337
+ const uG = fract(along * 0.85 + plankSeed * 3.7);
338
+ const vG = fract(across * 0.75 + plankSeed * 5.3);
339
+ const g = evalWoodGrain(uG, vG, (ctx.seed ^ Math.floor(plankSeed * 65535)) >>> 0, 2, 4, grainRepeatAcross);
340
+ const grainDark = lerp(0.82, 1.14, g.ring) * lerp(0.92, 1.04, g.pore);
341
+ const plankMul = (1 + toneShift) * grainDark;
342
+ const baseTone = warmShift >= 0 ? mixRgb(ctx.baseRgb, ctx.warmTint, warmShift) : mixRgb(ctx.baseRgb, ctx.coolTint, -warmShift);
343
+ const lift = (g.ring - 0.5) * 14 + (g.ray - 0.5) * 6;
344
+ applyRgbTint(out, baseTone, plankMul, lift);
345
+ const seamDark = lerp(0.58, 1, bevelMask);
346
+ out.r *= seamDark;
347
+ out.g *= seamDark;
348
+ out.b *= seamDark;
349
+ const poreDip = g.pore < 0.28 ? (0.28 - g.pore) * 0.45 : 0;
350
+ const surfH = 0.62 + (g.ring - 0.5) * 0.12 - poreDip + (g.ray - 0.5) * 0.04;
351
+ out.height = lerp(0.14, clamp(surfH, 0.35, 0.92), bevelMask);
352
+ out.roughness = clamp(lerp(0.84, 0.38 + (1 - g.ring) * 0.14 + poreDip * 0.3, bevelMask), 0.24, 0.92);
353
+ out.ao = clamp(lerp(0.36, 0.96 - poreDip * 0.25, bevelMask), 0.25, 1);
354
+ out.metallic = 0;
355
+ }
356
+ function evalConcretePlate(u, v, ctx, out) {
357
+ const cols = 2;
358
+ const rows = 2;
359
+ const gx = fract(u * cols);
360
+ const gy = fract(v * rows);
361
+ const px = Math.floor(fract(u) * cols);
362
+ const py = Math.floor(fract(v) * rows);
363
+ const panelVar = (hash2D(px, py, ctx.seed) - 0.5) * 0.1;
364
+ const seamDist = Math.min(gx, 1 - gx, gy, 1 - gy);
365
+ const seamMask = smoothstep(0.008, 0.024, seamDist);
366
+ const tx = Math.min(Math.abs(gx - 0.22), Math.abs(gx - 0.78));
367
+ const ty = Math.min(Math.abs(gy - 0.25), Math.abs(gy - 0.75));
368
+ const tieDist = Math.hypot(tx, ty);
369
+ const tieOuter = smoothstep(0.022, 0.042, tieDist);
370
+ const tieCore = smoothstep(0.011, 0.022, tieDist);
371
+ const trowel = periodicFbm2D(u, v, 4, 4, 4, ctx.seed, 0.55);
372
+ const grain = periodicFbm2D(u, v, 16, 16, 3, ctx.seed + 37 >>> 0, 0.6);
373
+ const pinhole = periodicValueNoise2D(u, v, 36, 36, ctx.seed + 91 >>> 0);
374
+ const pit = pinhole < 0.08 ? (0.08 - pinhole) * 3.2 : 0;
375
+ const lum = (1 + panelVar + (trowel - 0.5) * 0.16 + (grain - 0.5) * 0.1 - pit * 0.22) * lerp(0.68, 1, seamMask) * lerp(0.55, 1, tieCore);
376
+ applyRgbTint(out, ctx.baseRgb, lum);
377
+ const hPanel = 0.68 + (trowel - 0.5) * 0.14 + (grain - 0.5) * 0.08 - pit * 0.25;
378
+ const hSeam = lerp(0.24, hPanel, seamMask);
379
+ out.height = clamp(lerp(0.18, hSeam, tieOuter * 0.6 + tieCore * 0.4), 0.08, 0.95);
380
+ out.roughness = clamp(lerp(0.9, 0.72 + (grain - 0.5) * 0.16 + pit * 0.2, seamMask * tieOuter), 0.55, 0.96);
381
+ out.ao = clamp(lerp(0.45, 0.96 - pit * 0.3, seamMask * tieOuter) * lerp(0.5, 1, tieCore), 0.25, 1);
382
+ out.metallic = 0;
383
+ }
384
+ function evalConcretePolished(u, v, ctx, out) {
385
+ const cloud = periodicDomainWarpFbm2D(u, v, 3, 3, 4, 0.18, ctx.seed);
386
+ const sheen = periodicFbm2D(u, v, 6, 6, 3, ctx.seed + 53 >>> 0, 0.5);
387
+ const microAgg = periodicVoronoi2D(u, v, 24, 24, ctx.seed + 131 >>> 0, 0.9);
388
+ const fleck = microAgg.f1 < 0.24 && microAgg.cellId > 0.72 ? (microAgg.cellId - 0.72) * 1.4 : 0;
389
+ const mul = 0.88 + cloud * 0.22 + (sheen - 0.5) * 0.08;
390
+ applyRgbTint(out, ctx.baseRgb, mul, fleck * 28 - (1 - cloud) * 6);
391
+ out.height = clamp(0.5 + (cloud - 0.5) * 0.08 + (sheen - 0.5) * 0.05 + fleck * 0.04, 0.35, 0.7);
392
+ out.roughness = clamp(0.24 + (1 - sheen) * 0.18 + (1 - cloud) * 0.08 - fleck * 0.06, 0.14, 0.52);
393
+ out.ao = clamp(0.92 + cloud * 0.08, 0.84, 1);
394
+ out.metallic = 0;
395
+ }
396
+ function evalConcreteRaw(u, v, ctx, out) {
397
+ const mottle = periodicDomainWarpFbm2D(u, v, 4, 4, 4, 0.14, ctx.seed);
398
+ const coarse = periodicFbm2D(u, v, 10, 10, 4, ctx.seed + 79 >>> 0, 0.62);
399
+ const pores = periodicVoronoi2D(u, v, 18, 18, ctx.seed + 211 >>> 0, 0.95);
400
+ const streak = periodicValueNoise2D(u, v, 3, 18, ctx.seed + 307 >>> 0);
401
+ const pitMask = pores.cellId > 0.68 ? smoothstep(0.32, 0.12, pores.f1) : 0;
402
+ const mul = 0.8 + mottle * 0.26 + (coarse - 0.5) * 0.18 + (streak - 0.5) * 0.08 - pitMask * 0.28;
403
+ applyRgbTint(out, ctx.baseRgb, mul);
404
+ out.height = clamp(0.56 + (mottle - 0.5) * 0.22 + (coarse - 0.5) * 0.22 - pitMask * 0.38, 0.08, 0.95);
405
+ out.roughness = clamp(0.8 + (coarse - 0.5) * 0.16 + pitMask * 0.15, 0.68, 0.98);
406
+ out.ao = clamp(0.94 - pitMask * 0.42 - (1 - mottle) * 0.12, 0.35, 1);
407
+ out.metallic = 0;
408
+ }
409
+ function evalPlaster(u, v, ctx, out) {
410
+ const arcWarp = periodicDomainWarpFbm2D(u, v, 3, 3, 4, 0.26, ctx.seed);
411
+ const trowelRidge = periodicRidgedFbm2D(u + arcWarp * 0.08, v - arcWarp * 0.06, 4, 3, 3, ctx.seed + 83 >>> 0, 0.5);
412
+ const fine = periodicFbm2D(u, v, 14, 14, 3, ctx.seed + 197 >>> 0, 0.55);
413
+ const mul = 0.92 + (arcWarp - 0.5) * 0.14 + (trowelRidge - 0.5) * 0.09 + (fine - 0.5) * 0.05;
414
+ applyRgbTint(out, ctx.baseRgb, mul);
415
+ out.height = clamp(0.52 + (arcWarp - 0.5) * 0.18 + (trowelRidge - 0.5) * 0.16 + (fine - 0.5) * 0.06, 0.2, 0.88);
416
+ out.roughness = clamp(0.6 + (1 - arcWarp) * 0.16 + (fine - 0.5) * 0.1, 0.46, 0.82);
417
+ out.ao = clamp(0.93 + (trowelRidge - 0.5) * 0.1, 0.8, 1);
418
+ out.metallic = 0;
419
+ }
420
+ function evalDrywall(u, v, ctx, out) {
421
+ const roller = periodicFbm2D(u, v, 6, 6, 3, ctx.seed, 0.5);
422
+ const orangePeel = periodicFbm2D(u, v, 24, 24, 3, ctx.seed + 149 >>> 0, 0.6);
423
+ const paperFiber = periodicValueNoise2D(u, v, 40, 20, ctx.seed + 257 >>> 0);
424
+ const mul = 0.95 + (roller - 0.5) * 0.08 + (orangePeel - 0.5) * 0.07 + (paperFiber - 0.5) * 0.03;
425
+ applyRgbTint(out, ctx.baseRgb, mul);
426
+ out.height = clamp(0.5 + (roller - 0.5) * 0.06 + (orangePeel - 0.5) * 0.1 + (paperFiber - 0.5) * 0.04, 0.35, 0.68);
427
+ out.roughness = clamp(0.76 + (orangePeel - 0.5) * 0.14 + (roller - 0.5) * 0.06, 0.66, 0.92);
428
+ out.ao = clamp(0.95 + (orangePeel - 0.5) * 0.08, 0.86, 1);
429
+ out.metallic = 0;
430
+ }
431
+ function evalStucco(u, v, ctx, out) {
432
+ const undul = periodicFbm2D(u, v, 5, 5, 3, ctx.seed, 0.55);
433
+ const stipple = periodicVoronoi2D(u, v, 22, 22, ctx.seed + 67 >>> 0, 0.95);
434
+ const grit = periodicFbm2D(u, v, 28, 28, 3, ctx.seed + 173 >>> 0, 0.65);
435
+ const bump = clamp(1 - stipple.f1 * 1.35, 0, 1);
436
+ const mul = 0.84 + undul * 0.18 + bump * 0.14 + (grit - 0.5) * 0.12;
437
+ applyRgbTint(out, ctx.baseRgb, mul);
438
+ out.height = clamp(0.25 + undul * 0.25 + bump * 0.34 + grit * 0.16, 0.08, 0.96);
439
+ out.roughness = clamp(0.84 + (1 - bump) * 0.12 + (grit - 0.5) * 0.08, 0.74, 0.98);
440
+ out.ao = clamp(0.72 + bump * 0.22 + undul * 0.08, 0.48, 1);
441
+ out.metallic = 0;
442
+ }
443
+ function evalCotton(u, v, ctx, out) {
444
+ const threads = 20;
445
+ const gx = fract(u) * threads;
446
+ const gy = fract(v) * threads;
447
+ const ix = Math.floor(gx);
448
+ const iy = Math.floor(gy);
449
+ const fx = gx - ix;
450
+ const fy = gy - iy;
451
+ const warpProfile = Math.sin(Math.PI * fx);
452
+ const weftProfile = Math.sin(Math.PI * fy);
453
+ const warpOver = (ix + iy & 1) === 0;
454
+ const warpH = warpProfile * (warpOver ? 0.95 : 0.42);
455
+ const weftH = weftProfile * (warpOver ? 0.42 : 0.95);
456
+ const threadH = Math.max(warpH, weftH);
457
+ const fold = periodicFbm2D(u, v, 3, 3, 3, ctx.seed, 0.5);
458
+ const yarnNoise = periodicFbm2D(u, v, 24, 24, 2, ctx.seed + 41 >>> 0, 0.55);
459
+ const mul = 0.8 + threadH * 0.24 + (fold - 0.5) * 0.14 + (yarnNoise - 0.5) * 0.08;
460
+ applyRgbTint(out, ctx.baseRgb, mul, (threadH - 0.5) * 8);
461
+ out.height = clamp(0.18 + threadH * 0.64 + (fold - 0.5) * 0.12 + (yarnNoise - 0.5) * 0.06, 0.08, 0.96);
462
+ out.roughness = clamp(0.82 + (1 - threadH) * 0.12 + (yarnNoise - 0.5) * 0.06, 0.74, 0.96);
463
+ out.ao = clamp(0.62 + threadH * 0.36, 0.42, 1);
464
+ out.metallic = 0;
465
+ }
466
+ function evalLinen(u, v, ctx, out) {
467
+ const threads = 16;
468
+ const gx = fract(u) * threads;
469
+ const gy = fract(v) * threads;
470
+ const ix = Math.floor(gx);
471
+ const iy = Math.floor(gy);
472
+ const fx = gx - ix;
473
+ const fy = gy - iy;
474
+ const warpSlub = 0.78 + 0.44 * periodicValueNoise2D(u, v, threads, 4, ctx.seed + 11 >>> 0);
475
+ const weftSlub = 0.78 + 0.44 * periodicValueNoise2D(u, v, 4, threads, ctx.seed + 29 >>> 0);
476
+ const warpOver = (ix + iy & 1) === 0;
477
+ const warpProf = Math.pow(Math.sin(Math.PI * fx), 0.75) * warpSlub;
478
+ const weftProf = Math.pow(Math.sin(Math.PI * fy), 0.75) * weftSlub;
479
+ const hWarp = warpProf * (warpOver ? 0.82 : 0.36);
480
+ const hWeft = weftProf * (warpOver ? 0.36 : 0.82);
481
+ const weaveH = clamp(Math.max(hWarp, hWeft), 0, 1);
482
+ const dyeVar = periodicFbm2D(u, v, 4, 4, 3, ctx.seed + 97 >>> 0, 0.5);
483
+ const threadTone = (hash2D(warpOver ? ix : iy, warpOver ? 0 : 1, ctx.seed) - 0.5) * 0.12;
484
+ const mul = 0.78 + weaveH * 0.28 + threadTone + (dyeVar - 0.5) * 0.12;
485
+ applyRgbTint(out, ctx.baseRgb, mul, (weaveH - 0.5) * 10);
486
+ out.height = clamp(0.15 + weaveH * 0.72 + (dyeVar - 0.5) * 0.08, 0.06, 0.96);
487
+ out.roughness = clamp(0.8 + (1 - weaveH) * 0.14 + (dyeVar - 0.5) * 0.06, 0.72, 0.96);
488
+ out.ao = clamp(0.56 + weaveH * 0.42, 0.38, 1);
489
+ out.metallic = 0;
490
+ }
491
+ function evalVelvet(u, v, ctx, out) {
492
+ const softFold = periodicFbm2D(u, v, 2, 2, 3, ctx.seed, 0.45);
493
+ const finePile = periodicFbm2D(u, v, 16, 44, 3, ctx.seed + 61 >>> 0, 0.58);
494
+ const microNap = periodicValueNoise2D(u, v, 56, 56, ctx.seed + 137 >>> 0);
495
+ const sheen = clamp(0.45 + (softFold - 0.5) * 0.36 + (finePile - 0.5) * 0.24, 0, 1);
496
+ const mul = 0.88 + sheen * 0.2 + (microNap - 0.5) * 0.05;
497
+ applyRgbTint(out, ctx.baseRgb, mul, (sheen - 0.5) * 10);
498
+ out.height = clamp(0.46 + (softFold - 0.5) * 0.14 + (finePile - 0.5) * 0.22 + (microNap - 0.5) * 0.12, 0.2, 0.86);
499
+ out.roughness = clamp(0.56 + (1 - sheen) * 0.22 + (microNap - 0.5) * 0.08, 0.44, 0.84);
500
+ out.ao = clamp(0.86 + sheen * 0.12, 0.74, 1);
501
+ out.metallic = 0;
502
+ }
503
+ function evalSuede(u, v, ctx, out) {
504
+ const brushPatch = periodicDomainWarpFbm2D(u, v, 4, 4, 3, 0.22, ctx.seed);
505
+ const microNap = periodicFbm2D(u, v, 18, 30, 3, ctx.seed + 89 >>> 0, 0.62);
506
+ const grain = periodicValueNoise2D(u, v, 36, 36, ctx.seed + 181 >>> 0);
507
+ const mul = 0.82 + brushPatch * 0.24 + (microNap - 0.5) * 0.14 + (grain - 0.5) * 0.06;
508
+ applyRgbTint(out, ctx.baseRgb, mul, (brushPatch - 0.5) * 10);
509
+ out.height = clamp(0.4 + brushPatch * 0.22 + (microNap - 0.5) * 0.22 + (grain - 0.5) * 0.1, 0.16, 0.88);
510
+ out.roughness = clamp(0.78 + (1 - brushPatch) * 0.14 + (grain - 0.5) * 0.06, 0.68, 0.96);
511
+ out.ao = clamp(0.82 + brushPatch * 0.14 + (microNap - 0.5) * 0.06, 0.68, 1);
512
+ out.metallic = 0;
513
+ }
514
+ function evalWool(u, v, ctx, out) {
515
+ const cols = 12;
516
+ const rows = 16;
517
+ const gx = fract(u) * cols;
518
+ const gy = fract(v) * rows;
519
+ const fx = gx - Math.floor(gx);
520
+ const fy = gy - Math.floor(gy);
521
+ const halfX = Math.abs(fx - 0.5) * 2;
522
+ const vPhase = fract(fy + halfX * 0.55);
523
+ const loopRidge = Math.sin(Math.PI * vPhase);
524
+ const colSplit = smoothstep(0, 0.22, Math.min(fx, 1 - fx)) * smoothstep(0, 0.14, Math.abs(fx - 0.5));
525
+ const knitH = loopRidge * (0.45 + 0.55 * colSplit);
526
+ const heather = periodicFbm2D(u, v, 8, 8, 3, ctx.seed, 0.58);
527
+ const fuzz = periodicFbm2D(u, v, 28, 28, 2, ctx.seed + 157 >>> 0, 0.65);
528
+ const mul = 0.78 + knitH * 0.26 + (heather - 0.5) * 0.14 + (fuzz - 0.5) * 0.1;
529
+ applyRgbTint(out, ctx.baseRgb, mul);
530
+ out.height = clamp(0.16 + knitH * 0.64 + (fuzz - 0.5) * 0.14 + (heather - 0.5) * 0.08, 0.06, 0.96);
531
+ out.roughness = clamp(0.86 + (1 - knitH) * 0.1 + (fuzz - 0.5) * 0.06, 0.78, 0.98);
532
+ out.ao = clamp(0.58 + knitH * 0.38, 0.38, 1);
533
+ out.metallic = 0;
534
+ }
535
+ function evalBoucle(u, v, ctx, out) {
536
+ const loopsA = periodicVoronoi2D(u, v, 24, 24, ctx.seed, 0.92);
537
+ const loopsB = periodicVoronoi2D(u + 0.029, v + 0.043, 36, 36, ctx.seed + 199 >>> 0, 0.94);
538
+ const loopsC = periodicVoronoi2D(u + 0.061, v + 0.017, 48, 48, ctx.seed + 401 >>> 0, 0.95);
539
+ const nubA = smoothstep(0.76, 0.14, loopsA.f1);
540
+ const nubB = smoothstep(0.74, 0.16, loopsB.f1);
541
+ const nubC = smoothstep(0.72, 0.18, loopsC.f1);
542
+ const nub = Math.max(nubA * 0.95, nubB * 0.86, nubC * 0.76);
543
+ const yarnTone = (loopsA.cellId - 0.5) * 0.12 + (loopsB.cellId - 0.5) * 0.08;
544
+ const weaveBase = periodicFbm2D(u, v, 28, 28, 3, ctx.seed + 317 >>> 0, 0.6);
545
+ const mul = 0.84 + nub * 0.24 + yarnTone + (weaveBase - 0.5) * 0.08;
546
+ applyRgbTint(out, ctx.baseRgb, mul, nub * 7);
547
+ out.height = clamp(0.24 + nub * 0.66 + (weaveBase - 0.5) * 0.1, 0.12, 0.96);
548
+ out.roughness = clamp(0.84 + (1 - nub) * 0.12 + (weaveBase - 0.5) * 0.05, 0.78, 0.98);
549
+ out.ao = clamp(0.68 + nub * 0.3, 0.52, 1);
550
+ out.metallic = 0;
551
+ }
552
+ function evalLeather(u, v, ctx, out) {
553
+ const hideSupple = periodicDomainWarpFbm2D(u, v, 4, 4, 3, 0.14, ctx.seed + 19 >>> 0);
554
+ const uw = u + (hideSupple - 0.5) * 0.014;
555
+ const vw = v + (periodicFbm2D(u, v, 6, 6, 2, ctx.seed + 43 >>> 0, 0.5) - 0.5) * 0.014;
556
+ const pebble = periodicVoronoi2D(uw, vw, 44, 44, ctx.seed, 0.88);
557
+ const microGrain = periodicFbm2D(u, v, 32, 32, 2, ctx.seed + 263 >>> 0, 0.55);
558
+ const pebbleCrown = smoothstep(0.02, 0.25, pebble.edgeDist);
559
+ const dome = Math.pow(pebbleCrown, 0.6);
560
+ const grainVar = (pebble.cellId - 0.5) * 0.07;
561
+ const mul = 0.88 + dome * 0.17 + (hideSupple - 0.5) * 0.08 + grainVar;
562
+ const crownLift = dome * 10 + (microGrain - 0.5) * 4;
563
+ applyRgbTint(out, ctx.baseRgb, mul, crownLift);
564
+ out.height = clamp(0.3 + dome * 0.54 + (hideSupple - 0.5) * 0.1 + (microGrain - 0.5) * 0.06, 0.12, 0.94);
565
+ out.roughness = clamp(0.32 + (1 - pebbleCrown) * 0.24 + (1 - hideSupple) * 0.08 + grainVar * 0.25, 0.24, 0.66);
566
+ out.ao = clamp(0.78 + pebbleCrown * 0.2 + (hideSupple - 0.5) * 0.04, 0.62, 1);
567
+ out.metallic = 0;
568
+ }
569
+ function evalCopper(u, v, ctx, out) {
570
+ const hairline = periodicFbm2D(u, v, 2, 96, 3, ctx.seed, 0.62);
571
+ const microBrush = periodicValueNoise2D(u, v, 4, 144, ctx.seed + 73 >>> 0);
572
+ const rollSheen = periodicFbm2D(u, v, 3, 4, 2, ctx.seed + 191 >>> 0, 0.45);
573
+ const brush = hairline * 0.6 + microBrush * 0.4;
574
+ const mul = 0.94 + (brush - 0.5) * 0.1 + (rollSheen - 0.5) * 0.05;
575
+ applyRgbTint(out, ctx.baseRgb, mul, (brush - 0.5) * 6);
576
+ out.height = clamp(0.48 + (brush - 0.5) * 0.24 + (rollSheen - 0.5) * 0.04, 0.24, 0.78);
577
+ out.roughness = clamp(0.22 + (1 - brush) * 0.1 + (rollSheen - 0.5) * 0.03, 0.16, 0.34);
578
+ out.ao = clamp(0.94 + (brush - 0.5) * 0.06, 0.86, 1);
579
+ out.metallic = clamp(0.96 + (brush - 0.5) * 0.03, 0.92, 0.99);
580
+ }
581
+ function evalPolishedMetal(u, v, ctx, out) {
582
+ const roll = periodicFbm2D(u, v, 3, 6, 3, ctx.seed, 0.5);
583
+ const microPolish = periodicFbm2D(u, v, 4, 28, 2, ctx.seed + 109 >>> 0, 0.5);
584
+ const haze = periodicValueNoise2D(u, v, 10, 10, ctx.seed + 227 >>> 0);
585
+ const mul = 0.91 + roll * 0.08 + (microPolish - 0.5) * 0.05;
586
+ applyRgbTint(out, ctx.baseRgb, mul);
587
+ out.height = clamp(0.5 + (roll - 0.5) * 0.09 + (microPolish - 0.5) * 0.06, 0.36, 0.66);
588
+ out.roughness = clamp(0.09 + (1 - microPolish) * 0.06 + haze * 0.04, 0.06, 0.22);
589
+ out.ao = clamp(0.95 + (roll - 0.5) * 0.06, 0.88, 1);
590
+ out.metallic = clamp(0.94 + (microPolish - 0.5) * 0.04, 0.9, 0.98);
591
+ }
592
+ function evalBrushedMetal(u, v, ctx, out) {
593
+ const fineStreak = periodicFbm2D(u, v, 2, 48, 3, ctx.seed, 0.62);
594
+ const medStreak = periodicFbm2D(u, v, 1, 20, 2, ctx.seed + 89 >>> 0, 0.55);
595
+ const band = periodicValueNoise2D(u, v, 2, 8, ctx.seed + 233 >>> 0);
596
+ const streak = fineStreak * 0.68 + medStreak * 0.32;
597
+ const mul = 0.84 + streak * 0.28 + (band - 0.5) * 0.08;
598
+ applyRgbTint(out, ctx.baseRgb, mul, (streak - 0.5) * 14);
599
+ out.height = clamp(0.4 + streak * 0.34 + (band - 0.5) * 0.08, 0.16, 0.86);
600
+ out.roughness = clamp(0.28 + (1 - streak) * 0.14 + (band - 0.5) * 0.04, 0.2, 0.46);
601
+ out.ao = clamp(0.9 + (streak - 0.5) * 0.12, 0.78, 1);
602
+ out.metallic = clamp(0.89 + (streak - 0.5) * 0.06, 0.84, 0.95);
603
+ }
604
+ function evalRoofShingles(u, v, ctx, out) {
605
+ const rows = 8;
606
+ const cols = 4;
607
+ const gv = fract(v) * rows;
608
+ const row = Math.floor(gv);
609
+ const lv = gv - row;
610
+ const stagger = (row & 1) === 1 ? 0.5 : 0;
611
+ const gu = fract(u) * cols + stagger;
612
+ const col = wrapInt(Math.floor(gu), cols);
613
+ const lu = fract(gu);
614
+ const keyway = smoothstep(0.015, 0.05, Math.min(lu, 1 - lu));
615
+ const overlapShadow = smoothstep(0, 0.18, lv);
616
+ const bottomLip = smoothstep(1, 0.93, lv);
617
+ const shingleId = hash2D(col, row, ctx.seed);
618
+ const toneShift = (shingleId - 0.5) * 0.24;
619
+ const granules = periodicFbm2D(u, v, 32, 32, 3, ctx.seed + 131 >>> 0, 0.65);
620
+ const weather = periodicFbm2D(u, v, 6, 6, 3, ctx.seed + 251 >>> 0, 0.55);
621
+ const tabHeight = 0.44 + lv * 0.32 + (granules - 0.5) * 0.14 + (shingleId - 0.5) * 0.06;
622
+ const h = lerp(0.14, tabHeight, keyway) * lerp(0.65, 1, bottomLip);
623
+ const shadowMul = lerp(0.56, 1, overlapShadow) * lerp(0.45, 1, keyway);
624
+ const mul = (0.9 + toneShift + (granules - 0.5) * 0.2 + (weather - 0.5) * 0.14) * shadowMul;
625
+ applyRgbTint(out, ctx.baseRgb, mul, (granules - 0.5) * 10);
626
+ out.height = clamp(h, 0.06, 0.96);
627
+ out.roughness = clamp(0.8 + (granules - 0.5) * 0.14 + (1 - keyway) * 0.1, 0.72, 0.97);
628
+ out.ao = clamp(lerp(0.36, 0.96, overlapShadow * keyway) - (1 - granules) * 0.08, 0.24, 1);
629
+ out.metallic = 0;
630
+ }
631
+ function evalRoofTiles(u, v, ctx, out) {
632
+ const cols = 6;
633
+ const rows = 5;
634
+ const gu = fract(u) * cols;
635
+ const gv = fract(v) * rows;
636
+ const col = Math.floor(gu);
637
+ const row = Math.floor(gv);
638
+ const lu = gu - col;
639
+ const lv = gv - row;
640
+ const barrel = 0.5 + 0.5 * Math.cos(TWO_PI * (lu - 0.5));
641
+ const archProfile = Math.pow(barrel, 0.75);
642
+ const courseStep = smoothstep(0, 0.14, lv);
643
+ const courseSlope = 0.55 + lv * 0.45;
644
+ const tileVar = (hash2D(col, row, ctx.seed) - 0.5) * 0.2;
645
+ const clayGrain = periodicFbm2D(u, v, 14, 14, 3, ctx.seed + 79 >>> 0, 0.58);
646
+ const lichen = periodicFbm2D(u, v, 6, 6, 3, ctx.seed + 223 >>> 0, 0.5);
647
+ const h = (0.18 + archProfile * 0.6) * courseSlope * lerp(0.62, 1, courseStep) + (clayGrain - 0.5) * 0.06;
648
+ const valleyShade = 0.78 + 0.22 * archProfile;
649
+ const overlapShade = lerp(0.68, 1, courseStep);
650
+ const mul = (0.96 + tileVar + (clayGrain - 0.5) * 0.14 + (lichen - 0.5) * 0.12) * valleyShade * overlapShade;
651
+ applyRgbTint(out, ctx.baseRgb, mul, archProfile * 10);
652
+ out.height = clamp(h, 0.08, 0.98);
653
+ out.roughness = clamp(0.64 + (1 - archProfile) * 0.14 + (clayGrain - 0.5) * 0.12, 0.52, 0.88);
654
+ out.ao = clamp(lerp(0.46, 0.98, courseStep) * (0.7 + 0.3 * archProfile), 0.32, 1);
655
+ out.metallic = 0;
656
+ }
657
+ function evalBoardBatten(u, v, ctx, out) {
658
+ const boards = 4;
659
+ const gu = fract(u) * boards;
660
+ const boardIdx = Math.floor(gu);
661
+ const lu = gu - boardIdx;
662
+ const distFromBattenCenter = Math.min(lu, 1 - lu);
663
+ const battenHalfW = 0.095;
664
+ const battenTop = smoothstep(battenHalfW + 0.025, battenHalfW - 0.015, distFromBattenCenter);
665
+ const battenShadow = smoothstep(battenHalfW + 0.005, battenHalfW + 0.075, distFromBattenCenter);
666
+ const boardVar = (hash2D(boardIdx, 0, ctx.seed) - 0.5) * 0.07;
667
+ const grain = evalWoodGrain(v, u, ctx.seed + boardIdx * 37 >>> 0, 2, 10, 6);
668
+ const paintMicro = periodicFbm2D(u, v, 16, 16, 2, ctx.seed + 193 >>> 0, 0.5);
669
+ const edgeShade = lerp(1, lerp(0.76, 1, battenShadow), 1 - battenTop);
670
+ const battenLift = battenTop * 0.05;
671
+ const mul = (0.95 + boardVar + battenLift + (grain.ring - 0.5) * 0.07 + (paintMicro - 0.5) * 0.04) * edgeShade;
672
+ applyRgbTint(out, ctx.baseRgb, mul);
673
+ const baseH = 0.34 + (grain.ring - 0.5) * 0.05 + (paintMicro - 0.5) * 0.03;
674
+ out.height = clamp(lerp(baseH, 0.84 + (grain.ring - 0.5) * 0.04, battenTop), 0.15, 0.96);
675
+ out.roughness = clamp(0.62 + (1 - grain.ring) * 0.1 + (paintMicro - 0.5) * 0.08, 0.48, 0.82);
676
+ out.ao = clamp(lerp(lerp(0.58, 0.97, battenShadow), 0.99, battenTop), 0.45, 1);
677
+ out.metallic = 0;
678
+ }
679
+ function evalLapSiding(u, v, ctx, out) {
680
+ const courses = 6;
681
+ const gv = fract(v) * courses;
682
+ const courseIdx = Math.floor(gv);
683
+ const lv = gv - courseIdx;
684
+ const dropStep = smoothstep(1, 0.91, lv);
685
+ const underShadow = smoothstep(0, 0.18, lv);
686
+ const courseVar = (hash2D(0, courseIdx, ctx.seed) - 0.5) * 0.07;
687
+ const grain = evalWoodGrain(u, v, ctx.seed + courseIdx * 53 >>> 0, 2, 12, 6);
688
+ const paintMicro = periodicFbm2D(u, v, 18, 18, 2, ctx.seed + 167 >>> 0, 0.5);
689
+ const h = (0.24 + lv * 0.62) * lerp(0.32, 1, dropStep) + (grain.ring - 0.5) * 0.04;
690
+ const mul = (0.94 + courseVar + (grain.ring - 0.5) * 0.06 + (paintMicro - 0.5) * 0.04) * lerp(0.68, 1, underShadow) * lerp(0.75, 1, dropStep);
691
+ applyRgbTint(out, ctx.baseRgb, mul);
692
+ out.height = clamp(h, 0.08, 0.96);
693
+ out.roughness = clamp(0.6 + (1 - grain.ring) * 0.1 + (paintMicro - 0.5) * 0.08, 0.46, 0.82);
694
+ out.ao = clamp(lerp(0.48, 0.98, underShadow) * lerp(0.72, 1, dropStep), 0.35, 1);
695
+ out.metallic = 0;
696
+ }
697
+ function evalWoodFine(u, v, ctx, out) {
698
+ const g = evalWoodGrain(u, v, ctx.seed, 2, 6, 10);
699
+ const macroTone = periodicFbm2D(u, v, 2, 4, 3, ctx.seed + 139 >>> 0, 0.5);
700
+ const baseTone = macroTone > 0.5 ? mixRgb(ctx.baseRgb, ctx.warmTint, (macroTone - 0.5) * 0.32) : mixRgb(ctx.baseRgb, ctx.coolTint, (0.5 - macroTone) * 0.25);
701
+ const mul = lerp(0.82, 1.15, g.ring) * lerp(0.92, 1.05, g.pore) * (0.94 + macroTone * 0.12);
702
+ applyRgbTint(out, baseTone, mul, (g.ring - 0.5) * 12 + (g.ray - 0.5) * 6);
703
+ const poreDip = g.pore < 0.3 ? (0.3 - g.pore) * 0.5 : 0;
704
+ out.height = clamp(0.56 + (g.ring - 0.5) * 0.2 - poreDip + (g.ray - 0.5) * 0.06, 0.2, 0.9);
705
+ out.roughness = clamp(0.4 + (1 - g.ring) * 0.16 + poreDip * 0.28, 0.28, 0.68);
706
+ out.ao = clamp(0.95 - poreDip * 0.32 - (1 - g.ring) * 0.06, 0.72, 1);
707
+ out.metallic = 0;
708
+ }
709
+ function evalWoodPlank(u, v, ctx, out) {
710
+ const rows = 6;
711
+ const cols = 2;
712
+ const gv = fract(v) * rows;
713
+ const row = Math.floor(gv);
714
+ const lv = gv - row;
715
+ const rowOffset = Math.floor(hash2D(row, 3, ctx.seed) * 4) * 0.25;
716
+ const gu = fract(u + rowOffset) * cols;
717
+ const col = wrapInt(Math.floor(gu), cols);
718
+ const lu = gu - Math.floor(gu);
719
+ const plankSeed = hash2D(col, row, ctx.seed + 41 >>> 0);
720
+ const sideBevel = smoothstep(0.012, 0.055, Math.min(lv, 1 - lv));
721
+ const endBevel = smoothstep(0.005, 0.022, Math.min(lu, 1 - lu));
722
+ const bevelMask = sideBevel * endBevel;
723
+ shadeWoodBlock(out, ctx, lu, lv, plankSeed, bevelMask, 5);
724
+ }
725
+ function evalWoodParquet(u, v, ctx, out) {
726
+ const L = 3;
727
+ const M = 12;
728
+ const gx = fract(u) * M;
729
+ const gy = fract(v) * M;
730
+ const ix = Math.floor(gx);
731
+ const iy = Math.floor(gy);
732
+ const fx = gx - ix;
733
+ const fy = gy - iy;
734
+ const d = wrapInt(ix - iy, 2 * L);
735
+ let along;
736
+ let across;
737
+ let plankSeed;
738
+ if (d < L) {
739
+ const anchorX = wrapInt(ix - d, M);
740
+ const anchorY = wrapInt(iy, M);
741
+ along = (d + fx) / L;
742
+ across = fy;
743
+ plankSeed = hash2D(anchorX, anchorY, ctx.seed);
744
+ } else {
745
+ const vIdx = 2 * L - 1 - d;
746
+ const anchorX = wrapInt(ix, M);
747
+ const anchorY = wrapInt(iy - vIdx, M);
748
+ along = (vIdx + fy) / L;
749
+ across = fx;
750
+ plankSeed = hash2D(anchorX, anchorY, (ctx.seed ^ 608135816) >>> 0);
751
+ }
752
+ const distAlong = Math.min(along, 1 - along) * L;
753
+ const distAcross = Math.min(across, 1 - across);
754
+ const edgeDist = Math.min(distAlong, distAcross);
755
+ const bevelMask = smoothstep(0.025, 0.09, edgeDist);
756
+ shadeWoodBlock(out, ctx, along, across, plankSeed, bevelMask, 4);
757
+ }
758
+ function evalWoodChevron(u, v, ctx, out) {
759
+ const cols = 4;
760
+ const rows = 6;
761
+ const ux = fract(u) * cols;
762
+ const col = Math.floor(ux);
763
+ const cu = ux - col;
764
+ const isEven = (col & 1) === 0;
765
+ const along = isEven ? cu : 1 - cu;
766
+ const slanted = fract(v) * rows + along * (rows / cols);
767
+ const rowIdx = wrapInt(Math.floor(slanted), rows);
768
+ const sv = fract(slanted);
769
+ const plankSeed = hash2D(col, rowIdx, ctx.seed + 107 >>> 0);
770
+ const miterDist = Math.min(cu, 1 - cu) / cols;
771
+ const sideDist = Math.min(sv, 1 - sv) / rows * 0.70710678;
772
+ const bevelMask = smoothstep(0.0012, 0.006, Math.min(miterDist, sideDist));
773
+ shadeWoodBlock(out, ctx, along, sv, plankSeed, bevelMask, 4);
774
+ }
775
+ function evalWoodSquareParquet(u, v, ctx, out) {
776
+ const grid = 4;
777
+ const fingers = 5;
778
+ const gx = fract(u) * grid;
779
+ const gy = fract(v) * grid;
780
+ const sx = Math.floor(gx);
781
+ const sy = Math.floor(gy);
782
+ const lx = gx - sx;
783
+ const ly = gy - sy;
784
+ const horizontal = (sx + sy & 1) === 0;
785
+ const along = horizontal ? lx : ly;
786
+ const acrossFull = horizontal ? ly : lx;
787
+ const fingerIdx = Math.floor(acrossFull * fingers);
788
+ const across = fract(acrossFull * fingers);
789
+ const plankSeed = hash2D(sx * fingers + (horizontal ? 0 : fingerIdx), sy * fingers + (horizontal ? fingerIdx : 0), ctx.seed);
790
+ const sqEdge = Math.min(lx, 1 - lx, ly, 1 - ly);
791
+ const fgEdge = Math.min(across, 1 - across) / fingers;
792
+ const bevelMask = smoothstep(0.008, 0.032, sqEdge) * smoothstep(0.006, 0.022, fgEdge);
793
+ shadeWoodBlock(out, ctx, along, across, plankSeed, bevelMask, 3);
794
+ }
795
+ function evalBrick(u, v, ctx, out) {
796
+ const rows = 8;
797
+ const cols = 4;
798
+ const wobbleU = (periodicFbm2D(u, v, 14, 14, 3, ctx.seed + 13 >>> 0) - 0.5) * 0.018;
799
+ const wobbleV = (periodicFbm2D(u, v, 14, 14, 3, ctx.seed + 37 >>> 0) - 0.5) * 0.013;
800
+ const gv = fract(v + wobbleV) * rows;
801
+ const row = Math.floor(gv);
802
+ const lv = gv - row;
803
+ const stagger = (row & 1) === 1 ? 0.5 : 0;
804
+ const gu = fract(u + wobbleU) * cols + stagger;
805
+ const col = wrapInt(Math.floor(gu), cols);
806
+ const lu = fract(gu);
807
+ const mortarV = smoothstep(0.036, 0.098, Math.min(lv, 1 - lv));
808
+ const mortarU = smoothstep(0.018, 0.048, Math.min(lu, 1 - lu));
809
+ const brickMask = mortarU * mortarV;
810
+ const brickId = hash2D(col, row, ctx.seed);
811
+ const claySurf = periodicFbm2D(u, v, 16, 16, 4, ctx.seed + 101 >>> 0, 0.62);
812
+ const pit = periodicVoronoi2D(u, v, 24, 24, ctx.seed + 181 >>> 0, 0.9);
813
+ const mortarGrain = periodicFbm2D(u, v, 28, 28, 3, ctx.seed + 269 >>> 0, 0.65);
814
+ const brickTone = brickId > 0.5 ? mixRgb(ctx.baseRgb, ctx.warmTint, (brickId - 0.5) * 0.45) : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - brickId) * 0.45);
815
+ const pitDip = pit.cellId > 0.72 ? smoothstep(0.28, 0.1, pit.f1) * 0.2 : 0;
816
+ const brickMul = 0.84 + claySurf * 0.28 + (brickId - 0.5) * 0.16 - pitDip;
817
+ const brR = brickTone[0] * brickMul;
818
+ const brG = brickTone[1] * brickMul;
819
+ const brB = brickTone[2] * brickMul;
820
+ const warmSandyMortar = mixRgb(ctx.groutRgb, [172, 162, 148], 0.58);
821
+ const mortarMul = 0.82 + mortarGrain * 0.24;
822
+ const mR = warmSandyMortar[0] * mortarMul;
823
+ const mG = warmSandyMortar[1] * mortarMul;
824
+ const mB = warmSandyMortar[2] * mortarMul;
825
+ setRgb(out, lerp(mR, brR, brickMask), lerp(mG, brG, brickMask), lerp(mB, brB, brickMask));
826
+ const hBrick = 0.64 + (claySurf - 0.5) * 0.22 - pitDip * 0.45 + (brickId - 0.5) * 0.08;
827
+ const hMortar = 0.18 + (mortarGrain - 0.5) * 0.1;
828
+ out.height = clamp(lerp(hMortar, hBrick, brickMask), 0.06, 0.98);
829
+ out.roughness = clamp(lerp(0.93, 0.8 + (1 - claySurf) * 0.14 + pitDip * 0.2, brickMask), 0.68, 0.98);
830
+ out.ao = clamp(lerp(0.42, 0.96 - pitDip * 0.35, brickMask), 0.28, 1);
831
+ out.metallic = 0;
832
+ }
833
+ function evalStoneWall(u, v, ctx, out) {
834
+ const warpU = (periodicFbm2D(u, v, 6, 6, 3, ctx.seed + 17 >>> 0) - 0.5) * 0.036;
835
+ const warpV = (periodicFbm2D(u, v, 6, 6, 3, ctx.seed + 59 >>> 0) - 0.5) * 0.024;
836
+ const cell = periodicVoronoi2D(u + warpU, v + warpV, 5, 8, ctx.seed, 0.82);
837
+ const stoneMask = smoothstep(0.022, 0.075, cell.edgeDist);
838
+ const strata = periodicRidgedFbm2D(u, v, 8, 12, 4, ctx.seed + 149 >>> 0, 0.58);
839
+ const grit = periodicFbm2D(u, v, 20, 20, 3, ctx.seed + 271 >>> 0, 0.6);
840
+ const stoneTone = cell.cellId > 0.5 ? mixRgb(ctx.baseRgb, ctx.warmTint, (cell.cellId - 0.5) * 0.45) : mixRgb(ctx.baseRgb, ctx.coolTint, (0.5 - cell.cellId) * 0.45);
841
+ const stoneMul = 0.78 + strata * 0.3 + (grit - 0.5) * 0.14 + (cell.cellId - 0.5) * 0.22;
842
+ const naturalMortar = mixRgb(ctx.groutRgb, [132, 128, 120], 0.68);
843
+ const mortarMul = 0.78 + grit * 0.22;
844
+ setRgb(out, lerp(naturalMortar[0] * mortarMul, stoneTone[0] * stoneMul, stoneMask), lerp(naturalMortar[1] * mortarMul, stoneTone[1] * stoneMul, stoneMask), lerp(naturalMortar[2] * mortarMul, stoneTone[2] * stoneMul, stoneMask));
845
+ const hStone = 0.52 + strata * 0.32 + (grit - 0.5) * 0.12 + (cell.cellId - 0.5) * 0.12;
846
+ const hMortar = 0.14 + (grit - 0.5) * 0.08;
847
+ out.height = clamp(lerp(hMortar, hStone, stoneMask), 0.05, 0.98);
848
+ out.roughness = clamp(lerp(0.94, 0.74 + (1 - strata) * 0.18, stoneMask), 0.6, 0.98);
849
+ out.ao = clamp(lerp(0.36, 0.96 - (1 - strata) * 0.14, stoneMask), 0.24, 1);
850
+ out.metallic = 0;
851
+ }
852
+ function evalTerrazzo(u, v, ctx, out) {
853
+ const binder = periodicFbm2D(u, v, 8, 8, 3, ctx.seed, 0.5);
854
+ const fineSand = periodicValueNoise2D(u, v, 64, 64, ctx.seed + 43 >>> 0);
855
+ const microSpeck = periodicValueNoise2D(u, v, 96, 96, ctx.seed + 389 >>> 0);
856
+ const chipsLg = periodicVoronoi2D(u, v, 15, 15, ctx.seed + 103 >>> 0, 0.92);
857
+ const chipsSm = periodicVoronoi2D(u + 0.07, v + 0.11, 32, 32, ctx.seed + 239 >>> 0, 0.95);
858
+ const isLgChip = chipsLg.cellId > 0.58 && chipsLg.edgeDist > 0.2 && chipsLg.f1 < 0.38;
859
+ const isSmChip = !isLgChip && chipsSm.cellId > 0.62 && chipsSm.edgeDist > 0.18 && chipsSm.f1 < 0.36;
860
+ const speckShift = microSpeck > 0.88 ? 14 : microSpeck < 0.1 ? -18 : 0;
861
+ const binderMul = 1 + (binder - 0.5) * 0.08 + (fineSand - 0.5) * 0.04;
862
+ applyRgbTint(out, ctx.baseRgb, binderMul, 10 + speckShift);
863
+ let height = 0.48 + (binder - 0.5) * 0.05 + (fineSand - 0.5) * 0.03;
864
+ let rough = 0.32 + (1 - binder) * 0.09;
865
+ let ao = 0.96;
866
+ if (isLgChip || isSmChip) {
867
+ const chipId = isLgChip ? chipsLg.cellId : chipsSm.cellId;
868
+ const edge = isLgChip ? chipsLg.edgeDist : chipsSm.edgeDist;
869
+ const chipEdge = smoothstep(isLgChip ? 0.2 : 0.18, isLgChip ? 0.23 : 0.21, edge);
870
+ const bucket = Math.floor(fract(chipId * 13.7) * 5);
871
+ let chipRgb;
872
+ if (bucket === 0) {
873
+ chipRgb = modulateRgb(ctx.baseRgb, { satScale: 0.2, valScale: 1.38, valOffset: 0.12 });
874
+ } else if (bucket === 1) {
875
+ chipRgb = modulateRgb(ctx.baseRgb, { satScale: 0.45, valScale: 0.42 });
876
+ } else if (bucket === 2) {
877
+ chipRgb = modulateRgb(ctx.baseRgb, { hueShift: -20, satOffset: 0.14, valScale: 0.96 });
878
+ } else if (bucket === 3) {
879
+ chipRgb = modulateRgb(ctx.baseRgb, { hueShift: 22, satOffset: 0.12, valScale: 0.78 });
880
+ } else {
881
+ chipRgb = modulateRgb(ctx.baseRgb, { hueShift: -6, satOffset: 0.06, valScale: 1.18 });
882
+ }
883
+ const chipVein = periodicValueNoise2D(u, v, 32, 32, ctx.seed + 313 >>> 0);
884
+ const chipMul = 0.94 + chipVein * 0.12;
885
+ const cR = chipRgb[0] * chipMul;
886
+ const cG = chipRgb[1] * chipMul;
887
+ const cB = chipRgb[2] * chipMul;
888
+ out.r = lerp(out.r, cR, chipEdge);
889
+ out.g = lerp(out.g, cG, chipEdge);
890
+ out.b = lerp(out.b, cB, chipEdge);
891
+ height = lerp(height - 0.02, 0.56 + (chipVein - 0.5) * 0.04, chipEdge);
892
+ rough = lerp(rough, 0.16 + (1 - chipVein) * 0.06, chipEdge);
893
+ ao = lerp(0.9, 0.99, chipEdge);
894
+ }
895
+ out.height = clamp(height, 0.2, 0.85);
896
+ out.roughness = clamp(rough, 0.14, 0.52);
897
+ out.ao = clamp(ao, 0.72, 1);
898
+ out.metallic = 0;
899
+ }
900
+ function evalMarbleStone(u, v, ctx, out) {
901
+ const warp1 = periodicDomainWarpFbm2D(u, v, 2, 2, 4, 0.36, ctx.seed);
902
+ const warp2 = periodicFbm2D(u, v, 4, 4, 4, ctx.seed + 83 >>> 0, 0.55);
903
+ const fineWarp = periodicFbm2D(u, v, 10, 10, 3, ctx.seed + 211 >>> 0, 0.58);
904
+ const strata = periodicFbm2D(u + warp1 * 0.2, v - warp2 * 0.16, 3, 3, 4, ctx.seed + 179 >>> 0, 0.54);
905
+ const veinPhase = TWO_PI * (u + v + (warp1 - 0.5) * 1.15 + (warp2 - 0.5) * 0.42 + (fineWarp - 0.5) * 0.14);
906
+ const primaryVein = Math.pow(1 - Math.abs(Math.sin(veinPhase)), 10);
907
+ const softHalo = Math.pow(1 - Math.abs(Math.sin(veinPhase)), 3) * 0.24;
908
+ const secPhase = TWO_PI * (u + 2 * v + (warp2 - 0.5) * 1.25 + (fineWarp - 0.5) * 0.2);
909
+ const secondaryVein = Math.pow(1 - Math.abs(Math.sin(secPhase)), 14) * 0.52;
910
+ const vein = clamp(primaryVein + softHalo + secondaryVein, 0, 1);
911
+ const avgLum = (ctx.baseRgb[0] + ctx.baseRgb[1] + ctx.baseRgb[2]) / 3;
912
+ const veinRgb = avgLum < 90 ? modulateRgb(ctx.baseRgb, { hueShift: 18, satOffset: 0.12, valScale: 1.85, valOffset: 0.12 }) : modulateRgb(ctx.baseRgb, { satScale: 0.55, valScale: 0.58 });
913
+ const baseMul = 0.92 + strata * 0.14;
914
+ applyRgbTint(out, ctx.baseRgb, baseMul);
915
+ blendOutWithRgb(out, veinRgb, vein * 0.68);
916
+ out.height = clamp(0.54 + (strata - 0.5) * 0.09 - primaryVein * 0.12, 0.25, 0.8);
917
+ out.roughness = clamp(0.14 + (1 - strata) * 0.08 + vein * 0.16, 0.09, 0.4);
918
+ out.ao = clamp(0.97 - primaryVein * 0.12, 0.8, 1);
919
+ out.metallic = 0;
920
+ }
921
+ function evalMosaicTile(u, v, ctx, out) {
922
+ const grid = 12;
923
+ const gx = fract(u) * grid;
924
+ const gy = fract(v) * grid;
925
+ const tx = Math.floor(gx);
926
+ const ty = Math.floor(gy);
927
+ const lx = gx - tx;
928
+ const ly = gy - ty;
929
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly);
930
+ const tileMask = smoothstep(0.055, 0.135, edge);
931
+ const dx = (lx - 0.5) * 2;
932
+ const dy = (ly - 0.5) * 2;
933
+ const pillow = clamp(1 - (dx * dx + dy * dy) * 0.28, 0.5, 1);
934
+ const tileId = hash2D(tx, ty, ctx.seed);
935
+ const glaze = periodicFbm2D(u, v, 10, 10, 3, ctx.seed + 97 >>> 0, 0.5);
936
+ const tileTone = tileId > 0.5 ? mixRgb(ctx.baseRgb, ctx.lightTint, (tileId - 0.5) * 0.35) : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - tileId) * 0.35);
937
+ const tileMul = 0.9 + (tileId - 0.5) * 0.18 + (glaze - 0.5) * 0.08;
938
+ setRgb(out, lerp(ctx.groutRgb[0], tileTone[0] * tileMul, tileMask), lerp(ctx.groutRgb[1], tileTone[1] * tileMul, tileMask), lerp(ctx.groutRgb[2], tileTone[2] * tileMul, tileMask));
939
+ out.height = clamp(lerp(0.18, 0.55 + pillow * 0.32, tileMask), 0.08, 0.96);
940
+ out.roughness = clamp(lerp(0.88, 0.26 + (1 - glaze) * 0.12, tileMask), 0.18, 0.94);
941
+ out.ao = clamp(lerp(0.46, 0.98, tileMask), 0.32, 1);
942
+ out.metallic = 0;
943
+ }
944
+ function evalPoolTile(u, v, ctx, out) {
945
+ const grid = 20;
946
+ const gx = fract(u) * grid;
947
+ const gy = fract(v) * grid;
948
+ const tx = Math.floor(gx);
949
+ const ty = Math.floor(gy);
950
+ const lx = gx - tx;
951
+ const ly = gy - ty;
952
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly);
953
+ const tileMask = smoothstep(0.04, 0.095, edge);
954
+ const dx = (lx - 0.5) * 2;
955
+ const dy = (ly - 0.5) * 2;
956
+ const crown = clamp(1 - (dx * dx + dy * dy) * 0.28, 0.5, 1);
957
+ const tileId = hash2D(tx, ty, ctx.seed);
958
+ const ripple = periodicFbm2D(u, v, 8, 8, 3, ctx.seed + 131 >>> 0, 0.5);
959
+ const tileRgb = modulateRgb(ctx.baseRgb, {
960
+ hueShift: (tileId - 0.5) * 22,
961
+ satOffset: (tileId - 0.45) * 0.18,
962
+ valScale: 0.84 + tileId * 0.34
963
+ });
964
+ const whiteGrout = [234, 240, 244];
965
+ const glazeMul = 0.94 + crown * 0.11 + (ripple - 0.5) * 0.06;
966
+ setRgb(out, lerp(whiteGrout[0], tileRgb[0] * glazeMul, tileMask), lerp(whiteGrout[1], tileRgb[1] * glazeMul, tileMask), lerp(whiteGrout[2], tileRgb[2] * glazeMul, tileMask));
967
+ out.height = clamp(lerp(0.22, 0.58 + crown * 0.32, tileMask), 0.1, 0.96);
968
+ out.roughness = clamp(lerp(0.78, 0.08 + (1 - crown) * 0.09 + (ripple - 0.5) * 0.04, tileMask), 0.06, 0.86);
969
+ out.ao = clamp(lerp(0.58, 0.99, tileMask), 0.42, 1);
970
+ out.metallic = 0;
971
+ }
972
+ function evalCheckerTile(u, v, ctx, out) {
973
+ const grid = 4;
974
+ const gx = fract(u) * grid;
975
+ const gy = fract(v) * grid;
976
+ const tx = Math.floor(gx);
977
+ const ty = Math.floor(gy);
978
+ const lx = gx - tx;
979
+ const ly = gy - ty;
980
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly);
981
+ const tileMask = smoothstep(0.016, 0.042, edge);
982
+ const isLight = (tx + ty & 1) === 0;
983
+ const stoneCloud = periodicFbm2D(u, v, 8, 8, 3, ctx.seed + (isLight ? 17 : 73) >>> 0, 0.55);
984
+ const tileVar = (hash2D(tx, ty, ctx.seed) - 0.5) * 0.06;
985
+ const lightSquare = modulateRgb(ctx.baseRgb, { satScale: 0.5, valScale: 1.24, valOffset: 0.06 });
986
+ const darkSquare = modulateRgb(ctx.baseRgb, { satScale: 0.85, valScale: 0.52 });
987
+ const sqBase = isLight ? lightSquare : darkSquare;
988
+ const sqMul = 0.92 + stoneCloud * 0.15 + tileVar;
989
+ setRgb(out, lerp(ctx.groutRgb[0], sqBase[0] * sqMul, tileMask), lerp(ctx.groutRgb[1], sqBase[1] * sqMul, tileMask), lerp(ctx.groutRgb[2], sqBase[2] * sqMul, tileMask));
990
+ out.height = clamp(lerp(0.22, 0.68 + (stoneCloud - 0.5) * 0.08, tileMask), 0.1, 0.92);
991
+ out.roughness = clamp(lerp(0.86, 0.3 + (1 - stoneCloud) * 0.14 + (isLight ? -0.02 : 0.03), tileMask), 0.2, 0.92);
992
+ out.ao = clamp(lerp(0.48, 0.97, tileMask), 0.34, 1);
993
+ out.metallic = 0;
994
+ }
995
+ function evalGaragePanel(u, v, ctx, out) {
996
+ const sections = 4;
997
+ const coffers = 2;
998
+ const gv = fract(v) * sections;
999
+ const lv = gv - Math.floor(gv);
1000
+ const gu = fract(u) * coffers;
1001
+ const lu = gu - Math.floor(gu);
1002
+ const sectionSeam = smoothstep(0.012, 0.032, Math.min(lv, 1 - lv));
1003
+ const cofferDistX = Math.min(lu - 0.09, 0.91 - lu);
1004
+ const cofferDistY = Math.min(lv - 0.17, 0.83 - lv);
1005
+ const cofferInner = Math.min(cofferDistX, cofferDistY);
1006
+ const cofferRecess = smoothstep(-0.008, 0.014, cofferInner) * (1 - smoothstep(0.036, 0.056, cofferInner));
1007
+ const rib = 0.5 + 0.5 * Math.sin(TWO_PI * 24 * v);
1008
+ const microPaint = periodicFbm2D(u, v, 16, 40, 3, ctx.seed, 0.5);
1009
+ const hPanel = 0.68 - cofferRecess * 0.34 + (rib - 0.5) * 0.03 + (microPaint - 0.5) * 0.03;
1010
+ const mul = (0.96 + (microPaint - 0.5) * 0.05 - cofferRecess * 0.055) * lerp(0.72, 1, sectionSeam);
1011
+ applyRgbTint(out, ctx.baseRgb, mul);
1012
+ out.height = clamp(lerp(0.14, hPanel, sectionSeam), 0.08, 0.92);
1013
+ out.roughness = clamp(0.4 + (microPaint - 0.5) * 0.1 + (1 - sectionSeam) * 0.25, 0.3, 0.76);
1014
+ out.ao = clamp(lerp(0.48, 0.98 - cofferRecess * 0.16, sectionSeam), 0.34, 1);
1015
+ out.metallic = 0.14;
1016
+ }
1017
+ function evalEarth(u, v, ctx, out) {
1018
+ const clods = periodicRidgedFbm2D(u, v, 6, 6, 4, ctx.seed, 0.6);
1019
+ const loam = periodicFbm2D(u, v, 14, 14, 4, ctx.seed + 83 >>> 0, 0.62);
1020
+ const grit = periodicValueNoise2D(u, v, 36, 36, ctx.seed + 199 >>> 0);
1021
+ const pebbles = periodicVoronoi2D(u, v, 16, 16, ctx.seed + 281 >>> 0, 0.95);
1022
+ const hasPebble = pebbles.cellId > 0.76;
1023
+ const pebbleMask = hasPebble ? smoothstep(0.32, 0.12, pebbles.f1) : 0;
1024
+ const soilTone = loam > 0.5 ? mixRgb(ctx.baseRgb, ctx.warmTint, (loam - 0.5) * 0.35) : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - loam) * 0.4);
1025
+ const soilMul = 0.76 + clods * 0.34 + (grit - 0.5) * 0.16;
1026
+ const pebbleTone = modulateRgb(ctx.baseRgb, {
1027
+ satScale: 0.45,
1028
+ valScale: 1.15 + (pebbles.cellId - 0.76) * 1.4
1029
+ });
1030
+ setRgb(out, lerp(soilTone[0] * soilMul, pebbleTone[0], pebbleMask), lerp(soilTone[1] * soilMul, pebbleTone[1], pebbleMask), lerp(soilTone[2] * soilMul, pebbleTone[2], pebbleMask));
1031
+ const hSoil = 0.18 + clods * 0.48 + (loam - 0.5) * 0.2 + (grit - 0.5) * 0.1;
1032
+ out.height = clamp(lerp(hSoil, 0.88, pebbleMask * 0.75), 0.05, 0.98);
1033
+ out.roughness = clamp(lerp(0.9 + (1 - clods) * 0.08, 0.68, pebbleMask), 0.6, 0.99);
1034
+ out.ao = clamp(0.58 + clods * 0.36 + pebbleMask * 0.06, 0.34, 1);
1035
+ out.metallic = 0;
1036
+ }
1037
+ function evalTerracottaTile(u, v, ctx, out) {
1038
+ const grid = 4;
1039
+ const wobbleU = (periodicFbm2D(u, v, 8, 8, 2, ctx.seed + 23 >>> 0) - 0.5) * 0.012;
1040
+ const wobbleV = (periodicFbm2D(u, v, 8, 8, 2, ctx.seed + 59 >>> 0) - 0.5) * 0.012;
1041
+ const gx = fract(u + wobbleU) * grid;
1042
+ const gy = fract(v + wobbleV) * grid;
1043
+ const tx = Math.floor(gx);
1044
+ const ty = Math.floor(gy);
1045
+ const lx = gx - tx;
1046
+ const ly = gy - ty;
1047
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly);
1048
+ const tileMask = smoothstep(0.022, 0.058, edge);
1049
+ const tileId = hash2D(tx, ty, ctx.seed);
1050
+ const clay = periodicFbm2D(u, v, 10, 10, 4, ctx.seed + 137 >>> 0, 0.58);
1051
+ const speckle = periodicValueNoise2D(u, v, 32, 32, ctx.seed + 257 >>> 0);
1052
+ const kilnEdge = smoothstep(0.04, 0.28, edge);
1053
+ const tileTone = tileId > 0.5 ? mixRgb(ctx.baseRgb, ctx.warmTint, (tileId - 0.5) * 0.42) : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - tileId) * 0.4);
1054
+ const tileMul = (0.86 + clay * 0.24 + (tileId - 0.5) * 0.16) * (0.9 + 0.1 * kilnEdge) - (speckle < 0.1 ? 0.14 : 0);
1055
+ const quarryGrout = mixRgb(ctx.groutRgb, [136, 130, 122], 0.52);
1056
+ const groutMul = 0.8 + speckle * 0.2;
1057
+ setRgb(out, lerp(quarryGrout[0] * groutMul, tileTone[0] * tileMul, tileMask), lerp(quarryGrout[1] * groutMul, tileTone[1] * tileMul, tileMask), lerp(quarryGrout[2] * groutMul, tileTone[2] * tileMul, tileMask));
1058
+ const hTile = 0.62 + (clay - 0.5) * 0.18 + kilnEdge * 0.08 - (speckle < 0.1 ? 0.12 : 0);
1059
+ const hGrout = 0.18 + (speckle - 0.5) * 0.08;
1060
+ out.height = clamp(lerp(hGrout, hTile, tileMask), 0.06, 0.96);
1061
+ out.roughness = clamp(lerp(0.92, 0.6 + (1 - clay) * 0.18, tileMask), 0.48, 0.97);
1062
+ out.ao = clamp(lerp(0.42, 0.96, tileMask), 0.28, 1);
1063
+ out.metallic = 0;
1064
+ }
1065
+ function evalCeramicTile(u, v, ctx, out) {
1066
+ const grid = 3;
1067
+ const gx = fract(u) * grid;
1068
+ const gy = fract(v) * grid;
1069
+ const tx = Math.floor(gx);
1070
+ const ty = Math.floor(gy);
1071
+ const lx = gx - tx;
1072
+ const ly = gy - ty;
1073
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly);
1074
+ const tileMask = smoothstep(0.012, 0.032, edge);
1075
+ const tileId = hash2D(tx, ty, ctx.seed);
1076
+ const offU = hash2D(tx, ty, ctx.seed + 19 >>> 0);
1077
+ const offV = hash2D(tx, ty, ctx.seed + 67 >>> 0);
1078
+ const glazeCloud = periodicDomainWarpFbm2D(u + offU, v + offV, 4, 4, 4, 0.16, ctx.seed + 113 >>> 0);
1079
+ const microGrunge = periodicFbm2D(u, v, 18, 18, 3, ctx.seed + 241 >>> 0, 0.58);
1080
+ const tileMul = 0.88 + glazeCloud * 0.22 + (microGrunge - 0.5) * 0.1 + (tileId - 0.5) * 0.1;
1081
+ const groutMul = 0.85 + microGrunge * 0.18;
1082
+ setRgb(out, lerp(ctx.groutRgb[0] * groutMul, ctx.baseRgb[0] * tileMul, tileMask), lerp(ctx.groutRgb[1] * groutMul, ctx.baseRgb[1] * tileMul, tileMask), lerp(ctx.groutRgb[2] * groutMul, ctx.baseRgb[2] * tileMul, tileMask));
1083
+ const hTile = 0.66 + (glazeCloud - 0.5) * 0.1 + (microGrunge - 0.5) * 0.06;
1084
+ out.height = clamp(lerp(0.22, hTile, tileMask), 0.1, 0.92);
1085
+ out.roughness = clamp(lerp(0.86, 0.3 + (1 - glazeCloud) * 0.16 + (microGrunge - 0.5) * 0.08, tileMask), 0.2, 0.92);
1086
+ out.ao = clamp(lerp(0.46, 0.97, tileMask), 0.32, 1);
1087
+ out.metallic = 0;
1088
+ }
1089
+ function evaluatePattern(pattern, u, v, ctx, out) {
1090
+ switch (pattern) {
1091
+ case "concrete-plate":
1092
+ evalConcretePlate(u, v, ctx, out);
1093
+ return;
1094
+ case "concrete-polished":
1095
+ evalConcretePolished(u, v, ctx, out);
1096
+ return;
1097
+ case "concrete-raw":
1098
+ evalConcreteRaw(u, v, ctx, out);
1099
+ return;
1100
+ case "plaster":
1101
+ evalPlaster(u, v, ctx, out);
1102
+ return;
1103
+ case "drywall":
1104
+ evalDrywall(u, v, ctx, out);
1105
+ return;
1106
+ case "stucco":
1107
+ evalStucco(u, v, ctx, out);
1108
+ return;
1109
+ case "cotton":
1110
+ evalCotton(u, v, ctx, out);
1111
+ return;
1112
+ case "linen":
1113
+ evalLinen(u, v, ctx, out);
1114
+ return;
1115
+ case "velvet":
1116
+ evalVelvet(u, v, ctx, out);
1117
+ return;
1118
+ case "suede":
1119
+ evalSuede(u, v, ctx, out);
1120
+ return;
1121
+ case "wool":
1122
+ evalWool(u, v, ctx, out);
1123
+ return;
1124
+ case "boucle":
1125
+ evalBoucle(u, v, ctx, out);
1126
+ return;
1127
+ case "leather":
1128
+ evalLeather(u, v, ctx, out);
1129
+ return;
1130
+ case "copper":
1131
+ evalCopper(u, v, ctx, out);
1132
+ return;
1133
+ case "polished-metal":
1134
+ evalPolishedMetal(u, v, ctx, out);
1135
+ return;
1136
+ case "brushed-metal":
1137
+ evalBrushedMetal(u, v, ctx, out);
1138
+ return;
1139
+ case "roof-shingles":
1140
+ evalRoofShingles(u, v, ctx, out);
1141
+ return;
1142
+ case "roof-tiles":
1143
+ evalRoofTiles(u, v, ctx, out);
1144
+ return;
1145
+ case "board-batten":
1146
+ evalBoardBatten(u, v, ctx, out);
1147
+ return;
1148
+ case "lap-siding":
1149
+ evalLapSiding(u, v, ctx, out);
1150
+ return;
1151
+ case "wood-fine":
1152
+ evalWoodFine(u, v, ctx, out);
1153
+ return;
1154
+ case "wood-plank":
1155
+ evalWoodPlank(u, v, ctx, out);
1156
+ return;
1157
+ case "wood-parquet":
1158
+ evalWoodParquet(u, v, ctx, out);
1159
+ return;
1160
+ case "wood-chevron":
1161
+ evalWoodChevron(u, v, ctx, out);
1162
+ return;
1163
+ case "wood-square-parquet":
1164
+ evalWoodSquareParquet(u, v, ctx, out);
1165
+ return;
1166
+ case "brick":
1167
+ evalBrick(u, v, ctx, out);
1168
+ return;
1169
+ case "stone-wall":
1170
+ evalStoneWall(u, v, ctx, out);
1171
+ return;
1172
+ case "terrazzo":
1173
+ evalTerrazzo(u, v, ctx, out);
1174
+ return;
1175
+ case "marble-stone":
1176
+ evalMarbleStone(u, v, ctx, out);
1177
+ return;
1178
+ case "mosaic-tile":
1179
+ evalMosaicTile(u, v, ctx, out);
1180
+ return;
1181
+ case "pool-tile":
1182
+ evalPoolTile(u, v, ctx, out);
1183
+ return;
1184
+ case "checker-tile":
1185
+ evalCheckerTile(u, v, ctx, out);
1186
+ return;
1187
+ case "garage-panel":
1188
+ evalGaragePanel(u, v, ctx, out);
1189
+ return;
1190
+ case "earth":
1191
+ evalEarth(u, v, ctx, out);
1192
+ return;
1193
+ case "terracotta-tile":
1194
+ evalTerracottaTile(u, v, ctx, out);
1195
+ return;
1196
+ case "ceramic-tile":
1197
+ evalCeramicTile(u, v, ctx, out);
1198
+ return;
1199
+ default: {
1200
+ const exhaustive = pattern;
1201
+ throw new Error(`Unhandled MitMaterialPattern: ${String(exhaustive)}`);
1202
+ }
1203
+ }
1204
+ }
1205
+ function synthesizeMaterialPack(spec, size = 512) {
1206
+ if (!Number.isInteger(size) || size < 4 || size > 4096) {
1207
+ throw new RangeError(`Material texture size must be an integer in 4..4096, got ${size}`);
1208
+ }
1209
+ const width = size;
1210
+ const height = size;
1211
+ const pixelCount = width * height;
1212
+ const byteCount = pixelCount * 4;
1213
+ const albedo = new Uint8ClampedArray(byteCount);
1214
+ const normal = new Uint8ClampedArray(byteCount);
1215
+ const roughness = new Uint8ClampedArray(byteCount);
1216
+ const ao = new Uint8ClampedArray(byteCount);
1217
+ const metallic = new Uint8ClampedArray(byteCount);
1218
+ const heightBuf = new Float32Array(pixelCount);
1219
+ const baseAoBuf = new Float32Array(pixelCount);
1220
+ const baseRgb = hexToRgb(spec.previewColor);
1221
+ const seed = fnv1a32(spec.id);
1222
+ const avgLum = (baseRgb[0] + baseRgb[1] + baseRgb[2]) / 3;
1223
+ const groutRgb = avgLum > 140 ? modulateRgb(baseRgb, { satScale: 0.35, valScale: 0.72 }) : modulateRgb(baseRgb, { satScale: 0.25, valScale: 1.45, valOffset: 0.14 });
1224
+ const ctx = {
1225
+ baseRgb,
1226
+ seed,
1227
+ lightTint: modulateRgb(baseRgb, { satScale: 0.85, valScale: 1.22, valOffset: 0.05 }),
1228
+ darkTint: modulateRgb(baseRgb, { satScale: 1.05, valScale: 0.68 }),
1229
+ warmTint: modulateRgb(baseRgb, { hueShift: -10, satOffset: 0.08, valScale: 1.06 }),
1230
+ coolTint: modulateRgb(baseRgb, { hueShift: 12, satScale: 0.85, valScale: 0.92 }),
1231
+ groutRgb
1232
+ };
1233
+ const sample = { r: 0, g: 0, b: 0, height: 0.5, roughness: 0.5, ao: 1, metallic: 0 };
1234
+ for (let y = 0;y < height; y++) {
1235
+ const v = (y + 0.5) / height;
1236
+ const rowOffset = y * width;
1237
+ for (let x = 0;x < width; x++) {
1238
+ const u = (x + 0.5) / width;
1239
+ evaluatePattern(spec.pattern, u, v, ctx, sample);
1240
+ const idx = rowOffset + x;
1241
+ const p = idx * 4;
1242
+ albedo[p] = clampByte(sample.r);
1243
+ albedo[p + 1] = clampByte(sample.g);
1244
+ albedo[p + 2] = clampByte(sample.b);
1245
+ albedo[p + 3] = 255;
1246
+ const roughByte = clampByte(sample.roughness * 255);
1247
+ roughness[p] = roughByte;
1248
+ roughness[p + 1] = roughByte;
1249
+ roughness[p + 2] = roughByte;
1250
+ roughness[p + 3] = 255;
1251
+ const metalByte = clampByte(sample.metallic * 255);
1252
+ metallic[p] = metalByte;
1253
+ metallic[p + 1] = metalByte;
1254
+ metallic[p + 2] = metalByte;
1255
+ metallic[p + 3] = 255;
1256
+ heightBuf[idx] = sample.height;
1257
+ baseAoBuf[idx] = sample.ao;
1258
+ }
1259
+ }
1260
+ const cfg = PATTERN_CONFIG[spec.pattern];
1261
+ const resScale = Math.sqrt(size / 256);
1262
+ const slopeGain = cfg.normalStrength * 0.45 * resScale;
1263
+ const maxSlope = 2.15;
1264
+ for (let y = 0;y < height; y++) {
1265
+ const ym = (y - 1 + height) % height;
1266
+ const yp = (y + 1) % height;
1267
+ const rowM = ym * width;
1268
+ const rowC = y * width;
1269
+ const rowP = yp * width;
1270
+ for (let x = 0;x < width; x++) {
1271
+ const xm = (x - 1 + width) % width;
1272
+ const xp = (x + 1) % width;
1273
+ const tl = heightBuf[rowM + xm];
1274
+ const t = heightBuf[rowM + x];
1275
+ const tr = heightBuf[rowM + xp];
1276
+ const l = heightBuf[rowC + xm];
1277
+ const c = heightBuf[rowC + x];
1278
+ const r = heightBuf[rowC + xp];
1279
+ const bl = heightBuf[rowP + xm];
1280
+ const b = heightBuf[rowP + x];
1281
+ const br = heightBuf[rowP + xp];
1282
+ const gx = tr + 2 * r + br - (tl + 2 * l + bl);
1283
+ const gy = bl + 2 * b + br - (tl + 2 * t + tr);
1284
+ let nx = -gx * slopeGain;
1285
+ let ny = gy * slopeGain;
1286
+ const slopeMag = Math.hypot(nx, ny);
1287
+ if (slopeMag > maxSlope) {
1288
+ const shrink = maxSlope / slopeMag;
1289
+ nx *= shrink;
1290
+ ny *= shrink;
1291
+ }
1292
+ const invLen = 1 / Math.hypot(nx, ny, 1);
1293
+ const idx = rowC + x;
1294
+ const p = idx * 4;
1295
+ normal[p] = clampByte((nx * invLen * 0.5 + 0.5) * 255);
1296
+ normal[p + 1] = clampByte((ny * invLen * 0.5 + 0.5) * 255);
1297
+ normal[p + 2] = clampByte((invLen * 0.5 + 0.5) * 255);
1298
+ normal[p + 3] = 255;
1299
+ const neighborMean = (tl + t + tr + l + r + bl + b + br) * 0.125;
1300
+ const concavity = Math.max(0, neighborMean - c);
1301
+ const creviceDarken = clamp(concavity * cfg.aoCavityStrength, 0, 0.42);
1302
+ const finalAo = clamp(baseAoBuf[idx] * (1 - creviceDarken) - (1 - c) * cfg.globalHeightAo, 0.16, 1);
1303
+ const aoByte = clampByte(finalAo * 255);
1304
+ ao[p] = aoByte;
1305
+ ao[p + 1] = aoByte;
1306
+ ao[p + 2] = aoByte;
1307
+ ao[p + 3] = 255;
1308
+ }
1309
+ }
1310
+ return { width, height, albedo, normal, roughness, ao, metallic };
1311
+ }
1312
+ // src/materials/ktx2.ts
1313
+ import {
1314
+ KHR_DF_CHANNEL_RGBSDA_ALPHA,
1315
+ KHR_DF_CHANNEL_RGBSDA_BLUE,
1316
+ KHR_DF_CHANNEL_RGBSDA_GREEN,
1317
+ KHR_DF_CHANNEL_RGBSDA_RED,
1318
+ KHR_DF_FLAG_ALPHA_STRAIGHT,
1319
+ KHR_DF_KHR_DESCRIPTORTYPE_BASICFORMAT,
1320
+ KHR_DF_MODEL_RGBSDA,
1321
+ KHR_DF_PRIMARIES_BT709,
1322
+ KHR_DF_SAMPLE_DATATYPE_LINEAR,
1323
+ KHR_DF_TRANSFER_LINEAR,
1324
+ KHR_DF_TRANSFER_SRGB,
1325
+ KHR_DF_VENDORID_KHRONOS,
1326
+ KHR_DF_VERSION,
1327
+ KHR_SUPERCOMPRESSION_NONE,
1328
+ KHR_SUPERCOMPRESSION_ZSTD,
1329
+ KTX2Container,
1330
+ VK_FORMAT_R8G8B8A8_SRGB,
1331
+ VK_FORMAT_R8G8B8A8_UNORM,
1332
+ write
1333
+ } from "ktx-parse";
1334
+ var KTX2_WRITER_ID = "@kruschdev/pascal-plugin-mit-assets (MIT)";
1335
+ function encodeKtx2Rgba8(rgba, width, height, srgb, compressZstd) {
1336
+ if (!Number.isInteger(width) || width <= 0 || !Number.isInteger(height) || height <= 0) {
1337
+ throw new RangeError(`Invalid KTX2 dimensions: ${width}x${height}`);
1338
+ }
1339
+ const expectedByteLength = width * height * 4;
1340
+ if (rgba.byteLength !== expectedByteLength) {
1341
+ throw new RangeError(`Invalid RGBA8 buffer byteLength: expected ${expectedByteLength} for ${width}x${height}, got ${rgba.byteLength}`);
1342
+ }
1343
+ const rawBytes = new Uint8Array(rgba.buffer, rgba.byteOffset, rgba.byteLength);
1344
+ const useZstd = typeof compressZstd === "function";
1345
+ const levelData = useZstd ? compressZstd(rawBytes) : rawBytes;
1346
+ const container = new KTX2Container;
1347
+ container.vkFormat = srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
1348
+ container.typeSize = 1;
1349
+ container.pixelWidth = width;
1350
+ container.pixelHeight = height;
1351
+ container.pixelDepth = 0;
1352
+ container.layerCount = 0;
1353
+ container.faceCount = 1;
1354
+ container.supercompressionScheme = useZstd ? KHR_SUPERCOMPRESSION_ZSTD : KHR_SUPERCOMPRESSION_NONE;
1355
+ container.globalData = null;
1356
+ const alphaChannelType = srgb ? KHR_DF_CHANNEL_RGBSDA_ALPHA | KHR_DF_SAMPLE_DATATYPE_LINEAR : KHR_DF_CHANNEL_RGBSDA_ALPHA;
1357
+ container.dataFormatDescriptor = [
1358
+ {
1359
+ vendorId: KHR_DF_VENDORID_KHRONOS,
1360
+ descriptorType: KHR_DF_KHR_DESCRIPTORTYPE_BASICFORMAT,
1361
+ versionNumber: KHR_DF_VERSION,
1362
+ descriptorBlockSize: 24 + 4 * 16,
1363
+ colorModel: KHR_DF_MODEL_RGBSDA,
1364
+ colorPrimaries: KHR_DF_PRIMARIES_BT709,
1365
+ transferFunction: srgb ? KHR_DF_TRANSFER_SRGB : KHR_DF_TRANSFER_LINEAR,
1366
+ flags: KHR_DF_FLAG_ALPHA_STRAIGHT,
1367
+ texelBlockDimension: [0, 0, 0, 0],
1368
+ bytesPlane: [4, 0, 0, 0, 0, 0, 0, 0],
1369
+ samples: [
1370
+ {
1371
+ bitOffset: 0,
1372
+ bitLength: 7,
1373
+ channelType: KHR_DF_CHANNEL_RGBSDA_RED,
1374
+ samplePosition: [0, 0, 0, 0],
1375
+ sampleLower: 0,
1376
+ sampleUpper: 255
1377
+ },
1378
+ {
1379
+ bitOffset: 8,
1380
+ bitLength: 7,
1381
+ channelType: KHR_DF_CHANNEL_RGBSDA_GREEN,
1382
+ samplePosition: [0, 0, 0, 0],
1383
+ sampleLower: 0,
1384
+ sampleUpper: 255
1385
+ },
1386
+ {
1387
+ bitOffset: 16,
1388
+ bitLength: 7,
1389
+ channelType: KHR_DF_CHANNEL_RGBSDA_BLUE,
1390
+ samplePosition: [0, 0, 0, 0],
1391
+ sampleLower: 0,
1392
+ sampleUpper: 255
1393
+ },
1394
+ {
1395
+ bitOffset: 24,
1396
+ bitLength: 7,
1397
+ channelType: alphaChannelType,
1398
+ samplePosition: [0, 0, 0, 0],
1399
+ sampleLower: 0,
1400
+ sampleUpper: 255
1401
+ }
1402
+ ]
1403
+ }
1404
+ ];
1405
+ container.keyValue = {
1406
+ KTXwriter: KTX2_WRITER_ID
1407
+ };
1408
+ container.levels = [
1409
+ {
1410
+ levelData,
1411
+ uncompressedByteLength: rawBytes.byteLength
1412
+ }
1413
+ ];
1414
+ return write(container, { keepWriter: true });
1415
+ }
1416
+
1417
+ // src/materials/index.ts
1418
+ function inferChannelFromFilename(filename) {
1419
+ const clean = filename.split(/[?#]/, 1)[0] ?? filename;
1420
+ const base = (clean.split(/[\\/]/).pop() ?? clean).toLowerCase();
1421
+ const stem = base.replace(/\.(ktx2|webp|png|jpe?g)$/i, "");
1422
+ if (/(?:^|[._-])thumb(?:[._-]|$)/.test(stem))
1423
+ return "thumb";
1424
+ if (/(?:^|[._-])normal(?:gl|dx)?(?:[._-]|$)/.test(stem))
1425
+ return "normal";
1426
+ if (/(?:^|[._-])(roughness|specularlevel|specular)(?:[._-]|$)/.test(stem))
1427
+ return "roughness";
1428
+ if (/(?:^|[._-])(ambientocclusion|ao)(?:[._-]|$)/.test(stem))
1429
+ return "ao";
1430
+ if (/(?:^|[._-])(metallic|metalness)(?:[._-]|$)/.test(stem))
1431
+ return "metallic";
1432
+ if (/(?:^|[._-])(basecolor|diffuse|albedo|color)(?:[._-]|$)/.test(stem))
1433
+ return "albedo";
1434
+ return "albedo";
1435
+ }
1436
+ export {
1437
+ KTX2_WRITER_ID,
1438
+ clamp,
1439
+ clampByte,
1440
+ createSeededRng,
1441
+ encodeKtx2Rgba8,
1442
+ fnv1a32,
1443
+ fract,
1444
+ hash2D,
1445
+ hash2DVec,
1446
+ hexToRgb,
1447
+ hsvToRgb,
1448
+ inferChannelFromFilename,
1449
+ lerp,
1450
+ mixRgb,
1451
+ modulateRgb,
1452
+ mulberry32,
1453
+ periodicDomainWarpFbm2D,
1454
+ periodicFbm2D,
1455
+ periodicRidgedFbm2D,
1456
+ periodicValueNoise2D,
1457
+ periodicVoronoi2D,
1458
+ quintic,
1459
+ rgbToHsv,
1460
+ scaleRgb,
1461
+ smoothstep,
1462
+ synthesizeMaterialPack,
1463
+ wrapInt
1464
+ };