Skip to content

Commit

Permalink
finish migration story, last bugfixes and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Oct 2, 2019
1 parent e9b5447 commit f87a110
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 20 deletions.
32 changes: 16 additions & 16 deletions .size-snapshot.json
Expand Up @@ -19,41 +19,41 @@
"gzipped": 5244
},
"dist/web.js": {
"bundled": 34344,
"minified": 16342,
"gzipped": 5951,
"bundled": 33766,
"minified": 16023,
"gzipped": 5880,
"treeshaked": {
"rollup": {
"code": 12287,
"code": 12149,
"import_statements": 800
},
"webpack": {
"code": 14235
"code": 14058
}
}
},
"dist/web.cjs.js": {
"bundled": 41607,
"minified": 19189,
"gzipped": 6389
"bundled": 41043,
"minified": 18865,
"gzipped": 6338
},
"dist/native.js": {
"bundled": 35845,
"minified": 17065,
"gzipped": 6147,
"bundled": 35413,
"minified": 16866,
"gzipped": 6109,
"treeshaked": {
"rollup": {
"code": 13053,
"code": 13029,
"import_statements": 898
},
"webpack": {
"code": 14931
"code": 14884
}
}
},
"dist/native.cjs.js": {
"bundled": 44407,
"minified": 20333,
"gzipped": 6696
"bundled": 43973,
"minified": 20129,
"gzipped": 6674
}
}
26 changes: 26 additions & 0 deletions examples/components/Gestures.js
@@ -0,0 +1,26 @@
import React from 'react'
import { Canvas, useThree } from 'react-three-fiber'
import { useDrag } from 'react-use-gesture'
import { useSpring, a } from 'react-spring/three'

function Obj() {
const { size, viewport } = useThree()
const aspect = size.width / viewport.width
const [spring, set] = useSpring(() => ({ position: [0, 0, 0], config: { mass: 4, friction: 50, tension: 1500 } }))
const bind = useDrag(({ offset: [x, y] }) => set({ position: [x / aspect, -y / aspect, 0] }), { pointerEvents: true })

return (
<a.mesh {...spring} {...bind()}>
<dodecahedronBufferGeometry attach="geometry" />
<meshNormalMaterial attach="material" />
</a.mesh>
)
}

export default function App() {
return (
<Canvas>
<Obj />
</Canvas>
)
}
2 changes: 1 addition & 1 deletion examples/index.js
@@ -1,6 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './components/Stars'
import App from './components/Gestures'
import './styles.css'

//ReactDOM.unstable_createRoot(document.getElementById('root')).render(<App />)
Expand Down
3 changes: 3 additions & 0 deletions examples/package.json
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"cannon": "^0.6.2",
"d3-ease": "^1.0.5",
"fast-safe-stringify": "^2.0.7",
"pepjs": "^0.5.2",
"postprocessing": "^6.7.0",
"react": "^16.9.0",
Expand All @@ -21,6 +22,8 @@
"react-dom": "^16.9.0",
"react-promise-suspense": "^0.3.1",
"react-spring": "8.0.27",
"react-use-gesture": "6.0.2-beta.1",
"shree": "^0.1.14",
"three": "^0.108.0",
"three.meshline": "^1.2.0",
"vec-la": "^1.5.0",
Expand Down
22 changes: 22 additions & 0 deletions examples/yarn.lock
Expand Up @@ -2248,6 +2248,11 @@ eventemitter3@^3.0.0:
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.0.tgz#090b4d6cdbd645ed10bf750d4b5407942d7ba163"
integrity sha512-ivIvhpq/Y0uSjcHDcOIccjmYjGLcP09MFGE7ysAwkAvkXfpZlC985pH2/ui64DKazbTW/4kN3yqozUxlXzI6cA==

eventemitter3@^3.1.0:
version "3.1.2"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==

events@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/events/-/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88"
Expand Down Expand Up @@ -2464,6 +2469,11 @@ fast-json-stable-stringify@^2.0.0:
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
integrity sha1-1RQsDK7msRifh9OnYREGT4bIu/I=

fast-safe-stringify@^2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz#124aa885899261f68aedb42a7c080de9da608743"
integrity sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==

