lecodes-cli 0.18.2 → 0.19.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.
Files changed (224) hide show
  1. package/dist/index.js +1866 -301
  2. package/package.json +14 -12
  3. package/runtime/scene-harness.json +1 -1
  4. package/runtime/sdk-types.json +1 -1
  5. package/src/api.ts +302 -0
  6. package/src/browserAuth.ts +87 -0
  7. package/src/cmgenTool.ts +104 -0
  8. package/src/commands/app.ts +891 -0
  9. package/src/commands/appAndroid.ts +603 -0
  10. package/src/commands/appDesktop.ts +326 -0
  11. package/src/commands/appDesktopMac.ts +470 -0
  12. package/src/commands/appIcon.ts +187 -0
  13. package/src/commands/appShared.ts +448 -0
  14. package/src/commands/appTemplates.ts +883 -0
  15. package/src/commands/appTemplatesAndroid.ts +599 -0
  16. package/src/commands/appTemplatesGradlew.ts +9 -0
  17. package/src/commands/assets.ts +28 -0
  18. package/src/commands/clone.ts +60 -0
  19. package/src/commands/compile.ts +253 -0
  20. package/src/commands/create.ts +59 -0
  21. package/src/commands/design.ts +863 -0
  22. package/src/commands/designTemplates.ts +11 -0
  23. package/src/commands/desktop.ts +122 -0
  24. package/src/commands/dev.ts +214 -0
  25. package/src/commands/diff.ts +59 -0
  26. package/src/commands/init.ts +191 -0
  27. package/src/commands/install.ts +198 -0
  28. package/src/commands/lightmap.ts +290 -0
  29. package/src/commands/link.ts +147 -0
  30. package/src/commands/login.ts +24 -0
  31. package/src/commands/navmesh.ts +225 -0
  32. package/src/commands/pn.ts +305 -0
  33. package/src/commands/projectTemplates.ts +129 -0
  34. package/src/commands/pull.ts +109 -0
  35. package/src/commands/push.ts +173 -0
  36. package/src/commands/render.ts +414 -0
  37. package/src/commands/scene.ts +148 -0
  38. package/src/commands/shaders.ts +157 -0
  39. package/src/commands/status.ts +29 -0
  40. package/src/commands/test.ts +370 -0
  41. package/src/commands/thumbs.ts +178 -0
  42. package/src/commands/types.ts +87 -0
  43. package/src/commands/update.ts +190 -0
  44. package/src/compile/assetIcons.ts +214 -0
  45. package/src/compile/collect.ts +46 -0
  46. package/src/compile/collectLocal.ts +37 -0
  47. package/src/compile/designCompile.ts +104 -0
  48. package/src/compile/fonts.ts +156 -0
  49. package/src/compile/headlessBundle.ts +129 -0
  50. package/src/compile/nativeStack.ts +21 -0
  51. package/src/compile/projectCompile.ts +109 -0
  52. package/src/compile/sceneCompile.ts +113 -0
  53. package/src/compile/screenEntry.ts +127 -0
  54. package/src/compile/shaders.ts +245 -0
  55. package/src/config.ts +42 -0
  56. package/src/designMeta.ts +35 -0
  57. package/src/desktopRenderer.ts +532 -0
  58. package/src/desktopScript.ts +276 -0
  59. package/src/dev/clientTemplates.ts +160 -0
  60. package/src/dev/devServer.ts +292 -0
  61. package/src/dev/wsServer.ts +144 -0
  62. package/src/distRoot.ts +20 -0
  63. package/src/ignore.ts +163 -0
  64. package/src/index.ts +491 -0
  65. package/src/lecodes-3d-editor.d.ts +41 -0
  66. package/src/lecodes-assets.d.ts +7 -0
  67. package/src/lecodes-design.d.ts +191 -0
  68. package/src/lecodes-renderer.d.ts +131 -0
  69. package/src/localFiles.ts +144 -0
  70. package/src/manifest.ts +46 -0
  71. package/src/matcTool.ts +137 -0
  72. package/src/peers.ts +40 -0
  73. package/src/project.ts +61 -0
  74. package/src/projectEnv.ts +94 -0
  75. package/src/qrcode-terminal.d.ts +9 -0
  76. package/src/releases.ts +125 -0
  77. package/src/serverDiff.ts +78 -0
  78. package/src/textDiff.ts +103 -0
  79. package/src/types.ts +0 -0
  80. package/src/util.ts +146 -0
  81. package/runtime/sdk/animate/animate.ts +0 -238
  82. package/runtime/sdk/animate/bezier.ts +0 -138
  83. package/runtime/sdk/animate/easings.ts +0 -126
  84. package/runtime/sdk/canvas/Canvas.ts +0 -305
  85. package/runtime/sdk/core/Aspect.ts +0 -512
  86. package/runtime/sdk/core/InspectorUI.ts +0 -212
  87. package/runtime/sdk/core/color.ts +0 -66
  88. package/runtime/sdk/core/compWrite.ts +0 -42
  89. package/runtime/sdk/core/events.ts +0 -38
  90. package/runtime/sdk/core/fields.ts +0 -120
  91. package/runtime/sdk/core/registry.ts +0 -23
  92. package/runtime/sdk/core/signals.ts +0 -277
  93. package/runtime/sdk/core/time.ts +0 -81
  94. package/runtime/sdk/g2/Camera2D.ts +0 -40
  95. package/runtime/sdk/g2/CharacterController2D.ts +0 -276
  96. package/runtime/sdk/g2/Node2D.ts +0 -267
  97. package/runtime/sdk/g2/OneWay2D.ts +0 -66
  98. package/runtime/sdk/g2/Physics2D.ts +0 -346
  99. package/runtime/sdk/g2/Scene2D.ts +0 -209
  100. package/runtime/sdk/g2/Shape2D.ts +0 -259
  101. package/runtime/sdk/g2/Sprite.ts +0 -89
  102. package/runtime/sdk/g2/SpriteAnimation.ts +0 -171
  103. package/runtime/sdk/g2/SpriteSheet.ts +0 -166
  104. package/runtime/sdk/g2/Texture2D.ts +0 -47
  105. package/runtime/sdk/g2/Tilemap.ts +0 -41
  106. package/runtime/sdk/g2/Tileset.ts +0 -71
  107. package/runtime/sdk/g2/Trigger2D.ts +0 -77
  108. package/runtime/sdk/g2/autotile.ts +0 -433
  109. package/runtime/sdk/g2/cells.ts +0 -91
  110. package/runtime/sdk/g2/defineScene2d.ts +0 -381
  111. package/runtime/sdk/g2/groups2d.ts +0 -106
  112. package/runtime/sdk/g2/loop.ts +0 -50
  113. package/runtime/sdk/g2/scenarios2d.ts +0 -69
  114. package/runtime/sdk/g2/touch.ts +0 -83
  115. package/runtime/sdk/gl/Camera.ts +0 -160
  116. package/runtime/sdk/gl/CameraPlace.ts +0 -52
  117. package/runtime/sdk/gl/CharacterController.ts +0 -238
  118. package/runtime/sdk/gl/Gearbox.ts +0 -212
  119. package/runtime/sdk/gl/Geometry.ts +0 -279
  120. package/runtime/sdk/gl/IK.ts +0 -193
  121. package/runtime/sdk/gl/InstancedMesh.ts +0 -132
  122. package/runtime/sdk/gl/Light.ts +0 -99
  123. package/runtime/sdk/gl/Lightmap.ts +0 -179
  124. package/runtime/sdk/gl/Material.ts +0 -245
  125. package/runtime/sdk/gl/Mesh.ts +0 -83
  126. package/runtime/sdk/gl/Model.ts +0 -64
  127. package/runtime/sdk/gl/Node.ts +0 -350
  128. package/runtime/sdk/gl/Noise.ts +0 -30
  129. package/runtime/sdk/gl/Particles.ts +0 -676
  130. package/runtime/sdk/gl/Physics.ts +0 -222
  131. package/runtime/sdk/gl/Plane.ts +0 -53
  132. package/runtime/sdk/gl/Ray.ts +0 -16
  133. package/runtime/sdk/gl/Scene.ts +0 -479
  134. package/runtime/sdk/gl/Shape.ts +0 -377
  135. package/runtime/sdk/gl/Texture.ts +0 -46
  136. package/runtime/sdk/gl/Trigger.ts +0 -45
  137. package/runtime/sdk/gl/Vehicle.ts +0 -473
  138. package/runtime/sdk/gl/Wheel.ts +0 -240
  139. package/runtime/sdk/gl/animation/AnimationClip.ts +0 -204
  140. package/runtime/sdk/gl/animation/Animator.ts +0 -87
  141. package/runtime/sdk/gl/animation/Layer.ts +0 -29
  142. package/runtime/sdk/gl/animation/Loop.ts +0 -25
  143. package/runtime/sdk/gl/animation/Playback.ts +0 -43
  144. package/runtime/sdk/gl/animation/core.ts +0 -294
  145. package/runtime/sdk/gl/controls.ts +0 -95
  146. package/runtime/sdk/gl/physicsEvents.ts +0 -20
  147. package/runtime/sdk/gl/scenarios.ts +0 -291
  148. package/runtime/sdk/gl/state.ts +0 -6
  149. package/runtime/sdk/gl/touch.ts +0 -68
  150. package/runtime/sdk/inject.ts +0 -186
  151. package/runtime/sdk/math/Mathf.ts +0 -118
  152. package/runtime/sdk/math/mat4.ts +0 -278
  153. package/runtime/sdk/math/quat.ts +0 -232
  154. package/runtime/sdk/math/vec.ts +0 -255
  155. package/runtime/sdk/plugins/camera.ts +0 -81
  156. package/runtime/sdk/plugins/geolocation.ts +0 -123
  157. package/runtime/sdk/plugins/oauth.ts +0 -61
  158. package/runtime/sdk/plugins/permission.ts +0 -7
  159. package/runtime/sdk/plugins/push.ts +0 -132
  160. package/runtime/sdk/plugins/qr.ts +0 -73
  161. package/runtime/sdk/plugins/service.ts +0 -47
  162. package/runtime/sdk/runtime/app.ts +0 -101
  163. package/runtime/sdk/runtime/appEvents.ts +0 -54
  164. package/runtime/sdk/runtime/channel.ts +0 -61
  165. package/runtime/sdk/runtime/clipboard.ts +0 -20
  166. package/runtime/sdk/runtime/datetime.ts +0 -329
  167. package/runtime/sdk/runtime/device.ts +0 -293
  168. package/runtime/sdk/runtime/fetch.ts +0 -77
  169. package/runtime/sdk/runtime/files.ts +0 -23
  170. package/runtime/sdk/runtime/input.ts +0 -175
  171. package/runtime/sdk/runtime/media.ts +0 -111
  172. package/runtime/sdk/runtime/misc.ts +0 -16
  173. package/runtime/sdk/runtime/net.ts +0 -36
  174. package/runtime/sdk/runtime/rpc.ts +0 -218
  175. package/runtime/sdk/runtime/service.ts +0 -83
  176. package/runtime/sdk/runtime/share.ts +0 -9
  177. package/runtime/sdk/runtime/storage.ts +0 -13
  178. package/runtime/sdk/runtime/touch.ts +0 -76
  179. package/runtime/sdk/scene/defineScene.ts +0 -1227
  180. package/runtime/sdk/scene/editorPlugins.ts +0 -92
  181. package/runtime/sdk/scene/gizmos.ts +0 -148
  182. package/runtime/sdk/scene/grammar.ts +0 -120
  183. package/runtime/sdk/scene/material.ts +0 -188
  184. package/runtime/sdk/server/auth/appConfig.ts +0 -12
  185. package/runtime/sdk/server/auth/global.ts +0 -80
  186. package/runtime/sdk/server/auth/host.ts +0 -318
  187. package/runtime/sdk/server/auth/models.ts +0 -83
  188. package/runtime/sdk/server/auth/types.ts +0 -50
  189. package/runtime/sdk/server/channel.ts +0 -56
  190. package/runtime/sdk/server/context.ts +0 -36
  191. package/runtime/sdk/server/db/defineDb.ts +0 -237
  192. package/runtime/sdk/server/db/fields.ts +0 -132
  193. package/runtime/sdk/server/db/httpTransport.ts +0 -93
  194. package/runtime/sdk/server/db/index.ts +0 -7
  195. package/runtime/sdk/server/db/marci/query.ts +0 -412
  196. package/runtime/sdk/server/db/types.ts +0 -202
  197. package/runtime/sdk/server/errors.ts +0 -12
  198. package/runtime/sdk/server/host.ts +0 -74
  199. package/runtime/sdk/server/inject.ts +0 -13
  200. package/runtime/sdk/server/runtime.ts +0 -133
  201. package/runtime/sdk/server/validate.ts +0 -87
  202. package/runtime/sdk/ui/NativeView.ts +0 -142
  203. package/runtime/sdk/ui/UI.ts +0 -39
  204. package/runtime/sdk/ui/UIBottomSheet.ts +0 -139
  205. package/runtime/sdk/ui/UIButton.ts +0 -101
  206. package/runtime/sdk/ui/UIContainer.ts +0 -60
  207. package/runtime/sdk/ui/UIImage.ts +0 -83
  208. package/runtime/sdk/ui/UIInput.ts +0 -185
  209. package/runtime/sdk/ui/UIModal.ts +0 -139
  210. package/runtime/sdk/ui/UINode.ts +0 -826
  211. package/runtime/sdk/ui/UIPager.ts +0 -362
  212. package/runtime/sdk/ui/UIPopover.ts +0 -100
  213. package/runtime/sdk/ui/UIScreen.ts +0 -123
  214. package/runtime/sdk/ui/UIScrollable.ts +0 -87
  215. package/runtime/sdk/ui/UISpacer.ts +0 -14
  216. package/runtime/sdk/ui/UITabs.ts +0 -236
  217. package/runtime/sdk/ui/UIText.ts +0 -51
  218. package/runtime/sdk/ui/UIVideo.ts +0 -88
  219. package/runtime/sdk/ui/UIVirtualizedList.ts +0 -241
  220. package/runtime/sdk/ui/UIWidget.ts +0 -127
  221. package/runtime/sdk/ui/fonts.ts +0 -13
  222. package/runtime/sdk/ui/presentable.ts +0 -117
  223. package/runtime/sdk/ui/router.ts +0 -132
  224. package/runtime/sdk/ui/theme.ts +0 -84
