Below is a complete example using Three.js to create a 3D Earth animation that simulates seasonal change by using the Earth’s axial tilt and a fixed “sun” (directional light). In this example, the Earth is given a realistic texture, is tilted about 23.5° (its natural tilt), and slowly rotates so that the changing illumination mimics seasonal shifts. You can save this as an HTML file and open it in your browser.
Note: This example uses an Earth texture hosted online. You can replace the texture URL with your own if needed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>3D Earth Seasonal Change Animation</title>
<style>
body {
margin: 0;
background-color: #000;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<!-- Three.js from CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Optional: For orbit controls (to interact with the view) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/examples/js/controls/OrbitControls.js"></script>
<script>
// Scene, Camera and Renderer Setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(0, 0, 5);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add orbit controls (optional) for mouse interactivity
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// Create Earth Geometry and Material
const geometry = new THREE.SphereGeometry(1, 64, 64);
// Load texture for Earth surface (a sample daytime texture)
const textureLoader = new THREE.TextureLoader();
const earthTexture = textureLoader.load("https://threejs.org/examples/textures/earth_atmos_2048.jpg");
// Create material with the loaded Earth texture
const material = new THREE.MeshPhongMaterial({
map: earthTexture,
});
const earth = new THREE.Mesh(geometry, material);
// Apply Earth’s axial tilt (~23.5°)
earth.rotation.z = THREE.Math.degToRad(23.5);
scene.add(earth);
// Add a directional light simulating the Sun
const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
// Position the light so that it illuminates one pole more than the other.
directionalLight.position.set(5, 3, 5);
scene.add(directionalLight);
// Also add an ambient light for slight overall illumination
const ambientLight = new THREE.AmbientLight(0x333333);
scene.add(ambientLight);
// Animation: Earth rotation to simulate seasonal change in illumination
function animate() {
requestAnimationFrame(animate);
// Rotate the Earth on its Y-axis
// A slow rotation will simulate the change in the day and seasonal lighting effect.
earth.rotation.y += 0.002;
// Optionally, you can simulate seasonal variation by slightly changing the intensity
// or color of the directional light over time. For example:
// directionalLight.intensity = 1.0 + 0.2 * Math.sin(Date.now() * 0.001);
controls.update();
renderer.render(scene, camera);
}
animate();
// Handle Browser window resize
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
How It Works
-
Scene Setup:
A Three.js scene is created along with a perspective camera and a renderer sized to the window dimensions. OrbitControls are optionally added so you can interact with the view using your mouse.
-
Earth Model:
A sphere is created using SphereGeometry and textured using an Earth image. The mesh’s z-axis rotation is set to 23.5° to mimic the Earth’s axial tilt. This tilt is key to showing seasonal differences due to how sunlight hits the surface.
-
Lighting:
A directional light represents the Sun, and its position is chosen so that one hemisphere receives more direct light than the other. An ambient light is added to soften shadows and give a subtle fill.
-
Animation:
The Earth slowly rotates around its Y-axis. Because of the tilt and fixed light direction, different parts of the Earth receive varying amounts of sunlight over time—mimicking seasonal changes.
-
Interaction and Resize Handling:
The OrbitControls allow you to orbit around the Earth with your mouse, and the window resize listener readjusts the camera and renderer.
Feel free to customize textures, adjust light intensities, or add shaders for effects like snow on the poles during winter. Enjoy your 3D Earth seasonal animation!