1+ // Setup the scene, camera, and renderer
2+ const scene = new THREE . Scene ( ) ;
3+ const camera = new THREE . PerspectiveCamera ( 75 , window . innerWidth / window . innerHeight , 0.1 , 1000 ) ;
4+ const renderer = new THREE . WebGLRenderer ( ) ;
5+
6+ renderer . setSize ( window . innerWidth , window . innerHeight ) ;
7+ document . getElementById ( 'container' ) . appendChild ( renderer . domElement ) ;
8+
9+ // Create the Sun (as a light source)
10+ const sunLight = new THREE . PointLight ( 0xFFFFFF , 1 , 100 ) ;
11+ sunLight . position . set ( 0 , 0 , 0 ) ; // Sun at the center
12+ scene . add ( sunLight ) ;
13+
14+ // Create Sun as a yellow sphere
15+ const sunGeometry = new THREE . SphereGeometry ( 1.5 , 32 , 32 ) ;
16+ const sunMaterial = new THREE . MeshBasicMaterial ( { color : 0xFFFF00 } ) ;
17+ const sun = new THREE . Mesh ( sunGeometry , sunMaterial ) ;
18+ sun . position . set ( 0 , 0 , 0 ) ;
19+ scene . add ( sun ) ;
20+
21+ // Create Earth (as a mesh with a blue material)
22+ const earthGeometry = new THREE . SphereGeometry ( 1 , 32 , 32 ) ;
23+ const earthMaterial = new THREE . MeshBasicMaterial ( { color : 0x0000FF } ) ;
24+ const earth = new THREE . Mesh ( earthGeometry , earthMaterial ) ;
25+ earth . position . set ( 5 , 0 , 0 ) ; // Place Earth 5 units away from the Sun
26+ scene . add ( earth ) ;
27+
28+ // Create Mars (as a red sphere)
29+ const marsGeometry = new THREE . SphereGeometry ( 0.8 , 32 , 32 ) ;
30+ const marsMaterial = new THREE . MeshBasicMaterial ( { color : 0xFF5733 } ) ;
31+ const mars = a Mesh ( marsGeometry , marsMaterial ) ;
32+ mars . position . set ( 8 , 0 , 0 ) ; // Place Mars 8 units away from the Sun
33+ scene . add ( mars ) ;
34+
35+ // Set the camera position
36+ camera . position . z = 15 ;
37+
38+ // Add controls (orbit around the scene)
39+ const controls = new THREE . OrbitControls ( camera , renderer . domElement ) ;
40+
41+ // NASA NEO API setup with your API key
42+ const apiKey = 'pszf1uRds6MCHSyjlavhuCk9SPOnMCbYig4HNur3' ; // Your NASA API key
43+ const apiUrl = `https://api.nasa.gov/neo/rest/v1/feed?api_key=${ apiKey } ` ;
44+
45+ // Fetch NEO data from NASA's NEO API
46+ fetch ( apiUrl )
47+ . then ( response => response . json ( ) )
48+ . then ( data => {
49+ const neoObjects = data . near_earth_objects ;
50+ Object . keys ( neoObjects ) . forEach ( date => {
51+ neoObjects [ date ] . forEach ( neo => {
52+ // Create NEO object (small red sphere representing an asteroid)
53+ const neoGeometry = new THREE . SphereGeometry ( 0.2 , 32 , 32 ) ;
54+ const neoMaterial = new THREE . MeshBasicMaterial ( { color : 0xFF0000 } ) ; // Red for NEOs
55+ const neoMesh = new THREE . Mesh ( neoGeometry , neoMaterial ) ;
56+
57+ // Randomly position the NEO in space (for demo purposes)
58+ neoMesh . position . set ( Math . random ( ) * 10 - 5 , Math . random ( ) * 10 - 5 , Math . random ( ) * 10 - 5 ) ;
59+ scene . add ( neoMesh ) ;
60+ } ) ;
61+ } ) ;
62+ } )
63+ . catch ( error => console . error ( 'Error fetching NEO data:' , error ) ) ;
64+
65+ // Animation function to render the scene
66+ function animate ( ) {
67+ requestAnimationFrame ( animate ) ;
68+
69+ // Earth's orbit motion around the Sun
70+ earth . position . x = 5 * Math . cos ( Date . now ( ) * 0.001 ) ;
71+ earth . position . z = 5 * Math . sin ( Date . now ( ) * 0.001 ) ;
72+
73+ // Mars' orbit motion around the Sun
74+ mars . position . x = 8 * Math . cos ( Date . now ( ) * 0.0008 ) ;
75+ mars . position . z = 8 * Math . sin ( Date . now ( ) * 0.0008 ) ;
76+
77+ controls . update ( ) ; // Update camera controls
78+
79+ renderer . render ( scene , camera ) ; // Render the scene
80+ }
81+
82+ // Start the animation loop
83+ animate ( ) ;
84+ console . log ( "app.js is loaded" ) ;
0 commit comments