Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Have the animation listing use its own animation loop that can be turned off/on if it is active

Scott Petrovic36a699e06606
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index 9b2a5a9..dc19a1e 100644
--- a/src/retarget/RetargetAnimationListing.ts
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -27,6 +27,8 @@ export class RetargetAnimationListing extends EventTarget {
public animation_search: AnimationSearch | null = null
+ private is_animations_active: boolean = false
+
constructor (theme_manager: ThemeManager, step_bone_mapping: StepBoneMapping) {
super()
this.theme_manager = theme_manager
@@ -56,15 +58,18 @@ export class RetargetAnimationListing extends EventTarget {
return this.animation_mixer
}
- public frame_change (delta_time: number): void {
- this.mixer().update(delta_time)
- this.animation_player.update(delta_time)
- }
-
public animation_clips (): AnimationClip[] {
return this.animation_clips_loaded.map(clip => clip.display_animation_clip)
}
+ public start_preview (): void {
+ this.is_animations_active = true
+ }
+
+ public stop_preview (): void {
+ this.is_animations_active = false
+ }
+
public load_and_apply_default_animation_to_skinned_mesh (retarget_meshes: Scene): void {
// load the Group skinned mesh and convert to normal SkinnedMesh array
this.skinned_meshes_to_animate = retarget_meshes.children.filter((child: Object3D) => {
@@ -192,6 +197,7 @@ export class RetargetAnimationListing extends EventTarget {
private add_event_listeners (): void {
// Add any retarget-specific event listeners here
// For now, keeping it minimal
+ this.setup_animation_loop()
}
public get_animation_player (): AnimationPlayer {
@@ -201,4 +207,41 @@ export class RetargetAnimationListing extends EventTarget {
public get_selected_animation_indices (): number[] {
return this.animation_search?.get_selected_animation_indices() ?? []
}
+
+ // public frame_change (delta_time: number): void {
+ // this.mixer().update(delta_time)
+ // this.animation_player.update(delta_time)
+ // }
+
+ private setup_animation_loop (): void {
+ let last_time = performance.now()
+ const animate = (): void => {
+ requestAnimationFrame(animate)
+
+ // calculate delta time and pass it into preview
+ const current_time = performance.now()
+ const delta_time = (current_time - last_time) / 1000 // Convert to seconds
+ last_time = current_time
+
+ if (this.is_animations_active) {
+ this.animation_frame_logic(delta_time)
+ }
+ }
+ animate()
+ }
+
+ private animation_frame_logic (delta_time: number): void {
+ if (this.animation_mixer === null) return
+
+ this.animation_mixer.update(delta_time)
+ this.animation_player.update(delta_time)
+
+ // CRITICAL: Update the skeleton and skinned meshes after animation changes the bones
+ // Why do I need this when I don't need it in the main Mesh2Motion engine?
+ this.skinned_meshes_to_animate.forEach((skinned_mesh) => {
+ skinned_mesh.skeleton.bones.forEach(bone => {
+ bone.updateMatrixWorld(true)
+ })
+ })
+ }
}
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index c47df30..ed0d28f 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -234,8 +234,6 @@ export class RetargetAnimationPreview extends EventTarget {
this.animation_mixer.update(delta_time)
- console.log('Animation preview loop playing!!')
-
// CRITICAL: Update the skeleton and skinned meshes after animation changes the bones
// Why do I need this when I don't need it in the main Mesh2Motion engine?
this.target_skinned_meshes.forEach((skinned_mesh) => {
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index 835395f..dc5f319 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -66,7 +66,10 @@ class RetargetModule {
bone_mapping_step.style.display = 'inline'
}
- // TODO: stop the animation listing step
+ // stop the animation listing step
+ if (this.animation_listing_step !== null) {
+ this.animation_listing_step.stop_preview()
+ }
// start the live preview again
this.start_live_preview()
@@ -111,7 +114,13 @@ class RetargetModule {
this.animation_listing_step.begin(this.step_load_source_skeleton.get_skeleton_type())
const retargetable_meshes: Scene | null = this.step_load_target_model.get_retargetable_meshes()
- this.animation_listing_step.load_and_apply_default_animation_to_skinned_mesh(retargetable_meshes)
+
+ if (retargetable_meshes !== null) {
+ this.animation_listing_step.load_and_apply_default_animation_to_skinned_mesh(retargetable_meshes)
+ this.animation_listing_step.start_preview()
+ } else {
+ console.error('Retargetable meshes are null while processing click button.')
+ }
}
}