Cache scene bounds for camera fitting

This commit is contained in:
eust-w 2026-06-25 02:24:55 +08:00
parent 9df13a59ef
commit fad1f66a50
5 changed files with 76 additions and 36 deletions

View File

@ -323,21 +323,24 @@ Scene evidence:
- Frontend mesh source cache: parsed GLTF/OBJ/STL sources are retained across
scene rebuilds, while scene-owned clones still release their non-retained
resources. The release gate now checks mesh cache hit/miss diagnostics.
- Camera-fit bounds cache: object world bounds are captured while creating pick
proxies, so camera fitting no longer performs a second deep `Box3.setFromObject`
traversal across the full scene.
Bundle evidence:
- JavaScript chunks: `4`
- App entry chunk: `179.42 kB raw / 46.87 KiB gzip`
- App entry chunk: `180.28 kB raw / 47.07 KiB gzip`
- Largest JavaScript gzip chunk: `141.04 KiB / 170.00 KiB`
- Total JavaScript gzip: `210.67 KiB / 230.00 KiB`
- Total JavaScript gzip: `210.87 KiB / 230.00 KiB`
- Large-scene performance mode: `large-scene`, target `45 fps`,
actual idle render rate `0.8 fps`, DPR cap observed at `1.00` in release
gate, shadows `off`, hover picking throttled to `32ms`, `335` empty frames
actual idle render rate `0.7 fps`, DPR cap observed at `1.00` in release
gate, shadows `off`, hover picking throttled to `32ms`, `362` empty frames
skipped, `1839` static visual nodes frozen, `7` primitive geometry shapes
cached, `51` mesh source assets retained, `576/51` mesh cache hit/miss in
the primary large scene, `636` lightweight pick proxies used for
non-recursive raycasts, and a stable paused backend state cache reported
`state apply 0/skip 635`.
the primary large scene, `636` cached object bounds with `0` camera-fit
traversals, `636` lightweight pick proxies used for non-recursive raycasts,
and a stable paused backend state cache reported `state apply 0/skip 635`.
- Mesh cache lifecycle evidence: the Scene Builder compose switch reported
cumulative `1203/51` mesh cache hit/miss with `51` retained geometries and
`17` retained materials, avoiding repeated parser/network work for duplicate
@ -345,37 +348,39 @@ Bundle evidence:
- Body-state cache gate: `browser-body-state-cache` waits for the paused scene
to skip at least the full body-binding count; the current run passed with
`state apply 0/skip 635`.
- Camera-fit traversal gate: `browser-large-scene-performance-mode` now checks
`bounds cache 636` and `camera traversals 0`.
- Resource lifecycle on a real Scene Builder compose switch: released `1`
geometry, `10` materials, and `2` textures from non-retained scene graph
resources while preserving cached mesh sources.
Control evidence:
- Cold compose endpoint: `275ms`, `4/4` composed object bodies synced.
- Repeated compose cache: `62ms`, `cached=true`, same compose key.
- Scene Builder UI compose: representative run `3322ms` end to end with real
- Cold compose endpoint: `265ms`, `4/4` composed object bodies synced.
- Repeated compose cache: `75ms`, `cached=true`, same compose key.
- Scene Builder UI compose: representative run `3534ms` end to end with real
backend diagnostics.
- Share-link restore: representative full browser restore `7673ms`.
- Config import restore: representative full browser restore `8109ms`.
- Composed object body edit: `47ms`.
- Fixed inventory edit rejection: `58ms`.
- Share-link restore: representative full browser restore `7641ms`.
- Config import restore: representative full browser restore `8263ms`.
- Composed object body edit: `62ms`.
- Fixed inventory edit rejection: `54ms`.
- Dynamic composed object drop: `0.48012m`.
- Static shelf anchor stability: `max drift 0.00000m` after a 2500 ms resume
window.
- Resume backend state: `249ms paused=false`
- Resume backend state: `62ms paused=false`
- Resume visible UI state: `64ms`
- Pause backend state: `259ms paused=true`
- Pause visible UI state: `53ms`
- Desktop acceptance wait: `5ms` total, product telemetry `0ms`
- Mobile acceptance wait: `337ms` total, product telemetry `2ms`
- Pause backend state: `58ms paused=true`
- Pause visible UI state: `56ms`
- Desktop acceptance wait: `2ms` total, product telemetry `0ms`
- Mobile acceptance wait: `343ms` total, product telemetry `2ms`
- Runtime diagnostics panel:
`state=http://127.0.0.1:18090/state`,
`control=http://127.0.0.1:18090/control`,
`latency=响应 52 ms / 确认 53 ms / 可见 53 ms`,
`latency=响应 55 ms / 确认 55 ms / 可见 56 ms`,
`acceptanceLatency=0 ms / 11-0-0`,
`binding=635/636; 4/568`,
`builderPhysics=已添加物体 4: 后端动态 4 / 仅视觉 0`,
`renderPerformance=large-scene; target 45 fps; actual 0.8 fps; dpr 1.00; shadows off; skipped 335; hover 32ms; frozen 1839; primitive geometries 7; mesh cache 51 sources 576/51 hit/miss retained 51g/17m; pick proxies 636; state apply 0/skip 635; released 0g/0m/0t`.
`renderPerformance=large-scene; target 45 fps; actual 0.7 fps; dpr 1.00; shadows off; skipped 362; hover 32ms; frozen 1839; primitive geometries 7; mesh cache 51 sources 576/51 hit/miss retained 51g/17m; bounds cache 636 camera traversals 0; pick proxies 636; state apply 0/skip 635; released 0g/0m/0t`.
- Evidence screenshot artifacts:
`builder page; share page; import page; visual-only boundary canvas; desktop canvas; mobile canvas`
- Evidence export artifacts:

