@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.mjs CHANGED
@@ -1559,7 +1559,6 @@ function createLayerSetMeasurer(canvas) {
1559
1559
  const opaque = new Uint8Array(canvasW * canvasH);
1560
1560
  const layerInputs = [];
1561
1561
  const rowSpansByRole = [];
1562
- let hairFrontMask;
1563
1562
  function add({ role, fileName, rgba }) {
1564
1563
  if (rgba.length !== rgbaLength) {
1565
1564
  throw new Error(
@@ -1570,19 +1569,29 @@ function createLayerSetMeasurer(canvas) {
1570
1569
  if (bbox === null) return null;
1571
1570
  const rowLeft = new Int32Array(canvasH).fill(canvasW);
1572
1571
  const rowRight = new Int32Array(canvasH).fill(-1);
1573
- const ownMask = role === "hair_front" ? new Uint8Array(canvasW * canvasH) : void 0;
1572
+ const rowRuns = role === "hair_front" || role === "hair_back" ? [] : void 0;
1574
1573
  for (let y = 0; y < canvasH; y++) {
1574
+ let runs;
1575
+ if (rowRuns !== void 0 && y >= bbox.y && y < bbox.y + bbox.h) {
1576
+ runs = [];
1577
+ rowRuns.push(runs);
1578
+ }
1579
+ let inRun = false;
1575
1580
  for (let x = 0; x < canvasW; x++) {
1576
1581
  const p = y * canvasW + x;
1577
- if (rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
1582
+ const on = rgba[p * 4 + 3] >= ALPHA_OPAQUE;
1583
+ if (runs !== void 0 && on !== inRun) {
1584
+ runs.push(x);
1585
+ inRun = on;
1586
+ }
1587
+ if (!on) continue;
1578
1588
  opaque[p] = 1;
1579
- if (ownMask !== void 0) ownMask[p] = 1;
1580
1589
  if (x < rowLeft[y]) rowLeft[y] = x;
1581
1590
  if (x > rowRight[y]) rowRight[y] = x;
1582
1591
  }
1592
+ if (runs !== void 0 && inRun) runs.push(canvasW);
1583
1593
  }
1584
1594
  rowSpansByRole.push({ role, rowLeft, rowRight });
1585
- if (ownMask !== void 0) hairFrontMask = ownMask;
1586
1595
  const layer = {
1587
1596
  role,
1588
1597
  fileName,
@@ -1592,6 +1601,7 @@ function createLayerSetMeasurer(canvas) {
1592
1601
  cropW: bbox.w,
1593
1602
  cropH: bbox.h
1594
1603
  };
1604
+ if (rowRuns !== void 0) layer.rowRuns = rowRuns;
1595
1605
  if (role === "face") {
1596
1606
  const rowHalfWidths = [];
1597
1607
  for (let y = bbox.y; y < bbox.y + bbox.h; y++) {
@@ -1636,8 +1646,10 @@ function createLayerSetMeasurer(canvas) {
1636
1646
  }
1637
1647
  let strandEdges;
1638
1648
  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));
1639
- if (hairFrontMask !== void 0 && irisLayers.length === 2) {
1640
- const mask = hairFrontMask;
1649
+ const hairFront = layerInputs.find((l) => l.role === "hair_front");
1650
+ const hairRuns = hairFront?.rowRuns;
1651
+ if (hairFront !== void 0 && hairRuns !== void 0 && irisLayers.length === 2) {
1652
+ const hairTop = hairFront.bbox.y;
1641
1653
  const half = canvasW / 2;
1642
1654
  const strandOn = (iris, other, side) => {
1643
1655
  const r = Math.floor(iris.bbox.y + iris.bbox.h / 2);
@@ -1648,15 +1660,11 @@ function createLayerSetMeasurer(canvas) {
1648
1660
  if (span2.rowRight[r] < span2.rowLeft[r]) return void 0;
1649
1661
  const minRun = STRAND_MIN_RUN_FRACTION * (span2.rowRight[r] + 1 - span2.rowLeft[r]);
1650
1662
  const strand = new Uint8Array(canvasW);
1651
- for (let a = 0; a < canvasW; ) {
1652
- if (mask[r * canvasW + a] !== 1) {
1653
- a++;
1654
- continue;
1663
+ const runs = r >= hairTop && r < hairTop + hairRuns.length ? hairRuns[r - hairTop] : [];
1664
+ for (let k = 0; k < runs.length; k += 2) {
1665
+ if (runs[k + 1] - runs[k] >= minRun) {
1666
+ strand.fill(1, runs[k], runs[k + 1]);
1655
1667
  }
1656
- let b = a;
1657
- while (b < canvasW && mask[r * canvasW + b] === 1) b++;
1658
- if (b - a >= minRun) strand.fill(1, a, b);
1659
- a = b;
1660
1668
  }
1661
1669
  const strandAt = (x2) => x2 >= 0 && x2 < canvasW && strand[x2] === 1;
1662
1670
  const outerBoundary = (x2) => (side < 0 ? x2 : x2 + 1) - half;
@@ -2102,6 +2110,23 @@ function checkLayers(layers, canvas) {
2102
2110
  }
2103
2111
  });
2104
2112
  }
2113
+ if (l.rowRuns !== void 0) {
2114
+ const rows = l.rowRuns;
2115
+ if (!Array.isArray(rows) || rows.length !== h) {
2116
+ throw new Error(
2117
+ `${at}: rowRuns must have one entry per crop row (${h})`
2118
+ );
2119
+ }
2120
+ rows.forEach((runs, i) => {
2121
+ if (!Array.isArray(runs) || runs.length % 2 !== 0 || !runs.every(
2122
+ (c, k) => Number.isInteger(c) && c >= x && c <= x + w && (k === 0 || c > runs[k - 1])
2123
+ )) {
2124
+ throw new Error(
2125
+ `${at}: rowRuns[${i}] ${JSON.stringify(runs)} is not an even-length, strictly increasing list of columns inside its crop (${x}..${x + w})`
2126
+ );
2127
+ }
2128
+ });
2129
+ }
2105
2130
  if (l.jawRows !== void 0) {
2106
2131
  const cols = l.jawRows;
2107
2132
  if (!Array.isArray(cols) || cols.length !== w) {
@@ -2359,7 +2384,11 @@ var TURN = {
2359
2384
  * its translation (bangs 1.18×, side locks at the eye row ≈ 1.0×). */
2360
2385
  hairFollow: 1.1,
2361
2386
  /** The head's outline at the eye row holds (−0.014 / −0.004 hh): where the
2362
- * front hair draws it, its outer edge follows the face this much. */
2387
+ * front hair draws it, its outer edge follows the face this much on a row
2388
+ * where no back hair paints behind that edge. Where back hair does, the
2389
+ * edge rides further, as far as that back hair stays behind it through
2390
+ * the turn and the nod, up to the face's follow there (`hairFollow`;
2391
+ * over the far eye's rows, as far as that eye's outer corner goes). */
2363
2392
  outlineFollow: 0,
2364
2393
  /** The back hair's slight counter-motion. */
2365
2394
  hairBack: -0.027,
@@ -2379,6 +2408,13 @@ var NOD = {
2379
2408
  nose: { up: -0.207, down: 0.193 },
2380
2409
  mouth: { up: -0.172, down: 0.177 },
2381
2410
  hairFront: { up: -0.11, down: 0.189 },
2411
+ /** The front hair's cap top. The samples' cap top moves 1.215× their face's
2412
+ * centroid looking down (Mao 1.236, Haru 1.626, Hiyori 1.194, Natori
2413
+ * 0.730) and 0.415× looking up (0.385, 0.430, 0.400, 0.487); each ratio
2414
+ * times the plate's own nod halfway between its top and the chin (0.1435
2415
+ * down, 0.084 up). Looking down, the rig slides it only as far as the
2416
+ * back hair covers the crown behind it. */
2417
+ hairFrontTop: { up: -0.035, down: 0.174 },
2382
2418
  hairBack: { up: -0.011, down: 0.025 }
2383
2419
  };
2384
2420
  var ROLL_DEG = 10;
@@ -2426,433 +2462,82 @@ function resolveStyle(style, check) {
2426
2462
  };
2427
2463
  }
2428
2464
 
2429
- // src/auto-rig/fields.ts
2430
- var NOSE_TILT_DEG = 6;
2431
- function nodDy(v, ay, hh) {
2432
- const up = Math.max(0, ay) / 30;
2433
- const down = Math.max(0, -ay) / 30;
2434
- return -(up * v.up + down * v.down) * hh;
2435
- }
2436
- function spin(x, y, ox, oy, a) {
2437
- if (a === 0) return [0, 0];
2438
- const px = x - ox;
2439
- const py = y - oy;
2440
- return [
2441
- px * Math.cos(a) - py * Math.sin(a) - px,
2442
- px * Math.sin(a) + py * Math.cos(a) - py
2443
- ];
2444
- }
2445
- function faceField(m) {
2446
- const f = m.frame;
2447
- return (x, y, ax, ay) => {
2448
- const s = Math.sign(ax);
2449
- const a = Math.abs(ax) / 30;
2450
- const chin = smoothstep(f.mouthY, f.chinY, y);
2451
- const width = m.widthUpper + (m.widthJaw - m.widthUpper) * smoothstep(f.cheekY, f.jawY, y);
2452
- const shell = f.hairShell ? 0 : m.shellScale - 1;
2453
- const dx = s * a * (m.face + m.chinLead * chin) + a * (width - 1 + shell) * (x - f.axisX);
2454
- const t = Math.min(
2455
- 1,
2456
- Math.max(0, (f.face.y1 - y) / Math.max(1, f.face.y1 - f.chinY))
2457
- );
2458
- const v = {
2459
- up: NOD.faceTop.up + (NOD.chin.up - NOD.faceTop.up) * t,
2460
- down: NOD.faceTop.down + (NOD.chin.down - NOD.faceTop.down) * t
2461
- };
2462
- return [dx, nodDy(v, ay, f.hh)];
2463
- };
2464
- }
2465
- function earField(m) {
2466
- const f = m.frame;
2467
- const ears = f.ears;
2468
- if (ears === void 0) return () => [0, 0];
2469
- const face = faceField(m);
2470
- return (x, y, ax, ay) => {
2471
- const s = Math.sign(ax);
2472
- const a = Math.abs(ax) / 30;
2473
- const dy = face(x, y, 0, ay)[1];
2474
- if (sideOf(m, x, s) === "far") return [s * a * TURN.earFar * m.face, dy];
2475
- const d = Math.abs(x - f.axisX);
2476
- const attach = ears.attachAt(y);
2477
- if (d <= attach) return [face(x, y, ax, 0)[0], dy];
2478
- const root = face(f.axisX + Math.sign(x - f.axisX) * attach, y, ax, 0)[0];
2479
- const rim = s * a * TURN.earNear * m.face;
2480
- const t = Math.min(1, (d - attach) / Math.max(1, ears.outer - attach));
2481
- return [root + (rim - root) * t, dy];
2482
- };
2483
- }
2484
- function chinSlide(m) {
2485
- const f = m.frame;
2486
- return Math.abs(faceField(m)(f.axisX, f.chinY, 30, 0)[0]);
2487
- }
2488
- var NECK_SHADE_MAX = 0.45;
2489
- var NECK_SHADE_FULL = 0.2;
2490
- function neckField(m) {
2491
- const f = m.frame;
2492
- const nk = f.neck;
2493
- if (nk === void 0) return () => [0, 0];
2494
- const face = faceField(m);
2495
- const full = chinSlide(m);
2496
- const share = Math.min(1, NECK_SHADE_MAX * nk.waist / Math.max(1e-6, full));
2497
- const bottom = f.face.y0;
2498
- const tip = f.cutAt(f.axisX);
2499
- const top = tip - NECK_SHADE_FULL * (tip - bottom);
2500
- return (x, y, ax) => {
2501
- const wy = Math.min(
2502
- 1,
2503
- Math.max(0, (y - bottom) / Math.max(1, top - bottom))
2504
- );
2505
- const wx = Math.max(0, 1 - Math.abs(x - f.axisX) / nk.waist);
2506
- return [share * face(f.axisX, f.chinY, ax, 0)[0] * wx * wy, 0];
2507
- };
2465
+ // src/auto-rig/head.ts
2466
+ function outerEdge(runs, axisX, side) {
2467
+ if (runs.length === 0) return void 0;
2468
+ const x = side > 0 ? runs[runs.length - 1] : runs[0];
2469
+ return side * (x - axisX) > 0 ? x : void 0;
2508
2470
  }
2509
- function sideOf(m, x, s) {
2510
- return s * (x - m.frame.axisX) > 0 ? "far" : "near";
2471
+ function runReach(runs, x, side) {
2472
+ const px = side > 0 ? x - 1 : x;
2473
+ for (let k = 0; k < runs.length; k += 2) {
2474
+ const [a, b] = [runs[k], runs[k + 1]];
2475
+ if (a <= px && px + 1 <= b) {
2476
+ return side > 0 ? { inward: x - a, outward: b - x } : { inward: b - x, outward: x - a };
2477
+ }
2478
+ }
2479
+ return { inward: 0, outward: 0 };
2511
2480
  }
2512
- function eyeField(m, box) {
2513
- const xe = cx(box);
2514
- const ye = cy(box);
2515
- const hh = m.frame.hh;
2516
- return (x, y, ax, ay) => {
2517
- const s = Math.sign(ax);
2518
- const a = Math.abs(ax) / 30;
2519
- const side = sideOf(m, xe, s);
2520
- const dx = s * a * m.eye[side] + a * (m.eyeScale[side] - 1) * (x - xe);
2521
- const up = Math.max(0, ay) / 30;
2522
- const down = Math.max(0, -ay) / 30;
2523
- const squash = up * (NOD.eyeHeight.up - 1) + down * (NOD.eyeHeight.down - 1);
2524
- return [dx, nodDy(NOD.eye, ay, hh) + squash * (y - ye)];
2525
- };
2481
+ function filledProfile(rows) {
2482
+ if (rows === void 0 || !rows.some((v) => v > 0)) return null;
2483
+ const out = rows.slice();
2484
+ const first = out.findIndex((v) => v > 0);
2485
+ for (let i = 0; i < first; i++) out[i] = out[first];
2486
+ for (let i = first + 1; i < out.length; i++)
2487
+ if (!(out[i] > 0)) out[i] = out[i - 1];
2488
+ return out;
2526
2489
  }
2527
- function browField(m, box) {
2528
- const xb = cx(box);
2529
- return (_x, _y, ax, ay) => {
2530
- const s = Math.sign(ax);
2531
- const a = Math.abs(ax) / 30;
2532
- return [s * a * m.brow[sideOf(m, xb, s)], nodDy(NOD.brow, ay, m.frame.hh)];
2533
- };
2490
+ var smoothProfile = (rows) => boxSmooth(rows, Math.max(1, Math.round(rows.length * 0.01)));
2491
+ var EAR_STEP = 0.03;
2492
+ var EAR_ROWS = 3;
2493
+ function findEars(raw, wMax, firstRow, lastRow, minRows, maxRows) {
2494
+ const step = Math.max(4, EAR_STEP * wMax);
2495
+ for (let t = Math.max(firstRow, EAR_ROWS); t < lastRow; t++) {
2496
+ if (raw[t] - raw[t - EAR_ROWS] < step) continue;
2497
+ for (let b = t + minRows; b <= Math.min(lastRow, t + maxRows); b++) {
2498
+ if (raw[b - EAR_ROWS] - raw[b] >= step) {
2499
+ return { top: t - EAR_ROWS, bottom: b };
2500
+ }
2501
+ }
2502
+ return void 0;
2503
+ }
2504
+ return void 0;
2534
2505
  }
2535
- function noseField(m) {
2536
- const at = m.noseAt;
2537
- const top = m.noseTopY ?? at.y;
2538
- return (x, y, ax, ay) => {
2539
- const s = Math.sign(ax);
2540
- const a = Math.abs(ax) / 30;
2541
- const [tx, ty] = spin(
2542
- x,
2543
- y,
2544
- at.x,
2545
- top,
2546
- s * a * m.tilt * NOSE_TILT_DEG * DEG
2547
- );
2548
- return [s * a * m.nose + tx, nodDy(NOD.nose, ay, m.frame.hh) + ty];
2549
- };
2506
+ function boxSmooth(v, k) {
2507
+ return v.map((_, i) => {
2508
+ let s = 0;
2509
+ let n = 0;
2510
+ for (let j = i - k; j <= i + k; j++) {
2511
+ if (j < 0 || j >= v.length) continue;
2512
+ s += v[j];
2513
+ n++;
2514
+ }
2515
+ return s / n;
2516
+ });
2550
2517
  }
2551
- function mouthField(m) {
2552
- const c = m.mouthAt;
2553
- return (x, y, ax, ay) => {
2554
- const s = Math.sign(ax);
2555
- const a = Math.abs(ax) / 30;
2556
- const wx = c.x + (x - c.x) * (1 + a * (m.mouthWidth - 1));
2557
- const [tx, ty] = spin(
2558
- wx,
2559
- y,
2560
- c.x,
2561
- c.y,
2562
- s * a * m.tilt * TURN.mouthTiltDeg * DEG
2563
- );
2564
- return [
2565
- s * a * m.mouth + wx - x + tx,
2566
- nodDy(NOD.mouth, ay, m.frame.hh) + ty
2567
- ];
2568
- };
2518
+ var CUT_MARGIN = 2;
2519
+ var CUT_LOW = 3;
2520
+ function fadeToOutline(d, nk) {
2521
+ return Math.max(0, 1 - d / nk.waist);
2569
2522
  }
2570
- function hairField(m, shift, nod) {
2571
- const f = m.frame;
2572
- return (x, _y, ax, ay) => {
2573
- const s = Math.sign(ax);
2574
- const a = Math.abs(ax) / 30;
2575
- return [
2576
- s * a * shift + a * (m.shellScale - 1) * (x - f.axisX),
2577
- nodDy(nod, ay, f.hh)
2578
- ];
2523
+ var STROKE_RUN = 4;
2524
+ var STROKE_SLACK = 6;
2525
+ var JAW_SHADE = 0.05;
2526
+ function buildHeadFrame(layers, opts) {
2527
+ const byRole = new Map(layers.map((l) => [l.role, l]));
2528
+ const faceLayer = byRole.get("face");
2529
+ const { canvasW, canvasH } = faceLayer;
2530
+ const face = boxOfLayer(faceLayer);
2531
+ const axisX = cx(face);
2532
+ const plateHalf = faceLayer.cropW / 2;
2533
+ const centreY = (role) => {
2534
+ const l = byRole.get(role);
2535
+ return l === void 0 ? void 0 : cy(boxOfLayer(l));
2579
2536
  };
2580
- }
2581
- function hairFrontBends(frame, boxes) {
2582
- const f = frame;
2583
- const eyes = [boxes.eye_L, boxes.eye_R].filter(
2584
- (e) => e !== void 0
2585
- );
2586
- const inner = Math.max(
2587
- 1,
2588
- Math.min(
2589
- f.paintedHalfAt(f.eyeY),
2590
- ...eyes.map((e) => Math.max(f.axisX - e.x0, e.x1 - f.axisX))
2591
- )
2592
- );
2593
- return {
2594
- inner,
2595
- left: Math.max(inner + 1, f.axisX - f.shellLeft),
2596
- right: Math.max(inner + 1, f.shellRight - f.axisX)
2597
- };
2598
- }
2599
- function hairFrontField(m) {
2600
- const f = m.frame;
2601
- const back = hairField(m, m.hairBack, NOD.hairBack);
2602
- const top = f.face.y1;
2603
- const eyes = [m.boxes.eye_L, m.boxes.eye_R].filter(
2604
- (e) => e !== void 0
2605
- );
2606
- const { inner, left, right } = hairFrontBends(f, m.boxes);
2607
- const eyeY = eyes.length > 0 ? eyes.reduce((t, e) => t + cy(e), 0) / eyes.length : f.eyeY;
2608
- const eyeH = eyes.length > 0 ? eyes.reduce((t, e) => t + (e.y1 - e.y0), 0) / eyes.length : 1;
2609
- return (x, y, ax, ay) => {
2610
- const s = Math.sign(ax);
2611
- const a = Math.abs(ax) / 30;
2612
- const u = s * (x - f.axisX);
2613
- const across = Math.min(1, Math.max(0, (u + inner) / (2 * inner)));
2614
- const band = 1 - smoothstep(eyeH / 2, 1.5 * eyeH, Math.abs(y - eyeY));
2615
- const core = m.hairFront + (m.hairFar - m.hairFront) * across * band;
2616
- const out = Math.min(
2617
- 1,
2618
- Math.max(
2619
- 0,
2620
- (Math.abs(x - f.axisX) - inner) / ((x < f.axisX ? left : right) - inner)
2621
- )
2622
- );
2623
- const follow = core + (m.hairOuter - core) * out;
2624
- const rx = s * a * follow + a * (m.shellScale - 1) * (x - f.axisX);
2625
- const ry = nodDy(NOD.hairFront, ay, f.hh);
2626
- const [bx, by] = back(x, y, ax, ay);
2627
- const w = 1 - smoothstep(top, Math.max(top + 1, m.hairTop), y);
2628
- return [bx + w * (rx - bx), by + w * (ry - by)];
2629
- };
2630
- }
2631
- function familyField(m, family) {
2632
- switch (family) {
2633
- case "face":
2634
- return faceField(m);
2635
- case "eye_L":
2636
- case "eye_R":
2637
- return eyeField(m, m.boxes[family]);
2638
- case "brow_L":
2639
- case "brow_R":
2640
- return browField(m, m.boxes[family]);
2641
- case "nose":
2642
- return noseField(m);
2643
- case "mouth":
2644
- return mouthField(m);
2645
- case "hair_front":
2646
- return hairFrontField(m);
2647
- case "hair_back":
2648
- return hairField(m, m.hairBack, NOD.hairBack);
2649
- case "body":
2650
- return () => [0, 0];
2651
- }
2652
- }
2653
-
2654
- // src/auto-rig/context.ts
2655
- function sidePair(byRole, a, b) {
2656
- const la = byRole.get(a);
2657
- const lb = byRole.get(b);
2658
- if (la === void 0 || lb === void 0) return void 0;
2659
- const [lo, hi] = [
2660
- { role: a, box: boxOfLayer(la) },
2661
- { role: b, box: boxOfLayer(lb) }
2662
- ].sort((p, q) => cx(p.box) - cx(q.box));
2663
- return { left: lo, right: hi };
2664
- }
2665
- function sideIrises(byRole) {
2666
- const pair = sidePair(byRole, "iris_L", "iris_R");
2667
- return pair === void 0 ? {} : { left: pair.left.box, right: pair.right.box };
2668
- }
2669
- function solveContext(frame, byRole, options, hasNose) {
2670
- const box = (role) => boxOfLayer(byRole.get(role));
2671
- const row = (s, strand2, sideSign) => {
2672
- if (strand2 !== void 0) {
2673
- const lo = sideSign < 0 ? strand2.irisOuter : strand2.irisInner;
2674
- const hi = sideSign < 0 ? strand2.irisInner : strand2.irisOuter;
2675
- return { role: s.role, lo, hi, y: strand2.y };
2676
- }
2677
- const inset = bw(s.box) > 4 ? 1 : 0;
2678
- return {
2679
- role: s.role,
2680
- lo: s.box.x0 + inset,
2681
- hi: s.box.x1 - inset,
2682
- y: cy(s.box)
2683
- };
2684
- };
2685
- const eyes = sidePair(byRole, "eye_L", "eye_R");
2686
- const irises = sidePair(byRole, "iris_L", "iris_R");
2687
- const pair = irises ?? eyes;
2688
- const strand = irises ? options.strandEdges : void 0;
2689
- const left = row(pair.left, strand?.left, -1);
2690
- const right = row(pair.right, strand?.right, 1);
2691
- let candidates;
2692
- const edges = options.headEdges;
2693
- if (frame.hairShell && edges && edges.left.length > 0 && edges.right.length > 0) {
2694
- candidates = {
2695
- left: edges.left.map((e) => ({ role: e.role, x: e.x })),
2696
- // A right edge names its outermost pixel column; the boundary is one on.
2697
- right: edges.right.map((e) => ({ role: e.role, x: e.x + 1 }))
2698
- };
2699
- } else if (frame.hairShell) {
2700
- candidates = {
2701
- left: [{ role: "hair_back", x: frame.shellLeft }],
2702
- right: [{ role: "hair_back", x: frame.shellRight }]
2703
- };
2704
- } else {
2705
- const w = frame.paintedHalfAt(frame.eyeY);
2706
- candidates = {
2707
- left: [{ role: "face", x: frame.axisX - w }],
2708
- right: [{ role: "face", x: frame.axisX + w }]
2709
- };
2710
- }
2711
- const restHalf = (Math.max(...candidates.right.map((c) => c.x)) - Math.min(...candidates.left.map((c) => c.x))) / 2;
2712
- const nose = byRole.get("nose");
2713
- let noseAt;
2714
- let noseBox;
2715
- if (hasNose && nose !== void 0) {
2716
- noseBox = nose.denseCore ? boxOfPixels(nose.denseCore, nose.canvasW, nose.canvasH) : boxOfLayer(nose);
2717
- noseAt = { x: cx(noseBox), y: cy(noseBox) };
2718
- }
2719
- const mouthBox = byRole.has("mouth_open") ? unionBoxes([box("mouth"), box("mouth_open")]) : box("mouth");
2720
- const m = box("mouth");
2721
- return {
2722
- frame,
2723
- left,
2724
- right,
2725
- candidates,
2726
- restHalf,
2727
- strand,
2728
- hasHairFront: byRole.has("hair_front"),
2729
- eyeBoxes: { left: eyes.left.box, right: eyes.right.box },
2730
- noseAt,
2731
- noseBox,
2732
- mouthAt: { x: cx(m), y: cy(m) },
2733
- mouthBox
2734
- };
2735
- }
2736
- function renderedLander(turn, parts, grids, from = /* @__PURE__ */ new Map()) {
2737
- const analytic = /* @__PURE__ */ new Map();
2738
- const landed = /* @__PURE__ */ new Map();
2739
- const field = (role) => {
2740
- const family = roleSpec(role).family;
2741
- let f = analytic.get(family);
2742
- if (f === void 0) analytic.set(family, f = familyField(turn, family));
2743
- return f;
2744
- };
2745
- return (role, x, y, ax) => {
2746
- const family = roleSpec(role).family;
2747
- const part = parts.find((p) => p.id === role);
2748
- const g = grids.get(family);
2749
- const warp = part?.warps?.find((w) => w.parameter === P.AngleX);
2750
- if (part === void 0 || part.mesh === void 0 || !g && !warp) {
2751
- return x + field(role)(x, y, ax, 0)[0];
2752
- }
2753
- const box = {
2754
- x0: part.transform.x - part.width / 2,
2755
- x1: part.transform.x + part.width / 2,
2756
- y0: part.transform.y - part.height / 2,
2757
- y1: part.transform.y + part.height / 2
2758
- };
2759
- const rest = meshPoints(part.mesh, box);
2760
- const key = `${role}@${ax}`;
2761
- let xs = landed.get(key);
2762
- if (xs === void 0) {
2763
- if (g) {
2764
- const offsets = keyformAt(g.keyforms, ax);
2765
- xs = rest.map(([px, py]) => land(g.lattice, offsets, px, py)[0]);
2766
- } else {
2767
- const k = warp.keyforms;
2768
- const offsets = blendStops(
2769
- k.map((kf) => kf.value),
2770
- k.map((kf) => kf.offsets),
2771
- ax
2772
- );
2773
- xs = rest.map(([px], i) => px + offsets[2 * i] * bw(box));
2774
- }
2775
- landed.set(key, xs);
2776
- }
2777
- const idx = part.mesh.indices;
2778
- for (let t = from.get(role) ?? 0; t < idx.length; t += 3) {
2779
- const [a, b, c] = [idx[t], idx[t + 1], idx[t + 2]];
2780
- const [ax0, ay0] = rest[a];
2781
- const [bx0, by0] = rest[b];
2782
- const [cx0, cy0] = rest[c];
2783
- const d = (by0 - cy0) * (ax0 - cx0) + (cx0 - bx0) * (ay0 - cy0);
2784
- if (Math.abs(d) < 1e-9) continue;
2785
- const u = ((by0 - cy0) * (x - cx0) + (cx0 - bx0) * (y - cy0)) / d;
2786
- const v = ((cy0 - ay0) * (x - cx0) + (ax0 - cx0) * (y - cy0)) / d;
2787
- const w = 1 - u - v;
2788
- if (u < -1e-6 || v < -1e-6 || w < -1e-6) continue;
2789
- return u * xs[a] + v * xs[b] + w * xs[c];
2790
- }
2791
- return x + field(role)(x, y, ax, 0)[0];
2792
- };
2793
- }
2794
-
2795
- // src/auto-rig/head.ts
2796
- function filledProfile(rows) {
2797
- if (rows === void 0 || !rows.some((v) => v > 0)) return null;
2798
- const out = rows.slice();
2799
- const first = out.findIndex((v) => v > 0);
2800
- for (let i = 0; i < first; i++) out[i] = out[first];
2801
- for (let i = first + 1; i < out.length; i++)
2802
- if (!(out[i] > 0)) out[i] = out[i - 1];
2803
- return out;
2804
- }
2805
- var smoothProfile = (rows) => boxSmooth(rows, Math.max(1, Math.round(rows.length * 0.01)));
2806
- var EAR_STEP = 0.03;
2807
- var EAR_ROWS = 3;
2808
- function findEars(raw, wMax, firstRow, lastRow, minRows, maxRows) {
2809
- const step = Math.max(4, EAR_STEP * wMax);
2810
- for (let t = Math.max(firstRow, EAR_ROWS); t < lastRow; t++) {
2811
- if (raw[t] - raw[t - EAR_ROWS] < step) continue;
2812
- for (let b = t + minRows; b <= Math.min(lastRow, t + maxRows); b++) {
2813
- if (raw[b - EAR_ROWS] - raw[b] >= step) {
2814
- return { top: t - EAR_ROWS, bottom: b };
2815
- }
2816
- }
2817
- return void 0;
2818
- }
2819
- return void 0;
2820
- }
2821
- function boxSmooth(v, k) {
2822
- return v.map((_, i) => {
2823
- let s = 0;
2824
- let n = 0;
2825
- for (let j = i - k; j <= i + k; j++) {
2826
- if (j < 0 || j >= v.length) continue;
2827
- s += v[j];
2828
- n++;
2829
- }
2830
- return s / n;
2831
- });
2832
- }
2833
- var CUT_MARGIN = 2;
2834
- var CUT_LOW = 3;
2835
- function fadeToOutline(d, nk) {
2836
- return Math.max(0, 1 - d / nk.waist);
2837
- }
2838
- var STROKE_RUN = 4;
2839
- var STROKE_SLACK = 6;
2840
- var JAW_SHADE = 0.05;
2841
- function buildHeadFrame(layers, opts) {
2842
- const byRole = new Map(layers.map((l) => [l.role, l]));
2843
- const faceLayer = byRole.get("face");
2844
- const { canvasW, canvasH } = faceLayer;
2845
- const face = boxOfLayer(faceLayer);
2846
- const axisX = cx(face);
2847
- const plateHalf = faceLayer.cropW / 2;
2848
- const centreY = (role) => {
2849
- const l = byRole.get(role);
2850
- return l === void 0 ? void 0 : cy(boxOfLayer(l));
2851
- };
2852
- const pair = (a, b) => {
2853
- const ya = centreY(a);
2854
- const yb = centreY(b);
2855
- return ya === void 0 || yb === void 0 ? void 0 : (ya + yb) / 2;
2537
+ const pair = (a, b) => {
2538
+ const ya = centreY(a);
2539
+ const yb = centreY(b);
2540
+ return ya === void 0 || yb === void 0 ? void 0 : (ya + yb) / 2;
2856
2541
  };
2857
2542
  const eyeY = pair("iris_L", "iris_R") ?? pair("eye_L", "eye_R");
2858
2543
  const mouthY = centreY("mouth");
@@ -3005,75 +2690,613 @@ function buildHeadFrame(layers, opts) {
3005
2690
  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;
3006
2691
  cut[i] = y >= face.y1 ? face.y1 : y;
3007
2692
  }
3008
- const low = cut.map((_, i) => {
3009
- let m = Infinity;
3010
- for (let j = i - CUT_LOW; j <= i + CUT_LOW; j++) {
3011
- if (j >= 0 && j < cut.length) m = Math.min(m, cut[j]);
3012
- }
3013
- return m;
3014
- });
3015
- if (opts.chinSlide !== void 0 && banded >= 0)
3016
- nk.bandEnd = banded + CUT_LOW + 1;
3017
- cutAt = (x) => {
3018
- const f = x - (faceLayer.bbox.x - canvasW / 2) - 0.5;
3019
- const i = clamp(Math.floor(f), 0, low.length - 1);
3020
- const j = Math.min(i + 1, low.length - 1);
3021
- const t = clamp(f - i, 0, 1);
3022
- return Math.max(face.y0, low[i] + (low[j] - low[i]) * t);
3023
- };
3024
- } else {
3025
- const head = earless(chinY);
3026
- headMax = Math.max(...head);
3027
- if (ears !== void 0) {
3028
- const bottomOf = (d) => {
3029
- for (let r = head.length - 1; r >= 0; r--) {
3030
- if (head[r] >= d) return rowY(r) - 0.5;
3031
- }
3032
- return face.y1;
3033
- };
3034
- cutAt = (x) => {
3035
- const y = bottomOf(Math.abs(x - axisX));
3036
- return y >= face.y1 ? face.y1 : Math.max(face.y0, y - CUT_MARGIN);
3037
- };
2693
+ const low = cut.map((_, i) => {
2694
+ let m = Infinity;
2695
+ for (let j = i - CUT_LOW; j <= i + CUT_LOW; j++) {
2696
+ if (j >= 0 && j < cut.length) m = Math.min(m, cut[j]);
2697
+ }
2698
+ return m;
2699
+ });
2700
+ if (opts.chinSlide !== void 0 && banded >= 0)
2701
+ nk.bandEnd = banded + CUT_LOW + 1;
2702
+ cutAt = (x) => {
2703
+ const f = x - (faceLayer.bbox.x - canvasW / 2) - 0.5;
2704
+ const i = clamp(Math.floor(f), 0, low.length - 1);
2705
+ const j = Math.min(i + 1, low.length - 1);
2706
+ const t = clamp(f - i, 0, 1);
2707
+ return Math.max(face.y0, low[i] + (low[j] - low[i]) * t);
2708
+ };
2709
+ } else {
2710
+ const head = earless(chinY);
2711
+ headMax = Math.max(...head);
2712
+ if (ears !== void 0) {
2713
+ const bottomOf = (d) => {
2714
+ for (let r = head.length - 1; r >= 0; r--) {
2715
+ if (head[r] >= d) return rowY(r) - 0.5;
2716
+ }
2717
+ return face.y1;
2718
+ };
2719
+ cutAt = (x) => {
2720
+ const y = bottomOf(Math.abs(x - axisX));
2721
+ return y >= face.y1 ? face.y1 : Math.max(face.y0, y - CUT_MARGIN);
2722
+ };
2723
+ }
2724
+ }
2725
+ }
2726
+ const hh = HH_PER_EYE_TO_CHIN * Math.max(1, eyeY - chinY);
2727
+ const headHalf = opts.headHalfWidth;
2728
+ const hairShell = headHalf !== void 0;
2729
+ let shellLeft = axisX - plateHalf;
2730
+ let shellRight = axisX + plateHalf;
2731
+ if (hairShell) {
2732
+ if (opts.headEdges && opts.headEdges.left.length > 0 && opts.headEdges.right.length > 0) {
2733
+ shellLeft = Math.min(...opts.headEdges.left.map((e) => e.x));
2734
+ shellRight = Math.max(...opts.headEdges.right.map((e) => e.x)) + 1;
2735
+ } else {
2736
+ shellLeft = axisX - headHalf;
2737
+ shellRight = axisX + headHalf;
2738
+ }
2739
+ }
2740
+ const hairRuns = (role, y) => {
2741
+ const l = byRole.get(role);
2742
+ if (l?.rowRuns === void 0) return void 0;
2743
+ const runs = l.rowRuns[Math.floor(canvasH / 2 - y) - l.bbox.y];
2744
+ return runs === void 0 ? [] : runs.map((c) => c - canvasW / 2);
2745
+ };
2746
+ return {
2747
+ canvasW,
2748
+ canvasH,
2749
+ face,
2750
+ axisX,
2751
+ eyeY,
2752
+ mouthY,
2753
+ chinY,
2754
+ hh,
2755
+ paintedHalfAt,
2756
+ wMax,
2757
+ headMax,
2758
+ hasProfile: profile !== null,
2759
+ neck,
2760
+ ears,
2761
+ cutAt,
2762
+ cheekY: eyeY - 0.5 * (eyeY - chinY),
2763
+ jawY: eyeY - 0.8 * (eyeY - chinY),
2764
+ shellLeft,
2765
+ shellRight,
2766
+ hairShell,
2767
+ holdBase: headHalf ?? plateHalf,
2768
+ hairRuns
2769
+ };
2770
+ }
2771
+
2772
+ // src/auto-rig/fields.ts
2773
+ var NOSE_TILT_DEG = 6;
2774
+ function nodDy(v, ay, hh) {
2775
+ const up = Math.max(0, ay) / 30;
2776
+ const down = Math.max(0, -ay) / 30;
2777
+ return -(up * v.up + down * v.down) * hh;
2778
+ }
2779
+ function spin(x, y, ox, oy, a) {
2780
+ if (a === 0) return [0, 0];
2781
+ const px = x - ox;
2782
+ const py = y - oy;
2783
+ return [
2784
+ px * Math.cos(a) - py * Math.sin(a) - px,
2785
+ px * Math.sin(a) + py * Math.cos(a) - py
2786
+ ];
2787
+ }
2788
+ function faceField(m) {
2789
+ const f = m.frame;
2790
+ return (x, y, ax, ay) => {
2791
+ const s = Math.sign(ax);
2792
+ const a = Math.abs(ax) / 30;
2793
+ const chin = smoothstep(f.mouthY, f.chinY, y);
2794
+ const width = m.widthUpper + (m.widthJaw - m.widthUpper) * smoothstep(f.cheekY, f.jawY, y);
2795
+ const shell = f.hairShell ? 0 : m.shellScale - 1;
2796
+ const dx = s * a * (m.face + m.chinLead * chin) + a * (width - 1 + shell) * (x - f.axisX);
2797
+ const t = Math.min(
2798
+ 1,
2799
+ Math.max(0, (f.face.y1 - y) / Math.max(1, f.face.y1 - f.chinY))
2800
+ );
2801
+ const v = {
2802
+ up: NOD.faceTop.up + (NOD.chin.up - NOD.faceTop.up) * t,
2803
+ down: NOD.faceTop.down + (NOD.chin.down - NOD.faceTop.down) * t
2804
+ };
2805
+ return [dx, nodDy(v, ay, f.hh)];
2806
+ };
2807
+ }
2808
+ function earField(m) {
2809
+ const f = m.frame;
2810
+ const ears = f.ears;
2811
+ if (ears === void 0) return () => [0, 0];
2812
+ const face = faceField(m);
2813
+ return (x, y, ax, ay) => {
2814
+ const s = Math.sign(ax);
2815
+ const a = Math.abs(ax) / 30;
2816
+ const dy = face(x, y, 0, ay)[1];
2817
+ if (sideOf(m, x, s) === "far") return [s * a * TURN.earFar * m.face, dy];
2818
+ const d = Math.abs(x - f.axisX);
2819
+ const attach = ears.attachAt(y);
2820
+ if (d <= attach) return [face(x, y, ax, 0)[0], dy];
2821
+ const root = face(f.axisX + Math.sign(x - f.axisX) * attach, y, ax, 0)[0];
2822
+ const rim = s * a * TURN.earNear * m.face;
2823
+ const t = Math.min(1, (d - attach) / Math.max(1, ears.outer - attach));
2824
+ return [root + (rim - root) * t, dy];
2825
+ };
2826
+ }
2827
+ function chinSlide(m) {
2828
+ const f = m.frame;
2829
+ return Math.abs(faceField(m)(f.axisX, f.chinY, 30, 0)[0]);
2830
+ }
2831
+ var NECK_SHADE_MAX = 0.45;
2832
+ var NECK_SHADE_FULL = 0.2;
2833
+ function neckField(m) {
2834
+ const f = m.frame;
2835
+ const nk = f.neck;
2836
+ if (nk === void 0) return () => [0, 0];
2837
+ const face = faceField(m);
2838
+ const full = chinSlide(m);
2839
+ const share = Math.min(1, NECK_SHADE_MAX * nk.waist / Math.max(1e-6, full));
2840
+ const bottom = f.face.y0;
2841
+ const tip = f.cutAt(f.axisX);
2842
+ const top = tip - NECK_SHADE_FULL * (tip - bottom);
2843
+ return (x, y, ax) => {
2844
+ const wy = Math.min(
2845
+ 1,
2846
+ Math.max(0, (y - bottom) / Math.max(1, top - bottom))
2847
+ );
2848
+ const wx = Math.max(0, 1 - Math.abs(x - f.axisX) / nk.waist);
2849
+ return [share * face(f.axisX, f.chinY, ax, 0)[0] * wx * wy, 0];
2850
+ };
2851
+ }
2852
+ function sideOf(m, x, s) {
2853
+ return s * (x - m.frame.axisX) > 0 ? "far" : "near";
2854
+ }
2855
+ function eyeField(m, box) {
2856
+ const xe = cx(box);
2857
+ const ye = cy(box);
2858
+ const hh = m.frame.hh;
2859
+ return (x, y, ax, ay) => {
2860
+ const s = Math.sign(ax);
2861
+ const a = Math.abs(ax) / 30;
2862
+ const side = sideOf(m, xe, s);
2863
+ const dx = s * a * m.eye[side] + a * (m.eyeScale[side] - 1) * (x - xe);
2864
+ const up = Math.max(0, ay) / 30;
2865
+ const down = Math.max(0, -ay) / 30;
2866
+ const squash = up * (NOD.eyeHeight.up - 1) + down * (NOD.eyeHeight.down - 1);
2867
+ return [dx, nodDy(NOD.eye, ay, hh) + squash * (y - ye)];
2868
+ };
2869
+ }
2870
+ function browField(m, box) {
2871
+ const xb = cx(box);
2872
+ return (_x, _y, ax, ay) => {
2873
+ const s = Math.sign(ax);
2874
+ const a = Math.abs(ax) / 30;
2875
+ return [s * a * m.brow[sideOf(m, xb, s)], nodDy(NOD.brow, ay, m.frame.hh)];
2876
+ };
2877
+ }
2878
+ function noseField(m) {
2879
+ const at = m.noseAt;
2880
+ const top = m.noseTopY ?? at.y;
2881
+ return (x, y, ax, ay) => {
2882
+ const s = Math.sign(ax);
2883
+ const a = Math.abs(ax) / 30;
2884
+ const [tx, ty] = spin(
2885
+ x,
2886
+ y,
2887
+ at.x,
2888
+ top,
2889
+ s * a * m.tilt * NOSE_TILT_DEG * DEG
2890
+ );
2891
+ return [s * a * m.nose + tx, nodDy(NOD.nose, ay, m.frame.hh) + ty];
2892
+ };
2893
+ }
2894
+ function mouthField(m) {
2895
+ const c = m.mouthAt;
2896
+ return (x, y, ax, ay) => {
2897
+ const s = Math.sign(ax);
2898
+ const a = Math.abs(ax) / 30;
2899
+ const wx = c.x + (x - c.x) * (1 + a * (m.mouthWidth - 1));
2900
+ const [tx, ty] = spin(
2901
+ wx,
2902
+ y,
2903
+ c.x,
2904
+ c.y,
2905
+ s * a * m.tilt * TURN.mouthTiltDeg * DEG
2906
+ );
2907
+ return [
2908
+ s * a * m.mouth + wx - x + tx,
2909
+ nodDy(NOD.mouth, ay, m.frame.hh) + ty
2910
+ ];
2911
+ };
2912
+ }
2913
+ function hairField(m, shift, nod) {
2914
+ const f = m.frame;
2915
+ return (x, _y, ax, ay) => {
2916
+ const s = Math.sign(ax);
2917
+ const a = Math.abs(ax) / 30;
2918
+ return [
2919
+ s * a * shift + a * (m.shellScale - 1) * (x - f.axisX),
2920
+ nodDy(nod, ay, f.hh)
2921
+ ];
2922
+ };
2923
+ }
2924
+ function hairFrontBends(frame, boxes) {
2925
+ const f = frame;
2926
+ const eyes = [boxes.eye_L, boxes.eye_R].filter(
2927
+ (e) => e !== void 0
2928
+ );
2929
+ const inner = Math.max(
2930
+ 1,
2931
+ Math.min(
2932
+ f.paintedHalfAt(f.eyeY),
2933
+ ...eyes.map((e) => Math.max(f.axisX - e.x0, e.x1 - f.axisX))
2934
+ )
2935
+ );
2936
+ return {
2937
+ inner,
2938
+ left: Math.max(inner + 1, f.axisX - f.shellLeft),
2939
+ right: Math.max(inner + 1, f.shellRight - f.axisX)
2940
+ };
2941
+ }
2942
+ function hairFrontField(m) {
2943
+ const f = m.frame;
2944
+ const back = hairField(m, m.hairBack, NOD.hairBack);
2945
+ const cap = { up: NOD.hairFrontTop.up, down: capSlide(m) };
2946
+ const eyes = [m.boxes.eye_L, m.boxes.eye_R].filter(
2947
+ (e) => e !== void 0
2948
+ );
2949
+ const { inner, left, right } = hairFrontBends(f, m.boxes);
2950
+ const eyeY = eyes.length > 0 ? eyes.reduce((t, e) => t + cy(e), 0) / eyes.length : f.eyeY;
2951
+ const eyeH = eyes.length > 0 ? eyes.reduce((t, e) => t + (e.y1 - e.y0), 0) / eyes.length : 1;
2952
+ const coreAt = (x, y, s) => {
2953
+ const u = s * (x - f.axisX);
2954
+ const across = Math.min(1, Math.max(0, (u + inner) / (2 * inner)));
2955
+ const band = 1 - smoothstep(eyeH / 2, 1.5 * eyeH, Math.abs(y - eyeY));
2956
+ return m.hairFront + (m.hairFar - m.hairFront) * across * band;
2957
+ };
2958
+ const followAt = (x, y, s) => {
2959
+ const core = coreAt(x, y, s);
2960
+ const out = Math.min(
2961
+ 1,
2962
+ Math.max(
2963
+ 0,
2964
+ (Math.abs(x - f.axisX) - inner) / ((x < f.axisX ? left : right) - inner)
2965
+ )
2966
+ );
2967
+ return core + (m.hairOuter - core) * out;
2968
+ };
2969
+ const ride = outerRide(m, inner, coreAt, followAt);
2970
+ return (x, y, ax, ay) => {
2971
+ const s = Math.sign(ax);
2972
+ const a = Math.abs(ax) / 30;
2973
+ const follow = followAt(x, y, s) + (ride === void 0 ? 0 : ride(x, y, s));
2974
+ const rx = s * a * follow + a * (m.shellScale - 1) * (x - f.axisX);
2975
+ const ry = nodDy(NOD.hairFront, ay, f.hh);
2976
+ const bx = back(x, y, ax, ay)[0];
2977
+ const by = nodDy(cap, ay, f.hh);
2978
+ const w = crownBlend(m, y);
2979
+ return [bx + w * (rx - bx), by + w * (ry - by)];
2980
+ };
2981
+ }
2982
+ function crownBlend(m, y) {
2983
+ const top = m.frame.face.y1;
2984
+ return 1 - smoothstep(top, Math.max(top + 1, m.hairTop), y);
2985
+ }
2986
+ var CAP_COVER_LOSS = 0.01;
2987
+ function capSlide(m) {
2988
+ const f = m.frame;
2989
+ const least = NOD.hairBack.down;
2990
+ const grid = m.hairFrontGrid;
2991
+ if (grid === void 0 || f.hairRuns("hair_front", grid.ys[0]) === void 0 || f.hairRuns("hair_back", grid.ys[0]) === void 0) {
2992
+ return least;
2993
+ }
2994
+ const { xs, ys } = grid;
2995
+ const top = ys[0];
2996
+ const n = ys.length;
2997
+ const bangs = NOD.hairFront.down;
2998
+ const wr = ys.map((y) => crownBlend(m, y));
2999
+ const blendAt = (y) => {
3000
+ const v = clamp((top - y) / (top - ys[n - 1]) * (n - 1), 0, n - 1);
3001
+ const r = Math.min(n - 2, Math.floor(v));
3002
+ return wr[r] + (wr[r + 1] - wr[r]) * (v - r);
3003
+ };
3004
+ const flat = ys[wr.findIndex((w) => w >= 1)] ?? ys[n - 1];
3005
+ const x0 = xs[0];
3006
+ const tops = new Float64Array(Math.round(xs[xs.length - 1] - x0)).fill(NaN);
3007
+ for (let i = 0; i < top - flat; i++) {
3008
+ const runs = f.hairRuns("hair_front", top - i - 0.5);
3009
+ for (let k = 0; k < runs.length; k += 2) {
3010
+ for (let px = runs[k]; px < runs[k + 1]; px++) {
3011
+ const j = Math.round(px - x0);
3012
+ if (Number.isNaN(tops[j])) tops[j] = i;
3013
+ }
3014
+ }
3015
+ }
3016
+ const hh = f.hh;
3017
+ const most = Math.ceil(Math.max(bangs, NOD.hairFrontTop.down) * hh);
3018
+ const bared = (s) => clamp(Math.ceil(s - 0.5), 0, most);
3019
+ const dropAt = (w, slide) => (w * bangs + (1 - w) * slide) * hh;
3020
+ const backNod = NOD.hairBack.down * hh;
3021
+ const backRows = /* @__PURE__ */ new Map();
3022
+ const columns = [];
3023
+ tops.forEach((i, j) => {
3024
+ if (Number.isNaN(i)) return;
3025
+ const w = blendAt(top - i);
3026
+ const empty = new Int32Array(most + 1);
3027
+ for (let r = 0; r < most; r++) {
3028
+ const at = Math.floor(i + r + 0.5 - backNod);
3029
+ let runs = backRows.get(at);
3030
+ if (runs === void 0) {
3031
+ runs = f.hairRuns("hair_back", top - at - 0.5);
3032
+ backRows.set(at, runs);
3038
3033
  }
3034
+ empty[r + 1] = empty[r] + (paints(runs, x0 + j) ? 0 : 1);
3039
3035
  }
3036
+ columns.push({ w, empty, base: empty[bared(dropAt(w, least))] });
3037
+ });
3038
+ const budget = CAP_COVER_LOSS * hh;
3039
+ const fits = (slide) => columns.every((c) => c.empty[bared(dropAt(c.w, slide))] - c.base <= budget);
3040
+ let lo = least;
3041
+ let hi = NOD.hairFrontTop.down;
3042
+ if (fits(hi)) return hi;
3043
+ for (let k = 0; k < 30; k++) {
3044
+ const mid = (lo + hi) / 2;
3045
+ if (fits(mid)) lo = mid;
3046
+ else hi = mid;
3040
3047
  }
3041
- const hh = HH_PER_EYE_TO_CHIN * Math.max(1, eyeY - chinY);
3042
- const headHalf = opts.headHalfWidth;
3043
- const hairShell = headHalf !== void 0;
3044
- let shellLeft = axisX - plateHalf;
3045
- let shellRight = axisX + plateHalf;
3046
- if (hairShell) {
3047
- if (opts.headEdges && opts.headEdges.left.length > 0 && opts.headEdges.right.length > 0) {
3048
- shellLeft = Math.min(...opts.headEdges.left.map((e) => e.x));
3049
- shellRight = Math.max(...opts.headEdges.right.map((e) => e.x)) + 1;
3050
- } else {
3051
- shellLeft = axisX - headHalf;
3052
- shellRight = axisX + headHalf;
3048
+ return lo;
3049
+ }
3050
+ function paints(runs, px) {
3051
+ for (let k = 0; k < runs.length; k += 2) {
3052
+ if (runs[k] <= px && px + 1 <= runs[k + 1]) return true;
3053
+ }
3054
+ return false;
3055
+ }
3056
+ function outerRide(m, inner, coreAt, followAt) {
3057
+ const f = m.frame;
3058
+ const ys = m.hairFrontGrid?.ys;
3059
+ if (ys === void 0) return void 0;
3060
+ const top = ys[0];
3061
+ if (f.hairRuns("hair_front", top) === void 0 || f.hairRuns("hair_back", top) === void 0) {
3062
+ return void 0;
3063
+ }
3064
+ const drift = Math.abs(m.hairBack);
3065
+ const sides = [-1, 1];
3066
+ const n = ys.length;
3067
+ const count = Math.round(top - ys[n - 1]);
3068
+ const below = (NOD.hairFront.down - NOD.hairBack.down) * f.hh;
3069
+ const above = (NOD.hairBack.up - NOD.hairFront.up) * f.hh;
3070
+ const j0 = Math.floor(-above);
3071
+ const backRows = Array.from(
3072
+ { length: Math.ceil(count + below) - j0 },
3073
+ (_, j) => f.hairRuns("hair_back", top - (j + j0) - 0.5)
3074
+ );
3075
+ const offset = sides.map(() => new Float64Array(count).fill(NaN));
3076
+ const far = sides.map(() => new Float64Array(count));
3077
+ const near = sides.map(() => new Float64Array(count));
3078
+ for (let i = 0; i < count; i++) {
3079
+ const y = top - i - 0.5;
3080
+ const front = f.hairRuns("hair_front", y);
3081
+ sides.forEach((side, k) => {
3082
+ const edge = outerEdge(front, f.axisX, side);
3083
+ if (edge === void 0) return;
3084
+ let outward = Infinity;
3085
+ let inward = Infinity;
3086
+ for (let j = Math.floor(i - above); j < Math.ceil(i + 1 + below); j++) {
3087
+ const c = runReach(backRows[j - j0], edge, side);
3088
+ outward = Math.min(outward, c.outward);
3089
+ inward = Math.min(inward, c.inward);
3090
+ }
3091
+ offset[k][i] = side * (edge - f.axisX);
3092
+ far[k][i] = m.shellScale * outward - drift;
3093
+ near[k][i] = m.shellScale * inward - drift;
3094
+ });
3095
+ }
3096
+ const end = new Float64Array(n * 2);
3097
+ const lift = new Float64Array(n * 4);
3098
+ for (let r = 0; r < n; r++) {
3099
+ const i0 = Math.max(0, Math.floor(top - ys[Math.max(0, r - 1)]));
3100
+ const i1 = Math.min(count, Math.ceil(top - ys[Math.min(n - 1, r + 1)]));
3101
+ sides.forEach((side, k) => {
3102
+ let d = Infinity;
3103
+ let allowFar = Infinity;
3104
+ let allowNear = Infinity;
3105
+ for (let i = i0; i < i1; i++) {
3106
+ if (Number.isNaN(offset[k][i])) continue;
3107
+ d = Math.min(d, offset[k][i]);
3108
+ allowFar = Math.min(allowFar, far[k][i]);
3109
+ allowNear = Math.min(allowNear, near[k][i]);
3110
+ }
3111
+ end[r * 2 + k] = d;
3112
+ if (d === Infinity || d <= inner) return;
3113
+ const x = f.axisX + side * d;
3114
+ sides.forEach((s, j) => {
3115
+ const allow = Math.min(
3116
+ coreAt(x, ys[r], s),
3117
+ s === side ? allowFar : allowNear
3118
+ );
3119
+ lift[(r * 2 + k) * 2 + j] = Math.max(0, allow - followAt(x, ys[r], s));
3120
+ });
3121
+ });
3122
+ }
3123
+ const rowRide = (r, k, j, d) => {
3124
+ const v = lift[(r * 2 + k) * 2 + j];
3125
+ return v === 0 ? 0 : v * Math.min(1, (d - inner) / (end[r * 2 + k] - inner));
3126
+ };
3127
+ return (x, y, s) => {
3128
+ const d = Math.abs(x - f.axisX);
3129
+ if (s === 0 || d <= inner) return 0;
3130
+ const k = x < f.axisX ? 0 : 1;
3131
+ const j = s < 0 ? 0 : 1;
3132
+ let v = clamp((top - y) / (top - ys[n - 1]) * (n - 1), 0, n - 1);
3133
+ if (Math.abs(v - Math.round(v)) < 1e-3) v = Math.round(v);
3134
+ const r = Math.min(n - 2, Math.floor(v));
3135
+ const t = v - r;
3136
+ return rowRide(r, k, j, d) * (1 - t) + rowRide(r + 1, k, j, d) * t;
3137
+ };
3138
+ }
3139
+ function familyField(m, family) {
3140
+ switch (family) {
3141
+ case "face":
3142
+ return faceField(m);
3143
+ case "eye_L":
3144
+ case "eye_R":
3145
+ return eyeField(m, m.boxes[family]);
3146
+ case "brow_L":
3147
+ case "brow_R":
3148
+ return browField(m, m.boxes[family]);
3149
+ case "nose":
3150
+ return noseField(m);
3151
+ case "mouth":
3152
+ return mouthField(m);
3153
+ case "hair_front":
3154
+ return hairFrontField(m);
3155
+ case "hair_back":
3156
+ return hairField(m, m.hairBack, NOD.hairBack);
3157
+ case "body":
3158
+ return () => [0, 0];
3159
+ }
3160
+ }
3161
+
3162
+ // src/auto-rig/context.ts
3163
+ function sidePair(byRole, a, b) {
3164
+ const la = byRole.get(a);
3165
+ const lb = byRole.get(b);
3166
+ if (la === void 0 || lb === void 0) return void 0;
3167
+ const [lo, hi] = [
3168
+ { role: a, box: boxOfLayer(la) },
3169
+ { role: b, box: boxOfLayer(lb) }
3170
+ ].sort((p, q) => cx(p.box) - cx(q.box));
3171
+ return { left: lo, right: hi };
3172
+ }
3173
+ function sideIrises(byRole) {
3174
+ const pair = sidePair(byRole, "iris_L", "iris_R");
3175
+ return pair === void 0 ? {} : { left: pair.left.box, right: pair.right.box };
3176
+ }
3177
+ function solveContext(frame, byRole, options, hasNose) {
3178
+ const box = (role) => boxOfLayer(byRole.get(role));
3179
+ const row = (s, strand2, sideSign) => {
3180
+ if (strand2 !== void 0) {
3181
+ const lo = sideSign < 0 ? strand2.irisOuter : strand2.irisInner;
3182
+ const hi = sideSign < 0 ? strand2.irisInner : strand2.irisOuter;
3183
+ return { role: s.role, lo, hi, y: strand2.y };
3053
3184
  }
3185
+ const inset = bw(s.box) > 4 ? 1 : 0;
3186
+ return {
3187
+ role: s.role,
3188
+ lo: s.box.x0 + inset,
3189
+ hi: s.box.x1 - inset,
3190
+ y: cy(s.box)
3191
+ };
3192
+ };
3193
+ const eyes = sidePair(byRole, "eye_L", "eye_R");
3194
+ const irises = sidePair(byRole, "iris_L", "iris_R");
3195
+ const pair = irises ?? eyes;
3196
+ const strand = irises ? options.strandEdges : void 0;
3197
+ const left = row(pair.left, strand?.left, -1);
3198
+ const right = row(pair.right, strand?.right, 1);
3199
+ let candidates;
3200
+ const edges = options.headEdges;
3201
+ if (frame.hairShell && edges && edges.left.length > 0 && edges.right.length > 0) {
3202
+ candidates = {
3203
+ left: edges.left.map((e) => ({ role: e.role, x: e.x })),
3204
+ // A right edge names its outermost pixel column; the boundary is one on.
3205
+ right: edges.right.map((e) => ({ role: e.role, x: e.x + 1 }))
3206
+ };
3207
+ } else if (frame.hairShell) {
3208
+ candidates = {
3209
+ left: [{ role: "hair_back", x: frame.shellLeft }],
3210
+ right: [{ role: "hair_back", x: frame.shellRight }]
3211
+ };
3212
+ } else {
3213
+ const w = frame.paintedHalfAt(frame.eyeY);
3214
+ candidates = {
3215
+ left: [{ role: "face", x: frame.axisX - w }],
3216
+ right: [{ role: "face", x: frame.axisX + w }]
3217
+ };
3218
+ }
3219
+ const restHalf = (Math.max(...candidates.right.map((c) => c.x)) - Math.min(...candidates.left.map((c) => c.x))) / 2;
3220
+ const nose = byRole.get("nose");
3221
+ let noseAt;
3222
+ let noseBox;
3223
+ if (hasNose && nose !== void 0) {
3224
+ noseBox = nose.denseCore ? boxOfPixels(nose.denseCore, nose.canvasW, nose.canvasH) : boxOfLayer(nose);
3225
+ noseAt = { x: cx(noseBox), y: cy(noseBox) };
3054
3226
  }
3227
+ const mouthBox = byRole.has("mouth_open") ? unionBoxes([box("mouth"), box("mouth_open")]) : box("mouth");
3228
+ const m = box("mouth");
3055
3229
  return {
3056
- canvasW,
3057
- canvasH,
3058
- face,
3059
- axisX,
3060
- eyeY,
3061
- mouthY,
3062
- chinY,
3063
- hh,
3064
- paintedHalfAt,
3065
- wMax,
3066
- headMax,
3067
- hasProfile: profile !== null,
3068
- neck,
3069
- ears,
3070
- cutAt,
3071
- cheekY: eyeY - 0.5 * (eyeY - chinY),
3072
- jawY: eyeY - 0.8 * (eyeY - chinY),
3073
- shellLeft,
3074
- shellRight,
3075
- hairShell,
3076
- holdBase: headHalf ?? plateHalf
3230
+ frame,
3231
+ left,
3232
+ right,
3233
+ candidates,
3234
+ restHalf,
3235
+ strand,
3236
+ hasHairFront: byRole.has("hair_front"),
3237
+ eyeBoxes: { left: eyes.left.box, right: eyes.right.box },
3238
+ noseAt,
3239
+ noseBox,
3240
+ mouthAt: { x: cx(m), y: cy(m) },
3241
+ mouthBox
3242
+ };
3243
+ }
3244
+ function renderedLander(turn, parts, grids, from = /* @__PURE__ */ new Map()) {
3245
+ const analytic = /* @__PURE__ */ new Map();
3246
+ const landed = /* @__PURE__ */ new Map();
3247
+ const field = (role) => {
3248
+ const family = roleSpec(role).family;
3249
+ let f = analytic.get(family);
3250
+ if (f === void 0) analytic.set(family, f = familyField(turn, family));
3251
+ return f;
3252
+ };
3253
+ return (role, x, y, ax) => {
3254
+ const family = roleSpec(role).family;
3255
+ const part = parts.find((p) => p.id === role);
3256
+ const g = grids.get(family);
3257
+ const warp = part?.warps?.find((w) => w.parameter === P.AngleX);
3258
+ if (part === void 0 || part.mesh === void 0 || !g && !warp) {
3259
+ return x + field(role)(x, y, ax, 0)[0];
3260
+ }
3261
+ const box = {
3262
+ x0: part.transform.x - part.width / 2,
3263
+ x1: part.transform.x + part.width / 2,
3264
+ y0: part.transform.y - part.height / 2,
3265
+ y1: part.transform.y + part.height / 2
3266
+ };
3267
+ const rest = meshPoints(part.mesh, box);
3268
+ const key = `${role}@${ax}`;
3269
+ let xs = landed.get(key);
3270
+ if (xs === void 0) {
3271
+ if (g) {
3272
+ const offsets = keyformAt(g.keyforms, ax);
3273
+ xs = rest.map(([px, py]) => land(g.lattice, offsets, px, py)[0]);
3274
+ } else {
3275
+ const k = warp.keyforms;
3276
+ const offsets = blendStops(
3277
+ k.map((kf) => kf.value),
3278
+ k.map((kf) => kf.offsets),
3279
+ ax
3280
+ );
3281
+ xs = rest.map(([px], i) => px + offsets[2 * i] * bw(box));
3282
+ }
3283
+ landed.set(key, xs);
3284
+ }
3285
+ const idx = part.mesh.indices;
3286
+ for (let t = from.get(role) ?? 0; t < idx.length; t += 3) {
3287
+ const [a, b, c] = [idx[t], idx[t + 1], idx[t + 2]];
3288
+ const [ax0, ay0] = rest[a];
3289
+ const [bx0, by0] = rest[b];
3290
+ const [cx0, cy0] = rest[c];
3291
+ const d = (by0 - cy0) * (ax0 - cx0) + (cx0 - bx0) * (ay0 - cy0);
3292
+ if (Math.abs(d) < 1e-9) continue;
3293
+ const u = ((by0 - cy0) * (x - cx0) + (cx0 - bx0) * (y - cy0)) / d;
3294
+ const v = ((cy0 - ay0) * (x - cx0) + (ax0 - cx0) * (y - cy0)) / d;
3295
+ const w = 1 - u - v;
3296
+ if (u < -1e-6 || v < -1e-6 || w < -1e-6) continue;
3297
+ return u * xs[a] + v * xs[b] + w * xs[c];
3298
+ }
3299
+ return x + field(role)(x, y, ax, 0)[0];
3077
3300
  };
3078
3301
  }
3079
3302
 
@@ -3954,14 +4177,18 @@ function generateIkiFromLayerSet(layers, canvas, options = {}) {
3954
4177
  };
3955
4178
  const solveFrame = buildHeadFrame(layers, frameOptions);
3956
4179
  const ctx = solveContext(solveFrame, byRole, turnOptions, hasNose);
4180
+ const boxes = Object.fromEntries(
4181
+ ["eye_L", "eye_R", "brow_L", "brow_R"].filter(has).map((r) => [r, box(r)])
4182
+ );
3957
4183
  const landmarks = {
3958
- boxes: Object.fromEntries(
3959
- ["eye_L", "eye_R", "brow_L", "brow_R"].filter(has).map((r) => [r, box(r)])
3960
- ),
4184
+ boxes,
3961
4185
  noseAt: ctx.noseAt,
3962
4186
  noseTopY: ctx.noseBox?.y1,
3963
4187
  mouthAt: ctx.mouthAt,
3964
- hairTop: has("hair_front") ? box("hair_front").y1 : solveFrame.face.y1
4188
+ hairTop: has("hair_front") ? box("hair_front").y1 : solveFrame.face.y1,
4189
+ // The field reads the grid the mesh is built on (the rebuilt frame below
4190
+ // differs only in the jaw's cut, which the grid does not read).
4191
+ hairFrontGrid: has("hair_front") ? hairFrontGrid(box("hair_front"), solveFrame, boxes) : void 0
3965
4192
  };
3966
4193
  const build = (q) => buildTurn(solveFrame, landmarks, q);
3967
4194
  const fit = fitTurn(ctx, targets, style, build);
@@ -3983,7 +4210,8 @@ function generateIkiFromLayerSet(layers, canvas, options = {}) {
3983
4210
  parts.length,
3984
4211
  frame,
3985
4212
  byRole,
3986
- style
4213
+ style,
4214
+ landmarks.hairFrontGrid
3987
4215
  );
3988
4216
  if (built.region !== void 0) {
3989
4217
  regionOf = built.region;
@@ -4074,7 +4302,7 @@ function plateFields(turn, region, head) {
4074
4302
  const neck = neckField(turn);
4075
4303
  return (i) => region[i] === "ear" ? ear : region[i] === "neck" ? neck : head;
4076
4304
  }
4077
- function buildPart(spec, b, order, frame, byRole, style) {
4305
+ function buildPart(spec, b, order, frame, byRole, style, frontGrid) {
4078
4306
  const box = (role) => boxOfLayer(byRole.get(role));
4079
4307
  const part = {
4080
4308
  id: spec.role,
@@ -4097,30 +4325,15 @@ function buildPart(spec, b, order, frame, byRole, style) {
4097
4325
  mesh = fm.mesh;
4098
4326
  region = fm.region;
4099
4327
  headStart = fm.headStart;
4328
+ } else if (spec.role === "hair_front") {
4329
+ mesh = columnMesh(b, frontGrid);
4100
4330
  } else if (spec.family !== "body") {
4101
4331
  const cells = MESH_CELLS[spec.role] ?? FEATURE_MESH_CELLS;
4102
4332
  const px = Math.min(cells.px, meshScale(spec.role, frame));
4103
- const cols = cellsFor(bw(b), px, cells.min, cells.max);
4104
- const rows = cellsFor(bh(b), px, cells.min, cells.max);
4105
- if (spec.role === "hair_front") {
4106
- const boxes = Object.fromEntries(
4107
- ["eye_L", "eye_R"].filter((r) => byRole.has(r)).map((r) => [r, box(r)])
4108
- );
4109
- const { inner, left, right } = hairFrontBends(frame, boxes);
4110
- mesh = columnMesh(
4111
- b,
4112
- [
4113
- frame.axisX - left,
4114
- frame.axisX - inner,
4115
- frame.axisX + inner,
4116
- frame.axisX + right
4117
- ],
4118
- cols,
4119
- rows
4120
- );
4121
- } else {
4122
- mesh = gridMesh(cols, rows);
4123
- }
4333
+ mesh = gridMesh(
4334
+ cellsFor(bw(b), px, cells.min, cells.max),
4335
+ cellsFor(bh(b), px, cells.min, cells.max)
4336
+ );
4124
4337
  }
4125
4338
  if (mesh !== void 0) part.mesh = mesh;
4126
4339
  const side = spec.role.endsWith("_L") ? "L" : "R";
@@ -4239,16 +4452,35 @@ function buildPart(spec, b, order, frame, byRole, style) {
4239
4452
  headStart
4240
4453
  };
4241
4454
  }
4242
- function columnMesh(b, at, cols, rows) {
4455
+ function hairFrontGrid(b, frame, boxes) {
4456
+ const cells = MESH_CELLS.hair_front;
4457
+ const px = Math.min(cells.px, meshScale("hair_front", frame));
4458
+ const cols = cellsFor(bw(b), px, cells.min, cells.max);
4459
+ const rows = cellsFor(bh(b), px, cells.min, cells.max);
4460
+ const { inner, left, right } = hairFrontBends(frame, boxes);
4243
4461
  const step = bw(b) / cols;
4244
4462
  const xs = Array.from({ length: cols + 1 }, (_, c) => b.x0 + c * step);
4245
- for (const x of at) {
4463
+ for (const x of [
4464
+ frame.axisX - left,
4465
+ frame.axisX - inner,
4466
+ frame.axisX + inner,
4467
+ frame.axisX + right
4468
+ ]) {
4246
4469
  if (x <= b.x0 + 0.25 || x >= b.x1 - 0.25) continue;
4247
4470
  const near = xs.findIndex((v) => Math.abs(v - x) < 0.3 * step);
4248
4471
  if (near > 0 && near < xs.length - 1) xs[near] = x;
4249
4472
  else if (near < 0) xs.push(x);
4250
4473
  }
4251
4474
  xs.sort((p, q) => p - q);
4475
+ const ys = Array.from(
4476
+ { length: rows + 1 },
4477
+ (_, r) => b.y1 - bh(b) * r / rows
4478
+ );
4479
+ return { xs, ys };
4480
+ }
4481
+ function columnMesh(b, grid) {
4482
+ const { xs } = grid;
4483
+ const rows = grid.ys.length - 1;
4252
4484
  const vertices = [];
4253
4485
  const uvs = [];
4254
4486
  for (let r = 0; r <= rows; r++) {