Creating a 3D donut-like structure in React using react-three-fiber

In this article, we are going to see how to make a 3D figure which will look like a donut. We will apply this in a webpage using react-three-fiber. We will use this package to make a donut-like shape which will keep moving and we will see it from inside of it.


First, download important libraries −

npm i --save @react-three/fiber three

threejs and react-three/fiber will be used to add webGL renderer to the website. Three-fiber will be used to connect threejs and React.

Add the following lines of code in App.js

import React, { useRef } from "react";
import { Canvas, useFrame } from "@react-three/fiber";
function Tourus() {

   const ref = useRef(null);
   useFrame(() => (ref.current.rotation.x = ref.current.rotation.y += 0.01));

   return (
      <mesh visible position={[0, 0, 0]} castShadow ref={ref}>
         <torusGeometry args={[10, 3, 16, 100]} />
         <meshStandardMaterial attach="material" color="lightblue" />

export default function App() {
   return (
         <ambientLight />
         <Tourus />

The function Tourus takes 4 arguments: inner radius, tube radius, radial segment which decides the roundness of internal part, and tubular segment which decides the roundness of outer part.


It is the 3D view of camera where the camera is between the inner radius of donut.

It is rotating and it is a snip while it is rotating.

Updated on: 28-Sep-2021


Kickstart Your Career

Get certified by completing the course

Get Started