Skip to content

Commit d926ded

Browse files
authored
fix(viewer): normalize legacy door nodes against the schema before render (pascalorg#303)
Legacy/unparsed door nodes can miss schema-defaulted fields (segments, columnRatios, dividerThickness, panelInset/panelDepth …) and crash the door geometry build (EDITOR-AM). `updateDoorMesh` re-applies the Zod defaults once at entry via `DoorNodeSchema.safeParse` (with a drop-bad-segments retry and a full-defaults fallback), so every downstream read sees populated data — including the shaped-top divider path pascalorg#334 added. Also normalizes schema-valid legacy doors at load in `migrateNodes`, mirroring the existing stair normalizer, so the door panel and window system see defaulted data too. Reimplemented against main: the original used `segments ?? []` (wrong — legacy doors rendered an empty frame) and missed the pascalorg#334 `dividerThickness` sites. Verified: `bun run check-types` clean, biome clean on touched files.
1 parent 2809b01 commit d926ded

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

packages/core/src/store/use-scene.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { create, type StoreApi, type UseBoundStore } from 'zustand'
66
import { BuildingNode } from '../schema'
77
import type { Collection, CollectionId } from '../schema/collections'
88
import { generateCollectionId } from '../schema/collections'
9+
import { DoorNode as DoorNodeSchema } from '../schema/nodes/door'
910
import { LevelNode } from '../schema/nodes/level'
1011
import {
1112
getPitchFromActiveRoofHeight,
@@ -106,6 +107,11 @@ function normalizeStairSegmentNode(node: Record<string, unknown>) {
106107
return parsed.success ? parsed.data : null
107108
}
108109

110+
function normalizeDoorNode(node: Record<string, unknown>) {
111+
const parsed = DoorNodeSchema.safeParse(node)
112+
return parsed.success ? { ...node, ...parsed.data } : null
113+
}
114+
109115
function migrateWallSurfaceMaterials(node: Record<string, any>) {
110116
const hasInterior =
111117
node.interiorMaterial !== undefined || typeof node.interiorMaterialPreset === 'string'
@@ -365,6 +371,13 @@ function migrateNodes(nodes: Record<string, any>): Record<string, AnyNode> {
365371
}
366372
}
367373

374+
if (node.type === 'door') {
375+
const normalized = normalizeDoorNode(node)
376+
if (normalized) {
377+
patchedNodes[id] = normalized
378+
}
379+
}
380+
368381
if (node.type === 'stair') {
369382
const normalized = normalizeStairNode(migrateStairSurfaceMaterials(node))
370383
if (normalized) {

packages/viewer/src/systems/door/door-system.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
type AnyNodeId,
33
clampDoorOperationState,
44
type DoorNode,
5+
DoorNode as DoorNodeSchema,
56
getDoorRenderOpenAmount,
67
getEffectiveNode,
78
sceneRegistry,
@@ -26,6 +27,20 @@ let baseMaterial = getBaseMaterial()
2627
let revealMaterial: THREE.Material = defaultRevealMaterial
2728
let glassMaterial: THREE.Material = defaultGlassMaterial
2829

30+
const DOOR_RENDER_DEFAULTS = DoorNodeSchema.parse({ id: 'door_render_default' })
31+
32+
// Legacy/unparsed door nodes can miss schema-defaulted fields (segments,
33+
// columnRatios, dividerThickness, …) and crash the geometry build. Re-apply the
34+
// Zod defaults; if the node is structurally invalid (e.g. a segment missing a
35+
// required field) drop the bad segments, then fall back to defaults entirely.
36+
function normalizeDoorNodeForRender(node: DoorNode): DoorNode {
37+
const parsed = DoorNodeSchema.safeParse(node)
38+
if (parsed.success) return parsed.data
39+
const retry = DoorNodeSchema.safeParse({ ...node, segments: undefined })
40+
if (retry.success) return retry.data
41+
return { ...DOOR_RENDER_DEFAULTS, id: node.id, parentId: node.parentId }
42+
}
43+
2944
export const DoorSystem = () => {
3045
const dirtyNodes = useScene((state) => state.dirtyNodes)
3146
const clearDirty = useScene((state) => state.clearDirty)
@@ -1903,7 +1918,9 @@ function getEffectiveOpeningShape(node: DoorNode): DoorNode['openingShape'] {
19031918
: (node.openingShape ?? 'rectangle')
19041919
}
19051920

1906-
function updateDoorMesh(node: DoorNode, mesh: THREE.Mesh) {
1921+
function updateDoorMesh(rawNode: DoorNode, mesh: THREE.Mesh) {
1922+
const node = normalizeDoorNodeForRender(rawNode)
1923+
19071924
// Root mesh is an invisible hitbox; all visuals live in child meshes
19081925
mesh.geometry.dispose()
19091926
mesh.geometry = new THREE.BoxGeometry(node.width, node.height, node.frameDepth)

0 commit comments

Comments
 (0)