• Stars
    star
    3,949
  • Rank 10,532 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ðŸŽŪ Turns GLTFs into JSX components
gltfjsx-preview.mp4


Version Discord Shield

A small command-line tool that turns GLTF assets into declarative and re-usable react-three-fiber JSX components.

The GLTF workflow on the web is not ideal ...

  • GLTF is thrown whole into the scene which prevents re-use, in threejs objects can only be mounted once
  • Contents can only be found by traversal which is cumbersome and slow
  • Changes to queried nodes are made by mutation, which alters the source data and prevents re-use
  • Re-structuring content, making nodes conditional or adding/removing is cumbersome
  • Model compression is complex and not easily achieved
  • Models often have unnecessary nodes that cause extra work and matrix updates

GLTFJSX fixes that

  • 🧑‍ðŸ’ŧ It creates a virtual graph of all objects and materials. Now you can easily alter contents and re-use.
  • 🏎ïļ The graph gets pruned (empty groups, unnecessary transforms, ...) and will perform better.
  • ⚡ïļ It will optionally compress your model with up to 70%-90% size reduction.

Usage

Usage
  $ npx gltfjsx [Model.glb] [options]

Options
  --output, -o        Output file name/path
  --types, -t         Add Typescript definitions
  --keepnames, -k     Keep original names
  --keepgroups, -K    Keep (empty) groups, disable pruning
  --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: 3)
  --draco, -d         Draco binary path
  --root, -r          Sets directory from which .gltf file is served
  --instance, -i      Instance re-occuring geometry
  --instanceall, -I   Instance every geometry (for cheaper re-use)
  --transform, -T     Transform the asset for the web (draco, prune, resize)
    --resolution, -R  Resolution for texture resizing (default: 1024)
    --keepmeshes, -j  Do not join compatible meshes
    --keepnormals, -n Do not compress normal maps with loss
    --keepmaterials, -M Do not palette join materials
    --format, -f      Texture format (default: "webp")
    --simplify, -S    Mesh simplification (default: false)
      --weld          Weld tolerance (default: 0.00005)
      --ratio         Simplifier ratio (default: 0)
      --error         Simplifier error threshold (default: 0.0001)
  --debug, -D         Debug output

A typical use-case

First you run your model through gltfjsx. npx allows you to use npm packages without installing them.

npx gltfjsx model.gltf --transform

This will create a Model.jsx file that plots out all of the assets contents.

