👌THREE.js的React-fiber渲染器

These demos are real, you can click them! They contain the full code, too.

npm install react-three-fiber

React-three-fiber is a small React renderer for Three-js. Why, you might ask? React was made to drive complex tree structures, it makes just as much sense for Three as it makes 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 eco system, you can now apply generic packages for state, animation, gestures and so on.

This is a small reconciler config with a few additions for interaction and hooks holding it all together. It does not know or care about Three internals, it uses heuristics for objects and attributes, so that we can get away without creating a strong dependency. Three is constantly changing, we don't want to rely on a specific version or chase their release cycle. This library works with version 1 as well as their latest. At the same time we don't want to alter any rules, if something works in Three in a specific way, it will be the same here.

How it looks like ...

Copy the following into a project to get going. Here's the same running in a code sandbox.

Shortcuts and non-Object3D stow-away

All properties that have a .set() method (colors, vectors, euler, matrix, etc) can be given a shortcut. For example THREE.Color.set can take a color string, hence instead of color={new THREE.Color('peachpuff') you can do color="peachpuff". Some set-methods take multiple arguments (vectors for instance), in this case you can pass an array.

You can stow away non-Object3D primitives (geometries, materials, etc) into the render tree so that they become managed and reactive. They take the same properties they normally would, constructor arguments are passed with args. If you give them a name they attach automatically to their parent.

The following is the same as above, but it's leaner and critical properties aren't re-instanciated on every render.

Piercing into nested properties

Extending or using arbitrary objects

When you need managed local (or custom/extended) objects, you can use the primitive placeholder.

constmesh=new THREE.Mesh()
return <primitiveobject={mesh} />

Events

THREE objects that implement their own raycast method (for instance meshes, lines, etc) can be interacted with by declaring events on the object. For now that's prop-updates (very useful for things like verticesNeedUpdate), hovering-state and clicks. Touch follows soon!

<meshonClick={e=>console.log('click')}onHover={e=>console.log('hover')}onUnhover={e=>console.log('unhover')}onUpdate={self=>console.log('props have been updated')}
/>

Gl data & hooking into the render loop

Sometimes you're running effects, postprocessing, etc that needs to get updated. You can fetch the renderer, the camera, scene, and a render-loop subscribe to do this.

import { Canvas, useRender, useThree } from'react-three-fiber'functionApp() {
// gl is the webgl-renderer// canvas the dom element that was created// size the bounds of the view (which stretches 100% and auto-adjusts)// viewport is the calculated screen-size, it's a functionconst { gl, canvas, scene, camera, size, viewport } =useThree()
// Subscribes to the render-loop, gets cleaned up automatically when the component unmounts// Add a "true" as the 2nd argument and you take over the render-loopuseRender(({ gl, canvas, scene, camera }) =>console.log("i'm in the render-loop"))
return <group />
}

Receipes

Handling loaders

You can use Reacts built-in memoizing-features (as well as suspense) to build async dependence graphs.

Heads-up display (rendering multiple scenes)

useRender allows components to hook into the render-loop, or even to take it over entirely. That makes it possible for one component to render over the content of another. The order of these operations is established by the scene-graph.