admin管理员组文章数量:1203817
I’m working on a project where I’m switching between multiple animations (e.g., sitting, pointing, phone) based on scroll position. The animations are loaded using useFBX and applied to a model using useAnimations.
The issue I’m facing is that when I scroll the page, the current animation resets and starts playing again. This is causing bad user experience. I want my animation to keep playing continiuosely while I'm scrolling page. Here’s a simplified version of my code:
const { nodes, materials } = useGLTF('./models/developer.glb') as CustomGLTF;
const { animations: phoneAnimation } = useFBX('./models/phone.fbx');
const { animations: sittingAnimation } = useFBX('./models/sitting.fbx');
const { animations: pointingAnimation } = useFBX('./models/pointing.fbx');
phoneAnimation[0].name = 'phone';
sittingAnimation[0].name = 'sitting';
pointingAnimation[0].name = 'pointing';
const { actions } = useAnimations(
[phoneAnimation[0], sittingAnimation[0], pointingAnimation[0]],
group
);
const scrollOffset = useScrollAnimation();
const [currentAnimation, setCurrentAnimation] = useState<string>('sitting');
useEffect(() => {
if (scrollOffset < 0.45) {
setCurrentAnimation('sitting');
} else if (scrollOffset < 0.75) {
setCurrentAnimation('pointing');
} else {
setCurrentAnimation('phone');
}
}, [scrollOffset]);
useEffect(() => {
if (actions[currentAnimation]) {
actions[currentAnimation].play();
}
}, [currentAnimation, actions]);
I think the problem is that actions keep updating on every scroll, but when i remove it from dependencies array, the animations are not playing at all.
How can I prevent animations from resetting when scroll position changes?
版权声明:本文标题:reactjs - How to prevent animations from resetting when scrolling in Three.jsReact Three Fiber? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738676525a2106295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论