feat(paint-slots): unified slot defaults + paint for slab, ceiling, wall (phase 5)

Brings slab, ceiling, and wall onto the unified slot contract the shelf
established, so each declares its paintable slots with a declarative default and
(slab/ceiling) is painted through the registry capabilities.paint dispatch.

- Shared helper packages/nodes/src/shared/slot-paint.ts: a node.slots-based
  PaintCapability factory (commit/resolve/effective generic; preview injected).
  Distinct from surface-paint.ts, which writes the legacy inline node.material.
- slab: schema slots; def.geometry resolves node.slots.surface -> legacy
  material -> declared default, tags the mesh userData.slotId; slabPaint +
  capabilities.slots. Retires DEFAULT_SLAB_MATERIAL in the slab path.
- ceiling: schema slots; material builders extracted to ceiling/materials.ts
  (shared by renderer + paint preview, built BackSide so the hover preview is
  visible from below); renderer resolves the slot; ceilingPaint + slots.
- wall: WALL_SLOT_DEFAULT in core; the viewer's getMaterialsForWall renders an
  unpainted face with its declared default instead of the themed wall role;
  capabilities.slots (interior/exterior). wallPaint's inline interior/exterior
  fields are unchanged (node.slots migration is a later step).
- selection-manager + material-paint: drop slab/ceiling from the legacy
  single-surface arms (now registry-driven).

Behavior change (intended, matches the shelf precedent + the phase-5 plan):
colored-mode UNPAINTED slab/ceiling/wall surfaces now render their fixed slot
default (#e5e5e5 / #f5f5dc / #ffffff) instead of the theme role colour. The
textures-off (monochrome) role collapse is unchanged — the escape hatch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Wassim SAMAD
2026-06-17 07:51:37 -04:00
co-authored by Claude Opus 4.8
parent b0f4e1b8ee
commit 967a905e3b
19 changed files with 520 additions and 88 deletions
+211
View File
@@ -0,0 +1,211 @@
import {
type AnyNode,
type AnyNodeId,
generateSceneMaterialId,
type MaterialSchema,
type PaintCapability,
type PaintPreviewArgs,
type PaintResolveArgs,
parseMaterialRef,
type SceneMaterial,
type SceneMaterialId,
toSceneMaterialRef,
useScene,
} from '@pascal-app/core'
import { createMaterial, createMaterialFromPresetRef, useViewer } from '@pascal-app/viewer'
import type { Material, Mesh, Object3D } from 'three'
/**
* Shared paint capability for procedural kinds on the unified slot model
* (`node.slots: Record<slotId, MaterialRef>` + the shared scene-material
* palette) — the same data shape items derive from their GLB and the shelf
* declares via `capabilities.slots`. Distinct from `surface-paint.ts`, which
* writes the legacy inline `node.material` copy the plan is retiring.
*
* The commit / resolve / effective-material logic is identical across kinds;
* only the slot-resolution from a pointer hit and the mesh preview differ, so
* those are injected per kind.
*/
type SlotsNode = AnyNode & { slots?: Record<string, string> }
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 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 commitSlotPaint(
node: SlotsNode,
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 SlotsNode | 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) {
// 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.
const sceneMaterial = newSceneMaterial
useScene.setState((s) => {
if (s.readOnly) return s
const node2 = s.nodes[nodeId] as SlotsNode | undefined
if (!node2) return s
return {
materials: { ...s.materials, [sceneMaterial.id as SceneMaterialId]: sceneMaterial },
nodes: {
...s.nodes,
[nodeId]: { ...node2, slots: nextSlots } as AnyNode,
},
}
})
useScene.getState().markDirty(nodeId)
return
}
state.updateNode(nodeId, { slots: nextSlots } as Partial<AnyNode>)
}
/** Preview material for a slot paint — mirrors the commit's resolution. */
export function buildSlotPreviewMaterial(
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
}
/**
* Preview for kinds whose meshes are produced by `def.geometry` and tagged
* with `userData.slotId` (+ `__fromGeometry`). Swaps every builder mesh whose
* slot matches `role`, leaving hosted-child meshes (which can carry a colliding
* `userData.slotId` from their own GLB) untouched.
*/
export function previewGeometrySlot(args: PaintPreviewArgs): (() => void) | null {
const { role, root, material, materialPreset } = args
const preview = buildSlotPreviewMaterial(material, materialPreset)
if (!preview) return () => {}
const restores: Array<() => void> = []
;(root as Object3D).traverse((object) => {
const mesh = object as Mesh
if (!mesh.isMesh) return
const userData = mesh.userData as { slotId?: string | null; __fromGeometry?: boolean }
if (userData.__fromGeometry !== true) return
if (userData.slotId !== role) return
const previous = mesh.material
mesh.material = preview
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 type SlotPaintConfig = {
/** Resolve the slot id for a pointer hit (`null` = not paintable here). */
resolveRole: (args: PaintResolveArgs) => string | null
/** Apply a preview to the registered mesh subtree for `role`. */
applyPreview: (args: PaintPreviewArgs) => (() => void) | null
/**
* Optional legacy fallback for the picker's current-value indicator — read
* when no `node.slots[role]` ref exists yet (e.g. a scene painted before the
* kind moved onto the slot model still carries inline `material`/`preset`).
*/
legacyEffective?: (
node: AnyNode,
role: string,
) => { material: MaterialSchema | undefined; materialPreset: string | undefined } | null
}
export function createSlotPaintCapability(config: SlotPaintConfig): PaintCapability {
return {
resolveRole: config.resolveRole,
buildPatch: ({ node, role, materialPreset }) => {
const slots = { ...((node as SlotsNode).slots ?? {}) }
if (materialPreset) slots[role] = materialPreset
else delete slots[role]
return { slots } as Partial<AnyNode>
},
commit: ({ node, role, material, materialPreset }) =>
commitSlotPaint(node as SlotsNode, role, material, materialPreset),
applyPreview: config.applyPreview,
getEffectiveMaterial: ({ node, role }) => {
const ref = (node as SlotsNode).slots?.[role]
const parsed = parseMaterialRef(ref)
if (parsed) {
if (parsed.kind === 'library') return { material: undefined, materialPreset: ref }
const sceneMaterial = useScene.getState().materials[parsed.id as SceneMaterialId]
if (sceneMaterial) return { material: sceneMaterial.material, materialPreset: undefined }
}
return config.legacyEffective?.(node, role) ?? null
},
}
}