Hello, I have a collection of .glTF models I am loading and an HDR background.
My desire is that there be some kind of spinner or user notification indicating that these models are loading.
I’ve tried using the Loader component from Drei, but it has no effect seemingly. I’ve tried passing different components to the fallback prop of Suspense – but that causes an error that the passed component is not part of the THREE namespace.
(I’ve removed some other components that are sitting outside the suspense like OrbitControls, Effects, and Lights)
I am passing an array of JSX components (that load .glTF models) as props to the ProductLine component, which loads them in one group. I thought that could be causing an issue, so I tried it with just one component that loads a single model (instead of ProductLine) but I still got a crash trying to set the fallback of suspense.
Update: I noticed that I can pass a Three object as a fallback, which makes sense – but I’m still having an issue because, I get sort of the following logic here,
HDR background loads
Pause of nothing happening for 4-5 seconds
Fallback shows very briefly before rendering the glTF model
I have an async useEffect() running
useEffect(() => {
(async () => {
//Initializes the models prop to be rendered
})();
}, []);
you can also use threes own defaultloadingmanager, this is what vanilla apps would do, it’s completely up to you how or where you use it.
everything inside suspense loads the same time, you can add a fallback. the fallback runs in the threejs tree, not the dom. but you could useEffect communicate intent upwards to the dom if you wanted. you can make loading fine grained by using multiple suspense, if you want stuff to show immediately, or if you have things that aren’t async.
would also make sense to preload everything. all loaders have preload functions so that the models begin to fetch and parse immediately instead of waiting for the components to be mounted.
its a pre-cache. suspense is a cache, so it starts fetching and parsing: new GLTFLoader().load(url, data => …). if the component requests useGLTF with the same key (the url) it will refer to either the completed loader result, or it will await the ongoing fetch.
using useEffect to communicate intent upwards to the dom?
it’s completely open ended, you can make it your own. and again, defaultloadingmanager also works. you could also use tunnel-rat to write into the dom from within the canvas.
suspense is a good topic to study and read up on as it solves many of the traditional problems around dealing with async. there are patterns and features that can do wonders, for instance: