@react-three/fiber 9.2.0 → 9.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1168 -1160
- package/dist/declarations/src/core/events.d.ts +92 -92
- package/dist/declarations/src/core/hooks.d.ts +53 -53
- package/dist/declarations/src/core/index.d.ts +13 -13
- package/dist/declarations/src/core/loop.d.ts +31 -31
- package/dist/declarations/src/core/reconciler.d.ts +50 -50
- package/dist/declarations/src/core/renderer.d.ts +89 -89
- package/dist/declarations/src/core/store.d.ts +130 -130
- package/dist/declarations/src/core/utils.d.ts +191 -191
- package/dist/declarations/src/index.d.ts +6 -6
- package/dist/declarations/src/native/Canvas.d.ts +13 -13
- package/dist/declarations/src/native/events.d.ts +4 -4
- package/dist/declarations/src/native.d.ts +6 -6
- package/dist/declarations/src/three-types.d.ts +68 -68
- package/dist/declarations/src/web/Canvas.d.ts +23 -23
- package/dist/declarations/src/web/events.d.ts +4 -4
- package/dist/{events-e63d28a3.cjs.dev.js → events-d0449b76.cjs.dev.js} +91 -91
- package/dist/{events-59fae97e.cjs.prod.js → events-d0a20a9c.cjs.prod.js} +91 -91
- package/dist/{events-cf57b220.esm.js → events-e3cb66e2.esm.js} +90 -90
- package/dist/react-three-fiber.cjs.dev.js +5 -5
- package/dist/react-three-fiber.cjs.prod.js +5 -5
- package/dist/react-three-fiber.esm.js +6 -6
- package/native/dist/react-three-fiber-native.cjs.dev.js +8 -6
- package/native/dist/react-three-fiber-native.cjs.prod.js +8 -6
- package/native/dist/react-three-fiber-native.esm.js +9 -7
- package/native/package.json +5 -5
- package/package.json +2 -2
- package/readme.md +253 -253
package/readme.md
CHANGED
|
@@ -1,253 +1,253 @@
|
|
|
1
|
-
<h1>react-three-fiber</h1>
|
|
2
|
-
|
|
3
|
-
[](https://npmjs.com/package/@react-three/fiber)
|
|
4
|
-
[](https://npmjs.com/package/@react-three/fiber)
|
|
5
|
-
[](https://twitter.com/pmndrs)
|
|
6
|
-
[](https://discord.gg/ZZjjNvJ)
|
|
7
|
-
[](https://opencollective.com/react-three-fiber)
|
|
8
|
-
[](https://blockchain.com/eth/address/0x6E3f79Ea1d0dcedeb33D3fC6c34d2B1f156F2682)
|
|
9
|
-
[](https://blockchain.com/btc/address/36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH)
|
|
10
|
-
|
|
11
|
-
react-three-fiber is a <a href="https://reactjs.org/docs/codebase-overview.html#renderers">React renderer</a> for threejs.
|
|
12
|
-
|
|
13
|
-
Build your scene declaratively with re-usable, self-contained components that react to state, are readily interactive and can participate in React's ecosystem.
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install three @react-three/fiber
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
#### Does it have limitations?
|
|
20
|
-
|
|
21
|
-
None. Everything that works in Threejs will work here without exception.
|
|
22
|
-
|
|
23
|
-
#### Is it slower than plain Threejs?
|
|
24
|
-
|
|
25
|
-
No. There is no overhead. Components render outside of React. It outperforms Threejs in scale due to React's scheduling abilities.
|
|
26
|
-
|
|
27
|
-
#### Can it keep up with frequent feature updates to Threejs?
|
|
28
|
-
|
|
29
|
-
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.
|
|
30
|
-
|
|
31
|
-
### What does it look like?
|
|
32
|
-
|
|
33
|
-
<table>
|
|
34
|
-
<tbody>
|
|
35
|
-
<tr>
|
|
36
|
-
<td>Let's make a re-usable component that has its own state, reacts to user-input and participates in the render-loop. (<a href="https://codesandbox.io/s/rrppl0y8l4?file=/src/App.js">live demo</a>).</td>
|
|
37
|
-
<td>
|
|
38
|
-
<a href="https://codesandbox.io/s/rrppl0y8l4">
|
|
39
|
-
<img src="https://i.imgur.com/sS4ArrZ.gif" />
|
|
40
|
-
</a>
|
|
41
|
-
</td>
|
|
42
|
-
</tr>
|
|
43
|
-
</tbody>
|
|
44
|
-
</table>
|
|
45
|
-
|
|
46
|
-
```jsx
|
|
47
|
-
import { createRoot } from 'react-dom/client'
|
|
48
|
-
import React, { useRef, useState } from 'react'
|
|
49
|
-
import { Canvas, useFrame } from '@react-three/fiber'
|
|
50
|
-
|
|
51
|
-
function Box(props) {
|
|
52
|
-
// This reference gives us direct access to the THREE.Mesh object
|
|
53
|
-
const ref = useRef()
|
|
54
|
-
// Hold state for hovered and clicked events
|
|
55
|
-
const [hovered, hover] = useState(false)
|
|
56
|
-
const [clicked, click] = useState(false)
|
|
57
|
-
// Subscribe this component to the render-loop, rotate the mesh every frame
|
|
58
|
-
useFrame((state, delta) => (ref.current.rotation.x += delta))
|
|
59
|
-
// Return the view, these are regular Threejs elements expressed in JSX
|
|
60
|
-
return (
|
|
61
|
-
<mesh
|
|
62
|
-
{...props}
|
|
63
|
-
ref={ref}
|
|
64
|
-
scale={clicked ? 1.5 : 1}
|
|
65
|
-
onClick={(event) => click(!clicked)}
|
|
66
|
-
onPointerOver={(event) => hover(true)}
|
|
67
|
-
onPointerOut={(event) => hover(false)}>
|
|
68
|
-
<boxGeometry args={[1, 1, 1]} />
|
|
69
|
-
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
|
|
70
|
-
</mesh>
|
|
71
|
-
)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
createRoot(document.getElementById('root')).render(
|
|
75
|
-
<Canvas>
|
|
76
|
-
<ambientLight />
|
|
77
|
-
<pointLight position={[10, 10, 10]} />
|
|
78
|
-
<Box position={[-1.2, 0, 0]} />
|
|
79
|
-
<Box position={[1.2, 0, 0]} />
|
|
80
|
-
</Canvas>,
|
|
81
|
-
)
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
<details>
|
|
85
|
-
<summary>Show TypeScript example</summary>
|
|
86
|
-
|
|
87
|
-
```bash
|
|
88
|
-
npm install @types/three
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
```tsx
|
|
92
|
-
import * as THREE from 'three'
|
|
93
|
-
import { createRoot } from 'react-dom/client'
|
|
94
|
-
import React, { useRef, useState } from 'react'
|
|
95
|
-
import { Canvas, useFrame, ThreeElements } from '@react-three/fiber'
|
|
96
|
-
|
|
97
|
-
function Box(props: ThreeElements['mesh']) {
|
|
98
|
-
const ref = useRef<THREE.Mesh>(null!)
|
|
99
|
-
const [hovered, hover] = useState(false)
|
|
100
|
-
const [clicked, click] = useState(false)
|
|
101
|
-
useFrame((state, delta) => (ref.current.rotation.x += delta))
|
|
102
|
-
return (
|
|
103
|
-
<mesh
|
|
104
|
-
{...props}
|
|
105
|
-
ref={ref}
|
|
106
|
-
scale={clicked ? 1.5 : 1}
|
|
107
|
-
onClick={(event) => click(!clicked)}
|
|
108
|
-
onPointerOver={(event) => hover(true)}
|
|
109
|
-
onPointerOut={(event) => hover(false)}>
|
|
110
|
-
<boxGeometry args={[1, 1, 1]} />
|
|
111
|
-
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
|
|
112
|
-
</mesh>
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
createRoot(document.getElementById('root') as HTMLElement).render(
|
|
117
|
-
<Canvas>
|
|
118
|
-
<ambientLight />
|
|
119
|
-
<pointLight position={[10, 10, 10]} />
|
|
120
|
-
<Box position={[-1.2, 0, 0]} />
|
|
121
|
-
<Box position={[1.2, 0, 0]} />
|
|
122
|
-
</Canvas>,
|
|
123
|
-
)
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
Live demo: https://codesandbox.io/s/icy-tree-brnsm?file=/src/App.tsx
|
|
127
|
-
|
|
128
|
-
</details>
|
|
129
|
-
|
|
130
|
-
<details>
|
|
131
|
-
<summary>Show React Native example</summary>
|
|
132
|
-
|
|
133
|
-
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.
|
|
134
|
-
|
|
135
|
-
```bash
|
|
136
|
-
# Install expo-cli, this will create our app
|
|
137
|
-
npm install expo-cli -g
|
|
138
|
-
# Create app and cd into it
|
|
139
|
-
expo init my-app
|
|
140
|
-
cd my-app
|
|
141
|
-
# Install dependencies
|
|
142
|
-
npm install three @react-three/fiber react
|
|
143
|
-
# Start
|
|
144
|
-
expo start
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
Some configuration may be required to tell the Metro bundler about your assets if you use `useLoader` or Drei abstractions like `useGLTF` and `useTexture`:
|
|
148
|
-
|
|
149
|
-
```js
|
|
150
|
-
// metro.config.js
|
|
151
|
-
module.exports = {
|
|
152
|
-
resolver: {
|
|
153
|
-
sourceExts: ['js', 'jsx', 'json', 'ts', 'tsx', 'cjs'],
|
|
154
|
-
assetExts: ['glb', 'png', 'jpg'],
|
|
155
|
-
},
|
|
156
|
-
}
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
```tsx
|
|
160
|
-
import React, { useRef, useState } from 'react'
|
|
161
|
-
import { Canvas, useFrame } from '@react-three/fiber/native'
|
|
162
|
-
function Box(props) {
|
|
163
|
-
const mesh = useRef(null)
|
|
164
|
-
const [hovered, setHover] = useState(false)
|
|
165
|
-
const [active, setActive] = useState(false)
|
|
166
|
-
useFrame((state, delta) => (mesh.current.rotation.x += delta))
|
|
167
|
-
return (
|
|
168
|
-
<mesh
|
|
169
|
-
{...props}
|
|
170
|
-
ref={mesh}
|
|
171
|
-
scale={active ? 1.5 : 1}
|
|
172
|
-
onClick={(event) => setActive(!active)}
|
|
173
|
-
onPointerOver={(event) => setHover(true)}
|
|
174
|
-
onPointerOut={(event) => setHover(false)}>
|
|
175
|
-
<boxGeometry args={[1, 1, 1]} />
|
|
176
|
-
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
|
|
177
|
-
</mesh>
|
|
178
|
-
)
|
|
179
|
-
}
|
|
180
|
-
export default function App() {
|
|
181
|
-
return (
|
|
182
|
-
<Canvas>
|
|
183
|
-
<ambientLight />
|
|
184
|
-
<pointLight position={[10, 10, 10]} />
|
|
185
|
-
<Box position={[-1.2, 0, 0]} />
|
|
186
|
-
<Box position={[1.2, 0, 0]} />
|
|
187
|
-
</Canvas>
|
|
188
|
-
)
|
|
189
|
-
}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
</details>
|
|
193
|
-
|
|
194
|
-
---
|
|
195
|
-
|
|
196
|
-
# Documentation, tutorials, examples
|
|
197
|
-
|
|
198
|
-
Visit [docs.pmnd.rs](https://docs.pmnd.rs/react-three-fiber)
|
|
199
|
-
|
|
200
|
-
<a href="https://docs.pmnd.rs/react-three-fiber"><img src="/docs/preview.jpg"></a>
|
|
201
|
-
|
|
202
|
-
# Fundamentals
|
|
203
|
-
|
|
204
|
-
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](https://react.dev/learn), especially [the section about hooks](https://react.dev/reference/react). As for Threejs, make sure you at least glance over the following links:
|
|
205
|
-
|
|
206
|
-
1. Make sure you have a [basic grasp of Threejs](https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene). Keep that site open.
|
|
207
|
-
2. When you know what a scene is, a camera, mesh, geometry, material, fork the [demo above](https://github.com/pmndrs/react-three-fiber#what-does-it-look-like).
|
|
208
|
-
3. [Look up](https://threejs.org/docs/index.html#api/en/objects/Mesh) the JSX elements that you see (mesh, ambientLight, etc), _all_ threejs exports are native to three-fiber.
|
|
209
|
-
4. Try changing some values, scroll through our [API](https://docs.pmnd.rs/react-three-fiber/API) to see what the various settings and hooks do.
|
|
210
|
-
|
|
211
|
-
Some reading material:
|
|
212
|
-
|
|
213
|
-
- [Threejs-docs](https://threejs.org/docs)
|
|
214
|
-
- [Threejs-examples](https://threejs.org/examples)
|
|
215
|
-
- [Threejs-fundamentals](https://threejs.org/manual/#en/fundamentals)
|
|
216
|
-
- [three.js-journey](https://threejs-journey.com)
|
|
217
|
-
- [Discover Threejs](https://discoverthreejs.com)
|
|
218
|
-
- [Do's and don'ts](https://discoverthreejs.com/tips-and-tricks) for performance and best practices
|
|
219
|
-
- [react-three-fiber alligator.io tutorial](https://alligator.io/react/react-with-threejs) by [@dghez\_](https://twitter.com/dghez_)
|
|
220
|
-
|
|
221
|
-
# Ecosystem
|
|
222
|
-
|
|
223
|
-
- [`@react-three/gltfjsx`](https://github.com/pmndrs/gltfjsx) – turns GLTFs into JSX components
|
|
224
|
-
- [`@react-three/drei`](https://github.com/pmndrs/drei) – useful helpers for react-three-fiber
|
|
225
|
-
- [`@react-three/postprocessing`](https://github.com/pmndrs/react-postprocessing) – post-processing effects
|
|
226
|
-
- [`@react-three/flex`](https://github.com/pmndrs/react-three-flex) – flexbox for react-three-fiber
|
|
227
|
-
- [`@react-three/xr`](https://github.com/pmndrs/react-xr) – VR/AR controllers and events
|
|
228
|
-
- [`@react-three/cannon`](https://github.com/pmndrs/use-cannon) – physics based hooks
|
|
229
|
-
- [`@react-three/a11y`](https://github.com/pmndrs/react-three-a11y) – real a11y for your scene
|
|
230
|
-
- [`zustand`](https://github.com/pmndrs/zustand) – state management
|
|
231
|
-
- [`react-spring`](https://github.com/pmndrs/react-spring) – a spring-physics-based animation library
|
|
232
|
-
- [`react-use-gesture`](https://github.com/pmndrs/react-use-gesture) – mouse/touch gestures
|
|
233
|
-
- [`leva`](https://github.com/pmndrs/leva) – create GUI controls in seconds
|
|
234
|
-
|
|
235
|
-
# How to contribute
|
|
236
|
-
|
|
237
|
-
If you like this project, please consider helping out. All contributions are welcome as well as donations to [Opencollective](https://opencollective.com/react-three-fiber), or in crypto `BTC: 36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH`, `ETH: 0x6E3f79Ea1d0dcedeb33D3fC6c34d2B1f156F2682`.
|
|
238
|
-
|
|
239
|
-
#### Backers
|
|
240
|
-
|
|
241
|
-
Thank you to all our backers! 🙏
|
|
242
|
-
|
|
243
|
-
<a href="https://opencollective.com/react-three-fiber#backers" target="_blank">
|
|
244
|
-
<img src="https://opencollective.com/react-three-fiber/backers.svg?width=890"/>
|
|
245
|
-
</a>
|
|
246
|
-
|
|
247
|
-
#### Contributors
|
|
248
|
-
|
|
249
|
-
This project exists thanks to all the people who contribute.
|
|
250
|
-
|
|
251
|
-
<a href="https://github.com/pmndrs/react-three-fiber/graphs/contributors">
|
|
252
|
-
<img src="https://opencollective.com/react-three-fiber/contributors.svg?width=890" />
|
|
253
|
-
</a>
|
|
1
|
+
<h1>react-three-fiber</h1>
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/@react-three/fiber)
|
|
4
|
+
[](https://npmjs.com/package/@react-three/fiber)
|
|
5
|
+
[](https://twitter.com/pmndrs)
|
|
6
|
+
[](https://discord.gg/ZZjjNvJ)
|
|
7
|
+
[](https://opencollective.com/react-three-fiber)
|
|
8
|
+
[](https://blockchain.com/eth/address/0x6E3f79Ea1d0dcedeb33D3fC6c34d2B1f156F2682)
|
|
9
|
+
[](https://blockchain.com/btc/address/36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH)
|
|
10
|
+
|
|
11
|
+
react-three-fiber is a <a href="https://reactjs.org/docs/codebase-overview.html#renderers">React renderer</a> for threejs.
|
|
12
|
+
|
|
13
|
+
Build your scene declaratively with re-usable, self-contained components that react to state, are readily interactive and can participate in React's ecosystem.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install three @react-three/fiber
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
#### Does it have limitations?
|
|
20
|
+
|
|
21
|
+
None. Everything that works in Threejs will work here without exception.
|
|
22
|
+
|
|
23
|
+
#### Is it slower than plain Threejs?
|
|
24
|
+
|
|
25
|
+
No. There is no overhead. Components render outside of React. It outperforms Threejs in scale due to React's scheduling abilities.
|
|
26
|
+
|
|
27
|
+
#### Can it keep up with frequent feature updates to Threejs?
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
### What does it look like?
|
|
32
|
+
|
|
33
|
+
<table>
|
|
34
|
+
<tbody>
|
|
35
|
+
<tr>
|
|
36
|
+
<td>Let's make a re-usable component that has its own state, reacts to user-input and participates in the render-loop. (<a href="https://codesandbox.io/s/rrppl0y8l4?file=/src/App.js">live demo</a>).</td>
|
|
37
|
+
<td>
|
|
38
|
+
<a href="https://codesandbox.io/s/rrppl0y8l4">
|
|
39
|
+
<img src="https://i.imgur.com/sS4ArrZ.gif" />
|
|
40
|
+
</a>
|
|
41
|
+
</td>
|
|
42
|
+
</tr>
|
|
43
|
+
</tbody>
|
|
44
|
+
</table>
|
|
45
|
+
|
|
46
|
+
```jsx
|
|
47
|
+
import { createRoot } from 'react-dom/client'
|
|
48
|
+
import React, { useRef, useState } from 'react'
|
|
49
|
+
import { Canvas, useFrame } from '@react-three/fiber'
|
|
50
|
+
|
|
51
|
+
function Box(props) {
|
|
52
|
+
// This reference gives us direct access to the THREE.Mesh object
|
|
53
|
+
const ref = useRef()
|
|
54
|
+
// Hold state for hovered and clicked events
|
|
55
|
+
const [hovered, hover] = useState(false)
|
|
56
|
+
const [clicked, click] = useState(false)
|
|
57
|
+
// Subscribe this component to the render-loop, rotate the mesh every frame
|
|
58
|
+
useFrame((state, delta) => (ref.current.rotation.x += delta))
|
|
59
|
+
// Return the view, these are regular Threejs elements expressed in JSX
|
|
60
|
+
return (
|
|
61
|
+
<mesh
|
|
62
|
+
{...props}
|
|
63
|
+
ref={ref}
|
|
64
|
+
scale={clicked ? 1.5 : 1}
|
|
65
|
+
onClick={(event) => click(!clicked)}
|
|
66
|
+
onPointerOver={(event) => hover(true)}
|
|
67
|
+
onPointerOut={(event) => hover(false)}>
|
|
68
|
+
<boxGeometry args={[1, 1, 1]} />
|
|
69
|
+
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
|
|
70
|
+
</mesh>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
createRoot(document.getElementById('root')).render(
|
|
75
|
+
<Canvas>
|
|
76
|
+
<ambientLight />
|
|
77
|
+
<pointLight position={[10, 10, 10]} />
|
|
78
|
+
<Box position={[-1.2, 0, 0]} />
|
|
79
|
+
<Box position={[1.2, 0, 0]} />
|
|
80
|
+
</Canvas>,
|
|
81
|
+
)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
<details>
|
|
85
|
+
<summary>Show TypeScript example</summary>
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm install @types/three
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import * as THREE from 'three'
|
|
93
|
+
import { createRoot } from 'react-dom/client'
|
|
94
|
+
import React, { useRef, useState } from 'react'
|
|
95
|
+
import { Canvas, useFrame, ThreeElements } from '@react-three/fiber'
|
|
96
|
+
|
|
97
|
+
function Box(props: ThreeElements['mesh']) {
|
|
98
|
+
const ref = useRef<THREE.Mesh>(null!)
|
|
99
|
+
const [hovered, hover] = useState(false)
|
|
100
|
+
const [clicked, click] = useState(false)
|
|
101
|
+
useFrame((state, delta) => (ref.current.rotation.x += delta))
|
|
102
|
+
return (
|
|
103
|
+
<mesh
|
|
104
|
+
{...props}
|
|
105
|
+
ref={ref}
|
|
106
|
+
scale={clicked ? 1.5 : 1}
|
|
107
|
+
onClick={(event) => click(!clicked)}
|
|
108
|
+
onPointerOver={(event) => hover(true)}
|
|
109
|
+
onPointerOut={(event) => hover(false)}>
|
|
110
|
+
<boxGeometry args={[1, 1, 1]} />
|
|
111
|
+
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
|
|
112
|
+
</mesh>
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
createRoot(document.getElementById('root') as HTMLElement).render(
|
|
117
|
+
<Canvas>
|
|
118
|
+
<ambientLight />
|
|
119
|
+
<pointLight position={[10, 10, 10]} />
|
|
120
|
+
<Box position={[-1.2, 0, 0]} />
|
|
121
|
+
<Box position={[1.2, 0, 0]} />
|
|
122
|
+
</Canvas>,
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Live demo: https://codesandbox.io/s/icy-tree-brnsm?file=/src/App.tsx
|
|
127
|
+
|
|
128
|
+
</details>
|
|
129
|
+
|
|
130
|
+
<details>
|
|
131
|
+
<summary>Show React Native example</summary>
|
|
132
|
+
|
|
133
|
+
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.
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
# Install expo-cli, this will create our app
|
|
137
|
+
npm install expo-cli -g
|
|
138
|
+
# Create app and cd into it
|
|
139
|
+
expo init my-app
|
|
140
|
+
cd my-app
|
|
141
|
+
# Install dependencies
|
|
142
|
+
npm install three @react-three/fiber react
|
|
143
|
+
# Start
|
|
144
|
+
expo start
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Some configuration may be required to tell the Metro bundler about your assets if you use `useLoader` or Drei abstractions like `useGLTF` and `useTexture`:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
// metro.config.js
|
|
151
|
+
module.exports = {
|
|
152
|
+
resolver: {
|
|
153
|
+
sourceExts: ['js', 'jsx', 'json', 'ts', 'tsx', 'cjs'],
|
|
154
|
+
assetExts: ['glb', 'png', 'jpg'],
|
|
155
|
+
},
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```tsx
|
|
160
|
+
import React, { useRef, useState } from 'react'
|
|
161
|
+
import { Canvas, useFrame } from '@react-three/fiber/native'
|
|
162
|
+
function Box(props) {
|
|
163
|
+
const mesh = useRef(null)
|
|
164
|
+
const [hovered, setHover] = useState(false)
|
|
165
|
+
const [active, setActive] = useState(false)
|
|
166
|
+
useFrame((state, delta) => (mesh.current.rotation.x += delta))
|
|
167
|
+
return (
|
|
168
|
+
<mesh
|
|
169
|
+
{...props}
|
|
170
|
+
ref={mesh}
|
|
171
|
+
scale={active ? 1.5 : 1}
|
|
172
|
+
onClick={(event) => setActive(!active)}
|
|
173
|
+
onPointerOver={(event) => setHover(true)}
|
|
174
|
+
onPointerOut={(event) => setHover(false)}>
|
|
175
|
+
<boxGeometry args={[1, 1, 1]} />
|
|
176
|
+
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
|
|
177
|
+
</mesh>
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
export default function App() {
|
|
181
|
+
return (
|
|
182
|
+
<Canvas>
|
|
183
|
+
<ambientLight />
|
|
184
|
+
<pointLight position={[10, 10, 10]} />
|
|
185
|
+
<Box position={[-1.2, 0, 0]} />
|
|
186
|
+
<Box position={[1.2, 0, 0]} />
|
|
187
|
+
</Canvas>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
</details>
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
# Documentation, tutorials, examples
|
|
197
|
+
|
|
198
|
+
Visit [docs.pmnd.rs](https://docs.pmnd.rs/react-three-fiber)
|
|
199
|
+
|
|
200
|
+
<a href="https://docs.pmnd.rs/react-three-fiber"><img src="/docs/preview.jpg"></a>
|
|
201
|
+
|
|
202
|
+
# Fundamentals
|
|
203
|
+
|
|
204
|
+
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](https://react.dev/learn), especially [the section about hooks](https://react.dev/reference/react). As for Threejs, make sure you at least glance over the following links:
|
|
205
|
+
|
|
206
|
+
1. Make sure you have a [basic grasp of Threejs](https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene). Keep that site open.
|
|
207
|
+
2. When you know what a scene is, a camera, mesh, geometry, material, fork the [demo above](https://github.com/pmndrs/react-three-fiber#what-does-it-look-like).
|
|
208
|
+
3. [Look up](https://threejs.org/docs/index.html#api/en/objects/Mesh) the JSX elements that you see (mesh, ambientLight, etc), _all_ threejs exports are native to three-fiber.
|
|
209
|
+
4. Try changing some values, scroll through our [API](https://docs.pmnd.rs/react-three-fiber/API) to see what the various settings and hooks do.
|
|
210
|
+
|
|
211
|
+
Some reading material:
|
|
212
|
+
|
|
213
|
+
- [Threejs-docs](https://threejs.org/docs)
|
|
214
|
+
- [Threejs-examples](https://threejs.org/examples)
|
|
215
|
+
- [Threejs-fundamentals](https://threejs.org/manual/#en/fundamentals)
|
|
216
|
+
- [three.js-journey](https://threejs-journey.com)
|
|
217
|
+
- [Discover Threejs](https://discoverthreejs.com)
|
|
218
|
+
- [Do's and don'ts](https://discoverthreejs.com/tips-and-tricks) for performance and best practices
|
|
219
|
+
- [react-three-fiber alligator.io tutorial](https://alligator.io/react/react-with-threejs) by [@dghez\_](https://twitter.com/dghez_)
|
|
220
|
+
|
|
221
|
+
# Ecosystem
|
|
222
|
+
|
|
223
|
+
- [`@react-three/gltfjsx`](https://github.com/pmndrs/gltfjsx) – turns GLTFs into JSX components
|
|
224
|
+
- [`@react-three/drei`](https://github.com/pmndrs/drei) – useful helpers for react-three-fiber
|
|
225
|
+
- [`@react-three/postprocessing`](https://github.com/pmndrs/react-postprocessing) – post-processing effects
|
|
226
|
+
- [`@react-three/flex`](https://github.com/pmndrs/react-three-flex) – flexbox for react-three-fiber
|
|
227
|
+
- [`@react-three/xr`](https://github.com/pmndrs/react-xr) – VR/AR controllers and events
|
|
228
|
+
- [`@react-three/cannon`](https://github.com/pmndrs/use-cannon) – physics based hooks
|
|
229
|
+
- [`@react-three/a11y`](https://github.com/pmndrs/react-three-a11y) – real a11y for your scene
|
|
230
|
+
- [`zustand`](https://github.com/pmndrs/zustand) – state management
|
|
231
|
+
- [`react-spring`](https://github.com/pmndrs/react-spring) – a spring-physics-based animation library
|
|
232
|
+
- [`react-use-gesture`](https://github.com/pmndrs/react-use-gesture) – mouse/touch gestures
|
|
233
|
+
- [`leva`](https://github.com/pmndrs/leva) – create GUI controls in seconds
|
|
234
|
+
|
|
235
|
+
# How to contribute
|
|
236
|
+
|
|
237
|
+
If you like this project, please consider helping out. All contributions are welcome as well as donations to [Opencollective](https://opencollective.com/react-three-fiber), or in crypto `BTC: 36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH`, `ETH: 0x6E3f79Ea1d0dcedeb33D3fC6c34d2B1f156F2682`.
|
|
238
|
+
|
|
239
|
+
#### Backers
|
|
240
|
+
|
|
241
|
+
Thank you to all our backers! 🙏
|
|
242
|
+
|
|
243
|
+
<a href="https://opencollective.com/react-three-fiber#backers" target="_blank">
|
|
244
|
+
<img src="https://opencollective.com/react-three-fiber/backers.svg?width=890"/>
|
|
245
|
+
</a>
|
|
246
|
+
|
|
247
|
+
#### Contributors
|
|
248
|
+
|
|
249
|
+
This project exists thanks to all the people who contribute.
|
|
250
|
+
|
|
251
|
+
<a href="https://github.com/pmndrs/react-three-fiber/graphs/contributors">
|
|
252
|
+
<img src="https://opencollective.com/react-three-fiber/contributors.svg?width=890" />
|
|
253
|
+
</a>
|