Avoiding lag spikes when loading glTFs: Idiomatic, scalable way to precompile shaders? #821
-
When I load a glTF model in r3f, the scene visibly lags as the shaders have to be compiled after rendering has already started. In vanilla Three.js, this can be avoided by adding the glTF to the scene and running I can achieve the same result in r3f by loading my model in Here are examples of each implementation: NOTE: the CSB editor will contribute to lag, so open the preview in a new tab when checking for hiccups How should I go about loading models without lag in a way that isolates them to their own component? I need something like I'd also expect a solution should let me add the model to the scene declaratively instead of manually calling |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You can use suspense for this. use-asset. I've done this before and it works well. |
Beta Was this translation helpful? Give feedback.
-
the idea is that instead of doing this work in useEffect which just creates a lag next frame once the model has already loaded you suspend the component until everything's done. the drawback is that at that point the component has not rendered so there is no view and nothing's been added to the scene. but if you found a way to precompile a texture then you do something along the lines of this: import { useAsset } from "use-assets"
function Foo() {
const material = use???(???)
useAsset(() => {
precompile(material)
}, [material])
...
<Suspense fallback={...}>
<Foo />
</Suspense> |
Beta Was this translation helpful? Give feedback.
the idea is that instead of doing this work in useEffect which just creates a lag next frame once the model has already loaded you suspend the component until everything's done. the drawback is that at that point the component has not rendered so there is no view and nothing's been added to the scene. but if you found a way to precompile a texture then you do something along the lines of this: