@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.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/animation/Animation2DComponent.d.ts +46 -0
- package/dist/animation/Animation2DExtensionRegistry.d.ts +27 -0
- package/dist/animation/Animation2DRenderSystem.d.ts +89 -0
- package/dist/animation/Animation2DRuntime.d.ts +99 -0
- package/dist/animation/Animation2DStateMachine.d.ts +13 -0
- package/dist/animation/Animation2DStateMachineComponent.d.ts +54 -0
- package/dist/animation/Animation2DStateMachineRuntime.d.ts +30 -0
- package/dist/animation/Animation2DStateMachineSystem.d.ts +14 -0
- package/dist/animation/Animation2DStateMachineVisualRuntime.d.ts +27 -0
- package/dist/animation/Animation2DSystem.d.ts +14 -0
- package/dist/animation/AnimationAssetLoader.d.ts +9 -0
- package/dist/animation/AnimationAudioClip.d.ts +26 -0
- package/dist/animation/AnimationPathTessellator.d.ts +52 -0
- package/dist/animation/AnimationTextRasterizer.d.ts +21 -0
- package/dist/animation/AnimationVisual2D.d.ts +68 -0
- package/dist/animation/HyaAnimation2DClipAdapter.d.ts +16 -0
- package/dist/animation/Particle2DDescriptorAdapter.d.ts +5 -0
- package/dist/animation/runtime/mixer/Action.d.ts +150 -0
- package/dist/animation/runtime/mixer/Mixer.d.ts +54 -0
- package/dist/animation/runtime/mixer/PoseBuffer.d.ts +23 -0
- package/dist/animation/runtime/mixer/Sampler.d.ts +17 -0
- package/dist/animation/runtime/mixer/Types.d.ts +101 -0
- package/dist/animation/runtime/mixer/index.d.ts +6 -0
- package/dist/animation-state-machine/AnimationStateMachine.d.ts +111 -0
- package/dist/animation-state-machine/AnimationStateMachineChannels.d.ts +23 -0
- package/dist/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.d.ts +51 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.d.ts +14 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineCompiler.d.ts +109 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineController.d.ts +103 -0
- package/dist/animation-state-machine/runtime/AnimationStateMachineMixerPort.d.ts +35 -0
- package/dist/animation-state-machine/runtime/index.d.ts +7 -0
- package/dist/animation.d.ts +15 -0
- package/dist/animation.js +1171 -0
- package/dist/animation3d/Animation3DAction.d.ts +46 -0
- package/dist/animation3d/Animation3DBinding.d.ts +85 -0
- package/dist/animation3d/Animation3DClip.d.ts +20 -0
- package/dist/animation3d/Animation3DError.d.ts +13 -0
- package/dist/animation3d/Animation3DMixer.d.ts +27 -0
- package/dist/animation3d/Animation3DPose.d.ts +40 -0
- package/dist/animation3d/Animation3DPoseBuffer.d.ts +24 -0
- package/dist/animation3d/Animation3DResource.d.ts +28 -0
- package/dist/animation3d/Animation3DStateMachine.d.ts +95 -0
- package/dist/animation3d/Animation3DStateMachineRuntime.d.ts +70 -0
- package/dist/animation3d/Animation3DTrack.d.ts +19 -0
- package/dist/animation3d/hya/HyaAnimation3DRuntime.d.ts +73 -0
- package/dist/animation3d/hya/HyaAnimation3DStateMachineRuntime.d.ts +65 -0
- package/dist/animation3d/hya/HyaAnimation3DTypes.d.ts +170 -0
- package/dist/animation3d/hya/index.d.ts +4 -0
- package/dist/animation3d/index.d.ts +14 -0
- package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.d.ts +81 -0
- package/dist/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.d.ts +39 -0
- package/dist/animation3d/runtime/integration/index.d.ts +2 -0
- package/dist/animation3d/runtime/mixer/Action.d.ts +150 -0
- package/dist/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.d.ts +6 -0
- package/dist/animation3d/runtime/mixer/Mixer.d.ts +59 -0
- package/dist/animation3d/runtime/mixer/PoseBuffer.d.ts +1 -0
- package/dist/animation3d/runtime/mixer/TrackSampler.d.ts +24 -0
- package/dist/animation3d/runtime/mixer/index.d.ts +4 -0
- package/dist/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineController.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.d.ts +1 -0
- package/dist/animation3d/runtime/state-machine/index.d.ts +6 -0
- package/dist/animation3d.d.ts +5 -0
- package/dist/animation3d.js +1708 -0
- package/dist/benchmark.d.ts +30 -0
- package/dist/benchmark.js +119 -0
- package/dist/canvas-text/CanvasText2DRenderSystem.d.ts +24 -0
- package/dist/canvas-text/CanvasTextComponent.d.ts +18 -0
- package/dist/canvas-text.d.ts +4 -0
- package/dist/canvas-text.js +276 -0
- package/dist/chunks/2d-ui-animation-2d.generated-D4aHzFU2.js +4 -0
- package/dist/chunks/2d-ui-canvas-text-2d.generated-C0Eaej2m.js +4 -0
- package/dist/chunks/2d-ui-spine2d.generated-CUUi8xjl.js +4 -0
- package/dist/chunks/2d-ui-tilemap2d.generated-gebw4mX7.js +4 -0
- package/dist/chunks/Animation2DStateMachine-9IonguNa.js +3245 -0
- package/dist/chunks/AnimationStateMachineController-BIcAh-sT.js +1438 -0
- package/dist/chunks/GltfAnimation3DAdapter-Cu3rQQsW.js +2240 -0
- package/dist/chunks/GltfAnimationRuntime-CRHsLycN.js +631 -0
- package/dist/chunks/RenderSystem2DBase-DDT61dAu.js +227 -0
- package/dist/chunks/Spine2DRuntime-D9M9LAQR.js +3056 -0
- package/dist/chunks/SpineAssetParser-BSDmPge1.js +378 -0
- package/dist/chunks/gltfLoader-Dm22jLYB.js +2085 -0
- package/dist/chunks/pluginUtils-CvwroRMd.js +33 -0
- package/dist/experimental-gltf-worker.d.ts +4 -0
- package/dist/experimental-gltf-worker.js +138 -0
- package/dist/experimental-spine-worker.d.ts +4 -0
- package/dist/experimental-spine-worker.js +110 -0
- package/dist/gltf/GltfAccessorReader.d.ts +7 -0
- package/dist/gltf/GltfAnimation3DAdapter.d.ts +47 -0
- package/dist/gltf/GltfAnimationRuntime.d.ts +10 -0
- package/dist/gltf/GltfAssetWorkerClient.d.ts +20 -0
- package/dist/gltf/GltfCompatibilityReport.d.ts +70 -0
- package/dist/gltf/GltfConservativeBounds.d.ts +18 -0
- package/dist/gltf/GltfDracoDecoder.d.ts +4 -0
- package/dist/gltf/GltfExtensionAdapter.d.ts +54 -0
- package/dist/gltf/GltfLoaderContract.d.ts +249 -0
- package/dist/gltf/GltfLoaderErrors.d.ts +4 -0
- package/dist/gltf/GltfMaterialDescriptor.d.ts +23 -0
- package/dist/gltf/GltfMaterialEncoder.d.ts +20 -0
- package/dist/gltf/GltfMaterialLoader.d.ts +6 -0
- package/dist/gltf/GltfModelComponent.d.ts +72 -0
- package/dist/gltf/GltfModelSystem.d.ts +37 -0
- package/dist/gltf/GltfPlugin.d.ts +6 -0
- package/dist/gltf/GltfSchema.d.ts +225 -0
- package/dist/gltf/GltfUvSemanticPlanner.d.ts +33 -0
- package/dist/gltf/gltfLoader.d.ts +9 -0
- package/dist/gltf-animation3d.d.ts +3 -0
- package/dist/gltf-animation3d.js +8 -0
- package/dist/gltf-worker-runtime.d.ts +7 -0
- package/dist/gltf-worker-runtime.js +6981 -0
- package/dist/gltf.d.ts +12 -0
- package/dist/gltf.js +517 -0
- package/dist/grid/Grid2DComponent.d.ts +25 -0
- package/dist/grid.d.ts +2 -0
- package/dist/grid.js +56 -0
- package/dist/hya-state-machine.d.ts +7 -0
- package/dist/hya-state-machine.js +2934 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/internal/2d-ui-shader-artifact.js +870 -0
- package/dist/plugins/pluginUtils.d.ts +20 -0
- package/dist/plugins.d.ts +6 -0
- package/dist/shaders/generated/2d-ui-artifact.generated.d.ts +658 -0
- package/dist/spine/Spine2DComponent.d.ts +116 -0
- package/dist/spine/Spine2DGpuRenderer.d.ts +65 -0
- package/dist/spine/Spine2DRenderSystem.d.ts +27 -0
- package/dist/spine/Spine2DRuntime.d.ts +103 -0
- package/dist/spine/SpineAnimationBoneTimelines.d.ts +8 -0
- package/dist/spine/SpineAssetParser.d.ts +18 -0
- package/dist/spine/SpineAssetWorkerClient.d.ts +19 -0
- package/dist/spine/SpineAssetWorkerContract.d.ts +6 -0
- package/dist/spine/SpineAtlasParser.d.ts +21 -0
- package/dist/spine/SpineFloatBuilder.d.ts +15 -0
- package/dist/spine/SpinePathConstraintSolver.d.ts +32 -0
- package/dist/spine/SpinePlugin.d.ts +8 -0
- package/dist/spine/SpineSkeletonRuntime.d.ts +185 -0
- package/dist/spine/SpineSlotAnimation.d.ts +17 -0
- package/dist/spine/SpineTimelineRuntime.d.ts +25 -0
- package/dist/spine/SpineVertexBuilder.d.ts +61 -0
- package/dist/spine.d.ts +7 -0
- package/dist/spine.js +578 -0
- package/dist/tilemap/Tilemap2DComponent.d.ts +110 -0
- package/dist/tilemap/Tilemap2DRenderSystem.d.ts +11 -0
- package/dist/tilemap/Tilemap2DRenderer.d.ts +24 -0
- package/dist/tilemap/TilemapPlugin.d.ts +8 -0
- package/dist/tilemap.d.ts +6 -0
- package/dist/tilemap.js +535 -0
- package/dist/tween/Tween2DComponent.d.ts +70 -0
- package/dist/tween/Tween2DSystem.d.ts +12 -0
- package/dist/tween.d.ts +4 -0
- package/dist/tween.js +138 -0
- package/dist/utils/RenderSystem2DBase.d.ts +42 -0
- package/dist/utils/arrayAccess.d.ts +4 -0
- package/dist/utils/camera2d.d.ts +12 -0
- package/dist/utils/render2dGpu.d.ts +32 -0
- package/package.json +124 -0
- package/src/animation/Animation2DComponent.ts +135 -0
- package/src/animation/Animation2DExtensionRegistry.ts +52 -0
- package/src/animation/Animation2DRenderSystem.ts +1026 -0
- package/src/animation/Animation2DRuntime.ts +1326 -0
- package/src/animation/Animation2DStateMachine.ts +39 -0
- package/src/animation/Animation2DStateMachineComponent.ts +228 -0
- package/src/animation/Animation2DStateMachineRuntime.ts +141 -0
- package/src/animation/Animation2DStateMachineSystem.ts +60 -0
- package/src/animation/Animation2DStateMachineVisualRuntime.ts +282 -0
- package/src/animation/Animation2DSystem.ts +72 -0
- package/src/animation/AnimationAssetLoader.ts +84 -0
- package/src/animation/AnimationAudioClip.ts +140 -0
- package/src/animation/AnimationPathTessellator.ts +691 -0
- package/src/animation/AnimationTextRasterizer.ts +340 -0
- package/src/animation/AnimationVisual2D.ts +153 -0
- package/src/animation/HyaAnimation2DClipAdapter.ts +845 -0
- package/src/animation/Particle2DDescriptorAdapter.ts +35 -0
- package/src/animation/runtime/mixer/Action.ts +876 -0
- package/src/animation/runtime/mixer/Mixer.ts +505 -0
- package/src/animation/runtime/mixer/PoseBuffer.ts +105 -0
- package/src/animation/runtime/mixer/Sampler.ts +252 -0
- package/src/animation/runtime/mixer/Types.ts +138 -0
- package/src/animation/runtime/mixer/index.ts +31 -0
- package/src/animation-state-machine/AnimationStateMachine.ts +146 -0
- package/src/animation-state-machine/AnimationStateMachineChannels.ts +116 -0
- package/src/animation-state-machine/runtime/Animation2DStateMachineMixerAdapter.ts +197 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineBlendTreeWeights.ts +503 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineCompiler.ts +763 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineController.ts +987 -0
- package/src/animation-state-machine/runtime/AnimationStateMachineMixerPort.ts +54 -0
- package/src/animation-state-machine/runtime/index.ts +47 -0
- package/src/animation.ts +33 -0
- package/src/animation3d/Animation3DAction.ts +73 -0
- package/src/animation3d/Animation3DBinding.ts +116 -0
- package/src/animation3d/Animation3DClip.ts +22 -0
- package/src/animation3d/Animation3DError.ts +34 -0
- package/src/animation3d/Animation3DMixer.ts +82 -0
- package/src/animation3d/Animation3DPose.ts +100 -0
- package/src/animation3d/Animation3DPoseBuffer.ts +85 -0
- package/src/animation3d/Animation3DResource.ts +49 -0
- package/src/animation3d/Animation3DStateMachine.ts +126 -0
- package/src/animation3d/Animation3DStateMachineRuntime.ts +191 -0
- package/src/animation3d/Animation3DTrack.ts +29 -0
- package/src/animation3d/hya/HyaAnimation3DRuntime.ts +756 -0
- package/src/animation3d/hya/HyaAnimation3DStateMachineRuntime.ts +618 -0
- package/src/animation3d/hya/HyaAnimation3DTypes.ts +168 -0
- package/src/animation3d/hya/index.ts +10 -0
- package/src/animation3d/index.ts +76 -0
- package/src/animation3d/runtime/integration/Animation3DStateMachineMixerAdapter.ts +325 -0
- package/src/animation3d/runtime/integration/Animation3DStateMachineMixerIntegration.ts +158 -0
- package/src/animation3d/runtime/integration/index.ts +9 -0
- package/src/animation3d/runtime/mixer/Action.ts +833 -0
- package/src/animation3d/runtime/mixer/Animation3DMixerRuntimeStore.ts +26 -0
- package/src/animation3d/runtime/mixer/Mixer.ts +562 -0
- package/src/animation3d/runtime/mixer/PoseBuffer.ts +1 -0
- package/src/animation3d/runtime/mixer/TrackSampler.ts +399 -0
- package/src/animation3d/runtime/mixer/index.ts +8 -0
- package/src/animation3d/runtime/state-machine/Animation3DBlendTreeWeights.ts +4 -0
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineCompiler.ts +1 -0
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineController.ts +1 -0
- package/src/animation3d/runtime/state-machine/Animation3DStateMachineMixerPort.ts +1 -0
- package/src/animation3d/runtime/state-machine/index.ts +45 -0
- package/src/animation3d.ts +5 -0
- package/src/benchmark.ts +168 -0
- package/src/canvas-text/CanvasText2DRenderSystem.ts +292 -0
- package/src/canvas-text/CanvasTextComponent.ts +47 -0
- package/src/canvas-text.ts +4 -0
- package/src/experimental-gltf-worker.ts +18 -0
- package/src/experimental-spine-worker.ts +9 -0
- package/src/gltf/GltfAccessorReader.ts +271 -0
- package/src/gltf/GltfAnimation3DAdapter.ts +680 -0
- package/src/gltf/GltfAnimationRuntime.ts +392 -0
- package/src/gltf/GltfAssetWorkerClient.ts +136 -0
- package/src/gltf/GltfCompatibilityReport.ts +214 -0
- package/src/gltf/GltfConservativeBounds.ts +219 -0
- package/src/gltf/GltfDracoDecoder.ts +251 -0
- package/src/gltf/GltfExtensionAdapter.ts +340 -0
- package/src/gltf/GltfLoaderContract.ts +333 -0
- package/src/gltf/GltfLoaderErrors.ts +59 -0
- package/src/gltf/GltfMaterialDescriptor.ts +134 -0
- package/src/gltf/GltfMaterialEncoder.ts +173 -0
- package/src/gltf/GltfMaterialLoader.ts +293 -0
- package/src/gltf/GltfModelComponent.ts +115 -0
- package/src/gltf/GltfModelSystem.ts +208 -0
- package/src/gltf/GltfPlugin.ts +84 -0
- package/src/gltf/GltfSchema.ts +229 -0
- package/src/gltf/GltfUvSemanticPlanner.ts +166 -0
- package/src/gltf/gltfLoader.ts +910 -0
- package/src/gltf-animation3d.ts +10 -0
- package/src/gltf-worker-runtime.ts +10 -0
- package/src/gltf.ts +56 -0
- package/src/grid/Grid2DComponent.ts +69 -0
- package/src/grid.ts +2 -0
- package/src/hya-state-machine.ts +19 -0
- package/src/index.ts +2 -0
- package/src/plugins/pluginUtils.ts +50 -0
- package/src/plugins.ts +6 -0
- package/src/shaders/generated/2d-ui-animation-2d.generated.wgsl +242 -0
- package/src/shaders/generated/2d-ui-artifact.generated.ts +867 -0
- package/src/shaders/generated/2d-ui-canvas-text-2d.generated.wgsl +38 -0
- package/src/shaders/generated/2d-ui-spine2d.generated.wgsl +41 -0
- package/src/shaders/generated/2d-ui-tilemap2d.generated.wgsl +36 -0
- package/src/spine/Spine2DComponent.ts +168 -0
- package/src/spine/Spine2DGpuRenderer.ts +449 -0
- package/src/spine/Spine2DRenderSystem.ts +199 -0
- package/src/spine/Spine2DRuntime.ts +1529 -0
- package/src/spine/SpineAnimationBoneTimelines.ts +30 -0
- package/src/spine/SpineAssetParser.ts +20 -0
- package/src/spine/SpineAssetWorkerClient.ts +112 -0
- package/src/spine/SpineAssetWorkerContract.ts +4 -0
- package/src/spine/SpineAtlasParser.ts +127 -0
- package/src/spine/SpineFloatBuilder.ts +66 -0
- package/src/spine/SpinePathConstraintSolver.ts +582 -0
- package/src/spine/SpinePlugin.ts +76 -0
- package/src/spine/SpineSkeletonRuntime.ts +521 -0
- package/src/spine/SpineSlotAnimation.ts +195 -0
- package/src/spine/SpineTimelineRuntime.ts +453 -0
- package/src/spine/SpineVertexBuilder.ts +686 -0
- package/src/spine.ts +7 -0
- package/src/tilemap/Tilemap2DComponent.ts +232 -0
- package/src/tilemap/Tilemap2DRenderSystem.ts +58 -0
- package/src/tilemap/Tilemap2DRenderer.ts +289 -0
- package/src/tilemap/TilemapPlugin.ts +75 -0
- package/src/tilemap.ts +6 -0
- package/src/tween/Tween2DComponent.ts +83 -0
- package/src/tween/Tween2DSystem.ts +78 -0
- package/src/tween.ts +4 -0
- package/src/types/earcut.d.ts +7 -0
- package/src/types/wgsl.d.ts +4 -0
- package/src/utils/RenderSystem2DBase.ts +164 -0
- package/src/utils/arrayAccess.ts +17 -0
- package/src/utils/camera2d.ts +68 -0
- package/src/utils/render2dGpu.ts +132 -0
- package/test/fixtures/gltf/animation-characterization.gltf +344 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SpineAnimationBoneTimelines,
|
|
3
|
+
SpineAnimationData,
|
|
4
|
+
} from './SpineSkeletonRuntime';
|
|
5
|
+
|
|
6
|
+
interface SpineAnimationBoneTimelineEntry {
|
|
7
|
+
boneName: string;
|
|
8
|
+
timelines: SpineAnimationBoneTimelines;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const cache = new WeakMap<object, SpineAnimationBoneTimelineEntry[]>();
|
|
12
|
+
const EMPTY: SpineAnimationBoneTimelineEntry[] = [];
|
|
13
|
+
|
|
14
|
+
export function compileAnimationBoneTimelines(animations: Record<string, SpineAnimationData>): void {
|
|
15
|
+
for (const animation of Object.values(animations)) getAnimationBoneTimelines(animation);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function getAnimationBoneTimelines(
|
|
19
|
+
animation: SpineAnimationData | undefined,
|
|
20
|
+
): SpineAnimationBoneTimelineEntry[] {
|
|
21
|
+
if (!animation || typeof animation !== 'object') return EMPTY;
|
|
22
|
+
const cached = cache.get(animation);
|
|
23
|
+
if (cached) return cached;
|
|
24
|
+
const entries: SpineAnimationBoneTimelineEntry[] = [];
|
|
25
|
+
for (const [boneName, timelines] of Object.entries(animation.bones ?? {})) {
|
|
26
|
+
entries.push({ boneName, timelines });
|
|
27
|
+
}
|
|
28
|
+
cache.set(animation, entries);
|
|
29
|
+
return entries;
|
|
30
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { parseAtlas, type AtlasRegion } from './SpineAtlasParser';
|
|
2
|
+
import { normalizeSpineData } from './SpineSkeletonRuntime';
|
|
3
|
+
|
|
4
|
+
export interface SpineAssetParserInput { json: unknown; atlasText: string; }
|
|
5
|
+
/** Structured-clone-safe parser boundary; the runtime validates and narrows this payload before use. */
|
|
6
|
+
export interface SpineParsedAsset { data: unknown; regions: Array<[string, AtlasRegion]>; }
|
|
7
|
+
|
|
8
|
+
export const SPINE_ASSET_PARSER = Object.freeze({
|
|
9
|
+
type: 'skeleton/spine',
|
|
10
|
+
parse(input: SpineAssetParserInput, _context?: { signal?: AbortSignal; source?: string }): SpineParsedAsset {
|
|
11
|
+
return {
|
|
12
|
+
data: normalizeSpineData(input.json),
|
|
13
|
+
regions: [...parseAtlas(input.atlasText).entries()],
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export function parseSpineAssetPayload(input: SpineAssetParserInput): SpineParsedAsset {
|
|
19
|
+
return SPINE_ASSET_PARSER.parse(input, { source: 'spine' });
|
|
20
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { WorkerChannel, type WorkerChannelLike } from '@haiyue/engine/experimental/async';
|
|
2
|
+
import type { SpineParsedAsset } from './SpineAssetParser';
|
|
3
|
+
import type { SpineAssetWorker } from './SpineAssetWorkerContract';
|
|
4
|
+
|
|
5
|
+
type SpineWorkerLike = WorkerChannelLike;
|
|
6
|
+
|
|
7
|
+
export class SpineAssetWorkerClient implements SpineAssetWorker {
|
|
8
|
+
private readonly channel: WorkerChannel;
|
|
9
|
+
|
|
10
|
+
constructor(worker: SpineWorkerLike, private readonly objectUrl: string | null = null) {
|
|
11
|
+
this.channel = new WorkerChannel(worker, {
|
|
12
|
+
label: 'Spine asset worker',
|
|
13
|
+
path: 'spine.worker',
|
|
14
|
+
context: { resourceType: 'skeleton/spine' },
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
loadParsedAsset(jsonUrl: string, atlasUrl: string, options: { signal?: AbortSignal } = {}): Promise<SpineParsedAsset> {
|
|
19
|
+
return this.channel.request('loadParsedSpineAsset', { jsonUrl, atlasUrl }, {
|
|
20
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
21
|
+
context: { url: jsonUrl },
|
|
22
|
+
abortMessage: 'Spine load aborted.',
|
|
23
|
+
validate: isSpineParsedAsset,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
dispose(options: { terminate?: boolean } = {}): void {
|
|
28
|
+
const alreadyDisposed = this.channel.isDisposed;
|
|
29
|
+
this.channel.dispose(options);
|
|
30
|
+
if (!alreadyDisposed && this.objectUrl) URL.revokeObjectURL(this.objectUrl);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isSpineParsedAsset(value: unknown): value is SpineParsedAsset {
|
|
35
|
+
if (!value || typeof value !== 'object') return false;
|
|
36
|
+
const candidate = value as { data?: unknown; regions?: unknown };
|
|
37
|
+
const data = candidate.data as { bones?: unknown } | null | undefined;
|
|
38
|
+
return !!data && typeof data === 'object'
|
|
39
|
+
&& Array.isArray(data.bones)
|
|
40
|
+
&& Array.isArray(candidate.regions);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
export function createSpineAssetWorkerSource(extensionSpineModuleUrl: string): string {
|
|
45
|
+
return `
|
|
46
|
+
const parserModulePromise = import(${JSON.stringify(extensionSpineModuleUrl)});
|
|
47
|
+
const requests = new Map();
|
|
48
|
+
const assetError = (message, code, path, recovery, context, cause) => Object.assign(new Error(message), {
|
|
49
|
+
domain: 'component', code, path, recovery, recoverable: recovery === 'retry', context, cause,
|
|
50
|
+
});
|
|
51
|
+
self.addEventListener('message', async event => {
|
|
52
|
+
const request = event.data;
|
|
53
|
+
if (!request || request.version !== 1 || typeof request.id !== 'number') return;
|
|
54
|
+
if (request.type === 'cancel') { requests.get(request.id)?.abort('cancelled'); requests.delete(request.id); return; }
|
|
55
|
+
if (request.type !== 'loadParsedSpineAsset') return;
|
|
56
|
+
const controller = new AbortController();
|
|
57
|
+
requests.set(request.id, controller);
|
|
58
|
+
try {
|
|
59
|
+
const [parser, jsonResponse, atlasResponse] = await Promise.all([
|
|
60
|
+
parserModulePromise,
|
|
61
|
+
fetch(request.jsonUrl, { signal: controller.signal }),
|
|
62
|
+
request.atlasUrl ? fetch(request.atlasUrl, { signal: controller.signal }) : Promise.resolve(null),
|
|
63
|
+
]);
|
|
64
|
+
if (!jsonResponse.ok) throw assetError(
|
|
65
|
+
'Failed to load Spine JSON: ' + jsonResponse.status + ' ' + jsonResponse.statusText,
|
|
66
|
+
'E_ASSET_LOAD_FAILED', 'spine.json', 'retry',
|
|
67
|
+
{ url: request.jsonUrl, resourceType: 'skeleton/spine-json', status: jsonResponse.status },
|
|
68
|
+
);
|
|
69
|
+
if (atlasResponse && !atlasResponse.ok) throw assetError(
|
|
70
|
+
'Failed to load Spine atlas: ' + atlasResponse.status + ' ' + atlasResponse.statusText,
|
|
71
|
+
'E_ASSET_LOAD_FAILED', 'spine.atlas', 'retry',
|
|
72
|
+
{ url: request.atlasUrl, resourceType: 'skeleton/spine-atlas', status: atlasResponse.status },
|
|
73
|
+
);
|
|
74
|
+
let json;
|
|
75
|
+
try { json = await jsonResponse.json(); }
|
|
76
|
+
catch (cause) {
|
|
77
|
+
throw assetError(
|
|
78
|
+
'Spine JSON is not valid JSON.', 'E_ASSET_INVALID_DATA', 'spine.json', 'release-resource',
|
|
79
|
+
{ url: request.jsonUrl, resourceType: 'skeleton/spine-json' }, cause,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
const value = parser.parseSpineAssetPayload({
|
|
83
|
+
json,
|
|
84
|
+
atlasText: atlasResponse ? await atlasResponse.text() : '',
|
|
85
|
+
});
|
|
86
|
+
self.postMessage({ version: 1, id: request.id, ok: true, value });
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (controller.signal.aborted) return;
|
|
89
|
+
const serialized = error && typeof error.toJSON === 'function' ? error.toJSON() : {
|
|
90
|
+
name: 'EngineError', domain: error && error.domain || 'asset', code: error && error.code || 'E_ASSET_INVALID_DATA',
|
|
91
|
+
message: error instanceof Error ? error.message : String(error),
|
|
92
|
+
recovery: error && error.recovery || 'release-resource',
|
|
93
|
+
recoverable: error && typeof error.recoverable === 'boolean' ? error.recoverable : false,
|
|
94
|
+
context: Object.assign({ url: request.jsonUrl, resourceType: 'skeleton/spine' }, error && error.context || {}),
|
|
95
|
+
path: error && error.path || 'spine.parse',
|
|
96
|
+
cause: error instanceof Error ? { name: error.name, message: error.message, stack: error.stack } : undefined,
|
|
97
|
+
};
|
|
98
|
+
self.postMessage({ version: 1, id: request.id, ok: false, error: serialized });
|
|
99
|
+
} finally { requests.delete(request.id); }
|
|
100
|
+
});
|
|
101
|
+
`.trim();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function createInlineSpineAssetWorkerClient(extensionSpineModuleUrl: string, options?: WorkerOptions): SpineAssetWorkerClient {
|
|
105
|
+
const blob = new Blob([createSpineAssetWorkerSource(extensionSpineModuleUrl)], { type: 'text/javascript' });
|
|
106
|
+
const url = URL.createObjectURL(blob);
|
|
107
|
+
return new SpineAssetWorkerClient(new Worker(url, { type: 'module', ...options }), url);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function createSpineAssetWorkerClientFromUrl(url: string | URL, options?: WorkerOptions): SpineAssetWorkerClient {
|
|
111
|
+
return new SpineAssetWorkerClient(new Worker(url, options));
|
|
112
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { EngineError, EngineErrorCode } from '@haiyue/engine';
|
|
2
|
+
import { ErrorDomain, ErrorRecovery } from '@haiyue/engine/core';
|
|
3
|
+
|
|
4
|
+
export interface AtlasRegion {
|
|
5
|
+
name: string;
|
|
6
|
+
page: string;
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
originalWidth: number;
|
|
12
|
+
originalHeight: number;
|
|
13
|
+
offsetX: number;
|
|
14
|
+
offsetY: number;
|
|
15
|
+
rotate: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ResolveAtlasPageImageOptions {
|
|
19
|
+
atlasUrl: string;
|
|
20
|
+
imageUrl: string;
|
|
21
|
+
imageUrls: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseAtlas(text: string): Map<string, AtlasRegion> {
|
|
25
|
+
const regions = new Map<string, AtlasRegion>();
|
|
26
|
+
const lines = text.split(/\r?\n/);
|
|
27
|
+
let page = '';
|
|
28
|
+
for (let i = 0; i < lines.length; i++) {
|
|
29
|
+
const sourceLine = lines[i];
|
|
30
|
+
if (sourceLine === undefined) continue;
|
|
31
|
+
const line = sourceLine.trim();
|
|
32
|
+
if (!line) continue;
|
|
33
|
+
if (!line.includes(':')) {
|
|
34
|
+
const next = lines[i + 1]?.trim() ?? '';
|
|
35
|
+
if (next.startsWith('size:') || next.startsWith('format:') || next.startsWith('filter:') || next.startsWith('repeat:')) {
|
|
36
|
+
page = line;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const name = line;
|
|
40
|
+
const region: AtlasRegion = {
|
|
41
|
+
name,
|
|
42
|
+
page,
|
|
43
|
+
x: 0,
|
|
44
|
+
y: 0,
|
|
45
|
+
width: 1,
|
|
46
|
+
height: 1,
|
|
47
|
+
originalWidth: 0,
|
|
48
|
+
originalHeight: 0,
|
|
49
|
+
offsetX: 0,
|
|
50
|
+
offsetY: 0,
|
|
51
|
+
rotate: 0,
|
|
52
|
+
};
|
|
53
|
+
while (i + 1 < lines.length) {
|
|
54
|
+
const propertyLine = lines[i + 1];
|
|
55
|
+
if (propertyLine === undefined || !propertyLine.includes(':')) break;
|
|
56
|
+
i++;
|
|
57
|
+
const separator = propertyLine.indexOf(':');
|
|
58
|
+
const key = propertyLine.slice(0, separator).trim();
|
|
59
|
+
const raw = propertyLine.slice(separator + 1).trim();
|
|
60
|
+
const nums = raw.split(',').map(Number);
|
|
61
|
+
const numberAt = (index: number, fallback: number): number => {
|
|
62
|
+
const value = nums[index];
|
|
63
|
+
return value !== undefined && Number.isFinite(value) ? value : fallback;
|
|
64
|
+
};
|
|
65
|
+
if (key === 'rotate') region.rotate = raw === 'true' ? 90 : raw === 'false' ? 0 : Number(raw) || 0;
|
|
66
|
+
if (key === 'bounds') {
|
|
67
|
+
region.x = numberAt(0, region.x);
|
|
68
|
+
region.y = numberAt(1, region.y);
|
|
69
|
+
region.width = numberAt(2, region.width);
|
|
70
|
+
region.height = numberAt(3, region.height);
|
|
71
|
+
}
|
|
72
|
+
if (key === 'xy') {
|
|
73
|
+
region.x = numberAt(0, region.x);
|
|
74
|
+
region.y = numberAt(1, region.y);
|
|
75
|
+
}
|
|
76
|
+
if (key === 'size') {
|
|
77
|
+
region.width = numberAt(0, region.width);
|
|
78
|
+
region.height = numberAt(1, region.height);
|
|
79
|
+
}
|
|
80
|
+
if (key === 'orig') {
|
|
81
|
+
region.originalWidth = numberAt(0, region.originalWidth);
|
|
82
|
+
region.originalHeight = numberAt(1, region.originalHeight);
|
|
83
|
+
}
|
|
84
|
+
if (key === 'offsets') {
|
|
85
|
+
region.offsetX = numberAt(0, region.offsetX);
|
|
86
|
+
region.offsetY = numberAt(1, region.offsetY);
|
|
87
|
+
region.originalWidth = numberAt(2, region.originalWidth);
|
|
88
|
+
region.originalHeight = numberAt(3, region.originalHeight);
|
|
89
|
+
}
|
|
90
|
+
if (key === 'offset') {
|
|
91
|
+
region.offsetX = numberAt(0, region.offsetX);
|
|
92
|
+
region.offsetY = numberAt(1, region.offsetY);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (!region.originalWidth && !region.originalHeight) {
|
|
96
|
+
region.originalWidth = region.width;
|
|
97
|
+
region.originalHeight = region.height;
|
|
98
|
+
}
|
|
99
|
+
regions.set(name, region);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return regions;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function resolveAtlasPageImageUrl(
|
|
106
|
+
options: ResolveAtlasPageImageOptions,
|
|
107
|
+
atlas: Map<string, AtlasRegion>,
|
|
108
|
+
page: string,
|
|
109
|
+
): string {
|
|
110
|
+
const mapped = options.imageUrls[page] ?? options.imageUrls[page.split('/').pop() ?? page];
|
|
111
|
+
if (mapped) return mapped;
|
|
112
|
+
if (options.imageUrl && (!page || atlasPageCount(atlas) <= 1)) return options.imageUrl;
|
|
113
|
+
if (!page) {
|
|
114
|
+
if (options.imageUrl) return options.imageUrl;
|
|
115
|
+
throw new EngineError(EngineErrorCode.AssetInvalidData, 'Spine imageUrl is required when atlas has no page.', {
|
|
116
|
+
domain: ErrorDomain.Component,
|
|
117
|
+
recovery: ErrorRecovery.ReleaseResource,
|
|
118
|
+
context: { atlasUrl: options.atlasUrl, resourceType: 'skeleton/spine-atlas' },
|
|
119
|
+
path: 'spine.atlas.pages',
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return new URL(page, options.atlasUrl || window.location.href).href;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function atlasPageCount(atlas: Map<string, AtlasRegion>): number {
|
|
126
|
+
return new Set(Array.from(atlas.values()).map(region => region.page).filter(Boolean)).size;
|
|
127
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export class SpineFloatBuilder {
|
|
2
|
+
data: Float32Array;
|
|
3
|
+
length = 0;
|
|
4
|
+
|
|
5
|
+
constructor(initialCapacity = 1024) {
|
|
6
|
+
this.data = new Float32Array(initialCapacity);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
clear(): void {
|
|
10
|
+
this.length = 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get byteLength(): number {
|
|
14
|
+
return this.length * 4;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get(index: number): number {
|
|
18
|
+
return this.data[index] ?? 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
push(...values: number[]): void {
|
|
22
|
+
this.ensureCapacity(this.length + values.length);
|
|
23
|
+
this.data.set(values, this.length);
|
|
24
|
+
this.length += values.length;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
push8(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): void {
|
|
28
|
+
this.ensureCapacity(this.length + 8);
|
|
29
|
+
const offset = this.length;
|
|
30
|
+
this.data[offset] = a;
|
|
31
|
+
this.data[offset + 1] = b;
|
|
32
|
+
this.data[offset + 2] = c;
|
|
33
|
+
this.data[offset + 3] = d;
|
|
34
|
+
this.data[offset + 4] = e;
|
|
35
|
+
this.data[offset + 5] = f;
|
|
36
|
+
this.data[offset + 6] = g;
|
|
37
|
+
this.data[offset + 7] = h;
|
|
38
|
+
this.length += 8;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
appendArray(values: ArrayLike<number>, length = values.length): void {
|
|
42
|
+
this.ensureCapacity(this.length + length);
|
|
43
|
+
for (let i = 0; i < length; i++) this.data[this.length + i] = values[i] ?? 0;
|
|
44
|
+
this.length += length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
truncate(length: number): void {
|
|
48
|
+
this.length = Math.max(0, Math.min(this.length, length));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Grows storage if needed and exposes a reusable writable region without allocating a temporary array. */
|
|
52
|
+
reserveLength(length: number): void {
|
|
53
|
+
const nextLength = Math.max(0, length);
|
|
54
|
+
this.ensureCapacity(nextLength);
|
|
55
|
+
this.length = nextLength;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private ensureCapacity(required: number): void {
|
|
59
|
+
if (this.data.length >= required) return;
|
|
60
|
+
let nextCapacity = Math.max(1, this.data.length);
|
|
61
|
+
while (nextCapacity < required) nextCapacity *= 2;
|
|
62
|
+
const next = new Float32Array(nextCapacity);
|
|
63
|
+
next.set(this.data.subarray(0, this.length));
|
|
64
|
+
this.data = next;
|
|
65
|
+
}
|
|
66
|
+
}
|