-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2274 lines (2004 loc) · 73.5 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>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>humans - webgl</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<style>
body, html {
overflow: hidden;
width: 100vw;
height: 100vh;
margin: 0;
}
canvas {
position: absolute;
top: 0;
left: 0;
z-index: 0;
}
#overlay {
position: absolute;
top: 0;
left: 0;
z-index: 2;
}
#ui {
position: absolute;
top: 0;
left: 0;
z-index: 1;
overflow: hidden;
}
.waves-container {
position: fixed;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
}
.injectable-waves {
z-index: 1;
}
.video-container {
width: 100%;
height: 100%;
}
.main-video {
height: 100vh;
}
</style>
<script>setInterval(() => {var videoElement = document.getElementsByClassName('main-video');if (videoElement.playing) {} else {$('.main-video').trigger('play');}}, 1000)</script>
<div class="injectable-waves">
<div class="waves-container">
<div class="video-container">
</div>
<div id="overlay"></div>
<div id="ui">
<div id="camera">
<canvas id="profile" width="350" height="105"></canvas>
<div id="length"></div>
<div id="end"></div>
<div id="wind">
<span id="wind-speed"></span><span id="wind-unit"></span>
<div id="wind-label"></div>
</div>
<div id="size">
<span id="size-value"></span><span id="size-unit"></span>
</div>
<div id="size-label"></div>
<div id="choppiness"></div>
<div id="choppiness-label"></div>
</div>
</div>
<canvas id="simulator"></canvas>
<script>
var INITIAL_SIZE = 1000,
INITIAL_WIND = [24.5, 24.5],
INITIAL_CHOPPINESS = 2.4;
var CLEAR_COLOR = [0.0, 0.0, 0.0, 0.0],
GEOMETRY_ORIGIN = [-1000.0, -1000.0],
SUN_DIRECTION = [1.1, 2.5, -0.5],
OCEAN_COLOR = [0.0, 0.0, 0.255],
SKY_COLOR = [9.2, 5.6, 10.8],
EXPOSURE = 0.7,
GEOMETRY_RESOLUTION = 256,
GEOMETRY_SIZE = 8000,
RESOLUTION = 512;
var SIZE_OF_FLOAT = 4;
var OCEAN_COORDINATES_UNIT = 1;
var INITIAL_SPECTRUM_UNIT = 0,
SPECTRUM_UNIT = 1,
DISPLACEMENT_MAP_UNIT = 2,
NORMAL_MAP_UNIT = 3,
PING_PHASE_UNIT = 4,
PONG_PHASE_UNIT = 5,
PING_TRANSFORM_UNIT = 6,
PONG_TRANSFORM_UNIT = 7;
var UI_COLOR = "rgb(52, 137, 189)";
var PROFILE_AMPLITUDE = 50,
PROFILE_OMEGA = 0.05,
PROFILE_PHI = -2.5,
PROFILE_STEP = 5,
PROFILE_OFFSET = 52,
PROFILE_COLOR = "rgb(52, 137, 189)",
PROFILE_LINE_WIDTH = 3,
CHOPPINESS_SCALE = 0.15;
var ARROW_ORIGIN = [-1250, 0, 500],
ARROW_SHAFT_WIDTH = 80,
ARROW_HEAD_WIDTH = 160,
ARROW_HEAD_HEIGHT = 60,
ARROW_OFFSET = 40,
WIND_SCALE = 8.0,
MIN_WIND_SPEED = 5.0,
MAX_WIND_SPEED = 25.0;
var HANDLE_COLOR = "#ffffff",
SLIDER_LEFT_COLOR = UI_COLOR,
SLIDER_RIGHT_COLOR = "#999999";
var FOV = Math.PI / 4,
NEAR = 1,
FAR = 10000,
MIN_ASPECT = 1 / 3;
var WIND_SPEED_DECIMAL_PLACES = 1,
SIZE_DECIMAL_PLACES = 0,
CHOPPINESS_DECIMAL_PLACES = 1;
var SENSITIVITY = 1.0;
var WIND_SPEED_X = -1350;
var MIN_WIND_SPEED_Z = 600,
WIND_SPEED_OFFSET = 30;
var OVERLAY_DIV_ID = "overlay",
PROFILE_CANVAS_ID = "profile",
SIMULATOR_CANVAS_ID = "simulator",
UI_DIV_ID = "ui",
CAMERA_DIV_ID = "camera",
WIND_SPEED_DIV_ID = "wind",
WIND_SPEED_SPAN_ID = "wind-speed",
CHOPPINESS_DIV_ID = "choppiness";
var SIZE_SLIDER_X = -200,
SIZE_SLIDER_Z = 1100,
SIZE_SLIDER_LENGTH = 400,
MIN_SIZE = 100,
MAX_SIZE = 1000,
SIZE_SLIDER_BREADTH = 3,
SIZE_HANDLE_SIZE = 24;
var CHOPPINESS_SLIDER_X = -1420,
CHOPPINESS_SLIDER_Z = 75,
CHOPPINESS_SLIDER_LENGTH = 300,
MIN_CHOPPINESS = 0,
MAX_CHOPPINESS = 2.5,
CHOPPINESS_SLIDER_BREADTH = 6,
CHOPPINESS_HANDLE_SIZE = 30;
var ARROW_TIP_RADIUS = 100,
SIZE_HANDLE_RADIUS = 30,
CHOPPINESS_HANDLE_RADIUS = 100;
var NONE = 0,
ORBITING = 1,
ROTATING = 2,
SLIDING_SIZE = 3,
SLIDING_CHOPPINESS = 4;
var CAMERA_DISTANCE = 1900,
ORBIT_POINT = [3000.0, 600.0, 3000.0],
INITIAL_AZIMUTH = 0,
INITIAL_ELEVATION = 0.9,
MIN_AZIMUTH = 0,
MAX_AZIMUTH = 0,
MIN_ELEVATION = 0,
MAX_ELEVATION = 0;
var addToVector = function (out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
return out;
};
var subtractFromVector = function (out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
return out;
};
var multiplyVectorByScalar = function (out, v, k) {
out[0] = v[0] * k;
out[1] = v[1] * k;
out[2] = v[2] * k;
return out;
};
var makeIdentityMatrix = function (matrix) {
matrix[0] = 1.0;
matrix[1] = 0.0;
matrix[2] = 0.0;
matrix[3] = 0.0;
matrix[4] = 0.0;
matrix[5] = 1.0;
matrix[6] = 0.0;
matrix[7] = 0.0;
matrix[8] = 0.0;
matrix[9] = 0.0;
matrix[10] = 1.0;
matrix[11] = 0.0;
matrix[12] = 0.0;
matrix[13] = 0.0;
matrix[14] = 0.0;
matrix[15] = 1.0;
return matrix;
};
var makeXRotationMatrix = function (matrix, angle) {
matrix[0] = 1.0;
matrix[1] = 0.0;
matrix[2] = 0.0;
matrix[3] = 0.0;
matrix[4] = 0.0;
matrix[5] = Math.cos(angle);
matrix[6] = Math.sin(angle);
matrix[7] = 0.0;
matrix[8] = 0.0;
matrix[9] = -Math.sin(angle);
matrix[10] = Math.cos(angle);
matrix[11] = 0.0;
matrix[12] = 0.0;
matrix[13] = 0.0;
matrix[14] = 0.0;
matrix[15] = 1.0;
return matrix;
};
var makeYRotationMatrix = function (matrix, angle) {
matrix[0] = Math.cos(angle);
matrix[1] = 0.0;
matrix[2] = -Math.sin(angle);
matrix[3] = 0.0;
matrix[4] = 0.0;
matrix[5] = 1.0;
matrix[6] = 0.0;
matrix[7] = 0.0;
matrix[8] = Math.sin(angle);
matrix[9] = 0.0;
matrix[10] = Math.cos(angle);
matrix[11] = 0.0;
matrix[12] = 0.0;
matrix[13] = 0.0;
matrix[14] = 0.0;
matrix[15] = 1.0;
return matrix;
};
var distanceBetweenVectors = function (a, b) {
var xDist = b[0] - a[0],
yDist = b[1] - a[1],
zDist = b[2] - a[2];
return Math.sqrt(xDist * xDist + yDist * yDist + zDist * zDist);
};
var lengthOfVector = function (v) {
var x = v[0],
y = v[1],
z = v[2];
return Math.sqrt(x * x + y * y + z * z);
};
var setVector4 = function (out, x, y, z, w) {
out[0] = x;
out[1] = y;
out[2] = z;
out[3] = w;
return out;
};
var projectVector4 = function (out, v) {
var reciprocalW = 1 / v[3];
out[0] = v[0] * reciprocalW;
out[1] = v[1] * reciprocalW;
out[2] = v[2] * reciprocalW;
return out;
};
var transformVectorByMatrix = function (out, v, m) {
var x = v[0],
y = v[1],
z = v[2],
w = v[3];
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
return out;
};
var invertMatrix = function (out, m) {
var m0 = m[0],
m4 = m[4],
m8 = m[8],
m12 = m[12],
m1 = m[1],
m5 = m[5],
m9 = m[9],
m13 = m[13],
m2 = m[2],
m6 = m[6],
m10 = m[10],
m14 = m[14],
m3 = m[3],
m7 = m[7],
m11 = m[11],
m15 = m[15],
temp0 = m10 * m15,
temp1 = m14 * m11,
temp2 = m6 * m15,
temp3 = m14 * m7,
temp4 = m6 * m11,
temp5 = m10 * m7,
temp6 = m2 * m15,
temp7 = m14 * m3,
temp8 = m2 * m11,
temp9 = m10 * m3,
temp10 = m2 * m7,
temp11 = m6 * m3,
temp12 = m8 * m13,
temp13 = m12 * m9,
temp14 = m4 * m13,
temp15 = m12 * m5,
temp16 = m4 * m9,
temp17 = m8 * m5,
temp18 = m0 * m13,
temp19 = m12 * m1,
temp20 = m0 * m9,
temp21 = m8 * m1,
temp22 = m0 * m5,
temp23 = m4 * m1,
t0 =
temp0 * m5 +
temp3 * m9 +
temp4 * m13 -
(temp1 * m5 + temp2 * m9 + temp5 * m13),
t1 =
temp1 * m1 +
temp6 * m9 +
temp9 * m13 -
(temp0 * m1 + temp7 * m9 + temp8 * m13),
t2 =
temp2 * m1 +
temp7 * m5 +
temp10 * m13 -
(temp3 * m1 + temp6 * m5 + temp11 * m13),
t3 =
temp5 * m1 +
temp8 * m5 +
temp11 * m9 -
(temp4 * m1 + temp9 * m5 + temp10 * m9),
d = 1.0 / (m0 * t0 + m4 * t1 + m8 * t2 + m12 * t3);
out[0] = d * t0;
out[1] = d * t1;
out[2] = d * t2;
out[3] = d * t3;
out[4] =
d *
(temp1 * m4 +
temp2 * m8 +
temp5 * m12 -
(temp0 * m4 + temp3 * m8 + temp4 * m12));
out[5] =
d *
(temp0 * m0 +
temp7 * m8 +
temp8 * m12 -
(temp1 * m0 + temp6 * m8 + temp9 * m12));
out[6] =
d *
(temp3 * m0 +
temp6 * m4 +
temp11 * m12 -
(temp2 * m0 + temp7 * m4 + temp10 * m12));
out[7] =
d *
(temp4 * m0 +
temp9 * m4 +
temp10 * m8 -
(temp5 * m0 + temp8 * m4 + temp11 * m8));
out[8] =
d *
(temp12 * m7 +
temp15 * m11 +
temp16 * m15 -
(temp13 * m7 + temp14 * m11 + temp17 * m15));
out[9] =
d *
(temp13 * m3 +
temp18 * m11 +
temp21 * m15 -
(temp12 * m3 + temp19 * m11 + temp20 * m15));
out[10] =
d *
(temp14 * m3 +
temp19 * m7 +
temp22 * m15 -
(temp15 * m3 + temp18 * m7 + temp23 * m15));
out[11] =
d *
(temp17 * m3 +
temp20 * m7 +
temp23 * m11 -
(temp16 * m3 + temp21 * m7 + temp22 * m11));
out[12] =
d *
(temp14 * m10 +
temp17 * m14 +
temp13 * m6 -
(temp16 * m14 + temp12 * m6 + temp15 * m10));
out[13] =
d *
(temp20 * m14 +
temp12 * m2 +
temp19 * m10 -
(temp18 * m10 + temp21 * m14 + temp13 * m2));
out[14] =
d *
(temp18 * m6 +
temp23 * m14 +
temp15 * m2 -
(temp22 * m14 + temp14 * m2 + temp19 * m6));
out[15] =
d *
(temp22 * m10 +
temp16 * m2 +
temp21 * m6 -
(temp20 * m6 + temp23 * m10 + temp17 * m2));
return out;
};
var premultiplyMatrix = function (out, matrixA, matrixB) {
var b0 = matrixB[0],
b4 = matrixB[4],
b8 = matrixB[8],
b12 = matrixB[12],
b1 = matrixB[1],
b5 = matrixB[5],
b9 = matrixB[9],
b13 = matrixB[13],
b2 = matrixB[2],
b6 = matrixB[6],
b10 = matrixB[10],
b14 = matrixB[14],
b3 = matrixB[3],
b7 = matrixB[7],
b11 = matrixB[11],
b15 = matrixB[15],
aX = matrixA[0],
aY = matrixA[1],
aZ = matrixA[2],
aW = matrixA[3];
out[0] = b0 * aX + b4 * aY + b8 * aZ + b12 * aW;
out[1] = b1 * aX + b5 * aY + b9 * aZ + b13 * aW;
out[2] = b2 * aX + b6 * aY + b10 * aZ + b14 * aW;
out[3] = b3 * aX + b7 * aY + b11 * aZ + b15 * aW;
(aX = matrixA[4]),
(aY = matrixA[5]),
(aZ = matrixA[6]),
(aW = matrixA[7]);
out[4] = b0 * aX + b4 * aY + b8 * aZ + b12 * aW;
out[5] = b1 * aX + b5 * aY + b9 * aZ + b13 * aW;
out[6] = b2 * aX + b6 * aY + b10 * aZ + b14 * aW;
out[7] = b3 * aX + b7 * aY + b11 * aZ + b15 * aW;
(aX = matrixA[8]),
(aY = matrixA[9]),
(aZ = matrixA[10]),
(aW = matrixA[11]);
out[8] = b0 * aX + b4 * aY + b8 * aZ + b12 * aW;
out[9] = b1 * aX + b5 * aY + b9 * aZ + b13 * aW;
out[10] = b2 * aX + b6 * aY + b10 * aZ + b14 * aW;
out[11] = b3 * aX + b7 * aY + b11 * aZ + b15 * aW;
(aX = matrixA[12]),
(aY = matrixA[13]),
(aZ = matrixA[14]),
(aW = matrixA[15]);
out[12] = b0 * aX + b4 * aY + b8 * aZ + b12 * aW;
out[13] = b1 * aX + b5 * aY + b9 * aZ + b13 * aW;
out[14] = b2 * aX + b6 * aY + b10 * aZ + b14 * aW;
out[15] = b3 * aX + b7 * aY + b11 * aZ + b15 * aW;
return out;
};
var makePerspectiveMatrix = function (
matrix,
fov,
aspect,
near,
far
) {
var f = Math.tan(0.5 * (Math.PI - fov)),
range = near - far;
matrix[0] = f / aspect;
matrix[1] = 0;
matrix[2] = 0;
matrix[3] = 0;
matrix[4] = 0;
matrix[5] = f;
matrix[6] = 0;
matrix[7] = 0;
matrix[8] = 0;
matrix[9] = 0;
matrix[10] = far / range;
matrix[11] = -1;
matrix[12] = 0;
matrix[13] = 0;
matrix[14] = (near * far) / range;
matrix[15] = 0.0;
return matrix;
};
var clamp = function (x, min, max) {
return Math.min(Math.max(x, min), max);
};
var log2 = function (number) {
return Math.log(number) / Math.log(2);
};
var buildProgramWrapper = function (
gl,
vertexShader,
fragmentShader,
attributeLocations
) {
var programWrapper = {};
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
for (var attributeName in attributeLocations) {
gl.bindAttribLocation(
program,
attributeLocations[attributeName],
attributeName
);
}
gl.linkProgram(program);
var uniformLocations = {};
var numberOfUniforms = gl.getProgramParameter(
program,
gl.ACTIVE_UNIFORMS
);
for (var i = 0; i < numberOfUniforms; i += 1) {
var activeUniform = gl.getActiveUniform(program, i),
uniformLocation = gl.getUniformLocation(
program,
activeUniform.name
);
uniformLocations[activeUniform.name] = uniformLocation;
}
programWrapper.program = program;
programWrapper.uniformLocations = uniformLocations;
return programWrapper;
};
var buildShader = function (gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
return shader;
};
var buildTexture = function (
gl,
unit,
format,
type,
width,
height,
data,
wrapS,
wrapT,
minFilter,
magFilter
) {
var texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0 + unit);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
gl.TEXTURE_2D,
0,
format,
width,
height,
0,
format,
type,
data
);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
return texture;
};
var buildFramebuffer = function (gl, attachment) {
var framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
attachment,
0
);
return framebuffer;
};
var epsilon = function (x) {
return Math.abs(x) < 0.000001 ? 0 : x;
};
var toCSSMatrix = function (m) {
//flip y to make css and webgl coordinates consistent
return (
"matrix3d(" +
epsilon(m[0]) +
"," +
-epsilon(m[1]) +
"," +
epsilon(m[2]) +
"," +
epsilon(m[3]) +
"," +
epsilon(m[4]) +
"," +
-epsilon(m[5]) +
"," +
epsilon(m[6]) +
"," +
epsilon(m[7]) +
"," +
epsilon(m[8]) +
"," +
-epsilon(m[9]) +
"," +
epsilon(m[10]) +
"," +
epsilon(m[11]) +
"," +
epsilon(m[12]) +
"," +
-epsilon(m[13]) +
"," +
epsilon(m[14]) +
"," +
epsilon(m[15]) +
")"
);
};
var setPerspective = function (element, value) {
element.style.WebkitPerspective = value;
element.style.perspective = value;
};
var setTransformOrigin = function (element, value) {
element.style.WebkitTransformOrigin = value;
element.style.transformOrigin = value;
};
var setTransform = function (element, value) {
element.style.WebkitTransform = value;
element.style.transform = value;
};
var setText = function (element, value, decimalPlaces) {
element.textContent = value.toFixed(decimalPlaces);
};
var getMousePosition = function (event, element) {
var boundingRect = element.getBoundingClientRect();
return {
x: event.clientX - boundingRect.left,
y: event.clientY - boundingRect.top,
};
};
var hasWebGLSupportWithExtensions = function (extensions) {
var canvas = document.createElement("canvas");
var gl = null;
try {
gl =
canvas.getContext("webgl") ||
canvas.getContext("experimental-webgl");
} catch (e) {
return false;
}
if (gl === null) {
return false;
}
for (var i = 0; i < extensions.length; ++i) {
if (gl.getExtension(extensions[i]) === null) {
return false;
}
}
return true;
};
var requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame;
</script>
<script>
var Camera = function () {
var azimuth = INITIAL_AZIMUTH,
elevation = INITIAL_ELEVATION,
viewMatrix = makeIdentityMatrix(new Float32Array(16)),
position = new Float32Array(3),
changed = true;
this.changeAzimuth = function (deltaAzimuth) {
azimuth += deltaAzimuth;
azimuth = clamp(azimuth, MIN_AZIMUTH, MAX_AZIMUTH);
changed = true;
};
this.changeElevation = function (deltaElevation) {
elevation += deltaElevation;
elevation = clamp(elevation, MIN_ELEVATION, MAX_ELEVATION);
changed = true;
};
this.getPosition = function () {
return position;
};
var orbitTranslationMatrix = makeIdentityMatrix(
new Float32Array(16)
),
xRotationMatrix = new Float32Array(16),
yRotationMatrix = new Float32Array(16),
distanceTranslationMatrix = makeIdentityMatrix(
new Float32Array(16)
);
this.getViewMatrix = function () {
if (changed) {
makeIdentityMatrix(viewMatrix);
makeXRotationMatrix(xRotationMatrix, elevation);
makeYRotationMatrix(yRotationMatrix, azimuth);
distanceTranslationMatrix[14] = -CAMERA_DISTANCE;
orbitTranslationMatrix[12] = -ORBIT_POINT[0];
orbitTranslationMatrix[13] = -ORBIT_POINT[1];
orbitTranslationMatrix[14] = -ORBIT_POINT[2];
premultiplyMatrix(
viewMatrix,
viewMatrix,
orbitTranslationMatrix
);
premultiplyMatrix(viewMatrix, viewMatrix, yRotationMatrix);
premultiplyMatrix(viewMatrix, viewMatrix, xRotationMatrix);
premultiplyMatrix(
viewMatrix,
viewMatrix,
distanceTranslationMatrix
);
position[0] =
CAMERA_DISTANCE *
Math.sin(Math.PI / 2 - elevation) *
Math.sin(-azimuth) +
ORBIT_POINT[0];
position[1] =
CAMERA_DISTANCE * Math.cos(Math.PI / 2 - elevation) +
ORBIT_POINT[1];
position[2] =
CAMERA_DISTANCE *
Math.sin(Math.PI / 2 - elevation) *
Math.cos(-azimuth) +
ORBIT_POINT[2];
changed = false;
}
return viewMatrix;
};
};
var FULLSCREEN_VERTEX_SOURCE = [
"attribute vec2 a_position;",
"varying vec2 v_coordinates;",
"void main (void) {",
"v_coordinates = a_position * 0.5 + 0.5;",
"gl_Position = vec4(a_position, 0.0, 1.0);",
"}",
].join("\n");
//GPU FFT using the Stockham formulation
var SUBTRANSFORM_FRAGMENT_SOURCE = [
"precision highp float;",
"const float PI = 3.14159265359;",
"uniform sampler2D u_input;",
"uniform float u_transformSize;",
"uniform float u_subtransformSize;",
"varying vec2 v_coordinates;",
"vec2 multiplyComplex (vec2 a, vec2 b) {",
"return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);",
"}",
"void main (void) {",
"#ifdef HORIZONTAL",
"float index = v_coordinates.x * u_transformSize - 0.5;",
"#else",
"float index = v_coordinates.y * u_transformSize - 0.5;",
"#endif",
"float evenIndex = floor(index / u_subtransformSize) * (u_subtransformSize * 0.5) + mod(index, u_subtransformSize * 0.5);",
//transform two complex sequences simultaneously
"#ifdef HORIZONTAL",
"vec4 even = texture2D(u_input, vec2(evenIndex + 0.5, gl_FragCoord.y) / u_transformSize).rgba;",
"vec4 odd = texture2D(u_input, vec2(evenIndex + u_transformSize * 0.5 + 0.5, gl_FragCoord.y) / u_transformSize).rgba;",
"#else",
"vec4 even = texture2D(u_input, vec2(gl_FragCoord.x, evenIndex + 0.5) / u_transformSize).rgba;",
"vec4 odd = texture2D(u_input, vec2(gl_FragCoord.x, evenIndex + u_transformSize * 0.5 + 0.5) / u_transformSize).rgba;",
"#endif",
"float twiddleArgument = -2.0 * PI * (index / u_subtransformSize);",
"vec2 twiddle = vec2(cos(twiddleArgument), sin(twiddleArgument));",
"vec2 outputA = even.xy + multiplyComplex(twiddle, odd.xy);",
"vec2 outputB = even.zw + multiplyComplex(twiddle, odd.zw);",
"gl_FragColor = vec4(outputA, outputB);",
"}",
].join("\n");
var INITIAL_SPECTRUM_FRAGMENT_SOURCE = [
"precision highp float;",
"const float PI = 3.14159265359;",
"const float G = 9.81;",
"const float KM = 370.0;",
"const float CM = 0.23;",
"uniform vec2 u_wind;",
"uniform float u_resolution;",
"uniform float u_size;",
"float square (float x) {",
"return x * x;",
"}",
"float omega (float k) {",
"return sqrt(G * k * (1.0 + square(k / KM)));",
"}",
"float tanh (float x) {",
"return (1.0 - exp(-2.0 * x)) / (1.0 + exp(-2.0 * x));",
"}",
"void main (void) {",
"vec2 coordinates = gl_FragCoord.xy - 0.5;",
"float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;",
"float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;",
"vec2 waveVector = (2.0 * PI * vec2(n, m)) / u_size;",
"float k = length(waveVector);",
"float U10 = length(u_wind);",
"float Omega = 0.84;",
"float kp = G * square(Omega / U10);",
"float c = omega(k) / k;",
"float cp = omega(kp) / kp;",
"float Lpm = exp(-1.25 * square(kp / k));",
"float gamma = 1.7;",
"float sigma = 0.08 * (1.0 + 4.0 * pow(Omega, -3.0));",
"float Gamma = exp(-square(sqrt(k / kp) - 1.0) / 2.0 * square(sigma));",
"float Jp = pow(gamma, Gamma);",
"float Fp = Lpm * Jp * exp(-Omega / sqrt(10.0) * (sqrt(k / kp) - 1.0));",
"float alphap = 0.006 * sqrt(Omega);",
"float Bl = 0.5 * alphap * cp / c * Fp;",
"float z0 = 0.000037 * square(U10) / G * pow(U10 / cp, 0.9);",
"float uStar = 0.41 * U10 / log(10.0 / z0);",
"float alpham = 0.01 * ((uStar < CM) ? (1.0 + log(uStar / CM)) : (1.0 + 3.0 * log(uStar / CM)));",
"float Fm = exp(-0.25 * square(k / KM - 1.0));",
"float Bh = 0.5 * alpham * CM / c * Fm * Lpm;",
"float a0 = log(2.0) / 4.0;",
"float am = 0.13 * uStar / CM;",
"float Delta = tanh(a0 + 4.0 * pow(c / cp, 2.5) + am * pow(CM / c, 2.5));",
"float cosPhi = dot(normalize(u_wind), normalize(waveVector));",
"float S = (1.0 / (2.0 * PI)) * pow(k, -4.0) * (Bl + Bh) * (1.0 + Delta * (2.0 * cosPhi * cosPhi - 1.0));",
"float dk = 2.0 * PI / u_size;",
"float h = sqrt(S / 2.0) * dk;",
"if (waveVector.x == 0.0 && waveVector.y == 0.0) {",
"h = 0.0;", //no DC term
"}",
"gl_FragColor = vec4(h, 0.0, 0.0, 0.0);",
"}",
].join("\n");
//phases stored in separate texture to ensure wave continuity on resizing
var PHASE_FRAGMENT_SOURCE = [
"precision highp float;",
"const float PI = 3.14159265359;",
"const float G = 9.81;",
"const float KM = 370.0;",
"varying vec2 v_coordinates;",
"uniform sampler2D u_phases;",
"uniform float u_deltaTime;",
"uniform float u_resolution;",
"uniform float u_size;",
"float omega (float k) {",
"return sqrt(G * k * (1.0 + k * k / KM * KM));",
"}",
"void main (void) {",
"float deltaTime = 1.0 / 60.0;",
"vec2 coordinates = gl_FragCoord.xy - 0.5;",
"float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;",
"float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;",
"vec2 waveVector = (2.0 * PI * vec2(n, m)) / u_size;",
"float phase = texture2D(u_phases, v_coordinates).r;",
"float deltaPhase = omega(length(waveVector)) * u_deltaTime;",
"phase = mod(phase + deltaPhase, 2.0 * PI);",
"gl_FragColor = vec4(phase, 0.0, 0.0, 0.0);",
"}",
].join("\n");
var SPECTRUM_FRAGMENT_SOURCE = [
"precision highp float;",
"const float PI = 3.14159265359;",
"const float G = 9.81;",
"const float KM = 370.0;",
"varying vec2 v_coordinates;",
"uniform float u_size;",
"uniform float u_resolution;",