-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
544 lines (480 loc) · 19 KB
/
index.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Celestial</title>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<meta name="description" content="HTML5 Canvas rendering using Three.js - by Jay Crossler" />
<meta name="keywords" content="html5, canvas, ipad, three.js, ios, android, gestures" />
<meta name="robots" content="index, follow" />
<style type="text/css">
body {
color: #808080;
font-family:Monospace;
font-size:13px;
text-align:center;
background-color: #000000;
margin: 0px;
padding:0;
height:100%;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
}
a, span {
color: #0080ff;
cursor: pointer; cursor: hand;
}
#div_planetinfo {
position: absolute;
width: 150px; height: 150px;
background-color: #200;
padding: 5px;
right: 10px; bottom: 10px;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
opacity: 0.5;
}
//Handles mouse bouncing and other iPad-specific issues
* {
-webkit-touch-callout: none;
-webkit-text-size-adjust: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-user-select: none;
}
</style>
<script type="text/javascript" src="js/Stats.js"></script>
<script type="text/javascript" src="js/Three.js"></script>
<script type="text/javascript" src="js/Ray.js"></script>
<script type="text/javascript" src="js/Color.js"></script>
<script type="text/javascript" src="js/CameraControl.js"></script>
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="js/Celestial.js"></script>
<script type="text/javascript" src="planets.json"></script>
</head>
<body onload="preloader()">
<div id="container"></div>
<div id="info">
<a href="../StarPad/index.html">STARS</a>
[Adv <span onclick="clickit(11)">D</span>:<span onclick="clickit(12)">H</span>:<span onclick="clickit(13)">M</span>:<span onclick="clickit(14)">S</span> per frame]
<span id="timedisplay" style="color:white" onclick="clickit(10)"></span>
</div>
<div id="div_info"></div>
<div id="div_planetinfo">
<a href="?renderer=webgl">WEBGL</a>
<a href="?renderer=canvas">CANVAS</a>
<a href="?renderer=svg">SVG</a> <!-- Particles not working for SVG -->
</div>
<script type="text/javascript">
var option_PanScreenWithMouse = true, //when mouse moves, use it to pan camera
option_RenderEngine = true, //Use the 3D graphics acceleration
option_SphereQuality = 20, //how many tessalations to use when drawing spheres, more = higher resource use
option_ZoomOnClick = false, //Zoom ahead when the mouse is clicked
option_SolarSystem3DSizeXZ = 400, // how many "3d" pixels to use in the system length, width
option_SolarSystem3DSizeY = 400, // how many "3d" pixels to use in the system's height
option_TimeAdvanceType = 'd', // 'h','m','d','s', rate; how often to advance per second
option_TargetFPS = 30; // Goal for # of frames per second
var planetmeshlist = [];
var container, stats, timedisplay, div_planetinfo, div_info, cameraControl;
var camera, moveForward, moveBackward, moveShouldStop=false;
var mesh, scene, renderer, projector, texture_placeholder;
var loginfo=0;
var firstSceneToRender=true;
var texture_level='low';
var img_loaded_count = 0;
function preloader() {
container = document.getElementById( 'container' );
container.innerHTML="<br/><br/><br/><br/><h1>Loading Images: <span id='imgloadpercent'>0%</span></h1>";
var imgloadpercent = document.getElementById( 'imgloadpercent' );
//Initialize planets
Celestial.init(planet_init_list,option_SolarSystem3DSizeXZ,option_SolarSystem3DSizeY);
//Build the array of all planet textures to load
images = new Array();
for ( var i = 0, l = Celestial.planetsCount(); i < l; i ++ ) {
images.push(Celestial.planetInfo(i,'texture','low'));
images.push(Celestial.planetInfo(i,'texture','med'));
images.push(Celestial.planetInfo(i,'texture','high'));
// images.push(Celestial.planetInfo(i,'texture','max'));
}
images.push("textures/sun.png");
// images.push("textures/skybox/"+texture_level+"/hot_nebula_90.jpg");
// images.push("textures/skybox/"+texture_level+"/hot_nebula_270.jpg");
// images.push("textures/skybox/"+texture_level+"/hot_nebula_top.jpg");
// images.push("textures/skybox/"+texture_level+"/hot_nebula_bottom.jpg");
// images.push("textures/skybox/"+texture_level+"/hot_nebula_0.jpg");
// images.push("textures/skybox/"+texture_level+"/hot_nebula_180.jpg");
// Start preloading
for ( var i=0, l=images.length; i<l; i++ ) {
imageObj = new Image();
imageObj.src=images[i];
imageObj.onload = function() {
imgloadpercent.innerHTML= ((img_loaded_count * 100) / l).toFixed(2) + "%";
img_loaded_count++;
if (img_loaded_count == l) {
container.innerHTML="";
//Start the simulation
init();
animate();
}
}
}
}
function init() {
timedisplay=document.getElementById( 'timedisplay');
div_planetinfo =document.getElementById( 'div_planetinfo' );
div_info=document.getElementById( 'div_info' );
scene = new THREE.Scene();
init_camera();
init_sky();
init_grid();
init_planets();
init_renderer();
init_stats();
advanceAndPosition();
planetWasClicked(0);
}
//=======================================
function generateSunTexture(color, size, showShell) {
var size = (size) ? parseInt(size*24) : 24;
var showShell = (showShell) ? showShell : false;
var canvas = document.createElement( 'canvas' );
canvas.width = size;
canvas.height = size;
var col = new THREE.Color(color);
var context = canvas.getContext( '2d' );
var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
gradient.addColorStop( 0, col.rgbaString);
gradient.addColorStop( 0.1, col.rgbaString);
gradient.addColorStop( 0.6, 'rgba(125, 20, 0, 0.2)' );
if (showShell) {
gradient.addColorStop( 0.88, 'rgba(0, 20, 0, 1)' );
gradient.addColorStop( 0.9, 'rgba(255, 255, 255, 0.2)' );
}
gradient.addColorStop( .92, 'rgba(0,0,0,0)' );
context.fillStyle = gradient;
context.fillRect( 0, 0, canvas.width, canvas.height );
return canvas;
}
//=======================================
function init_sky() {
//Textures from Flex2D Cookbox - http://code.google.com/p/flex3cookbook1/downloads/list
var path = "textures/skybox/"+texture_level+"/hot_nebula_";
var format = '.jpg';
var urls = [path + '90' + format, path + '270' + format,
path + 'top' + format, path + 'bottom' + format,
path + '0' + format, path + '180' + format ];
var textureCube = ImageUtils.loadTextureCube( urls, new THREE.CubeRefractionMapping() );
var material = new THREE.MeshBasicMaterial( { color: 0xffffff, env_map: textureCube, refraction_ratio: 0.95 } );
if (option_RenderEngine == "webgl") {
SceneUtils.addPanoramaCubeWebGL( scene, option_SolarSystem3DSizeXZ*5, textureCube );
} else { //TODO: Not working
SceneUtils.addPanoramaCube( scene, option_SolarSystem3DSizeXZ*5, urls );
}
}
function init_planets() {
var resolution = 30;
var size = 360 / resolution;
material = new THREE.LineBasicMaterial( { color: 0x008080, opacity: 0.5, blending: THREE.AdditiveBlending } );
for ( var i = 0, l = Celestial.planetsCount(); i < l; i ++ ) {
var mesh = create_planet(i);
if (Celestial.planetInfo(i,'type') == 'star') {
if (option_RenderEngine == "webgl") {
var meshstar = new THREE.Mesh( new Sphere( Celestial.planetInfo(i,'pixelwidth')*1.1,8,8 ),
new THREE.MeshBasicMaterial( { color: 0x665500, blending: THREE.AdditiveBlending }) );
meshstar.overdraw = true;
// scene.addObject( meshstar );
} else {
//TODO: Update Textures
var materialstar = new THREE.ParticleBasicMaterial( { map: new THREE.Texture( generateSunTexture(0xfff2a1,3) ), blending: THREE.AdditiveBlending } );
var particle = new THREE.Particle( materialstar );
particle.position.x = 0;
particle.position.y = 0;
particle.position.z = 0;
particle.scale.x = particle.scale.y = .4;
particle.isClickable = false;
particle.name = "Sol";
particle.starid = 0;
scene.addObject( particle );
// particle = new THREE.Particle( new THREE.ParticleBasicMaterial( { map: ImageUtils.loadTexture( 'textures/sun.png' ), blending: THREE.AdditiveBlending } ) );
// scene.addObject( particle );
}
cameraControl.targetObject = mesh;
} else {
//For each step of the planet's location, build a series of orbit lines
var orbitepochstep = Celestial.Planets[i].orbit_sidereal_in_days / resolution;
geometry = new THREE.Geometry();
for ( var j = 0; j <= resolution; j ++ ) {
segment = ( j * size ) * Math.PI / 180;
var orbitepoch = orbitepochstep * (j+1);
var orbitpos = Celestial.planetLocation(i,orbitepoch);
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(
orbitpos.position.x,
orbitpos.position.y,
orbitpos.position.z)));
}
line = new THREE.Line( geometry, material );
scene.addObject( line );
}
planetmeshlist.push( mesh );
scene.addObject( mesh );
}
console.log('Planets created and added to Scene. SceneObjects now:'+scene.objects.length);
}
function create_planet(planetid) {
var pcolor = Celestial.planetInfo(planetid,'color');
var material = [
[ new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( Celestial.planetInfo(planetid,'texture','high')) } ) ],
[ new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( Celestial.planetInfo(planetid,'texture','med')) } ) ],
[ new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( Celestial.planetInfo(planetid,'texture','low')) } ) ],
[ new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( Celestial.planetInfo(planetid,'texture','low')) } ) ],
[ new THREE.MeshLambertMaterial( { color: pcolor } ) ],
[ new THREE.MeshLambertMaterial( { color: pcolor } ) ],
[ new THREE.MeshLambertMaterial( { color: pcolor } ) ],
[ new THREE.MeshLambertMaterial( { color: pcolor } ) ],
];
// new THREE.MeshLambertMaterial( { color: 0xffffff } ),
// new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, wireframe: true } )
var planet_width = Celestial.planetInfo(planetid,'pixelwidth');
var geometry = [
[ new Sphere( planet_width, 12, 8 ), parseInt(3*planet_width) ],
[ new Sphere( planet_width, 10, 5 ), parseInt(7*planet_width) ],
[ new Sphere( planet_width, 8, 4 ), parseInt(12*planet_width) ],
[ new Sphere( planet_width, 5, 4 ), parseInt(30*planet_width) ],
[ new Sphere( planet_width, 4, 3 ), parseInt(70*planet_width) ],
[ new Sphere( planet_width, 3, 3 ), parseInt(100*planet_width) ],
[ new Sphere( planet_width, 2, 2 ), parseInt(150*planet_width) ],
[ new Sphere( planet_width, 1, 1 ), parseInt(200*planet_width) ]
];
var i, j, mesh, lod;
lod = new THREE.LOD();
for ( i = 0; i < geometry.length; i++ ) {
mesh = new THREE.Mesh( geometry[ i ][ 0 ], material[i] );
mesh.name = Celestial.planetInfo(planetid,'name');
mesh.planetid = planetid;
mesh.isClickable = true;
mesh.overdraw = true;
lod.add( mesh, geometry[ i ][ 1 ] );
}
var orbitpos = Celestial.planetInfo(planetid,'posrot');
lod.position = new THREE.Vector3(
orbitpos.position.x,
orbitpos.position.y,
orbitpos.position.z);
lod.rotation = orbitpos.rotation;
return lod;
}
//=========================================
function resizePlanets() {
for ( var i = 0, l = planetmeshlist.length; i < l; i++ ) {
renderer.removeObject( scene, planetmeshlist[i] );
planetmeshlist[i] = create_planet(i);
scene.addObject(planetmeshlist[i]);
}
}
function clickit(mode) {
switch(mode) {
case 1:
Celestial.spacingMethod = 'evenly';
break;
case 2:
Celestial.spacingMethod = 'realistic';
break;
case 3:
Celestial.spacingMethod = 'artistic';
break;
case 4:
Celestial.sizingMethod = 'equal';
resizePlanets();
break;
case 5:
Celestial.sizingMethod = 'realistic';
resizePlanets();
break;
case 6:
Celestial.sizingMethod = 'artistic';
resizePlanets();
break;
case 7:
option_PanScreenWithMouse = !option_PanScreenWithMouse;
break;
case 8:
loginfo = ((loginfo+1) % 3);
break;
case 9:
Celestial.spacingMethod = 'realistic';
Celestial.sizingMethod = 'realistic';
resizePlanets();
camera.position.x = -2;
camera.position.y = 40;
camera.position.z = 60;
mouseY = -13;
mouseX = -1;
break;
case 10:
if (Celestial.time_display_method == 'yearday') {
Celestial.time_display_method = 'date';
} else {
Celestial.time_display_method = 'yearday';
}
break;
case 11:
option_TimeAdvanceType = 'd';
break;
case 12:
option_TimeAdvanceType = 'h';
break;
case 13:
option_TimeAdvanceType = 'm';
break;
case 14:
option_TimeAdvanceType = 's';
break;
}
}
function planetWasClicked(planetid) {
var P = Celestial.Planets[planetid];
console.log("Planet "+P.planetName +" was clicked");
var strP = "<div id='planetname'>"+P.planetName+"</div>";
strP+= "<img src='"+Celestial.planetInfo(planetid,'texture')+"' height='130'>"
div_planetinfo.innerHTML = strP;
// cameraControl.targetObject = planetmeshlist[planetid];
}
function advanceAndPosition() {
switch(option_TimeAdvanceType) {
case 'd':
Celestial.addDay();
break;
case 'h':
Celestial.addHour();
break;
case 'm':
Celestial.addMinute();
break;
case 's':
Celestial.addSecond();
break;
}
for ( var i = 0, l = planetmeshlist.length; i < l; i++ ) {
var posrot = Celestial.planetInfo(i,'posrot');
planetmeshlist[i].position = posrot.position;//Celestial.planetInfo(i,'position');
planetmeshlist[i].position.subSelf = function () {}; //Added because Celestial doesn't return a three.js position object
planetmeshlist[i].position.addSelf = function () {}; //Added because Celestial doesn't return a three.js position object
planetmeshlist[i].rotation = posrot.rotation;//Celestial.planetInfo(i,'rotation');
// var orbitpos = Celestial.planetInfo(i,'posrot');
// planetmeshlist[i].position = new THREE.Vector3(
// orbitpos.position.x,
// orbitpos.position.y,
// orbitpos.position.z);
// planetmeshlist[i].rotation = orbitpos.rotation;
}
timedisplay.innerHTML = Celestial.displayTime();
}
//=========================================
function init_grid() {
// Grid
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( -1 * 200, 0, 0 ) ) );
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 200, 0, 0 ) ) );
var material = new THREE.LineBasicMaterial( { color: 0x444444, opacity: 0.8 } );
var lineSteps = 10;
for ( var i = 0; i <= lineSteps; i ++ ) {
var step = (200 *2) / lineSteps;
var line = new THREE.Line( geometry, material );
line.position.y = 0;
line.position.z = ( i * step ) - 200;
scene.addObject( line );
var line = new THREE.Line( geometry, material );
line.position.x = ( i * step ) - 200;
line.position.y = 0;
line.rotation.y = Math.PI / 2;
scene.addObject( line );
}
console.log('Grid created and added to Scene. SceneObjects now:'+scene.objects.length);
}
function init_camera() {
camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = option_SolarSystem3DSizeY/6;
camera.position.z = option_SolarSystem3DSizeXZ/6;
window.addEventListener( 'resize', onWindowResize, false );
projector = new THREE.Projector();
cameraControl = new CameraControlWASD( camera, 10, 0.005, true, false );
}
function init_stats() {
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
}
function queryString(q) {
hu = window.location.search.substring(1);
gy = hu.split("&");
var result = null;
for (i=0;i<gy.length;i++) {
ft = gy[i].split("=");
if (ft[0] == q) {
result = ft[1]; break;
}
}
return result;
}
function init_renderer() {
var renderer_created = false;
var requested_renderer = queryString("renderer");
if (!requested_renderer) requested_renderer="canvas";
try {
switch(requested_renderer) {
case "webgl": renderer = new THREE.WebGLRenderer(); break;
case "canvas": renderer = new THREE.CanvasRenderer(); break;
case "svg": renderer = new THREE.SVGRenderer(); break;
case "dom": renderer = new THREE.DOMRenderer(); break;
default: break;
}
option_RenderEngine = requested_renderer;
console.log('Rendering: '+requested_renderer);
renderer_created = true;
} catch (err) {
//Try canvas
console.log("WebGL Rendering failed");
renderer = new THREE.CanvasRenderer();
console.log('Rendering: Canvas');
option_RenderEngine = "canvas";
renderer_created = true;
}
if (renderer_created) {
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
} else {
container.innerHTML = "<br/><br/><br/><h1>3D Rendering could not load</h1>";
console.log("FAIL! No renderer able to load");
}
}
function onWindowResize( event ) {
//When rotated, resize the window and rendering space
var width = window.innerWidth, height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
renderer.domElement.style.width = window.innerWidth + 'px';
renderer.domElement.style.height = window.innerHeight + 'px';
}
function animate() {
//Animate these as often as possible - up to 60fps
cameraControl.update();
requestAnimationFrame( animate );
render();
stats.update();
advanceAndPosition();
}
function render() {
//Render the updated canvas
renderer.render( scene, camera );
}
</script>
</body>
</html>