Here's a complete implementation for a 3D Earth using Three.js:
HTML Structure
<!DOCTYPE html>
<html>
<head>
<title>3D Earth</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
// JavaScript implementation
</script>
</body>
</html>
CSS Styling
body {
margin: 0;
overflow: hidden;
width: 100vw;
height: 100vh;
}
canvas {
display: block;
}
JavaScript Implementation
// Initialize Three.js components
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 });
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// Earth configuration
const EARTH_RADIUS = 2;
const TEXTURE_URL = 'https://threejs.org/examples/textures/planets/earth_atmos_2048.jpg';
// Create Earth mesh
const geometry = new THREE.SphereGeometry(EARTH_RADIUS, 32, 32);
const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load(TEXTURE_URL);
const material = new THREE.MeshPhongMaterial({ map: texture });
const earth = new THREE.Mesh(geometry, material);
scene.add(earth);
// Add lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
const pointLight = new THREE.PointLight(0xffffff, 1);
pointLight.position.set(5, 3, 5);
scene.add(ambientLight);
scene.add(pointLight);
// Camera positioning
camera.position.z = 5;
// Renderer setup
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Rotate Earth
earth.rotation.y += 0.001;
controls.update();
renderer.render(scene, camera);
}
// Handle window resizing
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
Key Features
-
3D Earth Model
Uses SphereGeometry with Earth texture mapping[1][7][8]
-
Interactive Controls
Implements OrbitControls for rotation/zoom[3][7][10]
-
Realistic Lighting
Combines ambient and directional lighting for depth[1][8]
-
Responsive Design
Handles window resizing automatically[7][8]
-
Continuous Animation
Smooth rotation using requestAnimationFrame[1][8]
Optional Enhancements
-
Add Cloud Layer
const cloudGeometry = new THREE.SphereGeometry(EARTH_RADIUS + 0.1, 32, 32);
const cloudMaterial = new THREE.MeshPhongMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.5
});
const clouds = new THREE.Mesh(cloudGeometry, cloudMaterial);
scene.add(clouds);
-
Add Atmosphere Effect
const atmosphereGeometry = new THREE.SphereGeometry(EARTH_RADIUS + 0.2, 32, 32);
const atmosphereMaterial = new THREE.ShaderMaterial({
vertexShader: 'varying vec3 vNormal; void main() { vNormal = normalize(normalMatrix * normal); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
fragmentShader: 'varying vec3 vNormal; void main() { float intensity = pow(0.8 - dot(vNormal, vec3(0,0,1)), 12.0); gl_FragColor = vec4(0.3, 0.5, 1.0, intensity * 0.3); }'
});
const atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
scene.add(atmosphere);
Usage Notes
-
Texture Requirements
Replace TEXTURE_URL with your Earth texture map[1][8][10]
-
Performance Optimization
Adjust geometry segments (32,32) for balance between quality/performance[7][8]
-
Lighting Adjustments
Modify light positions/intensities to achieve desired visual effects[1][8]
This implementation provides a solid foundation for a 3D Earth visualization that can be extended with additional features like:
- Real-time weather data integration
- Geographic marker placement
- Day/night cycles
- Cloud movement animations[7][10]