@ikijs/editor 0.10.1 → 0.11.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/dist/index.js CHANGED
@@ -1632,7 +1632,6 @@ function createLayerSetMeasurer(canvas) {
1632
1632
  const opaque = new Uint8Array(canvasW * canvasH);
1633
1633
  const layerInputs = [];
1634
1634
  const rowSpansByRole = [];
1635
- let hairFrontMask;
1636
1635
  function add({ role, fileName, rgba }) {
1637
1636
  if (rgba.length !== rgbaLength) {
1638
1637
  throw new Error(
@@ -1643,19 +1642,29 @@ function createLayerSetMeasurer(canvas) {
1643
1642
  if (bbox === null) return null;
1644
1643
  const rowLeft = new Int32Array(canvasH).fill(canvasW);
1645
1644
  const rowRight = new Int32Array(canvasH).fill(-1);
1646
- const ownMask = role === "hair_front" ? new Uint8Array(canvasW * canvasH) : void 0;
1645
+ const rowRuns = role === "hair_front" || role === "hair_back" ? [] : void 0;
1647
1646
  for (let y = 0; y < canvasH; y++) {
1647
+ let runs;
1648
+ if (rowRuns !== void 0 && y >= bbox.y && y < bbox.y + bbox.h) {
1649
+ runs = [];
1650
+ rowRuns.push(runs);
1651
+ }
1652
+ let inRun = false;
1648
1653
  for (let x = 0; x < canvasW; x++) {
1649
1654
  const p = y * canvasW + x;
1650
- if (rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
1655
+ const on = rgba[p * 4 + 3] >= ALPHA_OPAQUE;
1656
+ if (runs !== void 0 && on !== inRun) {
1657
+ runs.push(x);
1658
+ inRun = on;
1659
+ }
1660
+ if (!on) continue;
1651
1661
  opaque[p] = 1;
1652
- if (ownMask !== void 0) ownMask[p] = 1;
1653
1662
  if (x < rowLeft[y]) rowLeft[y] = x;
1654
1663
  if (x > rowRight[y]) rowRight[y] = x;
1655
1664
  }
1665
+ if (runs !== void 0 && inRun) runs.push(canvasW);
1656
1666
  }
1657
1667
  rowSpansByRole.push({ role, rowLeft, rowRight });
1658
- if (ownMask !== void 0) hairFrontMask = ownMask;
1659
1668
  const layer = {
1660
1669
  role,
1661
1670
  fileName,
@@ -1665,6 +1674,7 @@ function createLayerSetMeasurer(canvas) {
1665
1674
  cropW: bbox.w,
1666
1675
  cropH: bbox.h
1667
1676
  };
1677
+ if (rowRuns !== void 0) layer.rowRuns = rowRuns;
1668
1678
  if (role === "face") {
1669
1679
  const rowHalfWidths = [];
1670
1680
  for (let y = bbox.y; y < bbox.y + bbox.h; y++) {
@@ -1709,8 +1719,10 @@ function createLayerSetMeasurer(canvas) {
1709
1719
  }
1710
1720
  let strandEdges;
1711
1721
  const irisLayers = layerInputs.filter((l) => l.role === "iris_L" || l.role === "iris_R").sort((a, b) => a.bbox.x + a.bbox.w / 2 - (b.bbox.x + b.bbox.w / 2));
1712
- if (hairFrontMask !== void 0 && irisLayers.length === 2) {
1713
- const mask = hairFrontMask;
1722
+ const hairFront = layerInputs.find((l) => l.role === "hair_front");
1723
+ const hairRuns = hairFront?.rowRuns;
1724
+ if (hairFront !== void 0 && hairRuns !== void 0 && irisLayers.length === 2) {
1725
+ const hairTop = hairFront.bbox.y;
1714
1726
  const half = canvasW / 2;
1715
1727
  const strandOn = (iris, other, side) => {
1716
1728
  const r = Math.floor(iris.bbox.y + iris.bbox.h / 2);
@@ -1721,15 +1733,11 @@ function createLayerSetMeasurer(canvas) {
1721
1733
  if (span2.rowRight[r] < span2.rowLeft[r]) return void 0;
1722
1734
  const minRun = STRAND_MIN_RUN_FRACTION * (span2.rowRight[r] + 1 - span2.rowLeft[r]);
1723
1735
  const strand = new Uint8Array(canvasW);
1724
- for (let a = 0; a < canvasW; ) {
1725
- if (mask[r * canvasW + a] !== 1) {
1726
- a++;
1727
- continue;
1736
+ const runs = r >= hairTop && r < hairTop + hairRuns.length ? hairRuns[r - hairTop] : [];
1737
+ for (let k = 0; k < runs.length; k += 2) {
1738
+ if (runs[k + 1] - runs[k] >= minRun) {
1739
+ strand.fill(1, runs[k], runs[k + 1]);
1728
1740
  }
1729
- let b = a;
1730
- while (b < canvasW && mask[r * canvasW + b] === 1) b++;
1731
- if (b - a >= minRun) strand.fill(1, a, b);
1732
- a = b;
1733
1741
  }
1734
1742
  const strandAt = (x2) => x2 >= 0 && x2 < canvasW && strand[x2] === 1;
1735
1743
  const outerBoundary = (x2) => (side < 0 ? x2 : x2 + 1) - half;
@@ -2171,6 +2179,23 @@ function checkLayers(layers, canvas) {
2171
2179
  }
2172
2180
  });
2173
2181
  }
2182
+ if (l.rowRuns !== void 0) {
2183
+ const rows = l.rowRuns;
2184
+ if (!Array.isArray(rows) || rows.length !== h) {
2185
+ throw new Error(
2186
+ `${at}: rowRuns must have one entry per crop row (${h})`
2187
+ );
2188
+ }
2189
+ rows.forEach((runs, i) => {
2190
+ if (!Array.isArray(runs) || runs.length % 2 !== 0 || !runs.every(
2191
+ (c, k) => Number.isInteger(c) && c >= x && c <= x + w && (k === 0 || c > runs[k - 1])
2192
+ )) {
2193
+ throw new Error(
2194
+ `${at}: rowRuns[${i}] ${JSON.stringify(runs)} is not an even-length, strictly increasing list of columns inside its crop (${x}..${x + w})`
2195
+ );
2196
+ }
2197
+ });
2198
+ }
2174
2199
  if (l.jawRows !== void 0) {
2175
2200
  const cols = l.jawRows;
2176
2201
  if (!Array.isArray(cols) || cols.length !== w) {
@@ -2428,7 +2453,11 @@ var TURN = {
2428
2453
  * its translation (bangs 1.18×, side locks at the eye row ≈ 1.0×). */
2429
2454
  hairFollow: 1.1,
2430
2455
  /** The head's outline at the eye row holds (−0.014 / −0.004 hh): where the
2431
- * front hair draws it, its outer edge follows the face this much. */
2456
+ * front hair draws it, its outer edge follows the face this much on a row
2457
+ * where no back hair paints behind that edge. Where back hair does, the
2458
+ * edge rides further, as far as that back hair stays behind it through
2459
+ * the turn and the nod, up to the face's follow there (`hairFollow`;
2460
+ * over the far eye's rows, as far as that eye's outer corner goes). */
2432
2461
  outlineFollow: 0,
2433
2462
  /** The back hair's slight counter-motion. */
2434
2463
  hairBack: -0.027,
@@ -2448,6 +2477,13 @@ var NOD = {
2448
2477
  nose: { up: -0.207, down: 0.193 },
2449
2478
  mouth: { up: -0.172, down: 0.177 },
2450
2479
  hairFront: { up: -0.11, down: 0.189 },
2480
+ /** The front hair's cap top. The samples' cap top moves 1.215× their face's
2481
+ * centroid looking down (Mao 1.236, Haru 1.626, Hiyori 1.194, Natori
2482
+ * 0.730) and 0.415× looking up (0.385, 0.430, 0.400, 0.487); each ratio
2483
+ * times the plate's own nod halfway between its top and the chin (0.1435
2484
+ * down, 0.084 up). Looking down, the rig slides it only as far as the
2485
+ * back hair covers the crown behind it. */
2486
+ hairFrontTop: { up: -0.035, down: 0.174 },
2451
2487
  hairBack: { up: -0.011, down: 0.025 }
2452
2488
  };
2453
2489
  var ROLL_DEG = 10;
@@ -2495,433 +2531,82 @@ function resolveStyle(style, check) {
2495
2531
  };
2496
2532
  }
2497
2533
 
2498
- // src/auto-rig/fields.ts
2499
- var NOSE_TILT_DEG = 6;
2500
- function nodDy(v, ay, hh) {
2501
- const up = Math.max(0, ay) / 30;
2502
- const down = Math.max(0, -ay) / 30;
2503
- return -(up * v.up + down * v.down) * hh;
2504
- }
2505
- function spin(x, y, ox, oy, a) {
2506
- if (a === 0) return [0, 0];
2507
- const px = x - ox;
2508
- const py = y - oy;
2509
- return [
2510
- px * Math.cos(a) - py * Math.sin(a) - px,
2511
- px * Math.sin(a) + py * Math.cos(a) - py
2512
- ];
2513
- }
2514
- function faceField(m) {
2515
- const f = m.frame;
2516
- return (x, y, ax, ay) => {
2517
- const s = Math.sign(ax);
2518
- const a = Math.abs(ax) / 30;
2519
- const chin = smoothstep(f.mouthY, f.chinY, y);
2520
- const width = m.widthUpper + (m.widthJaw - m.widthUpper) * smoothstep(f.cheekY, f.jawY, y);
2521
- const shell = f.hairShell ? 0 : m.shellScale - 1;
2522
- const dx = s * a * (m.face + m.chinLead * chin) + a * (width - 1 + shell) * (x - f.axisX);
2523
- const t = Math.min(
2524
- 1,
2525
- Math.max(0, (f.face.y1 - y) / Math.max(1, f.face.y1 - f.chinY))
2526
- );
2527
- const v = {
2528
- up: NOD.faceTop.up + (NOD.chin.up - NOD.faceTop.up) * t,
2529
- down: NOD.faceTop.down + (NOD.chin.down - NOD.faceTop.down) * t
2530
- };
2531
- return [dx, nodDy(v, ay, f.hh)];
2532
- };
2533
- }
2534
- function earField(m) {
2535
- const f = m.frame;
2536
- const ears = f.ears;
2537
- if (ears === void 0) return () => [0, 0];
2538
- const face = faceField(m);
2539
- return (x, y, ax, ay) => {
2540
- const s = Math.sign(ax);
2541
- const a = Math.abs(ax) / 30;
2542
- const dy = face(x, y, 0, ay)[1];
2543
- if (sideOf(m, x, s) === "far") return [s * a * TURN.earFar * m.face, dy];
2544
- const d = Math.abs(x - f.axisX);
2545
- const attach = ears.attachAt(y);
2546
- if (d <= attach) return [face(x, y, ax, 0)[0], dy];
2547
- const root = face(f.axisX + Math.sign(x - f.axisX) * attach, y, ax, 0)[0];
2548
- const rim = s * a * TURN.earNear * m.face;
2549
- const t = Math.min(1, (d - attach) / Math.max(1, ears.outer - attach));
2550
- return [root + (rim - root) * t, dy];
2551
- };
2552
- }
2553
- function chinSlide(m) {
2554
- const f = m.frame;
2555
- return Math.abs(faceField(m)(f.axisX, f.chinY, 30, 0)[0]);
2556
- }
2557
- var NECK_SHADE_MAX = 0.45;
2558
- var NECK_SHADE_FULL = 0.2;
2559
- function neckField(m) {
2560
- const f = m.frame;
2561
- const nk = f.neck;
2562
- if (nk === void 0) return () => [0, 0];
2563
- const face = faceField(m);
2564
- const full = chinSlide(m);
2565
- const share = Math.min(1, NECK_SHADE_MAX * nk.waist / Math.max(1e-6, full));
2566
- const bottom = f.face.y0;
2567
- const tip = f.cutAt(f.axisX);
2568
- const top = tip - NECK_SHADE_FULL * (tip - bottom);
2569
- return (x, y, ax) => {
2570
- const wy = Math.min(
2571
- 1,
2572
- Math.max(0, (y - bottom) / Math.max(1, top - bottom))
2573
- );
2574
- const wx = Math.max(0, 1 - Math.abs(x - f.axisX) / nk.waist);
2575
- return [share * face(f.axisX, f.chinY, ax, 0)[0] * wx * wy, 0];
2576
- };
2534
+ // src/auto-rig/head.ts
2535
+ function outerEdge(runs, axisX, side) {
2536
+ if (runs.length === 0) return void 0;
2537
+ const x = side > 0 ? runs[runs.length - 1] : runs[0];
2538
+ return side * (x - axisX) > 0 ? x : void 0;
2577
2539
  }
2578
- function sideOf(m, x, s) {
2579
- return s * (x - m.frame.axisX) > 0 ? "far" : "near";
2540
+ function runReach(runs, x, side) {
2541
+ const px = side > 0 ? x - 1 : x;
2542
+ for (let k = 0; k < runs.length; k += 2) {
2543
+ const [a, b] = [runs[k], runs[k + 1]];
2544
+ if (a <= px && px + 1 <= b) {
2545
+ return side > 0 ? { inward: x - a, outward: b - x } : { inward: b - x, outward: x - a };
2546
+ }
2547
+ }
2548
+ return { inward: 0, outward: 0 };
2580
2549
  }
2581
- function eyeField(m, box) {
2582
- const xe = cx(box);
2583
- const ye = cy(box);
2584
- const hh = m.frame.hh;
2585
- return (x, y, ax, ay) => {
2586
- const s = Math.sign(ax);
2587
- const a = Math.abs(ax) / 30;
2588
- const side = sideOf(m, xe, s);
2589
- const dx = s * a * m.eye[side] + a * (m.eyeScale[side] - 1) * (x - xe);
2590
- const up = Math.max(0, ay) / 30;
2591
- const down = Math.max(0, -ay) / 30;
2592
- const squash = up * (NOD.eyeHeight.up - 1) + down * (NOD.eyeHeight.down - 1);
2593
- return [dx, nodDy(NOD.eye, ay, hh) + squash * (y - ye)];
2594
- };
2550
+ function filledProfile(rows) {
2551
+ if (rows === void 0 || !rows.some((v) => v > 0)) return null;
2552
+ const out = rows.slice();
2553
+ const first = out.findIndex((v) => v > 0);
2554
+ for (let i = 0; i < first; i++) out[i] = out[first];
2555
+ for (let i = first + 1; i < out.length; i++)
2556
+ if (!(out[i] > 0)) out[i] = out[i - 1];
2557
+ return out;
2595
2558
  }
2596
- function browField(m, box) {
2597
- const xb = cx(box);
2598
- return (_x, _y, ax, ay) => {
2599
- const s = Math.sign(ax);
2600
- const a = Math.abs(ax) / 30;
2601
- return [s * a * m.brow[sideOf(m, xb, s)], nodDy(NOD.brow, ay, m.frame.hh)];
2602
- };
2559
+ var smoothProfile = (rows) => boxSmooth(rows, Math.max(1, Math.round(rows.length * 0.01)));
2560
+ var EAR_STEP = 0.03;
2561
+ var EAR_ROWS = 3;
2562
+ function findEars(raw, wMax, firstRow, lastRow, minRows, maxRows) {
2563
+ const step = Math.max(4, EAR_STEP * wMax);
2564
+ for (let t = Math.max(firstRow, EAR_ROWS); t < lastRow; t++) {
2565
+ if (raw[t] - raw[t - EAR_ROWS] < step) continue;
2566
+ for (let b = t + minRows; b <= Math.min(lastRow, t + maxRows); b++) {
2567
+ if (raw[b - EAR_ROWS] - raw[b] >= step) {
2568
+ return { top: t - EAR_ROWS, bottom: b };
2569
+ }
2570
+ }
2571
+ return void 0;
2572
+ }
2573
+ return void 0;
2603
2574
  }
2604
- function noseField(m) {
2605
- const at = m.noseAt;
2606
- const top = m.noseTopY ?? at.y;
2607
- return (x, y, ax, ay) => {
2608
- const s = Math.sign(ax);
2609
- const a = Math.abs(ax) / 30;
2610
- const [tx, ty] = spin(
2611
- x,
2612
- y,
2613
- at.x,
2614
- top,
2615
- s * a * m.tilt * NOSE_TILT_DEG * DEG
2616
- );
2617
- return [s * a * m.nose + tx, nodDy(NOD.nose, ay, m.frame.hh) + ty];
2618
- };
2575
+ function boxSmooth(v, k) {
2576
+ return v.map((_, i) => {
2577
+ let s = 0;
2578
+ let n = 0;
2579
+ for (let j = i - k; j <= i + k; j++) {
2580
+ if (j < 0 || j >= v.length) continue;
2581
+ s += v[j];
2582
+ n++;
2583
+ }
2584
+ return s / n;
2585
+ });
2619
2586
  }
2620
- function mouthField(m) {
2621
- const c = m.mouthAt;
2622
- return (x, y, ax, ay) => {
2623
- const s = Math.sign(ax);
2624
- const a = Math.abs(ax) / 30;
2625
- const wx = c.x + (x - c.x) * (1 + a * (m.mouthWidth - 1));
2626
- const [tx, ty] = spin(
2627
- wx,
2628
- y,
2629
- c.x,
2630
- c.y,
2631
- s * a * m.tilt * TURN.mouthTiltDeg * DEG
2632
- );
2633
- return [
2634
- s * a * m.mouth + wx - x + tx,
2635
- nodDy(NOD.mouth, ay, m.frame.hh) + ty
2636
- ];
2637
- };
2587
+ var CUT_MARGIN = 2;
2588
+ var CUT_LOW = 3;
2589
+ function fadeToOutline(d, nk) {
2590
+ return Math.max(0, 1 - d / nk.waist);
2638
2591
  }
2639
- function hairField(m, shift, nod) {
2640
- const f = m.frame;
2641
- return (x, _y, ax, ay) => {
2642
- const s = Math.sign(ax);
2643
- const a = Math.abs(ax) / 30;
2644
- return [
2645
- s * a * shift + a * (m.shellScale - 1) * (x - f.axisX),
2646
- nodDy(nod, ay, f.hh)
2647
- ];
2592
+ var STROKE_RUN = 4;
2593
+ var STROKE_SLACK = 6;
2594
+ var JAW_SHADE = 0.05;
2595
+ function buildHeadFrame(layers, opts) {
2596
+ const byRole = new Map(layers.map((l) => [l.role, l]));
2597
+ const faceLayer = byRole.get("face");
2598
+ const { canvasW, canvasH } = faceLayer;
2599
+ const face = boxOfLayer(faceLayer);
2600
+ const axisX = cx(face);
2601
+ const plateHalf = faceLayer.cropW / 2;
2602
+ const centreY = (role) => {
2603
+ const l = byRole.get(role);
2604
+ return l === void 0 ? void 0 : cy(boxOfLayer(l));
2648
2605
  };
2649
- }
2650
- function hairFrontBends(frame, boxes) {
2651
- const f = frame;
2652
- const eyes = [boxes.eye_L, boxes.eye_R].filter(
2653
- (e) => e !== void 0
2654
- );
2655
- const inner = Math.max(
2656
- 1,
2657
- Math.min(
2658
- f.paintedHalfAt(f.eyeY),
2659
- ...eyes.map((e) => Math.max(f.axisX - e.x0, e.x1 - f.axisX))
2660
- )
2661
- );
2662
- return {
2663
- inner,
2664
- left: Math.max(inner + 1, f.axisX - f.shellLeft),
2665
- right: Math.max(inner + 1, f.shellRight - f.axisX)
2666
- };
2667
- }
2668
- function hairFrontField(m) {
2669
- const f = m.frame;
2670
- const back = hairField(m, m.hairBack, NOD.hairBack);
2671
- const top = f.face.y1;
2672
- const eyes = [m.boxes.eye_L, m.boxes.eye_R].filter(
2673
- (e) => e !== void 0
2674
- );
2675
- const { inner, left, right } = hairFrontBends(f, m.boxes);
2676
- const eyeY = eyes.length > 0 ? eyes.reduce((t, e) => t + cy(e), 0) / eyes.length : f.eyeY;
2677
- const eyeH = eyes.length > 0 ? eyes.reduce((t, e) => t + (e.y1 - e.y0), 0) / eyes.length : 1;
2678
- return (x, y, ax, ay) => {
2679
- const s = Math.sign(ax);
2680
- const a = Math.abs(ax) / 30;
2681
- const u = s * (x - f.axisX);
2682
- const across = Math.min(1, Math.max(0, (u + inner) / (2 * inner)));
2683
- const band = 1 - smoothstep(eyeH / 2, 1.5 * eyeH, Math.abs(y - eyeY));
2684
- const core = m.hairFront + (m.hairFar - m.hairFront) * across * band;
2685
- const out = Math.min(
2686
- 1,
2687
- Math.max(
2688
- 0,
2689
- (Math.abs(x - f.axisX) - inner) / ((x < f.axisX ? left : right) - inner)
2690
- )
2691
- );
2692
- const follow = core + (m.hairOuter - core) * out;
2693
- const rx = s * a * follow + a * (m.shellScale - 1) * (x - f.axisX);
2694
- const ry = nodDy(NOD.hairFront, ay, f.hh);
2695
- const [bx, by] = back(x, y, ax, ay);
2696
- const w = 1 - smoothstep(top, Math.max(top + 1, m.hairTop), y);
2697
- return [bx + w * (rx - bx), by + w * (ry - by)];
2698
- };
2699
- }
2700
- function familyField(m, family) {
2701
- switch (family) {
2702
- case "face":
2703
- return faceField(m);
2704
- case "eye_L":
2705
- case "eye_R":
2706
- return eyeField(m, m.boxes[family]);
2707
- case "brow_L":
2708
- case "brow_R":
2709
- return browField(m, m.boxes[family]);
2710
- case "nose":
2711
- return noseField(m);
2712
- case "mouth":
2713
- return mouthField(m);
2714
- case "hair_front":
2715
- return hairFrontField(m);
2716
- case "hair_back":
2717
- return hairField(m, m.hairBack, NOD.hairBack);
2718
- case "body":
2719
- return () => [0, 0];
2720
- }
2721
- }
2722
-
2723
- // src/auto-rig/context.ts
2724
- function sidePair(byRole, a, b) {
2725
- const la = byRole.get(a);
2726
- const lb = byRole.get(b);
2727
- if (la === void 0 || lb === void 0) return void 0;
2728
- const [lo, hi] = [
2729
- { role: a, box: boxOfLayer(la) },
2730
- { role: b, box: boxOfLayer(lb) }
2731
- ].sort((p, q) => cx(p.box) - cx(q.box));
2732
- return { left: lo, right: hi };
2733
- }
2734
- function sideIrises(byRole) {
2735
- const pair = sidePair(byRole, "iris_L", "iris_R");
2736
- return pair === void 0 ? {} : { left: pair.left.box, right: pair.right.box };
2737
- }
2738
- function solveContext(frame, byRole, options, hasNose) {
2739
- const box = (role) => boxOfLayer(byRole.get(role));
2740
- const row = (s, strand2, sideSign) => {
2741
- if (strand2 !== void 0) {
2742
- const lo = sideSign < 0 ? strand2.irisOuter : strand2.irisInner;
2743
- const hi = sideSign < 0 ? strand2.irisInner : strand2.irisOuter;
2744
- return { role: s.role, lo, hi, y: strand2.y };
2745
- }
2746
- const inset = bw(s.box) > 4 ? 1 : 0;
2747
- return {
2748
- role: s.role,
2749
- lo: s.box.x0 + inset,
2750
- hi: s.box.x1 - inset,
2751
- y: cy(s.box)
2752
- };
2753
- };
2754
- const eyes = sidePair(byRole, "eye_L", "eye_R");
2755
- const irises = sidePair(byRole, "iris_L", "iris_R");
2756
- const pair = irises ?? eyes;
2757
- const strand = irises ? options.strandEdges : void 0;
2758
- const left = row(pair.left, strand?.left, -1);
2759
- const right = row(pair.right, strand?.right, 1);
2760
- let candidates;
2761
- const edges = options.headEdges;
2762
- if (frame.hairShell && edges && edges.left.length > 0 && edges.right.length > 0) {
2763
- candidates = {
2764
- left: edges.left.map((e) => ({ role: e.role, x: e.x })),
2765
- // A right edge names its outermost pixel column; the boundary is one on.
2766
- right: edges.right.map((e) => ({ role: e.role, x: e.x + 1 }))
2767
- };
2768
- } else if (frame.hairShell) {
2769
- candidates = {
2770
- left: [{ role: "hair_back", x: frame.shellLeft }],
2771
- right: [{ role: "hair_back", x: frame.shellRight }]
2772
- };
2773
- } else {
2774
- const w = frame.paintedHalfAt(frame.eyeY);
2775
- candidates = {
2776
- left: [{ role: "face", x: frame.axisX - w }],
2777
- right: [{ role: "face", x: frame.axisX + w }]
2778
- };
2779
- }
2780
- const restHalf = (Math.max(...candidates.right.map((c) => c.x)) - Math.min(...candidates.left.map((c) => c.x))) / 2;
2781
- const nose = byRole.get("nose");
2782
- let noseAt;
2783
- let noseBox;
2784
- if (hasNose && nose !== void 0) {
2785
- noseBox = nose.denseCore ? boxOfPixels(nose.denseCore, nose.canvasW, nose.canvasH) : boxOfLayer(nose);
2786
- noseAt = { x: cx(noseBox), y: cy(noseBox) };
2787
- }
2788
- const mouthBox = byRole.has("mouth_open") ? unionBoxes([box("mouth"), box("mouth_open")]) : box("mouth");
2789
- const m = box("mouth");
2790
- return {
2791
- frame,
2792
- left,
2793
- right,
2794
- candidates,
2795
- restHalf,
2796
- strand,
2797
- hasHairFront: byRole.has("hair_front"),
2798
- eyeBoxes: { left: eyes.left.box, right: eyes.right.box },
2799
- noseAt,
2800
- noseBox,
2801
- mouthAt: { x: cx(m), y: cy(m) },
2802
- mouthBox
2803
- };
2804
- }
2805
- function renderedLander(turn, parts, grids, from = /* @__PURE__ */ new Map()) {
2806
- const analytic = /* @__PURE__ */ new Map();
2807
- const landed = /* @__PURE__ */ new Map();
2808
- const field = (role) => {
2809
- const family = roleSpec(role).family;
2810
- let f = analytic.get(family);
2811
- if (f === void 0) analytic.set(family, f = familyField(turn, family));
2812
- return f;
2813
- };
2814
- return (role, x, y, ax) => {
2815
- const family = roleSpec(role).family;
2816
- const part = parts.find((p) => p.id === role);
2817
- const g = grids.get(family);
2818
- const warp = part?.warps?.find((w) => w.parameter === import_format3.StandardParameter.AngleX);
2819
- if (part === void 0 || part.mesh === void 0 || !g && !warp) {
2820
- return x + field(role)(x, y, ax, 0)[0];
2821
- }
2822
- const box = {
2823
- x0: part.transform.x - part.width / 2,
2824
- x1: part.transform.x + part.width / 2,
2825
- y0: part.transform.y - part.height / 2,
2826
- y1: part.transform.y + part.height / 2
2827
- };
2828
- const rest = meshPoints(part.mesh, box);
2829
- const key = `${role}@${ax}`;
2830
- let xs = landed.get(key);
2831
- if (xs === void 0) {
2832
- if (g) {
2833
- const offsets = keyformAt(g.keyforms, ax);
2834
- xs = rest.map(([px, py]) => land(g.lattice, offsets, px, py)[0]);
2835
- } else {
2836
- const k = warp.keyforms;
2837
- const offsets = blendStops(
2838
- k.map((kf) => kf.value),
2839
- k.map((kf) => kf.offsets),
2840
- ax
2841
- );
2842
- xs = rest.map(([px], i) => px + offsets[2 * i] * bw(box));
2843
- }
2844
- landed.set(key, xs);
2845
- }
2846
- const idx = part.mesh.indices;
2847
- for (let t = from.get(role) ?? 0; t < idx.length; t += 3) {
2848
- const [a, b, c] = [idx[t], idx[t + 1], idx[t + 2]];
2849
- const [ax0, ay0] = rest[a];
2850
- const [bx0, by0] = rest[b];
2851
- const [cx0, cy0] = rest[c];
2852
- const d = (by0 - cy0) * (ax0 - cx0) + (cx0 - bx0) * (ay0 - cy0);
2853
- if (Math.abs(d) < 1e-9) continue;
2854
- const u = ((by0 - cy0) * (x - cx0) + (cx0 - bx0) * (y - cy0)) / d;
2855
- const v = ((cy0 - ay0) * (x - cx0) + (ax0 - cx0) * (y - cy0)) / d;
2856
- const w = 1 - u - v;
2857
- if (u < -1e-6 || v < -1e-6 || w < -1e-6) continue;
2858
- return u * xs[a] + v * xs[b] + w * xs[c];
2859
- }
2860
- return x + field(role)(x, y, ax, 0)[0];
2861
- };
2862
- }
2863
-
2864
- // src/auto-rig/head.ts
2865
- function filledProfile(rows) {
2866
- if (rows === void 0 || !rows.some((v) => v > 0)) return null;
2867
- const out = rows.slice();
2868
- const first = out.findIndex((v) => v > 0);
2869
- for (let i = 0; i < first; i++) out[i] = out[first];
2870
- for (let i = first + 1; i < out.length; i++)
2871
- if (!(out[i] > 0)) out[i] = out[i - 1];
2872
- return out;
2873
- }
2874
- var smoothProfile = (rows) => boxSmooth(rows, Math.max(1, Math.round(rows.length * 0.01)));
2875
- var EAR_STEP = 0.03;
2876
- var EAR_ROWS = 3;
2877
- function findEars(raw, wMax, firstRow, lastRow, minRows, maxRows) {
2878
- const step = Math.max(4, EAR_STEP * wMax);
2879
- for (let t = Math.max(firstRow, EAR_ROWS); t < lastRow; t++) {
2880
- if (raw[t] - raw[t - EAR_ROWS] < step) continue;
2881
- for (let b = t + minRows; b <= Math.min(lastRow, t + maxRows); b++) {
2882
- if (raw[b - EAR_ROWS] - raw[b] >= step) {
2883
- return { top: t - EAR_ROWS, bottom: b };
2884
- }
2885
- }
2886
- return void 0;
2887
- }
2888
- return void 0;
2889
- }
2890
- function boxSmooth(v, k) {
2891
- return v.map((_, i) => {
2892
- let s = 0;
2893
- let n = 0;
2894
- for (let j = i - k; j <= i + k; j++) {
2895
- if (j < 0 || j >= v.length) continue;
2896
- s += v[j];
2897
- n++;
2898
- }
2899
- return s / n;
2900
- });
2901
- }
2902
- var CUT_MARGIN = 2;
2903
- var CUT_LOW = 3;
2904
- function fadeToOutline(d, nk) {
2905
- return Math.max(0, 1 - d / nk.waist);
2906
- }
2907
- var STROKE_RUN = 4;
2908
- var STROKE_SLACK = 6;
2909
- var JAW_SHADE = 0.05;
2910
- function buildHeadFrame(layers, opts) {
2911
- const byRole = new Map(layers.map((l) => [l.role, l]));
2912
- const faceLayer = byRole.get("face");
2913
- const { canvasW, canvasH } = faceLayer;
2914
- const face = boxOfLayer(faceLayer);
2915
- const axisX = cx(face);
2916
- const plateHalf = faceLayer.cropW / 2;
2917
- const centreY = (role) => {
2918
- const l = byRole.get(role);
2919
- return l === void 0 ? void 0 : cy(boxOfLayer(l));
2920
- };
2921
- const pair = (a, b) => {
2922
- const ya = centreY(a);
2923
- const yb = centreY(b);
2924
- return ya === void 0 || yb === void 0 ? void 0 : (ya + yb) / 2;
2606
+ const pair = (a, b) => {
2607
+ const ya = centreY(a);
2608
+ const yb = centreY(b);
2609
+ return ya === void 0 || yb === void 0 ? void 0 : (ya + yb) / 2;
2925
2610
  };
2926
2611
  const eyeY = pair("iris_L", "iris_R") ?? pair("eye_L", "eye_R");
2927
2612
  const mouthY = centreY("mouth");
@@ -3074,75 +2759,613 @@ function buildHeadFrame(layers, opts) {
3074
2759
  const y = stroke !== void 0 ? stroke - shade * band - CUT_MARGIN * fadeToOutline(d, nk) : (d <= nk.half ? chin + (nk.topY - chin) * (d / Math.max(1, nk.half)) : outlineAt(d)) - CUT_MARGIN;
3075
2760
  cut[i] = y >= face.y1 ? face.y1 : y;
3076
2761
  }
3077
- const low = cut.map((_, i) => {
3078
- let m = Infinity;
3079
- for (let j = i - CUT_LOW; j <= i + CUT_LOW; j++) {
3080
- if (j >= 0 && j < cut.length) m = Math.min(m, cut[j]);
3081
- }
3082
- return m;
3083
- });
3084
- if (opts.chinSlide !== void 0 && banded >= 0)
3085
- nk.bandEnd = banded + CUT_LOW + 1;
3086
- cutAt = (x) => {
3087
- const f = x - (faceLayer.bbox.x - canvasW / 2) - 0.5;
3088
- const i = clamp(Math.floor(f), 0, low.length - 1);
3089
- const j = Math.min(i + 1, low.length - 1);
3090
- const t = clamp(f - i, 0, 1);
3091
- return Math.max(face.y0, low[i] + (low[j] - low[i]) * t);
3092
- };
3093
- } else {
3094
- const head = earless(chinY);
3095
- headMax = Math.max(...head);
3096
- if (ears !== void 0) {
3097
- const bottomOf = (d) => {
3098
- for (let r = head.length - 1; r >= 0; r--) {
3099
- if (head[r] >= d) return rowY(r) - 0.5;
3100
- }
3101
- return face.y1;
3102
- };
3103
- cutAt = (x) => {
3104
- const y = bottomOf(Math.abs(x - axisX));
3105
- return y >= face.y1 ? face.y1 : Math.max(face.y0, y - CUT_MARGIN);
3106
- };
2762
+ const low = cut.map((_, i) => {
2763
+ let m = Infinity;
2764
+ for (let j = i - CUT_LOW; j <= i + CUT_LOW; j++) {
2765
+ if (j >= 0 && j < cut.length) m = Math.min(m, cut[j]);
2766
+ }
2767
+ return m;
2768
+ });
2769
+ if (opts.chinSlide !== void 0 && banded >= 0)
2770
+ nk.bandEnd = banded + CUT_LOW + 1;
2771
+ cutAt = (x) => {
2772
+ const f = x - (faceLayer.bbox.x - canvasW / 2) - 0.5;
2773
+ const i = clamp(Math.floor(f), 0, low.length - 1);
2774
+ const j = Math.min(i + 1, low.length - 1);
2775
+ const t = clamp(f - i, 0, 1);
2776
+ return Math.max(face.y0, low[i] + (low[j] - low[i]) * t);
2777
+ };
2778
+ } else {
2779
+ const head = earless(chinY);
2780
+ headMax = Math.max(...head);
2781
+ if (ears !== void 0) {
2782
+ const bottomOf = (d) => {
2783
+ for (let r = head.length - 1; r >= 0; r--) {
2784
+ if (head[r] >= d) return rowY(r) - 0.5;
2785
+ }
2786
+ return face.y1;
2787
+ };
2788
+ cutAt = (x) => {
2789
+ const y = bottomOf(Math.abs(x - axisX));
2790
+ return y >= face.y1 ? face.y1 : Math.max(face.y0, y - CUT_MARGIN);
2791
+ };
2792
+ }
2793
+ }
2794
+ }
2795
+ const hh = HH_PER_EYE_TO_CHIN * Math.max(1, eyeY - chinY);
2796
+ const headHalf = opts.headHalfWidth;
2797
+ const hairShell = headHalf !== void 0;
2798
+ let shellLeft = axisX - plateHalf;
2799
+ let shellRight = axisX + plateHalf;
2800
+ if (hairShell) {
2801
+ if (opts.headEdges && opts.headEdges.left.length > 0 && opts.headEdges.right.length > 0) {
2802
+ shellLeft = Math.min(...opts.headEdges.left.map((e) => e.x));
2803
+ shellRight = Math.max(...opts.headEdges.right.map((e) => e.x)) + 1;
2804
+ } else {
2805
+ shellLeft = axisX - headHalf;
2806
+ shellRight = axisX + headHalf;
2807
+ }
2808
+ }
2809
+ const hairRuns = (role, y) => {
2810
+ const l = byRole.get(role);
2811
+ if (l?.rowRuns === void 0) return void 0;
2812
+ const runs = l.rowRuns[Math.floor(canvasH / 2 - y) - l.bbox.y];
2813
+ return runs === void 0 ? [] : runs.map((c) => c - canvasW / 2);
2814
+ };
2815
+ return {
2816
+ canvasW,
2817
+ canvasH,
2818
+ face,
2819
+ axisX,
2820
+ eyeY,
2821
+ mouthY,
2822
+ chinY,
2823
+ hh,
2824
+ paintedHalfAt,
2825
+ wMax,
2826
+ headMax,
2827
+ hasProfile: profile !== null,
2828
+ neck,
2829
+ ears,
2830
+ cutAt,
2831
+ cheekY: eyeY - 0.5 * (eyeY - chinY),
2832
+ jawY: eyeY - 0.8 * (eyeY - chinY),
2833
+ shellLeft,
2834
+ shellRight,
2835
+ hairShell,
2836
+ holdBase: headHalf ?? plateHalf,
2837
+ hairRuns
2838
+ };
2839
+ }
2840
+
2841
+ // src/auto-rig/fields.ts
2842
+ var NOSE_TILT_DEG = 6;
2843
+ function nodDy(v, ay, hh) {
2844
+ const up = Math.max(0, ay) / 30;
2845
+ const down = Math.max(0, -ay) / 30;
2846
+ return -(up * v.up + down * v.down) * hh;
2847
+ }
2848
+ function spin(x, y, ox, oy, a) {
2849
+ if (a === 0) return [0, 0];
2850
+ const px = x - ox;
2851
+ const py = y - oy;
2852
+ return [
2853
+ px * Math.cos(a) - py * Math.sin(a) - px,
2854
+ px * Math.sin(a) + py * Math.cos(a) - py
2855
+ ];
2856
+ }
2857
+ function faceField(m) {
2858
+ const f = m.frame;
2859
+ return (x, y, ax, ay) => {
2860
+ const s = Math.sign(ax);
2861
+ const a = Math.abs(ax) / 30;
2862
+ const chin = smoothstep(f.mouthY, f.chinY, y);
2863
+ const width = m.widthUpper + (m.widthJaw - m.widthUpper) * smoothstep(f.cheekY, f.jawY, y);
2864
+ const shell = f.hairShell ? 0 : m.shellScale - 1;
2865
+ const dx = s * a * (m.face + m.chinLead * chin) + a * (width - 1 + shell) * (x - f.axisX);
2866
+ const t = Math.min(
2867
+ 1,
2868
+ Math.max(0, (f.face.y1 - y) / Math.max(1, f.face.y1 - f.chinY))
2869
+ );
2870
+ const v = {
2871
+ up: NOD.faceTop.up + (NOD.chin.up - NOD.faceTop.up) * t,
2872
+ down: NOD.faceTop.down + (NOD.chin.down - NOD.faceTop.down) * t
2873
+ };
2874
+ return [dx, nodDy(v, ay, f.hh)];
2875
+ };
2876
+ }
2877
+ function earField(m) {
2878
+ const f = m.frame;
2879
+ const ears = f.ears;
2880
+ if (ears === void 0) return () => [0, 0];
2881
+ const face = faceField(m);
2882
+ return (x, y, ax, ay) => {
2883
+ const s = Math.sign(ax);
2884
+ const a = Math.abs(ax) / 30;
2885
+ const dy = face(x, y, 0, ay)[1];
2886
+ if (sideOf(m, x, s) === "far") return [s * a * TURN.earFar * m.face, dy];
2887
+ const d = Math.abs(x - f.axisX);
2888
+ const attach = ears.attachAt(y);
2889
+ if (d <= attach) return [face(x, y, ax, 0)[0], dy];
2890
+ const root = face(f.axisX + Math.sign(x - f.axisX) * attach, y, ax, 0)[0];
2891
+ const rim = s * a * TURN.earNear * m.face;
2892
+ const t = Math.min(1, (d - attach) / Math.max(1, ears.outer - attach));
2893
+ return [root + (rim - root) * t, dy];
2894
+ };
2895
+ }
2896
+ function chinSlide(m) {
2897
+ const f = m.frame;
2898
+ return Math.abs(faceField(m)(f.axisX, f.chinY, 30, 0)[0]);
2899
+ }
2900
+ var NECK_SHADE_MAX = 0.45;
2901
+ var NECK_SHADE_FULL = 0.2;
2902
+ function neckField(m) {
2903
+ const f = m.frame;
2904
+ const nk = f.neck;
2905
+ if (nk === void 0) return () => [0, 0];
2906
+ const face = faceField(m);
2907
+ const full = chinSlide(m);
2908
+ const share = Math.min(1, NECK_SHADE_MAX * nk.waist / Math.max(1e-6, full));
2909
+ const bottom = f.face.y0;
2910
+ const tip = f.cutAt(f.axisX);
2911
+ const top = tip - NECK_SHADE_FULL * (tip - bottom);
2912
+ return (x, y, ax) => {
2913
+ const wy = Math.min(
2914
+ 1,
2915
+ Math.max(0, (y - bottom) / Math.max(1, top - bottom))
2916
+ );
2917
+ const wx = Math.max(0, 1 - Math.abs(x - f.axisX) / nk.waist);
2918
+ return [share * face(f.axisX, f.chinY, ax, 0)[0] * wx * wy, 0];
2919
+ };
2920
+ }
2921
+ function sideOf(m, x, s) {
2922
+ return s * (x - m.frame.axisX) > 0 ? "far" : "near";
2923
+ }
2924
+ function eyeField(m, box) {
2925
+ const xe = cx(box);
2926
+ const ye = cy(box);
2927
+ const hh = m.frame.hh;
2928
+ return (x, y, ax, ay) => {
2929
+ const s = Math.sign(ax);
2930
+ const a = Math.abs(ax) / 30;
2931
+ const side = sideOf(m, xe, s);
2932
+ const dx = s * a * m.eye[side] + a * (m.eyeScale[side] - 1) * (x - xe);
2933
+ const up = Math.max(0, ay) / 30;
2934
+ const down = Math.max(0, -ay) / 30;
2935
+ const squash = up * (NOD.eyeHeight.up - 1) + down * (NOD.eyeHeight.down - 1);
2936
+ return [dx, nodDy(NOD.eye, ay, hh) + squash * (y - ye)];
2937
+ };
2938
+ }
2939
+ function browField(m, box) {
2940
+ const xb = cx(box);
2941
+ return (_x, _y, ax, ay) => {
2942
+ const s = Math.sign(ax);
2943
+ const a = Math.abs(ax) / 30;
2944
+ return [s * a * m.brow[sideOf(m, xb, s)], nodDy(NOD.brow, ay, m.frame.hh)];
2945
+ };
2946
+ }
2947
+ function noseField(m) {
2948
+ const at = m.noseAt;
2949
+ const top = m.noseTopY ?? at.y;
2950
+ return (x, y, ax, ay) => {
2951
+ const s = Math.sign(ax);
2952
+ const a = Math.abs(ax) / 30;
2953
+ const [tx, ty] = spin(
2954
+ x,
2955
+ y,
2956
+ at.x,
2957
+ top,
2958
+ s * a * m.tilt * NOSE_TILT_DEG * DEG
2959
+ );
2960
+ return [s * a * m.nose + tx, nodDy(NOD.nose, ay, m.frame.hh) + ty];
2961
+ };
2962
+ }
2963
+ function mouthField(m) {
2964
+ const c = m.mouthAt;
2965
+ return (x, y, ax, ay) => {
2966
+ const s = Math.sign(ax);
2967
+ const a = Math.abs(ax) / 30;
2968
+ const wx = c.x + (x - c.x) * (1 + a * (m.mouthWidth - 1));
2969
+ const [tx, ty] = spin(
2970
+ wx,
2971
+ y,
2972
+ c.x,
2973
+ c.y,
2974
+ s * a * m.tilt * TURN.mouthTiltDeg * DEG
2975
+ );
2976
+ return [
2977
+ s * a * m.mouth + wx - x + tx,
2978
+ nodDy(NOD.mouth, ay, m.frame.hh) + ty
2979
+ ];
2980
+ };
2981
+ }
2982
+ function hairField(m, shift, nod) {
2983
+ const f = m.frame;
2984
+ return (x, _y, ax, ay) => {
2985
+ const s = Math.sign(ax);
2986
+ const a = Math.abs(ax) / 30;
2987
+ return [
2988
+ s * a * shift + a * (m.shellScale - 1) * (x - f.axisX),
2989
+ nodDy(nod, ay, f.hh)
2990
+ ];
2991
+ };
2992
+ }
2993
+ function hairFrontBends(frame, boxes) {
2994
+ const f = frame;
2995
+ const eyes = [boxes.eye_L, boxes.eye_R].filter(
2996
+ (e) => e !== void 0
2997
+ );
2998
+ const inner = Math.max(
2999
+ 1,
3000
+ Math.min(
3001
+ f.paintedHalfAt(f.eyeY),
3002
+ ...eyes.map((e) => Math.max(f.axisX - e.x0, e.x1 - f.axisX))
3003
+ )
3004
+ );
3005
+ return {
3006
+ inner,
3007
+ left: Math.max(inner + 1, f.axisX - f.shellLeft),
3008
+ right: Math.max(inner + 1, f.shellRight - f.axisX)
3009
+ };
3010
+ }
3011
+ function hairFrontField(m) {
3012
+ const f = m.frame;
3013
+ const back = hairField(m, m.hairBack, NOD.hairBack);
3014
+ const cap = { up: NOD.hairFrontTop.up, down: capSlide(m) };
3015
+ const eyes = [m.boxes.eye_L, m.boxes.eye_R].filter(
3016
+ (e) => e !== void 0
3017
+ );
3018
+ const { inner, left, right } = hairFrontBends(f, m.boxes);
3019
+ const eyeY = eyes.length > 0 ? eyes.reduce((t, e) => t + cy(e), 0) / eyes.length : f.eyeY;
3020
+ const eyeH = eyes.length > 0 ? eyes.reduce((t, e) => t + (e.y1 - e.y0), 0) / eyes.length : 1;
3021
+ const coreAt = (x, y, s) => {
3022
+ const u = s * (x - f.axisX);
3023
+ const across = Math.min(1, Math.max(0, (u + inner) / (2 * inner)));
3024
+ const band = 1 - smoothstep(eyeH / 2, 1.5 * eyeH, Math.abs(y - eyeY));
3025
+ return m.hairFront + (m.hairFar - m.hairFront) * across * band;
3026
+ };
3027
+ const followAt = (x, y, s) => {
3028
+ const core = coreAt(x, y, s);
3029
+ const out = Math.min(
3030
+ 1,
3031
+ Math.max(
3032
+ 0,
3033
+ (Math.abs(x - f.axisX) - inner) / ((x < f.axisX ? left : right) - inner)
3034
+ )
3035
+ );
3036
+ return core + (m.hairOuter - core) * out;
3037
+ };
3038
+ const ride = outerRide(m, inner, coreAt, followAt);
3039
+ return (x, y, ax, ay) => {
3040
+ const s = Math.sign(ax);
3041
+ const a = Math.abs(ax) / 30;
3042
+ const follow = followAt(x, y, s) + (ride === void 0 ? 0 : ride(x, y, s));
3043
+ const rx = s * a * follow + a * (m.shellScale - 1) * (x - f.axisX);
3044
+ const ry = nodDy(NOD.hairFront, ay, f.hh);
3045
+ const bx = back(x, y, ax, ay)[0];
3046
+ const by = nodDy(cap, ay, f.hh);
3047
+ const w = crownBlend(m, y);
3048
+ return [bx + w * (rx - bx), by + w * (ry - by)];
3049
+ };
3050
+ }
3051
+ function crownBlend(m, y) {
3052
+ const top = m.frame.face.y1;
3053
+ return 1 - smoothstep(top, Math.max(top + 1, m.hairTop), y);
3054
+ }
3055
+ var CAP_COVER_LOSS = 0.01;
3056
+ function capSlide(m) {
3057
+ const f = m.frame;
3058
+ const least = NOD.hairBack.down;
3059
+ const grid = m.hairFrontGrid;
3060
+ if (grid === void 0 || f.hairRuns("hair_front", grid.ys[0]) === void 0 || f.hairRuns("hair_back", grid.ys[0]) === void 0) {
3061
+ return least;
3062
+ }
3063
+ const { xs, ys } = grid;
3064
+ const top = ys[0];
3065
+ const n = ys.length;
3066
+ const bangs = NOD.hairFront.down;
3067
+ const wr = ys.map((y) => crownBlend(m, y));
3068
+ const blendAt = (y) => {
3069
+ const v = clamp((top - y) / (top - ys[n - 1]) * (n - 1), 0, n - 1);
3070
+ const r = Math.min(n - 2, Math.floor(v));
3071
+ return wr[r] + (wr[r + 1] - wr[r]) * (v - r);
3072
+ };
3073
+ const flat = ys[wr.findIndex((w) => w >= 1)] ?? ys[n - 1];
3074
+ const x0 = xs[0];
3075
+ const tops = new Float64Array(Math.round(xs[xs.length - 1] - x0)).fill(NaN);
3076
+ for (let i = 0; i < top - flat; i++) {
3077
+ const runs = f.hairRuns("hair_front", top - i - 0.5);
3078
+ for (let k = 0; k < runs.length; k += 2) {
3079
+ for (let px = runs[k]; px < runs[k + 1]; px++) {
3080
+ const j = Math.round(px - x0);
3081
+ if (Number.isNaN(tops[j])) tops[j] = i;
3082
+ }
3083
+ }
3084
+ }
3085
+ const hh = f.hh;
3086
+ const most = Math.ceil(Math.max(bangs, NOD.hairFrontTop.down) * hh);
3087
+ const bared = (s) => clamp(Math.ceil(s - 0.5), 0, most);
3088
+ const dropAt = (w, slide) => (w * bangs + (1 - w) * slide) * hh;
3089
+ const backNod = NOD.hairBack.down * hh;
3090
+ const backRows = /* @__PURE__ */ new Map();
3091
+ const columns = [];
3092
+ tops.forEach((i, j) => {
3093
+ if (Number.isNaN(i)) return;
3094
+ const w = blendAt(top - i);
3095
+ const empty = new Int32Array(most + 1);
3096
+ for (let r = 0; r < most; r++) {
3097
+ const at = Math.floor(i + r + 0.5 - backNod);
3098
+ let runs = backRows.get(at);
3099
+ if (runs === void 0) {
3100
+ runs = f.hairRuns("hair_back", top - at - 0.5);
3101
+ backRows.set(at, runs);
3107
3102
  }
3103
+ empty[r + 1] = empty[r] + (paints(runs, x0 + j) ? 0 : 1);
3108
3104
  }
3105
+ columns.push({ w, empty, base: empty[bared(dropAt(w, least))] });
3106
+ });
3107
+ const budget = CAP_COVER_LOSS * hh;
3108
+ const fits = (slide) => columns.every((c) => c.empty[bared(dropAt(c.w, slide))] - c.base <= budget);
3109
+ let lo = least;
3110
+ let hi = NOD.hairFrontTop.down;
3111
+ if (fits(hi)) return hi;
3112
+ for (let k = 0; k < 30; k++) {
3113
+ const mid = (lo + hi) / 2;
3114
+ if (fits(mid)) lo = mid;
3115
+ else hi = mid;
3109
3116
  }
3110
- const hh = HH_PER_EYE_TO_CHIN * Math.max(1, eyeY - chinY);
3111
- const headHalf = opts.headHalfWidth;
3112
- const hairShell = headHalf !== void 0;
3113
- let shellLeft = axisX - plateHalf;
3114
- let shellRight = axisX + plateHalf;
3115
- if (hairShell) {
3116
- if (opts.headEdges && opts.headEdges.left.length > 0 && opts.headEdges.right.length > 0) {
3117
- shellLeft = Math.min(...opts.headEdges.left.map((e) => e.x));
3118
- shellRight = Math.max(...opts.headEdges.right.map((e) => e.x)) + 1;
3119
- } else {
3120
- shellLeft = axisX - headHalf;
3121
- shellRight = axisX + headHalf;
3117
+ return lo;
3118
+ }
3119
+ function paints(runs, px) {
3120
+ for (let k = 0; k < runs.length; k += 2) {
3121
+ if (runs[k] <= px && px + 1 <= runs[k + 1]) return true;
3122
+ }
3123
+ return false;
3124
+ }
3125
+ function outerRide(m, inner, coreAt, followAt) {
3126
+ const f = m.frame;
3127
+ const ys = m.hairFrontGrid?.ys;
3128
+ if (ys === void 0) return void 0;
3129
+ const top = ys[0];
3130
+ if (f.hairRuns("hair_front", top) === void 0 || f.hairRuns("hair_back", top) === void 0) {
3131
+ return void 0;
3132
+ }
3133
+ const drift = Math.abs(m.hairBack);
3134
+ const sides = [-1, 1];
3135
+ const n = ys.length;
3136
+ const count = Math.round(top - ys[n - 1]);
3137
+ const below = (NOD.hairFront.down - NOD.hairBack.down) * f.hh;
3138
+ const above = (NOD.hairBack.up - NOD.hairFront.up) * f.hh;
3139
+ const j0 = Math.floor(-above);
3140
+ const backRows = Array.from(
3141
+ { length: Math.ceil(count + below) - j0 },
3142
+ (_, j) => f.hairRuns("hair_back", top - (j + j0) - 0.5)
3143
+ );
3144
+ const offset = sides.map(() => new Float64Array(count).fill(NaN));
3145
+ const far = sides.map(() => new Float64Array(count));
3146
+ const near = sides.map(() => new Float64Array(count));
3147
+ for (let i = 0; i < count; i++) {
3148
+ const y = top - i - 0.5;
3149
+ const front = f.hairRuns("hair_front", y);
3150
+ sides.forEach((side, k) => {
3151
+ const edge = outerEdge(front, f.axisX, side);
3152
+ if (edge === void 0) return;
3153
+ let outward = Infinity;
3154
+ let inward = Infinity;
3155
+ for (let j = Math.floor(i - above); j < Math.ceil(i + 1 + below); j++) {
3156
+ const c = runReach(backRows[j - j0], edge, side);
3157
+ outward = Math.min(outward, c.outward);
3158
+ inward = Math.min(inward, c.inward);
3159
+ }
3160
+ offset[k][i] = side * (edge - f.axisX);
3161
+ far[k][i] = m.shellScale * outward - drift;
3162
+ near[k][i] = m.shellScale * inward - drift;
3163
+ });
3164
+ }
3165
+ const end = new Float64Array(n * 2);
3166
+ const lift = new Float64Array(n * 4);
3167
+ for (let r = 0; r < n; r++) {
3168
+ const i0 = Math.max(0, Math.floor(top - ys[Math.max(0, r - 1)]));
3169
+ const i1 = Math.min(count, Math.ceil(top - ys[Math.min(n - 1, r + 1)]));
3170
+ sides.forEach((side, k) => {
3171
+ let d = Infinity;
3172
+ let allowFar = Infinity;
3173
+ let allowNear = Infinity;
3174
+ for (let i = i0; i < i1; i++) {
3175
+ if (Number.isNaN(offset[k][i])) continue;
3176
+ d = Math.min(d, offset[k][i]);
3177
+ allowFar = Math.min(allowFar, far[k][i]);
3178
+ allowNear = Math.min(allowNear, near[k][i]);
3179
+ }
3180
+ end[r * 2 + k] = d;
3181
+ if (d === Infinity || d <= inner) return;
3182
+ const x = f.axisX + side * d;
3183
+ sides.forEach((s, j) => {
3184
+ const allow = Math.min(
3185
+ coreAt(x, ys[r], s),
3186
+ s === side ? allowFar : allowNear
3187
+ );
3188
+ lift[(r * 2 + k) * 2 + j] = Math.max(0, allow - followAt(x, ys[r], s));
3189
+ });
3190
+ });
3191
+ }
3192
+ const rowRide = (r, k, j, d) => {
3193
+ const v = lift[(r * 2 + k) * 2 + j];
3194
+ return v === 0 ? 0 : v * Math.min(1, (d - inner) / (end[r * 2 + k] - inner));
3195
+ };
3196
+ return (x, y, s) => {
3197
+ const d = Math.abs(x - f.axisX);
3198
+ if (s === 0 || d <= inner) return 0;
3199
+ const k = x < f.axisX ? 0 : 1;
3200
+ const j = s < 0 ? 0 : 1;
3201
+ let v = clamp((top - y) / (top - ys[n - 1]) * (n - 1), 0, n - 1);
3202
+ if (Math.abs(v - Math.round(v)) < 1e-3) v = Math.round(v);
3203
+ const r = Math.min(n - 2, Math.floor(v));
3204
+ const t = v - r;
3205
+ return rowRide(r, k, j, d) * (1 - t) + rowRide(r + 1, k, j, d) * t;
3206
+ };
3207
+ }
3208
+ function familyField(m, family) {
3209
+ switch (family) {
3210
+ case "face":
3211
+ return faceField(m);
3212
+ case "eye_L":
3213
+ case "eye_R":
3214
+ return eyeField(m, m.boxes[family]);
3215
+ case "brow_L":
3216
+ case "brow_R":
3217
+ return browField(m, m.boxes[family]);
3218
+ case "nose":
3219
+ return noseField(m);
3220
+ case "mouth":
3221
+ return mouthField(m);
3222
+ case "hair_front":
3223
+ return hairFrontField(m);
3224
+ case "hair_back":
3225
+ return hairField(m, m.hairBack, NOD.hairBack);
3226
+ case "body":
3227
+ return () => [0, 0];
3228
+ }
3229
+ }
3230
+
3231
+ // src/auto-rig/context.ts
3232
+ function sidePair(byRole, a, b) {
3233
+ const la = byRole.get(a);
3234
+ const lb = byRole.get(b);
3235
+ if (la === void 0 || lb === void 0) return void 0;
3236
+ const [lo, hi] = [
3237
+ { role: a, box: boxOfLayer(la) },
3238
+ { role: b, box: boxOfLayer(lb) }
3239
+ ].sort((p, q) => cx(p.box) - cx(q.box));
3240
+ return { left: lo, right: hi };
3241
+ }
3242
+ function sideIrises(byRole) {
3243
+ const pair = sidePair(byRole, "iris_L", "iris_R");
3244
+ return pair === void 0 ? {} : { left: pair.left.box, right: pair.right.box };
3245
+ }
3246
+ function solveContext(frame, byRole, options, hasNose) {
3247
+ const box = (role) => boxOfLayer(byRole.get(role));
3248
+ const row = (s, strand2, sideSign) => {
3249
+ if (strand2 !== void 0) {
3250
+ const lo = sideSign < 0 ? strand2.irisOuter : strand2.irisInner;
3251
+ const hi = sideSign < 0 ? strand2.irisInner : strand2.irisOuter;
3252
+ return { role: s.role, lo, hi, y: strand2.y };
3122
3253
  }
3254
+ const inset = bw(s.box) > 4 ? 1 : 0;
3255
+ return {
3256
+ role: s.role,
3257
+ lo: s.box.x0 + inset,
3258
+ hi: s.box.x1 - inset,
3259
+ y: cy(s.box)
3260
+ };
3261
+ };
3262
+ const eyes = sidePair(byRole, "eye_L", "eye_R");
3263
+ const irises = sidePair(byRole, "iris_L", "iris_R");
3264
+ const pair = irises ?? eyes;
3265
+ const strand = irises ? options.strandEdges : void 0;
3266
+ const left = row(pair.left, strand?.left, -1);
3267
+ const right = row(pair.right, strand?.right, 1);
3268
+ let candidates;
3269
+ const edges = options.headEdges;
3270
+ if (frame.hairShell && edges && edges.left.length > 0 && edges.right.length > 0) {
3271
+ candidates = {
3272
+ left: edges.left.map((e) => ({ role: e.role, x: e.x })),
3273
+ // A right edge names its outermost pixel column; the boundary is one on.
3274
+ right: edges.right.map((e) => ({ role: e.role, x: e.x + 1 }))
3275
+ };
3276
+ } else if (frame.hairShell) {
3277
+ candidates = {
3278
+ left: [{ role: "hair_back", x: frame.shellLeft }],
3279
+ right: [{ role: "hair_back", x: frame.shellRight }]
3280
+ };
3281
+ } else {
3282
+ const w = frame.paintedHalfAt(frame.eyeY);
3283
+ candidates = {
3284
+ left: [{ role: "face", x: frame.axisX - w }],
3285
+ right: [{ role: "face", x: frame.axisX + w }]
3286
+ };
3287
+ }
3288
+ const restHalf = (Math.max(...candidates.right.map((c) => c.x)) - Math.min(...candidates.left.map((c) => c.x))) / 2;
3289
+ const nose = byRole.get("nose");
3290
+ let noseAt;
3291
+ let noseBox;
3292
+ if (hasNose && nose !== void 0) {
3293
+ noseBox = nose.denseCore ? boxOfPixels(nose.denseCore, nose.canvasW, nose.canvasH) : boxOfLayer(nose);
3294
+ noseAt = { x: cx(noseBox), y: cy(noseBox) };
3123
3295
  }
3296
+ const mouthBox = byRole.has("mouth_open") ? unionBoxes([box("mouth"), box("mouth_open")]) : box("mouth");
3297
+ const m = box("mouth");
3124
3298
  return {
3125
- canvasW,
3126
- canvasH,
3127
- face,
3128
- axisX,
3129
- eyeY,
3130
- mouthY,
3131
- chinY,
3132
- hh,
3133
- paintedHalfAt,
3134
- wMax,
3135
- headMax,
3136
- hasProfile: profile !== null,
3137
- neck,
3138
- ears,
3139
- cutAt,
3140
- cheekY: eyeY - 0.5 * (eyeY - chinY),
3141
- jawY: eyeY - 0.8 * (eyeY - chinY),
3142
- shellLeft,
3143
- shellRight,
3144
- hairShell,
3145
- holdBase: headHalf ?? plateHalf
3299
+ frame,
3300
+ left,
3301
+ right,
3302
+ candidates,
3303
+ restHalf,
3304
+ strand,
3305
+ hasHairFront: byRole.has("hair_front"),
3306
+ eyeBoxes: { left: eyes.left.box, right: eyes.right.box },
3307
+ noseAt,
3308
+ noseBox,
3309
+ mouthAt: { x: cx(m), y: cy(m) },
3310
+ mouthBox
3311
+ };
3312
+ }
3313
+ function renderedLander(turn, parts, grids, from = /* @__PURE__ */ new Map()) {
3314
+ const analytic = /* @__PURE__ */ new Map();
3315
+ const landed = /* @__PURE__ */ new Map();
3316
+ const field = (role) => {
3317
+ const family = roleSpec(role).family;
3318
+ let f = analytic.get(family);
3319
+ if (f === void 0) analytic.set(family, f = familyField(turn, family));
3320
+ return f;
3321
+ };
3322
+ return (role, x, y, ax) => {
3323
+ const family = roleSpec(role).family;
3324
+ const part = parts.find((p) => p.id === role);
3325
+ const g = grids.get(family);
3326
+ const warp = part?.warps?.find((w) => w.parameter === import_format3.StandardParameter.AngleX);
3327
+ if (part === void 0 || part.mesh === void 0 || !g && !warp) {
3328
+ return x + field(role)(x, y, ax, 0)[0];
3329
+ }
3330
+ const box = {
3331
+ x0: part.transform.x - part.width / 2,
3332
+ x1: part.transform.x + part.width / 2,
3333
+ y0: part.transform.y - part.height / 2,
3334
+ y1: part.transform.y + part.height / 2
3335
+ };
3336
+ const rest = meshPoints(part.mesh, box);
3337
+ const key = `${role}@${ax}`;
3338
+ let xs = landed.get(key);
3339
+ if (xs === void 0) {
3340
+ if (g) {
3341
+ const offsets = keyformAt(g.keyforms, ax);
3342
+ xs = rest.map(([px, py]) => land(g.lattice, offsets, px, py)[0]);
3343
+ } else {
3344
+ const k = warp.keyforms;
3345
+ const offsets = blendStops(
3346
+ k.map((kf) => kf.value),
3347
+ k.map((kf) => kf.offsets),
3348
+ ax
3349
+ );
3350
+ xs = rest.map(([px], i) => px + offsets[2 * i] * bw(box));
3351
+ }
3352
+ landed.set(key, xs);
3353
+ }
3354
+ const idx = part.mesh.indices;
3355
+ for (let t = from.get(role) ?? 0; t < idx.length; t += 3) {
3356
+ const [a, b, c] = [idx[t], idx[t + 1], idx[t + 2]];
3357
+ const [ax0, ay0] = rest[a];
3358
+ const [bx0, by0] = rest[b];
3359
+ const [cx0, cy0] = rest[c];
3360
+ const d = (by0 - cy0) * (ax0 - cx0) + (cx0 - bx0) * (ay0 - cy0);
3361
+ if (Math.abs(d) < 1e-9) continue;
3362
+ const u = ((by0 - cy0) * (x - cx0) + (cx0 - bx0) * (y - cy0)) / d;
3363
+ const v = ((cy0 - ay0) * (x - cx0) + (ax0 - cx0) * (y - cy0)) / d;
3364
+ const w = 1 - u - v;
3365
+ if (u < -1e-6 || v < -1e-6 || w < -1e-6) continue;
3366
+ return u * xs[a] + v * xs[b] + w * xs[c];
3367
+ }
3368
+ return x + field(role)(x, y, ax, 0)[0];
3146
3369
  };
3147
3370
  }
3148
3371
 
@@ -4021,14 +4244,18 @@ function generateIkiFromLayerSet(layers, canvas, options = {}) {
4021
4244
  };
4022
4245
  const solveFrame = buildHeadFrame(layers, frameOptions);
4023
4246
  const ctx = solveContext(solveFrame, byRole, turnOptions, hasNose);
4247
+ const boxes = Object.fromEntries(
4248
+ ["eye_L", "eye_R", "brow_L", "brow_R"].filter(has).map((r) => [r, box(r)])
4249
+ );
4024
4250
  const landmarks = {
4025
- boxes: Object.fromEntries(
4026
- ["eye_L", "eye_R", "brow_L", "brow_R"].filter(has).map((r) => [r, box(r)])
4027
- ),
4251
+ boxes,
4028
4252
  noseAt: ctx.noseAt,
4029
4253
  noseTopY: ctx.noseBox?.y1,
4030
4254
  mouthAt: ctx.mouthAt,
4031
- hairTop: has("hair_front") ? box("hair_front").y1 : solveFrame.face.y1
4255
+ hairTop: has("hair_front") ? box("hair_front").y1 : solveFrame.face.y1,
4256
+ // The field reads the grid the mesh is built on (the rebuilt frame below
4257
+ // differs only in the jaw's cut, which the grid does not read).
4258
+ hairFrontGrid: has("hair_front") ? hairFrontGrid(box("hair_front"), solveFrame, boxes) : void 0
4032
4259
  };
4033
4260
  const build = (q) => buildTurn(solveFrame, landmarks, q);
4034
4261
  const fit = fitTurn(ctx, targets, style, build);
@@ -4050,7 +4277,8 @@ function generateIkiFromLayerSet(layers, canvas, options = {}) {
4050
4277
  parts.length,
4051
4278
  frame,
4052
4279
  byRole,
4053
- style
4280
+ style,
4281
+ landmarks.hairFrontGrid
4054
4282
  );
4055
4283
  if (built.region !== void 0) {
4056
4284
  regionOf = built.region;
@@ -4141,7 +4369,7 @@ function plateFields(turn, region, head) {
4141
4369
  const neck = neckField(turn);
4142
4370
  return (i) => region[i] === "ear" ? ear : region[i] === "neck" ? neck : head;
4143
4371
  }
4144
- function buildPart(spec, b, order, frame, byRole, style) {
4372
+ function buildPart(spec, b, order, frame, byRole, style, frontGrid) {
4145
4373
  const box = (role) => boxOfLayer(byRole.get(role));
4146
4374
  const part = {
4147
4375
  id: spec.role,
@@ -4164,30 +4392,15 @@ function buildPart(spec, b, order, frame, byRole, style) {
4164
4392
  mesh = fm.mesh;
4165
4393
  region = fm.region;
4166
4394
  headStart = fm.headStart;
4395
+ } else if (spec.role === "hair_front") {
4396
+ mesh = columnMesh(b, frontGrid);
4167
4397
  } else if (spec.family !== "body") {
4168
4398
  const cells = MESH_CELLS[spec.role] ?? FEATURE_MESH_CELLS;
4169
4399
  const px = Math.min(cells.px, meshScale(spec.role, frame));
4170
- const cols = cellsFor(bw(b), px, cells.min, cells.max);
4171
- const rows = cellsFor(bh(b), px, cells.min, cells.max);
4172
- if (spec.role === "hair_front") {
4173
- const boxes = Object.fromEntries(
4174
- ["eye_L", "eye_R"].filter((r) => byRole.has(r)).map((r) => [r, box(r)])
4175
- );
4176
- const { inner, left, right } = hairFrontBends(frame, boxes);
4177
- mesh = columnMesh(
4178
- b,
4179
- [
4180
- frame.axisX - left,
4181
- frame.axisX - inner,
4182
- frame.axisX + inner,
4183
- frame.axisX + right
4184
- ],
4185
- cols,
4186
- rows
4187
- );
4188
- } else {
4189
- mesh = gridMesh(cols, rows);
4190
- }
4400
+ mesh = gridMesh(
4401
+ cellsFor(bw(b), px, cells.min, cells.max),
4402
+ cellsFor(bh(b), px, cells.min, cells.max)
4403
+ );
4191
4404
  }
4192
4405
  if (mesh !== void 0) part.mesh = mesh;
4193
4406
  const side = spec.role.endsWith("_L") ? "L" : "R";
@@ -4306,16 +4519,35 @@ function buildPart(spec, b, order, frame, byRole, style) {
4306
4519
  headStart
4307
4520
  };
4308
4521
  }
4309
- function columnMesh(b, at, cols, rows) {
4522
+ function hairFrontGrid(b, frame, boxes) {
4523
+ const cells = MESH_CELLS.hair_front;
4524
+ const px = Math.min(cells.px, meshScale("hair_front", frame));
4525
+ const cols = cellsFor(bw(b), px, cells.min, cells.max);
4526
+ const rows = cellsFor(bh(b), px, cells.min, cells.max);
4527
+ const { inner, left, right } = hairFrontBends(frame, boxes);
4310
4528
  const step = bw(b) / cols;
4311
4529
  const xs = Array.from({ length: cols + 1 }, (_, c) => b.x0 + c * step);
4312
- for (const x of at) {
4530
+ for (const x of [
4531
+ frame.axisX - left,
4532
+ frame.axisX - inner,
4533
+ frame.axisX + inner,
4534
+ frame.axisX + right
4535
+ ]) {
4313
4536
  if (x <= b.x0 + 0.25 || x >= b.x1 - 0.25) continue;
4314
4537
  const near = xs.findIndex((v) => Math.abs(v - x) < 0.3 * step);
4315
4538
  if (near > 0 && near < xs.length - 1) xs[near] = x;
4316
4539
  else if (near < 0) xs.push(x);
4317
4540
  }
4318
4541
  xs.sort((p, q) => p - q);
4542
+ const ys = Array.from(
4543
+ { length: rows + 1 },
4544
+ (_, r) => b.y1 - bh(b) * r / rows
4545
+ );
4546
+ return { xs, ys };
4547
+ }
4548
+ function columnMesh(b, grid) {
4549
+ const { xs } = grid;
4550
+ const rows = grid.ys.length - 1;
4319
4551
  const vertices = [];
4320
4552
  const uvs = [];
4321
4553
  for (let r = 0; r <= rows; r++) {