-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-solar-system.js
More file actions
507 lines (412 loc) · 15.2 KB
/
script-solar-system.js
File metadata and controls
507 lines (412 loc) · 15.2 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
class SolarSystemScene {
constructor(containerId) {
this.container = document.getElementById(containerId);
this.scene = new THREE.Scene();
this.camera = null;
this.renderer = null;
this.sunGroup = null;
this.sun = null;
this.earth = null;
this.earthOrbitGroup = null;
this.moon = null;
this.moonOrbitGroup = null;
this.satellites = [];
this.grid = null;
this.animationPaused = false;
this.sunRotationSpeed = 0.0005;
this.earthOrbitSpeed = 0.0002;
this.moonOrbitSpeed = 0.0006;
this.satelliteSpeedMultiplier = 1;
// FPS limiting variables
this.targetFPS = 120;
this.frameInterval = 1000 / this.targetFPS;
this.lastFrameTime = 0;
this.deltaTime = 0;
// Performance monitoring
this.lastTime = performance.now();
this.frameCount = 0;
this.fps = 0;
this.fpsUpdateInterval = 1000;
this.lastFPSUpdate = 0;
this.zoomLevel = 10;
this.config = {
sunRadius: 8,
sunPoints: 5000,
earthRadius: 2,
earthPoints: 1000,
earthOrbitRadius: 15,
moonRadius: 0.5,
moonPoints: 250,
moonOrbitRadius: 4,
gridSize: 40,
gridDivisions: 50,
earthTilt: (23.5 * Math.PI) / 180,
satelliteConfigs: [
{ radius: 12.2, inclination: 85, color: 0x00ff00, speed: 0.00016 },
{ radius: 9.8, inclination: 10, color: 0x00ff00, speed: 0.00026 },
{ radius: 10.3, inclination: 35, color: 0x00ff00, speed: 0.0002 },
{ radius: 11.7, inclination: 50, color: 0x00ff00, speed: 0.00018 },
{ radius: 10.1, inclination: 65, color: 0x00ff00, speed: 0.00022 },
{ radius: 11.9, inclination: 25, color: 0x00ff00, speed: 0.00019 },
{ radius: 9.9, inclination: 5, color: 0x00ff00, speed: 0.00027 },
{ radius: 12.5, inclination: 95, color: 0x00ff00, speed: 0.00014 },
{ radius: 10.6, inclination: 40, color: 0x00ff00, speed: 0.0002 },
{ radius: 11.3, inclination: 70, color: 0x00ff00, speed: 0.00017 },
{ radius: 10, inclination: 0, color: 0x00ff00, speed: 0.0002 },
{ radius: 11, inclination: 45, color: 0x00ff00, speed: 0.00018 },
{ radius: 12, inclination: 90, color: 0x00ff00, speed: 0.00015 },
{ radius: 10.5, inclination: 30, color: 0x00ff00, speed: 0.00022 },
{ radius: 11.5, inclination: 60, color: 0x00ff00, speed: 0.00017 },
{ radius: 9.5, inclination: 15, color: 0x00ff00, speed: 0.00025 },
{ radius: 10.8, inclination: 75, color: 0x00ff00, speed: 0.00019 },
{ radius: 11.2, inclination: 20, color: 0x00ff00, speed: 0.00021 },
],
};
this.init();
}
calculateResponsiveZoom() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Detect device type based on screen width and touch capability
const isMobile = width <= 768 && ('ontouchstart' in window || navigator.maxTouchPoints > 0);
const isTablet = width > 768 && width <= 1024 && ('ontouchstart' in window || navigator.maxTouchPoints > 0);
let baseZoom;
if (isMobile) {
// Mobile devices - zoom out more to fit everything
if (width <= 480) {
baseZoom = 20; // Small phones
} else {
baseZoom = 18; // Larger phones
}
} else if (isTablet) {
// Tablet devices - moderate zoom
baseZoom = 16;
} else {
// Desktop - original zoom level
baseZoom = 10;
}
// Adjust based on aspect ratio
// For very wide screens, zoom out a bit more
if (aspect > 2) {
baseZoom *= 1.2;
} else if (aspect < 0.8) {
// For tall screens (portrait), zoom out more
baseZoom *= 1.3;
}
// Adjust for very small screens
if (width < 360 || height < 640) {
baseZoom *= 1.2;
}
return baseZoom;
}
init() {
this.setupScene();
this.createSun();
this.createEarth();
this.createMoon();
this.createSatellites();
this.setupEventListeners();
this.setupLighting();
this.animate(0);
}
setupScene() {
const aspect = window.innerWidth / window.innerHeight;
// Calculate responsive zoom level
this.zoomLevel = this.calculateResponsiveZoom();
const d = this.zoomLevel;
this.camera = new THREE.OrthographicCamera(
-d * aspect, d * aspect, d, -d, 1, 1000
);
// Adjust camera position based on aspect ratio for better framing
const cameraDistance = 20;
this.camera.position.set(cameraDistance, cameraDistance, cameraDistance);
// Look at the center of the scene (where sun is positioned)
this.camera.lookAt(0, 2, 0); // Sun is at y=2
this.renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
stencil: false,
depth: true,
preserveDrawingBuffer: true,
});
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setClearColor(0x000000, 0);
this.renderer.sortObjects = false;
this.container.appendChild(this.renderer.domElement);
}
createSun() {
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array(this.config.sunPoints * 3);
for (let i = 0; i < this.config.sunPoints; i++) {
const theta = Math.random() * 2 * Math.PI;
const phi = Math.acos(2 * Math.random() - 1);
const idx = i * 3;
vertices[idx] = this.config.sunRadius * Math.sin(phi) * Math.cos(theta);
vertices[idx + 1] =
this.config.sunRadius * Math.sin(phi) * Math.sin(theta);
vertices[idx + 2] = this.config.sunRadius * Math.cos(phi);
}
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
// Optimize geometry
geometry.computeBoundingSphere();
const material = new THREE.PointsMaterial({
// color: 0xffa500,
color: 0x00ff00,
size: 0.2,
sizeAttenuation: true,
transparent: false,
});
this.sun = new THREE.Points(geometry, material);
this.sun.position.y = 2;
this.sun.frustumCulled = true;
this.sunGroup = new THREE.Group();
this.sunGroup.add(this.sun);
this.scene.add(this.sunGroup);
}
createEarth() {
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array(this.config.earthPoints * 3);
for (let i = 0; i < this.config.earthPoints; i++) {
const theta = Math.random() * 2 * Math.PI;
const phi = Math.acos(2 * Math.random() - 1);
const idx = i * 3;
vertices[idx] = this.config.earthRadius * Math.sin(phi) * Math.cos(theta);
vertices[idx + 1] =
this.config.earthRadius * Math.sin(phi) * Math.sin(theta);
vertices[idx + 2] = this.config.earthRadius * Math.cos(phi);
}
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
// Optimize geometry
geometry.computeBoundingSphere();
const material = new THREE.PointsMaterial({
color: 0x00ff00,
size: 0.15,
sizeAttenuation: true,
transparent: false,
});
this.earth = new THREE.Points(geometry, material);
this.earth.frustumCulled = true;
// Add Earth's axial tilt
const earthGroup = new THREE.Group();
earthGroup.rotation.z = this.config.earthTilt;
earthGroup.add(this.earth);
this.earthOrbitGroup = new THREE.Group();
earthGroup.position.set(this.config.earthOrbitRadius, 3, 0);
this.earthOrbitGroup.add(earthGroup);
this.scene.add(this.earthOrbitGroup);
this.createEarthOrbitLine();
}
createMoon() {
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array(this.config.moonPoints * 3);
for (let i = 0; i < this.config.moonPoints; i++) {
const theta = Math.random() * 2 * Math.PI;
const phi = Math.acos(2 * Math.random() - 1);
const idx = i * 3;
vertices[idx] = this.config.moonRadius * Math.sin(phi) * Math.cos(theta);
vertices[idx + 1] =
this.config.moonRadius * Math.sin(phi) * Math.sin(theta);
vertices[idx + 2] = this.config.moonRadius * Math.cos(phi);
}
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.computeBoundingSphere();
const material = new THREE.PointsMaterial({
color: 0x00ff00,
size: 0.1,
sizeAttenuation: true,
transparent: false,
});
this.moon = new THREE.Points(geometry, material);
this.moon.frustumCulled = true;
// create moon orbit group with slight inclination
this.moonOrbitGroup = new THREE.Group();
this.moonOrbitGroup.rotation.z = (5 * Math.PI) / 180; // 5-degree inclination
this.moon.position.set(this.config.moonOrbitRadius, 0, 0);
this.moonOrbitGroup.add(this.moon);
// add moon orbit to Earth (not to Earth's orbit group)
this.earthOrbitGroup.children[0].add(this.moonOrbitGroup);
this.createMoonOrbitLine();
}
createEarthOrbitLine() {
const geometry = new THREE.BufferGeometry();
const points = new Float32Array(303); // 101 points * 3 coordinates
for (let i = 0; i <= 100; i++) {
const angle = (i / 100) * Math.PI * 2;
const idx = i * 3;
points[idx] = this.config.earthOrbitRadius * Math.cos(angle);
points[idx + 1] = 3;
points[idx + 2] = this.config.earthOrbitRadius * Math.sin(angle);
}
geometry.setAttribute("position", new THREE.BufferAttribute(points, 3));
// Optimize geometry
geometry.computeBoundingSphere();
const material = new THREE.LineBasicMaterial({
color: 0x00ff00,
opacity: 0.3,
transparent: true,
});
const line = new THREE.Line(geometry, material);
line.frustumCulled = true;
this.scene.add(line);
}
createMoonOrbitLine() {
const orbitPoints = 100;
const points = [];
for (let i = 0; i <= orbitPoints; i++) {
const angle = (i / orbitPoints) * Math.PI * 2;
const x = this.config.moonOrbitRadius * Math.cos(angle);
const z = this.config.moonOrbitRadius * Math.sin(angle);
const y = 0;
points.push(x, y, z);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0x00ff00,
opacity: 0.5,
transparent: true,
linewidth: 1,
});
const orbitLine = new THREE.Line(geometry, material);
this.moonOrbitGroup.add(orbitLine);
}
createSatellites() {
// Create a single geometry and material to reuse
const geometry = new THREE.SphereGeometry(0.04, 6, 6);
geometry.computeBoundingSphere();
this.config.satelliteConfigs.forEach((cfg, index) => {
const material = new THREE.MeshBasicMaterial({
color: cfg.color,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.x = cfg.radius;
mesh.frustumCulled = true;
const group = new THREE.Group();
group.add(mesh);
group.rotation.x = (cfg.inclination * Math.PI) / 180;
group.rotation.y = (index * 45 * Math.PI) / 180;
group.position.y = 2;
this.scene.add(group);
this.satellites.push({
group,
mesh,
config: cfg,
angle: Math.random() * Math.PI * 2,
});
});
}
setupLighting() {
const ambientLight = new THREE.AmbientLight(0xffffff, 0.2);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.3);
directionalLight.position.set(10, 10, 10);
// Add point light at sun position for more realistic lighting
const sunLight = new THREE.PointLight(0xffa500, 2, 100);
sunLight.position.set(0, 2, 0);
this.scene.add(ambientLight, directionalLight, sunLight);
}
animateSatellites(deltaMultiplier) {
this.satellites.forEach((sat) => {
sat.angle +=
sat.config.speed * this.satelliteSpeedMultiplier * deltaMultiplier;
sat.mesh.position.x = sat.config.radius * Math.cos(sat.angle);
sat.mesh.position.z = sat.config.radius * Math.sin(sat.angle);
});
}
animate = (currentTime) => {
requestAnimationFrame(this.animate);
// Skip rendering if tab is not visible
if (document.hidden) {
return;
}
// Calculate time since last frame
this.deltaTime = currentTime - this.lastFrameTime;
// Only render if enough time has passed for target FPS
if (this.deltaTime >= this.frameInterval) {
// Calculate delta multiplier for smooth animation regardless of framerate
const deltaMultiplier = this.deltaTime / 16.67; // Normalize to 60fps baseline
if (!this.animationPaused) {
this.sunGroup.rotation.y += this.sunRotationSpeed * deltaMultiplier;
this.earthOrbitGroup.rotation.y += this.earthOrbitSpeed * deltaMultiplier;
this.earthOrbitGroup.children[0].children[0].rotation.y += 0.001 * deltaMultiplier; // Earth rotation
this.moonOrbitGroup.rotation.y += this.moonOrbitSpeed * deltaMultiplier;
this.animateSatellites(deltaMultiplier);
}
this.renderer.render(this.scene, this.camera);
// Update FPS counter
this.frameCount++;
if (currentTime - this.lastFPSUpdate >= this.fpsUpdateInterval) {
this.fps = Math.round(
(this.frameCount * 1000) / (currentTime - this.lastFPSUpdate)
);
this.frameCount = 0;
this.lastFPSUpdate = currentTime;
}
this.lastFrameTime = currentTime - (this.deltaTime % this.frameInterval);
}
};
updateSatelliteSize(size) {
this.satellites.forEach((sat) => {
sat.mesh.scale.set(size, size, size);
});
}
setZoom(zoomLevel) {
this.zoomLevel = zoomLevel;
const aspect = window.innerWidth / window.innerHeight;
const d = this.zoomLevel;
this.camera.left = -d * aspect;
this.camera.right = d * aspect;
this.camera.top = d;
this.camera.bottom = -d;
this.camera.updateProjectionMatrix();
}
resetView() {
const cameraDistance = 20;
this.camera.position.set(cameraDistance, cameraDistance, cameraDistance);
this.camera.lookAt(0, 2, 0); // look at sun center
this.setZoom(this.calculateResponsiveZoom());
this.sunRotationSpeed = 0.00005;
this.earthOrbitSpeed = 0.00002;
this.satelliteSpeedMultiplier = 1;
this.updateSatelliteSize(0.5);
}
setupEventListeners() {
let resizeTimeout;
window.addEventListener("resize", () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
this.onWindowResize();
}, 250);
});
document.addEventListener("visibilitychange", () => {
this.animationPaused = document.hidden;
});
}
onWindowResize() {
this.zoomLevel = this.calculateResponsiveZoom();
const aspect = window.innerWidth / window.innerHeight;
const d = this.zoomLevel;
this.camera.left = -d * aspect;
this.camera.right = d * aspect;
this.camera.top = d;
this.camera.bottom = -d;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
setTargetFPS(fps) {
this.targetFPS = fps;
this.frameInterval = 1000 / this.targetFPS;
}
getCurrentFPS() {
return this.fps;
}
}
document.addEventListener("DOMContentLoaded", () => {
const solarSystem = new SolarSystemScene("animaContainer");
window.solarSystem = solarSystem;
});