@react-three/drei and Helper LibrariesWhile React Three Fiber (R3F) provides the core bridge between React and Three.js, @react-three/drei (often simply referred to as Drei) is an indispensable collection of helpful utilities and abstractions that significantly simplify common tasks in 3D scene creation. It's a vital part of the R3F ecosystem, offering ready-made components and hooks that save development time and effort.
@react-three/drei?Drei is a growing library that provides a vast array of components and hooks built on top of R3F. It aims to make working with R3F more efficient by offering pre-built solutions for frequently used functionalities, allowing developers to focus on unique aspects of their 3D applications rather than re-implementing common patterns.
<Billboard>, <Image>, <Text>, <Text3D>, <Trail>, and hooks like useAnimations for common 3D elements and animations.<OrbitControls>, <MapControls>, and <TransformControls>.<Environment> for easily adding realistic environments and lighting.<Detailed> and <LOD> for level-of-detail optimization.<MeshReflectorMaterial>, <MeshDistortMaterial>, and <MeshTransmissionMaterial>.<Stats> and <Leva> (though Leva is a separate library, it's often used alongside Drei for debugging).<OrbitControls> and <Text> from DreiThis example demonstrates how to use <OrbitControls> for camera interaction and <Text> to display 3D text. Note that this requires a React environment to run.
import React from 'react';
import { Canvas } from '@react-three/fiber';
import { OrbitControls, Text } from '@react-three/drei';
function SceneWithDrei() {
return (
<Canvas>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<mesh>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="orange" />
</mesh>
<Text
position={[0, 1.5, 0]}
fontSize={0.5}
color="white"
anchorX="center"
anchorY="middle"
>
Hello Drei!
</Text>
<OrbitControls />
</Canvas>
);
}
export default SceneWithDrei;