<!
let scene, camera, renderer, controls;
let celestialObjects = [];
let raycaster, mouse;
let infoBox = document.getElementById('info-box');
function init() {
// Create the scene
scene = new THREE.Scene();
// Create the camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 50;
// Create the renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add orbit controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.1;
controls.screenSpacePanning = false;
// Add lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
// Raycaster and mouse for detecting clicks
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
window.addEventListener('click', onMouseClick, false);
// Create celestial objects
createStars();
createPlanets();
// Handle window resize
window.addEventListener('resize', onWindowResize, false);
// Start animation loop
animate();
}
function createStars() {
const starGeometry = new THREE.SphereGeometry(0.2, 12, 12);
const starMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
for (let i = 0; i < 500; i++) {
const star = new THREE.Mesh(starGeometry, starMaterial);
star.position.set(
(Math.random() - 0.5) * 200,
(Math.random() - 0.5) * 200,
(Math.random() - 0.5) * 200
);
scene.add(star);
}
}
function createPlanets() {
const planetGeometry = new THREE.SphereGeometry(3, 32, 32);
const planetMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
let planet = new THREE.Mesh(planetGeometry, planetMaterial);
planet.position.set(10, 5, 0);
planet.name = "Planet X";
celestialObjects.push(planet);
scene.add(planet);
}
function onMouseClick(event) {
// Convert mouse position to normalized device coordinates (-1 to +1)
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(celestialObjects);
if (intersects.length > 0) {
displayInfo(intersects[0].object);
}
}
function displayInfo(object) {
let infoContent = `<h3>${object.name}</h3><p>Description of ${object.name}</p>`;
infoBox.innerHTML = infoContent;
infoBox.style.display = 'block';
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// Initialize the cosmos scene
init();