@rig-c/parts 1.66.0 → 1.68.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/src/turn.ts CHANGED
@@ -29,7 +29,9 @@
29
29
  * one piece. Pure: no file, no clock.
30
30
  */
31
31
  import { computeWorldTransforms, toBoneLocalVector, toWorld, type BoneTransform } from './coords.ts';
32
+ import { extendDepthSilhouette, type PartDepthReference } from './depthsheets.ts';
32
33
  import { type Problem, refuseIfAny } from './errors.ts';
34
+ import type { Raster } from './raster/index.ts';
33
35
  import { pyRound } from './round.ts';
34
36
 
35
37
  /**
@@ -220,10 +222,33 @@ export const DEFAULT_HEAD_TURN_PARTS: readonly string[] = [
220
222
  'topknot',
221
223
  ];
222
224
 
223
- /** `config.turn`: the pivot depth, each carried part's See-through `depth_median`, optional head-only pivot override, optional curved-surface cylinder radius, optional rated band, and optional face surface deform overrides. */
225
+ export const TURN_ROLES = ['surface', 'unit', 'rear'] as const;
226
+ export type TurnRole = (typeof TURN_ROLES)[number];
227
+
228
+ export const DEFAULT_TURN_PIVOT_25D = 55;
229
+ export const DEFAULT_TURN_COUNTER_25D = 0.1;
230
+ export const DEFAULT_TURN_GRID_25D: readonly [number, number] = [13, 16];
231
+ export const FACESHIFT_BONE = 'faceshift';
232
+ export const REAR_SHIFT_BONE = 'rear_shift';
233
+ export const YAW_DIAL_BONE = 'yaw_dial';
234
+ export const PITCH_DIAL_BONE = 'pitch_dial';
235
+ export const TURN_DIAL_SCALE = 0.0625;
236
+ export const TURN_GRID_MAX_TRIANGLES_PER_SLOT = 2400;
237
+ export const TURN_GRID_MAX_TRIANGLES_TOTAL = 4000;
238
+
239
+ /** The slot bone that carries a 2.5D `'surface'` grid mesh (`<part>_surface`), centred on the part's window at world rotation 0. */
240
+ export function turnSurfaceBone(part: string): string {
241
+ return `${part}_surface`;
242
+ }
243
+
244
+ /** `config.turn`: either the legacy `{ pivot, depths }` turn spec or the 2.5D depth-sampled `{ roles, pivot?, counter?, grid?, unitDepths?, band? }` spec (#763). */
224
245
  export interface TurnSpec {
225
- pivot: number;
226
- depths: Record<string, number>;
246
+ pivot?: number;
247
+ depths?: Record<string, number>;
248
+ roles?: Record<string, TurnRole>;
249
+ counter?: number;
250
+ grid?: [number, number] | Record<string, [number, number]>;
251
+ unitDepths?: Record<string, number>;
227
252
  head?: { pivot?: number; radius?: number };
228
253
  surface?: { radius?: number };
229
254
  radius?: number;
@@ -231,6 +256,32 @@ export interface TurnSpec {
231
256
  deform?: Partial<FaceDeformFractions>;
232
257
  }
233
258
 
259
+ /** Resolve `[cols, rows]` for a `'surface'` part under `config.turn`. */
260
+ export function resolveTurnGrid(spec: TurnSpec | null | undefined, part: string): [number, number] {
261
+ const g = spec?.grid;
262
+ if (Array.isArray(g) && g.length === 2) return [g[0], g[1]];
263
+ if (g !== undefined && !Array.isArray(g) && Object.hasOwn(g, part)) {
264
+ const entry = g[part];
265
+ return [entry[0], entry[1]];
266
+ }
267
+ return [DEFAULT_TURN_GRID_25D[0], DEFAULT_TURN_GRID_25D[1]];
268
+ }
269
+
270
+ /** Resolve `[yawBand, pitchBand]` from `config.turn.band`, defaulting to `[FACE_RATED_YAW_BAND, FACE_RATED_PITCH_BAND]`. */
271
+ export function resolveTurnBands(spec: TurnSpec | null | undefined): { yaw: number; pitch: number } {
272
+ const b = spec?.band;
273
+ if (typeof b === 'number' && Number.isFinite(b) && b > 0) {
274
+ return { yaw: b, pitch: FACE_RATED_PITCH_BAND };
275
+ }
276
+ if (b !== undefined && typeof b === 'object' && b !== null) {
277
+ return {
278
+ yaw: typeof b.yaw === 'number' && Number.isFinite(b.yaw) && b.yaw > 0 ? b.yaw : FACE_RATED_YAW_BAND,
279
+ pitch: typeof b.pitch === 'number' && Number.isFinite(b.pitch) && b.pitch > 0 ? b.pitch : FACE_RATED_PITCH_BAND,
280
+ };
281
+ }
282
+ return { yaw: FACE_RATED_YAW_BAND, pitch: FACE_RATED_PITCH_BAND };
283
+ }
284
+
234
285
  /**
235
286
  * Issue #479: the cylinder radius `R` (in rig px) for the curved-surface face
236
287
  * yaw projection — `spec.surface.radius`, `spec.radius`, or `spec.head.radius`
@@ -285,9 +336,12 @@ export function turnFraction(pivot: number, nearest: number, depth: number): num
285
336
 
286
337
  /** Every carried part's fraction, in `order` (parts.json's draw order); a name in `order` the spec does not list is not carried. */
287
338
  export function turnFractions(spec: TurnSpec, order: readonly string[]): TurnFraction[] {
288
- const nearest = turnNearest(spec.depths);
339
+ if (spec.depths === undefined || spec.pivot === undefined) return [];
340
+ const depths = spec.depths;
341
+ const pivot = spec.pivot;
342
+ const nearest = turnNearest(depths);
289
343
  if (nearest === null) return [];
290
- return order.filter((p) => Object.hasOwn(spec.depths, p)).map((part) => ({ part, depth: spec.depths[part], fraction: turnFraction(spec.pivot, nearest.depth, spec.depths[part]) }));
344
+ return order.filter((p) => Object.hasOwn(depths, p)).map((part) => ({ part, depth: depths[part], fraction: turnFraction(pivot, nearest.depth, depths[part]) }));
291
345
  }
292
346
 
293
347
  /**
@@ -319,6 +373,7 @@ export function headTurnPivot(
319
373
  if (typeof spec.head?.pivot === 'number' && Number.isFinite(spec.head.pivot)) {
320
374
  return pyRound(spec.head.pivot, TURN_PLACES);
321
375
  }
376
+ if (spec.depths === undefined) return null;
322
377
  const headEntries = Object.entries(spec.depths).filter(([part, d]) => headParts.has(part) && typeof d === 'number' && Number.isFinite(d));
323
378
  const entries = headEntries.length > 0 ? headEntries : Object.entries(spec.depths).filter(([, d]) => typeof d === 'number' && Number.isFinite(d));
324
379
  return turnPivot(entries.map(([part, depth]) => ({ depth, area: partAreas?.[part] ?? 1 })));
@@ -335,6 +390,7 @@ export function headTurnFraction(
335
390
  headParts: ReadonlySet<string> = new Set(DEFAULT_HEAD_TURN_PARTS),
336
391
  partAreas?: Readonly<Record<string, number>>,
337
392
  ): number | null {
393
+ if (spec.depths === undefined) return null;
338
394
  const depth = spec.depths[part];
339
395
  if (typeof depth !== 'number' || !Number.isFinite(depth)) return null;
340
396
  const pivot = headTurnPivot(spec, headParts, partAreas);
@@ -617,7 +673,7 @@ export function applyFaceSurfaceDeform(
617
673
  object: 'config.turn',
618
674
  detail: 'is absent and required when motion.animations_from carries a turn or tilt slider animation with derive keys; copy the turn block propose drafted into config.turn so the head pivot and each head part\'s depth are declared',
619
675
  });
620
- } else {
676
+ } else if (opts.turn.depths !== undefined) {
621
677
  for (const reqPart of ['face', 'hair_front', 'hair_back'] as const) {
622
678
  if (rig.skins.default[reqPart]?.[reqPart] !== undefined && !(reqPart in opts.turn.depths)) {
623
679
  preProblems.push({
@@ -1360,3 +1416,385 @@ export function applyFaceSurfaceDeform(
1360
1416
  turnAnim.deform = turnDeform;
1361
1417
  }
1362
1418
  }
1419
+
1420
+ /** Convert an 8-bit grey level `0..255` to a scalar depth in `[0, zScale]` using `ref.near` and `ref.zScale`. */
1421
+ export function levelToDepthZ(level: number, ref: PartDepthReference): number {
1422
+ const u = ref.near === 'white' ? level / 255 : (255 - level) / 255;
1423
+ return u * ref.zScale;
1424
+ }
1425
+
1426
+ /**
1427
+ * Sample a `'unit'` part's single scalar depth from its unpadded depth sheet
1428
+ * (`scalar`) over opaque art texels (`alpha >= 128`, falling back to `alpha > 0`).
1429
+ */
1430
+ export function sampleUnitPartDepth(scalar: Raster, art: Raster, ref: PartDepthReference): number {
1431
+ const levels: number[] = [];
1432
+ const total = scalar.width * scalar.height;
1433
+ for (let i = 0; i < total; i++) {
1434
+ if (art.data[i * 4 + 3] >= 128) levels.push(scalar.data[i * 4]);
1435
+ }
1436
+ if (levels.length === 0) {
1437
+ for (let i = 0; i < total; i++) {
1438
+ if (art.data[i * 4 + 3] > 0) levels.push(scalar.data[i * 4]);
1439
+ }
1440
+ }
1441
+ if (levels.length === 0) return 0;
1442
+ levels.sort((a, b) => a - b);
1443
+ const mid = Math.floor(levels.length / 2);
1444
+ const medLevel = levels.length % 2 === 1 ? levels[mid] : 0.5 * (levels[mid - 1] + levels[mid]);
1445
+ return pyRound(levelToDepthZ(medLevel, ref), 4);
1446
+ }
1447
+
1448
+ export interface Depth25DTurnOptions {
1449
+ turn: TurnSpec;
1450
+ parts: ReadonlyArray<{ name: string; x: number; y: number; w: number; h: number; depth?: PartDepthReference }>;
1451
+ images: ReadonlyMap<string, Raster>;
1452
+ regions: Readonly<Record<string, string | { bone: string; gather?: false }>>;
1453
+ stills: Readonly<Record<string, { row: number; bone: string }>>;
1454
+ boneLandmarks: ReadonlyMap<string, readonly [number, number]>;
1455
+ hasAlternateFaceSet: boolean;
1456
+ }
1457
+
1458
+ /**
1459
+ * Issue #763 (Stage 2): build the 2.5D head-turn `turn` and `tilt` animations
1460
+ * and `yaw` / `pitch` slider constraints using Core's native `derive` and
1461
+ * `deform` `transform` (`depth: true, narrowing: false`).
1462
+ */
1463
+ export function buildDepth25DTurn(
1464
+ rig: {
1465
+ bones: Array<{ name: string; parent?: string; x: number; y: number; rotation?: number; length?: number }>;
1466
+ slots: Array<{ name: string; bone: string; attachment: string | null }>;
1467
+ constraints?: Array<Record<string, unknown>>;
1468
+ },
1469
+ motion: {
1470
+ groups?: Record<string, string[]>;
1471
+ animations: Record<string, unknown>;
1472
+ },
1473
+ opts: Depth25DTurnOptions,
1474
+ ): void {
1475
+ const roles = opts.turn.roles;
1476
+ if (roles === undefined) return;
1477
+ const problems: Problem[] = [];
1478
+ const partMap = new Map(opts.parts.map((p) => [p.name, p]));
1479
+
1480
+ // Gap 3 check: all turn-bound parts that carry a depth sheet must share one near and one zScale.
1481
+ let sharedNear: PartDepthReference['near'] | null = null;
1482
+ let sharedZScale: number | null = null;
1483
+ let sharedFirstPart: string | null = null;
1484
+ for (const part of Object.keys(roles)) {
1485
+ const rec = partMap.get(part);
1486
+ if (rec?.depth === undefined) continue;
1487
+ if (sharedNear === null || sharedZScale === null) {
1488
+ sharedNear = rec.depth.near;
1489
+ sharedZScale = rec.depth.zScale;
1490
+ sharedFirstPart = part;
1491
+ } else if (rec.depth.near !== sharedNear || rec.depth.zScale !== sharedZScale) {
1492
+ problems.push({
1493
+ code: 'RIG_TURN_SHARED_DEPTH_SCALE',
1494
+ object: `parts.json part "${part}".depth`,
1495
+ detail: `states near="${rec.depth.near}", zScale=${rec.depth.zScale}, which disagrees with "${sharedFirstPart}" (near="${sharedNear}", zScale=${sharedZScale}); all parts bound to one head turn must share one near polarity and one zScale so Core's sampled z values are in one coordinate space`,
1496
+ });
1497
+ }
1498
+ }
1499
+
1500
+ // Prepare extended surface sheets and reference depths.
1501
+ const surfaceSheets = new Map<
1502
+ string,
1503
+ {
1504
+ rec: { name: string; x: number; y: number; w: number; h: number; depth: PartDepthReference };
1505
+ ext: Raster;
1506
+ zRef: number;
1507
+ }
1508
+ >();
1509
+ for (const [part, role] of Object.entries(roles)) {
1510
+ if (role !== 'surface') continue;
1511
+ const rec = partMap.get(part);
1512
+ const art = opts.images.get(part);
1513
+ const scalar = rec?.depth === undefined ? undefined : opts.images.get(rec.depth.image);
1514
+ if (rec === undefined || rec.depth === undefined || art === undefined || scalar === undefined) {
1515
+ problems.push({
1516
+ code: 'RIG_TURN_SURFACE_DEPTH_REQUIRED',
1517
+ object: `config.turn.roles.${part}`,
1518
+ detail: `declares role "surface" for "${part}", which requires a depth sheet in parts.json; run assemble with config.depth or provide "${part}_depth.png"`,
1519
+ });
1520
+ continue;
1521
+ }
1522
+ const ext = extendDepthSilhouette(scalar, art);
1523
+ const padX = Math.floor((ext.width - rec.w) / 2);
1524
+ const padY = Math.floor((ext.height - rec.h) / 2);
1525
+ const sampleExtAtCrop = (cx: number, cy: number): number => {
1526
+ const lx = Math.max(0, Math.min(ext.width - 1, Math.round(cx - rec.x + padX)));
1527
+ const ly = Math.max(0, Math.min(ext.height - 1, Math.round(cy - rec.y + padY)));
1528
+ return levelToDepthZ(ext.data[(ly * ext.width + lx) * 4], rec.depth as PartDepthReference);
1529
+ };
1530
+ const eyeR = opts.boneLandmarks.get('eye_r');
1531
+ const eyeL = opts.boneLandmarks.get('eye_l');
1532
+ let zRef: number;
1533
+ if (eyeR !== undefined && eyeL !== undefined) {
1534
+ zRef = pyRound(0.5 * (sampleExtAtCrop(eyeR[0], eyeR[1]) + sampleExtAtCrop(eyeL[0], eyeL[1])), 4);
1535
+ } else {
1536
+ zRef = sampleUnitPartDepth(ext, art, rec.depth);
1537
+ }
1538
+ surfaceSheets.set(part, { rec: rec as { name: string; x: number; y: number; w: number; h: number; depth: PartDepthReference }, ext, zRef });
1539
+ }
1540
+ refuseIfAny(problems);
1541
+
1542
+ const primarySurface = surfaceSheets.get('face') ?? [...surfaceSheets.values()][0];
1543
+ const pivot = pyRound(opts.turn.pivot ?? DEFAULT_TURN_PIVOT_25D, 4);
1544
+ const counter = opts.turn.counter ?? DEFAULT_TURN_COUNTER_25D;
1545
+ const rearDepth = pyRound(-counter * pivot, 4);
1546
+ const hasRear = Object.values(roles).includes('rear');
1547
+
1548
+ // Collect unit bones that sit directly under faceshift and resolve one scalar depth per unit bone.
1549
+ const faceshiftChildren = new Set(rig.bones.filter((b) => b.parent === FACESHIFT_BONE).map((b) => b.name));
1550
+ const unitDepthMap: Record<string, number> = {};
1551
+ const unitBones: string[] = [];
1552
+ const addUnitBone = (bone: string, depth: number): void => {
1553
+ if (!faceshiftChildren.has(bone)) return;
1554
+ if (!unitBones.includes(bone)) unitBones.push(bone);
1555
+ unitDepthMap[bone] = pyRound(depth, 4);
1556
+ };
1557
+
1558
+ for (const [part, role] of Object.entries(roles)) {
1559
+ if (role !== 'unit') continue;
1560
+ const regSpec = opts.regions[part];
1561
+ if (regSpec === undefined) continue;
1562
+ const baseBone = typeof regSpec === 'string' ? regSpec : regSpec.bone;
1563
+ const stillBone = opts.stills[part]?.bone;
1564
+ const candidateBones = [baseBone, ...(stillBone === undefined ? [] : [stillBone])].filter((b) => faceshiftChildren.has(b));
1565
+ if (candidateBones.length === 0) continue;
1566
+
1567
+ for (const bName of candidateBones) {
1568
+ const explicitPartDepth = opts.turn.unitDepths?.[part];
1569
+ const explicitBoneDepth = opts.turn.unitDepths?.[bName];
1570
+ let resolved: number | null = null;
1571
+ if (typeof explicitPartDepth === 'number' && Number.isFinite(explicitPartDepth)) {
1572
+ resolved = explicitPartDepth;
1573
+ } else if (typeof explicitBoneDepth === 'number' && Number.isFinite(explicitBoneDepth)) {
1574
+ resolved = explicitBoneDepth;
1575
+ } else if (primarySurface !== undefined) {
1576
+ const pt = opts.boneLandmarks.get(bName) ?? opts.boneLandmarks.get(baseBone);
1577
+ if (pt !== undefined) {
1578
+ const { rec, ext, zRef } = primarySurface;
1579
+ const padX = Math.floor((ext.width - rec.w) / 2);
1580
+ const padY = Math.floor((ext.height - rec.h) / 2);
1581
+ const lx = Math.max(0, Math.min(ext.width - 1, Math.round(pt[0] - rec.x + padX)));
1582
+ const ly = Math.max(0, Math.min(ext.height - 1, Math.round(pt[1] - rec.y + padY)));
1583
+ const zUnit = levelToDepthZ(ext.data[(ly * ext.width + lx) * 4], rec.depth);
1584
+ resolved = pyRound(pivot + (zUnit - zRef), 4);
1585
+ }
1586
+ }
1587
+ if (resolved === null) {
1588
+ const rec = partMap.get(part);
1589
+ const art = opts.images.get(part);
1590
+ const scalar = rec?.depth === undefined ? undefined : opts.images.get(rec.depth.image);
1591
+ if (rec?.depth !== undefined && art !== undefined && scalar !== undefined) {
1592
+ resolved = sampleUnitPartDepth(scalar, art, rec.depth);
1593
+ }
1594
+ }
1595
+ if (resolved === null) {
1596
+ problems.push({
1597
+ code: 'RIG_TURN_UNIT_DEPTH_REQUIRED',
1598
+ object: `config.turn.roles.${part}`,
1599
+ detail: `declares role "unit" for "${part}" (bone "${bName}"), and neither a surface depth sheet nor a part depth sheet nor config.turn.unitDepths.${part} provides its depth`,
1600
+ });
1601
+ continue;
1602
+ }
1603
+ if ((bName === 'ear_r' || bName === 'ear_l') && primarySurface !== undefined) {
1604
+ const pt = opts.boneLandmarks.get(bName);
1605
+ if (pt !== undefined) {
1606
+ const { rec, ext, zRef } = primarySurface;
1607
+ const padX = Math.floor((ext.width - rec.w) / 2);
1608
+ const padY = Math.floor((ext.height - rec.h) / 2);
1609
+ const cheekX = pt[0] < rec.x + rec.w / 2 ? 0 : ext.width - 1;
1610
+ const cheekY = Math.max(0, Math.min(ext.height - 1, Math.round(pt[1] - rec.y + padY)));
1611
+ const cheekDepth = pyRound(pivot + (levelToDepthZ(ext.data[(cheekY * ext.width + cheekX) * 4], rec.depth) - zRef), 4);
1612
+ if (resolved > cheekDepth) {
1613
+ problems.push({
1614
+ code: 'RIG_TURN_EAR_DEPTH_BEHIND_CHEEK',
1615
+ object: `config.turn.unitDepths.${part}`,
1616
+ detail: `places "${bName}" at depth ${pyRound(resolved, 4)}, which is in front of the adjacent cheek edge (${cheekDepth}); an ear depth at or behind ${cheekDepth} is required so the ear recedes behind the cheek`,
1617
+ });
1618
+ continue;
1619
+ }
1620
+ }
1621
+ }
1622
+ addUnitBone(bName, resolved);
1623
+ }
1624
+ }
1625
+ refuseIfAny(problems);
1626
+
1627
+ // Order unitBones in rig.bones declaration order for determinism.
1628
+ const boneIndex = new Map(rig.bones.map((b, i) => [b.name, i]));
1629
+ unitBones.sort((a, b) => (boneIndex.get(a) ?? 0) - (boneIndex.get(b) ?? 0));
1630
+ const orderedUnitDepthMap: Record<string, number> = {};
1631
+ for (const b of unitBones) orderedUnitDepthMap[b] = unitDepthMap[b];
1632
+
1633
+ if (unitBones.length > 0) {
1634
+ motion.groups ??= {};
1635
+ motion.groups.turn_units = unitBones;
1636
+ }
1637
+
1638
+ const { yaw: yawMax, pitch: pitchMax } = resolveTurnBands(opts.turn);
1639
+ const buildAxisAnim = (kind: 'yaw' | 'pitch', maxDeg: number): void => {
1640
+ const animName = kind === 'yaw' ? 'turn' : 'tilt';
1641
+ const prop = kind === 'yaw' ? 'translatex' : 'translatey';
1642
+ const duration = pyRound(2 * maxDeg * TURN_DIAL_SCALE, 6);
1643
+ const step = maxDeg / 2;
1644
+ const angles = [-maxDeg, -step, 0, step, maxDeg];
1645
+ const keyList = angles.map((deg) => ({
1646
+ t: pyRound((deg + maxDeg) * TURN_DIAL_SCALE, 6),
1647
+ deg: pyRound(deg, 6),
1648
+ }));
1649
+
1650
+ const tracks: Array<{
1651
+ bone?: string;
1652
+ group?: string;
1653
+ property: string;
1654
+ keys: Array<{
1655
+ t: number;
1656
+ v?: number[] | Record<string, number[]>;
1657
+ derive?: {
1658
+ kind: 'yaw' | 'pitch';
1659
+ degrees: number;
1660
+ about?: number;
1661
+ carried?: number;
1662
+ depth?: number | Record<string, number>;
1663
+ narrowing?: boolean;
1664
+ };
1665
+ }>;
1666
+ }> = [
1667
+ {
1668
+ bone: FACESHIFT_BONE,
1669
+ property: prop,
1670
+ keys: keyList.map(({ t, deg }) =>
1671
+ deg === 0
1672
+ ? { t, v: [0] }
1673
+ : { t, derive: { kind, degrees: deg, depth: pivot, narrowing: false } },
1674
+ ),
1675
+ },
1676
+ ];
1677
+ if (hasRear && rearDepth !== 0) {
1678
+ tracks.push({
1679
+ bone: REAR_SHIFT_BONE,
1680
+ property: prop,
1681
+ keys: keyList.map(({ t, deg }) =>
1682
+ deg === 0
1683
+ ? { t, v: [0] }
1684
+ : { t, derive: { kind, degrees: deg, depth: rearDepth, narrowing: false } },
1685
+ ),
1686
+ });
1687
+ }
1688
+ for (const [part, { zRef }] of surfaceSheets) {
1689
+ const surfShift = pyRound(-zRef, 4);
1690
+ if (surfShift !== 0) {
1691
+ tracks.push({
1692
+ bone: turnSurfaceBone(part),
1693
+ property: prop,
1694
+ keys: keyList.map(({ t, deg }) =>
1695
+ deg === 0
1696
+ ? { t, v: [0] }
1697
+ : { t, derive: { kind, degrees: deg, depth: surfShift, narrowing: false } },
1698
+ ),
1699
+ });
1700
+ }
1701
+ }
1702
+ if (unitBones.length > 0) {
1703
+ tracks.push({
1704
+ group: 'turn_units',
1705
+ property: prop,
1706
+ keys: keyList.map(({ t, deg }) =>
1707
+ deg === 0
1708
+ ? { t, v: [0] }
1709
+ : {
1710
+ t,
1711
+ derive: {
1712
+ kind,
1713
+ degrees: deg,
1714
+ carried: pivot,
1715
+ depth: { ...orderedUnitDepthMap },
1716
+ narrowing: false,
1717
+ },
1718
+ },
1719
+ ),
1720
+ });
1721
+ }
1722
+
1723
+ const deform: Array<{
1724
+ slot: string;
1725
+ attachment: string;
1726
+ keys: Array<{
1727
+ t: number;
1728
+ vertices?: number[];
1729
+ transform?: {
1730
+ kind: 'yaw' | 'pitch';
1731
+ degrees: number;
1732
+ depth?: boolean;
1733
+ narrowing?: boolean;
1734
+ };
1735
+ }>;
1736
+ }> = [];
1737
+ for (const part of surfaceSheets.keys()) {
1738
+ deform.push({
1739
+ slot: part,
1740
+ attachment: part,
1741
+ keys: keyList.map(({ t, deg }) =>
1742
+ deg === 0
1743
+ ? { t }
1744
+ : {
1745
+ t,
1746
+ transform: {
1747
+ kind,
1748
+ degrees: deg,
1749
+ depth: true,
1750
+ narrowing: false,
1751
+ },
1752
+ },
1753
+ ),
1754
+ });
1755
+ }
1756
+
1757
+ motion.animations[animName] = {
1758
+ duration,
1759
+ loop: false,
1760
+ tracks,
1761
+ ...(deform.length > 0 ? { deform } : {}),
1762
+ };
1763
+ };
1764
+
1765
+ buildAxisAnim('yaw', yawMax);
1766
+ buildAxisAnim('pitch', pitchMax);
1767
+
1768
+ rig.constraints ??= [];
1769
+ const upsertSlider = (name: string, animName: string, dialBone: string, maxDeg: number): void => {
1770
+ const existing = rig.constraints?.find((c) => c.type === 'slider' && c.animation === animName);
1771
+ if (existing !== undefined) {
1772
+ existing.bone = dialBone;
1773
+ existing.property = 'rotate';
1774
+ existing.from = -maxDeg;
1775
+ existing.to = 0;
1776
+ existing.scale = TURN_DIAL_SCALE;
1777
+ existing.max = maxDeg;
1778
+ existing.local = true;
1779
+ existing.additive = true;
1780
+ existing.mix = 1;
1781
+ } else {
1782
+ rig.constraints?.push({
1783
+ name,
1784
+ type: 'slider',
1785
+ animation: animName,
1786
+ bone: dialBone,
1787
+ property: 'rotate',
1788
+ from: -maxDeg,
1789
+ to: 0,
1790
+ scale: TURN_DIAL_SCALE,
1791
+ max: maxDeg,
1792
+ local: true,
1793
+ additive: true,
1794
+ mix: 1,
1795
+ });
1796
+ }
1797
+ };
1798
+ upsertSlider('yaw', 'turn', YAW_DIAL_BONE, yawMax);
1799
+ upsertSlider('pitch', 'tilt', PITCH_DIAL_BONE, pitchMax);
1800
+ }