Use cached bounds for large scene picking

This commit is contained in:
eust-w 2026-06-25 05:03:23 +08:00
parent f22d457bdd
commit 878f4e3cc2
4 changed files with 150 additions and 150 deletions

View File

@ -40,7 +40,7 @@ Important properties:
- Large-scene visual polling is intentionally slower than the control plane so
product controls stay responsive.
- Large-scene rendering freezes static visual subtrees, reuses primitive
geometries, caps DPR, disables shadows, uses lightweight pick proxies for
geometries, caps DPR, disables shadows, uses cached pick targets for
hover/drag raycasts, skips unchanged backend body states before transform
application with numeric pose caches instead of per-body string signatures,
switches high-frequency state polling to active robot/dynamic bodies after
@ -340,22 +340,26 @@ 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.
- Camera-fit bounds cache: object world bounds are captured while creating
cached pick targets, so camera fitting no longer performs a second deep
`Box3.setFromObject` traversal across the full scene.
- Mesh pick bounds cache: source mesh bounds are computed once per retained mesh
source, and repeated mesh clones create pick proxies from cached bounds instead
of re-traversing the whole cloned subtree.
source, and repeated mesh targets are created from cached bounds instead of
re-traversing the whole cloned subtree.
- 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.
- Fixed repeated mesh instancing: in large scenes, repeated fixed mesh assets
render through shared `InstancedMesh` batches while each manifest object keeps
its own root, body binding, pick proxy, editable group, and backend sync path.
its own root, body binding, pick target, editable group, and backend sync path.
This reduces WebGL draw work for shelf/product inventory without turning the
objects into frontend-only decorations.
- Bounds-only picking: hover and drag picking now ray-test cached `Box3` targets
directly instead of adding one invisible Three.js mesh per object. Fixed
instanced objects keep detached data roots for backend sync, while only the
shared instanced batches enter the render scene.
- Cached hover/drag bounds: hover focus, hover outline, and drag highlight use
the existing pick proxy bounds through a `Box3Helper` when present, so
the existing pick target 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.
@ -395,9 +399,9 @@ Scene evidence:
Bundle evidence:
- JavaScript chunks: `4`
- App entry chunk: `196.36 kB raw / 50.93 kB gzip`
- App entry chunk: `195.96 kB raw / 50.85 kB gzip`
- Largest JavaScript gzip chunk: `140.96 KiB / 170.00 KiB`
- Total JavaScript gzip: `214.56 KiB / 230.00 KiB`
- Total JavaScript gzip: `214.48 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
@ -409,14 +413,14 @@ Bundle evidence:
`51` mesh source assets retained, `24/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` with cached bounds
and `fallback 0`, with `0` camera-fit traversals, `1/652`
scene root batch insertion, `342`
and `fallback 0`, with `0` camera-fit traversals, `1/84`
scene root batch insertion under the computed `84` root cap, `344`
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
`4` incremental body-map merges,
UI physics-profile cache `31/3` hit/miss, UI builder-profile cache `34/1`
hit/miss, `636` lightweight pick proxies used for non-recursive raycasts,
hit/miss, `636` cached pick targets used for bounds-only ray tests,
and a stable paused backend state cache reported `manual roots 636 updates
67` with `state apply 0/skip 67`.
- Mesh cache lifecycle evidence: the Scene Builder compose switch reported
@ -451,14 +455,14 @@ Bundle evidence:
`instanced=568/16/16`, `matrices=568`, and that instanced draw units are fewer
than instanced objects, proving repeated fixed inventory renders through
shared GPU batches instead of one cloned render tree per item.
- Hover proxy bounds gate: `browser-hover-proxy-bounds` moves the mouse over the
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.
- Hover bounds gate: `browser-hover-proxy-bounds` moves the mouse over the real
canvas and now checks both `hoverProxy` and cached hover bounds increase while
`fallback=0`, proving hover highlight uses cached bounds instead of traversing
the visual root.
- Scene-load batching gate: `browser-large-scene-performance-mode` now checks
`scene batches 1/652` and `yields 26`, proving the large scene is attached as
a single batch while object construction still yields to the browser event
loop during load.
`scene batches 1/84`, `maxSceneRoots=84`, and `yields 26`, proving fixed
instanced object roots stay detached from the Three scene while object
construction still yields to the browser event loop during load.
- 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.
@ -470,19 +474,19 @@ Control evidence:
- Active state endpoint: full `/state` returned `602` bodies; active
`/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 `4400ms` end to end with real
- Scene Builder UI compose: representative run `4440ms` end to end with real
backend diagnostics.
- Share-link restore: representative full browser restore `6379ms`.
- Config import restore: representative full browser restore `6706ms`.
- Share-link restore: representative full browser restore `6437ms`.
- Config import restore: representative full browser restore `6914ms`.
- Composed object body edit: `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: `57ms paused=false`
- Resume visible UI state: `58ms`
- Pause backend state: `33ms paused=true`
- Pause visible UI state: `35ms`
- Resume backend state: `55ms paused=false`
- Resume visible UI state: `57ms`
- Pause backend state: `50ms paused=true`
- Pause visible UI state: `41ms`
- Desktop acceptance wait: `1243ms` total, product telemetry `0ms`
- Mobile acceptance wait: `277ms` total, product telemetry `0ms`
- Runtime diagnostics panel:
@ -492,7 +496,7 @@ Control evidence:
`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 703 prefrozen 59 spinSkip 627; instanced 568/16/16 matrices 568; manual roots 636 updates 67; primitive geometries 7; mesh cache 51 sources 24/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/652 suppressed 342 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 26; hover 32ms; frozen 703 prefrozen 59 spinSkip 627; instanced 568/16/16 matrices 568; manual roots 636 updates 67; primitive geometries 7; mesh cache 51 sources 24/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/84 suppressed 344 yields 26; state payload 34/602 reduced 3 incremental 4; ui cache physics 31/3 builder 34/1; pick targets 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:

