lecodes-cli 0.17.1 → 0.17.2

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.
@@ -1,291 +1,299 @@
1
- // A 3D scene: a draw set, a Camera, lights and environment. Opening a scene asks the host to bring up
2
- // the 3D engine; only the active scene renders. Grown from the Phase 0 seed. Ported from
3
- // worker/src/Scene.ts (Scene + ARScene), with the camera exposed as scene.camera.
4
- //
5
- // Lights are explicit: a scene gets image-based ambient (IBL) by default; add a Light.sun() for
6
- // direct light + shadows.
7
-
8
- import { Color, type ColorInput } from "../core/color"
9
- import { _installAspectFrames } from "../core/Aspect"
10
- import { _bumpNavEpoch, _navEpoch, _navSupported, _setCurrent, Presentable, type PresentOptions } from "../ui/presentable"
11
- import type { ClickEvent, TouchStartEvent } from "../runtime/touch"
12
- import type { FetchResponse } from "../runtime/fetch"
13
- import { _requestCameraPermission } from "../plugins/permission"
14
- import { Camera } from "./Camera"
15
- import { attachControls, type ControlsHandle, type ControlsOptions } from "./controls"
16
- import { Material } from "./Material"
17
- import { Node, nodeRegistry } from "./Node"
18
- import { glState } from "./state"
19
- import { registerTouchEndEvent, registerTouchStartEvent } from "./touch"
20
-
21
- export type SceneOptions = {
22
- /** Image-based ambient lighting (default on). */
23
- ibl?: boolean
24
- /** IBL intensity (default 20000). */
25
- environmentIntensity?: number
26
- /** Bloom post-processing. */
27
- bloom?: boolean
28
- bloomIntensity?: number
29
- /** Skybox / clear color. */
30
- skybox?: ColorInput
31
- /** Multisample anti-aliasing. */
32
- antialias?: boolean
33
- }
34
-
35
- // Route aspect update(dt) through the render-synced native phases (render() early = before physics,
36
- // late = after animations / before draw), so followers/cameras are in lockstep with the draw. Done from
37
- // the 3D layer (not core) so a 2D-only bundle never pulls in the 3D bridge. Installed once, lazily on
38
- // the first Scene. On an older wasm without the hooks the installer returns false → setLoop fallback.
39
- let framesInstalled = false
40
- const installRenderSyncedFrames = (): void => {
41
- if (framesInstalled) return
42
- framesInstalled = true
43
- _installAspectFrames((early, late) => {
44
- if (typeof _creator.setEarlyUpdate !== "function") return false
45
- // the frame stamp invalidates every Node's local-matrix cache once per frame (physics / character
46
- // controllers / animation rewrite transforms natively between frames)
47
- _creator.setEarlyUpdate((dt: number) => { glState.frame++; early(dt) })
48
- _creator.setLateUpdate(late)
49
- return true
50
- })
51
- }
52
-
53
- export class Scene implements Presentable {
54
- /** @internal native scene handle. */
55
- readonly _id: number
56
- readonly camera: Camera
57
-
58
- /** @internal touch listeners, read by the dispatch system. */
59
- readonly _clickListeners: Array<(ev: ClickEvent<Node | null>) => void> = []
60
- readonly _touchStartListeners: Array<(ev: TouchStartEvent<Node | null>) => void> = []
61
-
62
- private _material?: Material
63
-
64
- private static _active: Scene | null = null
65
- static get active(): Scene | null { return Scene._active }
66
-
67
- constructor(options: SceneOptions = {}) {
68
- installRenderSyncedFrames()
69
- const parent = (options as any)._parent as Scene | undefined
70
- if (parent) {
71
- this.camera = parent.camera
72
- this._id = _creator.createOverlayScene(parent._id)
73
- // No camera re-attach: the native createOverlayScene wires the parent's camera into the
74
- // overlay view. Re-attaching would (a) point camera._sceneId — the source for getRay /
75
- // getViewDirection / fov — at the overlay's view, whose viewport/projection aren't
76
- // maintained like the render view's, and (b) create a second native camera component on
77
- // the shared entity. (Found via the scene editor: gizmo pick rays came out distorted.)
78
- } else {
79
- this.camera = new Camera()
80
- this._id = _creator.createScene()
81
- this.camera._attach(this._id)
82
- }
83
-
84
- if (options.ibl !== false) _creator.setDefaultIbl(this._id, options.environmentIntensity ?? 20000)
85
- if (options.bloom) _creator.setBloomOptions(this._id, true, options.bloomIntensity ?? 0.2, 1)
86
- if (options.skybox !== undefined) _creator.setSkybox(this._id, Color.toPackedRgb(options.skybox))
87
- if (options.antialias) _creator.setSceneMultiSampleAntiAliasing(this._id, true, 4)
88
- }
89
-
90
- /** Scene occlusion material. */
91
- get occlusionMaterial(): Material {
92
- if (!this._material) this._material = new Material(_creator.getSceneMaterial(this._id))
93
- return this._material
94
- }
95
-
96
- set skybox(color: ColorInput) { _creator.setSkybox(this._id, Color.toPackedRgb(color)) }
97
-
98
- setMaterialGlobalParameter(i: number, x: number, y: number, z: number, w: number): void {
99
- _creator.setMaterialGlobalParameter(this._id, i, x, y, z, w)
100
- }
101
- setAntialias(enabled: boolean, scale = 4): void {
102
- _creator.setSceneMultiSampleAntiAliasing(this._id, enabled, scale)
103
- }
104
-
105
- add(...nodes: Node[]): this {
106
- for (const n of nodes) _creator.addEntityToScene(this._id, n.id)
107
- return this
108
- }
109
- remove(...nodes: Node[]): this {
110
- for (const n of nodes) _creator.removeEntityFromScene(this._id, n.id)
111
- return this
112
- }
113
-
114
- createOverlay(options: SceneOptions = {}): Scene {
115
- return new Scene({ ...options, _parent: this } as any)
116
- }
117
-
118
- warmRender(): Promise<void> {
119
- return new Promise<void>((res) => _creator.warmRender(this._id, res))
120
- }
121
-
122
- // ---- Presentable (docs/navigation-presentable-plan.md) ----
123
- // A scene is a destination like a screen: opening it replaces whatever is visible (suspending
124
- // an active Router until Router.restore()), and it can be pushed onto the Router stack. The
125
- // scene graph survives while covered — only the view detaches and rendering pauses.
126
-
127
- /** @internal presentation lifecycle — fired by the host on present/dismiss (new navigation),
128
- * or synchronously by open()/close() on legacy hosts. */
129
- readonly ol: (() => void)[] = []
130
- readonly cl: (() => void)[] = []
131
- _backButtonCallback?: () => void
132
- private _vd?: object
133
-
134
- /** Fires when the scene becomes the visible destination (incl. a pop revealing it). */
135
- onOpen(callback: () => void): this {
136
- this.ol.push(callback)
137
- return this
138
- }
139
- /** Fires when the scene stops being visible (closed, replaced, or covered by a push). */
140
- onClose(callback: () => void): this {
141
- this.cl.push(callback)
142
- return this
143
- }
144
- onBackPressed(callback: () => void): this {
145
- this._backButtonCallback = callback
146
- return this
147
- }
148
-
149
- /** @internal stable wire descriptor; hosts hand it back via router onChange (`_p` = this). */
150
- _viewDesc(): object {
151
- return this._vd ??= { type: "scene3d", sceneId: this._id, _p: this }
152
- }
153
-
154
- /** Make this the active scene — shows it as the current destination (replaces a screen /
155
- * another scene; only the active scene renders). */
156
- open(options?: PresentOptions): void | Promise<void> {
157
- _bumpNavEpoch()
158
- if (_navSupported()) {
159
- // The host activates the engine (openScene) as part of presenting the destination.
160
- _creatorUI.openView!(this._viewDesc(), options?.transition ?? "none")
161
- } else {
162
- _creator.openScene(this._id)
163
- for (const cb of this.ol) cb()
164
- }
165
- Scene._active = this
166
- glState.activeScene = this
167
- _setCurrent(this)
168
- }
169
- close(): void {
170
- _bumpNavEpoch()
171
- if (_navSupported()) {
172
- _creatorUI.closeView!()
173
- } else {
174
- _creator.closeScene()
175
- for (const cb of this.cl) cb()
176
- }
177
- if (Scene._active === this) Scene._active = null
178
- if (glState.activeScene === this) glState.activeScene = null
179
- if (Presentable.current === this) _setCurrent(null)
180
- }
181
-
182
- addEventListener(channel: "click", callback: (ev: ClickEvent<Node | null>) => void): void
183
- addEventListener(channel: "touchstart", callback: (ev: TouchStartEvent<Node | null>) => void): void
184
- addEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
185
- if (channel === "click") {
186
- if (!this._clickListeners.includes(callback)) { this._clickListeners.push(callback); registerTouchEndEvent() }
187
- } else if (channel === "touchstart") {
188
- if (!this._touchStartListeners.includes(callback)) { this._touchStartListeners.push(callback); registerTouchStartEvent() }
189
- }
190
- }
191
- removeEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
192
- const list = channel === "click" ? this._clickListeners : this._touchStartListeners
193
- const i = list.indexOf(callback)
194
- if (i >= 0) list.splice(i, 1)
195
- }
196
- }
197
-
198
- // --- AR ---
199
-
200
- export type ARMode = "default" | "markers" | "arcore" | "detached"
201
-
202
- const onTrackStateChange = (entityId: number, track: boolean): void => {
203
- const node = nodeRegistry.get(entityId)
204
- if (!node) return
205
- node.isTracked = track
206
- if (track) node._dispatchEvent("track")
207
- else node._dispatchEvent("untrack")
208
- }
209
-
210
- export class ARScene<T extends ARMode = "default"> extends Scene {
211
- readonly mode: T
212
- useWarmRender = true
213
- private _root?: Node
214
-
215
- constructor(options: SceneOptions & { mode?: T } = {}) {
216
- super(options)
217
- this.mode = options.mode ?? ("default" as T)
218
- _creator.createARController(this._id, this.camera.id, this.mode, onTrackStateChange)
219
- }
220
-
221
- /** Root anchor node (not available in marker mode). */
222
- get root(): Node {
223
- if (this._root) return this._root
224
- this._root = new Node(_creator.createRootAnchor(this._id))
225
- return this._root
226
- }
227
-
228
- /** Create an anchor from a tracking source (e.g. an image target). */
229
- createAnchor(source: FetchResponse, physicalWidth = 0.2): Node {
230
- return new Node(_creator.createAnchor(this._id, physicalWidth, (source as any)._id))
231
- }
232
-
233
- /**
234
- * Let the user place & manipulate a node with touch gestures — the staple AR object-placement
235
- * interaction: ONE finger drags it across the floor (relative to where the camera looks), TWO fingers
236
- * pinch to scale and twist to rotate. Returns a handle; call `.remove()` to detach. Tunable via
237
- * options (min/max scale, and toggles for `pan` / `pinch` / `twist`).
238
- */
239
- addControls(target: Node, options: ControlsOptions = {}): ControlsHandle {
240
- return attachControls(this, target, options)
241
- }
242
-
243
- override async open(options?: PresentOptions): Promise<void> {
244
- await this._prepare()
245
- super.open(options)
246
- }
247
-
248
- /** @internal Presentable prepare hook: camera permission + warm render + AR session, all while
249
- * the previous destination (typically a loading screen) stays visible — the swap to this scene
250
- * happens only after it resolves. Warm render is headless on every platform (offscreen
251
- * swapchain), so no GL view is created here — the host mounts the scene view at present time.
252
- * Rejects on camera denial / AR launch failure, or if another navigation happened while
253
- * preparing (superseded) — in every rejection case the current destination stays visible. */
254
- async _prepare(): Promise<void> {
255
- const epoch = _navEpoch()
256
- await _requestCameraPermission()
257
- if (this.useWarmRender) await this.warmRender()
258
- await new Promise<void>((res, rej) => _creator.launchAR(this._id, res, rej))
259
- if (_navEpoch() !== epoch) {
260
- _creator.stopAR()
261
- throw new Error("ARScene.open() superseded: another destination was opened while preparing")
262
- }
263
- }
264
-
265
- override close(): void {
266
- _creator.stopAR()
267
- super.close()
268
- }
269
- }
270
-
271
- // --- VR ---
272
-
273
- /**
274
- * A 3D scene rendered in a VR headset (OpenXR — e.g. Meta Quest). The headset drives the camera:
275
- * every frame both eyes are rendered from the tracked head pose, so `scene.camera` position/lookAt
276
- * have no effect while the scene is open. The world origin is at floor level (stage space) —
277
- * author content with the ground at y = 0 and the user standing at the origin.
278
- *
279
- * `open()` rejects on hosts without a VR runtime ("VR is not supported on this device").
280
- */
281
- export class VRScene extends Scene {
282
- override async open(options?: PresentOptions): Promise<void> {
283
- await new Promise<void>((res, rej) => _creator.launchVR(this._id, res, rej))
284
- super.open(options)
285
- }
286
-
287
- override close(): void {
288
- _creator.stopVR()
289
- super.close()
290
- }
291
- }
1
+ // A 3D scene: a draw set, a Camera, lights and environment. Opening a scene asks the host to bring up
2
+ // the 3D engine; only the active scene renders. Grown from the Phase 0 seed. Ported from
3
+ // worker/src/Scene.ts (Scene + ARScene), with the camera exposed as scene.camera.
4
+ //
5
+ // Lights are explicit: a scene gets image-based ambient (IBL) by default; add a Light.sun() for
6
+ // direct light + shadows.
7
+
8
+ import { Color, type ColorInput } from "../core/color"
9
+ import { _installAspectFrames } from "../core/Aspect"
10
+ import { _bumpNavEpoch, _navEpoch, _navSupported, _setCurrent, Presentable, type PresentOptions } from "../ui/presentable"
11
+ import type { ClickEvent, TouchStartEvent } from "../runtime/touch"
12
+ import type { FetchResponse } from "../runtime/fetch"
13
+ import { _requestCameraPermission } from "../plugins/permission"
14
+ import { Camera } from "./Camera"
15
+ import { attachControls, type ControlsHandle, type ControlsOptions } from "./controls"
16
+ import { Material } from "./Material"
17
+ import { Node, nodeRegistry } from "./Node"
18
+ import { glState } from "./state"
19
+ import { registerTouchEndEvent, registerTouchStartEvent } from "./touch"
20
+
21
+ export type SceneOptions = {
22
+ /** Image-based ambient lighting (default on). */
23
+ ibl?: boolean
24
+ /** IBL intensity (default 20000). */
25
+ environmentIntensity?: number
26
+ /** Bloom post-processing. */
27
+ bloom?: boolean
28
+ bloomIntensity?: number
29
+ /** Tone mapping operator. `'aces'` (default, filament's ACES legacy) desaturates bright colours
30
+ * towards white — HDR fire reads pale; `'neutral'` (Khronos PBR Neutral) keeps hue and
31
+ * saturation until very bright; `'linear'` clips each channel (what an engine without a
32
+ * tonemapper shows — saturated, Unity-without-post-processing look); `'filmic'` (Uncharted). */
33
+ toneMapping?: "aces" | "neutral" | "linear" | "filmic"
34
+ /** Skybox / clear color. */
35
+ skybox?: ColorInput
36
+ /** Multisample anti-aliasing. */
37
+ antialias?: boolean
38
+ }
39
+
40
+ // Route aspect update(dt) through the render-synced native phases (render() early = before physics,
41
+ // late = after animations / before draw), so followers/cameras are in lockstep with the draw. Done from
42
+ // the 3D layer (not core) so a 2D-only bundle never pulls in the 3D bridge. Installed once, lazily on
43
+ // the first Scene. On an older wasm without the hooks the installer returns false → setLoop fallback.
44
+ let framesInstalled = false
45
+ const installRenderSyncedFrames = (): void => {
46
+ if (framesInstalled) return
47
+ framesInstalled = true
48
+ _installAspectFrames((early, late) => {
49
+ if (typeof _creator.setEarlyUpdate !== "function") return false
50
+ // the frame stamp invalidates every Node's local-matrix cache once per frame (physics / character
51
+ // controllers / animation rewrite transforms natively between frames)
52
+ _creator.setEarlyUpdate((dt: number) => { glState.frame++; early(dt) })
53
+ _creator.setLateUpdate(late)
54
+ return true
55
+ })
56
+ }
57
+
58
+ export class Scene implements Presentable {
59
+ /** @internal native scene handle. */
60
+ readonly _id: number
61
+ readonly camera: Camera
62
+
63
+ /** @internal touch listeners, read by the dispatch system. */
64
+ readonly _clickListeners: Array<(ev: ClickEvent<Node | null>) => void> = []
65
+ readonly _touchStartListeners: Array<(ev: TouchStartEvent<Node | null>) => void> = []
66
+
67
+ private _material?: Material
68
+
69
+ private static _active: Scene | null = null
70
+ static get active(): Scene | null { return Scene._active }
71
+
72
+ constructor(options: SceneOptions = {}) {
73
+ installRenderSyncedFrames()
74
+ const parent = (options as any)._parent as Scene | undefined
75
+ if (parent) {
76
+ this.camera = parent.camera
77
+ this._id = _creator.createOverlayScene(parent._id)
78
+ // No camera re-attach: the native createOverlayScene wires the parent's camera into the
79
+ // overlay view. Re-attaching would (a) point camera._sceneId — the source for getRay /
80
+ // getViewDirection / fov — at the overlay's view, whose viewport/projection aren't
81
+ // maintained like the render view's, and (b) create a second native camera component on
82
+ // the shared entity. (Found via the scene editor: gizmo pick rays came out distorted.)
83
+ } else {
84
+ this.camera = new Camera()
85
+ this._id = _creator.createScene()
86
+ this.camera._attach(this._id)
87
+ }
88
+
89
+ if (options.ibl !== false) _creator.setDefaultIbl(this._id, options.environmentIntensity ?? 20000)
90
+ if (options.bloom) _creator.setBloomOptions(this._id, true, options.bloomIntensity ?? 0.2, 1)
91
+ if (options.toneMapping !== undefined && options.toneMapping !== "aces") {
92
+ _creator.setToneMapping?.(this._id, { neutral: 1, linear: 2, filmic: 3 }[options.toneMapping] ?? 0)
93
+ }
94
+ if (options.skybox !== undefined) _creator.setSkybox(this._id, Color.toPackedRgb(options.skybox))
95
+ if (options.antialias) _creator.setSceneMultiSampleAntiAliasing(this._id, true, 4)
96
+ }
97
+
98
+ /** Scene occlusion material. */
99
+ get occlusionMaterial(): Material {
100
+ if (!this._material) this._material = new Material(_creator.getSceneMaterial(this._id))
101
+ return this._material
102
+ }
103
+
104
+ set skybox(color: ColorInput) { _creator.setSkybox(this._id, Color.toPackedRgb(color)) }
105
+
106
+ setMaterialGlobalParameter(i: number, x: number, y: number, z: number, w: number): void {
107
+ _creator.setMaterialGlobalParameter(this._id, i, x, y, z, w)
108
+ }
109
+ setAntialias(enabled: boolean, scale = 4): void {
110
+ _creator.setSceneMultiSampleAntiAliasing(this._id, enabled, scale)
111
+ }
112
+
113
+ add(...nodes: Node[]): this {
114
+ for (const n of nodes) _creator.addEntityToScene(this._id, n.id)
115
+ return this
116
+ }
117
+ remove(...nodes: Node[]): this {
118
+ for (const n of nodes) _creator.removeEntityFromScene(this._id, n.id)
119
+ return this
120
+ }
121
+
122
+ createOverlay(options: SceneOptions = {}): Scene {
123
+ return new Scene({ ...options, _parent: this } as any)
124
+ }
125
+
126
+ warmRender(): Promise<void> {
127
+ return new Promise<void>((res) => _creator.warmRender(this._id, res))
128
+ }
129
+
130
+ // ---- Presentable (docs/navigation-presentable-plan.md) ----
131
+ // A scene is a destination like a screen: opening it replaces whatever is visible (suspending
132
+ // an active Router until Router.restore()), and it can be pushed onto the Router stack. The
133
+ // scene graph survives while covered — only the view detaches and rendering pauses.
134
+
135
+ /** @internal presentation lifecycle — fired by the host on present/dismiss (new navigation),
136
+ * or synchronously by open()/close() on legacy hosts. */
137
+ readonly ol: (() => void)[] = []
138
+ readonly cl: (() => void)[] = []
139
+ _backButtonCallback?: () => void
140
+ private _vd?: object
141
+
142
+ /** Fires when the scene becomes the visible destination (incl. a pop revealing it). */
143
+ onOpen(callback: () => void): this {
144
+ this.ol.push(callback)
145
+ return this
146
+ }
147
+ /** Fires when the scene stops being visible (closed, replaced, or covered by a push). */
148
+ onClose(callback: () => void): this {
149
+ this.cl.push(callback)
150
+ return this
151
+ }
152
+ onBackPressed(callback: () => void): this {
153
+ this._backButtonCallback = callback
154
+ return this
155
+ }
156
+
157
+ /** @internal stable wire descriptor; hosts hand it back via router onChange (`_p` = this). */
158
+ _viewDesc(): object {
159
+ return this._vd ??= { type: "scene3d", sceneId: this._id, _p: this }
160
+ }
161
+
162
+ /** Make this the active scene — shows it as the current destination (replaces a screen /
163
+ * another scene; only the active scene renders). */
164
+ open(options?: PresentOptions): void | Promise<void> {
165
+ _bumpNavEpoch()
166
+ if (_navSupported()) {
167
+ // The host activates the engine (openScene) as part of presenting the destination.
168
+ _creatorUI.openView!(this._viewDesc(), options?.transition ?? "none")
169
+ } else {
170
+ _creator.openScene(this._id)
171
+ for (const cb of this.ol) cb()
172
+ }
173
+ Scene._active = this
174
+ glState.activeScene = this
175
+ _setCurrent(this)
176
+ }
177
+ close(): void {
178
+ _bumpNavEpoch()
179
+ if (_navSupported()) {
180
+ _creatorUI.closeView!()
181
+ } else {
182
+ _creator.closeScene()
183
+ for (const cb of this.cl) cb()
184
+ }
185
+ if (Scene._active === this) Scene._active = null
186
+ if (glState.activeScene === this) glState.activeScene = null
187
+ if (Presentable.current === this) _setCurrent(null)
188
+ }
189
+
190
+ addEventListener(channel: "click", callback: (ev: ClickEvent<Node | null>) => void): void
191
+ addEventListener(channel: "touchstart", callback: (ev: TouchStartEvent<Node | null>) => void): void
192
+ addEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
193
+ if (channel === "click") {
194
+ if (!this._clickListeners.includes(callback)) { this._clickListeners.push(callback); registerTouchEndEvent() }
195
+ } else if (channel === "touchstart") {
196
+ if (!this._touchStartListeners.includes(callback)) { this._touchStartListeners.push(callback); registerTouchStartEvent() }
197
+ }
198
+ }
199
+ removeEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
200
+ const list = channel === "click" ? this._clickListeners : this._touchStartListeners
201
+ const i = list.indexOf(callback)
202
+ if (i >= 0) list.splice(i, 1)
203
+ }
204
+ }
205
+
206
+ // --- AR ---
207
+
208
+ export type ARMode = "default" | "markers" | "arcore" | "detached"
209
+
210
+ const onTrackStateChange = (entityId: number, track: boolean): void => {
211
+ const node = nodeRegistry.get(entityId)
212
+ if (!node) return
213
+ node.isTracked = track
214
+ if (track) node._dispatchEvent("track")
215
+ else node._dispatchEvent("untrack")
216
+ }
217
+
218
+ export class ARScene<T extends ARMode = "default"> extends Scene {
219
+ readonly mode: T
220
+ useWarmRender = true
221
+ private _root?: Node
222
+
223
+ constructor(options: SceneOptions & { mode?: T } = {}) {
224
+ super(options)
225
+ this.mode = options.mode ?? ("default" as T)
226
+ _creator.createARController(this._id, this.camera.id, this.mode, onTrackStateChange)
227
+ }
228
+
229
+ /** Root anchor node (not available in marker mode). */
230
+ get root(): Node {
231
+ if (this._root) return this._root
232
+ this._root = new Node(_creator.createRootAnchor(this._id))
233
+ return this._root
234
+ }
235
+
236
+ /** Create an anchor from a tracking source (e.g. an image target). */
237
+ createAnchor(source: FetchResponse, physicalWidth = 0.2): Node {
238
+ return new Node(_creator.createAnchor(this._id, physicalWidth, (source as any)._id))
239
+ }
240
+
241
+ /**
242
+ * Let the user place & manipulate a node with touch gestures — the staple AR object-placement
243
+ * interaction: ONE finger drags it across the floor (relative to where the camera looks), TWO fingers
244
+ * pinch to scale and twist to rotate. Returns a handle; call `.remove()` to detach. Tunable via
245
+ * options (min/max scale, and toggles for `pan` / `pinch` / `twist`).
246
+ */
247
+ addControls(target: Node, options: ControlsOptions = {}): ControlsHandle {
248
+ return attachControls(this, target, options)
249
+ }
250
+
251
+ override async open(options?: PresentOptions): Promise<void> {
252
+ await this._prepare()
253
+ super.open(options)
254
+ }
255
+
256
+ /** @internal Presentable prepare hook: camera permission + warm render + AR session, all while
257
+ * the previous destination (typically a loading screen) stays visible — the swap to this scene
258
+ * happens only after it resolves. Warm render is headless on every platform (offscreen
259
+ * swapchain), so no GL view is created here — the host mounts the scene view at present time.
260
+ * Rejects on camera denial / AR launch failure, or if another navigation happened while
261
+ * preparing (superseded) — in every rejection case the current destination stays visible. */
262
+ async _prepare(): Promise<void> {
263
+ const epoch = _navEpoch()
264
+ await _requestCameraPermission()
265
+ if (this.useWarmRender) await this.warmRender()
266
+ await new Promise<void>((res, rej) => _creator.launchAR(this._id, res, rej))
267
+ if (_navEpoch() !== epoch) {
268
+ _creator.stopAR()
269
+ throw new Error("ARScene.open() superseded: another destination was opened while preparing")
270
+ }
271
+ }
272
+
273
+ override close(): void {
274
+ _creator.stopAR()
275
+ super.close()
276
+ }
277
+ }
278
+
279
+ // --- VR ---
280
+
281
+ /**
282
+ * A 3D scene rendered in a VR headset (OpenXR — e.g. Meta Quest). The headset drives the camera:
283
+ * every frame both eyes are rendered from the tracked head pose, so `scene.camera` position/lookAt
284
+ * have no effect while the scene is open. The world origin is at floor level (stage space) —
285
+ * author content with the ground at y = 0 and the user standing at the origin.
286
+ *
287
+ * `open()` rejects on hosts without a VR runtime ("VR is not supported on this device").
288
+ */
289
+ export class VRScene extends Scene {
290
+ override async open(options?: PresentOptions): Promise<void> {
291
+ await new Promise<void>((res, rej) => _creator.launchVR(this._id, res, rej))
292
+ super.open(options)
293
+ }
294
+
295
+ override close(): void {
296
+ _creator.stopVR()
297
+ super.close()
298
+ }
299
+ }