<!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;
overflow: hidden;
font-family: Arial, sans-serif;
color: white;
}
canvas {
display: block;
}
.controls {
position: absolute;
bottom: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
z-index: 100;
}
.slider-container {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.slider-container label {
margin-right: 10px;
width: 120px;
}
input[type="range"] {
width: 150px;
}
.instructions {
position: absolute;
top: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
z-index: 100;
}
</style>
</head>
<body>
<div class="controls">
<div class="slider-container">
<label for="speedSlider">Simulation Speed:</label>
<input type="range" id="speedSlider" min="0.1" max="5" step="0.1" value="1">
<span id="speedValue">1.0x</span>
</div>
</div>
<div class="instructions">
<p>Controls: W (forward), A (left), S (backward), D (right)</p>
<p>Use slider to adjust simulation speed</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.js"></script>
<script>
// 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);
document.body.appendChild(renderer.domElement);
// Camera position
camera.position.z = 500;
camera.position.y = 200;
camera.lookAt(0, 0, 0);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const sunLight = new THREE.PointLight(0xffffff, 2, 0, 1);
scene.add(sunLight);
// Stars background
const starsGeometry = new THREE.BufferGeometry();
const starsMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 1 });
const starsVertices = [];
for (let i = 0; i < 10000; i++) {
const x = THREE.MathUtils.randFloatSpread(5000);
const y = THREE.MathUtils.randFloatSpread(5000);
const z = THREE.MathUtils.randFloatSpread(5000);
starsVertices.push(x, y, z);
}
starsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starsVertices, 3));
const stars = new THREE.Points(starsGeometry, starsMaterial);
scene.add(stars);
// Planets data
const planets = [
{ name: "Sun", radius: 30, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/sun.jpg", distance: 0, orbitSpeed: 0, rotationSpeed: 0.001, tilt: 0 },
{ name: "Mercury", radius: 5, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/mercury.jpg", distance: 60, orbitSpeed: 0.04, rotationSpeed: 0.004, tilt: 0.034 },
{ name: "Venus", radius: 8, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/venus.jpg", distance: 90, orbitSpeed: 0.015, rotationSpeed: 0.002, tilt: 3.1 },
{ name: "Earth", radius: 9, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/earth.jpg", distance: 130, orbitSpeed: 0.01, rotationSpeed: 0.01, tilt: 0.41 },
{ name: "Mars", radius: 7, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/mars.jpg", distance: 180, orbitSpeed: 0.008, rotationSpeed: 0.009, tilt: 0.44 },
{ name: "Jupiter", radius: 20, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/jupiter.jpg", distance: 250, orbitSpeed: 0.002, rotationSpeed: 0.02, tilt: 0.05 },
{ name: "Saturn", radius: 17, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/saturn.jpg", distance: 330, orbitSpeed: 0.0009, rotationSpeed: 0.018, tilt: 0.47, hasRings: true },
{ name: "Uranus", radius: 12, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/uranus.jpg", distance: 400, orbitSpeed: 0.0004, rotationSpeed: 0.012, tilt: 1.71 },
{ name: "Neptune", radius: 11, textureUrl: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/neptune.jpg", distance: 450, orbitSpeed: 0.0001, rotationSpeed: 0.011, tilt: 0.49 }
];
// Create planets
const textureLoader = new THREE.TextureLoader();
const planetObjects = [];
const orbitLines = [];
planets.forEach(planet => {
// Create planet
const texture = textureLoader.load(planet.textureUrl);
const geometry = new THREE.SphereGeometry(planet.radius, 32, 32);
const material = new THREE.MeshPhongMaterial({
map: texture,
shininess: planet.name === "Sun" ? 0 : 30
});
const mesh = new THREE.Mesh(geometry, material);
// Create orbit container
const orbitContainer = new THREE.Object3D();
scene.add(orbitContainer);
// Add planet to orbit container
orbitContainer.add(mesh);
// Position planet
mesh.position.x = planet.distance;
// Apply tilt
mesh.rotation.x = planet.tilt;
// Add rings for Saturn
if (planet.hasRings) {
const ringGeometry = new THREE.RingGeometry(planet.radius * 1.4, planet.radius * 2.2, 64);
const ringTexture = textureLoader.load('https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/planets/saturn-rings.png');
const ringMaterial = new THREE.MeshBasicMaterial({
map: ringTexture,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.9
});
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.rotation.x = Math.PI / 2;
mesh.add(ring);
}
// Create orbit line
if (planet.distance > 0) {
const orbitGeometry = new THREE.BufferGeometry();
const orbitMaterial = new THREE.LineBasicMaterial({ color: 0x444444 });
const orbitPoints = [];
const segments = 128;
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * Math.PI * 2;
orbitPoints.push(
Math.cos(angle) * planet.distance,
0,
Math.sin(angle) * planet.distance
);
}
orbitGeometry.setAttribute('position', new THREE.Float32BufferAttribute(orbitPoints, 3));
const orbitLine = new THREE.Line(orbitGeometry, orbitMaterial);
scene.add(orbitLine);
orbitLines.push(orbitLine);
}
// Special case for the sun - add glow and light
if (planet.name === "Sun") {
// Make sun emit light
mesh.material.emissive = new THREE.Color(0xffff00);
mesh.material.emissiveIntensity = 0.7;
}
planetObjects.push({
mesh,
orbitContainer,
data: planet,
angle: 0
});
});
// Controls
const keys = {
w: false,
a: false,
s: false,
d: false
};
const moveSpeed = 5;
let simulationSpeed = 1;
// Event listeners
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
window.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() === 'w') keys.w = true;
if (e.key.toLowerCase() === 'a') keys.a = true;
if (e.key.toLowerCase() === 's') keys.s = true;
if (e.key.toLowerCase() === 'd') keys.d = true;
});
window.addEventListener('keyup', (e) => {
if (e.key.toLowerCase() === 'w') keys.w = false;
if (e.key.toLowerCase() === 'a') keys.a = false;
if (e.key.toLowerCase() === 's') keys.s = false;
if (e.key.toLowerCase() === 'd') keys.d = false;
});
const speedSlider = document.getElementById('speedSlider');
const speedValue = document.getElementById('speedValue');
speedSlider.addEventListener('input', () => {
simulationSpeed = parseFloat(speedSlider.value);
speedValue.textContent = simulationSpeed.toFixed(1) + 'x';
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Update camera position based on WASD keys
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
const cameraRight = new THREE.Vector3();
cameraRight.crossVectors(camera.up, cameraDirection).normalize();
if (keys.w) {
camera.position.addScaledVector(cameraDirection, moveSpeed);
}
if (keys.s) {
camera.position.addScaledVector(cameraDirection, -moveSpeed);
}
if (keys.a) {
camera.position.addScaledVector(cameraRight, moveSpeed);
}
if (keys.d) {
camera.position.addScaledVector(cameraRight, -moveSpeed);
}
// Update planets
planetObjects.forEach(planetObj => {
// Update orbit
planetObj.angle += planetObj.data.orbitSpeed * simulationSpeed;
planetObj.orbitContainer.rotation.y = planetObj.angle;
// Update rotation
planetObj.mesh.rotation.y += planetObj.data.rotationSpeed * simulationSpeed;
});
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>