-
Notifications
You must be signed in to change notification settings - Fork 5
/
solar.html
524 lines (444 loc) · 16.9 KB
/
solar.html
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!DOCTYPE html>
<html>
<head>
<title>Solar System</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!-- Owl Carousel -->
<link type="text/css" rel="stylesheet" href="css/owl.carousel.css" />
<link type="text/css" rel="stylesheet" href="css/owl.theme.default.css" />
<!-- Custom stlylesheet -->
<link type="text/css" rel="stylesheet" href="css/style.css" />
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
#info {
position: absolute;
top: 0px;
width: 100%;
color: #ffffff;
padding: 5px;
font-family:Monospace;
font-size:13px;
text-align:right;
}
</style>
<!-- jQuery Plugins -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/owl.carousel.min.js"></script>
<script type="text/javascript" src="js/jquery.magnific-popup.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script src="js/three.min.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/GLTFLoader.js"></script>
<script src="js/AnimationMixer.js"></script>
<script src="js/stats.min.js"></script>
<script src="js/CSS2DRenderer.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
</head>
<body>
<div id="info">
</div>
<script>
// Load 3D Scene
var scene = new THREE.Scene();
scene2 = new THREE.Scene();
var Stats;
var innerw,height;
var stars=[];
var guic, controlConfig= {
freezState: false,
revolveSpeed: 24
};
// For orbit
var marker, spline;
var matrix = new THREE.Matrix4();
var up = new THREE.Vector3( 0, 1, 0 );
var axis = new THREE.Vector3( );
// Planet Orbit Variable Declaration
var mercpath,venpath,earthpath,moonpath,marspath,juppath,satpath,urapath,neppath;
var planobj=[];
var pathobj=[];
// the getPoint starting variable - !important - You get me ;)
var mt=vet=eat=mat=jupt=satt=urat=nept = 0;
// Ellipse class, which extends the virtual base class Curve
function Ellipse( xRadius, yRadius ) {
THREE.Curve.call( this );
// add radius as a property
this.xRadius = xRadius;
this.yRadius = yRadius;
}
Ellipse.prototype = Object.create( THREE.Curve.prototype );
Ellipse.prototype.constructor = Ellipse;
// define the getPoint function for the subClass
Ellipse.prototype.getPoint = function ( t ) {
var radians = 2 * Math.PI * t;
return new THREE.Vector3( this.xRadius * Math.cos( radians ),
0,
this.yRadius * Math.sin( radians ) );
};
// Load Camera Perspective
var camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 1, 20000 );
camera.position.set( 10, 30, 100 );
// RayCaster
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
// Load a Renderer
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setClearColor( 0x000000 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(window.innerWidth, window.innerHeight);
innerw=window.innerWidth;
height=window.innerHeight;
document.body.appendChild(renderer.domElement);
// Load the Orbitcontroller
var controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.minDistance=10;
controls.maxDistance=500;
// Load Light
var ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add( ambientLight );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 0, 1, 1 ).normalize();
scene.add( directionalLight );
// Stars
function addSphere(){
// The loop will move from z position of -1000 to z position 1000, adding a random particle at each position.
for ( var z= -1000; z < 1000; z+=20 ) {
// Make a sphere (exactly the same as before).
var geometry = new THREE.SphereGeometry(0.5, 32, 32)
var material = new THREE.MeshBasicMaterial( {color: 0xffffff} );
var sphere = new THREE.Mesh(geometry, material)
sphere.position.x= Math.random()*innerw-Math.random()*innerw*2;
sphere.position.y=Math.random()*height-Math.random()*height*2;
// Then set the z position to where it is in the loop (distance of camera)
sphere.position.z = z;
// scale it up a bit
sphere.scale.x = sphere.scale.y = 1;
//add the sphere to the scene
scene.add( sphere );
//finally push it to the stars array
stars.push(sphere);
}
}
function animateStars() {
// loop through each star
for(var i=0; i<stars.length; i++) {
star = stars[i];
// and move it forward dependent on the mouseY position.
star.position.z += i/10;
// if the particle is too close move it to the back
if(star.position.z>2000) star.position.z-=3000;
}
}
// params
var pathSegments = 128;
var tubeRadius = 0.03;
var radiusSegments = 3;
var closed = true;
// material
var material = new THREE.MeshPhongMaterial( {
color: 'white',
} );
// mercury orbit mesh
mercpath = new Ellipse( 15, 10 );
var mercgeometry = new THREE.TubeBufferGeometry( mercpath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( mercgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// venus orbit mesh
venpath = new Ellipse(22,15);
var venusgeometry = new THREE.TubeBufferGeometry( venpath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( venusgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Earth Orbit
earthpath = new Ellipse( 28, 22 );
var earthgeometry = new THREE.TubeBufferGeometry( earthpath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( earthgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Moon
moonpath = new Ellipse( 5, 3 );
var moongeometry = new THREE.TubeBufferGeometry( moonpath, pathSegments, 0.0001, radiusSegments, closed );
mesh = new THREE.Mesh( moongeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Mars Orbit
marspath = new Ellipse( 35, 28 );
var marsgeometry = new THREE.TubeBufferGeometry( marspath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( marsgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Jupiter Orbit
juppath = new Ellipse( 46, 34 );
var jupgeometry = new THREE.TubeBufferGeometry( juppath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( jupgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Saturn Orbit
satpath = new Ellipse( 57, 42 );
var satgeometry = new THREE.TubeBufferGeometry( satpath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( satgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Uranus Orbit
urapath = new Ellipse( 69, 49 );
var urageometry = new THREE.TubeBufferGeometry( urapath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( urageometry, material );
scene.add( mesh );
pathobj.push(mesh);
// Neptune Orbit
neppath = new Ellipse( 79, 59 );
var nepgeometry = new THREE.TubeBufferGeometry( neppath, pathSegments, tubeRadius, radiusSegments, closed );
mesh = new THREE.Mesh( nepgeometry, material );
scene.add( mesh );
pathobj.push(mesh);
// glTf 2.0 Loader
var loader = new THREE.GLTFLoader();
// Planets variables;
var sun,mercury,venus,earth,moon,mars,jupiter,saturn,uranus,neptune;
var moonorbit;
loader.load( 'model/sun/sun.gltf', function ( gltf ) { // <<--------- Model Path
sun = gltf.scene;
gltf.scene.scale.set( 18, 18, 18 );
gltf.scene.position.x = 0; //Position (x = right+ left-)
gltf.scene.position.y = -2; //Position (y = up+, down-)
gltf.scene.position.z = -3; //Position (z = front +, back-)
//gltf.animations;
scene.add( gltf.scene );
planobj.push(sun);
});
loader.load( 'model/mercury/mercury.gltf', function ( gltf ) { // <<--------- Model Path
mercury = gltf.scene;
gltf.scene.scale.set( 2, 2, 2 );
var pt = mercpath.getPoint( mt );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(mercury);
});
loader.load( 'model/venus/venus.gltf', function ( gltf ) { // <<--------- Model Path
venus = gltf.scene;
gltf.scene.scale.set( 2, 2, 2 );
var pt = venpath.getPoint( vet );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(venus);
});
loader.load( 'model/earth/earth.gltf', function ( gltf ) { // <<--------- Model Path
earth = gltf.scene;
gltf.scene.scale.set( 2, 2, 2 );
var pt = earthpath.getPoint( eat );
moonorbit = earthpath.getPoint( eat );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(earth);
});
/*loader.load( 'model/moon/moon.gltf', function ( gltf ) { // <<--------- Model Path
moon = gltf.scene;
gltf.scene.scale.set( 1, 1, 1 );
//var pt = moonpath.getPoint( eat );
gltf.scene.position.set(moonorbit.x+2,moonorbit.y+2,moonorbit.z);
scene.add( gltf.scene);
});
*/
loader.load( 'model/mars/mars.gltf', function ( gltf ) { // <<--------- Model Path
mars = gltf.scene;
gltf.scene.scale.set( 2, 2, 2 );
var pt = marspath.getPoint( mat );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(mars);
});
loader.load( 'model/jupiter/jupiter.gltf', function ( gltf ) { // <<--------- Model Path
jupiter = gltf.scene;
gltf.scene.scale.set( 8, 8, 8 );
var pt = juppath.getPoint( jupt );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(jupiter);
});
loader.load( 'model/saturn/saturn.gltf', function ( gltf ) { // <<--------- Model Path
saturn = gltf.scene;
gltf.scene.scale.set( 7, 7, 7 );
var pt = satpath.getPoint( satt );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(saturn);
});
loader.load( 'model/uranus/uranus.gltf', function ( gltf ) { // <<--------- Model Path
uranus = gltf.scene;
gltf.scene.scale.set( 3, 3, 3 );
var pt = urapath.getPoint( urat );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(uranus);
});
loader.load( 'model/neptune/neptune.gltf', function ( gltf ) { // <<--------- Model Path
neptune = gltf.scene;
gltf.scene.scale.set( 3, 3, 3 );
var pt = neppath.getPoint( nept );
gltf.scene.position.set(pt.x,pt.y,pt.z);
scene.add( gltf.scene);
planobj.push(neptune);
});
// FPS stats
stats = new Stats();
document.body.appendChild( stats.dom );
window.addEventListener( 'resize', onWindowResize, false );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
guic = new dat.GUI( { width: 350 } );
// Controls
var controlGUI = guic.addFolder( "Controls" );
//controlGUI.add(controlConfig.freezState,'Freeze State').listen();
controlGUI.add( controlConfig, 'freezState' )
controlGUI.add( controlConfig, 'revolveSpeed', 1, 24 ).onChange( function()
{
//sunLight.shadow.camera.near = shadowConfig.shadowCameraNear;
//sunLight.shadow.camera.updateProjectionMatrix();
//shadowCameraHelper.update();
});
function onWindowResize( event )
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
//controls.handleResize();
}
function onDocumentTouchStart( event ) {
event.preventDefault();
event.clientX = event.touches[0].clientX;
event.clientY = event.touches[0].clientY;
onDocumentMouseDown( event );
}
function onDocumentMouseDown( event ) {
event.preventDefault();
mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( pathobj );
if ( intersects.length > 0 ) {
var pt = intersects[0].getPoint( mt );
}
}
function animate()
{
requestAnimationFrame( animate );
renderer.render( scene, camera );
//if (!guic.freezState) return;
animateStars();
// TO freeze Planet State
if(!controlConfig.freezState)
revolvePlanet();
console.log(controlConfig.revolveSpeed);
stats.update();
}
function revolvePlanet(){
if (mercury)
{ //mercury.rotation.y+=0.05;
var pt = mercpath.getPoint( mt );
var tangent = mercpath.getTangent( mt ).normalize();
mercury.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
mt = (mt >= 1) ? 0 : mt += (0.005/controlConfig.revolveSpeed);
}
if (venus)
{
//venus.rotation.y+=0.04;
var pt = venpath.getPoint( vet );
var tangent = venpath.getTangent( vet ).normalize();
venus.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
vet = (vet >= 1) ? 0 : vet += 0.001/controlConfig.revolveSpeed;
}
if (earth)
{ earth.rotation.y+=0.02;
var pt = earthpath.getPoint( eat );
var tangent = earthpath.getTangent( eat ).normalize();
earth.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
eat = (eat >= 1) ? 0 : eat += 0.0003/controlConfig.revolveSpeed;
}
if (moon)
{ moon.rotation.y+=0.02;
var pt = earthpath.getPoint( eat );
var tangent = earthpath.getTangent( eat ).normalize();
moon.position.set(pt.x+2,pt.y+2,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
eat = (eat >= 1) ? 0 : eat += 0.0003/controlConfig.revolveSpeed;
}
if (mars)
{ //mars.rotation.z+=0.02;
var pt = marspath.getPoint( mat );
var tangent = marspath.getTangent( mat );
mars.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
mat = (mat >= 1) ? 0 : mat += 0.0001/controlConfig.revolveSpeed;
}
if (jupiter)
{ jupiter.rotation.y+=0.004;
var pt = juppath.getPoint( jupt );
var tangent = juppath.getTangent( jupt ).normalize();
jupiter.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
jupt = (jupt >= 1) ? 0 : jupt += 0.00006/controlConfig.revolveSpeed;
}
if (saturn)
{ saturn.rotation.y+=0.009;
var pt = satpath.getPoint( satt );
var tangent = satpath.getTangent( satt ).normalize();
saturn.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
satt = (satt >= 1) ? 0 : satt += 0.00003/controlConfig.revolveSpeed;
}
if (uranus)
{ uranus.rotation.y+=0.0004;
var pt = urapath.getPoint( urat );
var tangent = urapath.getTangent( urat ).normalize();
uranus.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
urat = (urat >= 1) ? 0 : urat += 0.000009/controlConfig.revolveSpeed;
}
if (neptune)
{ neptune.rotation.y+=0.0004;
var pt = neppath.getPoint( nept );
var tangent = neppath.getTangent( nept ).normalize();
neptune.position.set(pt.x,pt.y,pt.z);
// calculate the axis to rotate around
axis.crossVectors( up, tangent ).normalize();
// calcluate the angle between the up vector and the tangent
var radians = Math.acos( up.dot( tangent ) );
nept = (nept >= 1) ? 0 : nept += 0.0000009/controlConfig.revolveSpeed;
}
}
addSphere();
animate();
</script>
</body>
</html>