-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(animation): Act 992 nova animation transition fix
* fix(animation): ACT-992 use current anim time - nova * fix(animation): ACT-992 use animation service loader * feat(animation): ACT-992 animation configuration
- Loading branch information
Showing
5 changed files
with
81 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 47 additions & 36 deletions
83
src/components/Models/MultipleAnimationModel/MultipleAnimationModel.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,108 @@ | ||
import React, { FC, useEffect, useRef, useState } from 'react'; | ||
import { useFrame, useGraph } from '@react-three/fiber'; | ||
import { AnimationAction, AnimationClip, AnimationMixer, Group } from 'three'; | ||
import { GLTFLoader } from 'three-stdlib'; | ||
import { AnimationAction, AnimationClip, AnimationMixer } from 'three'; | ||
|
||
import { Model } from 'src/components/Models/Model'; | ||
import { BaseModelProps } from 'src/types'; | ||
import { useEmotion, useGltfCachedLoader, useIdleExpression } from 'src/services'; | ||
import { AnimationConfiguration, BaseModelProps } from 'src/types'; | ||
import { useEmotion, useFallback, useGltfCachedLoader, useIdleExpression } from 'src/services'; | ||
import { Emotion } from 'src/components/Avatar/Avatar.component'; | ||
import { loadAnimationClip } from 'src/services/Animation.service'; | ||
|
||
export interface MultipleAnimationModelProps extends BaseModelProps { | ||
modelSrc: string | Blob; | ||
animations: Record<string, string>; | ||
activeAnimation: string; | ||
scale?: number; | ||
emotion?: Emotion; | ||
animationConfig?: AnimationConfiguration; | ||
} | ||
|
||
export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({ | ||
modelSrc, | ||
animations, | ||
activeAnimation, | ||
scale = 1, | ||
setModelFallback, | ||
onLoaded, | ||
emotion, | ||
bloom, | ||
animationConfig, | ||
materialConfig | ||
}) => { | ||
const groupRef = useRef<Group>(null); | ||
const mixerRef = useRef<AnimationMixer | null>(null); | ||
const activeActionRef = useRef<AnimationAction | null>(null); | ||
const animationTimeRef = useRef<number>(0); | ||
|
||
const [loadedAnimations, setLoadedAnimations] = useState<Record<string, AnimationClip>>({}); | ||
const [activeAction, setActiveAction] = useState<AnimationAction | null>(null); | ||
|
||
const { scene } = useGltfCachedLoader(modelSrc); | ||
const { nodes } = useGraph(scene); | ||
|
||
useEffect(() => { | ||
if (scene && groupRef.current) { | ||
groupRef.current.add(scene); | ||
const loadAllAnimations = async () => { | ||
const clips: Record<string, AnimationClip> = {}; | ||
|
||
await Promise.all( | ||
Object.keys(animations).map(async (name) => { | ||
const newClip = await loadAnimationClip(animations[name]); | ||
clips[name] = newClip; | ||
}) | ||
); | ||
|
||
setLoadedAnimations(clips); | ||
}; | ||
|
||
mixerRef.current = new AnimationMixer(scene); | ||
loadAllAnimations(); | ||
}, [animations]); | ||
|
||
useEffect(() => { | ||
if (scene) { | ||
const mixer = new AnimationMixer(scene); | ||
mixerRef.current = mixer; | ||
|
||
if (activeActionRef.current) { | ||
const newAction = mixer.clipAction(activeActionRef.current.getClip()); | ||
|
||
newAction.play(); | ||
mixer.update(animationTimeRef.current); | ||
|
||
activeActionRef.current = newAction; | ||
} | ||
} | ||
|
||
return () => { | ||
mixerRef.current?.stopAllAction(); | ||
mixerRef.current?.uncacheRoot(scene); | ||
}; | ||
}, [scene]); | ||
|
||
useEffect(() => { | ||
const loader = new GLTFLoader(); | ||
Object.keys(animations).forEach((name) => { | ||
loader.load(animations[name], (gltf) => { | ||
const newClip = gltf.animations[0]; | ||
setLoadedAnimations((prev) => ({ | ||
...prev, | ||
[name]: newClip | ||
})); | ||
}); | ||
}); | ||
}, [animations]); | ||
|
||
useEffect(() => { | ||
const mixer = mixerRef.current; | ||
const prevAction = activeActionRef.current; | ||
const newClip = loadedAnimations[activeAnimation]; | ||
|
||
if (!newClip || !mixer) return; | ||
if (prevAction && prevAction.getClip().name === newClip.name) return; | ||
|
||
const newAction = mixer.clipAction(newClip); | ||
const prevAction = activeAction; | ||
activeActionRef.current = newAction; | ||
|
||
if (prevAction) { | ||
newAction.reset(); | ||
newAction.crossFadeFrom(prevAction, 0.5, true); | ||
newAction.crossFadeFrom(prevAction, animationConfig?.crossfadeDuration ?? 0.5, true); | ||
} | ||
|
||
newAction.play(); | ||
setActiveAction(newAction); | ||
}, [activeAnimation, loadedAnimations, activeAction, nodes.Armature]); | ||
}, [activeAnimation, loadedAnimations, animationConfig]); | ||
|
||
useFrame((state, delta) => { | ||
mixerRef.current?.update(delta); | ||
animationTimeRef.current = activeActionRef.current?.time || 0; | ||
}); | ||
|
||
useEmotion(nodes, emotion); | ||
useIdleExpression('blink', nodes); | ||
useFallback(nodes, setModelFallback); | ||
|
||
return ( | ||
<Model | ||
modelRef={groupRef} | ||
scene={scene} | ||
scale={scale} | ||
onLoaded={onLoaded} | ||
bloom={bloom} | ||
materialConfig={materialConfig} | ||
/> | ||
); | ||
return <Model scene={scene} scale={scale} onLoaded={onLoaded} bloom={bloom} materialConfig={materialConfig} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters