html Copy code
<!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</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="solarSystem"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-5, 5, 5, -5, 0.1, 1000);
camera.position.set(5, 5, 5);
camera.lookAt(scene.position);
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('solarSystem') });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(0, 0, 0);
scene.add(pointLight);
const solarSystem = new THREE.Group();
scene.add(solarSystem);
const sunGeometry = new THREE.SphereGeometry(1, 32, 32);
const sunMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const sun = new THREE.Mesh(sunGeometry, sunMaterial);
solarSystem.add(sun);
const planets = [
{ size: 0.2, distance: 2, color: 0xaaaaaa },
{ size: 0.3, distance: 3, color: 0x00ff00 },
{ size: 0.4, distance: 4, color: 0x0000ff },
{ size: 0.6, distance: 5, color: 0xff0000 },
{ size: 0.5, distance: 6, color: 0xffa500 },
{ size: 0.4, distance: 7, color: 0x8a2be2 },
{ size: 0.3, distance: 8, color: 0x87ceeb },
{ size: 0.2, distance: 9, color: 0x4682b4 }
];
planets.forEach(planet => {
const planetGroup = new THREE.Group();
const orbit = new THREE.RingGeometry(planet.distance - 0.01, planet.distance + 0.01, 64);
const orbitMaterial = new THREE.MeshBasicMaterial({ color: 0x888888, side: THREE.DoubleSide });
const orbitMesh = new THREE.Mesh(orbit, orbitMaterial);
orbitMesh.rotation.x = Math.PI / 2;
solarSystem.add(orbitMesh);
const planetGeometry = new THREE.SphereGeometry(planet.size, 32, 32);
const planetMaterial = new THREE.MeshLambertMaterial({ color: planet.color });
const planetMesh = new THREE.Mesh(planetGeometry, planetMaterial);
planetMesh.position.x = planet.distance;
planetGroup.add(planetMesh);
solarSystem.add(planetGroup);
planetGroup.orbitRadius = planet.distance;
});
function animate() {
requestAnimationFrame(animate);
solarSystem.rotation.y += 0.002;
solarSystem.children.forEach((object, index) => {
if (object instanceof THREE.Group && index > 0) {
object.rotation.y += 0.01;
}
});
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>