• Stars
    star
    26,202
  • Rank 771 (Top 0.02 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 6 months ago

Reviews

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

Repository Details

๐Ÿ‡จ๐Ÿ‡ญ A React renderer for Three.js

@react-three/fiber

Version Downloads Twitter Discord Open Collective ETH BTC

react-three-fiber is a React renderer for threejs.

Build your scene declaratively with re-usable, self-contained components that react to state, are readily interactive and can participate in React's ecosystem.

npm install three @types/three @react-three/fiber

Does it have limitations?

None. Everything that works in Threejs will work here without exception.

Is it slower than plain Threejs?

No. There is no overhead. Components render outside of React. It outperforms Threejs in scale due to Reacts scheduling abilities.

Can it keep up with frequent feature updates to Threejs?

Yes. It merely expresses Threejs in JSX, <mesh /> dynamically turns into new THREE.Mesh(). If a new Threejs version adds, removes or changes features, it will be available to you instantly without depending on updates to this library.

What does it look like?

Let's make a re-usable component that has its own state, reacts to user-input and participates in the render-loop. (live demo).
import { createRoot } from 'react-dom/client'
import React, { useRef, useState } from 'react'
import { Canvas, useFrame } from '@react-three/fiber'

function Box(props) {
  // This reference gives us direct access to the THREE.Mesh object
  const ref = useRef()
  // Hold state for hovered and clicked events
  const [hovered, hover] = useState(false)
  const [clicked, click] = useState(false)
  // Subscribe this component to the render-loop, rotate the mesh every frame
  useFrame((state, delta) => (ref.current.rotation.x += delta))
  // Return the view, these are regular Threejs elements expressed in JSX
  return (
    <mesh
      {...props}
      ref={ref}
      scale={clicked ? 1.5 : 1}
      onClick={(event) => click(!clicked)}
      onPointerOver={(event) => hover(true)}
      onPointerOut={(event) => hover(false)}>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  )
}

createRoot(document.getElementById('root')).render(
  <Canvas>
    <ambientLight intensity={Math.PI / 2} />
    <spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} decay={0} intensity={Math.PI} />
    <pointLight position={[-10, -10, -10]} decay={0} intensity={Math.PI} />
    <Box position={[-1.2, 0, 0]} />
    <Box position={[1.2, 0, 0]} />
  </Canvas>,
)
Show TypeScript example
npm install @types/three
import * as THREE from 'three'
import { createRoot } from 'react-dom/client'
import React, { useRef, useState } from 'react'
import { Canvas, useFrame, ThreeElements } from '@react-three/fiber'

function Box(props: ThreeElements['mesh']) {
  const ref = useRef<THREE.Mesh>(null!)
  const [hovered, hover] = useState(false)
  const [clicked, click] = useState(false)
  useFrame((state, delta) => (ref.current.rotation.x += delta))
  return (
    <mesh
      {...props}
      ref={ref}
      scale={clicked ? 1.5 : 1}
      onClick={(event) => click(!clicked)}
      onPointerOver={(event) => hover(true)}
      onPointerOut={(event) => hover(false)}>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  )
}

createRoot(document.getElementById('root') as HTMLElement).render(
  <Canvas>
    <ambientLight intensity={Math.PI / 2} />
    <spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} decay={0} intensity={Math.PI} />
    <pointLight position={[-10, -10, -10]} decay={0} intensity={Math.PI} />
    <Box position={[-1.2, 0, 0]} />
    <Box position={[1.2, 0, 0]} />
  </Canvas>,
)

Live demo: https://codesandbox.io/s/icy-tree-brnsm?file=/src/App.tsx

Show React Native example

This example relies on react 18 and uses expo-cli, but you can create a bare project with their template or with the react-native CLI.

# Install expo-cli, this will create our app
npm install expo-cli -g
# Create app and cd into it
expo init my-app
cd my-app
# Install dependencies
npm install three @react-three/fiber@beta react@rc
# Start
expo start

Some configuration may be required to tell the Metro bundler about your assets if you use useLoader or Drei abstractions like useGLTF and useTexture:

