-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_readable.html
2008 lines (1821 loc) · 68 KB
/
index_readable.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
<html>
<head>
<style>
/* use the full browser window and hide anything that doesn't fit inside it */
body
{
margin: 0;
overflow: hidden;
}
/* this is the class used for the title, with the fancy animated stroke effect - google 'css ants' for an explanation */
.a
{
fill: #09d;
stroke: white;
stroke-dasharray: 7% 28%;
stroke-width: 3px;
animation: c 9s infinite ease-in;
}
.a:nth-child(1) {
stroke: #ff9800;
stroke-dashoffset: 7%;
}
.a:nth-child(2) {
stroke: #ff0;
stroke-dashoffset: 14%;
}
.a:nth-child(3) {
stroke: #8f8;
stroke-dashoffset: 21%;
}
.a:nth-child(4) {
stroke: #fff;
stroke-dashoffset: 28%;
}
.a:nth-child(5) {
stroke: transparent;
stroke-dashoffset: 35%;
}
/* font used by the title */
.d
{
font: 6vw Arial;
font-weight: bolder;
}
/* stroke animation keyframes */
@keyframes c {
50% {
stroke-dashoffset: 35%;
stroke-dasharray: 0 87.5%;
}
}
/* text with fading white shadow used for feedback messages (ok, well done, and so on) */
.b{
letter-spacing: -7px;
font-weight:700;
animation:e 1s ease-out;
opacity: 0;
position: absolute;
left: 0;
width: 90%;
text-shadow:0 0 5px #fff,
0 0 7px #fff;
text-align:right;
color:#fff;
top: 20%;
font-size: 6em;
}
/* text with fading black shadow used for the 'time bonus' message */
.l{
letter-spacing: -7px;
font-weight:700;
animation:e 1s ease-out;
opacity: 0;
position: absolute;
left: 0;
text-shadow:0 0 5px #000,
0 0 7px #000;
text-align:left;
color:#000;
bottom: 20%;
font-size: 4vw;
}
/* fading text shadow for large text */
@keyframes e{
from{
opacity: 1;
text-shadow:0 0 10px #fff,
0 0 10px #fff,
0 0 25px #fff,
0 0 25px #fff,
0 0 25px #fff,
0 0 25px #fff,
0 0 25px #fff,
0 0 25px #fff,
0 0 50px #fff,
0 0 50px #fff,
0 0 50px #7B96B8,
0 0 150px #7B96B8,
0 10px 100px #7B96B8,
0 10px 100px #7B96B8,
0 10px 100px #7B96B8,
0 10px 100px #7B96B8,
0 -10px 100px #7B96B8,
0 -10px 100px #7B96B8;}
}
/* fading text shadow for tiny text */
@keyframes f{
from{
opacity: 1;
text-shadow:0 0 2px #fff,
0 0 2px #fff,
0 0 5px #fff,
0 0 5px #fff,
0 0 5px #fff,
0 0 5px #fff,
0 0 5px #fff,
0 0 5px #fff,
0 0 10px #fff,
0 0 10px #fff,
0 0 10px rgba(255,255,255,0.75),
0 0 30px rgba(255,255,255,0.75),
0 2px 20px rgba(255,255,255,0.75),
0 2px 20px rgba(255,255,255,0.75),
0 2px 20px rgba(255,255,255,0.75),
0 2px 20px rgba(255,255,255,0.75),
0 -2px 20px rgba(255,255,255,0.75),
0 -2px 20px rgba(255,255,255,0.75);}
}
/* simplified 3d text effect for small screens (see media query below for the full thing) */
.g {
color: #fff;
font: 3vw Helvetica;
letter-spacing: .2em;
font-weight: bold;
text-shadow: 0 1px 0 #ccc, 0 2px 0 #c9c9c9;
}
/* class for text with pulsating white shadow (used in 'loading' text and text inside buttons) */
.h {
font: 4vw "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
font-weight: bolder;
text-rendering: optimizeLegibility;
color: #ff9800;
letter-spacing: .15em;
animation: f 1s linear alternate infinite;
}
/* class used for buttons */
.i
{
border-radius: 4vw;
box-shadow: 0 0 1vw white;
padding: 2vw;
margin: auto;
cursor: pointer;
text-align: center;
left:0;
right: 0;
position: absolute;
width: 20%;
bottom: 2%;
}
/* the dots representing colors when it says 'remember this sequence' */
.j
{
width: 5vw;
height: 5vw;
border-radius: 2.5vw;
margin: 5% 0.5%;
margin-top: 100%;
display: inline-block;
vertical-align: top;
box-shadow: 0 0 2vw #000;
transition: margin-top 1s, width 1s, height 1s;
}
/* dummy class to make reflowing less expensive when restarting css animations */
.k
{
display: none;
position: absolute;
}
/* what happens when the window is big enough */
@media (min-width: 800px)
{
/* title can be bigger and proportional to the width of the screen */
.d
{
font-size: 2vw;
}
/* cool 3d text effect made with shadows */
.g
{
text-shadow: 0 1px 0 #ccc, 0 2px 0 #c9c9c9, 0 3px 0 #bbb, 0 4px 0 #b9b9b9, 0 5px 0 #aaa, 0 6px 1px rgba(0,0,0,.1), 0 0 5px rgba(0,0,0,.1), 0 1px 3px rgba(0,0,0,.3), 0 3px 5px rgba(0,0,0,.2), 0 5px 10px rgba(0,0,0,.25), 0 10px 10px rgba(0,0,0,.2), 0 20px 20px rgba(0,0,0,.15);
}
/* move the title a bit higher up */
#w
{
top: -100px;
}
}
</style>
</head>
<!-- when the page loads, wait half a second to be sure some stuff is displayed on the screen, then execute a function to actually load the game -->
<body onload="setTimeout(f,500)" onresize="s()">
<!-- this is our webgl canvas !-->
<canvas id="c"></canvas>
<!-- level menu -->
<div id="0" style="background:#0bf; margin:0; width:100%; height:100%; transition: opacity 1s; position: absolute; top: 0; overflow: hidden; display: none"> <!-- level menu -->
<div id='v' class='g' style="width:100%; margin-top: 1%; text-align: center;">
LEVEL <span id="!"></span><br/><br/>remember this sequence:<br/>
</div>
<div id="1" class="i" style="display: none">
<div class="h">OK</div>
</div>
</div>
<!-- game over menu -->
<div id="5" style="background:#0bf; margin:0; width:100%; height:100%; transition: opacity 1s; position: absolute; top: 0; display: none">
<div id='r' class='g' style="width:100%; margin-top: 1%; text-align: center;">
GAME OVER<br/><br/>score: <span id="p"></span><br/><span id="o"></span>
</div>
<div id='q' class="h" style="text-align:center">BEST SCORE</div>
<div id='6' class="i">
<div class="h">Retry</div>
</div>
</div>
<!-- loading screen -->
<div id="$" style="background:#0bf; margin:0; width:100%; height:100%; transition: opacity 1s; position: absolute; top: 0">
<div id='s' class='h' style='position: absolute; bottom: 10%; left: 10%'>
Loading
</div>
<div id="2" style="position:absolute; top: 20%; width: 100%">
<svg id="w" viewBox="0 0 400 60" style="position: relative; width: 100%; top: 0">
<symbol id="y">
<text x="10%" y="35" class="d" id=":">
R3V3RS3 IT!
</text>
</symbol>
<g id="3">
<use xlink:href="#y" class="a"></use>
<use xlink:href="#y" class="a"></use>
<use xlink:href="#y" class="a"></use>
<use xlink:href="#y" class="a"></use>
<use xlink:href="#y" class="a"></use>
</g>
</svg>
<div id='t' class='g' style="position: relative; text-align: right; margin-left: 30%">
a web game in 10kb by GIO
</div>
</div>
</div>
<!-- in-game UI -->
<div id="_" style="position: absolute; width: 100%; height: 100%; top: 0; display: none">
<svg viewBox="0 0 400 20" version="1.1" style="width: 100%">
<radialGradient id="g0">
<stop offset="0%" stop-color="#000" />
<stop offset="100%" stop-color="#002" />
</radialGradient>
<radialGradient id="g1">
<stop offset="0%" stop-color="#224" />
<stop offset="50%" stop-color="#aaf" />
<stop offset="100%" stop-color="#224" />
</radialGradient>
<linearGradient id="g3">
<stop offset="0%" stop-color="#224" />
<stop offset="100%" stop-color="#aaf" />
</linearGradient>
<linearGradient id="g4">
<stop offset="0%" stop-color="#aaf" />
<stop offset="100%" stop-color="#224" />
</linearGradient>
<path d="M 200 20 Q 300 0 400 10 L 400 -2 L 200 -2 Z" stroke="url(#g1)" fill="url(#g3)" stroke-width="0.5"/>
<path d="M 200 20 Q 100 0 0 10 L 0 -2 L 200 -2 Z" stroke="url(#g1)" fill="url(#g4)" stroke-width="0.5"/>
<circle cx="200" cy="10" r="9" stroke="url(#g1)" fill="url(#g0)" stroke-width="2"/>
</svg>
<svg viewBox="0 0 400 20" version="1.1" style="width: 100%; position:absolute; bottom: 0; left: 0">
<radialGradient id="g0">
<stop offset="0%" stop-color="#000" />
<stop offset="100%" stop-color="#002" />
</radialGradient>
<radialGradient id="g1">
<stop offset="0%" stop-color="#224" />
<stop offset="50%" stop-color="#aaf" />
<stop offset="100%" stop-color="#224" />
</radialGradient>
<linearGradient id="g3">
<stop offset="0%" stop-color="#224" />
<stop offset="100%" stop-color="#aaf" />
</linearGradient>
<linearGradient id="g4">
<stop offset="0%" stop-color="#aaf" />
<stop offset="100%" stop-color="#224" />
</linearGradient>
<path d="M 200 0 Q 300 20 400 10 L 400 22 L 200 22 Z" stroke="url(#g1)" fill="url(#g3)" stroke-width="0.5"/>
<path d="M 200 0 Q 100 20 0 10 L 0 22 L 200 22 Z" stroke="url(#g1)" fill="url(#g4)" stroke-width="0.5"/>
<ellipse cx="200" cy="10" rx="27" ry="9" stroke="url(#g1)" fill="url(#g0)" stroke-width="2"/>
</svg>
<svg id="9" viewBox="0 0 400 20" version="1.1" style="width: 100%; position:absolute; bottom: 0; left: 0; transition: opacity 0.3s; opacity: 0">
<radialGradient id="g5">
<stop offset="0%" stop-color="#0ff" />
<stop offset="100%" stop-color="#8ff" />
</radialGradient>
<ellipse cx="200" cy="10" rx="27" ry="9" stroke="transparent" fill="url(#g5)" stroke-width="2"/>
</svg>
<div id="." style="font: 2.5vw 'Lucida Console', Monaco, monospace; color: white; text-align:center; position:absolute; top: 1.45vw; width: 100%"></div>
<div id="," style="font: 2.5vw 'Lucida Console', Monaco, monospace; color: #006687; text-align:center; position:absolute; bottom: 1.2vw; width: 100%; text-shadow: 0 0 0.2vw black"></div>
</div>
<!-- feedback message -->
<div id="%" class="b"></div>
<!-- time bonus message -->
<div id="+" class="l"></div>
<!-- all our webgl shaders -->
<div style="display:none">
<div id="a"> <!-- terrain vertex shader -->
attribute vec4 v;
attribute vec3 n;
uniform mat4 w2s, w2c;
varying vec4 normal, screenPosition;
varying vec3 uv, position, viewDir;
void main(void)
{
gl_Position = screenPosition = w2s * vec4(v.xyz, 1.);
normal = vec4(n, v.y);
position = v.xyz;
viewDir = (w2c * vec4(-v.xyz, 1.)).xyz;
uv = vec3(fract(v.w) * 1.001, floor(v.w) / 10000., w2c[1].y);
}
</div>
<div id="b"> <!-- terrain pixel shader -->
#extension GL_OES_standard_derivatives : enable
precision mediump float;
varying vec4 normal, screenPosition;
varying vec3 uv, position, viewDir;
uniform sampler2D s0, s1, s2, s3, s4;
uniform vec4 uniformData;
void main(void)
{
vec3 ambient = vec3(0.1, 0.05, 0.);
vec3 lightDir = vec3(0.41, -0.82, 0.41);
vec3 t0 = texture2D(s0, uv.xy * 37.).xyz;
vec3 t1 = texture2D(s1, uv.xy * 49.).xyz;
vec3 t2 = texture2D(s2, uv.xy * 51.).xyz;
vec4 noise = texture2D(s3, uv.xy * 40.);
vec2 tc = fract(uv.xy * 0.5 + uniformData.y / 1000.);
vec4 clouds = texture2D(s3, tc) * smoothstep(0.,0.03,tc.x) * smoothstep(0.,0.03,1.-tc.x) * smoothstep(0.,0.03,tc.y) * smoothstep(0.,0.03,1.-tc.y);
float i = pow(normal.y, 8.);
vec3 texCol = mix(t2, t1, i);
float m = clamp((normal.w - 3. - noise.x) / 3., 0., 1.);
texCol = mix(t0, texCol, m) * (1. - clouds.x / 2.) ;
vec3 n = normal.xyz;
~
highp float ndotl= max(0., -dot(lightDir, n));
vec3 halfDir = normalize(-lightDir + normalize(viewDir));
float specular = max(0., pow(dot(halfDir, n), 4.) * (1. - i * 0.9) * (1. - m * 0.9));
vec3 outColor = ndotl * texCol + ambient + specular * length(texCol.xyz) * vec3(0.9, 0.8, 0.6);
gl_FragColor = vec4(outColor, 1.);
}
</div>
<div id="e"> <!-- sky vertex shader -->
attribute vec4 v;
uniform mat4 w2s, w2c;
uniform vec4 uniformData;
varying vec4 uvt;
void main(void)
{
gl_Position = vec4(v.xy, 0.999, 1.);
float angle = atan(w2c[2].z, w2c[2].x) / 6.282 + 0.5;
uvt = vec4(fract(v.w) * uniformData.x / 8. - angle, floor(v.w) / 80000., uniformData.y, angle);
}
</div>
<div id="f"> <!-- sky pixel shader -->
precision mediump float;
varying vec4 uvt;
uniform sampler2D s0;
void main(void)
{
float sunFactor = pow(sin(uvt.w * 3.14), 5.);
vec3 skyColor = mix(vec3(0.5,0.5,1.), vec3(1.,0.8,0.5), clamp(uvt.y*10., 0., 1.)) + sunFactor * vec3(0.8,0.3,0.1);
vec2 tc = fract(vec2(uvt.x + uvt.z * 0.001, uvt.y));
vec4 texCol = texture2D(s0, tc);
vec3 cloudColor = mix(pow(texCol.xxx, vec3(0.25)), vec3(0.9,0.8,0.6), sunFactor) * (1. - pow(texCol.z, 4.));
vec4 col0 = vec4(cloudColor, clamp(texCol.x,0.,1.) * smoothstep(0.,0.03,tc.x) * smoothstep(0.,0.03,1.-tc.x));
tc = fract(vec2(uvt.x + uvt.z * 0.00021 + 0.5, uvt.y));
texCol = texture2D(s0, tc);
cloudColor = mix(pow(texCol.yyy, vec3(0.66,0.66,0.66)), vec3(1.,0.5,0.5), sunFactor) * (1. - pow(texCol.z, 4.));
vec4 col1 = vec4(cloudColor, clamp(2. * texCol.y - 1.,0.,1.) * smoothstep(0.,0.03,tc.x) * smoothstep(0.,0.03,1.-tc.x));
gl_FragColor = vec4((skyColor * (1. - col0.w) + col0.xyz * col0.w) * (1. - col1.w) + col1.xyz * col1.w,1.);
}
</div>
<div id="g"> <!-- water pixel shader -->
precision mediump float;
varying vec4 normal, screenPosition;
varying vec3 uv;
uniform vec4 uniformData;
uniform sampler2D s0, s1, s2, s3;
@
void main(void)
{
float height = texture2D(s1, fract(clamp(uv.xy * 3., 1.001, 1.999) * vec2(1., -1.))).x * 15.;
vec4 texColor = texture2D(s0, uv.xy * 54. + sin(uniformData.y * 0.1) / 25. + sin(uniformData.yy * 0.1 + uv.xy * 100.) / 10.);
texColor.w = height > 1.? 0. : pow(clamp((1. - height) * 5., 0., 1.), 1.7);
vec2 c = (screenPosition.xy * 0.5 / screenPosition.w + 0.5);
c.y = 1. - c.y;
vec2 dist;
dist.x = sin(uv.x * 1000. + sin(uniformData.y * 0.4) + height * 20.);
dist.y = sin(uv.y * 868. + cos(uniformData.y * 0.7) + height * 20.);
float fade = 1. - pow(screenPosition.z * 0.5 / screenPosition.w + 0.5, 50.);
dist *= fade;
c += dist * 0.01;
vec4 reflection = texture2D(s2, c);
texColor = mix(texColor, reflection, texColor.w * clamp(uv.z, 0., 1.) * 0.8 * clamp(1. - (uniformData.z - 2.5) / 12., 0., 1.));
vec4 z = texture2D(s3, uv.xy * 100. + sin(uniformData.yy * 0.3 + dist.yx * 0.2) * 0.1);
float threshold = 0.3 + z.x * z.y;
float specular = smoothstep(threshold - 0.1, threshold + 0.1, pow(nestedNoise(uv.xy * 600., uniformData.y / 5.), 5.)) * pow(fade, 0.3) * texColor.w * 0.5;
texColor.rgb += specular;
gl_FragColor = texColor;
}
</div>
<div id="h"> <!-- post process vertex shader -->
attribute vec4 v;
varying vec2 uv;
void main(void)
{
uv = vec2(fract(v.w) * 1.001, floor(v.w) / 10000.);
gl_Position = vec4(v.xyz, 1.);
}
</div>
<div id="i"> <!-- post process pixel shader -->
precision mediump float;
varying vec2 uv;
uniform sampler2D s0;
uniform vec4 uniformData, tintColor;
void main(void)
{
vec2 t = uv * 2. - 1.;
float d = abs(sin(length(t) * 6.28 - uniformData.y));
float m = min(tintColor.r, min(tintColor.g, tintColor.b));
float s = clamp(d + m, 0., 1.);
vec3 c = texture2D(s0, uv + (1. - tintColor.w) * (1. - s) * sin(t) / 4.).xyz;
vec3 effect = mix(c, tintColor.xyz, (1. - s) * 0.5);
vec3 tint = mix(dot(c, vec3(0.299, 0.587, 0.114)) * tintColor.xyz, c, pow(m, 4.));
gl_FragColor = vec4(mix(effect, tint, tintColor.w), 1.);
}
</div>
<div id="j"> <!-- marker vertex shader -->
attribute vec4 v;
attribute vec3 n;
uniform mat4 w2c, c2s;
uniform vec4 uniformData;
uniform vec4 positions[16];
varying vec3 uv;
varying vec4 color;
void main(void)
{
uv = vec3(vec2(fract(v.w) * 1.001, floor(v.w) / 10000.) * 2. - 1., uniformData.y / 4.);
vec4 pos = positions[int(v.x)];
color = vec4(n.xyz, pos.w);
gl_Position = c2s * (w2c * vec4(pos.xyz, 1.) + vec4(uv.xy * (3. + abs(sin(uv.z))), 0., 0.));
}
</div>
<div id="k"> <!-- marker pixel shader -->
precision mediump float;
uniform sampler2D s0;
varying vec3 uv;
varying vec4 color;
@
void main(void)
{
vec3 c = color.xyz + nestedNoise(uv.xy + uv.z, uv.z) * mix(color.zxy, vec3(1), abs(sin(uv.z)));
gl_FragColor = vec4(c, clamp(1. - pow(length(uv.xy), 2.), 0., 1.) * color.w);
}
</div>
<div id="l"> <!--ui clouds vertex shader-->
attribute vec4 v;
uniform mat4 w2s, w2c;
uniform vec4 uniformData;
varying vec3 uv;
void main(void)
{
gl_Position = vec4(v.xy, -0.999, 1.);
uv = vec3(v.xy / 2. + 0.5, uniformData.y / 10.);
}
</div>
<div id="m"> <!-- cool fade out effect-->
precision mediump float;
varying vec3 uv;
uniform vec4 tintColor;
@
void main(void)
{
float n = sin(3.14 * nestedNoise(uv.xy * 8., uv.z));
vec3 c = vec3(0.69,0.94,1.);
gl_FragColor = vec4(c, n * tintColor.w + pow(tintColor.w, 8.));
}
</div>
<div id="x"> // GPU-based noise function
float noise(vec2 p)
{
return fract(sin(p.x + p.y*100.) * 100.);
}
vec2 sw(vec2 p) {return vec2( floor(p.x) , floor(p.y) );}
vec2 se(vec2 p) {return vec2( ceil(p.x) , floor(p.y) );}
vec2 nw(vec2 p) {return vec2( floor(p.x) , ceil(p.y) );}
vec2 ne(vec2 p) {return vec2( ceil(p.x) , ceil(p.y) );}
float smoothNoise(vec2 p)
{
vec2 inter = smoothstep(0., 1., fract(p));
float s = mix(noise(sw(p)), noise(se(p)), inter.x);
float n = mix(noise(nw(p)), noise(ne(p)), inter.x);
return mix(s, n, inter.y);
}
float movingNoise(vec2 p, float time)
{
float total = 0.;
total += smoothNoise(p - time);
total += smoothNoise(p*2. + time) / 2.;
total += smoothNoise(p*4. - time) / 4.;
total += smoothNoise(p*8. + time) / 8.;
total += smoothNoise(p*16. - time) / 16.;
total /= 1. + 1./2. + 1./4. + 1./8. + 1./16.;
return total;
}
float nestedNoise(vec2 p, float time)
{
float x = movingNoise(p, time);
float y = movingNoise(p + 100., time);
return movingNoise(p + vec2(x, y), time);
}
</div>
<div id="~">
// bump mapping shader code that may or may not be supported by a user's hardware (because of dFdx/dFdy), so it's included conditionally
vec4 bump = texture2D(s4, uv.xy * (50. + 200. * m)) - 0.5;
bump.xy = mix(bump.zw, bump.xy, m);
vec3 dpdx = dFdx(position);
vec3 dpdy = dFdy(position);
vec2 dudx = dFdx(uv.xy) * uniformData.w * (1. + 4. * m) * uniformData.x;
vec2 dudy = dFdy(uv.xy) * uniformData.w * (1. + 4. * m);
float dhdx = dot(bump.xy, dudx);
float dhdy = dot(bump.xy, dudy);
vec3 r1 = cross(dpdy, n);
vec3 r2 = cross(n, dpdx);
vec3 grad = (r1 * dhdx + r2 * dhdy) / dot(dpdx, r1);
n = normalize(n - grad * 0.1 + (1. - m * 0.2));
</div>
</div>
<script>
// let's wrap everything in a top-level anonymous function so the minifier knows that all the var's inside this are not actually global and can be shortened
(function()
{
// these are defined just for brevity; it doesn't help with the overall size because the zip algorithm compresses duplicate strings very well,
// but it means I have to type less. Except now I've written this long comment, so all in all it's completely useless.
var d = document;
d.e = d.getElementById;
var F = Float32Array;
var e = addEventListener;
// a bunch of useful globals
var gl, // the webgl context
canvas, // the webgl canvas element
glClearMask, // the mask used to clear the webgl buffer
worldToClip, // a matrix to go from world space to clip space
cameraToClip, // a matrix to go from camera space to clip space
uniformData, // a vector of useful values to use in shaders (time, aspect ratio etc)
mainFramebuffer, // the current frame buffer
previousFramebuffer, // the previous frame's frame buffer
mainFrameTexture, // the current frame buffer to be used as a texture
previousFrameTexture, // the previous frame buffer to be used as a texture
composite, // the full-screen quad used in the final composite pass
renderbuffer, // the current render buffer
markerSpeed, // the speed of balls. When I say marker, I mean ball
numMarkers, // number of balls
score, // current score
markersObject, // balls - literally
levelStartTime, // when we started the current level
levelTime, // current time left for the current level
musicToggle, // function to toggle music tracks on
musicStop, // function to stop music
musicPlay, // function to play music, no shit
musicInitialized, // whether the music has been initialized
lastLoopTime, // the last time we drew something onto the screen
timeAdjust, // time scale factor to compensate for slow frames
stdDerivativesSupported, // whether we can use dFdx and dFdy in our shaders (for bump mapping)
theta; // current camera rotation angle along the x axis
var cameraPosition = [0, 1.2, 110]; // where the camera is in the world
var keysDown = {}; // which keys are currently pressed
var heightMap = []; // the terrain height map
var sceneObjects = []; // objects in the scene
var time = 0; // global time for cool shader effects
var speed = 0; // our own movement speed
var rotationSpeed = 0; // our own rotation speed
var markerPositions = []; // the position of balls in the world
var markerColors = []; // the colors of balls
var markerTargets = []; // where balls are currently moving to
var tintColor = new F([1, 1, 1, 1]); // a global tint color that is used for both the 'game over' and the 'correct ball' effects in the composite shader
var gameState = 'loading'; // current game state
var currentLevel = 0; // current level
var worldToCamera = [1, 0, 0, 0, // a matrix to go from world space to camera space
0, 0, 1, 0,
0, -1, 0, 0,
0, -1.2, -150, 1];
// an array of gradients (for noise calculations)
var grad3 = [[1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0], [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1], [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1]];
// a special dot product function used in perlin noise calculations
var perlinDot = function (g, x, y, z)
{
return g[0] * x + g[1] * y + g[2] * z;
};
// linear interpolation
var mix = function (a, b, t)
{
return (1 - t) * a + t * b;
};
// fade function - and right here I'm stopping writing useless comments
var fade = function (t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
};
// classic Perlin noise in 3D (inspired by an implementation I found somewhere on github)
var NoiseGenerator = function (numOctaves, attenuation, roughness, startingOctave)
{
startingOctave = startingOctave || 0;
var p = [];
for (var i = 0; i < 256; i++)
{
p[i] = Math.floor(Math.random() * 256);
}
// To remove the need for index wrapping, double the permutation table length
var perm = [];
for (i = 0; i < 512; i++)
{
perm[i] = p[i & 255];
}
var n = function (x, y, z)
{
// Find unit grid cell containing point
var X = Math.floor(x);
var Y = Math.floor(y);
var Z = Math.floor(z);
// Get relative xyz coordinates of point within that cell
x = x - X;
y = y - Y;
z = z - Z;
// Wrap the integer cells at 255 (smaller integer period can be introduced here)
X = X & 255;
Y = Y & 255;
Z = Z & 255;
// Calculate a set of eight hashed gradient indices
var gi000 = perm[X + perm[Y + perm[Z]]] % 12;
var gi001 = perm[X + perm[Y + perm[Z + 1]]] % 12;
var gi010 = perm[X + perm[Y + 1 + perm[Z]]] % 12;
var gi011 = perm[X + perm[Y + 1 + perm[Z + 1]]] % 12;
var gi100 = perm[X + 1 + perm[Y + perm[Z]]] % 12;
var gi101 = perm[X + 1 + perm[Y + perm[Z + 1]]] % 12;
var gi110 = perm[X + 1 + perm[Y + 1 + perm[Z]]] % 12;
var gi111 = perm[X + 1 + perm[Y + 1 + perm[Z + 1]]] % 12;
// Calculate noise contributions from each of the eight corners
var n000 = perlinDot(grad3[gi000], x, y, z);
var n100 = perlinDot(grad3[gi100], x - 1, y, z);
var n010 = perlinDot(grad3[gi010], x, y - 1, z);
var n110 = perlinDot(grad3[gi110], x - 1, y - 1, z);
var n001 = perlinDot(grad3[gi001], x, y, z - 1);
var n101 = perlinDot(grad3[gi101], x - 1, y, z - 1);
var n011 = perlinDot(grad3[gi011], x, y - 1, z - 1);
var n111 = perlinDot(grad3[gi111], x - 1, y - 1, z - 1);
// Compute the fade curve value for each of x, y, z
var u = fade(x);
var v = fade(y);
var w = fade(z);
// Interpolate along x the contributions from each of the corners
var nx00 = mix(n000, n100, u);
var nx01 = mix(n001, n101, u);
var nx10 = mix(n010, n110, u);
var nx11 = mix(n011, n111, u);
// Interpolate the four results along y
var nxy0 = mix(nx00, nx10, v);
var nxy1 = mix(nx01, nx11, v);
// Interpolate the two last results along z
return mix(nxy0, nxy1, w);
};
this.noise = function (x, y, z)
{
var a = Math.pow(attenuation, -startingOctave);
var f = Math.pow(roughness, startingOctave);
var m = 0;
for (var i = startingOctave; i < numOctaves + startingOctave; i++)
{
m += n(x * f, y * f, z * f) * a;
a /= attenuation;
f *= roughness;
}
return m / numOctaves;
};
};
// on resize do this (do it once on load too)
window.s = function ()
{
if (gl)
{
canvas.width = mainFramebuffer.width = previousFramebuffer.width = innerWidth;
canvas.height = mainFramebuffer.height = previousFramebuffer.height = innerHeight;
var oldRenderbuffer = renderbuffer;
renderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, canvas.width, canvas.height);
gl.bindTexture(gl.TEXTURE_2D, mainFrameTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, mainFramebuffer.width, mainFramebuffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.bindTexture(gl.TEXTURE_2D, previousFrameTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, mainFramebuffer.width, mainFramebuffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, mainFramebuffer);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
gl.bindFramebuffer(gl.FRAMEBUFFER, previousFramebuffer);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
gl.viewport(0, 0, canvas.width, canvas.height);
oldRenderbuffer && gl.deleteRenderbuffer(oldRenderbuffer);
}
};
// on load do this
window.f = function ()
{
// create a canvas
canvas = d.e('c');
gl = canvas.getContext('webgl', {premultipliedAlpha: false});
c.style.width = c.style.height = '100%';
// set up web gl
mainFramebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, mainFramebuffer);
mainFrameTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, mainFrameTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, canvas.width, canvas.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, mainFrameTexture, 0);
previousFramebuffer = gl.createFramebuffer();
previousFrameTexture = gl.createTexture();
gl.bindFramebuffer(gl.FRAMEBUFFER, previousFramebuffer);
gl.bindTexture(gl.TEXTURE_2D, previousFrameTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, canvas.width, canvas.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, previousFrameTexture, 0);
glClearMask = gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT;
gl.clearColor(0, 0, 0, 1);
gl.clear(glClearMask);
gl.enable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
stdDerivativesSupported = gl.getExtension('OES_standard_derivatives');
// resize
s();
var createObject = function (vertexSource, fragmentSource, vertices, indices, normals, textures, transparent)
{
// get source from the DOM as a string
vertexSource = d.e(vertexSource).textContent;
fragmentSource = d.e(fragmentSource).textContent.replace('@', d.e('x').textContent);
fragmentSource = fragmentSource.replace('~', stdDerivativesSupported? d.e('~').textContent : '');
// compile source
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(vertexShader, vertexSource);
gl.shaderSource(fragmentShader, fragmentSource);
gl.compileShader(vertexShader);
gl.compileShader(fragmentShader);
// create a shader program and link it
var shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
console.log('***');
console.log(gl.getShaderInfoLog(vertexShader));
console.log(gl.getShaderInfoLog(fragmentShader));
console.log(gl.getProgramInfoLog(shaderProgram));
// set up vertex buffer
shaderProgram.v = gl.getAttribLocation(shaderProgram, 'v');
gl.enableVertexAttribArray(shaderProgram.v);
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new F(vertices), gl.STATIC_DRAW);
// set up normal buffer
if (normals)
{
shaderProgram.n = gl.getAttribLocation(shaderProgram, 'n');
gl.enableVertexAttribArray(shaderProgram.n);
var normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new F(normals), gl.STATIC_DRAW);
}
// set up textures
shaderProgram.s = [];
if (textures)
{
for (var i = 0; i < textures.length; i++)
{
shaderProgram.s[i] = gl.getUniformLocation(shaderProgram, 's' + i);
}
}
// set up uniform locations
shaderProgram.b = gl.getUniformLocation(shaderProgram, 'w2s');
shaderProgram.c = gl.getUniformLocation(shaderProgram, 'uniformData');
shaderProgram.d = gl.getUniformLocation(shaderProgram, 'w2c');
shaderProgram.e = gl.getUniformLocation(shaderProgram, 'c2s');
shaderProgram.f = gl.getUniformLocation(shaderProgram, 'positions');
shaderProgram.g = gl.getUniformLocation(shaderProgram, 'tintColor');
var indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
shaderProgram.draw = function ()
{
if (transparent)
{
gl.enable(gl.BLEND);
gl.depthMask(false);
}
else
{
gl.disable(gl.BLEND);
gl.depthMask(true);
}
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.useProgram(shaderProgram);
gl.vertexAttribPointer(shaderProgram.v, 4, gl.FLOAT, false, 0, 0);
if (normals)
{
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.vertexAttribPointer(shaderProgram.n, 3, gl.FLOAT, false, 0, 0);
}
for (var i = 0; i < shaderProgram.s.length; i++)
{
gl.activeTexture(gl['TEXTURE' + i]);
gl.bindTexture(gl.TEXTURE_2D, textures[i] == mainFrameTexture ? previousFrameTexture : textures[i]);
gl.uniform1i(shaderProgram.s[i], i);
}
shaderProgram.b && gl.uniformMatrix4fv(shaderProgram.b, false, worldToClip);
shaderProgram.c && gl.uniform4fv(shaderProgram.c, uniformData);
shaderProgram.d && gl.uniformMatrix4fv(shaderProgram.d, false, worldToCamera);
shaderProgram.e && gl.uniformMatrix4fv(shaderProgram.e, false, cameraToClip);
shaderProgram.f && gl.uniform4fv(shaderProgram.f, markerPositions);
shaderProgram.g && gl.uniform4fv(shaderProgram.g, tintColor);
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
};
// return the shader
return shaderProgram;
};
var textureFromCanvas = function (canvas)
{
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
return texture;
};
// generate textures based on a bunch of input parameters using perlin noise
// if wrap is set, use toroidal unwrapping to make them tileable (see http://gamedev.stackexchange.com/a/23705)
var generateTexture = function (size, data, baseColor, wrap)
{
var canvas = d.createElement('canvas');
canvas.width = canvas.height = size;
var context = canvas.getContext('2d');
var imageDataObject = context.createImageData(size, size);
var imageData = imageDataObject.data;
for (var i = 0; i < size * size * 4; i += 4)
{
imageData[i] = baseColor[0];
imageData[i + 1] = baseColor[1];
imageData[i + 2] = baseColor[2];
imageData[i + 3] = baseColor[3];
}
var twoPi = Math.PI * 2;
var at = 1;
var ct = 4;
for (i = 0; i < data.length; i++)
{
var k = data[i];
var n = new NoiseGenerator(k.o, k.a, k.r, k.s);
var p = 0;
for (var y = 0; y < size; y++)
{
for (var x = 0; x < size; x++)
{
var xt = (ct + at * Math.cos(twoPi * y / size)) * Math.cos(twoPi * x / size);
var yt = (ct + at * Math.cos(twoPi * y / size)) * Math.sin(twoPi * x / size);
var zt = at * Math.sin(twoPi * y / size);
var v = Math.abs(n.noise(wrap ? xt : x / size, wrap ? yt : y / size, wrap ? zt : 0));
for (var c = 0; c < 3; c++, p++)
{
imageData[p] += v * k.c[c] * k.c[3];
}
p++;
}
}
}
context.putImageData(imageDataObject, 0, 0);
return textureFromCanvas(canvas);
};
// precomputed derivative map for bump mapping (see http://mmikkelsen3d.blogspot.co.uk/2011/07/derivative-maps.html)
var derivativeTexture = function ()
{
var canvas = d.createElement('canvas');