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
+112 -72
View File
@@ -1,14 +1,15 @@
import { getMaterialPresetByRef } from '@pascal-app/core'
import { type GeometryContext, getMaterialPresetByRef } from '@pascal-app/core'
import {
applyMaterialPresetToMaterials,
createDefaultMaterial,
createMaterial,
DEFAULT_SHELF_MATERIAL,
type RenderShading,
resolveMaterialRef,
} from '@pascal-app/viewer'
import { BoxGeometry, FrontSide, Group, type Material, Mesh } from 'three'
import { BoxGeometry, Group, type Material, Mesh } from 'three'
import { sanitizeShelfDimensions } from './dimensions'
import type { ShelfNode } from './schema'
import { SHELF_SLOT_DEFAULT_COLOR, type ShelfSlotId } from './slots'
/**
* Pure shelf geometry builder. Takes a `ShelfNode` and returns a `Group`
@@ -23,75 +24,72 @@ import type { ShelfNode } from './schema'
* index arrays directly, and lets AI-generated nodes follow the same
* shape with no editor-specific knowledge.
*
* Materials: the kind exposes a single paintable surface via
* `node.material` / `node.materialPreset` — same shape walls / slabs /
* stairs use. When neither is set, every mesh shares the
* `DEFAULT_SHELF_MATERIAL` (off-white). When the user paints, the
* library preset's properties land on a cloned material here. The cache
* key includes the preset / material signature so paint changes
* invalidate without stomping unrelated shelves.
* Materials: the kind exposes per-slot paintable surfaces through
* `node.slots`, while `node.material` / `node.materialPreset` remain as
* legacy whole-shelf fallbacks. Every generated mesh is tagged with the
* slot it belongs to so paint mode can target shelves, frame, or back.
*
* Style dispatch lives at the top of the function; each style helper
* mutates the same `group`.
*/
type ShelfMaterial = Material & {
depthWrite: boolean
type ShelfSlotMaterials = Record<ShelfSlotId, Material>
function getShelfSlotMaterial(
node: ShelfNode,
slotId: ShelfSlotId,
materials: GeometryContext['materials'],
shading: RenderShading,
): Material {
const ref = node.slots?.[slotId]
if (ref) {
const resolved = resolveMaterialRef(ref, materials, shading)
if (resolved) return resolved
}
// Legacy whole-shelf paint applies to every slot when set (no per-slot override).
if (node.materialPreset) {
const preset = getMaterialPresetByRef(node.materialPreset)
if (preset) {
const base = createDefaultMaterial('#ffffff', 0.5, shading)
applyMaterialPresetToMaterials(base, preset)
return base
}
}
if (node.material) return createMaterial(node.material, shading)
return createDefaultMaterial(SHELF_SLOT_DEFAULT_COLOR, 0.9, shading)
}
const shelfMaterialCache = new Map<string, Material>()
function getShelfMaterial(node: ShelfNode, shading: RenderShading): Material {
const cacheKey = JSON.stringify({
shading,
material: node.material ?? null,
materialPreset: node.materialPreset ?? null,
})
const cached = shelfMaterialCache.get(cacheKey)
if (cached) return cached
const preset = getMaterialPresetByRef(node.materialPreset)
const material = preset
? createDefaultMaterial('#ffffff', 0.5, shading)
: node.material
? createMaterial(node.material, shading).clone()
: DEFAULT_SHELF_MATERIAL(shading).clone()
if (preset) {
applyMaterialPresetToMaterials(material, preset)
}
const shelfMaterial = material as ShelfMaterial
shelfMaterial.side = FrontSide
shelfMaterial.depthWrite = true
shelfMaterial.needsUpdate = true
shelfMaterialCache.set(cacheKey, material)
return material
function stampShelfSlot(mesh: Mesh, slotId: ShelfSlotId): Mesh {
mesh.userData.slotId = slotId
return mesh
}
export function buildShelfGeometry(
rawNode: ShelfNode,
_ctx?: unknown,
ctx?: GeometryContext,
shading: RenderShading = 'rendered',
): Group {
const node = sanitizeShelfDimensions(rawNode)
const group = new Group()
group.name = 'shelf-geometry'
const material = getShelfMaterial(node, shading)
const materials: ShelfSlotMaterials = {
shelves: getShelfSlotMaterial(node, 'shelves', ctx?.materials, shading),
frame: getShelfSlotMaterial(node, 'frame', ctx?.materials, shading),
back: getShelfSlotMaterial(node, 'back', ctx?.materials, shading),
}
switch (node.style) {
case 'wall-shelf':
buildWallShelf(group, node, material)
buildWallShelf(group, node, materials)
break
case 'bookshelf':
buildBookshelf(group, node, material)
buildBookshelf(group, node, materials)
break
case 'open-rack':
buildOpenRack(group, node, material)
buildOpenRack(group, node, materials)
break
case 'cubby':
buildCubby(group, node, material)
buildCubby(group, node, materials)
break
}
@@ -112,9 +110,12 @@ export function buildShelfGeometry(
* evenly-spaced boards from `height/rows` up to `height`. Brackets
* span from floor to the topmost board.
*/
function buildWallShelf(group: Group, node: ShelfNode, material: Material) {
function buildWallShelf(group: Group, node: ShelfNode, materials: ShelfSlotMaterials) {
for (const y of boardCenterYs(node)) {
const board = new Mesh(new BoxGeometry(node.width, node.thickness, node.depth), material)
const board = stampShelfSlot(
new Mesh(new BoxGeometry(node.width, node.thickness, node.depth), materials.shelves),
'shelves',
)
board.name = `shelf-board-${boardRowIndex(node, y)}`
board.position.set(0, y, 0)
group.add(board)
@@ -131,7 +132,10 @@ function buildWallShelf(group: Group, node: ShelfNode, material: Material) {
const bracketDepth = node.bracketStyle === 'industrial' ? node.depth * 0.95 : node.depth * 0.7
for (const sign of [-1, 1] as const) {
const bracket = new Mesh(new BoxGeometry(bracketWidth, bracketHeight, bracketDepth), material)
const bracket = stampShelfSlot(
new Mesh(new BoxGeometry(bracketWidth, bracketHeight, bracketDepth), materials.frame),
'frame',
)
bracket.name = `shelf-bracket-${sign === -1 ? 'left' : 'right'}`
bracket.position.set(sign * (node.width / 2 - inset), bracketHeight / 2, 0)
group.add(bracket)
@@ -144,20 +148,26 @@ function buildWallShelf(group: Group, node: ShelfNode, material: Material) {
* `withSides === false`, side panels become slim corner posts (a rack
* silhouette).
*/
function buildBookshelf(group: Group, node: ShelfNode, material: Material) {
function buildBookshelf(group: Group, node: ShelfNode, materials: ShelfSlotMaterials) {
const unitHeight = node.height + node.thickness
const innerWidth = node.withSides ? node.width - 2 * node.thickness : node.width
// Top + bottom + intermediate boards
for (const y of boardCenterYs(node)) {
const board = new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), material)
const board = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), materials.shelves),
'shelves',
)
board.name = `shelf-board-${boardRowIndex(node, y)}`
board.position.set(0, y, 0)
group.add(board)
}
if (node.withBottom) {
const bottom = new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), material)
const bottom = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), materials.shelves),
'shelves',
)
bottom.name = 'shelf-board-bottom'
bottom.position.set(0, node.thickness / 2, 0)
group.add(bottom)
@@ -166,17 +176,23 @@ function buildBookshelf(group: Group, node: ShelfNode, material: Material) {
// Side panels (or corner posts) — span the full unit height.
if (node.withSides) {
for (const sign of [-1, 1] as const) {
const side = new Mesh(new BoxGeometry(node.thickness, unitHeight, node.depth), material)
const side = stampShelfSlot(
new Mesh(new BoxGeometry(node.thickness, unitHeight, node.depth), materials.frame),
'frame',
)
side.name = `shelf-side-${sign === -1 ? 'left' : 'right'}`
side.position.set(sign * (node.width / 2 - node.thickness / 2), unitHeight / 2, 0)
group.add(side)
}
} else {
addCornerPosts(group, node, material, unitHeight, 'rack')
addCornerPosts(group, node, materials.frame, unitHeight, 'rack')
}
if (node.withBack) {
const back = new Mesh(new BoxGeometry(innerWidth, unitHeight, node.thickness), material)
const back = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, unitHeight, node.thickness), materials.back),
'back',
)
back.name = 'shelf-back'
back.position.set(0, unitHeight / 2, -(node.depth / 2 - node.thickness / 2))
group.add(back)
@@ -187,7 +203,10 @@ function buildBookshelf(group: Group, node: ShelfNode, material: Material) {
const colStep = innerWidth / node.columns
for (let c = 1; c < node.columns; c++) {
const x = -innerWidth / 2 + c * colStep
const divider = new Mesh(new BoxGeometry(node.thickness, unitHeight, node.depth), material)
const divider = stampShelfSlot(
new Mesh(new BoxGeometry(node.thickness, unitHeight, node.depth), materials.frame),
'frame',
)
divider.name = `shelf-divider-col-${c}`
divider.position.set(x, unitHeight / 2, 0)
group.add(divider)
@@ -200,26 +219,32 @@ function buildBookshelf(group: Group, node: ShelfNode, material: Material) {
* X-brace on the back face for stability. `withSides` / `bracketStyle`
* are ignored (the rack defines its own posts).
*/
function buildOpenRack(group: Group, node: ShelfNode, material: Material) {
function buildOpenRack(group: Group, node: ShelfNode, materials: ShelfSlotMaterials) {
const unitHeight = node.height + node.thickness
const innerWidth = node.width
const boardThickness = Math.max(0.02, node.thickness * 0.8)
for (const y of boardCenterYs(node)) {
const board = new Mesh(new BoxGeometry(innerWidth, boardThickness, node.depth), material)
const board = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, boardThickness, node.depth), materials.shelves),
'shelves',
)
board.name = `shelf-board-${boardRowIndex(node, y)}`
board.position.set(0, y, 0)
group.add(board)
}
addCornerPosts(group, node, material, unitHeight, 'rack')
addCornerPosts(group, node, materials.frame, unitHeight, 'rack')
if (node.withBack) {
const braceThickness = Math.max(0.015, node.thickness * 0.6)
for (const y of [boardThickness, unitHeight - boardThickness] as const) {
const brace = new Mesh(
new BoxGeometry(node.width - braceThickness * 2, braceThickness, braceThickness),
material,
const brace = stampShelfSlot(
new Mesh(
new BoxGeometry(node.width - braceThickness * 2, braceThickness, braceThickness),
materials.frame,
),
'frame',
)
brace.name = `shelf-brace-h-${y < unitHeight / 2 ? 'bottom' : 'top'}`
brace.position.set(0, y, -(node.depth / 2 - braceThickness / 2))
@@ -233,32 +258,44 @@ function buildOpenRack(group: Group, node: ShelfNode, material: Material) {
* boards + vertical dividers. `withBack` / `withSides` are forced on
* because the cubby shape requires them.
*/
function buildCubby(group: Group, node: ShelfNode, material: Material) {
function buildCubby(group: Group, node: ShelfNode, materials: ShelfSlotMaterials) {
const unitHeight = node.height + node.thickness
const innerWidth = node.width - 2 * node.thickness
for (const y of boardCenterYs(node)) {
const board = new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), material)
const board = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), materials.shelves),
'shelves',
)
board.name = `shelf-board-${boardRowIndex(node, y)}`
board.position.set(0, y, 0)
group.add(board)
}
if (node.withBottom) {
const bottom = new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), material)
const bottom = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, node.thickness, node.depth), materials.shelves),
'shelves',
)
bottom.name = 'shelf-board-bottom'
bottom.position.set(0, node.thickness / 2, 0)
group.add(bottom)
}
for (const sign of [-1, 1] as const) {
const side = new Mesh(new BoxGeometry(node.thickness, unitHeight, node.depth), material)
const side = stampShelfSlot(
new Mesh(new BoxGeometry(node.thickness, unitHeight, node.depth), materials.frame),
'frame',
)
side.name = `shelf-side-${sign === -1 ? 'left' : 'right'}`
side.position.set(sign * (node.width / 2 - node.thickness / 2), unitHeight / 2, 0)
group.add(side)
}
const back = new Mesh(new BoxGeometry(innerWidth, unitHeight, node.thickness), material)
const back = stampShelfSlot(
new Mesh(new BoxGeometry(innerWidth, unitHeight, node.thickness), materials.back),
'back',
)
back.name = 'shelf-back'
back.position.set(0, unitHeight / 2, -(node.depth / 2 - node.thickness / 2))
group.add(back)
@@ -273,9 +310,9 @@ function buildCubby(group: Group, node: ShelfNode, material: Material) {
if (dividerHeight <= 0) continue
for (let c = 1; c < node.columns; c++) {
const x = -innerWidth / 2 + c * colStep
const divider = new Mesh(
new BoxGeometry(node.thickness, dividerHeight, node.depth),
material,
const divider = stampShelfSlot(
new Mesh(new BoxGeometry(node.thickness, dividerHeight, node.depth), materials.frame),
'frame',
)
divider.name = `shelf-divider-${r}-${c}`
divider.position.set(x, cellBottomY + dividerHeight / 2, 0)
@@ -324,7 +361,10 @@ function addCornerPosts(
const inset = postThickness / 2
for (const xSign of [-1, 1] as const) {
for (const zSign of [-1, 1] as const) {
const post = new Mesh(new BoxGeometry(postThickness, unitHeight, postThickness), material)
const post = stampShelfSlot(
new Mesh(new BoxGeometry(postThickness, unitHeight, postThickness), material),
'frame',
)
post.name = `shelf-post-${xSign === -1 ? 'l' : 'r'}${zSign === -1 ? 'b' : 'f'}`
post.position.set(
xSign * (node.width / 2 - inset),