Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

temporarily try to add corrective rotation to the retargeting area to try and improve some rigs

Scott Petrovic11c3b0a8625b
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index 9ba27ab..62460c3 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -28,6 +28,19 @@ export class AnimationRetargetService {
private target_mapping_type: TargetBoneMappingType = TargetBoneMappingType.Custom
private bone_mappings: Map<string, string> = new Map<string, string>()
+ // corrective X-axis rotation (degrees) applied to the whole target rig during
+ // human swing-twist retargeting. Helps rigs that have no root bone and whose
+ // pelvis rest orientation leaves the character tilted (face planting)
+ private root_correction_x_degrees: number = 0
+
+ public set_root_correction_x_degrees (degrees: number): void {
+ this.root_correction_x_degrees = degrees
+ }
+
+ public get_root_correction_x_degrees (): number {
+ return this.root_correction_x_degrees
+ }
+
public set_bone_mappings (mappings: Map<string, string>): void {
this.bone_mappings = mappings
}
@@ -218,6 +231,10 @@ export class AnimationRetargetService {
const retargeter: Retargeter = new Retargeter(source_rig, target_rig, source_clip)
+ // apply any global rig correction the user has set (fixes tilted rigs that
+ // have no root bone, only a pelvis bone at the top of the hierarchy)
+ retargeter.set_root_correction_x(this.root_correction_x_degrees)
+
// TODO: experiment with the additives later with T-pose correction
// retargeter.additives.push(
// (Ref.addAxis = new AxisAdditive( 'armL', 'y', 0 * Math.PI / 180 )),
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index 72da041..e773cca 100644
--- a/src/retarget/RetargetAnimationListing.ts
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -32,6 +32,10 @@ export class RetargetAnimationListing extends EventTarget {
private export_button: HTMLButtonElement | null = null
+ // tracks which animation is currently playing so we can re-play it when
+ // settings (like the root rotation correction) change
+ private current_playing_index: number = -1
+
constructor (theme_manager: ThemeManager, download_settings: DownloadSettings) {
super()
this.theme_manager = theme_manager
@@ -199,6 +203,8 @@ export class RetargetAnimationListing extends EventTarget {
const animation_pair = this.animation_clips_loaded[index]
const display_clip = animation_pair.display_animation_clip
+ this.current_playing_index = index
+
// Stop all current actions
this.animation_mixer.stopAllAction()
@@ -254,6 +260,19 @@ export class RetargetAnimationListing extends EventTarget {
})
})
+ // global rig rotation correction. stored on the shared service so the
+ // retargeter picks it up when clips are retargeted for playback/export,
+ // then re-play the current animation so the change is visible immediately
+ const root_rotation_correction_select = document.getElementById('root-rotation-correction-select') as HTMLSelectElement
+ root_rotation_correction_select?.addEventListener('change', () => {
+ const degrees: number = Number(root_rotation_correction_select.value ?? 0)
+ AnimationRetargetService.getInstance().set_root_correction_x_degrees(degrees)
+
+ if (this.current_playing_index >= 0) {
+ this.play_animation(this.current_playing_index)
+ }
+ })
+
// event listener for select all / deselect all button
const toggle_select_all_button = document.querySelector('#toggle-select-all-animations')
toggle_select_all_button?.addEventListener('click', () => {
diff --git a/src/retarget/human-retargeting/Retargeter.ts b/src/retarget/human-retargeting/Retargeter.ts
index a770f74..49ba171 100644
--- a/src/retarget/human-retargeting/Retargeter.ts
+++ b/src/retarget/human-retargeting/Retargeter.ts
@@ -22,6 +22,13 @@ export class Retargeter {
private animation_frame_id: number | null = null
private last_time: number = 0
+ // corrective rotation applied to top-level target bones (bones with no bone parent).
+ // useful when the imported rig has no root bone and the pelvis rest orientation
+ // leaves the character tilted (for example face planting after retargeting)
+ private root_correction_quat: THREE.Quaternion = new THREE.Quaternion() // identity = no correction
+ private has_root_correction: boolean = false
+ private readonly top_level_bones: THREE.Bone[] = []
+
constructor (source_rig: Rig, target_rig: Rig, clip: THREE.AnimationClip) {
this.srcRig = source_rig
this.tarRig = target_rig
@@ -29,6 +36,10 @@ export class Retargeter {
this.pose = this.tarRig.tpose.clone()
+ // cache the bones that sit at the top of the hierarchy (no Bone parent).
+ // these are the bones a global rig correction must be applied to
+ this.top_level_bones = this.tarRig.skel.bones.filter((bone) => bone.parent?.type !== 'Bone')
+
this.action = this.mixer.clipAction(this.clip, this.srcRig.skel.bones[0])
this.action.play()
}
@@ -63,6 +74,38 @@ export class Retargeter {
// Apply working pose to 3JS skeleton for rendering
this.pose.toSkeleton(this.tarRig.skel)
+
+ // apply any global rig correction last so it rotates the whole skeleton
+ this.apply_root_correction()
+ }
+
+ /**
+ * Sets a corrective rotation (in degrees) around the X axis that is applied to
+ * the top-level target bone(s) on every update. Use 0 to clear the correction.
+ * @param degrees rotation around the X axis in degrees (for example -90 or 90)
+ */
+ public set_root_correction_x (degrees: number): void {
+ this.root_correction_quat.setFromAxisAngle(new THREE.Vector3(1, 0, 0), degrees * Math.PI / 180)
+ this.has_root_correction = degrees !== 0
+ }
+
+ /**
+ * Rotates every top-level target bone by the root correction quaternion.
+ * Since these bones have no bone parent, this rigidly rotates the entire rig.
+ * Both the rotation and position are transformed so baked root motion follows
+ * the corrected orientation.
+ */
+ private apply_root_correction (): void {
+ if (!this.has_root_correction) {
+ return
+ }
+
+ this.top_level_bones.forEach((bone) => {
+ // fresh objects are intentionally not used here: premultiply/applyQuaternion
+ // mutate the bone transform in place, so no shared temp vector aliasing issues
+ bone.quaternion.premultiply(this.root_correction_quat)
+ bone.position.applyQuaternion(this.root_correction_quat)
+ })
}
/**
diff --git a/src/retarget/index.html b/src/retarget/index.html
index d798704..76f1333 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -174,6 +174,18 @@
<!-- Select all / Deselect all button and animation count -->
<div style="display: flex; gap: 1rem; align-items: center; margin-top: 0.5rem; justify-content: space-between;">
<button id="toggle-select-all-animations" class="secondary-button">Select All</button>
+
+
+ <!-- global rig rotation correction for tilted rigs -->
+ <select id="root-rotation-correction-select" class="button"
+ data-tippy-content="If the retargeted animation is tilted (for example the character face plants), apply a global X-axis rotation to the whole rig. Useful for rigs that have no root bone above the pelvis.">
+ <option value="0" selected>X Rotation: 0°</option>
+ <option value="90">X Rotation: 90°</option>
+ <option value="-90">X Rotation: -90°</option>
+ <option value="180">X Rotation: 180°</option>
+ </select>
+
+
<div id="animation-listing-count" style="white-space: nowrap;">0 animations</div>
</div>