Import a 3D Model and automatically assign and export animations
TypeScript
Start implementing the swing test retargeting algorithm
Scott Petrovic14490fc4b468
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.tsindex 23f86d6..286eef1 100644--- a/src/retarget/AnimationRetargetService.ts+++ b/src/retarget/AnimationRetargetService.ts@@ -1,10 +1,14 @@import {AnimationClip, Euler, type Object3D, Quaternion, QuaternionKeyframeTrack, Vector3,- VectorKeyframeTrack, Scene, Group, type SkinnedMesh+ VectorKeyframeTrack, Scene, Group, type SkinnedMesh,+ Skeleton,+ type Bone} from 'three'import { RetargetUtils } from './RetargetUtils.ts'import { TargetBoneMappingType } from './steps/StepBoneMapping.ts'import { SkeletonType } from '../lib/enums/SkeletonType.ts'+import { Retargeter } from './human-retargeting/Retargeter.ts'+import { Rig } from './human-retargeting/Rig.ts'/*** Parsed animation track name containing bone name and property type@@ -99,12 +103,23 @@ export class AnimationRetargetService {/*** Retarget an animation clip using bone mappings* @param source_clip - The original animation clip from the source skeleton- * @param bone_mappings - Map of target bone name -> source bone name* @returns A new animation clip retargeted for the target skeleton*/public retarget_animation_clip (source_clip: AnimationClip): AnimationClip {const new_tracks: any[] = [] // store new retargeted tracks+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ // if the source skeleton is of type human and the target mapping is Mixamo,+ // we can try to apply the new Human Retarger system to do the retargeting+ if (this.skeleton_type === SkeletonType.Human && this.target_mapping_type === TargetBoneMappingType.Mixamo) {+ console.log('Using Human Retargeter for retargeting animation clip:', source_clip.name)+ return this.apply_swing_twist_retargeting(source_clip)+ }++ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ // Besides the above special case, the standard retargeting will just be bone mapping applied+ // Maybe in the future we can do other more advanced things.+// Process each track in the source animationsource_clip.tracks.forEach((track) => {// Parse the track name to get the bone name and property@@ -140,13 +155,6 @@ export class AnimationRetargetService {// Create the retargeted animation clipconst retargeted_clip = new AnimationClip(`${source_clip.name}`, source_clip.duration, new_tracks)- // Apply Mixamo-specific corrections if needed- if (this.target_mapping_type === TargetBoneMappingType.Mixamo) {- this.apply_bone_rotation_correction(- retargeted_clip- )- }-console.log(`Retargeted animation: ${source_clip.name} (${new_tracks.length} tracks)`)return retargeted_clip}@@ -155,6 +163,85 @@ export class AnimationRetargetService {// #region PRIVATE METHODS+ private apply_swing_twist_retargeting (source_clip: AnimationClip): AnimationClip {+ // the retargeter needs Skeleton inputs fot both source and target.+ // the source armature is a Group, so we need to convert to a THREE.Skeleton before we can continue+ const source_skeleton: Skeleton | null = this.create_skeleton_from_source(this.source_armature)+ if (source_skeleton === null) {+ console.error('Failed to extract source skeleton from source armature for Human Retargeter.')+ return source_clip+ }++ // create a custom "Rig" for the source and the target skeletons+ const source_rig: Rig = new Rig(source_skeleton)+ source_rig.fromConfig({+ pelvis: { names: ['DEF-hips'] },+ spine: { names: ['DEF-spine001', 'DEF-spine002', 'DEF-spine003'] },+ head: { names: ['DEF-neck', 'DEF-head'] },+ 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'] }+ })++ // TODO: Can build out the target config from the bone mappings if they should be 1:1+ const target_rig: Rig = new Rig(this.target_skinned_meshes[0].skeleton)+ target_rig.fromConfig({+ pelvis: { names: ['mixamorigHips'] },+ spine: { names: ['mixamorigSpine', 'mixamorigSpine1', 'mixamorigSpine2'] },+ head: { names: ['mixamorigNeck', 'mixamorigHead'] },+ armL: { names: ['mixamorigLeftArm', 'mixamorigLeftForeArm', 'mixamorigLeftHand'] },+ armR: { names: ['mixamorigRightArm', 'mixamorigRightForeArm', 'mixamorigRightHand'] },+ legL: { names: ['mixamorigLeftUpLeg', 'mixamorigLeftLeg', 'mixamorigLeftFoot'] },+ legR: { names: ['mixamorigRightUpLeg', 'mixamorigRightLeg', 'mixamorigRightFoot'] }+ })++ const retargeter: Retargeter = new Retargeter(source_rig, target_rig, source_clip)++ // TODO: experiment with the additives later with T-pose correction+ // retargeter.additives.push(+ // (Ref.addAxis = new AxisAdditive( 'armL', 'y', 0 * Math.PI / 180 )),+ // (Ref.addTwist = new ChainTwistAdditive( 'armR', 0 * Math.PI / 180 )),+ // );++ retargeter.update(0.001) // small delta to initalize++ return source_clip+ }++ /**+ * Utility: Convert a Group (with Armature and Bone hierarchy) to a THREE.Skeleton+ * @param group - The root Group containing the Armature and Bone hierarchy+ * @returns Skeleton or null if not found+ */+ private create_skeleton_from_source (group: Group): Skeleton | null {+ // Find the Armature child+ const armature = group.children.find(child => child.type === 'Object3D' &&+ child.name.toLowerCase().includes('armature'))++ if (armature === undefined) return null++ // Find the root Bone under the Armature+ const root_bone = armature.children.find(child => child.type === 'Bone') as Bone | undefined++ if (root_bone === undefined) return null++ const bones = this.collect_bones(root_bone)++ if (bones.length === 0) return null++ const skeleton = new Skeleton(bones)+ skeleton.calculateInverses()+ return skeleton+ }++ // Recursively collect all bones. part of extract_skeleton_from_group()+ private collect_bones (object: Object3D, bones: Bone[] = []): Bone[] {+ if (object.type === 'Bone') bones.push(object as Bone)+ object.children.forEach(child => this.collect_bones(child, bones))+ return bones+ }+/*** Create a reverse mapping: source bone name -> array of target bone names*/diff --git a/src/retarget/human-retargeting/Pose.ts b/src/retarget/human-retargeting/Pose.tsindex 3632a4c..3655126 100644--- a/src/retarget/human-retargeting/Pose.ts+++ b/src/retarget/human-retargeting/Pose.ts@@ -32,7 +32,8 @@ export class Pose {*/getJoint (o: number | string): Joint | null {switch (typeof o) {- case 'number': return this.joints[o]+ case 'number':+ return this.joints[o]case 'string': {const idx = this.nameIdx.get(o)return (idx !== undefined) ? this.joints[idx] : null@@ -203,6 +204,10 @@ export class Pose {}// Work up the heirarchy till the root bone+ if (joint.local === undefined) {+ console.error('Pose.getWorld - joint.local is undefined', joint)+ }+out_transform.copy(joint.local)while (joint.pindex !== -1) {joint = this.joints[joint.pindex]diff --git a/src/retarget/human-retargeting/Retargeter.ts b/src/retarget/human-retargeting/Retargeter.tsindex a65cdb9..c3b35af 100644--- a/src/retarget/human-retargeting/Retargeter.ts+++ b/src/retarget/human-retargeting/Retargeter.ts@@ -197,8 +197,8 @@ export class Retargeter {this.pose.setRot(itm.idx, rot)// ------------------------ const debug_transform: Transform = new Transform() // Debug- this.pose.getWorld(itm.idx, debug_transform)+ // const debug_transform: Transform = new Transform() // Debug+ // this.pose.getWorld(itm.idx, debug_transform)// const vv: Vec3 = new Vec3() // Debug// Debug.pnt.add(debug_transform.pos, 0x00ff00, 1, 1)// Debug.ln.add(debug_transform.pos, vv.fromQuat(debug_transform.rot, itm.swing).scale(0.1).add(debug_transform.pos), 0xffff00)diff --git a/src/retarget/human-retargeting/Rig.ts b/src/retarget/human-retargeting/Rig.tsindex 05ae97e..a01fb0f 100644--- a/src/retarget/human-retargeting/Rig.ts+++ b/src/retarget/human-retargeting/Rig.ts@@ -12,6 +12,11 @@ import type * as THREE from 'three'// that speaks a single language of whats what.export class Rig {public readonly skel: THREE.Skeleton++ /**+ * The T-Pose of the rig, representing the default pose of the skeleton+ * Initially set by passing in the base skeleton data+ */public readonly tpose: Posepublic readonly chains: Record<string, RigItem[]> = {}public scalar: number = 1.0