I have an object that’s displaying on the canvas fine and it’s in a function but when I try to re-use that function and display it a second time, it only shows one?
Anyone know how I can copy the same object many times in a scene?
not the enrite model just the Object3D. geometries and materials are still the same (unlike with the useEffect method lol).
edit: since pika was overwhelmingly insulted by this simple answer, I thought I would add that you might want to wrap obj.clone() call in useMemo(…) to make it more garbage collector friendly.
model will be in parent2 bc three unmounts it. thats all that is happening here. useLoader is perfectly fine, it isn’t the cause of your problem.
if you want to duplicate, just like in vanilla you have to clone, or use gltfjsx. drei has a nice helper for cloning btw, it supports things that plain three object.clone() wouldn’t support like skinned meshes and has some shortcuts (for settings shadows and injecting materials).
useloader is nothing magical. i think it would be not good to use useEffect + fooloader directly because that way you don’t have react integration:
nothing knows when anything has loaded, nothing can respond, 90% of drei wouldn’t work, all interop down the drain. think <Center><Car /></Center>, luck bc center can’t expect model to be ready in useEffect, model is ready … whenever, basically pure chaos.
useloader executes new FooLoader().load(url, set... in a useEffect and integrates the result into react suspense. this is how other components can have interop. the react eco system couldn’t exist w/o it. i get that suspense is … different, even for devs familiar with react, but it’s such a critical feature.
also, avoiding it would be very bad for re-use and memory.
yeah but you could put something like const [loaded, setLoaded] = useState(false) and flip it once it loaded, no? you probably would have to put it up above in “CenteredCar” component
not so much. only the component itself would know loading state. nothing else outside would, unless you introduce leaks that tell the outside world, but that would require an api surface and couldn’t be dynamic. interop is perhaps the most important aspect about fiber, it means components that do not know one another can be orchestrated, like you chain functions in fp.
<Center>
<Car />
</Center>
this can only work if Center knows when Car has finished loading, it must calculate a box3 after all for bounds. async is an integral part of react, they call it “suspense”.
that’s how everything is built. even physics. great-robinson-i7nmsp - CodeSandbox<RigidBody> takes models because react ensures that useEffect means “the end of all processes” and if a component suspends that includes loading.