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
10 changes: 10 additions & 0 deletions packages/editor/src/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SceneEnvironment,
useViewer,
Viewer,
type ViewerBreadcrumbHandler,
type ViewerImmersiveSession,
ViewerPresentations,
} from '@pascal-app/viewer'
Expand Down Expand Up @@ -284,6 +285,9 @@ export interface EditorProps {
// Thumbnail
onThumbnailCapture?: (blob: Blob, cameraData: SnapshotCameraData) => void

/** Viewer diagnostics for the host's error-reporter trail; see `<Viewer onBreadcrumb>`. */
onViewerBreadcrumb?: ViewerBreadcrumbHandler

/**
* When true, skip the viewer post-FX pipeline (same as `?disable=postFx`).
* Hosts use this for a stable local "light preview" without relying only on
Expand Down Expand Up @@ -1115,6 +1119,7 @@ const ViewerCanvas = memo(function ViewerCanvas({
sceneReadyKey,
onSceneReadyChange,
onThumbnailCapture,
onViewerBreadcrumb,
presentationsReady,
viewerSceneSlot,
floorplanSceneSlot,
Expand All @@ -1133,6 +1138,7 @@ const ViewerCanvas = memo(function ViewerCanvas({
sceneReadyKey: number
onSceneReadyChange: (ready: boolean) => void
onThumbnailCapture?: (blob: Blob, cameraData: SnapshotCameraData) => void
onViewerBreadcrumb?: ViewerBreadcrumbHandler
presentationsReady: boolean
viewerSceneSlot?: ReactNode
floorplanSceneSlot?: ReactNode
Expand Down Expand Up @@ -1292,6 +1298,7 @@ const ViewerCanvas = memo(function ViewerCanvas({
// Other modes retain the viewer's configured background policy.
transparent={presetIsolation === null ? undefined : true}
onSceneReadyChange={onSceneReadyChange}
onBreadcrumb={onViewerBreadcrumb}
renderContext="editor"
renderPaused={!show3d && !showLoader}
sceneReadyKey={sceneReadyKey}
Expand Down Expand Up @@ -1438,6 +1445,7 @@ function EditorContent({
loadingSlot,
onLoaderChange,
onThumbnailCapture,
onViewerBreadcrumb,
disablePostFx = false,
immersive,
sidebarOverlay,
Expand Down Expand Up @@ -1711,6 +1719,7 @@ function EditorContent({
defaultRender={EDITOR_DEFAULT_RENDER}
disablePostFx={disablePostFx}
hoverStyles={EDITOR_HOVER_STYLES}
onBreadcrumb={onViewerBreadcrumb}
renderContext="editor"
selectionManager="custom"
>
Expand Down Expand Up @@ -1752,6 +1761,7 @@ function EditorContent({
loadingOverlay={loadingOverlay}
onSceneReadyChange={handleSceneReadyChange}
onThumbnailCapture={onThumbnailCapture}
onViewerBreadcrumb={onViewerBreadcrumb}
presentationsReady={presentationsReady}
sceneReadyKey={sceneReadyKey}
showLoader={showLoader}
Expand Down
40 changes: 40 additions & 0 deletions packages/nodes/src/shared/swap-preview-material.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, test } from 'bun:test'
import { hasMaterialsForGroups } from '@pascal-app/viewer'
import { BufferGeometry, Mesh, MeshBasicMaterial } from 'three'
import { swapPreviewMaterial } from './swap-preview-material'

Expand All @@ -19,3 +20,42 @@ test('a paint preview undoes itself, but never over materials a rebuild gave the
again()
expect(mesh.material).toBe(rebuilt)
})

test('a regrouping preview swaps groups and materials together and undoes them as a pair', () => {
const base = new MeshBasicMaterial()
const palette = [base, base, base]
const geometry = new BufferGeometry()
geometry.addGroup(0, 3, 1)
const groups = geometry.groups
const mesh = new Mesh(geometry, palette)

const undo = swapPreviewMaterial(
mesh,
[base, base, base, base],
[{ start: 0, count: 3, materialIndex: 3 }],
)
expect(mesh.geometry.groups[0]!.materialIndex).toBe(3)
expect(hasMaterialsForGroups(mesh)).toBe(true)
undo()
expect(mesh.geometry.groups).toBe(groups)
expect(mesh.material).toBe(palette)
})

test('a preview never restores a shorter palette over groups a rebuild gave the mesh', () => {
const base = new MeshBasicMaterial()
const palette = [base, base, base]
const mesh = new Mesh(new BufferGeometry(), palette)
mesh.geometry.addGroup(0, 3, 1)
const preview = [base, base, base, base]
const undo = swapPreviewMaterial(mesh, preview, [{ start: 0, count: 3, materialIndex: 3 }])

// The click commits a new finish: the wall rebuilds a geometry drawing palette entry 3,
// while the cutout cache still holds the hovered wall's preview material.
const rebuilt = new BufferGeometry()
rebuilt.addGroup(0, 3, 3)
mesh.geometry = rebuilt
undo()
expect(mesh.geometry.groups[0]!.materialIndex).toBe(3)
expect(mesh.material).toBe(preview)
expect(hasMaterialsForGroups(mesh)).toBe(true)
})
31 changes: 23 additions & 8 deletions packages/nodes/src/shared/swap-preview-material.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import type { Material, Mesh } from 'three'
import { hasMaterialsForGroups } from '@pascal-app/viewer'
import type { BufferGeometry, Material, Mesh } from 'three'

/**
* Shows a paint preview by giving `mesh` another material (or material
* array), and returns the undo. The undo puts the original back only while the
* mesh still wears the preview: a commit or rebuild that gave it new materials
* meanwhile (a new finish adds a palette entry and a geometry group) keeps
* them — restoring the older, shorter array would leave a group with no
* material, which the next raycast trips over.
* array), and `groups` with it when the preview also regroups the geometry's
* triangles onto that array; returns the undo. Groups and materials index into
* each other, so the undo only puts back what still is the preview's: groups
* a rebuild or commit replaced meanwhile stay, and the original material goes
* back only over groups it can draw — restoring an older, shorter array over a
* rebuilt geometry's groups would leave a group with no material, which the
* next raycast trips over on `material.side`.
*/
export function swapPreviewMaterial(mesh: Mesh, preview: Material | Material[]): () => void {
export function swapPreviewMaterial(
mesh: Mesh,
preview: Material | Material[],
groups?: BufferGeometry['groups'],
): () => void {
const geometry = mesh.geometry
const previous = mesh.material
const previousGroups = geometry.groups
if (groups) geometry.groups = groups
mesh.material = preview
return () => {
if (mesh.material === preview) mesh.material = previous
if (groups && mesh.geometry === geometry && geometry.groups === groups) {
geometry.groups = previousGroups
}
if (mesh.material === preview && hasMaterialsForGroups(mesh, previous)) {
mesh.material = previous
}
}
}
15 changes: 4 additions & 11 deletions packages/nodes/src/wall/paint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ function sameChain(a: WallFaceChainFinish, b: WallFaceChainFinish) {
* triangle of this wall is resolved again against the planned wall and rooms,
* and takes the material that resolution draws — the paint where the click
* paints, what shows once erased where it erases. The material array is copied
* (never the shared cache) and the triangles are regrouped; cleanup restores
* both.
* (never the shared cache) and the triangles are regrouped onto it in one swap;
* cleanup restores both.
*/
function applyWallPreview(args: PaintPreviewArgs): (() => void) | null {
const { role, material, materialPreset } = args
Expand Down Expand Up @@ -299,16 +299,9 @@ function applyWallPreview(args: PaintPreviewArgs): (() => void) | null {
return lookIndex(chain.kind === 'legacy' ? chain.spec : { materialPreset: chain.ref })
}

const geometry = mesh.geometry
const groups = geometry.groups.map((group) => ({ ...group }))
const regrouped = regroupWallFaces(geometry, before, indexFor)
const regrouped = regroupWallFaces(mesh.geometry, before, indexFor)
if (!regrouped) return () => {}
geometry.groups = regrouped
const restoreMaterial = swapPreviewMaterial(mesh, materials)
return () => {
restoreMaterial()
if (mesh.geometry === geometry) geometry.groups = groups
}
return swapPreviewMaterial(mesh, materials, regrouped)
}

/**
Expand Down
15 changes: 13 additions & 2 deletions packages/viewer/src/components/viewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { PERF_OVERLAY_ENABLED } from '../../lib/gpu-perf'
import { applyIsolation, clearIsolation } from '../../lib/isolation'
import { ensureKtx2Support } from '../../lib/ktx2-loader'
import type { ColorPreset, RenderShading } from '../../lib/materials'
import { choosePointerEvents } from '../../lib/pointer-events'
import { choosePointerEvents, type ViewerBreadcrumbHandler } from '../../lib/pointer-events'
import { initializeGpuRenderer, type RendererPowerPreference } from '../../lib/renderer-capability'
import { getSceneTheme } from '../../lib/scene-themes'
import { installTextureNodeNullGuard } from '../../lib/texture-node-guard'
Expand Down Expand Up @@ -443,6 +443,12 @@ interface ViewerProps {
* that must not carry on silently (a bake) fail with its own error.
*/
onRenderError?: (cause: unknown) => void
/**
* Called with diagnostics worth keeping on a host's error-reporter trail,
* such as `raycast_invalid_material` when a pointer raycast skips a mesh
* whose geometry groups point past its material array.
*/
onBreadcrumb?: ViewerBreadcrumbHandler
/**
* Wall-clock give-up cap for scene readiness, replacing the default
* frame-count cap. Set it on hosts whose frame cadence is decoupled from
Expand Down Expand Up @@ -503,6 +509,7 @@ const Viewer = forwardRef<ViewerHandle, ViewerProps>(function Viewer(
sceneReadyKey,
onSceneReadyChange,
onRenderError,
onBreadcrumb,
sceneReadyMaxWaitMs,
maxFps = 50,
disablePostFx = false,
Expand Down Expand Up @@ -541,7 +548,11 @@ const Viewer = forwardRef<ViewerHandle, ViewerProps>(function Viewer(
}
}, [isolate])

const [pointerEvents] = useState(() => choosePointerEvents())
const onBreadcrumbRef = useRef(onBreadcrumb)
onBreadcrumbRef.current = onBreadcrumb
const [pointerEvents] = useState(() =>
choosePointerEvents(undefined, (breadcrumb) => onBreadcrumbRef.current?.(breadcrumb)),
)

const [rendererInitFailed, setRendererInitFailed] = useState(false)

Expand Down
7 changes: 6 additions & 1 deletion packages/viewer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ export { createNodeTopSurfaceHeightSampler } from './lib/node-top-surface-height
export * from './lib/perf-actions'
export { type PerfBatchStats, publishPerfBatchStats } from './lib/perf-panel-store'
export * from './lib/perf-tracks'
export { hasMaterialsForGroups, markPureRaycast } from './lib/pointer-events'
export {
hasMaterialsForGroups,
markPureRaycast,
type ViewerBreadcrumb,
type ViewerBreadcrumbHandler,
} from './lib/pointer-events'
export {
cloneWithProceduralEmission,
decorateProceduralEmission,
Expand Down
8 changes: 4 additions & 4 deletions packages/viewer/src/lib/pointer-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,11 +842,11 @@ describe('R3F 9.8.2 pointer-event differential', () => {
try {
for (const environment of ['production', 'development', 'test']) {
process.env.NODE_ENV = environment
expect(choosePointerEvents('')).toBe(createPascalPointerEvents)
expect(choosePointerEvents('')).not.toBe(stockEvents)
for (const search of ['?stockEvents', '?stockEvents=false']) {
expect(choosePointerEvents(search)).toBe(
environment === 'production' ? createPascalPointerEvents : stockEvents,
)
const chosen = choosePointerEvents(search)
if (environment === 'production') expect(chosen).not.toBe(stockEvents)
else expect(chosen).toBe(stockEvents)
}
}
} finally {
Expand Down
Loading
Loading