LCOV - code coverage report
Current view: top level - packages/viewport3d/src/load3d - AnimationManager.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 65.9 % 88 58
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import * as THREE from 'three'
       2              : import type { GLTF } from 'three/examples/jsm/loaders/GLTFLoader'
       3              : 
       4              : import type {
       5              :   AnimationItem,
       6              :   AnimationManagerInterface,
       7              :   EventManagerInterface
       8              : } from './interfaces'
       9              : 
      10           24 : export class AnimationManager implements AnimationManagerInterface {
      11              :   currentAnimation: THREE.AnimationMixer | null = null
      12              :   animationActions: THREE.AnimationAction[] = []
      13              :   animationClips: THREE.AnimationClip[] = []
      14              :   selectedAnimationIndex: number = 0
      15              :   isAnimationPlaying: boolean = false
      16              :   animationSpeed: number = 1.0
      17              : 
      18              :   private eventManager: EventManagerInterface
      19              : 
      20              :   constructor(eventManager: EventManagerInterface) {
      21           27 :     this.eventManager = eventManager
      22              :   }
      23              : 
      24              :   init(): void {}
      25              : 
      26              :   dispose(): void {
      27           27 :     if (this.currentAnimation) {
      28            1 :       this.animationActions.forEach((action) => {
      29            1 :         action.stop()
      30              :       })
      31            1 :       this.currentAnimation = null
      32              :     }
      33           27 :     this.animationActions = []
      34           27 :     this.animationClips = []
      35           27 :     this.selectedAnimationIndex = 0
      36           27 :     this.isAnimationPlaying = false
      37           27 :     this.animationSpeed = 1.0
      38              : 
      39           27 :     this.eventManager.emitEvent('animationListChange', [])
      40              :   }
      41              : 
      42              :   setupModelAnimations(
      43              :     model: THREE.Object3D,
      44              :     originalModel: THREE.Object3D | THREE.BufferGeometry | GLTF | null
      45              :   ): void {
      46           17 :     if (this.currentAnimation) {
      47            0 :       this.currentAnimation.stopAllAction()
      48            0 :       this.animationActions = []
      49              :     }
      50              : 
      51           17 :     let animations: THREE.AnimationClip[] = []
      52           17 :     if (model.animations.length > 0) {
      53            0 :       animations = model.animations
      54           17 :     } else if (
      55              :       originalModel &&
      56              :       'animations' in originalModel &&
      57              :       Array.isArray(originalModel.animations)
      58              :     ) {
      59            2 :       animations = originalModel.animations
      60              :     }
      61              : 
      62           17 :     if (animations.length > 0) {
      63            2 :       this.animationClips = animations
      64              : 
      65            2 :       this.currentAnimation = new THREE.AnimationMixer(model)
      66              : 
      67            2 :       if (this.animationClips.length > 0) {
      68            2 :         this.updateSelectedAnimation(0)
      69              :       }
      70              :     } else {
      71           15 :       this.animationClips = []
      72              :     }
      73              : 
      74           17 :     this.updateAnimationList()
      75              :   }
      76              : 
      77              :   updateAnimationList(): void {
      78           17 :     let updatedAnimationList: AnimationItem[] = []
      79              : 
      80           17 :     if (this.animationClips.length > 0) {
      81            2 :       updatedAnimationList = this.animationClips.map((clip, index) => ({
      82              :         name: clip.name || `Animation ${index + 1}`,
      83              :         index
      84              :       }))
      85              :     }
      86              : 
      87           17 :     this.eventManager.emitEvent('animationListChange', updatedAnimationList)
      88              :   }
      89              : 
      90              :   setAnimationSpeed(speed: number): void {
      91            0 :     this.animationSpeed = speed
      92            0 :     this.animationActions.forEach((action) => {
      93            0 :       action.setEffectiveTimeScale(speed)
      94              :     })
      95              :   }
      96              : 
      97              :   updateSelectedAnimation(index: number): void {
      98            2 :     if (!this.currentAnimation || index >= this.animationClips.length) {
      99            0 :       console.warn('Invalid animation update request')
     100            0 :       return
     101              :     }
     102              : 
     103            2 :     this.animationActions.forEach((action) => {
     104            0 :       action.stop()
     105              :     })
     106            2 :     this.currentAnimation.stopAllAction()
     107            2 :     this.animationActions = []
     108              : 
     109            2 :     this.selectedAnimationIndex = index
     110            2 :     const clip = this.animationClips[index]
     111              : 
     112            2 :     const action = this.currentAnimation.clipAction(clip)
     113              : 
     114            2 :     action.setEffectiveTimeScale(this.animationSpeed)
     115              : 
     116            2 :     action.reset()
     117            2 :     action.clampWhenFinished = false
     118            2 :     action.loop = THREE.LoopRepeat
     119              : 
     120            2 :     if (this.isAnimationPlaying) {
     121            0 :       action.play()
     122              :     } else {
     123            2 :       action.play()
     124            2 :       action.paused = true
     125              :     }
     126              : 
     127            2 :     this.animationActions = [action]
     128              : 
     129              :     // Emit initial progress to set duration
     130            2 :     this.eventManager.emitEvent('animationProgressChange', {
     131              :       progress: 0,
     132              :       currentTime: 0,
     133              :       duration: clip.duration
     134              :     })
     135              :   }
     136              : 
     137              :   toggleAnimation(play?: boolean): void {
     138            1 :     if (!this.currentAnimation || this.animationActions.length === 0) {
     139            0 :       console.warn('No animation to toggle')
     140            0 :       return
     141              :     }
     142              : 
     143            1 :     this.isAnimationPlaying = play ?? !this.isAnimationPlaying
     144              : 
     145            1 :     this.animationActions.forEach((action) => {
     146            1 :       if (this.isAnimationPlaying) {
     147            1 :         action.paused = false
     148            1 :         if (action.time === 0 || action.time === action.getClip().duration) {
     149            1 :           action.reset()
     150              :         }
     151              :       } else {
     152            0 :         action.paused = true
     153              :       }
     154              :     })
     155              :   }
     156              : 
     157              :   update(delta: number): void {
     158          999 :     if (this.currentAnimation && this.isAnimationPlaying) {
     159            6 :       this.currentAnimation.update(delta)
     160              : 
     161            6 :       if (this.animationActions.length > 0) {
     162            6 :         const action = this.animationActions[0]
     163            6 :         const clip = action.getClip()
     164            6 :         const progress = (action.time / clip.duration) * 100
     165            6 :         this.eventManager.emitEvent('animationProgressChange', {
     166              :           progress,
     167              :           currentTime: action.time,
     168              :           duration: clip.duration
     169              :         })
     170              :       }
     171              :     }
     172              :   }
     173              : 
     174              :   getAnimationTime(): number {
     175            0 :     if (this.animationActions.length === 0) return 0
     176            0 :     return this.animationActions[0].time
     177              :   }
     178              : 
     179              :   getAnimationDuration(): number {
     180            0 :     if (this.animationActions.length === 0) return 0
     181            0 :     return this.animationActions[0].getClip().duration
     182              :   }
     183              : 
     184              :   setAnimationTime(time: number): void {
     185            0 :     if (this.animationActions.length === 0) return
     186            0 :     const duration = this.getAnimationDuration()
     187            0 :     const clampedTime = Math.max(0, Math.min(time, duration))
     188              : 
     189              :     // Temporarily unpause to allow time update, then restore
     190            0 :     const wasPaused = this.animationActions.map((action) => action.paused)
     191            0 :     this.animationActions.forEach((action) => {
     192            0 :       action.paused = false
     193            0 :       action.time = clampedTime
     194              :     })
     195              : 
     196            0 :     if (this.currentAnimation) {
     197            0 :       this.currentAnimation.setTime(clampedTime)
     198            0 :       this.currentAnimation.update(0)
     199              :     }
     200              : 
     201              :     // Restore paused state
     202            0 :     this.animationActions.forEach((action, i) => {
     203            0 :       action.paused = wasPaused[i]
     204              :     })
     205              : 
     206            0 :     this.eventManager.emitEvent('animationProgressChange', {
     207              :       progress: (clampedTime / duration) * 100,
     208              :       currentTime: clampedTime,
     209              :       duration
     210              :     })
     211              :   }
     212              : 
     213              :   reset(): void {}
     214              : }
        

Generated by: LCOV version 2.0-1