Sure! Below is an HTML file that uses JavaScript to create a 3D visualization of the solar system. It includes the features you requested:
- Zoom in/out on planets: You can use the mouse wheel to zoom in and out.
- Stars: The background is populated with stars.
To use it, simply save the code below into a file with a .html extension (e.g., solar_system.html) and open it in a modern web browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Solar System Visualization</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Include Three.js via CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r148/three.min.js"></script>
<!-- OrbitControls -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r148/examples/js/controls/OrbitControls.min.js"></script>
<script>
// Scene, Camera, Renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000); // Black background
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 50, 150);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// OrbitControls for zooming and rotating
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Smooth controls
controls.dampingFactor = 0.05;
controls.enablePan = false;
// Ambient Light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.2);
scene.add(ambientLight);
// Point Light representing the Sun
const sunLight = new THREE.PointLight(0xffffff, 2, 0);
sunLight.position.set(0, 0, 0);
scene.add(sunLight);
// Sun
const sunGeometry = new THREE.SphereGeometry(10, 32, 32);
const sunMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
const sun = new THREE.Mesh(sunGeometry, sunMaterial);
scene.add(sun);
// Stars Background
function createStars() {
const starGeometry = new THREE.BufferGeometry();
const starMaterial = new THREE.PointsMaterial({ color: 0xffffff });
const starVertices = [];
for (let i = 0; i < 10000; i++) {
const x = THREE.MathUtils.randFloatSpread(2000);
const y = THREE.MathUtils.randFloatSpread(2000);
const z = THREE.MathUtils.randFloatSpread(2000);
starVertices.push(x, y, z);
}
starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
const stars = new THREE.Points(starGeometry, starMaterial);
scene.add(stars);
}
createStars();
// Planets Data
const planets = [
{ name: 'Mercury', color: 0xaaaaaa, size: 0.5, distance: 15, speed: 0.04 },
{ name: 'Venus', color: 0xffcc66, size: 1.2, distance: 25, speed: 0.015 },
{ name: 'Earth', color: 0x2233ff, size: 1.3, distance: 35, speed: 0.01 },
{ name: 'Mars', color: 0xff0000, size: 0.8, distance: 45, speed: 0.008 },
{ name: 'Jupiter', color: 0xff9966, size: 5, distance: 60, speed: 0.002 },
{ name: 'Saturn', color: 0xffcc66, size: 4, distance: 75, speed: 0.001 },
{ name: 'Uranus', color: 0x66ccff, size: 3, distance: 85, speed: 0.0005 },
{ name: 'Neptune', color: 0x3333ff, size: 3, distance: 95, speed: 0.0003 }
];
// Create Planets
planets.forEach(planet => {
const geometry = new THREE.SphereGeometry(planet.size, 32, 32);
const material = new THREE.MeshStandardMaterial({ color: planet.color });
const mesh = new THREE.Mesh(geometry, material);
mesh.position.x = planet.distance;
planet.mesh = mesh;
scene.add(mesh);
// Orbit Path
const orbitGeometry = new THREE.RingGeometry(planet.distance - 0.05, planet.distance + 0.05, 64);
const orbitMaterial = new THREE.MeshBasicMaterial({ color: 0x888888, side: THREE.DoubleSide });
const orbit = new THREE.Mesh(orbitGeometry, orbitMaterial);
orbit.rotation.x = Math.PI / 2;
scene.add(orbit);
});
// Animation Loop
function animate() {
requestAnimationFrame(animate);
// Rotate planets around the sun
planets.forEach(planet => {
const time = Date.now() * planet.speed * 0.0001;
planet.mesh.position.x = Math.cos(time) * planet.distance;
planet.mesh.position.z = Math.sin(time) * planet.distance;
});
controls.update();
renderer.render(scene, camera);
}
animate();
// Handle Window Resize
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>
Explanation of the Code:
- Three.js Library: The code uses the Three.js library for 3D rendering. It's included via CDN links in the
<script> tags.
- Scene, Camera, Renderer: Sets up the basic components required for rendering a 3D scene.
- OrbitControls: Allows for zooming and rotating the view with the mouse.
- Zoom In/Out: Use the mouse wheel to zoom in and out on the planets.
- Lighting:
- Ambient Light: Provides a base light level for the scene.
- Point Light (Sun): Acts as a light source at the sun's position.
- Sun: A yellow sphere at the center of the solar system.
- Stars Background: Randomly positions points in space to simulate stars.
- Planets:
- Each planet is created with its own size, color, distance from the sun, and orbital speed.
- Orbit Paths: Rings around the sun to visualize the planets' orbits.
- Animation Loop: Updates the positions of the planets to simulate orbiting around the sun.
- Window Resize Handling: Ensures the scene adjusts if the browser window size changes.
Usage Instructions:
- Save the File: Copy the code into a text editor and save it with an
.html extension, such as solar_system.html.
- Open in Browser: Open the file in a modern web browser (e.g., Chrome, Firefox, Edge).
- Interact:
- Zoom: Use the mouse wheel to zoom in and out.
- Rotate: Click and drag to rotate the view.
- Reset View: Refresh the page to reset.
Note on Performance:
- This visualization should run smoothly on most modern computers. If you experience performance issues, you can reduce the number of stars in the
createStars function by decreasing the iteration count in the loop.
Customizations:
- Adding More Planets or Objects: You can add more celestial bodies (like moons or asteroids) by adding entries to the
planets array and adjusting their parameters.
- Textures: For a more realistic look, you can apply textures to the planets and the sun by using images mapped onto the sphere geometries.
References:
Feel free to modify and expand upon this code to add more features or improve the visualization!