faye-websocket@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/faye-websocket/-/faye-websocket-0.10.0.tgz#4e492f8d04dfb6f89003507f6edbf2d501e7c6f4"
Expand Down Expand Up @@ -5269,6 +5279,11 @@ react-spring@8.0.27:
"@babel/runtime" "^7.3.1"
prop-types "^15.5.8"

react-use-gesture@6.0.2-beta.1:
version "6.0.2-beta.1"
resolved "https://registry.yarnpkg.com/react-use-gesture/-/react-use-gesture-6.0.2-beta.1.tgz#cb09be4684a8ae44d8c843bc66560d8f32c42bb6"
integrity sha512-3HbRi3c1HYApjUKJydISK3tJ/Tv1EsAyxDuOiOGWgqB+D+KbJOiUSUtAhXiFbHT81k2l49JPLzVpmeRFcOAZkw==

react@^16.9.0:
version "16.9.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.9.0.tgz#40ba2f9af13bc1a38d75dbf2f4359a5185c4f7aa"
Expand Down Expand Up @@ -5692,6 +5707,13 @@ shell-quote@1.6.1:
array-reduce "~0.0.0"
jsonify "~0.0.0"

shree@^0.1.14:
version "0.1.14"
resolved "https://registry.yarnpkg.com/shree/-/shree-0.1.14.tgz#224f6bf5056d5abac91ac407838597682c195c23"
integrity sha512-K3rT9La5VVZcv+W4tEgoY4tnB7LNBnJiD8rgpJUr1hJM7xmOmTscMYD6UwdeSESSpyVGq93IOVoCMiXCDqLQBw==
dependencies:
eventemitter3 "^3.1.0"

signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
Expand Down
120 changes: 120 additions & 0 deletions migration.md
@@ -0,0 +1,120 @@
# 2.x -> 3.x new features/migration guide

# Canvas

```jsx
<Canvas
raycaster // Props that go into the default raycaster
shadowMap // Props that go into gl.shadowMap, can also be set true for PCFsoft
noEvents = false // Switch off raytracing and event support
```

# Defaults that the canvas component sets up

A default _shadowMap_ if Canvas.shadowMap is true: `type: PCFSoftShadowMap`

# Events

#### Event data

```jsx
({
...DomEvent // All the original event data
...ThreeEvent // All of Three's intersection data
object: Object3D // The object that was actually hit
eventObject: Object3D // The object that registered the event
unprojectedPoint: Vector3 // Camera-unprojected point
ray: Ray // The ray that was used to strike the object
sourceEvent: DomEvent // A reference to the host event
delta: number // Initial-click delta
}) => ...
```

# Hooks

#### useThree(): SharedCanvasContext

```jsx
const {
mouse, // Current 2D mouse coordinates
clock, // THREE.Clock (usefull for useFrame deltas)
} = useThree()
```

#### useFrame(callback: (state, delta) => void, renderPriority: number = 0)

This hooks calls you back every frame, which is good for running effects, updating controls, etc. You receive the state (same as useThree) and a clock delta. If you supply a render priority greater than zero it will switch off automatic rendering entirely, you can then control rendering yourself. If you have multiple frames with a render priority then they are ordered highest priority last, similar to the web's z-index. Frames are managed, three-fiber will remove them automatically when the component that holds them is unmounted.

Updating controls:

```jsx
import { useFrame } from 'react-three-fiber'

const controls = useRef()
useFrame(state => controls.current.update())
return <orbitControls ref={controls} />
```

Taking over the render-loop:

```jsx
useFrame(({ gl, scene, camera }) => gl.render(scene, camera), 1)
```

#### useLoader(loader, url, [extensions]) (experimental!)

This hooks loads assets and suspends for easier fallback- and error-handling. It returns two values, the asset itself and a look-up-table of props. If you need to lay out GLTF's declaratively check out [gltfjsx](https://github.com/react-spring/gltfjsx).

