Quick Overview (for the impatient)
React Three Fiber (R3F) makes working with 3D objects easy. In this post, we'll explore basic geometries like Box, Sphere, and Torus, and show how to manipulate them in 3D space. Jump to the Table of Contents to explore specific topics!
Table of Contents
- What is React Three Fiber?
- Understanding Geometries in R3F
- Basic Geometries
- Transforming Geometries
- Conclusion
Warning
Though the content is generated by AI as a placeholder but it might be usefull 😉
What is React Three Fiber?
React Three Fiber (R3F) is a React renderer for Three.js. It lets you create 3D objects, animations, and scenes inside a React app, blending React’s component-based architecture with Three.js’s 3D capabilities.
Understanding Geometries in R3F
In R3F, geometries define the shape of 3D objects. You can use basic predefined geometries like BoxGeometry, SphereGeometry, and TorusGeometry. These geometries are then added to your scene to create 3D visuals.
Basic Geometries
Let’s explore some basic geometries you can work with in R3F:
Box Geometry
A simple box, defined by width, height, and depth:
import { Canvas } from '@react-three/fiber';
import { BoxGeometry, MeshBasicMaterial, Mesh } from 'three';
const Box = () => {
return (
<Canvas>
<mesh>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="blue" />
</mesh>
</Canvas>
);
};
This creates a 1x1x1 blue box in 3D space.
Sphere Geometry
A sphere, defined by its radius and width/height segments:
const Sphere = () => {
return (
<Canvas>
<mesh>
<sphereGeometry args={[1, 32, 32]} />
<meshStandardMaterial color="red" />
</mesh>
</Canvas>
);
};
This creates a red sphere with a radius of 1 and 32 segments.
Torus Geometry
A torus (donut shape), defined by radius and tube size:
const Torus = () => {
return (
<Canvas>
<mesh>
<torusGeometry args={[1, 0.4, 16, 100]} />
<meshStandardMaterial color="green" />
</mesh>
</Canvas>
);
};
This creates a green torus with a radius of 1 and tube thickness of 0.4.
Transforming Geometries
You can easily transform geometries by changing their position, rotation, or scale. For example, to rotate the BoxGeometry:
const RotatingBox = () => {
return (
<Canvas>
<mesh rotation={[Math.PI / 4, Math.PI / 4, 0]}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="blue" />
</mesh>
</Canvas>
);
};
This will rotate the box by 45 degrees on the X and Y axes.
Conclusion
R3F simplifies the process of creating and transforming 3D objects in React applications. By understanding basic geometries like Box, Sphere, and Torus, you can start building complex 3D scenes with ease. Experiment with different geometries and transformations to bring your 3D creations to life!
Now you have a quick overview of geometries in R3F. Dive in and start building your own 3D world!