@volter/editor-threejs 0.5.211 → 0.5.212

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/NOTICE +7 -2
  2. package/README.md +8 -13
  3. package/contributions/animation-mixers.service.ts +2 -4
  4. package/contributions/animation-timeline.utility.tsx +1 -2
  5. package/contributions/three.page.ts +50 -0
  6. package/dist-node/serving.mjs +84 -3
  7. package/dist-node/shared-three.mjs +145 -0
  8. package/package.json +296 -16
  9. package/serving/animation-stamp.ts +1 -1
  10. package/serving/index.ts +22 -5
  11. package/serving/model-import-conversion.ts +6 -3
  12. package/serving/shared-three.ts +176 -0
  13. package/src/adapter/constraint.ts +1 -18
  14. package/src/adapter/object3d-authoring-subject.ts +1 -12
  15. package/src/adapter/reflection-probe.ts +1 -9
  16. package/src/adapter/three-contract.ts +6 -60
  17. package/src/animation/live-mixers.ts +0 -4
  18. package/src/api.ts +7 -0
  19. package/src/asset-loaders.ts +2 -178
  20. package/src/capture/photograph-sampling.ts +333 -0
  21. package/src/capture/scene.ts +1 -1
  22. package/src/ecs/object-marks.ts +6 -14
  23. package/src/ecs/user-data.ts +3 -138
  24. package/src/kit/animation/three-clips-subject.ts +19 -42
  25. package/src/kit/asset-compare-core.ts +161 -0
  26. package/src/kit/asset-compare.ts +8 -27
  27. package/src/kit/asset-preview-command.ts +14 -19
  28. package/src/kit/asset-preview-framing.ts +8 -67
  29. package/src/kit/asset-preview.ts +109 -342
  30. package/src/kit/asset-workflow/model-inspection.ts +11 -154
  31. package/src/kit/asset-workflow/preview-resource-lifetime.ts +25 -0
  32. package/src/kit/authoring/component-instance-root.ts +3 -39
  33. package/src/kit/authoring/live-object-transform.ts +1 -30
  34. package/src/kit/authoring/object3d-document-session-registry.ts +7 -15
  35. package/src/kit/authoring/object3d-document-session.ts +471 -208
  36. package/src/kit/authoring/shell-viewport-policy.ts +18 -24
  37. package/src/kit/authoring/source-object3d-authoring-adapter.ts +6 -48
  38. package/src/kit/authoring/three-projection-core.ts +11 -40
  39. package/src/kit/authoring/viewport-pick-context.ts +5 -12
  40. package/src/kit/authoring/viewport-raycast.ts +15 -93
  41. package/src/kit/authoring/world-hidden-viewport.ts +8 -61
  42. package/src/kit/capture-camera-pose.ts +68 -0
  43. package/src/kit/collaboration-presence.ts +41 -0
  44. package/src/kit/components/CameraInfo.tsx +4 -6
  45. package/src/kit/components/InspectorObjectPreview.tsx +3 -30
  46. package/src/kit/components/Object3DDocumentToolbar.css +75 -0
  47. package/src/kit/components/Object3DDocumentToolbar.tsx +18 -84
  48. package/src/kit/components/Object3DDocumentViewport.tsx +5 -8
  49. package/src/kit/components/StageHost.css +40 -0
  50. package/src/kit/components/StageHost.tsx +181 -404
  51. package/src/kit/components/StageOverlays.tsx +1 -2
  52. package/src/kit/components/StatsOverlay.tsx +5 -17
  53. package/src/kit/components/ViewportFurniture.tsx +27 -154
  54. package/src/kit/components/ViewportOverlay.tsx +13 -17
  55. package/src/kit/components/ViewportOverlaysMenu.tsx +177 -0
  56. package/src/kit/components/ViewportShadingMenu.tsx +2 -42
  57. package/src/kit/components/ViewportViewMenu.tsx +7 -18
  58. package/src/kit/components/asset-viewers/EntityModelDocument.tsx +4 -13
  59. package/src/kit/components/asset-viewers/EnvironmentAssetDocument.tsx +5 -5
  60. package/src/kit/components/asset-viewers/LiveModuleDocument.tsx +24 -102
  61. package/src/kit/components/asset-viewers/LutAssetDocument.tsx +6 -6
  62. package/src/kit/components/asset-viewers/Object3DPreview.tsx +10 -45
  63. package/src/kit/components/asset-viewers/QuarksAssetDocument.tsx +2 -2
  64. package/src/kit/components/asset-viewers/ShaderAssetDocument.tsx +11 -10
  65. package/src/kit/components/asset-viewers/shader-source.ts +144 -0
  66. package/src/kit/components/asset-viewers/three-asset-viewers.tsx +2 -14
  67. package/src/kit/components/object3d-contribution-surfaces.tsx +1 -1
  68. package/src/kit/components/stage-keyboard.tsx +6 -12
  69. package/src/kit/components/stage-overlay-set.tsx +11 -20
  70. package/src/kit/components/stage-presence-markers.ts +9 -32
  71. package/src/kit/components/stage-transform-chrome.ts +4 -4
  72. package/src/kit/components/stage-transform-tools.tsx +7 -14
  73. package/src/kit/components/standard-viewport-dressing.ts +47 -167
  74. package/src/kit/components/world-root-binding.ts +2 -7
  75. package/src/kit/constraint-helper.ts +1 -1
  76. package/src/kit/document-renderer-session.ts +114 -0
  77. package/src/kit/editor-presentation-activity.ts +58 -0
  78. package/src/kit/editor-shell-store.ts +31 -403
  79. package/src/kit/editor-viewport.ts +476 -1762
  80. package/src/kit/entity-lod.ts +3 -16
  81. package/src/kit/entity-object.ts +1 -39
  82. package/src/kit/hierarchy-mark-reader.ts +13 -30
  83. package/src/kit/instanced-presentation.ts +1 -47
  84. package/src/kit/live-gesture-lock.ts +35 -0
  85. package/src/kit/live-module-source.ts +12 -52
  86. package/src/kit/model-thumbnail.ts +7 -35
  87. package/src/kit/play-camera-flight.ts +7 -16
  88. package/src/kit/projection/three.ts +13 -366
  89. package/src/kit/retained-document-states.ts +91 -0
  90. package/src/kit/scene-document-viewport.ts +3 -3
  91. package/src/kit/scene-framing.ts +11 -152
  92. package/src/kit/scene-view-fog.ts +2 -47
  93. package/src/kit/spatial-handle-visuals.ts +19 -28
  94. package/src/kit/stories/three-story-model.ts +5 -33
  95. package/src/kit/three-canvas-render.ts +3 -4
  96. package/src/kit/three-hierarchy-row-media.ts +3 -11
  97. package/src/kit/three-inspection-media.ts +6 -6
  98. package/src/kit/three-integration.ts +18 -18
  99. package/src/kit/three-state.ts +2 -19
  100. package/src/kit/three-viewport/camera-fit.ts +2 -6
  101. package/src/kit/three-viewport/selection-brackets.ts +5 -20
  102. package/src/kit/three-viewport/selection-outline.ts +4 -56
  103. package/src/kit/three-viewport/skeleton-helper.ts +1 -32
  104. package/src/kit/three-viewport/source-color.ts +1 -33
  105. package/src/kit/three-viewport/studio-environment.ts +4 -55
  106. package/src/kit/three-viewport-presentation.ts +23 -0
  107. package/src/kit/transport-strip.tsx +147 -0
  108. package/src/kit/viewport-actions.ts +8 -50
  109. package/src/kit/viewport-authoring-policy.ts +5 -52
  110. package/src/kit/viewport-commands.ts +28 -57
  111. package/src/kit/viewport-hotkeys.ts +29 -31
  112. package/src/kit/viewport-status-facet.ts +4 -5
  113. package/src/kit/viewport-surface-status.tsx +44 -0
  114. package/src/loader.ts +8 -71
  115. package/src/object3d-contributions.ts +18 -34
  116. package/src/viewport/clip-planes.ts +4 -32
  117. package/src/viewport/content-bounds.ts +13 -142
  118. package/src/viewport/editor-layers.ts +4 -26
  119. package/src/viewport/environment.ts +1 -1
  120. package/src/viewport/preview-renderer.ts +7 -45
  121. package/src/viewport/renderer-ownership.ts +14 -62
  122. package/src/viewport-api.ts +14 -33
  123. package/src/viewport-door.ts +16 -86
  124. package/contributions/three-integration.service.ts +0 -13
  125. package/src/adapter/hierarchy-marks.ts +0 -156
  126. package/src/adapter/ingest/scene-capture.ts +0 -843
  127. package/src/adapter/ingest/structural-ids.ts +0 -139
  128. package/src/adapter/ingest/visible-capture-window.ts +0 -302
  129. package/src/adapter/renderer-config.ts +0 -115
  130. package/src/asset-parse-error.ts +0 -29
  131. package/src/host-hierarchy-objects.ts +0 -31
  132. package/src/kit/authoring/design-time-settle.ts +0 -343
  133. package/src/kit/camera-authoring.ts +0 -175
