Skip to content

Commit 87644eb

Browse files
authored
docs(links): Add Threejs Journey link to recommended documentation (#2592)
1 parent 17ff08f commit 87644eb

File tree

3 files changed

+116
-66
lines changed

3 files changed

+116
-66
lines changed

docs/getting-started/introduction.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ You need to be versed in both React and `three.js` before rushing into this. If
189189
- [three.js-docs](https://threejs.org/docs)
190190
- [three.js-examples](https://threejs.org/examples)
191191
- [three.js-fundamentals](https://threejs.org/manual/#en/fundamentals)
192+
- [three.js-journey](https://threejs-journey.com)
192193
- [Discover three.js](https://discoverthreejs.com)
193194
- [Do's and don'ts](https://discoverthreejs.com/tips-and-tricks) for performance and best practices
194195
- [react-three-fiber alligator.io tutorial](https://alligator.io/react/react-with-threejs) by [@dghez\_](https://twitter.com/dghez_)

packages/fiber/readme.md

+114-66
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,23 @@
1010

1111
react-three-fiber is a <a href="https://reactjs.org/docs/codebase-overview.html#renderers">React renderer</a> for threejs.
1212

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+
1315
```bash
1416
npm install three @react-three/fiber
1517
```
1618

17-
### Why?
18-
19-
Build your scene declaratively with re-usable, self-contained components that react to state, are readily interactive and can tap into React's ecosystem.
20-
2119
#### Does it have limitations?
2220

23-
None. Everything that works in threejs will work here without exception.
21+
None. Everything that works in Threejs will work here without exception.
2422

25-
#### Can it keep up with frequent updates to threejs?
23+
#### Is it slower than plain Threejs?
2624

27-
Yes, because it merely expresses threejs in JSX: `<mesh />` becomes `new THREE.Mesh()`, and that happens dynamically. There is no hard dependency on a particular threejs version, it does not wrap or duplicate a single threejs class.
25+
No. There is no overhead. Components render outside of React. It outperforms Threejs in scale due to Reacts scheduling abilities.
2826

29-
#### Is it slower than plain threejs?
27+
#### Can it keep up with frequent feature updates to Threejs?
3028

31-
There is no additional overhead. Components participate in the renderloop outside of React.
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.
3230

3331
### What does it look like?
3432

@@ -45,80 +43,127 @@ There is no additional overhead. Components participate in the renderloop outsid
4543
</tbody>
4644
</table>
4745

48-
#### Imports first
49-
5046
```jsx
47+
import { createRoot } from 'react-dom/client'
5148
import React, { useRef, useState } from 'react'
52-
import ReactDOM from 'react-dom'
5349
import { Canvas, useFrame } from '@react-three/fiber'
54-
```
5550

56-
#### Define a component
57-
58-
```jsx
5951
function Box(props) {
60-
// This reference will give us direct access to the mesh
61-
const mesh = useRef()
62-
// Set up state for the hovered and active state
63-
const [hovered, setHover] = useState(false)
64-
const [active, setActive] = useState(false)
65-
// Rotate mesh every frame, this is outside of React without overhead
66-
useFrame(() => (mesh.current.rotation.x += 0.01))
67-
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 += 0.01))
59+
// Return the view, these are regular Threejs elements expressed in JSX
6860
return (
6961
<mesh
7062
{...props}
71-
ref={mesh}
72-
scale={active ? 1.5 : 1}
73-
onClick={(event) => setActive(!active)}
74-
onPointerOver={(event) => setHover(true)}
75-
onPointerOut={(event) => setHover(false)}>
76-
<boxGeometry args={[1, 2, 3]} />
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]} />
7769
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
7870
</mesh>
7971
)
8072
}
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+
)
8182
```
8283

83-
#### Compose the scene
84+
<details>
85+
<summary>Show TypeScript example</summary>
86+
87+
```bash
88+
npm install @types/three
89+
```
8490

85-
Either use `Canvas`, which you can think of as a portal to threejs inside your regular dom graph. Everything within it is a [native threejs element](https://threejs.org/docs). If you want to mix Webgl and Html (react-dom) this is what you should use.
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'
8696

87-
```jsx
88-
ReactDOM.render(
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 += 0.01))
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(
89117
<Canvas>
90118
<ambientLight />
91119
<pointLight position={[10, 10, 10]} />
92120
<Box position={[-1.2, 0, 0]} />
93121
<Box position={[1.2, 0, 0]} />
94122
</Canvas>,
95-
document.getElementById('root'),
96123
)
97124
```
98125

99-
Or use react-three-fibers own `render` function, which is a little more low-level but could save you the extra cost of carrying react-dom. It renders into a dom `canvas` element. Use this for Webgl-only apps.
126+
Live demo: https://codesandbox.io/s/icy-tree-brnsm?file=/src/App.tsx
100127

101-
```jsx
102-
import { render } from '@react-three/fiber'
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.
103134

104-
render(<Scene />, document.querySelector('canvas'))
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@beta react@rc
143+
# Start
144+
expo start
105145
```
106146

107-
<details>
108-
<summary>Show TypeScript example</summary>
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`:
109148

110-
```tsx
111-
import { Canvas, MeshProps, useFrame } from '@react-three/fiber'
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+
```
112158

113-
const Box: React.FC<MeshProps> = (props) => {
114-
// This reference will give us direct access to the mesh
115-
const mesh = useRef<THREE.Mesh>(null!)
116-
// Set up state for the hovered and active state
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)
117164
const [hovered, setHover] = useState(false)
118165
const [active, setActive] = useState(false)
119-
// Rotate mesh every frame, this is outside of React without overhead
120-
useFrame(() => (mesh.current.rotation.x += 0.01))
121-
166+
useFrame((state, delta) => (mesh.current.rotation.x += 0.01))
122167
return (
123168
<mesh
124169
{...props}
@@ -127,32 +172,32 @@ const Box: React.FC<MeshProps> = (props) => {
127172
onClick={(event) => setActive(!active)}
128173
onPointerOver={(event) => setHover(true)}
129174
onPointerOut={(event) => setHover(false)}>
130-
<boxGeometry args={[1, 2, 3]} />
175+
<boxGeometry args={[1, 1, 1]} />
131176
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
132177
</mesh>
133178
)
134179
}
135-
136-
ReactDOM.render(
137-
<Canvas>
138-
<ambientLight />
139-
<pointLight position={[10, 10, 10]} />
140-
<Box position={[-1.2, 0, 0]} />
141-
<Box position={[1.2, 0, 0]} />
142-
</Canvas>,
143-
document.getElementById('root'),
144-
)
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+
}
145190
```
146191

147192
</details>
148193

149194
---
150195

151-
# Documentation
196+
# Documentation, tutorials, examples
197+
198+
Visit [docs.pmnd.rs](https://docs.pmnd.rs/react-three-fiber)
152199

153-
- [api.md](/markdown/api.md)
154-
- [pitfalls.md](/markdown/pitfalls.md)
155-
- [testing.md](/packages/test-renderer)
200+
<a href="https://docs.pmnd.rs/react-three-fiber"><img src="/docs/preview.jpg"></a>
156201

157202
# Fundamentals
158203

@@ -161,13 +206,14 @@ You need to be versed in both React and Threejs before rushing into this. If you
161206
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.
162207
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).
163208
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.
164-
4. Try changing some values, scroll though our [Api](/markdown/api.md) to see what the various settings and hooks do.
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.
165210

166211
Some reading material:
167212

168213
- [Threejs-docs](https://threejs.org/docs)
169214
- [Threejs-examples](https://threejs.org/examples)
170215
- [Threejs-fundamentals](https://threejs.org/manual/#en/fundamentals)
216+
- [three.js-journey](https://threejs-journey.com)
171217
- [Discover Threejs](https://discoverthreejs.com)
172218
- [Do's and don'ts](https://discoverthreejs.com/tips-and-tricks) for performance and best practices
173219
- [react-three-fiber alligator.io tutorial](https://alligator.io/react/react-with-threejs) by [@dghez\_](https://twitter.com/dghez_)
@@ -180,9 +226,11 @@ Some reading material:
180226
- [`@react-three/flex`](https://github.com/pmndrs/react-three-flex) &ndash; flexbox for react-three-fiber
181227
- [`@react-three/xr`](https://github.com/pmndrs/react-xr) &ndash; VR/AR controllers and events
182228
- [`@react-three/cannon`](https://github.com/pmndrs/use-cannon) &ndash; physics based hooks
229+
- [`@react-three/a11y`](https://github.com/pmndrs/react-three-a11y) &ndash; real a11y for your scene
183230
- [`zustand`](https://github.com/pmndrs/zustand) &ndash; state management
184231
- [`react-spring`](https://github.com/pmndrs/react-spring) &ndash; a spring-physics-based animation library
185232
- [`react-use-gesture`](https://github.com/pmndrs/react-use-gesture) &ndash; mouse/touch gestures
233+
- [`leva`](https://github.com/pmndrs/leva) &ndash; create GUI controls in seconds
186234

187235
# How to contribute
188236

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Some reading material:
213213
- [Threejs-docs](https://threejs.org/docs)
214214
- [Threejs-examples](https://threejs.org/examples)
215215
- [Threejs-fundamentals](https://threejs.org/manual/#en/fundamentals)
216+
- [three.js-journey](https://threejs-journey.com)
216217
- [Discover Threejs](https://discoverthreejs.com)
217218
- [Do's and don'ts](https://discoverthreejs.com/tips-and-tricks) for performance and best practices
218219
- [react-three-fiber alligator.io tutorial](https://alligator.io/react/react-with-threejs) by [@dghez\_](https://twitter.com/dghez_)

0 commit comments

Comments
 (0)