@rig-c/parts 1.70.1 → 1.71.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/rig.ts CHANGED
@@ -187,7 +187,7 @@ import { alphaAbove, crop, pad, type Raster } from './raster/index.ts';
187
187
  import { pyRound } from './round.ts';
188
188
  import { boneHeat, type HeatDust, heatInfluences, HEAT_LIMITS, heatRow, type HeatRow, latticeSilhouette, tracedSilhouette } from './heat.ts';
189
189
  import type { PosePoints } from './skeleton.ts';
190
- import { applyFaceSurfaceDeform, buildDepth25DTurn, FACE_RATED_BAND, FACESHIFT_BONE, PITCH_DIAL_BONE, REAR_SHIFT_BONE, resolveTurnGrid, TURN_BONE, TURN_CARRIER_TAG, TURN_GRID_MAX_TRIANGLES_PER_SLOT, TURN_GRID_MAX_TRIANGLES_TOTAL, TURN_PLACES, TURN_RIDER_APART, TURN_RIDER_TAGS, turnRiderDepth, type TurnSpec, turnConstraintName, turnFractions, turnNearest, turnSurfaceBone, turnTwin, YAW_DIAL_BONE } from './turn.ts';
190
+ import { applyFaceSurfaceDeform, BODY_REAR_SHIFT_BONE, BODY_YAW_DIAL_BONE, BODYSHIFT_BONE, buildDepth25DTurn, FACE_RATED_BAND, FACESHIFT_BONE, PITCH_DIAL_BONE, REAR_SHIFT_BONE, resolveTurnGrid, TURN_BONE, TURN_CARRIER_TAG, TURN_GRID_MAX_TRIANGLES_PER_SLOT, TURN_GRID_MAX_TRIANGLES_TOTAL, TURN_PLACES, TURN_RIDER_APART, TURN_RIDER_TAGS, turnRiderDepth, type TurnRole, type TurnSpec, turnConstraintName, turnFractions, turnNearest, turnSurfaceBone, turnTwin, YAW_DIAL_BONE } from './turn.ts';
191
191
  import { DISTANCE_EXPONENT, type FloorForm, type Influence, influences, type InfluenceLimits, type MeshInfluences, meshLimits, type Segment } from './weights.ts';
192
192
 
193
193
  /** Transparent pixels added round every part image — the reference's `PAD`. */
@@ -1537,6 +1537,18 @@ export function buildWaistPairSource(
1537
1537
  if (!B.has(s.against)) fail('RIG_NAME_RESOLVES', `${at}.against`, `names the bone "${s.against}", which config.bones does not declare`);
1538
1538
  });
1539
1539
  }
