@ikijs/mcp 0.26.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/dist/{chunk-K2CWU4AL.mjs → chunk-7POOGY7U.mjs} +989 -70
- package/dist/chunk-7POOGY7U.mjs.map +1 -0
- package/dist/cli.js +990 -74
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +88 -1
- package/dist/index.d.ts +88 -1
- package/dist/index.js +994 -76
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/render-page.global.js +3 -0
- package/package.json +3 -3
- package/dist/chunk-K2CWU4AL.mjs.map +0 -1
|
@@ -8,6 +8,10 @@ function clamp(value, min, max) {
|
|
|
8
8
|
function lerp(a, b, t) {
|
|
9
9
|
return a + (b - a) * t;
|
|
10
10
|
}
|
|
11
|
+
function smoothstep(x) {
|
|
12
|
+
const t = clamp(x, 0, 1);
|
|
13
|
+
return t * t * (3 - 2 * t);
|
|
14
|
+
}
|
|
11
15
|
|
|
12
16
|
// ../engine/src/parameter-store.ts
|
|
13
17
|
var ParameterStore = class {
|
|
@@ -413,17 +417,472 @@ function resolveDeformers(deformers, params) {
|
|
|
413
417
|
import { StandardParameter } from "@ikijs/format";
|
|
414
418
|
|
|
415
419
|
// ../engine/src/frame-clock.ts
|
|
420
|
+
var MAX_DT_MS = 100;
|
|
416
421
|
var FIXED_DT_S = 1 / 60;
|
|
422
|
+
var MAX_SUBSTEPS = 6;
|
|
423
|
+
var FixedStepClock = class {
|
|
424
|
+
prevNowMs = void 0;
|
|
425
|
+
accumulatorS = 0;
|
|
426
|
+
/**
|
|
427
|
+
* True until the first {@link advance}. Drivers seed their rest pose on that
|
|
428
|
+
* frame and integrate nothing, so a model loaded mid-motion does not kick.
|
|
429
|
+
*/
|
|
430
|
+
get isSeedFrame() {
|
|
431
|
+
return this.prevNowMs === void 0;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Fold `nowMs` into the accumulator and return how many {@link FIXED_DT_S}
|
|
435
|
+
* sub-steps to run this frame. On the seed frame it only records the
|
|
436
|
+
* timestamp and returns 0. A non-monotonic `nowMs` floors to 0 — no rewind.
|
|
437
|
+
*/
|
|
438
|
+
advance(nowMs) {
|
|
439
|
+
if (!Number.isFinite(nowMs)) return 0;
|
|
440
|
+
if (this.prevNowMs === void 0) {
|
|
441
|
+
this.prevNowMs = nowMs;
|
|
442
|
+
return 0;
|
|
443
|
+
}
|
|
444
|
+
const dtMs = clamp(nowMs - this.prevNowMs, 0, MAX_DT_MS);
|
|
445
|
+
this.prevNowMs = nowMs;
|
|
446
|
+
this.accumulatorS += dtMs / 1e3;
|
|
447
|
+
let steps = 0;
|
|
448
|
+
while (this.accumulatorS >= FIXED_DT_S && steps < MAX_SUBSTEPS) {
|
|
449
|
+
this.accumulatorS -= FIXED_DT_S;
|
|
450
|
+
steps++;
|
|
451
|
+
}
|
|
452
|
+
return steps;
|
|
453
|
+
}
|
|
454
|
+
};
|
|
417
455
|
|
|
418
456
|
// ../engine/src/idle-motion.ts
|
|
457
|
+
var BLINK_INTERVAL_MIN_MS = 1500;
|
|
458
|
+
var BLINK_INTERVAL_MAX_MS = 6e3;
|
|
419
459
|
var BLINK_CLOSE_MS = 60;
|
|
420
460
|
var BLINK_HOLD_MS = 20;
|
|
421
461
|
var BLINK_OPEN_MS = 100;
|
|
422
462
|
var BLINK_DURATION_MS = BLINK_CLOSE_MS + BLINK_HOLD_MS + BLINK_OPEN_MS;
|
|
463
|
+
var BREATH_PERIOD_MS = 3500;
|
|
464
|
+
var GAZE_RADIUS = 0.3;
|
|
465
|
+
var GAZE_RETARGET_MIN_MS = 1200;
|
|
466
|
+
var GAZE_RETARGET_MAX_MS = 3e3;
|
|
467
|
+
var GAZE_EASE_RATE = 1e-3;
|
|
468
|
+
var SWAY_X_AMP_A_DEG = 2.2;
|
|
469
|
+
var SWAY_X_PERIOD_A_MS = 6100;
|
|
470
|
+
var SWAY_X_AMP_B_DEG = 1.3;
|
|
471
|
+
var SWAY_X_PERIOD_B_MS = 9700;
|
|
472
|
+
var SWAY_Y_AMP_DEG = 1.6;
|
|
473
|
+
var SWAY_Y_PERIOD_MS = 7300;
|
|
474
|
+
var SWAY_Z_AMP_DEG = 1.1;
|
|
475
|
+
var SWAY_Z_PERIOD_MS = 11300;
|
|
476
|
+
function blinkEnvelope(phase) {
|
|
477
|
+
const ms = phase * BLINK_DURATION_MS;
|
|
478
|
+
if (ms <= 0) return 1;
|
|
479
|
+
if (ms < BLINK_CLOSE_MS) return 1 - smoothstep(ms / BLINK_CLOSE_MS);
|
|
480
|
+
if (ms <= BLINK_CLOSE_MS + BLINK_HOLD_MS) return 0;
|
|
481
|
+
if (ms < BLINK_DURATION_MS) {
|
|
482
|
+
const t = (ms - BLINK_CLOSE_MS - BLINK_HOLD_MS) / BLINK_OPEN_MS;
|
|
483
|
+
return 1 - (1 - t) * (1 - t);
|
|
484
|
+
}
|
|
485
|
+
return 1;
|
|
486
|
+
}
|
|
487
|
+
function randomInDisk(rng, radius) {
|
|
488
|
+
const r = Math.sqrt(rng()) * radius;
|
|
489
|
+
const theta = rng() * 2 * Math.PI;
|
|
490
|
+
return [r * Math.cos(theta), r * Math.sin(theta)];
|
|
491
|
+
}
|
|
492
|
+
var IdleMotion = class {
|
|
493
|
+
/**
|
|
494
|
+
* Every parameter id {@link update} and `emitRestingPose` write, in write
|
|
495
|
+
* order. Published so a host (or IkiMotion) can restore them after it
|
|
496
|
+
* stops driving — keep in step with those two methods; the test suite
|
|
497
|
+
* pins this list to the actual emissions.
|
|
498
|
+
*/
|
|
499
|
+
drivenParameterIds = [
|
|
500
|
+
StandardParameter.EyeOpenLeft,
|
|
501
|
+
StandardParameter.EyeOpenRight,
|
|
502
|
+
StandardParameter.Breath,
|
|
503
|
+
StandardParameter.EyeballX,
|
|
504
|
+
StandardParameter.EyeballY,
|
|
505
|
+
StandardParameter.AngleX,
|
|
506
|
+
StandardParameter.AngleY,
|
|
507
|
+
StandardParameter.AngleZ
|
|
508
|
+
];
|
|
509
|
+
sink;
|
|
510
|
+
rng;
|
|
511
|
+
// Internal clock: advances by clamped dt, NOT by raw wall-clock jumps.
|
|
512
|
+
// This is the only clock blink/breath/gaze scheduling reads.
|
|
513
|
+
clockMs = 0;
|
|
514
|
+
prevNowMs = void 0;
|
|
515
|
+
// Blink state
|
|
516
|
+
nextBlinkAtMs;
|
|
517
|
+
blinkStartMs = -1;
|
|
518
|
+
// -1 means not currently blinking
|
|
519
|
+
// Gaze state
|
|
520
|
+
gazeCurrentX = 0;
|
|
521
|
+
gazeCurrentY = 0;
|
|
522
|
+
gazeTargetX = 0;
|
|
523
|
+
gazeTargetY = 0;
|
|
524
|
+
nextGazeRetargetMs;
|
|
525
|
+
constructor(sink, options) {
|
|
526
|
+
this.sink = sink;
|
|
527
|
+
this.rng = options?.rng ?? Math.random;
|
|
528
|
+
this.nextBlinkAtMs = lerp(
|
|
529
|
+
BLINK_INTERVAL_MIN_MS,
|
|
530
|
+
BLINK_INTERVAL_MAX_MS,
|
|
531
|
+
this.rng()
|
|
532
|
+
);
|
|
533
|
+
this.nextGazeRetargetMs = lerp(
|
|
534
|
+
GAZE_RETARGET_MIN_MS,
|
|
535
|
+
GAZE_RETARGET_MAX_MS,
|
|
536
|
+
this.rng()
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Advance the idle animation to the given wall-clock timestamp (milliseconds).
|
|
541
|
+
*
|
|
542
|
+
* On the first call: record prevNowMs, emit the resting pose, and return —
|
|
543
|
+
* no animation advance happens so there is no jump from time 0.
|
|
544
|
+
*
|
|
545
|
+
* On subsequent calls: compute dt = clamp(nowMs - prevNowMs, 0, MAX_DT_MS)
|
|
546
|
+
* and advance the internal clock by dt. A non-monotonic nowMs produces a
|
|
547
|
+
* negative raw delta that the clamp floors to 0 — no rewind.
|
|
548
|
+
*/
|
|
549
|
+
update(nowMs) {
|
|
550
|
+
if (!Number.isFinite(nowMs)) return;
|
|
551
|
+
if (this.prevNowMs === void 0) {
|
|
552
|
+
this.prevNowMs = nowMs;
|
|
553
|
+
this.emitRestingPose();
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
const rawDt = nowMs - this.prevNowMs;
|
|
557
|
+
const dt = clamp(rawDt, 0, MAX_DT_MS);
|
|
558
|
+
this.prevNowMs = nowMs;
|
|
559
|
+
this.clockMs += dt;
|
|
560
|
+
const eyeVal = this.advanceBlink();
|
|
561
|
+
this.sink(StandardParameter.EyeOpenLeft, eyeVal);
|
|
562
|
+
this.sink(StandardParameter.EyeOpenRight, eyeVal);
|
|
563
|
+
const breath = this.advanceBreath();
|
|
564
|
+
this.advanceGaze(dt);
|
|
565
|
+
this.sink(StandardParameter.Breath, breath);
|
|
566
|
+
this.sink(StandardParameter.EyeballX, this.gazeCurrentX);
|
|
567
|
+
this.sink(StandardParameter.EyeballY, this.gazeCurrentY);
|
|
568
|
+
this.sink(StandardParameter.AngleX, this.swayX());
|
|
569
|
+
this.sink(StandardParameter.AngleY, this.swayY());
|
|
570
|
+
this.sink(StandardParameter.AngleZ, this.swayZ());
|
|
571
|
+
}
|
|
572
|
+
// ---------------------------------------------------------------------------
|
|
573
|
+
// Private helpers
|
|
574
|
+
// ---------------------------------------------------------------------------
|
|
575
|
+
emitRestingPose() {
|
|
576
|
+
this.sink(StandardParameter.EyeOpenLeft, 1);
|
|
577
|
+
this.sink(StandardParameter.EyeOpenRight, 1);
|
|
578
|
+
this.sink(StandardParameter.Breath, 0.5);
|
|
579
|
+
this.sink(StandardParameter.EyeballX, 0);
|
|
580
|
+
this.sink(StandardParameter.EyeballY, 0);
|
|
581
|
+
this.sink(StandardParameter.AngleX, 0);
|
|
582
|
+
this.sink(StandardParameter.AngleY, 0);
|
|
583
|
+
this.sink(StandardParameter.AngleZ, 0);
|
|
584
|
+
}
|
|
585
|
+
/** Returns the current eye-open value (0..1) and advances blink state. */
|
|
586
|
+
advanceBlink() {
|
|
587
|
+
const { clockMs } = this;
|
|
588
|
+
if (this.blinkStartMs >= 0) {
|
|
589
|
+
const phase = (clockMs - this.blinkStartMs) / BLINK_DURATION_MS;
|
|
590
|
+
if (phase >= 1) {
|
|
591
|
+
this.blinkStartMs = -1;
|
|
592
|
+
this.nextBlinkAtMs = clockMs + lerp(BLINK_INTERVAL_MIN_MS, BLINK_INTERVAL_MAX_MS, this.rng());
|
|
593
|
+
return 1;
|
|
594
|
+
}
|
|
595
|
+
return blinkEnvelope(phase);
|
|
596
|
+
}
|
|
597
|
+
if (clockMs >= this.nextBlinkAtMs) {
|
|
598
|
+
this.blinkStartMs = clockMs;
|
|
599
|
+
return blinkEnvelope(0);
|
|
600
|
+
}
|
|
601
|
+
return 1;
|
|
602
|
+
}
|
|
603
|
+
advanceBreath() {
|
|
604
|
+
return 0.5 + 0.5 * Math.sin(2 * Math.PI * this.clockMs / BREATH_PERIOD_MS);
|
|
605
|
+
}
|
|
606
|
+
/** Horizontal head sway in degrees, pure function of the internal clock. */
|
|
607
|
+
swayX() {
|
|
608
|
+
const t = 2 * Math.PI * this.clockMs;
|
|
609
|
+
return SWAY_X_AMP_A_DEG * Math.sin(t / SWAY_X_PERIOD_A_MS) + SWAY_X_AMP_B_DEG * Math.sin(t / SWAY_X_PERIOD_B_MS);
|
|
610
|
+
}
|
|
611
|
+
/** Vertical head sway in degrees, pure function of the internal clock. */
|
|
612
|
+
swayY() {
|
|
613
|
+
return SWAY_Y_AMP_DEG * Math.sin(2 * Math.PI * this.clockMs / SWAY_Y_PERIOD_MS);
|
|
614
|
+
}
|
|
615
|
+
/** Head roll sway in degrees, pure function of the internal clock. */
|
|
616
|
+
swayZ() {
|
|
617
|
+
return SWAY_Z_AMP_DEG * Math.sin(2 * Math.PI * this.clockMs / SWAY_Z_PERIOD_MS);
|
|
618
|
+
}
|
|
619
|
+
/** Ease gaze current toward target; pick a new target on the internal clock. */
|
|
620
|
+
advanceGaze(dt) {
|
|
621
|
+
if (this.clockMs >= this.nextGazeRetargetMs) {
|
|
622
|
+
const [tx, ty] = randomInDisk(this.rng, GAZE_RADIUS);
|
|
623
|
+
this.gazeTargetX = tx;
|
|
624
|
+
this.gazeTargetY = ty;
|
|
625
|
+
this.nextGazeRetargetMs = this.clockMs + lerp(GAZE_RETARGET_MIN_MS, GAZE_RETARGET_MAX_MS, this.rng());
|
|
626
|
+
}
|
|
627
|
+
const factor = 1 - Math.pow(1 - GAZE_EASE_RATE, dt);
|
|
628
|
+
this.gazeCurrentX = lerp(this.gazeCurrentX, this.gazeTargetX, factor);
|
|
629
|
+
this.gazeCurrentY = lerp(this.gazeCurrentY, this.gazeTargetY, factor);
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
// ../engine/src/physics-motion.ts
|
|
634
|
+
function signedNormalized(value, param) {
|
|
635
|
+
const rest = clamp(param.default, param.min, param.max);
|
|
636
|
+
const den = Math.max(Math.abs(param.max - rest), Math.abs(rest - param.min));
|
|
637
|
+
if (den === 0) return 0;
|
|
638
|
+
return clamp((value - rest) / den, -1, 1);
|
|
639
|
+
}
|
|
640
|
+
var PhysicsMotion = class {
|
|
641
|
+
/** The output parameter of every rig, in rig order — what `emit` writes. */
|
|
642
|
+
drivenParameterIds;
|
|
643
|
+
/**
|
|
644
|
+
* Per rig: how many times the finiteness guard reset its state or `emit`
|
|
645
|
+
* replaced its non-finite product. Up to two per frame, so guard activations,
|
|
646
|
+
* not repaired frames. A diagnostic; emitted values are unchanged.
|
|
647
|
+
*/
|
|
648
|
+
resets;
|
|
649
|
+
resetCounts;
|
|
650
|
+
rigs;
|
|
651
|
+
read;
|
|
652
|
+
sink;
|
|
653
|
+
params;
|
|
654
|
+
// Integrator state, owned here — never stored in a ParameterStore.
|
|
655
|
+
state;
|
|
656
|
+
clock = new FixedStepClock();
|
|
657
|
+
constructor(rigs, params, read, sink) {
|
|
658
|
+
this.rigs = rigs;
|
|
659
|
+
this.read = read;
|
|
660
|
+
this.sink = sink;
|
|
661
|
+
this.params = new Map(params.map((p) => [p.id, p]));
|
|
662
|
+
this.state = rigs.map(() => ({ x: 0, v: 0 }));
|
|
663
|
+
this.drivenParameterIds = rigs.map((rig) => rig.output.parameter);
|
|
664
|
+
this.resetCounts = rigs.map(() => 0);
|
|
665
|
+
this.resets = this.resetCounts;
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Advance every rig to the given wall-clock timestamp (milliseconds).
|
|
669
|
+
*
|
|
670
|
+
* First call: seed each spring to rest AT its current target (so a model
|
|
671
|
+
* loaded with a nonzero input does not kick), emit the resting output, and
|
|
672
|
+
* return without integrating — mirroring IdleMotion's first-frame behavior.
|
|
673
|
+
*
|
|
674
|
+
* Subsequent calls: {@link FixedStepClock} folds the clamped frame delta into
|
|
675
|
+
* its accumulator and returns how many {@link FIXED_DT_S} sub-steps are due;
|
|
676
|
+
* the spring advances that many semi-implicit Euler steps, then each rig emits
|
|
677
|
+
* its output once. The clock's dt clamp and sub-step cap plus the symplectic
|
|
678
|
+
* integrator are what keep it stable across hitches.
|
|
679
|
+
*/
|
|
680
|
+
update(nowMs) {
|
|
681
|
+
if (this.clock.isSeedFrame) {
|
|
682
|
+
this.clock.advance(nowMs);
|
|
683
|
+
for (let i = 0; i < this.rigs.length; i++) {
|
|
684
|
+
const st = this.state[i];
|
|
685
|
+
st.x = this.targetFor(this.rigs[i]);
|
|
686
|
+
st.v = 0;
|
|
687
|
+
this.emit(i, st);
|
|
688
|
+
}
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
const steps = this.clock.advance(nowMs);
|
|
692
|
+
const targets = this.rigs.map((rig) => this.targetFor(rig));
|
|
693
|
+
for (let s = 0; s < steps; s++) {
|
|
694
|
+
for (let i = 0; i < this.rigs.length; i++) {
|
|
695
|
+
this.step(this.rigs[i], this.state[i], targets[i]);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
for (let i = 0; i < this.rigs.length; i++) {
|
|
699
|
+
const st = this.state[i];
|
|
700
|
+
if (!Number.isFinite(st.x) || !Number.isFinite(st.v)) {
|
|
701
|
+
st.x = Number.isFinite(targets[i]) ? targets[i] : 0;
|
|
702
|
+
st.v = 0;
|
|
703
|
+
this.resetCounts[i]++;
|
|
704
|
+
}
|
|
705
|
+
this.emit(i, st);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
/** Spring target = signed-normalized input value × weight. */
|
|
709
|
+
targetFor(rig) {
|
|
710
|
+
const param = this.params.get(rig.input.parameter);
|
|
711
|
+
const value = this.read(rig.input.parameter);
|
|
712
|
+
const norm = param ? signedNormalized(value, param) : 0;
|
|
713
|
+
return norm * rig.input.weight;
|
|
714
|
+
}
|
|
715
|
+
/** One semi-implicit (symplectic) Euler sub-step of FIXED_DT_S seconds. */
|
|
716
|
+
step(rig, st, target2) {
|
|
717
|
+
const accel = (rig.stiffness * (target2 - st.x) - rig.damping * st.v) / rig.mass;
|
|
718
|
+
st.v += accel * FIXED_DT_S;
|
|
719
|
+
st.x += st.v * FIXED_DT_S;
|
|
720
|
+
}
|
|
721
|
+
/** Write outputDefault + x * scale onto the output param via the sink. */
|
|
722
|
+
emit(index, st) {
|
|
723
|
+
const rig = this.rigs[index];
|
|
724
|
+
const outParam = this.params.get(rig.output.parameter);
|
|
725
|
+
const outDefault = outParam ? clamp(outParam.default, outParam.min, outParam.max) : 0;
|
|
726
|
+
const value = outDefault + st.x * rig.output.scale;
|
|
727
|
+
const finite = Number.isFinite(value);
|
|
728
|
+
if (!finite) this.resetCounts[index]++;
|
|
729
|
+
this.sink(rig.output.parameter, finite ? value : outDefault);
|
|
730
|
+
}
|
|
731
|
+
};
|
|
423
732
|
|
|
424
733
|
// ../engine/src/hair-chain-motion.ts
|
|
425
734
|
var DEG2RAD = Math.PI / 180;
|
|
426
735
|
var RAD2DEG = 180 / Math.PI;
|
|
736
|
+
var HairChainMotion = class {
|
|
737
|
+
/** Every segment's output, chain by chain — what `emitSegment` writes. */
|
|
738
|
+
drivenParameterIds;
|
|
739
|
+
/**
|
|
740
|
+
* Per segment, in `drivenParameterIds` order: how many times the finiteness
|
|
741
|
+
* guard reset its state or `emitSegment` replaced its non-finite product. Up
|
|
742
|
+
* to two per frame, so guard activations, not repaired frames. A diagnostic;
|
|
743
|
+
* emitted values are unchanged.
|
|
744
|
+
*/
|
|
745
|
+
resets;
|
|
746
|
+
resetCounts;
|
|
747
|
+
chainData;
|
|
748
|
+
params;
|
|
749
|
+
deformers;
|
|
750
|
+
store;
|
|
751
|
+
read;
|
|
752
|
+
sink;
|
|
753
|
+
clock = new FixedStepClock();
|
|
754
|
+
constructor(chains, params, deformers, read, sink) {
|
|
755
|
+
this.params = new Map(params.map((p) => [p.id, p]));
|
|
756
|
+
this.deformers = deformers;
|
|
757
|
+
this.store = new ParameterStore(params);
|
|
758
|
+
this.read = read;
|
|
759
|
+
this.sink = sink;
|
|
760
|
+
this.chainData = chains.map((chain) => ({
|
|
761
|
+
chain,
|
|
762
|
+
restAnglesRad: chain.segments.map(
|
|
763
|
+
(seg) => seg.restAngle !== void 0 ? seg.restAngle * DEG2RAD : 0
|
|
764
|
+
),
|
|
765
|
+
state: chain.segments.map(() => ({ angle: 0, angularVelocity: 0 }))
|
|
766
|
+
}));
|
|
767
|
+
this.drivenParameterIds = chains.flatMap(
|
|
768
|
+
(c) => c.segments.map((s) => s.output.parameter)
|
|
769
|
+
);
|
|
770
|
+
this.resetCounts = this.drivenParameterIds.map(() => 0);
|
|
771
|
+
this.resets = this.resetCounts;
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Advance every chain to the given wall-clock timestamp (milliseconds).
|
|
775
|
+
*
|
|
776
|
+
* First call: seed every segment to θ=0/ω=0 (rest), emit the rest output
|
|
777
|
+
* (outDefault + 0), and return without integrating — mirrors PhysicsMotion's
|
|
778
|
+
* first-frame behavior so a model loaded in motion does not kick.
|
|
779
|
+
*
|
|
780
|
+
* Subsequent calls: dt = clamp(nowMs - prevNowMs, 0, MAX_DT_MS) → seconds into
|
|
781
|
+
* accumulator. The per-frame world snapshot (anchor world angles) is taken ONCE
|
|
782
|
+
* per update() — NOT per chain — so all chains share a consistent frame snapshot.
|
|
783
|
+
* Fixed FIXED_DT_S sub-steps are run root→tip, capped at MAX_SUBSTEPS; leftover
|
|
784
|
+
* time is carried to the next frame. Segments emit after substeps (even on zero
|
|
785
|
+
* substeps) with a non-finite guard.
|
|
786
|
+
*/
|
|
787
|
+
update(nowMs) {
|
|
788
|
+
if (this.chainData.length === 0) return;
|
|
789
|
+
if (this.clock.isSeedFrame) {
|
|
790
|
+
this.clock.advance(nowMs);
|
|
791
|
+
let flat2 = 0;
|
|
792
|
+
for (const cd of this.chainData) {
|
|
793
|
+
for (let i = 0; i < cd.chain.segments.length; i++) {
|
|
794
|
+
cd.state[i].angle = 0;
|
|
795
|
+
cd.state[i].angularVelocity = 0;
|
|
796
|
+
this.emitSegment(flat2++, cd.chain.segments[i], cd.state[i]);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
const steps = this.clock.advance(nowMs);
|
|
802
|
+
for (const param of this.params.values()) {
|
|
803
|
+
this.store.set(param.id, this.read(param.id));
|
|
804
|
+
}
|
|
805
|
+
const worldMap = resolveDeformers(this.deformers, this.store).worlds;
|
|
806
|
+
const anchorAnglesRad = this.chainData.map(
|
|
807
|
+
(cd) => this.anchorWorldAngleRad(worldMap, cd.chain.anchorDeformer)
|
|
808
|
+
);
|
|
809
|
+
for (let s = 0; s < steps; s++) {
|
|
810
|
+
for (let c = 0; c < this.chainData.length; c++) {
|
|
811
|
+
this.stepChain(this.chainData[c], anchorAnglesRad[c]);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
let flat = 0;
|
|
815
|
+
for (const cd of this.chainData) {
|
|
816
|
+
for (let i = 0; i < cd.chain.segments.length; i++) {
|
|
817
|
+
const st = cd.state[i];
|
|
818
|
+
if (!Number.isFinite(st.angle) || !Number.isFinite(st.angularVelocity)) {
|
|
819
|
+
st.angle = 0;
|
|
820
|
+
st.angularVelocity = 0;
|
|
821
|
+
this.resetCounts[flat]++;
|
|
822
|
+
}
|
|
823
|
+
this.emitSegment(flat++, cd.chain.segments[i], st);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Extract world rotation (radians) from the anchor's Affine tuple.
|
|
829
|
+
* Affine = [a,b,c,d,e,f]; rotation column = (a,b) → atan2(b,a).
|
|
830
|
+
*
|
|
831
|
+
* If the anchor id is absent from the map, THROWS an internal Error — the
|
|
832
|
+
* format validator guarantees the anchor exists, so absence is an invariant
|
|
833
|
+
* break (mirrors resolveDeformers' throw on an unresolved parent).
|
|
834
|
+
*/
|
|
835
|
+
anchorWorldAngleRad(worldMap, anchorId) {
|
|
836
|
+
const world = worldMap.get(anchorId);
|
|
837
|
+
if (!world) {
|
|
838
|
+
throw new Error(
|
|
839
|
+
`HairChainMotion: anchor deformer "${anchorId}" not found in resolved world map \u2014 model not validated?`
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
return Math.atan2(world[1], world[0]);
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* One fixed sub-step of FIXED_DT_S seconds for all segments in a chain.
|
|
846
|
+
*
|
|
847
|
+
* Segments are integrated ROOT→TIP so each segment can read its upstream
|
|
848
|
+
* neighbor's current-substep state when computing the world angle Φ_i.
|
|
849
|
+
* (The chain is causal root-to-tip; reversing the order would use stale θ
|
|
850
|
+
* values from the previous substep for Φ_i computation.)
|
|
851
|
+
*
|
|
852
|
+
* Per-segment semi-implicit (symplectic) Euler:
|
|
853
|
+
* Φ_i = anchorWorldAngleRad + Σ_{j≤i}(restAngle_j + θ_j)
|
|
854
|
+
* α_i = (−stiffness_i·θ_i − strength·sin(Φ_i − gravityAngle_rad) − damping_i·ω_i) / mass_i
|
|
855
|
+
* ω_i += α_i · FIXED_DT_S (velocity updated FIRST = semi-implicit)
|
|
856
|
+
* θ_i += ω_i · FIXED_DT_S (position updated from NEW velocity)
|
|
857
|
+
*
|
|
858
|
+
* The spring term is −stiffness·θ (restoring θ→0); restAngle does NOT appear
|
|
859
|
+
* in the spring term, only in Φ_i for the gravity torque.
|
|
860
|
+
*/
|
|
861
|
+
stepChain(cd, anchorAngleRad) {
|
|
862
|
+
const { chain, restAnglesRad, state } = cd;
|
|
863
|
+
const gravityAngleRad = chain.gravity.angle * DEG2RAD;
|
|
864
|
+
const strength = chain.gravity.strength;
|
|
865
|
+
let worldAngleAccumRad = anchorAngleRad;
|
|
866
|
+
for (let i = 0; i < chain.segments.length; i++) {
|
|
867
|
+
const seg = chain.segments[i];
|
|
868
|
+
const st = state[i];
|
|
869
|
+
worldAngleAccumRad += restAnglesRad[i] + st.angle;
|
|
870
|
+
const phi = worldAngleAccumRad;
|
|
871
|
+
const alpha = (-seg.stiffness * st.angle - strength * Math.sin(phi - gravityAngleRad) - seg.damping * st.angularVelocity) / seg.mass;
|
|
872
|
+
st.angularVelocity += alpha * FIXED_DT_S;
|
|
873
|
+
st.angle += st.angularVelocity * FIXED_DT_S;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
/** Emit outDefault + (θ_i · RAD2DEG) · scale for one segment. */
|
|
877
|
+
emitSegment(flat, seg, st) {
|
|
878
|
+
const outParam = this.params.get(seg.output.parameter);
|
|
879
|
+
const outDefault = outParam ? clamp(outParam.default, outParam.min, outParam.max) : 0;
|
|
880
|
+
const value = outDefault + st.angle * RAD2DEG * seg.output.scale;
|
|
881
|
+
const finite = Number.isFinite(value);
|
|
882
|
+
if (!finite) this.resetCounts[flat]++;
|
|
883
|
+
this.sink(seg.output.parameter, finite ? value : outDefault);
|
|
884
|
+
}
|
|
885
|
+
};
|
|
427
886
|
|
|
428
887
|
// ../engine/src/iki-motion.ts
|
|
429
888
|
import { StandardParameter as StandardParameter2 } from "@ikijs/format";
|
|
@@ -452,6 +911,62 @@ var HEAD_AND_GAZE = /* @__PURE__ */ new Set([
|
|
|
452
911
|
import {
|
|
453
912
|
StandardParameter as P
|
|
454
913
|
} from "@ikijs/format";
|
|
914
|
+
|
|
915
|
+
// ../engine/src/land.ts
|
|
916
|
+
function staticDeformers(model) {
|
|
917
|
+
return (model.deformers ?? []).map(
|
|
918
|
+
(d) => d.kind === "warp" ? { ...d, warps: void 0, warp2d: void 0 } : { ...d, bindings: void 0 }
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
function placement(part, t) {
|
|
922
|
+
return multiply(
|
|
923
|
+
multiply(translate(t.x, t.y), rotate(t.rotation)),
|
|
924
|
+
scale(part.width * t.scaleX, part.height * t.scaleY)
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
function landedPart(part, vertices, deformers, worlds, store) {
|
|
928
|
+
const warpDeformer = deformers.find(
|
|
929
|
+
(d) => d.kind === "warp" && d.id === part.deformer
|
|
930
|
+
);
|
|
931
|
+
const frame = warpDeformer ? warpDeformer.parent : part.deformer;
|
|
932
|
+
const placed = placement(
|
|
933
|
+
part,
|
|
934
|
+
evaluateTransform(part.transform, void 0, store)
|
|
935
|
+
);
|
|
936
|
+
const restPoints = transformGridPoints(
|
|
937
|
+
vertices,
|
|
938
|
+
frame === void 0 ? placed : multiply(worlds.get(frame), placed),
|
|
939
|
+
new Float64Array(vertices.length)
|
|
940
|
+
);
|
|
941
|
+
return {
|
|
942
|
+
part,
|
|
943
|
+
warpDeformer,
|
|
944
|
+
vertices: Float32Array.from(vertices),
|
|
945
|
+
local: new Float32Array(vertices.length),
|
|
946
|
+
landed: new Float32Array(vertices.length),
|
|
947
|
+
restPoints
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
function landPart(landing, store, { worlds, grids }) {
|
|
951
|
+
const { part, warpDeformer, local, landed } = landing;
|
|
952
|
+
applyWarps(landing.vertices, part.warps, store, local);
|
|
953
|
+
const placed = placement(
|
|
954
|
+
part,
|
|
955
|
+
evaluateTransform(part.transform, part.bindings, store)
|
|
956
|
+
);
|
|
957
|
+
if (warpDeformer) {
|
|
958
|
+
const grid = grids.get(warpDeformer.id);
|
|
959
|
+
applyWarpToChild(local, placed, warpDeformer.grid, grid, landed);
|
|
960
|
+
} else if (part.deformer !== void 0) {
|
|
961
|
+
const world = worlds.get(part.deformer);
|
|
962
|
+
transformGridPoints(local, multiply(world, placed), landed);
|
|
963
|
+
} else {
|
|
964
|
+
transformGridPoints(local, placed, landed);
|
|
965
|
+
}
|
|
966
|
+
return landed;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// ../engine/src/folds.ts
|
|
455
970
|
var FLIP = 0;
|
|
456
971
|
var COLLAPSE = 0.1;
|
|
457
972
|
var ANGLES = [
|
|
@@ -515,11 +1030,6 @@ function poseName(values) {
|
|
|
515
1030
|
if (ids.length === 0) return "rest";
|
|
516
1031
|
return ids.map((id) => `${id}=${values[id]}`).join(" ");
|
|
517
1032
|
}
|
|
518
|
-
function staticDeformers(model) {
|
|
519
|
-
return (model.deformers ?? []).map(
|
|
520
|
-
(d) => d.kind === "warp" ? { ...d, warps: void 0, warp2d: void 0 } : { ...d, bindings: void 0 }
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
1033
|
function cornerAreas(points, cols, rows) {
|
|
524
1034
|
const stride = cols + 1;
|
|
525
1035
|
const out = new Float64Array(cols * rows * 4);
|
|
@@ -551,12 +1061,6 @@ function triangleAreas(points, indices) {
|
|
|
551
1061
|
}
|
|
552
1062
|
return out;
|
|
553
1063
|
}
|
|
554
|
-
function placement(part, t) {
|
|
555
|
-
return multiply(
|
|
556
|
-
multiply(translate(t.x, t.y), rotate(t.rotation)),
|
|
557
|
-
scale(part.width * t.scaleX, part.height * t.scaleY)
|
|
558
|
-
);
|
|
559
|
-
}
|
|
560
1064
|
function keepLeast(sweep, areas, pose, expected) {
|
|
561
1065
|
const slot = expected(sweep.target) ? "expected" : "worst";
|
|
562
1066
|
for (let i = 0; i < areas.length; i++) {
|
|
@@ -569,50 +1073,15 @@ function keepLeast(sweep, areas, pose, expected) {
|
|
|
569
1073
|
}
|
|
570
1074
|
}
|
|
571
1075
|
function partSweep(part, mesh, deformers, worlds, store) {
|
|
572
|
-
const
|
|
573
|
-
(d) => d.kind === "warp" && d.id === part.deformer
|
|
574
|
-
);
|
|
575
|
-
const frame = warpDeformer ? warpDeformer.parent : part.deformer;
|
|
576
|
-
const placed = placement(
|
|
577
|
-
part,
|
|
578
|
-
evaluateTransform(part.transform, void 0, store)
|
|
579
|
-
);
|
|
580
|
-
const restPoints = transformGridPoints(
|
|
581
|
-
mesh.vertices,
|
|
582
|
-
frame === void 0 ? placed : multiply(worlds.get(frame), placed),
|
|
583
|
-
new Float64Array(mesh.vertices.length)
|
|
584
|
-
);
|
|
1076
|
+
const landing = landedPart(part, mesh.vertices, deformers, worlds, store);
|
|
585
1077
|
return {
|
|
1078
|
+
...landing,
|
|
586
1079
|
target: { kind: "part", id: part.id },
|
|
587
|
-
restAreas: triangleAreas(restPoints, mesh.indices),
|
|
1080
|
+
restAreas: triangleAreas(landing.restPoints, mesh.indices),
|
|
588
1081
|
tiny: 1e-9 * part.width * part.height,
|
|
589
|
-
|
|
590
|
-
indices: mesh.indices,
|
|
591
|
-
warpDeformer,
|
|
592
|
-
vertices: Float32Array.from(mesh.vertices),
|
|
593
|
-
local: new Float32Array(mesh.vertices.length),
|
|
594
|
-
landed: new Float32Array(mesh.vertices.length),
|
|
595
|
-
restPoints
|
|
1082
|
+
indices: mesh.indices
|
|
596
1083
|
};
|
|
597
1084
|
}
|
|
598
|
-
function landPart(sweep, store, { worlds, grids }) {
|
|
599
|
-
const { part, warpDeformer, local, landed } = sweep;
|
|
600
|
-
applyWarps(sweep.vertices, part.warps, store, local);
|
|
601
|
-
const placed = placement(
|
|
602
|
-
part,
|
|
603
|
-
evaluateTransform(part.transform, part.bindings, store)
|
|
604
|
-
);
|
|
605
|
-
if (warpDeformer) {
|
|
606
|
-
const grid = grids.get(warpDeformer.id);
|
|
607
|
-
applyWarpToChild(local, placed, warpDeformer.grid, grid, landed);
|
|
608
|
-
} else if (part.deformer !== void 0) {
|
|
609
|
-
const world = worlds.get(part.deformer);
|
|
610
|
-
transformGridPoints(local, multiply(world, placed), landed);
|
|
611
|
-
} else {
|
|
612
|
-
transformGridPoints(local, placed, landed);
|
|
613
|
-
}
|
|
614
|
-
return landed;
|
|
615
|
-
}
|
|
616
1085
|
function checkFolds(model, options = {}) {
|
|
617
1086
|
const poses = options.poses ?? foldPoses(model);
|
|
618
1087
|
const deformers = model.deformers ?? [];
|
|
@@ -639,7 +1108,7 @@ function checkFolds(model, options = {}) {
|
|
|
639
1108
|
const store = new ParameterStore(model.parameters);
|
|
640
1109
|
for (const [id, v] of Object.entries(pose.values)) store.set(id, v);
|
|
641
1110
|
const value = (id) => store.get(id);
|
|
642
|
-
const expected = (
|
|
1111
|
+
const expected = (target2) => options.expected?.(target2, pose, value) === true;
|
|
643
1112
|
for (const sweep of gridSweeps) {
|
|
644
1113
|
const { cols, rows } = sweep.deformer.grid;
|
|
645
1114
|
const areas = cornerAreas(
|
|
@@ -730,6 +1199,401 @@ function classify(worst) {
|
|
|
730
1199
|
return "ok";
|
|
731
1200
|
}
|
|
732
1201
|
|
|
1202
|
+
// ../engine/src/physics-runs.ts
|
|
1203
|
+
var FRAME_MS = FIXED_DT_S * 1e3 * (1 + 1e-9);
|
|
1204
|
+
var STEP_FRAMES = 300;
|
|
1205
|
+
var IDLE_FRAMES = 1800;
|
|
1206
|
+
var IDLE_SEED = 1;
|
|
1207
|
+
var MIN_TURN = 1e-9;
|
|
1208
|
+
function run(model, store, rigs, chains, frames, excite) {
|
|
1209
|
+
const raw = /* @__PURE__ */ new Map();
|
|
1210
|
+
const read = (id) => store.get(id);
|
|
1211
|
+
const sink = (id, value) => {
|
|
1212
|
+
let trace = raw.get(id);
|
|
1213
|
+
if (trace === void 0) raw.set(id, trace = []);
|
|
1214
|
+
trace.push(value);
|
|
1215
|
+
store.set(id, value);
|
|
1216
|
+
};
|
|
1217
|
+
const { parameters } = model;
|
|
1218
|
+
const drivers = [
|
|
1219
|
+
new PhysicsMotion(rigs, parameters, read, sink),
|
|
1220
|
+
new HairChainMotion(chains, parameters, model.deformers ?? [], read, sink)
|
|
1221
|
+
];
|
|
1222
|
+
for (let frame = 0; frame <= frames; frame++) {
|
|
1223
|
+
const nowMs = frame * FRAME_MS;
|
|
1224
|
+
excite(frame, nowMs);
|
|
1225
|
+
for (const driver of drivers) driver.update(nowMs);
|
|
1226
|
+
}
|
|
1227
|
+
const readings = /* @__PURE__ */ new Map();
|
|
1228
|
+
for (const driver of drivers) {
|
|
1229
|
+
driver.drivenParameterIds.forEach((id, i) => {
|
|
1230
|
+
const param = parameters.find((p) => p.id === id);
|
|
1231
|
+
readings.set(id, readTrace(raw.get(id), param, driver.resets[i]));
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
return readings;
|
|
1235
|
+
}
|
|
1236
|
+
function readTrace(raw, param, resets) {
|
|
1237
|
+
const rest = clamp(param.default, param.min, param.max);
|
|
1238
|
+
const reading = {
|
|
1239
|
+
peak: 0,
|
|
1240
|
+
at: 0,
|
|
1241
|
+
final: rest,
|
|
1242
|
+
values: [],
|
|
1243
|
+
lo: rest,
|
|
1244
|
+
hi: rest,
|
|
1245
|
+
pinned: { frames: 0, end: "max" },
|
|
1246
|
+
resets
|
|
1247
|
+
};
|
|
1248
|
+
const seen = /* @__PURE__ */ new Set([rest]);
|
|
1249
|
+
let outside = 0;
|
|
1250
|
+
let end = "max";
|
|
1251
|
+
raw.forEach((value, frame) => {
|
|
1252
|
+
const held = clamp(value, param.min, param.max);
|
|
1253
|
+
if (Math.abs(held - rest) > Math.abs(reading.peak)) {
|
|
1254
|
+
reading.peak = held - rest;
|
|
1255
|
+
reading.at = frame * FIXED_DT_S;
|
|
1256
|
+
}
|
|
1257
|
+
if (!seen.has(held)) {
|
|
1258
|
+
seen.add(held);
|
|
1259
|
+
reading.values.push(held);
|
|
1260
|
+
}
|
|
1261
|
+
reading.lo = Math.min(reading.lo, held);
|
|
1262
|
+
reading.hi = Math.max(reading.hi, held);
|
|
1263
|
+
reading.final = held;
|
|
1264
|
+
if (value >= param.min && value <= param.max) {
|
|
1265
|
+
outside = 0;
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
const side = value < param.min ? "min" : "max";
|
|
1269
|
+
if (outside === 0 || side !== end) {
|
|
1270
|
+
outside = 0;
|
|
1271
|
+
end = side;
|
|
1272
|
+
}
|
|
1273
|
+
outside++;
|
|
1274
|
+
if (outside > reading.pinned.frames) {
|
|
1275
|
+
reading.pinned = { frames: outside, end };
|
|
1276
|
+
}
|
|
1277
|
+
});
|
|
1278
|
+
return reading;
|
|
1279
|
+
}
|
|
1280
|
+
function farEnd(param) {
|
|
1281
|
+
if (param.min === param.max) return void 0;
|
|
1282
|
+
const rest = clamp(param.default, param.min, param.max);
|
|
1283
|
+
return param.max - rest >= rest - param.min ? param.max : param.min;
|
|
1284
|
+
}
|
|
1285
|
+
function stepRun(model, rigs, chains, values) {
|
|
1286
|
+
const store = new ParameterStore(model.parameters);
|
|
1287
|
+
return run(model, store, rigs, chains, STEP_FRAMES, (frame) => {
|
|
1288
|
+
if (frame !== 1) return;
|
|
1289
|
+
for (const [id, v] of Object.entries(values)) store.set(id, v);
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
function idleRun(model) {
|
|
1293
|
+
const store = new ParameterStore(model.parameters);
|
|
1294
|
+
const idle = new IdleMotion((id, v) => store.set(id, v), {
|
|
1295
|
+
rng: lcg(IDLE_SEED)
|
|
1296
|
+
});
|
|
1297
|
+
return run(
|
|
1298
|
+
model,
|
|
1299
|
+
store,
|
|
1300
|
+
model.physics ?? [],
|
|
1301
|
+
model.physicsChains ?? [],
|
|
1302
|
+
IDLE_FRAMES,
|
|
1303
|
+
(_, nowMs) => idle.update(nowMs)
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
function lcg(seed) {
|
|
1307
|
+
let state = seed >>> 0;
|
|
1308
|
+
return () => {
|
|
1309
|
+
state = Math.imul(state, 1664525) + 1013904223 >>> 0;
|
|
1310
|
+
return state / 2 ** 32;
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
function anchorInputs(model, chain) {
|
|
1314
|
+
const store = new ParameterStore(model.parameters);
|
|
1315
|
+
const angle = () => {
|
|
1316
|
+
const { worlds } = resolveDeformers(model.deformers ?? [], store);
|
|
1317
|
+
const world = worlds.get(chain.anchorDeformer);
|
|
1318
|
+
return Math.atan2(world[1], world[0]);
|
|
1319
|
+
};
|
|
1320
|
+
const rest = angle();
|
|
1321
|
+
const found = [];
|
|
1322
|
+
for (const p of model.parameters) {
|
|
1323
|
+
const probes = new Set(
|
|
1324
|
+
[p.max, p.min, ...stopsOf(model, p.id)].map(
|
|
1325
|
+
(v) => clamp(v, p.min, p.max)
|
|
1326
|
+
)
|
|
1327
|
+
);
|
|
1328
|
+
probes.delete(store.get(p.id));
|
|
1329
|
+
let most = 0;
|
|
1330
|
+
let best;
|
|
1331
|
+
for (const value of probes) {
|
|
1332
|
+
store.set(p.id, value);
|
|
1333
|
+
const d = angle() - rest;
|
|
1334
|
+
const turn = Math.abs(Math.atan2(Math.sin(d), Math.cos(d)));
|
|
1335
|
+
if (turn <= most + MIN_TURN) continue;
|
|
1336
|
+
[most, best] = [turn, { parameter: p.id, value }];
|
|
1337
|
+
}
|
|
1338
|
+
store.reset();
|
|
1339
|
+
if (best) found.push(best);
|
|
1340
|
+
}
|
|
1341
|
+
return found;
|
|
1342
|
+
}
|
|
1343
|
+
function stopsOf(model, parameter) {
|
|
1344
|
+
const stops = /* @__PURE__ */ new Set();
|
|
1345
|
+
for (const d of model.deformers ?? []) {
|
|
1346
|
+
if (d.kind !== "warp") continue;
|
|
1347
|
+
for (const w of d.warps ?? []) {
|
|
1348
|
+
if (w.parameter !== parameter) continue;
|
|
1349
|
+
for (const k of w.keyforms) stops.add(k.value);
|
|
1350
|
+
}
|
|
1351
|
+
const { warp2d } = d;
|
|
1352
|
+
if (warp2d?.parameter === parameter) {
|
|
1353
|
+
for (const v of warp2d.valuesX) stops.add(v);
|
|
1354
|
+
}
|
|
1355
|
+
if (warp2d?.parameterY === parameter) {
|
|
1356
|
+
for (const v of warp2d.valuesY) stops.add(v);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
return [...stops].sort((a, b) => a - b);
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
// ../engine/src/physics-check.ts
|
|
1363
|
+
var DISPLACEMENT_PX = 1;
|
|
1364
|
+
var PINNED_FRAMES = 60;
|
|
1365
|
+
var TOLERANCE = 1e-9;
|
|
1366
|
+
var UNIT_QUAD = [-0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5];
|
|
1367
|
+
function checkPhysics(model) {
|
|
1368
|
+
const idle = idleRun(model);
|
|
1369
|
+
const displace = displacer(model);
|
|
1370
|
+
const idleExcitation = (output) => {
|
|
1371
|
+
const reading = idle.get(output.id);
|
|
1372
|
+
return {
|
|
1373
|
+
name: "the idle",
|
|
1374
|
+
reading,
|
|
1375
|
+
displacementPx: displace(output, reading.values)
|
|
1376
|
+
};
|
|
1377
|
+
};
|
|
1378
|
+
const param = (id) => model.parameters.find((p) => p.id === id);
|
|
1379
|
+
const springs = (model.physics ?? []).map((rig) => {
|
|
1380
|
+
const input = param(rig.input.parameter);
|
|
1381
|
+
const output = param(rig.output.parameter);
|
|
1382
|
+
const far = farEnd(input);
|
|
1383
|
+
const values = far === void 0 ? {} : { [input.id]: far };
|
|
1384
|
+
const reading = stepRun(model, [rig], [], values).get(output.id);
|
|
1385
|
+
const step = {
|
|
1386
|
+
name: "the step",
|
|
1387
|
+
reading,
|
|
1388
|
+
displacementPx: displace(output, reading.values),
|
|
1389
|
+
values
|
|
1390
|
+
};
|
|
1391
|
+
const atIdle = idleExcitation(output);
|
|
1392
|
+
return target(model, {
|
|
1393
|
+
rig: { kind: "spring", id: rig.id },
|
|
1394
|
+
input: [input.id],
|
|
1395
|
+
output,
|
|
1396
|
+
step,
|
|
1397
|
+
idle: atIdle,
|
|
1398
|
+
unstable: springWhy(rig) ?? unstableWhy(output, [step, atIdle]),
|
|
1399
|
+
inert: far === void 0 ? `${input.id} has no range` : void 0
|
|
1400
|
+
});
|
|
1401
|
+
});
|
|
1402
|
+
const chains = (model.physicsChains ?? []).flatMap((chain) => {
|
|
1403
|
+
const candidates = anchorInputs(model, chain);
|
|
1404
|
+
const settings = candidates.length === 0 ? [{}] : candidates.map((c) => ({ [c.parameter]: c.value }));
|
|
1405
|
+
const runs = settings.map((values) => ({
|
|
1406
|
+
values,
|
|
1407
|
+
readings: stepRun(model, [], [chain], values)
|
|
1408
|
+
}));
|
|
1409
|
+
return chain.segments.map((segment, i) => {
|
|
1410
|
+
const output = param(segment.output.parameter);
|
|
1411
|
+
const steps = runs.map(({ values, readings }) => {
|
|
1412
|
+
const reading = readings.get(output.id);
|
|
1413
|
+
const setting = Object.entries(values).map(([id, v]) => `${id}=${v}`);
|
|
1414
|
+
return {
|
|
1415
|
+
name: ["the step", ...setting].join(" "),
|
|
1416
|
+
reading,
|
|
1417
|
+
displacementPx: displace(output, reading.values),
|
|
1418
|
+
values
|
|
1419
|
+
};
|
|
1420
|
+
});
|
|
1421
|
+
const atIdle = idleExcitation(output);
|
|
1422
|
+
const clean = steps.filter((s) => !unstableWhy(output, [s]));
|
|
1423
|
+
return target(model, {
|
|
1424
|
+
rig: { kind: "chain", id: chain.id, segment: i },
|
|
1425
|
+
input: candidates.map((c) => c.parameter),
|
|
1426
|
+
output,
|
|
1427
|
+
step: mostMoving(clean.length > 0 ? clean : steps),
|
|
1428
|
+
idle: atIdle,
|
|
1429
|
+
unstable: unstableWhy(output, [...steps, atIdle]),
|
|
1430
|
+
inert: candidates.length === 0 ? `nothing turns anchor ${chain.anchorDeformer}` : void 0
|
|
1431
|
+
});
|
|
1432
|
+
});
|
|
1433
|
+
});
|
|
1434
|
+
const targets = [...springs, ...chains];
|
|
1435
|
+
return {
|
|
1436
|
+
thresholds: {
|
|
1437
|
+
displacementPx: DISPLACEMENT_PX,
|
|
1438
|
+
pinnedFrames: PINNED_FRAMES
|
|
1439
|
+
},
|
|
1440
|
+
targets,
|
|
1441
|
+
offenders: [
|
|
1442
|
+
...targets.filter((t) => t.kind === "unstable"),
|
|
1443
|
+
...targets.filter((t) => t.kind === "frozen")
|
|
1444
|
+
]
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
function target(model, t) {
|
|
1448
|
+
const { step, idle, output } = t;
|
|
1449
|
+
const drives = drivesOf(model, output.id);
|
|
1450
|
+
const px2 = Math.floor(step.displacementPx * 10) / 10;
|
|
1451
|
+
const still = step.displacementPx < DISPLACEMENT_PX ? `moves ${px2.toFixed(1)} model px over ${step.name}` : void 0;
|
|
1452
|
+
const frozen = drives.length === 0 ? "drives nothing" : t.inert ?? still;
|
|
1453
|
+
const { peak, at, final } = step.reading;
|
|
1454
|
+
return {
|
|
1455
|
+
rig: t.rig,
|
|
1456
|
+
input: t.input,
|
|
1457
|
+
output: output.id,
|
|
1458
|
+
drives,
|
|
1459
|
+
kind: t.unstable ? "unstable" : frozen ? "frozen" : "ok",
|
|
1460
|
+
why: t.unstable ?? frozen,
|
|
1461
|
+
step: {
|
|
1462
|
+
values: step.values,
|
|
1463
|
+
peak,
|
|
1464
|
+
at,
|
|
1465
|
+
final,
|
|
1466
|
+
displacementPx: step.displacementPx
|
|
1467
|
+
},
|
|
1468
|
+
idle: {
|
|
1469
|
+
peak: Math.abs(idle.reading.peak),
|
|
1470
|
+
at: idle.reading.at,
|
|
1471
|
+
displacementPx: idle.displacementPx
|
|
1472
|
+
}
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
function mostMoving(steps) {
|
|
1476
|
+
return steps.reduce((best, s) => {
|
|
1477
|
+
if (s.displacementPx !== best.displacementPx) {
|
|
1478
|
+
return s.displacementPx > best.displacementPx ? s : best;
|
|
1479
|
+
}
|
|
1480
|
+
return Math.abs(s.reading.peak) > Math.abs(best.reading.peak) ? s : best;
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
function drivesOf(model, output) {
|
|
1484
|
+
const parts = model.parts.filter(
|
|
1485
|
+
(p) => [
|
|
1486
|
+
...(p.bindings ?? []).map((b) => b.parameter),
|
|
1487
|
+
...(p.warps ?? []).map((w) => w.parameter)
|
|
1488
|
+
].includes(output)
|
|
1489
|
+
);
|
|
1490
|
+
const deformers = (model.deformers ?? []).filter(
|
|
1491
|
+
(d) => (d.kind === "warp" ? [
|
|
1492
|
+
...(d.warps ?? []).map((w) => w.parameter),
|
|
1493
|
+
d.warp2d?.parameter,
|
|
1494
|
+
d.warp2d?.parameterY
|
|
1495
|
+
] : (d.bindings ?? []).map((b) => b.parameter)).includes(output)
|
|
1496
|
+
);
|
|
1497
|
+
return [
|
|
1498
|
+
...parts.map((p) => ({ kind: "part", id: p.id })),
|
|
1499
|
+
...deformers.map((d) => ({ kind: "deformer", id: d.id }))
|
|
1500
|
+
];
|
|
1501
|
+
}
|
|
1502
|
+
function springStepRadius({ mass, stiffness, damping }) {
|
|
1503
|
+
const k = FIXED_DT_S * FIXED_DT_S * stiffness / mass;
|
|
1504
|
+
const c = FIXED_DT_S * damping / mass;
|
|
1505
|
+
const trace = 2 - k - c;
|
|
1506
|
+
const det = 1 - c;
|
|
1507
|
+
const disc = trace * trace - 4 * det;
|
|
1508
|
+
const radius = disc >= 0 ? (Math.abs(trace) + Math.sqrt(disc)) / 2 : Math.sqrt(det);
|
|
1509
|
+
return { radius, k };
|
|
1510
|
+
}
|
|
1511
|
+
function springWhy(rig) {
|
|
1512
|
+
const { radius, k } = springStepRadius(rig);
|
|
1513
|
+
if (radius > 1 + TOLERANCE) {
|
|
1514
|
+
let digits = 2;
|
|
1515
|
+
while (Number(radius.toFixed(digits)) <= 1) digits++;
|
|
1516
|
+
return `spring step map radius ${radius.toFixed(digits)} over 1 at 1/60 s`;
|
|
1517
|
+
}
|
|
1518
|
+
if (rig.damping === 0 && k >= 4 - TOLERANCE) {
|
|
1519
|
+
return `spring on its undamped boundary, dt\xB2k/m ${k.toFixed(2)}`;
|
|
1520
|
+
}
|
|
1521
|
+
return void 0;
|
|
1522
|
+
}
|
|
1523
|
+
function unstableWhy(output, excitations) {
|
|
1524
|
+
for (const { name, reading } of excitations) {
|
|
1525
|
+
const n = reading.resets;
|
|
1526
|
+
if (n === 0) continue;
|
|
1527
|
+
return `finiteness guard reset it ${n} time${n === 1 ? "" : "s"} in ${name}`;
|
|
1528
|
+
}
|
|
1529
|
+
for (const { name, reading } of excitations) {
|
|
1530
|
+
const { frames, end } = reading.pinned;
|
|
1531
|
+
if (frames < PINNED_FRAMES) continue;
|
|
1532
|
+
return `pinned at ${output.id} ${end} ${output[end]} for ${frames} frames of ${name}`;
|
|
1533
|
+
}
|
|
1534
|
+
return void 0;
|
|
1535
|
+
}
|
|
1536
|
+
function displacer(model) {
|
|
1537
|
+
const deformers = model.deformers ?? [];
|
|
1538
|
+
const store = new ParameterStore(model.parameters);
|
|
1539
|
+
const { worlds } = resolveDeformers(staticDeformers(model), store);
|
|
1540
|
+
const landings = model.parts.map(
|
|
1541
|
+
(part) => landedPart(
|
|
1542
|
+
part,
|
|
1543
|
+
part.mesh?.vertices ?? UNIT_QUAD,
|
|
1544
|
+
deformers,
|
|
1545
|
+
worlds,
|
|
1546
|
+
store
|
|
1547
|
+
)
|
|
1548
|
+
);
|
|
1549
|
+
const atRest = resolveDeformers(deformers, store);
|
|
1550
|
+
const baselines = landings.map(
|
|
1551
|
+
(l) => Float32Array.from(landPart(l, store, atRest))
|
|
1552
|
+
);
|
|
1553
|
+
return (output, values) => {
|
|
1554
|
+
let most = 0;
|
|
1555
|
+
const affected = affectedParts(model, output.id);
|
|
1556
|
+
for (const value of values) {
|
|
1557
|
+
store.set(output.id, value);
|
|
1558
|
+
const resolved = resolveDeformers(deformers, store);
|
|
1559
|
+
landings.forEach((landing, i) => {
|
|
1560
|
+
if (!affected.has(landing.part)) return;
|
|
1561
|
+
const landed = landPart(landing, store, resolved);
|
|
1562
|
+
const base = baselines[i];
|
|
1563
|
+
for (let j = 0; j < landed.length; j += 2) {
|
|
1564
|
+
const dx = landed[j] - base[j];
|
|
1565
|
+
const dy = landed[j + 1] - base[j + 1];
|
|
1566
|
+
most = Math.max(most, dx * dx + dy * dy);
|
|
1567
|
+
}
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
store.reset();
|
|
1571
|
+
return Math.sqrt(most);
|
|
1572
|
+
};
|
|
1573
|
+
}
|
|
1574
|
+
function affectedParts(model, output) {
|
|
1575
|
+
const deformers = new Map((model.deformers ?? []).map((d) => [d.id, d]));
|
|
1576
|
+
const reads = (d) => (d.kind === "warp" ? [
|
|
1577
|
+
...(d.warps ?? []).map((w) => w.parameter),
|
|
1578
|
+
d.warp2d?.parameter,
|
|
1579
|
+
d.warp2d?.parameterY
|
|
1580
|
+
] : (d.bindings ?? []).map((b) => b.parameter)).includes(output);
|
|
1581
|
+
const hung = (id) => {
|
|
1582
|
+
for (let d = id && deformers.get(id); d; d = d.parent && deformers.get(d.parent)) {
|
|
1583
|
+
if (reads(d)) return true;
|
|
1584
|
+
}
|
|
1585
|
+
return false;
|
|
1586
|
+
};
|
|
1587
|
+
return new Set(
|
|
1588
|
+
model.parts.filter(
|
|
1589
|
+
(p) => [
|
|
1590
|
+
...(p.bindings ?? []).map((b) => b.parameter),
|
|
1591
|
+
...(p.warps ?? []).map((w) => w.parameter)
|
|
1592
|
+
].includes(output) || hung(p.deformer)
|
|
1593
|
+
)
|
|
1594
|
+
);
|
|
1595
|
+
}
|
|
1596
|
+
|
|
733
1597
|
// src/fold-report.ts
|
|
734
1598
|
function checkModelFolds(model, extraValues = []) {
|
|
735
1599
|
const declared = new Set(model.parameters.map((p) => p.id));
|
|
@@ -738,9 +1602,9 @@ function checkModelFolds(model, extraValues = []) {
|
|
|
738
1602
|
// The effective value, not the pose's overrides: the auto-rig's mouth
|
|
739
1603
|
// rests shut at its default, so `rest` and the other parameters' singles
|
|
740
1604
|
// are shut-mouth poses too.
|
|
741
|
-
expected: (
|
|
742
|
-
if (
|
|
743
|
-
const shutBy = SHUT_BY.get(
|
|
1605
|
+
expected: (target2, _pose, value) => {
|
|
1606
|
+
if (target2.kind !== "part") return false;
|
|
1607
|
+
const shutBy = SHUT_BY.get(target2.id);
|
|
744
1608
|
return shutBy !== void 0 && declared.has(shutBy) && value(shutBy) === 0;
|
|
745
1609
|
}
|
|
746
1610
|
});
|
|
@@ -1446,6 +2310,7 @@ async function renderPoses(input) {
|
|
|
1446
2310
|
model,
|
|
1447
2311
|
input.poses?.map((p) => p.values)
|
|
1448
2312
|
);
|
|
2313
|
+
const physics = checkPhysics(model);
|
|
1449
2314
|
const { failedTextures, poses } = await withBrowser(
|
|
1450
2315
|
browser,
|
|
1451
2316
|
async (evaluate) => {
|
|
@@ -1482,7 +2347,8 @@ async function renderPoses(input) {
|
|
|
1482
2347
|
failedTextures,
|
|
1483
2348
|
poses,
|
|
1484
2349
|
modelCanvas: model.canvas,
|
|
1485
|
-
folds
|
|
2350
|
+
folds,
|
|
2351
|
+
physics
|
|
1486
2352
|
};
|
|
1487
2353
|
} catch (error) {
|
|
1488
2354
|
if (error instanceof AutoRigInputError || error instanceof IkiFormatError) {
|
|
@@ -2015,6 +2881,7 @@ async function autoRigFromLayers(input) {
|
|
|
2015
2881
|
const finalModel = parseIkiModel(patched);
|
|
2016
2882
|
writeFileAtomic(outPath, JSON.stringify(finalModel));
|
|
2017
2883
|
const folds = checkModelFolds(finalModel);
|
|
2884
|
+
const physics = checkPhysics(finalModel);
|
|
2018
2885
|
return {
|
|
2019
2886
|
ok: true,
|
|
2020
2887
|
path: outPath,
|
|
@@ -2028,6 +2895,7 @@ async function autoRigFromLayers(input) {
|
|
|
2028
2895
|
...noseCore === void 0 ? {} : { noseCore },
|
|
2029
2896
|
...turn === void 0 ? {} : { turn },
|
|
2030
2897
|
folds,
|
|
2898
|
+
physics,
|
|
2031
2899
|
...patches === void 0 ? {} : { patches }
|
|
2032
2900
|
};
|
|
2033
2901
|
} catch (err) {
|
|
@@ -3477,16 +4345,16 @@ async function layerStats(filePath) {
|
|
|
3477
4345
|
let flatCutRun = 0;
|
|
3478
4346
|
let flatCutY = -1;
|
|
3479
4347
|
for (let y = minY + 1; y <= maxY; y++) {
|
|
3480
|
-
let
|
|
4348
|
+
let run2 = 0;
|
|
3481
4349
|
for (let x = minX; x <= maxX; x++) {
|
|
3482
4350
|
if (alpha(x, y - 1) < FLAT_CUT_ALPHA_ABOVE && alpha(x, y) > FLAT_CUT_ALPHA_BELOW) {
|
|
3483
|
-
|
|
3484
|
-
if (
|
|
3485
|
-
flatCutRun =
|
|
4351
|
+
run2++;
|
|
4352
|
+
if (run2 > flatCutRun) {
|
|
4353
|
+
flatCutRun = run2;
|
|
3486
4354
|
flatCutY = y;
|
|
3487
4355
|
}
|
|
3488
4356
|
} else {
|
|
3489
|
-
|
|
4357
|
+
run2 = 0;
|
|
3490
4358
|
}
|
|
3491
4359
|
}
|
|
3492
4360
|
}
|
|
@@ -3832,6 +4700,7 @@ var IRIS_W = 50;
|
|
|
3832
4700
|
var EYEWHITE_SRC = "eyewhite.png";
|
|
3833
4701
|
var EYE_LASH_LUMA = 120;
|
|
3834
4702
|
var LASH_KEEP_FRACTION = 0.5;
|
|
4703
|
+
var EYE_LID_WARMTH = 30;
|
|
3835
4704
|
var NOSE_TIP_AT = 0.66;
|
|
3836
4705
|
var DEFAULT_LAYOUT = {
|
|
3837
4706
|
// Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
|
|
@@ -4315,6 +5184,36 @@ async function prepEyeSplit(partsDir) {
|
|
|
4315
5184
|
}
|
|
4316
5185
|
const lashCutoffY = minY + LASH_KEEP_FRACTION * (maxY - minY + 1);
|
|
4317
5186
|
const detached = detachedAboveWhite(data, W, H);
|
|
5187
|
+
const isDark = (i) => data[i + 3] > 0 && luma(data, i) < EYE_LASH_LUMA;
|
|
5188
|
+
const isLight = (i) => data[i + 3] >= ALPHA_OPAQUE5 && luma(data, i) >= EYE_LASH_LUMA;
|
|
5189
|
+
const isWarm = (i) => data[i] - data[i + 2] >= EYE_LID_WARMTH;
|
|
5190
|
+
const whiteTop = new Int32Array(W).fill(-1);
|
|
5191
|
+
for (let x = 0; x < W; x++) {
|
|
5192
|
+
for (let y = 0; y < H; y++) {
|
|
5193
|
+
const i = (y * W + x) * 4;
|
|
5194
|
+
if (!detached[y * W + x] && isLight(i) && !isWarm(i)) {
|
|
5195
|
+
whiteTop[x] = y;
|
|
5196
|
+
break;
|
|
5197
|
+
}
|
|
5198
|
+
}
|
|
5199
|
+
}
|
|
5200
|
+
let first = 0;
|
|
5201
|
+
while (first < W && whiteTop[first] === -1) first++;
|
|
5202
|
+
if (first < W) {
|
|
5203
|
+
let last = W - 1;
|
|
5204
|
+
while (whiteTop[last] === -1) last--;
|
|
5205
|
+
whiteTop.fill(whiteTop[first], 0, first);
|
|
5206
|
+
whiteTop.fill(whiteTop[last], last + 1);
|
|
5207
|
+
}
|
|
5208
|
+
const lidStop = new Int32Array(W).fill(-1);
|
|
5209
|
+
for (let x = 0; x < W; x++) {
|
|
5210
|
+
for (let y = whiteTop[x] - 1; y >= 0; y--) {
|
|
5211
|
+
if (isDark((y * W + x) * 4)) {
|
|
5212
|
+
lidStop[x] = y;
|
|
5213
|
+
break;
|
|
5214
|
+
}
|
|
5215
|
+
}
|
|
5216
|
+
}
|
|
4318
5217
|
const sclera = Buffer.from(data);
|
|
4319
5218
|
const lash = Buffer.from(data);
|
|
4320
5219
|
const lashLower = Buffer.from(data);
|
|
@@ -4322,12 +5221,13 @@ async function prepEyeSplit(partsDir) {
|
|
|
4322
5221
|
for (let y = 0; y < H; y++) {
|
|
4323
5222
|
for (let x = 0; x < W; x++) {
|
|
4324
5223
|
const i = (y * W + x) * 4;
|
|
4325
|
-
const
|
|
5224
|
+
const dark = isDark(i);
|
|
4326
5225
|
const upper = y <= lashCutoffY;
|
|
4327
|
-
if (
|
|
5226
|
+
if (dark && upper) sclera[i] = sclera[i + 1] = sclera[i + 2] = 255;
|
|
4328
5227
|
if (detached[y * W + x]) sclera[i + 3] = 0;
|
|
4329
|
-
if (
|
|
4330
|
-
if (
|
|
5228
|
+
if (y < lidStop[x] && isLight(i) && isWarm(i)) sclera[i + 3] = 0;
|
|
5229
|
+
if (!(dark && upper)) lash[i + 3] = 0;
|
|
5230
|
+
if (dark && !upper) lowerInk = true;
|
|
4331
5231
|
else lashLower[i + 3] = 0;
|
|
4332
5232
|
}
|
|
4333
5233
|
}
|
|
@@ -4717,6 +5617,22 @@ async function composeLayersFromParts(input) {
|
|
|
4717
5617
|
}
|
|
4718
5618
|
}
|
|
4719
5619
|
|
|
5620
|
+
// src/physics-report.ts
|
|
5621
|
+
function formatPhysicsReport(report) {
|
|
5622
|
+
if (report.targets.length === 0) return ["PHYSICS: no rigs"];
|
|
5623
|
+
return report.targets.map(targetLine);
|
|
5624
|
+
}
|
|
5625
|
+
function targetLine(t) {
|
|
5626
|
+
const inputs = t.input.join(", ");
|
|
5627
|
+
const rig = t.rig.kind === "chain" ? `${t.rig.id}/${t.rig.segment} [${inputs}]` : `${t.rig.id} ${inputs}`;
|
|
5628
|
+
const drives = t.drives.length === 0 ? "" : ` (${t.drives.map((d) => d.id).join(", ")})`;
|
|
5629
|
+
const { step, idle } = t;
|
|
5630
|
+
return `PHYSICS ${t.kind} ${rig} -> ${t.output}: step ${foldPose(step.values).name} peak ${step.peak.toFixed(2)} at ${step.at.toFixed(2)} s, final ${step.final.toFixed(2)}, moves ${pxText(step.displacementPx)} model px${drives}; idle peak ${idle.peak.toFixed(2)} at ${idle.at.toFixed(2)} s, ${pxText(idle.displacementPx)} model px` + (t.why === void 0 ? "" : ` \u2014 ${t.why}`);
|
|
5631
|
+
}
|
|
5632
|
+
function pxText(px2) {
|
|
5633
|
+
return (Math.floor(px2 * 10) / 10).toFixed(1);
|
|
5634
|
+
}
|
|
5635
|
+
|
|
4720
5636
|
// src/server.ts
|
|
4721
5637
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4722
5638
|
import { z } from "zod";
|
|
@@ -4728,7 +5644,7 @@ function formatPatchReports(reports) {
|
|
|
4728
5644
|
});
|
|
4729
5645
|
}
|
|
4730
5646
|
function createIkiMcpServer() {
|
|
4731
|
-
const server = new McpServer({ name: "iki", version: "0.
|
|
5647
|
+
const server = new McpServer({ name: "iki", version: "0.27.0" });
|
|
4732
5648
|
server.registerTool(
|
|
4733
5649
|
"validate_iki",
|
|
4734
5650
|
{
|
|
@@ -4803,7 +5719,7 @@ function createIkiMcpServer() {
|
|
|
4803
5719
|
server.registerTool(
|
|
4804
5720
|
"auto_rig_from_layers",
|
|
4805
5721
|
{
|
|
4806
|
-
description: "Auto-rigs role-named PNG layers (face, eye_L/eye_R and either mouth or the lip set (mouth_inner + lip_lower + lip_upper, a mouth that folds open instead of crossfading) required; iris_L/R, brow_L/R, hair_front, body, arm_L/arm_R, forearm_pose_L/forearm_pose_R, mouth_tongue/mouth_teeth (only with the lip set: drawn under its lips and clipped to mouth_inner), etc. optional; an arm needs the body, a pose forearm needs its arm, and a partial lip set, one mixed with mouth/mouth_open, or mouth_tongue/mouth_teeth without it is refused) into a renderable .iki written to disk. Pass full-canvas PNG file paths; returns the output path + summary (the model is NOT inlined). Filenames map to roles unless `fileName` is given. The result's `folds` is the written rig's fold report, printed after the path the way render_poses prints it, and report-only: a fold never refuses the rig. `patches` (see its field) adds displacements to existing keyform stops of the freshly generated rig; each op then prints one line between the path and the fold report \u2014 `PATCH <i> <target> at <Param=v \u2026>: <moved> of <of> points (cols a-b, rows c-d), largest move <maxPx> model px`, with `(vertices)` in place of the columns and rows for a mesh part \u2014 and the result's `patches` carries the same reports (`index`, `target`, `at`, `moved`, `of`, `cols`/`rows` for a grid, `maxPx`), present whenever `patches` was passed, `[]` included. An op the input schema accepts but the rig refuses comes back as `INVALID: patches[i]\u2026` and nothing is written; an op with a missing field or a field of the wrong type is the SDK's input validation error.",
|
|
5722
|
+
description: "Auto-rigs role-named PNG layers (face, eye_L/eye_R and either mouth or the lip set (mouth_inner + lip_lower + lip_upper, a mouth that folds open instead of crossfading) required; iris_L/R, brow_L/R, hair_front, body, arm_L/arm_R, forearm_pose_L/forearm_pose_R, mouth_tongue/mouth_teeth (only with the lip set: drawn under its lips and clipped to mouth_inner), etc. optional; an arm needs the body, a pose forearm needs its arm, and a partial lip set, one mixed with mouth/mouth_open, or mouth_tongue/mouth_teeth without it is refused) into a renderable .iki written to disk. Pass full-canvas PNG file paths; returns the output path + summary (the model is NOT inlined). Filenames map to roles unless `fileName` is given. The result's `folds` is the written rig's fold report, printed after the path the way render_poses prints it, and report-only: a fold never refuses the rig. The `PHYSICS` lines follow the fold report (the result's `physics`), one per physics rig output \u2014 a spring's, or each chain segment's \u2014 reading ok, frozen or unstable with the step and idle amplitudes, or `PHYSICS: no rigs`; report-only too. `patches` (see its field) adds displacements to existing keyform stops of the freshly generated rig; each op then prints one line between the path and the fold report \u2014 `PATCH <i> <target> at <Param=v \u2026>: <moved> of <of> points (cols a-b, rows c-d), largest move <maxPx> model px`, with `(vertices)` in place of the columns and rows for a mesh part \u2014 and the result's `patches` carries the same reports (`index`, `target`, `at`, `moved`, `of`, `cols`/`rows` for a grid, `maxPx`), present whenever `patches` was passed, `[]` included. An op the input schema accepts but the rig refuses comes back as `INVALID: patches[i]\u2026` and nothing is written; an op with a missing field or a field of the wrong type is the SDK's input validation error.",
|
|
4807
5723
|
inputSchema: {
|
|
4808
5724
|
layers: z.array(
|
|
4809
5725
|
z.object({
|
|
@@ -4869,7 +5785,8 @@ function createIkiMcpServer() {
|
|
|
4869
5785
|
const text = r.ok ? [
|
|
4870
5786
|
r.path,
|
|
4871
5787
|
...formatPatchReports(r.patches ?? []),
|
|
4872
|
-
...formatFoldReport(r.folds, r.canvas)
|
|
5788
|
+
...formatFoldReport(r.folds, r.canvas),
|
|
5789
|
+
...formatPhysicsReport(r.physics)
|
|
4873
5790
|
].join("\n") : `INVALID: ${r.error}`;
|
|
4874
5791
|
return { content: [{ type: "text", text }], structuredContent: r };
|
|
4875
5792
|
} catch (err) {
|
|
@@ -4998,7 +5915,7 @@ function createIkiMcpServer() {
|
|
|
4998
5915
|
server.registerTool(
|
|
4999
5916
|
"render_poses",
|
|
5000
5917
|
{
|
|
5001
|
-
description: "Renders a rigged .iki into the character loop's pose PNGs with the real engine, through the installed Chrome, Edge or Chromium run headless (set CHROME_PATH to pick one). The model decides the set: a bust gets 560x560 shots (rest, turns, nod, blinks, gaze, combined \u2014 ParamAngleX/Y/Z all at 30 \u2014 and stress: ParamAngleX/Y/Z all at -30, ParamMouthOpenY 1, both eyes at 0.5, the brows at opposite extremes (ParamBrowLY -1, ParamBrowRY 1, ParamBrowLAngle 1, ParamBrowRAngle -1) and each body angle the model declares at -10); a mouth adds 720x480 mouth crops; a full body (taller than wide) adds the full-canvas body, arm and elbow poses on a 1400-wide canvas, and a model with a pose forearm adds the full-pose-* shots. Given `poses` (see its field), the standard set is not rendered: only those poses are, each crop clamped to the model's canvas. Writes <pose>.png files into outDir, overwriting same-named files in place; returns the written paths one per line, then the model's fold report (the result's `folds`), checked by the engine over its own pose set plus any `poses`, each of which a `FOLD` line names by its settings, not its PNG name: a `FOLD` line per warp deformer or mesh part that flips (ratio <= 0) or collapses (under 0.1) at some pose \u2014 its cell or triangle, the ratio of its area there to at rest, the pose as its parameter settings (`rest` for none) and where it rests, in model space and in model px from the top-left (the layer PNGs' own pixels, not a render's) \u2014 or `FOLDS: none over N poses`, then a `FOLDS expected:` line naming the closures the rig draws on purpose (an eye or the mouth shut), each by part id, ratio and the parameter that shuts it. Report-only: a fold never fails the render. Fails with INVALID when no browser is found or an input is refused; a browser that cannot start is a tool error.",
|
|
5918
|
+
description: "Renders a rigged .iki into the character loop's pose PNGs with the real engine, through the installed Chrome, Edge or Chromium run headless (set CHROME_PATH to pick one). The model decides the set: a bust gets 560x560 shots (rest, turns, nod, blinks, gaze, combined \u2014 ParamAngleX/Y/Z all at 30 \u2014 and stress: ParamAngleX/Y/Z all at -30, ParamMouthOpenY 1, both eyes at 0.5, the brows at opposite extremes (ParamBrowLY -1, ParamBrowRY 1, ParamBrowLAngle 1, ParamBrowRAngle -1) and each body angle the model declares at -10); a mouth adds 720x480 mouth crops; a full body (taller than wide) adds the full-canvas body, arm and elbow poses on a 1400-wide canvas, and a model with a pose forearm adds the full-pose-* shots. Given `poses` (see its field), the standard set is not rendered: only those poses are, each crop clamped to the model's canvas. Writes <pose>.png files into outDir, overwriting same-named files in place; returns the written paths one per line, then the model's fold report (the result's `folds`), checked by the engine over its own pose set plus any `poses`, each of which a `FOLD` line names by its settings, not its PNG name: a `FOLD` line per warp deformer or mesh part that flips (ratio <= 0) or collapses (under 0.1) at some pose \u2014 its cell or triangle, the ratio of its area there to at rest, the pose as its parameter settings (`rest` for none) and where it rests, in model space and in model px from the top-left (the layer PNGs' own pixels, not a render's) \u2014 or `FOLDS: none over N poses`, then a `FOLDS expected:` line naming the closures the rig draws on purpose (an eye or the mouth shut), each by part id, ratio and the parameter that shuts it. Report-only: a fold never fails the render. The `PHYSICS` lines follow the fold report (the result's `physics`), one per physics rig output of the model's own rigs \u2014 `poses` do not enter it \u2014 reading ok, frozen or unstable with the step and idle amplitudes, or `PHYSICS: no rigs`; report-only too. Fails with INVALID when no browser is found or an input is refused; a browser that cannot start is a tool error.",
|
|
5002
5919
|
inputSchema: {
|
|
5003
5920
|
modelPath: z.string().describe(
|
|
5004
5921
|
"Rigged .iki file path (resolved against cwd; must sit under the working directory)."
|
|
@@ -5027,7 +5944,8 @@ function createIkiMcpServer() {
|
|
|
5027
5944
|
const r = await renderPoses(args);
|
|
5028
5945
|
const text = r.ok ? [
|
|
5029
5946
|
...r.poses.map((p) => p.path),
|
|
5030
|
-
...formatFoldReport(r.folds, r.modelCanvas)
|
|
5947
|
+
...formatFoldReport(r.folds, r.modelCanvas),
|
|
5948
|
+
...formatPhysicsReport(r.physics)
|
|
5031
5949
|
].join("\n") : `INVALID: ${r.error}`;
|
|
5032
5950
|
return { content: [{ type: "text", text }], structuredContent: r };
|
|
5033
5951
|
} catch (err) {
|
|
@@ -5057,6 +5975,7 @@ export {
|
|
|
5057
5975
|
measureLayers,
|
|
5058
5976
|
formatMeasureReport,
|
|
5059
5977
|
composeLayersFromParts,
|
|
5978
|
+
formatPhysicsReport,
|
|
5060
5979
|
createIkiMcpServer
|
|
5061
5980
|
};
|
|
5062
|
-
//# sourceMappingURL=chunk-
|
|
5981
|
+
//# sourceMappingURL=chunk-7POOGY7U.mjs.map
|