lecodes-sdk 0.19.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.
- package/README.md +114 -0
- package/dist/global.d.ts +503 -0
- package/dist/host.d.ts +61 -0
- package/dist/inject.js +4730 -0
- package/dist/types/animate/animate.d.ts +20 -0
- package/dist/types/animate/bezier.d.ts +2 -0
- package/dist/types/animate/easings.d.ts +21 -0
- package/dist/types/canvas/Canvas.d.ts +168 -0
- package/dist/types/core/Aspect.d.ts +171 -0
- package/dist/types/core/InspectorUI.d.ts +88 -0
- package/dist/types/core/StateMachine.d.ts +81 -0
- package/dist/types/core/color.d.ts +13 -0
- package/dist/types/core/compWrite.d.ts +11 -0
- package/dist/types/core/events.d.ts +11 -0
- package/dist/types/core/fields.d.ts +58 -0
- package/dist/types/core/registry.d.ts +7 -0
- package/dist/types/core/signals.d.ts +29 -0
- package/dist/types/core/time.d.ts +31 -0
- package/dist/types/g2/Camera2D.d.ts +19 -0
- package/dist/types/g2/CharacterController2D.d.ts +134 -0
- package/dist/types/g2/Node2D.d.ts +91 -0
- package/dist/types/g2/OneWay2D.d.ts +24 -0
- package/dist/types/g2/Physics2D.d.ts +147 -0
- package/dist/types/g2/Scene2D.d.ts +72 -0
- package/dist/types/g2/Shape2D.d.ts +58 -0
- package/dist/types/g2/Sprite.d.ts +51 -0
- package/dist/types/g2/SpriteAnimation.d.ts +50 -0
- package/dist/types/g2/SpriteSheet.d.ts +69 -0
- package/dist/types/g2/Texture2D.d.ts +16 -0
- package/dist/types/g2/Tilemap.d.ts +20 -0
- package/dist/types/g2/Tileset.d.ts +32 -0
- package/dist/types/g2/Trigger2D.d.ts +20 -0
- package/dist/types/g2/autotile.d.ts +133 -0
- package/dist/types/g2/cells.d.ts +14 -0
- package/dist/types/g2/defineScene2d.d.ts +141 -0
- package/dist/types/g2/groups2d.d.ts +16 -0
- package/dist/types/g2/loop.d.ts +4 -0
- package/dist/types/g2/scenarios2d.d.ts +30 -0
- package/dist/types/g2/touch.d.ts +2 -0
- package/dist/types/gl/Camera.d.ts +79 -0
- package/dist/types/gl/CameraPlace.d.ts +26 -0
- package/dist/types/gl/CharacterController.d.ts +98 -0
- package/dist/types/gl/Gearbox.d.ts +86 -0
- package/dist/types/gl/Geometry.d.ts +39 -0
- package/dist/types/gl/IK.d.ts +53 -0
- package/dist/types/gl/InstancedMesh.d.ts +46 -0
- package/dist/types/gl/Light.d.ts +73 -0
- package/dist/types/gl/Lightmap.d.ts +85 -0
- package/dist/types/gl/Locomotion.d.ts +279 -0
- package/dist/types/gl/Material.d.ts +100 -0
- package/dist/types/gl/Mesh.d.ts +33 -0
- package/dist/types/gl/Model.d.ts +53 -0
- package/dist/types/gl/NavAgent.d.ts +99 -0
- package/dist/types/gl/NavMesh.d.ts +169 -0
- package/dist/types/gl/Node.d.ts +99 -0
- package/dist/types/gl/Noise.d.ts +20 -0
- package/dist/types/gl/Particles.d.ts +277 -0
- package/dist/types/gl/Physics.d.ts +91 -0
- package/dist/types/gl/Plane.d.ts +19 -0
- package/dist/types/gl/Ragdoll.d.ts +86 -0
- package/dist/types/gl/Ray.d.ts +8 -0
- package/dist/types/gl/Scene.d.ts +221 -0
- package/dist/types/gl/Shape.d.ts +122 -0
- package/dist/types/gl/Terrain.d.ts +280 -0
- package/dist/types/gl/Texture.d.ts +27 -0
- package/dist/types/gl/Trigger.d.ts +10 -0
- package/dist/types/gl/Vehicle.d.ts +191 -0
- package/dist/types/gl/Wheel.d.ts +95 -0
- package/dist/types/gl/animation/AnimationClip.d.ts +60 -0
- package/dist/types/gl/animation/Animator.d.ts +219 -0
- package/dist/types/gl/animation/Layer.d.ts +31 -0
- package/dist/types/gl/animation/Loop.d.ts +17 -0
- package/dist/types/gl/animation/Playback.d.ts +36 -0
- package/dist/types/gl/animation/core.d.ts +367 -0
- package/dist/types/gl/controls.d.ts +19 -0
- package/dist/types/gl/physicsEvents.d.ts +1 -0
- package/dist/types/gl/scenarios.d.ts +98 -0
- package/dist/types/gl/state.d.ts +5 -0
- package/dist/types/gl/terrainMesh.d.ts +46 -0
- package/dist/types/gl/touch.d.ts +2 -0
- package/dist/types/inject.d.ts +123 -0
- package/dist/types/math/Mathf.d.ts +39 -0
- package/dist/types/math/mat4.d.ts +66 -0
- package/dist/types/math/quat.d.ts +53 -0
- package/dist/types/math/vec.d.ts +129 -0
- package/dist/types/net/codec.d.ts +25 -0
- package/dist/types/net/core.d.ts +106 -0
- package/dist/types/net/index.d.ts +80 -0
- package/dist/types/net/replication.d.ts +101 -0
- package/dist/types/plugins/camera.d.ts +25 -0
- package/dist/types/plugins/geolocation.d.ts +39 -0
- package/dist/types/plugins/oauth.d.ts +25 -0
- package/dist/types/plugins/permission.d.ts +1 -0
- package/dist/types/plugins/push.d.ts +46 -0
- package/dist/types/plugins/qr.d.ts +21 -0
- package/dist/types/plugins/service.d.ts +33 -0
- package/dist/types/runtime/app.d.ts +67 -0
- package/dist/types/runtime/appEvents.d.ts +7 -0
- package/dist/types/runtime/channel.d.ts +1 -0
- package/dist/types/runtime/clipboard.d.ts +7 -0
- package/dist/types/runtime/datetime.d.ts +80 -0
- package/dist/types/runtime/device.d.ts +126 -0
- package/dist/types/runtime/fetch.d.ts +31 -0
- package/dist/types/runtime/files.d.ts +33 -0
- package/dist/types/runtime/input.d.ts +121 -0
- package/dist/types/runtime/media.d.ts +46 -0
- package/dist/types/runtime/misc.d.ts +11 -0
- package/dist/types/runtime/net.d.ts +15 -0
- package/dist/types/runtime/rpc.d.ts +44 -0
- package/dist/types/runtime/service.d.ts +1 -0
- package/dist/types/runtime/share.d.ts +2 -0
- package/dist/types/runtime/storage.d.ts +5 -0
- package/dist/types/runtime/touch.d.ts +56 -0
- package/dist/types/scene/defineScene.d.ts +296 -0
- package/dist/types/scene/editorPlugins.d.ts +82 -0
- package/dist/types/scene/gizmos.d.ts +56 -0
- package/dist/types/scene/grammar.d.ts +56 -0
- package/dist/types/scene/level.d.ts +98 -0
- package/dist/types/scene/material.d.ts +76 -0
- package/dist/types/server/auth/appConfig.d.ts +16 -0
- package/dist/types/server/auth/global.d.ts +56 -0
- package/dist/types/server/auth/models.d.ts +164 -0
- package/dist/types/server/auth/types.d.ts +56 -0
- package/dist/types/server/channel.d.ts +28 -0
- package/dist/types/server/context.d.ts +27 -0
- package/dist/types/server/db/defineDb.d.ts +36 -0
- package/dist/types/server/db/fields.d.ts +146 -0
- package/dist/types/server/db/index.d.ts +6 -0
- package/dist/types/server/db/marci/query.d.ts +326 -0
- package/dist/types/server/db/types.d.ts +163 -0
- package/dist/types/server/errors.d.ts +8 -0
- package/dist/types/server/inject.d.ts +5 -0
- package/dist/types/ui/NativeView.d.ts +38 -0
- package/dist/types/ui/UI.d.ts +27 -0
- package/dist/types/ui/UIBottomSheet.d.ts +71 -0
- package/dist/types/ui/UIButton.d.ts +30 -0
- package/dist/types/ui/UIContainer.d.ts +33 -0
- package/dist/types/ui/UIImage.d.ts +35 -0
- package/dist/types/ui/UIInput.d.ts +110 -0
- package/dist/types/ui/UIModal.d.ts +70 -0
- package/dist/types/ui/UINode.d.ts +363 -0
- package/dist/types/ui/UIPager.d.ts +123 -0
- package/dist/types/ui/UIPopover.d.ts +39 -0
- package/dist/types/ui/UIScreen.d.ts +57 -0
- package/dist/types/ui/UIScrollable.d.ts +40 -0
- package/dist/types/ui/UISpacer.d.ts +9 -0
- package/dist/types/ui/UITabs.d.ts +96 -0
- package/dist/types/ui/UIText.d.ts +19 -0
- package/dist/types/ui/UIVideo.d.ts +28 -0
- package/dist/types/ui/UIVirtualizedList.d.ts +97 -0
- package/dist/types/ui/UIWidget.d.ts +65 -0
- package/dist/types/ui/fonts.d.ts +9 -0
- package/dist/types/ui/presentable.d.ts +68 -0
- package/dist/types/ui/router.d.ts +40 -0
- package/dist/types/ui/theme.d.ts +26 -0
- package/dist/types.json +1 -0
- package/package.json +46 -0
- package/prompts/2d.md +395 -0
- package/prompts/3d-scene-files.md +113 -0
- package/prompts/3d-scene.md +119 -0
- package/prompts/3d.md +303 -0
- package/prompts/README.md +142 -0
- package/prompts/ar.md +109 -0
- package/prompts/canvas.md +27 -0
- package/prompts/compose.ts +46 -0
- package/prompts/concept.md +49 -0
- package/prompts/core-design.md +75 -0
- package/prompts/core.md +264 -0
- package/prompts/design.md +218 -0
- package/prompts/dist/2d-game.md +1530 -0
- package/prompts/dist/3d-app.md +1672 -0
- package/prompts/dist/ar-app.md +1548 -0
- package/prompts/dist/concept.md +49 -0
- package/prompts/dist/design.md +498 -0
- package/prompts/dist/ui-app.md +1134 -0
- package/prompts/index.md +37 -0
- package/prompts/intake-prompt.md +33 -0
- package/prompts/namer-prompt.md +12 -0
- package/prompts/router-prompt.md +50 -0
- package/prompts/select.ts +111 -0
- package/prompts/ui-design.md +203 -0
- package/prompts/ui.md +803 -0
- package/src/animate/animate.ts +238 -0
- package/src/animate/bezier.ts +138 -0
- package/src/animate/easings.ts +126 -0
- package/src/bridges.d.ts +1345 -0
- package/src/canvas/Canvas.ts +305 -0
- package/src/compile/__tests__/assetIconMacro.test.ts +219 -0
- package/src/compile/__tests__/assetMacro.test.ts +74 -0
- package/src/compile/__tests__/assetName.test.ts +55 -0
- package/src/compile/__tests__/compile.test.ts +287 -0
- package/src/compile/__tests__/detectEntry.test.ts +132 -0
- package/src/compile/__tests__/fontMacro.test.ts +199 -0
- package/src/compile/aspectMacro.ts +86 -0
- package/src/compile/assetIconMacro.ts +384 -0
- package/src/compile/assetMacro.ts +146 -0
- package/src/compile/assetName.ts +50 -0
- package/src/compile/bundler.ts +287 -0
- package/src/compile/compileProject.ts +127 -0
- package/src/compile/detectEntry.ts +128 -0
- package/src/compile/fontMacro.ts +459 -0
- package/src/compile/fontRegistry.ts +78 -0
- package/src/compile/header.ts +67 -0
- package/src/compile/index.ts +100 -0
- package/src/compile/libraryImports.ts +52 -0
- package/src/compile/liteMaterial.ts +247 -0
- package/src/compile/sceneEditor.ts +88 -0
- package/src/compile/serverSplit.ts +233 -0
- package/src/compile/serverTypes.ts +227 -0
- package/src/compile/sfnt.ts +98 -0
- package/src/compile/shaderSchema.ts +202 -0
- package/src/compile/shaderTargets.ts +77 -0
- package/src/compile/sourcemap.ts +25 -0
- package/src/core/Aspect.ts +568 -0
- package/src/core/InspectorUI.ts +212 -0
- package/src/core/StateMachine.ts +308 -0
- package/src/core/__tests__/stateMachine.test.ts +132 -0
- package/src/core/color.ts +66 -0
- package/src/core/compWrite.ts +42 -0
- package/src/core/events.ts +38 -0
- package/src/core/fields.ts +120 -0
- package/src/core/registry.ts +23 -0
- package/src/core/signals.ts +277 -0
- package/src/core/time.ts +84 -0
- package/src/g2/Camera2D.ts +40 -0
- package/src/g2/CharacterController2D.ts +276 -0
- package/src/g2/Node2D.ts +267 -0
- package/src/g2/OneWay2D.ts +66 -0
- package/src/g2/Physics2D.ts +346 -0
- package/src/g2/Scene2D.ts +209 -0
- package/src/g2/Shape2D.ts +259 -0
- package/src/g2/Sprite.ts +89 -0
- package/src/g2/SpriteAnimation.ts +171 -0
- package/src/g2/SpriteSheet.ts +166 -0
- package/src/g2/Texture2D.ts +47 -0
- package/src/g2/Tilemap.ts +41 -0
- package/src/g2/Tileset.ts +71 -0
- package/src/g2/Trigger2D.ts +77 -0
- package/src/g2/autotile.ts +433 -0
- package/src/g2/cells.ts +91 -0
- package/src/g2/defineScene2d.ts +381 -0
- package/src/g2/groups2d.ts +106 -0
- package/src/g2/loop.ts +50 -0
- package/src/g2/scenarios2d.ts +69 -0
- package/src/g2/touch.ts +83 -0
- package/src/gl/Camera.ts +160 -0
- package/src/gl/CameraPlace.ts +52 -0
- package/src/gl/CharacterController.ts +238 -0
- package/src/gl/Gearbox.ts +212 -0
- package/src/gl/Geometry.ts +279 -0
- package/src/gl/IK.ts +193 -0
- package/src/gl/InstancedMesh.ts +132 -0
- package/src/gl/Light.ts +135 -0
- package/src/gl/Lightmap.ts +402 -0
- package/src/gl/Locomotion.ts +498 -0
- package/src/gl/Material.ts +279 -0
- package/src/gl/Mesh.ts +83 -0
- package/src/gl/Model.ts +124 -0
- package/src/gl/NavAgent.ts +337 -0
- package/src/gl/NavMesh.ts +397 -0
- package/src/gl/Node.ts +350 -0
- package/src/gl/Noise.ts +30 -0
- package/src/gl/Particles.ts +676 -0
- package/src/gl/Physics.ts +222 -0
- package/src/gl/Plane.ts +53 -0
- package/src/gl/Ragdoll.ts +270 -0
- package/src/gl/Ray.ts +16 -0
- package/src/gl/Scene.ts +505 -0
- package/src/gl/Shape.ts +409 -0
- package/src/gl/Terrain.ts +1092 -0
- package/src/gl/Texture.ts +63 -0
- package/src/gl/Trigger.ts +45 -0
- package/src/gl/Vehicle.ts +473 -0
- package/src/gl/Wheel.ts +240 -0
- package/src/gl/animation/AnimationClip.ts +204 -0
- package/src/gl/animation/Animator.ts +329 -0
- package/src/gl/animation/Layer.ts +39 -0
- package/src/gl/animation/Loop.ts +31 -0
- package/src/gl/animation/Playback.ts +52 -0
- package/src/gl/animation/core.ts +670 -0
- package/src/gl/controls.ts +95 -0
- package/src/gl/physicsEvents.ts +20 -0
- package/src/gl/scenarios.ts +291 -0
- package/src/gl/state.ts +6 -0
- package/src/gl/terrainMesh.ts +219 -0
- package/src/gl/touch.ts +68 -0
- package/src/host.d.ts +61 -0
- package/src/inject.ts +203 -0
- package/src/math/Mathf.ts +118 -0
- package/src/math/mat4.ts +278 -0
- package/src/math/quat.ts +232 -0
- package/src/math/vec.ts +255 -0
- package/src/net/codec.ts +119 -0
- package/src/net/core.ts +384 -0
- package/src/net/index.ts +181 -0
- package/src/net/replication.ts +622 -0
- package/src/plugins/camera.ts +81 -0
- package/src/plugins/geolocation.ts +123 -0
- package/src/plugins/oauth.ts +61 -0
- package/src/plugins/permission.ts +7 -0
- package/src/plugins/push.ts +132 -0
- package/src/plugins/qr.ts +73 -0
- package/src/plugins/service.ts +47 -0
- package/src/runtime/app.ts +101 -0
- package/src/runtime/appEvents.ts +54 -0
- package/src/runtime/channel.ts +61 -0
- package/src/runtime/clipboard.ts +20 -0
- package/src/runtime/datetime.ts +329 -0
- package/src/runtime/device.ts +293 -0
- package/src/runtime/fetch.ts +77 -0
- package/src/runtime/files.ts +108 -0
- package/src/runtime/input.ts +175 -0
- package/src/runtime/media.ts +111 -0
- package/src/runtime/misc.ts +16 -0
- package/src/runtime/net.ts +36 -0
- package/src/runtime/rpc.ts +218 -0
- package/src/runtime/service.ts +83 -0
- package/src/runtime/share.ts +9 -0
- package/src/runtime/storage.ts +13 -0
- package/src/runtime/touch.ts +76 -0
- package/src/scene/defineScene.ts +1396 -0
- package/src/scene/editorPlugins.ts +110 -0
- package/src/scene/gizmos.ts +148 -0
- package/src/scene/grammar.ts +120 -0
- package/src/scene/level.ts +296 -0
- package/src/scene/material.ts +188 -0
- package/src/server/auth/appConfig.ts +12 -0
- package/src/server/auth/global.ts +80 -0
- package/src/server/auth/host.ts +318 -0
- package/src/server/auth/models.ts +83 -0
- package/src/server/auth/types.ts +50 -0
- package/src/server/channel.ts +56 -0
- package/src/server/context.ts +36 -0
- package/src/server/db/defineDb.ts +237 -0
- package/src/server/db/fields.ts +132 -0
- package/src/server/db/httpTransport.ts +93 -0
- package/src/server/db/index.ts +7 -0
- package/src/server/db/marci/query.ts +412 -0
- package/src/server/db/types.ts +202 -0
- package/src/server/errors.ts +12 -0
- package/src/server/host.ts +74 -0
- package/src/server/inject.ts +13 -0
- package/src/server/runtime.ts +133 -0
- package/src/server/validate.ts +87 -0
- package/src/ui/NativeView.ts +142 -0
- package/src/ui/UI.ts +39 -0
- package/src/ui/UIBottomSheet.ts +139 -0
- package/src/ui/UIButton.ts +101 -0
- package/src/ui/UIContainer.ts +60 -0
- package/src/ui/UIImage.ts +83 -0
- package/src/ui/UIInput.ts +185 -0
- package/src/ui/UIModal.ts +139 -0
- package/src/ui/UINode.ts +830 -0
- package/src/ui/UIPager.ts +362 -0
- package/src/ui/UIPopover.ts +100 -0
- package/src/ui/UIScreen.ts +123 -0
- package/src/ui/UIScrollable.ts +87 -0
- package/src/ui/UISpacer.ts +14 -0
- package/src/ui/UITabs.ts +236 -0
- package/src/ui/UIText.ts +51 -0
- package/src/ui/UIVideo.ts +88 -0
- package/src/ui/UIVirtualizedList.ts +241 -0
- package/src/ui/UIWidget.ts +127 -0
- package/src/ui/fonts.ts +13 -0
- package/src/ui/presentable.ts +117 -0
- package/src/ui/router.ts +132 -0
- package/src/ui/theme.ts +84 -0
package/src/gl/touch.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// 3D pointer dispatch. Registers the host touch callbacks lazily (first listener) and routes hits to
|
|
2
|
+
// the struck Node and to the active scene's listeners. Ported from worker/src/TouchEventSystem.ts.
|
|
3
|
+
|
|
4
|
+
import { ClickEvent, TouchStartEvent, type TrackHandler } from "../runtime/touch"
|
|
5
|
+
import { nodeRegistry } from "./Node"
|
|
6
|
+
import { glState } from "./state"
|
|
7
|
+
|
|
8
|
+
// Hits inside a loaded model (GLB) land on sub-entities the user never created a Node for — walk up
|
|
9
|
+
// the transform hierarchy to the nearest entity someone actually holds (the model's Node).
|
|
10
|
+
const resolveHitNode = (entityId: number) => {
|
|
11
|
+
let node = nodeRegistry.get(entityId)
|
|
12
|
+
for (let id = entityId, i = 0; !node && i < 32; i++) {
|
|
13
|
+
id = _creator.getParent(id)
|
|
14
|
+
if (id === 0) break
|
|
15
|
+
node = nodeRegistry.get(id)
|
|
16
|
+
}
|
|
17
|
+
return node ?? null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const onTouchEnd = (pointerId: number, clientX: number, clientY: number, entityId: number): TrackHandler | null => {
|
|
21
|
+
const scene = glState.activeScene
|
|
22
|
+
if (!scene) return null
|
|
23
|
+
const ev = new ClickEvent<any>(clientX, clientY, pointerId)
|
|
24
|
+
if (entityId > 0) {
|
|
25
|
+
const node = resolveHitNode(entityId)
|
|
26
|
+
ev.target = node
|
|
27
|
+
if (node) node._dispatchEvent("click", ev)
|
|
28
|
+
} else {
|
|
29
|
+
ev.target = null
|
|
30
|
+
}
|
|
31
|
+
for (const listener of scene._clickListeners.slice()) listener(ev)
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const onTouchStart = (pointerId: number, clientX: number, clientY: number, entityId: number): TrackHandler | null => {
|
|
36
|
+
const scene = glState.activeScene
|
|
37
|
+
if (!scene) return null
|
|
38
|
+
const ev = new TouchStartEvent<any>(clientX, clientY, pointerId)
|
|
39
|
+
if (entityId > 0) {
|
|
40
|
+
const node = resolveHitNode(entityId)
|
|
41
|
+
ev.target = node
|
|
42
|
+
if (node) {
|
|
43
|
+
node._dispatchEvent("touchstart", ev)
|
|
44
|
+
if (ev._trackHandler) return ev._trackHandler
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
ev.target = null
|
|
48
|
+
}
|
|
49
|
+
for (const listener of scene._touchStartListeners.slice()) {
|
|
50
|
+
listener(ev)
|
|
51
|
+
if (ev._trackHandler) return ev._trackHandler
|
|
52
|
+
}
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let touchStartRegistered = false
|
|
57
|
+
export const registerTouchStartEvent = (): void => {
|
|
58
|
+
if (touchStartRegistered) return
|
|
59
|
+
touchStartRegistered = true
|
|
60
|
+
_creatorUI.registerTouchStartEvent(onTouchStart)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let touchEndRegistered = false
|
|
64
|
+
export const registerTouchEndEvent = (): void => {
|
|
65
|
+
if (touchEndRegistered) return
|
|
66
|
+
touchEndRegistered = true
|
|
67
|
+
_creatorUI.registerTouchEndEvent(onTouchEnd)
|
|
68
|
+
}
|
package/src/host.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// JS-runtime globals provided by the host engine (web viewer / desktop / iOS), NOT by the SDK.
|
|
2
|
+
// Declared only so SDK and user code type-check — never bundled or injected. Mirrors
|
|
3
|
+
// packages/projects/src/global.d.ts.
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
function setTimeout(handler: (...args: any[]) => void, timeout?: number): number
|
|
7
|
+
function setInterval(handler: (...args: any[]) => void, timeout?: number): number
|
|
8
|
+
function clearTimeout(id: number): void
|
|
9
|
+
function clearInterval(id: number): void
|
|
10
|
+
/** Per-frame loop, driven by the host (viewer RAF / desktop runTick). `dt` is **seconds** since
|
|
11
|
+
* the previous frame. */
|
|
12
|
+
function setLoop(handler: (dt: number) => void): number
|
|
13
|
+
function clearLoop(id: number): void
|
|
14
|
+
|
|
15
|
+
/** π/180 — substituted at compile time (bundler `define`). */
|
|
16
|
+
var DEG2RAD: number
|
|
17
|
+
/** 180/π — substituted at compile time (bundler `define`). */
|
|
18
|
+
var RAD2DEG: number
|
|
19
|
+
|
|
20
|
+
/** Compile-time boolean: `true` in scene-editor bundles, `false` (const-folded + DCE'd) in
|
|
21
|
+
* shipped ones — `if (EDITOR) { … }` costs zero bytes in production. `*.editor.ts` files are
|
|
22
|
+
* the file-level form. */
|
|
23
|
+
var EDITOR: boolean
|
|
24
|
+
|
|
25
|
+
var console: {
|
|
26
|
+
log(...data: any[]): void
|
|
27
|
+
warn(...data: any[]): void
|
|
28
|
+
info(...data: any[]): void
|
|
29
|
+
error(...data: any[]): void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Compile-time macro: `asset('./hero.png')` is desugared by the bundler into the module import
|
|
33
|
+
* for that resource. The file must exist — a path that resolves to nothing fails the compile
|
|
34
|
+
* (`asset not found: ./hero.png (main.ts:3)`); there is no runtime fallback. Calls inside
|
|
35
|
+
* comments and string literals are text, not macros. */
|
|
36
|
+
function asset(path: string): string
|
|
37
|
+
|
|
38
|
+
/** Compile-time macro: installs a font and returns its family name — using a font IS declaring
|
|
39
|
+
* it. A registry id (`font("manrope")`) resolves against the font registry; a project path
|
|
40
|
+
* (`font("./fonts/Brand.ttf")`) reads family/weight/italic from the file itself. `opts`
|
|
41
|
+
* narrows/extends the face set (default: the family's standard weights). String-literal
|
|
42
|
+
* argument only. No awaiting — faces load with app boot and the host holds first paint for
|
|
43
|
+
* them. */
|
|
44
|
+
function font(id: string, opts?: { weights?: number[], italic?: boolean }): string
|
|
45
|
+
|
|
46
|
+
/** Compile-time macro: resolves one icon from the icon registry at build time and inlines it as
|
|
47
|
+
* an image source for `UIImage(...)` — offline, identical on every host. `id` is `"pack:name"`
|
|
48
|
+
* (e.g. `"lucide:bell"`), string literal only. Recolor via `{ color }`: a hex LITERAL is baked
|
|
49
|
+
* into the SVG at compile time, a token/expression is applied as a tint (as is the `tintColor`
|
|
50
|
+
* style prop). */
|
|
51
|
+
function assetIcon(id: string, opts?: { color?: string }): { readonly svg: string, tintColor: string | null }
|
|
52
|
+
|
|
53
|
+
/** Type-only editor convenience: the style object type of a UI element. `Style<UIButton>` (or
|
|
54
|
+
* `Style<typeof myButton>`) is what you'd pass to `el.style(...)` — for typing reusable style
|
|
55
|
+
* presets. Deliberately no `Partial<>` wrapper: every style prop is already optional, and
|
|
56
|
+
* Partial over ClassStyles' `$`-index signature adds `| undefined`, breaking assignability back
|
|
57
|
+
* into `.style(...)`. Round-trip guarded by sdk/tests/ui-types.test.ts. */
|
|
58
|
+
type Style<T> = T extends { style: (s: infer S) => any } ? S : never
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export {}
|
package/src/inject.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// THE aggregation point. Every public global the SDK provides is re-exported here under the exact
|
|
2
|
+
// name user code uses it as. The bundler passes this file to esbuild's `inject:` option, so user
|
|
3
|
+
// code references `Scene`, `Sprite`, `toast`, … with no imports; esbuild tree-shakes away whatever
|
|
4
|
+
// a given project does not use (so a pure-2D project drops all 3D classes, and vice versa).
|
|
5
|
+
//
|
|
6
|
+
// Rule: this is the ONLY file that `export`s public names. Everything else is internal and imported
|
|
7
|
+
// by it. Host-provided globals (setLoop, console, setTimeout, DEG2RAD, asset, …) are NOT here —
|
|
8
|
+
// they live in src/host.d.ts as declarations and stay as free references at runtime.
|
|
9
|
+
|
|
10
|
+
// ---- math ----
|
|
11
|
+
export { Mathf } from "./math/Mathf"
|
|
12
|
+
export { Vec2, Vec3 } from "./math/vec"
|
|
13
|
+
export { Quat } from "./math/quat"
|
|
14
|
+
export { Mat4 } from "./math/mat4"
|
|
15
|
+
export { Color } from "./core/color"
|
|
16
|
+
|
|
17
|
+
// ---- aspects (attachable node capabilities) ----
|
|
18
|
+
export { Aspect, System, type With, type SystemHost } from "./core/Aspect"
|
|
19
|
+
export { type EventMap } from "./core/events"
|
|
20
|
+
export { Time } from "./core/time"
|
|
21
|
+
export { StateMachine, defineStates, type StateDef, type StatesDef, type StateGuard, type StateHandler, type StateMachineEvents } from "./core/StateMachine"
|
|
22
|
+
export { InspectorUI } from "./core/InspectorUI"
|
|
23
|
+
|
|
24
|
+
// ---- reactivity (signals; UI text/style props also accept `() => value` bindings) ----
|
|
25
|
+
export { signal, computed, effect, type Signal, type Computed } from "./core/signals"
|
|
26
|
+
export { __compWrite, __compOp, type CompAxis, type CompWriter } from "./core/compWrite"
|
|
27
|
+
|
|
28
|
+
// ---- canvas (core 2D drawing that bakes to a texture; shared by 2D/3D/UI) ----
|
|
29
|
+
export { Canvas, Bitmap } from "./canvas/Canvas"
|
|
30
|
+
|
|
31
|
+
// ---- platform runtime (injected globals; the host provides the lower-level _creator* bridges) ----
|
|
32
|
+
export { toast, openURL, SvgSource } from "./runtime/misc"
|
|
33
|
+
export { app, type AppState, type OrientationLock } from "./runtime/app"
|
|
34
|
+
export { clipboard } from "./runtime/clipboard"
|
|
35
|
+
export { date, type DateValue, type DateInput, type Unit, type Locale } from "./runtime/datetime"
|
|
36
|
+
export { fetch, fetchLocal, FormData, File, type FetchResponse } from "./runtime/fetch"
|
|
37
|
+
export { localStorage } from "./runtime/storage"
|
|
38
|
+
export { device, type HapticStyle, type MotionOptions } from "./runtime/device"
|
|
39
|
+
export { Input, InputChannel, type InputKeyEvent, type InputGamepadEvent, type InputEventName, type GamepadState, type GamepadAxisName } from "./runtime/input"
|
|
40
|
+
export { WebSocket } from "./runtime/net"
|
|
41
|
+
// Multiplayer (docs/multiplayer-plan.md): roles, players, JSON messages over the _creatorNet transport.
|
|
42
|
+
export { Net, NetPlayer, Replicated, NetEntity, type NetMessage, type NetRole, type NetStatus, type NetEvents, type NetLaunch, type NetInput, type NetKind, type NetTransform } from "./net"
|
|
43
|
+
// App-backend transport: the compiler-generated stubs of `*.server.ts` modules call these; user code
|
|
44
|
+
// only ever imports its server functions (docs/backend-plan.md §4).
|
|
45
|
+
export { __rpc, __channel, __serverOnly, RpcError, type ClientChannel } from "./runtime/rpc"
|
|
46
|
+
export { AudioPlayer, VideoPlayer } from "./runtime/media"
|
|
47
|
+
export { openFilePicker, files, type FileData } from "./runtime/files"
|
|
48
|
+
export { share } from "./runtime/share"
|
|
49
|
+
export { ClickEvent, TouchStartEvent, LongPressEvent } from "./runtime/touch"
|
|
50
|
+
|
|
51
|
+
// ---- 2D engine (creator-2d) ----
|
|
52
|
+
export { Scene2D } from "./g2/Scene2D"
|
|
53
|
+
export { Node2D } from "./g2/Node2D"
|
|
54
|
+
export { Camera2D } from "./g2/Camera2D"
|
|
55
|
+
export { Sprite } from "./g2/Sprite"
|
|
56
|
+
export { SpriteAnimation } from "./g2/SpriteAnimation"
|
|
57
|
+
export { Shape2D } from "./g2/Shape2D"
|
|
58
|
+
export { Physics2D } from "./g2/Physics2D"
|
|
59
|
+
export { Trigger2D } from "./g2/Trigger2D"
|
|
60
|
+
export { CharacterController2D } from "./g2/CharacterController2D"
|
|
61
|
+
export { OneWay2D } from "./g2/OneWay2D"
|
|
62
|
+
export { PhysicsGroup2D } from "./g2/groups2d"
|
|
63
|
+
export { Tilemap } from "./g2/Tilemap"
|
|
64
|
+
export { Texture2D } from "./g2/Texture2D"
|
|
65
|
+
// sprite sheets as data (.sprite.ts files — see src/g2/SpriteSheet.ts)
|
|
66
|
+
export { SpriteSheet, defineSpriteSheet } from "./g2/SpriteSheet"
|
|
67
|
+
export type { SpriteSheetDef, SheetSprite, SpriteMakeOptions } from "./g2/SpriteSheet"
|
|
68
|
+
// tilesets as data (.tiles.ts files — autotile materials, see src/g2/Tileset.ts + autotile.ts)
|
|
69
|
+
export { Tileset, defineTileset, Autotile2D } from "./g2/Tileset"
|
|
70
|
+
export { rawCell, isRawCell, rawIndex } from "./g2/autotile"
|
|
71
|
+
export { rot90, rot180, rot270, flipX, packTile, tileIndex, tileTurns, tileFlip, tileOriented } from "./g2/autotile"
|
|
72
|
+
export type {
|
|
73
|
+
TilesetDef, TileMaterial, PatchMaterial, PathMaterial, PatchSlots, EdgeSlot, FillBlock, BlockEntry,
|
|
74
|
+
TileBlock, TilePart,
|
|
75
|
+
} from "./g2/autotile"
|
|
76
|
+
// 2D scenes as data (.scene2d.ts files — see src/g2/defineScene2d.ts) + the cells('…') payload
|
|
77
|
+
export { defineScene2d, Scene2dHandle } from "./g2/defineScene2d"
|
|
78
|
+
export type { Scene2dDef, Scene2dNodeDef, Scene2dEnv, SpriteSourceDef, TilemapSourceDef, Camera2dNodeDef, LoadedScene2d } from "./g2/defineScene2d"
|
|
79
|
+
export { cells, encodeCells, type CellsData } from "./g2/cells"
|
|
80
|
+
// no-code 2D scenario aspects (attachable in scene files — see src/g2/scenarios2d.ts)
|
|
81
|
+
export { CameraFollow } from "./g2/scenarios2d"
|
|
82
|
+
|
|
83
|
+
// ---- UI (creator-ui) ----
|
|
84
|
+
export {
|
|
85
|
+
UIScreen, type UIScreenStyle,
|
|
86
|
+
UIRow, UIColumn, UIBox, type UIContainerStyle,
|
|
87
|
+
UIText, type UITextStyle,
|
|
88
|
+
UIButton, type UIButtonStyle,
|
|
89
|
+
UIImage, type UIImageStyle,
|
|
90
|
+
UIVideo, type UIVideoStyle,
|
|
91
|
+
UIInput, UITextArea, type UIInputStyle,
|
|
92
|
+
UIScrollable, type UIScrollableStyle,
|
|
93
|
+
UIPager, type UIPagerStyle,
|
|
94
|
+
UITabs, type UITabDef,
|
|
95
|
+
defineTabs, type TabDef, type TabsHandle,
|
|
96
|
+
UIWidget, type UIWidgetStyle,
|
|
97
|
+
UIModal, type UIModalStyle,
|
|
98
|
+
UIBottomSheet, type UIBottomSheetStyle,
|
|
99
|
+
UIPopover, type UIPopoverStyle,
|
|
100
|
+
UISpacer, type UISpacerStyle,
|
|
101
|
+
UIVirtualizedList,
|
|
102
|
+
Router,
|
|
103
|
+
NativeView, type UINativeViewStyle,
|
|
104
|
+
registerFont,
|
|
105
|
+
__uiMap,
|
|
106
|
+
__UIColumn, __UIRow, __UIBox, __UIButton, __UIScreen, __UIScrollable, __UIWidget,
|
|
107
|
+
} from "./ui/UI"
|
|
108
|
+
// The Presentable navigation contract (docs/navigation-presentable-plan.md). `Presentable` is
|
|
109
|
+
// interface + runtime companion (declaration merging): `Presentable.current` = what's on screen.
|
|
110
|
+
export { Presentable } from "./ui/UI"
|
|
111
|
+
export type { PresentOptions, Transition, TransitionName, TransitionSpec, TransitionTransform } from "./ui/UI"
|
|
112
|
+
// The base node type + the union a container accepts as children — documented as the signature of
|
|
113
|
+
// UIColumn/UIRow/append/etc., so they must be global (type-only; erased at runtime).
|
|
114
|
+
export type { UINode, UINodeChild } from "./ui/UINode"
|
|
115
|
+
// App theme variables (docs/ui-theme-plan.md): theme({...}) merges + returns typed var()
|
|
116
|
+
// accessors; system vars are static props (theme.primaryColor, theme["comfort-top"], …).
|
|
117
|
+
export { theme, type ThemeValues, type ThemeAccessors } from "./ui/theme"
|
|
118
|
+
|
|
119
|
+
// ---- animate ----
|
|
120
|
+
export { animate, animateMat4, stopAnimation, pauseAnimation, resumeAnimation } from "./animate/animate"
|
|
121
|
+
export { cubicBezier } from "./animate/bezier"
|
|
122
|
+
export { easeIn, easeOut, easeInOut } from "./animate/easings"
|
|
123
|
+
|
|
124
|
+
// ---- plugins (optional, host-provided capabilities — typed NativeView/service wrappers, see src/plugins/) ----
|
|
125
|
+
export { QRScanner } from "./plugins/qr"
|
|
126
|
+
export { CameraView, type CameraFacing } from "./plugins/camera"
|
|
127
|
+
export { Geolocation, type GeoPosition, type GeoOptions, type GeoWatch } from "./plugins/geolocation"
|
|
128
|
+
export { Push, type PushPayload, type PushStatus, type PushRegisterOptions, type PushEvent } from "./plugins/push"
|
|
129
|
+
export { OAuth, type OAuthCredential, type OAuthProviderName } from "./plugins/oauth"
|
|
130
|
+
export { Service } from "./plugins/service"
|
|
131
|
+
|
|
132
|
+
// ---- scenes as data (.scene.ts files — see src/scene/defineScene.ts) ----
|
|
133
|
+
export { defineScene, use, ref, make, SceneHandle, applyEditorPose as __applyEditorPose } from "./scene/defineScene"
|
|
134
|
+
export {
|
|
135
|
+
defineMaterial, MaterialHandle,
|
|
136
|
+
// editor-harness helpers (the scene editor's live material paths — not app API)
|
|
137
|
+
assignMaterialDef as __assignMaterialDef, applyMaterialParam as __applyMaterialParam,
|
|
138
|
+
} from "./scene/material"
|
|
139
|
+
export type { MaterialData, MaterialParamValue, ShaderMaterialDef, LitMaterialDef, UnlitMaterialDef } from "./scene/material"
|
|
140
|
+
export type { SceneDef, SceneNodeDef, MeshDef, LightDef, MaterialDef, CameraNodeDef, AspectEntry, MakeEntry, LoadedScene, SceneInstance } from "./scene/defineScene"
|
|
141
|
+
export { CameraPlace } from "./gl/CameraPlace"
|
|
142
|
+
// Procedural placement for large maps (line/grid/ring/scatter/place) — built for make() factories.
|
|
143
|
+
export { Level } from "./scene/level"
|
|
144
|
+
export type { LevelSpot, LevelBuild, LevelCollider, LevelLineOptions, LevelGridOptions, LevelRingOptions, LevelScatterOptions, LevelPlaceOptions } from "./scene/level"
|
|
145
|
+
export type { FieldMeta } from "./core/fields"
|
|
146
|
+
// Editor-only line drawing from generator rebuild() / make() factories / tool hooks — drawn by the
|
|
147
|
+
// scene editor's viewport overlay, a no-op everywhere else (see src/scene/gizmos.ts).
|
|
148
|
+
export { Gizmos, GizmoBuffer as __GizmoBuffer, withGizmoScope as __withGizmoScope } from "./scene/gizmos"
|
|
149
|
+
export type { GizmoStyle, GizmoAnchor } from "./scene/gizmos"
|
|
150
|
+
|
|
151
|
+
// ---- no-code scenario aspects (attachable in the scene editor — see src/gl/scenarios.ts) ----
|
|
152
|
+
export { MoveTo, FollowPath, Spin, LookAt, PlayAnimation } from "./gl/scenarios"
|
|
153
|
+
|
|
154
|
+
// ---- editor plugins (called from `*.editor.ts` files — editor bundles only; DCE'd from apps) ----
|
|
155
|
+
export { registerEditorWindow, registerEditorTool, __editorPlugins } from "./scene/editorPlugins"
|
|
156
|
+
export type { EditorApi, EditorRayHit, EditorToolHooks, EditorWindowFn } from "./scene/editorPlugins"
|
|
157
|
+
|
|
158
|
+
// ---- 3D engine (creator-gl) ----
|
|
159
|
+
export { Scene, ARScene, VRScene } from "./gl/Scene"
|
|
160
|
+
export { Node } from "./gl/Node"
|
|
161
|
+
export { Mesh } from "./gl/Mesh"
|
|
162
|
+
export { Model } from "./gl/Model"
|
|
163
|
+
export { Shape } from "./gl/Shape"
|
|
164
|
+
export { Physics } from "./gl/Physics"
|
|
165
|
+
export { Trigger } from "./gl/Trigger"
|
|
166
|
+
export { CharacterController } from "./gl/CharacterController"
|
|
167
|
+
export { Vehicle } from "./gl/Vehicle"
|
|
168
|
+
export type { DriveLayout, DifferentialMode, EngineConfig, SteeringConfig, AeroConfig } from "./gl/Vehicle"
|
|
169
|
+
export { Wheel } from "./gl/Wheel"
|
|
170
|
+
export { Ragdoll } from "./gl/Ragdoll"
|
|
171
|
+
export type { RagdollPart, RagdollHinge } from "./gl/Ragdoll"
|
|
172
|
+
export type { TireCurve, WheelSuspension, WheelTire, WheelVisual } from "./gl/Wheel"
|
|
173
|
+
export { Gearbox } from "./gl/Gearbox"
|
|
174
|
+
export { AnimationClip } from "./gl/animation/AnimationClip"
|
|
175
|
+
export type { ClipDef, ClipTrackDef, ClipKey } from "./gl/animation/AnimationClip"
|
|
176
|
+
export { Animator } from "./gl/animation/Animator"
|
|
177
|
+
export type { WarpOptions, FeetOptions, FootState } from "./gl/animation/Animator"
|
|
178
|
+
export { Playback } from "./gl/animation/Playback"
|
|
179
|
+
export { Loop } from "./gl/animation/Loop"
|
|
180
|
+
export { Layer } from "./gl/animation/Layer"
|
|
181
|
+
export type { PlayOptions, StopOptions, LayerOptions, LoopOptions, LoopDef, BlendPosition, ActiveClip } from "./gl/animation/core"
|
|
182
|
+
export { IK } from "./gl/IK"
|
|
183
|
+
export type { IKTwoBone, IKLookAt } from "./gl/IK"
|
|
184
|
+
export { Light } from "./gl/Light"
|
|
185
|
+
export { Lightmap } from "./gl/Lightmap"
|
|
186
|
+
export { Terrain } from "./gl/Terrain"
|
|
187
|
+
export type { TerrainOptions, TerrainLayer, TerrainRegion, ConformOptions, RibbonOptions, AutoPaintRules, SculptOptions, TerrainHit, TerrainSnapshot } from "./gl/Terrain"
|
|
188
|
+
export { NavMesh, NavCrowd } from "./gl/NavMesh"
|
|
189
|
+
export type { NavMeshLoadOptions, NavMeshInfo, NavPath, NavQueryOptions, NavRaycastHit, NavAgentSize } from "./gl/NavMesh"
|
|
190
|
+
export { NavAgent } from "./gl/NavAgent"
|
|
191
|
+
export type { NavAgentState, NavAgentAvoidance, NavAgentDrive, NavAgentEvents } from "./gl/NavAgent"
|
|
192
|
+
export { Locomotion } from "./gl/Locomotion"
|
|
193
|
+
export type { LocomotionSet, LocomotionClip, LocomotionTuning, LocomotionState, LocomotionEvents, Displacement, Gait } from "./gl/Locomotion"
|
|
194
|
+
export { Material } from "./gl/Material"
|
|
195
|
+
export { Geometry } from "./gl/Geometry"
|
|
196
|
+
export { InstancedMesh } from "./gl/InstancedMesh"
|
|
197
|
+
export type { InstanceTransform } from "./gl/InstancedMesh"
|
|
198
|
+
export { Texture } from "./gl/Texture"
|
|
199
|
+
export { Camera } from "./gl/Camera"
|
|
200
|
+
export { Ray } from "./gl/Ray"
|
|
201
|
+
export { Plane } from "./gl/Plane"
|
|
202
|
+
export { Noise } from "./gl/Noise"
|
|
203
|
+
export { Particles, Trail, curve, colorCurve } from "./gl/Particles"
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Scalar / general math helpers, exposed to user code as the `Mathf` global. Vector & quaternion
|
|
2
|
+
// helpers live with the engine that uses them (vec2 in g2/, vec3+quat in gl/). `smoothDamp` works on
|
|
3
|
+
// both numbers and numeric arrays here (no gl-matrix), which covers 2D (vec2) and 3D position damping.
|
|
4
|
+
|
|
5
|
+
export type Damper<T extends number | number[]> = ((target: T, dt: number) => T) & {
|
|
6
|
+
/** Snap the damper's internal current value (e.g. on teleport). */
|
|
7
|
+
setValue(value: T): void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const clamp = (v: number, min: number, max: number): number => (v < min ? min : v > max ? max : v)
|
|
11
|
+
|
|
12
|
+
const smoothDampNumber = (initial: number, smoothTime: number, maxSpeed: number): Damper<number> => {
|
|
13
|
+
let current = initial
|
|
14
|
+
let velocity = 0
|
|
15
|
+
const damp = ((target: number, dt: number): number => {
|
|
16
|
+
smoothTime = Math.max(0.0001, smoothTime)
|
|
17
|
+
const omega = 2 / smoothTime
|
|
18
|
+
const x = omega * dt
|
|
19
|
+
const exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x)
|
|
20
|
+
const maxDelta = maxSpeed * smoothTime
|
|
21
|
+
let delta = clamp(current - target, -maxDelta, maxDelta)
|
|
22
|
+
const newTarget = current - delta
|
|
23
|
+
const temp = (velocity + omega * delta) * dt
|
|
24
|
+
velocity = (velocity - omega * temp) * exp
|
|
25
|
+
current = newTarget + (delta + temp) * exp
|
|
26
|
+
return current
|
|
27
|
+
}) as Damper<number>
|
|
28
|
+
damp.setValue = (v) => { current = v; velocity = 0 }
|
|
29
|
+
return damp
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function smoothDamp(initial: number, smoothTime: number, maxSpeed?: number): Damper<number>
|
|
33
|
+
function smoothDamp(initial: number[], smoothTime: number, maxSpeed?: number): Damper<number[]>
|
|
34
|
+
function smoothDamp(initial: any, smoothTime: number, maxSpeed = Infinity): any {
|
|
35
|
+
return typeof initial === "number"
|
|
36
|
+
? smoothDampNumber(initial, smoothTime, maxSpeed)
|
|
37
|
+
: smoothDampArray(initial, smoothTime, maxSpeed)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const smoothDampArray = (initial: number[], smoothTime: number, maxSpeed: number): Damper<number[]> => {
|
|
41
|
+
const n = initial.length
|
|
42
|
+
let current = initial.slice()
|
|
43
|
+
const velocity = new Array(n).fill(0)
|
|
44
|
+
const damp = ((target: number[], dt: number): number[] => {
|
|
45
|
+
smoothTime = Math.max(0.0001, smoothTime)
|
|
46
|
+
const omega = 2 / smoothTime
|
|
47
|
+
const x = omega * dt
|
|
48
|
+
const exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x)
|
|
49
|
+
const maxDelta = maxSpeed * smoothTime
|
|
50
|
+
|
|
51
|
+
const delta = new Array(n)
|
|
52
|
+
let mag = 0
|
|
53
|
+
for (let i = 0; i < n; i++) { delta[i] = current[i] - target[i]; mag += delta[i] * delta[i] }
|
|
54
|
+
mag = Math.sqrt(mag)
|
|
55
|
+
if (mag > maxDelta && mag > 0) {
|
|
56
|
+
const s = maxDelta / mag
|
|
57
|
+
for (let i = 0; i < n; i++) delta[i] *= s
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const out = new Array(n)
|
|
61
|
+
for (let i = 0; i < n; i++) {
|
|
62
|
+
const newTarget = current[i] - delta[i]
|
|
63
|
+
const temp = (velocity[i] + omega * delta[i]) * dt
|
|
64
|
+
velocity[i] = (velocity[i] - omega * temp) * exp
|
|
65
|
+
out[i] = newTarget + (delta[i] + temp) * exp
|
|
66
|
+
}
|
|
67
|
+
current = out
|
|
68
|
+
return out.slice()
|
|
69
|
+
}) as Damper<number[]>
|
|
70
|
+
damp.setValue = (v) => { current = v.slice(); velocity.fill(0) }
|
|
71
|
+
return damp
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const Mathf = {
|
|
75
|
+
PI: Math.PI,
|
|
76
|
+
TAU: Math.PI * 2,
|
|
77
|
+
EPSILON: 1e-6,
|
|
78
|
+
DEG2RAD: Math.PI / 180,
|
|
79
|
+
RAD2DEG: 180 / Math.PI,
|
|
80
|
+
|
|
81
|
+
deg2rad: (deg: number): number => deg * (Math.PI / 180),
|
|
82
|
+
rad2deg: (rad: number): number => rad * (180 / Math.PI),
|
|
83
|
+
|
|
84
|
+
clamp,
|
|
85
|
+
clamp01: (v: number): number => clamp(v, 0, 1),
|
|
86
|
+
lerp: (a: number, b: number, t: number): number => a + (b - a) * t,
|
|
87
|
+
inverseLerp: (a: number, b: number, v: number): number => (a === b ? 0 : (v - a) / (b - a)),
|
|
88
|
+
remap: (v: number, inMin: number, inMax: number, outMin: number, outMax: number): number =>
|
|
89
|
+
outMin + ((v - inMin) / (inMax - inMin)) * (outMax - outMin),
|
|
90
|
+
|
|
91
|
+
/** Move `a` toward `b` by at most `maxDelta`. */
|
|
92
|
+
moveTowards: (a: number, b: number, maxDelta: number): number => {
|
|
93
|
+
const d = b - a
|
|
94
|
+
return Math.abs(d) <= maxDelta ? b : a + Math.sign(d) * maxDelta
|
|
95
|
+
},
|
|
96
|
+
/** Frame-rate-independent exponential approach (good default: lambda 5..15). */
|
|
97
|
+
damp: (a: number, b: number, lambda: number, dt: number): number => a + (b - a) * (1 - Math.exp(-lambda * dt)),
|
|
98
|
+
|
|
99
|
+
sign: Math.sign,
|
|
100
|
+
/** Loop a value into [0, length). */
|
|
101
|
+
repeat: (t: number, length: number): number => clamp(t - Math.floor(t / length) * length, 0, length),
|
|
102
|
+
/** Ping-pong a value in [0, length]. */
|
|
103
|
+
pingPong: (t: number, length: number): number => {
|
|
104
|
+
const r = clamp(t - Math.floor(t / (length * 2)) * (length * 2), 0, length * 2)
|
|
105
|
+
return length - Math.abs(r - length)
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
random: (min = 0, max = 1): number => min + Math.random() * (max - min),
|
|
109
|
+
randomInt: (min: number, max: number): number => Math.floor(min + Math.random() * (max - min + 1)),
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Critically-damped follow (Unity SmoothDamp). Returns a stateful damper you call every frame:
|
|
113
|
+
* const d = Mathf.smoothDamp([x, y], 0.15)
|
|
114
|
+
* pos = d(targetPos, dt)
|
|
115
|
+
* Works on a number or a numeric array (vec2 / vec3).
|
|
116
|
+
*/
|
|
117
|
+
smoothDamp,
|
|
118
|
+
}
|