threejs realistic globe in typescript

To create a realistic globe in Three.js using TypeScript, you can follow these steps:

  1. First, you need to set up a basic Three.js scene with a camera and a renderer. You can use the following code to create a simple scene:
index.ts
import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 5);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
345 chars
10 lines
  1. Next, you need to create a sphere geometry to represent the globe. You can use the SphereGeometry class provided by Three.js to create a sphere. The SphereGeometry constructor takes three parameters: radius, widthSegments, and heightSegments.
index.ts
const globeGeometry = new THREE.SphereGeometry(1, 32, 32);
59 chars
2 lines
  1. After that, you need to load a texture map for the globe. You can use a texture of the Earth's surface, which you can download from any free image repository on the web. In this example, we will use a texture map called earth_texture.jpg.
index.ts
import earthTexture from './earth_texture.jpg';

const textureLoader = new THREE.TextureLoader();
const globeTexture = textureLoader.load(earthTexture);
153 chars
5 lines
  1. Once you have loaded the texture map, you need to create a material for the globe using the MeshBasicMaterial class. The MeshBasicMaterial class requires a map parameter, which should be set to the texture you loaded earlier.
index.ts
const globeMaterial = new THREE.MeshBasicMaterial({ map: globeTexture });
74 chars
2 lines
  1. Finally, you can create a mesh using the globe geometry and the globe material, and add it to the scene.
index.ts
const globeMesh = new THREE.Mesh(globeGeometry, globeMaterial);
scene.add(globeMesh);
86 chars
3 lines
  1. To make the globe look more realistic, you can add ambient and directional lights to the scene. You can use the AmbientLight and DirectionalLight classes provided by Three.js to create these lights.
index.ts
const ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(0, 1, 0);
scene.add(directionalLight);
218 chars
7 lines
  1. Finally, you need to update the Three.js scene in the render loop by calling the renderer.render() method. You should also rotate the globe mesh to make it spin.
index.ts
function render() {
  requestAnimationFrame(render);

  globeMesh.rotation.y += 0.01;

  renderer.render(scene, camera);
}

render();
134 chars
10 lines

This is the basic code that you can use to create a realistic globe in Three.js using TypeScript. You can customize the globe's appearance by adjusting the materials and lighting, and you can add other features to the scene to make it more interesting.

gistlibby LogSnag