Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Creating 3D Globe using React-three-fiber
In this article, you will learn how to create a globe using react-threefiber. We will first make a sphere and then map a whole Earth map on it. This is an interesting texture loader feature using which we canmake any image to wrap over a sphere as texture. So, let's get started!
Example
First, download important libraries −
npm i --save @react-three/fiber three
This library react-three/fiber will be used to add webGL renderer to the website and to connect threejs and React.
Download an image of Earth map and place it in the “src” folder. We have named the image file as "world-map.gif".
Add the following lines of code in App.js −
import React, { useRef } from "react";
import { Canvas,useFrame,useLoader } from "@reactthree/fiber";
import * as THREE from "three";
import earthImg from './world-map.gif'
import "./App.css";
const Sphere=()=>{
const base=new THREE.TextureLoader().load(earthImg)
const ref=useRef()
useFrame(() => (ref.current.rotation.x=ref.current.rotation.y += 0.01))
)
}
export default function App(){
return (
);
};
Explanation
Here we first created a Sphere and loaded a texture. Then we wrapped the texture over the sphere.
meshBasicMaterial which is use to design our geometrical structure.
The Position argument simply positions the object.
Output
It will produce the following output −
