Live Demo https://react-three-fiber-paul.netlify.app
To display anything in three.js, we need:
- Scene
- Camera
- Render
The <Canvas />
component sets up the Scene and Camera
- React Library for Three.js (everything that works Three.js works in react-three-fiber)
- Expresses Three.js in JSX
- To create a mesh object we can run
new THREE.Mesh()
<mesh />
(Declarative)
- To create a mesh object we can run
- Examples
React + Threejs
- Concepts to grasp
- Scene
- Camera
- Mesh
- Geometry (shape)
- Material (color, look)
- Set up the Canvas
- Sets up a Scene and a Camera, which are the essential building blocks of rendering.
- Add a mesh component
- Basic object, it holds geometry and material to represent a 3d Shape.
- To see it we add a box and standard material
- Add lights
- Simple, just adds lights
- Can configure element options with props.
# React-Three-Fiber
<Canvas>
<mesh>
<boxGeometry />
<meshStandardMaterial />
</mesh>
</Canvas>
# Three.js
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000)
const renderer = new THREE.WebGLRenderer()
renderer.setSize(width, height)
document.querySelector('#canvas-container').appendChild(renderer.domElement)
const mesh = new THREE.Mesh()
mesh.geometry = new THREE.BoxGeometry()
mesh.material = new THREE.MeshStandardMaterial()
scene.add(mesh)
function animate() {
requestAnimationFrame(animate)
renderer.render(scene, camera)
}
animate()
# React-Three-Fiber
<boxGeometry args={[2, 2, 2]} />
# Three.js
new THREE.BoxGeometry(2, 2, 2)
args
height width and depth- When referring to position then refers to horizontal (x), vertical (y), 3D (z)
useFrame
hook is used to rotate objects