Spaces:
Running

terrasphere-3d / index.html
MatMata's picture
Generate 3d Earth spining with clouds and detail, also background reacting.
21e2839 verified
Raw History Blame Contribute Delete
11.3 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TerraSphere 3D</title>
<link rel="icon" type="image/x-icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🌍</text></svg>">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vanta@latest/dist/vanta.globe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<style>
@keyframes float {
0%, 100% { transform: translateY(0px); }
50% { transform: translateY(-20px); }
}
.float-animation {
animation: float 6s ease-in-out infinite;
}
.glow {
box-shadow: 0 0 20px rgba(59, 130, 246, 0.5), 0 0 40px rgba(59, 130, 246, 0.3);
}
</style>
</head>
<body class="bg-black overflow-hidden">
<div id="vanta-bg" class="fixed inset-0 z-0"></div>
<div class="relative z-10 min-h-screen flex flex-col items-center justify-center">
<div class="text-center mb-8">
<h1 class="text-6xl md:text-8xl font-bold text-white mb-4 bg-gradient-to-r from-blue-400 via-green-400 to-blue-600 bg-clip-text text-transparent float-animation">
TerraSphere
</h1>
<p class="text-xl md:text-2xl text-gray-300 max-w-2xl mx-auto">
Experience our beautiful planet in stunning 3D detail
</p>
</div>
<div class="relative">
<canvas id="earth-canvas" class="w-96 h-96 md:w-[500px] md:h-[500px] rounded-full glow"></canvas>
<div class="absolute -bottom-16 left-1/2 transform -translate-x-1/2 flex space-x-4">
<button id="toggleClouds" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-3 rounded-full transition-all duration-300 flex items-center space-x-2 backdrop-blur-sm bg-opacity-80">
<i data-feather="cloud" class="w-5 h-5"></i>
<span>Toggle Clouds</span>
</button>
<button id="toggleRotation" class="bg-green-600 hover:bg-green-700 text-white px-6 py-3 rounded-full transition-all duration-300 flex items-center space-x-2 backdrop-blur-sm bg-opacity-80">
<i data-feather="refresh-cw" class="w-5 h-5"></i>
<span>Pause Rotation</span>
</button>
</div>
</div>
<div class="absolute bottom-8 left-8 text-white">
<div class="bg-black bg-opacity-50 backdrop-blur-md rounded-lg p-4">
<h3 class="text-lg font-semibold mb-2">Controls</h3>
<p class="text-sm text-gray-300">🖱️ Drag to rotate</p>
<p class="text-sm text-gray-300">🔍 Scroll to zoom</p>
</div>
</div>
</div>
<script>
// Initialize Vanta.js background
VANTA.GLOBE({
el: "#vanta-bg",
mouseControls: true,
touchControls: true,
gyroControls: false,
minHeight: 200.00,
minWidth: 200.00,
scale: 1.00,
scaleMobile: 1.00,
color: 0x3b82f6,
color2: 0x10b981,
size: 1.20,
backgroundColor: 0x0
});
// Three.js Earth setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
canvas: document.getElementById('earth-canvas'),
antialias: true,
alpha: true
});
const canvasSize = Math.min(window.innerWidth * 0.8, 500);
renderer.setSize(canvasSize, canvasSize);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Create Earth geometry
const earthGeometry = new THREE.SphereGeometry(2, 64, 64);
// Create Earth texture using canvas
const canvas = document.createElement('canvas');
canvas.width = 1024;
canvas.height = 512;
const ctx = canvas.getContext('2d');
// Draw Earth-like texture
const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, '#1e40af');
gradient.addColorStop(0.3, '#065f46');
gradient.addColorStop(0.6, '#16a34a');
gradient.addColorStop(0.8, '#ca8a04');
gradient.addColorStop(1, '#1e40af');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Add continents
ctx.fillStyle = '#16a34a';
for (let i = 0; i < 20; i++) {
ctx.beginPath();
ctx.arc(
Math.random() * canvas.width,
Math.random() * canvas.height,
Math.random() * 50 + 20,
0,
Math.PI * 2
);
ctx.fill();
}
const earthTexture = new THREE.CanvasTexture(canvas);
// Earth material
const earthMaterial = new THREE.MeshPhongMaterial({
map: earthTexture,
shininess: 30,
specular: 0x111111
});
const earth = new THREE.Mesh(earthGeometry, earthMaterial);
earth.castShadow = true;
earth.receiveShadow = true;
scene.add(earth);
// Cloud layer
const cloudGeometry = new THREE.SphereGeometry(2.02, 64, 64);
const cloudCanvas = document.createElement('canvas');
cloudCanvas.width = 1024;
cloudCanvas.height = 512;
const cloudCtx = cloudCanvas.getContext('2d');
// Create cloud texture
cloudCtx.fillStyle = 'rgba(255, 255, 255, 0)';
cloudCtx.fillRect(0, 0, cloudCanvas.width, cloudCanvas.height);
cloudCtx.fillStyle = 'rgba(255, 255, 255, 0.8)';
for (let i = 0; i < 50; i++) {
cloudCtx.beginPath();
cloudCtx.arc(
Math.random() * cloudCanvas.width,
Math.random() * cloudCanvas.height,
Math.random() * 30 + 10,
0,
Math.PI * 2
);
cloudCtx.fill();
}
const cloudTexture = new THREE.CanvasTexture(cloudCanvas);
const cloudMaterial = new THREE.MeshPhongMaterial({
map: cloudTexture,
transparent: true,
opacity: 0.6
});
const clouds = new THREE.Mesh(cloudGeometry, cloudMaterial);
scene.add(clouds);
// Atmosphere glow
const atmosphereGeometry = new THREE.SphereGeometry(2.1, 64, 64);
const atmosphereMaterial = new THREE.MeshPhongMaterial({
color: 0x44aaff,
transparent: true,
opacity: 0.15,
side: THREE.BackSide
});
const atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
scene.add(atmosphere);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 3, 5);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Stars
const starsGeometry = new THREE.BufferGeometry();
const starsVertices = [];
for (let i = 0; i < 10000; i++) {
const x = (Math.random() - 0.5) * 2000;
const y = (Math.random() - 0.5) * 2000;
const z = (Math.random() - 0.5) * 2000;
starsVertices.push(x, y, z);
}
starsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starsVertices, 3));
const starsMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 2 });
const stars = new THREE.Points(starsGeometry, starsMaterial);
scene.add(stars);
camera.position.z = 5;
// Mouse controls
let mouseX = 0;
let mouseY = 0;
let targetRotationX = 0;
let targetRotationY = 0;
let isMouseDown = false;
document.addEventListener('mousemove', (event) => {
if (isMouseDown) {
mouseX = (event.clientX / window.innerWidth) * 2 - 1;
mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
targetRotationX = mouseY * 0.5;
targetRotationY = mouseX * 0.5;
}
});
document.addEventListener('mousedown', () => isMouseDown = true);
document.addEventListener('mouseup', () => isMouseDown = false);
// Zoom control
document.addEventListener('wheel', (event) => {
camera.position.z += event.deltaY * 0.01;
camera.position.z = Math.max(3, Math.min(10, camera.position.z));
});
// Animation
let isRotating = true;
let showClouds = true;
function animate() {
requestAnimationFrame(animate);
if (isRotating) {
earth.rotation.y += 0.005;
clouds.rotation.y += 0.003;
}
// Smooth mouse rotation
earth.rotation.x += (targetRotationX - earth.rotation.x) * 0.05;
earth.rotation.y += (targetRotationY - earth.rotation.y) * 0.05;
clouds.rotation.x = earth.rotation.x;
clouds.rotation.y = earth.rotation.y + 0.2;
// Animate stars
stars.rotation.x += 0.0001;
stars.rotation.y += 0.0001;
renderer.render(scene, camera);
}
// Toggle controls
document.getElementById('toggleClouds').addEventListener('click', () => {
showClouds = !showClouds;
clouds.visible = showClouds;
const icon = showClouds ? 'cloud' : 'cloud-off';
document.querySelector('#toggleClouds i').setAttribute('data-feather', icon);
feather.replace();
});
document.getElementById('toggleRotation').addEventListener('click', () => {
isRotating = !isRotating;
const text = isRotating ? 'Pause Rotation' : 'Resume Rotation';
const icon = isRotating ? 'pause' : 'play';
document.querySelector('#toggleRotation span').textContent = text;
document.querySelector('#toggleRotation i').setAttribute('data-feather', icon);
feather.replace();
});
// Handle window resize
window.addEventListener('resize', () => {
const newSize = Math.min(window.innerWidth * 0.8, 500);
renderer.setSize(newSize, newSize);
});
animate();
feather.replace();
</script>
</body>
</html>