A growing collection of useful helpers and abstractions for react-three-fiber, saving you some boilerplate.
If you find yourself repeating set-up code often and if it's generic enough, add it here, everyone benefits!
Requirements
- Types
- ForwardRefs if possible, so that objects can be referenced back
- Invalidate frames on any movement for those using invalidateFrameloop
- Cleanup on unmount, no left-overs, restore previous states
yarn add drei
import { ... } from 'drei'
Index
- Cameras
<PerspectiveCamera />
<OrthographicCamera />
- Controls
<OrbitControls />
<MapControls />
<TrackballControls />
<TransformControls />
- Shapes
<Plane />
<Box />
<Sphere />
<Circle />
<Cone />
<Cylinder />
<Tube />
<Torus />
<TorusKnot />
<Ring />
<Tetrahedron />
<Polyhedron />
<Icosahedron />
<Octahedron />
<Dodecahedron />
<Extrude />
<Lathe />
<Parametric />
- Abstractions
<Text />
<Detailed />
<PositionalAudio />
<StandardEffects />
- Shaders
<Sky />
softShadows()
- Misc
<HTML />
<Shadow />
<Stats />
draco()
meshBounds()
useCamera()
useHelper()
useAspect()
Exports
Cameras
⚡️ <PerspectiveCamera />
A responsive THREE.PerspectiveCamera that can set itself as the default.
<PerspectiveCamera
makeDefault
{...props}
>
<mesh />
</PerspectiveCamera>
⚡️ <OrthographicCamera />
A responsive THREE.OrthographicCamera that can set itself as the default.
Controls
If available controls have damping enabled by default, they manage their own updates, remove themselves on unmount, are compatible with the invalidateFrameloop
canvas-flag. They inherit all props from their underlying THREE controls.
⚡️ <OrbitControls />
⚡️ <MapControls />
⚡️ <TrackballControls />
⚡️ <TransformControls />
Shapes
Buffer-geometry short-cuts:
<Plane args={[2, 2]} />
<Sphere>
<meshBasicMaterial attach="material" color="hotpink" />
</Sphere>
⚡️ <Plane />
⚡️ <Box />
⚡️ <Sphere />
⚡️ <Circle />
⚡️ <Cone />
⚡️ <Cylinder />
⚡️ <Tube />
⚡️ <Torus />
⚡️ <TorusKnot />
⚡️ <Ring />
⚡️ <Tetrahedron />
⚡️ <Polyhedron />
⚡️ <Icosahedron />
⚡️ <Octahedron />
⚡️ <Dodecahedron />
⚡️ <Extrude />
⚡️ <Lathe />
⚡️ <Parametric />
Abstractions
⚡️ <Text />
Hi-quality text rendering w/ signed distance fields (SDF) and antialiasing, using troika-3d-text. All of troikas props are valid!
<Text>hello world!</Text>
⚡️ <Detailed
>
A wrapper around THREE.LOD (Level of detail).
<Detailed
distances={[0, 10, 20]}
{...props}
>
<mesh geometry={highDetail} />
<mesh geometry={mediumDetail} />
<mesh geometry={lowDetail} />
</Detailed>
⚡️ <PositionalAudio />
A wrapper around THREE.PositionalAudio. Add this to groups or meshes to tie them to a sound that plays when the camera comes near.
<PositionalAudio
url="/sound.mp3"
distance={1}
loop
{...props}
/>
⚡️ <StandardEffects />
Adds ambient-occlusion, bloom and SMAA using the postprocessing library.
⚠️ AO relies on the depthbuffer! Make sure your near and far clipping planes are narrow enough, or use <Canvas gl={{ logarithmicDepthBuffer: true }} ... />
.
<StandardEffects
smaa
ao
bloom
edgeDetection={0.1}
bloomOpacity={1}
effects={() => [...fx]}
/>
Shaders
⚡️ <Sky />
Adds a sky to your scene.
<Sky
distance={450000}
sunPosition={[0, 1, 0]}
{...props}
/>
⚡️ softShadows()
Injects percent closer soft shadows (pcss) into threes shader chunk.
softShadows({
frustrum: 3.75,
size: 0.005,
near: 9.5,
samples: 17,
rings: 11,
})
Misc
⚡️ <HTML />
Allows you to tie HTML content to any object of your scene. It will be projected to the objects whereabouts automatically.
<HTML
prepend
center
fullscreen
scaleFactor={10}
zIndexRange={[100, 0]}
portal={domnodeRef}
{...groupProps}
{...divProps}
>
<h1>hello</h1>
<p>world</p>
</HTML>
⚡️ <Shadow />
A cheap canvas-texture-based circular gradient.
<Shadow
color="black"
colorStop={0}
opacity={0.5}
fog={false}
/>
⚡️ <Stats />
Adds stats to document.body. It takes over the render-loop!
<Stats
showPanel={0}
{...props}
/>
⚡️ draco()
Adds the Draco extension to your GLTFLoader.
useLoader(
GLTFLoader,
url,
draco(
'/draco-gtltf/'
)
)
⚡️ meshBounds()
A very fast, but often good-enough bounds-only raycast for meshes. You can use this if performance has precidence over pointer precision.
<mesh raycast={meshBounds} />
⚡️ useCamera()
A hook for the rare case when you are using non-default cameras for heads-up-displays or portals, and you need events/raytracing to function properly (raycasting uses the default camera otherwise).
<mesh raycast={useCamera(customCamera)} />
⚡️ useHelper()
A hook for a quick way to add helpers to existing nodes in the scene. It handles removal of the helper on unmount and auto-updates it by default.
const mesh = useRef()
useHelper(mesh, BoxHelper, "cyan")
⚡️ useAspect()
This hook calculates aspect ratios (for now only what in css would be image-size: cover
is supported). You can use it to make an image fill the screen. It is responsive and adapts to viewport resize. Just give the hook the image bounds in pixels. It returns an array: [width, height, 1]
.
const scale = useAspect(
"cover"
1024,
512,
1
)
return (
<mesh scale={scale}>
<planeBufferGeometry />
<meshBasicMaterial map={imageTexture} />