<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Solar System Simulation</title>
<style>
body {
margin: 0;
padding: 0;
background: #000;
font-family: Arial, sans-serif;
overflow: hidden;
}
#container {
width: 100vw;
height: 100vh;
}
.controls {
position: absolute;
top: 20px;
left: 20px;
color: white;
background: rgba(0, 0, 0, 0.8);
padding: 15px;
border-radius: 10px;
z-index: 100;
min-width: 250px;
}
.speed-control {
margin-bottom: 15px;
}
.speed-control label {
display: block;
margin-bottom: 5px;
}
#speedSlider {
width: 200px;
}
.view-controls {
margin-bottom: 15px;
}
.view-controls button {
background: rgba(255, 255, 255, 0.2);
color: white;
border: 1px solid rgba(255, 255, 255, 0.3);
padding: 5px 10px;
margin: 2px;
border-radius: 5px;
cursor: pointer;
font-size: 12px;
}
.view-controls button:hover {
background: rgba(255, 255, 255, 0.3);
}
.instructions {
font-size: 12px;
line-height: 1.4;
}
.planet-info {
position: absolute;
top: 20px;
right: 20px;
color: white;
background: rgba(0, 0, 0, 0.8);
padding: 15px;
border-radius: 10px;
max-width: 200px;
font-size: 12px;
}
#planetDetails {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid rgba(255, 255, 255, 0.3);
}
</style>
</head>
<body>
<div id="container"></div>
<div class="controls">
<div class="speed-control">
<label for="speedSlider">Simulation Speed: <span id="speedValue">1.0x</span></label>
<input type="range" id="speedSlider" min="0" max="5" step="0.1" value="1">
</div>
<div class="view-controls">
<button onclick="resetView()">Reset View</button>
<button onclick="followEarth()">Follow Earth</button>
<button onclick="topView()">Top View</button>
<button onclick="sideView()">Side View</button>
</div>
<div class="instructions">
<strong>Controls:</strong><br>
WASD: Move camera<br>
Mouse: Look around<br>
Scroll: Zoom in/out<br>
Click planet: Focus on it
</div>
</div>
<div class="planet-info">
<h3>3D Solar System</h3>
<p>Interactive 3D simulation with realistic planet textures and orbital mechanics.</p>
<div id="planetDetails">
<strong>Click on a planet to see details</strong>
</div>
</div>
<!-- Three.js CDN -->
<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, 10000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById('container').appendChild(renderer.domElement);
// Controls
const speedSlider = document.getElementById('speedSlider');
const speedValue = document.getElementById('speedValue');
const planetDetails = document.getElementById('planetDetails');
let simulationSpeed = 1;
let followTarget = null;
let cameraTarget = new THREE.Vector3();
// Input handling
const keys = {};
let mouseX = 0, mouseY = 0;
let isMouseDown = false;
// Camera controls
const cameraSpeed = 2;
const mouseSensitivity = 0.002;
// Raycaster for planet clicking
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
// Create starfield
function createStarfield() {
const starsGeometry = new THREE.BufferGeometry();
const starsMaterial = new THREE.PointsMaterial({ color: 0xFFFFFF, size: 2 });
const starsVertices = [];
for (let i = 0; i < 10000; i++) {
const x = (Math.random() - 0.5) * 4000;
const y = (Math.random() - 0.5) * 4000;
const z = (Math.random() - 0.5) * 4000;
starsVertices.push(x, y, z);
}
starsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starsVertices, 3));
const stars = new THREE.Points(starsGeometry, starsMaterial);
scene.add(stars);
}
// Create textured sphere
function createTexturedSphere(radius, color, textureUrl = null) {
const geometry = new THREE.SphereGeometry(radius, 32, 32);
let material;
if (textureUrl) {
const texture = new THREE.TextureLoader().load(textureUrl);
material = new THREE.MeshPhongMaterial({ map: texture });
} else {
material = new THREE.MeshPhongMaterial({ color: color });
}
const sphere = new THREE.Mesh(geometry, material);
sphere.castShadow = true;
sphere.receiveShadow = true;
return sphere;
}
// Create the Sun
function createSun() {
const sunGeometry = new THREE.SphereGeometry(20, 32, 32);
const sunMaterial = new THREE.MeshBasicMaterial({
color: 0xFFD700,
emissive: 0xFFAA00,
emissiveIntensity: 0.3
});
const sun = new THREE.Mesh(sunGeometry, sunMaterial);
// Add sun light
const sunLight = new THREE.PointLight(0xFFFFFF, 2, 2000);
sunLight.position.set(0, 0, 0);
sunLight.castShadow = true;
sunLight.shadow.mapSize.width = 2048;
sunLight.shadow.mapSize.height = 2048;
scene.add(sunLight);
// Add sun glow
const glowGeometry = new THREE.SphereGeometry(25, 32, 32);
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0xFFAA00,
transparent: true,
opacity: 0.3
});
const glow = new THREE.Mesh(glowGeometry, glowMaterial);
sun.add(glow);
scene.add(sun);
return sun;
}
// Planet data with realistic properties
const planetData = [
{
name: 'Mercury',
distance: 80,
size: 2,
speed: 0.02,
color: 0x8C7853,
info: 'Closest planet to the Sun. Extremely hot days, freezing nights.'
},
{
name: 'Venus',
distance: 110,
size: 3.5,
speed: 0.015,
color: 0xFFC649,
info: 'Hottest planet due to greenhouse effect. Thick toxic atmosphere.'
},
{
name: 'Earth',
distance: 150,
size: 4,
speed: 0.01,
color: 0x6B93D6,
info: 'Our home planet. Only known planet with life. Has one moon.'
},
{
name: 'Mars',
distance: 200,
size: 3,
speed: 0.008,
color: 0xCD5C5C,
info: 'The Red Planet. Has polar ice caps and the largest volcano in the solar system.'
},
{
name: 'Jupiter',
distance: 300,
size: 12,
speed: 0.005,
color: 0xD8CA9D,
info: 'Largest planet. Gas giant with Great Red Spot storm and 79+ moons.'
},
{
name: 'Saturn',
distance: 400,
size: 10,
speed: 0.003,
color: 0xFAD5A5,
info: 'Famous for its ring system. Less dense than water.'
},
{
name: 'Uranus',
distance: 500,
size: 7,
speed: 0.002,
color: 0x4FD0E7,
info: 'Ice giant that rotates on its side. Has faint rings.'
},
{
name: 'Neptune',
distance: 600,
size: 7,
speed: 0.001,
color: 0x4B70DD,
info: 'Windiest planet with speeds up to 2,100 km/h. Deep blue color.'
}
];
// Create planets
const planets = [];
const planetMeshes = [];
function createPlanets() {
planetData.forEach((data, index) => {
const planet = createTexturedSphere(data.size, data.color);
planet.userData = { ...data, angle: Math.random() * Math.PI * 2 };
// Create orbit line
const orbitGeometry = new THREE.RingGeometry(data.distance - 0.5, data.distance + 0.5, 64);
const orbitMaterial = new THREE.MeshBasicMaterial({
color: 0xFFFFFF,
transparent: true,
opacity: 0.1,
side: THREE.DoubleSide
});
const orbit = new THREE.Mesh(orbitGeometry, orbitMaterial);
orbit.rotation.x = Math.PI / 2;
scene.add(orbit);
// Special case for Saturn's rings
if (data.name === 'Saturn') {
const ringGeometry = new THREE.RingGeometry(data.size * 1.2, data.size * 2, 32);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xDAA520,
transparent: true,
opacity: 0.6,
side: THREE.DoubleSide
});
const rings = new THREE.Mesh(ringGeometry, ringMaterial);
rings.rotation.x = Math.PI / 2;
planet.add(rings);
}
// Add Earth's moon
if (data.name === 'Earth') {
const moon = createTexturedSphere(0.8, 0x888888);
moon.userData = { distance: 8, speed: 0.1, angle: 0 };
planet.userData.moon = moon;
scene.add(moon);
}
planets.push(planet.userData);
planetMeshes.push(planet);
scene.add(planet);
});
}
// Update planet positions
function updatePlanets() {
planetMeshes.forEach((planetMesh, index) => {
const planet = planets[index];
planet.angle += planet.speed * simulationSpeed;
const x = Math.cos(planet.angle) * planet.distance;
const z = Math.sin(planet.angle) * planet.distance;
planetMesh.position.set(x, 0, z);
planetMesh.rotation.y += 0.01 * simulationSpeed;
// Update Earth's moon
if (planet.moon) {
planet.moon.angle += planet.moon.speed * simulationSpeed;
const moonX = x + Math.cos(planet.moon.angle) * planet.moon.distance;
const moonZ = z + Math.sin(planet.moon.angle) * planet.moon.distance;
planet.moon.position.set(moonX, 0, moonZ);
}
});
}
// Camera follow system
function updateCamera() {
if (followTarget) {
const targetPos = followTarget.position.clone();
targetPos.y += 20;
targetPos.z += 30;
camera.position.lerp(targetPos, 0.05);
camera.lookAt(followTarget.position);
return;
}
// Manual camera controls
const moveVector = new THREE.Vector3();
if (keys['w']) moveVector.z -= cameraSpeed;
if (keys['s']) moveVector.z += cameraSpeed;
if (keys['a']) moveVector.x -= cameraSpeed;
if (keys['d']) moveVector.x += cameraSpeed;
// Apply camera rotation to movement
moveVector.applyQuaternion(camera.quaternion);
camera.position.add(moveVector);
// Mouse look
if (isMouseDown) {
camera.rotation.y -= mouseX * mouseSensitivity;
camera.rotation.x -= mouseY * mouseSensitivity;
camera.rotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, camera.rotation.x));
}
}
// View presets
function resetView() {
followTarget = null;
camera.position.set(0, 100, 300);
camera.lookAt(0, 0, 0);
}
function followEarth() {
followTarget = planetMeshes[2]; // Earth is index 2
}
function topView() {
followTarget = null;
camera.position.set(0, 800, 0);
camera.lookAt(0, 0, 0);
}
function sideView() {
followTarget = null;
camera.position.set(800, 0, 0);
camera.lookAt(0, 0, 0);
}
// Planet clicking
function onMouseClick(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(planetM