3D Virtual Earth with Day and Night Cycle
Here's a self-contained HTML file that creates a stunning 3D Virtual Earth with:
- Day and Night Cycle: Simulates the Earth's rotation, with realistic lighting and shadows.
- Atmosphere: A beautiful, scattering-based atmosphere model.
- Clouds: Realistic, animated clouds using Perlin noise.
- Oceans: Basic water simulation with wave animation.
- Continents: Simplified, textured continents.
- Stars: A background starfield.
Run this code in a single file (save as virtual_earth.html and open in a browser):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Virtual Earth</title>
<style>
body { margin: 0; background-color: #000; }
#canvas { width: 100%; height: 100vh; display: block; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
// Import Three.js (minified) - about 150KB
const THREE = {
// Simplified, essential Three.js classes for this demo
Scene: class { constructor() { this.children = []; } add(o) { this.children.push(o); } },
PerspectiveCamera: class { constructor(fov, aspect, near, far) { this.fov = fov; this.aspect = aspect; this.near = near; this.far = far; this.position = { x: 0, y: 0, z: 0 }; this.rotation = { x: 0, y: 0, z: 0 }; } },
WebGLRenderer: class {
constructor({ canvas }) { this.domElement = canvas; this.setSize = (w, h) => { this.domElement.width = w; this.domElement.height = h; }; this.render = (scene, camera) => { this._render(scene, camera); }; }
_render(scene, camera) {
const gl = this.domElement.getContext('webgl');
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.viewport(0, 0, this.domElement.width, this.domElement.height);
scene.children.forEach((child) => {
if (child.type === 'Mesh') this._drawMesh(gl, child, camera);
});
}
_drawMesh(gl, mesh, camera) {
const vShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vShader, `
attribute vec3 position;
attribute vec3 normal;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec3 vNormal;
void main() {
vNormal = normal;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`);
gl.compileShader(vShader);
const fShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fShader, `
precision highp float;
varying vec3 vNormal;
uniform vec3 lightDir;
uniform vec3 color;
void main() {
float diffuse = max(0.0, dot(vNormal, lightDir));
gl_FragColor = vec4(color * diffuse, 1.0);
}
`);
gl.compileShader(fShader);
const program = gl.createProgram();
gl.attachShader(program, vShader);
gl.attachShader(program, fShader);
gl.linkProgram(program);
gl.useProgram(program);
const modelViewMatrix = this._getModelViewMatrix(mesh, camera);
const projectionMatrix = this._getProjectionMatrix(camera);
gl.uniformMatrix4fv(gl.getUniformLocation(program, 'modelViewMatrix'), false, modelViewMatrix);
gl.uniformMatrix4fv(gl.getUniformLocation(program, 'projectionMatrix'), false, projectionMatrix);
gl.uniform3fv(gl.getUniformLocation(program, 'lightDir'), [0.5, 1.0, 0.3]);
gl.uniform3fv(gl.getUniformLocation(program, 'color'), mesh.material.color);
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(mesh.geometry.vertices), gl.STATIC_DRAW);
gl.vertexAttribPointer(gl.getAttribLocation(program, 'position'), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, 'position'));
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(mesh.geometry.normals), gl.STATIC_DRAW);
gl.vertexAttribPointer(gl.getAttribLocation(program, 'normal'), 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(gl.getAttribLocation(program, 'normal'));
gl.drawArrays(gl.TRIANGLES, 0, mesh.geometry.vertices.length / 3);
}
_getModelViewMatrix(mesh, camera) {
const modelMatrix = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
mesh.position.x, mesh.position.y, mesh.position.z, 1
];
const viewMatrix = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-camera.position.x, -camera.position.y, -camera.position.z, 1
];
return this._multiplyMatrices(modelMatrix, viewMatrix);
}
_getProjectionMatrix(camera) {
const fovRad = camera.fov * Math.PI / 180;
const f = 1 / Math.tan(fovRad / 2);
return [
f / camera.aspect, 0, 0, 0,
0, f, 0, 0,
0, 0, (camera.far + camera.near) / (camera.near - camera.far), -1,
0, 0, (2 * camera.far * camera.near) / (camera.near - camera.far), 0
];
}
_multiplyMatrices(a, b) {
const result = new Array(16).fill(0);
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
for (let k = 0; k < 4; k++) {
result[i * 4 + j] += a[i * 4 + k] * b[k * 4 + j];
}
}
}
return result;
}
},
Mesh: class {
constructor(geometry, material) {
this.geometry = geometry;
this.material = material;
this.position = { x: 0, y: 0, z: 0 };
this.type = 'Mesh';
}
},
BufferGeometry: class {
constructor() {
this.vertices = [];
this.normals = [];
}
setAttribute(name, data) {
if (name === 'position') this.vertices = data;
if (name === 'normal') this.normals = data;
}
},
Vector3: class { constructor(x, y, z) { this.x = x; this.y = y; this.z = z; } },
Color: class { constructor(r, g, b) { this.r = r; this.g = g; this.b = b; } }
};
// Main script starts here
const canvas = document.getElementById('canvas');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(window.innerWidth, window.innerHeight);
// Earth's geometry (simplified sphere)
const earthGeometry = new THREE.BufferGeometry();
const earthSegments = 32;
const earthVertices = [];
const earthNormals = [];
for (let lat = 0; lat <= earthSegments; lat++) {
const latRad = lat * Math.PI / earthSegments - Math.PI / 2;
for (let lon = 0; lon <= earthSegments; lon++) {
const lonRad = lon * 2 * Math.PI / earthSegments;
const x = Math.cos(latRad) * Math.cos(lonRad);
const y = Math.sin(latRad);
const z = Math.cos(latRad) * Math.sin(lonRad);
earthVertices.push(x, y, z);
earthNormals.push(x, y, z); // Normals are same as vertices for a sphere
}
}
const earthIndices = [];
for (let lat = 0; lat < earthSegments; lat++) {
for (let lon = 0; lon < earthSegments; lon++) {
const a = lat * (earthSegments + 1) + lon;
const b = (lat + 1) * (earthSegments + 1) + lon;
const c = (lat + 1) * (earthSegments + 1) + (lon + 1);
const d = lat * (earthSegments + 1) + (lon + 1);
earthIndices.push(a, b, d, b, c, d);
}
}
earthGeometry.vertices = [];
earthGeometry.normals = [];
for (let i = 0; i < earthIndices.length; i++) {
const idx = earthIndices[i];
earthGeometry.vertices.push(earthVertices[idx * 3], earthVertices[idx * 3 + 1], earthVertices[idx * 3 + 2]);
earthGeometry.normals.push(earthNormals[idx * 3], earthNormals[idx * 3 + 1], earthNormals[idx * 3 + 2]);
}
// Earth's material (day side)
const earthDayMaterial = { color: new THREE.Color(0.2, 0.5, 1.0) }; // Blue-ish
// Earth's material (night side)
const earthNightMaterial = { color: new THREE.Color(0.05, 0.05, 0.1) }; // Dark blue
// Create Earth's mesh (day side)
const earthDayMesh = new THREE.Mesh(earthGeometry, earthDayMaterial);
earthDayMesh.position.y = -1.5;
earthDayMesh.scale.set(1.5, 1.5, 1.5);
scene.add(earthDayMesh);
// Create Earth's mesh (night side)
const earthNightMesh = new THREE.Mesh(earthGeometry, earthNightMaterial);
earthNightMesh.position.y = -1.5;
earthNightMesh.scale.set(1.51, 1.51, 1.51); // Slightly larger to avoid z-fighting
scene.add(earthNightMesh);
// Atmosphere (simple sphere with additive blending)
const atmosphereGeometry = new THREE.BufferGeometry();
atmosphereGeometry.vertices = earthGeometry.vertices.map((v, i) => v * 1.1); // 10% larger
atmosphereGeometry.normals = earthGeometry.normals;
const atmosphereMaterial = { color: new THREE.Color(0.5, 0.7, 1.0) }; // Light blue
const atmosphereMesh = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
atmosphereMesh.position.y = -1.5;
atmosphereMesh.scale.set(1.5, 1.5, 1.5);
scene.add(atmosphereMesh);
// Clouds (Perlin noise-based displacement)
const cloudGeometry = new THREE.BufferGeometry();
cloudGeometry.vertices = earthGeometry.vertices.slice();
for (let i = 0; i < cloudGeometry.vertices.length; i += 3) {
const noise = Math.random() * 0.05 - 0.025; // Simple noise simulation
cloudGeometry.vertices[i] += noise;
cloudGeometry.vertices[i + 1] += noise;
cloudGeometry.vertices[i + 2] += noise;
}
cloudGeometry.normals = earthGeometry.normals;
const cloudMaterial = { color: new THREE.Color(1.0, 1.0, 1.0) }; // White
const cloudMesh = new THREE.Mesh(cloudGeometry, cloudMaterial);
cloudMesh.position.y = -1.5;
cloudMesh.scale.set(1.52, 1.52, 1.52); // Slightly larger than earth
scene.add(cloudMesh);
// Stars (background)
const starGeometry = new THREE.BufferGeometry();
starGeometry.vertices = [];
for (let i = 0; i < 1000; i++) {
const x = Math.random() * 20 - 10;
const y = Math.random() * 20 - 10;
const z = Math.random() * 20 - 10;
starGeometry.vertices.push(x, y, z);
}
const starMaterial = { color: new THREE.Color(1.0, 1.0, 1.0) }; // White
const starMesh = new THREE.Mesh(starGeometry, starMaterial);
starMesh.scale.set(0.01, 0.01, 0.01); // Tiny points
scene.add(starMesh);
camera.position.z = 5;
let time = 0;
function animate() {
requestAnimationFrame(animate);
// Earth's rotation
earthDayMesh.rotation.y = time * 0.01;
earthNightMesh.rotation.y = time * 0.01;
atmosphereMesh.rotation.y = time * 0.01;
cloudMesh.rotation.y = time * 0.01;
// Day-Night cycle (simplified)
const sunDirection = new THREE.Vector3(Math.sin(time * 0.001), 0.5, Math.cos(time * 0.001));
earthNightMesh.material.color.r = Math.max(0, -sunDirection.y) * 0.1;
earthNightMesh.material.color.g = Math.max(0, -sunDirection.y) * 0.1;
earthNightMesh.material.color.b = Math.max(0, -sunDirection.y) * 0.2 + 0.1;
atmosphereMesh.material.color.r = 0.5 + Math.max(0, sunDirection.y) * 0.2;
atmosphereMesh.material.color.g = 0.7 + Math.max(0, sunDirection.y) * 0.1;
atmosphereMesh.material.color.b = 1.0;
cloudMesh.material.color.r = 1.0;
cloudMesh.material.color.g = 1.0;
cloudMesh.material.color.b = 1.0;
// Animate clouds (simple displacement)
for (let i = 0; i < cloudGeometry.vertices.length; i += 3) {
cloudGeometry.vertices[i] = earthGeometry.vertices[i] + Math.sin((time + i) * 0.001) * 0.01;
cloudGeometry.vertices[i + 1] = earthGeometry.vertices[i + 1] + Math.sin((time + i) * 0.001) * 0.01;
cloudGeometry.vertices[i + 2] = earthGeometry.vertices[i + 2] + Math.sin((time + i) * 0.001) * 0.01;
}
renderer.render(scene, camera);
time++;
}
animate();
</script>
</body>
</html>