View File

@ -1626,6 +1626,12 @@ async function verifyBrowser() {
builderPhysicsOk,
`builderDynamic=${builderPhysics.backendDynamic ?? "-"} builderVisual=${builderPhysics.visualOnly ?? "-"} total=${builderPhysics.total ?? "-"} ready=${builderPhysics.ready}; ${desktopStats.diagnostics.builderPhysics}`,
);
const maxInstancedSceneRoots = Math.max(
1,
objectCount
- (renderPerformance.instancedMeshObjectCount ?? 0)
+ (renderPerformance.instancedMeshBatchCount ?? 0),
);
check(
"browser-large-scene-performance-mode",
renderPerformance.largeSceneMode === true
@ -1659,14 +1665,14 @@ async function verifyBrowser() {
&& (renderPerformance.incrementalBodyMergeCount ?? 0) > 0
&& (renderPerformance.statePayloadBodyCount ?? Number.POSITIVE_INFINITY) < (renderPerformance.mergedBodyStateCount ?? 0)
&& (renderPerformance.mergedBodyStateCount ?? 0) >= thresholds.minBodyBindings
&& (renderPerformance.batchedSceneRootCount ?? 0) >= objectCount
&& (renderPerformance.batchedSceneRootCount ?? Number.POSITIVE_INFINITY) <= maxInstancedSceneRoots
&& (renderPerformance.sceneRootBatchCount ?? 0) === 1
&& (renderPerformance.objectPhysicsProfileCacheHitCount ?? 0) > 0
&& (renderPerformance.builderPhysicsProfileCacheHitCount ?? 0) > 0
&& (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 ?? "-"} instanced=${renderPerformance.instancedMeshObjectCount ?? "-"}/${renderPerformance.instancedMeshBatchCount ?? "-"}/${renderPerformance.instancedMeshDrawCount ?? "-"} matrices=${renderPerformance.instancedMeshMatrixUpdateCount ?? "-"} 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}`,
`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 ?? "-"} instanced=${renderPerformance.instancedMeshObjectCount ?? "-"}/${renderPerformance.instancedMeshBatchCount ?? "-"}/${renderPerformance.instancedMeshDrawCount ?? "-"} matrices=${renderPerformance.instancedMeshMatrixUpdateCount ?? "-"} 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 ?? "-"} maxSceneRoots=${maxInstancedSceneRoots} 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 ?? "-"} pickTargets=${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",

View File

@ -2601,7 +2601,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}; instanced ${stats.instancedMeshObjectCount}/${stats.instancedMeshBatchCount}/${stats.instancedMeshDrawCount} matrices ${stats.instancedMeshMatrixUpdateCount}; 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`;
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}; instanced ${stats.instancedMeshObjectCount}/${stats.instancedMeshBatchCount}/${stats.instancedMeshDrawCount} matrices ${stats.instancedMeshMatrixUpdateCount}; 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 targets ${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 {

View File

@ -50,6 +50,12 @@ interface InstancedVisualBinding {
local: THREE.Object3D;
}
interface PickBoundsTarget {
id: string;
localBounds: THREE.Box3;
worldBounds: THREE.Box3;
}
export interface DragTargetPose {
id: string;
source: [number, number, number];
@ -115,8 +121,6 @@ export class UniversalPreviewer {
private readonly environmentTarget: THREE.WebGLRenderTarget;
private readonly materials = new Map<string, THREE.Material>();
private readonly primitiveGeometryCache = new Map<string, THREE.BufferGeometry>();
private readonly pickProxyGeometry = new THREE.BoxGeometry(1, 1, 1);
private readonly pickProxyMaterial = new THREE.MeshBasicMaterial({ color: "#ffffff", opacity: 0, transparent: true });
private readonly gltfLoader = new GLTFLoader();
private readonly objLoader = new OBJLoader();
private readonly stlLoader = new STLLoader();
@ -130,17 +134,18 @@ export class UniversalPreviewer {
private readonly instancedVisualBindings = new Map<string, InstancedVisualBinding>();
private readonly bodyBindings = new Map<string, BoundObject[]>();
private readonly objectsById = new Map<string, BoundObject>();
private readonly pickProxyById = new Map<string, THREE.Object3D>();
private readonly pickBoundsById = new Map<string, PickBoundsTarget>();
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[] = [];
private readonly editablePickRoots: THREE.Object3D[] = [];
private readonly pickTargets: PickBoundsTarget[] = [];
private readonly editablePickTargets: PickBoundsTarget[] = [];
private readonly spinningObjects: THREE.Object3D[] = [];
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 scratchPickPoint = new THREE.Vector3();
private readonly scratchPreviousFocusPosition = new THREE.Vector3();
private readonly scratchPreviousFocusQuaternion = new THREE.Quaternion();
private readonly scratchMatrix = new THREE.Matrix4();
@ -249,7 +254,6 @@ 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);
@ -362,6 +366,7 @@ export class UniversalPreviewer {
for (const item of bound) {
const itemChanged = this.applyBodyState(item.root, bodyName, bodyState);
if (itemChanged) {
this.updatePickTargetBounds(item.manifest.id);
this.updateInstancedVisualMatrix(item.manifest.id);
}
changed = itemChanged || changed;
@ -447,7 +452,7 @@ export class UniversalPreviewer {
}
}
}
this.refreshEditablePickRoots();
this.refreshEditablePickTargets();
}
getStats(): RendererStats {
@ -475,7 +480,7 @@ export class UniversalPreviewer {
manualMatrixRootCount: this.manualMatrixRootCount,
manualMatrixUpdateCount: this.manualMatrixUpdateCount,
primitiveGeometryCount: this.primitiveGeometryCache.size,
pickProxyCount: this.pickProxyById.size,
pickProxyCount: this.pickBoundsById.size,
appliedBodyStateCount: this.appliedBodyStateCount,
skippedBodyStateCount: this.skippedBodyStateCount,
unchangedFrameCount: this.unchangedFrameCount,
@ -581,8 +586,6 @@ export class UniversalPreviewer {
geometry.dispose();
}
this.primitiveGeometryCache.clear();
this.pickProxyGeometry.dispose();
this.pickProxyMaterial.dispose();
this.renderer.dispose();
this.controls.dispose();
}
@ -915,15 +918,10 @@ export class UniversalPreviewer {
}
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;
}
const target = this.pickBoundsById.get(id);
if (target && !target.worldBounds.isEmpty()) {
this.hoverCachedBoundsHitCount += 1;
return this.scratchBounds.copy(target.worldBounds);
}
this.hoverRootBoundsFallbackCount += 1;
return this.scratchBounds.setFromObject(root);
@ -944,7 +942,7 @@ export class UniversalPreviewer {
}
if (this.hoveredObjectId !== pick.id) {
this.hoveredObjectId = pick.id;
if (this.pickProxyById.has(pick.id)) {
if (this.pickBoundsById.has(pick.id)) {
this.hoverProxyBoundsHitCount += 1;
}
const color = pick.editable ? "#38bdf8" : "#f59e0b";
@ -1016,11 +1014,11 @@ export class UniversalPreviewer {
this.disposeMaterials();
this.bodyBindings.clear();
this.objectsById.clear();
this.pickProxyById.clear();
this.pickBoundsById.clear();
this.objectBodyIds.clear();
this.editableGroups.clear();
this.pickRoots.length = 0;
this.editablePickRoots.length = 0;
this.pickTargets.length = 0;
this.editablePickTargets.length = 0;
this.spinningObjects.length = 0;
this.bodySourceOffsets.clear();
this.bodyVisualOffsets.clear();
@ -1320,6 +1318,7 @@ export class UniversalPreviewer {
}
bound.root.position.copy(start).add(delta);
this.updateManualObjectMatrix(bound.root);
this.updatePickTargetBounds(targetId);
this.updateInstancedVisualMatrix(targetId);
}
this.updateDraggedBodyOffsets(deltaSource);
@ -1369,6 +1368,7 @@ export class UniversalPreviewer {
}
bound.root.position.copy(start).add(delta);
this.updateManualObjectMatrix(bound.root);
this.updatePickTargetBounds(targetId);
this.updateInstancedVisualMatrix(targetId);
}
this.updateDraggedBodyOffsets(deltaSource);
@ -1474,24 +1474,12 @@ export class UniversalPreviewer {
if (this.editableObjectIds.size === 0 && this.robotObjectIds.size === 0) {
return undefined;
}
this.updatePointerFromEvent(event);
this.raycaster.setFromCamera(this.pointer, this.camera);
const intersects = this.raycaster.intersectObjects(this.editablePickRoots, false);
for (const hit of intersects) {
const manifestId = this.extractManifestId(hit.object);
if (!manifestId) {
continue;
}
if (!this.editableObjectIds.has(manifestId) && !this.robotObjectIds.has(manifestId)) {
continue;
}
const bound = this.objectsById.get(manifestId);
if (!bound) {
continue;
}
return { id: manifestId, bound };
const target = this.pickBoundsTarget(event, this.editablePickTargets);
if (!target) {
return undefined;
}
return undefined;
const bound = this.objectsById.get(target.id);
return bound ? { id: target.id, bound } : undefined;
}
private pickAnyObject(
@ -1500,36 +1488,56 @@ export class UniversalPreviewer {
if (this.objectsById.size === 0) {
return undefined;
}
const target = this.pickBoundsTarget(event, this.pickTargets);
if (!target) {
return undefined;
}
const bound = this.objectsById.get(target.id);
if (!bound) {
return undefined;
}
const isEditable = this.editableObjectIds.has(target.id) || this.robotObjectIds.has(target.id);
const disabledReason = isEditable && this.dragOptions.enabled === false
? "需暂停后拖拽"
: bound.manifest.physics === "fixed"
? "固定物理体"
: bound.manifest.physics === "stateOnly"
? "状态同步体"
: !isEditable && !bound.manifest.body
? "静态对象"
: undefined;
return {
id: target.id,
bound,
editable: isEditable && this.dragOptions.enabled !== false,
disabledReason,
};
}
private pickBoundsTarget(event: PointerLikeEvent, targets: PickBoundsTarget[]): PickBoundsTarget | undefined {
if (targets.length === 0) {
return undefined;
}
this.updatePointerFromEvent(event);
this.raycaster.setFromCamera(this.pointer, this.camera);
const intersects = this.raycaster.intersectObjects(this.pickRoots, false);
for (const hit of intersects) {
const manifestId = this.extractManifestId(hit.object);
if (!manifestId) {
const origin = this.raycaster.ray.origin;
let closest: PickBoundsTarget | undefined;
let closestDistanceSq = Number.POSITIVE_INFINITY;
for (const target of targets) {
if (target.worldBounds.isEmpty()) {
continue;
}
const bound = this.objectsById.get(manifestId);
if (!bound) {
const point = this.raycaster.ray.intersectBox(target.worldBounds, this.scratchPickPoint);
if (!point) {
continue;
}
const isEditable = this.editableObjectIds.has(manifestId) || this.robotObjectIds.has(manifestId);
const disabledReason = isEditable && this.dragOptions.enabled === false
? "需暂停后拖拽"
: bound.manifest.physics === "fixed"
? "固定物理体"
: bound.manifest.physics === "stateOnly"
? "状态同步体"
: !isEditable && !bound.manifest.body
? "静态对象"
: undefined;
return {
id: manifestId,
bound,
editable: isEditable && this.dragOptions.enabled !== false,
disabledReason,
};
const distanceSq = origin.distanceToSquared(point);
if (distanceSq < closestDistanceSq) {
closestDistanceSq = distanceSq;
closest = target;
}
}
return undefined;
return closest;
}
private currentSourceQuat(bodyName?: string): Quat | undefined {
@ -1559,18 +1567,6 @@ export class UniversalPreviewer {
this.bodyStateCache.delete(body);
}
}
private extractManifestId(object: THREE.Object3D): string | undefined {
let current: THREE.Object3D | null = object;
while (current) {
const id = current.userData?.manifestId;
if (typeof id === "string" && id.length > 0) {
return id;
}
current = current.parent;
}
return undefined;
}
private pointerWorld(event: PointerLikeEvent): THREE.Vector3 | undefined {
this.updatePointerFromEvent(event);
this.raycaster.setFromCamera(this.pointer, this.camera);
@ -1973,19 +1969,17 @@ export class UniversalPreviewer {
root.add(visual);
}
this.freezeStaticVisualSubtree(root, visual);
const pickTarget = this.createPickProxy(root, object.id, visual);
const pickProxy = pickTarget?.proxy;
const pickTarget = this.createPickTarget(root, object.id, visual);
if (pickTarget) {
this.sceneObjectBounds.union(pickTarget.worldBounds);
this.sceneObjectBoundsCount += 1;
}
if (pickProxy) {
root.add(pickProxy);
this.pickProxyById.set(object.id, pickProxy);
this.pickBoundsById.set(object.id, pickTarget);
this.pickTargets.push(pickTarget);
}
this.freezeObjectRootMatrix(root);
sceneRoots.push(root);
this.pickRoots.push(pickProxy ?? root);
if (!instancedVisual) {
sceneRoots.push(root);
}
this.registerSpinningObjects(root, visual);
if (instancedVisual) {
this.updateInstancedVisualMatrix(object.id);
@ -2049,7 +2043,7 @@ export class UniversalPreviewer {
this.batchedSceneRootCount += sceneRoots.length;
this.sceneRootBatchCount += 1;
}
this.refreshEditablePickRoots();
this.refreshEditablePickTargets();
}
private async yieldDuringSceneBuild(token: number): Promise<void> {
@ -2060,22 +2054,22 @@ export class UniversalPreviewer {
}
}
private refreshEditablePickRoots(): void {
this.editablePickRoots.length = 0;
const roots = new Set<THREE.Object3D>();
private refreshEditablePickTargets(): void {
this.editablePickTargets.length = 0;
const targets = new Map<string, PickBoundsTarget>();
for (const id of this.editableObjectIds) {
const root = this.pickProxyById.get(id) ?? this.objectsById.get(id)?.root;
if (root) {
roots.add(root);
const target = this.pickBoundsById.get(id);
if (target) {
targets.set(id, target);
}
}
for (const id of this.robotObjectIds) {
const root = this.pickProxyById.get(id) ?? this.objectsById.get(id)?.root;
if (root) {
roots.add(root);
const target = this.pickBoundsById.get(id);
if (target) {
targets.set(id, target);
}
}
this.editablePickRoots.push(...roots);
this.editablePickTargets.push(...targets.values());
}
private registerSpinningObjects(root: THREE.Object3D, visual?: THREE.Object3D): void {
@ -2141,12 +2135,12 @@ export class UniversalPreviewer {
}
}
private createPickProxy(
private createPickTarget(
root: THREE.Object3D,
manifestId: string,
visual?: THREE.Object3D,
): { proxy: THREE.Object3D; worldBounds: THREE.Box3 } | undefined {
const cached = visual ? this.createPickProxyFromCachedBounds(root, visual, manifestId) : undefined;
): PickBoundsTarget | undefined {
const cached = visual ? this.createPickTargetFromCachedBounds(root, visual, manifestId) : undefined;
if (cached) {
this.fastPickProxyBoundsCount += 1;
return cached;
@ -2170,14 +2164,14 @@ export class UniversalPreviewer {
if (localBounds.isEmpty()) {
return undefined;
}
return this.createPickProxyFromBounds(localBounds, worldBounds, manifestId);
return this.createPickTargetFromBounds(localBounds, worldBounds, manifestId);
}
private createPickProxyFromCachedBounds(
private createPickTargetFromCachedBounds(
root: THREE.Object3D,
visual: THREE.Object3D,
manifestId: string,
): { proxy: THREE.Object3D; worldBounds: THREE.Box3 } | undefined {
): PickBoundsTarget | undefined {
const sourceBounds = visual.userData.sourceLocalBounds as THREE.Box3 | undefined;
if (!sourceBounds || sourceBounds.isEmpty()) {
return undefined;
@ -2192,35 +2186,31 @@ export class UniversalPreviewer {
if (localBounds.isEmpty()) {
return undefined;
}
return this.createPickProxyFromBounds(localBounds, worldBounds, manifestId);
return this.createPickTargetFromBounds(localBounds, worldBounds, manifestId);
}
private createPickProxyFromBounds(
private createPickTargetFromBounds(
localBounds: THREE.Box3,
worldBounds: THREE.Box3,
manifestId: string,
): { proxy: THREE.Object3D; worldBounds: THREE.Box3 } {
const center = localBounds.getCenter(new THREE.Vector3());
const size = localBounds.getSize(new THREE.Vector3());
const proxy = new THREE.Mesh(this.pickProxyGeometry, this.pickProxyMaterial);
proxy.name = `${manifestId}_pick_proxy`;
proxy.userData.manifestId = manifestId;
proxy.userData.pickProxy = true;
proxy.visible = false;
proxy.position.copy(center);
proxy.scale.set(
Math.max(size.x, 0.05),
Math.max(size.y, 0.05),
Math.max(size.z, 0.05),
);
proxy.updateMatrix();
proxy.matrixAutoUpdate = false;
): PickBoundsTarget {
return {
proxy,
id: manifestId,
localBounds: localBounds.clone(),
worldBounds: worldBounds.clone(),
};
}
private updatePickTargetBounds(id: string): void {
const target = this.pickBoundsById.get(id);
const bound = this.objectsById.get(id);
if (!target || !bound) {
return;
}
bound.root.updateWorldMatrix(true, false);
target.worldBounds.copy(target.localBounds).applyMatrix4(bound.root.matrixWorld);
}
private transformBounds(bounds: THREE.Box3, matrix: THREE.Matrix4): THREE.Box3 {
const transformed = new THREE.Box3();
const min = bounds.min;