Dynamically use useGLTF.preload() #2383
-
Sorry, if its a silly question but. How can i dynamically use const DynaModel({mtype, ...props}) => {
// Inside Component dynamically change glb file.
const { nodes, materials } = useGLTF(`/${mtype}.glb`) as GLTFResult
const meshes: THREE.Mesh[] = Object.values(nodes).filter(
(value) => value.type === 'Mesh'
)
return(
<>
{meshes.map((mesh) => (
<mesh
key={mesh.name}
geometry={mesh.geometry}
material={materials[mesh.material.name]} // I plan to load materials in separate glb files, so I am refering like this.
></mesh>
))}
</>
)
}
// Outside Component How to dynamically use preload here?
useGTLF.preload( ) Since Just for some Context: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can call it whenever you want, it does no harm. Caching will be fine, first usegltf or preload that executes will load and parse, consecutive calls will suspend |
Beta Was this translation helpful? Give feedback.
-
@senorbeast, have you solved? Do you mind show how? Currently doing something similar and would be awesome to see the implementation and trying to figure out the best way to do that 👀 |
Beta Was this translation helpful? Give feedback.
You can call it whenever you want, it does no harm. Caching will be fine, first usegltf or preload that executes will load and parse, consecutive calls will suspend