GLTF to JSX converter
npm install @react-three/gltfjsxhttps://user-images.githubusercontent.com/2223602/126318148-99da7ed6-a578-48dd-bdd2-21056dbad003.mp4
 
A small command-line tool that turns GLTF assets into declarative and re-usable react-three-fiber JSX components.
- They're dumped wholesale into the scene which prevents re-use
- Contents can only be found by traversal which is cumbersome and slow
- Changes are made by mutation, which alters the source data and, again, prevents re-use
- Making contents conditional or adding/removing nodes is hard
Gltfjsx creates a virtual, nested graph of all the objects and materials inside your asset. It will not touch or modify your files in any way. Now you can easily make the data dynamic, alter contents, add events, or re-use the asset.
``bash
Usage
$ npx gltfjsx [Model.js] [options]
Options
--types, -t Add Typescript definitions
--verbose, -v Verbose output w/ names and empty groups
--meta, -m Include metadata (as userData)
--shadows, s Let meshes cast and receive shadows
--printwidth, w Prettier printWidth (default: 120)
--precision, -p Number of fractional digits (default: 2)
--draco, -d Draco binary path
--root, -r Sets directory from which .gltf file is served
--transform, -T Transform the asset for the web (draco, prune, resize)
--debug, -D Debug output
`
First you run your model through gltfjsx. npx allows you to use npm packages without installing them.
`bash`
npx gltfjsx model.gltf
It creates a javascript file that plots out all of the assets contents. The original gltf must still be be in your /public folder of course.
`jsx
/*
auto-generated by: https://github.com/pmdrs/gltfjsx
author: abcdef (https://sketchfab.com/abcdef)
license: CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
source: https://sketchfab.com/models/...
title: Model
*/
import { useGLTF, PerspectiveCamera } from '@react-three/drei'
export default function Model(props) {
const { nodes, materials } = useGLTF('/model.gltf')
return (
)
}
useGLTF.preload('/model.gltf')
`
This component can now be dropped into your scene. It is asynchronous and therefore must be wrapped into which gives you full control over intermediary loading-fallbacks and error handling.
`jsx
import { Canvas } from '@react-three/fiber'
import { Suspense } from 'react'
import Model from './Model'
function App() {
return (
Now you could re-use it:
`jsx`
Or make the model dynamic. Change its colors for example:
`jsx`
Or exchange materials:
`jsx`
Make contents conditional:
`jsx`
{condition &&
Add events:
`jsx`
#### ⚡️ Draco compression ootb
You don't need to do anything if your models are draco compressed, since useGLTF defaults to a draco CDN. By adding the --draco flag you can refer to local binaries which must reside in your /public folder.
#### ⚡️ Easier access to animations
If your GLTF contains animations it will add drei's useAnimations hook, which extracts all clips and prepares them as actions:
`jsx`
const { nodes, materials, animations } = useGLTF('/model.gltf')
const { actions } = useAnimations(animations, group)
If you want to play an animation you can do so at any time:
`jsx`
If you want to blend animations:
`jsx`
const [name, setName] = useState("jump")
...
useEffect(() => {
actions[name].reset().fadeIn(0.5).play()
return () => actions[name].fadeOut(0.5)
}, [name])
#### ⚡️ Preload your assets for faster response
The asset will be preloaded by default, this makes it quicker to load and reduces time-to-paint. Remove the preloader if you don't need it.
`jsx`
useGLTF.preload('/model.gltf')
#### ⚡️ Type-safety
Add the --types flag and your GLTF will be typesafe.
`tsx
type GLTFResult = GLTF & {
nodes: { robot: THREE.Mesh; rocket: THREE.Mesh }
materials: { metal: THREE.MeshStandardMaterial; wood: THREE.MeshStandardMaterial }
}
export default function Model(props: JSX.IntrinsicElements['group']) {
const { nodes, materials } = useGLTF
`
#### ⚡️ Auto-transform (compression, resize)
With the --transform flag it creates a binary-packed, draco-compressed, texture-resized (1024x1024), deduped and pruned GLTF ready to be consumed on a web site. It uses glTF-Transform. It will not alter the original but create a copy and append [modelname]-transformed.glb.
`jsx
import { parse } from '@react-three/gltfjsx'
import { GLTFLoader, DRACOLoader } from 'three-stdlib'
const gltfLoader = new GLTFLoader()
const dracoloader = new DRACOLoader()
dracoloader.setDecoderPath('https://www.gstatic.com/draco/v1/decoders/')
gltfLoader.setDRACOLoader(dracoloader)
gltfLoader.load(url, (gltf) => {
const jsx = parse(filename, gltf, config)
})
`
The GLTFStructureLoader can come in handy while testing gltf assets. It allows you to extract the structure without the actual binaries and textures making it possible to run in a testing environment.
`jsx
import { GLTFStructureLoader } from '@react-three/gltfjsx'
import fs from 'fs/promises'
it('should have a scene with a blue mesh', async () => {
const data = await fs.readFile('./model.glb')
const { scene } = await new Promise(res => loader.parse(data, '', res))
expect(() => scene.children.length).toEqual(1)
expect(() => scene.children[0].type).toEqual("mesh")
expect(() => scene.children[0].material.color).toEqual("blue")
})
`
- Nodejs must be installed
- The GLTF file has to be present in your projects /public` folder
- three (>= 121.x)
- @react-three/fiber (>= 5.x)
- @react-three/drei (>= 2.x)