Hi all,
in my R3F project, I have multiple scenes, each with their own camera rendering to a renderTarget (set up in SceneComponent.jsx). In my main scene, I show these scenes and the transitions between with a shader material based on the scroll position. The main scene gets rendered with the default camera.
How would I go about compiling my custom scenes with their own cameras, other than the default scene from the Canvas component? I want to eliminate the stutter at the first transition which you can see in the video.
The important part of my MainScene.jsx look like this:
useFrame(({ gl, scene, camera }) => {
gl.setRenderTarget(null);
gl.render(scene, camera);
materialRef.current.progression = props.globalProgress.current;
});
return (
<>
<mesh scale={scale}>
<planeGeometry />
<transitionMaterial
ref={materialRef}
progression={props.globalProgress.current}
tex0={globalScenes[0].renderTarget.texture}
tex1={globalScenes[1].renderTarget.texture}
tex2={globalScenes[2].renderTarget.texture}
/>
</mesh>
{globalScenes.map((scene, index) => (
<SceneComponent
key={index}
renderTarget={scene.renderTarget}
></SceneComponent>
))}
</>
);
The custom SceneComponent.jsx roughly looks like this:
const { cameras, animations } = useGLTF(props.model);
const scene = useRef();
const { gl } = useThree();
useEffect(() => {
useGLTF.preload(props.model);
// gl.compile(scene, cameras[0]);
// This leads to TypeError: targetScene.traverseVisible is not a function
}, [props.model]);
useFrame(({ gl }) => {
// if (!props.active) return;
gl.autoClear = true;
gl.setRenderTarget(props.renderTarget);
gl.render(scene.current, cameras[0]);
});
return (
<>
<scene ref={scene}>
<group ref={group} dispose={null}>
{props.sceneJSX}
</group>
</scene>
</>
)
I can’t wrap my head around how to compile these scenes, if I try to do gl.compile() and pass in the sceneRef and the camera which I got from the GLTF file, I get a TypeError: targetScene.traverseVisible is not a function
I’m also unsure if the way I set this up is valid at all. For instance, I noticed that in the first scene the EnvironmentMap reflections don’t show up until the stutter, when the next scenes get compiled (also visible in the video).
Any hint as to how to use gl.compile() in R3F would be much appreciated!