@haiyue/animation-spec 0.1.0 → 0.2.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 (77) hide show
  1. package/README.md +40 -6
  2. package/SPECIFICATION.md +12 -2
  3. package/bin/hya-live2d-convert.mjs +76 -0
  4. package/bin/{hya-convert.mjs → hya-lottie-convert.mjs} +1 -1
  5. package/dist/chunks/binary-DvuTJ_hy.js +1 -0
  6. package/dist/chunks/clip-baked-Dv-wjX9d.js +1 -0
  7. package/dist/chunks/errors-Bzs3GOD9.js +1 -0
  8. package/dist/chunks/expression-D1cKtNwM.js +1 -0
  9. package/dist/chunks/extensions-DxL4AxOC.js +1 -0
  10. package/dist/conversion.d.ts +1 -0
  11. package/dist/conversion.js +1 -0
  12. package/dist/cubism.d.ts +1 -0
  13. package/dist/cubism.js +1 -0
  14. package/dist/deformable2d.d.ts +1 -0
  15. package/dist/deformable2d.js +1 -0
  16. package/dist/index.d.ts +1 -10
  17. package/dist/index.js +1 -2927
  18. package/dist/live2d/clip-baked.d.ts +1 -0
  19. package/dist/live2d/clip-baked.js +1 -0
  20. package/dist/live2d.d.ts +1 -0
  21. package/dist/live2d.js +1 -0
  22. package/dist/lottie.d.ts +1 -62
  23. package/dist/lottie.js +1 -2642
  24. package/dist/native3d.d.ts +1 -2
  25. package/dist/native3d.js +1 -907
  26. package/dist/shared.d.ts +1493 -0
  27. package/package.json +30 -10
  28. package/schema/animation.schema.json +92 -0
  29. package/schema/deformable-mesh-2d-extension.schema.json +18 -0
  30. package/schema/deformable-mesh-2d.contract.json +58 -0
  31. package/schema/parameterized-rig-2d.contract.json +63 -0
  32. package/schema/parameterized-rig-2d.schema.json +57 -0
  33. package/schema/parameterized-rig-census-map.json +128 -0
  34. package/schema/responsive-layout-2d-census-map.json +49 -0
  35. package/schema/responsive-layout-2d.contract.json +58 -0
  36. package/schema/responsive-layout-2d.schema.json +34 -0
  37. package/schema/state-machine-v2-census-map.json +1 -0
  38. package/schema/state-machine-v2.contract.json +66 -0
  39. package/schema/state-machine-v2.schema.json +55 -0
  40. package/schema/vector-visual-census-map.json +120 -0
  41. package/schema/vector-visual.contract.json +47 -0
  42. package/schema/vector-visual.schema.json +72 -0
  43. package/dist/binary.d.ts +0 -7
  44. package/dist/chunks/types-l6nBVYwx.js +0 -8
  45. package/dist/chunks/types-l6nBVYwx.js.map +0 -1
  46. package/dist/errors.d.ts +0 -7
  47. package/dist/extensions.d.ts +0 -19
  48. package/dist/index.js.map +0 -1
  49. package/dist/lottie-merge-path.d.ts +0 -11
  50. package/dist/lottie.js.map +0 -1
  51. package/dist/native3d/Animation3DError.d.ts +0 -8
  52. package/dist/native3d/Animation3DParser.d.ts +0 -7
  53. package/dist/native3d/Animation3DTypes.d.ts +0 -285
  54. package/dist/native3d/index.d.ts +0 -5
  55. package/dist/native3d.js.map +0 -1
  56. package/dist/parser.d.ts +0 -2
  57. package/dist/state-machine.d.ts +0 -130
  58. package/dist/text-selector.d.ts +0 -2
  59. package/dist/types.d.ts +0 -419
  60. package/dist/validation.d.ts +0 -32
  61. package/src/binary.ts +0 -1441
  62. package/src/errors.ts +0 -18
  63. package/src/extensions.ts +0 -55
  64. package/src/index.ts +0 -90
  65. package/src/lottie-merge-path.ts +0 -204
  66. package/src/lottie.ts +0 -3073
  67. package/src/native3d/Animation3DError.ts +0 -20
  68. package/src/native3d/Animation3DParser.ts +0 -964
  69. package/src/native3d/Animation3DTypes.ts +0 -322
  70. package/src/native3d/index.ts +0 -14
  71. package/src/native3d.ts +0 -2
  72. package/src/parser.ts +0 -41
  73. package/src/state-machine.ts +0 -645
  74. package/src/text-selector.ts +0 -7
  75. package/src/types.ts +0 -483
  76. package/src/validation.ts +0 -1286
  77. package/test/fixtures/basic-lottie.json +0 -31
