@kruschdev/pascal-plugin-furnishings 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +70 -2
- package/docs/ARCHITECTURE.md +56 -0
- package/docs/CAD-CATALOG.md +124 -0
- package/package.json +92 -4
- package/src/agent-operations.ts +851 -0
- package/src/agent-tools.ts +273 -0
- package/src/cad/catalog.ts +107 -0
- package/src/cad/electronics-geometry.ts +298 -0
- package/src/cad/furniture-geometry.ts +846 -0
- package/src/cad/index.ts +10 -0
- package/src/cad/materials.ts +274 -0
- package/src/cad/plants-geometry.ts +506 -0
- package/src/furnishings/audit.ts +711 -0
- package/src/furnishings/autopopulate.ts +1474 -0
- package/src/furnishings/catalog.ts +1848 -0
- package/src/furnishings/geometry.ts +753 -0
- package/src/furnishings/index.ts +14 -0
- package/src/furnishings/schedule.ts +503 -0
- package/src/furnishings/spatial-grounding.ts +1753 -0
- package/src/furnishings/types.ts +416 -0
- package/src/index.ts +145 -0
- package/src/nodes/assembly.ts +105 -0
- package/src/nodes/electronics.ts +187 -0
- package/src/nodes/glb-hydration.ts +269 -0
- package/src/nodes/item.ts +202 -0
- package/src/nodes/parametrics.ts +102 -0
- package/src/nodes/plant.ts +197 -0
- package/src/render.ts +55 -0
- package/src/ui/chat-card.tsx +119 -0
- package/src/ui/furnishings-panel.tsx +679 -0
- package/src/ui/inspector-card.tsx +350 -0
- package/src/ui/schedule-view.tsx +336 -0
- package/src/ui/store.ts +94 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `furnishings:electronics` — TVs, Speaker Systems, Projectors, AV Racks & Computer Workstations Node.
|
|
3
|
+
* Render-safe module: zero `@pascal-app/editor` or `react` imports.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
BaseNode,
|
|
8
|
+
type FloorplanGeometry,
|
|
9
|
+
type GeometryContext,
|
|
10
|
+
type NodeDefinition,
|
|
11
|
+
type NodePort,
|
|
12
|
+
nodeType,
|
|
13
|
+
objectId,
|
|
14
|
+
} from '@pascal-app/core'
|
|
15
|
+
import { Group } from 'three'
|
|
16
|
+
import { z } from 'zod'
|
|
17
|
+
import { buildElectronicsCad } from '../cad/electronics-geometry'
|
|
18
|
+
import { isSafeAssetBaseUrl } from '../furnishings/catalog'
|
|
19
|
+
import {
|
|
20
|
+
DOMAIN_COLORS,
|
|
21
|
+
ELECTRONICS_CHANNEL_ROLES,
|
|
22
|
+
ELECTRONICS_FINISHES,
|
|
23
|
+
ELECTRONICS_MOUNT_STYLES,
|
|
24
|
+
SCREEN_STATES,
|
|
25
|
+
} from '../furnishings/types'
|
|
26
|
+
import { attachProceduralWithOptionalGlb } from './glb-hydration'
|
|
27
|
+
|
|
28
|
+
export const FurnishingsElectronicsNode = BaseNode.extend({
|
|
29
|
+
id: objectId('frnav'),
|
|
30
|
+
type: nodeType('furnishings:electronics'),
|
|
31
|
+
itemKind: z.string().min(2).default('tv-oled-wall'),
|
|
32
|
+
position: z.tuple([z.number().finite(), z.number().finite(), z.number().finite()]).default([0, 1.05, 0]),
|
|
33
|
+
rotation: z.number().finite().default(0),
|
|
34
|
+
roomName: z.string().default('Living Room'),
|
|
35
|
+
screenDiagonalIn: z.number().finite().min(13).max(150).optional(),
|
|
36
|
+
mountStyle: z.enum(ELECTRONICS_MOUNT_STYLES).default('wall-mount'),
|
|
37
|
+
finish: z.enum(ELECTRONICS_FINISHES).default('matte-black'),
|
|
38
|
+
widthM: z.number().finite().min(0.12).max(5).default(1.45),
|
|
39
|
+
depthM: z.number().finite().min(0.03).max(2).default(0.06),
|
|
40
|
+
heightM: z.number().finite().min(0.05).max(3).default(0.83),
|
|
41
|
+
wattageW: z.number().finite().min(0).max(4000).default(165),
|
|
42
|
+
channelRole: z.enum(ELECTRONICS_CHANNEL_ROLES).default('aux'),
|
|
43
|
+
screenState: z.enum(SCREEN_STATES).default('ambient'),
|
|
44
|
+
quantity: z.number().int().min(1).max(100).default(1),
|
|
45
|
+
unitPriceOverrideUsd: z.number().finite().min(0).optional(),
|
|
46
|
+
customSku: z.string().optional(),
|
|
47
|
+
customBrand: z.string().optional(),
|
|
48
|
+
customGlbUrl: z
|
|
49
|
+
.string()
|
|
50
|
+
.refine((v) => v.trim() === '' || isSafeAssetBaseUrl(v), 'Unsafe customGlbUrl')
|
|
51
|
+
.optional(),
|
|
52
|
+
electricalDemand: z.record(z.string(), z.unknown()).optional(),
|
|
53
|
+
notes: z.string().default(''),
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export type FurnishingsElectronicsNode = z.infer<typeof FurnishingsElectronicsNode>
|
|
57
|
+
|
|
58
|
+
export function getFurnishingsElectronicsPorts(node: FurnishingsElectronicsNode): NodePort[] {
|
|
59
|
+
const [x, y, zPos] = node.position
|
|
60
|
+
const r = typeof node.rotation === 'number' && Number.isFinite(node.rotation) ? node.rotation : 0
|
|
61
|
+
const dirX = Math.abs(-Math.sin(r)) < 1e-12 ? 0 : -Math.sin(r)
|
|
62
|
+
const dirZ = Math.abs(-Math.cos(r)) < 1e-12 ? 0 : -Math.cos(r)
|
|
63
|
+
return [
|
|
64
|
+
{ id: 'power-in', position: [x, y, zPos], direction: [0, -1, 0], diameter: 0.03, system: 'electrical' },
|
|
65
|
+
{ id: 'signal-av', position: [x, y + node.heightM * 0.3, zPos], direction: [dirX, 0, dirZ], diameter: 0.02, system: 'av' },
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function buildFurnishingsElectronicsGeometry(
|
|
70
|
+
node: FurnishingsElectronicsNode,
|
|
71
|
+
_ctx?: GeometryContext,
|
|
72
|
+
): Group {
|
|
73
|
+
const root = new Group()
|
|
74
|
+
root.name = `furnishings-electronics:${node.itemKind}`
|
|
75
|
+
const cad = buildElectronicsCad({
|
|
76
|
+
kind: node.itemKind,
|
|
77
|
+
widthM: node.widthM,
|
|
78
|
+
depthM: node.depthM,
|
|
79
|
+
heightM: node.heightM,
|
|
80
|
+
screenDiagonalIn: node.screenDiagonalIn,
|
|
81
|
+
mountStyle: node.mountStyle,
|
|
82
|
+
finish: node.finish,
|
|
83
|
+
screenState: node.screenState,
|
|
84
|
+
})
|
|
85
|
+
attachProceduralWithOptionalGlb({
|
|
86
|
+
root,
|
|
87
|
+
proceduralGroup: cad,
|
|
88
|
+
customGlbUrl: node.customGlbUrl,
|
|
89
|
+
tag: `${node.itemKind}:${node.id}`,
|
|
90
|
+
})
|
|
91
|
+
return root
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function buildFurnishingsElectronicsFloorplan(
|
|
95
|
+
node: FurnishingsElectronicsNode,
|
|
96
|
+
_ctx?: GeometryContext,
|
|
97
|
+
): FloorplanGeometry {
|
|
98
|
+
const stroke = DOMAIN_COLORS['electronics-av']
|
|
99
|
+
const cx = node.position[0]
|
|
100
|
+
const cz = node.position[2]
|
|
101
|
+
const cos = Math.cos(node.rotation)
|
|
102
|
+
const sin = Math.sin(node.rotation)
|
|
103
|
+
const hw = Math.max(0.14, node.widthM / 2)
|
|
104
|
+
const hd = Math.max(0.06, node.depthM / 2)
|
|
105
|
+
const corners: [number, number][] = (
|
|
106
|
+
[
|
|
107
|
+
[-hw, -hd],
|
|
108
|
+
[hw, -hd],
|
|
109
|
+
[hw, hd],
|
|
110
|
+
[-hw, hd],
|
|
111
|
+
] as const
|
|
112
|
+
).map(([lx, lz]) => [cx + lx * cos + lz * sin, cz - lx * sin + lz * cos])
|
|
113
|
+
|
|
114
|
+
// Directional projection cone line (+Z forward)
|
|
115
|
+
const dirLen = Math.min(0.65, Math.max(0.25, hw * 0.6))
|
|
116
|
+
const tipX = cx + (hd + dirLen) * sin
|
|
117
|
+
const tipZ = cz + (hd + dirLen) * cos
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
kind: 'group',
|
|
121
|
+
children: [
|
|
122
|
+
{ kind: 'polygon', points: corners, stroke, strokeWidth: 0.028, fill: `${stroke}33` },
|
|
123
|
+
{ kind: 'line', x1: cx, y1: cz, x2: tipX, y2: tipZ, stroke, strokeWidth: 0.022 },
|
|
124
|
+
],
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const furnishingsElectronicsRenderDefinition: NodeDefinition<typeof FurnishingsElectronicsNode> = {
|
|
129
|
+
kind: 'furnishings:electronics',
|
|
130
|
+
schemaVersion: 1,
|
|
131
|
+
schema: FurnishingsElectronicsNode,
|
|
132
|
+
category: 'furnish',
|
|
133
|
+
defaults: () => ({
|
|
134
|
+
object: 'node',
|
|
135
|
+
parentId: null,
|
|
136
|
+
visible: true,
|
|
137
|
+
metadata: {},
|
|
138
|
+
name: 'Wall-Mounted 65" 4K OLED TV',
|
|
139
|
+
itemKind: 'tv-oled-wall',
|
|
140
|
+
position: [0, 1.05, 0],
|
|
141
|
+
rotation: 0,
|
|
142
|
+
roomName: 'Living Room',
|
|
143
|
+
screenDiagonalIn: 65,
|
|
144
|
+
mountStyle: 'wall-mount',
|
|
145
|
+
finish: 'matte-black',
|
|
146
|
+
widthM: 1.45,
|
|
147
|
+
depthM: 0.06,
|
|
148
|
+
heightM: 0.83,
|
|
149
|
+
wattageW: 165,
|
|
150
|
+
channelRole: 'display',
|
|
151
|
+
screenState: 'ambient',
|
|
152
|
+
quantity: 1,
|
|
153
|
+
notes: '',
|
|
154
|
+
}),
|
|
155
|
+
capabilities: {
|
|
156
|
+
selectable: { hitVolume: 'bbox' },
|
|
157
|
+
movable: { axes: ['x', 'y', 'z'], gridSnap: true },
|
|
158
|
+
rotatable: { axes: ['y'], snapAngles: [Math.PI / 6] },
|
|
159
|
+
duplicable: true,
|
|
160
|
+
deletable: true,
|
|
161
|
+
snappable: {},
|
|
162
|
+
},
|
|
163
|
+
geometry: buildFurnishingsElectronicsGeometry,
|
|
164
|
+
geometryKey: (n) =>
|
|
165
|
+
JSON.stringify([
|
|
166
|
+
n.itemKind,
|
|
167
|
+
n.widthM,
|
|
168
|
+
n.depthM,
|
|
169
|
+
n.heightM,
|
|
170
|
+
n.screenDiagonalIn,
|
|
171
|
+
n.mountStyle,
|
|
172
|
+
n.finish,
|
|
173
|
+
n.screenState,
|
|
174
|
+
n.customGlbUrl ?? '',
|
|
175
|
+
]),
|
|
176
|
+
ports: getFurnishingsElectronicsPorts,
|
|
177
|
+
floorplan: buildFurnishingsElectronicsFloorplan,
|
|
178
|
+
presentation: {
|
|
179
|
+
label: 'TV, Audio & Computer System',
|
|
180
|
+
description: 'Parametric OLED TVs, Dolby soundbars, tower/surround speakers, subwoofers, projectors & desktop PC workstations.',
|
|
181
|
+
icon: { kind: 'iconify', name: 'lucide:tv' },
|
|
182
|
+
paletteSection: 'furnish',
|
|
183
|
+
},
|
|
184
|
+
mcp: {
|
|
185
|
+
description: 'Consumer electronics, home theater AV display/speaker, or computer workstation with wattage coordination.',
|
|
186
|
+
},
|
|
187
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe Optional `.glb` Hydration Helper (`src/nodes/glb-hydration.ts`).
|
|
3
|
+
*
|
|
4
|
+
* Allows any `furnishings:item`, `furnishings:electronics`, or `furnishings:plant` node to
|
|
5
|
+
* render its procedural Three.js CAD mesh instantaneously on frame 0, and optionally swap in
|
|
6
|
+
* a verified custom `.glb` mesh if `customGlbUrl` is provided.
|
|
7
|
+
*
|
|
8
|
+
* Security & GPU isolation invariants:
|
|
9
|
+
* - Validates every URL via `isSafeAssetBaseUrl()`.
|
|
10
|
+
* - Bounded LRU cache (`MAX_GLB_CACHE = 48`) with full GPU disposal on eviction or `clearFurnishingsGlbCache()`.
|
|
11
|
+
* - Always clones geometries, materials, and textures via `cloneIsolatedTemplate()`.
|
|
12
|
+
* - Guards async swap via `isAttachedToActiveHierarchy()` so disposed host groups never leak GPU memory.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
Group,
|
|
17
|
+
LoadingManager,
|
|
18
|
+
type Material,
|
|
19
|
+
type Mesh,
|
|
20
|
+
type Object3D,
|
|
21
|
+
type Texture,
|
|
22
|
+
} from 'three'
|
|
23
|
+
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
|
|
24
|
+
import { isSafeAssetBaseUrl } from '../furnishings/catalog'
|
|
25
|
+
|
|
26
|
+
const MAX_GLB_CACHE = 48
|
|
27
|
+
const glbTemplateCache = new Map<string, Promise<Group>>()
|
|
28
|
+
let lazyGlbLoader: GLTFLoader | null = null
|
|
29
|
+
|
|
30
|
+
const MATERIAL_TEXTURE_KEYS = [
|
|
31
|
+
'map',
|
|
32
|
+
'normalMap',
|
|
33
|
+
'roughnessMap',
|
|
34
|
+
'metalnessMap',
|
|
35
|
+
'aoMap',
|
|
36
|
+
'emissiveMap',
|
|
37
|
+
'alphaMap',
|
|
38
|
+
'bumpMap',
|
|
39
|
+
'lightMap',
|
|
40
|
+
'envMap',
|
|
41
|
+
'clearcoatMap',
|
|
42
|
+
'clearcoatRoughnessMap',
|
|
43
|
+
'clearcoatNormalMap',
|
|
44
|
+
'sheenColorMap',
|
|
45
|
+
'sheenRoughnessMap',
|
|
46
|
+
'transmissionMap',
|
|
47
|
+
'thicknessMap',
|
|
48
|
+
'specularIntensityMap',
|
|
49
|
+
'specularColorMap',
|
|
50
|
+
'iridescenceMap',
|
|
51
|
+
'iridescenceThicknessMap',
|
|
52
|
+
] as const
|
|
53
|
+
|
|
54
|
+
function getGlbLoader(): GLTFLoader {
|
|
55
|
+
if (!lazyGlbLoader) {
|
|
56
|
+
const manager = new LoadingManager()
|
|
57
|
+
manager.setURLModifier((url) => {
|
|
58
|
+
if (url.startsWith('blob:') || isSafeAssetBaseUrl(url)) return url
|
|
59
|
+
return 'data:,'
|
|
60
|
+
})
|
|
61
|
+
lazyGlbLoader = new GLTFLoader(manager)
|
|
62
|
+
}
|
|
63
|
+
return lazyGlbLoader
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function disposeMaterial(mat: Material, disposedTextures: Set<Texture>): void {
|
|
67
|
+
const rec = mat as unknown as Record<string, unknown>
|
|
68
|
+
for (const key of MATERIAL_TEXTURE_KEYS) {
|
|
69
|
+
const tex = rec[key] as (Texture & { __furnishingsDisposed?: boolean }) | null | undefined
|
|
70
|
+
if (tex && typeof tex.dispose === 'function' && !disposedTextures.has(tex)) {
|
|
71
|
+
disposedTextures.add(tex)
|
|
72
|
+
if (!tex.__furnishingsDisposed) {
|
|
73
|
+
tex.__furnishingsDisposed = true
|
|
74
|
+
tex.dispose()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
mat.dispose()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function disposeThreeSubtree(root: Object3D): void {
|
|
82
|
+
const disposedMaterials = new Set<Material>()
|
|
83
|
+
const disposedTextures = new Set<Texture>()
|
|
84
|
+
root.traverse((obj) => {
|
|
85
|
+
const candidate = obj as unknown as {
|
|
86
|
+
geometry?: { dispose?: () => void }
|
|
87
|
+
material?: Material | Material[]
|
|
88
|
+
}
|
|
89
|
+
candidate.geometry?.dispose?.()
|
|
90
|
+
if (candidate.material) {
|
|
91
|
+
const mats = Array.isArray(candidate.material) ? candidate.material : [candidate.material]
|
|
92
|
+
for (const m of mats) {
|
|
93
|
+
if (m && !disposedMaterials.has(m)) {
|
|
94
|
+
disposedMaterials.add(m)
|
|
95
|
+
disposeMaterial(m, disposedTextures)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type ClonedTextureEntry = {
|
|
103
|
+
texture: Texture & { __furnishingsDisposed?: boolean }
|
|
104
|
+
refs: Set<Material>
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function cloneMaterialWithTextures(
|
|
108
|
+
mat: Material,
|
|
109
|
+
clonedTextures: Map<Texture, ClonedTextureEntry>,
|
|
110
|
+
): Material {
|
|
111
|
+
const cloned = mat.clone()
|
|
112
|
+
const src = mat as unknown as Record<string, unknown>
|
|
113
|
+
const dst = cloned as unknown as Record<string, unknown>
|
|
114
|
+
const usedEntries = new Set<ClonedTextureEntry>()
|
|
115
|
+
for (const key of MATERIAL_TEXTURE_KEYS) {
|
|
116
|
+
const tex = src[key] as Texture | null | undefined
|
|
117
|
+
if (tex && typeof tex.clone === 'function') {
|
|
118
|
+
let entry = clonedTextures.get(tex)
|
|
119
|
+
if (!entry) {
|
|
120
|
+
const clonedTex = tex.clone() as Texture & { __furnishingsDisposed?: boolean }
|
|
121
|
+
clonedTex.needsUpdate = true
|
|
122
|
+
entry = { texture: clonedTex, refs: new Set<Material>() }
|
|
123
|
+
clonedTextures.set(tex, entry)
|
|
124
|
+
}
|
|
125
|
+
entry.refs.add(cloned)
|
|
126
|
+
usedEntries.add(entry)
|
|
127
|
+
dst[key] = entry.texture
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (usedEntries.size > 0) {
|
|
131
|
+
let handled = false
|
|
132
|
+
const onDispose = () => {
|
|
133
|
+
if (handled) return
|
|
134
|
+
handled = true
|
|
135
|
+
cloned.removeEventListener('dispose', onDispose)
|
|
136
|
+
for (const entry of usedEntries) {
|
|
137
|
+
entry.refs.delete(cloned)
|
|
138
|
+
if (entry.refs.size === 0 && !entry.texture.__furnishingsDisposed) {
|
|
139
|
+
entry.texture.__furnishingsDisposed = true
|
|
140
|
+
entry.texture.dispose()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
cloned.addEventListener('dispose', onDispose)
|
|
145
|
+
}
|
|
146
|
+
return cloned
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function cloneIsolatedTemplate(template: Group): Group {
|
|
150
|
+
const clone = template.clone(true)
|
|
151
|
+
const clonedTextures = new Map<Texture, ClonedTextureEntry>()
|
|
152
|
+
clone.traverse((obj) => {
|
|
153
|
+
const mesh = obj as Mesh
|
|
154
|
+
if (!mesh.isMesh) return
|
|
155
|
+
if (mesh.geometry) mesh.geometry = mesh.geometry.clone()
|
|
156
|
+
if (Array.isArray(mesh.material)) {
|
|
157
|
+
mesh.material = mesh.material.map((m) => (m ? cloneMaterialWithTextures(m, clonedTextures) : m))
|
|
158
|
+
} else if (mesh.material) {
|
|
159
|
+
mesh.material = cloneMaterialWithTextures(mesh.material, clonedTextures)
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
return clone
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function isAttachedToActiveHierarchy(obj: Object3D, originalRoot: Object3D): boolean {
|
|
166
|
+
if (originalRoot.parent === null) return false
|
|
167
|
+
let cur: Object3D | null = obj
|
|
168
|
+
let sawOriginalRoot = false
|
|
169
|
+
while (cur) {
|
|
170
|
+
if (cur === originalRoot) sawOriginalRoot = true
|
|
171
|
+
if (!cur.parent) {
|
|
172
|
+
return sawOriginalRoot && Boolean((cur as { isScene?: boolean }).isScene)
|
|
173
|
+
}
|
|
174
|
+
cur = cur.parent
|
|
175
|
+
}
|
|
176
|
+
return false
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function loadGlbTemplate(url: string): Promise<Group> {
|
|
180
|
+
if (!isSafeAssetBaseUrl(url)) {
|
|
181
|
+
return Promise.reject(new Error(`Unsafe GLB URL rejected: ${url}`))
|
|
182
|
+
}
|
|
183
|
+
const cached = glbTemplateCache.get(url)
|
|
184
|
+
if (cached) {
|
|
185
|
+
glbTemplateCache.delete(url)
|
|
186
|
+
glbTemplateCache.set(url, cached)
|
|
187
|
+
return cached
|
|
188
|
+
}
|
|
189
|
+
if (glbTemplateCache.size >= MAX_GLB_CACHE) {
|
|
190
|
+
const oldestKey = glbTemplateCache.keys().next().value
|
|
191
|
+
if (oldestKey !== undefined) {
|
|
192
|
+
const oldest = glbTemplateCache.get(oldestKey)
|
|
193
|
+
glbTemplateCache.delete(oldestKey)
|
|
194
|
+
if (oldest) void oldest.then(disposeThreeSubtree).catch(() => {})
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const pending = getGlbLoader()
|
|
198
|
+
.loadAsync(url)
|
|
199
|
+
.then((gltf) => {
|
|
200
|
+
const template = new Group()
|
|
201
|
+
template.name = `furnishings-glb:${url}`
|
|
202
|
+
gltf.scene.traverse((obj) => {
|
|
203
|
+
if ((obj as Mesh).isMesh) {
|
|
204
|
+
const mesh = obj as Mesh
|
|
205
|
+
mesh.castShadow = true
|
|
206
|
+
mesh.receiveShadow = true
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
template.add(gltf.scene)
|
|
210
|
+
return template
|
|
211
|
+
})
|
|
212
|
+
.catch((err) => {
|
|
213
|
+
glbTemplateCache.delete(url)
|
|
214
|
+
throw err
|
|
215
|
+
})
|
|
216
|
+
glbTemplateCache.set(url, pending)
|
|
217
|
+
return pending
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Clear the in-memory `GLTFLoader` template cache and dispose cached GPU resources.
|
|
222
|
+
*/
|
|
223
|
+
export function clearFurnishingsGlbCache(): void {
|
|
224
|
+
for (const pending of glbTemplateCache.values()) {
|
|
225
|
+
void pending.then(disposeThreeSubtree).catch(() => {})
|
|
226
|
+
}
|
|
227
|
+
glbTemplateCache.clear()
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Attach procedural geometry inside a child container and optionally swap in a validated
|
|
232
|
+
* `customGlbUrl` once fetched in browser environments.
|
|
233
|
+
*/
|
|
234
|
+
export function attachProceduralWithOptionalGlb(params: {
|
|
235
|
+
root: Group
|
|
236
|
+
proceduralGroup: Group
|
|
237
|
+
customGlbUrl?: string
|
|
238
|
+
tag: string
|
|
239
|
+
}): void {
|
|
240
|
+
const container = new Group()
|
|
241
|
+
container.name = `furnishings-container:${params.tag}`
|
|
242
|
+
container.add(params.proceduralGroup)
|
|
243
|
+
params.root.add(container)
|
|
244
|
+
|
|
245
|
+
const url = typeof params.customGlbUrl === 'string' ? params.customGlbUrl.trim() : ''
|
|
246
|
+
if (
|
|
247
|
+
!url ||
|
|
248
|
+
!isSafeAssetBaseUrl(url) ||
|
|
249
|
+
typeof window === 'undefined' ||
|
|
250
|
+
typeof fetch !== 'function'
|
|
251
|
+
) {
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
void loadGlbTemplate(url)
|
|
256
|
+
.then((template) => {
|
|
257
|
+
if (!isAttachedToActiveHierarchy(container, params.root) || params.proceduralGroup.parent !== container) {
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
const clone = cloneIsolatedTemplate(template)
|
|
261
|
+
clone.name = `furnishings-custom-glb:${params.tag}`
|
|
262
|
+
container.remove(params.proceduralGroup)
|
|
263
|
+
disposeThreeSubtree(params.proceduralGroup)
|
|
264
|
+
container.add(clone)
|
|
265
|
+
})
|
|
266
|
+
.catch(() => {
|
|
267
|
+
// Keep procedural CAD mesh gracefully if custom GLB URL is offline or invalid
|
|
268
|
+
})
|
|
269
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `furnishings:item` — Freestanding Furniture, Tables, Desks, Beds, Soft Goods & Outdoor Patio Node.
|
|
3
|
+
* Render-safe module: zero `@pascal-app/editor` or `react` imports.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
BaseNode,
|
|
8
|
+
type FloorplanGeometry,
|
|
9
|
+
type GeometryContext,
|
|
10
|
+
type NodeDefinition,
|
|
11
|
+
type NodePort,
|
|
12
|
+
nodeType,
|
|
13
|
+
objectId,
|
|
14
|
+
} from '@pascal-app/core'
|
|
15
|
+
import { Group } from 'three'
|
|
16
|
+
import { z } from 'zod'
|
|
17
|
+
import { buildFurnitureCad } from '../cad/furniture-geometry'
|
|
18
|
+
import { getFurnishingSpec, isSafeAssetBaseUrl } from '../furnishings/catalog'
|
|
19
|
+
import {
|
|
20
|
+
DOMAIN_COLORS,
|
|
21
|
+
FABRIC_FINISHES,
|
|
22
|
+
METAL_FINISHES,
|
|
23
|
+
STYLE_PRESETS,
|
|
24
|
+
WOOD_FINISHES,
|
|
25
|
+
} from '../furnishings/types'
|
|
26
|
+
import { attachProceduralWithOptionalGlb } from './glb-hydration'
|
|
27
|
+
|
|
28
|
+
export const FurnishingsItemNode = BaseNode.extend({
|
|
29
|
+
id: objectId('frnitm'),
|
|
30
|
+
type: nodeType('furnishings:item'),
|
|
31
|
+
itemKind: z.string().min(2).default('sofa-3seat'),
|
|
32
|
+
position: z.tuple([z.number().finite(), z.number().finite(), z.number().finite()]).default([0, 0, 0]),
|
|
33
|
+
rotation: z.number().finite().default(0),
|
|
34
|
+
roomName: z.string().default('Living Room'),
|
|
35
|
+
stylePreset: z.enum(STYLE_PRESETS).default('mid-century'),
|
|
36
|
+
woodFinish: z.enum(WOOD_FINISHES).default('american-walnut'),
|
|
37
|
+
fabricFinish: z.enum(FABRIC_FINISHES).default('warm-linen'),
|
|
38
|
+
metalFinish: z.enum(METAL_FINISHES).default('matte-black'),
|
|
39
|
+
widthM: z.number().finite().min(0.15).max(10).default(2.24),
|
|
40
|
+
depthM: z.number().finite().min(0.03).max(10).default(0.94),
|
|
41
|
+
heightM: z.number().finite().min(0.01).max(6).default(0.84),
|
|
42
|
+
wattageW: z.number().finite().min(0).max(4000).optional(),
|
|
43
|
+
quantity: z.number().int().min(1).max(200).default(1),
|
|
44
|
+
unitPriceOverrideUsd: z.number().finite().min(0).optional(),
|
|
45
|
+
customSku: z.string().optional(),
|
|
46
|
+
customBrand: z.string().optional(),
|
|
47
|
+
customGlbUrl: z
|
|
48
|
+
.string()
|
|
49
|
+
.refine((v) => v.trim() === '' || isSafeAssetBaseUrl(v), 'Unsafe customGlbUrl')
|
|
50
|
+
.optional(),
|
|
51
|
+
electricalDemand: z.record(z.string(), z.unknown()).optional(),
|
|
52
|
+
notes: z.string().default(''),
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
export type FurnishingsItemNode = z.infer<typeof FurnishingsItemNode>
|
|
56
|
+
|
|
57
|
+
export function getFurnishingsItemPorts(node: FurnishingsItemNode): NodePort[] {
|
|
58
|
+
const [x, y, zPos] = node.position
|
|
59
|
+
return [
|
|
60
|
+
{ id: 'base-center', position: [x, y, zPos], direction: [0, 1, 0], diameter: 0.05 },
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function buildFurnishingsItemGeometry(
|
|
65
|
+
node: FurnishingsItemNode,
|
|
66
|
+
_ctx?: GeometryContext,
|
|
67
|
+
): Group {
|
|
68
|
+
const root = new Group()
|
|
69
|
+
root.name = `furnishings-item:${node.itemKind}`
|
|
70
|
+
const cad = buildFurnitureCad({
|
|
71
|
+
kind: node.itemKind,
|
|
72
|
+
widthM: node.widthM,
|
|
73
|
+
depthM: node.depthM,
|
|
74
|
+
heightM: node.heightM,
|
|
75
|
+
woodFinish: node.woodFinish,
|
|
76
|
+
fabricFinish: node.fabricFinish,
|
|
77
|
+
metalFinish: node.metalFinish,
|
|
78
|
+
})
|
|
79
|
+
// Emit local-space children; Pascal's ParametricNodeRenderer applies node.position/rotation on the host group.
|
|
80
|
+
attachProceduralWithOptionalGlb({
|
|
81
|
+
root,
|
|
82
|
+
proceduralGroup: cad,
|
|
83
|
+
customGlbUrl: node.customGlbUrl,
|
|
84
|
+
tag: `${node.itemKind}:${node.id}`,
|
|
85
|
+
})
|
|
86
|
+
return root
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function buildFurnishingsItemFloorplan(
|
|
90
|
+
node: FurnishingsItemNode,
|
|
91
|
+
_ctx?: GeometryContext,
|
|
92
|
+
): FloorplanGeometry {
|
|
93
|
+
const spec = getFurnishingSpec(node.itemKind)
|
|
94
|
+
const stroke = spec ? DOMAIN_COLORS[spec.domain] : '#f59e0b'
|
|
95
|
+
const cx = node.position[0]
|
|
96
|
+
const cz = node.position[2]
|
|
97
|
+
const cos = Math.cos(node.rotation)
|
|
98
|
+
const sin = Math.sin(node.rotation)
|
|
99
|
+
const hw = node.widthM / 2
|
|
100
|
+
const hd = node.depthM / 2
|
|
101
|
+
|
|
102
|
+
const isRound =
|
|
103
|
+
node.itemKind === 'dining-table-round' ||
|
|
104
|
+
node.itemKind === 'bistro-table' ||
|
|
105
|
+
node.itemKind === 'side-end-table' ||
|
|
106
|
+
node.itemKind === 'area-rug-round' ||
|
|
107
|
+
node.itemKind === 'patio-umbrella' ||
|
|
108
|
+
node.itemKind === 'beanbag-lounger'
|
|
109
|
+
|
|
110
|
+
if (isRound) {
|
|
111
|
+
const pts: [number, number][] = []
|
|
112
|
+
const segs = 16
|
|
113
|
+
for (let i = 0; i < segs; i++) {
|
|
114
|
+
const a = (i / segs) * Math.PI * 2
|
|
115
|
+
pts.push([cx + Math.cos(a) * hw, cz + Math.sin(a) * hd])
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
kind: 'group',
|
|
119
|
+
children: [{ kind: 'polygon', points: pts, stroke, strokeWidth: 0.025, fill: `${stroke}22` }],
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const localCorners: readonly (readonly [number, number])[] = [
|
|
124
|
+
[-hw, -hd],
|
|
125
|
+
[hw, -hd],
|
|
126
|
+
[hw, hd],
|
|
127
|
+
[-hw, hd],
|
|
128
|
+
]
|
|
129
|
+
const corners: [number, number][] = localCorners.map(([lx, lz]) => [
|
|
130
|
+
cx + lx * cos + lz * sin,
|
|
131
|
+
cz - lx * sin + lz * cos,
|
|
132
|
+
])
|
|
133
|
+
// Orientation front tick (+Z edge)
|
|
134
|
+
const frontX = cx + hd * sin
|
|
135
|
+
const frontZ = cz + hd * cos
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
kind: 'group',
|
|
139
|
+
children: [
|
|
140
|
+
{ kind: 'polygon', points: corners, stroke, strokeWidth: 0.025, fill: `${stroke}24` },
|
|
141
|
+
{ kind: 'line', x1: cx, y1: cz, x2: frontX, y2: frontZ, stroke, strokeWidth: 0.018 },
|
|
142
|
+
],
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const furnishingsItemRenderDefinition: NodeDefinition<typeof FurnishingsItemNode> = {
|
|
147
|
+
kind: 'furnishings:item',
|
|
148
|
+
schemaVersion: 1,
|
|
149
|
+
schema: FurnishingsItemNode,
|
|
150
|
+
category: 'furnish',
|
|
151
|
+
defaults: () => ({
|
|
152
|
+
object: 'node',
|
|
153
|
+
parentId: null,
|
|
154
|
+
visible: true,
|
|
155
|
+
metadata: {},
|
|
156
|
+
name: '3-Seat Upholstered Sofa',
|
|
157
|
+
itemKind: 'sofa-3seat',
|
|
158
|
+
position: [0, 0, 0],
|
|
159
|
+
rotation: 0,
|
|
160
|
+
roomName: 'Living Room',
|
|
161
|
+
stylePreset: 'mid-century',
|
|
162
|
+
woodFinish: 'american-walnut',
|
|
163
|
+
fabricFinish: 'warm-linen',
|
|
164
|
+
metalFinish: 'matte-black',
|
|
165
|
+
widthM: 2.24,
|
|
166
|
+
depthM: 0.94,
|
|
167
|
+
heightM: 0.84,
|
|
168
|
+
quantity: 1,
|
|
169
|
+
notes: '',
|
|
170
|
+
}),
|
|
171
|
+
capabilities: {
|
|
172
|
+
selectable: { hitVolume: 'bbox' },
|
|
173
|
+
movable: { axes: ['x', 'y', 'z'], gridSnap: true },
|
|
174
|
+
rotatable: { axes: ['y'], snapAngles: [Math.PI / 4] },
|
|
175
|
+
duplicable: true,
|
|
176
|
+
deletable: true,
|
|
177
|
+
snappable: {},
|
|
178
|
+
},
|
|
179
|
+
geometry: buildFurnishingsItemGeometry,
|
|
180
|
+
geometryKey: (n) =>
|
|
181
|
+
JSON.stringify([
|
|
182
|
+
n.itemKind,
|
|
183
|
+
n.widthM,
|
|
184
|
+
n.depthM,
|
|
185
|
+
n.heightM,
|
|
186
|
+
n.woodFinish,
|
|
187
|
+
n.fabricFinish,
|
|
188
|
+
n.metalFinish,
|
|
189
|
+
n.customGlbUrl ?? '',
|
|
190
|
+
]),
|
|
191
|
+
ports: getFurnishingsItemPorts,
|
|
192
|
+
floorplan: buildFurnishingsItemFloorplan,
|
|
193
|
+
presentation: {
|
|
194
|
+
label: 'Furniture & Decor Piece',
|
|
195
|
+
description: 'Parametric freestanding seating, tables, desks, beds, rugs, lighting & outdoor patio furniture.',
|
|
196
|
+
icon: { kind: 'iconify', name: 'lucide:armchair' },
|
|
197
|
+
paletteSection: 'furnish',
|
|
198
|
+
},
|
|
199
|
+
mcp: {
|
|
200
|
+
description: 'Freestanding furniture, soft-goods, decor, or outdoor patio furnishing piece (non-cabinetry).',
|
|
201
|
+
},
|
|
202
|
+
}
|