React Three Fiber (R3F) is a React renderer for Three.js, a powerful JavaScript 3D library. It allows developers to build 3D scenes and animations for the web and React Native applications using React's familiar component-based architecture and declarative syntax.
useFrame, useThree, useLoader) and context, allowing you to leverage the existing React development workflow and state management solutions.@react-three/drei for helpful abstractions, ready-to-use components, and utilities that simplify common tasks.R3F brings the benefits of React development (component reusability, declarative code, state management) to the world of 3D graphics. If you're already familiar with React, R3F significantly lowers the barrier to entry for creating complex and interactive 3D experiences on the web.
Here's a simple example of a rotating box using React Three Fiber. Note that this requires a React environment to run.
import React, { useRef } from 'react';
import { Canvas, useFrame } from '@react-three/fiber';
function Box(props) {
// This reference will give us direct access to the THREE.Mesh object
const meshRef = useRef();
// Subscribe this component to the render-loop, rotate the mesh every frame
useFrame(() => {
if (meshRef.current) {
meshRef.current.rotation.x += 0.01;
meshRef.current.rotation.y += 0.01;
}
});
// Return the view, these are regular Three.js elements expressed in JSX
return (
<mesh {...props} ref={meshRef}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={'hotpink'} />
</mesh>
);
}
export default function App() {
return (
<Canvas>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<Box position={[-1.2, 0, 0]} />
<Box position={[1.2, 0, 0]} />
</Canvas>
);
}