1540
+ if (cfg.turn?.roles !== undefined) {
1541
+ for (const part of Object.keys(cfg.turn.roles)) {
1542
+ if (!byPart.has(part)) fail('RIG_PART_PRESENT', `config.turn.roles.${part}`, `names the part "${part}", which is not in parts.json; ${where(part)}`);
1543
+ else if (!(part in cfg.meshes) && !(part in cfg.regions)) fail('RIG_NAME_RESOLVES', `config.turn.roles.${part}`, `names "${part}", which neither config.meshes nor config.regions attaches`);
1544
+ }
1545
+ }
1546
+ if (cfg.turn?.body !== undefined) {
1547
+ for (const part of Object.keys(cfg.turn.body.roles)) {
1548
+ if (!byPart.has(part)) fail('RIG_PART_PRESENT', `config.turn.body.roles.${part}`, `names the part "${part}", which is not in parts.json; ${where(part)}`);
1549
+ else if (!(part in cfg.meshes) && !(part in cfg.regions)) fail('RIG_NAME_RESOLVES', `config.turn.body.roles.${part}`, `names "${part}", which neither config.meshes nor config.regions attaches`);
1550
+ }
1551
+ }
1540
1552
  for (const p of parts.parts) {
1541
1553
  const inM = p.name in cfg.meshes;
1542
1554
  const inR = p.name in cfg.regions;
@@ -1636,16 +1648,21 @@ export function buildWaistPairSource(
1636
1648
  moveKeysToControls(motion, controls);
1637
1649
  // Parents first: a depth-first walk over the table in insertion order, each
1638
1650
  // bone after its parent — which puts every control directly above its bone.
1639
- const ordered: Bone[] = [];
1640
- const done = new Set<string>();
1641
- const visit = (n: string): void => {
1642
- if (done.has(n)) return;
1643
- const b = B.get(n) as Bone;
1644
- if (b.parent !== null) visit(b.parent);
1645
- done.add(n);
1646
- ordered.push(b);
1651
+ const orderParentsFirst = <T extends { name: string; parent?: string | null }>(table: ReadonlyMap<string, T>): T[] => {
1652
+ const out: T[] = [];
1653
+ const done = new Set<string>();
1654
+ const visit = (n: string): void => {
1655
+ if (done.has(n)) return;
1656
+ const b = table.get(n);
1657
+ if (b === undefined) return;
1658
+ if (b.parent !== null && b.parent !== undefined) visit(b.parent);
1659
+ done.add(n);
1660
+ out.push(b);
1661
+ };
1662
+ for (const n of table.keys()) visit(n);
1663
+ return out;
1647
1664
  };
1648
- for (const n of B.keys()) visit(n);
1665
+ const ordered = orderParentsFirst(B);
1649
1666
 
1650
1667
  const spineX = (x: number): number => x - CX;
1651
1668
  const spineY = (y: number): number => cropToSpineY(y, H);
@@ -1878,8 +1895,9 @@ export function buildWaistPairSource(
1878
1895
  return;
1879
1896
  }
1880
1897
  const art = images.get(part);
1898
+ const isTurnPart = cfg.turn?.roles?.[part] !== undefined || cfg.turn?.body?.roles?.[part] !== undefined;
1881
1899
  const image =
1882
- cfg.turn?.roles?.[part] !== undefined && art !== undefined && art.width === scalar.width && art.height === scalar.height
1900
+ isTurnPart && art !== undefined && art.width === scalar.width && art.height === scalar.height
1883
1901
  ? padTurnDepthSheet(scalar, art, PAD)
1884
1902
  : padDepthSheet(scalar, PAD);
1885
1903
  outImages.push([reference.image, image]);
@@ -2413,29 +2431,46 @@ export function buildWaistPairSource(
2413
2431
  }
2414
2432
  }
2415
2433
 
2434
+ const isHeadSurface = cfg.turn?.roles?.[p.name] === 'surface';
2435
+ const isBodySurface = cfg.turn?.body?.roles?.[p.name] === 'surface';
2436
+ if (isHeadSurface || isBodySurface) {
2437
+ const roleField = isHeadSurface ? `config.turn.roles.${p.name}` : `config.turn.body.roles.${p.name}`;
2438
+ if (cfg.meshes[p.name] !== undefined) {
2439
+ fail(
2440
+ 'CONFIG_TURN_SURFACE_REGION',
2441
+ roleField,
2442
+ `declares "${p.name}" as "surface", and config.meshes.${p.name} also declares a mesh for "${p.name}"; a 2.5D turn "surface" part must be attached in config.regions so the rig stage can promote it to a depth-backed grid mesh`,
2443
+ );
2444
+ continue;
2445
+ }
2446
+ if (p.depth === undefined || scalar === undefined) {
2447
+ fail(
2448
+ 'RIG_TURN_SURFACE_DEPTH_REQUIRED',
2449
+ roleField,
2450
+ `declares role "surface" for "${p.name}", which requires a depth sheet in parts.json; run assemble with config.depth or provide "${p.name}_depth.png"`,
2451
+ );
2452
+ continue;
2453
+ }
2454
+ }
2455
+
2416
2456
  const mesh = cfg.meshes[p.name];
2417
2457
  if (mesh === undefined) {
2418
- if (cfg.turn?.roles?.[p.name] === 'surface') {
2419
- if (p.depth === undefined || scalar === undefined) {
2420
- fail(
2421
- 'RIG_TURN_SURFACE_DEPTH_REQUIRED',
2422
- `config.turn.roles.${p.name}`,
2423
- `declares role "surface" for "${p.name}", which requires a depth sheet in parts.json; run assemble with config.depth or provide "${p.name}_depth.png"`,
2424
- );
2425
- continue;
2426
- }
2427
- const [cols, rows] = resolveTurnGrid(cfg.turn, p.name);
2458
+ if (isHeadSurface || isBodySurface) {
2459
+ const [cols, rows] = isHeadSurface
2460
+ ? resolveTurnGrid(cfg.turn!, p.name)
2461
+ : resolveTurnGrid(cfg.turn!.body!, p.name, cfg.turn);
2428
2462
  const triCount = 2 * (cols - 1) * (rows - 1);
2463
+ const gridField = isHeadSurface ? `config.turn.grid (${p.name})` : `config.turn.body.grid (${p.name})`;
2429
2464
  if (triCount > TURN_GRID_MAX_TRIANGLES_PER_SLOT) {
2430
2465
  fail(
2431
2466
  'RIG_TURN_GRID_BUDGET',
2432
- `config.turn.grid (${p.name})`,
2467
+ gridField,
2433
2468
  `specifies a ${cols}x${rows} grid (${triCount} triangles), which exceeds the per-slot budget of ${TURN_GRID_MAX_TRIANGLES_PER_SLOT} triangles`,
2434
2469
  );
2435
2470
  continue;
2436
2471
  }
2437
2472
  const surfBone = turnSurfaceBone(p.name);
2438
- const { sources: _sources, ...coreDepth } = p.depth;
2473
+ const { sources: _sources, ...coreDepth } = p.depth!;
2439
2474
  const gridAtt: Omit<MeshAttachment, 'uvs' | 'triangles' | 'hull' | 'weights'> = {
2440
2475
  type: 'mesh',
2441
2476
  image: file,
@@ -3076,135 +3111,300 @@ export function buildWaistPairSource(
3076
3111
  // ---- the turn control (issue #251) -------------------------------------
3077
3112
  // Only when the config has `turn`: every other build adds nothing here, so no byte moves.
3078
3113
  if (cfg.turn !== undefined) refuseIfAny(turnRiderProblems(cfg.turn, cfg, parts.parts));
3079
- const turned = cfg.turn === undefined || cfg.turn.roles !== undefined ? null : applyTurn(cfg.turn, parts.parts.map((p) => p.name), bones, rebound, skin, cfg.constraints ?? [], declaredFront, slotFamilies, stops, turnOrderByDeclaration);
3114
+ const turned = cfg.turn === undefined || cfg.turn.roles !== undefined || cfg.turn.depths === undefined ? null : applyTurn(cfg.turn, parts.parts.map((p) => p.name), bones, rebound, skin, cfg.constraints ?? [], declaredFront, slotFamilies, stops, turnOrderByDeclaration);
3080
3115
  for (const row of turned?.rows ?? []) for (const t of row.twins) layoutAlias(explicitLayout.plan, t.bone, t.twin);
3081
3116
  const finalSlots = frontSlots.length === 0 ? explicitLayout.slots : spliceFrontSlots(explicitLayout.slots, frontSlots, frontTargets, slotFamilies);
3082
3117
  let generatedMeshBudget: { meshSlots: number; meshTriangles: number } | null = null;
3083
- if (cfg.turn?.roles !== undefined) {
3084
- const roles = cfg.turn.roles;
3118
+ if (cfg.turn?.roles !== undefined || cfg.turn?.body !== undefined) {
3085
3119
  let totalGridTriangles = 0;
3086
- const headIdx = bones.findIndex((b) => b.name === 'head');
3087
- if (headIdx === -1) {
3088
- fail('RIG_TURN_HEAD_BONE', 'config.turn.roles', 'requires a declared "head" bone in config.bones to parent the 2.5D turn carriers');
3089
- } else {
3090
- const headTransform = world.get('head') as BoneTransform;
3091
- const headTurnDeg = turn.get('head') ?? 0;
3092
- const surfBones: RigBone[] = [];
3093
- for (const p of parts.parts) {
3094
- if (roles[p.name] !== 'surface' || cfg.meshes[p.name] !== undefined) continue;
3095
- const [cols, rows] = resolveTurnGrid(cfg.turn, p.name);
3096
- totalGridTriangles += 2 * (cols - 1) * (rows - 1);
3097
- const cx = p.x + p.w / 2;
3098
- const cy = p.y + p.h / 2;
3099
- const [lx, ly] = toBoneLocal(headTransform, spineX(cx), spineY(cy));
3100
- const rot = places(normaliseDegrees(-headTurnDeg));
3101
- surfBones.push({
3102
- name: turnSurfaceBone(p.name),
3103
- parent: FACESHIFT_BONE,
3104
- x: places(lx),
3105
- y: places(ly),
3106
- ...(rot === 0 ? {} : { rotation: rot }),
3107
- });
3108
- }
3109
- if (totalGridTriangles > TURN_GRID_MAX_TRIANGLES_TOTAL) {
3110
- fail(
3111
- 'RIG_TURN_GRID_BUDGET',
3112
- 'config.turn.grid',
3113
- `allocates ${totalGridTriangles} total grid triangles across surface slots, which exceeds the total budget of ${TURN_GRID_MAX_TRIANGLES_TOTAL} triangles`,
3114
- );
3115
- }
3116
- const hasRear = Object.values(roles).includes('rear');
3117
- const carriers: RigBone[] = [
3118
- { name: FACESHIFT_BONE, parent: 'head', x: 0, y: 0 },
3119
- ...(hasRear ? [{ name: REAR_SHIFT_BONE, parent: 'head', x: 0, y: 0 }] : []),
3120
- ...surfBones,
3121
- ];
3122
- bones.splice(headIdx + 1, 0, ...carriers);
3123
- const existingNames = new Set(bones.map((b) => b.name));
3124
- if (!existingNames.has(YAW_DIAL_BONE)) bones.push({ name: YAW_DIAL_BONE, parent: ROOT_BONE, x: 0, y: 0 });
3125
- if (!existingNames.has(PITCH_DIAL_BONE)) bones.push({ name: PITCH_DIAL_BONE, parent: ROOT_BONE, x: 0, y: 0 });
3126
-
3127
- const boneByName = new Map(bones.map((b) => [b.name, b]));
3128
- const slotByName = new Map(finalSlots.map((s) => [s.name, s]));
3129
- const reparentFromHead = (boneName: string, targetParent: string): void => {
3130
- const b = boneByName.get(boneName);
3131
- if (b !== undefined && b.parent === 'head') b.parent = targetParent;
3132
- };
3133
- const rebindPartToCarrier = (part: string, carrierBone: string): void => {
3134
- const sl = slotByName.get(part);
3135
- if (sl !== undefined && sl.bone === 'head') sl.bone = carrierBone;
3136
- const stillSl = slotByName.get(`${part}${STILL_SUFFIX}`);
3137
- if (stillSl !== undefined) {
3138
- if (stillSl.bone === 'head') stillSl.bone = carrierBone;
3139
- else reparentFromHead(stillSl.bone, carrierBone);
3120
+ const slotByName = new Map(finalSlots.map((s) => [s.name, s]));
3121
+
3122
+ if (cfg.turn.roles !== undefined) {
3123
+ const roles = cfg.turn.roles;
3124
+ const headIdx = bones.findIndex((b) => b.name === 'head');
3125
+ if (headIdx === -1) {
3126
+ fail('RIG_TURN_HEAD_BONE', 'config.turn.roles', 'requires a declared "head" bone in config.bones to parent the 2.5D turn carriers');
3127
+ } else {
3128
+ const headTransform = world.get('head') as BoneTransform;
3129
+ const headTurnDeg = turn.get('head') ?? 0;
3130
+ const surfBones: RigBone[] = [];
3131
+ for (const p of parts.parts) {
3132
+ if (roles[p.name] !== 'surface') continue;
3133
+ const [cols, rows] = resolveTurnGrid(cfg.turn, p.name);
3134
+ totalGridTriangles += 2 * (cols - 1) * (rows - 1);
3135
+ const cx = p.x + p.w / 2;
3136
+ const cy = p.y + p.h / 2;
3137
+ const [lx, ly] = toBoneLocal(headTransform, spineX(cx), spineY(cy));
3138
+ const rot = places(normaliseDegrees(-headTurnDeg));
3139
+ surfBones.push({
3140
+ name: turnSurfaceBone(p.name),
3141
+ parent: FACESHIFT_BONE,
3142
+ x: places(lx),
3143
+ y: places(ly),
3144
+ ...(rot === 0 ? {} : { rotation: rot }),
3145
+ });
3140
3146
  }
3141
- const atts = skin[part] ?? {};
3142
- for (const att of Object.values(atts)) {
3143
- if (!('type' in att) || att.weights === undefined) continue;
3144
- for (const row of att.weights) {
3145
- for (const w of row) {
3146
- if (w.bone === 'head') {
3147
- w.bone = carrierBone;
3148
- } else {
3149
- // Find the ancestor of w.bone that hangs directly under head and reparent it to carrierBone.
3150
- let cur = boneByName.get(w.bone);
3151
- while (cur !== undefined && cur.parent !== undefined && cur.parent !== 'head' && cur.parent !== carrierBone) {
3152
- cur = boneByName.get(cur.parent);
3147
+ const hasRear = Object.values(roles).includes('rear');
3148
+ const carriers: RigBone[] = [
3149
+ { name: FACESHIFT_BONE, parent: 'head', x: 0, y: 0 },
3150
+ ...(hasRear ? [{ name: REAR_SHIFT_BONE, parent: 'head', x: 0, y: 0 }] : []),
3151
+ ...surfBones,
3152
+ ];
3153
+ bones.splice(headIdx + 1, 0, ...carriers);
3154
+ const existingNames = new Set(bones.map((b) => b.name));
3155
+ if (!existingNames.has(YAW_DIAL_BONE)) bones.push({ name: YAW_DIAL_BONE, parent: ROOT_BONE, x: 0, y: 0 });
3156
+ if (!existingNames.has(PITCH_DIAL_BONE)) bones.push({ name: PITCH_DIAL_BONE, parent: ROOT_BONE, x: 0, y: 0 });
3157
+
3158
+ const boneByName = new Map(bones.map((b) => [b.name, b]));
3159
+ const reparentFromHead = (boneName: string, targetParent: string): void => {
3160
+ const b = boneByName.get(boneName);
3161
+ if (b !== undefined && b.parent === 'head') b.parent = targetParent;
3162
+ };
3163
+ const rebindPartToCarrier = (part: string, carrierBone: string): void => {
3164
+ const sl = slotByName.get(part);
3165
+ if (sl !== undefined && sl.bone === 'head') sl.bone = carrierBone;
3166
+ const stillSl = slotByName.get(`${part}${STILL_SUFFIX}`);
3167
+ if (stillSl !== undefined) {
3168
+ if (stillSl.bone === 'head') stillSl.bone = carrierBone;
3169
+ else reparentFromHead(stillSl.bone, carrierBone);
3170
+ }
3171
+ const atts = skin[part] ?? {};
3172
+ for (const att of Object.values(atts)) {
3173
+ if (!('type' in att) || att.weights === undefined) continue;
3174
+ for (const row of att.weights) {
3175
+ for (const w of row) {
3176
+ if (w.bone === 'head') {
3177
+ w.bone = carrierBone;
3178
+ } else {
3179
+ let cur = boneByName.get(w.bone);
3180
+ while (cur !== undefined && cur.parent !== undefined && cur.parent !== 'head' && cur.parent !== carrierBone) {
3181
+ cur = boneByName.get(cur.parent);
3182
+ }
3183
+ if (cur !== undefined && cur.parent === 'head') cur.parent = carrierBone;
3153
3184
  }
3154
- if (cur !== undefined && cur.parent === 'head') cur.parent = carrierBone;
3155
3185
  }
3156
3186
  }
3157
3187
  }
3188
+ };
3189
+
3190
+ for (const [part, role] of Object.entries(roles)) {
3191
+ if (role === 'unit') {
3192
+ if (cfg.regions[part] !== undefined) {
3193
+ const bName = regionBone(cfg.regions[part]);
3194
+ if (bName === 'head') {
3195
+ const sl = slotByName.get(part);
3196
+ if (sl !== undefined) sl.bone = FACESHIFT_BONE;
3197
+ } else {
3198
+ reparentFromHead(bName, FACESHIFT_BONE);
3199
+ }
3200
+ const st = stills[part];
3201
+ if (st !== undefined) reparentFromHead(st.bone, FACESHIFT_BONE);
3202
+ } else if (cfg.meshes[part] !== undefined) {
3203
+ rebindPartToCarrier(part, FACESHIFT_BONE);
3204
+ }
3205
+ } else if (role === 'rear' && hasRear) {
3206
+ if (cfg.regions[part] !== undefined) {
3207
+ const bName = regionBone(cfg.regions[part]);
3208
+ if (bName === 'head') {
3209
+ const sl = slotByName.get(part);
3210
+ if (sl !== undefined) sl.bone = REAR_SHIFT_BONE;
3211
+ } else {
3212
+ reparentFromHead(bName, REAR_SHIFT_BONE);
3213
+ }
3214
+ } else if (cfg.meshes[part] !== undefined) {
3215
+ rebindPartToCarrier(part, REAR_SHIFT_BONE);
3216
+ }
3217
+ }
3218
+ }
3219
+ }
3220
+ }
3221
+
3222
+ if (cfg.turn.body !== undefined) {
3223
+ const bodySpec = cfg.turn.body;
3224
+ const bodyRoles = bodySpec.roles;
3225
+ const bodyRoot = bodySpec.bone ?? (bones.some((b) => b.name === 'chest') ? 'chest' : 'hip');
3226
+ const bodyIdx = bones.findIndex((b) => b.name === bodyRoot);
3227
+ if (bodyIdx === -1) {
3228
+ fail('RIG_TURN_HEAD_BONE', 'config.turn.body', `requires a declared "${bodyRoot}" bone in config.bones to parent the 2.5D body turn carriers`);
3229
+ } else {
3230
+ const bodyTransform = world.get(bodyRoot) as BoneTransform;
3231
+ const bodyTurnDeg = turn.get(bodyRoot) ?? 0;
3232
+ const surfBones: RigBone[] = [];
3233
+ for (const p of parts.parts) {
3234
+ if (bodyRoles[p.name] !== 'surface') continue;
3235
+ const [cols, rows] = resolveTurnGrid(bodySpec, p.name, cfg.turn);
3236
+ totalGridTriangles += 2 * (cols - 1) * (rows - 1);
3237
+ const cx = p.x + p.w / 2;
3238
+ const cy = p.y + p.h / 2;
3239
+ const [lx, ly] = toBoneLocal(bodyTransform, spineX(cx), spineY(cy));
3240
+ const rot = places(normaliseDegrees(-bodyTurnDeg));
3241
+ surfBones.push({
3242
+ name: turnSurfaceBone(p.name),
3243
+ parent: BODYSHIFT_BONE,
3244
+ x: places(lx),
3245
+ y: places(ly),
3246
+ ...(rot === 0 ? {} : { rotation: rot }),
3247
+ });
3248
+ }
3249
+ const hasRear = Object.values(bodyRoles).includes('rear');
3250
+ const carriers: RigBone[] = [
3251
+ { name: BODYSHIFT_BONE, parent: bodyRoot, x: 0, y: 0 },
3252
+ ...(hasRear ? [{ name: BODY_REAR_SHIFT_BONE, parent: bodyRoot, x: 0, y: 0 }] : []),
3253
+ ...surfBones,
3254
+ ];
3255
+ bones.splice(bodyIdx + 1, 0, ...carriers);
3256
+ const bodyDialBone = bodySpec.slider ?? BODY_YAW_DIAL_BONE;
3257
+ if (!bones.some((b) => b.name === bodyDialBone)) {
3258
+ bones.push({ name: bodyDialBone, parent: ROOT_BONE, x: 0, y: 0 });
3158
3259
  }
3159
- };
3160
3260
 
3161
- for (const [part, role] of Object.entries(roles)) {
3162
- if (role === 'unit') {
3163
- if (cfg.regions[part] !== undefined) {
3164
- const bName = regionBone(cfg.regions[part]);
3165
- if (bName === 'head') {
3166
- const sl = slotByName.get(part);
3167
- if (sl !== undefined) sl.bone = FACESHIFT_BONE;
3168
- } else {
3169
- reparentFromHead(bName, FACESHIFT_BONE);
3261
+ const boneByName = new Map(bones.map((b) => [b.name, b]));
3262
+ const torsoRoots = new Set<string>([bodyRoot, 'chest', 'hip', `${bodyRoot}${CONTROL_SUFFIX}`, `chest${CONTROL_SUFFIX}`, `hip${CONTROL_SUFFIX}`]);
3263
+ const headExclusion = new Set<string>(['head', `head${CONTROL_SUFFIX}`, 'neck', `neck${CONTROL_SUFFIX}`, FACESHIFT_BONE, REAR_SHIFT_BONE]);
3264
+ const reparentFromTorso = (boneName: string, targetParent: string): void => {
3265
+ if (torsoRoots.has(boneName) || headExclusion.has(boneName)) return;
3266
+ let cur = boneByName.get(boneName);
3267
+ while (
3268
+ cur !== undefined &&
3269
+ cur.parent !== undefined &&
3270
+ !torsoRoots.has(cur.parent) &&
3271
+ cur.parent !== targetParent &&
3272
+ !headExclusion.has(cur.parent)
3273
+ ) {
3274
+ cur = boneByName.get(cur.parent);
3275
+ }
3276
+ if (cur !== undefined && cur.parent !== undefined && torsoRoots.has(cur.parent) && !torsoRoots.has(cur.name) && !headExclusion.has(cur.name)) {
3277
+ const oldParent = cur.parent;
3278
+ if (oldParent !== bodyRoot) {
3279
+ const oldTr = world.get(oldParent);
3280
+ if (oldTr !== undefined) {
3281
+ const [wx, wy] = toWorld(oldTr, cur.x, cur.y);
3282
+ const [lx, ly] = toBoneLocal(bodyTransform, wx, wy);
3283
+ cur.x = places(lx);
3284
+ cur.y = places(ly);
3285
+ }
3286
+ }
3287
+ cur.parent = targetParent;
3288
+ }
3289
+ };
3290
+ const rebindBodyPartToCarrier = (part: string, carrierBone: string): void => {
3291
+ const sl = slotByName.get(part);
3292
+ if (sl !== undefined && torsoRoots.has(sl.bone)) sl.bone = carrierBone;
3293
+ const stillSl = slotByName.get(`${part}${STILL_SUFFIX}`);
3294
+ if (stillSl !== undefined) {
3295
+ if (torsoRoots.has(stillSl.bone)) stillSl.bone = carrierBone;
3296
+ else reparentFromTorso(stillSl.bone, carrierBone);
3297
+ }
3298
+ const atts = skin[part] ?? {};
3299
+ for (const att of Object.values(atts)) {
3300
+ if (!('type' in att) || att.weights === undefined) continue;
3301
+ for (const row of att.weights) {
3302
+ for (const w of row) {
3303
+ if (torsoRoots.has(w.bone)) {
3304
+ if (w.bone !== bodyRoot) {
3305
+ const oldTr = world.get(w.bone);
3306
+ if (oldTr !== undefined) {
3307
+ const [wx, wy] = toWorld(oldTr, w.x, w.y);
3308
+ const [lx, ly] = toBoneLocal(bodyTransform, wx, wy);
3309
+ w.x = places(lx);
3310
+ w.y = places(ly);
3311
+ }
3312
+ }
3313
+ w.bone = carrierBone;
3314
+ } else {
3315
+ reparentFromTorso(w.bone, carrierBone);
3316
+ }
3317
+ }
3170
3318
  }
3171
- const st = stills[part];
3172
- if (st !== undefined) reparentFromHead(st.bone, FACESHIFT_BONE);
3173
- } else if (cfg.meshes[part] !== undefined) {
3174
- rebindPartToCarrier(part, FACESHIFT_BONE);
3175
3319
  }
3176
- } else if (role === 'rear' && hasRear) {
3177
- if (cfg.regions[part] !== undefined) {
3178
- const bName = regionBone(cfg.regions[part]);
3179
- if (bName === 'head') {
3180
- const sl = slotByName.get(part);
3181
- if (sl !== undefined) sl.bone = REAR_SHIFT_BONE;
3182
- } else {
3183
- reparentFromHead(bName, REAR_SHIFT_BONE);
3320
+ };
3321
+
3322
+ for (const [part, role] of Object.entries(bodyRoles)) {
3323
+ if (role === 'unit') {
3324
+ if (cfg.regions[part] !== undefined) {
3325
+ const bName = regionBone(cfg.regions[part]);
3326
+ if (torsoRoots.has(bName)) {
3327
+ const sl = slotByName.get(part);
3328
+ if (sl !== undefined) sl.bone = BODYSHIFT_BONE;
3329
+ if (bName !== bodyRoot) {
3330
+ const oldTr = world.get(bName);
3331
+ if (oldTr !== undefined) {
3332
+ for (const att of Object.values(skin[part] ?? {})) {
3333
+ if (!('type' in att)) {
3334
+ const [wx, wy] = toWorld(oldTr, att.x, att.y);
3335
+ const [lx, ly] = toBoneLocal(bodyTransform, wx, wy);
3336
+ att.x = places(lx);
3337
+ att.y = places(ly);
3338
+ }
3339
+ }
3340
+ }
3341
+ }
3342
+ } else {
3343
+ reparentFromTorso(bName, BODYSHIFT_BONE);
3344
+ }
3345
+ const st = stills[part];
3346
+ if (st !== undefined) reparentFromTorso(st.bone, BODYSHIFT_BONE);
3347
+ } else if (cfg.meshes[part] !== undefined) {
3348
+ rebindBodyPartToCarrier(part, BODYSHIFT_BONE);
3349
+ }
3350
+ } else if (role === 'rear' && hasRear) {
3351
+ if (cfg.regions[part] !== undefined) {
3352
+ const bName = regionBone(cfg.regions[part]);
3353
+ if (torsoRoots.has(bName)) {
3354
+ const sl = slotByName.get(part);
3355
+ if (sl !== undefined) sl.bone = BODY_REAR_SHIFT_BONE;
3356
+ if (bName !== bodyRoot) {
3357
+ const oldTr = world.get(bName);
3358
+ if (oldTr !== undefined) {
3359
+ for (const att of Object.values(skin[part] ?? {})) {
3360
+ if (!('type' in att)) {
3361
+ const [wx, wy] = toWorld(oldTr, att.x, att.y);
3362
+ const [lx, ly] = toBoneLocal(bodyTransform, wx, wy);
3363
+ att.x = places(lx);
3364
+ att.y = places(ly);
3365
+ }
3366
+ }
3367
+ }
3368
+ }
3369
+ } else {
3370
+ reparentFromTorso(bName, BODY_REAR_SHIFT_BONE);
3371
+ }
3372
+ } else if (cfg.meshes[part] !== undefined) {
3373
+ rebindBodyPartToCarrier(part, BODY_REAR_SHIFT_BONE);
3184
3374
  }
3185
- } else if (cfg.meshes[part] !== undefined) {
3186
- rebindPartToCarrier(part, REAR_SHIFT_BONE);
3187
3375
  }
3188
3376
  }
3189
3377
  }
3378
+ }
3190
3379
 
3191
- let meshSlots = 0;
3192
- let meshTriangles = 0;
3193
- for (const atts of Object.values(skin)) {
3194
- let slotHasMesh = false;
3195
- for (const att of Object.values(atts)) {
3196
- if (!('type' in att)) continue;
3197
- slotHasMesh = true;
3198
- const tris =
3199
- att.generator?.kind === 'grid'
3200
- ? 2 * (att.generator.cols - 1) * (att.generator.rows - 1)
3201
- : (att.triangles?.length ?? 0) / 3;
3202
- if (tris > meshTriangles) meshTriangles = tris;
3203
- }
3204
- if (slotHasMesh) meshSlots++;
3380
+ if (cfg.turn.roles !== undefined || cfg.turn.body !== undefined) {
3381
+ bones.splice(0, bones.length, ...orderParentsFirst(new Map(bones.map((b) => [b.name, b]))));
3382
+ }
3383
+
3384
+ if (totalGridTriangles > TURN_GRID_MAX_TRIANGLES_TOTAL) {
3385
+ fail(
3386
+ 'RIG_TURN_GRID_BUDGET',
3387
+ 'config.turn.grid',
3388
+ `allocates ${totalGridTriangles} total grid triangles across surface slots, which exceeds the total budget of ${TURN_GRID_MAX_TRIANGLES_TOTAL} triangles`,
3389
+ );
3390
+ }
3391
+
3392
+ let meshSlots = 0;
3393
+ let meshTriangles = 0;
3394
+ for (const atts of Object.values(skin)) {
3395
+ let slotHasMesh = false;
3396
+ for (const att of Object.values(atts)) {
3397
+ if (!('type' in att)) continue;
3398
+ slotHasMesh = true;
3399
+ const tris =
3400
+ att.generator?.kind === 'grid'
3401
+ ? 2 * (att.generator.cols - 1) * (att.generator.rows - 1)
3402
+ : (att.triangles?.length ?? 0) / 3;
3403
+ if (tris > meshTriangles) meshTriangles = tris;
3205
3404
  }
3206
- generatedMeshBudget = { meshSlots, meshTriangles };
3405
+ if (slotHasMesh) meshSlots++;
3207
3406
  }
3407
+ generatedMeshBudget = { meshSlots, meshTriangles };
3208
3408
  }
3209
3409
  // issue #536: every row names the bones the emitted attachment binds, read once the turn and the stops have
3210
3410
  // rebound the weights; a rig with neither rebinds nothing, so this reads back the names each row already holds.
@@ -3265,7 +3465,7 @@ export function buildWaistPairSource(
3265
3465
  for (const [bName, bEntry] of B) {
3266
3466
  boneLandmarks.set(bName, [bEntry.x, bEntry.y]);
3267
3467
  }
3268
- if (cfg.turn?.roles !== undefined) {
3468
+ if (cfg.turn?.roles !== undefined || cfg.turn?.body !== undefined) {
3269
3469
  buildDepth25DTurn(rig, motion, {
3270
3470
  turn: cfg.turn,
3271
3471
  parts: parts.parts,
@@ -3280,7 +3480,7 @@ export function buildWaistPairSource(
3280
3480
  if (cfg.waist !== undefined) refuseSharedWaistMotion(merged, cfg.waist.top, cfg.waist.bone);
3281
3481
  remapWaistDeforms(merged, waistTargets, waistRegionTargets, waistBindingsBeforeRebind);
3282
3482
  expandSetAttachmentKeys(rig, sets, merged);
3283
- if (cfg.turn?.roles !== undefined) {
3483
+ if (cfg.turn?.roles !== undefined || cfg.turn?.body !== undefined) {
3284
3484
  buildDepth25DTurn(rig, merged, {
3285
3485
  turn: cfg.turn,
3286
3486
  parts: parts.parts,
@@ -3290,7 +3490,8 @@ export function buildWaistPairSource(
3290
3490
  boneLandmarks,
3291
3491
  hasAlternateFaceSet,
3292
3492
  });
3293
- } else {
3493
+ }
3494
+ if (cfg.turn?.roles === undefined && (cfg.turn === undefined || cfg.turn.body === undefined || cfg.turn.depths !== undefined)) {
3294
3495
  applyFaceSurfaceDeform(rig, merged, {
3295
3496
  imageWidth: faceImg?.width,
3296
3497
  imageHeight: faceImg?.height,