```jsx
import React, { Suspense } from 'react'
import { useLoader } from 'react-three-fiber'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'

function Asset({ url }) {
const [gltf] = useLoader(GLTFLoader, url, loader => {
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('/draco-gltf/')
loader.setDRACOLoader(dracoLoader)
})
return <primitive object={gltf.scene} />
}

<Suspense fallback={<Cube />}>
<Asset url="/spaceship.gltf" />
</Suspense>
```

# React Native OOTB support

You can leverage Expo's WebGL port to react-native and use react-three-fiber as the renderer.

```bash
expo init myapp
cd myapp
yarn add expo-gl expo-three three@latest react-three-fiber
yarn start
```

# GLTF to JSX

https://github.com/react-spring/gltfjsx

An experimental tool that turns GLTF's files into react-three-fiber-JSX components.

```bash
npx gltfjsx input.gltf [Output.js] [options]

Options:
--draco, -d adds DRACOLoader [string] [default: "/draco-gltf/"]
--animation, -a extracts animation clips [boolean]
--help Show help [boolean]
--version Show version number [boolean]
```

<img src="https://i.imgur.com/DmdTMcL.gif" />

# Misc

- Support for object.layers, [example](https://codesandbox.io/s/react-three-fiber-gltf-loader-animations-w633u)
- Finally compatible with [react-use-gesture](https://github.com/react-spring/react-use-gesture), [example](https://codesandbox.io/s/react-three-fiber-gestures-dh2jc)
10 changes: 9 additions & 1 deletion readme.md
@@ -1,3 +1,11 @@
# Version 3.x is out 🎉 🎊 🥳

Find out [here](/migration.md) about the changes. TLDR: bugfixes, new features, react-native, better types, very few breaking changes (mostly just useRender -> useFrame and apply -> extend).

<br />
<br />
<br />

<p align="center">
<a href="https://codesandbox.io/embed/react-three-fiber-gltf-loader-animations-w633u"><img width="288" height="172" src="https://i.imgur.com/UzG518m.gif" /></a>
<a href="https://github.com/drcmda/learnwithjason"><img width="288" height="172" src="https://i.imgur.com/1atWRR3.gif" /></a>
Expand All @@ -16,7 +24,7 @@

npm install three react-three-fiber

React-three-fiber is a small React renderer for Threejs on the web and react-native. Why, you might ask? React was made to drive complex tree structures, it makes just as much sense for Threejs as for the DOM. Building a dynamic scene graph becomes so much easier because you can break it up into declarative, re-usable components with clean, reactive semantics. This also opens up the ecosystem, you can now apply generic packages for state, animation, gestures and so on.
React-three-fiber is a renderer for Threejs on the web and react-native. Why? Building a dynamic scene graph becomes so much easier when you can break it up into declarative, re-usable, reactive components. It also opens up the ecosystem, you can apply generic packages for state, animation, gestures and so on. This is less of an abstraction and more of a pure reconciler (think react-dom in relation to HTML). It does not target a specific Threejs version nor does it need updates when Threejs alters, adds or removes features, it won't change any specifics or rules, and there are zero limitations.

# What it looks like ...

Expand Down
2 changes: 1 addition & 1 deletion recipes.md
Expand Up @@ -193,7 +193,7 @@ render((
## Reducing bundle-size
Threejs is a heavy-weight. But you can always create your own exports file and alias the "three" towards it. This way you can reduce it to 50-80kb or perhaps less. Gist: https://gist.github.com/drcmda/974f84240a329fa8a9ce04bbdaffc04d
Threejs is quite heavy and tree-shaking doesn't yet yield the results you would hope for atm. But you can always create your own export-file and alias "three" towards it. This way you can reduce it to 80-50kb, or perhaps less, depending on what you need. Gist: https://gist.github.com/drcmda/974f84240a329fa8a9ce04bbdaffc04d
## Usage with React Native
Expand Down
2 changes: 1 addition & 1 deletion src/targets/web/canvas.tsx
Expand Up @@ -42,7 +42,7 @@ const IsReady = React.memo(

// Init canvas, fetch events, hand them back to the warpping div
const events = useCanvas({ ...props, gl })
useEffect(() => void setEvents(events), [])
useEffect(() => void setEvents(events), [events])
return null
}
)
Expand Down

0 comments on commit f87a110

Please sign in to comment.