@haiyue/extensions 0.1.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 (293) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -0
  3. package/dist/animation/Animation2DComponent.d.ts +46 -0
  4. package/dist/animation/Animation2DExtensionRegistry.d.ts +27 -0
  5. package/dist/animation/Animation2DRenderSystem.d.ts +89 -0
  6. package/dist/animation/Animation2DRuntime.d.ts +99 -0
  7. package/dist/animation/Animation2DStateMachine.d.ts +13 -0
  8. package/dist/animation/Animation2DStateMachineComponent.d.ts +54 -0
  9. package/dist/animation/Animation2DStateMachineRuntime.d.ts +30 -0
  10. package/dist/animation/Animation2DStateMachineSystem.d.ts +14 -0
  11. package/dist/animation/Animation2DStateMachineVisualRuntime.d.ts +27 -0
  12. package/dist/animation/Animation2DSystem.d.ts +14 -0
  13. package/dist/animation/AnimationAssetLoader.d.ts +9 -0
  14. package/dist/animation/AnimationAudioClip.d.ts +26 -0
  15. package/dist/animation/AnimationPathTessellator.d.ts +52 -0
  16. package/dist/animation/AnimationTextRasterizer.d.ts +21 -0
  17. package/dist/animation/AnimationVisual2D.d.ts +68 -0
  18. package/dist/animation/HyaAnimation2DClipAdapter.d.ts +16 -0
  19. package/dist/animation/Particle2DDescriptorAdapter.d.ts +5 -0
  20. package/dist/animation/runtime/mixer/Action.d.ts +150 -0
  21. package/dist/animation/runtime/mixer/Mixer.d.ts +54 -0
  22. package/dist/animation/runtime/mixer/PoseBuffer.d.ts +23 -0
  23. package/dist/animation/runtime/mixer/Sampler.d.ts +17 -0
  24. package/dist/animation/runtime/mixer/Types.d.ts +101 -0
  25. package/dist/animation/runtime/mixer/index.d.ts +6 -0
  26. package/dist/animation-state-machine/AnimationStateMachine.d.ts +111 -0
  27. package/dist/animation-state-machine/AnimationStateMachineChannels.d.ts +23 -0
  28. package/dist/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.d.ts +51 -0
  29. package/dist/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.d.ts +14 -0
  30. package/dist/animation-state-machine/runtime/AnimationStateMachineCompiler.d.ts +109 -0
  31. package/dist/animation-state-machine/runtime/AnimationStateMachineController.d.ts +103 -0
  32. package/dist/animation-state-machine/runtime/AnimationStateMachineMixerPort.d.ts +35 -0
  33. package/dist/animation-state-machine/runtime/index.d.ts +7 -0
  34. package/dist/animation.d.ts +15 -0
  35. package/dist/animation.js +1171 -0
  36. package/dist/animation3d/Animation3DAction.d.ts +46 -0
  37. package/dist/animation3d/Animation3DBinding.d.ts +85 -0
  38. package/dist/animation3d/Animation3DClip.d.ts +20 -0
  39. package/dist/animation3d/Animation3DError.d.ts +13 -0
  40. package/dist/animation3d/Animation3DMixer.d.ts +27 -0
  41. package/dist/animation3d/Animation3DPose.d.ts +40 -0
  42. package/dist/animation3d/Animation3DPoseBuffer.d.ts +24 -0
  43. package/dist/animation3d/Animation3DResource.d.ts +28 -0
  44. package/dist/animation3d/Animation3DStateMachine.d.ts +95 -0
  45. package/dist/animation3d/Animation3DStateMachineRuntime.d.ts +70 -0
  46. package/dist/animation3d/Animation3DTrack.d.ts +19 -0
  47. package/dist/animation3d/hya/HyaAnimation3DRuntime.d.ts +73 -0
  48. package/dist/animation3d/hya/HyaAnimation3DStateMachineRuntime.d.ts +65 -0
  49. package/dist/animation3d/hya/HyaAnimation3DTypes.d.ts +170 -0
  50. package/dist/animation3d/hya/index.d.ts +4 -0
  51. package/dist/animation3d/index.d.ts +14 -0
  52. package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.d.ts +81 -0
  53. package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.d.ts +39 -0
  54. package/dist/animation3d/runtime/integration/index.d.ts +2 -0
  55. package/dist/animation3d/runtime/mixer/Action.d.ts +150 -0
  56. package/dist/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.d.ts +6 -0
  57. package/dist/animation3d/runtime/mixer/Mixer.d.ts +59 -0
  58. package/dist/animation3d/runtime/mixer/PoseBuffer.d.ts +1 -0
  59. package/dist/animation3d/runtime/mixer/TrackSampler.d.ts +24 -0
  60. package/dist/animation3d/runtime/mixer/index.d.ts +4 -0
  61. package/dist/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.d.ts +1 -0
  62. package/dist/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.d.ts +1 -0
  63. package/dist/animation3d/runtime/state-machine/Animation3DStateMachineController.d.ts +1 -0
  64. package/dist/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.d.ts +1 -0
  65. package/dist/animation3d/runtime/state-machine/index.d.ts +6 -0
  66. package/dist/animation3d.d.ts +5 -0
  67. package/dist/animation3d.js +1708 -0
  68. package/dist/benchmark.d.ts +30 -0
  69. package/dist/benchmark.js +119 -0
  70. package/dist/canvas-text/CanvasText2DRenderSystem.d.ts +24 -0
  71. package/dist/canvas-text/CanvasTextComponent.d.ts +18 -0
  72. package/dist/canvas-text.d.ts +4 -0
  73. package/dist/canvas-text.js +276 -0
  74. package/dist/chunks/2d-ui-animation-2d.generated-D4aHzFU2.js +4 -0
  75. package/dist/chunks/2d-ui-canvas-text-2d.generated-C0Eaej2m.js +4 -0
  76. package/dist/chunks/2d-ui-spine2d.generated-CUUi8xjl.js +4 -0
  77. package/dist/chunks/2d-ui-tilemap2d.generated-gebw4mX7.js +4 -0
  78. package/dist/chunks/Animation2DStateMachine-9IonguNa.js +3245 -0
  79. package/dist/chunks/AnimationStateMachineController-BIcAh-sT.js +1438 -0
  80. package/dist/chunks/GltfAnimation3DAdapter-Cu3rQQsW.js +2240 -0
  81. package/dist/chunks/GltfAnimationRuntime-CRHsLycN.js +631 -0
  82. package/dist/chunks/RenderSystem2DBase-DDT61dAu.js +227 -0
  83. package/dist/chunks/Spine2DRuntime-D9M9LAQR.js +3056 -0
  84. package/dist/chunks/SpineAssetParser-BSDmPge1.js +378 -0
  85. package/dist/chunks/gltfLoader-Dm22jLYB.js +2085 -0
  86. package/dist/chunks/pluginUtils-CvwroRMd.js +33 -0
  87. package/dist/experimental-gltf-worker.d.ts +4 -0
  88. package/dist/experimental-gltf-worker.js +138 -0
  89. package/dist/experimental-spine-worker.d.ts +4 -0
  90. package/dist/experimental-spine-worker.js +110 -0
  91. package/dist/gltf/GltfAccessorReader.d.ts +7 -0
  92. package/dist/gltf/GltfAnimation3DAdapter.d.ts +47 -0
  93. package/dist/gltf/GltfAnimationRuntime.d.ts +10 -0
  94. package/dist/gltf/GltfAssetWorkerClient.d.ts +20 -0
  95. package/dist/gltf/GltfCompatibilityReport.d.ts +70 -0
  96. package/dist/gltf/GltfConservativeBounds.d.ts +18 -0
  97. package/dist/gltf/GltfDracoDecoder.d.ts +4 -0
  98. package/dist/gltf/GltfExtensionAdapter.d.ts +54 -0
  99. package/dist/gltf/GltfLoaderContract.d.ts +249 -0
  100. package/dist/gltf/GltfLoaderErrors.d.ts +4 -0
  101. package/dist/gltf/GltfMaterialDescriptor.d.ts +23 -0
  102. package/dist/gltf/GltfMaterialEncoder.d.ts +20 -0
  103. package/dist/gltf/GltfMaterialLoader.d.ts +6 -0
  104. package/dist/gltf/GltfModelComponent.d.ts +72 -0
  105. package/dist/gltf/GltfModelSystem.d.ts +37 -0
  106. package/dist/gltf/GltfPlugin.d.ts +6 -0
  107. package/dist/gltf/GltfSchema.d.ts +225 -0
  108. package/dist/gltf/GltfUvSemanticPlanner.d.ts +33 -0
  109. package/dist/gltf/gltfLoader.d.ts +9 -0
  110. package/dist/gltf-animation3d.d.ts +3 -0
  111. package/dist/gltf-animation3d.js +8 -0
  112. package/dist/gltf-worker-runtime.d.ts +7 -0
  113. package/dist/gltf-worker-runtime.js +6981 -0
  114. package/dist/gltf.d.ts +12 -0
  115. package/dist/gltf.js +517 -0
  116. package/dist/grid/Grid2DComponent.d.ts +25 -0
  117. package/dist/grid.d.ts +2 -0
  118. package/dist/grid.js +56 -0
  119. package/dist/hya-state-machine.d.ts +7 -0
  120. package/dist/hya-state-machine.js +2934 -0
  121. package/dist/index.d.ts +2 -0
  122. package/dist/index.js +7 -0
  123. package/dist/internal/2d-ui-shader-artifact.js +870 -0
  124. package/dist/plugins/pluginUtils.d.ts +20 -0
  125. package/dist/plugins.d.ts +6 -0
  126. package/dist/shaders/generated/2d-ui-artifact.generated.d.ts +658 -0
  127. package/dist/spine/Spine2DComponent.d.ts +116 -0
  128. package/dist/spine/Spine2DGpuRenderer.d.ts +65 -0
  129. package/dist/spine/Spine2DRenderSystem.d.ts +27 -0
  130. package/dist/spine/Spine2DRuntime.d.ts +103 -0
  131. package/dist/spine/SpineAnimationBoneTimelines.d.ts +8 -0
  132. package/dist/spine/SpineAssetParser.d.ts +18 -0
  133. package/dist/spine/SpineAssetWorkerClient.d.ts +19 -0
  134. package/dist/spine/SpineAssetWorkerContract.d.ts +6 -0
  135. package/dist/spine/SpineAtlasParser.d.ts +21 -0
  136. package/dist/spine/SpineFloatBuilder.d.ts +15 -0
  137. package/dist/spine/SpinePathConstraintSolver.d.ts +32 -0
  138. package/dist/spine/SpinePlugin.d.ts +8 -0
  139. package/dist/spine/SpineSkeletonRuntime.d.ts +185 -0
  140. package/dist/spine/SpineSlotAnimation.d.ts +17 -0
  141. package/dist/spine/SpineTimelineRuntime.d.ts +25 -0
  142. package/dist/spine/SpineVertexBuilder.d.ts +61 -0
  143. package/dist/spine.d.ts +7 -0
  144. package/dist/spine.js +578 -0
  145. package/dist/tilemap/Tilemap2DComponent.d.ts +110 -0
  146. package/dist/tilemap/Tilemap2DRenderSystem.d.ts +11 -0
  147. package/dist/tilemap/Tilemap2DRenderer.d.ts +24 -0
  148. package/dist/tilemap/TilemapPlugin.d.ts +8 -0
  149. package/dist/tilemap.d.ts +6 -0
  150. package/dist/tilemap.js +535 -0
  151. package/dist/tween/Tween2DComponent.d.ts +70 -0
  152. package/dist/tween/Tween2DSystem.d.ts +12 -0
  153. package/dist/tween.d.ts +4 -0
  154. package/dist/tween.js +138 -0
  155. package/dist/utils/RenderSystem2DBase.d.ts +42 -0
  156. package/dist/utils/arrayAccess.d.ts +4 -0
  157. package/dist/utils/camera2d.d.ts +12 -0
  158. package/dist/utils/render2dGpu.d.ts +32 -0
  159. package/package.json +124 -0
  160. package/src/animation/Animation2DComponent.ts +135 -0
  161. package/src/animation/Animation2DExtensionRegistry.ts +52 -0
  162. package/src/animation/Animation2DRenderSystem.ts +1026 -0
  163. package/src/animation/Animation2DRuntime.ts +1326 -0
  164. package/src/animation/Animation2DStateMachine.ts +39 -0
  165. package/src/animation/Animation2DStateMachineComponent.ts +228 -0
  166. package/src/animation/Animation2DStateMachineRuntime.ts +141 -0
  167. package/src/animation/Animation2DStateMachineSystem.ts +60 -0
  168. package/src/animation/Animation2DStateMachineVisualRuntime.ts +282 -0
  169. package/src/animation/Animation2DSystem.ts +72 -0
  170. package/src/animation/AnimationAssetLoader.ts +84 -0
  171. package/src/animation/AnimationAudioClip.ts +140 -0
  172. package/src/animation/AnimationPathTessellator.ts +691 -0
  173. package/src/animation/AnimationTextRasterizer.ts +340 -0
  174. package/src/animation/AnimationVisual2D.ts +153 -0
  175. package/src/animation/HyaAnimation2DClipAdapter.ts +845 -0
  176. package/src/animation/Particle2DDescriptorAdapter.ts +35 -0
  177. package/src/animation/runtime/mixer/Action.ts +876 -0
  178. package/src/animation/runtime/mixer/Mixer.ts +505 -0
  179. package/src/animation/runtime/mixer/PoseBuffer.ts +105 -0
  180. package/src/animation/runtime/mixer/Sampler.ts +252 -0
  181. package/src/animation/runtime/mixer/Types.ts +138 -0
  182. package/src/animation/runtime/mixer/index.ts +31 -0
  183. package/src/animation-state-machine/AnimationStateMachine.ts +146 -0
  184. package/src/animation-state-machine/AnimationStateMachineChannels.ts +116 -0
  185. package/src/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.ts +197 -0
  186. package/src/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.ts +503 -0
  187. package/src/animation-state-machine/runtime/AnimationStateMachineCompiler.ts +763 -0
  188. package/src/animation-state-machine/runtime/AnimationStateMachineController.ts +987 -0
  189. package/src/animation-state-machine/runtime/AnimationStateMachineMixerPort.ts +54 -0
  190. package/src/animation-state-machine/runtime/index.ts +47 -0
  191. package/src/animation.ts +33 -0
  192. package/src/animation3d/Animation3DAction.ts +73 -0
  193. package/src/animation3d/Animation3DBinding.ts +116 -0
  194. package/src/animation3d/Animation3DClip.ts +22 -0
  195. package/src/animation3d/Animation3DError.ts +34 -0
  196. package/src/animation3d/Animation3DMixer.ts +82 -0
  197. package/src/animation3d/Animation3DPose.ts +100 -0
  198. package/src/animation3d/Animation3DPoseBuffer.ts +85 -0
  199. package/src/animation3d/Animation3DResource.ts +49 -0
  200. package/src/animation3d/Animation3DStateMachine.ts +126 -0
  201. package/src/animation3d/Animation3DStateMachineRuntime.ts +191 -0
  202. package/src/animation3d/Animation3DTrack.ts +29 -0
  203. package/src/animation3d/hya/HyaAnimation3DRuntime.ts +756 -0
  204. package/src/animation3d/hya/HyaAnimation3DStateMachineRuntime.ts +618 -0
  205. package/src/animation3d/hya/HyaAnimation3DTypes.ts +168 -0
  206. package/src/animation3d/hya/index.ts +10 -0
  207. package/src/animation3d/index.ts +76 -0
  208. package/src/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.ts +325 -0
  209. package/src/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.ts +158 -0
  210. package/src/animation3d/runtime/integration/index.ts +9 -0
  211. package/src/animation3d/runtime/mixer/Action.ts +833 -0
  212. package/src/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.ts +26 -0
  213. package/src/animation3d/runtime/mixer/Mixer.ts +562 -0
  214. package/src/animation3d/runtime/mixer/PoseBuffer.ts +1 -0
  215. package/src/animation3d/runtime/mixer/TrackSampler.ts +399 -0
  216. package/src/animation3d/runtime/mixer/index.ts +8 -0
  217. package/src/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.ts +4 -0
  218. package/src/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.ts +1 -0
  219. package/src/animation3d/runtime/state-machine/Animation3DStateMachineController.ts +1 -0
  220. package/src/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.ts +1 -0
  221. package/src/animation3d/runtime/state-machine/index.ts +45 -0
  222. package/src/animation3d.ts +5 -0
  223. package/src/benchmark.ts +168 -0
  224. package/src/canvas-text/CanvasText2DRenderSystem.ts +292 -0
  225. package/src/canvas-text/CanvasTextComponent.ts +47 -0
  226. package/src/canvas-text.ts +4 -0
  227. package/src/experimental-gltf-worker.ts +18 -0
  228. package/src/experimental-spine-worker.ts +9 -0
  229. package/src/gltf/GltfAccessorReader.ts +271 -0
  230. package/src/gltf/GltfAnimation3DAdapter.ts +680 -0
  231. package/src/gltf/GltfAnimationRuntime.ts +392 -0
  232. package/src/gltf/GltfAssetWorkerClient.ts +136 -0
  233. package/src/gltf/GltfCompatibilityReport.ts +214 -0
  234. package/src/gltf/GltfConservativeBounds.ts +219 -0
  235. package/src/gltf/GltfDracoDecoder.ts +251 -0
  236. package/src/gltf/GltfExtensionAdapter.ts +340 -0
  237. package/src/gltf/GltfLoaderContract.ts +333 -0
  238. package/src/gltf/GltfLoaderErrors.ts +59 -0
  239. package/src/gltf/GltfMaterialDescriptor.ts +134 -0
  240. package/src/gltf/GltfMaterialEncoder.ts +173 -0
  241. package/src/gltf/GltfMaterialLoader.ts +293 -0
  242. package/src/gltf/GltfModelComponent.ts +115 -0
  243. package/src/gltf/GltfModelSystem.ts +208 -0
  244. package/src/gltf/GltfPlugin.ts +84 -0
  245. package/src/gltf/GltfSchema.ts +229 -0
  246. package/src/gltf/GltfUvSemanticPlanner.ts +166 -0
  247. package/src/gltf/gltfLoader.ts +910 -0
  248. package/src/gltf-animation3d.ts +10 -0
  249. package/src/gltf-worker-runtime.ts +10 -0
  250. package/src/gltf.ts +56 -0
  251. package/src/grid/Grid2DComponent.ts +69 -0
  252. package/src/grid.ts +2 -0
  253. package/src/hya-state-machine.ts +19 -0
  254. package/src/index.ts +2 -0
  255. package/src/plugins/pluginUtils.ts +50 -0
  256. package/src/plugins.ts +6 -0
  257. package/src/shaders/generated/2d-ui-animation-2d.generated.wgsl +242 -0
  258. package/src/shaders/generated/2d-ui-artifact.generated.ts +867 -0
  259. package/src/shaders/generated/2d-ui-canvas-text-2d.generated.wgsl +38 -0
  260. package/src/shaders/generated/2d-ui-spine2d.generated.wgsl +41 -0
  261. package/src/shaders/generated/2d-ui-tilemap2d.generated.wgsl +36 -0
  262. package/src/spine/Spine2DComponent.ts +168 -0
  263. package/src/spine/Spine2DGpuRenderer.ts +449 -0
  264. package/src/spine/Spine2DRenderSystem.ts +199 -0
  265. package/src/spine/Spine2DRuntime.ts +1529 -0
  266. package/src/spine/SpineAnimationBoneTimelines.ts +30 -0
  267. package/src/spine/SpineAssetParser.ts +20 -0
  268. package/src/spine/SpineAssetWorkerClient.ts +112 -0
  269. package/src/spine/SpineAssetWorkerContract.ts +4 -0
  270. package/src/spine/SpineAtlasParser.ts +127 -0
  271. package/src/spine/SpineFloatBuilder.ts +66 -0
  272. package/src/spine/SpinePathConstraintSolver.ts +582 -0
  273. package/src/spine/SpinePlugin.ts +76 -0
  274. package/src/spine/SpineSkeletonRuntime.ts +521 -0
  275. package/src/spine/SpineSlotAnimation.ts +195 -0
  276. package/src/spine/SpineTimelineRuntime.ts +453 -0
  277. package/src/spine/SpineVertexBuilder.ts +686 -0
  278. package/src/spine.ts +7 -0
  279. package/src/tilemap/Tilemap2DComponent.ts +232 -0
  280. package/src/tilemap/Tilemap2DRenderSystem.ts +58 -0
  281. package/src/tilemap/Tilemap2DRenderer.ts +289 -0
  282. package/src/tilemap/TilemapPlugin.ts +75 -0
  283. package/src/tilemap.ts +6 -0
  284. package/src/tween/Tween2DComponent.ts +83 -0
  285. package/src/tween/Tween2DSystem.ts +78 -0
  286. package/src/tween.ts +4 -0
  287. package/src/types/earcut.d.ts +7 -0
  288. package/src/types/wgsl.d.ts +4 -0
  289. package/src/utils/RenderSystem2DBase.ts +164 -0
  290. package/src/utils/arrayAccess.ts +17 -0
  291. package/src/utils/camera2d.ts +68 -0
  292. package/src/utils/render2dGpu.ts +132 -0
  293. package/test/fixtures/gltf/animation-characterization.gltf +344 -0
