-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
149 lines (122 loc) · 4.65 KB
/
main.js
File metadata and controls
149 lines (122 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as THREE from "three";
import SceneInit from "./lib/SceneInit";
import Planet from "./lib/Planets";
import { planets } from "./lib/PlanetData";
let multiplierSlider = document.getElementById("multiplierSlider");
let multiplier = parseFloat(multiplierSlider.value);
multiplierSlider.addEventListener("input", (event) => {
multiplier = parseFloat(event.target.value);
});
let scene = new SceneInit();
scene.initScene();
scene.animate();
// Create the star field
const starsGeometry = new THREE.BufferGeometry();
const starsCount = 50000; // Number of stars
const positions = new Float32Array(starsCount * 3); // x, y, z for each star
const sizes = new Float32Array(starsCount); // Size for each star
for (let i = 0; i < starsCount; i++) {
positions[i * 3] = (Math.random() - 0.5) * 1000; // x position
positions[i * 3 + 1] = (Math.random() - 0.5) * 1000; // y position
positions[i * 3 + 2] = (Math.random() - 0.5) * 1000; // z position
sizes[i] = Math.random() * 2 + 1;
}
starsGeometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
starsGeometry.setAttribute("size", new THREE.BufferAttribute(sizes, 1));
const starsMaterial = new THREE.PointsMaterial({ color: 0x888888, size: 0.5 });
const stars = new THREE.Points(starsGeometry, starsMaterial);
const sunGeometry = new THREE.SphereGeometry(15, 50, 50);
const sunTexture = new THREE.TextureLoader().load("/2k_sun.jpg");
const sunMaterial = new THREE.MeshBasicMaterial({
map: sunTexture,
});
const sunMesh = new THREE.Mesh(sunGeometry, sunMaterial);
const solarSystem = new THREE.Group();
solarSystem.add(sunMesh);
solarSystem.add(stars);
solarSystem.add(sunMesh);
scene.scene.add(solarSystem);
const mercury = new Planet(3.2, 28, "/2k_mercury.jpg");
const mercuryMesh = mercury.getMesh();
let mercurySystem = new THREE.Group();
mercurySystem.add(mercuryMesh);
const venus = new Planet(5.8, 44, "/2k_venus_surface.jpg");
const venusMesh = venus.getMesh();
let venusSystem = new THREE.Group();
venusSystem.add(venusMesh);
const earth = new Planet(6, 62, "/2k_earth_daymap.jpg");
const earthMesh = earth.getMesh();
let earthSystem = new THREE.Group();
earthSystem.add(earthMesh);
const mars = new Planet(4, 78, "/2k_mars.jpg");
const marsMesh = mars.getMesh();
let marsSystem = new THREE.Group();
marsSystem.add(marsMesh);
const jupiter = new Planet(12, 100, "/2k_jupiter.jpg");
const jupiterMesh = jupiter.getMesh();
let jupiterSystem = new THREE.Group();
jupiterSystem.add(jupiterMesh);
const saturn = new Planet(10, 138, "/2k_saturn.jpg");
const saturnMesh = saturn.getMesh();
let saturnSystem = new THREE.Group();
saturnSystem.add(saturnMesh);
const ringGeo = new THREE.RingGeometry(12, 20);
const ringMat = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load("/saturn_ring.png"),
side: THREE.DoubleSide,
});
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
saturnSystem.add(ringMesh);
ringMesh.position.set(saturnMesh.position.x, 0, 0);
ringMesh.rotation.x = -0.5 * Math.PI;
// Position and rotate the rings
//TODO: fix ring
ringMesh.rotation.x = Math.PI / 2; // Rotate to lay flat
ringMesh.position.x = saturnMesh.position.x;
saturnSystem.add(ringMesh);
const uranus = new Planet(7, 176, "/2k_uranus.jpg");
const uranusMesh = uranus.getMesh();
let uranusSystem = new THREE.Group();
uranusSystem.add(uranusMesh);
const neptune = new Planet(7, 200, "/2k_neptune");
const neptuneMesh = neptune.getMesh();
let neptuneSystem = new THREE.Group();
neptuneSystem.add(neptuneMesh);
solarSystem.add(
mercurySystem,
venusSystem,
earthSystem,
marsSystem,
jupiterSystem,
saturnSystem,
uranusSystem,
neptuneSystem
);
const animate = () => {
sunMesh.rotation.y += 0.001;
mercurySystem.rotation.y += planets[0].rotationSpeed * multiplier * 100;
venusSystem.rotation.y += planets[1].rotationSpeed * multiplier * -400;
earthSystem.rotation.y += planets[2].rotationSpeed * multiplier;
marsSystem.rotation.y += planets[3].rotationSpeed * multiplier;
jupiterSystem.rotation.y += planets[4].rotationSpeed * multiplier;
saturnSystem.rotation.y += planets[5].rotationSpeed * multiplier;
uranusSystem.rotation.y += planets[6].rotationSpeed * multiplier;
neptuneSystem.rotation.y += planets[7].rotationSpeed * multiplier;
// Orbital Tilt
mercurySystem.rotation.x = 0.000593;
venusSystem.rotation.x = 3.094;
earthSystem.rotation.x = 0.41;
marsSystem.rotation.x = 0.44;
jupiterSystem.rotation.x = 0.054;
saturnSystem.rotation.x = 0.466;
uranusSystem.rotation.x = 1.706;
neptuneSystem.rotation.x = 0.494;
stars.position.z += 0.01;
stars.position.y += 0.01;
stars.position.x += 0.01;
if (stars.position.z > 100) {
stars.position.z = 0;
}
requestAnimationFrame(animate);
};
animate();