From d805d544d3daba1c4333c656da28233ca87d2730 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 15 Oct 2026 17:27:28 +0000 Subject: [PATCH 1/3] viewer: a wall keeps the groups a room-wide paint preview gave it until the preview restores them The cutout cache knew only the hovered wall as a paint preview's owner. A preview spread over a room or every matching surface regroups each wall's faces onto its own longer array; re-applying such a wall put its shorter palette back over the preview's groups, and every raycast over it threw on material.side (MONOREPO-EDITOR-ZT/ZV/ZW). The cache now defers a palette that cannot draw the mesh's current groups and retries each frame. --- packages/viewer/src/lib/pointer-events.ts | 9 ++++-- .../systems/wall/wall-cutout-cache.test.ts | 27 +++++++++++++++++ .../src/systems/wall/wall-cutout-cache.ts | 29 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/packages/viewer/src/lib/pointer-events.ts b/packages/viewer/src/lib/pointer-events.ts index ad3ebfebf9..de2789e437 100644 --- a/packages/viewer/src/lib/pointer-events.ts +++ b/packages/viewer/src/lib/pointer-events.ts @@ -169,12 +169,15 @@ type CachedQuery = { * a material array by each geometry group, and one with no entry throws on * `material.side`. A mesh caught mid-swap (a preview restored an older array * over a rebuilt geometry) is skipped for this move instead of taking the whole - * pointer event down; its children are still tested. + * pointer event down; its children are still tested. `material` asks the same of + * an array the mesh would wear. */ -export function hasMaterialsForGroups(object: THREE.Object3D): boolean { +export function hasMaterialsForGroups( + object: THREE.Object3D, + material: THREE.Material | THREE.Material[] | undefined = (object as THREE.Mesh).material, +): boolean { const mesh = object as THREE.Mesh if (!mesh.isMesh) return true - const material = mesh.material if (!material) return false if (!Array.isArray(material)) return true for (const group of mesh.geometry?.groups ?? []) { diff --git a/packages/viewer/src/systems/wall/wall-cutout-cache.test.ts b/packages/viewer/src/systems/wall/wall-cutout-cache.test.ts index 806b1c52b6..ca72bd2b50 100644 --- a/packages/viewer/src/systems/wall/wall-cutout-cache.test.ts +++ b/packages/viewer/src/systems/wall/wall-cutout-cache.test.ts @@ -329,6 +329,33 @@ describe('WallCutoutCache', () => { expect((mesh.material as Material[])[2]).toBeDefined() }) + test('a room-wide preview keeps the groups it gave a wall that is not hovered', () => { + viewerStore.setState({ wallMode: 'up' }) + const hovered = addWall() + const spread = addWall() + cache.update(camera, 1) + const palette = spread.mesh.material as Material[] + viewerStore.setState({ hoveredId: spread.node.id, hoverHighlightMode: 'paint-ready' }) + cache.update(camera, 1.005) + // The hover moves on to another wall of the room; its preview spreads over + // this one: faces regrouped onto entries past the palette's end. + viewerStore.setState({ hoveredId: hovered.node.id }) + const preview = [...palette, new MeshBasicMaterial(), new MeshBasicMaterial()] + spread.mesh.material = preview + spread.mesh.geometry.addGroup(0, 3, preview.length - 1) + cache.update(camera, 1.01) + expect(spread.mesh.material).toBe(preview) + camera.position.x++ + cache.update(camera, 2) + expect(spread.mesh.material).toBe(preview) + // The preview's restore gives the groups back; the palette goes on next frame. + spread.mesh.geometry.clearGroups() + spread.mesh.material = new MeshBasicMaterial() + viewerStore.setState({ hoveredId: null, hoverHighlightMode: 'default' }) + cache.update(camera, 2.01) + expect(spread.mesh.material).toBe(cache.walls.get(spread.node.id)!.visibleVariant.materials) + }) + test('appearance changes during preview are applied when temporary ownership ends', () => { viewerStore.setState({ wallMode: 'up' }) const { node, mesh } = addWall() diff --git a/packages/viewer/src/systems/wall/wall-cutout-cache.ts b/packages/viewer/src/systems/wall/wall-cutout-cache.ts index dec34d5149..9a5d324525 100644 --- a/packages/viewer/src/systems/wall/wall-cutout-cache.ts +++ b/packages/viewer/src/systems/wall/wall-cutout-cache.ts @@ -119,6 +119,8 @@ export type WallCutoutViewerStore = { getState: () => WallCutoutViewerState } export class WallCutoutCache { readonly walls = new Map() readonly rebuilt = new Set() + /** Walls whose palette waits for a preview to give back their groups (see `mayAssign`). */ + private readonly deferred = new Set() private viewer: WallCutoutViewerState | undefined private nodes: ReturnType['nodes'] | undefined private materials: ReturnType['materials'] | undefined @@ -201,6 +203,11 @@ export class WallCutoutCache { : null const releasedPreview = previewId(previous) === previewId(viewer) ? null : previewId(previous) this.viewer = viewer + for (const id of this.deferred.size > 0 ? Array.from(this.deferred) : []) { + this.deferred.delete(id) + const wall = this.walls.get(id) + if (wall) this.apply(wall, viewer.wallMode, false) + } const invalidated = overridesChanged || appearanceChanged || @@ -408,12 +415,34 @@ export class WallCutoutCache { wall.mesh.material !== variant.materials && !sameMaterialArray(wall.mesh.material, variant.materials) ) { + if (!this.mayAssign(wall, variant.materials)) { + wall.variantKey = undefined + return + } wall.mesh.material = variant.materials } wall.variantKey = variant.key wall.assignedMaterials = wall.mesh.material } else if (wall.mesh.material !== wall.assignedMaterials) { + if (!this.mayAssign(wall, wall.assignedMaterials!)) return wall.mesh.material = wall.assignedMaterials! } } + + /** + * A paint preview spread over several walls (a room, all matching) regroups + * each wall's faces onto its own longer array, but only the hovered wall is + * known here as a preview's owner. A palette that cannot draw the groups the + * mesh wears now waits for the preview's restore to put them back: the mesh + * would otherwise keep groups with no material, and every raycast over it + * throws on `material.side`. + */ + private mayAssign(wall: CachedWall, materials: Material | Material[]): boolean { + if (hasMaterialsForGroups(wall.mesh, materials) || !hasMaterialsForGroups(wall.mesh)) { + this.deferred.delete(wall.node.id) + return true + } + this.deferred.add(wall.node.id) + return false + } } From 0d2b3f99c4c08bfd6e4ee053641d48eba71ef61b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 15 Oct 2026 17:27:28 +0000 Subject: [PATCH 2/3] editor: paint hover skips a wall mesh it cannot raycast instead of throwing wallHitInOpening and the curtain-wall role resolve call Mesh.raycast on the wall's drawn mesh directly, outside the guarded R3F collector. --- packages/editor/src/lib/paint-pass-through.test.ts | 11 +++++++++++ packages/editor/src/lib/paint-pass-through.ts | 6 ++++-- packages/nodes/src/wall/paint.ts | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/lib/paint-pass-through.test.ts b/packages/editor/src/lib/paint-pass-through.test.ts index 75a6a67353..7ed11745f7 100644 --- a/packages/editor/src/lib/paint-pass-through.test.ts +++ b/packages/editor/src/lib/paint-pass-through.test.ts @@ -7,6 +7,7 @@ import { Group, type Intersection, Mesh, + MeshBasicMaterial, Path, Ray, Shape, @@ -74,6 +75,16 @@ describe('paint passes through an empty opening', () => { it('without a drawn mesh the hit is the wall', () => { expect(wallHitInOpening(null, new Ray(), 1)).toBe(false) }) + + it('a drawn mesh with a group its material array has no entry for is the wall, without throwing', () => { + const torn = new Mesh(drawn.geometry.clone(), [new MeshBasicMaterial()]) + torn.geometry.clearGroups() + torn.geometry.addGroup(0, Number.POSITIVE_INFINITY, 3) + torn.updateMatrixWorld(true) + const ray = new Ray(new Vector3(2, 1, 5), new Vector3(0, 0, -1)) + expect(() => wallHitInOpening(torn, ray, 5 - 0.2)).not.toThrow() + expect(wallHitInOpening(torn, ray, 5 - 0.2)).toBe(false) + }) }) describe('a door part found behind the floor seen through its opening', () => { diff --git a/packages/editor/src/lib/paint-pass-through.ts b/packages/editor/src/lib/paint-pass-through.ts index ea4a76ce7c..95d297a57a 100644 --- a/packages/editor/src/lib/paint-pass-through.ts +++ b/packages/editor/src/lib/paint-pass-through.ts @@ -1,5 +1,5 @@ import type { AnyNode } from '@pascal-app/core' -import { setSurfaceRaycastLayers } from '@pascal-app/viewer' +import { hasMaterialsForGroups, setSurfaceRaycastLayers } from '@pascal-app/viewer' import { type Intersection, Mesh, type Object3D, type Ray, Raycaster } from 'three' /** @@ -62,7 +62,9 @@ export function wallHitInOpening( ray: Ray, hitDistance: number, ): boolean { - if (!(drawn instanceof Mesh)) return false + // A mesh caught between a palette and the groups a preview gave it cannot be + // raycast (three reads each group's material); the hit stands this move. + if (!(drawn instanceof Mesh && hasMaterialsForGroups(drawn))) return false drawnRaycaster.ray.copy(ray) drawnRaycaster.far = Number.POSITIVE_INFINITY const hits: Intersection[] = [] diff --git a/packages/nodes/src/wall/paint.ts b/packages/nodes/src/wall/paint.ts index f076337f75..84ae198449 100644 --- a/packages/nodes/src/wall/paint.ts +++ b/packages/nodes/src/wall/paint.ts @@ -127,7 +127,7 @@ export function resolveWallRole(args: { const { node, hitObject, localPosition, ray } = args if (node.wallType === 'curtain') { const root = sceneRegistry.nodes.get(node.id) - if (!(root && ray)) return null + if (!(root && ray && hasMaterialsForGroups(root))) return null wallSlotRaycaster.ray.copy(ray) const hit = wallSlotRaycaster.intersectObject(root, false)[0] return CURTAIN_WALL_ROLES[hit?.face?.materialIndex ?? -1] ?? null From 191bbc3615ebabe559d3ce3fbb6fc7f202a2bcd7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 15 Oct 2026 17:27:28 +0000 Subject: [PATCH 3/3] viewer: elevator clicks raycast only elevator buttons The capture-phase pointerdown raycast the whole scene on every click, so any mesh mid material swap threw from it. Any button along the ray takes the click, so raycasting the button subtrees gives the same result. --- .../elevator-interaction-system.test.ts | 26 +++++++++++++++++++ .../elevator/elevator-interaction-system.tsx | 19 +++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/viewer/src/systems/elevator/elevator-interaction-system.test.ts diff --git a/packages/viewer/src/systems/elevator/elevator-interaction-system.test.ts b/packages/viewer/src/systems/elevator/elevator-interaction-system.test.ts new file mode 100644 index 0000000000..5c87fc47a1 --- /dev/null +++ b/packages/viewer/src/systems/elevator/elevator-interaction-system.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, test } from 'bun:test' +import { BoxGeometry, Group, Mesh, MeshBasicMaterial, Raycaster, Scene, Vector3 } from 'three' +import { collectElevatorButtons } from './elevator-interaction-system' + +describe('elevator buttons', () => { + test('a click raycasts only the buttons, past a mesh whose groups have no material', () => { + const scene = new Scene() + // A wall caught mid paint preview in front of the button: three throws on any raycast of it. + const torn = new Mesh(new BoxGeometry(2, 2, 0.2), [new MeshBasicMaterial()]) + torn.geometry.clearGroups() + torn.geometry.addGroup(0, Number.POSITIVE_INFINITY, 4) + torn.position.z = 1 + const panel = new Group() + const button = new Mesh(new BoxGeometry(0.1, 0.1, 0.05), new MeshBasicMaterial()) + panel.userData.elevatorButton = { action: 'open-door', disabled: false, elevatorId: 'e' } + panel.add(button) + scene.add(torn, new Group().add(panel)) + scene.updateMatrixWorld(true) + + const buttons = collectElevatorButtons(scene) + expect(buttons).toEqual([panel]) + const raycaster = new Raycaster(new Vector3(0, 0, 5), new Vector3(0, 0, -1)) + expect(() => raycaster.intersectObjects(scene.children, true)).toThrow() + expect(raycaster.intersectObjects(buttons, true).map((hit) => hit.object)).toContain(button) + }) +}) diff --git a/packages/viewer/src/systems/elevator/elevator-interaction-system.tsx b/packages/viewer/src/systems/elevator/elevator-interaction-system.tsx index 2b5699dba5..539723ae5e 100644 --- a/packages/viewer/src/systems/elevator/elevator-interaction-system.tsx +++ b/packages/viewer/src/systems/elevator/elevator-interaction-system.tsx @@ -32,6 +32,20 @@ function getElevatorButtonData(object: Object3D): ElevatorButtonUserData | null return null } +/** + * The roots of every elevator button under `object`. Any button along the ray + * takes the click, so only they are raycast — never the rest of the scene, + * whose meshes this capture-phase listener must not trip over. + */ +export function collectElevatorButtons(object: Object3D, into: Object3D[] = []): Object3D[] { + if ((object.userData as { elevatorButton?: unknown }).elevatorButton) { + into.push(object) + return into + } + for (const child of object.children) collectElevatorButtons(child, into) + return into +} + export function ElevatorInteractionSystem() { const camera = useThree((state) => state.camera) const gl = useThree((state) => state.gl) @@ -45,6 +59,9 @@ export function ElevatorInteractionSystem() { const handlePointerDown = (event: PointerEvent) => { if (event.button !== 0) return + const buttons = collectElevatorButtons(scene) + if (buttons.length === 0) return + const rect = canvas.getBoundingClientRect() const pointer = pointerRef.current pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1 @@ -54,7 +71,7 @@ export function ElevatorInteractionSystem() { raycaster.setFromCamera(pointer, camera) const button = raycaster - .intersectObjects(scene.children, true) + .intersectObjects(buttons, true) .map((intersection) => getElevatorButtonData(intersection.object)) .find((data): data is ElevatorButtonUserData => data !== null)