Here's a complete implementation of a 3D Earth using Three.js with realistic textures and rotation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>3D Earth Globe</title>
<style>
body {
margin: 0;
background-color: #000;
overflow: hidden;
}
#globe-container {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div id="globe-container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
antialias: true
});
// Add renderer to DOM
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('globe-container').appendChild(renderer.domElement);
// Earth geometry and materials
const earthGeometry = new THREE.SphereGeometry(1, 32, 32);
const earthMaterial = new THREE.MeshPhongMaterial({
map: new THREE.TextureLoader().load('https://raw.githubusercontent.com/ArjunCodess/earth-globe-threejs/main/texture/earthmap.jpg'),
bumpMap: new THREE.TextureLoader().load('https://raw.githubusercontent.com/ArjunCodess/earth-globe-threejs/main/texture/earthbump.jpg'),
bumpScale: 0.05
});
const earth = new THREE.Mesh(earthGeometry, earthMaterial);
scene.add(earth);
// Lighting
const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
// Camera positioning
camera.position.z = 3;
// Animation loop
function animate() {
requestAnimationFrame(animate);
earth.rotation.y += 0.001;
renderer.render(scene, camera);
}
animate();
// Responsive handling
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
Key components explained:
-
Scene Setup
Uses Three.js to create a basic 3D scene with camera and renderer[1][14].
-
Earth Geometry
Creates a sphere geometry with SphereGeometry and applies realistic textures using TextureLoader[14][11].
-
Materials
Uses MeshPhongMaterial with:
- Diffuse map: Earth surface texture
- Bump map: Topography depth effect[1][14]
-
Lighting
Adds directional lighting for surface illumination[1][14].
-
Animation
Smooth rotation using requestAnimationFrame and incremental rotation[1][14].
-
Responsive Design
Handles window resizing with camera aspect ratio updates[14].
To enhance realism:
- Add cloud layer with semi-transparent texture
- Implement orbit controls for user interaction
- Add atmosphere shader for glow effect
- Include day/night cycle animation
Textures used are from NASA's Blue Marble collection (public domain). For production use, consider hosting textures locally or using CDN links with proper attribution[11][14].