@@ -1,479 +0,0 @@
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, _installTimeScale, _attachSystem, _detachSystem, _detachAllSystems, type Aspect, type AspectCtor, type FieldOf, type TargetOf } from "../core/Aspect"
10
- import { _bumpNavEpoch, _navEpoch, _navSupported, _setCurrent, Presentable, type PresentOptions } from "../ui/presentable"
11
- import { device } from "../runtime/device"
12
- import type { ClickEvent, TouchStartEvent } from "../runtime/touch"
13
- import type { FetchResponse } from "../runtime/fetch"
14
- import { _requestCameraPermission } from "../plugins/permission"
15
- import { Camera } from "./Camera"
16
- import { attachControls, type ControlsHandle, type ControlsOptions } from "./controls"
17
- import { Material } from "./Material"
18
- import { Node, nodeRegistry } from "./Node"
19
- import { glState } from "./state"
20
- import { registerTouchEndEvent, registerTouchStartEvent } from "./touch"
21
-
22
- export type AmbientOcclusionOptions = {
23
- /** Strength of the darkening (default 1). */
24
- intensity?: number
25
- /** How far the occlusion reaches, in metres (default 0.3). */
26
- radius?: number
27
- /** Falloff contrast; >1 tightens it into the crease (default 1). */
28
- power?: number
29
- /** Sample count + filtering (default 'medium'). Not the buffer resolution — that stays half. */
30
- quality?: "low" | "medium" | "high" | "ultra"
31
- }
32
-
33
- export type FogOptions = {
34
- /** A TINT on the in-scattered ambient, not an absolute colour: the engine multiplies it by the
35
- * environment luminance, so white (the default) means "fog as bright as the ambient" and the fog
36
- * brightens with `environmentIntensity`. Do NOT expect the same hex to look like it does in
37
- * `skybox` — that one IS an absolute radiance and reads roughly an order of magnitude brighter.
38
- * Tint towards the sky's hue; leave it white to sit at the ambient level. */
39
- color?: ColorInput
40
- /** Metres from the camera before the fog starts (default 0). */
41
- start?: number
42
- /** Extinction per metre at `height`; 0.01 ≈ clearly visible over ~100 m (default 0.01). */
43
- density?: number
44
- /** The fog's "sea level" in world Y (default 0). */
45
- height?: number
46
- /** How fast it thins with altitude, 1/m. 0 = uniform everywhere; higher = a ground-hugging layer
47
- * you can see over (default 0). */
48
- heightFalloff?: number
49
- /** Cap on how opaque it can get, 0–1 — keeps far shapes readable (default 1). */
50
- maxOpacity?: number
51
- /** Metres after which fog stops applying; 0 = everywhere (default 0). */
52
- cutoff?: number
53
- /** Take the colour from the environment in the view direction, tinted by `color`, instead of a
54
- * flat `color`. Convincing when the IBL is a real sky (default false). */
55
- fromEnvironment?: boolean
56
- }
57
-
58
- export type SceneOptions = {
59
- /** Image-based ambient lighting (default on). */
60
- ibl?: boolean
61
- /** IBL intensity (default 20000). */
62
- environmentIntensity?: number
63
- /** Bloom post-processing. */
64
- bloom?: boolean
65
- bloomIntensity?: number
66
- /** Tone mapping operator. `'aces'` (default, filament's ACES legacy) desaturates bright colours
67
- * towards white — HDR fire reads pale; `'neutral'` (Khronos PBR Neutral) keeps hue and
68
- * saturation until very bright; `'linear'` clips each channel (what an engine without a
69
- * tonemapper shows — saturated, Unity-without-post-processing look); `'filmic'` (Uncharted). */
70
- toneMapping?: "aces" | "neutral" | "linear" | "filmic"
71
- /** The sky. Three forms:
72
- * - a colour — a flat clear colour;
73
- * - `{ texture }` — a KTX1 **cubemap**, the sharp `<name>_skybox.ktx` that filament's `cmgen`
74
- * produces from an equirectangular .hdr/.exr. This is the one to use for a real sky. Pass an
75
- * `asset('../assets/sky.ktx')` handle or a bare staged filename;
76
- * - `'environment'` — reuse the scene's IBL cubemap. Cheapest (no second texture) but that file
77
- * is prefiltered for roughness, so the sky comes out soft; fine as a fallback, not as the goal.
78
- *
79
- * Either texture form draws through filament's own skybox pass: a full-screen pass in device
80
- * space after the opaque queue. No geometry, no meridian seam, no pole distortion — do NOT build
81
- * a sky dome or a fullscreen equirect material by hand, both are strictly worse. */
82
- skybox?: ColorInput | "environment" | { texture: string }
83
- /** Screen-space ambient occlusion — the contact darkening in creases and where props meet the
84
- * ground. Without it an IBL lights a crease exactly as brightly as an open face, so everything
85
- * reads as pasted onto the floor rather than standing on it. `true` takes defaults tuned for
86
- * human-scale props; `radius` is world-space metres and is the one knob that must follow the
87
- * scene's scale (~0.3 for objects on a table, ~0.6-1 for a yard of crates and containers). */
88
- ambientOcclusion?: boolean | AmbientOcclusionOptions
89
- /** Distance fog / aerial perspective: distant geometry loses contrast so the eye reads depth, and
90
- * the hard edge where a finite level ends against the skybox goes away. By default the fog applies
91
- * at every distance — the skybox included — so the sky itself takes the fog colour and the horizon
92
- * blends on its own; `cutoff` opts geometry beyond a distance back out. */
93
- fog?: FogOptions
94
- /** Multisample anti-aliasing: `true` = 4×, `2`/`4` = that many samples, `false` = off (FXAA
95
- * takes over). Unset keeps the host default (desktop 4×, mobile/web off). The biggest single
96
- * fill-rate cost after resolution — turn it down on big screens before anything else. */
97
- antialias?: boolean | 2 | 4
98
- /** Render the 3D at this fraction of the viewport (0.25–1) and upscale; the UI stays at native
99
- * resolution. A fixed, predictable cut of per-pixel GPU work — `0.75` is ~45 % cheaper and
100
- * barely visible in motion, `0.5` quarters it. Headless renders ignore it. */
101
- renderScale?: number
102
- /** Let the engine shrink the 3D buffers under `renderScale` only when frames run over budget
103
- * (Filament dynamic resolution, sharpened upscale) down to `min` (default 0.5). Off by default:
104
- * it makes the output frame-time dependent, so flow tests / headless renders never enable it. */
105
- dynamicResolution?: boolean | { min?: number }
106
- /** Anisotropic filtering, 1 (off) … 16. Default 2. What it buys is detail on surfaces seen at a
107
- * GRAZING angle — a first-person weapon, a floor, a wall — where the sample footprint is far
108
- * longer in one axis than the other and isotropic filtering has to pick a mip for the long one,
109
- * several levels coarser than the short axis deserves. Measured on a weapon seen from the side
110
- * (local contrast): 8.18 at 1× → 10.02 at 2× → 10.84 at 4× → 11.29 at 8×. The default is 2
111
- * because it is where the curve is steepest per unit of memory bandwidth; raise it if you have
112
- * the headroom.
113
- *
114
- * It is an ENGINE-WIDE default, not a property of this scene: a sampler is baked when its
115
- * texture is bound, so this reaches the models a scene loads AFTER it (which is every model in a
116
- * scene file — `env` is applied before the nodes build) and leaves already-loaded ones alone.
117
- * On the desktop host `CREATOR_TEXTURE_ANISOTROPY` overrides it, for tuning without a rebuild. */
118
- anisotropy?: number
119
- /** The look on an HDR display (a screen with headroom above SDR white — Apple XDR panels, the
120
- * macOS host today); ignored on SDR. `strength` 0..1 is how much of the picture reaches for the
121
- * display's headroom (0 only what SDR clipped, 1 nearly everything; default 0.35). `paperWhite`
122
- * 1..8 is where white lands as a multiple of SDR white — the "HDR brightness" of a console
123
- * calibration screen, where 1.5–2 is the norm; default 1 keeps a white wall at the UI's white,
124
- * faithful but dim next to what "HDR on" is expected to look like. Engine-wide like `anisotropy`;
125
- * `device.hdr` is the runtime form and tells you whether the display has any headroom at all. */
126
- hdr?: { strength?: number; paperWhite?: number }
127
- }
128
-
129
- // Route aspect update(dt) through the render-synced native phases (render() early = before physics,
130
- // late = after animations / before draw), so followers/cameras are in lockstep with the draw. Done from
131
- // the 3D layer (not core) so a 2D-only bundle never pulls in the 3D bridge. Installed once, lazily on
132
- // the first Scene. On an older wasm without the hooks the installer returns false → setLoop fallback.
133
- let framesInstalled = false
134
- const installRenderSyncedFrames = (): void => {
135
- if (framesInstalled) return
136
- framesInstalled = true
137
- _installAspectFrames((early, late) => {
138
- if (typeof _creator.setEarlyUpdate !== "function") return false
139
- // the frame stamp invalidates every Node's local-matrix cache once per frame (physics / character
140
- // controllers / animation rewrite transforms natively between frames)
141
- _creator.setEarlyUpdate((dt: number) => { glState.frame++; early(dt) })
142
- _creator.setLateUpdate(late)
143
- return true
144
- })
145
- // Time.scale / Time.paused reach the engine's own clocks (physics, animators, particles) through
146
- // setTimeScale; a host without it keeps running its sims at wall-clock speed (feature-detected).
147
- _installTimeScale((scale) => { _creator.setTimeScale?.(scale) })
148
- }
149
-
150
- export class Scene implements Presentable {
151
- /** @internal native scene handle. */
152
- readonly _id: number
153
- /** @internal the IBL intensity this scene was built with (Lightmap.load's ambient floor). */
154
- _environmentIntensity = 20000
155
- readonly camera: Camera
156
-
157
- /** @internal touch listeners, read by the dispatch system. */
158
- readonly _clickListeners: Array<(ev: ClickEvent<Node | null>) => void> = []
159
- readonly _touchStartListeners: Array<(ev: TouchStartEvent<Node | null>) => void> = []
160
-
161
- private _material?: Material
162
-
163
- private static _active: Scene | null = null
164
- static get active(): Scene | null { return Scene._active }
165
-
166
- constructor(options: SceneOptions = {}) {
167
- installRenderSyncedFrames()
168
- const parent = (options as any)._parent as Scene | undefined
169
- if (parent) {
170
- this.camera = parent.camera
171
- this._id = _creator.createOverlayScene(parent._id)
172
- // No camera re-attach: the native createOverlayScene wires the parent's camera into the
173
- // overlay view. Re-attaching would (a) point camera._sceneId — the source for getRay /
174
- // getViewDirection / fov — at the overlay's view, whose viewport/projection aren't
175
- // maintained like the render view's, and (b) create a second native camera component on
176
- // the shared entity. (Found via the scene editor: gizmo pick rays came out distorted.)
177
- } else {
178
- this.camera = new Camera()
179
- this._id = _creator.createScene()
180
- this.camera._attach(this._id)
181
- }
182
-
183
- if (options.ibl !== false) _creator.setDefaultIbl(this._id, options.environmentIntensity ?? 20000)
184
- this._environmentIntensity = options.ibl === false ? 0 : (options.environmentIntensity ?? 20000)
185
- if (options.bloom) _creator.setBloomOptions(this._id, true, options.bloomIntensity ?? 0.2, 1)
186
- if (options.toneMapping !== undefined && options.toneMapping !== "aces") {
187
- _creator.setToneMapping?.(this._id, { neutral: 1, linear: 2, filmic: 3 }[options.toneMapping] ?? 0)
188
- }
189
- if (options.skybox !== undefined) this.skybox = options.skybox
190
- if (options.ambientOcclusion) this.setAmbientOcclusion(options.ambientOcclusion)
191
- if (options.fog) this.setFog(options.fog)
192
- if (options.antialias !== undefined) {
193
- const a = options.antialias
194
- // `false` must reach the host: desktop defaults to 4× MSAA, so an explicit off is a real change.
195
- _creator.setSceneMultiSampleAntiAliasing(this._id, a !== false, typeof a === "number" ? a : 4)
196
- }
197
- // Before anything else in this constructor that could load a texture: the value has to be in
198
- // place by the time assets bind their samplers.
199
- if (options.anisotropy !== undefined) _creator.setTextureAnisotropy?.(options.anisotropy)
200
- if (options.hdr) {
201
- if (options.hdr.strength !== undefined) device.hdr.strength = options.hdr.strength
202
- if (options.hdr.paperWhite !== undefined) device.hdr.paperWhite = options.hdr.paperWhite
203
- }
204
- if (options.renderScale !== undefined || options.dynamicResolution) {
205
- this.setRenderOptions(options.renderScale ?? 1, options.dynamicResolution ?? false)
206
- }
207
- }
208
-
209
- /** Runtime form of `renderScale` / `dynamicResolution` (a graphics-settings menu). `renderScale`
210
- * 0.25–1; older hosts without the method ignore it. */
211
- setRenderOptions(renderScale: number, dynamicResolution: boolean | { min?: number } = false): void {
212
- const min = typeof dynamicResolution === "object" ? (dynamicResolution.min ?? 0.5) : 0.5
213
- _creator.setSceneRenderOptions?.(this._id, renderScale, !!dynamicResolution, min)
214
- }
215
-
216
- /** Scene occlusion material. */
217
- get occlusionMaterial(): Material {
218
- if (!this._material) this._material = new Material(_creator.getSceneMaterial(this._id))
219
- return this._material
220
- }
221
-
222
- set skybox(sky: ColorInput | "environment" | { texture: string }) {
223
- if (sky === "environment") { _creator.setSkyboxFromEnvironment?.(this._id); return }
224
- if (typeof sky === "object" && sky !== null && "texture" in sky) {
225
- const src = sky.texture
226
- // `asset()` hands back "id:<n>" (already staged); a bare name is resolved on the spot.
227
- const id = src.startsWith("id:") ? Number(src.slice(3)) : _creatorUtils.fetchLocal(src)
228
- _creator.setSkyboxTexture?.(this._id, id)
229
- return
230
- }
231
- _creator.setSkybox(this._id, Color.toPackedRgb(sky))
232
- }
233
-
234
- /** Runtime form of `ambientOcclusion` (a graphics-settings menu). `false` turns it off. */
235
- setAmbientOcclusion(options: boolean | AmbientOcclusionOptions): void {
236
- const o: AmbientOcclusionOptions = typeof options === "object" ? options : {}
237
- const quality = { low: 0, medium: 1, high: 2, ultra: 3 }[o.quality ?? "medium"] ?? 1
238
- _creator.setAmbientOcclusionOptions?.(this._id, options !== false, o.intensity ?? 1, o.radius ?? 0.3, o.power ?? 1, quality)
239
- }
240
-
241
- /** Runtime form of `fog` (weather, entering a building). `false` turns it off. */
242
- setFog(options: FogOptions | false): void {
243
- const o: FogOptions = options === false ? {} : options
244
- _creator.setFogOptions?.(
245
- this._id, options !== false, Color.toPackedRgb(o.color ?? "#ffffff"),
246
- o.start ?? 0, o.density ?? 0.01, o.height ?? 0, o.heightFalloff ?? 0,
247
- o.maxOpacity ?? 1, o.cutoff ?? 0, o.fromEnvironment ?? false,
248
- )
249
- }
250
-
251
- setMaterialGlobalParameter(i: number, x: number, y: number, z: number, w: number): void {
252
- _creator.setMaterialGlobalParameter(this._id, i, x, y, z, w)
253
- }
254
- setAntialias(enabled: boolean, scale = 4): void {
255
- _creator.setSceneMultiSampleAntiAliasing(this._id, enabled, scale)
256
- }
257
-
258
- add(...nodes: Node[]): this {
259
- for (const n of nodes) _creator.addEntityToScene(this._id, n.id)
260
- return this
261
- }
262
- remove(...nodes: Node[]): this {
263
- for (const n of nodes) _creator.removeEntityFromScene(this._id, n.id)
264
- return this
265
- }
266
-
267
- // ---- systems: aspects of the scene (core/Aspect.ts `System`) ----
268
- /** @internal class → instance, the authoritative store (named accessors mirror this). */
269
- readonly _systems = new Map<Function, Aspect<any, any, any>>()
270
-
271
- /** Attach (and configure) a system, or reconfigure it if already present. Returns the scene typed
272
- * as now-having it (`scene.system(Hud).hud`). A `System<'x', Scene2D>` is rejected here. */
273
- // `this: Self` (not `Self & Scene`): with the intersection TS infers Self = Scene and a chain
274
- // `scene.system(A).system(B)` loses A's accessor from the result type.
275
- system<Self extends TargetOf<A>, A extends Aspect<any, any, any>>(
276
- this: Self,
277
- ctor: AspectCtor<A>,
278
- opts?: Partial<A>,
279
- ): Self & FieldOf<A> {
280
- return _attachSystem(this as unknown as Scene, ctor, opts) as unknown as Self & FieldOf<A>
281
- }
282
- /** Safe access — undefined if the system isn't attached. */
283
- get<A extends Aspect<any, any, any>>(ctor: AspectCtor<A>): A | undefined {
284
- return this._systems.get(ctor) as A | undefined
285
- }
286
- /** Existence check AND type guard: inside `if (scene.has(Hud))`, `scene.hud` is present. */
287
- has<A extends Aspect<any, any, any>>(ctor: AspectCtor<A>): this is this & FieldOf<A> {
288
- return this._systems.has(ctor)
289
- }
290
- /** Detach a system (runs its onDetach). Chainable. */
291
- removeSystem<A extends Aspect<any, any, any>>(ctor: AspectCtor<A>): this {
292
- _detachSystem(this, ctor)
293
- return this
294
- }
295
- /** Tear the scene down: every system detaches (last-attached first) and the scene closes. Nodes
296
- * are yours — destroy the ones you own; the native scene object itself is not released. */
297
- destroy(): void {
298
- _detachAllSystems(this)
299
- this.close()
300
- }
301
-
302
- createOverlay(options: SceneOptions = {}): Scene {
303
- return new Scene({ ...options, _parent: this } as any)
304
- }
305
-
306
- warmRender(): Promise<void> {
307
- return new Promise<void>((res) => _creator.warmRender(this._id, res))
308
- }
309
-
310
- // ---- Presentable (docs/navigation-presentable-plan.md) ----
311
- // A scene is a destination like a screen: opening it replaces whatever is visible (suspending
312
- // an active Router until Router.restore()), and it can be pushed onto the Router stack. The
313
- // scene graph survives while covered — only the view detaches and rendering pauses.
314
-
315
- /** @internal presentation lifecycle — fired by the host on present/dismiss (new navigation),
316
- * or synchronously by open()/close() on legacy hosts. */
317
- readonly ol: (() => void)[] = []
318
- readonly cl: (() => void)[] = []
319
- _backButtonCallback?: () => void
320
- private _vd?: object
321
-
322
- /** Fires when the scene becomes the visible destination (incl. a pop revealing it). */
323
- onOpen(callback: () => void): this {
324
- this.ol.push(callback)
325
- return this
326
- }
327
- /** Fires when the scene stops being visible (closed, replaced, or covered by a push). */
328
- onClose(callback: () => void): this {
329
- this.cl.push(callback)
330
- return this
331
- }
332
- onBackPressed(callback: () => void): this {
333
- this._backButtonCallback = callback
334
- return this
335
- }
336
-
337
- /** @internal stable wire descriptor; hosts hand it back via router onChange (`_p` = this). */
338
- _viewDesc(): object {
339
- return this._vd ??= { type: "scene3d", sceneId: this._id, _p: this }
340
- }
341
-
342
- /** Make this the active scene — shows it as the current destination (replaces a screen /
343
- * another scene; only the active scene renders). */
344
- open(options?: PresentOptions): void | Promise<void> {
345
- _bumpNavEpoch()
346
- if (_navSupported()) {
347
- // The host activates the engine (openScene) as part of presenting the destination.
348
- _creatorUI.openView!(this._viewDesc(), options?.transition ?? "none")
349
- } else {
350
- _creator.openScene(this._id)
351
- for (const cb of this.ol) cb()
352
- }
353
- Scene._active = this
354
- glState.activeScene = this
355
- _setCurrent(this)
356
- }
357
- close(): void {
358
- _bumpNavEpoch()
359
- if (_navSupported()) {
360
- _creatorUI.closeView!()
361
- } else {
362
- _creator.closeScene()
363
- for (const cb of this.cl) cb()
364
- }
365
- if (Scene._active === this) Scene._active = null
366
- if (glState.activeScene === this) glState.activeScene = null
367
- if (Presentable.current === this) _setCurrent(null)
368
- }
369
-
370
- addEventListener(channel: "click", callback: (ev: ClickEvent<Node | null>) => void): void
371
- addEventListener(channel: "touchstart", callback: (ev: TouchStartEvent<Node | null>) => void): void
372
- addEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
373
- if (channel === "click") {
374
- if (!this._clickListeners.includes(callback)) { this._clickListeners.push(callback); registerTouchEndEvent() }
375
- } else if (channel === "touchstart") {
376
- if (!this._touchStartListeners.includes(callback)) { this._touchStartListeners.push(callback); registerTouchStartEvent() }
377
- }
378
- }
379
- removeEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
380
- const list = channel === "click" ? this._clickListeners : this._touchStartListeners
381
- const i = list.indexOf(callback)
382
- if (i >= 0) list.splice(i, 1)
383
- }
384
- }
385
-
386
- // --- AR ---
387
-
388
- export type ARMode = "default" | "markers" | "arcore" | "detached"
389
-
390
- const onTrackStateChange = (entityId: number, track: boolean): void => {
391
- const node = nodeRegistry.get(entityId)
392
- if (!node) return
393
- node.isTracked = track
394
- if (track) node._dispatchEvent("track")
395
- else node._dispatchEvent("untrack")
396
- }
397
-
398
- export class ARScene<T extends ARMode = "default"> extends Scene {
399
- readonly mode: T
400
- useWarmRender = true
401
- private _root?: Node
402
-
403
- constructor(options: SceneOptions & { mode?: T } = {}) {
404
- super(options)
405
- this.mode = options.mode ?? ("default" as T)
406
- _creator.createARController(this._id, this.camera.id, this.mode, onTrackStateChange)
407
- }
408
-
409
- /** Root anchor node (not available in marker mode). */
410
- get root(): Node {
411
- if (this._root) return this._root
412
- this._root = new Node(_creator.createRootAnchor(this._id))
413
- return this._root
414
- }
415
-
416
- /** Create an anchor from a tracking source (e.g. an image target). */
417
- createAnchor(source: FetchResponse, physicalWidth = 0.2): Node {
418
- return new Node(_creator.createAnchor(this._id, physicalWidth, (source as any)._id))
419
- }
420
-
421
- /**
422
- * Let the user place & manipulate a node with touch gestures — the staple AR object-placement
423
- * interaction: ONE finger drags it across the floor (relative to where the camera looks), TWO fingers
424
- * pinch to scale and twist to rotate. Returns a handle; call `.remove()` to detach. Tunable via
425
- * options (min/max scale, and toggles for `pan` / `pinch` / `twist`).
426
- */
427
- addControls(target: Node, options: ControlsOptions = {}): ControlsHandle {
428
- return attachControls(this, target, options)
429
- }
430
-
431
- override async open(options?: PresentOptions): Promise<void> {
432
- await this._prepare()
433
- super.open(options)
434
- }
435
-
436
- /** @internal Presentable prepare hook: camera permission + warm render + AR session, all while
437
- * the previous destination (typically a loading screen) stays visible — the swap to this scene
438
- * happens only after it resolves. Warm render is headless on every platform (offscreen
439
- * swapchain), so no GL view is created here — the host mounts the scene view at present time.
440
- * Rejects on camera denial / AR launch failure, or if another navigation happened while
441
- * preparing (superseded) — in every rejection case the current destination stays visible. */
442
- async _prepare(): Promise<void> {
443
- const epoch = _navEpoch()
444
- await _requestCameraPermission()
445
- if (this.useWarmRender) await this.warmRender()
446
- await new Promise<void>((res, rej) => _creator.launchAR(this._id, res, rej))
447
- if (_navEpoch() !== epoch) {
448
- _creator.stopAR()
449
- throw new Error("ARScene.open() superseded: another destination was opened while preparing")
450
- }
451
- }
452
-
453
- override close(): void {
454
- _creator.stopAR()
455
- super.close()
456
- }
457
- }
458
-
459
- // --- VR ---
460
-
461
- /**
462
- * A 3D scene rendered in a VR headset (OpenXR — e.g. Meta Quest). The headset drives the camera:
463
- * every frame both eyes are rendered from the tracked head pose, so `scene.camera` position/lookAt
464
- * have no effect while the scene is open. The world origin is at floor level (stage space) —
465
- * author content with the ground at y = 0 and the user standing at the origin.
466
- *
467
- * `open()` rejects on hosts without a VR runtime ("VR is not supported on this device").
468
- */
469
- export class VRScene extends Scene {
470
- override async open(options?: PresentOptions): Promise<void> {
471
- await new Promise<void>((res, rej) => _creator.launchVR(this._id, res, rej))
472
- super.open(options)
473
- }
474
-
475
- override close(): void {
476
- _creator.stopVR()
477
- super.close()
478
- }
479
- }