@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,993 @@
1
+ /**
2
+ * Unified 155-Item Clean-Room MIT Parametric 3D CAD Registry (`@kruschdev/pascal-plugin-mit-assets/cad`).
3
+ * Strictly React-free, editor-free, and node:*-free.
4
+ */
5
+
6
+ import * as THREE from 'three'
7
+ import { MIT_ITEM_SPECS, getMitItemSpec } from '../manifest'
8
+ import { APPLIANCE_MEP_BUILDERS } from './appliance-mep-builders'
9
+ import { FURNITURE_BUILDERS } from './furniture-builders'
10
+ import { KITCHEN_BATH_BUILDERS } from './kitchen-bath-builders'
11
+ import { OUTDOOR_FENESTRATION_BUILDERS } from './outdoor-fenestration-builders'
12
+ import {
13
+ type SlugBuilderFn,
14
+ createMitMaterialPalette,
15
+ normalizeAndGroundGroup,
16
+ } from './primitives'
17
+ import { PROPS_VEHICLES_BUILDERS } from './props-vehicles-builders'
18
+
19
+ export { createMitMaterialPalette, normalizeAndGroundGroup } from './primitives'
20
+ export type { MaterialPalette, SlugBuilderFn } from './primitives'
21
+
22
+ export const ALL_MIT_ITEM_BUILDERS: Readonly<Record<string, SlugBuilderFn>> = {
23
+ ...FURNITURE_BUILDERS,
24
+ ...KITCHEN_BATH_BUILDERS,
25
+ ...APPLIANCE_MEP_BUILDERS,
26
+ ...OUTDOOR_FENESTRATION_BUILDERS,
27
+ ...PROPS_VEHICLES_BUILDERS,
28
+ }
29
+
30
+ export type MitCadBuildOptions = {
31
+ readonly widthM?: number
32
+ readonly heightM?: number
33
+ readonly depthM?: number
34
+ readonly accentHex?: number
35
+ }
36
+
37
+ function clampDim(value: number | undefined, fallback: number, min = 0.01, max = 30): number {
38
+ const v = typeof value === 'number' && Number.isFinite(value) ? value : fallback
39
+ return Math.min(max, Math.max(min, v))
40
+ }
41
+
42
+ /**
43
+ * Slugs whose Pascal `CATALOG_ITEMS` `dimensions` encode a narrow trunk/pole footprint
44
+ * rather than the overhanging canopy diameter (`patio-umbrella`, `palm`, `tree`, `fir-tree`).
45
+ * These scale uniformly to match exact target height `h` while preserving natural crown width.
46
+ */
47
+ export const CANOPY_OVERHANG_SLUGS: ReadonlySet<string> = new Set([
48
+ 'patio-umbrella',
49
+ 'palm',
50
+ 'tree',
51
+ 'fir-tree',
52
+ ])
53
+
54
+ function normalizeUniformHeightAndGround(inner: THREE.Group, targetH: number): THREE.Group {
55
+ const root = new THREE.Group()
56
+ root.add(inner)
57
+ inner.updateMatrixWorld(true)
58
+ const box = new THREE.Box3().setFromObject(inner)
59
+ if (box.isEmpty()) return root
60
+
61
+ const size = new THREE.Vector3()
62
+ const center = new THREE.Vector3()
63
+ box.getSize(size)
64
+ box.getCenter(center)
65
+
66
+ const safeH = Number.isFinite(targetH) && targetH > 1e-4 ? targetH : 1
67
+ const s = size.y > 1e-5 ? safeH / size.y : 1
68
+ inner.position.set(-center.x * s, -box.min.y * s, -center.z * s)
69
+ inner.scale.set(s, s, s)
70
+ root.updateMatrixWorld(true)
71
+ return root
72
+ }
73
+
74
+ /**
75
+ * Build a clean-room MIT procedural Three.js `Group` for any of the 155 Pascal item slugs.
76
+ * Guaranteed grounded at `y = 0`, centered on `x = 0, z = 0`, matching target `[w, h, d]`
77
+ * (or exact target `h` with natural canopy overhang for trunk/pole-footprint botanicals & umbrellas).
78
+ */
79
+ export function buildMitItemCad(slugOrId: string, options: MitCadBuildOptions = {}): THREE.Group {
80
+ const spec = getMitItemSpec(slugOrId)
81
+ const slug = spec?.slug ?? slugOrId
82
+ const builder = Object.hasOwn(ALL_MIT_ITEM_BUILDERS, slug) ? ALL_MIT_ITEM_BUILDERS[slug] : undefined
83
+ if (!builder) {
84
+ throw new Error(`No clean-room MIT CAD builder registered for slug "${slugOrId}".`)
85
+ }
86
+ const w = clampDim(options.widthM, spec?.dimensions[0] ?? 0.8)
87
+ const h = clampDim(options.heightM, spec?.dimensions[1] ?? 0.8)
88
+ const d = clampDim(options.depthM, spec?.dimensions[2] ?? 0.8)
89
+ const palette = createMitMaterialPalette(options.accentHex)
90
+
91
+ const hasCustomFootprint =
92
+ (options.widthM !== undefined && (!spec || Math.abs(options.widthM - spec.dimensions[0]) > 1e-3)) ||
93
+ (options.depthM !== undefined && (!spec || Math.abs(options.depthM - spec.dimensions[2]) > 1e-3))
94
+ const isCanopyOverhang = CANOPY_OVERHANG_SLUGS.has(slug) && !hasCustomFootprint
95
+ const buildW = isCanopyOverhang ? Math.max(w, h * 0.62) : w
96
+ const buildD = isCanopyOverhang ? Math.max(d, h * 0.62) : d
97
+
98
+ const inner = builder(buildW, h, buildD, palette, slug)
99
+ const grounded = isCanopyOverhang
100
+ ? normalizeUniformHeightAndGround(inner, h)
101
+ : normalizeAndGroundGroup(inner, w, h, d)
102
+ grounded.name = `mit-item:${slug}`
103
+ return grounded
104
+ }
105
+
106
+ export function listSupportedMitCadSlugs(): string[] {
107
+ return MIT_ITEM_SPECS.map((s) => s.slug)
108
+ }
109
+
110
+ function escapeXml(text: string): string {
111
+ return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
112
+ }
113
+
114
+ /**
115
+ * Generate a crisp architectural 2D top-down CAD drafting SVG (`128x128`) for any item slug.
116
+ * Uses standard architectural plan conventions (door swing arcs, stair treads + UP arrow,
117
+ * sanitary fixture bowls/drains, stove burners, cushion/pillow lines, canopy ribs, etc.).
118
+ */
119
+ export function renderFloorPlanSvg(slug: string, widthM: number, depthM: number): string {
120
+ const safeW = clampDim(widthM, 0.8, 0.05, 30)
121
+ const safeD = clampDim(depthM, 0.8, 0.05, 30)
122
+ const s = slug.toLowerCase()
123
+ const pad = 14
124
+ const maxSpan = 128 - pad * 2
125
+ const scale = Math.min(maxSpan / safeW, maxSpan / safeD)
126
+ const rw = Math.max(12, safeW * scale)
127
+ const rd = Math.max(12, safeD * scale)
128
+ const x = (128 - rw) / 2
129
+ const y = (128 - rd) / 2
130
+ const f = (n: number) => n.toFixed(1)
131
+
132
+ const parts: string[] = []
133
+ const strokeMain = '#1e293b'
134
+ const strokeDetail = '#475569'
135
+ const fillBase = '#f1f5f9'
136
+ const fillAccent = '#e2e8f0'
137
+ let drawFrontTick = true
138
+
139
+ // 1. Doors (wall jambs + 90-degree open leaf + quarter-circle architectural swing arc)
140
+ if (/^door($|-)|glass-door|doorway-front/.test(s)) {
141
+ drawFrontTick = false
142
+ const leaf = 76
143
+ const x0 = 26
144
+ const y0 = 98
145
+ parts.push(
146
+ `<line x1="14" y1="${y0}" x2="${x0}" y2="${y0}" stroke="${strokeMain}" stroke-width="4" stroke-linecap="round"/>`,
147
+ `<line x1="${x0 + leaf}" y1="${y0}" x2="114" y2="${y0}" stroke="${strokeMain}" stroke-width="4" stroke-linecap="round"/>`,
148
+ `<path d="M ${x0 + leaf} ${y0} A ${leaf} ${leaf} 0 0 0 ${x0} ${y0 - leaf}" fill="none" stroke="${strokeDetail}" stroke-width="1.6" stroke-dasharray="4 3"/>`,
149
+ `<rect x="${x0}" y="${y0 - leaf}" width="6" height="${leaf}" rx="1.5" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2"/>`,
150
+ )
151
+ }
152
+ // 2. Windows (including window-round — flat architectural wall cut with jambs & center glazing line)
153
+ else if (/^window/.test(s)) {
154
+ drawFrontTick = false
155
+ const wh = 18
156
+ const wy = 64 - wh / 2
157
+ parts.push(
158
+ `<rect x="16" y="${wy}" width="96" height="${wh}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
159
+ `<line x1="16" y1="64" x2="112" y2="64" stroke="${strokeMain}" stroke-width="1.8"/>`,
160
+ `<line x1="22" y1="${wy}" x2="22" y2="${wy + wh}" stroke="${strokeMain}" stroke-width="2.2"/>`,
161
+ `<line x1="106" y1="${wy}" x2="106" y2="${wy + wh}" stroke="${strokeMain}" stroke-width="2.2"/>`,
162
+ `<line x1="64" y1="${wy + 2}" x2="64" y2="${wy + wh - 2}" stroke="${strokeDetail}" stroke-width="1.6"/>`,
163
+ )
164
+ }
165
+ // 3. Stairs (parallel treads, diagonal architectural break line & UP arrow)
166
+ else if (s === 'stairs') {
167
+ drawFrontTick = false
168
+ const sw = Math.max(36, rw)
169
+ const sx = (128 - sw) / 2
170
+ parts.push(`<rect x="${f(sx)}" y="${f(y)}" width="${f(sw)}" height="${f(rd)}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`)
171
+ const treads = 11
172
+ for (let i = 1; i < treads; i++) {
173
+ const ty = y + (rd * i) / treads
174
+ parts.push(`<line x1="${f(sx)}" y1="${f(ty)}" x2="${f(sx + sw)}" y2="${f(ty)}" stroke="${strokeDetail}" stroke-width="1.3"/>`)
175
+ }
176
+ // Center UP arrow pointing from bottom (+Z) to top (-Z)
177
+ parts.push(
178
+ `<line x1="64" y1="${f(y + rd - 10)}" x2="64" y2="${f(y + 12)}" stroke="${strokeMain}" stroke-width="2.2"/>`,
179
+ `<polyline points="57,${f(y + 20)} 64,${f(y + 11)} 71,${f(y + 20)}" fill="none" stroke="${strokeMain}" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/>`,
180
+ `<line x1="${f(sx - 4)}" y1="${f(y + rd * 0.54)}" x2="${f(sx + sw + 4)}" y2="${f(y + rd * 0.46)}" stroke="${strokeMain}" stroke-width="2"/>`,
181
+ )
182
+ }
183
+ // 4. Bathtub / Tub (outer rim + recessed oval soaking basin + drain + faucet)
184
+ else if (s === 'bathtub' || s === 'tub') {
185
+ drawFrontTick = false
186
+ const inset = Math.min(7, rw * 0.22, rd * 0.22)
187
+ const innerW = Math.max(2, rw - inset * 2)
188
+ const innerH = Math.max(2, rd - inset * 2)
189
+ parts.push(
190
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="${f(Math.min(s === 'bathtub' ? 14 : 4, rw / 2, rd / 2))}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.4"/>`,
191
+ `<rect x="${f(x + inset)}" y="${f(y + inset)}" width="${f(innerW)}" height="${f(innerH)}" rx="${f(Math.min(18, innerW / 2, innerH / 2))}" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.6"/>`,
192
+ `<circle cx="${f(x + rw * 0.72)}" cy="64" r="${f(Math.min(3.5, innerW * 0.2, innerH * 0.2))}" fill="none" stroke="${strokeMain}" stroke-width="1.8"/>`,
193
+ `<line x1="${f(x + rw - 2)}" y1="64" x2="${f(x + rw - Math.min(14, rw * 0.4))}" y2="64" stroke="${strokeMain}" stroke-width="2.4" stroke-linecap="round"/>`,
194
+ )
195
+ }
196
+ // 5. Toilet (rear cistern tank + oval bowl + inner seat)
197
+ else if (s === 'toilet') {
198
+ drawFrontTick = false
199
+ const tankH = rd * 0.28
200
+ const bowlCy = y + tankH + (rd - tankH) * 0.52
201
+ const bowlRy = (rd - tankH) * 0.48
202
+ parts.push(
203
+ `<rect x="${f(x + 2)}" y="${f(y)}" width="${f(rw - 4)}" height="${f(tankH)}" rx="4" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2.2"/>`,
204
+ `<ellipse cx="64" cy="${f(bowlCy)}" rx="${f(rw * 0.44)}" ry="${f(bowlRy)}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
205
+ `<ellipse cx="64" cy="${f(bowlCy + 2)}" rx="${f(rw * 0.3)}" ry="${f(bowlRy * 0.68)}" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.4"/>`,
206
+ )
207
+ }
208
+ // 6. Urinal (wall backplate + D-basin + drain)
209
+ else if (s === 'urinal') {
210
+ drawFrontTick = false
211
+ parts.push(
212
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd * 0.22)}" rx="2" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2"/>`,
213
+ `<path d="M ${f(x + 4)} ${f(y + rd * 0.22)} L ${f(x + 4)} ${f(y + rd * 0.58)} A ${f(rw * 0.46)} ${f(rd * 0.42)} 0 0 0 ${f(x + rw - 4)} ${f(y + rd * 0.58)} L ${f(x + rw - 4)} ${f(y + rd * 0.22)} Z" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
214
+ `<circle cx="64" cy="${f(y + rd * 0.56)}" r="3.5" fill="none" stroke="${strokeDetail}" stroke-width="1.6"/>`,
215
+ )
216
+ }
217
+ // 7. Showers (pentagon neo-angle vs square stall + diagonal X-drain lines)
218
+ else if (/^shower($|-angle|-square)/.test(s)) {
219
+ drawFrontTick = false
220
+ if (s === 'shower-angle') {
221
+ const c = rw * 0.38
222
+ const pts = `${f(x)},${f(y)} ${f(x + rw)},${f(y)} ${f(x + rw)},${f(y + rd - c)} ${f(x + rw - c)},${f(y + rd)} ${f(x)},${f(y + rd)}`
223
+ parts.push(
224
+ `<polygon points="${pts}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.4"/>`,
225
+ `<line x1="${f(x + 4)}" y1="${f(y + 4)}" x2="${f(x + rw - c * 0.55)}" y2="${f(y + rd - c * 0.55)}" stroke="${strokeDetail}" stroke-width="1.3"/>`,
226
+ `<line x1="${f(x + rw - 4)}" y1="${f(y + 4)}" x2="${f(x + 4)}" y2="${f(y + rd - 4)}" stroke="${strokeDetail}" stroke-width="1.3"/>`,
227
+ `<circle cx="${f(x + rw * 0.44)}" cy="${f(y + rd * 0.44)}" r="4.5" fill="#ffffff" stroke="${strokeMain}" stroke-width="1.8"/>`,
228
+ )
229
+ } else {
230
+ parts.push(
231
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="3" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.4"/>`,
232
+ `<line x1="${f(x + 4)}" y1="${f(y + 4)}" x2="${f(x + rw - 4)}" y2="${f(y + rd - 4)}" stroke="${strokeDetail}" stroke-width="1.3"/>`,
233
+ `<line x1="${f(x + rw - 4)}" y1="${f(y + 4)}" x2="${f(x + 4)}" y2="${f(y + rd - 4)}" stroke="${strokeDetail}" stroke-width="1.3"/>`,
234
+ `<circle cx="64" cy="64" r="4.5" fill="#ffffff" stroke="${strokeMain}" stroke-width="1.8"/>`,
235
+ )
236
+ }
237
+ }
238
+ // 8. Sinks & Kitchen Units (recessed basin + faucet + optional 4-burner cooktop)
239
+ else if (s === 'kitchen' || /sink/.test(s)) {
240
+ drawFrontTick = false
241
+ parts.push(`<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="3" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`)
242
+ if (s === 'kitchen') {
243
+ const bw = rw * 0.28
244
+ const bh = rd * 0.62
245
+ const bx = x + rw * 0.22
246
+ const by = y + rd * 0.22
247
+ parts.push(
248
+ `<rect x="${f(bx)}" y="${f(by)}" width="${f(bw)}" height="${f(bh)}" rx="3" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.6"/>`,
249
+ `<circle cx="${f(bx + bw / 2)}" cy="${f(by + bh / 2)}" r="2.2" fill="${strokeMain}"/>`,
250
+ )
251
+ // 4 cooktop burners on right
252
+ const cx0 = x + rw * 0.72
253
+ const cy0 = y + rd * 0.52
254
+ const rxOff = rw * 0.08
255
+ const ryOff = rd * 0.2
256
+ for (const [dx, dy, r] of [[-1, -1, 4.2], [1, -1, 3.4], [-1, 1, 3.4], [1, 1, 4.2]] as const) {
257
+ parts.push(`<circle cx="${f(cx0 + dx * rxOff)}" cy="${f(cy0 + dy * ryOff)}" r="${r}" fill="none" stroke="${strokeMain}" stroke-width="1.5"/>`)
258
+ }
259
+ } else if (s === 'bathroom-sink' && rw > rd * 1.8) {
260
+ // Double vanity basins
261
+ for (const fx of [0.28, 0.72]) {
262
+ const bx = x + rw * fx
263
+ parts.push(
264
+ `<ellipse cx="${f(bx)}" cy="${f(y + rd * 0.55)}" rx="${f(rw * 0.14)}" ry="${f(rd * 0.3)}" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.6"/>`,
265
+ `<circle cx="${f(bx)}" cy="${f(y + rd * 0.55)}" r="2" fill="${strokeMain}"/>`,
266
+ `<line x1="${f(bx)}" y1="${f(y + 3)}" x2="${f(bx)}" y2="${f(y + rd * 0.32)}" stroke="${strokeMain}" stroke-width="2"/>`,
267
+ )
268
+ }
269
+ } else {
270
+ parts.push(
271
+ `<rect x="${f(x + rw * 0.15)}" y="${f(y + rd * 0.22)}" width="${f(rw * 0.7)}" height="${f(rd * 0.62)}" rx="4" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.6"/>`,
272
+ `<circle cx="64" cy="${f(y + rd * 0.52)}" r="2.6" fill="${strokeMain}"/>`,
273
+ `<line x1="64" y1="${f(y + 3)}" x2="64" y2="${f(y + rd * 0.3)}" stroke="${strokeMain}" stroke-width="2.2"/>`,
274
+ )
275
+ }
276
+ }
277
+ // 9. Stove / Range (4 circular cooktop burner rings)
278
+ else if (s === 'stove') {
279
+ drawFrontTick = false
280
+ parts.push(`<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="3" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`)
281
+ parts.push(`<line x1="${f(x)}" y1="${f(y + rd * 0.14)}" x2="${f(x + rw)}" y2="${f(y + rd * 0.14)}" stroke="${strokeDetail}" stroke-width="1.4"/>`)
282
+ for (const [fx, fy, r] of [[0.3, 0.38, 12], [0.7, 0.38, 9.5], [0.3, 0.72, 9.5], [0.7, 0.72, 12]] as const) {
283
+ const br = Math.min(r, rw * 0.14, rd * 0.14)
284
+ parts.push(
285
+ `<circle cx="${f(x + rw * fx)}" cy="${f(y + rd * fy)}" r="${f(br)}" fill="#ffffff" stroke="${strokeMain}" stroke-width="1.8"/>`,
286
+ `<circle cx="${f(x + rw * fx)}" cy="${f(y + rd * fy)}" r="${f(br * 0.42)}" fill="none" stroke="${strokeDetail}" stroke-width="1.2"/>`,
287
+ )
288
+ }
289
+ }
290
+ // 10. Sofas, Couches & Armchairs (backrest + armrests + seat cushion dividers)
291
+ else if (/sofa|couch|livingroom-chair/.test(s)) {
292
+ drawFrontTick = false
293
+ const seats = s === 'livingroom-chair' ? 1 : s === 'sofa' || /leather-couch/.test(s) ? 3 : 2
294
+ const armW = Math.max(7, rw * (seats === 1 ? 0.16 : 0.1))
295
+ const backD = Math.max(8, rd * 0.24)
296
+ parts.push(
297
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="5" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
298
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(backD)}" rx="3" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="1.8"/>`,
299
+ `<rect x="${f(x)}" y="${f(y + backD)}" width="${f(armW)}" height="${f(rd - backD)}" rx="2" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="1.8"/>`,
300
+ `<rect x="${f(x + rw - armW)}" y="${f(y + backD)}" width="${f(armW)}" height="${f(rd - backD)}" rx="2" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="1.8"/>`,
301
+ )
302
+ const innerW = rw - armW * 2
303
+ for (let i = 1; i < seats; i++) {
304
+ const cx = x + armW + (innerW * i) / seats
305
+ parts.push(`<line x1="${f(cx)}" y1="${f(y + backD)}" x2="${f(cx)}" y2="${f(y + rd)}" stroke="${strokeDetail}" stroke-width="1.5"/>`)
306
+ }
307
+ }
308
+ // 11. Lounge Chair + Ottoman (two distinct top-down contours)
309
+ else if (s === 'lounge-chair') {
310
+ drawFrontTick = false
311
+ const chairD = rd * 0.54
312
+ const ottD = rd * 0.28
313
+ parts.push(
314
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(chairD)}" rx="6" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
315
+ `<rect x="${f(x + 4)}" y="${f(y + 4)}" width="${f(rw - 8)}" height="${f(chairD * 0.28)}" rx="3" fill="${fillAccent}" stroke="${strokeDetail}" stroke-width="1.5"/>`,
316
+ `<rect x="${f(x + rw * 0.1)}" y="${f(y + rd - ottD)}" width="${f(rw * 0.8)}" height="${f(ottD)}" rx="5" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
317
+ )
318
+ }
319
+ // 12. Beds (headboard + pillows + folded duvet fold line)
320
+ else if (/bed/.test(s) && s !== 'sunbed') {
321
+ drawFrontTick = false
322
+ const isDouble = s === 'double-bed' || rw > rd * 0.65
323
+ const hbH = Math.max(5, rd * 0.08)
324
+ parts.push(
325
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="4" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
326
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(hbH)}" rx="2" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="1.8"/>`,
327
+ )
328
+ const pY = y + hbH + 5
329
+ const pH = Math.max(10, rd * 0.16)
330
+ if (isDouble) {
331
+ const pW = rw * 0.38
332
+ parts.push(
333
+ `<rect x="${f(x + rw * 0.08)}" y="${f(pY)}" width="${f(pW)}" height="${f(pH)}" rx="4" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.5"/>`,
334
+ `<rect x="${f(x + rw * 0.54)}" y="${f(pY)}" width="${f(pW)}" height="${f(pH)}" rx="4" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.5"/>`,
335
+ )
336
+ } else {
337
+ parts.push(`<rect x="${f(x + rw * 0.16)}" y="${f(pY)}" width="${f(rw * 0.68)}" height="${f(pH)}" rx="4" fill="#ffffff" stroke="${strokeDetail}" stroke-width="1.5"/>`)
338
+ }
339
+ const foldY = y + rd * 0.42
340
+ parts.push(
341
+ `<line x1="${f(x)}" y1="${f(foldY)}" x2="${f(x + rw)}" y2="${f(foldY)}" stroke="${strokeMain}" stroke-width="1.8"/>`,
342
+ `<line x1="${f(x)}" y1="${f(foldY + 8)}" x2="${f(x + rw)}" y2="${f(foldY + 8)}" stroke="${strokeDetail}" stroke-width="1.2"/>`,
343
+ )
344
+ }
345
+ // 13. Vehicles (aligned along their longer axis: hood, windshield, roof, rear window, 4 wheels)
346
+ else if (s === 'tesla' || /camaro/.test(s)) {
347
+ drawFrontTick = false
348
+ const horiz = rw > rd
349
+ parts.push(`<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="${horiz ? 12 : 12}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`)
350
+ if (horiz) {
351
+ // Oriented along X (front at right +X)
352
+ parts.push(
353
+ `<rect x="${f(x + rw * 0.24)}" y="${f(y + rd * 0.14)}" width="${f(rw * 0.44)}" height="${f(rd * 0.72)}" rx="5" fill="${fillAccent}" stroke="${strokeDetail}" stroke-width="1.6"/>`,
354
+ `<line x1="${f(x + rw * 0.54)}" y1="${f(y + rd * 0.14)}" x2="${f(x + rw * 0.68)}" y2="${f(y + rd * 0.18)}" stroke="${strokeMain}" stroke-width="1.5"/>`,
355
+ `<line x1="${f(x + rw * 0.54)}" y1="${f(y + rd * 0.86)}" x2="${f(x + rw * 0.68)}" y2="${f(y + rd * 0.82)}" stroke="${strokeMain}" stroke-width="1.5"/>`,
356
+ `<line x1="${f(x + rw * 0.54)}" y1="${f(y + rd * 0.14)}" x2="${f(x + rw * 0.54)}" y2="${f(y + rd * 0.86)}" stroke="${strokeMain}" stroke-width="1.5"/>`,
357
+ `<line x1="${f(x + rw * 0.34)}" y1="${f(y + rd * 0.14)}" x2="${f(x + rw * 0.34)}" y2="${f(y + rd * 0.86)}" stroke="${strokeDetail}" stroke-width="1.4"/>`,
358
+ )
359
+ } else {
360
+ // Oriented along Z (front at bottom +Z)
361
+ parts.push(
362
+ `<rect x="${f(x + rw * 0.14)}" y="${f(y + rd * 0.24)}" width="${f(rw * 0.72)}" height="${f(rd * 0.46)}" rx="5" fill="${fillAccent}" stroke="${strokeDetail}" stroke-width="1.6"/>`,
363
+ `<line x1="${f(x + rw * 0.14)}" y1="${f(y + rd * 0.56)}" x2="${f(x + rw * 0.86)}" y2="${f(y + rd * 0.56)}" stroke="${strokeMain}" stroke-width="1.5"/>`,
364
+ `<line x1="${f(x + rw * 0.14)}" y1="${f(y + rd * 0.35)}" x2="${f(x + rw * 0.86)}" y2="${f(y + rd * 0.35)}" stroke="${strokeDetail}" stroke-width="1.4"/>`,
365
+ )
366
+ }
367
+ }
368
+ // 14. Patio Umbrella (octagonal canopy + 8 ribs + center pole)
369
+ else if (s === 'patio-umbrella') {
370
+ drawFrontTick = false
371
+ const R = 48
372
+ const pts: string[] = []
373
+ for (let i = 0; i < 8; i++) {
374
+ const a = (i / 8) * Math.PI * 2 + Math.PI / 8
375
+ const px = 64 + Math.cos(a) * R
376
+ const py = 64 + Math.sin(a) * R
377
+ pts.push(`${f(px)},${f(py)}`)
378
+ parts.push(`<line x1="64" y1="64" x2="${f(px)}" y2="${f(py)}" stroke="${strokeDetail}" stroke-width="1.2"/>`)
379
+ }
380
+ parts.unshift(`<polygon points="${pts.join(' ')}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`)
381
+ parts.push(`<circle cx="64" cy="64" r="4" fill="${strokeMain}"/>`)
382
+ }
383
+ // 15. Botanicals (palm fronds, tree/fir-tree concentric organic foliage + trunk center)
384
+ else if (s === 'palm') {
385
+ drawFrontTick = false
386
+ const R = 48
387
+ for (let i = 0; i < 8; i++) {
388
+ const a = (i / 8) * Math.PI * 2
389
+ const px = 64 + Math.cos(a) * R
390
+ const py = 64 + Math.sin(a) * R
391
+ const mx = 64 + Math.cos(a + 0.18) * R * 0.58
392
+ const my = 64 + Math.sin(a + 0.18) * R * 0.58
393
+ const nx = 64 + Math.cos(a - 0.18) * R * 0.58
394
+ const ny = 64 + Math.sin(a - 0.18) * R * 0.58
395
+ parts.push(`<polygon points="64,64 ${f(mx)},${f(my)} ${f(px)},${f(py)} ${f(nx)},${f(ny)}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="1.6"/>`)
396
+ }
397
+ parts.push(`<circle cx="64" cy="64" r="6" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2"/>`)
398
+ } else if (/^(tree|fir-tree|indoor-plant|small-indoor-plant|cactus)$/.test(s)) {
399
+ drawFrontTick = false
400
+ const R = 46
401
+ parts.push(
402
+ `<circle cx="64" cy="64" r="${R}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
403
+ `<circle cx="64" cy="64" r="${(R * 0.64).toFixed(1)}" fill="none" stroke="${strokeDetail}" stroke-width="1.3" stroke-dasharray="5 3"/>`,
404
+ `<circle cx="64" cy="64" r="5" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2"/>`,
405
+ )
406
+ }
407
+ // 16. Pool Table (outer wood rail + inner felt + 6 pockets)
408
+ else if (s === 'pool-table') {
409
+ drawFrontTick = false
410
+ const rail = Math.min(7, rw * 0.22, rd * 0.22)
411
+ const feltW = Math.max(2, rw - rail * 2)
412
+ const feltH = Math.max(2, rd - rail * 2)
413
+ const pocketR = Math.min(3.4, rail * 0.7)
414
+ parts.push(
415
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="5" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2.2"/>`,
416
+ `<rect x="${f(x + rail)}" y="${f(y + rail)}" width="${f(feltW)}" height="${f(feltH)}" rx="2" fill="${fillBase}" stroke="${strokeDetail}" stroke-width="1.4"/>`,
417
+ )
418
+ for (const [px, py] of [
419
+ [x + rail, y + rail], [x + rw - rail, y + rail], [x + rail, y + rd - rail],
420
+ [x + rw - rail, y + rd - rail], [x + rail, 64], [x + rw - rail, 64],
421
+ ] as const) {
422
+ parts.push(`<circle cx="${f(px)}" cy="${f(py)}" r="${f(pocketR)}" fill="${strokeMain}"/>`)
423
+ }
424
+ }
425
+ // 17. Piano (cabinet + projecting front keyboard strip with key dividers)
426
+ else if (s === 'piano') {
427
+ drawFrontTick = false
428
+ parts.push(
429
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd * 0.62)}" rx="3" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2.2"/>`,
430
+ `<rect x="${f(x + 4)}" y="${f(y + rd * 0.62)}" width="${f(rw - 8)}" height="${f(rd * 0.38)}" rx="2" fill="#ffffff" stroke="${strokeMain}" stroke-width="2"/>`,
431
+ )
432
+ for (let i = 1; i < 10; i++) {
433
+ const kx = x + 4 + ((rw - 8) * i) / 10
434
+ parts.push(`<line x1="${f(kx)}" y1="${f(y + rd * 0.62)}" x2="${f(kx)}" y2="${f(y + rd)}" stroke="${strokeDetail}" stroke-width="1"/>`)
435
+ }
436
+ }
437
+ // 18. Circular Fixtures (ac-block fan, suspended-fireplace hearth, bean-bag, lamps, round mirrors/rugs)
438
+ else if (s === 'ac-block' || s === 'suspended-fireplace') {
439
+ drawFrontTick = false
440
+ if (s === 'ac-block') {
441
+ parts.push(`<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="5" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`)
442
+ }
443
+ const r = Math.min(rw, rd) * (s === 'suspended-fireplace' ? 0.46 : 0.36)
444
+ parts.push(
445
+ `<circle cx="64" cy="64" r="${f(r)}" fill="${s === 'suspended-fireplace' ? fillBase : '#ffffff'}" stroke="${strokeMain}" stroke-width="2.2"/>`,
446
+ `<circle cx="64" cy="64" r="${f(r * 0.42)}" fill="${fillAccent}" stroke="${strokeDetail}" stroke-width="1.5"/>`,
447
+ `<line x1="${f(64 - r)}" y1="64" x2="${f(64 + r)}" y2="64" stroke="${strokeDetail}" stroke-width="1.2"/>`,
448
+ `<line x1="64" y1="${f(64 - r)}" x2="64" y2="${f(64 + r)}" stroke="${strokeDetail}" stroke-width="1.2"/>`,
449
+ )
450
+ } else if (s === 'bean-bag') {
451
+ drawFrontTick = false
452
+ const r = Math.min(rw, rd) * 0.48
453
+ parts.push(
454
+ `<circle cx="64" cy="64" r="${f(r)}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
455
+ `<path d="M ${f(64 - r * 0.56)} ${f(64 - r * 0.1)} Q 64 ${f(64 - r * 0.62)} ${f(64 + r * 0.56)} ${f(64 - r * 0.1)}" fill="none" stroke="${strokeDetail}" stroke-width="1.6"/>`,
456
+ )
457
+ } else if (/lamp|light|ceiling-fan/.test(s)) {
458
+ drawFrontTick = false
459
+ const r = Math.min(rw, rd, 44) * 0.46
460
+ parts.push(
461
+ `<circle cx="64" cy="64" r="${f(Math.max(16, r))}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
462
+ `<line x1="${f(64 - Math.max(16, r))}" y1="64" x2="${f(64 + Math.max(16, r))}" y2="64" stroke="${strokeDetail}" stroke-width="1.3"/>`,
463
+ `<line x1="64" y1="${f(64 - Math.max(16, r))}" x2="64" y2="${f(64 + Math.max(16, r))}" stroke="${strokeDetail}" stroke-width="1.3"/>`,
464
+ `<circle cx="64" cy="64" r="4" fill="${strokeMain}"/>`,
465
+ )
466
+ } else if (/electric-panel|ev-wall-charger/.test(s)) {
467
+ drawFrontTick = false
468
+ parts.push(
469
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="3" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
470
+ `<line x1="${f(x)}" y1="${f(y)}" x2="${f(x + rw)}" y2="${f(y + rd)}" stroke="${strokeMain}" stroke-width="1.8"/>`,
471
+ `<polygon points="${f(x)},${f(y)} ${f(x + rw)},${f(y)} ${f(x + rw)},${f(y + rd)}" fill="${strokeMain}" fill-opacity="0.18"/>`,
472
+ )
473
+ } else if (/office-chair|herman-miller|dining-chair|stool/.test(s)) {
474
+ drawFrontTick = false
475
+ parts.push(
476
+ `<rect x="${f(x + rw * 0.08)}" y="${f(y + rd * 0.18)}" width="${f(rw * 0.84)}" height="${f(rd * 0.74)}" rx="8" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
477
+ `<rect x="${f(x + rw * 0.12)}" y="${f(y + rd * 0.06)}" width="${f(rw * 0.76)}" height="${f(rd * 0.18)}" rx="4" fill="${fillAccent}" stroke="${strokeMain}" stroke-width="2"/>`,
478
+ )
479
+ } else if (/fridge|freezer|washing-machine|dishwasher|microwave/.test(s)) {
480
+ drawFrontTick = false
481
+ parts.push(
482
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="3" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
483
+ `<line x1="${f(x)}" y1="${f(y + rd - 10)}" x2="${f(x + rw)}" y2="${f(y + rd - 10)}" stroke="${strokeMain}" stroke-width="1.6"/>`,
484
+ `<line x1="${f(x + rw * 0.25)}" y1="${f(y + rd - 5)}" x2="${f(x + rw * 0.75)}" y2="${f(y + rd - 5)}" stroke="${strokeMain}" stroke-width="2.4" stroke-linecap="round"/>`,
485
+ )
486
+ } else {
487
+ // Clean architectural fallback with crisp inner inset border
488
+ const isRound = /round|circular|ball|hydrant|sprinkler|smoke-detector|toilet-brush/.test(s)
489
+ if (isRound) {
490
+ parts.push(
491
+ `<ellipse cx="64" cy="64" rx="${f(rw / 2)}" ry="${f(rd / 2)}" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
492
+ `<ellipse cx="64" cy="64" rx="${f(rw * 0.34)}" ry="${f(rd * 0.34)}" fill="none" stroke="${strokeDetail}" stroke-width="1.2"/>`,
493
+ )
494
+ } else {
495
+ parts.push(
496
+ `<rect x="${f(x)}" y="${f(y)}" width="${f(rw)}" height="${f(rd)}" rx="4" fill="${fillBase}" stroke="${strokeMain}" stroke-width="2.2"/>`,
497
+ `<rect x="${f(x + 5)}" y="${f(y + 5)}" width="${f(Math.max(4, rw - 10))}" height="${f(Math.max(4, rd - 10))}" rx="2" fill="none" stroke="${strokeDetail}" stroke-width="1.1"/>`,
498
+ )
499
+ }
500
+ }
501
+
502
+ if (drawFrontTick) {
503
+ const tickY = y + rd
504
+ parts.push(`<line x1="56" y1="${f(tickY)}" x2="72" y2="${f(tickY)}" stroke="#0f172a" stroke-width="3.2" stroke-linecap="round"/>`)
505
+ }
506
+
507
+ return `<?xml version="1.0" encoding="UTF-8"?>
508
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128" width="128" height="128">
509
+ <title>${escapeXml(slug)}</title>
510
+ ${parts.join('\n ')}
511
+ </svg>`
512
+ }
513
+
514
+ type ProjectedPoly = {
515
+ readonly points: string
516
+ readonly depth: number
517
+ readonly fill: string
518
+ readonly stroke: string
519
+ readonly opacity: number
520
+ }
521
+
522
+ /**
523
+ * True per-pixel Z-buffered software 3D rasterizer (pure TypeScript, zero GPU/browser deps).
524
+ * Renders crisp 2x2 supersampled studio thumbnails (`RGBA8`) with exact per-pixel depth testing,
525
+ * eliminating painter's-algorithm diagonal triangle overlap on flush doors, screens, felt, and trim.
526
+ */
527
+ export function renderIsometricPreviewRgba(
528
+ group: THREE.Group,
529
+ outSize = 256,
530
+ ): { width: number; height: number; rgba: Uint8ClampedArray } {
531
+ const safeOut = Number.isFinite(outSize) ? Math.min(1024, Math.max(32, Math.round(outSize))) : 256
532
+ const ss = 2
533
+ const W = safeOut * ss
534
+ const H = safeOut * ss
535
+ const colorBuf = new Float32Array(W * H * 4)
536
+ const zBuf = new Float32Array(W * H)
537
+ zBuf.fill(Number.NEGATIVE_INFINITY)
538
+
539
+ // Soft radial studio background + subtle floor contact shadow
540
+ const cxBg = W * 0.5
541
+ const cyBg = H * 0.42
542
+ const maxR = W * 0.65
543
+ const shCx = W * 0.5
544
+ const shCy = H * 0.84
545
+ const shRx = W * 0.3
546
+ const shRy = H * 0.068
547
+ for (let y = 0; y < H; y++) {
548
+ for (let x = 0; x < W; x++) {
549
+ const idx = (y * W + x) * 4
550
+ const dist = Math.min(1, Math.hypot(x - cxBg, y - cyBg) / maxR)
551
+ let r = 248 + (226 - 248) * dist
552
+ let g = 250 + (232 - 250) * dist
553
+ let b = 252 + (240 - 252) * dist
554
+
555
+ const sdx = (x - shCx) / shRx
556
+ const sdy = (y - shCy) / shRy
557
+ const sd2 = sdx * sdx + sdy * sdy
558
+ if (sd2 < 1) {
559
+ const sAlpha = 0.2 * (1 - sd2) * (1 - sd2)
560
+ r = r * (1 - sAlpha) + 140 * sAlpha
561
+ g = g * (1 - sAlpha) + 155 * sAlpha
562
+ b = b * (1 - sAlpha) + 172 * sAlpha
563
+ }
564
+ colorBuf[idx] = r
565
+ colorBuf[idx + 1] = g
566
+ colorBuf[idx + 2] = b
567
+ colorBuf[idx + 3] = 255
568
+ }
569
+ }
570
+
571
+ group.updateMatrixWorld(true)
572
+ const rootBox = new THREE.Box3().setFromObject(group)
573
+ const rootCenter = new THREE.Vector3()
574
+ const rootSize = new THREE.Vector3()
575
+ rootBox.getCenter(rootCenter)
576
+ rootBox.getSize(rootSize)
577
+ const maxDim = Math.max(rootSize.x, rootSize.y, rootSize.z, 0.05)
578
+
579
+ const az = (38 * Math.PI) / 180
580
+ const el = (26 * Math.PI) / 180
581
+ const cosA = Math.cos(az)
582
+ const sinA = Math.sin(az)
583
+ const cosE = Math.cos(el)
584
+ const sinE = Math.sin(el)
585
+
586
+ const camDir = new THREE.Vector3(sinA * cosE, sinE, cosA * cosE).normalize()
587
+ const keyDir = new THREE.Vector3(0.55, 0.78, 0.45).normalize()
588
+ const fillDir = new THREE.Vector3(-0.65, 0.35, 0.55).normalize()
589
+ const halfVec = camDir.clone().add(keyDir).normalize()
590
+
591
+ const project = (v: THREE.Vector3): [number, number, number] => {
592
+ const dx = v.x - rootCenter.x
593
+ const dy = v.y - rootCenter.y
594
+ const dz = v.z - rootCenter.z
595
+ const rx = dx * cosA - dz * sinA
596
+ const rz = dx * sinA + dz * cosA
597
+ const ry = dy * cosE - rz * sinE
598
+ const depth = dy * sinE + rz * cosE
599
+ return [rx, ry, depth]
600
+ }
601
+
602
+ type RastTri = {
603
+ x0: number; y0: number; z0: number
604
+ x1: number; y1: number; z1: number
605
+ x2: number; y2: number; z2: number
606
+ r: number; g: number; b: number; alpha: number
607
+ }
608
+ const opaqueTris: RastTri[] = []
609
+ const transTris: RastTri[] = []
610
+ let minX = Number.POSITIVE_INFINITY
611
+ let maxX = Number.NEGATIVE_INFINITY
612
+ let minY = Number.POSITIVE_INFINITY
613
+ let maxY = Number.NEGATIVE_INFINITY
614
+
615
+ const v0 = new THREE.Vector3()
616
+ const v1 = new THREE.Vector3()
617
+ const v2 = new THREE.Vector3()
618
+ const e1 = new THREE.Vector3()
619
+ const e2 = new THREE.Vector3()
620
+ const faceN = new THREE.Vector3()
621
+
622
+ group.traverse((obj) => {
623
+ const mesh = obj as THREE.Mesh
624
+ if (!mesh.isMesh || !mesh.geometry) return
625
+ const posAttr = mesh.geometry.getAttribute('position')
626
+ if (!posAttr || posAttr.count < 3) return
627
+
628
+ const mat = Array.isArray(mesh.material) ? mesh.material[0] : mesh.material
629
+ const std = mat as THREE.MeshStandardMaterial | undefined
630
+ const col = std?.color ? std.color.clone() : new THREE.Color(0x94a3b8)
631
+ const em = std?.emissive ? std.emissive.clone() : null
632
+ const emInt = typeof std?.emissiveIntensity === 'number' ? std.emissiveIntensity : 0
633
+ const roughness = typeof std?.roughness === 'number' ? std.roughness : 0.6
634
+ const metalness = typeof std?.metalness === 'number' ? std.metalness : 0.0
635
+ const isDoubleSide = std?.side === THREE.DoubleSide
636
+ const alpha =
637
+ std?.transparent && typeof std.opacity === 'number' ? Math.max(0.22, Math.min(1, std.opacity)) : 1
638
+
639
+ const indexAttr = mesh.geometry.getIndex()
640
+ const triCount = indexAttr ? Math.floor(indexAttr.count / 3) : Math.floor(posAttr.count / 3)
641
+ const matW = mesh.matrixWorld
642
+
643
+ for (let t = 0; t < triCount; t++) {
644
+ const i0 = indexAttr ? indexAttr.getX(t * 3) : t * 3
645
+ const i1 = indexAttr ? indexAttr.getX(t * 3 + 1) : t * 3 + 1
646
+ const i2 = indexAttr ? indexAttr.getX(t * 3 + 2) : t * 3 + 2
647
+
648
+ v0.fromBufferAttribute(posAttr, i0).applyMatrix4(matW)
649
+ v1.fromBufferAttribute(posAttr, i1).applyMatrix4(matW)
650
+ v2.fromBufferAttribute(posAttr, i2).applyMatrix4(matW)
651
+
652
+ e1.subVectors(v1, v0)
653
+ e2.subVectors(v2, v0)
654
+ faceN.crossVectors(e1, e2)
655
+ const area2 = faceN.length()
656
+ if (area2 < 1e-9) continue
657
+ faceN.divideScalar(area2)
658
+
659
+ const viewDot = faceN.dot(camDir)
660
+ if (!isDoubleSide && viewDot <= -0.02) continue
661
+ if (viewDot < 0) faceN.negate()
662
+
663
+ const p0 = project(v0)
664
+ const p1 = project(v1)
665
+ const p2 = project(v2)
666
+ if (
667
+ !Number.isFinite(p0[0]) || !Number.isFinite(p0[1]) || !Number.isFinite(p0[2]) ||
668
+ !Number.isFinite(p1[0]) || !Number.isFinite(p1[1]) || !Number.isFinite(p1[2]) ||
669
+ !Number.isFinite(p2[0]) || !Number.isFinite(p2[1]) || !Number.isFinite(p2[2])
670
+ ) {
671
+ continue
672
+ }
673
+
674
+ if (p0[0] < minX) minX = p0[0]
675
+ if (p1[0] < minX) minX = p1[0]
676
+ if (p2[0] < minX) minX = p2[0]
677
+ if (p0[0] > maxX) maxX = p0[0]
678
+ if (p1[0] > maxX) maxX = p1[0]
679
+ if (p2[0] > maxX) maxX = p2[0]
680
+ if (p0[1] < minY) minY = p0[1]
681
+ if (p1[1] < minY) minY = p1[1]
682
+ if (p2[1] < minY) minY = p2[1]
683
+ if (p0[1] > maxY) maxY = p0[1]
684
+ if (p1[1] > maxY) maxY = p1[1]
685
+ if (p2[1] > maxY) maxY = p2[1]
686
+
687
+ const key = Math.max(0, faceN.dot(keyDir))
688
+ const fill = Math.max(0, faceN.dot(fillDir))
689
+ const hemi = 0.34 + 0.16 * (faceN.y * 0.5 + 0.5)
690
+ const specPow = Math.max(4, (1 - roughness) * 44)
691
+ const spec = Math.pow(Math.max(0, faceN.dot(halfVec)), specPow) * (0.14 + metalness * 0.35)
692
+ const diffScale = hemi + key * 0.62 + fill * 0.22
693
+
694
+ const rLin = Math.min(1, col.r * diffScale + spec + (em ? em.r * emInt * 0.45 : 0))
695
+ const gLin = Math.min(1, col.g * diffScale + spec + (em ? em.g * emInt * 0.45 : 0))
696
+ const bLin = Math.min(1, col.b * diffScale + spec + (em ? em.b * emInt * 0.45 : 0))
697
+ const r = Math.min(255, Math.pow(Math.max(0, rLin), 1 / 2.1) * 255)
698
+ const g = Math.min(255, Math.pow(Math.max(0, gLin), 1 / 2.1) * 255)
699
+ const b = Math.min(255, Math.pow(Math.max(0, bLin), 1 / 2.1) * 255)
700
+
701
+ const tri: RastTri = {
702
+ x0: p0[0], y0: p0[1], z0: p0[2],
703
+ x1: p1[0], y1: p1[1], z1: p1[2],
704
+ x2: p2[0], y2: p2[1], z2: p2[2],
705
+ r, g, b, alpha,
706
+ }
707
+ if (alpha < 0.99) transTris.push(tri)
708
+ else opaqueTris.push(tri)
709
+ }
710
+ })
711
+
712
+ if (!Number.isFinite(minX) || !Number.isFinite(maxX) || !Number.isFinite(minY) || !Number.isFinite(maxY)) {
713
+ minX = -0.5
714
+ maxX = 0.5
715
+ minY = -0.5
716
+ maxY = 0.5
717
+ }
718
+ const spanX = Math.max(maxX - minX, maxDim * 0.2)
719
+ const spanY = Math.max(maxY - minY, maxDim * 0.2)
720
+ const scale = Math.min((W * 0.78) / spanX, (H * 0.75) / spanY)
721
+ const midX = (minX + maxX) / 2
722
+ const midY = (minY + maxY) / 2
723
+ const ox = W * 0.5
724
+ const oy = H * 0.515
725
+
726
+ const rasterize = (tri: RastTri, isTrans: boolean) => {
727
+ const sx0 = ox + (tri.x0 - midX) * scale
728
+ const sy0 = oy - (tri.y0 - midY) * scale
729
+ const sx1 = ox + (tri.x1 - midX) * scale
730
+ const sy1 = oy - (tri.y1 - midY) * scale
731
+ const sx2 = ox + (tri.x2 - midX) * scale
732
+ const sy2 = oy - (tri.y2 - midY) * scale
733
+
734
+ const minPx = Math.max(0, Math.floor(Math.min(sx0, sx1, sx2)))
735
+ const maxPx = Math.min(W - 1, Math.ceil(Math.max(sx0, sx1, sx2)))
736
+ const minPy = Math.max(0, Math.floor(Math.min(sy0, sy1, sy2)))
737
+ const maxPy = Math.min(H - 1, Math.ceil(Math.max(sy0, sy1, sy2)))
738
+
739
+ const denom = (sy1 - sy2) * (sx0 - sx2) + (sx2 - sx1) * (sy0 - sy2)
740
+ if (Math.abs(denom) < 1e-6) return
741
+ const invDenom = 1 / denom
742
+
743
+ for (let py = minPy; py <= maxPy; py++) {
744
+ const pcy = py + 0.5
745
+ for (let px = minPx; px <= maxPx; px++) {
746
+ const pcx = px + 0.5
747
+ const w0 = ((sy1 - sy2) * (pcx - sx2) + (sx2 - sx1) * (pcy - sy2)) * invDenom
748
+ if (w0 < -1e-4) continue
749
+ const w1 = ((sy2 - sy0) * (pcx - sx2) + (sx0 - sx2) * (pcy - sy2)) * invDenom
750
+ if (w1 < -1e-4) continue
751
+ const w2 = 1 - w0 - w1
752
+ if (w2 < -1e-4) continue
753
+
754
+ const z = w0 * tri.z0 + w1 * tri.z1 + w2 * tri.z2
755
+ const zi = py * W + px
756
+ if (isTrans ? z <= zBuf[zi]! + 1e-5 : z <= zBuf[zi]!) continue
757
+ const ci = zi * 4
758
+ if (isTrans) {
759
+ const a = tri.alpha
760
+ colorBuf[ci] = colorBuf[ci]! * (1 - a) + tri.r * a
761
+ colorBuf[ci + 1] = colorBuf[ci + 1]! * (1 - a) + tri.g * a
762
+ colorBuf[ci + 2] = colorBuf[ci + 2]! * (1 - a) + tri.b * a
763
+ } else {
764
+ zBuf[zi] = z
765
+ colorBuf[ci] = tri.r
766
+ colorBuf[ci + 1] = tri.g
767
+ colorBuf[ci + 2] = tri.b
768
+ }
769
+ }
770
+ }
771
+ }
772
+
773
+ for (const t of opaqueTris) rasterize(t, false)
774
+ transTris.sort((a, b) => (a.z0 + a.z1 + a.z2) - (b.z0 + b.z1 + b.z2))
775
+ for (const t of transTris) rasterize(t, true)
776
+
777
+ // 2x2 box-filter downsample for crisp anti-aliased edges
778
+ const rgba = new Uint8ClampedArray(safeOut * safeOut * 4)
779
+ for (let y = 0; y < safeOut; y++) {
780
+ for (let x = 0; x < safeOut; x++) {
781
+ let r = 0, g = 0, b = 0
782
+ for (let dy = 0; dy < ss; dy++) {
783
+ for (let dx = 0; dx < ss; dx++) {
784
+ const si = ((y * ss + dy) * W + (x * ss + dx)) * 4
785
+ r += colorBuf[si]!
786
+ g += colorBuf[si + 1]!
787
+ b += colorBuf[si + 2]!
788
+ }
789
+ }
790
+ const di = (y * safeOut + x) * 4
791
+ rgba[di] = Math.round(r / 4)
792
+ rgba[di + 1] = Math.round(g / 4)
793
+ rgba[di + 2] = Math.round(b / 4)
794
+ rgba[di + 3] = 255
795
+ }
796
+ }
797
+ return { width: safeOut, height: safeOut, rgba }
798
+ }
799
+
800
+ /**
801
+ * Deterministic software 3D triangle renderer (SVG).
802
+ */
803
+ export function renderIsometricPreviewSvg(
804
+ group: THREE.Group,
805
+ label: string,
806
+ _category?: string,
807
+ ): string {
808
+ group.updateMatrixWorld(true)
809
+ const rootBox = new THREE.Box3().setFromObject(group)
810
+ const rootCenter = new THREE.Vector3()
811
+ const rootSize = new THREE.Vector3()
812
+ rootBox.getCenter(rootCenter)
813
+ rootBox.getSize(rootSize)
814
+ const maxDim = Math.max(rootSize.x, rootSize.y, rootSize.z, 0.05)
815
+
816
+ // Three-quarter architectural isometric camera (azimuth 38 deg, elevation 26 deg)
817
+ const az = (38 * Math.PI) / 180
818
+ const el = (26 * Math.PI) / 180
819
+ const cosA = Math.cos(az)
820
+ const sinA = Math.sin(az)
821
+ const cosE = Math.cos(el)
822
+ const sinE = Math.sin(el)
823
+
824
+ // Camera view direction (pointing toward camera from origin)
825
+ const camDir = new THREE.Vector3(sinA * cosE, sinE, cosA * cosE).normalize()
826
+ // Studio 3-point lighting directions (world space)
827
+ const keyDir = new THREE.Vector3(0.55, 0.78, 0.45).normalize()
828
+ const fillDir = new THREE.Vector3(-0.65, 0.35, 0.55).normalize()
829
+
830
+ const project = (v: THREE.Vector3): [number, number, number] => {
831
+ const dx = v.x - rootCenter.x
832
+ const dy = v.y - rootCenter.y
833
+ const dz = v.z - rootCenter.z
834
+ const rx = dx * cosA - dz * sinA
835
+ const rz = dx * sinA + dz * cosA
836
+ const ry = dy * cosE - rz * sinE
837
+ const depth = dy * sinE + rz * cosE
838
+ return [rx, ry, depth]
839
+ }
840
+
841
+ let minX = Number.POSITIVE_INFINITY
842
+ let maxX = Number.NEGATIVE_INFINITY
843
+ let minY = Number.POSITIVE_INFINITY
844
+ let maxY = Number.NEGATIVE_INFINITY
845
+
846
+ const rawTris: Array<{
847
+ pts: [number, number, number, number, number, number]
848
+ depth: number
849
+ fill: string
850
+ opacity: number
851
+ }> = []
852
+
853
+ const v0 = new THREE.Vector3()
854
+ const v1 = new THREE.Vector3()
855
+ const v2 = new THREE.Vector3()
856
+ const e1 = new THREE.Vector3()
857
+ const e2 = new THREE.Vector3()
858
+ const faceN = new THREE.Vector3()
859
+
860
+ group.traverse((obj) => {
861
+ const mesh = obj as THREE.Mesh
862
+ if (!mesh.isMesh || !mesh.geometry) return
863
+ const posAttr = mesh.geometry.getAttribute('position')
864
+ if (!posAttr || posAttr.count < 3) return
865
+
866
+ const mat = Array.isArray(mesh.material) ? mesh.material[0] : mesh.material
867
+ const std = mat as THREE.MeshStandardMaterial | undefined
868
+ const col = std?.color ? std.color.clone() : new THREE.Color(0x94a3b8)
869
+ const em = std?.emissive ? std.emissive.clone() : null
870
+ const emInt = typeof std?.emissiveIntensity === 'number' ? std.emissiveIntensity : 0
871
+ const isDoubleSide = std?.side === THREE.DoubleSide
872
+ const opacity =
873
+ std?.transparent && typeof std.opacity === 'number' ? Math.max(0.28, Math.min(1, std.opacity)) : 1
874
+
875
+ const indexAttr = mesh.geometry.getIndex()
876
+ const triCount = indexAttr ? Math.floor(indexAttr.count / 3) : Math.floor(posAttr.count / 3)
877
+ const matW = mesh.matrixWorld
878
+
879
+ for (let t = 0; t < triCount; t++) {
880
+ const i0 = indexAttr ? indexAttr.getX(t * 3) : t * 3
881
+ const i1 = indexAttr ? indexAttr.getX(t * 3 + 1) : t * 3 + 1
882
+ const i2 = indexAttr ? indexAttr.getX(t * 3 + 2) : t * 3 + 2
883
+
884
+ v0.fromBufferAttribute(posAttr, i0).applyMatrix4(matW)
885
+ v1.fromBufferAttribute(posAttr, i1).applyMatrix4(matW)
886
+ v2.fromBufferAttribute(posAttr, i2).applyMatrix4(matW)
887
+
888
+ e1.subVectors(v1, v0)
889
+ e2.subVectors(v2, v0)
890
+ faceN.crossVectors(e1, e2)
891
+ const area2 = faceN.length()
892
+ if (area2 < 1e-9) continue
893
+ faceN.divideScalar(area2)
894
+
895
+ const viewDot = faceN.dot(camDir)
896
+ if (!isDoubleSide && viewDot <= -0.02) continue
897
+ if (viewDot < 0) faceN.negate()
898
+
899
+ const p0 = project(v0)
900
+ const p1 = project(v1)
901
+ const p2 = project(v2)
902
+ if (
903
+ !Number.isFinite(p0[0]) || !Number.isFinite(p0[1]) || !Number.isFinite(p0[2]) ||
904
+ !Number.isFinite(p1[0]) || !Number.isFinite(p1[1]) || !Number.isFinite(p1[2]) ||
905
+ !Number.isFinite(p2[0]) || !Number.isFinite(p2[1]) || !Number.isFinite(p2[2])
906
+ ) {
907
+ continue
908
+ }
909
+
910
+ if (p0[0] < minX) minX = p0[0]
911
+ if (p1[0] < minX) minX = p1[0]
912
+ if (p2[0] < minX) minX = p2[0]
913
+ if (p0[0] > maxX) maxX = p0[0]
914
+ if (p1[0] > maxX) maxX = p1[0]
915
+ if (p2[0] > maxX) maxX = p2[0]
916
+ if (p0[1] < minY) minY = p0[1]
917
+ if (p1[1] < minY) minY = p1[1]
918
+ if (p2[1] < minY) minY = p2[1]
919
+ if (p0[1] > maxY) maxY = p0[1]
920
+ if (p1[1] > maxY) maxY = p1[1]
921
+ if (p2[1] > maxY) maxY = p2[1]
922
+
923
+ const depth = (p0[2] + p1[2] + p2[2]) / 3
924
+ const key = Math.max(0, faceN.dot(keyDir))
925
+ const fill = Math.max(0, faceN.dot(fillDir))
926
+ const hemi = 0.36 + 0.14 * (faceN.y * 0.5 + 0.5)
927
+ const shade = Math.min(1.22, hemi + key * 0.62 + fill * 0.22)
928
+
929
+ const rLin = Math.min(1, col.r * shade + (em ? em.r * emInt * 0.45 : 0))
930
+ const gLin = Math.min(1, col.g * shade + (em ? em.g * emInt * 0.45 : 0))
931
+ const bLin = Math.min(1, col.b * shade + (em ? em.b * emInt * 0.45 : 0))
932
+ // sRGB gamma lift for crisp studio thumbnails
933
+ const r = Math.min(255, Math.round(Math.pow(Math.max(0, rLin), 1 / 2.0) * 255))
934
+ const g = Math.min(255, Math.round(Math.pow(Math.max(0, gLin), 1 / 2.0) * 255))
935
+ const b = Math.min(255, Math.round(Math.pow(Math.max(0, bLin), 1 / 2.0) * 255))
936
+
937
+ rawTris.push({
938
+ pts: [p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]],
939
+ depth,
940
+ fill: `rgb(${r},${g},${b})`,
941
+ opacity,
942
+ })
943
+ }
944
+ })
945
+
946
+ if (!Number.isFinite(minX) || !Number.isFinite(maxX) || !Number.isFinite(minY) || !Number.isFinite(maxY)) {
947
+ minX = -0.5
948
+ maxX = 0.5
949
+ minY = -0.5
950
+ maxY = 0.5
951
+ }
952
+ const spanX = Math.max(maxX - minX, maxDim * 0.2)
953
+ const spanY = Math.max(maxY - minY, maxDim * 0.2)
954
+ const scale = Math.min(200 / spanX, 194 / spanY)
955
+ const midX = (minX + maxX) / 2
956
+ const midY = (minY + maxY) / 2
957
+
958
+ rawTris.sort((a, b) => a.depth - b.depth)
959
+ const polys: ProjectedPoly[] = rawTris.map((t) => {
960
+ const x0 = (128 + (t.pts[0] - midX) * scale).toFixed(1)
961
+ const y0 = (132 - (t.pts[1] - midY) * scale).toFixed(1)
962
+ const x1 = (128 + (t.pts[2] - midX) * scale).toFixed(1)
963
+ const y1 = (132 - (t.pts[3] - midY) * scale).toFixed(1)
964
+ const x2 = (128 + (t.pts[4] - midX) * scale).toFixed(1)
965
+ const y2 = (132 - (t.pts[5] - midY) * scale).toFixed(1)
966
+ return {
967
+ points: `${x0},${y0} ${x1},${y1} ${x2},${y2}`,
968
+ depth: t.depth,
969
+ fill: t.fill,
970
+ stroke: t.fill,
971
+ opacity: t.opacity,
972
+ }
973
+ })
974
+
975
+ return `<?xml version="1.0" encoding="UTF-8"?>
976
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="256" height="256">
977
+ <title>${escapeXml(label)}</title>
978
+ <defs>
979
+ <radialGradient id="bg" cx="50%" cy="42%" r="65%">
980
+ <stop offset="0%" stop-color="#f8fafc"/>
981
+ <stop offset="100%" stop-color="#e2e8f0"/>
982
+ </radialGradient>
983
+ </defs>
984
+ <rect width="256" height="256" rx="16" fill="url(#bg)"/>
985
+ <ellipse cx="128" cy="218" rx="74" ry="16" fill="#94a3b8" fill-opacity="0.22"/>
986
+ ${polys
987
+ .map(
988
+ (p) =>
989
+ `<polygon points="${p.points}" fill="${p.fill}" fill-opacity="${p.opacity.toFixed(2)}" stroke="${p.stroke}" stroke-width="0.45" stroke-linejoin="round"/>`,
990
+ )
991
+ .join('\n ')}
992
+ </svg>`
993
+ }