-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
527 lines (388 loc) · 13.8 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
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Schwarzschild Black Hole</title>
<!-- using a couple functions from these guys
yes I'm a WebGL noob --!>
<link type="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
<!--jQuery & Bootstrap --!>
<script src="http://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<!--WebGL Utils--!>
<script type="text/javascript" src="resources/webgl-utils.js"></script>
<!--Fullscreen query--!>
<script>
function fullscreen(){
var el = document.getElementById('canvas');
if(el.webkitRequestFullScreen) {
el.webkitRequestFullScreen();
}
else {
el.mozRequestFullScreen();
}
}
</script>
<!--The real magic--!>
<script>
var gl;
var neheTexture;
var bgTexture;
var haTexture;
var curtex;
var clicked = 0;
var clx;
var cly;
var orbiting = 1;
var regime = 1.0;
var targetvangle = 0.0;
document.addEventListener("DOMContentLoaded", initev, false);
function initev()
{
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", getPosition, false);
}
function getPosition(event)
{
var x = new Number();
var y = new Number();
var canvas = document.getElementById("canvas");
if (event.x != undefined && event.y != undefined)
{
x = event.x;
y = event.y;
}
else // Firefox method to get the position
{
x = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
clicked = 1;
clx = x;
cly = y;
}
//functions called on settings form click events
function checkres(value)
{
var canvas = document.getElementById("canvas");
if (value == "240p")
{canvas.width=320;canvas.height=240;}
else if (value == "480p")
{canvas.width=640;canvas.height=480;}
else if (value == "786p")
{canvas.width=1024;canvas.height=786;}
else if (value =="1080p")
{canvas.width=1920;canvas.height=1080;}
}
function checktex(value)
{
if (value == "bg")
curtex = bgTexture;
else if (value == "ha")
curtex = haTexture;
else if (value == "lp")
curtex = lpTexture;
else if (value == "uv")
curtex = uvTexture;
else
curtex = Null;
}
function checkorb(value)
{
orbiting = value;
}
function checklook(value)
{
targetvangle = value;
}
// MAIN FUNCTION
window.onload = main;
function main() {
var canvas = document.getElementById("canvas");
gl = getWebGLContext(canvas);
if (!gl) {
return;
}
//Textures are loaded
initTexture();
//Shader compilation
vertexShader = createShaderFromScriptElement(gl, "2d-vertex-shader");
fragmentShader = createShaderFromScriptElement(gl, "2d-fragment-shader");
program = createProgram(gl, [vertexShader, fragmentShader]);
gl.useProgram(program);
//Uniform location
var positionLocation = gl.getAttribLocation(program, "a_position");
var timeUniform = gl.getUniformLocation(program, "time"); // Getting location
var rUniform = gl.getUniformLocation(program, "R"); // Getting location
var betaUniform = gl.getUniformLocation(program, "beta"); // Getting location
var gammaUniform = gl.getUniformLocation(program, "gamma"); // Getting location
var resUniform = gl.getUniformLocation(program, "resolution");
var lookUniform = gl.getUniformLocation(program, "viewrot");
//Radius/velocity display ID
var radiusP = document.getElementById("radiusP");
//Geometry (just a quad)
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
1.0, 1.0]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
//The two uniform sampler location
var u_defLocation = gl.getUniformLocation(program, "defSampler"); //deflection lookup tex
var u_bgLocation = gl.getUniformLocation(program, "bgSampler"); //lat long sky sphere
//binding
gl.uniform1i(u_defLocation,2);
gl.uniform1i(u_bgLocation,3);
//start time
var timec = 3000*Math.random();
var R = 0.0;
var R_target;
var beta;
var gamma;
var lookangle = 0.0;
//canvas starts at high resolution and is only scaled down here, to fit screen
//this is done because things work weird otherwise
if (screen.width < 786){
canvas.width=640;
canvas.height=480;
}
else
{
canvas.width = 1024;
canvas.height = 786;
}
//MAIN LOOP
setInterval(function () {
if (clicked){
R_target = 1.55+8.35*cly/canvas.height;
R += 0.02 * (R_target - R);
}
else{
R = 7.0+2.9 * Math.sin(timec*0.0031);
}
regime += 0.01 * (orbiting - regime);
//relativity shit
var beta_orb = 0.707 * 1.0 / Math.sqrt(R-1.0);
var gamma_orb = 1. / Math.sqrt(1.0 - beta*beta);
beta = regime * beta_orb;
gamma = (1.-regime) + regime*gamma_orb;
//look angle
lookangle += 0.01 * (targetvangle - lookangle);
//updating info div
radiusP.innerHTML = "Distance: " + R.toFixed(2) + " S. radii<br>Orbital velocity: " + beta.toFixed(2) + " c<br>Orbital γ: "+gamma.toFixed(3)+"<br>";
if(orbiting)
if (R<=3.0)
radiusP.innerHTML += "<span class=\"label label-warning\">Unstable orbit</span><br><small>Inertial, but corrections needed.</small>";
else
radiusP.innerHTML += "<span class=\"label label-primary\">Stable orbit</span><br><small>Inertial, engines off</small>";
else
radiusP.innerHTML += "<span class=\"label label-danger\">Hovering</span><br><small>Accelerating, engines on.</small>";
//we must rebind textures every frame
gl.activeTexture(gl.TEXTURE2);
gl.bindTexture(gl.TEXTURE_2D, neheTexture);
gl.activeTexture(gl.TEXTURE3);
gl.bindTexture(gl.TEXTURE_2D, curtex);
// updating uniforms
gl.uniform1f(timeUniform, timec );
gl.uniform1f(rUniform, R);
gl.uniform1f(betaUniform, beta);
gl.uniform1f(gammaUniform, gamma);
gl.uniform2f(resUniform, canvas.width, canvas.height);
gl.uniform1f(lookUniform,lookangle);
// draw
gl.drawArrays(gl.TRIANGLES, 0, 6);
timec += regime;
},10);
}
function initTexture() {
neheTexture = gl.createTexture();
neheTexture.image = new Image();
neheTexture.image.onload = function() {
handleLoadedTexture(neheTexture)
}
neheTexture.image.src = "deflections_tex.png";
bgTexture = gl.createTexture();
bgTexture.image = new Image();
bgTexture.image.onload = function() {
handleLoadedTexture(bgTexture)
}
bgTexture.image.src = "tex/bg1024.png";
haTexture = gl.createTexture();
haTexture.image = new Image();
haTexture.image.onload = function() {
handleLoadedTexture(haTexture)
}
haTexture.image.src = "tex/ha1024.jpg";
lpTexture = gl.createTexture();
lpTexture.image = new Image();
lpTexture.image.onload = function() {
handleLoadedTexture(lpTexture)
}
lpTexture.image.src = "tex/lp1024.jpg";
uvTexture = gl.createTexture();
uvTexture.image = new Image();
uvTexture.image.onload = function() {
handleLoadedTexture(uvTexture)
}
uvTexture.image.src = "tex/uv1024.jpg";
curtex = bgTexture;
}
function handleLoadedTexture(texture) {
window.console.log("handleLoadedTexture " + texture.image.src);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.bindTexture(gl.TEXTURE_2D, null);
}
</script>
<!-- vertex shader -->
<script id="2d-vertex-shader" type="x-shader/x-vertex">
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1);
}
</script>
<!-- fragment shader -->
<script id="2d-fragment-shader" type="x-shader/x-fragment">
//This shader is part magic, part luck
//so it stays uncommented for now
precision mediump float;
#define M_PI 3.1415926535897932384626433832795 //not enough digits
uniform sampler2D defSampler;
uniform sampler2D bgSampler;
uniform float time;
uniform vec2 resolution;
uniform float R;
uniform float beta;
uniform float gamma;
uniform float viewrot;
const float tanfov = 3.;
vec3 rotate(const vec3 v, float theta, const vec3 axis) {
float cosTheta = cos(theta);
return v * cosTheta + cross(axis, v) * sin(theta) + axis * dot(v, axis) * (1.0 - cosTheta);
}
vec4 stripes(vec3 view, float horizonflag) { //argument must be normalized
return vec4(horizonflag * step(fract(5.*atan(view.z,view.x)),.8) * step(fract(5.*acos(view.y)),.8) * vec3(1.,-view.y,view.y), 1.) ;
}
vec3 aberrate(vec3 original)
{
return vec3(-beta * length(original) + gamma * original.x, original.yz );
}
vec4 starfield(vec3 view, float horizonflag) {
float latitude = acos(view.y)/M_PI;
float longitude = atan(view.z,view.x)/(2.0*M_PI);
return min(
texture2D( bgSampler, vec2(longitude,latitude)),
vec4(horizonflag*vec3(1.,1.,1.),1.)
);
}
const float textureSize = 512.0;
const float texelSize = 1.0 / textureSize;
vec4 texture2DInterp( sampler2D textureSampler, vec2 uv ) //not actually used right now
{
vec4 tl = texture2D(textureSampler, uv);
vec4 tr = texture2D(textureSampler, uv + vec2(texelSize, 0));
vec4 bl = texture2D(textureSampler, uv + vec2(0, texelSize));
vec4 br = texture2D(textureSampler, uv + vec2(texelSize , texelSize));
vec2 f = fract( uv.xy * textureSize ); // get the decimal part
vec4 tA = mix( tl, tr, f.x ); // will interpolate the red dot in the image
vec4 tB = mix( bl, br, f.x ); // will interpolate the blue dot in the image
//return mix( tA, tB, f.y ); // will interpolate the green dot in the image
float st = smoothstep(0.,1.,f.y);
return tA*(1.0-st) + st*tB;
}
void main() {
//get screen coords
vec2 uvscr = gl_FragCoord.xy / resolution;
vec2 uv = (uvscr - 0.5) * vec2(1.,resolution.y/resolution.x);
uv *= tanfov;
vec3 uv3 = normalize( aberrate( rotate( vec3(uv, -1.0),viewrot,vec3(0,1,0) )));
float theta = acos(dot(uv3,vec3(0,0,-1)));
//float dphi = 2./(R*sin(theta));
//small-angle approximation
//float Phi_sa = M_PI - (theta - dphi);
vec2 params = vec2( (R-1.0)/9.0, 1. - theta /M_PI );
vec4 distcolor = texture2D(defSampler,params);
// VVV why we should have a .98 here will forever be a mistery
float Phi_ba = 2.* M_PI * (0.98*distcolor.r+0.1*(distcolor.g));
float Phi = Phi_ba; //(1.-step(theta,1.0))*Phi_sa + (step(theta,1.0))*Phi_ba;
vec3 v3 = -normalize(cross(vec3(0,0,-1),uv3));
uv3 = rotate(vec3(0,0,R), Phi, v3);
float nothorizon = max(
smoothstep(0.,0.05,texture2D(defSampler,params).r) * (1.-step(theta,0.01)) ,
(1.-step(theta,1.57))
);
uv3 = rotate(uv3, time * 0.0015, vec3(0., 1., 0.));
//vec4 color = textureCube(iChannel0, cubeTexCoord);
vec4 color = starfield(normalize(uv3), nothorizon);
gl_FragColor = color;
//gl_FragColor = texture2DInterp(bgSampler,(uv)*0.1); //DEBUG: test uv
//gl_FragColor = vec4(theta * vec3(1.,1.,1.),1.); //DEBUG: test theta
// gl_FragColor = vec4(Phi/(2.*M_PI) * vec3(1.,1.,1.),1.); //DEBUG: test Phi
}
</script>
</head>
<body >
<div class="container">
<div class="col-md-3 col-sm-3 col-xs-6">
<p>Click/tap on canvas to set distance.</p>
<p id="radiusP">Radius</p>
<h4>Look...</h4>
<form>
<input checked type="radio" name="lookradio" onclick="checklook(0.0)">Straight at the center<br>
<input type="radio" name="lookradio" onclick="checklook(-1.57)">Ahead in your orbit<br>
</form>
<h4><a href="../">
<span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span> Explanation
</a></h4>
</div>
<div class="col-md-3 col-sm-3 col-xs-6">
<h4>Resolution</h4>
<form>
<input type="radio" name="resradio" onclick="checkres(this.value)" value="240p">320x240<br>
<input type="radio" name="resradio" onclick="checkres(this.value)" value="480p">640x480<br>
<input checked type="radio" name="resradio" onclick="checkres(this.value)" value="786p">1024x786<br>
<input type="radio" name="resradio" onclick="checkres(this.value)" value="1080p">1920x1080<br>
<br>
</form>
<button class="btn btn-default" onclick="fullscreen();"><span class="glyphicon glyphicon-fullscreen" aria-hidden="true"></span> Go Fullscreen</button>
<br>
</div>
<div class="col-md-3 col-sm-3 col-xs-6">
<h4>Texture</h4>
<form>
<input checked type="radio" name="texradio" onclick="checktex(this.value)" value="bg">Milky Way<br>
<input type="radio" name="texradio" onclick="checktex(this.value)" value="ha">H-alpha<br>
<input type="radio" name="texradio" onclick="checktex(this.value)" value="uv">lat-long grid<br>
<input type="radio" name="texradio" onclick="checktex(this.value)" value="lp">Image from from <a href="http://paulbourke.net/miscellaneous/littleplanet/">"Little Planet"</a><br>
<br>
</form>
</div>
<div class="col-md-3 col-sm-3 col-xs-6">
<h4>Orbit</h4>
<form>
<input checked type="radio" name="orbradio" onclick="checkorb(1)">Circular orbit<br>
<input type="radio" name="orbradio" onclick="checkorb(0)">Hover Schwarzschild-stationary<br>
</form>
</div>
</div>
<div height="30" style="display: inline-block;"></div>
<div class="row" width="1920">
<canvas id="canvas" width="1920" height="1080" style="margin-left: auto; margin-right: auto; display: block; padding: 0px;" ></canvas>
</div>
</body>
</html>