Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/editor/src/lib/paint-pass-through.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Group,
type Intersection,
Mesh,
MeshBasicMaterial,
Path,
Ray,
Shape,
Expand Down Expand Up @@ -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', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/lib/paint-pass-through.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -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[] = []
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes/src/wall/paint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions packages/viewer/src/lib/pointer-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? []) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions packages/viewer/src/systems/wall/wall-cutout-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions packages/viewer/src/systems/wall/wall-cutout-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export type WallCutoutViewerStore = { getState: () => WallCutoutViewerState }
export class WallCutoutCache {
readonly walls = new Map<string, CachedWall>()
readonly rebuilt = new Set<string>()
/** Walls whose palette waits for a preview to give back their groups (see `mayAssign`). */
private readonly deferred = new Set<string>()
private viewer: WallCutoutViewerState | undefined
private nodes: ReturnType<typeof useScene.getState>['nodes'] | undefined
private materials: ReturnType<typeof useScene.getState>['materials'] | undefined
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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
}
}
Loading