@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,1507 @@
1
+ /* Clean-room MIT procedural PBR material synthesizer covering all 36 MitMaterialPattern variants. */
2
+
3
+ import type { MitMaterialPattern, MitMaterialSpec } from '../manifest'
4
+ import {
5
+ clamp,
6
+ clampByte,
7
+ fnv1a32,
8
+ fract,
9
+ hash2D,
10
+ hexToRgb,
11
+ lerp,
12
+ mixRgb,
13
+ modulateRgb,
14
+ periodicDomainWarpFbm2D,
15
+ periodicFbm2D,
16
+ periodicRidgedFbm2D,
17
+ periodicValueNoise2D,
18
+ periodicVoronoi2D,
19
+ smoothstep,
20
+ wrapInt,
21
+ } from './noise'
22
+
23
+ const TWO_PI = Math.PI * 2
24
+
25
+ export type SynthesizedMaterialMaps = {
26
+ width: number
27
+ height: number
28
+ albedo: Uint8ClampedArray
29
+ normal: Uint8ClampedArray
30
+ roughness: Uint8ClampedArray
31
+ ao: Uint8ClampedArray
32
+ metallic: Uint8ClampedArray
33
+ }
34
+
35
+ type PixelSample = {
36
+ r: number
37
+ g: number
38
+ b: number
39
+ height: number
40
+ roughness: number
41
+ ao: number
42
+ metallic: number
43
+ }
44
+
45
+ type PatternConfig = {
46
+ normalStrength: number
47
+ aoCavityStrength: number
48
+ globalHeightAo: number
49
+ }
50
+
51
+ type EvalContext = {
52
+ baseRgb: readonly [number, number, number]
53
+ seed: number
54
+ lightTint: readonly [number, number, number]
55
+ darkTint: readonly [number, number, number]
56
+ warmTint: readonly [number, number, number]
57
+ coolTint: readonly [number, number, number]
58
+ groutRgb: readonly [number, number, number]
59
+ }
60
+
61
+ const PATTERN_CONFIG: Record<MitMaterialPattern, PatternConfig> = {
62
+ 'concrete-plate': { normalStrength: 2.8, aoCavityStrength: 1.8, globalHeightAo: 0.08 },
63
+ 'concrete-polished': { normalStrength: 1.1, aoCavityStrength: 0.9, globalHeightAo: 0.04 },
64
+ 'concrete-raw': { normalStrength: 3.6, aoCavityStrength: 2.2, globalHeightAo: 0.1 },
65
+ plaster: { normalStrength: 1.8, aoCavityStrength: 1.2, globalHeightAo: 0.06 },
66
+ drywall: { normalStrength: 1.0, aoCavityStrength: 0.9, globalHeightAo: 0.04 },
67
+ stucco: { normalStrength: 4.2, aoCavityStrength: 2.4, globalHeightAo: 0.1 },
68
+ cotton: { normalStrength: 2.4, aoCavityStrength: 1.8, globalHeightAo: 0.07 },
69
+ linen: { normalStrength: 2.9, aoCavityStrength: 2.0, globalHeightAo: 0.08 },
70
+ velvet: { normalStrength: 1.6, aoCavityStrength: 1.2, globalHeightAo: 0.06 },
71
+ suede: { normalStrength: 2.0, aoCavityStrength: 1.4, globalHeightAo: 0.06 },
72
+ wool: { normalStrength: 3.4, aoCavityStrength: 2.2, globalHeightAo: 0.09 },
73
+ boucle: { normalStrength: 4.4, aoCavityStrength: 2.8, globalHeightAo: 0.12 },
74
+ leather: { normalStrength: 3.0, aoCavityStrength: 2.2, globalHeightAo: 0.09 },
75
+ copper: { normalStrength: 1.7, aoCavityStrength: 1.1, globalHeightAo: 0.04 },
76
+ 'polished-metal': { normalStrength: 0.9, aoCavityStrength: 0.7, globalHeightAo: 0.02 },
77
+ 'brushed-metal': { normalStrength: 1.5, aoCavityStrength: 1.0, globalHeightAo: 0.03 },
78
+ 'roof-shingles': { normalStrength: 4.5, aoCavityStrength: 2.5, globalHeightAo: 0.12 },
79
+ 'roof-tiles': { normalStrength: 5.0, aoCavityStrength: 2.6, globalHeightAo: 0.14 },
80
+ 'board-batten': { normalStrength: 4.0, aoCavityStrength: 2.2, globalHeightAo: 0.1 },
81
+ 'lap-siding': { normalStrength: 4.2, aoCavityStrength: 2.4, globalHeightAo: 0.11 },
82
+ 'wood-fine': { normalStrength: 1.9, aoCavityStrength: 1.4, globalHeightAo: 0.05 },
83
+ 'wood-plank': { normalStrength: 3.0, aoCavityStrength: 2.0, globalHeightAo: 0.08 },
84
+ 'wood-parquet': { normalStrength: 3.1, aoCavityStrength: 2.1, globalHeightAo: 0.08 },
85
+ 'wood-chevron': { normalStrength: 3.1, aoCavityStrength: 2.1, globalHeightAo: 0.08 },
86
+ 'wood-square-parquet': { normalStrength: 3.2, aoCavityStrength: 2.2, globalHeightAo: 0.08 },
87
+ brick: { normalStrength: 4.8, aoCavityStrength: 2.6, globalHeightAo: 0.12 },
88
+ 'stone-wall': { normalStrength: 5.2, aoCavityStrength: 2.8, globalHeightAo: 0.14 },
89
+ terrazzo: { normalStrength: 1.5, aoCavityStrength: 1.1, globalHeightAo: 0.05 },
90
+ 'marble-stone': { normalStrength: 1.4, aoCavityStrength: 1.0, globalHeightAo: 0.04 },
91
+ 'mosaic-tile': { normalStrength: 3.6, aoCavityStrength: 2.2, globalHeightAo: 0.09 },
92
+ 'pool-tile': { normalStrength: 3.4, aoCavityStrength: 2.0, globalHeightAo: 0.08 },
93
+ 'checker-tile': { normalStrength: 2.8, aoCavityStrength: 1.8, globalHeightAo: 0.07 },
94
+ 'garage-panel': { normalStrength: 3.6, aoCavityStrength: 2.0, globalHeightAo: 0.09 },
95
+ earth: { normalStrength: 4.6, aoCavityStrength: 2.6, globalHeightAo: 0.12 },
96
+ 'terracotta-tile': { normalStrength: 3.8, aoCavityStrength: 2.4, globalHeightAo: 0.1 },
97
+ 'ceramic-tile': { normalStrength: 2.8, aoCavityStrength: 1.9, globalHeightAo: 0.07 },
98
+ }
99
+
100
+ function setRgb(out: PixelSample, r: number, g: number, b: number): void {
101
+ out.r = r
102
+ out.g = g
103
+ out.b = b
104
+ }
105
+
106
+ function applyRgbTint(
107
+ out: PixelSample,
108
+ base: readonly [number, number, number],
109
+ mul: number,
110
+ add = 0,
111
+ ): void {
112
+ out.r = base[0] * mul + add
113
+ out.g = base[1] * mul + add
114
+ out.b = base[2] * mul + add
115
+ }
116
+
117
+ function blendOutWithRgb(out: PixelSample, rgb: readonly [number, number, number], t: number): void {
118
+ const k = clamp(t, 0, 1)
119
+ out.r = out.r + (rgb[0] - out.r) * k
120
+ out.g = out.g + (rgb[1] - out.g) * k
121
+ out.b = out.b + (rgb[2] - out.b) * k
122
+ }
123
+
124
+ /** Seamless organic wood grain evaluator (strictly periodic when `ringCount` is integer). */
125
+ function evalWoodGrain(
126
+ u: number,
127
+ v: number,
128
+ seed: number,
129
+ alongPeriod: number,
130
+ acrossPeriod: number,
131
+ ringCount: number,
132
+ ): { ring: number; pore: number; ray: number } {
133
+ const warp = (periodicFbm2D(u, v, alongPeriod, acrossPeriod, 3, seed, 0.55) - 0.5) * 0.9
134
+ const fineWarp = (periodicValueNoise2D(u, v, alongPeriod * 2, acrossPeriod * 4, (seed + 71) >>> 0) - 0.5) * 0.22
135
+ const wave = 0.5 + 0.5 * Math.sin(TWO_PI * (v * ringCount + warp + fineWarp))
136
+ const ring = Math.pow(wave, 0.65) * 0.72 + Math.pow(wave, 3.2) * 0.28
137
+ const pore = periodicFbm2D(u, v, alongPeriod * 2, acrossPeriod * 10, 2, (seed + 193) >>> 0, 0.5)
138
+ const ray = periodicValueNoise2D(u, v, alongPeriod * 6, acrossPeriod * 2, (seed + 311) >>> 0)
139
+ return { ring, pore, ray }
140
+ }
141
+
142
+ /** Write wood surface shading for a plank/block with bevel and per-plank tone shift. */
143
+ function shadeWoodBlock(
144
+ out: PixelSample,
145
+ ctx: EvalContext,
146
+ along: number,
147
+ across: number,
148
+ plankSeed: number,
149
+ bevelMask: number,
150
+ grainRepeatAcross: number,
151
+ ): void {
152
+ const toneShift = (plankSeed - 0.5) * 0.26
153
+ const warmShift = (hash2D(Math.floor(plankSeed * 1000), 17, ctx.seed) - 0.5) * 0.35
154
+ const uG = fract(along * 0.85 + plankSeed * 3.7)
155
+ const vG = fract(across * 0.75 + plankSeed * 5.3)
156
+ const g = evalWoodGrain(uG, vG, (ctx.seed ^ Math.floor(plankSeed * 0xffff)) >>> 0, 2, 4, grainRepeatAcross)
157
+
158
+ const grainDark = lerp(0.82, 1.14, g.ring) * lerp(0.92, 1.04, g.pore)
159
+ const plankMul = (1 + toneShift) * grainDark
160
+ const baseTone = warmShift >= 0
161
+ ? mixRgb(ctx.baseRgb, ctx.warmTint, warmShift)
162
+ : mixRgb(ctx.baseRgb, ctx.coolTint, -warmShift)
163
+
164
+ // Slight additive lift so very dark woods retain visible grain highlights.
165
+ const lift = (g.ring - 0.5) * 14 + (g.ray - 0.5) * 6
166
+ applyRgbTint(out, baseTone, plankMul, lift)
167
+
168
+ // Darken bevel seams with warm dark wood tone (avoid harsh black ink lines)
169
+ const seamDark = lerp(0.58, 1.0, bevelMask)
170
+ out.r *= seamDark
171
+ out.g *= seamDark
172
+ out.b *= seamDark
173
+
174
+ const poreDip = g.pore < 0.28 ? (0.28 - g.pore) * 0.45 : 0
175
+ const surfH = 0.62 + (g.ring - 0.5) * 0.12 - poreDip + (g.ray - 0.5) * 0.04
176
+ out.height = lerp(0.14, clamp(surfH, 0.35, 0.92), bevelMask)
177
+ out.roughness = clamp(lerp(0.84, 0.38 + (1 - g.ring) * 0.14 + poreDip * 0.3, bevelMask), 0.24, 0.92)
178
+ out.ao = clamp(lerp(0.36, 0.96 - poreDip * 0.25, bevelMask), 0.25, 1.0)
179
+ out.metallic = 0
180
+ }
181
+
182
+ // ============================================================================
183
+ // Concrete / Plaster / Stucco / Drywall (6 patterns)
184
+ // ============================================================================
185
+
186
+ function evalConcretePlate(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
187
+ const cols = 2
188
+ const rows = 2
189
+ const gx = fract(u * cols)
190
+ const gy = fract(v * rows)
191
+ const px = Math.floor(fract(u) * cols)
192
+ const py = Math.floor(fract(v) * rows)
193
+ const panelVar = (hash2D(px, py, ctx.seed) - 0.5) * 0.1
194
+
195
+ // Panel seam grooves along panel borders
196
+ const seamDist = Math.min(gx, 1 - gx, gy, 1 - gy)
197
+ const seamMask = smoothstep(0.008, 0.024, seamDist)
198
+
199
+ // 4 architectural tie-rod cone recesses per panel (symmetric at 0.22 / 0.78)
200
+ const tx = Math.min(Math.abs(gx - 0.22), Math.abs(gx - 0.78))
201
+ const ty = Math.min(Math.abs(gy - 0.25), Math.abs(gy - 0.75))
202
+ const tieDist = Math.hypot(tx, ty)
203
+ const tieOuter = smoothstep(0.022, 0.042, tieDist)
204
+ const tieCore = smoothstep(0.011, 0.022, tieDist)
205
+
206
+ const trowel = periodicFbm2D(u, v, 4, 4, 4, ctx.seed, 0.55)
207
+ const grain = periodicFbm2D(u, v, 16, 16, 3, (ctx.seed + 37) >>> 0, 0.6)
208
+ const pinhole = periodicValueNoise2D(u, v, 36, 36, (ctx.seed + 91) >>> 0)
209
+ const pit = pinhole < 0.08 ? (0.08 - pinhole) * 3.2 : 0
210
+
211
+ const lum = (1 + panelVar + (trowel - 0.5) * 0.16 + (grain - 0.5) * 0.1 - pit * 0.22) *
212
+ lerp(0.68, 1.0, seamMask) *
213
+ lerp(0.55, 1.0, tieCore)
214
+ applyRgbTint(out, ctx.baseRgb, lum)
215
+
216
+ const hPanel = 0.68 + (trowel - 0.5) * 0.14 + (grain - 0.5) * 0.08 - pit * 0.25
217
+ const hSeam = lerp(0.24, hPanel, seamMask)
218
+ out.height = clamp(lerp(0.18, hSeam, tieOuter * 0.6 + tieCore * 0.4), 0.08, 0.95)
219
+ out.roughness = clamp(lerp(0.9, 0.72 + (grain - 0.5) * 0.16 + pit * 0.2, seamMask * tieOuter), 0.55, 0.96)
220
+ out.ao = clamp(lerp(0.45, 0.96 - pit * 0.3, seamMask * tieOuter) * lerp(0.5, 1.0, tieCore), 0.25, 1.0)
221
+ out.metallic = 0
222
+ }
223
+
224
+ function evalConcretePolished(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
225
+ const cloud = periodicDomainWarpFbm2D(u, v, 3, 3, 4, 0.18, ctx.seed)
226
+ const sheen = periodicFbm2D(u, v, 6, 6, 3, (ctx.seed + 53) >>> 0, 0.5)
227
+ const microAgg = periodicVoronoi2D(u, v, 24, 24, (ctx.seed + 131) >>> 0, 0.9)
228
+ const fleck = microAgg.f1 < 0.24 && microAgg.cellId > 0.72 ? (microAgg.cellId - 0.72) * 1.4 : 0
229
+
230
+ const mul = 0.88 + cloud * 0.22 + (sheen - 0.5) * 0.08
231
+ applyRgbTint(out, ctx.baseRgb, mul, fleck * 28 - (1 - cloud) * 6)
232
+ out.height = clamp(0.5 + (cloud - 0.5) * 0.08 + (sheen - 0.5) * 0.05 + fleck * 0.04, 0.35, 0.7)
233
+ out.roughness = clamp(0.24 + (1 - sheen) * 0.18 + (1 - cloud) * 0.08 - fleck * 0.06, 0.14, 0.52)
234
+ out.ao = clamp(0.92 + cloud * 0.08, 0.84, 1.0)
235
+ out.metallic = 0
236
+ }
237
+
238
+ function evalConcreteRaw(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
239
+ const mottle = periodicDomainWarpFbm2D(u, v, 4, 4, 4, 0.14, ctx.seed)
240
+ const coarse = periodicFbm2D(u, v, 10, 10, 4, (ctx.seed + 79) >>> 0, 0.62)
241
+ const pores = periodicVoronoi2D(u, v, 18, 18, (ctx.seed + 211) >>> 0, 0.95)
242
+ const streak = periodicValueNoise2D(u, v, 3, 18, (ctx.seed + 307) >>> 0)
243
+
244
+ const pitMask = pores.cellId > 0.68 ? smoothstep(0.32, 0.12, pores.f1) : 0
245
+ const mul = 0.8 + mottle * 0.26 + (coarse - 0.5) * 0.18 + (streak - 0.5) * 0.08 - pitMask * 0.28
246
+ applyRgbTint(out, ctx.baseRgb, mul)
247
+
248
+ out.height = clamp(0.56 + (mottle - 0.5) * 0.22 + (coarse - 0.5) * 0.22 - pitMask * 0.38, 0.08, 0.95)
249
+ out.roughness = clamp(0.8 + (coarse - 0.5) * 0.16 + pitMask * 0.15, 0.68, 0.98)
250
+ out.ao = clamp(0.94 - pitMask * 0.42 - (1 - mottle) * 0.12, 0.35, 1.0)
251
+ out.metallic = 0
252
+ }
253
+
254
+ function evalPlaster(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
255
+ // Overlapping curved trowel arcs
256
+ const arcWarp = periodicDomainWarpFbm2D(u, v, 3, 3, 4, 0.26, ctx.seed)
257
+ const trowelRidge = periodicRidgedFbm2D(u + arcWarp * 0.08, v - arcWarp * 0.06, 4, 3, 3, (ctx.seed + 83) >>> 0, 0.5)
258
+ const fine = periodicFbm2D(u, v, 14, 14, 3, (ctx.seed + 197) >>> 0, 0.55)
259
+
260
+ const mul = 0.92 + (arcWarp - 0.5) * 0.14 + (trowelRidge - 0.5) * 0.09 + (fine - 0.5) * 0.05
261
+ applyRgbTint(out, ctx.baseRgb, mul)
262
+ out.height = clamp(0.52 + (arcWarp - 0.5) * 0.18 + (trowelRidge - 0.5) * 0.16 + (fine - 0.5) * 0.06, 0.2, 0.88)
263
+ out.roughness = clamp(0.6 + (1 - arcWarp) * 0.16 + (fine - 0.5) * 0.1, 0.46, 0.82)
264
+ out.ao = clamp(0.93 + (trowelRidge - 0.5) * 0.1, 0.8, 1.0)
265
+ out.metallic = 0
266
+ }
267
+
268
+ function evalDrywall(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
269
+ const roller = periodicFbm2D(u, v, 6, 6, 3, ctx.seed, 0.5)
270
+ const orangePeel = periodicFbm2D(u, v, 24, 24, 3, (ctx.seed + 149) >>> 0, 0.6)
271
+ const paperFiber = periodicValueNoise2D(u, v, 40, 20, (ctx.seed + 257) >>> 0)
272
+
273
+ const mul = 0.95 + (roller - 0.5) * 0.08 + (orangePeel - 0.5) * 0.07 + (paperFiber - 0.5) * 0.03
274
+ applyRgbTint(out, ctx.baseRgb, mul)
275
+ out.height = clamp(0.5 + (roller - 0.5) * 0.06 + (orangePeel - 0.5) * 0.1 + (paperFiber - 0.5) * 0.04, 0.35, 0.68)
276
+ out.roughness = clamp(0.76 + (orangePeel - 0.5) * 0.14 + (roller - 0.5) * 0.06, 0.66, 0.92)
277
+ out.ao = clamp(0.95 + (orangePeel - 0.5) * 0.08, 0.86, 1.0)
278
+ out.metallic = 0
279
+ }
280
+
281
+ function evalStucco(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
282
+ const undul = periodicFbm2D(u, v, 5, 5, 3, ctx.seed, 0.55)
283
+ const stipple = periodicVoronoi2D(u, v, 22, 22, (ctx.seed + 67) >>> 0, 0.95)
284
+ const grit = periodicFbm2D(u, v, 28, 28, 3, (ctx.seed + 173) >>> 0, 0.65)
285
+ const bump = clamp(1 - stipple.f1 * 1.35, 0, 1)
286
+
287
+ const mul = 0.84 + undul * 0.18 + bump * 0.14 + (grit - 0.5) * 0.12
288
+ applyRgbTint(out, ctx.baseRgb, mul)
289
+ out.height = clamp(0.25 + undul * 0.25 + bump * 0.34 + grit * 0.16, 0.08, 0.96)
290
+ out.roughness = clamp(0.84 + (1 - bump) * 0.12 + (grit - 0.5) * 0.08, 0.74, 0.98)
291
+ out.ao = clamp(0.72 + bump * 0.22 + undul * 0.08, 0.48, 1.0)
292
+ out.metallic = 0
293
+ }
294
+
295
+ // ============================================================================
296
+ // Fabrics (6 patterns)
297
+ // ============================================================================
298
+
299
+ function evalCotton(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
300
+ const threads = 20 // even so plain weave parity wraps seamlessly
301
+ const gx = fract(u) * threads
302
+ const gy = fract(v) * threads
303
+ const ix = Math.floor(gx)
304
+ const iy = Math.floor(gy)
305
+ const fx = gx - ix
306
+ const fy = gy - iy
307
+
308
+ const warpProfile = Math.sin(Math.PI * fx)
309
+ const weftProfile = Math.sin(Math.PI * fy)
310
+ const warpOver = ((ix + iy) & 1) === 0
311
+
312
+ const warpH = warpProfile * (warpOver ? 0.95 : 0.42)
313
+ const weftH = weftProfile * (warpOver ? 0.42 : 0.95)
314
+ const threadH = Math.max(warpH, weftH)
315
+
316
+ const fold = periodicFbm2D(u, v, 3, 3, 3, ctx.seed, 0.5)
317
+ const yarnNoise = periodicFbm2D(u, v, 24, 24, 2, (ctx.seed + 41) >>> 0, 0.55)
318
+
319
+ const mul = 0.8 + threadH * 0.24 + (fold - 0.5) * 0.14 + (yarnNoise - 0.5) * 0.08
320
+ applyRgbTint(out, ctx.baseRgb, mul, (threadH - 0.5) * 8)
321
+
322
+ out.height = clamp(0.18 + threadH * 0.64 + (fold - 0.5) * 0.12 + (yarnNoise - 0.5) * 0.06, 0.08, 0.96)
323
+ out.roughness = clamp(0.82 + (1 - threadH) * 0.12 + (yarnNoise - 0.5) * 0.06, 0.74, 0.96)
324
+ out.ao = clamp(0.62 + threadH * 0.36, 0.42, 1.0)
325
+ out.metallic = 0
326
+ }
327
+
328
+ function evalLinen(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
329
+ const threads = 16 // coarser visible criss-cross weave
330
+ const gx = fract(u) * threads
331
+ const gy = fract(v) * threads
332
+ const ix = Math.floor(gx)
333
+ const iy = Math.floor(gy)
334
+ const fx = gx - ix
335
+ const fy = gy - iy
336
+
337
+ // Organic slub thickness modulation along each thread
338
+ const warpSlub = 0.78 + 0.44 * periodicValueNoise2D(u, v, threads, 4, (ctx.seed + 11) >>> 0)
339
+ const weftSlub = 0.78 + 0.44 * periodicValueNoise2D(u, v, 4, threads, (ctx.seed + 29) >>> 0)
340
+
341
+ const warpOver = ((ix + iy) & 1) === 0
342
+ const warpProf = Math.pow(Math.sin(Math.PI * fx), 0.75) * warpSlub
343
+ const weftProf = Math.pow(Math.sin(Math.PI * fy), 0.75) * weftSlub
344
+
345
+ const hWarp = warpProf * (warpOver ? 0.82 : 0.36)
346
+ const hWeft = weftProf * (warpOver ? 0.36 : 0.82)
347
+ const weaveH = clamp(Math.max(hWarp, hWeft), 0, 1)
348
+
349
+ const dyeVar = periodicFbm2D(u, v, 4, 4, 3, (ctx.seed + 97) >>> 0, 0.5)
350
+ const threadTone = (hash2D(warpOver ? ix : iy, warpOver ? 0 : 1, ctx.seed) - 0.5) * 0.12
351
+
352
+ const mul = 0.78 + weaveH * 0.28 + threadTone + (dyeVar - 0.5) * 0.12
353
+ applyRgbTint(out, ctx.baseRgb, mul, (weaveH - 0.5) * 10)
354
+
355
+ out.height = clamp(0.15 + weaveH * 0.72 + (dyeVar - 0.5) * 0.08, 0.06, 0.96)
356
+ out.roughness = clamp(0.8 + (1 - weaveH) * 0.14 + (dyeVar - 0.5) * 0.06, 0.72, 0.96)
357
+ out.ao = clamp(0.56 + weaveH * 0.42, 0.38, 1.0)
358
+ out.metallic = 0
359
+ }
360
+
361
+ function evalVelvet(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
362
+ // Plush fine velvet upholstery: tight directional micro-pile with subtle soft grazing nap sheen (no blobby clouds)
363
+ const softFold = periodicFbm2D(u, v, 2, 2, 3, ctx.seed, 0.45)
364
+ const finePile = periodicFbm2D(u, v, 16, 44, 3, (ctx.seed + 61) >>> 0, 0.58)
365
+ const microNap = periodicValueNoise2D(u, v, 56, 56, (ctx.seed + 137) >>> 0)
366
+
367
+ // Tight tonal range around rich base hue so velvet reads luxurious & uniform, never raw-meat blotchy
368
+ const sheen = clamp(0.45 + (softFold - 0.5) * 0.36 + (finePile - 0.5) * 0.24, 0, 1)
369
+ const mul = 0.88 + sheen * 0.2 + (microNap - 0.5) * 0.05
370
+ applyRgbTint(out, ctx.baseRgb, mul, (sheen - 0.5) * 10)
371
+
372
+ out.height = clamp(0.46 + (softFold - 0.5) * 0.14 + (finePile - 0.5) * 0.22 + (microNap - 0.5) * 0.12, 0.2, 0.86)
373
+ out.roughness = clamp(0.56 + (1 - sheen) * 0.22 + (microNap - 0.5) * 0.08, 0.44, 0.84)
374
+ out.ao = clamp(0.86 + sheen * 0.12, 0.74, 1.0)
375
+ out.metallic = 0
376
+ }
377
+
378
+ function evalSuede(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
379
+ const brushPatch = periodicDomainWarpFbm2D(u, v, 4, 4, 3, 0.22, ctx.seed)
380
+ const microNap = periodicFbm2D(u, v, 18, 30, 3, (ctx.seed + 89) >>> 0, 0.62)
381
+ const grain = periodicValueNoise2D(u, v, 36, 36, (ctx.seed + 181) >>> 0)
382
+
383
+ const mul = 0.82 + brushPatch * 0.24 + (microNap - 0.5) * 0.14 + (grain - 0.5) * 0.06
384
+ applyRgbTint(out, ctx.baseRgb, mul, (brushPatch - 0.5) * 10)
385
+
386
+ out.height = clamp(0.4 + brushPatch * 0.22 + (microNap - 0.5) * 0.22 + (grain - 0.5) * 0.1, 0.16, 0.88)
387
+ out.roughness = clamp(0.78 + (1 - brushPatch) * 0.14 + (grain - 0.5) * 0.06, 0.68, 0.96)
388
+ out.ao = clamp(0.82 + brushPatch * 0.14 + (microNap - 0.5) * 0.06, 0.68, 1.0)
389
+ out.metallic = 0
390
+ }
391
+
392
+ function evalWool(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
393
+ // Knitted ribbed V-stitch wool columns
394
+ const cols = 12
395
+ const rows = 16
396
+ const gx = fract(u) * cols
397
+ const gy = fract(v) * rows
398
+ const fx = gx - Math.floor(gx)
399
+ const fy = gy - Math.floor(gy)
400
+
401
+ // Symmetric V-chevron loop inside each stitch column
402
+ const halfX = Math.abs(fx - 0.5) * 2
403
+ const vPhase = fract(fy + halfX * 0.55)
404
+ const loopRidge = Math.sin(Math.PI * vPhase)
405
+ const colSplit = smoothstep(0.0, 0.22, Math.min(fx, 1 - fx)) * smoothstep(0.0, 0.14, Math.abs(fx - 0.5))
406
+ const knitH = loopRidge * (0.45 + 0.55 * colSplit)
407
+
408
+ const heather = periodicFbm2D(u, v, 8, 8, 3, ctx.seed, 0.58)
409
+ const fuzz = periodicFbm2D(u, v, 28, 28, 2, (ctx.seed + 157) >>> 0, 0.65)
410
+
411
+ const mul = 0.78 + knitH * 0.26 + (heather - 0.5) * 0.14 + (fuzz - 0.5) * 0.1
412
+ applyRgbTint(out, ctx.baseRgb, mul)
413
+
414
+ out.height = clamp(0.16 + knitH * 0.64 + (fuzz - 0.5) * 0.14 + (heather - 0.5) * 0.08, 0.06, 0.96)
415
+ out.roughness = clamp(0.86 + (1 - knitH) * 0.1 + (fuzz - 0.5) * 0.06, 0.78, 0.98)
416
+ out.ao = clamp(0.58 + knitH * 0.38, 0.38, 1.0)
417
+ out.metallic = 0
418
+ }
419
+
420
+ function evalBoucle(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
421
+ // 100%-coverage dense overlapping curled wool bouclé upholstery (no dark sparse voids)
422
+ const loopsA = periodicVoronoi2D(u, v, 24, 24, ctx.seed, 0.92)
423
+ const loopsB = periodicVoronoi2D(u + 0.029, v + 0.043, 36, 36, (ctx.seed + 199) >>> 0, 0.94)
424
+ const loopsC = periodicVoronoi2D(u + 0.061, v + 0.017, 48, 48, (ctx.seed + 401) >>> 0, 0.95)
425
+ // Soft full-coverage curled yarn crowns (broad overlap prevents dark void pockets)
426
+ const nubA = smoothstep(0.76, 0.14, loopsA.f1)
427
+ const nubB = smoothstep(0.74, 0.16, loopsB.f1)
428
+ const nubC = smoothstep(0.72, 0.18, loopsC.f1)
429
+ const nub = Math.max(nubA * 0.95, nubB * 0.86, nubC * 0.76)
430
+ const yarnTone = (loopsA.cellId - 0.5) * 0.12 + (loopsB.cellId - 0.5) * 0.08
431
+ const weaveBase = periodicFbm2D(u, v, 28, 28, 3, (ctx.seed + 317) >>> 0, 0.6)
432
+
433
+ const mul = 0.84 + nub * 0.24 + yarnTone + (weaveBase - 0.5) * 0.08
434
+ applyRgbTint(out, ctx.baseRgb, mul, nub * 7)
435
+
436
+ out.height = clamp(0.24 + nub * 0.66 + (weaveBase - 0.5) * 0.1, 0.12, 0.96)
437
+ out.roughness = clamp(0.84 + (1 - nub) * 0.12 + (weaveBase - 0.5) * 0.05, 0.78, 0.98)
438
+ out.ao = clamp(0.68 + nub * 0.3, 0.52, 1.0)
439
+ out.metallic = 0
440
+ }
441
+
442
+ // ============================================================================
443
+ // Leather (1 pattern)
444
+ // ============================================================================
445
+
446
+ function evalLeather(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
447
+ // Fine full-grain aniline leather: crisp micro-pebble grain readable at 512px without mud-cracks
448
+ const hideSupple = periodicDomainWarpFbm2D(u, v, 4, 4, 3, 0.14, (ctx.seed + 19) >>> 0)
449
+ const uw = u + (hideSupple - 0.5) * 0.014
450
+ const vw = v + (periodicFbm2D(u, v, 6, 6, 2, (ctx.seed + 43) >>> 0, 0.5) - 0.5) * 0.014
451
+
452
+ // High-frequency tactile micro-pebble hide grain (scale 44x44) + subtle pore relief
453
+ const pebble = periodicVoronoi2D(uw, vw, 44, 44, ctx.seed, 0.88)
454
+ const microGrain = periodicFbm2D(u, v, 32, 32, 2, (ctx.seed + 263) >>> 0, 0.55)
455
+
456
+ const pebbleCrown = smoothstep(0.02, 0.25, pebble.edgeDist)
457
+ const dome = Math.pow(pebbleCrown, 0.6)
458
+ const grainVar = (pebble.cellId - 0.5) * 0.07
459
+
460
+ // Balanced 5-8% pebble albedo contrast + tactile normal/roughness micro-relief
461
+ const mul = 0.88 + dome * 0.17 + (hideSupple - 0.5) * 0.08 + grainVar
462
+ const crownLift = dome * 10 + (microGrain - 0.5) * 4
463
+ applyRgbTint(out, ctx.baseRgb, mul, crownLift)
464
+
465
+ out.height = clamp(0.3 + dome * 0.54 + (hideSupple - 0.5) * 0.1 + (microGrain - 0.5) * 0.06, 0.12, 0.94)
466
+ out.roughness = clamp(0.32 + (1 - pebbleCrown) * 0.24 + (1 - hideSupple) * 0.08 + grainVar * 0.25, 0.24, 0.66)
467
+ out.ao = clamp(0.78 + pebbleCrown * 0.2 + (hideSupple - 0.5) * 0.04, 0.62, 1.0)
468
+ out.metallic = 0
469
+ }
470
+
471
+ // ============================================================================
472
+ // Metals (3 patterns)
473
+ // ============================================================================
474
+
475
+ function evalCopper(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
476
+ // High-frequency fine anisotropic brushed architectural copper (zero wide wood-grain bands)
477
+ const hairline = periodicFbm2D(u, v, 2, 96, 3, ctx.seed, 0.62)
478
+ const microBrush = periodicValueNoise2D(u, v, 4, 144, (ctx.seed + 73) >>> 0)
479
+ const rollSheen = periodicFbm2D(u, v, 3, 4, 2, (ctx.seed + 191) >>> 0, 0.45)
480
+
481
+ const brush = hairline * 0.6 + microBrush * 0.4
482
+ const mul = 0.94 + (brush - 0.5) * 0.1 + (rollSheen - 0.5) * 0.05
483
+ applyRgbTint(out, ctx.baseRgb, mul, (brush - 0.5) * 6)
484
+
485
+ out.height = clamp(0.48 + (brush - 0.5) * 0.24 + (rollSheen - 0.5) * 0.04, 0.24, 0.78)
486
+ out.roughness = clamp(0.22 + (1 - brush) * 0.1 + (rollSheen - 0.5) * 0.03, 0.16, 0.34)
487
+ out.ao = clamp(0.94 + (brush - 0.5) * 0.06, 0.86, 1.0)
488
+ out.metallic = clamp(0.96 + (brush - 0.5) * 0.03, 0.92, 0.99)
489
+ }
490
+
491
+ function evalPolishedMetal(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
492
+ const roll = periodicFbm2D(u, v, 3, 6, 3, ctx.seed, 0.5)
493
+ const microPolish = periodicFbm2D(u, v, 4, 28, 2, (ctx.seed + 109) >>> 0, 0.5)
494
+ const haze = periodicValueNoise2D(u, v, 10, 10, (ctx.seed + 227) >>> 0)
495
+
496
+ // Slight tonal modulation below 255 so bright chrome retains subtle specular sheen variance
497
+ const mul = 0.91 + roll * 0.08 + (microPolish - 0.5) * 0.05
498
+ applyRgbTint(out, ctx.baseRgb, mul)
499
+
500
+ out.height = clamp(0.5 + (roll - 0.5) * 0.09 + (microPolish - 0.5) * 0.06, 0.36, 0.66)
501
+ out.roughness = clamp(0.09 + (1 - microPolish) * 0.06 + haze * 0.04, 0.06, 0.22)
502
+ out.ao = clamp(0.95 + (roll - 0.5) * 0.06, 0.88, 1.0)
503
+ out.metallic = clamp(0.94 + (microPolish - 0.5) * 0.04, 0.9, 0.98)
504
+ }
505
+
506
+ function evalBrushedMetal(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
507
+ // Strongly anisotropic horizontal brush lines
508
+ const fineStreak = periodicFbm2D(u, v, 2, 48, 3, ctx.seed, 0.62)
509
+ const medStreak = periodicFbm2D(u, v, 1, 20, 2, (ctx.seed + 89) >>> 0, 0.55)
510
+ const band = periodicValueNoise2D(u, v, 2, 8, (ctx.seed + 233) >>> 0)
511
+
512
+ const streak = fineStreak * 0.68 + medStreak * 0.32
513
+ const mul = 0.84 + streak * 0.28 + (band - 0.5) * 0.08
514
+ applyRgbTint(out, ctx.baseRgb, mul, (streak - 0.5) * 14)
515
+
516
+ out.height = clamp(0.4 + streak * 0.34 + (band - 0.5) * 0.08, 0.16, 0.86)
517
+ out.roughness = clamp(0.28 + (1 - streak) * 0.14 + (band - 0.5) * 0.04, 0.2, 0.46)
518
+ out.ao = clamp(0.9 + (streak - 0.5) * 0.12, 0.78, 1.0)
519
+ out.metallic = clamp(0.89 + (streak - 0.5) * 0.06, 0.84, 0.95)
520
+ }
521
+
522
+ // ============================================================================
523
+ // Roofing (2 patterns)
524
+ // ============================================================================
525
+
526
+ function evalRoofShingles(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
527
+ const rows = 8 // even integer so half-stagger wraps cleanly
528
+ const cols = 4
529
+ const gv = fract(v) * rows
530
+ const row = Math.floor(gv)
531
+ const lv = gv - row // 0 = top of course, 1 = bottom overlap lip
532
+
533
+ const stagger = (row & 1) === 1 ? 0.5 : 0
534
+ const gu = fract(u) * cols + stagger
535
+ const col = wrapInt(Math.floor(gu), cols)
536
+ const lu = fract(gu)
537
+
538
+ // Keyway vertical slot between tabs + horizontal overlap step at course bottom
539
+ const keyway = smoothstep(0.015, 0.05, Math.min(lu, 1 - lu))
540
+ const overlapShadow = smoothstep(0.0, 0.18, lv) // dark underside shadow right below upper course
541
+ const bottomLip = smoothstep(1.0, 0.93, lv)
542
+
543
+ const shingleId = hash2D(col, row, ctx.seed)
544
+ const toneShift = (shingleId - 0.5) * 0.24
545
+ const granules = periodicFbm2D(u, v, 32, 32, 3, (ctx.seed + 131) >>> 0, 0.65)
546
+ const weather = periodicFbm2D(u, v, 6, 6, 3, (ctx.seed + 251) >>> 0, 0.55)
547
+
548
+ // Architectural shingle bevel slopes slightly higher toward bottom overlap edge
549
+ const tabHeight = 0.44 + lv * 0.32 + (granules - 0.5) * 0.14 + (shingleId - 0.5) * 0.06
550
+ const h = lerp(0.14, tabHeight, keyway) * lerp(0.65, 1.0, bottomLip)
551
+
552
+ const shadowMul = lerp(0.56, 1.0, overlapShadow) * lerp(0.45, 1.0, keyway)
553
+ const mul = (0.9 + toneShift + (granules - 0.5) * 0.2 + (weather - 0.5) * 0.14) * shadowMul
554
+ applyRgbTint(out, ctx.baseRgb, mul, (granules - 0.5) * 10)
555
+
556
+ out.height = clamp(h, 0.06, 0.96)
557
+ out.roughness = clamp(0.8 + (granules - 0.5) * 0.14 + (1 - keyway) * 0.1, 0.72, 0.97)
558
+ out.ao = clamp(lerp(0.36, 0.96, overlapShadow * keyway) - (1 - granules) * 0.08, 0.24, 1.0)
559
+ out.metallic = 0
560
+ }
561
+
562
+ function evalRoofTiles(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
563
+ const cols = 6 // barrel arches across X
564
+ const rows = 5 // overlapping courses down Y
565
+ const gu = fract(u) * cols
566
+ const gv = fract(v) * rows
567
+ const col = Math.floor(gu)
568
+ const row = Math.floor(gv)
569
+ const lu = gu - col
570
+ const lv = gv - row
571
+
572
+ // Sinusoidal barrel / Spanish clay tile arch
573
+ const barrel = 0.5 + 0.5 * Math.cos(TWO_PI * (lu - 0.5)) // 1 at crown (0.5), 0 at valley (0/1)
574
+ const archProfile = Math.pow(barrel, 0.75)
575
+
576
+ // Course overlap lip at lv near 0 (shadow cast by tile above) and raised butt at lv near 1
577
+ const courseStep = smoothstep(0.0, 0.14, lv)
578
+ const courseSlope = 0.55 + lv * 0.45
579
+
580
+ const tileVar = (hash2D(col, row, ctx.seed) - 0.5) * 0.2
581
+ const clayGrain = periodicFbm2D(u, v, 14, 14, 3, (ctx.seed + 79) >>> 0, 0.58)
582
+ const lichen = periodicFbm2D(u, v, 6, 6, 3, (ctx.seed + 223) >>> 0, 0.5)
583
+
584
+ const h = (0.18 + archProfile * 0.6) * courseSlope * lerp(0.62, 1.0, courseStep) + (clayGrain - 0.5) * 0.06
585
+ const valleyShade = 0.78 + 0.22 * archProfile
586
+ const overlapShade = lerp(0.68, 1.0, courseStep)
587
+ const mul = (0.96 + tileVar + (clayGrain - 0.5) * 0.14 + (lichen - 0.5) * 0.12) * valleyShade * overlapShade
588
+ applyRgbTint(out, ctx.baseRgb, mul, archProfile * 10)
589
+
590
+ out.height = clamp(h, 0.08, 0.98)
591
+ out.roughness = clamp(0.64 + (1 - archProfile) * 0.14 + (clayGrain - 0.5) * 0.12, 0.52, 0.88)
592
+ out.ao = clamp(lerp(0.46, 0.98, courseStep) * (0.7 + 0.3 * archProfile), 0.32, 1.0)
593
+ out.metallic = 0
594
+ }
595
+
596
+ // ============================================================================
597
+ // Siding (2 patterns)
598
+ // ============================================================================
599
+
600
+ function evalBoardBatten(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
601
+ const boards = 4
602
+ const gu = fract(u) * boards
603
+ const boardIdx = Math.floor(gu)
604
+ const lu = gu - boardIdx
605
+
606
+ // Narrow raised vertical batten centered at lu = 0 / 1 (edges of each board cycle)
607
+ const distFromBattenCenter = Math.min(lu, 1 - lu) // 0 at center of batten
608
+ const battenHalfW = 0.095
609
+ const battenTop = smoothstep(battenHalfW + 0.025, battenHalfW - 0.015, distFromBattenCenter)
610
+ const battenShadow = smoothstep(battenHalfW + 0.005, battenHalfW + 0.075, distFromBattenCenter)
611
+
612
+ const boardVar = (hash2D(boardIdx, 0, ctx.seed) - 0.5) * 0.07
613
+ // Subtle vertical wood grain under exterior paint
614
+ const grain = evalWoodGrain(v, u, (ctx.seed + boardIdx * 37) >>> 0, 2, 10, 6)
615
+ const paintMicro = periodicFbm2D(u, v, 16, 16, 2, (ctx.seed + 193) >>> 0, 0.5)
616
+
617
+ const edgeShade = lerp(1.0, lerp(0.76, 1.0, battenShadow), 1 - battenTop)
618
+ const battenLift = battenTop * 0.05
619
+ const mul = (0.95 + boardVar + battenLift + (grain.ring - 0.5) * 0.07 + (paintMicro - 0.5) * 0.04) * edgeShade
620
+ applyRgbTint(out, ctx.baseRgb, mul)
621
+
622
+ const baseH = 0.34 + (grain.ring - 0.5) * 0.05 + (paintMicro - 0.5) * 0.03
623
+ out.height = clamp(lerp(baseH, 0.84 + (grain.ring - 0.5) * 0.04, battenTop), 0.15, 0.96)
624
+ out.roughness = clamp(0.62 + (1 - grain.ring) * 0.1 + (paintMicro - 0.5) * 0.08, 0.48, 0.82)
625
+ out.ao = clamp(lerp(lerp(0.58, 0.97, battenShadow), 0.99, battenTop), 0.45, 1.0)
626
+ out.metallic = 0
627
+ }
628
+
629
+ function evalLapSiding(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
630
+ const courses = 6
631
+ const gv = fract(v) * courses
632
+ const courseIdx = Math.floor(gv)
633
+ const lv = gv - courseIdx // 0 = top tucked under upper plank, 1 = bottom thick exposed lip
634
+
635
+ // Clapboard ramps outward from lv=0 to lv=0.92 then drops sharply at lv=0.92..1.0
636
+ const dropStep = smoothstep(1.0, 0.91, lv)
637
+ const underShadow = smoothstep(0.0, 0.18, lv)
638
+ const courseVar = (hash2D(0, courseIdx, ctx.seed) - 0.5) * 0.07
639
+ const grain = evalWoodGrain(u, v, (ctx.seed + courseIdx * 53) >>> 0, 2, 12, 6)
640
+ const paintMicro = periodicFbm2D(u, v, 18, 18, 2, (ctx.seed + 167) >>> 0, 0.5)
641
+
642
+ const h = (0.24 + lv * 0.62) * lerp(0.32, 1.0, dropStep) + (grain.ring - 0.5) * 0.04
643
+ const mul = (0.94 + courseVar + (grain.ring - 0.5) * 0.06 + (paintMicro - 0.5) * 0.04) *
644
+ lerp(0.68, 1.0, underShadow) *
645
+ lerp(0.75, 1.0, dropStep)
646
+ applyRgbTint(out, ctx.baseRgb, mul)
647
+
648
+ out.height = clamp(h, 0.08, 0.96)
649
+ out.roughness = clamp(0.6 + (1 - grain.ring) * 0.1 + (paintMicro - 0.5) * 0.08, 0.46, 0.82)
650
+ out.ao = clamp(lerp(0.48, 0.98, underShadow) * lerp(0.72, 1.0, dropStep), 0.35, 1.0)
651
+ out.metallic = 0
652
+ }
653
+
654
+ // ============================================================================
655
+ // Wood (5 patterns)
656
+ // ============================================================================
657
+
658
+ function evalWoodFine(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
659
+ const g = evalWoodGrain(u, v, ctx.seed, 2, 6, 10)
660
+ const macroTone = periodicFbm2D(u, v, 2, 4, 3, (ctx.seed + 139) >>> 0, 0.5)
661
+
662
+ const baseTone = macroTone > 0.5
663
+ ? mixRgb(ctx.baseRgb, ctx.warmTint, (macroTone - 0.5) * 0.32)
664
+ : mixRgb(ctx.baseRgb, ctx.coolTint, (0.5 - macroTone) * 0.25)
665
+
666
+ const mul = lerp(0.82, 1.15, g.ring) * lerp(0.92, 1.05, g.pore) * (0.94 + macroTone * 0.12)
667
+ applyRgbTint(out, baseTone, mul, (g.ring - 0.5) * 12 + (g.ray - 0.5) * 6)
668
+
669
+ const poreDip = g.pore < 0.3 ? (0.3 - g.pore) * 0.5 : 0
670
+ out.height = clamp(0.56 + (g.ring - 0.5) * 0.2 - poreDip + (g.ray - 0.5) * 0.06, 0.2, 0.9)
671
+ out.roughness = clamp(0.4 + (1 - g.ring) * 0.16 + poreDip * 0.28, 0.28, 0.68)
672
+ out.ao = clamp(0.95 - poreDip * 0.32 - (1 - g.ring) * 0.06, 0.72, 1.0)
673
+ out.metallic = 0
674
+ }
675
+
676
+ function evalWoodPlank(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
677
+ const rows = 6
678
+ const cols = 2
679
+ const gv = fract(v) * rows
680
+ const row = Math.floor(gv)
681
+ const lv = gv - row
682
+
683
+ // Rational deterministic row stagger (in multiples of 0.25) keeps X strictly 1-periodic
684
+ const rowOffset = Math.floor(hash2D(row, 3, ctx.seed) * 4) * 0.25
685
+ const gu = fract(u + rowOffset) * cols
686
+ const col = wrapInt(Math.floor(gu), cols)
687
+ const lu = gu - Math.floor(gu)
688
+
689
+ const plankSeed = hash2D(col, row, (ctx.seed + 41) >>> 0)
690
+
691
+ // Micro-bevel joints along plank sides and butt ends
692
+ const sideBevel = smoothstep(0.012, 0.055, Math.min(lv, 1 - lv))
693
+ const endBevel = smoothstep(0.005, 0.022, Math.min(lu, 1 - lu))
694
+ const bevelMask = sideBevel * endBevel
695
+
696
+ shadeWoodBlock(out, ctx, lu, lv, plankSeed, bevelMask, 5)
697
+ }
698
+
699
+ function evalWoodParquet(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
700
+ // Exact periodic 90-degree herringbone lattice: L = 3 aspect ratio, M = 12 (2 full cycles)
701
+ const L = 3
702
+ const M = 12
703
+ const gx = fract(u) * M
704
+ const gy = fract(v) * M
705
+ const ix = Math.floor(gx)
706
+ const iy = Math.floor(gy)
707
+ const fx = gx - ix
708
+ const fy = gy - iy
709
+
710
+ const d = wrapInt(ix - iy, 2 * L)
711
+ let along: number
712
+ let across: number
713
+ let plankSeed: number
714
+
715
+ if (d < L) {
716
+ // Horizontal block spanning [ix - d .. ix - d + L - 1]
717
+ const anchorX = wrapInt(ix - d, M)
718
+ const anchorY = wrapInt(iy, M)
719
+ along = (d + fx) / L
720
+ across = fy
721
+ plankSeed = hash2D(anchorX, anchorY, ctx.seed)
722
+ } else {
723
+ // Vertical block spanning [iy - vIdx .. iy - vIdx + L - 1]
724
+ const vIdx = 2 * L - 1 - d
725
+ const anchorX = wrapInt(ix, M)
726
+ const anchorY = wrapInt(iy - vIdx, M)
727
+ along = (vIdx + fy) / L
728
+ across = fx
729
+ plankSeed = hash2D(anchorX, anchorY, (ctx.seed ^ 0x243f6a88) >>> 0)
730
+ }
731
+
732
+ // Uniform physical bevel width in cell units on all 4 block edges
733
+ const distAlong = Math.min(along, 1 - along) * L
734
+ const distAcross = Math.min(across, 1 - across)
735
+ const edgeDist = Math.min(distAlong, distAcross)
736
+ const bevelMask = smoothstep(0.025, 0.09, edgeDist)
737
+
738
+ shadeWoodBlock(out, ctx, along, across, plankSeed, bevelMask, 4)
739
+ }
740
+
741
+ function evalWoodChevron(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
742
+ // Exact periodic 45-degree Hungarian chevron parquet (wider refined planks, crisp <2% micro-bevels)
743
+ const cols = 4
744
+ const rows = 6
745
+ const ux = fract(u) * cols
746
+ const col = Math.floor(ux)
747
+ const cu = ux - col
748
+ const isEven = (col & 1) === 0
749
+
750
+ const along = isEven ? cu : 1 - cu
751
+ const slanted = fract(v) * rows + along * (rows / cols)
752
+ const rowIdx = wrapInt(Math.floor(slanted), rows)
753
+ const sv = fract(slanted)
754
+
755
+ const plankSeed = hash2D(col, rowIdx, (ctx.seed + 107) >>> 0)
756
+
757
+ // Tight architectural micro-bevel seam (<2% of plank width)
758
+ const miterDist = Math.min(cu, 1 - cu) / cols
759
+ const sideDist = (Math.min(sv, 1 - sv) / rows) * 0.70710678
760
+ const bevelMask = smoothstep(0.0012, 0.006, Math.min(miterDist, sideDist))
761
+
762
+ shadeWoodBlock(out, ctx, along, sv, plankSeed, bevelMask, 4)
763
+ }
764
+
765
+ function evalWoodSquareParquet(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
766
+ // Alternating horizontal / vertical basketweave square parquet tiles (even 4x4 grid)
767
+ const grid = 4
768
+ const fingers = 5
769
+ const gx = fract(u) * grid
770
+ const gy = fract(v) * grid
771
+ const sx = Math.floor(gx)
772
+ const sy = Math.floor(gy)
773
+ const lx = gx - sx
774
+ const ly = gy - sy
775
+
776
+ const horizontal = ((sx + sy) & 1) === 0
777
+ const along = horizontal ? lx : ly
778
+ const acrossFull = horizontal ? ly : lx
779
+ const fingerIdx = Math.floor(acrossFull * fingers)
780
+ const across = fract(acrossFull * fingers)
781
+
782
+ const plankSeed = hash2D(sx * fingers + (horizontal ? 0 : fingerIdx), sy * fingers + (horizontal ? fingerIdx : 0), ctx.seed)
783
+
784
+ // Slightly deeper square perimeter seam + crisp finger joints
785
+ const sqEdge = Math.min(lx, 1 - lx, ly, 1 - ly)
786
+ const fgEdge = Math.min(across, 1 - across) / fingers
787
+ const bevelMask = smoothstep(0.008, 0.032, sqEdge) * smoothstep(0.006, 0.022, fgEdge)
788
+
789
+ shadeWoodBlock(out, ctx, along, across, plankSeed, bevelMask, 3)
790
+ }
791
+
792
+ // ============================================================================
793
+ // Flooring / Masonry / Stone / Tiles (11 patterns)
794
+ // ============================================================================
795
+
796
+ function evalBrick(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
797
+ const rows = 8 // even for seamless running bond
798
+ const cols = 4
799
+
800
+ // Organic periodic fBM edge wobble for natural kiln-fired rustic brick silhouette
801
+ const wobbleU = (periodicFbm2D(u, v, 14, 14, 3, (ctx.seed + 13) >>> 0) - 0.5) * 0.018
802
+ const wobbleV = (periodicFbm2D(u, v, 14, 14, 3, (ctx.seed + 37) >>> 0) - 0.5) * 0.013
803
+
804
+ const gv = fract(v + wobbleV) * rows
805
+ const row = Math.floor(gv)
806
+ const lv = gv - row
807
+
808
+ const stagger = (row & 1) === 1 ? 0.5 : 0
809
+ const gu = fract(u + wobbleU) * cols + stagger
810
+ const col = wrapInt(Math.floor(gu), cols)
811
+ const lu = fract(gu)
812
+
813
+ // Mortar joints: realistic weathered warm sandy lime-cement mortar
814
+ const mortarV = smoothstep(0.036, 0.098, Math.min(lv, 1 - lv))
815
+ const mortarU = smoothstep(0.018, 0.048, Math.min(lu, 1 - lu))
816
+ const brickMask = mortarU * mortarV
817
+
818
+ const brickId = hash2D(col, row, ctx.seed)
819
+ const claySurf = periodicFbm2D(u, v, 16, 16, 4, (ctx.seed + 101) >>> 0, 0.62)
820
+ const pit = periodicVoronoi2D(u, v, 24, 24, (ctx.seed + 181) >>> 0, 0.9)
821
+ const mortarGrain = periodicFbm2D(u, v, 28, 28, 3, (ctx.seed + 269) >>> 0, 0.65)
822
+
823
+ // Kiln-varied brick hue/value around previewColor
824
+ const brickTone = brickId > 0.5
825
+ ? mixRgb(ctx.baseRgb, ctx.warmTint, (brickId - 0.5) * 0.45)
826
+ : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - brickId) * 0.45)
827
+
828
+ const pitDip = pit.cellId > 0.72 ? smoothstep(0.28, 0.1, pit.f1) * 0.2 : 0
829
+ const brickMul = 0.84 + claySurf * 0.28 + (brickId - 0.5) * 0.16 - pitDip
830
+ const brR = brickTone[0] * brickMul
831
+ const brG = brickTone[1] * brickMul
832
+ const brB = brickTone[2] * brickMul
833
+
834
+ const warmSandyMortar = mixRgb(ctx.groutRgb, [172, 162, 148], 0.58)
835
+ const mortarMul = 0.82 + mortarGrain * 0.24
836
+ const mR = warmSandyMortar[0] * mortarMul
837
+ const mG = warmSandyMortar[1] * mortarMul
838
+ const mB = warmSandyMortar[2] * mortarMul
839
+
840
+ setRgb(out, lerp(mR, brR, brickMask), lerp(mG, brG, brickMask), lerp(mB, brB, brickMask))
841
+
842
+ const hBrick = 0.64 + (claySurf - 0.5) * 0.22 - pitDip * 0.45 + (brickId - 0.5) * 0.08
843
+ const hMortar = 0.18 + (mortarGrain - 0.5) * 0.1
844
+ out.height = clamp(lerp(hMortar, hBrick, brickMask), 0.06, 0.98)
845
+ out.roughness = clamp(lerp(0.93, 0.8 + (1 - claySurf) * 0.14 + pitDip * 0.2, brickMask), 0.68, 0.98)
846
+ out.ao = clamp(lerp(0.42, 0.96 - pitDip * 0.35, brickMask), 0.28, 1.0)
847
+ out.metallic = 0
848
+ }
849
+
850
+ function evalStoneWall(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
851
+ // Irregular coursed rubble / ashlar stone blocks via domain-warped stretched Voronoi
852
+ const warpU = (periodicFbm2D(u, v, 6, 6, 3, (ctx.seed + 17) >>> 0) - 0.5) * 0.036
853
+ const warpV = (periodicFbm2D(u, v, 6, 6, 3, (ctx.seed + 59) >>> 0) - 0.5) * 0.024
854
+
855
+ const cell = periodicVoronoi2D(u + warpU, v + warpV, 5, 8, ctx.seed, 0.82)
856
+ const stoneMask = smoothstep(0.022, 0.075, cell.edgeDist)
857
+
858
+ const strata = periodicRidgedFbm2D(u, v, 8, 12, 4, (ctx.seed + 149) >>> 0, 0.58)
859
+ const grit = periodicFbm2D(u, v, 20, 20, 3, (ctx.seed + 271) >>> 0, 0.6)
860
+ const stoneTone = cell.cellId > 0.5
861
+ ? mixRgb(ctx.baseRgb, ctx.warmTint, (cell.cellId - 0.5) * 0.45)
862
+ : mixRgb(ctx.baseRgb, ctx.coolTint, (0.5 - cell.cellId) * 0.45)
863
+
864
+ const stoneMul = 0.78 + strata * 0.3 + (grit - 0.5) * 0.14 + (cell.cellId - 0.5) * 0.22
865
+ const naturalMortar = mixRgb(ctx.groutRgb, [132, 128, 120], 0.68)
866
+ const mortarMul = 0.78 + grit * 0.22
867
+
868
+ setRgb(
869
+ out,
870
+ lerp(naturalMortar[0] * mortarMul, stoneTone[0] * stoneMul, stoneMask),
871
+ lerp(naturalMortar[1] * mortarMul, stoneTone[1] * stoneMul, stoneMask),
872
+ lerp(naturalMortar[2] * mortarMul, stoneTone[2] * stoneMul, stoneMask),
873
+ )
874
+
875
+ const hStone = 0.52 + strata * 0.32 + (grit - 0.5) * 0.12 + (cell.cellId - 0.5) * 0.12
876
+ const hMortar = 0.14 + (grit - 0.5) * 0.08
877
+ out.height = clamp(lerp(hMortar, hStone, stoneMask), 0.05, 0.98)
878
+ out.roughness = clamp(lerp(0.94, 0.74 + (1 - strata) * 0.18, stoneMask), 0.6, 0.98)
879
+ out.ao = clamp(lerp(0.36, 0.96 - (1 - strata) * 0.14, stoneMask), 0.24, 1.0)
880
+ out.metallic = 0
881
+ }
882
+
883
+ function evalTerrazzo(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
884
+ // Clean architectural cement binder + crisp well-spaced Venetian terrazzo chips + fine salt/pepper micro-aggregate
885
+ const binder = periodicFbm2D(u, v, 8, 8, 3, ctx.seed, 0.5)
886
+ const fineSand = periodicValueNoise2D(u, v, 64, 64, (ctx.seed + 43) >>> 0)
887
+ const microSpeck = periodicValueNoise2D(u, v, 96, 96, (ctx.seed + 389) >>> 0)
888
+
889
+ const chipsLg = periodicVoronoi2D(u, v, 15, 15, (ctx.seed + 103) >>> 0, 0.92)
890
+ const chipsSm = periodicVoronoi2D(u + 0.07, v + 0.11, 32, 32, (ctx.seed + 239) >>> 0, 0.95)
891
+
892
+ // Non-overlapping distance-field clipped angular chips (well-spaced inside clean cement matrix)
893
+ const isLgChip = chipsLg.cellId > 0.58 && chipsLg.edgeDist > 0.2 && chipsLg.f1 < 0.38
894
+ const isSmChip = !isLgChip && chipsSm.cellId > 0.62 && chipsSm.edgeDist > 0.18 && chipsSm.f1 < 0.36
895
+
896
+ const speckShift = microSpeck > 0.88 ? 14 : microSpeck < 0.1 ? -18 : 0
897
+ const binderMul = 1.0 + (binder - 0.5) * 0.08 + (fineSand - 0.5) * 0.04
898
+ applyRgbTint(out, ctx.baseRgb, binderMul, 10 + speckShift)
899
+
900
+ let height = 0.48 + (binder - 0.5) * 0.05 + (fineSand - 0.5) * 0.03
901
+ let rough = 0.32 + (1 - binder) * 0.09
902
+ let ao = 0.96
903
+
904
+ if (isLgChip || isSmChip) {
905
+ const chipId = isLgChip ? chipsLg.cellId : chipsSm.cellId
906
+ const edge = isLgChip ? chipsLg.edgeDist : chipsSm.edgeDist
907
+ const chipEdge = smoothstep(isLgChip ? 0.2 : 0.18, isLgChip ? 0.23 : 0.21, edge)
908
+ const bucket = Math.floor(fract(chipId * 13.7) * 5)
909
+
910
+ let chipRgb: readonly [number, number, number]
911
+ if (bucket === 0) {
912
+ chipRgb = modulateRgb(ctx.baseRgb, { satScale: 0.2, valScale: 1.38, valOffset: 0.12 })
913
+ } else if (bucket === 1) {
914
+ chipRgb = modulateRgb(ctx.baseRgb, { satScale: 0.45, valScale: 0.42 })
915
+ } else if (bucket === 2) {
916
+ chipRgb = modulateRgb(ctx.baseRgb, { hueShift: -20, satOffset: 0.14, valScale: 0.96 })
917
+ } else if (bucket === 3) {
918
+ chipRgb = modulateRgb(ctx.baseRgb, { hueShift: 22, satOffset: 0.12, valScale: 0.78 })
919
+ } else {
920
+ chipRgb = modulateRgb(ctx.baseRgb, { hueShift: -6, satOffset: 0.06, valScale: 1.18 })
921
+ }
922
+
923
+ const chipVein = periodicValueNoise2D(u, v, 32, 32, (ctx.seed + 313) >>> 0)
924
+ const chipMul = 0.94 + chipVein * 0.12
925
+ const cR = chipRgb[0] * chipMul
926
+ const cG = chipRgb[1] * chipMul
927
+ const cB = chipRgb[2] * chipMul
928
+
929
+ out.r = lerp(out.r, cR, chipEdge)
930
+ out.g = lerp(out.g, cG, chipEdge)
931
+ out.b = lerp(out.b, cB, chipEdge)
932
+
933
+ height = lerp(height - 0.02, 0.56 + (chipVein - 0.5) * 0.04, chipEdge)
934
+ rough = lerp(rough, 0.16 + (1 - chipVein) * 0.06, chipEdge)
935
+ ao = lerp(0.9, 0.99, chipEdge)
936
+ }
937
+
938
+ out.height = clamp(height, 0.2, 0.85)
939
+ out.roughness = clamp(rough, 0.14, 0.52)
940
+ out.ao = clamp(ao, 0.72, 1.0)
941
+ out.metallic = 0
942
+ }
943
+
944
+ function evalMarbleStone(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
945
+ // Organic domain-warped diagonal Calacatta/Statuaretto marble veining (zero Voronoi cellular artifacts)
946
+ const warp1 = periodicDomainWarpFbm2D(u, v, 2, 2, 4, 0.36, ctx.seed)
947
+ const warp2 = periodicFbm2D(u, v, 4, 4, 4, (ctx.seed + 83) >>> 0, 0.55)
948
+ const fineWarp = periodicFbm2D(u, v, 10, 10, 3, (ctx.seed + 211) >>> 0, 0.58)
949
+ const strata = periodicFbm2D(u + warp1 * 0.2, v - warp2 * 0.16, 3, 3, 4, (ctx.seed + 179) >>> 0, 0.54)
950
+
951
+ // Sweeping diagonal primary vein + delicate branching hairline veins (strictly 1-periodic)
952
+ const veinPhase = TWO_PI * (u + v + (warp1 - 0.5) * 1.15 + (warp2 - 0.5) * 0.42 + (fineWarp - 0.5) * 0.14)
953
+ const primaryVein = Math.pow(1 - Math.abs(Math.sin(veinPhase)), 10)
954
+ const softHalo = Math.pow(1 - Math.abs(Math.sin(veinPhase)), 3) * 0.24
955
+ const secPhase = TWO_PI * (u + 2 * v + (warp2 - 0.5) * 1.25 + (fineWarp - 0.5) * 0.2)
956
+ const secondaryVein = Math.pow(1 - Math.abs(Math.sin(secPhase)), 14) * 0.52
957
+ const vein = clamp(primaryVein + softHalo + secondaryVein, 0, 1)
958
+
959
+ // Light stones get elegant grey mineral veins; dark stones (labradorite) get luminous veins
960
+ const avgLum = (ctx.baseRgb[0] + ctx.baseRgb[1] + ctx.baseRgb[2]) / 3
961
+ const veinRgb = avgLum < 90
962
+ ? modulateRgb(ctx.baseRgb, { hueShift: 18, satOffset: 0.12, valScale: 1.85, valOffset: 0.12 })
963
+ : modulateRgb(ctx.baseRgb, { satScale: 0.55, valScale: 0.58 })
964
+
965
+ const baseMul = 0.92 + strata * 0.14
966
+ applyRgbTint(out, ctx.baseRgb, baseMul)
967
+ blendOutWithRgb(out, veinRgb, vein * 0.68)
968
+
969
+ out.height = clamp(0.54 + (strata - 0.5) * 0.09 - primaryVein * 0.12, 0.25, 0.8)
970
+ out.roughness = clamp(0.14 + (1 - strata) * 0.08 + vein * 0.16, 0.09, 0.4)
971
+ out.ao = clamp(0.97 - primaryVein * 0.12, 0.8, 1.0)
972
+ out.metallic = 0
973
+ }
974
+
975
+ function evalMosaicTile(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
976
+ const grid = 12
977
+ const gx = fract(u) * grid
978
+ const gy = fract(v) * grid
979
+ const tx = Math.floor(gx)
980
+ const ty = Math.floor(gy)
981
+ const lx = gx - tx
982
+ const ly = gy - ty
983
+
984
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly)
985
+ const tileMask = smoothstep(0.055, 0.135, edge)
986
+
987
+ // Gentle pillowed ceramic crown
988
+ const dx = (lx - 0.5) * 2
989
+ const dy = (ly - 0.5) * 2
990
+ const pillow = clamp(1 - (dx * dx + dy * dy) * 0.28, 0.5, 1.0)
991
+
992
+ const tileId = hash2D(tx, ty, ctx.seed)
993
+ const glaze = periodicFbm2D(u, v, 10, 10, 3, (ctx.seed + 97) >>> 0, 0.5)
994
+ const tileTone = tileId > 0.5
995
+ ? mixRgb(ctx.baseRgb, ctx.lightTint, (tileId - 0.5) * 0.35)
996
+ : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - tileId) * 0.35)
997
+
998
+ const tileMul = 0.9 + (tileId - 0.5) * 0.18 + (glaze - 0.5) * 0.08
999
+ setRgb(
1000
+ out,
1001
+ lerp(ctx.groutRgb[0], tileTone[0] * tileMul, tileMask),
1002
+ lerp(ctx.groutRgb[1], tileTone[1] * tileMul, tileMask),
1003
+ lerp(ctx.groutRgb[2], tileTone[2] * tileMul, tileMask),
1004
+ )
1005
+
1006
+ out.height = clamp(lerp(0.18, 0.55 + pillow * 0.32, tileMask), 0.08, 0.96)
1007
+ out.roughness = clamp(lerp(0.88, 0.26 + (1 - glaze) * 0.12, tileMask), 0.18, 0.94)
1008
+ out.ao = clamp(lerp(0.46, 0.98, tileMask), 0.32, 1.0)
1009
+ out.metallic = 0
1010
+ }
1011
+
1012
+ function evalPoolTile(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
1013
+ const grid = 20 // fine vitreous glass swimming pool mosaic
1014
+ const gx = fract(u) * grid
1015
+ const gy = fract(v) * grid
1016
+ const tx = Math.floor(gx)
1017
+ const ty = Math.floor(gy)
1018
+ const lx = gx - tx
1019
+ const ly = gy - ty
1020
+
1021
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly)
1022
+ const tileMask = smoothstep(0.04, 0.095, edge)
1023
+
1024
+ const dx = (lx - 0.5) * 2
1025
+ const dy = (ly - 0.5) * 2
1026
+ const crown = clamp(1 - (dx * dx + dy * dy) * 0.28, 0.5, 1.0)
1027
+
1028
+ const tileId = hash2D(tx, ty, ctx.seed)
1029
+ const ripple = periodicFbm2D(u, v, 8, 8, 3, (ctx.seed + 131) >>> 0, 0.5)
1030
+
1031
+ // Luminous glazed cerulean/aqua per-tile variation + crisp bright white grout
1032
+ const tileRgb = modulateRgb(ctx.baseRgb, {
1033
+ hueShift: (tileId - 0.5) * 22,
1034
+ satOffset: (tileId - 0.45) * 0.18,
1035
+ valScale: 0.84 + tileId * 0.34,
1036
+ })
1037
+ const whiteGrout: readonly [number, number, number] = [234, 240, 244]
1038
+ const glazeMul = 0.94 + crown * 0.11 + (ripple - 0.5) * 0.06
1039
+
1040
+ setRgb(
1041
+ out,
1042
+ lerp(whiteGrout[0], tileRgb[0] * glazeMul, tileMask),
1043
+ lerp(whiteGrout[1], tileRgb[1] * glazeMul, tileMask),
1044
+ lerp(whiteGrout[2], tileRgb[2] * glazeMul, tileMask),
1045
+ )
1046
+
1047
+ out.height = clamp(lerp(0.22, 0.58 + crown * 0.32, tileMask), 0.1, 0.96)
1048
+ out.roughness = clamp(lerp(0.78, 0.08 + (1 - crown) * 0.09 + (ripple - 0.5) * 0.04, tileMask), 0.06, 0.86)
1049
+ out.ao = clamp(lerp(0.58, 0.99, tileMask), 0.42, 1.0)
1050
+ out.metallic = 0
1051
+ }
1052
+
1053
+ function evalCheckerTile(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
1054
+ const grid = 4 // even for seamless alternating checker wrap
1055
+ const gx = fract(u) * grid
1056
+ const gy = fract(v) * grid
1057
+ const tx = Math.floor(gx)
1058
+ const ty = Math.floor(gy)
1059
+ const lx = gx - tx
1060
+ const ly = gy - ty
1061
+
1062
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly)
1063
+ const tileMask = smoothstep(0.016, 0.042, edge)
1064
+ const isLight = ((tx + ty) & 1) === 0
1065
+
1066
+ const stoneCloud = periodicFbm2D(u, v, 8, 8, 3, (ctx.seed + (isLight ? 17 : 73)) >>> 0, 0.55)
1067
+ const tileVar = (hash2D(tx, ty, ctx.seed) - 0.5) * 0.06
1068
+
1069
+ // Two contrasting stone tones balanced around previewColor
1070
+ const lightSquare = modulateRgb(ctx.baseRgb, { satScale: 0.5, valScale: 1.24, valOffset: 0.06 })
1071
+ const darkSquare = modulateRgb(ctx.baseRgb, { satScale: 0.85, valScale: 0.52 })
1072
+ const sqBase = isLight ? lightSquare : darkSquare
1073
+ const sqMul = 0.92 + stoneCloud * 0.15 + tileVar
1074
+
1075
+ setRgb(
1076
+ out,
1077
+ lerp(ctx.groutRgb[0], sqBase[0] * sqMul, tileMask),
1078
+ lerp(ctx.groutRgb[1], sqBase[1] * sqMul, tileMask),
1079
+ lerp(ctx.groutRgb[2], sqBase[2] * sqMul, tileMask),
1080
+ )
1081
+
1082
+ out.height = clamp(lerp(0.22, 0.68 + (stoneCloud - 0.5) * 0.08, tileMask), 0.1, 0.92)
1083
+ out.roughness = clamp(lerp(0.86, 0.3 + (1 - stoneCloud) * 0.14 + (isLight ? -0.02 : 0.03), tileMask), 0.2, 0.92)
1084
+ out.ao = clamp(lerp(0.48, 0.97, tileMask), 0.34, 1.0)
1085
+ out.metallic = 0
1086
+ }
1087
+
1088
+ function evalGaragePanel(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
1089
+ const sections = 4 // horizontal sectional door panels
1090
+ const coffers = 2 // stamped rectangular recessed coffers per width
1091
+ const gv = fract(v) * sections
1092
+ const lv = gv - Math.floor(gv)
1093
+ const gu = fract(u) * coffers
1094
+ const lu = gu - Math.floor(gu)
1095
+
1096
+ // Crisp narrow horizontal section seam (sharp architectural bevel, zero muddy blur)
1097
+ const sectionSeam = smoothstep(0.012, 0.032, Math.min(lv, 1 - lv))
1098
+
1099
+ // Crisp stamped rectangular coffer molding groove inside each panel
1100
+ const cofferDistX = Math.min(lu - 0.09, 0.91 - lu)
1101
+ const cofferDistY = Math.min(lv - 0.17, 0.83 - lv)
1102
+ const cofferInner = Math.min(cofferDistX, cofferDistY)
1103
+ const cofferRecess = smoothstep(-0.008, 0.014, cofferInner) * (1 - smoothstep(0.036, 0.056, cofferInner))
1104
+
1105
+ // Fine painted steel micro-embossing grain
1106
+ const rib = 0.5 + 0.5 * Math.sin(TWO_PI * 24 * v)
1107
+ const microPaint = periodicFbm2D(u, v, 16, 40, 3, ctx.seed, 0.5)
1108
+
1109
+ const hPanel = 0.68 - cofferRecess * 0.34 + (rib - 0.5) * 0.03 + (microPaint - 0.5) * 0.03
1110
+ const mul = (0.96 + (microPaint - 0.5) * 0.05 - cofferRecess * 0.055) * lerp(0.72, 1.0, sectionSeam)
1111
+ applyRgbTint(out, ctx.baseRgb, mul)
1112
+
1113
+ out.height = clamp(lerp(0.14, hPanel, sectionSeam), 0.08, 0.92)
1114
+ out.roughness = clamp(0.4 + (microPaint - 0.5) * 0.1 + (1 - sectionSeam) * 0.25, 0.3, 0.76)
1115
+ out.ao = clamp(lerp(0.48, 0.98 - cofferRecess * 0.16, sectionSeam), 0.34, 1.0)
1116
+ out.metallic = 0.14 // lightly powder-coated galvanized steel
1117
+ }
1118
+
1119
+ function evalEarth(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
1120
+ const clods = periodicRidgedFbm2D(u, v, 6, 6, 4, ctx.seed, 0.6)
1121
+ const loam = periodicFbm2D(u, v, 14, 14, 4, (ctx.seed + 83) >>> 0, 0.62)
1122
+ const grit = periodicValueNoise2D(u, v, 36, 36, (ctx.seed + 199) >>> 0)
1123
+
1124
+ // Embedded small rounded pebbles
1125
+ const pebbles = periodicVoronoi2D(u, v, 16, 16, (ctx.seed + 281) >>> 0, 0.95)
1126
+ const hasPebble = pebbles.cellId > 0.76
1127
+ const pebbleMask = hasPebble ? smoothstep(0.32, 0.12, pebbles.f1) : 0
1128
+
1129
+ const soilTone = loam > 0.5
1130
+ ? mixRgb(ctx.baseRgb, ctx.warmTint, (loam - 0.5) * 0.35)
1131
+ : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - loam) * 0.4)
1132
+ const soilMul = 0.76 + clods * 0.34 + (grit - 0.5) * 0.16
1133
+
1134
+ const pebbleTone = modulateRgb(ctx.baseRgb, {
1135
+ satScale: 0.45,
1136
+ valScale: 1.15 + (pebbles.cellId - 0.76) * 1.4,
1137
+ })
1138
+
1139
+ setRgb(
1140
+ out,
1141
+ lerp(soilTone[0] * soilMul, pebbleTone[0], pebbleMask),
1142
+ lerp(soilTone[1] * soilMul, pebbleTone[1], pebbleMask),
1143
+ lerp(soilTone[2] * soilMul, pebbleTone[2], pebbleMask),
1144
+ )
1145
+
1146
+ const hSoil = 0.18 + clods * 0.48 + (loam - 0.5) * 0.2 + (grit - 0.5) * 0.1
1147
+ out.height = clamp(lerp(hSoil, 0.88, pebbleMask * 0.75), 0.05, 0.98)
1148
+ out.roughness = clamp(lerp(0.9 + (1 - clods) * 0.08, 0.68, pebbleMask), 0.6, 0.99)
1149
+ out.ao = clamp(0.58 + clods * 0.36 + pebbleMask * 0.06, 0.34, 1.0)
1150
+ out.metallic = 0
1151
+ }
1152
+
1153
+ function evalTerracottaTile(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
1154
+ const grid = 4
1155
+ const wobbleU = (periodicFbm2D(u, v, 8, 8, 2, (ctx.seed + 23) >>> 0) - 0.5) * 0.012
1156
+ const wobbleV = (periodicFbm2D(u, v, 8, 8, 2, (ctx.seed + 59) >>> 0) - 0.5) * 0.012
1157
+
1158
+ const gx = fract(u + wobbleU) * grid
1159
+ const gy = fract(v + wobbleV) * grid
1160
+ const tx = Math.floor(gx)
1161
+ const ty = Math.floor(gy)
1162
+ const lx = gx - tx
1163
+ const ly = gy - ty
1164
+
1165
+ // Crisp architectural quarry/terracotta grout lines (cleanly Nyquist-sampled & warm sandy mortar)
1166
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly)
1167
+ const tileMask = smoothstep(0.022, 0.058, edge)
1168
+
1169
+ const tileId = hash2D(tx, ty, ctx.seed)
1170
+ const clay = periodicFbm2D(u, v, 10, 10, 4, (ctx.seed + 137) >>> 0, 0.58)
1171
+ const speckle = periodicValueNoise2D(u, v, 32, 32, (ctx.seed + 257) >>> 0)
1172
+ const kilnEdge = smoothstep(0.04, 0.28, edge)
1173
+
1174
+ const tileTone = tileId > 0.5
1175
+ ? mixRgb(ctx.baseRgb, ctx.warmTint, (tileId - 0.5) * 0.42)
1176
+ : mixRgb(ctx.baseRgb, ctx.darkTint, (0.5 - tileId) * 0.4)
1177
+
1178
+ const tileMul = (0.86 + clay * 0.24 + (tileId - 0.5) * 0.16) * (0.9 + 0.1 * kilnEdge) - (speckle < 0.1 ? 0.14 : 0)
1179
+ const quarryGrout = mixRgb(ctx.groutRgb, [136, 130, 122], 0.52)
1180
+ const groutMul = 0.8 + speckle * 0.2
1181
+
1182
+ setRgb(
1183
+ out,
1184
+ lerp(quarryGrout[0] * groutMul, tileTone[0] * tileMul, tileMask),
1185
+ lerp(quarryGrout[1] * groutMul, tileTone[1] * tileMul, tileMask),
1186
+ lerp(quarryGrout[2] * groutMul, tileTone[2] * tileMul, tileMask),
1187
+ )
1188
+
1189
+ const hTile = 0.62 + (clay - 0.5) * 0.18 + kilnEdge * 0.08 - (speckle < 0.1 ? 0.12 : 0)
1190
+ const hGrout = 0.18 + (speckle - 0.5) * 0.08
1191
+ out.height = clamp(lerp(hGrout, hTile, tileMask), 0.06, 0.96)
1192
+ out.roughness = clamp(lerp(0.92, 0.6 + (1 - clay) * 0.18, tileMask), 0.48, 0.97)
1193
+ out.ao = clamp(lerp(0.42, 0.96, tileMask), 0.28, 1.0)
1194
+ out.metallic = 0
1195
+ }
1196
+
1197
+ function evalCeramicTile(u: number, v: number, ctx: EvalContext, out: PixelSample): void {
1198
+ const grid = 3 // large-format architectural ceramic/porcelain grid
1199
+ const gx = fract(u) * grid
1200
+ const gy = fract(v) * grid
1201
+ const tx = Math.floor(gx)
1202
+ const ty = Math.floor(gy)
1203
+ const lx = gx - tx
1204
+ const ly = gy - ty
1205
+
1206
+ // Fine precision grout line + crisp micro-bevel
1207
+ const edge = Math.min(lx, 1 - lx, ly, 1 - ly)
1208
+ const tileMask = smoothstep(0.012, 0.032, edge)
1209
+
1210
+ const tileId = hash2D(tx, ty, ctx.seed)
1211
+ // Per-tile offset into seamless glaze/grunge noise so adjacent tiles don't clone
1212
+ const offU = hash2D(tx, ty, (ctx.seed + 19) >>> 0)
1213
+ const offV = hash2D(tx, ty, (ctx.seed + 67) >>> 0)
1214
+ const glazeCloud = periodicDomainWarpFbm2D(u + offU, v + offV, 4, 4, 4, 0.16, (ctx.seed + 113) >>> 0)
1215
+ const microGrunge = periodicFbm2D(u, v, 18, 18, 3, (ctx.seed + 241) >>> 0, 0.58)
1216
+
1217
+ const tileMul = 0.88 + glazeCloud * 0.22 + (microGrunge - 0.5) * 0.1 + (tileId - 0.5) * 0.1
1218
+ const groutMul = 0.85 + microGrunge * 0.18
1219
+
1220
+ setRgb(
1221
+ out,
1222
+ lerp(ctx.groutRgb[0] * groutMul, ctx.baseRgb[0] * tileMul, tileMask),
1223
+ lerp(ctx.groutRgb[1] * groutMul, ctx.baseRgb[1] * tileMul, tileMask),
1224
+ lerp(ctx.groutRgb[2] * groutMul, ctx.baseRgb[2] * tileMul, tileMask),
1225
+ )
1226
+
1227
+ const hTile = 0.66 + (glazeCloud - 0.5) * 0.1 + (microGrunge - 0.5) * 0.06
1228
+ out.height = clamp(lerp(0.22, hTile, tileMask), 0.1, 0.92)
1229
+ out.roughness = clamp(lerp(0.86, 0.3 + (1 - glazeCloud) * 0.16 + (microGrunge - 0.5) * 0.08, tileMask), 0.2, 0.92)
1230
+ out.ao = clamp(lerp(0.46, 0.97, tileMask), 0.32, 1.0)
1231
+ out.metallic = 0
1232
+ }
1233
+
1234
+ // ============================================================================
1235
+ // Pattern Dispatch & Normal/AO Bake Pipeline
1236
+ // ============================================================================
1237
+
1238
+ function evaluatePattern(
1239
+ pattern: MitMaterialPattern,
1240
+ u: number,
1241
+ v: number,
1242
+ ctx: EvalContext,
1243
+ out: PixelSample,
1244
+ ): void {
1245
+ switch (pattern) {
1246
+ case 'concrete-plate':
1247
+ evalConcretePlate(u, v, ctx, out)
1248
+ return
1249
+ case 'concrete-polished':
1250
+ evalConcretePolished(u, v, ctx, out)
1251
+ return
1252
+ case 'concrete-raw':
1253
+ evalConcreteRaw(u, v, ctx, out)
1254
+ return
1255
+ case 'plaster':
1256
+ evalPlaster(u, v, ctx, out)
1257
+ return
1258
+ case 'drywall':
1259
+ evalDrywall(u, v, ctx, out)
1260
+ return
1261
+ case 'stucco':
1262
+ evalStucco(u, v, ctx, out)
1263
+ return
1264
+ case 'cotton':
1265
+ evalCotton(u, v, ctx, out)
1266
+ return
1267
+ case 'linen':
1268
+ evalLinen(u, v, ctx, out)
1269
+ return
1270
+ case 'velvet':
1271
+ evalVelvet(u, v, ctx, out)
1272
+ return
1273
+ case 'suede':
1274
+ evalSuede(u, v, ctx, out)
1275
+ return
1276
+ case 'wool':
1277
+ evalWool(u, v, ctx, out)
1278
+ return
1279
+ case 'boucle':
1280
+ evalBoucle(u, v, ctx, out)
1281
+ return
1282
+ case 'leather':
1283
+ evalLeather(u, v, ctx, out)
1284
+ return
1285
+ case 'copper':
1286
+ evalCopper(u, v, ctx, out)
1287
+ return
1288
+ case 'polished-metal':
1289
+ evalPolishedMetal(u, v, ctx, out)
1290
+ return
1291
+ case 'brushed-metal':
1292
+ evalBrushedMetal(u, v, ctx, out)
1293
+ return
1294
+ case 'roof-shingles':
1295
+ evalRoofShingles(u, v, ctx, out)
1296
+ return
1297
+ case 'roof-tiles':
1298
+ evalRoofTiles(u, v, ctx, out)
1299
+ return
1300
+ case 'board-batten':
1301
+ evalBoardBatten(u, v, ctx, out)
1302
+ return
1303
+ case 'lap-siding':
1304
+ evalLapSiding(u, v, ctx, out)
1305
+ return
1306
+ case 'wood-fine':
1307
+ evalWoodFine(u, v, ctx, out)
1308
+ return
1309
+ case 'wood-plank':
1310
+ evalWoodPlank(u, v, ctx, out)
1311
+ return
1312
+ case 'wood-parquet':
1313
+ evalWoodParquet(u, v, ctx, out)
1314
+ return
1315
+ case 'wood-chevron':
1316
+ evalWoodChevron(u, v, ctx, out)
1317
+ return
1318
+ case 'wood-square-parquet':
1319
+ evalWoodSquareParquet(u, v, ctx, out)
1320
+ return
1321
+ case 'brick':
1322
+ evalBrick(u, v, ctx, out)
1323
+ return
1324
+ case 'stone-wall':
1325
+ evalStoneWall(u, v, ctx, out)
1326
+ return
1327
+ case 'terrazzo':
1328
+ evalTerrazzo(u, v, ctx, out)
1329
+ return
1330
+ case 'marble-stone':
1331
+ evalMarbleStone(u, v, ctx, out)
1332
+ return
1333
+ case 'mosaic-tile':
1334
+ evalMosaicTile(u, v, ctx, out)
1335
+ return
1336
+ case 'pool-tile':
1337
+ evalPoolTile(u, v, ctx, out)
1338
+ return
1339
+ case 'checker-tile':
1340
+ evalCheckerTile(u, v, ctx, out)
1341
+ return
1342
+ case 'garage-panel':
1343
+ evalGaragePanel(u, v, ctx, out)
1344
+ return
1345
+ case 'earth':
1346
+ evalEarth(u, v, ctx, out)
1347
+ return
1348
+ case 'terracotta-tile':
1349
+ evalTerracottaTile(u, v, ctx, out)
1350
+ return
1351
+ case 'ceramic-tile':
1352
+ evalCeramicTile(u, v, ctx, out)
1353
+ return
1354
+ default: {
1355
+ const exhaustive: never = pattern
1356
+ throw new Error(`Unhandled MitMaterialPattern: ${String(exhaustive)}`)
1357
+ }
1358
+ }
1359
+ }
1360
+
1361
+ /**
1362
+ * Synthesizes a complete seamless tileable PBR texture pack (albedo, tangent-space normal,
1363
+ * roughness, ambient occlusion, metallic) as RGBA8 buffers for any `MitMaterialSpec`.
1364
+ */
1365
+ export function synthesizeMaterialPack(spec: MitMaterialSpec, size = 512): SynthesizedMaterialMaps {
1366
+ if (!Number.isInteger(size) || size < 4 || size > 4096) {
1367
+ throw new RangeError(`Material texture size must be an integer in 4..4096, got ${size}`)
1368
+ }
1369
+
1370
+ const width = size
1371
+ const height = size
1372
+ const pixelCount = width * height
1373
+ const byteCount = pixelCount * 4
1374
+
1375
+ const albedo = new Uint8ClampedArray(byteCount)
1376
+ const normal = new Uint8ClampedArray(byteCount)
1377
+ const roughness = new Uint8ClampedArray(byteCount)
1378
+ const ao = new Uint8ClampedArray(byteCount)
1379
+ const metallic = new Uint8ClampedArray(byteCount)
1380
+
1381
+ const heightBuf = new Float32Array(pixelCount)
1382
+ const baseAoBuf = new Float32Array(pixelCount)
1383
+
1384
+ const baseRgb = hexToRgb(spec.previewColor)
1385
+ const seed = fnv1a32(spec.id)
1386
+ const avgLum = (baseRgb[0] + baseRgb[1] + baseRgb[2]) / 3
1387
+
1388
+ // Derive harmonizing warm/cool/light/dark/grout palette tints from previewColor
1389
+ const groutRgb = avgLum > 140
1390
+ ? modulateRgb(baseRgb, { satScale: 0.35, valScale: 0.72 })
1391
+ : modulateRgb(baseRgb, { satScale: 0.25, valScale: 1.45, valOffset: 0.14 })
1392
+
1393
+ const ctx: EvalContext = {
1394
+ baseRgb,
1395
+ seed,
1396
+ lightTint: modulateRgb(baseRgb, { satScale: 0.85, valScale: 1.22, valOffset: 0.05 }),
1397
+ darkTint: modulateRgb(baseRgb, { satScale: 1.05, valScale: 0.68 }),
1398
+ warmTint: modulateRgb(baseRgb, { hueShift: -10, satOffset: 0.08, valScale: 1.06 }),
1399
+ coolTint: modulateRgb(baseRgb, { hueShift: 12, satScale: 0.85, valScale: 0.92 }),
1400
+ groutRgb,
1401
+ }
1402
+
1403
+ const sample: PixelSample = { r: 0, g: 0, b: 0, height: 0.5, roughness: 0.5, ao: 1, metallic: 0 }
1404
+
1405
+ // Pass 1: Synthesize seamless albedo, roughness, metallic, heightfield, and base AO
1406
+ for (let y = 0; y < height; y++) {
1407
+ const v = (y + 0.5) / height
1408
+ const rowOffset = y * width
1409
+ for (let x = 0; x < width; x++) {
1410
+ const u = (x + 0.5) / width
1411
+ evaluatePattern(spec.pattern, u, v, ctx, sample)
1412
+
1413
+ const idx = rowOffset + x
1414
+ const p = idx * 4
1415
+
1416
+ albedo[p] = clampByte(sample.r)
1417
+ albedo[p + 1] = clampByte(sample.g)
1418
+ albedo[p + 2] = clampByte(sample.b)
1419
+ albedo[p + 3] = 255
1420
+
1421
+ const roughByte = clampByte(sample.roughness * 255)
1422
+ roughness[p] = roughByte
1423
+ roughness[p + 1] = roughByte
1424
+ roughness[p + 2] = roughByte
1425
+ roughness[p + 3] = 255
1426
+
1427
+ const metalByte = clampByte(sample.metallic * 255)
1428
+ metallic[p] = metalByte
1429
+ metallic[p + 1] = metalByte
1430
+ metallic[p + 2] = metalByte
1431
+ metallic[p + 3] = 255
1432
+
1433
+ heightBuf[idx] = sample.height
1434
+ baseAoBuf[idx] = sample.ao
1435
+ }
1436
+ }
1437
+
1438
+ // Pass 2: Seamless wrap-around 3x3 Sobel tangent-space Normal Map (OpenGL Y+) + crevice AO
1439
+ const cfg = PATTERN_CONFIG[spec.pattern]
1440
+ const resScale = Math.sqrt(size / 256)
1441
+ const slopeGain = cfg.normalStrength * 0.45 * resScale
1442
+ const maxSlope = 2.15 // Guarantees Nz >= 1 / sqrt(1 + 2.15^2) = 0.422 -> byte >= 181 > 128
1443
+
1444
+ for (let y = 0; y < height; y++) {
1445
+ const ym = (y - 1 + height) % height
1446
+ const yp = (y + 1) % height
1447
+ const rowM = ym * width
1448
+ const rowC = y * width
1449
+ const rowP = yp * width
1450
+
1451
+ for (let x = 0; x < width; x++) {
1452
+ const xm = (x - 1 + width) % width
1453
+ const xp = (x + 1) % width
1454
+
1455
+ const tl = heightBuf[rowM + xm]!
1456
+ const t = heightBuf[rowM + x]!
1457
+ const tr = heightBuf[rowM + xp]!
1458
+ const l = heightBuf[rowC + xm]!
1459
+ const c = heightBuf[rowC + x]!
1460
+ const r = heightBuf[rowC + xp]!
1461
+ const bl = heightBuf[rowP + xm]!
1462
+ const b = heightBuf[rowP + x]!
1463
+ const br = heightBuf[rowP + xp]!
1464
+
1465
+ // 3x3 Sobel gradients on the wrapped torus
1466
+ const gx = tr + 2 * r + br - (tl + 2 * l + bl)
1467
+ const gy = bl + 2 * b + br - (tl + 2 * t + tr)
1468
+
1469
+ // OpenGL Y+ tangent-space normal convention:
1470
+ // +X right => Nx = -dh/dx; +Y up (top-to-bottom scan => -v) => Ny = +dh/dy
1471
+ let nx = -gx * slopeGain
1472
+ let ny = gy * slopeGain
1473
+ const slopeMag = Math.hypot(nx, ny)
1474
+ if (slopeMag > maxSlope) {
1475
+ const shrink = maxSlope / slopeMag
1476
+ nx *= shrink
1477
+ ny *= shrink
1478
+ }
1479
+
1480
+ const invLen = 1 / Math.hypot(nx, ny, 1)
1481
+ const idx = rowC + x
1482
+ const p = idx * 4
1483
+
1484
+ normal[p] = clampByte((nx * invLen * 0.5 + 0.5) * 255)
1485
+ normal[p + 1] = clampByte((ny * invLen * 0.5 + 0.5) * 255)
1486
+ normal[p + 2] = clampByte((invLen * 0.5 + 0.5) * 255)
1487
+ normal[p + 3] = 255
1488
+
1489
+ // Height-crevice ambient occlusion from 8-neighbor concavity + global relief depth
1490
+ const neighborMean = (tl + t + tr + l + r + bl + b + br) * 0.125
1491
+ const concavity = Math.max(0, neighborMean - c)
1492
+ const creviceDarken = clamp(concavity * cfg.aoCavityStrength, 0, 0.42)
1493
+ const finalAo = clamp(
1494
+ baseAoBuf[idx]! * (1 - creviceDarken) - (1 - c) * cfg.globalHeightAo,
1495
+ 0.16,
1496
+ 1.0,
1497
+ )
1498
+ const aoByte = clampByte(finalAo * 255)
1499
+ ao[p] = aoByte
1500
+ ao[p + 1] = aoByte
1501
+ ao[p + 2] = aoByte
1502
+ ao[p + 3] = 255
1503
+ }
1504
+ }
1505
+
1506
+ return { width, height, albedo, normal, roughness, ao, metallic }
1507
+ }