8.17.10 • Published 2 months ago

@react-three/fiber v8.17.10

Weekly downloads
37
License
MIT
Repository
github
Last release
2 months ago

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 @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 React's scheduling abilities.

Can it keep up with frequent feature updates to Threejs?

Yes. It merely expresses Threejs in JSX: <mesh /> becomes new THREE.Mesh(), and that happens dynamically. 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?

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 />
    <pointLight position={[10, 10, 10]} />
    <Box position={[-1.2, 0, 0]} />
    <Box position={[1.2, 0, 0]} />
  </Canvas>,
)
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 />
    <pointLight position={[10, 10, 10]} />
    <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

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 />
      <pointLight position={[10, 10, 10]} />
      <Box position={[-1.2, 0, 0]} />
      <Box position={[1.2, 0, 0]} />
    </Canvas>
  )
}

Documentation, tutorials, examples

Visit docs.pmnd.rs

Fundamentals

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 reading material:

Ecosystem

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.

r3f-sdk-testprova-react-three-fiberreact-three-scisor@puneet_epigraph/epigraph_configurator_uispace-sessionsspacesessionselect-sessionselect-sessionsthreeddd-editor@product360/editorninja-coreninjagl-corecaroumesh-backup@partykit/site-new@kuraykaraaslan/control-view-cube@everything-registry/sub-chunk-767@nospoon/stereo-media@nospoon/webxr-components@nospoon/webxr-utils@typetool/commonpolygona-3d-clientpt-crystal-olivetailthemetest-foresttest-forest-sceneteacher-aitest-npm-rajwood-deck-demosaarshed-buildersams-web-game-enginereactavatarcreatroinventoryreact-xr-libreact3deasyroblox_next_3d_avatarspillthebeans_threejstest-r3f-projectswf-visualizerthreed-nav-testthreed-aithreed-gardenthrd-appthree-cityjsonthree-covervizboxx-viewervrmcanvasvite-react-ts-dashboardvitexprogram5vexel-toolsvmi-gscmeshbakermodel3dviewloan-frontendlemons-bloghehai-poc-frontjdls-boids-canvashuihuihuikrishmakhijanikrishmakhijani-portfoliokroulykrouly-embedhtml-fiberhpg-r3f-model-scenehagokitchen-color-studiojewelry-glamirahomogeneous-coordinates-visualisationlam-mvtlanding-page-3dlakmal-packagegle-scene-componentsgle-r3f-componentsgloreagraphmoon_threejsncage-cubemulti-ui-bundlemulti-ui-bundle-betamy-test-jewelname-sphere-threejs-npmname-sphere-threejspagenotfound_threejsopen-react-3d-viewerpshh-components-libraryprp-ui-componentsposchh-components-libraryportfolio-website-posilvapixetronics-editorplot3dpoint-cloud-visualiseroct-24-2pbl6-shop-onlinemodern-web-swiss-army-knifer3f-reliquaryr3f-splashscreenr3f-viamr3f-water-dropsr3f_xr3dyr3dynewr3f-dom-masonry
8.17.9

2 months ago

8.17.10

2 months ago

8.17.8

2 months ago

8.17.7

3 months ago

9.0.0-beta.1

5 months ago

9.0.0-beta.0

6 months ago

9.0.0-alpha.7

6 months ago

9.0.0-alpha.8

6 months ago

8.16.8

6 months ago

8.16.7

6 months ago

8.17.1

3 months ago

8.17.0

3 months ago

8.17.3

3 months ago

8.17.2

3 months ago

8.17.5

3 months ago

8.17.4

3 months ago

8.17.6

3 months ago

8.16.6

7 months ago

9.0.0-alpha.6

7 months ago

8.16.4

7 months ago

8.16.5

7 months ago

9.0.0-alpha.5

7 months ago

8.16.3

7 months ago

9.0.0-alpha.4

7 months ago

9.0.0-alpha.3

7 months ago

8.16.2

7 months ago

8.16.1

8 months ago

8.16.0

8 months ago

8.15.19

9 months ago

8.15.17

9 months ago

8.15.18

9 months ago

8.15.16

10 months ago

8.15.15

10 months ago

8.15.14

11 months ago

8.15.13

11 months ago

8.15.12

12 months ago

8.13.5

1 year ago

8.15.10

1 year ago

8.15.11

1 year ago

8.13.7

1 year ago

8.13.6

1 year ago

8.13.9

1 year ago

8.13.8

1 year ago

8.14.0

1 year ago

8.14.2

1 year ago

8.14.1

1 year ago

8.14.4

1 year ago

8.14.3

1 year ago

8.14.6

1 year ago

8.14.5

1 year ago

8.14.7

1 year ago

8.15.1

1 year ago

8.15.0

1 year ago

8.15.3

1 year ago

8.15.2

1 year ago

8.15.5

1 year ago

8.15.4

1 year ago

8.15.7

1 year ago

8.15.6

1 year ago

8.15.9

1 year ago

8.15.8

1 year ago

8.13.1

1 year ago

8.13.3

1 year ago

8.13.2

1 year ago

8.13.4

1 year ago

8.11.7

2 years ago

8.11.6

2 years ago

8.11.11

2 years ago

8.11.10

2 years ago

8.11.9

2 years ago

8.11.8

2 years ago

8.12.0

