diff --git a/docs/product-readiness.md b/docs/product-readiness.md index 2088552..bb2c1dd 100644 --- a/docs/product-readiness.md +++ b/docs/product-readiness.md @@ -349,9 +349,11 @@ Scene evidence: - Pre-frozen mesh clone cache: retained mesh sources are frozen once after load. Repeated clone construction reuses that frozen state and skips the per-clone static-subtree traversal and spin scan when the source has no animated nodes. -- Hover bounds reuse: hover focus and outline use the existing pick proxy bounds - when present, so hovering a large shelf/product group does not call - `BoxHelper.setFromObject` on the full visual subtree. +- Cached hover/drag bounds: hover focus, hover outline, and drag highlight use + the existing pick proxy bounds through a `Box3Helper` when present, so + hovering or dragging a large shelf/product group does not call + `BoxHelper.setFromObject` on the full visual subtree and does not allocate + cloned focus vectors every animation frame. - Scene-load render gating: large scene roots are batched into one Three.js insertion after object preparation, and the render loop skips partial scene frames while the batch is still loading. @@ -388,19 +390,20 @@ Scene evidence: Bundle evidence: - JavaScript chunks: `4` -- App entry chunk: `190.55 kB raw / 49.66 KiB gzip` -- Largest JavaScript gzip chunk: `141.04 KiB / 170.00 KiB` -- Total JavaScript gzip: `213.40 KiB / 230.00 KiB` +- App entry chunk: `191.83 kB raw / 49.90 kB gzip` +- Largest JavaScript gzip chunk: `140.95 KiB / 170.00 KiB` +- Total JavaScript gzip: `213.54 KiB / 230.00 KiB` - Large-scene performance mode: `large-scene`, target `45 fps`, actual idle render rate `0.7 fps`, DPR cap observed at `1.00` in release gate, `preserveDrawingBuffer=false`, `1` explicit readback render, shadows - `off`, hover picking throttled to `32ms`, `27` empty frames + `off`, hover picking throttled to `32ms`, `28` empty frames skipped, `1839` static visual nodes frozen, `627` mesh clones using pre-frozen source state, `627` repeated spin scans skipped, `7` primitive geometry shapes cached, `51` mesh source assets retained, `576/51` mesh cache hit/miss in the primary large scene, `636` cached object bounds, `627` fast pick-proxy - bounds from `51` cached source bounds, real hover proof `hoverProxy 1/fallback - 0`, with `0` camera-fit traversals, `1/636` scene root batch insertion, `330` + bounds from `51` cached source bounds, real hover proof `hoverProxy 1` with + cached bounds and `fallback 0`, with `0` camera-fit traversals, `1/636` + scene root batch insertion, `329` partial render frames suppressed during the primary load, `26` browser event-loop yields during scene construction, active state payload `34/602` with `3` reduced frames and @@ -437,8 +440,9 @@ Bundle evidence: `prefrozen=627` and `spinSkip=627`, proving repeated mesh clones avoid re-traversing static visual subtrees and non-spinning clone trees. - Hover proxy bounds gate: `browser-hover-proxy-bounds` moves the mouse over the - real canvas and now checks `hoverProxy=1/fallback=0`, proving hover highlight - uses proxy bounds instead of traversing the visual root. + real canvas and now checks both `hoverProxy` and cached hover bounds increase + while `fallback=0`, proving hover highlight uses proxy bounds instead of + traversing the visual root. - Scene-load batching gate: `browser-large-scene-performance-mode` now checks `scene batches 1/636` and `yields 26`, proving the large scene is attached as a single batch while object construction still yields to the browser event @@ -449,34 +453,34 @@ Bundle evidence: Control evidence: -- Cold compose endpoint: `269ms`, `4/4` composed object bodies synced. -- Repeated compose cache: `64ms`, `cached=true`, same compose key. +- Cold compose endpoint: `275ms`, `4/4` composed object bodies synced. +- Repeated compose cache: `67ms`, `cached=true`, same compose key. - Active state endpoint: full `/state` returned `602` bodies; active - `/state?bodies=active` returned `34` bodies in `59ms`, filtered `568` fixed + `/state?bodies=active` returned `34` bodies in `64ms`, filtered `568` fixed bodies, and retained `4/4` composed dynamic bodies. -- Scene Builder UI compose: representative run `4298ms` end to end with real +- Scene Builder UI compose: representative run `4400ms` end to end with real backend diagnostics. -- Share-link restore: representative full browser restore `6138ms`. -- Config import restore: representative full browser restore `6665ms`. +- Share-link restore: representative full browser restore `6379ms`. +- Config import restore: representative full browser restore `6706ms`. - Composed object body edit: `57ms`. -- Fixed inventory edit rejection: `57ms`. +- Fixed inventory edit rejection: `58ms`. - Dynamic composed object drop: `0.48012m`. - Static shelf anchor stability: `max drift 0.00000m` after a 2500 ms resume window. -- Resume backend state: `59ms paused=false` -- Resume visible UI state: `57ms` -- Pause backend state: `37ms paused=true` -- Pause visible UI state: `39ms` +- Resume backend state: `57ms paused=false` +- Resume visible UI state: `58ms` +- Pause backend state: `33ms paused=true` +- Pause visible UI state: `35ms` - Desktop acceptance wait: `1243ms` total, product telemetry `0ms` - Mobile acceptance wait: `277ms` total, product telemetry `0ms` - Runtime diagnostics panel: `state=http://127.0.0.1:18090/state`, `control=http://127.0.0.1:18090/control`, - `latency=响应 36 ms / 确认 36 ms / 可见 37 ms`, + `latency=响应 34 ms / 确认 35 ms / 可见 35 ms`, `acceptanceLatency=0 ms / 11-0-0`, `binding=635/636; 4/568`, `builderPhysics=已添加物体 4: 后端动态 4 / 仅视觉 0`, - `renderPerformance=large-scene; target 45 fps; actual 0.7 fps; dpr 1.00; buffer preserved false; readback renders 1; shadows off; skipped 26; hover 32ms; frozen 1839 prefrozen 627 spinSkip 627; manual roots 636 updates 67; primitive geometries 7; mesh cache 51 sources 576/51 hit/miss retained 51g/17m; bounds cache 636 fast 627 source 51 hoverProxy 1/fallback 0 camera traversals 0; scene batches 1/636 suppressed 330 yields 26; state payload 34/602 reduced 3 incremental 4; ui cache physics 31/3 builder 34/1; pick proxies 636; state apply 0/skip 67; unchanged frames 2; released 0g/0m/0t`. + `renderPerformance=large-scene; target 45 fps; actual 0.7 fps; dpr 1.00; buffer preserved false; readback renders 1; shadows off; skipped 28; hover 32ms; frozen 1839 prefrozen 627 spinSkip 627; manual roots 636 updates 67; primitive geometries 7; mesh cache 51 sources 576/51 hit/miss retained 51g/17m; bounds cache 636 fast 627 source 51 hoverProxy 1 cached 1/fallback 0 camera traversals 0; scene batches 1/636 suppressed 329 yields 26; state payload 34/602 reduced 3 incremental 4; ui cache physics 30/3 builder 33/1; pick proxies 636; state apply 0/skip 67; unchanged frames 1; released 0g/0m/0t`. - Evidence screenshot artifacts: `builder page; share page; import page; visual-only boundary canvas; desktop canvas; debug PNG; mobile canvas` - Evidence export artifacts: @@ -573,7 +577,7 @@ Current performance boundaries: first load, Scene Builder compose, share restore, and config import restore still include browser asset creation and scene graph setup time. - The real backend control path is not the observed bottleneck in the current - gate: pause/resume visible UI confirmation is `52-57ms`, and the backend + gate: pause/resume visible UI confirmation is `35-58ms`, and the backend physics step sample during pause verification reports `0.51ms` mean and `0.81ms` p95 step time. - The remaining high-impact optimization area is asset-level rendering: diff --git a/scripts/product-readiness-gate.mjs b/scripts/product-readiness-gate.mjs index 3d34953..5e4b3ab 100644 --- a/scripts/product-readiness-gate.mjs +++ b/scripts/product-readiness-gate.mjs @@ -665,6 +665,7 @@ async function readBrowserStats(page) { fastPickProxyBoundsCount: numberData("fastPickProxyBoundsCount"), meshSourceBoundsCount: numberData("meshSourceBoundsCount"), hoverProxyBoundsHitCount: numberData("hoverProxyBoundsHitCount"), + hoverCachedBoundsHitCount: numberData("hoverCachedBoundsHitCount"), hoverRootBoundsFallbackCount: numberData("hoverRootBoundsFallbackCount"), cameraFitTraversalCount: numberData("cameraFitTraversalCount"), sceneLoadSuppressedRenderCount: numberData("sceneLoadSuppressedRenderCount"), @@ -720,6 +721,7 @@ async function waitForStableBodyStateCache(page) { async function exerciseHoverProxyBounds(page) { const before = await readBrowserStats(page); const beforeHits = before.renderPerformance?.hoverProxyBoundsHitCount ?? 0; + const beforeCachedHits = before.renderPerformance?.hoverCachedBoundsHitCount ?? 0; const canvasBox = await page.locator("canvas").boundingBox(); if (!canvasBox) { return { ok: false, before, stats: before, hoverText: "", detail: "canvas not found" }; @@ -737,13 +739,18 @@ async function exerciseHoverProxyBounds(page) { await page.waitForTimeout(90); stats = await readBrowserStats(page); hoverText = await page.locator("#hover-target").textContent().catch(() => "") ?? ""; - if ((stats.renderPerformance?.hoverProxyBoundsHitCount ?? 0) > beforeHits && hoverText.trim()) { + if ( + (stats.renderPerformance?.hoverProxyBoundsHitCount ?? 0) > beforeHits + && (stats.renderPerformance?.hoverCachedBoundsHitCount ?? 0) > beforeCachedHits + && hoverText.trim() + ) { return { ok: true, before, stats, hoverText: hoverText.trim(), point }; } } } return { - ok: (stats.renderPerformance?.hoverProxyBoundsHitCount ?? 0) > beforeHits, + ok: (stats.renderPerformance?.hoverProxyBoundsHitCount ?? 0) > beforeHits + && (stats.renderPerformance?.hoverCachedBoundsHitCount ?? 0) > beforeCachedHits, before, stats, hoverText: hoverText.trim(), @@ -1636,6 +1643,7 @@ async function verifyBrowser() { && (renderPerformance.sceneObjectBoundsCount ?? 0) >= objectCount && (renderPerformance.fastPickProxyBoundsCount ?? 0) >= thresholds.minMeshes && (renderPerformance.meshSourceBoundsCount ?? 0) > 0 + && (renderPerformance.hoverCachedBoundsHitCount ?? 0) > 0 && (renderPerformance.cameraFitTraversalCount ?? Number.POSITIVE_INFINITY) === 0 && (renderPerformance.sceneBuildYieldCount ?? 0) > 0 && (renderPerformance.reducedStateFrameCount ?? 0) > 0 @@ -1649,7 +1657,7 @@ async function verifyBrowser() { && (renderPerformance.pickProxyCount ?? 0) >= objectCount && (renderPerformance.skippedBodyStateCount ?? 0) > 0 && desktopStats.diagnostics.renderPerformance.includes("large-scene"), - `mode=${renderPerformance.largeSceneMode} fps=${renderPerformance.targetRenderFps ?? "-"} dpr=${renderPerformance.renderPixelRatio ?? "-"} buffer=${renderPerformance.preserveDrawingBufferEnabled} readback=${renderPerformance.readbackRenderCount ?? "-"} shadows=${renderPerformance.shadowsEnabled} hover=${renderPerformance.hoverPickIntervalMs ?? "-"} skipped=${renderPerformance.renderSkipCount ?? "-"} frozen=${renderPerformance.frozenStaticObjectCount ?? "-"} prefrozen=${renderPerformance.preFrozenMeshCloneCount ?? "-"} spinSkip=${renderPerformance.spinningScanSkippedCount ?? "-"} manualRoots=${renderPerformance.manualMatrixRootCount ?? "-"} manualUpdates=${renderPerformance.manualMatrixUpdateCount ?? "-"} primitiveGeometries=${renderPerformance.primitiveGeometryCount ?? "-"} meshCache=${renderPerformance.meshCacheSourceCount ?? "-"} hit=${renderPerformance.meshCacheHitCount ?? "-"} miss=${renderPerformance.meshCacheMissCount ?? "-"} retained=${renderPerformance.retainedMeshGeometryCount ?? "-"}g/${renderPerformance.retainedMeshMaterialCount ?? "-"}m bounds=${renderPerformance.sceneObjectBoundsCount ?? "-"} fast=${renderPerformance.fastPickProxyBoundsCount ?? "-"} source=${renderPerformance.meshSourceBoundsCount ?? "-"} hoverProxy=${renderPerformance.hoverProxyBoundsHitCount ?? "-"}/fallback=${renderPerformance.hoverRootBoundsFallbackCount ?? "-"} cameraTraversals=${renderPerformance.cameraFitTraversalCount ?? "-"} sceneBatches=${renderPerformance.sceneRootBatchCount ?? "-"}/${renderPerformance.batchedSceneRootCount ?? "-"} suppressed=${renderPerformance.sceneLoadSuppressedRenderCount ?? "-"} yields=${renderPerformance.sceneBuildYieldCount ?? "-"} statePayload=${renderPerformance.statePayloadBodyCount ?? "-"}/${renderPerformance.mergedBodyStateCount ?? "-"} reduced=${renderPerformance.reducedStateFrameCount ?? "-"} incremental=${renderPerformance.incrementalBodyMergeCount ?? "-"} uiCache=physics ${renderPerformance.objectPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.objectPhysicsProfileCacheMissCount ?? "-"} builder ${renderPerformance.builderPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.builderPhysicsProfileCacheMissCount ?? "-"} pickProxies=${renderPerformance.pickProxyCount ?? "-"} stateApply=${renderPerformance.appliedBodyStateCount ?? "-"}/skip=${renderPerformance.skippedBodyStateCount ?? "-"} unchanged=${renderPerformance.unchangedFrameCount ?? "-"} released=${renderPerformance.releasedGeometryCount ?? "-"}g/${renderPerformance.releasedMaterialCount ?? "-"}m/${renderPerformance.releasedTextureCount ?? "-"}t; ${desktopStats.diagnostics.renderPerformance}`, + `mode=${renderPerformance.largeSceneMode} fps=${renderPerformance.targetRenderFps ?? "-"} dpr=${renderPerformance.renderPixelRatio ?? "-"} buffer=${renderPerformance.preserveDrawingBufferEnabled} readback=${renderPerformance.readbackRenderCount ?? "-"} shadows=${renderPerformance.shadowsEnabled} hover=${renderPerformance.hoverPickIntervalMs ?? "-"} skipped=${renderPerformance.renderSkipCount ?? "-"} frozen=${renderPerformance.frozenStaticObjectCount ?? "-"} prefrozen=${renderPerformance.preFrozenMeshCloneCount ?? "-"} spinSkip=${renderPerformance.spinningScanSkippedCount ?? "-"} manualRoots=${renderPerformance.manualMatrixRootCount ?? "-"} manualUpdates=${renderPerformance.manualMatrixUpdateCount ?? "-"} primitiveGeometries=${renderPerformance.primitiveGeometryCount ?? "-"} meshCache=${renderPerformance.meshCacheSourceCount ?? "-"} hit=${renderPerformance.meshCacheHitCount ?? "-"} miss=${renderPerformance.meshCacheMissCount ?? "-"} retained=${renderPerformance.retainedMeshGeometryCount ?? "-"}g/${renderPerformance.retainedMeshMaterialCount ?? "-"}m bounds=${renderPerformance.sceneObjectBoundsCount ?? "-"} fast=${renderPerformance.fastPickProxyBoundsCount ?? "-"} source=${renderPerformance.meshSourceBoundsCount ?? "-"} hoverProxy=${renderPerformance.hoverProxyBoundsHitCount ?? "-"} cached=${renderPerformance.hoverCachedBoundsHitCount ?? "-"}/fallback=${renderPerformance.hoverRootBoundsFallbackCount ?? "-"} cameraTraversals=${renderPerformance.cameraFitTraversalCount ?? "-"} sceneBatches=${renderPerformance.sceneRootBatchCount ?? "-"}/${renderPerformance.batchedSceneRootCount ?? "-"} suppressed=${renderPerformance.sceneLoadSuppressedRenderCount ?? "-"} yields=${renderPerformance.sceneBuildYieldCount ?? "-"} statePayload=${renderPerformance.statePayloadBodyCount ?? "-"}/${renderPerformance.mergedBodyStateCount ?? "-"} reduced=${renderPerformance.reducedStateFrameCount ?? "-"} incremental=${renderPerformance.incrementalBodyMergeCount ?? "-"} uiCache=physics ${renderPerformance.objectPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.objectPhysicsProfileCacheMissCount ?? "-"} builder ${renderPerformance.builderPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.builderPhysicsProfileCacheMissCount ?? "-"} pickProxies=${renderPerformance.pickProxyCount ?? "-"} stateApply=${renderPerformance.appliedBodyStateCount ?? "-"}/skip=${renderPerformance.skippedBodyStateCount ?? "-"} unchanged=${renderPerformance.unchangedFrameCount ?? "-"} released=${renderPerformance.releasedGeometryCount ?? "-"}g/${renderPerformance.releasedMaterialCount ?? "-"}m/${renderPerformance.releasedTextureCount ?? "-"}t; ${desktopStats.diagnostics.renderPerformance}`, ); check( "browser-debug-capture-png", @@ -1660,8 +1668,9 @@ async function verifyBrowser() { "browser-hover-proxy-bounds", hoverProxyBounds.ok && (hoverProxyBounds.stats.renderPerformance?.hoverProxyBoundsHitCount ?? 0) > (hoverProxyBounds.before.renderPerformance?.hoverProxyBoundsHitCount ?? 0) + && (hoverProxyBounds.stats.renderPerformance?.hoverCachedBoundsHitCount ?? 0) > (hoverProxyBounds.before.renderPerformance?.hoverCachedBoundsHitCount ?? 0) && (hoverProxyBounds.stats.renderPerformance?.hoverRootBoundsFallbackCount ?? Number.POSITIVE_INFINITY) === 0, - `point=${hoverProxyBounds.point ?? "-"} text=${hoverProxyBounds.hoverText || "-"} hit=${hoverProxyBounds.stats.renderPerformance?.hoverProxyBoundsHitCount ?? "-"} before=${hoverProxyBounds.before.renderPerformance?.hoverProxyBoundsHitCount ?? "-"} fallback=${hoverProxyBounds.stats.renderPerformance?.hoverRootBoundsFallbackCount ?? "-"}; ${hoverProxyBounds.stats.diagnostics.renderPerformance}`, + `point=${hoverProxyBounds.point ?? "-"} text=${hoverProxyBounds.hoverText || "-"} hit=${hoverProxyBounds.stats.renderPerformance?.hoverProxyBoundsHitCount ?? "-"} cached=${hoverProxyBounds.stats.renderPerformance?.hoverCachedBoundsHitCount ?? "-"} before=${hoverProxyBounds.before.renderPerformance?.hoverCachedBoundsHitCount ?? "-"} fallback=${hoverProxyBounds.stats.renderPerformance?.hoverRootBoundsFallbackCount ?? "-"}; ${hoverProxyBounds.stats.diagnostics.renderPerformance}`, ); const builderRelease = builderCompose.renderPerformance ?? {}; check( diff --git a/src/main.ts b/src/main.ts index abfdeb6..043de80 100644 --- a/src/main.ts +++ b/src/main.ts @@ -192,6 +192,7 @@ interface RuntimeDiagnostics { fastPickProxyBoundsCount: number; meshSourceBoundsCount: number; hoverProxyBoundsHitCount: number; + hoverCachedBoundsHitCount: number; hoverRootBoundsFallbackCount: number; cameraFitTraversalCount: number; sceneLoadSuppressedRenderCount: number; @@ -2519,6 +2520,7 @@ function currentRuntimeDiagnostics( fastPickProxyBoundsCount: stats.fastPickProxyBoundsCount, meshSourceBoundsCount: stats.meshSourceBoundsCount, hoverProxyBoundsHitCount: stats.hoverProxyBoundsHitCount, + hoverCachedBoundsHitCount: stats.hoverCachedBoundsHitCount, hoverRootBoundsFallbackCount: stats.hoverRootBoundsFallbackCount, cameraFitTraversalCount: stats.cameraFitTraversalCount, sceneLoadSuppressedRenderCount: stats.sceneLoadSuppressedRenderCount, @@ -2591,7 +2593,7 @@ function currentDiagnosticsReport(): string { function renderPerformanceLabel(stats: RuntimeDiagnostics["stats"]): string { const mode = stats.largeSceneMode ? "large-scene" : "standard"; const shadows = stats.shadowsEnabled ? "shadows on" : "shadows off"; - return `${mode}; target ${stats.targetRenderFps} fps; actual ${stats.actualRenderFps.toFixed(1)} fps; dpr ${stats.renderPixelRatio.toFixed(2)}; buffer preserved ${stats.preserveDrawingBufferEnabled}; readback renders ${stats.readbackRenderCount}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; frozen ${stats.frozenStaticObjectCount} prefrozen ${stats.preFrozenMeshCloneCount} spinSkip ${stats.spinningScanSkippedCount}; manual roots ${stats.manualMatrixRootCount} updates ${stats.manualMatrixUpdateCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; bounds cache ${stats.sceneObjectBoundsCount} fast ${stats.fastPickProxyBoundsCount} source ${stats.meshSourceBoundsCount} hoverProxy ${stats.hoverProxyBoundsHitCount}/fallback ${stats.hoverRootBoundsFallbackCount} camera traversals ${stats.cameraFitTraversalCount}; scene batches ${stats.sceneRootBatchCount}/${stats.batchedSceneRootCount} suppressed ${stats.sceneLoadSuppressedRenderCount} yields ${stats.sceneBuildYieldCount}; state payload ${stats.statePayloadBodyCount}/${stats.mergedBodyStateCount} reduced ${stats.reducedStateFrameCount} incremental ${stats.incrementalBodyMergeCount}; ui cache physics ${stats.objectPhysicsProfileCacheHitCount}/${stats.objectPhysicsProfileCacheMissCount} builder ${stats.builderPhysicsProfileCacheHitCount}/${stats.builderPhysicsProfileCacheMissCount}; pick proxies ${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; unchanged frames ${stats.unchangedFrameCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`; + return `${mode}; target ${stats.targetRenderFps} fps; actual ${stats.actualRenderFps.toFixed(1)} fps; dpr ${stats.renderPixelRatio.toFixed(2)}; buffer preserved ${stats.preserveDrawingBufferEnabled}; readback renders ${stats.readbackRenderCount}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; frozen ${stats.frozenStaticObjectCount} prefrozen ${stats.preFrozenMeshCloneCount} spinSkip ${stats.spinningScanSkippedCount}; manual roots ${stats.manualMatrixRootCount} updates ${stats.manualMatrixUpdateCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; bounds cache ${stats.sceneObjectBoundsCount} fast ${stats.fastPickProxyBoundsCount} source ${stats.meshSourceBoundsCount} hoverProxy ${stats.hoverProxyBoundsHitCount} cached ${stats.hoverCachedBoundsHitCount}/fallback ${stats.hoverRootBoundsFallbackCount} camera traversals ${stats.cameraFitTraversalCount}; scene batches ${stats.sceneRootBatchCount}/${stats.batchedSceneRootCount} suppressed ${stats.sceneLoadSuppressedRenderCount} yields ${stats.sceneBuildYieldCount}; state payload ${stats.statePayloadBodyCount}/${stats.mergedBodyStateCount} reduced ${stats.reducedStateFrameCount} incremental ${stats.incrementalBodyMergeCount}; ui cache physics ${stats.objectPhysicsProfileCacheHitCount}/${stats.objectPhysicsProfileCacheMissCount} builder ${stats.builderPhysicsProfileCacheHitCount}/${stats.builderPhysicsProfileCacheMissCount}; pick proxies ${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; unchanged frames ${stats.unchangedFrameCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`; } function renderDiagnosticsPanel(diagnostics = currentRuntimeDiagnostics()): void { @@ -2637,6 +2639,7 @@ function renderDiagnosticsPanel(diagnostics = currentRuntimeDiagnostics()): void data.fastPickProxyBoundsCount = String(diagnostics.stats.fastPickProxyBoundsCount); data.meshSourceBoundsCount = String(diagnostics.stats.meshSourceBoundsCount); data.hoverProxyBoundsHitCount = String(diagnostics.stats.hoverProxyBoundsHitCount); + data.hoverCachedBoundsHitCount = String(diagnostics.stats.hoverCachedBoundsHitCount); data.hoverRootBoundsFallbackCount = String(diagnostics.stats.hoverRootBoundsFallbackCount); data.cameraFitTraversalCount = String(diagnostics.stats.cameraFitTraversalCount); data.sceneLoadSuppressedRenderCount = String(diagnostics.stats.sceneLoadSuppressedRenderCount); diff --git a/src/preview/UniversalPreviewer.ts b/src/preview/UniversalPreviewer.ts index 9b59d11..da20e3d 100644 --- a/src/preview/UniversalPreviewer.ts +++ b/src/preview/UniversalPreviewer.ts @@ -114,7 +114,12 @@ export class UniversalPreviewer { private readonly pickRoots: THREE.Object3D[] = []; private readonly editablePickRoots: THREE.Object3D[] = []; private readonly spinningObjects: THREE.Object3D[] = []; - private readonly hoverOutline = new THREE.BoxHelper(new THREE.Object3D(), "#38bdf8"); + private readonly hoverOutlineBounds = new THREE.Box3(); + private readonly hoverOutline = new THREE.Box3Helper(this.hoverOutlineBounds, "#38bdf8"); + private readonly scratchBounds = new THREE.Box3(); + private readonly scratchSize = new THREE.Vector3(); + private readonly scratchPreviousFocusPosition = new THREE.Vector3(); + private readonly scratchPreviousFocusQuaternion = new THREE.Quaternion(); private readonly editableObjectIds = new Set(); private readonly clock = new THREE.Clock(); private readonly raycaster = new THREE.Raycaster(); @@ -159,6 +164,7 @@ export class UniversalPreviewer { private sceneObjectBoundsCount = 0; private fastPickProxyBoundsCount = 0; private hoverProxyBoundsHitCount = 0; + private hoverCachedBoundsHitCount = 0; private hoverRootBoundsFallbackCount = 0; private cameraFitTraversalCount = 0; private sceneLoadInProgress = false; @@ -215,6 +221,7 @@ export class UniversalPreviewer { this.renderer.shadowMap.type = THREE.PCFSoftShadowMap; this.renderer.domElement.className = "preview-canvas"; this.container.appendChild(this.renderer.domElement); + this.pickProxyGeometry.computeBoundingBox(); this.scene.background = new THREE.Color("#0f172a"); const environmentGenerator = new THREE.PMREMGenerator(this.renderer); @@ -449,6 +456,7 @@ export class UniversalPreviewer { fastPickProxyBoundsCount: this.fastPickProxyBoundsCount, meshSourceBoundsCount: this.meshSourceBounds.size, hoverProxyBoundsHitCount: this.hoverProxyBoundsHitCount, + hoverCachedBoundsHitCount: this.hoverCachedBoundsHitCount, hoverRootBoundsFallbackCount: this.hoverRootBoundsFallbackCount, cameraFitTraversalCount: this.cameraFitTraversalCount, sceneLoadSuppressedRenderCount: this.sceneLoadSuppressedRenderCount, @@ -811,8 +819,8 @@ export class UniversalPreviewer { } } - private showHoverOutline(target: THREE.Object3D, colorHex: string): void { - this.hoverOutline.setFromObject(target); + private showHoverOutlineAtBounds(bounds: THREE.Box3, colorHex: string): void { + this.hoverOutlineBounds.copy(bounds); (this.hoverOutline.material as THREE.LineBasicMaterial).color.set(colorHex); this.hoverOutline.visible = true; this.markRenderDirty(); @@ -845,25 +853,45 @@ export class UniversalPreviewer { this.hoverLabel.title = name; } - private showFocusAtObject(target: THREE.Object3D, colorHex: string, radiusScale = 1): void { - const bounds = new THREE.Box3().setFromObject(target); - const center = new THREE.Vector3(); - const size = new THREE.Vector3(); + private showFocusAtBounds(bounds: THREE.Box3, target: THREE.Object3D, colorHex: string, radiusScale = 1): void { if (bounds.isEmpty()) { - target.getWorldPosition(center); + target.getWorldPosition(this.focusMarkerTarget); this.focusMarkerRadius = 0.12 * radiusScale; } else { - bounds.getCenter(center); - bounds.getSize(size); - this.focusMarkerRadius = Math.max(0.08, Math.max(size.x, size.y, size.z) * 0.5 * radiusScale); + bounds.getCenter(this.focusMarkerTarget); + bounds.getSize(this.scratchSize); + this.focusMarkerRadius = Math.max( + 0.08, + Math.max(this.scratchSize.x, this.scratchSize.y, this.scratchSize.z) * 0.5 * radiusScale, + ); } - this.focusMarkerTarget.copy(center); this.focusMarker.visible = true; (this.focusMarker.material as THREE.MeshBasicMaterial).color.set(colorHex); window.clearTimeout(this.focusMarkerHideHandle); this.markRenderDirty(); } + private showFocusAndOutlineForObject(id: string, root: THREE.Object3D, colorHex: string, radiusScale = 1): void { + const bounds = this.cachedInteractionBounds(id, root); + this.showFocusAtBounds(bounds, root, colorHex, radiusScale); + this.showHoverOutlineAtBounds(bounds, colorHex); + } + + private cachedInteractionBounds(id: string, root: THREE.Object3D): THREE.Box3 { + const proxy = this.pickProxyById.get(id); + const proxyGeometryBounds = this.pickProxyGeometry.boundingBox; + if (proxy && proxyGeometryBounds) { + proxy.updateWorldMatrix(true, false); + this.scratchBounds.copy(proxyGeometryBounds).applyMatrix4(proxy.matrixWorld); + if (!this.scratchBounds.isEmpty()) { + this.hoverCachedBoundsHitCount += 1; + return this.scratchBounds; + } + } + this.hoverRootBoundsFallbackCount += 1; + return this.scratchBounds.setFromObject(root); + } + private setHoverFromPick( pick: { id: string; bound: BoundObject; editable: boolean; disabledReason?: string } | undefined, ): void { @@ -879,16 +907,11 @@ export class UniversalPreviewer { } if (this.hoveredObjectId !== pick.id) { this.hoveredObjectId = pick.id; - const color = pick.editable ? "#38bdf8" : "#f59e0b"; - const highlightTarget = this.pickProxyById.get(pick.id); - if (highlightTarget) { + if (this.pickProxyById.has(pick.id)) { this.hoverProxyBoundsHitCount += 1; - } else { - this.hoverRootBoundsFallbackCount += 1; } - const boundsTarget = highlightTarget ?? pick.bound.root; - this.showFocusAtObject(boundsTarget, color, 1.05); - this.showHoverOutline(boundsTarget, color); + const color = pick.editable ? "#38bdf8" : "#f59e0b"; + this.showFocusAndOutlineForObject(pick.id, pick.bound.root, color, 1.05); this.setInteractionMode(pick.editable ? "hover" : "inspect"); } this.updateHoverLabel(pick.id, pick.bound, pick.editable, pick.disabledReason); @@ -928,16 +951,16 @@ export class UniversalPreviewer { if (!this.focusMarker.visible) { return false; } - const previousPosition = this.focusMarker.position.clone(); - const previousQuaternion = this.focusMarker.quaternion.clone(); + this.scratchPreviousFocusPosition.copy(this.focusMarker.position); + this.scratchPreviousFocusQuaternion.copy(this.focusMarker.quaternion); const previousScale = this.focusMarker.scale.x; this.focusMarker.position.copy(this.focusMarkerTarget); this.focusMarker.quaternion.copy(this.camera.quaternion); const distance = this.camera.position.distanceTo(this.focusMarker.position); const scale = Math.max(0.06, distance * 0.12 * this.focusMarkerRadius); this.focusMarker.scale.setScalar(scale); - return previousPosition.distanceToSquared(this.focusMarker.position) > 1e-10 - || Math.abs(previousQuaternion.dot(this.focusMarker.quaternion)) < 0.999999 + return this.scratchPreviousFocusPosition.distanceToSquared(this.focusMarker.position) > 1e-10 + || Math.abs(this.scratchPreviousFocusQuaternion.dot(this.focusMarker.quaternion)) < 0.999999 || Math.abs(previousScale - scale) > 1e-6; } @@ -991,6 +1014,7 @@ export class UniversalPreviewer { this.sceneObjectBoundsCount = 0; this.fastPickProxyBoundsCount = 0; this.hoverProxyBoundsHitCount = 0; + this.hoverCachedBoundsHitCount = 0; this.hoverRootBoundsFallbackCount = 0; this.cameraFitTraversalCount = 0; this.sceneLoadSuppressedRenderCount = 0; @@ -1226,8 +1250,7 @@ export class UniversalPreviewer { start: true, end: false, }); - this.showFocusAtObject(bound.root, "#f97316", 1.2); - this.showHoverOutline(bound.root, "#f97316"); + this.showFocusAndOutlineForObject(id, bound.root, "#f97316", 1.2); this.setInteractionMode("dragging"); return true; } @@ -1252,6 +1275,7 @@ export class UniversalPreviewer { this.updateManualObjectMatrix(bound.root); } this.updateDraggedBodyOffsets(deltaSource); + this.refreshDragHighlight(); this.markRenderDirty(); const primarySource = this.toSourceTuple(this.objectsById.get(this.dragState.targetId)?.root.position ?? this.dragState.worldStart); @@ -1299,6 +1323,7 @@ export class UniversalPreviewer { this.updateManualObjectMatrix(bound.root); } this.updateDraggedBodyOffsets(deltaSource); + this.refreshDragHighlight(); this.markRenderDirty(); const source: [number, number, number] = this.dragState.kind === "robot" @@ -1385,6 +1410,17 @@ export class UniversalPreviewer { this.renderer.domElement.style.cursor = ""; } + private refreshDragHighlight(): void { + if (!this.dragState) { + return; + } + const bound = this.objectsById.get(this.dragState.targetId); + if (!bound) { + return; + } + this.showFocusAndOutlineForObject(this.dragState.targetId, bound.root, "#f97316", 1.2); + } + private pickEditableObject(event: PointerLikeEvent): { id: string; bound: BoundObject } | undefined { if (this.editableObjectIds.size === 0 && this.robotObjectIds.size === 0) { return undefined; diff --git a/src/types.ts b/src/types.ts index 02fafba..7fd844d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -228,6 +228,7 @@ export interface RendererStats { fastPickProxyBoundsCount: number; meshSourceBoundsCount: number; hoverProxyBoundsHitCount: number; + hoverCachedBoundsHitCount: number; hoverRootBoundsFallbackCount: number; cameraFitTraversalCount: number; sceneLoadSuppressedRenderCount: number;