feat(paint-slots): paintable slots on the procedural shelf (phase 5)

Proves the unified (nodeId, slotId) slot contract on a procedural generator,
beyond items and walls. A shelf now exposes three paintable slots — shelves /
frame / back — painted through the same PaintCapability dispatch and the same
node.slots: Record<slotId, MaterialRef> shape items use.

Foundation (shared, reusable by future procedural kinds):
- core: SlotDeclaration type + capabilities.slots(node) registry declaration;
  GeometryContext gains `materials` so a pure builder can resolve scene:<id>
  slot refs without importing useScene.
- viewer GeometrySystem: threads the scene material library into every builder
  ctx, and re-dirties (bypassing the geometryKey skip) any geometry node that
  references a scene material when that material changes — so editing a custom
  colour propagates to every shelf using it, matching items.

Shelf:
- schema: slots: Record<string, MaterialRef> (mirrors ItemNode).
- geometry: per-slot material resolution (slot override -> legacy whole-shelf
  -> declared default colour); every mesh stamped with userData.slotId;
  DEFAULT_SHELF_MATERIAL retired (declared default gives identical off-white).
- paint.ts: PaintCapability (resolveRole from userData.slotId, scene-material
  commit for one-off colours, preview restricted to __fromGeometry meshes so
  hosted items aren't ghosted, getEffectiveMaterial incl. legacy fallback).
- definition: paint + slots capabilities; slots folded into geometryKey.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Wassim SAMAD
2026-06-16 10:59:59 -04:00
co-authored by Claude Opus 4.8
parent 89a7232a67
commit 101341d98d
10 changed files with 455 additions and 87 deletions
+218
View File
@@ -0,0 +1,218 @@
import {
type AnyNode,
type AnyNodeId,
generateSceneMaterialId,
type MaterialSchema,
type PaintCapability,
parseMaterialRef,
type SceneMaterial,
type SceneMaterialId,
type ShelfNode,
toSceneMaterialRef,
useScene,
} from '@pascal-app/core'
import { createMaterial, createMaterialFromPresetRef, useViewer } from '@pascal-app/viewer'
import type { Material, Mesh } from 'three'
type ShelfSlotUserData = {
slotId?: string | null
}
function deepEqual(a: unknown, b: unknown): boolean {
if (Object.is(a, b)) return true
if (typeof a !== typeof b) return false
if (a === null || b === null) return false
if (Array.isArray(a) || Array.isArray(b)) {
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false
for (let index = 0; index < a.length; index += 1) {
if (!deepEqual(a[index], b[index])) return false
}
return true
}
if (typeof a === 'object') {
const aRecord = a as Record<string, unknown>
const bRecord = b as Record<string, unknown>
const aKeys = Object.keys(aRecord)
const bKeys = Object.keys(bRecord)
if (aKeys.length !== bKeys.length) return false
for (const key of aKeys) {
if (!Object.hasOwn(bRecord, key)) return false
if (!deepEqual(aRecord[key], bRecord[key])) return false
}
return true
}
return false
}
function resolveShelfSlotId(args: { hitObject?: { userData?: ShelfSlotUserData } }): string | null {
const slotId = args.hitObject?.userData?.slotId
return typeof slotId === 'string' ? slotId : null
}
function buildShelfSlotsPatch(
node: ShelfNode,
role: string,
material: MaterialSchema | undefined,
materialPreset: string | undefined,
): Partial<ShelfNode> {
const slots = { ...(node.slots ?? {}) }
if (material === undefined && materialPreset === undefined) {
delete slots[role]
return { slots }
}
if (materialPreset) {
slots[role] = materialPreset
return { slots }
}
return { slots }
}
function findMatchingSceneMaterial(
materials: Record<SceneMaterialId, SceneMaterial>,
material: MaterialSchema,
): SceneMaterial | null {
for (const sceneMaterial of Object.values(materials)) {
if (deepEqual(sceneMaterial.material, material)) return sceneMaterial
}
return null
}
function commitNewSceneMaterialAndSlots(
nodeId: AnyNodeId,
nextSlots: ShelfNode['slots'],
sceneMaterial: SceneMaterial,
): void {
// Creating the scene material and setting the slot ref are one logical
// edit, so apply both in a single `set` — zundo records one history entry,
// and one undo removes both the ref and its (now orphaned) material.
useScene.setState((state) => {
if (state.readOnly) return state
const currentNode = state.nodes[nodeId]
if (currentNode?.type !== 'shelf') return state
return {
materials: { ...state.materials, [sceneMaterial.id as SceneMaterialId]: sceneMaterial },
nodes: {
...state.nodes,
[nodeId]: { ...currentNode, slots: nextSlots } as AnyNode,
},
}
})
useScene.getState().markDirty(nodeId)
}
function commitShelfPaint(
node: ShelfNode,
role: string,
material: MaterialSchema | undefined,
materialPreset: string | undefined,
): void {
const nodeId = node.id as AnyNodeId
const state = useScene.getState()
const currentNode = (state.nodes[nodeId] as ShelfNode | undefined) ?? node
let ref: string | undefined
let newSceneMaterial: SceneMaterial | null = null
if (material === undefined && materialPreset === undefined) {
ref = undefined
} else if (materialPreset) {
ref = materialPreset
} else if (material) {
const existing = findMatchingSceneMaterial(state.materials, material)
if (existing) {
ref = toSceneMaterialRef(existing.id)
} else {
const id = generateSceneMaterialId()
newSceneMaterial = {
id,
name: `Material ${Object.keys(state.materials).length + 1}`,
material,
}
ref = toSceneMaterialRef(id)
}
} else {
return
}
const nextSlots = { ...(currentNode.slots ?? {}) }
if (ref) nextSlots[role] = ref
else delete nextSlots[role]
if (newSceneMaterial) {
commitNewSceneMaterialAndSlots(nodeId, nextSlots, newSceneMaterial)
return
}
state.updateNode(nodeId, { slots: nextSlots } as Partial<AnyNode>)
}
function buildPreviewMaterial(
material: MaterialSchema | undefined,
materialPreset: string | undefined,
): Material | null {
const shading = useViewer.getState().shading
if (materialPreset) return createMaterialFromPresetRef(materialPreset, shading)
if (material) return createMaterial(material, shading)
return null
}
function applyShelfPreview(
role: string,
root: import('three').Object3D,
material: MaterialSchema | undefined,
materialPreset: string | undefined,
): (() => void) | null {
const previewMaterial = buildPreviewMaterial(material, materialPreset)
if (!previewMaterial) return () => {}
const restores: Array<() => void> = []
root.traverse((object) => {
const mesh = object as Mesh
if (!mesh.isMesh) return
const userData = mesh.userData as ShelfSlotUserData & { __fromGeometry?: boolean }
// Only the shelf's own builder meshes — never hosted item children, whose
// GLB meshes can carry a colliding `userData.slotId` (slot_frame, etc.).
if (userData.__fromGeometry !== true) return
if (userData.slotId !== role) return
const previous = mesh.material
mesh.material = previewMaterial
restores.push(() => {
mesh.material = previous
})
})
if (restores.length === 0) return null
return () => {
for (let index = restores.length - 1; index >= 0; index -= 1) {
restores[index]?.()
}
}
}
export const shelfPaint: PaintCapability = {
resolveRole: ({ hitObject }) =>
resolveShelfSlotId({ hitObject: hitObject as { userData?: ShelfSlotUserData } }),
buildPatch: ({ node, role, material, materialPreset }) =>
buildShelfSlotsPatch(node as ShelfNode, role, material, materialPreset) as Partial<AnyNode>,
commit: ({ node, role, material, materialPreset }) =>
commitShelfPaint(node as ShelfNode, role, material, materialPreset),
applyPreview: ({ role, root, material, materialPreset }) =>
applyShelfPreview(role, root, material, materialPreset),
getEffectiveMaterial: ({ node, role }) => {
const shelf = node as ShelfNode
const parsed = parseMaterialRef(shelf.slots?.[role])
if (parsed) {
if (parsed.kind === 'library') {
return { material: undefined, materialPreset: shelf.slots?.[role] }
}
const sceneMaterial = useScene.getState().materials[parsed.id as SceneMaterialId]
if (sceneMaterial) return { material: sceneMaterial.material, materialPreset: undefined }
}
// No (or dangling) slot ref — surface the legacy whole-shelf paint the
// geometry builder still falls back to, so the picker matches what renders.
if (shelf.materialPreset || shelf.material) {
return { material: shelf.material, materialPreset: shelf.materialPreset }
}
return null
},
}