# drei-use-gltf **Use useGLTF for model loading with preloading.** ## Why It Matters `useGLTF` from Drei provides: - Suspense integration (automatic loading states) - Caching (same model loaded once) - Preloading capability - Draco decompression support ## Basic Example ```jsx import { useGLTF } from '@react-three/drei'; function Model() { const { scene, nodes, materials } = useGLTF('/model.glb'); return ; } ``` ## Preloading (Critical for UX) ```jsx import { useGLTF } from '@react-three/drei'; function Model() { const { scene } = useGLTF('/model.glb'); return ; } // Preload at module level - starts loading immediately useGLTF.preload('/model.glb'); // Or preload multiple models useGLTF.preload(['/model1.glb', '/model2.glb', '/model3.glb']); ``` ## With Draco Compression ```jsx function DracoModel() { // Second argument is the Draco decoder path const { scene } = useGLTF('/model.glb', '/draco/'); return ; } useGLTF.preload('/model.glb', '/draco/'); ``` ## Accessing Nodes and Materials ```jsx function Character() { const { nodes, materials } = useGLTF('/character.glb'); return ( ); } ``` ## Clone for Multiple Instances ```jsx function Tree({ position }) { const { scene } = useGLTF('/tree.glb'); // Clone to avoid sharing state between instances return ; } function Forest() { return ( <> ); } ``` ## With Suspense ```jsx import { Suspense } from 'react'; import { useGLTF } from '@react-three/drei'; function Model() { const { scene } = useGLTF('/model.glb'); return ; } function Scene() { return ( }> ); } function LoadingBox() { return ( ); } ``` ## TypeScript Types ```tsx import { useGLTF } from '@react-three/drei'; import { GLTF } from 'three-stdlib'; type GLTFResult = GLTF & { nodes: { Body: THREE.Mesh; Head: THREE.Mesh; }; materials: { Skin: THREE.MeshStandardMaterial; }; }; function Model() { const { nodes, materials } = useGLTF('/model.glb') as GLTFResult; // Now nodes.Body and materials.Skin are typed } ``` ## Dispose Handling R3F auto-disposes by default. To prevent disposal (for shared models): ```jsx function SharedModel({ model }) { return ; } ``` ## References - [Drei useGLTF](https://github.com/pmndrs/drei#usegltf) - [gltfjsx](https://github.com/pmndrs/gltfjsx) - Generate JSX from GLTF