/*
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 function Model(props) {
  const { nodes, materials } = useGLTF('/model-transformed.glb')
  return (
    <group {...props} dispose={null}>
      <PerspectiveCamera name="camera" fov={40} near={10} far={1000} position={[10, 0, 50]} />      
      <pointLight intensity={10} position={[100, 50, 100]} rotation={[-Math.PI / 2, 0, 0]} />
      <group position={[10, -5, 0]}>
        <mesh geometry={nodes.robot.geometry} material={materials.metal} />
        <mesh geometry={nodes.rocket.geometry} material={materials.wood} />
      </group>
    </group>
  )
}

useGLTF.preload('/model.gltf')

Add your model to your /public folder as you would normally do. With the --transform flag it has created a compressed copy of it (in the above case model-transformed.glb). Without the flag just copy the original model.

/public
  model-transformed.glb

The component can now be dropped into your scene.

import { Canvas } from '@react-three/fiber'
import { Model } from './Model'

function App() {
  return (
    <Canvas>
      <Model />      

You can re-use it, it will re-use geometries and materials out of the box:

<Model position={[0, 0, 0]} />
<Model position={[10, 0, -10]} />

Or make the model dynamic. Change its colors for example:

<mesh geometry={nodes.robot.geometry} material={materials.metal} material-color="green" />

Or exchange materials:

<mesh geometry={nodes.robot.geometry}>
  <meshPhysicalMaterial color="hotpink" />
</mesh>

Make contents conditional:

{condition && <mesh geometry={nodes.robot.geometry} material={materials.metal} />}

Add events:

<mesh geometry={nodes.robot.geometry} material={materials.metal} onClick={handleClick} />

Features

⚡ïļ Draco and meshopt 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.

⚡ïļ 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.

useGLTF.preload('/model.gltf')

⚡ïļ Auto-transform (compression, resize)

With the --transform flag it creates a binary-packed, draco-compressed, texture-resized (1024x1024), webp compressed, deduped, instanced and pruned *.glb ready to be consumed on a web site. It uses glTF-Transform. This can reduce the size of an asset by 70%-90%.

It will not alter the original but create a copy and append [modelname]-transformed.glb.

⚡ïļ Type-safety

Add the --types flag and your GLTF will be typesafe.

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<GLTFResult>('/model.gltf')

⚡ïļ 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:

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:

<mesh onClick={(e) => actions.jump.play()} />

If you want to blend animations:

const [name, setName] = useState("jump")
...
useEffect(() => {
  actions[name].reset().fadeIn(0.5).play()
  return () => actions[name].fadeOut(0.5)
}, [name])

⚡ïļ Auto-instancing

Use the --instance flag and it will look for similar geometry and create instances of them. Look into drei/Merged to understand how it works. It does not matter if you instanced the model previously in Blender, it creates instances for each mesh that has a specific geometry and/or material.

--instanceall will create instances of all the geometry. This allows you to re-use the model with the smallest amount of drawcalls.

Your export will look like something like this:

const context = createContext()
export function Instances({ children, ...props }) {
  const { nodes } = useGLTF('/model-transformed.glb')
  const instances = useMemo(() => ({ Screw1: nodes['Screw1'], Screw2: nodes['Screw2'] }), [nodes])
  return (
    <Merged meshes={instances} {...props}>
      {(instances) => <context.Provider value={instances} children={children} />}
    </Merged>
  )
}

export function Model(props) {
  const instances = useContext(context)
  return (
    <group {...props} dispose={null}>
      <instances.Screw1 position={[-0.42, 0.04, -0.08]} rotation={[-Math.PI / 2, 0, 0]} />
      <instances.Screw2 position={[-0.42, 0.04, -0.08]} rotation={[-Math.PI / 2, 0, 0]} />
    </group>
  )
}

Note that similar to --transform it also has to transform the model. In order to use and re-use the model import both Instances and Model. Put all your models into the Instances component (you can nest).

The following will show the model three times, but you will only have 2 drawcalls tops.

import { Instances, Model } from './Model'

<Instances>
  <Model position={[10,0,0]}>
  <Model position={[-10,0,0]}>
  <Model position={[-10,10,0]}>
</Instance>

Using the parser stand-alone

import { parse } from '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(gltf, optionalConfig)
})

Using the parser stand-alone for scenes (object3d's)

const jsx = parse(scene, optionalConfig)

Using GLTFStructureLoader stand-alone

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.

import { GLTFStructureLoader } from 'gltfjsx'
import fs from 'fs/promises'

it('should have a scene with a blue mesh', async () => {
  const loader = new GLTFStructureLoader()
  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')  
})

Requirements

More Repositories

1

zustand

ðŸŧ Bear necessities for state management in React
TypeScript
40,808
star
2

react-spring

✌ïļ A spring physics based React animation library
TypeScript
27,160
star
3

react-three-fiber

ðŸ‡Ļ🇭 A React renderer for Three.js
TypeScript
25,320
star
4

jotai

ðŸ‘ŧ Primitive and flexible state management for React
TypeScript
16,893
star
5

use-gesture

👇Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
TypeScript
8,537
star
6

valtio

💊 Valtio makes proxy-state simple for React and Vanilla
TypeScript
8,257
star
7

drei

ðŸĨ‰ useful helpers for react-three-fiber
JavaScript
7,122
star
8

leva

🌋 React-first components GUI
TypeScript
4,528
star
9

use-cannon

👋ðŸ’Ģ physics based hooks for @react-three/fiber
TypeScript
2,654
star
10

react-three-next

React Three Fiber, Threejs, Nextjs starter
JavaScript
2,140
star
11

postprocessing

A post processing library for three.js.
JavaScript
2,100
star
12

racing-game

🏎 Open source racing game developed by everyone willing
TypeScript
2,094
star
13

react-xr

ðŸĪģ VR/AR with react-three-fiber
TypeScript
1,890
star
14

react-three-flex

💊ðŸ“Ķ Flexbox for react-three-fiber
TypeScript
1,606
star
15

suspend-react

ðŸšĨ Async/await for React components
TypeScript
1,308
star
16

react-postprocessing

📎 postprocessing for react-three-fiber
JavaScript
1,009
star
17

detect-gpu

Classifies GPUs based on their 3D rendering benchmark score allowing the developer to provide sensible default settings for graphically intensive applications.
TypeScript
979
star
18

lamina

🍰 An extensible, layer based shader material for ThreeJS
TypeScript
976
star
19

its-fine

ðŸķðŸ”Ĩ A collection of escape hatches for React.
TypeScript
891
star
20

react-use-measure

🙌 Utility to measure view bounds
TypeScript
799
star
21

react-nil

⃝ A react null renderer
TypeScript
772
star
22

react-three-rapier

ðŸĪš Rapier physics in React
TypeScript
765
star
23

maath

ðŸŠķ Math helpers for the rest of us
TypeScript
755
star
24

threejs-journey

⚛ïļ Bruno Simons journey demos in React
TypeScript
685
star
25

three-stdlib

📚 Stand-alone library of threejs examples designed to run without transpilation in node & browser
JavaScript
624
star
26

react-three-editor

🔌 A one of a kind scene editor that writes changes back into your code
TypeScript
602
star
27

react-three-a11y

â™ŋïļ Accessibility tools for React Three Fiber
TypeScript
507
star
28

react-zdog

⚡ïļðŸķ React bindings for zdog
JavaScript
441
star
29

use-asset

ðŸ“Ķ A promise caching strategy for React Suspense
TypeScript
414
star
30

react-three-offscreen

📚 Offscreen worker canvas for react-three-fiber
TypeScript
397
star
31

drei-vanilla

ðŸĶ drei-inspired helpers for threejs
TypeScript
364
star
32

ecctrl

ðŸ•đïļ A floating rigibody character controller
TypeScript
349
star
33

tunnel-rat

🐀 Non gratum anus rodentum
TypeScript
287
star
34

react-three-lgl

🔆 A React abstraction for the LGL Raycaster
TypeScript
260
star
35

market

ðŸ“Ķ Download CC0 assets ready to use in your next 3D Project
JavaScript
249
star
36

react-three-csg

🚧 Constructive solid geometry for React
TypeScript
242
star
37

gltf-react-three

Convert GLTF files to React Three Fiber Components
JavaScript
230
star
38

component-material

ðŸ§Đ Compose modular materials in React
TypeScript
161
star
39

env

💄 An app to create, edit, and preview HDR environment maps in the browser
TypeScript
143
star
40

use-p2

👋ðŸ’Ģ 2d physics hooks for @react-three/fiber
TypeScript
141
star
41

react-ogl

ðŸĶī A barebones react renderer for ogl.
TypeScript
139
star
42

react-spring-examples

JavaScript
138
star
43

react-three-gpu-pathtracer

⚡ïļ A React abstraction for the popular three-gpu-pathtracer
TypeScript
125
star
44

react-three-lightmap

In-browser lightmap/AO baker for react-three-fiber and ThreeJS
TypeScript
123
star
45

cannon-es-debugger

Wireframe debugger for use with cannon-es https://github.com/react-spring/cannon-es
HTML
103
star
46

rafz

💍 One loop to frame them all.
TypeScript
96
star
47

website

Poimandres developer collective website
JavaScript
87
star
48

swc-jotai

Rust
85
star
49

assets

ðŸ“Ķ Importable base64 encoded CC0 assets
Makefile
84
star
50

react-three-scissor

✂ Multiple scenes, one canvas! WebGL Scissoring implementation for React Three Fiber.
TypeScript
81
star
51

eslint-plugin-valtio

An eslint plugin for better valtio experience
JavaScript
69
star
52

react-spring.io

✌ïļ A spring physics based React animation library
TypeScript
56
star
53

react-three-babel

🛍 A Babel plugin that automatically builds the extend catalogue of known native Three.js elements
TypeScript
53
star
54

r3f-website

Website for React Three Fiber
JavaScript
26
star
55

market-assets

JavaScript
19
star
56

react-three-8thwall

JavaScript
17
star
57

drei-assets

JavaScript
16
star
58

discord

ðŸĪ– Poimandres Discord Bot
TypeScript
10
star
59

react-three-jolt

⚡ Jolt physics in React
CSS
10
star
60

branding

TypeScript
7
star
61

market-assets-do

JavaScript
5
star
62

envinfo

Easily collect useful information for bug reports
JavaScript
4
star
63

leva-wg

1
star