lecodes-sdk 1.2.0 → 2.0.0

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 (260) hide show
  1. package/README.md +5 -2
  2. package/dist/editor.d.ts +12 -0
  3. package/dist/global.d.ts +4 -8
  4. package/dist/host.d.ts +2 -3
  5. package/dist/types/animate/tween/Animation.d.ts +0 -3
  6. package/dist/types/animate/tween/animateValue.d.ts +4 -2
  7. package/dist/types/animate/tween/easing.d.ts +8 -0
  8. package/dist/types/animate/tween/spec.d.ts +15 -7
  9. package/dist/types/audio/audio.d.ts +2 -1
  10. package/dist/types/canvas/Canvas.d.ts +40 -108
  11. package/dist/types/canvas/gen/cssColor.d.ts +17 -0
  12. package/dist/types/canvas/gen/recorder.d.ts +118 -0
  13. package/dist/types/canvas/gen/spec.d.ts +144 -0
  14. package/dist/types/core/color.d.ts +3 -1
  15. package/dist/types/core/pins.d.ts +18 -0
  16. package/dist/types/g2/Node2D.d.ts +5 -8
  17. package/dist/types/g2/Scene2D.d.ts +6 -2
  18. package/dist/types/gl/Foliage.d.ts +30 -7
  19. package/dist/types/gl/Light.d.ts +8 -0
  20. package/dist/types/gl/Lightmap.d.ts +13 -2
  21. package/dist/types/gl/Material.d.ts +17 -3
  22. package/dist/types/gl/Model.d.ts +6 -2
  23. package/dist/types/gl/Node.d.ts +3 -6
  24. package/dist/types/gl/Scene.d.ts +41 -13
  25. package/dist/types/gl/Texture.d.ts +1 -1
  26. package/dist/types/gl/animation/Locomotion.d.ts +8 -1
  27. package/dist/types/inject.d.ts +11 -11
  28. package/dist/types/inject.editor.d.ts +1 -0
  29. package/dist/types/net/core.d.ts +7 -0
  30. package/dist/types/plugin.d.ts +76 -0
  31. package/dist/types/plugins/gen/camera/sdk/camera.d.ts +24 -0
  32. package/dist/types/plugins/gen/camera/sdk/camera.gen.d.ts +25 -0
  33. package/dist/types/plugins/{geolocation.d.ts → gen/geolocation/sdk/geolocation.d.ts} +2 -20
  34. package/dist/types/plugins/gen/geolocation/sdk/geolocation.gen.d.ts +31 -0
  35. package/dist/types/plugins/{map.d.ts → gen/map/sdk/map.d.ts} +9 -50
  36. package/dist/types/plugins/gen/map/sdk/map.gen.d.ts +53 -0
  37. package/dist/types/plugins/gen/push/sdk/push.d.ts +23 -0
  38. package/dist/types/plugins/gen/push/sdk/push.gen.d.ts +35 -0
  39. package/dist/types/plugins/{qr.d.ts → gen/qr-scanner/sdk/qr-scanner.d.ts} +2 -3
  40. package/dist/types/plugins/gen/qr-scanner/sdk/qr-scanner.gen.d.ts +15 -0
  41. package/dist/types/runtime/app.d.ts +9 -2
  42. package/dist/types/runtime/fetch.d.ts +2 -0
  43. package/dist/types/runtime/input.d.ts +1 -1
  44. package/dist/types/runtime/media.d.ts +6 -10
  45. package/dist/types/runtime/misc.d.ts +4 -1
  46. package/dist/types/runtime/net.d.ts +3 -2
  47. package/dist/types/runtime/touch.d.ts +32 -0
  48. package/dist/types/scene/defineScene.d.ts +43 -2
  49. package/dist/types/scene/editor.d.ts +52 -0
  50. package/dist/types/scene/gizmos.d.ts +7 -4
  51. package/dist/types/ui/NativeView.d.ts +6 -4
  52. package/dist/types/ui/UI.d.ts +1 -1
  53. package/dist/types/ui/UIBottomSheet.d.ts +6 -12
  54. package/dist/types/ui/UIButton.d.ts +12 -14
  55. package/dist/types/ui/UIContainer.d.ts +0 -6
  56. package/dist/types/ui/UIImage.d.ts +1 -4
  57. package/dist/types/ui/UIInput.d.ts +8 -24
  58. package/dist/types/ui/UIModal.d.ts +0 -2
  59. package/dist/types/ui/UINode.d.ts +70 -56
  60. package/dist/types/ui/UIPager.d.ts +28 -27
  61. package/dist/types/ui/UIPopover.d.ts +0 -2
  62. package/dist/types/ui/UIScreen.d.ts +12 -17
  63. package/dist/types/ui/UIScrollable.d.ts +1 -4
  64. package/dist/types/ui/UIText.d.ts +0 -2
  65. package/dist/types/ui/UIVideo.d.ts +3 -5
  66. package/dist/types/ui/UIVirtualizedList.d.ts +14 -16
  67. package/dist/types/ui/UIWidget.d.ts +10 -9
  68. package/dist/types/ui/colorKeys.gen.d.ts +9 -0
  69. package/dist/types/ui/presentable.d.ts +46 -32
  70. package/dist/types/ui/router.d.ts +18 -7
  71. package/dist/types/ui/styleColor.d.ts +1 -0
  72. package/dist/types/ui/transitions.d.ts +18 -0
  73. package/dist/types/ui/tree.d.ts +75 -0
  74. package/dist/types/version.d.ts +10 -0
  75. package/dist/types.json +1 -1
  76. package/package.json +12 -3
  77. package/prompts/2d.md +2 -6
  78. package/prompts/3d.md +1 -5
  79. package/prompts/README.md +1 -1
  80. package/prompts/canvas.md +9 -8
  81. package/prompts/compose.ts +1 -1
  82. package/prompts/core.md +3 -3
  83. package/prompts/design.md +1 -1
  84. package/prompts/dist/2d-game.md +473 -239
  85. package/prompts/dist/3d-app.md +553 -205
  86. package/prompts/dist/ar-app.md +435 -202
  87. package/prompts/dist/design.md +113 -95
  88. package/prompts/dist/ui-app.md +386 -170
  89. package/prompts/ui-design.md +2 -3
  90. package/prompts/ui.md +45 -37
  91. package/src/animate/tween/Animation.ts +34 -150
  92. package/src/animate/tween/Timeline.ts +175 -175
  93. package/src/animate/tween/animateValue.ts +6 -3
  94. package/src/animate/tween/easing.ts +10 -3
  95. package/src/animate/tween/spec.ts +41 -15
  96. package/src/audio/Sound.ts +3 -3
  97. package/src/audio/audio.ts +2 -1
  98. package/src/bridges/2d.d.ts +317 -0
  99. package/src/bridges/app.d.ts +91 -0
  100. package/src/bridges/audio.d.ts +97 -0
  101. package/src/bridges/canvas.d.ts +79 -0
  102. package/src/bridges/device.d.ts +72 -0
  103. package/src/bridges/fetch.d.ts +80 -0
  104. package/src/bridges/files.d.ts +70 -0
  105. package/src/bridges/gl.d.ts +1133 -0
  106. package/src/bridges/input.d.ts +72 -0
  107. package/src/bridges/media.d.ts +55 -0
  108. package/src/bridges/nav.d.ts +71 -0
  109. package/src/bridges/net.d.ts +52 -0
  110. package/src/bridges/service.d.ts +52 -0
  111. package/src/bridges/socket.d.ts +31 -0
  112. package/src/bridges/storage.d.ts +33 -0
  113. package/src/bridges/tree.d.ts +301 -0
  114. package/src/bridges/types.d.ts +49 -0
  115. package/src/canvas/Canvas.ts +114 -159
  116. package/src/canvas/gen/cssColor.ts +224 -0
  117. package/src/canvas/gen/recorder.ts +212 -0
  118. package/src/canvas/gen/spec.ts +201 -0
  119. package/src/chisel.ts +193 -0
  120. package/src/compile/assetMacro.ts +1 -1
  121. package/src/compile/bundler.ts +11 -2
  122. package/src/compile/compileProject.ts +43 -4
  123. package/src/compile/fontMacro.ts +3 -4
  124. package/src/compile/header.ts +26 -5
  125. package/src/compile/index.ts +3 -1
  126. package/src/compile/liteMaterial.ts +1 -1
  127. package/src/compile/sceneEditor.ts +11 -26
  128. package/src/core/color.ts +73 -30
  129. package/src/core/pins.ts +51 -0
  130. package/src/core/signals.ts +8 -1
  131. package/src/g2/CharacterController2D.ts +3 -3
  132. package/src/g2/Node2D.ts +57 -39
  133. package/src/g2/Physics2D.ts +2 -2
  134. package/src/g2/Scene2D.ts +35 -23
  135. package/src/g2/Texture2D.ts +1 -1
  136. package/src/g2/loop.ts +4 -4
  137. package/src/gl/CameraPlace.ts +52 -52
  138. package/src/gl/Foliage.ts +72 -17
  139. package/src/gl/Geometry.ts +1 -2
  140. package/src/gl/Light.ts +10 -0
  141. package/src/gl/Lightmap.ts +45 -29
  142. package/src/gl/Material.ts +95 -51
  143. package/src/gl/Mesh.ts +120 -120
  144. package/src/gl/Model.ts +21 -16
  145. package/src/gl/Node.ts +91 -24
  146. package/src/gl/Particles.ts +1 -1
  147. package/src/gl/Scene.ts +100 -44
  148. package/src/gl/Texture.ts +8 -7
  149. package/src/gl/animation/AnimationClip.ts +1 -1
  150. package/src/gl/animation/DynamicBone.ts +482 -482
  151. package/src/gl/animation/Locomotion.ts +8 -3
  152. package/src/gl/nav/NavMesh.ts +3 -4
  153. package/src/gl/physics/Physics.ts +2 -2
  154. package/src/gl/physics/physicsEvents.ts +3 -3
  155. package/src/gl/scenarios.ts +291 -291
  156. package/src/gl/terrain/Terrain.ts +4 -5
  157. package/src/gl/touch.ts +14 -15
  158. package/src/host.d.ts +2 -3
  159. package/src/inject.editor.ts +7 -0
  160. package/src/inject.ts +13 -16
  161. package/src/net/core.ts +6 -5
  162. package/src/net/index.ts +1 -1
  163. package/src/net/replication.ts +1 -1
  164. package/src/plugin.ts +191 -0
  165. package/src/plugins/gen/camera/contract.d.ts +27 -0
  166. package/src/plugins/gen/camera/sdk/camera.gen.ts +46 -0
  167. package/src/plugins/gen/camera/sdk/camera.ts +57 -0
  168. package/src/plugins/gen/geolocation/contract.d.ts +50 -0
  169. package/src/plugins/gen/geolocation/sdk/geolocation.gen.ts +54 -0
  170. package/src/plugins/{geolocation.ts → gen/geolocation/sdk/geolocation.ts} +22 -43
  171. package/src/plugins/gen/map/contract.d.ts +144 -0
  172. package/src/plugins/gen/map/sdk/map.gen.ts +88 -0
  173. package/src/plugins/{map.ts → gen/map/sdk/map.ts} +68 -102
  174. package/src/plugins/gen/push/contract.d.ts +61 -0
  175. package/src/plugins/gen/push/sdk/push.gen.ts +60 -0
  176. package/src/plugins/gen/push/sdk/push.ts +105 -0
  177. package/src/plugins/gen/qr-scanner/contract.d.ts +16 -0
  178. package/src/plugins/gen/qr-scanner/sdk/qr-scanner.gen.ts +29 -0
  179. package/src/plugins/gen/qr-scanner/sdk/qr-scanner.ts +52 -0
  180. package/src/plugins/permission.ts +5 -4
  181. package/src/runtime/app.ts +20 -9
  182. package/src/runtime/appEvents.ts +5 -4
  183. package/src/runtime/channel.ts +18 -15
  184. package/src/runtime/clipboard.ts +4 -3
  185. package/src/runtime/datetime.ts +2 -1
  186. package/src/runtime/device.ts +17 -15
  187. package/src/runtime/fetch.ts +30 -20
  188. package/src/runtime/files.ts +16 -15
  189. package/src/runtime/input.ts +12 -10
  190. package/src/runtime/media.ts +50 -46
  191. package/src/runtime/misc.ts +7 -3
  192. package/src/runtime/net.ts +8 -7
  193. package/src/runtime/rpc.ts +1 -3
  194. package/src/runtime/service.ts +19 -14
  195. package/src/runtime/share.ts +4 -3
  196. package/src/runtime/storage.ts +6 -4
  197. package/src/runtime/touch.ts +32 -0
  198. package/src/scene/defineScene.ts +61 -365
  199. package/src/scene/editor.ts +408 -0
  200. package/src/scene/editorPlugins.ts +3 -3
  201. package/src/scene/gizmos.ts +15 -9
  202. package/src/server/db/marci/query.ts +1 -1
  203. package/src/server/host.ts +1 -1
  204. package/src/server/runtime.ts +1 -1
  205. package/src/ui/NativeView.ts +72 -25
  206. package/src/ui/UI.ts +3 -3
  207. package/src/ui/UIBottomSheet.ts +16 -17
  208. package/src/ui/UIButton.ts +54 -16
  209. package/src/ui/UIContainer.ts +0 -6
  210. package/src/ui/UIImage.ts +34 -30
  211. package/src/ui/UIInput.ts +29 -37
  212. package/src/ui/UIModal.ts +1 -3
  213. package/src/ui/UINode.ts +347 -297
  214. package/src/ui/UIPager.ts +93 -78
  215. package/src/ui/UIPopover.ts +0 -2
  216. package/src/ui/UIScreen.ts +41 -36
  217. package/src/ui/UIScrollable.ts +19 -13
  218. package/src/ui/UISpacer.ts +1 -1
  219. package/src/ui/UITabs.ts +8 -6
  220. package/src/ui/UIText.ts +7 -17
  221. package/src/ui/UIVideo.ts +30 -27
  222. package/src/ui/UIVirtualizedList.ts +58 -59
  223. package/src/ui/UIWidget.ts +38 -18
  224. package/src/ui/colorKeys.gen.ts +37 -0
  225. package/src/ui/fonts.ts +2 -2
  226. package/src/ui/presentable.ts +59 -42
  227. package/src/ui/router.ts +52 -35
  228. package/src/ui/styleColor.ts +56 -0
  229. package/src/ui/theme.ts +7 -6
  230. package/src/ui/transitions.ts +249 -0
  231. package/src/ui/tree.ts +346 -0
  232. package/src/version.ts +24 -0
  233. package/tests/helpers/engineWorld.ts +11 -0
  234. package/tests/helpers/fakeTree.ts +353 -0
  235. package/tests/helpers/hostStubs.ts +31 -0
  236. package/tests/helpers/index.ts +14 -0
  237. package/tests/helpers/memoryMarci.ts +124 -0
  238. package/tests/helpers/phases.ts +23 -0
  239. package/tests/helpers/preload.ts +18 -0
  240. package/tests/helpers/stubApp.ts +2 -0
  241. package/tests/helpers/stubDevice.ts +2 -0
  242. package/tests/helpers/stubFetch.ts +2 -0
  243. package/tests/helpers/stubInput.ts +2 -0
  244. package/dist/inject.js +0 -4629
  245. package/dist/types/core/registry.d.ts +0 -7
  246. package/dist/types/plugins/camera.d.ts +0 -25
  247. package/dist/types/plugins/push.d.ts +0 -46
  248. package/src/bridges.d.ts +0 -1769
  249. package/src/compile/__tests__/assetIconMacro.test.ts +0 -219
  250. package/src/compile/__tests__/assetMacro.test.ts +0 -100
  251. package/src/compile/__tests__/assetName.test.ts +0 -55
  252. package/src/compile/__tests__/compile.test.ts +0 -310
  253. package/src/compile/__tests__/detectEntry.test.ts +0 -151
  254. package/src/compile/__tests__/fontMacro.test.ts +0 -199
  255. package/src/compile/__tests__/serverSplit.test.ts +0 -27
  256. package/src/core/__tests__/stateMachine.test.ts +0 -132
  257. package/src/core/registry.ts +0 -23
  258. package/src/plugins/camera.ts +0 -81
  259. package/src/plugins/push.ts +0 -132
  260. package/src/plugins/qr.ts +0 -73