// metro.config.js
module.exports = {
  resolver: {
    sourceExts: ['js', 'jsx', 'json', 'ts', 'tsx', 'cjs'],
    assetExts: ['glb', 'png', 'jpg'],
  },
}
import React, { useRef, useState } from 'react'
import { Canvas, useFrame } from '@react-three/fiber/native'
function Box(props) {
  const mesh = useRef(null)
  const [hovered, setHover] = useState(false)
  const [active, setActive] = useState(false)
  useFrame((state, delta) => (mesh.current.rotation.x += delta))
  return (
    <mesh
      {...props}
      ref={mesh}
      scale={active ? 1.5 : 1}
      onClick={(event) => setActive(!active)}
      onPointerOver={(event) => setHover(true)}
      onPointerOut={(event) => setHover(false)}>
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  )
}
export default function App() {
  return (
    <Canvas>
      <ambientLight intensity={Math.PI / 2} />
      <spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} decay={0} intensity={Math.PI} />
      <pointLight position={[-10, -10, -10]} decay={0} intensity={Math.PI} />
      <Box position={[-1.2, 0, 0]} />
      <Box position={[1.2, 0, 0]} />
    </Canvas>
  )
}

Documentation, tutorials, examples

Visit docs.pmnd.rs

First steps

You need to be versed in both React and Threejs before rushing into this. If you are unsure about React consult the official React docs, especially the section about hooks. As for Threejs, make sure you at least glance over the following links:

  1. Make sure you have a basic grasp of Threejs. Keep that site open.
  2. When you know what a scene is, a camera, mesh, geometry, material, fork the demo above.
  3. Look up the JSX elements that you see (mesh, ambientLight, etc), all threejs exports are native to three-fiber.
  4. Try changing some values, scroll through our API to see what the various settings and hooks do.

Some helpful material:

Ecosystem

There is a vibrant and extensive eco system around three-fiber, full of libraries, helpers and abstractions.

Who is using Three-fiber

A small selection of companies and projects relying on three-fiber.

How to contribute

If you like this project, please consider helping out. All contributions are welcome as well as donations to Opencollective, or in crypto BTC: 36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH, ETH: 0x6E3f79Ea1d0dcedeb33D3fC6c34d2B1f156F2682.

Backers

Thank you to all our backers! ๐Ÿ™

Contributors

This project exists thanks to all the people who contribute.

More Repositories

1

zustand

๐Ÿป Bear necessities for state management in React
TypeScript
45,348
star
2

react-spring

โœŒ๏ธ A spring physics based React animation library
TypeScript
27,857
star
3

jotai

๐Ÿ‘ป Primitive and flexible state management for React
TypeScript
18,007
star
4

use-gesture

๐Ÿ‘‡Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
TypeScript
8,861
star
5

valtio

๐Ÿ’Š Valtio makes proxy-state simple for React and Vanilla
TypeScript
8,738
star
6

drei

๐Ÿฅ‰ useful helpers for react-three-fiber
JavaScript
8,042
star
7

leva

๐ŸŒ‹ React-first components GUI
TypeScript
4,825
star
8

gltfjsx

๐ŸŽฎ Turns GLTFs into JSX components
JavaScript
4,251
star
9

use-cannon

๐Ÿ‘‹๐Ÿ’ฃ physics based hooks for @react-three/fiber
TypeScript
2,700
star
10

react-three-next

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

postprocessing

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

racing-game

๐ŸŽ Open source racing game developed by everyone willing
TypeScript
2,120
star
13

xr

๐Ÿคณ VR/AR for react-three-fiber
TypeScript
2,051
star
14

uikit

๐ŸŽจ user interfaces for react-three-fiber
TypeScript
2,048
star
15

react-three-flex

๐Ÿ’ช๐Ÿ“ฆ Flexbox for react-three-fiber
TypeScript
1,640
star
16

suspend-react

๐Ÿšฅ Async/await for React components
TypeScript
1,358
star
17

react-postprocessing

๐Ÿ“ฌ postprocessing for react-three-fiber
JavaScript
1,074
star
18

detect-gpu

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

react-three-rapier

๐Ÿคบ Rapier physics in React
TypeScript
1,005
star
20

lamina

๐Ÿฐ An extensible, layer based shader material for ThreeJS
TypeScript
1,005
star
21

its-fine

