Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add all the fingers to the config and get those to retarget too

Scott Petrovic6a3129f63df4
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index 018fe23..866738e 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -181,10 +181,20 @@ export class AnimationRetargetService {
armL: { names: ['DEF-upper_armL', 'DEF-forearmL', 'DEF-handL'] },
armR: { names: ['DEF-upper_armR', 'DEF-forearmR', 'DEF-handR'] },
legL: { names: ['DEF-thighL', 'DEF-shinL', 'DEF-footL'] },
- legR: { names: ['DEF-thighR', 'DEF-shinR', 'DEF-footR'] }
+ legR: { names: ['DEF-thighR', 'DEF-shinR', 'DEF-footR'] },
+ fingersThumbL: { names: ['DEF-thumb01L', 'DEF-thumb02L', 'DEF-thumb03L', 'DEF-thumb04_tipL'] },
+ fingersThumbR: { names: ['DEF-thumb01R', 'DEF-thumb02R', 'DEF-thumb03R', 'DEF-thumb04_tipR'] },
+ fingersIndexL: { names: ['DEF-f_index01L', 'DEF-f_index02L', 'DEF-f_index03L', 'DEF-f_index04_tipL'] },
+ fingersIndexR: { names: ['DEF-f_index01R', 'DEF-f_index02R', 'DEF-f_index03R', 'DEF-f_index04_tipR'] },
+ fingersMiddleL: { names: ['DEF-f_middle01L', 'DEF-f_middle02L', 'DEF-f_middle03L', 'DEF-f_middle04_tipL'] },
+ fingersMiddleR: { names: ['DEF-f_middle01R', 'DEF-f_middle02R', 'DEF-f_middle03R', 'DEF-f_middle04_tipR'] },
+ fingersRingL: { names: ['DEF-f_ring01L', 'DEF-f_ring02L', 'DEF-f_ring03L', 'DEF-f_ring04_tipL'] },
+ fingersRingR: { names: ['DEF-f_ring01R', 'DEF-f_ring02R', 'DEF-f_ring03R', 'DEF-f_ring04_tipR'] },
+ fingersPinkyL: { names: ['DEF-f_pinky01L', 'DEF-f_pinky02L', 'DEF-f_pinky03L', 'DEF-f_pinky04_tipL'] },
+ fingersPinkyR: { names: ['DEF-f_pinky01R', 'DEF-f_pinky02R', 'DEF-f_pinky03R', 'DEF-f_pinky04_tipR'] }
})
- // TODO: Can build out the target config from the bone mappings if they should be 1:1
+ // maybe convert this from the bone map?
const target_rig: Rig = new Rig(this.target_skinned_meshes[0].skeleton)
target_rig.fromConfig({
pelvis: { names: ['mixamorigHips'] },
@@ -193,7 +203,17 @@ export class AnimationRetargetService {
armL: { names: ['mixamorigLeftArm', 'mixamorigLeftForeArm', 'mixamorigLeftHand'] },
armR: { names: ['mixamorigRightArm', 'mixamorigRightForeArm', 'mixamorigRightHand'] },
legL: { names: ['mixamorigLeftUpLeg', 'mixamorigLeftLeg', 'mixamorigLeftFoot'] },
- legR: { names: ['mixamorigRightUpLeg', 'mixamorigRightLeg', 'mixamorigRightFoot'] }
+ legR: { names: ['mixamorigRightUpLeg', 'mixamorigRightLeg', 'mixamorigRightFoot'] },
+ fingersThumbL: { names: ['mixamorigLeftHandThumb1', 'mixamorigLeftHandThumb2', 'mixamorigLeftHandThumb3', 'mixamorigLeftHandThumb4'] },
+ fingersThumbR: { names: ['mixamorigRightHandThumb1', 'mixamorigRightHandThumb2', 'mixamorigRightHandThumb3', 'mixamorigRightHandThumb4'] },
+ fingersIndexL: { names: ['mixamorigLeftHandIndex1', 'mixamorigLeftHandIndex2', 'mixamorigLeftHandIndex3', 'mixamorigLeftHandIndex4'] },
+ fingersIndexR: { names: ['mixamorigRightHandIndex1', 'mixamorigRightHandIndex2', 'mixamorigRightHandIndex3', 'mixamorigRightHandIndex4'] },
+ fingersMiddleL: { names: ['mixamorigLeftHandMiddle1', 'mixamorigLeftHandMiddle2', 'mixamorigLeftHandMiddle3', 'mixamorigLeftHandMiddle4'] },
+ fingersMiddleR: { names: ['mixamorigRightHandMiddle1', 'mixamorigRightHandMiddle2', 'mixamorigRightHandMiddle3', 'mixamorigRightHandMiddle4'] },
+ fingersRingL: { names: ['mixamorigLeftHandRing1', 'mixamorigLeftHandRing2', 'mixamorigLeftHandRing3', 'mixamorigLeftHandRing4'] },
+ fingersRingR: { names: ['mixamorigRightHandRing1', 'mixamorigRightHandRing2', 'mixamorigRightHandRing3', 'mixamorigRightHandRing4'] },
+ fingersPinkyL: { names: ['mixamorigLeftHandPinky1', 'mixamorigLeftHandPinky2', 'mixamorigLeftHandPinky3', 'mixamorigLeftHandPinky4'] },
+ fingersPinkyR: { names: ['mixamorigRightHandPinky1', 'mixamorigRightHandPinky2', 'mixamorigRightHandPinky3', 'mixamorigRightHandPinky4'] }
})
const retargeter: Retargeter = new Retargeter(source_rig, target_rig, source_clip)
diff --git a/src/retarget/human-retargeting/Retargeter.ts b/src/retarget/human-retargeting/Retargeter.ts
index a2723b1..140a0eb 100644
--- a/src/retarget/human-retargeting/Retargeter.ts
+++ b/src/retarget/human-retargeting/Retargeter.ts
@@ -39,16 +39,6 @@ export class Retargeter {
this.mixer.update(delta_time)
// Compute vectors from animation source
- // then align target joints to it
- // this.applyScaledTranslation('pelvis')
- // this.applyEndInterp('spine')
- // this.applyEndInterp('head')
- // this.applyEndInterp('armL')
- // this.applyEndInterp('armR')
- // this.applyEndInterp('legL')
- // this.applyEndInterp('legR')
-
- // TODO: Figure out why the applyChain() version is doing nothing
this.applyScaledTranslation('pelvis') // apply position scaling for hips
this.applyChain('pelvis')
this.applyEndInterp('spine')
@@ -59,6 +49,18 @@ export class Retargeter {
this.applyChain('legL')
this.applyChain('legR')
+ // fingers
+ this.applyChain('fingersThumbL')
+ this.applyChain('fingersThumbR')
+ this.applyChain('fingersIndexL')
+ this.applyChain('fingersIndexR')
+ this.applyChain('fingersMiddleL')
+ this.applyChain('fingersMiddleR')
+ this.applyChain('fingersRingL')
+ this.applyChain('fingersRingR')
+ this.applyChain('fingersPinkyL')
+ this.applyChain('fingersPinkyR')
+
// Run Additives if any exist
for (const i of this.additives) {
i.apply(this)
diff --git a/src/retarget/human-retargeting/Rig.ts b/src/retarget/human-retargeting/Rig.ts
index a01fb0f..39591cf 100644
--- a/src/retarget/human-retargeting/Rig.ts
+++ b/src/retarget/human-retargeting/Rig.ts
@@ -42,29 +42,64 @@ export class Rig {
this.buildRigScalar(k)
break
case 'spine' :
+ // Swing: points up (Y+), Twist: forward (Z+)
this.buildItem(k, chain_names, new Vec3(0, 1, 0), new Vec3(0, 0, 1))
break
case 'head' :
this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, 1, 0))
break
case 'armL' :
+ // Swing: points left (X+), Twist: back (Z-)
this.buildItem(k, chain_names, new Vec3(1, 0, 0), new Vec3(0, 0, -1))
break
case 'armR' :
+ // Swing: points right (X-), Twist: back (Z-)
this.buildItem(k, chain_names, new Vec3(-1, 0, 0), new Vec3(0, 0, -1))
break
case 'legL' :
- this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, -1, 0))
- break
case 'legR' :
+ // Both Legs pointing down and twist in same direction
+ // Swing: forward (Z+), Twist: down (Y-)
this.buildItem(k, chain_names, new Vec3(0, 0, 1), new Vec3(0, -1, 0))
break
+ case 'fingersThumbL' :
+ // Thumb points more forward/outward
+ this.buildItem(k, chain_names, new Vec3(0.5, 0, 0.866), new Vec3(0, -1, 0))
+ break
+ case 'fingersIndexL' :
+ case 'fingersMiddleL' :
+ case 'fingersRingL' :
+ case 'fingersPinkyL' :
+ // Fingers extend similar to left arm
+ this.buildItem(k, chain_names, new Vec3(1, 0, 0), new Vec3(0, 0, -1))
+ break
+ // Right hand fingers
+ case 'fingersThumbR' :
+ // Thumb points more forward/outward (mirrored)
+ this.buildItem(k, chain_names, new Vec3(-0.5, 0, 0.866), new Vec3(0, -1, 0))
+ break
+ case 'fingersIndexR' :
+ case 'fingersMiddleR' :
+ case 'fingersRingR' :
+ case 'fingersPinkyR' :
+ // Fingers extend forward from palm (mirrored twist)
+ this.buildItem(k, chain_names, new Vec3(-1, 0, 0), new Vec3(0, 0, -1))
+ break
}
}
return this
}
// k = chain key like 'pelvis', 'armL', etc
+
+ /**
+ * Builds a rig item chain for a given key using the provided joint names, swing, and twist vectors.
+ * @param k - The key representing the chain (e.g., 'pelvis', 'armL').
+ * @param names - An array of joint names to be included in the chain.
+ * @param swing - The swing vector for the joints in the chain.
+ * @param twist - The twist vector for the joints in the chain.
+ * @returns The current Rig instance for chaining.
+ */
buildItem (k: string, names: string[], swing: Vec3, twist: Vec3): this {
const rig_items: RigItem[] = []
let j
diff --git a/src/retarget/human-retargeting/RigItem.ts b/src/retarget/human-retargeting/RigItem.ts
index 12c6ed1..92bf48e 100644
--- a/src/retarget/human-retargeting/RigItem.ts
+++ b/src/retarget/human-retargeting/RigItem.ts
@@ -9,6 +9,14 @@ export class RigItem {
public readonly swing: Vec3 = new Vec3(0, 0, 1) // Swing Direction - Z
public readonly twist: Vec3 = new Vec3(0, 1, 0) // Twist Direction - Y
+ /**
+ * Initializes the RigItem from a Joint, setting its index, parent index,
+ * and computing the swing and twist directions based on the joint's world rotation.
+ * @param j - The Joint to initialize from.
+ * @param swing - Optional swing direction vector.
+ * @param twist - Optional twist direction vector.
+ * @returns The current RigItem instance for chaining.
+ */
public fromJoint (j: Joint, swing: Vec3 | null = null, twist: Vec3 | null = null): this {
this.idx = j.index
this.pidx = j.pindex