-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
291 lines (260 loc) · 7.61 KB
/
main.js
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
if (!Detector.webgl) {
Detector.addGetWebGLMessage();
}
var renderer, camera, scene, div, controller;
var aspect = window.innerWidth / window.innerHeight;
var clock = new THREE.Clock();
var option_play = true;
var option_playback_speed = 1;
var option_camera_focus;
var option_camera_pos;
var init_camera_pos = [60000, 12000, 0];
SYSTEM = {}
SYSTEM.init = function() {
var self = this;
camera = new THREE.PerspectiveCamera(30, aspect, 100, 600000);
// Camera position
camera.position.set(init_camera_pos[0], init_camera_pos[1], init_camera_pos[2]);
camera.lookAt(0,0,0);
// Create a scene
scene = new THREE.Scene();
// Create a renderer
renderer = new THREE.WebGLRenderer({
antialias: true
});
// renderer settings
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.domElement.style.position = 'absolute';
renderer.domElement.style.top = '0';
div = document.getElementById('main_display');
div.appendChild(renderer.domElement);
window.addEventListener('resize', self.on_window_resize, false );
controller = new THREE.TrackballControls(camera, renderer.domElement);
// add misc objects
Utils.addBackgroundStars(scene,120000);
// LIGHT
var light = new THREE.AmbientLight(0xffffff, 0.2);
scene.add(light);
var light1 = new THREE.PointLight(0xffffff, 2, 100000);
light1.position.set(0, 0, 0);
scene.add(light1);
Utils.addSun(scene, 500,0,0,0);
self.animate();
self.create_solar_sys();
// create GUI
displayGUI();
}
// define on_window_resize listener
SYSTEM.on_window_resize = function() {
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
controller.handleResize();
}
// global var solar_sys to store the solar system instance
var solar_sys;
var gl_objects=[];
SYSTEM.create_solar_sys = function() {
// create star from solar_system_info
var mul = 2;
var scl = 1.5;
var star = {
radius : 2400,
rot_time : 350,
satellites : [
{
name : 'mercury',
radius : 150*scl,
rotation : 0,
pos : [1800*mul, 0, 0],
rev_time : 70,
rot_time : 10,
satellites : [{
name : 'moon1',
radius : 12*scl,
rotation:40,
pos : [200*scl, 0, 0],
rev_time : 20,
rot_time : 3
}]
}, {
name : 'venus',
radius : 260*scl,
rotation : 0,
pos : [2800*mul, 0, 0],
rev_time : 140,
rot_time : 30
}, {
name : 'earth',
radius : 300*scl,
rotation : 0,
pos : [4000*mul, 0, 0],
rev_time : 200,
rot_time : 30,
satellites : [{
name : 'OVERLAY:earth_clouds_map.jpg',
radius : 320*scl,
pos : [0, 0, 0],
rev_time : 1000,
rot_time : 23,
},{
name : 'moon',
radius : 60*scl,
pos : [540*scl, 0, 0],
rev_time : 45,
rot_time : 3
}]
}, {
name : 'mars',
radius : 200*scl,
pos : [5200*mul, 0, 0],
rev_time : 350,
rot_time : 30,
satellites : [{
name : 'moon2',
radius : 20*scl,
pos : [300*scl, 0, 0],
rev_time : 15,
rotation : 0,
rot_time : 3
}, {
name : 'moon3',
radius : 10*scl,
pos : [240*scl, 0, 0],
rev_time : 20,
rot_time : 3,
rotation : -20
}]
}, {
name : 'jupiter',
radius : 1000*scl,
rotation : 0,
pos : [10000*mul, 0, 0],
rev_time : 700,
rot_time : 100,
satellites : [{
name : 'moon1',
radius : 80*scl,
rotation : 50,
pos : [1050*scl, 0, 0],
rev_time : 200,
rot_time : 3
}, {
name : 'moon3',
radius : 50*scl,
rotation : 10,
pos : [1150*scl, 100, 0],
rev_time : 100,
rot_time : 3
}]
},{
name : 'saturn',
radius : 900*scl,
rotation : 0,
pos : [14000*mul, 0, 0],
rev_time : 1100,
rot_time : 40,
satellites : [{
name : 'moon2',
radius : 100*scl,
rotation : 50,
pos : [1000*scl, 0, 0],
rev_time : 200,
rot_time : 3
}]
}, {
name : 'uranus',
radius : 700*scl,
rotation : 0,
pos : [17000*mul, 0, 0],
rev_time : 2500,
rot_time : 180
}, {
name : 'neptune',
radius : 750*scl,
rotation : 0,
pos : [-22000*mul, 0, 0],
rev_time : 3500,
rot_time : 100,
satellites : [{
name : 'moon4',
radius : 30*scl,
pos : [700*scl, 0, 0],
rev_time : 15,
rotation : 40,
rot_time : 8
}, {
name : 'moon3',
radius : 60*scl,
pos : [780*scl, 0, 0],
rev_time : 20,
rot_time : 6,
rotation : -200
}, {
name : 'moon2',
radius : 50*scl,
pos : [800*scl, 0, 0],
rev_time : 30,
rot_time : 12,
rotation : -80
}]
}
]
}
solar_sys = new SolarSystem({
name : 'Galaxy',
radius : 45000,
scene : scene,
show_grids : false,
star: star
});
}
// camera look-at and look-from controls
function camera_control() {
if (option_camera_focus==1){
} else if (option_camera_focus) {
scene.updateMatrixWorld();
var vector = new THREE.Vector3();
vector.setFromMatrixPosition(option_camera_focus.matrixWorld);
controller.target = vector;
} else {
controller.target = new THREE.Vector3(0,0,0);
}
if (option_camera_pos==1) {
var vector = new THREE.Vector3();
var radius = solar_sys.star.radius;
camera.position.set(0,0,0);
var view_vector = new THREE.Vector3();
camera.getWorldDirection(view_vector);
vector.x = view_vector.x * radius;
vector.y = view_vector.y * radius;
vector.z = view_vector.z * radius;
camera.position.set(vector.x, vector.y, vector.z);
} else if (option_camera_pos) {
scene.updateMatrixWorld();
var vector = new THREE.Vector3();
var radius = option_camera_pos.radius;
var view_vector = new THREE.Vector3();
camera.getWorldDirection(view_vector);
vector.setFromMatrixPosition(option_camera_pos.body_mesh.matrixWorld);
vector.x += view_vector.x * radius;
vector.y += view_vector.y * radius;
vector.z += view_vector.z * radius;
camera.position.set(vector.x, vector.y, vector.z);
}
}
SYSTEM.animate = function() {
var delta = clock.getDelta();
requestAnimationFrame(SYSTEM.animate);
if (solar_sys && option_play) {
solar_sys.parent.traverse(function(child) {
if (child.animate)
child.animate(option_playback_speed*delta*60);
});
}
// camera focus
camera_control();
controller.update();
renderer.render(scene, camera);
}
SYSTEM.init();