<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abdullah Nadeem</title>
    <description>The latest articles on Forem by Abdullah Nadeem (@abdnadeem382).</description>
    <link>https://forem.com/abdnadeem382</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1134502%2F3e21c9ca-7966-49a6-82ef-1a3e63ed05b0.jpeg</url>
      <title>Forem: Abdullah Nadeem</title>
      <link>https://forem.com/abdnadeem382</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abdnadeem382"/>
    <language>en</language>
    <item>
      <title>Add Text as Texture on a 3D Object in a React App</title>
      <dc:creator>Abdullah Nadeem</dc:creator>
      <pubDate>Tue, 06 Feb 2024 20:31:00 +0000</pubDate>
      <link>https://forem.com/abdnadeem382/add-text-as-texture-on-a-3d-object-in-a-react-app-252o</link>
      <guid>https://forem.com/abdnadeem382/add-text-as-texture-on-a-3d-object-in-a-react-app-252o</guid>
      <description>&lt;p&gt;Let's learn how to add text to a 3D object in a React application!&lt;/p&gt;

&lt;p&gt;We will use &lt;code&gt;@react-three/fiber&lt;/code&gt; to accomplish this feat.&lt;br&gt;
I'll be assuming you already have a basic understanding of React and a new React application set up.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Install Required Packages
&lt;/h3&gt;

&lt;p&gt;Let's install the required packages first. Run the following command in your terminal (make sure you are in the root directory of your project):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install three @react-three/fiber @react-three/drei&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;strong&gt;&lt;em&gt;three&lt;/em&gt;&lt;/strong&gt; is the base library that &lt;strong&gt;&lt;em&gt;react-three-fiber&lt;/em&gt;&lt;/strong&gt; uses to create and render the 3D scene. &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt; is a helper library for &lt;strong&gt;&lt;em&gt;react-three-fiber&lt;/em&gt;&lt;/strong&gt; that provides a set of reusable components and functions.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Setup the Canvas
&lt;/h3&gt;