@@ -1,343 +0,0 @@
1
- /**
2
- * THE DESIGN-TIME SETTLE, and the only place its constants are spelled.
3
- *
4
- * This is deliberately NOT an interactive-document boot step. Scene and Asset
5
- * Lab documents show authored state immediately and advance content only when
6
- * the user starts an explicit simulation or animation transport. A hidden
7
- * settle before reveal violated Edit ≠ Play and made a complex local scene wait
8
- * up to four seconds for construction quiescence plus 90 invisible steps.
9
- *
10
- * The remaining callers are deliberate off-screen preview/capture operations
11
- * that ask for a derived rest pose. That is why {@link settleDesignWorld} takes
12
- * a {@link SettleTarget} rather than a viewport type: the seam is "a world with
13
- * a host-driven tick," while the caller owns the decision to simulate.
14
- *
15
- * **The settle is a PURE FUNCTION OF SOURCE.** It never reads accumulated
16
- * state, never persists a result, and is never a second truth beside the
17
- * source: every mount re-derives the world from the file and re-runs the same
18
- * bounded simulation from the same authored spawn. That is what makes the two
19
- * interaction contracts hold — a gizmo commit writes the JSX literal, the
20
- * session re-derives from source, and the settle re-runs from the EDIT's
21
- * position (the edit wins, nothing snaps back); and two mounts of identical
22
- * source produce the same rest state.
23
- *
24
- * ## THE SEAM IT DRIVES, and why it is not a physics API
25
- *
26
- * `MountedThreeRoot.update(dt)` — the world's own host-driven tick, the exact
27
- * one `runFrameImpl` (`@volter/editor-game/runtime/game`) calls at play time. For an R3F
28
- * world that is `editor-game/src/host/roots/r3f-root.tsx`'s `update()`: it runs the
29
- * engine phases and then `advance(elapsed, true, state)`, which under
30
- * `frameloop: 'never'` is what runs every `useFrame` subscriber.
31
- *
32
- * A physics integration is one of those subscribers, so driving `update()`
33
- * drives the world's physics THROUGH THE WORLD'S OWN DECLARATION — including
34
- * whatever gravity/iterations/solver props its own `<Physics>` element
35
- * carries. Worked example, `@react-three/cannon@6.6.0`: its provider registers
36
- * `useFrame(loop)` (`node_modules/@react-three/cannon/dist/index.js:12166`)
37
- * and `loop` posts `worker.step({ maxSubSteps, stepSize, timeSinceLastCalled })`
38
- * (`:12032`) to its own Web Worker. Nothing here knows any of that, and
39
- * nothing here should: the editor reaching for a named physics library's step
40
- * API would be an editor that only settles the physics engines it has heard
41
- * of.
42
- *
43
- * ## WHY IT IS ASYNC, and why it steps in ROUNDS
44
- *
45
- * That worker is OFF-THREAD. `worker.step()` posts a message; the resulting
46
- * pose arrives on a later task and is applied to `object.matrix` from the
47
- * worker's own `frame` handler (`:12120`). A synchronous burst of `update()`
48
- * calls would therefore measure nothing but the spawn pose. So the settle
49
- * yields to the event loop, and it does so once per ROUND of
50
- * {@link SETTLE_STEPS_PER_ROUND} steps rather than once per step: the worker
51
- * queue is FIFO, so a round's steps pipeline and its frames land together,
52
- * which costs one event-loop turn per round instead of one per step.
53
- *
54
- * ## WHY REST IS ONLY DECLARED AFTER MOTION
55
- *
56
- * The naive rest test — "the scene did not move this round, so we are done" —
57
- * is TRUE at round one for the exact reason the settle exists: an off-thread
58
- * physics engine has not answered yet, so nothing has moved YET. Declaring
59
- * rest there would settle to the spawn pose and call it a rest state. Rest is
60
- * therefore gated on having OBSERVED motion first; a world that never moves
61
- * under simulation ends at {@link SETTLE_MAX_ROUNDS} with reason `'still'`,
62
- * which costs it that many event-loop turns and nothing else.
63
- *
64
- * Non-finite poses are the same class of not-yet-answered and are handled the
65
- * same way: `@react-three/cannon` writes `object.matrix` from worker buffers
66
- * that are unwritten at frame zero, so two of racing-game's meshes are
67
- * measurably `NaN` before the first frame lands (see `content-bounds.ts`'s own
68
- * measurement of exactly this). A round whose sample is non-finite is
69
- * NOT COMPARABLE: it resets the rest run and is never counted as motion.
70
- */
71
-
72
- import type * as THREE from 'three';
73
-
74
- /**
75
- * What a settle needs from a world, and nothing more: the graph to measure and
76
- * the world's OWN host-driven tick to advance. `MountedThreeRoot` satisfies it
77
- * structurally, and so does every other design-time surface's source (a story
78
- * mount's `advance`, a project contribution's `build().update`) — which is the
79
- * point: one settle, one shape, no per-surface copy.
80
- */
81
- export interface SettleTarget {
82
- /** The subtree whose motion is measured. A whole `THREE.Scene` for a mounted
83
- * root; a story's own wrapper group for an off-screen story mount. */
84
- readonly scene: THREE.Object3D;
85
- /** The world's own tick. Absent ⇒ nothing to advance (`'unsupported'`). */
86
- readonly update?: ((dt: number) => void) | undefined;
87
- /**
88
- * Whether this mount has an off-thread physics stepper subscribed (a
89
- * `<Physics>` provider, a cannon worker, a first-party adapter).
90
- *
91
- * The quiet-wait and still-rounds exist because that stepper is SILENT at
92
- * spawn: declaring rest before it answers would freeze a vehicle at a pose
93
- * the game never has. A tree, a jump pad, a mesh with no physics has
94
- * nothing that can move later, so both waits are wasted — pass `false` and
95
- * the settle returns immediately. Omitted is the SAFE default (settle):
96
- * callers that have not looked must not skip a rigid body.
97
- */
98
- readonly hasPhysicsSubscriber?: boolean;
99
- }
100
-
101
- /** The simulated timestep each settle step advances — the engine's own fixed
102
- * timestep (`core/game-loop.ts`'s `fixedTimestep ?? 1/60`), so a settle step
103
- * is the same quantum a play frame is. */
104
- export const SETTLE_STEP_SECONDS = 1 / 60;
105
-
106
- /** Steps advanced between event-loop yields. 6 steps = 0.1 simulated second —
107
- * enough that an off-thread physics worker's frames for the round have
108
- * something to show, small enough that rest is detected within 0.1s of it
109
- * happening. */
110
- export const SETTLE_STEPS_PER_ROUND = 6;
111
-
112
- /** Hard budget: 15 rounds = 90 steps = **1.5 simulated seconds**. A body
113
- * dropped onto its own suspension or resolved out of an interpenetration
114
- * reaches rest well inside that; a world that is still moving at 1.5s is
115
- * animating rather than settling, and Edit mode owes it a freeze, not a
116
- * longer run. */
117
- export const SETTLE_MAX_ROUNDS = 15;
118
-
119
- /** Rest threshold: world units moved by the FASTEST-moving node across one
120
- * round. 1e-4 units per 0.1s is 1 millimetre per second in a metres-scaled
121
- * world — below it, nothing a viewer or a capture can distinguish is
122
- * happening. */
123
- export const SETTLE_REST_EPSILON = 1e-4;
124
-
125
- /** Consecutive quiet rounds required before rest is declared, so a body
126
- * pausing at the top of a bounce is not mistaken for a rest state. */
127
- export const SETTLE_REST_ROUNDS = 2;
128
-
129
- /** After the last step, event-loop turns awaited for an off-thread physics
130
- * engine's in-flight frames to land. This is what makes "then FREEZES" true
131
- * rather than asserted: the settle does not return while a pose it caused is
132
- * still on its way. */
133
- export const SETTLE_DRAIN_TURNS = 10;
134
-
135
- /**
136
- * How long the scene graph must stop CHANGING SHAPE before the settle starts
137
- * stepping, and the hard ceiling on waiting for that.
138
- *
139
- * WHY THIS EXISTS, measured. A world's `mount()` resolves at fiber's first
140
- * commit, which is NOT the end of construction: suspended asset loads
141
- * (racing-game's Draco chassis/track GLBs and its HDR environment) add nodes —
142
- * including physics bodies — for as long as they take. Step the world during
143
- * that window and a body created at round 3 gets 12 fewer steps than one
144
- * created at round 0, so the SAME source settles differently depending on how
145
- * warm the HTTP cache was. Measured directly: two boots of unchanged
146
- * racing-game source produced chassis Y = 0.7549 and 0.7927.
147
- *
148
- * So the settle waits for the world to stop being BUILT before it simulates.
149
- * Node count is the signal (an asset resolving adds nodes; a texture arriving
150
- * does not change the count and does not change physics either). Both bounds
151
- * are wall-clock because what is being waited on is I/O, not frames.
152
- */
153
- export const SETTLE_BUILD_STABLE_MS = 250;
154
- export const SETTLE_BUILD_TIMEOUT_MS = 4000;
155
-
156
- /** Why the settle ended. */
157
- export type SettleOutcome =
158
- /** The world moved and then came to rest. */
159
- | 'rest'
160
- /** The world was still moving when the step budget ran out. */
161
- | 'budget'
162
- /** Nothing in the world ever moved under simulation. */
163
- | 'still'
164
- /** The world is not host-driven — there is no `update(dt)` to advance. */
165
- | 'unsupported';
166
-
167
- export interface SettleReport {
168
- readonly outcome: SettleOutcome;
169
- /** Simulation steps actually advanced. */
170
- readonly steps: number;
171
- /** World units the fastest node moved across the final measured round. */
172
- readonly lastMotion: number;
173
- /** Milliseconds spent waiting for the world to stop being BUILT, and whether
174
- * that wait ended because the graph went quiet (`true`) or because it hit
175
- * {@link SETTLE_BUILD_TIMEOUT_MS} (`false` — the settle then runs against a
176
- * world still loading, and its result is honestly not reproducible). */
177
- readonly buildWaitMs: number;
178
- readonly buildSettled: boolean;
179
- }
180
-
181
- /** Injectables. Real callers pass nothing; a test drives the loop and the
182
- * clock so the wall-clock bounds above cost it no wall clock. */
183
- export interface SettleHooks {
184
- yieldTurn?: () => Promise<void>;
185
- now?: () => number;
186
- }
187
-
188
- /** Every node's world position, flattened. Length changes (an async model
189
- * resolving mid-settle) make two samples NOT COMPARABLE, which is the honest
190
- * answer rather than a wrong distance. */
191
- function samplePositions(scene: THREE.Object3D): number[] {
192
- scene.updateMatrixWorld(true);
193
- const out: number[] = [];
194
- scene.traverse((object) => {
195
- const e = object.matrixWorld.elements;
196
- out.push(e[12] as number, e[13] as number, e[14] as number);
197
- });
198
- return out;
199
- }
200
-
201
- /** How many nodes the world currently has — the build-quiesce signal. */
202
- function countNodes(scene: THREE.Object3D): number {
203
- let n = 0;
204
- scene.traverse(() => {
205
- n++;
206
- });
207
- return n;
208
- }
209
-
210
- /**
211
- * The largest distance any node moved between two samples, or `NaN` when the
212
- * two are not comparable (different node counts, or a pose that is not a
213
- * number yet).
214
- */
215
- function maxMotion(before: number[], after: number[]): number {
216
- if (before.length !== after.length) return Number.NaN;
217
- let max = 0;
218
- for (let i = 0; i < after.length; i += 3) {
219
- const dx = (after[i] as number) - (before[i] as number);
220
- const dy = (after[i + 1] as number) - (before[i + 1] as number);
221
- const dz = (after[i + 2] as number) - (before[i + 2] as number);
222
- const d = Math.sqrt(dx * dx + dy * dy + dz * dz);
223
- if (!Number.isFinite(d)) return Number.NaN;
224
- if (d > max) max = d;
225
- }
226
- return max;
227
- }
228
-
229
- /** One event-loop turn — a MACROtask, because an off-thread worker's message
230
- * cannot land in a microtask drain. */
231
- const nextTurn = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0));
232
-
233
- /**
234
- * Block until the world's node count has held still for
235
- * {@link SETTLE_BUILD_STABLE_MS}, or {@link SETTLE_BUILD_TIMEOUT_MS} runs out.
236
- * See those constants for the measurement that put this phase here.
237
- */
238
- async function awaitConstructionQuiesced(
239
- scene: THREE.Object3D,
240
- yieldTurn: () => Promise<void>,
241
- now: () => number,
242
- ): Promise<{ buildWaitMs: number; buildSettled: boolean }> {
243
- const start = now();
244
- let nodes = countNodes(scene);
245
- let quietSince = start;
246
- let buildSettled = false;
247
- while (now() - start < SETTLE_BUILD_TIMEOUT_MS) {
248
- if (now() - quietSince >= SETTLE_BUILD_STABLE_MS) {
249
- buildSettled = true;
250
- break;
251
- }
252
- await yieldTurn();
253
- const next = countNodes(scene);
254
- if (next !== nodes) {
255
- nodes = next;
256
- quietSince = now();
257
- }
258
- }
259
- return { buildWaitMs: now() - start, buildSettled };
260
- }
261
-
262
- function idleSettleReport(outcome: 'unsupported' | 'still'): SettleReport {
263
- return { outcome, steps: 0, lastMotion: 0, buildWaitMs: 0, buildSettled: true };
264
- }
265
-
266
- /**
267
- * The narrow skip: no physics subscriber means nothing can move after spawn,
268
- * so the construction quiet-wait and the still-rounds are both wasted. Omitted
269
- * is treated as "yes, settle" — a caller that has not looked must not skip a
270
- * rigid body. The predicate lives here so a revert (always-skip, or treating
271
- * omitted as skip) reds the physics rest test.
272
- */
273
- export function designWorldHasPhysicsSubscriber(root: SettleTarget): boolean {
274
- return root.hasPhysicsSubscriber !== false;
275
- }
276
-
277
- /**
278
- * Derive a rest pose for a caller that explicitly requested one, then leave the
279
- * world frozen. Advances the world's OWN `update(dt)` — see this module's
280
- * header for why that is the seam and why the loop is shaped the way it is.
281
- */
282
- export async function settleDesignWorld(
283
- root: SettleTarget,
284
- hooks: SettleHooks = {},
285
- ): Promise<SettleReport> {
286
- const yieldTurn = hooks.yieldTurn ?? nextTurn;
287
- const now = hooks.now ?? (() => Date.now());
288
- const update = root.update?.bind(root);
289
- if (!update) return idleSettleReport('unsupported');
290
- if (!designWorldHasPhysicsSubscriber(root)) return idleSettleReport('still');
291
-
292
- // ---- 1. wait for the world to stop being BUILT
293
- const { buildWaitMs, buildSettled } = await awaitConstructionQuiesced(root.scene, yieldTurn, now);
294
-
295
- // ---- 2. the bounded settle itself
296
- let previous = samplePositions(root.scene);
297
- let steps = 0;
298
- let quietRounds = 0;
299
- let observedMotion = false;
300
- let lastMotion = 0;
301
- let outcome: SettleOutcome = 'still';
302
-
303
- for (let round = 0; round < SETTLE_MAX_ROUNDS; round++) {
304
- for (let step = 0; step < SETTLE_STEPS_PER_ROUND; step++) {
305
- update(SETTLE_STEP_SECONDS);
306
- steps++;
307
- }
308
- await yieldTurn();
309
- const current = samplePositions(root.scene);
310
- const motion = maxMotion(previous, current);
311
- previous = current;
312
- if (!Number.isFinite(motion)) {
313
- // Not comparable — the world is mid-answer. Never rest, never motion.
314
- quietRounds = 0;
315
- continue;
316
- }
317
- lastMotion = motion;
318
- if (motion > SETTLE_REST_EPSILON) {
319
- observedMotion = true;
320
- quietRounds = 0;
321
- outcome = 'budget';
322
- continue;
323
- }
324
- quietRounds++;
325
- if (observedMotion && quietRounds >= SETTLE_REST_ROUNDS) {
326
- outcome = 'rest';
327
- break;
328
- }
329
- }
330
-
331
- // ---- 3. FREEZE. Nothing is stepped from here; the drain only waits out
332
- // poses this settle already caused, so the scene the editor draws from now
333
- // on is a real rest state rather than one still being written into.
334
- for (let turn = 0; turn < SETTLE_DRAIN_TURNS; turn++) {
335
- await yieldTurn();
336
- const current = samplePositions(root.scene);
337
- const motion = maxMotion(previous, current);
338
- previous = current;
339
- if (Number.isFinite(motion) && motion <= SETTLE_REST_EPSILON) break;
340
- }
341
-
342
- return { outcome, steps, lastMotion, buildWaitMs, buildSettled };
343
- }
@@ -1,175 +0,0 @@
1
- import type * as THREE from 'three';
2
-
3
- /**
4
- * Transient camera-authoring presentation for the Scene viewport.
5
- *
6
- * Cameras remain ordinary native Three.js objects owned by the active
7
- * AuthoringAdapter. This module carries only viewport presentation (pin,
8
- * view-through, pilot) and delegates every authored mutation to the host's
9
- * canonical transform gesture path. There is no camera document or sidecar.
10
- */
11
-
12
- export type CameraViewMode = 'view' | 'pilot';
13
-
14
- export interface CameraAuthoringSubject {
15
- readonly id: string;
16
- readonly name: string;
17
- readonly camera: THREE.Camera;
18
- readonly lens: string;
19
- /** Position and rotation can both enter the adapter's normal edit gesture. */
20
- readonly canAuthorPose: boolean;
21
- readonly poseRefusal?: string;
22
- }
23
-
24
- export interface CameraAuthoringPresentation {
25
- readonly preview: CameraAuthoringSubject | null;
26
- readonly previewPinned: boolean;
27
- readonly view: {
28
- readonly mode: CameraViewMode;
29
- readonly subject: CameraAuthoringSubject;
30
- } | null;
31
- readonly guidesVisible: boolean;
32
- }
33
-
34
- export interface CameraAuthoringHost {
35
- /** Camera authoring is an Edit/Scene affordance, never a Play overlay. */
36
- readonly active: () => boolean;
37
- readonly selected: () => CameraAuthoringSubject | null;
38
- readonly resolve: (id: string) => CameraAuthoringSubject | null;
39
- readonly showView: (subject: CameraAuthoringSubject, mode: CameraViewMode) => void;
40
- readonly leaveView: () => void;
41
- readonly alignToViewport: (subject: CameraAuthoringSubject) => void;
42
- readonly subscribe: (listener: () => void) => () => void;
43
- }
44
-
45
- let host: CameraAuthoringHost | null = null;
46
- let disposeHostSubscription: (() => void) | null = null;
47
- let pinnedId: string | null = null;
48
- let viewState: { id: string; mode: CameraViewMode; camera: THREE.Camera } | null = null;
49
- let guidesVisible = true;
50
- let version = 0;
51
- const listeners = new Set<() => void>();
52
-
53
- function notify(): void {
54
- version++;
55
- for (const listener of listeners) listener();
56
- }
57
-
58
- function syncHostState(): void {
59
- if (!host) return;
60
- if (!host.active()) {
61
- if (viewState) host.leaveView();
62
- viewState = null;
63
- notify();
64
- return;
65
- }
66
- if (pinnedId && !host.resolve(pinnedId)) pinnedId = null;
67
- if (viewState) {
68
- const subject = host.resolve(viewState.id);
69
- if (!subject) {
70
- viewState = null;
71
- host.leaveView();
72
- } else if (subject.camera !== viewState.camera) {
73
- // HMR may replace the native camera while preserving its semantic id.
74
- // Rebind the transient view to the fresh object rather than retaining a
75
- // dead Three.js reference.
76
- viewState = { ...viewState, camera: subject.camera };
77
- host.showView(subject, viewState.mode);
78
- }
79
- }
80
- notify();
81
- }
82
-
83
- /** Install the one live Scene viewport host. The returned disposer is
84
- * identity-guarded so a stale unmount cannot clear a newer viewport. */
85
- export function installCameraAuthoringHost(next: CameraAuthoringHost): () => void {
86
- disposeHostSubscription?.();
87
- host?.leaveView();
88
- host = next;
89
- pinnedId = null;
90
- viewState = null;
91
- guidesVisible = true;
92
- disposeHostSubscription = next.subscribe(syncHostState);
93
- notify();
94
- return () => {
95
- if (host !== next) return;
96
- disposeHostSubscription?.();
97
- disposeHostSubscription = null;
98
- next.leaveView();
99
- host = null;
100
- pinnedId = null;
101
- viewState = null;
102
- guidesVisible = true;
103
- notify();
104
- };
105
- }
106
-
107
- export function subscribeCameraAuthoring(listener: () => void): () => void {
108
- listeners.add(listener);
109
- return () => listeners.delete(listener);
110
- }
111
-
112
- export function cameraAuthoringVersion(): number {
113
- return version;
114
- }
115
-
116
- export function cameraAuthoringPresentation(): CameraAuthoringPresentation {
117
- if (!host?.active()) {
118
- return { preview: null, previewPinned: false, view: null, guidesVisible };
119
- }
120
- const selected = host?.selected() ?? null;
121
- const pinned = pinnedId ? (host?.resolve(pinnedId) ?? null) : null;
122
- const viewSubject = viewState ? (host?.resolve(viewState.id) ?? null) : null;
123
- return {
124
- preview: viewSubject ? null : (pinned ?? selected),
125
- previewPinned: Boolean(pinned),
126
- view: viewState && viewSubject ? { mode: viewState.mode, subject: viewSubject } : null,
127
- guidesVisible,
128
- };
129
- }
130
-
131
- export function currentCameraAuthoringSubject(): CameraAuthoringSubject | null {
132
- const presentation = cameraAuthoringPresentation();
133
- return presentation.view?.subject ?? presentation.preview;
134
- }
135
-
136
- export function toggleCameraPreviewPin(): void {
137
- const presentation = cameraAuthoringPresentation();
138
- const subject = presentation.preview;
139
- if (!subject) return;
140
- pinnedId = presentation.previewPinned ? null : subject.id;
141
- notify();
142
- }
143
-
144
- export function viewThroughCamera(subject = currentCameraAuthoringSubject()): void {
145
- if (!host || !subject) return;
146
- if (viewState) host.leaveView();
147
- viewState = { id: subject.id, mode: 'view', camera: subject.camera };
148
- host.showView(subject, 'view');
149
- notify();
150
- }
151
-
152
- export function pilotCamera(subject = currentCameraAuthoringSubject()): void {
153
- if (!host || !subject || !subject.canAuthorPose) return;
154
- if (viewState) host.leaveView();
155
- viewState = { id: subject.id, mode: 'pilot', camera: subject.camera };
156
- host.showView(subject, 'pilot');
157
- notify();
158
- }
159
-
160
- export function alignCameraToViewport(subject = currentCameraAuthoringSubject()): void {
161
- if (!host || !subject || !subject.canAuthorPose) return;
162
- host.alignToViewport(subject);
163
- }
164
-
165
- export function leaveCameraView(): void {
166
- if (!host || !viewState) return;
167
- viewState = null;
168
- host.leaveView();
169
- notify();
170
- }
171
-
172
- export function toggleCameraCompositionGuides(): void {
173
- guidesVisible = !guidesVisible;
174
- notify();
175
- }