-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolar.js
More file actions
206 lines (177 loc) · 5.76 KB
/
solar.js
File metadata and controls
206 lines (177 loc) · 5.76 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls';
// Create a scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 5000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(ambientLight);
// SUN
const sunScaledRadius = 5; // this will help in calculations
const sunGeometry = new THREE.SphereGeometry(sunScaledRadius, 32, 32);
const sunMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
const sun = new THREE.Mesh(sunGeometry, sunMaterial);
scene.add(sun);
// directional light coming from the sun
const sunLight = new THREE.DirectionalLight(0xffffff, 1);
sunLight.position.set(0, 0, 0);
scene.add(sunLight);
// PLANETS
const planets = [
{
name: 'mercury',
color: 0xb2b2b2,
rotationDays: 88,
distanceFromSun: 58,
realRadius: 2439.7
},
{
name: 'venus',
color: 0xe1c16e,
rotationDays: 224,
distanceFromSun: 108,
realRadius: 6051.8
},
{
name: 'earth',
color: 0x2e8b57,
rotationDays: 365,
distanceFromSun: 150,
realRadius: 6371
},
{
name: 'mars',
color: 0xb7410e,
rotationDays: 687,
distanceFromSun: 228,
realRadius: 3389.5
},
{
name: 'jupiter',
color: 0xd9a066,
rotationDays: 4333,
distanceFromSun: 778,
realRadius: 69911
},
{
name: 'saturn',
color: 0xf4c542,
rotationDays: 10759,
distanceFromSun: 1433,
realRadius: 58232
},
{
name: 'uranus',
color: 0x70a4ff,
rotationDays: 30687,
distanceFromSun: 2870,
realRadius: 25362
},
{
name: 'neptune',
color: 0x2b65ec,
rotationDays: 60190,
distanceFromSun: 4500,
realRadius: 24622
}
];
const planetMeshes = [];
const sunRadius = 696340; // sun radius in km
// normal circle geometry fills the circle, we need a ring
function createOrbit(radius) {
const points = [];
const segments = 128;
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * Math.PI * 2;
points.push(new THREE.Vector3(Math.cos(angle) * radius, 0, Math.sin(angle) * radius));
}
const orbitGeometry = new THREE.BufferGeometry().setFromPoints(points);
const orbitMaterial = new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.1 });
const orbitLine = new THREE.LineLoop(orbitGeometry, orbitMaterial);
return orbitLine;
}
// place planets in scene
planets.forEach(planet => {
// normalize planet values (distance from sun, rotation speed, etc)
//
// speed -> lets say 365 days is 3.65*2=7.3 seconds
const speed = (planet.rotationDays * 7.3) / 365;
// Rotation speeds (based on 2π / period)
const rotationSpeed = (2 * Math.PI) / speed;
// Distance from sun (normalized)
const drawDistance = (planet.distanceFromSun / 5) + 1 + sunScaledRadius;
// scale planet radius
const radius = (planet.realRadius / sunRadius) * 50;
console.log(
planet.name,
"rotationSpeed: " + rotationSpeed,
"distance: " + drawDistance,
"radius: " + radius
);
const planetGeometry = new THREE.SphereGeometry(radius, 32, 32 );
const planetMaterial = new THREE.MeshBasicMaterial( { color: planet.color } );
const planetMesh = new THREE.Mesh( planetGeometry, planetMaterial );
planetMesh.position.set(drawDistance, 0, 0);
scene.add(planetMesh);
planetMeshes.push({ mesh: planetMesh, rotationSpeed: rotationSpeed, drawDistance: drawDistance, radius: radius });
// draw orbit
const orbitMesh = createOrbit(drawDistance);
scene.add(orbitMesh);
});
// for asteroid and kupier belt
function createBelt(beltConfig) {
const drawDistanceMin = (beltConfig.distanceFromSunMin / 5) + 1 + sunScaledRadius;
const drawDistanceMax = (beltConfig.distanceFromSunMax / 5) + 1 + sunScaledRadius;
const asteroidGroup = new THREE.Group();
for (let i = 0; i < beltConfig.count; i++) {
const angle = Math.random() * Math.PI * 2; // Random angle around the Sun
const distance = THREE.MathUtils.lerp(drawDistanceMin, drawDistanceMax, Math.random());
const height = (Math.random() - 0.5) * 10; // Random height offset for thickness
const asteroidGeometry = new THREE.SphereGeometry(Math.random() * beltConfig.scaleMultiplier, 8, 8);
const asteroidMaterial = new THREE.MeshBasicMaterial({
color: 0x888888,
transparent: true,
opacity: 0.6
});
const asteroid = new THREE.Mesh(asteroidGeometry, asteroidMaterial);
// Position asteroid along a circular orbit with slight vertical variation
asteroid.position.set(Math.cos(angle) * distance, height, Math.sin(angle) * distance);
asteroidGroup.add(asteroid);
}
return asteroidGroup;
}
const asteroidBelt = {
distanceFromSunMin: 300,
distanceFromSunMax: 400,
count: 5000,
scaleMultiplier: 0.2
};
const asteroidBeltMesh = createBelt(asteroidBelt);
scene.add(asteroidBeltMesh);
const kupierBelt = {
distanceFromSunMin: 4800,
distanceFromSunMax: 7000,
count: 5000,
scaleMultiplier: 0.5
};
const kupierBeltMesh = createBelt(kupierBelt);
scene.add(kupierBeltMesh);
// default camera position
camera.position.z = 20;
// setup controls
const controls = new OrbitControls(camera, renderer.domElement);
// animation loop
function animate() {
const time = performance.now() / 1000; // seconds
// any updates to the scene, camera, or objects go here
planetMeshes.forEach(planet => {
planet.mesh.position.x = Math.cos(time * planet.rotationSpeed) * planet.drawDistance;
planet.mesh.position.z = Math.sin(time * planet.rotationSpeed) * planet.drawDistance;
});
controls.update();
renderer.render( scene, camera );
}
renderer.setAnimationLoop( animate );