&lt;p&gt;Now, import the &lt;code&gt;Canvas&lt;/code&gt; from &lt;strong&gt;&lt;em&gt;@react-three/fiber&lt;/em&gt;&lt;/strong&gt; in your React component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { Canvas } from '@react-three/fiber';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Use the Canvas inside return in a functional component like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CubeCanvas = () =&amp;gt; {
  return (
     &amp;lt;Canvas
      camera={{
        fov: 25,
        position: [5, 5, 5],
      }}
    &amp;gt;
    &amp;lt;/Canvas&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the &lt;code&gt;fov (Field of View)&lt;/code&gt; and &lt;code&gt;position&lt;/code&gt; of the &lt;code&gt;Canvas&lt;/code&gt; according to your use case. (A larger &lt;code&gt;fov&lt;/code&gt; will result in a wider view.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Add Some Lights
&lt;/h3&gt;

&lt;p&gt;Add some lights inside the Canvas so the Cube isn't too dark when added later on. Let's use &lt;code&gt;ambientLight&lt;/code&gt; and &lt;code&gt;directionalLight&lt;/code&gt; (They don't need importing).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;Canvas
      camera={{
        fov: 25,
        position: [5, 5, 5],
      }}
    &amp;gt;
      &amp;lt;ambientLight intensity={1} /&amp;gt;
      &amp;lt;directionalLight position={[3, 2, 1]} /&amp;gt;
    &amp;lt;/Canvas&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Create a Cube
&lt;/h3&gt;

&lt;p&gt;Now, let's create a separate component to render our cube.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Cube() {
  return (
    &amp;lt;mesh&amp;gt;
      &amp;lt;boxGeometry /&amp;gt;
      &amp;lt;meshStandardMaterial&amp;gt;
        &amp;lt;RenderTexture attach="map"&amp;gt;
          &amp;lt;color attach={"background"} args={["#dc9dcd"]} /&amp;gt;
        &amp;lt;/RenderTexture&amp;gt;
      &amp;lt;/meshStandardMaterial&amp;gt;
    &amp;lt;/mesh&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, a &lt;code&gt;mesh&lt;/code&gt; component is used, which is a low-level react-three-fiber component used to create and manipulate 3D objects. Inside &lt;code&gt;mesh&lt;/code&gt;, we use &lt;code&gt;boxGeometry&lt;/code&gt; for displaying a cube shaped object.&lt;/p&gt;

&lt;p&gt;We also need a &lt;code&gt;meshStandardMaterial&lt;/code&gt; to define the appearance of the cube. Inside, &lt;code&gt;meshStandardMaterial&lt;/code&gt;, a &lt;code&gt;RenderTexture&lt;/code&gt; component, imported from &lt;code&gt;@react-three/drei&lt;/code&gt; is used to render texture on the cube. &lt;code&gt;attach="map"&lt;/code&gt; indicates that the rendered texture should be used as the texture map for the material of the parent mesh.&lt;/p&gt;

&lt;p&gt;We also give a color to the cube by using a &lt;code&gt;color&lt;/code&gt; component.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Add Text to Cube
&lt;/h3&gt;

&lt;p&gt;Let's use the &lt;code&gt;Text&lt;/code&gt; component from &lt;code&gt;@react-three/drei&lt;/code&gt; to use text as a texture on our cube. Add the following inside the &lt;code&gt;RenderTexture&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Text position={[-0.2, 0, 0]} fontSize={1} color={"#555"}&amp;gt;
 hello
&amp;lt;/Text&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd3d6w824vsz11ee6p11c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd3d6w824vsz11ee6p11c.png" alt="Cube with unaligned text" width="800" height="654"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's all we need to add text to the cube. The positioning of the text may seem a little odd at the moment. But we can fix that using the &lt;code&gt;PerspectiveCamera&lt;/code&gt; from &lt;code&gt;@react-three/drei&lt;/code&gt;. Also, add &lt;code&gt;Cube&lt;/code&gt; inside the &lt;code&gt;CubeCanvas&lt;/code&gt; This is how both components finally look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  Text,
  PerspectiveCamera,
  RenderTexture,
} from "@react-three/drei";
import { Canvas } from "@react-three/fiber";

function Cube() {
  return (
    &amp;lt;mesh&amp;gt;
      &amp;lt;boxGeometry /&amp;gt;
      &amp;lt;meshStandardMaterial&amp;gt;
        &amp;lt;RenderTexture attach={"map"}&amp;gt;
          &amp;lt;PerspectiveCamera makeDefault position={[0, 0, 5]} /&amp;gt;
          &amp;lt;color attach={"background"} args={["#dc9dcd"]} /&amp;gt;
          &amp;lt;Text position={[-0.2, 0, 0]} fontSize={1} color={"#555"}&amp;gt;
            hello
          &amp;lt;/Text&amp;gt;
        &amp;lt;/RenderTexture&amp;gt;
      &amp;lt;/meshStandardMaterial&amp;gt;
    &amp;lt;/mesh&amp;gt;
  );
}

function CubeCanvas() {
  return (
    &amp;lt;Canvas
      camera={{
        fov: 25,
        position: [5, 5, 5],
      }}
    &amp;gt;
      &amp;lt;ambientLight intensity={1} /&amp;gt;
      &amp;lt;directionalLight position={[3, 2, 1]} /&amp;gt;
      &amp;lt;Cube /&amp;gt;
    &amp;lt;/Canvas&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the text is now fixed 👌&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxq7niyprvg4wk2a6bq7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxq7niyprvg4wk2a6bq7z.png" alt="Cube with aligned text" width="800" height="661"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;That's the end of this tutorial. We rendered a 3D object in our React app using &lt;code&gt;threejs&lt;/code&gt;, &lt;code&gt;react-three-fiber&lt;/code&gt; and &lt;code&gt;react-three/drei&lt;/code&gt;, and learned how to add text as texture to it.&lt;/p&gt;

&lt;p&gt;You can find the official docs &lt;a href="https://docs.pmnd.rs/"&gt;here&lt;/a&gt; and explore the vast world of 3D rendering in React using &lt;strong&gt;&lt;em&gt;React-Three-Fiber&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>3d</category>
      <category>threejs</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Add an Image as Texture to a 3D Object in a React App</title>
      <dc:creator>Abdullah Nadeem</dc:creator>
      <pubDate>Thu, 17 Aug 2023 09:31:57 +0000</pubDate>
      <link>https://forem.com/abdnadeem382/add-an-image-as-texture-to-a-3d-object-in-a-react-app-2caa</link>
      <guid>https://forem.com/abdnadeem382/add-an-image-as-texture-to-a-3d-object-in-a-react-app-2caa</guid>
      <description>&lt;p&gt;In this article, we will learn how to add a picture as a texture to a 3D object in a React application. We will use &lt;code&gt;@react-three/fiber&lt;/code&gt; to accomplish this feat.&lt;br&gt;
We will be assuming you already have a basic understanding of React and a new React application set up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install Required Packages
&lt;/h3&gt;

&lt;p&gt;Let's install the dependencies first. Run the following command in your terminal (make sure you are in the root directory of your project):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install three @react-three/fiber @react-three/drei&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;strong&gt;&lt;em&gt;three&lt;/em&gt;&lt;/strong&gt; is the base library that &lt;strong&gt;&lt;em&gt;react-three-fiber&lt;/em&gt;&lt;/strong&gt; uses to create and render the 3D scene. &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt; is a helper library for &lt;strong&gt;&lt;em&gt;react-three-fiber&lt;/em&gt;&lt;/strong&gt; that provides a set of reusable components and functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Setup the Canvas
&lt;/h3&gt;

&lt;p&gt;Now, import the &lt;code&gt;Canvas&lt;/code&gt; from &lt;strong&gt;&lt;em&gt;@react-three/fiber&lt;/em&gt;&lt;/strong&gt; in your React component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { Canvas } from '@react-three/fiber';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Use the Canvas inside return like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const ShapeCanvas = () =&amp;gt; {
  return (
    &amp;lt;Canvas frameloop="demand"&amp;gt;
    &amp;lt;/Canvas&amp;gt;
  );
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;frameloop&lt;/code&gt; prop set to &lt;code&gt;demand&lt;/code&gt; is telling react-three-fiber to only re-render when state changes occur.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Render a 3D Shape
&lt;/h3&gt;

&lt;p&gt;Let's make another component and name it &lt;code&gt;Shape&lt;/code&gt;. We will use this to render a 3D model and use it inside our &lt;code&gt;Canvas&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We first use the &lt;code&gt;Float&lt;/code&gt; component from &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt; to give our model a floating effect. Import &lt;code&gt;Float&lt;/code&gt; like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { Float } from '@react-three/drei';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;return&lt;/code&gt; statement, write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Float speed={1.75} rotationIntensity={1} floatIntensity={2}&amp;gt;&amp;lt;/Float&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;speed&lt;/code&gt; refers to the animation speed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rotationIntensity&lt;/code&gt; is the XYZ rotation intensity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;floatIntensity&lt;/code&gt; is the Up/Down float intensity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll now use a &lt;code&gt;mesh&lt;/code&gt; component which is a low-level react-three-fiber component used to create and manipulate 3D objects. Inside &lt;code&gt;Float&lt;/code&gt;, write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;mesh castShadow receiveShadow scale={1.75}&amp;gt;&amp;lt;/mesh&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have allowed the mesh to cast and receive shadows and set its scale according to our use case.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;mesh&lt;/code&gt; is usually composed of a &lt;strong&gt;&lt;em&gt;geometry&lt;/em&gt;&lt;/strong&gt; and a &lt;strong&gt;&lt;em&gt;material&lt;/em&gt;&lt;/strong&gt;. So let's add these inside our &lt;code&gt;mesh&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will use an &lt;code&gt;icosahedron&lt;/code&gt; geometry as our shape to render. Inside &lt;code&gt;mesh&lt;/code&gt;, write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;icosahedronGeometry args={[1, 1]} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The icosahedron is a polyhedron with 20 identical equilateral triangular faces. The args={[1, 1]} prop specifies the radius and detail of the icosahedron.&lt;/p&gt;

&lt;p&gt;Now, add a &lt;strong&gt;&lt;em&gt;material&lt;/em&gt;&lt;/strong&gt; inside &lt;code&gt;mesh&lt;/code&gt; like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;meshStandardMaterial color="#fff8eb" polygonOffset polygonOffsetFactor={-5} flatShading /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;color&lt;/code&gt; is used to specify a color for the material that will be used on our geometry.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;polygonOffset&lt;/code&gt; and &lt;code&gt;polygonOffsetFactor&lt;/code&gt; properties are used to avoid an artifact known as &lt;strong&gt;&lt;em&gt;z-fighting&lt;/em&gt;&lt;/strong&gt;, where two faces occupy the same space and create a flickering effect.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;flatShading&lt;/code&gt; set to &lt;strong&gt;&lt;em&gt;true&lt;/em&gt;&lt;/strong&gt; means that the material will have a sharp and flat look instead of the default smooth one.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;Shape&lt;/code&gt; component now looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const Shape = () =&amp;gt; {
  return (
    &amp;lt;Float speed={1.75} rotationIntensity={1} floatIntensity={2}&amp;gt;
      &amp;lt;mesh castShadow receiveShadow scale={1.75}&amp;gt;
        &amp;lt;icosahedronGeometry args={[1, 1]} /&amp;gt;
        &amp;lt;meshStandardMaterial color="#fff8eb" polygonOffset polygonOffsetFactor={-5} flatShading /&amp;gt; 
      &amp;lt;/mesh&amp;gt;
    &amp;lt;/Float&amp;gt;
  );
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now use this in &lt;code&gt;ShapeCanvas&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const ShapeCanvas = () =&amp;gt; {
  return (
    &amp;lt;Canvas frameloop="demand"&amp;gt;
      &amp;lt;Shape /&amp;gt;
    &amp;lt;/Canvas&amp;gt;
  );
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Right now, we can see a dark shape in our browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbupdn4kttd9lou3z3frt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbupdn4kttd9lou3z3frt.png" alt="3d model without light"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Does this mean we did something wrong?&lt;/em&gt;&lt;/strong&gt; Not really! We just need to add some lights to the scene to see our 3D shape clearly. So let's do that.&lt;/p&gt;

&lt;p&gt;There are multiple options for us to add lights in a scene which you can explore &lt;a href="https://docs.pmnd.rs/react-three-fiber/getting-started/your-first-scene#adding-lights" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For now, we will use &lt;code&gt;ambientLight&lt;/code&gt; and &lt;code&gt;directionalLight&lt;/code&gt;. Write the following lines above the &lt;code&gt;mesh&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;ambientLight intensity={0.25} /&amp;gt;
&amp;lt;directionalLight position={[0, 0, 0.05]} /&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We have adjusted the &lt;code&gt;intensity&lt;/code&gt; and &lt;code&gt;position&lt;/code&gt; props for the lights according to our need. Our 3D shape now looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe85n5ezkiiwxw3me0l59.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe85n5ezkiiwxw3me0l59.png" alt="Illuminated 3D shape"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Add an Image as Texture to the Shape
&lt;/h3&gt;

&lt;p&gt;To use an image as texture on our shape, we will use the &lt;code&gt;useTexture&lt;/code&gt; hook from &lt;code&gt;@react-three/drei&lt;/code&gt;. This hook is used to load one or several textures asynchronously. A texture, in this context, is essentially an image that gets mapped onto the surface of a 3D model. We use the hook as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [texture] = useTexture([threejs]);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;threejs&lt;/code&gt; is just an &lt;code&gt;svg&lt;/code&gt; that can be imported in our component.&lt;/p&gt;

&lt;p&gt;We now use &lt;code&gt;Decal&lt;/code&gt; from &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt; to map the texture onto our model.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Decal position={[0, 0, 1]} rotation={[2 * Math.PI, 0, 6.25]} scale={1} map={texture} flatShading /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;position&lt;/code&gt; determines where the decal is located relative to the origin of the object it's applied to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rotation&lt;/code&gt; determines the rotation of the decal around the x, y, and z axes in radians.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;scale&lt;/code&gt; determines the size of the decal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;map&lt;/code&gt; specifies the texture/image to be used as the decal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;flatShading&lt;/code&gt; does the same thing it does to our &lt;code&gt;material&lt;/code&gt; as explained before.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our &lt;code&gt;Shape&lt;/code&gt; component now looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { Decal, Float, useTexture } from '@react-three/drei';
import { threejs } from '../../assets';

const Shape = () =&amp;gt; {
  const [texture] = useTexture([threejs]);

  return (
    &amp;lt;Float speed={1.75} rotationIntensity={1} floatIntensity={2}&amp;gt;
      &amp;lt;ambientLight intensity={0.25} /&amp;gt;
      &amp;lt;directionalLight position={[0, 0, 0.05]} /&amp;gt;
      &amp;lt;mesh castShadow receiveShadow scale={1.75}&amp;gt;
        &amp;lt;icosahedronGeometry args={[1, 1]} /&amp;gt;
        &amp;lt;meshStandardMaterial color="#fff8eb" polygonOffset polygonOffsetFactor={-5} flatShading /&amp;gt;
        &amp;lt;Decal position={[0, 0, 1]} rotation={[2 * Math.PI, 0, 6.25]} scale={1} map={texture} flatShading /&amp;gt;
      &amp;lt;/mesh&amp;gt;
    &amp;lt;/Float&amp;gt;
  );
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that's all we need to add an image as a texture to our 3D object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Adding Interactivity
&lt;/h3&gt;

&lt;p&gt;We can finally add some interactivity to make it more fun by using &lt;code&gt;OrbitControls&lt;/code&gt; from &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt;. In the &lt;code&gt;ShapeCanvas&lt;/code&gt; component, import &lt;code&gt;OrbitControls&lt;/code&gt; from &lt;code&gt;@react-three/drei&lt;/code&gt; and use it like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const ShapeCanvas = () =&amp;gt; {
  return (
    &amp;lt;Canvas frameloop="demand"&amp;gt;
      &amp;lt;OrbitControls enableZoom={false} enablePan={false} /&amp;gt;
      &amp;lt;Shape /&amp;gt;
    &amp;lt;/Canvas&amp;gt;
  );
};



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will disable the zooming and panning of our 3D shape but will allow us to rotate it using our cursor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, we dove deep into integrating 3D objects within a React application using &lt;code&gt;@react-three/fiber&lt;/code&gt;. We started from setting up a basic 3D canvas, progressing to rendering a 3D shape, and finally applying a textured image onto it. By adding the OrbitControls, we also introduced a layer of interactivity, letting users rotate the object. The combination of React with 3D holds vast potential, setting the stage for richer web interactions and experiences and &lt;code&gt;@react-three/fiber&lt;/code&gt; is just the right place to start for you.&lt;/p&gt;

</description>
      <category>react</category>
      <category>3d</category>
      <category>frontend</category>
      <category>threejs</category>
    </item>
    <item>
      <title>Importing and Interacting with a 3D Model in React with React-Three-Fiber</title>
      <dc:creator>Abdullah Nadeem</dc:creator>
      <pubDate>Mon, 07 Aug 2023 13:30:40 +0000</pubDate>
      <link>https://forem.com/abdnadeem382/importing-and-interacting-with-a-3d-model-in-react-with-react-three-fiber-2fni</link>
      <guid>https://forem.com/abdnadeem382/importing-and-interacting-with-a-3d-model-in-react-with-react-three-fiber-2fni</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;React-Three-Fiber&lt;/em&gt;&lt;/strong&gt; is a powerful library that combines the strengths of &lt;strong&gt;&lt;em&gt;React&lt;/em&gt;&lt;/strong&gt; with the capabilities of &lt;strong&gt;&lt;em&gt;Three.js&lt;/em&gt;&lt;/strong&gt;. In this article, we're going to examine a particular piece of code which demonstrates how to implement an interactive 3D Earth model using &lt;strong&gt;&lt;em&gt;React-Three-Fiber&lt;/em&gt;&lt;/strong&gt; and additional helper libraries such as &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt;. &lt;br&gt;
We will be assuming you already have a basic understanding of React and a new React application set up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install Required Packages
&lt;/h3&gt;

&lt;p&gt;First off, let's install the dependencies. Run the following command in your terminal (make sure you are in the root directory of your project):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install three @react-three/fiber @react-three/drei&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;strong&gt;&lt;em&gt;three&lt;/em&gt;&lt;/strong&gt; is the base library that &lt;strong&gt;&lt;em&gt;react-three-fiber&lt;/em&gt;&lt;/strong&gt; uses to create and render the 3D scene. &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt; is a helper library for &lt;strong&gt;&lt;em&gt;react-three-fiber&lt;/em&gt;&lt;/strong&gt; that provides a set of reusable components and functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Download a 3D Model
&lt;/h3&gt;

&lt;p&gt;Make sure you have downloaded a 3D model and placed it in the &lt;code&gt;public&lt;/code&gt; folder in the root directory of your project like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchjnd3semq9sa9ey37fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchjnd3semq9sa9ey37fu.png" alt="3d asset in the public folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can download one from &lt;a href="https://sketchfab.com/3d-models/popular" rel="noopener noreferrer"&gt;here.&lt;/a&gt;&lt;br&gt;
We will be using one of &lt;code&gt;.gltf&lt;/code&gt; format in this tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Setup the Canvas
&lt;/h3&gt;

&lt;p&gt;Now, import the &lt;code&gt;Canvas&lt;/code&gt; from &lt;strong&gt;&lt;em&gt;@react-three/fiber&lt;/em&gt;&lt;/strong&gt; in your React component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { Canvas } from '@react-three/fiber';&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Use the Canvas inside return like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { Canvas } from '@react-three/fiber';

const EarthCanvas = () =&amp;gt; {
  return (
    &amp;lt;Canvas frameloop="demand" camera={{ position: [-4, 3, 6], fov: 45, near: 0.1, far: 200 }}&amp;gt; 
    &amp;lt;/Canvas&amp;gt;
  );
};

export default EarthCanvas;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Canvas&lt;/code&gt; is taking a couple of props:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;frameloop&lt;/code&gt; prop set to &lt;code&gt;demand&lt;/code&gt; tells react-three-fiber to only re-render when state changes occur.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;camera&lt;/code&gt; prop is used to adjust the &lt;em&gt;&lt;strong&gt;camera's position&lt;/strong&gt;&lt;/em&gt; in the scene, the &lt;em&gt;&lt;strong&gt;field of view&lt;/strong&gt;&lt;/em&gt;, and the &lt;strong&gt;&lt;em&gt;near and far clipping planes&lt;/em&gt;&lt;/strong&gt;. You can adjust these to your liking.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 4: Import 3D Model in React Component
&lt;/h3&gt;

&lt;p&gt;To import the 3D model that we placed in the &lt;code&gt;public&lt;/code&gt; folder of our project, we will use a hook called &lt;code&gt;useGLTF&lt;/code&gt; from &lt;em&gt;&lt;strong&gt;@react-three/drei&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Use it like this on top of your component:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const earth = useGLTF('./planet/scene.gltf');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The useGLTF hook takes in as argument, the path to your 3D model &lt;code&gt;scene&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Render the 3D Model
&lt;/h3&gt;

&lt;p&gt;We can render the component by using the &lt;code&gt;primitive&lt;/code&gt; component. It is a low-level react-three-fiber component that can represent any three.js object. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;primitive object={earth.scene} scale={2.5} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;scale&lt;/code&gt; prop is used to adjust the model's size.&lt;/p&gt;

&lt;p&gt;Now, we can use this component in &lt;code&gt;App.jsx&lt;/code&gt; to see it in our browser. The return statement in &lt;code&gt;App.jsx&lt;/code&gt; will look like this: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  return (
    &amp;lt;div className="flex justify-center items-center h-screen w-screen"&amp;gt;
      &amp;lt;EarthCanvas /&amp;gt;
    &amp;lt;/div&amp;gt;
)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And voilà, that's all you need to render your 3D model in your React app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkynyxybzz3n46sh2xljb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkynyxybzz3n46sh2xljb.png" alt="Earth Model in app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are unable to see your model or if it's too dark, try adding an &lt;code&gt;ambientLight&lt;/code&gt; in the scene.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;ambientLight intensity={0.5} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are other options for adding light like &lt;code&gt;directionalLight&lt;/code&gt; and &lt;code&gt;spotLight&lt;/code&gt; as well. You can explore more about these &lt;a href="https://docs.pmnd.rs/react-three-fiber/getting-started/your-first-scene#adding-lights" rel="noopener noreferrer"&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Adding Interactivity
&lt;/h3&gt;

&lt;p&gt;To add interactivity to our model, we can use the &lt;code&gt;OrbitControls&lt;/code&gt; from &lt;strong&gt;&lt;em&gt;@react-three/drei&lt;/em&gt;&lt;/strong&gt; like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;OrbitControls autoRotate enableZoom={false} maxPolarAngle={Math.PI / 2} minPolarAngle={Math.PI / 2} enablePan={false} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;autoRotate&lt;/code&gt; prop enables the model to rotate automatically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;enableZoom&lt;/code&gt; and &lt;code&gt;enablePan&lt;/code&gt; props are set to false in this case to disable zooming and panning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;maxPolarAngle&lt;/code&gt; and &lt;code&gt;minPolarAngle&lt;/code&gt; props are used to restrict the vertical orbiting range of the camera around the 3D object.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will now be able to rotate the model with the cursor as well.&lt;/p&gt;

&lt;p&gt;So the final code of our EarthCanvas Component looks like this:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;import { Canvas } from '@react-three/fiber';&lt;br&gt;
import { OrbitControls, useGLTF } from '@react-three/drei';&lt;/p&gt;

&lt;p&gt;const EarthCanvas = () =&amp;gt; {&lt;br&gt;
  const earth = useGLTF('./planet/scene.gltf');&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &amp;lt;Canvas className="cursor-pointer" frameloop="demand" camera={{ position: [-4, 3, 6], fov: 45, near: 0.1, far: 200 }}&amp;gt;&lt;br&gt;
      &amp;lt;OrbitControls autoRotate enableZoom={false} maxPolarAngle={Math.PI / 2} minPolarAngle={Math.PI / 2} enablePan={false} /&amp;gt;&lt;br&gt;
      &amp;lt;primitive object={earth.scene} scale={2.5} /&amp;gt;&lt;br&gt;
    &amp;lt;/Canvas&amp;gt;&lt;br&gt;
  );&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;export default EarthCanvas;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Final Thoughts&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;That's the end of this tutorial. We rendered a 3D model in our React app using &lt;code&gt;threejs&lt;/code&gt;, &lt;code&gt;react-three-fiber&lt;/code&gt; and &lt;code&gt;react-three/drei&lt;/code&gt;, and added some interactivity to make it more fun.&lt;/p&gt;

&lt;p&gt;You can find the official docs &lt;a href="https://docs.pmnd.rs/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and explore the vast world of 3D rendering in React using &lt;strong&gt;&lt;em&gt;React-Three-Fiber&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>threejs</category>
      <category>3d</category>
    </item>
  </channel>
</rss>
