This program is a WebGL-based solar system simulation built using Three.js. It features:
- View and move around the solar system using OrbitControls.
- Dynamic camera movement via keyboard inputs (
W,A,S,D, Arrow keys). - Toggle rotation and orbit animations on/off using the
Ekey. - Reset camera position with the
Rkey. - Simulate planetary rotations and orbital movements, with orbital tracks toggled by the
Okey. - Realistic textures for planets.
The simulation runs in a browser and renders the 3D scene on an HTML <canvas> element.
The program requires the following:
- Three.js: For 3D graphics and rendering.
- OrbitControls: For interactive camera controls.
- Node.js Modules: Including
gl-matrixfor mathematical computations. - Babel: For ES6+ JavaScript compatibility.
- Ensure the project includes proper textures in the
textures/directory (e.g.,stars.jpg,sun2.jpeg,earth2.jpeg). - Install dependencies via npm:
npm install
- Build the project using:
npx webpack
- Serve the project locally using:
Access the application through the localhost URL provided by Webpack.
npx webpack serve
- Scene: A
THREE.Sceneholds all objects in the simulation. - Camera: A
THREE.PerspectiveCameraobserves the scene. - Renderer: A
THREE.WebGLRendererdraws the scene onto the canvas. - OrbitControls: Enables interactive scene exploration.
- Load Textures: Import planet textures (e.g., Sun, Earth, Moon).
- Create Planets:
- Use
THREE.SphereGeometryfor the shape. - Apply textures using
THREE.MeshPhongMaterial.
- Use
- Add Lighting:
- Include ambient and point lights to mimic sunlight.
- Group Planets:
- Use
THREE.Object3D()to group planets and simulate relationships (e.g., Moon orbits Earth).
- Use
- Animation:
- Use
requestAnimationFramefor rendering and animating orbits.
- Use
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Smooth camera movements
controls.minDistance = 10; // Limit zoom-in
controls.maxDistance = 100; // Limit zoom-outconst earthGeometry = new THREE.SphereGeometry(1, 32, 32);
const earthMaterial = new THREE.MeshPhongMaterial({
map: textureLoader.load("./textures/earth2.jpeg")
});
const earth = new THREE.Mesh(earthGeometry, earthMaterial);
const earthGroup = new THREE.Object3D(); // Group for Earth and its orbit
earthGroup.add(earth);const pointLight = new THREE.PointLight(0xffffff, 20, 200, 0.5);
pointLight.position.set(0, 0, 0);
pointLight.castShadow = true;
scene.add(ambientLight, pointLight);angleEarth += 0.01;
earthGroup.position.set(
15 * Math.cos(angleEarth), // Semi-major axis for Earth
10 * Math.sin(angleEarth), // Semi-minor axis for Earth
0
);- Initial state with all planets in their correct positions.
- Ability to rotate and move the system using mouse and keyboard shortcuts.
- Toggle orbit lines visibility using the
Okey.