View File

@ -640,6 +640,8 @@ async function readBrowserStats(page) {
meshCacheMissCount: numberData("meshCacheMissCount"),
retainedMeshGeometryCount: numberData("retainedMeshGeometryCount"),
retainedMeshMaterialCount: numberData("retainedMeshMaterialCount"),
sceneObjectBoundsCount: numberData("sceneObjectBoundsCount"),
cameraFitTraversalCount: numberData("cameraFitTraversalCount"),
},
diagnostics: {
manifestUrl: text("#diag-manifest-url"),
@ -1539,9 +1541,11 @@ async function verifyBrowser() {
&& (renderPerformance.meshCacheSourceCount ?? 0) > 0
&& (renderPerformance.meshCacheHitCount ?? 0) > 0
&& (renderPerformance.retainedMeshGeometryCount ?? 0) > 0
&& (renderPerformance.sceneObjectBoundsCount ?? 0) >= objectCount
&& (renderPerformance.cameraFitTraversalCount ?? Number.POSITIVE_INFINITY) === 0
&& (renderPerformance.pickProxyCount ?? 0) >= objectCount
&& desktopStats.diagnostics.renderPerformance.includes("large-scene"),
`mode=${renderPerformance.largeSceneMode} fps=${renderPerformance.targetRenderFps ?? "-"} dpr=${renderPerformance.renderPixelRatio ?? "-"} shadows=${renderPerformance.shadowsEnabled} hover=${renderPerformance.hoverPickIntervalMs ?? "-"} skipped=${renderPerformance.renderSkipCount ?? "-"} frozen=${renderPerformance.frozenStaticObjectCount ?? "-"} primitiveGeometries=${renderPerformance.primitiveGeometryCount ?? "-"} meshCache=${renderPerformance.meshCacheSourceCount ?? "-"} hit=${renderPerformance.meshCacheHitCount ?? "-"} miss=${renderPerformance.meshCacheMissCount ?? "-"} retained=${renderPerformance.retainedMeshGeometryCount ?? "-"}g/${renderPerformance.retainedMeshMaterialCount ?? "-"}m pickProxies=${renderPerformance.pickProxyCount ?? "-"} stateApply=${renderPerformance.appliedBodyStateCount ?? "-"}/skip=${renderPerformance.skippedBodyStateCount ?? "-"} released=${renderPerformance.releasedGeometryCount ?? "-"}g/${renderPerformance.releasedMaterialCount ?? "-"}m/${renderPerformance.releasedTextureCount ?? "-"}t; ${desktopStats.diagnostics.renderPerformance}`,
`mode=${renderPerformance.largeSceneMode} fps=${renderPerformance.targetRenderFps ?? "-"} dpr=${renderPerformance.renderPixelRatio ?? "-"} shadows=${renderPerformance.shadowsEnabled} hover=${renderPerformance.hoverPickIntervalMs ?? "-"} skipped=${renderPerformance.renderSkipCount ?? "-"} frozen=${renderPerformance.frozenStaticObjectCount ?? "-"} primitiveGeometries=${renderPerformance.primitiveGeometryCount ?? "-"} meshCache=${renderPerformance.meshCacheSourceCount ?? "-"} hit=${renderPerformance.meshCacheHitCount ?? "-"} miss=${renderPerformance.meshCacheMissCount ?? "-"} retained=${renderPerformance.retainedMeshGeometryCount ?? "-"}g/${renderPerformance.retainedMeshMaterialCount ?? "-"}m bounds=${renderPerformance.sceneObjectBoundsCount ?? "-"} cameraTraversals=${renderPerformance.cameraFitTraversalCount ?? "-"} pickProxies=${renderPerformance.pickProxyCount ?? "-"} stateApply=${renderPerformance.appliedBodyStateCount ?? "-"}/skip=${renderPerformance.skippedBodyStateCount ?? "-"} released=${renderPerformance.releasedGeometryCount ?? "-"}g/${renderPerformance.releasedMaterialCount ?? "-"}m/${renderPerformance.releasedTextureCount ?? "-"}t; ${desktopStats.diagnostics.renderPerformance}`,
);
const builderRelease = builderCompose.renderPerformance ?? {};
check(

View File

@ -180,6 +180,8 @@ interface RuntimeDiagnostics {
meshCacheMissCount: number;
retainedMeshGeometryCount: number;
retainedMeshMaterialCount: number;
sceneObjectBoundsCount: number;
cameraFitTraversalCount: number;
editableGroups: number;
groupedObjects: number;
meshes: number;
@ -2434,6 +2436,8 @@ function currentRuntimeDiagnostics(): RuntimeDiagnostics {
meshCacheMissCount: stats.meshCacheMissCount,
retainedMeshGeometryCount: stats.retainedMeshGeometryCount,
retainedMeshMaterialCount: stats.retainedMeshMaterialCount,
sceneObjectBoundsCount: stats.sceneObjectBoundsCount,
cameraFitTraversalCount: stats.cameraFitTraversalCount,
editableGroups: stats.editableGroupCount,
groupedObjects: stats.groupedObjectCount,
meshes: stats.loadedMeshCount,
@ -2492,7 +2496,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)}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; frozen ${stats.frozenStaticObjectCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; pick proxies ${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; 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)}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; frozen ${stats.frozenStaticObjectCount}; 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} camera traversals ${stats.cameraFitTraversalCount}; pick proxies ${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`;
}
function renderDiagnosticsPanel(): void {
@ -2528,6 +2532,8 @@ function renderDiagnosticsPanel(): void {
data.meshCacheMissCount = String(diagnostics.stats.meshCacheMissCount);
data.retainedMeshGeometryCount = String(diagnostics.stats.retainedMeshGeometryCount);
data.retainedMeshMaterialCount = String(diagnostics.stats.retainedMeshMaterialCount);
data.sceneObjectBoundsCount = String(diagnostics.stats.sceneObjectBoundsCount);
data.cameraFitTraversalCount = String(diagnostics.stats.cameraFitTraversalCount);
setText("#diag-manifest-url", diagnostics.manifestUrl);
setText("#diag-state-url", endpointLabel(diagnostics.stateUrl));
setText("#diag-control-url", endpointLabel(diagnostics.controlUrl));

View File

@ -102,6 +102,7 @@ export class UniversalPreviewer {
private readonly bodyBindings = new Map<string, BoundObject[]>();
private readonly objectsById = new Map<string, BoundObject>();
private readonly pickProxyById = new Map<string, THREE.Object3D>();
private readonly sceneObjectBounds = new THREE.Box3();
private readonly objectBodyIds = new Map<string, string[]>();
private readonly editableGroups = new Map<string, string[]>();
private readonly pickRoots: THREE.Object3D[] = [];
@ -144,6 +145,8 @@ export class UniversalPreviewer {
private fallbackCount = 0;
private meshCacheHitCount = 0;
private meshCacheMissCount = 0;
private sceneObjectBoundsCount = 0;
private cameraFitTraversalCount = 0;
private lastPixelSampleAt = 0;
private lastPixelSamples = 0;
private renderSuspendedUntil = 0;
@ -378,6 +381,8 @@ export class UniversalPreviewer {
meshCacheMissCount: this.meshCacheMissCount,
retainedMeshGeometryCount: this.retainedMeshGeometries.size,
retainedMeshMaterialCount: this.retainedMeshMaterials.size,
sceneObjectBoundsCount: this.sceneObjectBoundsCount,
cameraFitTraversalCount: this.cameraFitTraversalCount,
editableGroupCount: this.editableGroups.size,
groupedObjectCount: Array.from(this.editableGroups.values()).reduce((sum, ids) => sum + ids.length, 0),
frameCount: this.frameCount,
@ -875,6 +880,9 @@ export class UniversalPreviewer {
this.lastRenderDurationMs = 0;
this.measuredRenderFps = 0;
this.frozenStaticObjectCount = 0;
this.sceneObjectBounds.makeEmpty();
this.sceneObjectBoundsCount = 0;
this.cameraFitTraversalCount = 0;
this.renderWindowFrameCount = 0;
this.renderWindowStartedAt = performance.now();
this.markRenderDirty();
@ -1547,18 +1555,25 @@ export class UniversalPreviewer {
}
private fitCameraToObjects(): void {
const bounds = new THREE.Box3();
const bounds = this.sceneObjectBoundsCount > 0
? this.sceneObjectBounds.clone()
: new THREE.Box3();
let found = false;
for (const child of this.root.children) {
if (!child.userData.manifestId) {
continue;
if (this.sceneObjectBoundsCount > 0) {
found = !bounds.isEmpty();
} else {
for (const child of this.root.children) {
if (!child.userData.manifestId) {
continue;
}
const childBounds = new THREE.Box3().setFromObject(child);
this.cameraFitTraversalCount += 1;
if (childBounds.isEmpty()) {
continue;
}
bounds.union(childBounds);
found = true;
}
const childBounds = new THREE.Box3().setFromObject(child);
if (childBounds.isEmpty()) {
continue;
}
bounds.union(childBounds);
found = true;
}
if (!found || bounds.isEmpty()) {
return;
@ -1604,7 +1619,12 @@ export class UniversalPreviewer {
root.add(visual);
}
this.freezeStaticVisualSubtree(root);
const pickProxy = this.createPickProxy(root, object.id);
const pickTarget = this.createPickProxy(root, object.id);
const pickProxy = pickTarget?.proxy;
if (pickTarget) {
this.sceneObjectBounds.union(pickTarget.worldBounds);
this.sceneObjectBoundsCount += 1;
}
if (pickProxy) {
root.add(pickProxy);
this.pickProxyById.set(object.id, pickProxy);
@ -1702,7 +1722,7 @@ export class UniversalPreviewer {
});
}
private createPickProxy(root: THREE.Object3D, manifestId: string): THREE.Object3D | undefined {
private createPickProxy(root: THREE.Object3D, manifestId: string): { proxy: THREE.Object3D; worldBounds: THREE.Box3 } | undefined {
root.updateWorldMatrix(true, true);
const worldBounds = new THREE.Box3().setFromObject(root);
if (worldBounds.isEmpty()) {
@ -1737,7 +1757,10 @@ export class UniversalPreviewer {
);
proxy.updateMatrix();
proxy.matrixAutoUpdate = false;
return proxy;
return {
proxy,
worldBounds: worldBounds.clone(),
};
}
private async createVisual(geometry: PreviewGeometry, materialId: string | undefined, token: number): Promise<THREE.Object3D> {

View File

@ -210,6 +210,8 @@ export interface RendererStats {
meshCacheMissCount: number;
retainedMeshGeometryCount: number;
retainedMeshMaterialCount: number;
sceneObjectBoundsCount: number;
cameraFitTraversalCount: number;
editableGroupCount: number;
groupedObjectCount: number;
frameCount: number;