2 years ago

8.12.2

2 years ago

8.12.1

2 years ago

8.13.0

2 years ago

8.11.1

2 years ago

8.11.0

2 years ago

8.11.3

2 years ago

8.11.2

2 years ago

8.11.5

2 years ago

8.11.4

2 years ago

8.10.2

2 years ago

8.10.4

2 years ago

8.10.3

2 years ago

8.8.11

2 years ago

8.8.10

2 years ago

8.8.9

2 years ago

8.8.8

2 years ago

8.10.0

2 years ago

8.10.1

2 years ago

8.9.0

2 years ago

8.9.2

2 years ago

8.9.1

2 years ago

8.6.2

2 years ago

9.0.0-alpha.2

2 years ago

8.6.1

2 years ago

8.7.2

2 years ago

8.7.1

2 years ago

8.7.4

2 years ago

8.7.3

2 years ago

8.7.0

2 years ago

8.8.1

2 years ago

8.8.0

2 years ago

8.8.3

2 years ago

8.8.2

2 years ago

8.8.5

2 years ago

8.8.4

2 years ago

8.8.7

2 years ago

8.8.6

2 years ago

8.2.3

2 years ago

8.2.2

2 years ago

9.0.0-alpha.1

2 years ago

8.6.0

2 years ago

9.0.0-alpha.0

2 years ago

8.1.0

2 years ago

8.3.1

2 years ago

8.0.25

2 years ago

8.0.24

2 years ago

8.0.27

2 years ago

8.0.26

2 years ago

8.2.1

2 years ago

8.2.0

2 years ago

8.4.1

2 years ago

8.4.0

2 years ago

8.3.0

2 years ago

8.5.0

2 years ago

8.5.1

2 years ago

8.0.14

3 years ago

8.0.16

3 years ago

8.0.15

3 years ago

8.0.18

3 years ago

8.0.17

3 years ago

8.0.19

3 years ago

8.0.21

2 years ago

8.0.20

2 years ago

8.0.23

2 years ago

8.0.22

2 years ago

7.0.29

3 years ago

7.0.28

3 years ago

8.0.0-beta.8

3 years ago

8.0.0-beta.7

3 years ago

8.0.0-beta.9

3 years ago

8.0.10

3 years ago

8.0.12

3 years ago

8.0.11

3 years ago

8.0.13

3 years ago

8.0.9

3 years ago

8.0.8

3 years ago

8.0.5

3 years ago

8.0.4

3 years ago

8.0.7

3 years ago

8.0.6

3 years ago

7.0.27

3 years ago

8.0.0-beta.10

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

8.0.3

3 years ago

8.0.2

3 years ago

8.0.0-beta.6

3 years ago

8.0.0-beta.5

3 years ago

8.0.0-beta.2

3 years ago

8.0.0-beta.4

3 years ago

8.0.0-beta.3

3 years ago

8.0.0-beta-05

3 years ago

7.0.25

3 years ago

7.0.26

3 years ago

8.0.0-beta.0

3 years ago

8.0.0-beta.1

3 years ago

8.0.0-beta-01

3 years ago

8.0.0-beta-03

3 years ago

8.0.0-beta-02

3 years ago

7.0.23

3 years ago

7.0.24

3 years ago

8.0.0-beta-04

3 years ago

7.0.21

3 years ago

7.0.22

3 years ago

7.0.20

3 years ago

7.0.18

3 years ago

7.0.19

3 years ago

7.0.16

3 years ago

7.0.17

3 years ago

7.0.15

3 years ago

7.0.12

3 years ago

7.0.13

3 years ago

7.0.10

3 years ago

7.0.11

3 years ago

7.0.14

3 years ago

7.0.8

3 years ago

7.0.9

3 years ago

8.0.0-alpha-09

3 years ago

8.0.0-alpha-08

3 years ago

8.0.0-alpha-06

3 years ago

8.0.0-alpha-07

3 years ago

7.0.7

3 years ago

8.0.0-alpha-04

3 years ago

8.0.0-alpha-05

3 years ago

8.0.0-alpha-02

3 years ago

8.0.0-alpha-03

3 years ago

8.0.0-alpha-01

3 years ago

8.0.0-alpha.0

3 years ago

7.0.6

3 years ago

7.0.5

3 years ago

7.0.4

3 years ago

7.0.3

3 years ago

7.0.2

3 years ago

6.2.3

3 years ago

7.0.0

3 years ago

7.0.1

3 years ago

6.1.0

4 years ago

6.2.1

4 years ago

6.1.2

4 years ago

6.2.0

4 years ago

6.1.1

4 years ago

6.1.4

4 years ago

6.0.21

4 years ago

6.2.2

3 years ago

6.1.3

4 years ago

6.0.20

4 years ago

6.0.19

4 years ago

6.0.18

4 years ago

6.0.17

4 years ago

6.1.5

4 years ago

6.0.16

4 years ago

6.0.15

4 years ago

6.0.14

4 years ago

6.0.13

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.8

4 years ago

6.0.7

4 years ago

6.0.6

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.0-alpha.15

4 years ago

6.0.0-alpha.11

4 years ago

6.0.0-alpha.12

4 years ago

6.0.0-alpha.13

4 years ago

6.0.0-alpha.14

4 years ago

5.3.15

4 years ago