Good day everyone. I am building an interactive kids story experience and I have this situation where I am trying to set up a react three fiber project that has different scenes inside, switched based on a currentScene state along with some camera animations for the shots. Currently i am facing a good deal of lag during this switch no matter the hardware.
Here i am importing all my scenes with the switching logic
App.jsx
const sceneMap = {
1: Scene1,
2: Scene2,
3: Scene3,
4: Scene4,
5: Scene5,
6: Scene6,
};
const SceneComponent = ({ num, shotNumberRef }) => {
const Scene = sceneMap[num] || Scene1;
return <Scene shotNumber={shotNumberRef.current}\ />;
};
const App = () => {
const [currentScene, setCurrentScene] = useState(0);
const shotNumberRef = useRef(0);
.
.
.
<Canvas camera={{ position: [53, 8, 15], fov: 40 }}>
<SceneComponent num={currentScene} shotNumberRef={shotNumberRef} handleBlimpClick={handleBlimpClick} />
</Canvas>
This file controls the global timeline using gsap.
TimelineController.jsx
const t = gsap.timeline();
t.to( {}, { duration: 6.0, onStart: () => { setCurrentScene([2]); shotNumberRef.current = 1; }, immediateRender: true, })
.to( {}, { duration: 9.5, onStart: () => { setCurrentScene([4]); shotNumberRef.current = 1; }, immediateRender: true, },)
.to( {}, { duration: 2.5, onStart: () => { setCurrentScene([2]); shotNumberRef.current = 2; }, immediateRender: true, },)
.to( {}, { duration: 6.0, onStart: () => { setCurrentScene([4]); shotNumberRef.current = 2; }, immediateRender: true, },)
.to( {}, { duration: 4.0, onStart: () => { setCurrentScene([4]); shotNumberRef.current = 3; }, immediateRender: true, },)
.to( {}, { duration: 14, onStart: () => { setCurrentScene(2); shotNumberRef.current = 3;}, immediateRender: true,},)
This is an example of the camera animation involved based on shotNumber.
Scene files:
useGSAP(() => {
if (shotNumber == 1) {
gsap.fromTo(
camera,
{
fov: 10,
},
{
fov: 28,
duration: 3,
onUpdateParams: [camera],
onUpdate() {
if (!this.isActive()) return;
camera.updateProjectionMatrix();
},
},
);
}
}, [camera]);
This lag is only evident using gsap timeline, and not when using debug tools to switch the scene.
Here’s a sample recording.
I would be really grateful if someone could help me debug this.