@@ -1,291 +1,291 @@
1
- // No-code scenario aspects: ready-made behaviors a scene builder attaches in the editor without
2
- // writing any code — move to a target, travel a waypoint path, spin, face something, start a GLB
3
- // animation. They are ordinary aspects (work from hand-written code too); in the scene editor the
4
- // movement ones double as `static editor` generators that draw their paths as editor gizmos
5
- // (`Gizmos.*` in rebuild() — overlay lines, never scene content; play mode never calls rebuild(),
6
- // so they cost nothing at runtime).
7
- //
8
- // Conventions shared by the family:
9
- // • targets/waypoints are scene nodes — place an Empty, then pick it in the inspector (`ref()`);
10
- // • times are seconds, angles degrees; all motion runs in `update(dt)` (play mode only);
11
- // • orientation is quaternion-composed — never euler-incremented (see math/quat.ts).
12
-
13
- import { Aspect } from "../core/Aspect"
14
- import type { FieldMeta } from "../core/fields"
15
- import type { InspectorUI } from "../core/InspectorUI"
16
- import { Vec3 } from "../math/vec"
17
- import { Quat } from "../math/quat"
18
- import { Node } from "./Node"
19
- import { Model } from "./Model"
20
- import type { Playback } from "./animation/Playback"
21
- import { Gizmos } from "../scene/gizmos"
22
-
23
- const MOVE_LINE = "#5b8ef0"
24
- const LOOK_LINE = "#d9a13f"
25
- const DEG2RAD = Math.PI / 180
26
-
27
- type LoopMode = "once" | "loop" | "pingpong"
28
-
29
- /** Normalized phase for elapsed time `t` over one trip of `duration`: once clamps at 1, loop
30
- * wraps, pingpong triangles 0→1→0. */
31
- const phase = (t: number, duration: number, mode: LoopMode): number => {
32
- if (duration <= 0) return 1
33
- const u = t / duration
34
- if (mode === "once") return Math.min(u, 1)
35
- if (mode === "loop") return u - Math.floor(u)
36
- const v = u % 2
37
- return v <= 1 ? v : 2 - v
38
- }
39
-
40
- const ease = (k: number, easing: "smooth" | "linear"): number =>
41
- easing === "smooth" ? k * k * (3 - 2 * k) : k
42
-
43
- /** A world point in `node`'s PARENT space — what `node.position` is expressed in. */
44
- const toParentLocal = (node: Node, world: Vec3): Vec3 => {
45
- const parent = node.parent
46
- return parent ? parent.worldMatrix.invert().transformPoint(world) : world
47
- }
48
-
49
- /** A path node's waypoints: its children in scene-FILE order (`_sceneChildOrder`, stamped by
50
- * defineScene — the engine's live child order is insertion-based and may differ), editor helper
51
- * nodes (`__`-prefixed) excluded. Runtime-added children append in engine order. */
52
- const pathWaypoints = (path: Node): Node[] => {
53
- const children = path.children.filter((c) => !(c.name ?? "").startsWith("__"))
54
- const order = (path as { _sceneChildOrder?: string[] })._sceneChildOrder
55
- if (!order) return children
56
- const byName = new Map(children.map((c) => [ c.name, c ]))
57
- const sorted: Node[] = []
58
- for (const n of order) { const c = byName.get(n); if (c) sorted.push(c) }
59
- const seen = new Set(sorted)
60
- for (const c of children) if (!seen.has(c)) sorted.push(c)
61
- return sorted
62
- }
63
-
64
- // ---- the aspects ---------------------------------------------------------------------------------
65
-
66
- /** Travel from the node's starting position to a target node in `duration` seconds. */
67
- export class MoveTo extends Aspect<"moveTo"> {
68
- /** Where to travel — place an Empty node and pick it. */
69
- target: Node | null = null
70
-
71
- /** Seconds for the full trip. */
72
- duration = 2
73
-
74
- /** Seconds to wait before starting. */
75
- delay = 0
76
-
77
- /** once = stop at the target · loop = restart from the start point · pingpong = back and forth. */
78
- mode: LoopMode = "once"
79
-
80
- /** Motion curve. */
81
- easing: "smooth" | "linear" = "smooth"
82
-
83
- static readonly aspect = "moveTo"
84
- static editor = { rebuild: true }
85
- static fields: FieldMeta<MoveTo> = {
86
- target: { editor: "node" },
87
- duration: { min: 0.05, max: 120, step: 0.05 },
88
- delay: { min: 0, max: 120, step: 0.05 },
89
- mode: { options: [ "once", "loop", "pingpong" ] },
90
- easing: { options: [ "smooth", "linear" ] },
91
- }
92
-
93
- private _t = 0
94
- private _start: Vec3 | null = null
95
-
96
- update(dt: number): void {
97
- if (!this.target) return
98
- this._start ??= this.node.position
99
- this._t += dt
100
- const k = ease(phase(Math.max(0, this._t - this.delay), this.duration, this.mode), this.easing)
101
- const end = toParentLocal(this.node, this.target.worldPosition)
102
- this.node.position = new Vec3(this._start).add(end.sub(this._start).scale(k))
103
- }
104
-
105
- /** Editor-only path line (edit mode; play mode never calls this). */
106
- rebuild(): void {
107
- if (!this.target) return
108
- // the target is an empty — its own anchored marker shows the endpoint
109
- Gizmos.line(this.node.worldPosition, this.target.worldPosition, { color: MOVE_LINE })
110
- }
111
- }
112
-
113
- /** Travel through the children of a path node (add an Empty per waypoint) in `duration` seconds.
114
- * `loop` runs the circuit closed (last → first); `pingpong` goes back and forth along the open
115
- * path; `once` stops at the last waypoint. */
116
- export class FollowPath extends Aspect<"followPath"> {
117
- /** A node whose CHILDREN are the waypoints, in file order. */
118
- path: Node | null = null
119
-
120
- /** Seconds for one full pass of the path. */
121
- duration = 6
122
-
123
- /** Seconds to wait before starting. */
124
- delay = 0
125
-
126
- mode: LoopMode = "loop"
127
-
128
- /** Turn to face the direction of travel. */
129
- orient = true
130
-
131
- /** Which local axis leads when orienting. */
132
- forward: "-z" | "z" | "x" | "-x" = "-z"
133
-
134
- static readonly aspect = "followPath"
135
- static editor = { rebuild: true }
136
- static fields: FieldMeta<FollowPath> = {
137
- path: { editor: "node" },
138
- duration: { min: 0.05, max: 300, step: 0.05 },
139
- delay: { min: 0, max: 120, step: 0.05 },
140
- mode: { options: [ "loop", "once", "pingpong" ] },
141
- forward: { options: [ "-z", "z", "x", "-x" ] },
142
- }
143
-
144
- private _t = 0
145
-
146
- private _points(): Vec3[] {
147
- if (!this.path) return []
148
- const points = pathWaypoints(this.path).map((c) => c.worldPosition)
149
- if (this.mode === "loop" && points.length > 1) points.push(points[0]!.clone())
150
- return points
151
- }
152
-
153
- update(dt: number): void {
154
- const points = this._points()
155
- if (points.length < 2) return
156
- this._t += dt
157
- const k = phase(Math.max(0, this._t - this.delay), this.duration, this.mode === "loop" ? "loop" : this.mode)
158
-
159
- const lengths: number[] = []
160
- let total = 0
161
- for (let i = 0; i < points.length - 1; i++) {
162
- const l = points[i + 1]!.sub(points[i]!).length()
163
- lengths.push(l)
164
- total += l
165
- }
166
- if (total <= 0) return
167
-
168
- let dist = k * total
169
- let i = 0
170
- while (i < lengths.length - 1 && dist > lengths[i]!) { dist -= lengths[i]!; i++ }
171
- const a = points[i]!
172
- const b = points[i + 1]!
173
- const seg = b.sub(a)
174
- const u = lengths[i]! > 0 ? Math.min(dist / lengths[i]!, 1) : 0
175
- const world = a.add(seg.scale(u))
176
- this.node.position = toParentLocal(this.node, world)
177
- if (this.orient && seg.lengthSq() > 1e-12) this.node.lookAt(world.add(seg), this.forward)
178
- }
179
-
180
- /** Editor-only waypoint polyline (edit mode; play mode never calls this). */
181
- rebuild(): void {
182
- const points = this._points() // loop mode already appends the first point again
183
- Gizmos.polyline(points, { color: MOVE_LINE }) // waypoints are empties: they carry their own markers
184
- }
185
- }
186
-
187
- /** Continuous rotation about one of the node's local axes, degrees per second. */
188
- export class Spin extends Aspect<"spin"> {
189
- /** Degrees per second. */
190
- speed = 45
191
-
192
- /** Local axis to spin around. */
193
- axis: "x" | "y" | "z" = "y"
194
-
195
- static readonly aspect = "spin"
196
- static fields: FieldMeta<Spin> = {
197
- speed: { min: -720, max: 720, step: 5 },
198
- axis: { options: [ "x", "y", "z" ] },
199
- }
200
-
201
- update(dt: number): void {
202
- const axis: [number, number, number] = this.axis === "x" ? [ 1, 0, 0 ] : this.axis === "y" ? [ 0, 1, 0 ] : [ 0, 0, 1 ]
203
- // compose in quaternion space about the LOCAL axis (q ⊗ dq) — never euler-increment
204
- this.node.quaternion = this.node.quaternion.mul(Quat.fromAxisAngle(axis, this.speed * dt * DEG2RAD))
205
- }
206
- }
207
-
208
- /** Keep the node facing a target node (a turret tracking, a signpost, a camera aimed at a hero).
209
- * Accessor `node.facing` — NOT `lookAt`, which would shadow the `Node.lookAt()` method. */
210
- export class LookAt extends Aspect<"facing"> {
211
- /** The node to face. */
212
- target: Node | null = null
213
-
214
- /** Which local axis points at the target. */
215
- forward: "-z" | "z" | "x" | "-x" = "-z"
216
-
217
- /** 0 = snap instantly; otherwise seconds of turn lag (bigger = slower, smoother). */
218
- smoothing = 0
219
-
220
- static readonly aspect = "facing"
221
- static editor = { rebuild: true }
222
- static fields: FieldMeta<LookAt> = {
223
- target: { editor: "node" },
224
- forward: { options: [ "-z", "z", "x", "-x" ] },
225
- smoothing: { min: 0, max: 5, step: 0.05 },
226
- }
227
-
228
- update(dt: number): void {
229
- if (!this.target) return
230
- const point = this.target.worldPosition
231
- if (this.smoothing <= 0) { this.node.lookAt(point, this.forward); return }
232
- const q0 = this.node.quaternion
233
- this.node.lookAt(point, this.forward)
234
- const q1 = this.node.quaternion
235
- this.node.quaternion = q0.slerp(q1, 1 - Math.exp(-dt / this.smoothing))
236
- }
237
-
238
- /** Editor-only sight line (edit mode; play mode never calls this). */
239
- rebuild(): void {
240
- if (!this.target) return
241
- Gizmos.line(this.node.worldPosition, this.target.worldPosition, { color: LOOK_LINE })
242
- }
243
- }
244
-
245
- /** Start a GLB animation clip when the scene runs. Attach to a `model:` node; the inspector card
246
- * lists the model's clips live and can preview them while editing. */
247
- export class PlayAnimation extends Aspect<"playAnimation", Model> {
248
- /** Clip name; empty = the model's first clip. */
249
- clip = ""
250
-
251
- loop = true
252
-
253
- /** Playback rate (1 = authored speed). */
254
- speed = 1
255
-
256
- static readonly aspect = "playAnimation"
257
- static fields: FieldMeta<PlayAnimation> = {
258
- speed: { min: 0.05, max: 5, step: 0.05 },
259
- }
260
-
261
- private _preview?: Playback
262
-
263
- onAttach(): void {
264
- const anim = (this.node as Model | undefined)?.anim
265
- if (!anim) return
266
- if (this.loop) anim.playLoop(this.clip === "" ? 0 : this.clip, { speed: this.speed })
267
- else anim.play(this.clip === "" ? 0 : this.clip, { speed: this.speed })
268
- }
269
-
270
- static inspector(ui: InspectorUI, a: PlayAnimation): void {
271
- const anim = (a.node as Model | undefined)?.anim
272
- const clips = anim ? anim.clips : []
273
- if (clips.length === 0) {
274
- ui.warn("No animation clips — attach this to a GLB model node")
275
- return
276
- }
277
- ui.select("clip", clips.map((c) => c.name))
278
- ui.auto("loop", "speed")
279
- if (anim) {
280
- const clip = a.clip === "" ? clips[0]?.name : a.clip
281
- const playing = a._preview?.playing === true
282
- if (ui.button(playing ? "Restart preview" : "Preview")) {
283
- if (a.loop) { anim.playLoop(clip, { speed: a.speed }); a._preview = undefined }
284
- else a._preview = anim.play(clip, { speed: a.speed, restart: true })
285
- }
286
- if (playing && ui.button("Stop")) { anim.stop(); a._preview = undefined }
287
- const duration = clips.find((c) => c.name === clip)?.duration
288
- ui.info(playing ? "previewing…" : duration !== undefined ? `${duration.toFixed(2)}s` : "")
289
- }
290
- }
291
- }
1
+ // No-code scenario aspects: ready-made behaviors a scene builder attaches in the editor without
2
+ // writing any code — move to a target, travel a waypoint path, spin, face something, start a GLB
3
+ // animation. They are ordinary aspects (work from hand-written code too); in the scene editor the
4
+ // movement ones double as `static editor` generators that draw their paths as editor gizmos
5
+ // (`Gizmos.*` in rebuild() — overlay lines, never scene content; play mode never calls rebuild(),
6
+ // so they cost nothing at runtime).
7
+ //
8
+ // Conventions shared by the family:
9
+ // • targets/waypoints are scene nodes — place an Empty, then pick it in the inspector (`ref()`);
10
+ // • times are seconds, angles degrees; all motion runs in `update(dt)` (play mode only);
11
+ // • orientation is quaternion-composed — never euler-incremented (see math/quat.ts).
12
+
13
+ import { Aspect } from "../core/Aspect"
14
+ import type { FieldMeta } from "../core/fields"
15
+ import type { InspectorUI } from "../core/InspectorUI"
16
+ import { Vec3 } from "../math/vec"
17
+ import { Quat } from "../math/quat"
18
+ import { Node } from "./Node"
19
+ import { Model } from "./Model"
20
+ import type { Playback } from "./animation/Playback"
21
+ import { Gizmos } from "../scene/gizmos"
22
+
23
+ const MOVE_LINE = "#5b8ef0"
24
+ const LOOK_LINE = "#d9a13f"
25
+ const DEG2RAD = Math.PI / 180
26
+
27
+ type LoopMode = "once" | "loop" | "pingpong"
28
+
29
+ /** Normalized phase for elapsed time `t` over one trip of `duration`: once clamps at 1, loop
30
+ * wraps, pingpong triangles 0→1→0. */
31
+ const phase = (t: number, duration: number, mode: LoopMode): number => {
32
+ if (duration <= 0) return 1
33
+ const u = t / duration
34
+ if (mode === "once") return Math.min(u, 1)
35
+ if (mode === "loop") return u - Math.floor(u)
36
+ const v = u % 2
37
+ return v <= 1 ? v : 2 - v
38
+ }
39
+
40
+ const ease = (k: number, easing: "smooth" | "linear"): number =>
41
+ easing === "smooth" ? k * k * (3 - 2 * k) : k
42
+
43
+ /** A world point in `node`'s PARENT space — what `node.position` is expressed in. */
44
+ const toParentLocal = (node: Node, world: Vec3): Vec3 => {
45
+ const parent = node.parent
46
+ return parent ? parent.worldMatrix.invert().transformPoint(world) : world
47
+ }
48
+
49
+ /** A path node's waypoints: its children in scene-FILE order (`_sceneChildOrder`, stamped by
50
+ * defineScene — the engine's live child order is insertion-based and may differ), editor helper
51
+ * nodes (`__`-prefixed) excluded. Runtime-added children append in engine order. */
52
+ const pathWaypoints = (path: Node): Node[] => {
53
+ const children = path.children.filter((c) => !(c.name ?? "").startsWith("__"))
54
+ const order = (path as { _sceneChildOrder?: string[] })._sceneChildOrder
55
+ if (!order) return children
56
+ const byName = new Map(children.map((c) => [ c.name, c ]))
57
+ const sorted: Node[] = []
58
+ for (const n of order) { const c = byName.get(n); if (c) sorted.push(c) }
59
+ const seen = new Set(sorted)
60
+ for (const c of children) if (!seen.has(c)) sorted.push(c)
61
+ return sorted
62
+ }
63
+
64
+ // ---- the aspects ---------------------------------------------------------------------------------
65
+
66
+ /** Travel from the node's starting position to a target node in `duration` seconds. */
67
+ export class MoveTo extends Aspect<"moveTo"> {
68
+ /** Where to travel — place an Empty node and pick it. */
69
+ target: Node | null = null
70
+
71
+ /** Seconds for the full trip. */
72
+ duration = 2
73
+
74
+ /** Seconds to wait before starting. */
75
+ delay = 0
76
+
77
+ /** once = stop at the target · loop = restart from the start point · pingpong = back and forth. */
78
+ mode: LoopMode = "once"
79
+
80
+ /** Motion curve. */
81
+ easing: "smooth" | "linear" = "smooth"
82
+
83
+ static readonly aspect = "moveTo"
84
+ static editor = { rebuild: true }
85
+ static fields: FieldMeta<MoveTo> = {
86
+ target: { editor: "node" },
87
+ duration: { min: 0.05, max: 120, step: 0.05 },
88
+ delay: { min: 0, max: 120, step: 0.05 },
89
+ mode: { options: [ "once", "loop", "pingpong" ] },
90
+ easing: { options: [ "smooth", "linear" ] },
91
+ }
92
+
93
+ private _t = 0
94
+ private _start: Vec3 | null = null
95
+
96
+ update(dt: number): void {
97
+ if (!this.target) return
98
+ this._start ??= this.node.position
99
+ this._t += dt
100
+ const k = ease(phase(Math.max(0, this._t - this.delay), this.duration, this.mode), this.easing)
101
+ const end = toParentLocal(this.node, this.target.worldPosition)
102
+ this.node.position = new Vec3(this._start).add(end.sub(this._start).scale(k))
103
+ }
104
+
105
+ /** Editor-only path line (edit mode; play mode never calls this). */
106
+ rebuild(): void {
107
+ if (!this.target) return
108
+ // the target is an empty — its own anchored marker shows the endpoint
109
+ Gizmos.line(this.node.worldPosition, this.target.worldPosition, { color: MOVE_LINE })
110
+ }
111
+ }
112
+
113
+ /** Travel through the children of a path node (add an Empty per waypoint) in `duration` seconds.
114
+ * `loop` runs the circuit closed (last → first); `pingpong` goes back and forth along the open
115
+ * path; `once` stops at the last waypoint. */
116
+ export class FollowPath extends Aspect<"followPath"> {
117
+ /** A node whose CHILDREN are the waypoints, in file order. */
118
+ path: Node | null = null
119
+
120
+ /** Seconds for one full pass of the path. */
121
+ duration = 6
122
+
123
+ /** Seconds to wait before starting. */
124
+ delay = 0
125
+
126
+ mode: LoopMode = "loop"
127
+
128
+ /** Turn to face the direction of travel. */
129
+ orient = true
130
+
131
+ /** Which local axis leads when orienting. */
132
+ forward: "-z" | "z" | "x" | "-x" = "-z"
133
+
134
+ static readonly aspect = "followPath"
135
+ static editor = { rebuild: true }
136
+ static fields: FieldMeta<FollowPath> = {
137
+ path: { editor: "node" },
138
+ duration: { min: 0.05, max: 300, step: 0.05 },
139
+ delay: { min: 0, max: 120, step: 0.05 },
140
+ mode: { options: [ "loop", "once", "pingpong" ] },
141
+ forward: { options: [ "-z", "z", "x", "-x" ] },
142
+ }
143
+
144
+ private _t = 0
145
+
146
+ private _points(): Vec3[] {
147
+ if (!this.path) return []
148
+ const points = pathWaypoints(this.path).map((c) => c.worldPosition)
149
+ if (this.mode === "loop" && points.length > 1) points.push(points[0]!.clone())
150
+ return points
151
+ }
152
+
153
+ update(dt: number): void {
154
+ const points = this._points()
155
+ if (points.length < 2) return
156
+ this._t += dt
157
+ const k = phase(Math.max(0, this._t - this.delay), this.duration, this.mode === "loop" ? "loop" : this.mode)
158
+
159
+ const lengths: number[] = []
160
+ let total = 0
161
+ for (let i = 0; i < points.length - 1; i++) {
162
+ const l = points[i + 1]!.sub(points[i]!).length()
163
+ lengths.push(l)
164
+ total += l
165
+ }
166
+ if (total <= 0) return
167
+
168
+ let dist = k * total
169
+ let i = 0
170
+ while (i < lengths.length - 1 && dist > lengths[i]!) { dist -= lengths[i]!; i++ }
171
+ const a = points[i]!
172
+ const b = points[i + 1]!
173
+ const seg = b.sub(a)
174
+ const u = lengths[i]! > 0 ? Math.min(dist / lengths[i]!, 1) : 0
175
+ const world = a.add(seg.scale(u))
176
+ this.node.position = toParentLocal(this.node, world)
177
+ if (this.orient && seg.lengthSq() > 1e-12) this.node.lookAt(world.add(seg), this.forward)
178
+ }
179
+
180
+ /** Editor-only waypoint polyline (edit mode; play mode never calls this). */
181
+ rebuild(): void {
182
+ const points = this._points() // loop mode already appends the first point again
183
+ Gizmos.polyline(points, { color: MOVE_LINE }) // waypoints are empties: they carry their own markers
184
+ }
185
+ }
186
+
187
+ /** Continuous rotation about one of the node's local axes, degrees per second. */
188
+ export class Spin extends Aspect<"spin"> {
189
+ /** Degrees per second. */
190
+ speed = 45
191
+
192
+ /** Local axis to spin around. */
193
+ axis: "x" | "y" | "z" = "y"
194
+
195
+ static readonly aspect = "spin"
196
+ static fields: FieldMeta<Spin> = {
197
+ speed: { min: -720, max: 720, step: 5 },
198
+ axis: { options: [ "x", "y", "z" ] },
199
+ }
200
+
201
+ update(dt: number): void {
202
+ const axis: [number, number, number] = this.axis === "x" ? [ 1, 0, 0 ] : this.axis === "y" ? [ 0, 1, 0 ] : [ 0, 0, 1 ]
203
+ // compose in quaternion space about the LOCAL axis (q ⊗ dq) — never euler-increment
204
+ this.node.quaternion = this.node.quaternion.mul(Quat.fromAxisAngle(axis, this.speed * dt * DEG2RAD))
205
+ }
206
+ }
207
+
208
+ /** Keep the node facing a target node (a turret tracking, a signpost, a camera aimed at a hero).
209
+ * Accessor `node.facing` — NOT `lookAt`, which would shadow the `Node.lookAt()` method. */
210
+ export class LookAt extends Aspect<"facing"> {
211
+ /** The node to face. */
212
+ target: Node | null = null
213
+
214
+ /** Which local axis points at the target. */
215
+ forward: "-z" | "z" | "x" | "-x" = "-z"
216
+
217
+ /** 0 = snap instantly; otherwise seconds of turn lag (bigger = slower, smoother). */
218
+ smoothing = 0
219
+
220
+ static readonly aspect = "facing"
221
+ static editor = { rebuild: true }
222
+ static fields: FieldMeta<LookAt> = {
223
+ target: { editor: "node" },
224
+ forward: { options: [ "-z", "z", "x", "-x" ] },
225
+ smoothing: { min: 0, max: 5, step: 0.05 },
226
+ }
227
+
228
+ update(dt: number): void {
229
+ if (!this.target) return
230
+ const point = this.target.worldPosition
231
+ if (this.smoothing <= 0) { this.node.lookAt(point, this.forward); return }
232
+ const q0 = this.node.quaternion
233
+ this.node.lookAt(point, this.forward)
234
+ const q1 = this.node.quaternion
235
+ this.node.quaternion = q0.slerp(q1, 1 - Math.exp(-dt / this.smoothing))
236
+ }
237
+
238
+ /** Editor-only sight line (edit mode; play mode never calls this). */
239
+ rebuild(): void {
240
+ if (!this.target) return
241
+ Gizmos.line(this.node.worldPosition, this.target.worldPosition, { color: LOOK_LINE })
242
+ }
243
+ }
244
+
245
+ /** Start a GLB animation clip when the scene runs. Attach to a `model:` node; the inspector card
246
+ * lists the model's clips live and can preview them while editing. */
247
+ export class PlayAnimation extends Aspect<"playAnimation", Model> {
248
+ /** Clip name; empty = the model's first clip. */
249
+ clip = ""
250
+
251
+ loop = true
252
+
253
+ /** Playback rate (1 = authored speed). */
254
+ speed = 1
255
+
256
+ static readonly aspect = "playAnimation"
257
+ static fields: FieldMeta<PlayAnimation> = {
258
+ speed: { min: 0.05, max: 5, step: 0.05 },
259
+ }
260
+
261
+ private _preview?: Playback
262
+
263
+ onAttach(): void {
264
+ const anim = (this.node as Model | undefined)?.anim
265
+ if (!anim) return
266
+ if (this.loop) anim.playLoop(this.clip === "" ? 0 : this.clip, { speed: this.speed })
267
+ else anim.play(this.clip === "" ? 0 : this.clip, { speed: this.speed })
268
+ }
269
+
270
+ static inspector(ui: InspectorUI, a: PlayAnimation): void {
271
+ const anim = (a.node as Model | undefined)?.anim
272
+ const clips = anim ? anim.clips : []
273
+ if (clips.length === 0) {
274
+ ui.warn("No animation clips — attach this to a GLB model node")
275
+ return
276
+ }
277
+ ui.select("clip", clips.map((c) => c.name))
278
+ ui.auto("loop", "speed")
279
+ if (anim) {
280
+ const clip = a.clip === "" ? clips[0]?.name : a.clip
281
+ const playing = a._preview?.playing === true
282
+ if (ui.button(playing ? "Restart preview" : "Preview")) {
283
+ if (a.loop) { anim.playLoop(clip, { speed: a.speed }); a._preview = undefined }
284
+ else a._preview = anim.play(clip, { speed: a.speed, restart: true })
285
+ }
286
+ if (playing && ui.button("Stop")) { anim.stop(); a._preview = undefined }
287
+ const duration = clips.find((c) => c.name === clip)?.duration
288
+ ui.info(playing ? "previewing…" : duration !== undefined ? `${duration.toFixed(2)}s` : "")
289
+ }
290
+ }
291
+ }
@@ -236,9 +236,8 @@ export class Terrain {
236
236
  static async load(url: string, opts: Omit<TerrainOptions, "size" | "sizeX" | "sizeZ" | "heights" | "holes" | "control"> = {}): Promise<Terrain> {
237
237
  const resp = await fetch(url, { useOnce: true })
238
238
  if (resp.status >= 400) throw new Error(`Terrain: could not fetch ${url} (HTTP ${resp.status})`)
239
- if (!_creatorUtils.fetchToBytes) { resp.dispose(); throw new Error("Terrain.load needs a newer host (no binary fetch reader)") }
240
- const bytes = _creatorUtils.fetchToBytes((resp as unknown as { _id: number })._id)
241
- resp.dispose()
239
+ if (!_creatorFetch.toBytes) { throw new Error("Terrain.load needs a newer host (no binary fetch reader)") }
240
+ const bytes = _creatorFetch.toBytes((resp as unknown as { _h: Handle })._h.id)
242
241
  const data = Terrain.parse(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer)
243
242
  const layers = opts.layers ?? data.layers
244
243
  return new Terrain({ ...opts, ...data, layers, cellSize: opts.cellSize ?? data.cellSize, chunk: opts.chunk ?? data.chunk })
@@ -860,7 +859,7 @@ export class Terrain {
860
859
  }
861
860
  if (!this._controlTex) {
862
861
  const id = _creator.createTexturePixels(this.sizeX, this.sizeZ, 4, this.control, false)
863
- if (id === 0xFFFFFFFF) return
862
+ if (id.id === 0xFFFFFFFF) return
864
863
  this._controlTex = new Texture(this.sizeX, this.sizeZ, id)
865
864
  this._controlTex.wrapS = 0
866
865
  this._controlTex.wrapT = 0
@@ -871,7 +870,7 @@ export class Terrain {
871
870
  const w = r.x1 - r.x0 + 1, h = r.z1 - r.z0 + 1
872
871
  const sub = new Uint8Array(w * h * 4)
873
872
  for (let z = 0; z < h; z++) sub.set(this.control.subarray(((r.z0 + z) * this.sizeX + r.x0) * 4, ((r.z0 + z) * this.sizeX + r.x0 + w) * 4), z * w * 4)
874
- _creator.updateTexturePixels(this._controlTex._id, r.x0, r.z0, w, h, sub)
873
+ _creator.updateTexturePixels(this._controlTex._h.id, r.x0, r.z0, w, h, sub)
875
874
  }
876
875
 
877
876
  private async _applyLayers(normals?: TerrainOptions["normals"]): Promise<void> {