Skip to content

Turn one illustration into an animated 2D mesh avatar with a coding agent and a local editor

Keep arm movement from leaking into tracked torso lean

shinshin86e9de7e429780
diff --git a/e2e/live-streaming.spec.ts b/e2e/live-streaming.spec.ts
index e48cc2a..1ad2345 100644
--- a/e2e/live-streaming.spec.ts
+++ b/e2e/live-streaming.spec.ts
@@ -302,13 +302,16 @@ test('optional body and arm tracking maps mirrored joints, calibrates and releas
}})};
self.exports.PoseLandmarker={createFromOptions:async()=>({close(){self.bodyTestClosed=true;self.postMessage({type:'pose-close-test'});},setOptions:async()=>{},detectForVideo(){
self.postMessage({type:'pose-test',mode});if(mode==='lost')return {landmarks:[]};
- const moved=mode!=='neutral',shift=moved?.1:0,point=(x,y,visibility=1)=>({x,y,visibility,presence:1});
+ const moved=mode!=='neutral',armOnly=['arm-only','hips-hidden'].includes(mode),shift=moved&&!armOnly?.1:0,point=(x,y,visibility=1)=>({x,y,visibility,presence:1});
const l=Array.from({length:33},()=>point(.5,.5,0));l[11]=point(.65+shift,.3);l[12]=point(.35+shift,.3);
+ l[23]=point(.6+shift,.7);l[24]=point(.4+shift,.7);
for(let i=0;i<2;i++){
- const sign=i===0?1:-1,a=(moved&&i===0?30:0)*Math.PI/180,b=a-(moved&&i===0?90:0)*Math.PI/180;
+ const sign=i===0?1:-1,a=(moved&&i===0?(armOnly?60:30):0)*Math.PI/180,b=a-(moved&&i===0?90:0)*Math.PI/180;
const s=l[11+i],e=point(s.x+Math.sin(a)*.2/(4/3),s.y+Math.cos(a)*.2);
l[13+i]=e;l[15+i]=point(e.x+Math.sin(b)*.2/(4/3),e.y+Math.cos(b)*.2);
}
+ if(armOnly){l[11].x+=.01;l[11].y-=.02;}
+ if(mode==='hips-hidden'){l[23].visibility=0;l[24].visibility=0;}
if(mode==='wrist-hidden')l[15].visibility=0;
if(mode==='elbow-hidden')l[13].visibility=0;
return {landmarks:[l]};
@@ -342,8 +345,19 @@ test('optional body and arm tracking maps mirrored joints, calibrates and releas
await expect.poll(async () => (await stats()).results).toBeGreaterThan(5); expect((await stats()).poses).toBe(0);
await toggle.check();
await expect.poll(async () => (await sample()).parameterWeights.armSwingR ?? 0).toBeGreaterThan(.9);
+ await mode('arm-only');
+ await expect.poll(async () => (await nextParams()).armSwingR).toBeGreaterThan(.9);
+ await expect.poll(async () => Math.abs((await nextParams()).bodyAngleX)).toBeLessThan(.05);
+ await expect.poll(async () => Math.abs((await nextParams()).bodyAngleZ)).toBeLessThan(.05);
+ expect(Math.abs((await nextParams()).weightShift)).toBeLessThan(.01);
+ expect((await sample()).parameterWeights.weightShift).toBeGreaterThan(.99);
+ await mode('hips-hidden');
+ await expect.poll(async () => (await sample()).parameterWeights.weightShift).toBeUndefined();
+ expect(Math.abs((await sample()).params.bodyAngleX)).toBeLessThan(.05);
+ await mode('neutral');
+ await expect.poll(async () => (await sample()).parameterWeights.weightShift ?? 0).toBeGreaterThan(.99);
await mode('move');
- await expect.poll(async () => (await nextParams()).bodyAngleX).toBeGreaterThan(6);
+ await expect.poll(async () => (await nextParams()).bodyAngleX).toBeGreaterThan(5);
await expect.poll(async () => (await nextParams()).armSwingR).toBeGreaterThan(.6);
await expect.poll(async () => (await nextParams()).elbowBendR).toBeGreaterThan(.5);
expect((await nextParams()).elbowBendR).toBeLessThan(.65);
diff --git a/src/live/body-tracking.ts b/src/live/body-tracking.ts
index e1b26ac..554b357 100644
--- a/src/live/body-tracking.ts
+++ b/src/live/body-tracking.ts
@@ -8,49 +8,99 @@ export interface BodyResult {
}
export interface BodyNeutral { shoulderX: number; width: number; roll: number; hipX?: number }
const usable = (p: BodyPoint, threshold: number) => p && [p.x, p.y, p.visibility, p.presence ?? 1].every(Number.isFinite) && p.visibility >= threshold && (p.presence ?? 1) >= 0.5;
+// Fractions of calibrated shoulder width, and degrees of shoulder-line roll.
+export const BODY_DEAD_ZONE = Object.freeze({ shift: 0.04, roll: 2, hips: 0.04 });
+const smooth = (a: number, b: number, value: number) => {
+ const t = clamp((value - a) / (b - a), 0, 1); return t * t * (3 - 2 * t);
+};
+/** Zero inside the threshold, with a zero-slope onset and no threshold jump. */
+export function softDeadZone(value: number, threshold: number) {
+ if (!Number.isFinite(value)) return 0;
+ const excess = Math.max(0, Math.abs(value) - threshold);
+ return excess === 0 ? 0 : Math.sign(value) * excess * smooth(0, threshold, excess);
+}
+function readHips(body: BodyResult | null | undefined) {
+ if (!body || !Number.isFinite(body.aspect) || body.aspect <= 0 || body.hips?.length !== 2 || !body.hips.every(p => usable(p, 0.65))) return undefined;
+ return (body.hips[0].x + body.hips[1].x) / 2 * body.aspect;
+}
export function readBody(body: BodyResult | null | undefined): BodyNeutral | null {
if (!body || !Number.isFinite(body.aspect) || body.aspect <= 0 || body.shoulders?.length !== 2 || !body.shoulders?.every(p => usable(p, 0.55))) return null;
const [a, b] = [...body.shoulders].sort((a, b) => a.x - b.x), dx = (b.x - a.x) * body.aspect, dy = b.y - a.y;
const width = Math.hypot(dx, dy); if (width < 0.03) return null;
return { shoulderX: (a.x + b.x) / 2 * body.aspect, width, roll: Math.atan2(dy, dx) * 180 / Math.PI,
- ...(body.hips?.length === 2 && body.hips.every(p => usable(p, 0.65)) ? { hipX: (body.hips[0].x + body.hips[1].x) / 2 * body.aspect } : {}) };
+ ...(readHips(body) !== undefined ? { hipX: readHips(body) } : {}) };
}
export function mapBody(body: BodyNeutral, neutral: BodyNeutral, options: TrackingOptions) {
const gain = clamp(options.sensitivity, 0.25, 2) * (options.mirror ? -1 : 1);
const shoulders = (body.shoulderX - neutral.shoulderX) / neutral.width;
- const hips = body.hipX !== undefined && neutral.hipX !== undefined ? (body.hipX - neutral.hipX) / neutral.width : shoulders;
- return { bodyAngleX: clamp(shoulders * gain * 20, -10, 10), bodyAngleZ: clamp((body.roll - neutral.roll) * gain, -10, 10), weightShift: clamp(hips * gain * 1.4, -1, 1) };
+ const hips = body.hipX !== undefined && neutral.hipX !== undefined ? (body.hipX - neutral.hipX) / neutral.width : 0;
+ return { bodyAngleX: clamp(softDeadZone(shoulders, BODY_DEAD_ZONE.shift) * gain * 20, -10, 10), bodyAngleZ: clamp(softDeadZone(body.roll - neutral.roll, BODY_DEAD_ZONE.roll) * gain, -10, 10), weightShift: clamp(softDeadZone(hips, BODY_DEAD_ZONE.hips) * gain * 1.4, -1, 1) };
}
export class BodyPose {
private body: BodyNeutral | null = null;
private neutral: BodyNeutral | null = null;
private seen = -Infinity;
private weight = 0;
+ private hipX: number | undefined;
+ private hipSeen = -Infinity;
+ private hipWeight = 0;
+ private trusted = { shift: 0, roll: 0 };
private params = { bodyAngleX: 0, bodyAngleZ: 0, weightShift: 0 };
update(result: BodyResult | null, now: number) {
this.body = readBody(result); this.seen = this.body ? now : -Infinity;
if (!this.neutral && this.body) this.neutral = { ...this.body };
- // Hips may first appear after a seated calibration. Keep the current shoulder
- // displacement as their initial offset, avoiding a new calibration jump.
- if (this.body?.hipX !== undefined && this.neutral && this.neutral.hipX === undefined)
- this.neutral.hipX = this.body.hipX - (this.body.shoulderX - this.neutral.shoulderX);
+ const hipX = readHips(result);
+ if (hipX !== undefined && this.neutral) {
+ // First-visible hips define their own neutral; shoulder motion must never
+ // become a weight-shift offset, including after seated calibration.
+ this.neutral.hipX ??= hipX;
+ this.hipX = hipX; this.hipSeen = now;
+ }
}
calibrate(now: number) {
if (!this.body || now - this.seen > 500) return false;
- this.neutral = { ...this.body }; this.params = { bodyAngleX: 0, bodyAngleZ: 0, weightShift: 0 }; return true;
+ this.neutral = { ...this.body };
+ this.trusted = { shift: 0, roll: 0 };
+ this.params = { bodyAngleX: 0, bodyAngleZ: 0, weightShift: 0 }; return true;
}
- reset() { this.body = null; this.neutral = null; this.seen = -Infinity; }
- sample(now: number, dt: number, options: TrackingOptions) {
- const active = !!options.bodyTracking && !!this.body && !!this.neutral && now - this.seen <= 500;
+ reset() {
+ this.body = null; this.neutral = null; this.seen = -Infinity;
+ this.hipX = undefined; this.hipSeen = -Infinity; this.trusted = { shift: 0, roll: 0 };
+ }
+ sample(now: number, dt: number, options: TrackingOptions, armActivity = 0) {
+ const shouldersActive = !!options.bodyTracking && !!this.body && !!this.neutral && now - this.seen <= 500;
const alpha = 1 - Math.exp(-Math.max(0, dt) / 0.2);
- this.weight += ((active ? 1 : 0) - this.weight) * alpha;
- if (!active && this.weight < 0.005) this.weight = 0;
- if (active) {
- const target = mapBody(this.body!, this.neutral!, options), tau = options.smoothing * 0.25;
- const a = tau <= 0 ? 1 : 1 - Math.exp(-Math.max(0, dt) / tau);
- for (const key of ['bodyAngleX', 'bodyAngleZ', 'weightShift'] as const) this.params[key] += (target[key] - this.params[key]) * a;
+ const hipsActive = !!options.bodyTracking && !!this.neutral && this.neutral.hipX !== undefined && this.hipX !== undefined && now - this.hipSeen <= 500;
+ this.weight += ((shouldersActive ? 1 : 0) - this.weight) * alpha;
+ this.hipWeight += ((hipsActive ? 1 : 0) - this.hipWeight) * alpha;
+ if (!shouldersActive && this.weight < 0.005) this.weight = 0;
+ if (!hipsActive && this.hipWeight < 0.005) this.hipWeight = 0;
+ const tau = clamp(options.smoothing, 0, 1) * 0.25;
+ const a = tau <= 0 ? 1 : 1 - Math.exp(-Math.max(0, dt) / tau);
+ if (shouldersActive) {
+ const body = this.body!, neutral = this.neutral!;
+ const shift = (body.shoulderX - neutral.shoulderX) / neutral.width, roll = body.roll - neutral.roll;
+ const activity = clamp(armActivity, 0, 1);
+ if (!activity) this.trusted = { shift, roll };
+ const hipShift = body.hipX !== undefined && neutral.hipX !== undefined ? (body.hipX - neutral.hipX) / neutral.width : 0;
+ // Independently visible hips can corroborate a real sideways lean. Tiny
+ // hip jitter and opposite/disagreeing shifts cannot unlock arm-driven roll.
+ const corroboration = shift * hipShift > 0
+ ? smooth(0.04, 0.1, Math.abs(hipShift)) * (1 - smooth(0.04, 0.12, Math.abs(shift - hipShift))) : 0;
+ const xHold = activity * (1 - corroboration);
+ const guarded = { ...body,
+ shoulderX: neutral.shoulderX + (shift + (this.trusted.shift - shift) * xHold) * neutral.width,
+ roll: neutral.roll + roll + (this.trusted.roll - roll) * activity };
+ const target = mapBody(guarded, neutral, options);
+ for (const key of ['bodyAngleX', 'bodyAngleZ'] as const) this.params[key] += (target[key] - this.params[key]) * a;
+ }
+ if (hipsActive) {
+ const neutral = this.neutral!, gain = clamp(options.sensitivity, 0.25, 2) * (options.mirror ? -1 : 1);
+ const shift = (this.hipX! - neutral.hipX!) / neutral.width;
+ const target = clamp(softDeadZone(shift, BODY_DEAD_ZONE.hips) * gain * 1.4, -1, 1);
+ this.params.weightShift += (target - this.params.weightShift) * a;
}
- return { active, weight: this.weight, params: { ...this.params } };
+ return { active: shouldersActive || hipsActive, weight: this.weight, hipWeight: this.hipWeight, params: { ...this.params } };
}
}
@@ -98,6 +148,16 @@ export class ArmPose {
return available;
}
reset() { this.sides = [[channel(), channel()], [channel(), channel()]]; this.roll = 0; }
+ activity(now: number) {
+ let activity = 0;
+ for (const channels of this.sides) for (const [i, c] of channels.entries()) {
+ if (c.neutral === null) continue;
+ const confidence = Math.exp(-Math.max(0, now - c.seen - 500) / 200);
+ const delta = Math.abs(i === 0 ? angleDelta(c.value, c.neutral) : c.value - c.neutral);
+ activity = Math.max(activity, confidence * (i === 0 ? smooth(10, 30, delta) : smooth(15, 45, delta)));
+ }
+ return activity;
+ }
sample(now: number, dt: number, options: TrackingOptions) {
const params: Record<string, number> = {}, weights: Record<string, number> = {};
let active = false;
diff --git a/src/live/tracking.ts b/src/live/tracking.ts
index 4acb1c3..7ace224 100644
--- a/src/live/tracking.ts
+++ b/src/live/tracking.ts
@@ -82,7 +82,7 @@ export class FacePose {
reset() { this.body.reset(); this.arms.reset(); this.face = null; this.neutral = null; this.seen = -Infinity; }
sample(now: number, dt: number, options: TrackingOptions) {
this.bodyEnabled = !!options.bodyTracking;
- const body = this.body.sample(now, dt, options);
+ const body = this.body.sample(now, dt, options, this.arms.activity(now));
const arms = this.arms.sample(now, dt, options);
const tracking = !!this.face && now - this.seen <= 500;
const target = tracking ? mapFace(this.face!, this.neutral, options) : faceNeutral;
@@ -93,15 +93,15 @@ export class FacePose {
const params = { ...this.params };
if (body.weight > 0) {
for (const key of ['bodyAngleX', 'bodyAngleZ'] as const) params[key] += (body.params[key] - params[key]) * body.weight;
- params.weightShift = body.params.weightShift;
}
+ if (body.hipWeight > 0) params.weightShift = body.params.weightShift;
Object.assign(params, arms.params);
const weight = body.active || arms.active ? 1 : this.weight;
// Runtime multiplies global and individual weights. Preserve each arm channel's
// own fade even when the face and torso are fading at the same time.
const armWeights = Object.fromEntries(Object.entries(arms.weights).map(([key, value]) => [key, weight > 0 ? clamp(value / weight) : 0]));
return { tracking: tracking || body.active || arms.active, params, weight,
- parameterWeights: { ...(body.weight > 0 ? { weightShift: body.weight } : {}), ...armWeights } as Record<string, number> };
+ parameterWeights: { ...(body.hipWeight > 0 ? { weightShift: weight > 0 ? clamp(body.hipWeight / weight) : 0 } : {}), ...armWeights } as Record<string, number> };
}
}
export function rmsLevel(samples: Float32Array, gain: number): number {
diff --git a/tests/body-tracking.test.ts b/tests/body-tracking.test.ts
index e33aeaf..0f632bc 100644
--- a/tests/body-tracking.test.ts
+++ b/tests/body-tracking.test.ts
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest';
import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
-import { ArmPose, BodyPose, readArm, readBody, mapBody, type BodyResult } from '../src/live/body-tracking';
+import { ArmPose, BodyPose, BODY_DEAD_ZONE, softDeadZone, readArm, readBody, mapBody, type BodyResult } from '../src/live/body-tracking';
import { FacePose, type FaceResult } from '../src/live/tracking';
const options = { mirror: false, sensitivity: 1, smoothing: 0, bodyTracking: true };
const point = (x: number, y: number, visibility = 1) => ({ x, y, visibility, presence: 1 });
@@ -12,23 +12,25 @@ test('shoulder displacement/tilt map independently, hips take weight priority, m
const zero = readBody(body())!;
const shifted = readBody(body(.06, -.03))!;
const normal = mapBody(shifted, zero, options), mirror = mapBody(shifted, zero, { ...options, mirror: true });
- expect(normal.bodyAngleX).toBeCloseTo(4); expect(normal.weightShift).toBeCloseTo(-.14);
+ expect(normal.bodyAngleX).toBeCloseTo(3.2); expect(normal.weightShift).toBeCloseTo(-.084);
expect(mirror.bodyAngleX).toBeCloseTo(-normal.bodyAngleX); expect(mirror.weightShift).toBeCloseTo(-normal.weightShift);
- expect(mapBody(shifted, zero, { ...options, sensitivity: 2 }).bodyAngleX).toBeCloseTo(8);
+ expect(mapBody(shifted, zero, { ...options, sensitivity: 2 }).bodyAngleX).toBeCloseTo(6.4);
expect(mapBody(readBody(body(1))!, zero, options).bodyAngleX).toBe(10);
- const seated = mapBody(readBody(body(.06, -.03, false))!, zero, options); expect(seated.weightShift).toBeCloseTo(.28);
+ const seated = mapBody(readBody(body(.06, -.03, false))!, zero, options); expect(seated.weightShift).toBe(0);
const tilted = body(); tilted.shoulders[1].y += .04;
- expect(mapBody(readBody(tilted)!, zero, options).bodyAngleZ).toBeGreaterThan(5);
- expect(mapBody(readBody(tilted)!, zero, { ...options, mirror: true }).bodyAngleZ).toBeLessThan(-5);
+ expect(mapBody(readBody(tilted)!, zero, options).bodyAngleZ).toBeGreaterThan(3);
+ expect(mapBody(readBody(tilted)!, zero, { ...options, mirror: true }).bodyAngleZ).toBeLessThan(-3);
tilted.shoulders[0].visibility = .1; expect(readBody(tilted)).toBeNull();
tilted.shoulders[0].visibility = 1; tilted.shoulders[0].x = NaN; expect(readBody(tilted)).toBeNull();
});
test('body neutral calibration, stale/lost/off fade, and late hip visibility stay continuous', () => {
const pose = new BodyPose(); pose.update(body(), 0); pose.sample(0, .2, options); pose.update(body(.06, .06, false), 10);
- expect(pose.sample(10, .2, options).params.weightShift).toBeCloseTo(.28);
+ expect(pose.sample(10, .2, options).params.weightShift).toBe(0);
pose.calibrate(10); expect(pose.sample(10, .2, options).params.bodyAngleX).toBe(0);
pose.update(body(.09, .09), 20); const active = pose.sample(20, .2, options);
- expect(active.params.weightShift).toBeCloseTo(.14);
+ expect(active.params.weightShift).toBe(0); // first visible hips establish their own neutral
+ pose.update(body(.12, .12), 30);
+ expect(pose.sample(30, .2, options).params.weightShift).toBeCloseTo(.084);
pose.reset();
const off = pose.sample(21, .016, { ...options, bodyTracking: false }); expect(off.weight).toBeLessThan(active.weight); expect(off.weight).toBeGreaterThan(0);
expect(pose.sample(600, .016, options).active).toBe(false);
@@ -39,8 +41,8 @@ test('face-only output stays identical when body is off; body overrides rather t
const a = new FacePose(), b = new FacePose(); a.update(face(body()), 0); b.update(face(), 0);
expect(a.sample(1, .016, { ...options, bodyTracking: false })).toEqual(b.sample(1, .016, { ...options, bodyTracking: false }));
a.sample(1, 1, options); const turned=face(body(.06)); const yaw=Math.PI/6;turned.facialTransformationMatrixes[0].data=[Math.cos(yaw),0,-Math.sin(yaw),0,0,1,0,0,Math.sin(yaw),0,Math.cos(yaw),0,0,0,0,1];a.update(turned, 10); const active = a.sample(10, 1, options);
- expect(active.params.bodyAngleX).toBeCloseTo(4, 1); expect(active.parameterWeights.weightShift).toBeGreaterThan(.99);
- const lost = a.sample(600, .016, options); expect(lost.parameterWeights.weightShift).toBeGreaterThan(0); expect(lost.parameterWeights.weightShift).toBeLessThan(1);
+ expect(active.params.bodyAngleX).toBeCloseTo(3.2, 1); expect(active.parameterWeights.weightShift).toBeGreaterThan(.99);
+ const lost = a.sample(600, .016, options); expect(lost.parameterWeights.weightShift).toBeGreaterThan(0); expect(lost.weight * lost.parameterWeights.weightShift).toBeLessThan(1);
for (let i = 0; i < 100; i++) a.sample(700+i*16, .016, options);
expect(a.sample(3000,.016,options).params.weightShift).toBeUndefined();
});
@@ -207,3 +209,98 @@ test('FacePose forwards arm weights to idle without double fading; unsupported a
const disabled = a.sample(601, .2, { ...armOptions, bodyTracking: false });
expect(disabled.parameterWeights.armSwingR).toBeLessThan(1);
});
+
+
+test('body dead zones are zero for jitter, odd, continuous and have a smooth onset', () => {
+ for (const zone of Object.values(BODY_DEAD_ZONE)) {
+ for (const v of [-zone, -zone / 2, 0, zone / 2, zone]) expect(softDeadZone(v, zone)).toBe(0);
+ for (const v of [zone * 1.1, zone * 1.5, zone * 2, zone * 3]) expect(softDeadZone(-v, zone)).toBeCloseTo(-softDeadZone(v, zone), 12);
+ const epsilon = zone * 1e-4;
+ expect(softDeadZone(zone + epsilon, zone)).toBeLessThan(epsilon * .001);
+ expect(Math.abs(softDeadZone(2 * zone + epsilon, zone) - softDeadZone(2 * zone - epsilon, zone))).toBeLessThan(epsilon * 2.01);
+ }
+ const zero = readBody(body())!, noisy = body(.005, .005); noisy.shoulders[0].y -= .005;
+ expect(mapBody(readBody(noisy)!, zero, options)).toEqual({ bodyAngleX: 0, bodyAngleZ: 0, weightShift: 0 });
+});
+
+test('arm raises and elbow bends cannot turn correlated shoulder errors into torso or skirt targets', () => {
+ for (const armsTracking of [false, true]) for (const mirror of [false, true]) {
+ const pose = new FacePose(), controls = { ...options, armsTracking, mirror, smoothing: .35 };
+ pose.update(face(armsBody()), 0); const baseline = pose.sample(0, 5, controls);
+ let latest = baseline;
+ // Ramp slowly so an implementation that learns the shoulder error before
+ // recognizing the arm pose is also exercised, then hold the raised arm.
+ for (let f = 1; f <= 180; f++) {
+ const t = Math.min(1, f / 60), shifted = armsBody(60 * t, 0, 60 * t);
+ shifted.shoulders[0].x += .01 * t; shifted.shoulders[0].y -= .02 * t;
+ pose.update(face(shifted), f * 16);
+ latest = pose.sample(f * 16, .016, controls);
+ expect(Math.abs(latest.params.bodyAngleX)).toBeLessThan(.05);
+ expect(Math.abs(latest.params.bodyAngleZ)).toBeLessThan(.05);
+ expect(latest.params.weightShift).toBe(0);
+ expect(latest.weight).toBe(baseline.weight);
+ expect(latest.parameterWeights.weightShift).toBeCloseTo(baseline.parameterWeights.weightShift, 8);
+ }
+ if (armsTracking) expect(Math.max(Math.abs(latest.params.armSwingL), Math.abs(latest.params.armSwingR))).toBeGreaterThan(.9);
+ else expect(latest.params.armSwingL).toBeUndefined();
+ }
+});
+
+test('real torso lean remains tracked and visible hips can corroborate lean during arm motion', () => {
+ for (const raising of [false, true]) {
+ const pose = new FacePose(); pose.update(face(armsBody()), 0); pose.sample(0, 2, armOptions);
+ const lean = armsBody(raising ? 60 : 0);
+ for (const pair of [lean.shoulders, lean.hips, lean.elbows!, lean.wrists!]) for (const p of pair) p.x += .09;
+ pose.update(face(lean), 10);
+ const result = pose.sample(10, 2, armOptions);
+ expect(result.params.bodyAngleX).toBeCloseTo(5.2, 2);
+ expect(result.params.weightShift).toBeCloseTo(.364, 2);
+ expect(result.parameterWeights.weightShift).toBeGreaterThan(.99);
+ }
+ // A whole-body roll with hanging arms must not be treated as an arm raise.
+ const pose = new FacePose(); pose.update(face(armsBody()), 0); pose.sample(0, 2, armOptions);
+ const roll = armsBody(), angle = .15, center = { x: .5, y: .5 };
+ for (const pair of [roll.shoulders, roll.hips, roll.elbows!, roll.wrists!]) for (const p of pair) {
+ const x = (p.x - center.x) * roll.aspect, y = p.y - center.y;
+ p.x = center.x + (x * Math.cos(angle) - y * Math.sin(angle)) / roll.aspect;
+ p.y = center.y + x * Math.sin(angle) + y * Math.cos(angle);
+ }
+ pose.update(face(roll), 10); expect(pose.sample(10, 2, armOptions).params.bodyAngleZ).toBeGreaterThan(6);
+});
+
+test('hidden hips release only weightShift to idle; late hips never inherit the shoulder offset', () => {
+ const pose = new FacePose(); pose.update(face(body()), 0); pose.sample(0, 5, options);
+ pose.update(face(body(.09)), 10); const active = pose.sample(10, 1, options);
+ expect(active.params.weightShift).toBeGreaterThan(.35);
+ for (let f = 1; f <= 200; f++) {
+ const now = 10 + f * 16;
+ pose.update(face(body(.09, 0, false)), now);
+ const result = pose.sample(now, .016, options);
+ expect(result.params.bodyAngleX).toBeGreaterThan(5);
+ expect(result.weight).toBe(1);
+ if (f === 25) expect(result.parameterWeights.weightShift).toBeGreaterThan(.99);
+ if (f === 45) expect(result.parameterWeights.weightShift).toBeGreaterThan(.1);
+ }
+ const lost = pose.sample(3220, .016, options);
+ expect(lost.params.weightShift).toBeUndefined(); expect(lost.parameterWeights.weightShift).toBeUndefined();
+ const seated = new FacePose(); seated.update(face(body(0, 0, false)), 0); seated.sample(0, 1, options);
+ seated.update(face(body(.09, 0, false)), 10);
+ expect(seated.sample(10, 1, options).params.weightShift).toBeUndefined();
+ seated.update(face(body(.09, .09)), 20);
+ expect(seated.sample(20, 1, options).params.weightShift).toBe(0);
+});
+
+test('arm guarding holds an already tracked lean without changing its tracking weight', () => {
+ const pose = new FacePose(); pose.update(face(armsBody()), 0); pose.sample(0, 5, armOptions);
+ const lean = armsBody(); for (const pair of [lean.shoulders, lean.hips, lean.elbows!, lean.wrists!]) for (const p of pair) p.x += .09;
+ pose.update(face(lean), 10); const before = pose.sample(10, 5, armOptions);
+ const raised = armsBody(60, 0, 90);
+ for (const pair of [raised.shoulders, raised.elbows!, raised.wrists!]) for (const p of pair) p.x += .09;
+ raised.shoulders[0].y -= .02;
+ // Hips stay visible at their real, already tracked lean.
+ for (const p of raised.hips) p.x += .09;
+ pose.update(face(raised), 20); const after = pose.sample(20, 1, armOptions);
+ expect(after.params.bodyAngleX).toBeCloseTo(before.params.bodyAngleX, 2);
+ expect(after.params.bodyAngleZ).toBeCloseTo(before.params.bodyAngleZ, 6);
+ expect(after.parameterWeights.weightShift).toBeCloseTo(before.parameterWeights.weightShift, 6);
+});