Import a 3D Model and automatically assign and export animations
TypeScript
Remove a bunch of bad code for trying to fix bone rotations
Scott Petrovica6e9a757129b
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.tsindex 866738e..6c258f1 100644--- a/src/retarget/AnimationRetargetService.ts+++ b/src/retarget/AnimationRetargetService.ts@@ -295,148 +295,6 @@ export class AnimationRetargetService {return reverse_mappings}- /**- * Apply bone rotation correction for fixing bone roll delta between target and source skeleton- */- private apply_bone_rotation_correction (- animation_clip: AnimationClip- ): void {- this.bone_mappings.forEach((source_bone_name, target_bone_name) => {- const delta = this.calculate_bone_rotation_delta(- source_bone_name,- target_bone_name- )-- if (delta !== null) {- const delta_euler = new Euler().setFromQuaternion(delta)-- AnimationRetargetService.rotate_bone_for_retargeting(animation_clip, [target_bone_name], delta_euler)- } else {- console.log(`Warning: delta is NULL when fixing bone roll. Skipping correction for bone: ${target_bone_name}`)- }- })-- console.log('Applied Mixamo-specific corrections to retargeted animation (dynamically calculated)', animation_clip)- }-- /**- * Calculate the bone roll delta between source and target bones in bind pose- * Bone roll is the rotation around the bone's primary axis (Y-axis in Three.js)- * @param source_bone_name - Name of the bone in the source skeleton- * @param target_bone_name - Name of the bone in the target skeleton- * @param source_armature - The source skeleton armature- * @param target_armature - The target skeleton data- * @param target_skinned_meshes - The target skinned meshes- * @returns The rotation difference as a quaternion (Y-axis only), or null if bones not found- */- private calculate_bone_rotation_delta (- source_bone_name: string,- target_bone_name: string- ): Quaternion | null {- if (this.source_armature === null || this.target_armature === null) {- console.warn('Cannot calculate rotation delta: missing skeleton data')- return null- }-- // Find source bone and target bone with normalized matching- const source_bone = this.find_bone_by_name(this.source_armature, [], source_bone_name)- const target_bone = this.find_bone_by_name(this.target_armature, this.target_skinned_meshes, target_bone_name)-- if (source_bone === null || target_bone === null) {- const source_label = source_bone === null ? 'null' : source_bone.name- const target_label = target_bone === null ? 'null' : target_bone.name- console.debug(`Cannot calculate rotation delta: bone not found (source: ${source_bone_name}=${source_label}, target: ${target_bone_name}=${target_label})`)- return null- }-- // Local-space bind pose quaternions- const source_quat = new Quaternion().copy(source_bone.quaternion)- const target_quat = new Quaternion().copy(target_bone.quaternion)-- // Relative local delta (target -> source)- const full_delta_quat = target_quat.clone().invert().multiply(source_quat)-- // Extract only the twist around local Y (bone roll) via swing–twist decomposition- const axis_y = new Vector3(0, 1, 0)- const v = new Vector3(full_delta_quat.x, full_delta_quat.y, full_delta_quat.z)- const proj = axis_y.clone().multiplyScalar(v.dot(axis_y))- const twist_y = new Quaternion(proj.x, proj.y, proj.z, full_delta_quat.w).normalize()-- return twist_y- }-- /**- * Apply rotations to specific bones to correct them and normalize them for Mesh2Motion animations- */- private static rotate_bone_for_retargeting (- animation_clip: AnimationClip,- bone_match_pattern: string[],- rotate_obj: Euler- ): void {- // Find all shoulder quaternion tracks (e.g., mixamorigLeftShoulder.quaternion)- const tracks_to_change = animation_clip.tracks.filter(track =>- bone_match_pattern.some(pattern => track.name.toLowerCase().includes(pattern.toLowerCase())) && track.name.includes('quaternion')- ) as Array<{ name: string, times: Float32Array | number[], values: Float32Array | number[] }>-- if (tracks_to_change.length === 0) return-- // Object axis rotation amount. Final value is quaternion- const rotation_amount: Quaternion = new Quaternion().setFromEuler(rotate_obj)-- for (const track of tracks_to_change) {- const name_info = AnimationRetargetService.getInstance().parse_track_name_for_metadata(track.name)- if (name_info === null) continue-- const values = track.values- for (let i = 0; i < values.length; i += 4) {- const original_quat: Quaternion = new Quaternion(- values[i], // x- values[i + 1], // y- values[i + 2], // z- values[i + 3] // w- )-- // apply local-space correction- original_quat.multiply(rotation_amount)-- values[i] = original_quat.x- values[i + 1] = original_quat.y- values[i + 2] = original_quat.z- values[i + 3] = original_quat.w- }- }- }-- /**- * Find a bone by name in the skeleton hierarchy- */- private find_bone_by_name (- root: Object3D,- skinned_meshes: SkinnedMesh[],- bone_name: string- ): Object3D | null {- let found_bone: Object3D | null = null-- root.traverse((child) => {- if (found_bone !== null) return- if (child.name === bone_name) {- found_bone = child- }- })-- if (found_bone !== null) return found_bone-- skinned_meshes.forEach((mesh) => {- if (found_bone !== null) return- const match = mesh.skeleton?.bones.find((bone) => bone.name === bone_name)- if (match !== undefined) {- found_bone = match- }- })-- return found_bone- }-/*** Parse a track name to extract bone name and property (e.g., "quaternion", "position", "scale")* Handles various formats like "boneName.property" or ".bones[boneName].property"