๐Ÿถ๐Ÿ”ฅ A collection of escape hatches for React.
TypeScript
978
star
22

react-use-measure

๐Ÿ™Œ Utility to measure view bounds
TypeScript
832
star
23

react-nil

โƒ A react null renderer
TypeScript
785
star
24

maath

๐Ÿชถ Math helpers for the rest of us
TypeScript
783
star
25

threejs-journey

โš›๏ธ Bruno Simons journey demos in React
TypeScript
718
star
26

three-stdlib

๐Ÿ“š Stand-alone library of threejs examples designed to run without transpilation in node & browser
JavaScript
651
star
27

react-three-editor

๐Ÿ”Œ A one of a kind scene editor that writes changes back into your code
TypeScript
615
star
28

react-three-a11y

โ™ฟ๏ธ Accessibility tools for React Three Fiber
TypeScript
534
star
29

ecctrl

๐Ÿ•น๏ธ A floating rigibody character controller
TypeScript
498
star
30

react-three-offscreen

๐Ÿ“บ Offscreen worker canvas for react-three-fiber
TypeScript
443
star
31

react-zdog

โšก๏ธ๐Ÿถ React bindings for zdog
JavaScript
441
star
32

drei-vanilla

๐Ÿฆ drei-inspired helpers for threejs
TypeScript
436
star
33

use-asset

๐Ÿ“ฆ A promise caching strategy for React Suspense
TypeScript
413
star
34

tunnel-rat

๐Ÿ€ Non gratum anus rodentum
TypeScript
329
star
35

react-three-csg

๐Ÿšง Constructive solid geometry for React
TypeScript
264
star
36

react-three-lgl

๐Ÿ”† A React abstraction for the LGL Raycaster
TypeScript
262
star
37

gltf-react-three

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

market

๐Ÿ“ฆ Download CC0 assets ready to use in your next 3D Project
JavaScript
250
star
39

component-material

๐Ÿงฉ Compose modular materials in React
TypeScript
160
star
40

env

๐Ÿ’„ An app to create, edit, and preview HDR environment maps in the browser
TypeScript
151
star
41

react-ogl

๐Ÿฆด A barebones react renderer for ogl.
TypeScript
150
star
42

use-p2

๐Ÿ‘‹๐Ÿ’ฃ 2d physics hooks for @react-three/fiber
TypeScript
144
star
43

react-spring-examples

JavaScript
139
star
44

react-three-gpu-pathtracer

โšก๏ธ A React abstraction for the popular three-gpu-pathtracer
TypeScript
132
star
45

react-three-lightmap

In-browser lightmap/AO baker for react-three-fiber and ThreeJS
TypeScript
127
star
46

cannon-es-debugger

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

rafz

๐Ÿ’ One loop to frame them all.
TypeScript
96
star
48

docs

๐Ÿ–จ๏ธ mdx documentation generator for `pmndrs/*/docs` folders
TypeScript
91
star
49

assets

๐Ÿ“ฆ Importable base64 encoded CC0 assets
Makefile
91
star
50

swc-jotai

Rust
88
star
51

react-three-jolt

โšก Jolt physics in React
TypeScript
84
star
52

react-three-scissor

โœ‚ Multiple scenes, one canvas! WebGL Scissoring implementation for React Three Fiber.
TypeScript
79
star
53

eslint-plugin-valtio

An eslint plugin for better valtio experience
JavaScript
74
star
54

react-three-babel

๐Ÿ› A Babel plugin that automatically builds the extend catalogue of known native Three.js elements
TypeScript
60
star
55

react-spring.io

โœŒ๏ธ A spring physics based React animation library
TypeScript
56
star
56

r3f-website

Website for React Three Fiber
JavaScript
27
star
57

directed

A flexible, minimal scheduler written in TypeScript
TypeScript
24
star
58

market-assets

JavaScript
19
star
59

react-three-8thwall

JavaScript
17
star
60

drei-assets

JavaScript
16
star
61

discord

๐Ÿค– Poimandres Discord Bot
TypeScript
10
star
62

branding

TypeScript
7
star
63

market-assets-do

JavaScript
5
star
64

envinfo

Easily collect useful information for bug reports
JavaScript
4
star
65

leva-wg

1
star