@@ -1,645 +0,0 @@
1
- import { AnimationFormatError } from './errors';
2
-
3
- export const HYA_STATE_MACHINE_EXTENSION_ID = 'org.haiyue.animation-state-machine@1' as const;
4
-
5
- /**
6
- * Stable channel families shared by the HYA compiler, editor capability UI,
7
- * and runtime adapters. A family describes value semantics; concrete channel
8
- * ids below additionally describe whether the current shared runtime can
9
- * execute that binding without falling back to a second sampler.
10
- */
11
- export type HyaStateMachineChannelKind =
12
- | 'continuous-number'
13
- | 'discrete-step'
14
- | 'shape-morph'
15
- | 'resource-switch'
16
- | 'particle-side-effect'
17
- | 'audio-side-effect'
18
- | 'pose-3d';
19
-
20
- export type HyaStateMachineChannelSupport = 'full' | 'degraded' | 'unsupported';
21
-
22
- export type HyaStateMachineChannelId =
23
- | 'core-transform'
24
- | 'sprite-uv'
25
- | 'vector-morph'
26
- | 'vector-paint'
27
- | 'text-animator'
28
- | 'visual-effect'
29
- | 'composite-expansion'
30
- | 'resource-switch'
31
- | 'particle-2d'
32
- | 'particle-3d'
33
- | 'audio'
34
- | 'pose-3d';
35
-
36
- export interface HyaStateMachineChannelCapability {
37
- readonly id: HyaStateMachineChannelId;
38
- readonly kind: HyaStateMachineChannelKind;
39
- readonly support: HyaStateMachineChannelSupport;
40
- readonly sampling: 'numeric-track' | 'step-track' | 'effect-cue' | 'pose-buffer';
41
- readonly mixing:
42
- | 'override-additive-layer-mask'
43
- | 'dominant-weight-then-action-order'
44
- | 'shared-instance-lifecycle'
45
- | 'reject-overlap';
46
- readonly ownership: 'pose-buffer' | 'shared-visual' | 'shared-side-effect';
47
- readonly transition:
48
- | 'cross-fade'
49
- | 'switch-at-dominant-weight'
50
- | 'destination-takeover'
51
- | 'immediate-only'
52
- | 'rejected';
53
- /** Stable diagnostic emitted when support is not full. */
54
- readonly diagnosticCode?:
55
- | 'E_STATE_MACHINE_CHANNEL_ADVANCED_INLINE_UNSUPPORTED'
56
- | 'E_STATE_MACHINE_CHANNEL_RESOURCE_SWITCH_OVERLAP'
57
- | 'E_STATE_MACHINE_CHANNEL_AUDIO_UNMIXABLE_RANGE';
58
- }
59
-
60
- const STATE_MACHINE_CHANNEL_CAPABILITIES: readonly HyaStateMachineChannelCapability[] = Object.freeze([
61
- channel('core-transform', 'continuous-number', 'full', 'numeric-track', 'override-additive-layer-mask', 'pose-buffer', 'cross-fade'),
62
- channel('sprite-uv', 'discrete-step', 'full', 'step-track', 'dominant-weight-then-action-order', 'shared-visual', 'switch-at-dominant-weight'),
63
- channel('vector-morph', 'shape-morph', 'full', 'numeric-track', 'override-additive-layer-mask', 'shared-visual', 'cross-fade'),
64
- channel('vector-paint', 'continuous-number', 'unsupported', 'numeric-track', 'override-additive-layer-mask', 'shared-visual', 'rejected', 'E_STATE_MACHINE_CHANNEL_ADVANCED_INLINE_UNSUPPORTED'),
65
- channel('text-animator', 'continuous-number', 'unsupported', 'numeric-track', 'override-additive-layer-mask', 'shared-visual', 'rejected', 'E_STATE_MACHINE_CHANNEL_ADVANCED_INLINE_UNSUPPORTED'),
66
- channel('visual-effect', 'continuous-number', 'unsupported', 'numeric-track', 'override-additive-layer-mask', 'shared-visual', 'rejected', 'E_STATE_MACHINE_CHANNEL_ADVANCED_INLINE_UNSUPPORTED'),
67
- channel('composite-expansion', 'continuous-number', 'unsupported', 'numeric-track', 'override-additive-layer-mask', 'shared-visual', 'rejected', 'E_STATE_MACHINE_CHANNEL_ADVANCED_INLINE_UNSUPPORTED'),
68
- channel('resource-switch', 'resource-switch', 'degraded', 'step-track', 'dominant-weight-then-action-order', 'shared-visual', 'switch-at-dominant-weight', 'E_STATE_MACHINE_CHANNEL_RESOURCE_SWITCH_OVERLAP'),
69
- channel('particle-2d', 'particle-side-effect', 'full', 'effect-cue', 'shared-instance-lifecycle', 'shared-side-effect', 'destination-takeover'),
70
- channel('particle-3d', 'particle-side-effect', 'full', 'effect-cue', 'shared-instance-lifecycle', 'shared-side-effect', 'destination-takeover'),
71
- channel('audio', 'audio-side-effect', 'degraded', 'effect-cue', 'reject-overlap', 'shared-side-effect', 'immediate-only', 'E_STATE_MACHINE_CHANNEL_AUDIO_UNMIXABLE_RANGE'),
72
- channel('pose-3d', 'pose-3d', 'full', 'pose-buffer', 'override-additive-layer-mask', 'pose-buffer', 'cross-fade'),
73
- ]);
74
-
75
- /** Canonical immutable channel registry for the built-in state machine. */
76
- export const HYA_STATE_MACHINE_CHANNEL_REGISTRY: ReadonlyMap<
77
- HyaStateMachineChannelId,
78
- HyaStateMachineChannelCapability
79
- > = immutableReadonlyMap(
80
- STATE_MACHINE_CHANNEL_CAPABILITIES.map(capability => [capability.id, capability] as const),
81
- );
82
-
83
- export function hyaStateMachineChannelCapability(
84
- id: HyaStateMachineChannelId,
85
- ): HyaStateMachineChannelCapability {
86
- return HYA_STATE_MACHINE_CHANNEL_REGISTRY.get(id)!;
87
- }
88
-
89
- function immutableReadonlyMap<K, V>(entries: readonly (readonly [K, V])[]): ReadonlyMap<K, V> {
90
- const source = new Map(entries);
91
- let view: ReadonlyMap<K, V>;
92
- view = Object.freeze({
93
- get size(): number { return source.size; },
94
- get: (key: K): V | undefined => source.get(key),
95
- has: (key: K): boolean => source.has(key),
96
- entries: (): MapIterator<[K, V]> => source.entries(),
97
- keys: (): MapIterator<K> => source.keys(),
98
- values: (): MapIterator<V> => source.values(),
99
- forEach: (
100
- callbackfn: (value: V, key: K, map: ReadonlyMap<K, V>) => void,
101
- thisArg?: unknown,
102
- ): void => source.forEach((value, key) => callbackfn.call(thisArg, value, key, view)),
103
- [Symbol.iterator]: (): MapIterator<[K, V]> => source[Symbol.iterator](),
104
- });
105
- return view;
106
- }
107
-
108
- export interface HyaAnimationClipRange {
109
- readonly id: string;
110
- readonly name?: string;
111
- /** Start in the document composition, in seconds. */
112
- readonly start: number;
113
- readonly duration: number;
114
- }
115
-
116
- export type HyaStateMachineLoopMode = 'once' | 'repeat' | 'ping-pong';
117
- export type HyaStateMachineBlendMode = 'override' | 'additive';
118
-
119
- export interface HyaStateMachineBindingMask {
120
- readonly include?: readonly string[];
121
- readonly exclude?: readonly string[];
122
- }
123
-
124
- export type HyaStateMachineParameter =
125
- | Readonly<{ name: string; type: 'float'; defaultValue: number }>
126
- | Readonly<{ name: string; type: 'integer'; defaultValue: number }>
127
- | Readonly<{ name: string; type: 'boolean'; defaultValue: boolean }>
128
- | Readonly<{ name: string; type: 'trigger' }>;
129
-
130
- export type HyaStateMachineCondition =
131
- | Readonly<{
132
- parameter: string;
133
- operator: 'greater' | 'greater-or-equal' | 'less' | 'less-or-equal';
134
- value: number;
135
- }>
136
- | Readonly<{
137
- parameter: string;
138
- operator: 'equal' | 'not-equal';
139
- value: number | boolean;
140
- }>
141
- | Readonly<{
142
- parameter: string;
143
- operator: 'is-true' | 'is-false' | 'triggered';
144
- }>;
145
-
146
- export interface HyaStateMachineClipMotion {
147
- readonly kind: 'clip';
148
- readonly clipId: string;
149
- }
150
-
151
- export interface HyaStateMachineBlend1DMotion {
152
- readonly kind: 'blend-1d';
153
- readonly parameter: string;
154
- readonly children: readonly Readonly<{
155
- threshold: number;
156
- motion: HyaStateMachineMotion;
157
- }>[];
158
- }
159
-
160
- export interface HyaStateMachineBlend2DMotion {
161
- readonly kind: 'blend-2d';
162
- readonly algorithm: 'cartesian' | 'directional';
163
- readonly parameterX: string;
164
- readonly parameterY: string;
165
- readonly children: readonly Readonly<{
166
- position: readonly [number, number];
167
- motion: HyaStateMachineMotion;
168
- }>[];
169
- }
170
-
171
- export type HyaStateMachineMotion =
172
- | HyaStateMachineClipMotion
173
- | HyaStateMachineBlend1DMotion
174
- | HyaStateMachineBlend2DMotion;
175
-
176
- export interface HyaStateMachineState {
177
- readonly id: string;
178
- readonly name: string;
179
- readonly motion: HyaStateMachineMotion;
180
- readonly speed?: number;
181
- readonly speedParameter?: string;
182
- readonly loop?: HyaStateMachineLoopMode;
183
- }
184
-
185
- export interface HyaStateMachineTransition {
186
- readonly id: string;
187
- readonly from: string | '*';
188
- readonly to: string;
189
- readonly conditions: readonly HyaStateMachineCondition[];
190
- readonly duration: number;
191
- readonly hasExitTime?: boolean;
192
- readonly exitTime?: number;
193
- readonly destinationOffset?: number;
194
- readonly interruption?:
195
- | 'none'
196
- | 'source'
197
- | 'destination'
198
- | 'source-then-destination'
199
- | 'destination-then-source';
200
- }
201
-
202
- export interface HyaStateMachineLayer {
203
- readonly id: string;
204
- readonly name: string;
205
- readonly initialStateId: string;
206
- readonly states: readonly HyaStateMachineState[];
207
- readonly transitions: readonly HyaStateMachineTransition[];
208
- readonly blendMode?: HyaStateMachineBlendMode;
209
- readonly weight?: number;
210
- readonly mask?: HyaStateMachineBindingMask;
211
- }
212
-
213
- export interface HyaStateMachineDefinition {
214
- readonly format: 'haiyue-animation-state-machine@1';
215
- readonly id: string;
216
- readonly name: string;
217
- readonly parameters: readonly HyaStateMachineParameter[];
218
- readonly layers: readonly HyaStateMachineLayer[];
219
- }
220
-
221
- export interface HyaStateMachineExtension {
222
- readonly clips: readonly HyaAnimationClipRange[];
223
- readonly stateMachine: HyaStateMachineDefinition;
224
- }
225
-
226
- const MAX_CLIPS = 10_000;
227
- const MAX_PARAMETERS = 1_000;
228
- const MAX_LAYERS = 128;
229
- const MAX_STATES = 100_000;
230
- const MAX_TRANSITIONS = 200_000;
231
- const MAX_BLEND_DEPTH = 32;
232
- const MAX_BLEND_CHILDREN = 4_096;
233
-
234
- function channel(
235
- id: HyaStateMachineChannelId,
236
- kind: HyaStateMachineChannelKind,
237
- support: HyaStateMachineChannelSupport,
238
- sampling: HyaStateMachineChannelCapability['sampling'],
239
- mixing: HyaStateMachineChannelCapability['mixing'],
240
- ownership: HyaStateMachineChannelCapability['ownership'],
241
- transition: HyaStateMachineChannelCapability['transition'],
242
- diagnosticCode?: HyaStateMachineChannelCapability['diagnosticCode'],
243
- ): HyaStateMachineChannelCapability {
244
- return Object.freeze({
245
- id,
246
- kind,
247
- support,
248
- sampling,
249
- mixing,
250
- ownership,
251
- transition,
252
- ...(diagnosticCode === undefined ? {} : { diagnosticCode }),
253
- });
254
- }
255
-
256
- /** Strictly validates and freezes the built-in HYA state-machine extension. */
257
- export function parseHyaStateMachineExtension(
258
- value: unknown,
259
- compositionDuration: number,
260
- path = `$.extensions.${HYA_STATE_MACHINE_EXTENSION_ID}`,
261
- ): HyaStateMachineExtension {
262
- positive(compositionDuration, '$.duration');
263
- const root = record(value, path);
264
- knownKeys(root, ['clips', 'stateMachine'], path);
265
- const clipValues = array(root.clips, `${path}.clips`);
266
- bounded(clipValues.length, 1, MAX_CLIPS, 'clips', `${path}.clips`);
267
- const clipIds = new Set<string>();
268
- const clips = clipValues.map((entry, index) => {
269
- const clipPath = `${path}.clips[${index}]`;
270
- const clip = record(entry, clipPath);
271
- knownKeys(clip, ['id', 'name', 'start', 'duration'], clipPath);
272
- const id = identifier(clip.id, `${clipPath}.id`);
273
- unique(clipIds, id, `${clipPath}.id`, 'clip');
274
- const start = nonNegative(clip.start, `${clipPath}.start`);
275
- const duration = positive(clip.duration, `${clipPath}.duration`);
276
- if (start + duration > compositionDuration + 1e-6) {
277
- invalid('Clip range must fit inside the HYA composition duration.', clipPath);
278
- }
279
- return Object.freeze({
280
- id,
281
- ...(clip.name === undefined ? {} : { name: text(clip.name, `${clipPath}.name`) }),
282
- start,
283
- duration,
284
- });
285
- });
286
-
287
- const stateMachine = parseStateMachine(root.stateMachine, clipIds, `${path}.stateMachine`);
288
- return Object.freeze({ clips: Object.freeze(clips), stateMachine });
289
- }
290
-
291
- function parseStateMachine(
292
- value: unknown,
293
- clipIds: ReadonlySet<string>,
294
- path: string,
295
- ): HyaStateMachineDefinition {
296
- const root = record(value, path);
297
- knownKeys(root, ['format', 'id', 'name', 'parameters', 'layers'], path);
298
- if (root.format !== 'haiyue-animation-state-machine@1') {
299
- invalid('State machine format must be "haiyue-animation-state-machine@1".', `${path}.format`);
300
- }
301
- const parametersInput = array(root.parameters, `${path}.parameters`);
302
- bounded(parametersInput.length, 0, MAX_PARAMETERS, 'parameters', `${path}.parameters`);
303
- const parameterNames = new Set<string>();
304
- const parameterTypes = new Map<string, HyaStateMachineParameter['type']>();
305
- const parameters = parametersInput.map((entry, index) => {
306
- const parameterPath = `${path}.parameters[${index}]`;
307
- const parameter = record(entry, parameterPath);
308
- knownKeys(parameter, ['name', 'type', 'defaultValue'], parameterPath);
309
- const name = identifier(parameter.name, `${parameterPath}.name`);
310
- unique(parameterNames, name, `${parameterPath}.name`, 'parameter');
311
- const type = oneOf(parameter.type, ['float', 'integer', 'boolean', 'trigger'] as const, `${parameterPath}.type`);
312
- parameterTypes.set(name, type);
313
- if (type === 'float') return Object.freeze({ name, type, defaultValue: finite(parameter.defaultValue, `${parameterPath}.defaultValue`) });
314
- if (type === 'integer') return Object.freeze({ name, type, defaultValue: safeInteger(parameter.defaultValue, `${parameterPath}.defaultValue`) });
315
- if (type === 'boolean') return Object.freeze({ name, type, defaultValue: booleanValue(parameter.defaultValue, `${parameterPath}.defaultValue`) });
316
- if (parameter.defaultValue !== undefined) invalid('Trigger parameters do not have a defaultValue.', `${parameterPath}.defaultValue`);
317
- return Object.freeze({ name, type });
318
- });
319
-
320
- const layersInput = array(root.layers, `${path}.layers`);
321
- bounded(layersInput.length, 1, MAX_LAYERS, 'layers', `${path}.layers`);
322
- const layerIds = new Set<string>();
323
- let stateCount = 0;
324
- let transitionCount = 0;
325
- const layers = layersInput.map((entry, layerIndex) => {
326
- const layerPath = `${path}.layers[${layerIndex}]`;
327
- const layer = record(entry, layerPath);
328
- knownKeys(layer, [
329
- 'id', 'name', 'initialStateId', 'states', 'transitions', 'blendMode', 'weight', 'mask',
330
- ], layerPath);
331
- const id = identifier(layer.id, `${layerPath}.id`);
332
- unique(layerIds, id, `${layerPath}.id`, 'layer');
333
- const stateInputs = array(layer.states, `${layerPath}.states`);
334
- bounded(stateInputs.length, 1, MAX_STATES, 'states', `${layerPath}.states`);
335
- stateCount += stateInputs.length;
336
- bounded(stateCount, 1, MAX_STATES, 'total states', `${layerPath}.states`);
337
- const stateIds = new Set<string>();
338
- const states = stateInputs.map((stateEntry, stateIndex) => {
339
- const statePath = `${layerPath}.states[${stateIndex}]`;
340
- const state = record(stateEntry, statePath);
341
- knownKeys(state, ['id', 'name', 'motion', 'speed', 'speedParameter', 'loop'], statePath);
342
- const stateId = identifier(state.id, `${statePath}.id`);
343
- unique(stateIds, stateId, `${statePath}.id`, 'state');
344
- const speed = state.speed === undefined ? undefined : finite(state.speed, `${statePath}.speed`);
345
- const speedParameter = state.speedParameter === undefined
346
- ? undefined
347
- : numericParameterReference(state.speedParameter, parameterTypes, `${statePath}.speedParameter`);
348
- return Object.freeze({
349
- id: stateId,
350
- name: text(state.name, `${statePath}.name`),
351
- motion: parseMotion(state.motion, clipIds, parameterTypes, `${statePath}.motion`, 0),
352
- ...(speed === undefined ? {} : { speed }),
353
- ...(speedParameter === undefined ? {} : { speedParameter }),
354
- ...(state.loop === undefined ? {} : { loop: oneOf(state.loop, ['once', 'repeat', 'ping-pong'] as const, `${statePath}.loop`) }),
355
- });
356
- });
357
- const initialStateId = identifier(layer.initialStateId, `${layerPath}.initialStateId`);
358
- if (!stateIds.has(initialStateId)) invalid(`Unknown initial state "${initialStateId}".`, `${layerPath}.initialStateId`);
359
-
360
- const transitionInputs = array(layer.transitions, `${layerPath}.transitions`);
361
- transitionCount += transitionInputs.length;
362
- bounded(transitionCount, 0, MAX_TRANSITIONS, 'total transitions', `${layerPath}.transitions`);
363
- const transitionIds = new Set<string>();
364
- const transitions = transitionInputs.map((transitionEntry, transitionIndex) => {
365
- const transitionPath = `${layerPath}.transitions[${transitionIndex}]`;
366
- const transition = record(transitionEntry, transitionPath);
367
- knownKeys(transition, [
368
- 'id', 'from', 'to', 'conditions', 'duration', 'hasExitTime', 'exitTime',
369
- 'destinationOffset', 'interruption',
370
- ], transitionPath);
371
- const transitionId = identifier(transition.id, `${transitionPath}.id`);
372
- unique(transitionIds, transitionId, `${transitionPath}.id`, 'transition');
373
- const from = transition.from === '*' ? '*' : identifier(transition.from, `${transitionPath}.from`);
374
- const to = identifier(transition.to, `${transitionPath}.to`);
375
- if (from !== '*' && !stateIds.has(from)) invalid(`Unknown source state "${from}".`, `${transitionPath}.from`);
376
- if (!stateIds.has(to)) invalid(`Unknown destination state "${to}".`, `${transitionPath}.to`);
377
- const conditions = array(transition.conditions, `${transitionPath}.conditions`).map((condition, conditionIndex) => (
378
- parseCondition(condition, parameterTypes, `${transitionPath}.conditions[${conditionIndex}]`)
379
- ));
380
- if (transition.hasExitTime === true && transition.exitTime === undefined) {
381
- invalid('exitTime is required when hasExitTime is true.', `${transitionPath}.exitTime`);
382
- }
383
- const exitTime = transition.exitTime === undefined ? undefined : nonNegative(transition.exitTime, `${transitionPath}.exitTime`);
384
- const destinationOffset = transition.destinationOffset === undefined
385
- ? undefined
386
- : nonNegative(transition.destinationOffset, `${transitionPath}.destinationOffset`);
387
- return Object.freeze({
388
- id: transitionId,
389
- from,
390
- to,
391
- conditions: Object.freeze(conditions),
392
- duration: nonNegative(transition.duration, `${transitionPath}.duration`),
393
- ...(transition.hasExitTime === undefined ? {} : { hasExitTime: booleanValue(transition.hasExitTime, `${transitionPath}.hasExitTime`) }),
394
- ...(exitTime === undefined ? {} : { exitTime }),
395
- ...(destinationOffset === undefined ? {} : { destinationOffset }),
396
- ...(transition.interruption === undefined ? {} : {
397
- interruption: oneOf(transition.interruption, [
398
- 'none', 'source', 'destination', 'source-then-destination', 'destination-then-source',
399
- ] as const, `${transitionPath}.interruption`),
400
- }),
401
- });
402
- });
403
- const mask = layer.mask === undefined ? undefined : parseMask(layer.mask, `${layerPath}.mask`);
404
- return Object.freeze({
405
- id,
406
- name: text(layer.name, `${layerPath}.name`),
407
- initialStateId,
408
- states: Object.freeze(states),
409
- transitions: Object.freeze(transitions),
410
- ...(layer.blendMode === undefined ? {} : { blendMode: oneOf(layer.blendMode, ['override', 'additive'] as const, `${layerPath}.blendMode`) }),
411
- ...(layer.weight === undefined ? {} : { weight: unit(layer.weight, `${layerPath}.weight`) }),
412
- ...(mask === undefined ? {} : { mask }),
413
- });
414
- });
415
-
416
- return Object.freeze({
417
- format: 'haiyue-animation-state-machine@1',
418
- id: identifier(root.id, `${path}.id`),
419
- name: text(root.name, `${path}.name`),
420
- parameters: Object.freeze(parameters),
421
- layers: Object.freeze(layers),
422
- });
423
- }
424
-
425
- function parseMotion(
426
- value: unknown,
427
- clipIds: ReadonlySet<string>,
428
- parameterTypes: ReadonlyMap<string, HyaStateMachineParameter['type']>,
429
- path: string,
430
- depth: number,
431
- ): HyaStateMachineMotion {
432
- if (depth > MAX_BLEND_DEPTH) invalid(`Blend Tree depth exceeds ${MAX_BLEND_DEPTH}.`, path);
433
- const motion = record(value, path);
434
- const kind = oneOf(motion.kind, ['clip', 'blend-1d', 'blend-2d'] as const, `${path}.kind`);
435
- if (kind === 'clip') {
436
- knownKeys(motion, ['kind', 'clipId'], path);
437
- const clipId = identifier(motion.clipId, `${path}.clipId`);
438
- if (!clipIds.has(clipId)) invalid(`Unknown HYA clip "${clipId}".`, `${path}.clipId`);
439
- return Object.freeze({ kind, clipId });
440
- }
441
- const childInputs = array(motion.children, `${path}.children`);
442
- bounded(childInputs.length, 1, MAX_BLEND_CHILDREN, 'Blend Tree children', `${path}.children`);
443
- if (kind === 'blend-1d') {
444
- knownKeys(motion, ['kind', 'parameter', 'children'], path);
445
- const parameter = numericParameterReference(motion.parameter, parameterTypes, `${path}.parameter`);
446
- let previous = -Infinity;
447
- const children = childInputs.map((entry, index) => {
448
- const childPath = `${path}.children[${index}]`;
449
- const child = record(entry, childPath);
450
- knownKeys(child, ['threshold', 'motion'], childPath);
451
- const threshold = finite(child.threshold, `${childPath}.threshold`);
452
- if (threshold <= previous) invalid('Blend-1D thresholds must be strictly increasing.', `${childPath}.threshold`);
453
- previous = threshold;
454
- return Object.freeze({ threshold, motion: parseMotion(child.motion, clipIds, parameterTypes, `${childPath}.motion`, depth + 1) });
455
- });
456
- return Object.freeze({ kind, parameter, children: Object.freeze(children) });
457
- }
458
- const parameterX = numericParameterReference(motion.parameterX, parameterTypes, `${path}.parameterX`);
459
- const parameterY = numericParameterReference(motion.parameterY, parameterTypes, `${path}.parameterY`);
460
- knownKeys(motion, ['kind', 'algorithm', 'parameterX', 'parameterY', 'children'], path);
461
- const children = childInputs.map((entry, index) => {
462
- const childPath = `${path}.children[${index}]`;
463
- const child = record(entry, childPath);
464
- knownKeys(child, ['position', 'motion'], childPath);
465
- return Object.freeze({
466
- position: pair(child.position, `${childPath}.position`),
467
- motion: parseMotion(child.motion, clipIds, parameterTypes, `${childPath}.motion`, depth + 1),
468
- });
469
- });
470
- return Object.freeze({
471
- kind,
472
- algorithm: oneOf(motion.algorithm, ['cartesian', 'directional'] as const, `${path}.algorithm`),
473
- parameterX,
474
- parameterY,
475
- children: Object.freeze(children),
476
- });
477
- }
478
-
479
- function parseCondition(
480
- value: unknown,
481
- parameterTypes: ReadonlyMap<string, HyaStateMachineParameter['type']>,
482
- path: string,
483
- ): HyaStateMachineCondition {
484
- const condition = record(value, path);
485
- knownKeys(condition, ['parameter', 'operator', 'value'], path);
486
- const parameter = parameterReference(condition.parameter, parameterTypes, `${path}.parameter`);
487
- const parameterType = parameterTypes.get(parameter)!;
488
- const operator = oneOf(condition.operator, [
489
- 'greater', 'greater-or-equal', 'less', 'less-or-equal',
490
- 'equal', 'not-equal', 'is-true', 'is-false', 'triggered',
491
- ] as const, `${path}.operator`);
492
- const numericOperator = operator === 'greater' || operator === 'greater-or-equal'
493
- || operator === 'less' || operator === 'less-or-equal';
494
- if ((parameterType === 'float' || parameterType === 'integer')
495
- && (numericOperator || operator === 'equal' || operator === 'not-equal')) {
496
- const value = finite(condition.value, `${path}.value`);
497
- if (parameterType === 'integer' && !Number.isSafeInteger(value)) {
498
- invalid('Integer conditions require a safe integer value.', `${path}.value`);
499
- }
500
- return Object.freeze({ parameter, operator, value }) as HyaStateMachineCondition;
501
- }
502
- if (parameterType === 'boolean'
503
- && (operator === 'is-true' || operator === 'is-false')) {
504
- if (condition.value !== undefined) invalid(`Operator "${operator}" does not accept a value.`, `${path}.value`);
505
- return Object.freeze({ parameter, operator });
506
- }
507
- if (parameterType === 'boolean' && (operator === 'equal' || operator === 'not-equal')) {
508
- return Object.freeze({ parameter, operator, value: booleanValue(condition.value, `${path}.value`) });
509
- }
510
- if (parameterType === 'trigger' && operator === 'triggered') {
511
- if (condition.value !== undefined) invalid('Operator "triggered" does not accept a value.', `${path}.value`);
512
- return Object.freeze({ parameter, operator });
513
- }
514
- invalid(`Operator "${operator}" is invalid for ${parameterType} parameters.`, `${path}.operator`);
515
- }
516
-
517
- function parseMask(value: unknown, path: string): HyaStateMachineBindingMask {
518
- const mask = record(value, path);
519
- knownKeys(mask, ['include', 'exclude'], path);
520
- const include = mask.include === undefined ? undefined : stringList(mask.include, `${path}.include`);
521
- const exclude = mask.exclude === undefined ? undefined : stringList(mask.exclude, `${path}.exclude`);
522
- return Object.freeze({
523
- ...(include === undefined ? {} : { include }),
524
- ...(exclude === undefined ? {} : { exclude }),
525
- });
526
- }
527
-
528
- function record(value: unknown, path: string): Record<string, unknown> {
529
- if (!value || typeof value !== 'object' || Array.isArray(value)) invalid('Expected an object.', path);
530
- return value as Record<string, unknown>;
531
- }
532
-
533
- function array(value: unknown, path: string): unknown[] {
534
- if (!Array.isArray(value)) invalid('Expected an array.', path);
535
- return value;
536
- }
537
-
538
- function identifier(value: unknown, path: string): string {
539
- const result = text(value, path);
540
- if (!result.trim()) invalid('Expected a non-empty identifier.', path);
541
- return result;
542
- }
543
-
544
- function text(value: unknown, path: string): string {
545
- if (typeof value !== 'string') invalid('Expected a string.', path);
546
- return value;
547
- }
548
-
549
- function finite(value: unknown, path: string): number {
550
- if (typeof value !== 'number' || !Number.isFinite(value)) invalid('Expected a finite number.', path);
551
- return value;
552
- }
553
-
554
- function safeInteger(value: unknown, path: string): number {
555
- const result = finite(value, path);
556
- if (!Number.isSafeInteger(result)) invalid('Expected a safe integer.', path);
557
- return result;
558
- }
559
-
560
- function nonNegative(value: unknown, path: string): number {
561
- const result = finite(value, path);
562
- if (result < 0) invalid('Expected a non-negative number.', path);
563
- return result;
564
- }
565
-
566
- function positive(value: unknown, path: string): number {
567
- const result = finite(value, path);
568
- if (result <= 0) invalid('Expected a positive number.', path);
569
- return result;
570
- }
571
-
572
- function unit(value: unknown, path: string): number {
573
- const result = finite(value, path);
574
- if (result < 0 || result > 1) invalid('Expected a number in [0, 1].', path);
575
- return result;
576
- }
577
-
578
- function booleanValue(value: unknown, path: string): boolean {
579
- if (typeof value !== 'boolean') invalid('Expected a boolean.', path);
580
- return value;
581
- }
582
-
583
- function pair(value: unknown, path: string): readonly [number, number] {
584
- const values = array(value, path);
585
- if (values.length !== 2) invalid('Expected two numbers.', path);
586
- return Object.freeze([finite(values[0], `${path}[0]`), finite(values[1], `${path}[1]`)]) as readonly [number, number];
587
- }
588
-
589
- function stringList(value: unknown, path: string): readonly string[] {
590
- const values = array(value, path);
591
- const seen = new Set<string>();
592
- return Object.freeze(values.map((entry, index) => {
593
- const result = identifier(entry, `${path}[${index}]`);
594
- unique(seen, result, `${path}[${index}]`, 'mask binding');
595
- return result;
596
- }));
597
- }
598
-
599
- function parameterReference(
600
- value: unknown,
601
- parameters: ReadonlyMap<string, HyaStateMachineParameter['type']>,
602
- path: string,
603
- ): string {
604
- const result = identifier(value, path);
605
- if (!parameters.has(result)) invalid(`Unknown state-machine parameter "${result}".`, path);
606
- return result;
607
- }
608
-
609
- function numericParameterReference(
610
- value: unknown,
611
- parameters: ReadonlyMap<string, HyaStateMachineParameter['type']>,
612
- path: string,
613
- ): string {
614
- const result = parameterReference(value, parameters, path);
615
- const type = parameters.get(result)!;
616
- if (type !== 'float' && type !== 'integer') {
617
- invalid(`State-machine parameter "${result}" must be float or integer.`, path);
618
- }
619
- return result;
620
- }
621
-
622
- function unique(values: Set<string>, value: string, path: string, kind: string): void {
623
- if (values.has(value)) invalid(`Duplicate ${kind} id "${value}".`, path);
624
- values.add(value);
625
- }
626
-
627
- function knownKeys(value: Record<string, unknown>, allowed: readonly string[], path: string): void {
628
- const allowedKeys = new Set(allowed);
629
- for (const key of Object.keys(value)) {
630
- if (!allowedKeys.has(key)) invalid(`Unknown state-machine property "${key}".`, `${path}.${key}`);
631
- }
632
- }
633
-
634
- function bounded(value: number, minimum: number, maximum: number, label: string, path: string): void {
635
- if (value < minimum || value > maximum) invalid(`${label} count must be in [${minimum}, ${maximum}].`, path);
636
- }
637
-
638
- function oneOf<const T extends readonly string[]>(value: unknown, allowed: T, path: string): T[number] {
639
- if (typeof value !== 'string' || !allowed.includes(value)) invalid(`Expected one of ${allowed.join(', ')}.`, path);
640
- return value as T[number];
641
- }
642
-
643
- function invalid(message: string, path: string): never {
644
- throw new AnimationFormatError('E_ANIMATION_INVALID_FORMAT', message, path);
645
- }
@@ -1,7 +0,0 @@
1
- export const TEXT_SELECTOR_SHAPES = [
2
- 'square', 'ramp-up', 'ramp-down', 'triangle', 'round', 'smooth',
3
- ] as const;
4
-
5
- export const TEXT_SELECTOR_BASES = [
6
- 'characters', 'characters-excluding-spaces', 'words', 'lines',
7
- ] as const;