Hello everyone. First time three.js user here. Im trying to make and interactive webapp for a hackathon but my sphere doesnt render and it just shows a black screen. Can someone pls tell me why this is happening. Sry if my eng is bad.
java script
// Create the scene, camera, and renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add ambient lighting
const AmbientLightlight = new THREE.AmbientLight(0xffffff, 1);
scene.add(ambientLight);
// Function to create planets and celestial bodies
function createCelestialBody(size, textureUrl, position) {
const geometry = new THREE.SphereGeometry(size, 20, 20);
const material = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load(textureUrl)
});
const body = new THREE.Mesh(geometry, material);
body.position.set(position.x, position.y, position.z);
scene.add(body);
return body;
}
// Add example planets (Earth and Mars)
const earth = createCelestialBody(1, ‘earth-daymap hosted at ImgBB — ImgBB’, { x: 5, y: 0, z: 0 });
const mars = createCelestialBody(0.8, ‘mars hosted at ImgBB — ImgBB’, { x: 20, y: 0, z: 0 });
// Animate the scene (simple rotation for the planets)
function animate() {
requestAnimationFrame(animate);
earth.rotation.y += 0.01; // Earth rotation
mars.rotation.y += 0.01; // Mars rotation
renderer.render(scene, camera);
}
camera.position.z = 20; // Set camera distance
camera.lookAt(15,0,0);
animate(); // Start animation
// Handle window resizing
window.addEventListener(‘resize’, () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});