@@ -0,0 +1,39 @@
1
+ import type {
2
+ AnimationStateMachineDefinition,
3
+ } from '../animation-state-machine/AnimationStateMachine.js';
4
+ import {
5
+ AnimationStateMachineController,
6
+ compileAnimationStateMachine,
7
+ type AnimationStateMachineActionHandle,
8
+ type AnimationStateMachineControllerOptions,
9
+ type AnimationStateMachineMixerPort,
10
+ } from '../animation-state-machine/runtime/index.js';
11
+
12
+ export type Animation2DStateMachineActionHandle =
13
+ AnimationStateMachineActionHandle;
14
+ export type Animation2DStateMachineMixerPort<
15
+ TAction extends Animation2DStateMachineActionHandle =
16
+ Animation2DStateMachineActionHandle,
17
+ > = AnimationStateMachineMixerPort<TAction>;
18
+
19
+ /**
20
+ * Creates a shared state-machine controller for a 2D mixer adapter.
21
+ *
22
+ * Animation2DComponent remains a single-composition player. A port must own
23
+ * the 2D pose sampling, blending, discrete-property policy, and side-effect
24
+ * lifecycle; this factory intentionally does not emulate those semantics by
25
+ * switching component timelines.
26
+ */
27
+ export function createAnimation2DStateMachineController<
28
+ TAction extends Animation2DStateMachineActionHandle,
29
+ >(
30
+ definition: AnimationStateMachineDefinition,
31
+ port: Animation2DStateMachineMixerPort<TAction>,
32
+ options?: AnimationStateMachineControllerOptions,
33
+ ): AnimationStateMachineController<TAction> {
34
+ return new AnimationStateMachineController(
35
+ compileAnimationStateMachine(definition),
36
+ port,
37
+ options,
38
+ );
39
+ }
@@ -0,0 +1,228 @@
1
+ import {
2
+ parseAnimation,
3
+ type AnimationSource,
4
+ type HyaStateMachineExtension,
5
+ type HyaStateMachineParameter,
6
+ type ParsedAnimation,
7
+ } from '@haiyue/animation-spec';
8
+ import { Component, type Entity, type World } from '@haiyue/engine';
9
+ import { ComponentLifecycleFlags, UniqueCheckType } from '@haiyue/engine/ecs';
10
+ import type { AnimationStateMachineLayerSnapshot } from '../animation-state-machine/runtime/index.js';
11
+ import type { AnimationStateMachineChannelDiagnostic } from '../animation-state-machine/AnimationStateMachineChannels.js';
12
+ import type { Animation2DExtensionRegistry } from './Animation2DExtensionRegistry.js';
13
+ import type {
14
+ Animation2DStateMachineParameterValue,
15
+ Animation2DStateMachineRuntime,
16
+ } from './Animation2DStateMachineRuntime.js';
17
+ import type { Animation2DRuntimeStats } from './Animation2DComponent.js';
18
+ import { getHyaStateMachineExtension } from './HyaAnimation2DClipAdapter.js';
19
+
20
+ export interface Animation2DStateMachineComponentOptions {
21
+ autoplay?: boolean;
22
+ speed?: number;
23
+ runtimeExtensions?: Animation2DExtensionRegistry;
24
+ }
25
+
26
+ /**
27
+ * Plays multiple named animation ranges and a state machine from one HYA
28
+ * asset. Transitions blend one pose onto one generated scene hierarchy.
29
+ */
30
+ export class Animation2DStateMachineComponent extends Component {
31
+ static override UniqueCheckType = UniqueCheckType.SYMBOL | UniqueCheckType.REPLACE;
32
+ static override UniqueSymbol = Symbol.for('Animation2DStateMachineComponent');
33
+ static override Lifecycle = ComponentLifecycleFlags.EntityRemoveComponent
34
+ | ComponentLifecycleFlags.EntityRemoveFromWorld;
35
+
36
+ readonly animation: ParsedAnimation;
37
+ readonly stateMachineExtension: HyaStateMachineExtension;
38
+ readonly runtimeExtensions: Animation2DExtensionRegistry | undefined;
39
+ playing: boolean;
40
+ speed: number;
41
+ _runtime: Animation2DStateMachineRuntime | null = null;
42
+ _needsUpdate = true;
43
+
44
+ private readonly _parametersByName: ReadonlyMap<string, HyaStateMachineParameter>;
45
+ private readonly _parameterValues = new Map<string, Animation2DStateMachineParameterValue>();
46
+
47
+ constructor(
48
+ source: AnimationSource | ParsedAnimation,
49
+ options: Animation2DStateMachineComponentOptions = {},
50
+ ) {
51
+ super('Animation2DStateMachineComponent');
52
+ this.animation = isParsed(source) ? source : parseAnimation(source);
53
+ this.stateMachineExtension = getHyaStateMachineExtension(this.animation);
54
+ this.runtimeExtensions = options.runtimeExtensions;
55
+ this.playing = options.autoplay ?? true;
56
+ this.speed = finiteSpeed(options.speed ?? 1);
57
+ this._parametersByName = new Map(
58
+ this.stateMachineExtension.stateMachine.parameters.map(parameter => [parameter.name, parameter]),
59
+ );
60
+ }
61
+
62
+ get runtimeStats(): Animation2DRuntimeStats {
63
+ return this._runtime?.stats ?? EMPTY_RUNTIME_STATS;
64
+ }
65
+
66
+ get layerSnapshots(): readonly AnimationStateMachineLayerSnapshot[] {
67
+ if (this._runtime) return this._runtime.layerSnapshots;
68
+ return Object.freeze(this.stateMachineExtension.stateMachine.layers.map(layer => Object.freeze({
69
+ layerId: layer.id,
70
+ currentStateId: layer.initialStateId,
71
+ currentTime: 0,
72
+ transitionId: null,
73
+ sourceStateId: null,
74
+ destinationStateId: null,
75
+ transitionProgress: 0,
76
+ })));
77
+ }
78
+
79
+ get channelDiagnostics(): readonly AnimationStateMachineChannelDiagnostic[] {
80
+ return this._runtime?.diagnostics ?? EMPTY_CHANNEL_DIAGNOSTICS;
81
+ }
82
+
83
+ get liveActionCount(): number { return this._runtime?.liveActionCount ?? 0; }
84
+ get liveBindingCount(): number { return this._runtime?.liveBindingCount ?? 0; }
85
+ get sideEffectOwnerCount(): number { return this._runtime?.sideEffectOwnerCount ?? 0; }
86
+
87
+ play(): this {
88
+ this.playing = true;
89
+ this._runtime?.setPlaying(true);
90
+ this._needsUpdate = true;
91
+ return this;
92
+ }
93
+
94
+ pause(): this {
95
+ this.playing = false;
96
+ this._runtime?.setPlaying(false);
97
+ return this;
98
+ }
99
+
100
+ setSpeed(speed: number): this {
101
+ this.speed = finiteSpeed(speed);
102
+ return this;
103
+ }
104
+
105
+ setFloat(name: string, value: number): this {
106
+ if (!Number.isFinite(value)) throw new RangeError('Float parameters must be finite.');
107
+ this._setParameter(name, 'float', value);
108
+ this._runtime?.controller.setFloat(name, value);
109
+ return this;
110
+ }
111
+
112
+ setInteger(name: string, value: number): this {
113
+ if (!Number.isSafeInteger(value)) throw new RangeError('Integer parameters must be safe integers.');
114
+ this._setParameter(name, 'integer', value);
115
+ this._runtime?.controller.setInteger(name, value);
116
+ return this;
117
+ }
118
+
119
+ setBoolean(name: string, value: boolean): this {
120
+ if (typeof value !== 'boolean') throw new TypeError('Boolean parameters require a boolean.');
121
+ this._setParameter(name, 'boolean', value);
122
+ this._runtime?.controller.setBoolean(name, value);
123
+ return this;
124
+ }
125
+
126
+ setTrigger(name: string): this {
127
+ this._setParameter(name, 'trigger', true);
128
+ this._runtime?.controller.setTrigger(name);
129
+ return this;
130
+ }
131
+
132
+ resetTrigger(name: string): this {
133
+ this._setParameter(name, 'trigger', false);
134
+ this._runtime?.controller.resetTrigger(name);
135
+ return this;
136
+ }
137
+
138
+ getParameter(name: string): number | boolean {
139
+ const parameter = this._requireParameter(name);
140
+ if (this._runtime) return this._runtime.controller.getParameter(name);
141
+ if (this._parameterValues.has(name)) return this._parameterValues.get(name)!;
142
+ return parameter.type === 'trigger' ? false : parameter.defaultValue;
143
+ }
144
+
145
+ reset(): this {
146
+ this._parameterValues.clear();
147
+ this._runtime?.reset();
148
+ this._needsUpdate = this._runtime === null;
149
+ return this;
150
+ }
151
+
152
+ onEntityRemoveComponent(_entity: Entity, component: Component): void {
153
+ if (component === this) this._disposeRuntime();
154
+ }
155
+
156
+ onEntityRemoveFromWorld(_entity: Entity, _world: World): void {
157
+ this._disposeRuntime();
158
+ }
159
+
160
+ override clone(): Animation2DStateMachineComponent {
161
+ const clone = new Animation2DStateMachineComponent(this.animation, {
162
+ autoplay: this.playing,
163
+ speed: this.speed,
164
+ ...(this.runtimeExtensions ? { runtimeExtensions: this.runtimeExtensions } : {}),
165
+ });
166
+ for (const parameter of this.stateMachineExtension.stateMachine.parameters) {
167
+ clone._parameterValues.set(parameter.name, this.getParameter(parameter.name));
168
+ }
169
+ clone.disabled = this.disabled;
170
+ return clone;
171
+ }
172
+
173
+ _getParameterValues(): ReadonlyMap<string, Animation2DStateMachineParameterValue> {
174
+ return this._parameterValues;
175
+ }
176
+
177
+ _disposeRuntime(): void {
178
+ if (!this._runtime) return;
179
+ for (const parameter of this.stateMachineExtension.stateMachine.parameters) {
180
+ this._parameterValues.set(parameter.name, this._runtime.controller.getParameter(parameter.name));
181
+ }
182
+ this._runtime.destroy();
183
+ this._runtime = null;
184
+ this._needsUpdate = true;
185
+ }
186
+
187
+ private _setParameter(
188
+ name: string,
189
+ type: HyaStateMachineParameter['type'],
190
+ value: Animation2DStateMachineParameterValue,
191
+ ): void {
192
+ const parameter = this._requireParameter(name);
193
+ if (parameter.type !== type) {
194
+ throw new TypeError(`State-machine parameter "${name}" is ${parameter.type}, not ${type}.`);
195
+ }
196
+ this._parameterValues.set(name, value);
197
+ this._needsUpdate = true;
198
+ }
199
+
200
+ private _requireParameter(name: string): HyaStateMachineParameter {
201
+ const parameter = this._parametersByName.get(name);
202
+ if (!parameter) throw new ReferenceError(`Unknown state-machine parameter "${name}".`);
203
+ return parameter;
204
+ }
205
+ }
206
+
207
+ const EMPTY_RUNTIME_STATS: Animation2DRuntimeStats = Object.freeze({
208
+ nodeCount: 0,
209
+ visualCount: 0,
210
+ unsupportedComponentCount: 0,
211
+ pendingResourceCount: 0,
212
+ failedResourceCount: 0,
213
+ textCount: 0,
214
+ particleCount: 0,
215
+ audioCount: 0,
216
+ });
217
+ const EMPTY_CHANNEL_DIAGNOSTICS: readonly AnimationStateMachineChannelDiagnostic[] = Object.freeze([]);
218
+
219
+ function isParsed(source: AnimationSource | ParsedAnimation): source is ParsedAnimation {
220
+ return typeof source === 'object' && source !== null && 'source' in source;
221
+ }
222
+
223
+ function finiteSpeed(value: number): number {
224
+ if (!Number.isFinite(value) || value < 0) {
225
+ throw new RangeError('Animation2D state-machine speed must be finite and non-negative.');
226
+ }
227
+ return value;
228
+ }
@@ -0,0 +1,141 @@
1
+ import type {
2
+ HyaStateMachineExtension,
3
+ ParsedAnimation,
4
+ } from '@haiyue/animation-spec';
5
+ import type { Entity } from '@haiyue/engine';
6
+ import type { AssetManager } from '@haiyue/engine/assets';
7
+ import type {
8
+ AnimationStateMachineDefinition,
9
+ } from '../animation-state-machine/AnimationStateMachine.js';
10
+ import type {
11
+ AnimationStateMachineController,
12
+ AnimationStateMachineLayerSnapshot,
13
+ } from '../animation-state-machine/runtime/index.js';
14
+ import {
15
+ Animation2DStateMachineMixerAdapter,
16
+ runAnimationStateMachineControllerUpdateTransaction,
17
+ type Animation2DStateMachineActionRuntimeHandle,
18
+ } from '../animation-state-machine/runtime/index.js';
19
+ import type { AnimationStateMachineChannelDiagnostic } from '../animation-state-machine/AnimationStateMachineChannels.js';
20
+ import { createAnimation2DStateMachineController } from './Animation2DStateMachine.js';
21
+ import type { Animation2DExtensionRegistry } from './Animation2DExtensionRegistry.js';
22
+ import { Animation2DStateMachineVisualRuntime } from './Animation2DStateMachineVisualRuntime.js';
23
+ import { createHyaAnimation2DClips } from './HyaAnimation2DClipAdapter.js';
24
+ import {
25
+ Animation2DMixerRuntime,
26
+ Animation2DPoseBuffer,
27
+ } from './runtime/mixer/index.js';
28
+ import type { Animation2DRuntimeStats } from './Animation2DComponent.js';
29
+
30
+ export type Animation2DStateMachineParameterValue = number | boolean;
31
+
32
+ export class Animation2DStateMachineRuntime {
33
+ private readonly _mixer: Animation2DMixerRuntime;
34
+ private readonly _adapter: Animation2DStateMachineMixerAdapter;
35
+ private readonly _pose = new Animation2DPoseBuffer();
36
+ private readonly _visual!: Animation2DStateMachineVisualRuntime;
37
+ private _destroyed = false;
38
+
39
+ readonly controller!: AnimationStateMachineController<Animation2DStateMachineActionRuntimeHandle>;
40
+
41
+ constructor(
42
+ owner: Entity,
43
+ animation: ParsedAnimation,
44
+ readonly extension: HyaStateMachineExtension,
45
+ parameterValues: ReadonlyMap<string, Animation2DStateMachineParameterValue>,
46
+ runtimeExtensions?: Animation2DExtensionRegistry,
47
+ assetManager?: AssetManager,
48
+ ) {
49
+ const clips = createHyaAnimation2DClips(animation, extension);
50
+ this._mixer = new Animation2DMixerRuntime(clips);
51
+ this._adapter = new Animation2DStateMachineMixerAdapter(this._mixer);
52
+ let visual: Animation2DStateMachineVisualRuntime | undefined;
53
+ try {
54
+ this.controller = createAnimation2DStateMachineController(
55
+ extension.stateMachine as AnimationStateMachineDefinition,
56
+ this._adapter,
57
+ );
58
+ applyParameterValues(this.controller, extension, parameterValues);
59
+ this.controller.update(0);
60
+ visual = new Animation2DStateMachineVisualRuntime(
61
+ owner,
62
+ animation,
63
+ runtimeExtensions,
64
+ assetManager,
65
+ );
66
+ this._visual = visual;
67
+ } catch (error) {
68
+ visual?.destroy();
69
+ this.controller?.destroy();
70
+ this._adapter.destroy();
71
+ this._mixer.destroy();
72
+ throw error;
73
+ }
74
+ }
75
+
76
+ get stats(): Animation2DRuntimeStats { return this._visual.stats; }
77
+ get diagnostics(): readonly AnimationStateMachineChannelDiagnostic[] { return this._visual.diagnostics; }
78
+ get liveActionCount(): number { return this._adapter.liveActionCount; }
79
+ get liveBindingCount(): number { return this._adapter.liveBindingCount; }
80
+ get sideEffectOwnerCount(): number { return this._visual.sideEffectOwnerCount; }
81
+ get layerSnapshots(): readonly AnimationStateMachineLayerSnapshot[] {
82
+ return this.controller.layerSnapshots;
83
+ }
84
+
85
+ update(deltaSeconds: number, playing: boolean, speed: number): void {
86
+ this._requireActive();
87
+ const pose = runAnimationStateMachineControllerUpdateTransaction(
88
+ this.controller,
89
+ playing ? deltaSeconds * speed : 0,
90
+ () => undefined,
91
+ () => this._mixer.evaluate(this._pose),
92
+ );
93
+ this._visual.applyPose(pose, playing, speed);
94
+ }
95
+
96
+ setPlaying(playing: boolean): void {
97
+ this._requireActive();
98
+ this._visual.setPlaying(playing);
99
+ }
100
+
101
+ reset(): void {
102
+ this._requireActive();
103
+ this._visual.resetSideEffects();
104
+ this.controller.reset();
105
+ const pose = runAnimationStateMachineControllerUpdateTransaction(
106
+ this.controller,
107
+ 0,
108
+ () => undefined,
109
+ () => this._mixer.evaluate(this._pose),
110
+ );
111
+ this._visual.applyPose(pose, false, 1);
112
+ }
113
+
114
+ destroy(): void {
115
+ if (this._destroyed) return;
116
+ this._destroyed = true;
117
+ this.controller.destroy();
118
+ this._adapter.destroy();
119
+ this._mixer.destroy();
120
+ this._visual.destroy();
121
+ }
122
+
123
+ private _requireActive(): void {
124
+ if (this._destroyed) throw new Error('Animation2D state-machine runtime has been destroyed.');
125
+ }
126
+ }
127
+
128
+ function applyParameterValues(
129
+ controller: Animation2DStateMachineRuntime['controller'],
130
+ extension: HyaStateMachineExtension,
131
+ values: ReadonlyMap<string, Animation2DStateMachineParameterValue>,
132
+ ): void {
133
+ for (const parameter of extension.stateMachine.parameters) {
134
+ if (!values.has(parameter.name)) continue;
135
+ const value = values.get(parameter.name)!;
136
+ if (parameter.type === 'float') controller.setFloat(parameter.name, value as number);
137
+ else if (parameter.type === 'integer') controller.setInteger(parameter.name, value as number);
138
+ else if (parameter.type === 'boolean') controller.setBoolean(parameter.name, value as boolean);
139
+ else if (value) controller.setTrigger(parameter.name);
140
+ }
141
+ }
@@ -0,0 +1,60 @@
1
+ import { System, type Entity, type World } from '@haiyue/engine';
2
+ import type { AssetManager } from '@haiyue/engine/assets';
3
+ import { Animation2DStateMachineComponent } from './Animation2DStateMachineComponent.js';
4
+ import { Animation2DStateMachineRuntime } from './Animation2DStateMachineRuntime.js';
5
+
6
+ export interface Animation2DStateMachineSystemOptions {
7
+ priority?: number;
8
+ /** Shared engine asset manager used by sprite2d resources. */
9
+ assetManager?: AssetManager;
10
+ }
11
+
12
+ export class Animation2DStateMachineSystem extends System {
13
+ private readonly _assetManager: AssetManager | undefined;
14
+
15
+ constructor(options: Animation2DStateMachineSystemOptions = {}) {
16
+ super({ all: [Animation2DStateMachineComponent] });
17
+ this.name = 'Animation2DStateMachineSystem';
18
+ if (options.priority !== undefined) this.priority = options.priority;
19
+ this._assetManager = options.assetManager;
20
+ }
21
+
22
+ override update(world: World, _time: number, delta: number): this {
23
+ if (this.disabled) return this;
24
+ const entities = this.entitySet.get(world);
25
+ if (!entities) return this;
26
+ for (const entity of entities) this._updateEntity(entity, delta);
27
+ return this;
28
+ }
29
+
30
+ override destroy(): this {
31
+ for (const world of this.usedBy) {
32
+ const entities = this.entitySet.get(world);
33
+ if (!entities) continue;
34
+ for (const entity of entities) {
35
+ entity.getComponent(Animation2DStateMachineComponent)?._disposeRuntime();
36
+ }
37
+ }
38
+ return super.destroy();
39
+ }
40
+
41
+ private _updateEntity(entity: Entity, delta: number): void {
42
+ const component = entity.getComponent(Animation2DStateMachineComponent);
43
+ if (!component) return;
44
+ if (component.disabled) {
45
+ component._disposeRuntime();
46
+ return;
47
+ }
48
+ component._runtime ??= new Animation2DStateMachineRuntime(
49
+ entity,
50
+ component.animation,
51
+ component.stateMachineExtension,
52
+ component._getParameterValues(),
53
+ component.runtimeExtensions,
54
+ this._assetManager,
55
+ );
56
+ if ((!component.playing || component.speed === 0) && !component._needsUpdate) return;
57
+ component._runtime.update(Math.max(0, delta) * 0.001, component.playing, component.speed);
58
+ component._needsUpdate = false;
59
+ }
60
+ }