-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.html
1010 lines (860 loc) · 33.1 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head><title>GenGen</title></head>
<script src="jquery-3.2.0.min.js"></script>
<script src="seedrandom.js"></script>
<link href="https://fonts.googleapis.com/css?family=Dosis|Raleway" rel="stylesheet">
<style>
h1 { margin-top: 0; padding-top: 0; font-family: Raleway; }
body {
background: black;
background-image: url('starfield.png');
color: #ffffff;
font-size: 3vmin;
font-family: Dosis;
line-height: 1.5;
padding: 0;
margin: 0;
}
#stats, #txt, #genIDDiv {
top: 1em;
padding: 1em;
border-radius: 1em;
border: 2px solid rgba(222, 255, 255, 0.2);
background: url('boxbg.png');
}
#stats {
position: fixed;
right: 1em;
}
#txt {
padding-top: 0.5em;
position: fixed;
left: 1em;
width: 24em;
max-width: 25%;
}
#c {
position: fixed;
left: 2em;
width: 800px;
height: 800px;
}
</style>
<body>
<canvas id="c" width="800" height="800" onclick="genFromRandomID();"></canvas>
<div id="stats"></div>
<div id="txt">
</div>
<div id="DownloadDiv" style="width: 100% !important; position: absolute;">
<a download="planet.txt" id="downloadText" class="button"><img src="download-text.png"><div class="tooltip">Download Text</div></a>
<a download="Planet.png" id="download" onmouseover="writeImageData();" class="button"><img src="download-image.png"><div class="tooltip">Download Image</div></a>
<a id="mapSwitch" class="button" onclick="doMapSwitch();"><img src="map.png"><div class="tooltip">Switch to Map</div></a>
<a id="setID" class="button" onclick="setID();"><img src="id.png"><div class="tooltip">View/Set ID</div></a>
<a id="nextPlanet" class="button" onclick="genFromRandomID();"><img src="next.png"><div class="tooltip">Random New Planet</div></a>
</br>
</div>
<script id="2d-vertex-shader" type="notjs">
// an attribute will receive data from a buffer
attribute vec4 a_position;
// all shaders have a main function
void main() {
// gl_Position is a special variable a vertex shader
// is responsible for setting
gl_Position = a_position;
}
</script>
<script id="2d-fragment-shader" type="notjs">
// fragment shaders don't have a default precision so we need
// to pick one. mediump is a good default
precision mediump float;
void main() {
// gl_FragColor is a special variable a fragment shader
// is responsible for setting
gl_FragColor = vec4(1, 0, 0.5, 1); // return redish-purple
}
</script>
<script id="map-shader" type="notjs">
#ifdef GL_ES
precision highp float;
#endif
uniform int cities;
uniform float time;
uniform float left;
uniform float top;
uniform vec2 resolution;
uniform float angle;
uniform float rotspeed;
uniform float light;
uniform float zLight;
uniform float modValue;
uniform vec2 noiseOffset;
uniform vec2 noiseScale;
uniform vec2 noiseScale2;
uniform vec2 noiseScale3;
uniform vec2 cloudNoise;
uniform float cloudiness;
uniform float waterLevel;
uniform float rivers;
uniform float temperature;
uniform vec3 ocean;
uniform vec3 ice;
uniform vec3 cold;
uniform vec3 temperate;
uniform vec3 warm;
uniform vec3 hot;
uniform vec3 speckle;
uniform vec3 clouds;
uniform vec3 haze;
uniform vec3 lightColor;
//
// GLSL textureless classic 2D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson (stefan.gustavson@liu.se)
// Version: 2011-08-22
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / modValue)) * modValue;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec2 fade(vec2 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise, periodic variant
float pnoise(vec2 P, vec2 rep)
{
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
float spow(float x, float y) { float s = sign(x); return s * pow(s * x, y); }
vec4 planet(in vec2 pix) {
//vec2 p = -1.0 + 2.0 * pix;
//p.x *= resolution.x / resolution.y;
//vec2 uv = p;
vec3 col = vec3(0.0);
// uv.y * 2.0 - 100.5
// so if we take uv * 2 - 100.5 we get 0-1, which means if we have 0-1 we need to add 100.5 and halve
//uv = uv * 2.0 - vec2(100.5, 100.5);
vec2 uv = (pix + vec2(100.0, 99.9)) * vec2(0.125, 0.25);
float n2 = pnoise(uv * noiseScale2, noiseScale2) * 0.05;
float n = pnoise(uv * noiseScale, noiseScale) + n2;
float n3 = pnoise(uv * noiseScale3, noiseScale3);
float temp = cos(uv.y * 16.0) + n3 * 0.8 + n * 0.5 + temperature;
float oceanity = min(1.0, 1.0 - smoothstep(0.19, 0.2, n - waterLevel) + rivers * (1.0 - smoothstep(0.01, 0.04, mod(temp - uv.x * 35.0 + 0.3, 1.0) + n * n * 0.35))) * smoothstep(-0.9, -0.75, temp);
float iceity = max(0.0, 1.0 + waterLevel - oceanity - smoothstep(-0.8, -0.6, temp));
float specklity = max(0.0, step(0.009, n2 * n3) - iceity - oceanity);
float coldity = max(0.0, 1.0 - iceity - oceanity - specklity - smoothstep(-0.4, 0.0, temp));
float temperateity = max(0.0, 1.0 - iceity - coldity - oceanity - specklity - smoothstep(0.3, 0.8, temp));
float warmity = max(0.0, 1.0 - iceity - coldity - temperateity - oceanity - specklity - smoothstep(1.05, 1.3, temp));
float hottity = max(0.0, 1.0 - oceanity - iceity - coldity - temperateity - warmity - specklity);
col = ocean * oceanity + ice * iceity + cold * coldity + temperate * temperateity + warm * warmity + hot * hottity + speckle * specklity;
col *= (0.7 + abs(temp + n * 0.2) * 0.3);
col *= 0.92 + step(0.1, mod(n2, 0.4)) * 0.08;
col *= 1.0 + step(0.39, mod(n + uv.x, 0.4)) * 0.1;
if (
cities == 1 &&
(n > 0.05 && n < 0.2 ||// Next to the water
mod(uv.x * uv.y + n / 10.0, 1.0) < 0.1)
)
{
col += vec3(1.0, 1.0, 0.7) * (max(0.7, pnoise(uv * vec2(122.0, 122.0), vec2(122.0, 122.0))) - 0.7) * 3.0;
}
return vec4(col, 1.0);
}
void main(void)
{
vec3 coord = vec3(gl_FragCoord);
coord.x += left;
coord.y += top;
gl_FragColor = planet(coord.xy / resolution.xy), 1.0;
}
</script>
<script id="planet-shader" type="notjs">
#ifdef GL_ES
precision highp float;
#endif
uniform int cities;
uniform float time;
uniform float left;
uniform float top;
uniform vec2 resolution;
uniform float angle;
uniform float rotspeed;
uniform float light;
uniform float zLight;
uniform float modValue;
uniform vec2 noiseOffset;
uniform vec2 noiseScale;
uniform vec2 noiseScale2;
uniform vec2 noiseScale3;
uniform vec2 cloudNoise;
uniform float cloudiness;
uniform float waterLevel;
uniform float rivers;
uniform float temperature;
uniform vec3 ocean;
uniform vec3 ice;
uniform vec3 cold;
uniform vec3 temperate;
uniform vec3 warm;
uniform vec3 hot;
uniform vec3 speckle;
uniform vec3 clouds;
uniform vec3 haze;
uniform vec3 lightColor;
//
// GLSL textureless classic 2D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson (stefan.gustavson@liu.se)
// Version: 2011-08-22
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / modValue)) * modValue;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec2 fade(vec2 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise, periodic variant
float pnoise(vec2 P, vec2 rep)
{
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
float spow(float x, float y) { float s = sign(x); return s * pow(s * x, y); }
vec4 planet(in vec2 pix) {
vec2 p = -1.0 + 2.0 * pix;
p.x *= resolution.x / resolution.y;
float rot = time * rotspeed;
p = mat2(cos(angle), sin(angle), -sin(angle), cos(angle)) * p;
vec3 ro = vec3( 0.0, 0.0, 2.25 );
vec3 rd = normalize( vec3( p, -2.0 ) );
vec3 col = vec3(0.0);
// intersect sphere
float b = dot(ro,rd);
float c = dot(ro,ro) - 1.0;
float h = b*b - c;
float t = -b - sqrt(h);
vec3 pos = ro + t*rd;
vec3 nor = pos;
// texture mapping
vec2 uv;
uv.x = atan(nor.x,nor.z)/6.2831 + rot;
uv.y = acos(nor.y)/3.1416;
uv.y = 0.5 + spow(uv.y - 0.5, 1.2);
uv += noiseOffset;
float n2 = pnoise(uv * noiseScale2, noiseScale2) * 0.05;
float n = pnoise(uv * noiseScale, noiseScale) + n2;
float n3 = pnoise(uv * noiseScale3, noiseScale3);
float temp = cos(nor.y * 4.0) + n3 * 0.8 + n * 0.5 + temperature;
float oceanity = min(1.0, 1.0 - smoothstep(0.19, 0.2, n - waterLevel) + rivers * (1.0 - smoothstep(0.01, 0.04, mod(temp - uv.x * 35.0 + 0.3, 1.0) + n * n * 0.35))) * smoothstep(-0.9, -0.75, temp);
float iceity = max(0.0, 1.0 + waterLevel - oceanity - smoothstep(-0.8, -0.6, temp));
float specklity = max(0.0, step(0.009, n2 * n3) - iceity - oceanity);
float coldity = max(0.0, 1.0 - iceity - oceanity - specklity - smoothstep(-0.4, 0.0, temp));
float temperateity = max(0.0, 1.0 - iceity - coldity - oceanity - specklity - smoothstep(0.3, 0.8, temp));
float warmity = max(0.0, 1.0 - iceity - coldity - temperateity - oceanity - specklity - smoothstep(1.05, 1.3, temp));
float hottity = max(0.0, 1.0 - oceanity - iceity - coldity - temperateity - warmity - specklity);
col = ocean * oceanity + ice * iceity + cold * coldity + temperate * temperateity + warm * warmity + hot * hottity + speckle * specklity;
col *= (0.7 + abs(temp + n * 0.2) * 0.3);
col *= 0.92 + step(0.1, mod(n2, 0.4)) * 0.08;
col *= 1.0 + step(0.39, mod(n + uv.x, 0.4)) * 0.1;
float cloudN = max(0.0, pnoise((uv + vec2(rotspeed * time, 0)) * cloudNoise, cloudNoise) + cloudiness + n2);
col = col * (1.0 - cloudN) + clouds * cloudN;
float lighting = max(sin(light) * nor.y * 2.0 + cos(light) * nor.x * 2.0 + nor.z * zLight,0.0);
col = col * 0.2 + col * lightColor * lighting * 0.8;
if (
cities == 1 &&
lighting <= 0.8 &&
(n > 0.05 && n < 0.2 ||// Next to the water
mod(uv.x * uv.y + n / 10.0, 1.0) < 0.1)
)
{
col += vec3(1.0, 1.0, 0.7) * (1.0 - smoothstep(0.4, 0.8, lighting)) * (max(0.7, pnoise(uv * vec2(122.0, 122.0), vec2(122.0, 122.0))) - 0.7) * 3.0;
}
//return vec4(col, smoothstep(0.0, 8.0 / resolution.x, h));
//return vec4(clamp(col, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0)) * smoothstep(0.0, 8.0 / resolution.x, h) + haze * smoothstep(0.0, 30.0 / resolution.x, h + 0.1), smoothstep(0.0, 8.0 / resolution.x, h + 0.2));
float hazeAlpha = length(haze) * smoothstep(0.0, 30.0 / resolution.x, h + 0.1);
vec3 haze2 = normalize(haze);
//vec4 out = vec4(clamp(col, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0)) * smoothstep(0.0, 8.0 / resolution.x, h), smoothstep(0.0, 8.0 / resolution.x, h));
float solidity = smoothstep(0.0, 8.0 / resolution.x, h);
// + vec4(haze2, smoothstep(0.0, 30.0 / resolution.x, h + 0.1) * hazeAlpha);
return solidity * vec4(clamp(col + haze, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0)), 1.0) + (1.0 - solidity) * vec4(haze2, hazeAlpha);
}
void main(void)
{
vec3 coord = vec3(gl_FragCoord);
coord.x += left;
coord.y += top;
gl_FragColor = planet(coord.xy / resolution.xy), 1.0;
}
</script>
<script>
var canvas = document.getElementById("c");
var structs = {};
var slots = {};
function createShader(gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.log(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
}
function createProgram(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return program;
}
console.log(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
}
function resize(canvas) {
// Lookup the size the browser is displaying the canvas.
var displayWidth = canvas.clientWidth;
var displayHeight = canvas.clientHeight;
// Check if the canvas is not the same size.
if (canvas.width != displayWidth ||
canvas.height != displayHeight) {
// Make the canvas the same size
canvas.width = displayWidth;
canvas.height = displayHeight;
}
}
var gl = canvas.getContext("webgl", {
preserveDrawingBuffer: true,
premultipliedAlpha: false
});
var vertexShaderSource = document.getElementById("2d-vertex-shader").text;
var fragmentShaderSource = document.getElementById("planet-shader").text;
var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
var program = createProgram(gl, vertexShader, fragmentShader);
var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
var uCities = gl.getUniformLocation(program, "cities");
var uTime = gl.getUniformLocation(program, "time");
var uLeft = gl.getUniformLocation(program, "left");
var uTop = gl.getUniformLocation(program, "top");
var uResolution = gl.getUniformLocation(program, "resolution");
var uAngle = gl.getUniformLocation(program, "angle");
var uRotspeed = gl.getUniformLocation(program, "rotspeed");
var uLight = gl.getUniformLocation(program, "light");
var uZLight = gl.getUniformLocation(program, "zLight");
var uLightColor = gl.getUniformLocation(program, "lightColor");
var uModValue = gl.getUniformLocation(program, "modValue");
var uNoiseOffset = gl.getUniformLocation(program, "noiseOffset");
var uNoiseScale = gl.getUniformLocation(program, "noiseScale");
var uNoiseScale2 = gl.getUniformLocation(program, "noiseScale2");
var uNoiseScale3 = gl.getUniformLocation(program, "noiseScale3");
var uCloudNoise = gl.getUniformLocation(program, "cloudNoise");
var uCloudiness = gl.getUniformLocation(program, "cloudiness");
var uOcean = gl.getUniformLocation(program, "ocean");
var uIce = gl.getUniformLocation(program, "ice");
var uCold = gl.getUniformLocation(program, "cold");
var uTemperate = gl.getUniformLocation(program, "temperate");
var uWarm = gl.getUniformLocation(program, "warm");
var uHot = gl.getUniformLocation(program, "hot");
var uSpeckle = gl.getUniformLocation(program, "speckle");
var uClouds = gl.getUniformLocation(program, "clouds");
var uWaterLevel = gl.getUniformLocation(program, "waterLevel");
var uRivers = gl.getUniformLocation(program, "rivers");
var uTemperature = gl.getUniformLocation(program, "temperature");
var uHaze = gl.getUniformLocation(program, "haze");
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// three 2d points
var positions = [
-1, -1,
-1, 1,
1, 1,
-1, -1,
1, 1,
1, -1
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
var vWaterLevel = 0;
var vRivers = 0;
var vTemperature = 0;
var vCold = [0.5, 0.5, 0.5];
var vOcean = [0.5, 0.5, 0.5];
var vTemperate = [0.5, 0.5, 0.5];
var vWarm = [0.5, 0.5, 0.5];
var vHot = [0.5, 0.5, 0.5];
var vSpeckle = [0.5, 0.5, 0.5];
var vClouds = [0.9, 0.9, 0.9];
var vCloudiness = 0.35;
var vLightColor = [1.0, 1.0, 1.0];
var vHaze = [0.15, 0.15, 0.2];
var vAngle = 0.3;
var vRotspeed = 0.01;
var vLight = 1.9;
var vZLight = 0.5;
var vModValue = 29;
var vNoiseOffset = [0, 0];
var vNoiseScale = [11, 8];
var vNoiseScale2 = [200, 200];
var vNoiseScale3 = [50, 50];
var vCloudNoise = [6, 30];
function renderPlanet(sz) {
sz = sz || Math.round(Math.min(window.innerWidth, window.innerHeight) * 0.5);
jQuery("#c").
css("width", sz + "px").css("height", sz + "px").
css("top", (jQuery(window).innerHeight() / 2 - jQuery("#c").height() / 2) + "px").
css("left", (jQuery(window).innerWidth() / 2 - jQuery("#c").width() / 2) + "px");
resize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Clear the canvas
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
gl.enableVertexAttribArray(positionAttributeLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
var size = 2; // 2 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer(
positionAttributeLocation, size, type, normalize, stride, offset)
gl.uniform1i(uCities, 0);
gl.uniform1f(uTime, t * 0.001); // qqDPS
var shift = Math.round(sz / 40);
gl.uniform1f(uLeft, -shift);
gl.uniform1f(uTop, -shift);
var res = Math.round(sz * 0.95);
gl.uniform2f(uResolution, res, res);
gl.uniform1f(uAngle, vAngle);
gl.uniform1f(uRotspeed, vRotspeed);
gl.uniform1f(uLight, vLight);
gl.uniform1f(uZLight, vZLight);
gl.uniform3fv(uLightColor, vLightColor);
gl.uniform1f(uModValue, vModValue);
gl.uniform2fv(uNoiseOffset, vNoiseOffset);
gl.uniform2fv(uNoiseScale, vNoiseScale);
gl.uniform2fv(uNoiseScale2, vNoiseScale2);
gl.uniform2fv(uNoiseScale3, vNoiseScale3);
gl.uniform2fv(uCloudNoise, vCloudNoise);
gl.uniform1f(uCloudiness, vCloudiness);
gl.uniform3fv(uOcean, vOcean);
gl.uniform3f(uIce, 250/255.0, 250/255.0, 250/255.0);
gl.uniform3fv(uCold, vCold);//53/255.0, 102/255.0, 100/255.0);
gl.uniform3fv(uTemperate, vTemperate);//79/255.0, 109/255.0, 68/255.0);
gl.uniform3fv(uWarm, vWarm);//119/255.0, 141/255.0, 82/255.0);
gl.uniform3fv(uHot, vHot);//223/255.0, 193/255.0, 148/255.0);
gl.uniform3fv(uSpeckle, vSpeckle);
gl.uniform3fv(uClouds, vClouds);
gl.uniform3fv(uHaze, vHaze);
gl.uniform1f(uWaterLevel, vWaterLevel);
gl.uniform1f(uRivers, vRivers);
gl.uniform1f(uTemperature, vTemperature);
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = 6;
gl.drawArrays(primitiveType, offset, count);
}
renderPlanet();
var vertexShaderSource2 = document.getElementById("2d-vertex-shader").text;
var fragmentShaderSource2 = document.getElementById("map-shader").text;
var vertexShader2 = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource2);
var fragmentShader2 = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource2);
var program2 = createProgram(gl, vertexShader2, fragmentShader2);
var positionAttributeLocation2 = gl.getAttribLocation(program2, "a_position");
var u2Cities = gl.getUniformLocation(program2, "cities");
var u2Time = gl.getUniformLocation(program2, "time");
var u2Left = gl.getUniformLocation(program2, "left");
var u2Top = gl.getUniformLocation(program2, "top");
var u2Resolution = gl.getUniformLocation(program2, "resolution");
var u2Angle = gl.getUniformLocation(program2, "angle");
var u2Rotspeed = gl.getUniformLocation(program2, "rotspeed");
var u2Light = gl.getUniformLocation(program2, "light");
var u2ZLight = gl.getUniformLocation(program2, "zLight");
var u2LightColor = gl.getUniformLocation(program2, "lightColor");
var u2ModValue = gl.getUniformLocation(program2, "modValue");
var u2NoiseOffset = gl.getUniformLocation(program2, "noiseOffset");
var u2NoiseScale = gl.getUniformLocation(program2, "noiseScale");
var u2NoiseScale2 = gl.getUniformLocation(program2, "noiseScale2");
var u2NoiseScale3 = gl.getUniformLocation(program2, "noiseScale3");
var u2CloudNoise = gl.getUniformLocation(program2, "cloudNoise");
var u2Cloudiness = gl.getUniformLocation(program2, "cloudiness");
var u2Ocean = gl.getUniformLocation(program2, "ocean");
var u2Ice = gl.getUniformLocation(program2, "ice");
var u2Cold = gl.getUniformLocation(program2, "cold");
var u2Temperate = gl.getUniformLocation(program2, "temperate");
var u2Warm = gl.getUniformLocation(program2, "warm");
var u2Hot = gl.getUniformLocation(program2, "hot");
var u2Speckle = gl.getUniformLocation(program2, "speckle");
var u2Clouds = gl.getUniformLocation(program2, "clouds");
var u2WaterLevel = gl.getUniformLocation(program2, "waterLevel");
var u2Rivers = gl.getUniformLocation(program2, "rivers");
var u2Temperature = gl.getUniformLocation(program2, "temperature");
var u2Haze = gl.getUniformLocation(program2, "haze");
function renderMap(sz) {
sz = sz || 1024;
jQuery("#c").
css("width", sz + "px").css("height", (sz / 2) + "px").
css("top", (jQuery(window).innerHeight() / 2 - jQuery("#c").height() / 2) + "px").
css("left", (jQuery(window).innerWidth() / 2 - jQuery("#c").width() / 2) + "px");
resize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Clear the canvas
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Tell it to use our program (pair of shaders)
gl.useProgram(program2);
gl.enableVertexAttribArray(positionAttributeLocation2);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
var size = 2; // 2 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer(
positionAttributeLocation2, size, type, normalize, stride, offset)
gl.uniform1i(u2Cities, 0);
gl.uniform1f(u2Time, t * 0.001); // qqDPS
var resFake = Math.round(380 / 1024 * sz);
var offsetFake = Math.round(-10 / 1024 * sz);
gl.uniform1f(u2Left, offsetFake);
gl.uniform1f(u2Top, offsetFake);
gl.uniform2f(u2Resolution, resFake, resFake);
gl.uniform1f(u2Angle, vAngle);
gl.uniform1f(u2Rotspeed, vRotspeed);
gl.uniform1f(u2Light, vLight);
gl.uniform1f(u2ZLight, vZLight);
gl.uniform3fv(u2LightColor, vLightColor);
gl.uniform1f(u2ModValue, vModValue);
gl.uniform2fv(u2NoiseOffset, vNoiseOffset);
gl.uniform2fv(u2NoiseScale, vNoiseScale);
gl.uniform2fv(u2NoiseScale2, vNoiseScale2);
gl.uniform2fv(u2NoiseScale3, vNoiseScale3);
gl.uniform2fv(u2CloudNoise, vCloudNoise);
gl.uniform1f(u2Cloudiness, vCloudiness);
gl.uniform3fv(u2Ocean, vOcean);
gl.uniform3f(u2Ice, 250/255.0, 250/255.0, 250/255.0);
gl.uniform3fv(u2Cold, vCold);//53/255.0, 102/255.0, 100/255.0);
gl.uniform3fv(u2Temperate, vTemperate);//79/255.0, 109/255.0, 68/255.0);
gl.uniform3fv(u2Warm, vWarm);//119/255.0, 141/255.0, 82/255.0);
gl.uniform3fv(u2Hot, vHot);//223/255.0, 193/255.0, 148/255.0);
gl.uniform3fv(u2Speckle, vSpeckle);
gl.uniform3fv(u2Clouds, vClouds);
gl.uniform3fv(u2Haze, vHaze);
gl.uniform1f(u2WaterLevel, vWaterLevel);
gl.uniform1f(u2Rivers, vRivers);
gl.uniform1f(u2Temperature, vTemperature);
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = 6;
gl.drawArrays(primitiveType, offset, count);
}
function rnd() {
return Math.random();
}
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
var t = new Date().getTime() % 1000000;
var doRenderMap = false;
function nextFrame() {
t = new Date().getTime() % 1000000;
if (doRenderMap) { renderMap(); } else { renderPlanet(); }
requestAnimationFrame(nextFrame);
}
// Once everything is set up, start game loop.
requestAnimationFrame(nextFrame);
function doParse(text) {
var struct = null;
var value = null;
text.split("\n").forEach(function(line) {
var k = line.split(" ")[0];
var v = line.substring(k.length + 1);
if (k.length == 0 || v.length == 0) { return; }
if (k == "struct") {
value = null;
struct = { slots: [], vals: {} };
structs[v] = struct;
return;
}
if (k == "slot") {
struct.slots.push(v);
if (!slots[v]) {
slots[v] = [];
}
return;
}
if (k == "blocker") {
value.blockers.push([v.split(" ")[0], v.split(" ")[1]]);
return;
}
if (slots[k]) {
struct = null;
value = {"id": v, "blockers": []};
slots[k].push(value);
return;
}
if (struct) {
struct.vals[k] = v;
} else {
value[k] = v;
}
});
//console.log(structs);
//console.log(slots);
}
var randHex = function(len) {
var maxlen = 8,
min = Math.pow(16,Math.min(len,maxlen)-1)
max = Math.pow(16,Math.min(len,maxlen)) - 1,
n = Math.floor( Math.random() * (max-min+1) ) + min,
r = n.toString(16);
while ( r.length < len ) {
r = r + randHex( len - maxlen );
}
return r;
};
var genID = "?";
function genFromRandomID() {
Math.seedrandom();
genFromID(randHex(8));
}
function genFromID(id) {
genID = id;
console.log(id);
Math.seedrandom(id);
var result = doGen("planet")
doDisplay(result);
var name = doExpand(result.struct.vals["desc"], result).split("</h1>")[0].replace("<h1>", "");
document.getElementById("download").download = name + " " + genID + ".png";
document.getElementById("downloadText").download = name + " " + genID + ".txt";
document.getElementById("downloadText").href = "data:text/plain," + encodeURI(doExpand(result.struct.vals["desc"], result).replace("<h1>", "").replace("</h1>", "\n\n").replace(/<br>/g, "\n") + "\n\n" +
"Habitability: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["hab"], result)))) * 10) + "%\n" +
"Size: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["sze"], result))))) + "\n" +
"Industry: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["min"], result))))) + "\n" +
"Science: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["sci"], result))))) + "\n\n" + id);
}
function setID() {
genFromID(prompt("Planet ID", genID) || genID);
}
function doGen(structID) {
var result = {"struct": structs[structID]};
structs[structID].slots.forEach(function(slot) {
var availableSlots = slots[slot].filter(function(value) {
return !value.blockers.some(function(blocker) {
var blockerSlot = blocker[0];
if (blockerSlot.indexOf(":") != -1) {
var blockerKey = blockerSlot.substring(blockerSlot.indexOf(":") + 1);
blockerSlot = blockerSlot.split(":")[0];
var blockerValue = blocker[1];
return result[blockerSlot] && result[blockerSlot][blockerKey] == blockerValue;
} else{
var blockerID = blocker[1];
return result[blockerSlot] && result[blockerSlot].id == blockerID;
}
});
});
if (availableSlots.length == 0) {
console.log(slot + " fail");
console.log(result);
availableSlots = slots[slot]; // qqDPS
}
result[slot] = availableSlots[Math.floor(rnd() * availableSlots.length)];
});
return result;
}
function doDisplay(result) {
jQuery("body").css("background-position", Math.ceil(rnd() * 2000) + "px " + Math.ceil(rnd() * 2000) + "px");
/*jQuery("#c").
css("top", (jQuery(window).innerHeight() / 2 - jQuery("#c").height() / 2) + "px").
css("left", (jQuery(window).innerWidth() / 2 - jQuery("#c").width() / 2) + "px");*/
//jQuery("#setID").html("ID: " + genID);
jQuery("#txt").html(doExpand(result.struct.vals["desc"], result));
jQuery("#stats").html(
"Habitability: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["hab"], result)))) * 10) + "%<br>" +
"Size: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["sze"], result))))) + "<br>" +
"Industry: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["min"], result))))) + "<br>" +
"Science: " + (Math.max(1, Math.min(9, eval(doExpand(result.struct.vals["sci"], result)))))
);
vWaterLevel = eval(doExpand(result.struct.vals["watL"], result));
vTemperature = eval(doExpand(result.struct.vals["temp"], result));
vRivers = eval(doExpand(result.struct.vals["rive"], result));
vCold = eval(doExpand(result.struct.vals["coldC"], result));
vOcean = eval(doExpand(result.struct.vals["oceanC"], result)) || [0.05, 0.22, 0.38];
vTemperate = eval(doExpand(result.struct.vals["temperateC"], result));
vWarm = eval(doExpand(result.struct.vals["warmC"], result));
vHot = eval(doExpand(result.struct.vals["hotC"], result));
vSpeckle = eval(doExpand(result.struct.vals["speckleC"], result));
vLightColor = eval(doExpand(result.struct.vals["lightC"], result));
vHaze = eval(doExpand(result.struct.vals["hazeC"], result)) || [0.15, 0.15, 0.2];
vCloudiness = Math.min(1.5, Math.max(0, eval(doExpand(result.struct.vals["clouds"], result))));
vClouds = eval(doExpand(result.struct.vals["cloudC"], result)) || [0.9, 0.9, 0.9];
vAngle = 0.6 * rnd();
vRotspeed = (0.005 + rnd() * 0.01) * (rnd() < 0.3 ? -1 : 1) * eval(doExpand(result.struct.vals["rotspeedMult"], result));;
vLight = 4 * rnd();
vZLight = 0.2 + rnd();
vModValue = 17 + Math.ceil(rnd() * 20);
vNoiseOffset = [Math.ceil(rnd() * 100), Math.ceil(rnd() * 100)];
vNoiseScale = [6 + Math.ceil(rnd() * 8), 5 + Math.ceil(rnd() * 6)];
var sc = 80 + Math.ceil(rnd() * 220);
vNoiseScale2 = [sc, sc];
sc = 20 + Math.ceil(rnd() * 80);
vNoiseScale3 = [sc, sc];
vCloudNoise = [4 + Math.ceil(rnd() * 9), 20 + Math.ceil(rnd() * 20)];
}
function doExpand(txt, context) {
if (!txt) { return ""; }
if (txt.indexOf("{") == -1) { return txt; }
return txt.replace(/[{]([^}]*)[}]/g, function(m, capture) {
if (capture.indexOf(":") == -1) {
return context[capture].id;
} else {
var slot = capture.split(":")[0];
var prop = capture.substring(slot.length + 1);
return doExpand(context[slot][prop], context);
}
});
}
jQuery.ajax({
url: "data.txt?" + (new Date()).getTime(),
success: function(txt) { doParse(txt); genFromRandomID(); }
});
function writeImageData() {
if (doRenderMap) {
renderMap(2048);
} else {
renderPlanet(2048);
}
document.getElementById("download").href = canvas.toDataURL();
}
/*setInterval(function(){
document.getElementById("download").href = canvas.toDataURL();
}, 1000);*/
function doMapSwitch() {
doRenderMap = !doRenderMap;
document.getElementById("mapSwitch").innerHTML = doRenderMap ? '<img src="planet.png">' : '<img src="map.png">';
}
</script>
<style>
.button {
top: 1em;
position:relative;
cursor: pointer;
text-decoration: none !important;
color: white;
}
.button img {
width: 40px;
}
.button .tooltip {
position: absolute;
display: none;
top: 40px;
left: 0px;
width: 300px;
text-align: left;
}
.button:hover .tooltip {
display: block;
}
#download, #mapSwitch, #setID, #nextPlanet {
margin-left: 1em;
}