rig-parts 2.63.1 → 2.65.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/cli.ts +38 -15
- package/docs/AUTHORING.md +194 -36
- package/docs/STABILITY.md +7 -3
- package/package.json +1 -1
- package/src/build.ts +6 -4
- package/src/check.ts +24 -10
- package/src/runstamp.ts +2 -1
- package/src/scene.ts +246 -27
- package/src/sceneclips.ts +195 -45
- package/src/scenerepeat.ts +82 -14
package/src/sceneclips.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
import { bezierForChannel, f32 } from '@rig-c/core/compile';
|
|
3
3
|
import { CompileError } from '@rig-c/core/errors';
|
|
4
4
|
import { parseMotionSpec, type MotionSpec as CoreMotion } from '@rig-c/core/motion';
|
|
5
|
-
import { bezierPolyline, CORE_DOCUMENT_SPEC, CoreInputError, poseRawAnimation, readModel, type RawPose } from '@rig-c/core/core';
|
|
5
|
+
import { bezierPolyline, CORE_DOCUMENT_SPEC, CoreInputError, poseRawAnimation, readModel, type CompiledDocument, type RawPose } from '@rig-c/core/core';
|
|
6
6
|
import { PartsError, refuseIfAny, type Problem } from './errors.ts';
|
|
7
7
|
import type { MotionSpec } from './motion.ts';
|
|
8
8
|
import type { RigSpec } from './rig.ts';
|
|
9
|
+
import { repeatSceneKeys, repeatSceneTimeline, type SceneRepeatSeam } from './scenerepeat.ts';
|
|
9
10
|
|
|
10
11
|
export type SceneAnimations = CoreMotion['animations'];
|
|
11
12
|
|
|
@@ -30,6 +31,9 @@ export interface ResolvedSceneCameraTrack {
|
|
|
30
31
|
keys: Omit<SceneCameraKey, 'ease'>[];
|
|
31
32
|
duration: number;
|
|
32
33
|
loop: boolean;
|
|
34
|
+
/** The source clock for exact per-period coverage phases. */
|
|
35
|
+
period?: number;
|
|
36
|
+
samplePeriod?(time: number): SceneCameraSample;
|
|
33
37
|
/** Sample on the authored clock, including the unwrapped terminal time. */
|
|
34
38
|
sample(time: number): SceneCameraSample;
|
|
35
39
|
}
|
|
@@ -198,7 +202,7 @@ function cameraLoopProblem(first: SceneCameraSample, last: SceneCameraSample, du
|
|
|
198
202
|
}
|
|
199
203
|
|
|
200
204
|
/** Resolve only declared tracks, against the final take and easing names. */
|
|
201
|
-
export function resolveSceneCamera(camera: SceneCamera, motion: MotionSpec): ResolvedSceneCamera {
|
|
205
|
+
export function resolveSceneCamera(camera: SceneCamera, motion: MotionSpec, repeats?: Readonly<Record<string, number>>): ResolvedSceneCamera {
|
|
202
206
|
const problems: Problem[] = [];
|
|
203
207
|
const tracks = new Map<string, ResolvedSceneCameraTrack>();
|
|
204
208
|
for (const [name, authored] of Object.entries(camera.tracks)) {
|
|
@@ -210,14 +214,21 @@ export function resolveSceneCamera(camera: SceneCamera, motion: MotionSpec): Res
|
|
|
210
214
|
}
|
|
211
215
|
const { duration } = take;
|
|
212
216
|
const loop = take.loop === true;
|
|
217
|
+
const count = repeats !== undefined && Object.hasOwn(repeats, name) ? repeats[name] : undefined;
|
|
218
|
+
const period = count === undefined ? duration : duration / count;
|
|
219
|
+
const closedPeriod = loop || count !== undefined;
|
|
213
220
|
const first = authored[0], last = authored.at(-1);
|
|
214
|
-
if (last === undefined || last.t !==
|
|
215
|
-
|
|
221
|
+
if (last === undefined || last.t !== period) {
|
|
222
|
+
const found = last === undefined ? 'no key' : `${last.t} s`;
|
|
223
|
+
const detail = count === undefined
|
|
224
|
+
? `Take ${JSON.stringify(name)} declares ${duration} s and the terminal key must match exactly`
|
|
225
|
+
: `Take ${JSON.stringify(name)} declares ${duration} s and scene.repeats[${JSON.stringify(name)}] is ${count}. The terminal key must match the ${period} s period exactly`;
|
|
226
|
+
problems.push({ code: CAMERA_TRACK, object, detail: `ends at ${found}. ${detail}` });
|
|
216
227
|
continue;
|
|
217
228
|
}
|
|
218
229
|
const before = problems.length;
|
|
219
|
-
if (
|
|
220
|
-
const problem = cameraLoopProblem(first, last,
|
|
230
|
+
if (closedPeriod) {
|
|
231
|
+
const problem = cameraLoopProblem(first, last, period, object, 'Authored');
|
|
221
232
|
if (problem !== null) problems.push(problem);
|
|
222
233
|
}
|
|
223
234
|
|
|
@@ -256,14 +267,22 @@ export function resolveSceneCamera(camera: SceneCamera, motion: MotionSpec): Res
|
|
|
256
267
|
let coreStart: SceneCameraSample | undefined, coreEnd: SceneCameraSample | undefined;
|
|
257
268
|
if (eased) {
|
|
258
269
|
try {
|
|
259
|
-
coreSample = cameraCoreSampler(keys, times,
|
|
270
|
+
coreSample = cameraCoreSampler(keys, times, period, object);
|
|
260
271
|
const storedEnd = Math.fround(times[times.length - 1]);
|
|
261
272
|
coreStart = coreSample(0);
|
|
262
273
|
coreEnd = coreSample(storedEnd);
|
|
263
|
-
if (
|
|
274
|
+
if (closedPeriod) {
|
|
264
275
|
const problem = cameraLoopProblem(coreStart, coreEnd, storedEnd, object, 'Core-evaluated');
|
|
265
276
|
if (problem !== null) problems.push(problem);
|
|
266
277
|
}
|
|
278
|
+
if (count !== undefined && problems.length === before) {
|
|
279
|
+
const penultimate = keys.length - 2;
|
|
280
|
+
const curve = keys[penultimate]?.curve;
|
|
281
|
+
if (Array.isArray(curve)) {
|
|
282
|
+
const problem = curveSeamProblem(curve, times[penultimate], times[times.length - 1], storedEnd, object);
|
|
283
|
+
if (problem !== null) problems.push(problem);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
267
286
|
} catch (err) {
|
|
268
287
|
if (err instanceof PartsError) problems.push(...err.problems);
|
|
269
288
|
else if (err instanceof CoreInputError) problems.push({ code: CAMERA_TRACK, object, detail: err.message });
|
|
@@ -273,32 +292,55 @@ export function resolveSceneCamera(camera: SceneCamera, motion: MotionSpec): Res
|
|
|
273
292
|
}
|
|
274
293
|
const easedStart = keys[0].curve !== undefined;
|
|
275
294
|
const easedEnd = keys.at(-2)?.curve !== undefined;
|
|
276
|
-
const easedSeam =
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
295
|
+
const easedSeam = closedPeriod && (easedStart || easedEnd);
|
|
296
|
+
const checkTime = (time: number, end: number): void => {
|
|
297
|
+
if (!Number.isFinite(time) || time < 0 || time > end) {
|
|
298
|
+
throw new PartsError([{ code: CAMERA_TRACK, object, detail: `cannot sample t=${time} s. Authored time in [0, ${end}] s is required` }]);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
const samplePeriod = (time: number): SceneCameraSample => {
|
|
302
|
+
checkTime(time, period);
|
|
303
|
+
// At a curved loop seam use both unwrapped Core reads. The linear-only
|
|
304
|
+
// seam uses both authored values, so the two ends share a precision.
|
|
305
|
+
const endpoint = time === 0 && (easedSeam || easedStart) ? coreStart
|
|
306
|
+
: time === period && (easedSeam || easedEnd) ? coreEnd : undefined;
|
|
307
|
+
if (endpoint !== undefined) return { pan: [...endpoint.pan], zoom: endpoint.zoom };
|
|
308
|
+
let low = 0, high = keys.length - 1;
|
|
309
|
+
while (low < high) {
|
|
310
|
+
const mid = Math.ceil((low + high) / 2);
|
|
311
|
+
if (keys[mid].t <= time) low = mid;
|
|
312
|
+
else high = mid - 1;
|
|
313
|
+
}
|
|
314
|
+
const from = keys[low], to = keys[low + 1];
|
|
315
|
+
if (from.curve !== undefined && coreSample !== undefined) return coreSample(time);
|
|
316
|
+
if (from.t === time || to === undefined) return checkCameraSample([...from.pan], from.zoom, time, object);
|
|
317
|
+
const phase = (time - from.t) / (to.t - from.t);
|
|
318
|
+
const mix = (a: number, b: number): number => a + (b - a) * phase;
|
|
319
|
+
return checkCameraSample([mix(from.pan[0], to.pan[0]), mix(from.pan[1], to.pan[1])], 1 / mix(1 / from.zoom, 1 / to.zoom), time, object);
|
|
320
|
+
};
|
|
321
|
+
if (count === undefined) {
|
|
322
|
+
tracks.set(name, { keys, duration, loop, sample: samplePeriod });
|
|
323
|
+
} else if (problems.length === before) {
|
|
324
|
+
try {
|
|
325
|
+
tracks.set(name, {
|
|
326
|
+
keys: repeatSceneTimeline(keys, count, period, duration, object), duration, loop,
|
|
327
|
+
period, samplePeriod,
|
|
328
|
+
sample(time) {
|
|
329
|
+
checkTime(time, duration);
|
|
330
|
+
if (time === duration) return samplePeriod(period);
|
|
331
|
+
// Locate the splice by its emitted clock, including a quotient
|
|
332
|
+
// that may round to an adjacent integer at a boundary.
|
|
333
|
+
let copy = Math.min(Math.floor(time / period), count - 1);
|
|
334
|
+
if (copy * period > time) copy--;
|
|
335
|
+
else if (copy + 1 < count && (copy + 1) * period <= time) copy++;
|
|
336
|
+
return samplePeriod(Math.min(time - copy * period, period));
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
} catch (err) {
|
|
340
|
+
if (!(err instanceof PartsError)) throw err;
|
|
341
|
+
problems.push(...err.problems);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
302
344
|
}
|
|
303
345
|
refuseIfAny(problems);
|
|
304
346
|
return { backdrop: camera.backdrop, tracks };
|
|
@@ -355,6 +397,40 @@ export function readSceneAnimations(raw: unknown, where: string): SceneAnimation
|
|
|
355
397
|
}
|
|
356
398
|
}
|
|
357
399
|
|
|
400
|
+
/** Expand scene-authored keys on the composed clock before merging timelines. */
|
|
401
|
+
export function repeatSceneAnimations(clips: SceneAnimations | undefined, groups: Readonly<Record<string, string[]>>,
|
|
402
|
+
repeats?: Readonly<Record<string, number>>, repeatSeams?: SceneRepeatSeam[]): SceneAnimations {
|
|
403
|
+
const authored = clips ?? {};
|
|
404
|
+
if (repeats === undefined) return authored;
|
|
405
|
+
const problems: Problem[] = [];
|
|
406
|
+
const expanded = { ...authored };
|
|
407
|
+
for (const [name, count] of Object.entries(repeats)) {
|
|
408
|
+
const field = `scene.repeats[${JSON.stringify(name)}]`;
|
|
409
|
+
const object = `scene.animations.${name}`;
|
|
410
|
+
const clip = Object.hasOwn(authored, name) ? authored[name] : undefined;
|
|
411
|
+
if (clip === undefined || !(clip.duration > 0)) {
|
|
412
|
+
problems.push({ code: 'SCENE_REPEAT_DURATION', object: field,
|
|
413
|
+
detail: `is ${count} and ${object}.duration is ${clip === undefined ? 'absent' : `${clip.duration} s`}. Declare a positive whole take duration` });
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
const before = problems.length;
|
|
417
|
+
for (const family of ['ik', 'transform', 'sequence', 'drawOrder', 'events'] as const) {
|
|
418
|
+
if ((clip[family]?.length ?? 0) > 0) problems.push({ code: 'SCENE_REPEAT_KEY_SEAM', object: `${object}.${family}`,
|
|
419
|
+
detail: `${field} is ${count}. This declaration splices scene tracks, deforms and camera keys; this timeline has no defined repeat splice` });
|
|
420
|
+
}
|
|
421
|
+
if (problems.length !== before) continue;
|
|
422
|
+
try {
|
|
423
|
+
const capture = repeatSeams === undefined ? undefined : { take: name, into: repeatSeams };
|
|
424
|
+
expanded[name] = repeatSceneKeys({ ...clip, duration: clip.duration / count }, count, clip.duration, groups, object, field, capture);
|
|
425
|
+
} catch (err) {
|
|
426
|
+
if (!(err instanceof PartsError)) throw err;
|
|
427
|
+
problems.push(...err.problems);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
refuseIfAny(problems);
|
|
431
|
+
return expanded;
|
|
432
|
+
}
|
|
433
|
+
|
|
358
434
|
/** Core slider source properties and the bone timeline channels that feed them. */
|
|
359
435
|
function sliderInputChannel(source: unknown, property: string): number | null {
|
|
360
436
|
switch (source) {
|
|
@@ -531,6 +607,87 @@ function storedIdleLoopDifference(modelText: string): { difference: string | nul
|
|
|
531
607
|
interface StoredCurveKey { time: number; curve?: number[] | 'stepped' }
|
|
532
608
|
interface StoredTrackTarget { name: string; timelines: Array<{ name: string; keys: StoredCurveKey[] }> }
|
|
533
609
|
|
|
610
|
+
function curveSeamProblem(curve: number[], start: number, end: number, duration: number, object: string): Problem | null {
|
|
611
|
+
for (let i = 0; i < curve.length; i += 4) {
|
|
612
|
+
const points = bezierPolyline(start, 0, curve[i], 0, curve[i + 2], 0, end, 0);
|
|
613
|
+
const overlap = points.find((n, j) => j % 2 === 0 && n >= duration);
|
|
614
|
+
if (overlap !== undefined) return { code: 'SCENE_REPEAT_KEY_SEAM', object,
|
|
615
|
+
detail: `Bezier channel ${i / 4} has a stored time sample ${overlap} s at or past the closing key ${duration} s. The source channel must splice without changing its keyed motion` };
|
|
616
|
+
}
|
|
617
|
+
return null;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
type CompiledSceneAnimation = CompiledDocument['animations'][number];
|
|
621
|
+
interface CompiledSeamKey {
|
|
622
|
+
time: number;
|
|
623
|
+
stated: number | { time: number };
|
|
624
|
+
curve: number[] | 'linear' | 'stepped';
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function compiledSeamKeys(animation: CompiledSceneAnimation, address: SceneRepeatSeam['timeline']): readonly CompiledSeamKey[] | null | undefined {
|
|
628
|
+
const { timelines, constraints } = animation;
|
|
629
|
+
if (address.family === 'deform') {
|
|
630
|
+
return timelines.attachments.find(a => a.skin === address.skin && a.slot === address.slot && a.attachment === address.attachment)?.deform;
|
|
631
|
+
}
|
|
632
|
+
const { family, target, property } = address;
|
|
633
|
+
if (family === 'path') {
|
|
634
|
+
const track = constraints.path.find(t => t.name === target);
|
|
635
|
+
return property === 'position' || property === 'spacing' || property === 'mix' ? track?.[property] : undefined;
|
|
636
|
+
}
|
|
637
|
+
if (family === 'slider') {
|
|
638
|
+
const track = constraints.slider.find(t => t.name === target);
|
|
639
|
+
return property === 'time' || property === 'mix' ? track?.[property] : undefined;
|
|
640
|
+
}
|
|
641
|
+
const bone = family === 'bone' || family === 'group'
|
|
642
|
+
? timelines.bones.find(t => t.name === target)?.timelines.find(t => t.kind === property)?.keys : undefined;
|
|
643
|
+
const slot = family === 'slot' || family === 'group'
|
|
644
|
+
? timelines.slots.find(t => t.name === target)?.timelines.find(t => t.kind === property)?.keys : undefined;
|
|
645
|
+
const physics = family === 'physics' || family === 'group'
|
|
646
|
+
? constraints.physicsKeyed?.find(t => t.name === target && t.kind === property)?.keys : undefined;
|
|
647
|
+
const matches = [bone, slot, physics].filter(keys => keys !== undefined);
|
|
648
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** Read the final Core model so both raw curves and expanded named easings use
|
|
652
|
+
* the actual stored handles. Key ordinals identify the splice after quantizing
|
|
653
|
+
* the clock; a different timeline can have a key at the same time.
|
|
654
|
+
*/
|
|
655
|
+
export function sceneRepeatCurveProblems(modelText: string, seams: readonly SceneRepeatSeam[]): Problem[] {
|
|
656
|
+
if (seams.length === 0) return [];
|
|
657
|
+
let doc: CompiledDocument;
|
|
658
|
+
try { doc = readModel(modelText, 'composed repeat model'); }
|
|
659
|
+
catch (err) {
|
|
660
|
+
if (!(err instanceof CoreInputError)) throw err;
|
|
661
|
+
return [{ code: 'SCENE_REPEAT_KEY_SEAM', object: seams[0].object, detail: err.message }];
|
|
662
|
+
}
|
|
663
|
+
const problems: Problem[] = [];
|
|
664
|
+
const statedTime = (key: CompiledSeamKey): number => typeof key.stated === 'number' ? key.stated : key.stated.time;
|
|
665
|
+
for (const { take, object, timeline, stride, count } of seams) {
|
|
666
|
+
const animation = doc.animations.find(a => a.name === take);
|
|
667
|
+
const keys = animation === undefined ? undefined : compiledSeamKeys(animation, timeline);
|
|
668
|
+
const timelineName = timeline.family === 'deform' ? `deform ${timeline.skin}/${timeline.slot}/${timeline.attachment}`
|
|
669
|
+
: `${timeline.family} ${JSON.stringify(timeline.target)} ${timeline.property}`;
|
|
670
|
+
const where = `${object} (${timelineName} in take ${JSON.stringify(take)})`;
|
|
671
|
+
const expected = stride * count + 1;
|
|
672
|
+
if (keys?.length !== expected) {
|
|
673
|
+
problems.push({ code: 'SCENE_REPEAT_KEY_SEAM', object: where,
|
|
674
|
+
detail: `compiled timeline has ${keys === undefined || keys === null ? 'no uniquely resolved key list' : `${keys.length} keys`}. The splice requires ${expected} keys to locate its closing segments` });
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
for (let copy = 1; copy <= count; copy++) {
|
|
678
|
+
const i = copy * stride;
|
|
679
|
+
const before = keys[i - 1], end = keys[i];
|
|
680
|
+
if (!Array.isArray(before.curve)) continue;
|
|
681
|
+
const problem = curveSeamProblem(before.curve, statedTime(before), statedTime(end), end.time, where);
|
|
682
|
+
if (problem !== null) {
|
|
683
|
+
problems.push(problem);
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return problems;
|
|
689
|
+
}
|
|
690
|
+
|
|
534
691
|
/** At a splice, Core's sampled Bezier must reach the closing key from the left.
|
|
535
692
|
* Check the stored polyline, including curves generated from named easings.
|
|
536
693
|
*/
|
|
@@ -548,17 +705,10 @@ function sourceCurveSeamProblems(modelText: string, duration: number, object: st
|
|
|
548
705
|
const check = (keys: StoredCurveKey[], timeline: string, deform = false): void => {
|
|
549
706
|
const before = keys.at(-2), end = keys.at(-1);
|
|
550
707
|
if (end === undefined || Math.fround(end.time) !== duration || !Array.isArray(before?.curve)) return;
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
const points = bezierPolyline(deform ? Math.fround(before.time) : before.time, 0, curve[i], 0, curve[i + 2], 0, end.time, 0);
|
|
556
|
-
const overlap = points.find((n, j) => j % 2 === 0 && n >= duration);
|
|
557
|
-
if (overlap === undefined) continue;
|
|
558
|
-
problems.push({ code: 'SCENE_REPEAT_KEY_SEAM', object: `${object}.${timeline}`,
|
|
559
|
-
detail: `Bezier channel ${i / 4} has a stored time sample ${overlap} s at or past the closing key ${duration} s. The source channel must splice without changing its keyed motion` });
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
708
|
+
// Core's deform polyline uses the stored float for the leading key time;
|
|
709
|
+
// bone and physics curves use its name.
|
|
710
|
+
const problem = curveSeamProblem(before.curve, deform ? Math.fround(before.time) : before.time, end.time, duration, `${object}.${timeline}`);
|
|
711
|
+
if (problem !== null) problems.push(problem);
|
|
562
712
|
};
|
|
563
713
|
for (const [family, targets] of [['bone', idle.bones], ['slot', idle.slots], ['physics', idle.constraints.physics]] as const) {
|
|
564
714
|
for (const target of targets) for (const track of target.timelines) check(track.keys, `${family} ${JSON.stringify(target.name)} ${track.name}`);
|
package/src/scenerepeat.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Periodic splicing for
|
|
1
|
+
/** Periodic splicing for character motion, scene motion and camera keys. */
|
|
2
2
|
import { isDeepStrictEqual } from 'node:util';
|
|
3
3
|
import { PartsError } from './errors.ts';
|
|
4
4
|
|
|
@@ -10,8 +10,26 @@ interface Key {
|
|
|
10
10
|
|
|
11
11
|
interface Idle {
|
|
12
12
|
duration: number;
|
|
13
|
-
tracks: Array<{ property: string; bone?: string; group?: string; physics?: string; lag?: number; stagger?: number; keys: Key[] }>;
|
|
14
|
-
deform?: Array<{ slot: string; attachment: string; keys: Key[] }>;
|
|
13
|
+
tracks: Array<{ property: string; bone?: string; slot?: string; group?: string; physics?: string; path?: string; slider?: string; lag?: number; stagger?: number; keys: Key[] }>;
|
|
14
|
+
deform?: Array<{ skin?: string; slot: string; attachment: string; keys: Key[] }>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type TrackFamily = 'bone' | 'slot' | 'physics' | 'path' | 'slider' | 'group';
|
|
18
|
+
|
|
19
|
+
export interface SceneRepeatSeam {
|
|
20
|
+
take: string;
|
|
21
|
+
object: string;
|
|
22
|
+
timeline: { family: TrackFamily; target: string; property: string }
|
|
23
|
+
| { family: 'deform'; skin: string; slot: string; attachment: string };
|
|
24
|
+
/** The closing key of copy i is at i * stride, including the final key. */
|
|
25
|
+
stride: number;
|
|
26
|
+
count: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface SceneRepeatSeamCapture {
|
|
30
|
+
take: string;
|
|
31
|
+
into: SceneRepeatSeam[];
|
|
32
|
+
mapTarget?: (name: string) => string;
|
|
15
33
|
}
|
|
16
34
|
|
|
17
35
|
function refuse(object: string, detail: string): never {
|
|
@@ -42,7 +60,7 @@ function atOffset<K extends Key>(key: K, offset: number, time = key.t + offset):
|
|
|
42
60
|
}
|
|
43
61
|
|
|
44
62
|
function repeatTimeline<K extends Key>(keys: K[], count: number, period: number, duration: number, object: string,
|
|
45
|
-
clocks: ReadonlyArray<{ target: string; shift: number }>, property?: string): K[] {
|
|
63
|
+
clocks: ReadonlyArray<{ target: string; shift: number }>, property?: string, captureCurveSeam?: (stride: number) => void): K[] {
|
|
46
64
|
const first = keys[0], last = keys[keys.length - 1], before = keys[keys.length - 2];
|
|
47
65
|
if (first === undefined) refuse(object, 'has no source keys');
|
|
48
66
|
const startState = state(first), endState = state(last);
|
|
@@ -85,27 +103,77 @@ function repeatTimeline<K extends Key>(keys: K[], count: number, period: number,
|
|
|
85
103
|
out.push(atOffset(keys[j], k * period, terminal));
|
|
86
104
|
}
|
|
87
105
|
}
|
|
106
|
+
if (sharesKey && before !== undefined && (Array.isArray(before.curve) || (typeof before.ease === 'string' && before.ease !== 'stepped'))) {
|
|
107
|
+
captureCurveSeam?.(stride);
|
|
108
|
+
}
|
|
88
109
|
return out;
|
|
89
110
|
}
|
|
90
111
|
|
|
91
|
-
|
|
92
|
-
const period = idle.duration;
|
|
112
|
+
function checkRepeatClock(count: number, period: number, duration: number, object: string): void {
|
|
93
113
|
if (!Number.isFinite(Math.fround(duration)) || Math.fround(period) === 0 || Math.fround((count - 1) * period) === Math.fround(duration)) {
|
|
94
114
|
refuse(object, `the source period ${period} s cannot separate ${count} copies on the float32 playback clock at ${duration} s`);
|
|
95
115
|
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function repeatSceneTimeline<K extends Key>(keys: K[], count: number, period: number, duration: number, object: string): K[] {
|
|
119
|
+
checkRepeatClock(count, period, duration, object);
|
|
120
|
+
return repeatTimeline(keys, count, period, duration, object, [{ target: object, shift: 0 }]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function repeatSceneKeys<T extends Idle>(idle: T, count: number, duration: number, groups: Readonly<Record<string, string[]>>, object: string,
|
|
124
|
+
sceneRepeatField?: string, capture?: SceneRepeatSeamCapture): T {
|
|
125
|
+
const period = idle.duration;
|
|
126
|
+
checkRepeatClock(count, period, duration, object);
|
|
127
|
+
const recordSeam = (timeline: SceneRepeatSeam['timeline'], where: string, stride: number): void => {
|
|
128
|
+
if (capture !== undefined) capture.into.push({ take: capture.take, object: where, timeline, stride, count });
|
|
129
|
+
};
|
|
130
|
+
const trackClocks = (track: Idle['tracks'][number], where: string) => {
|
|
131
|
+
const sceneTarget = sceneRepeatField === undefined ? undefined : track.slot ?? track.path ?? track.slider;
|
|
132
|
+
const targets = track.group === undefined ? [track.bone ?? track.physics ?? sceneTarget ?? track.property]
|
|
133
|
+
: Object.hasOwn(groups, track.group) ? groups[track.group] : [];
|
|
134
|
+
if (targets.length === 0) refuse(where, `group ${JSON.stringify(track.group)} has no resolved members`);
|
|
135
|
+
return targets.map((name, index) => ({ name, target: JSON.stringify(name), shift: (track.lag ?? 0) + (track.stagger ?? 0) * index }));
|
|
136
|
+
};
|
|
137
|
+
if (sceneRepeatField !== undefined) {
|
|
138
|
+
// A clip's end is the latest effective key across its timelines. Closed
|
|
139
|
+
// state channels can hold through a gap, and resets must stay interior.
|
|
140
|
+
let end = -Infinity;
|
|
141
|
+
for (const [i, track] of idle.tracks.entries()) {
|
|
142
|
+
const key = track.keys.at(-1);
|
|
143
|
+
if (key === undefined) continue;
|
|
144
|
+
for (const { shift } of trackClocks(track, `${object}.tracks[${i}]`)) end = Math.max(end, key.t + shift);
|
|
145
|
+
}
|
|
146
|
+
for (const deform of idle.deform ?? []) {
|
|
147
|
+
const key = deform.keys.at(-1);
|
|
148
|
+
if (key !== undefined) end = Math.max(end, key.t);
|
|
149
|
+
}
|
|
150
|
+
if (end !== -Infinity && end !== period) throw new PartsError([{
|
|
151
|
+
code: 'SCENE_REPEAT_DURATION', object,
|
|
152
|
+
detail: `keys end at ${end} s. ${object}.duration declares ${duration} s and ${sceneRepeatField} is ${count}. The terminal time must match the ${period} s period exactly`,
|
|
153
|
+
}]);
|
|
154
|
+
}
|
|
96
155
|
return {
|
|
97
156
|
...idle,
|
|
98
157
|
duration,
|
|
99
158
|
tracks: idle.tracks.map((track, i) => {
|
|
100
159
|
const where = `${object}.tracks[${i}]`;
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
160
|
+
const clocks = trackClocks(track, where);
|
|
161
|
+
const record = capture === undefined ? undefined : (stride: number): void => {
|
|
162
|
+
const family = (['bone', 'slot', 'physics', 'path', 'slider', 'group'] as const).find(field => track[field] !== undefined);
|
|
163
|
+
if (family === undefined) return; // Core refuses a track with no target before the seam check.
|
|
164
|
+
for (const { name } of clocks) {
|
|
165
|
+
recordSeam({ family, target: capture.mapTarget?.(name) ?? name, property: track.property }, where, stride);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
return { ...track, keys: repeatTimeline(track.keys, count, period, duration, where, clocks, track.property, record) };
|
|
105
169
|
}),
|
|
106
|
-
...(idle.deform === undefined ? {} : { deform: idle.deform.map((deform, i) =>
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
170
|
+
...(idle.deform === undefined ? {} : { deform: idle.deform.map((deform, i) => {
|
|
171
|
+
const where = `${object}.deform[${i}]`;
|
|
172
|
+
const clocks = [{ target: `${deform.slot}/${deform.attachment}`, shift: 0 }];
|
|
173
|
+
const record = capture === undefined ? undefined : (stride: number): void => {
|
|
174
|
+
recordSeam({ family: 'deform', skin: deform.skin ?? 'default', slot: deform.slot, attachment: deform.attachment }, where, stride);
|
|
175
|
+
};
|
|
176
|
+
return { ...deform, keys: repeatTimeline(deform.keys, count, period, duration, where, clocks, undefined, record) };
|
|
177
|
+
}) }),
|
|
110
178
|
};
|
|
111
179
|
}
|