-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
695 lines (555 loc) · 22.4 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
<!DOCTYPE html>
<html>
<head>
<title>The Procedural Ocean</title>
<script type="text/javascript" src="libs/three.js"></script>
<script type="text/javascript" src="libs/stats.js"></script>
<script type="text/javascript" src="libs/simplex-noise.js"></script>
<script type="text/javascript" src="libs/dat.gui.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<script type="x-shader/x-vertex" id="vertexShader">
// Description : Array and textureless GLSL 2D/3D/4D simplex noise
// functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec3 mod289(vec3 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec2 mod289(vec2 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec3 permute(vec3 x) {
return mod289(((x*34.0)+1.0)*x);
}
float snoise(vec2 v) {
const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
-0.577350269189626, // -1.0 + 2.0 * C.x
0.024390243902439); // 1.0 / 41.0
// First corner
vec2 i = floor(v + dot(v, C.yy) );
vec2 x0 = v - i + dot(i, C.xx);
// Other corners
vec2 i1;
//i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
//i1.y = 1.0 - i1.x;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
// x0 = x0 - 0.0 + 0.0 * C.xx ;
// x1 = x0 - i1 + 1.0 * C.xx ;
// x2 = x0 - 1.0 + 2.0 * C.xx ;
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
// Gradients: 41 points uniformly over a line, mapped onto a diamond.
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt( a0*a0 + h*h );
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
// Compute final noise value at P
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
vec4 mod289(vec4 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x) {
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r) {
return 1.79284291400159 - 0.85373472095314 * r;
}
float snoise(vec3 v, out vec3 gradient) {
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
// First corner
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;
// Other corners
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );
// x0 = x0 - 0.0 + 0.0 * C.xxx;
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1.0/7.0
vec3 ns = n_ * D.wyz - D.xzx;
vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);
vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );
//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);
//Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
vec4 m2 = m * m;
vec4 m4 = m2 * m2;
vec4 pdotx = vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3));
// Determine noise gradient
vec4 temp = m2 * m * pdotx;
gradient = -8.0 * (temp.x * x0 + temp.y * x1 + temp.z * x2 + temp.w * x3);
gradient += m4.x * p0 + m4.y * p1 + m4.z * p2 + m4.w * p3;
gradient *= 42.0;
return 42.0 * dot(m4, pdotx);
}
const float DEG_TO_RAD = 3.141592653589793 / 180.0;
mat4 rotationMatrix(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
}
float speedFromFrequency(float freq) {
// Using the equation for big waves http://hyperphysics.phy-astr.gsu.edu/hbase/Waves/watwav2.html
return 9.8/(2.0*3.14*freq);
}
float waveHeight(vec2 position, float time, float frequency, float scale, float stretch, float zOffset, bool pointy) {
vec3 grad = vec3(0.0);
vec3 pStretched = vec3(position.x, position.y * stretch - time * speedFromFrequency(frequency), zOffset);
if (pointy) {
return scale * -abs(snoise(frequency * pStretched, grad));
}
return scale * snoise(frequency * pStretched, grad);
}
float waterHeightMap(vec2 p, float time, float rotation, float intensity) {
vec2 position = (rotationMatrix(vec3(0.0, 0.0, 1.0), rotation * DEG_TO_RAD) * vec4(vec3(p, 0.0), 1.0)).xy;
float wave = 0.0;
wave += waveHeight(position, time, 0.0025, 12.8*intensity, 2.5, 1.0, false);
wave += waveHeight(position, time, 0.005, 6.4*intensity, 1.0, 11.0, false);
wave += waveHeight(position, time, 0.01, 3.2*intensity, 1.5, 9.0, true);
wave += waveHeight(position, time, 0.02, 1.6*intensity, 1.5, 3.0, true);
wave += waveHeight(position, time, 0.04, 0.8*intensity, 1.5, 7.0, true);
wave += waveHeight(position, time, 0.08, 0.4*(intensity*0.5 + 0.5), 1.4, 50.0, true);
wave += waveHeight(position, time, 0.16, 0.2*(intensity*0.5 + 0.5), 1.4, 30.0, true);
wave += waveHeight(position, time, 0.32, 0.1*(intensity*0.3 + 0.7), 1.2, 20.0, true);
wave += waveHeight(position, time, 0.64, 0.05*(intensity*0.2 + 0.8), 1.2, 20.0, true);
wave += waveHeight(position, time, 1.28, 0.025*(intensity*0.1 + 0.9), 1.0, 10.0, true);
return wave;
}
//Vertex shader
varying float noise;
varying vec3 vNormal;
varying vec3 vPosition;
uniform float time;
uniform float rotation;
uniform float intensity;
void main() {
float displacement = waterHeightMap(position.xy, time, rotation, intensity);
// move the position along the normal and transform it
vec3 newPosition = position + normal * displacement;
//Recalculate normal
float delta = 0.004;
vec3 diffX = vec3(position.x + delta, position.y, position.z);
vec3 diffY = vec3(position.x, position.y + delta, position.z);
vec3 posX = diffX + normal * waterHeightMap(diffX.xy, time, rotation, intensity);
vec3 posY = diffY + normal * waterHeightMap(diffY.xy, time, rotation, intensity);
vec3 newNormal = normalize(cross(normalize(posX - newPosition), normalize(posY - newPosition)));
//share the normal and position with the fragment shader
vNormal = newNormal;
vPosition = newPosition;
gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );
}
</script>
<script type="x-shader/x-vertex" id="fragmentShader">
// Description : Array and textureless GLSL 2D/3D/4D simplex noise
// functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec3 mod289(vec3 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec2 mod289(vec2 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec3 permute(vec3 x) {
return mod289(((x*34.0)+1.0)*x);
}
float snoise(vec2 v) {
const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
-0.577350269189626, // -1.0 + 2.0 * C.x
0.024390243902439); // 1.0 / 41.0
// First corner
vec2 i = floor(v + dot(v, C.yy) );
vec2 x0 = v - i + dot(i, C.xx);
// Other corners
vec2 i1;
//i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
//i1.y = 1.0 - i1.x;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
// x0 = x0 - 0.0 + 0.0 * C.xx ;
// x1 = x0 - i1 + 1.0 * C.xx ;
// x2 = x0 - 1.0 + 2.0 * C.xx ;
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
// Permutations
i = mod289(i); // Avoid truncation effects in permutation
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
// Gradients: 41 points uniformly over a line, mapped onto a diamond.
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt( a0*a0 + h*h );
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
// Compute final noise value at P
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
vec4 mod289(vec4 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x) {
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r) {
return 1.79284291400159 - 0.85373472095314 * r;
}
float snoise(vec3 v, out vec3 gradient) {
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
// First corner
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;
// Other corners
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );
// x0 = x0 - 0.0 + 0.0 * C.xxx;
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1.0/7.0
vec3 ns = n_ * D.wyz - D.xzx;
vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);
vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );
//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);
//Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
vec4 m2 = m * m;
vec4 m4 = m2 * m2;
vec4 pdotx = vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3));
// Determine noise gradient
vec4 temp = m2 * m * pdotx;
gradient = -8.0 * (temp.x * x0 + temp.y * x1 + temp.z * x2 + temp.w * x3);
gradient += m4.x * p0 + m4.y * p1 + m4.z * p2 + m4.w * p3;
gradient *= 42.0;
return 42.0 * dot(m4, pdotx);
}
float waveHeight(vec2 position, float time, float frequency, float scale, float stretch, float zOffset, bool pointy) {
vec3 grad = vec3(0.0);
vec3 pStretched = vec3(position.x, position.y * stretch - time*20.0, time);
if (pointy) {
return scale * (1.0 - abs(snoise(frequency * pStretched, grad)));
} else {
return scale * snoise(frequency * pStretched, grad);
}
}
const float DEG_TO_RAD = 3.141592653589793 / 180.0;
mat4 rotationMatrix(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
}
float cutoffFilter(float min, float max, float displacement) {
if (displacement < max && displacement > min) {
float dist = max - min;
return (displacement - min + dist/2.0)/(max-min + dist/2.0);
} else {
return 0.0;
}
}
float foamMap(vec3 p, float time, float rotation) {
vec2 position = (rotationMatrix(vec3(0.0, 0.0, 1.0), rotation * DEG_TO_RAD) * vec4(p, 1.0)).xy;
float wave = 0.5;
wave += waveHeight(position, time, 0.32, 0.1, 1.0, time, true);
wave += waveHeight(position, time, 0.64, 0.05, 1.0, time, true);
wave += waveHeight(position, time, 1.28, 0.025, 1.2, time, true);
wave += waveHeight(position, time, 0.02, 0.2, 3.0, time, false);
float displacement = cutoffFilter(0.7, 0.9, wave);
return displacement;
}
float clipFoam(float foam, float height, vec3 normal) {
return foam * max(0.0, dot(normal, normalize(vec3(normal.x, normal.y, 0.0))));
}
varying float noise;
varying vec3 vNormal;
varying vec3 vPosition;
uniform float time;
uniform float rotation;
uniform float intensity;
uniform float foam;
void main() {
vec3 lightDirection = normalize(vec3(-5.0, -3.0, 4.0));
vec3 sLightDirection = normalize(vec3(-5.0, 3.0, -4.0));
float lightA = 0.01; // Ambient level for diffuse reflection
float phongD = max(0.0, dot(lightDirection, vNormal));
vec3 reflDir = normalize(2.0 * dot(sLightDirection, vNormal) * vNormal - sLightDirection);
vec3 viewDir = normalize(vPosition - cameraPosition);
float lightS = max(0.0, dot(reflDir, viewDir));
lightS = 0.5 * pow(lightS, 200.0);
vec3 totalColor = phongD * vec3(0.2, 0.4, 0.6) + lightS * vec3(1.0, 1.0, 0.7) + lightA*vec3(0.5, 0.6, 0.8) + foam * intensity * clipFoam(foamMap(vPosition, time, rotation), vPosition.z, vNormal) * vec3(1.0, 1.0, 1.0);
gl_FragColor = vec4( totalColor, 1.0 );
}
</script>
<script type="text/javascript">
var camera;
var scene;
var renderer;
var attributes = {
displacement: {
type: 'f', // a float
value: [] // an empty array
}
};
var uniforms = {
time: {
type: 'f',
value: 0
},
amplitude: {
type: 'f', // a float
value: 0
},
intensity: {
type: 'f',
value: 25
},
rotation: {
type: 'f', // a float
value: 160
},
foam: {
type: 'f',
value: 1
}
};
// once everything is loaded, we run our Three.js stuff.
function init() {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
scene = new THREE.Scene();
// scene.fog = new THREE.FogExp2( "#DDDDDD", 0.002 );
// create a camera, which defines where we're looking at.
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0xEEEEEE));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
// show axes in the screen
// var axes = new THREE.AxisHelper(20);
// scene.add(axes);
// GEOMETRIES
var planeGeometry = new THREE.PlaneGeometry(120, 120, 700, 700);
var shaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
});
var plane = new THREE.Mesh(planeGeometry, shaderMaterial);
plane.receiveShadow = true;
// rotate and position the plane
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 15;// 10;
plane.position.y = 0;
plane.position.z = -15;
// add the plane to the scene
scene.add(plane);
// var sphereGeometry = new THREE.SphereGeometry(2, 20, 20);
// var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
// var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
// // position the sphere
// sphere.position.x = 5;
// sphere.position.y = 3;
// sphere.position.z = 1;
// sphere.castShadow = true;
// // add the sphere to the scene
// scene.add(sphere);
// position and point the camera to the center of the scene
camera.position.x = -70;
camera.position.y = 40; // up
camera.position.z = 70;
camera.lookAt(scene.position);
//LIGHTS
// var ambientLight = new THREE.AmbientLight(0x404040, 1);
// scene.add(ambientLight);
// add spotlight for the shadows
// var spotLight = new THREE.SpotLight(0xffffff);
// spotLight.position.set(-50, 40, -10);
// spotLight.castShadow = true;
// scene.add(spotLight);
//
// var light = new THREE.PointLight( 0x404040, 1, 0 );
// light.position.set( -50, 40, -40 );
// scene.add( light );
//CONTROLS
var controls = new function () {
this.Intensity = 70.0;
this.Rotation = 160.0;
this.Speed = 1.5;
this.Foam = 1;
};
var gui = new dat.GUI();
gui.add(controls, 'Intensity', 0, 100);
gui.add(controls, 'Rotation', 0, 360);
gui.add(controls, 'Speed', 0, 5);
gui.add(controls, 'Foam', 0, 1);
// add the output of the renderer to the html element
document.getElementById("WebGL-output").appendChild(renderer.domElement);
// call the render function
var step = 0;
renderScene();
function renderScene() {
stats.update();
uniforms.time.value = step;
uniforms.amplitude.value = step;
uniforms.intensity.value = controls.Intensity/100.0;
uniforms.rotation.value = controls.Rotation;
uniforms.foam.value = controls.Foam;
step += 0.001*controls.Speed;
renderer.render(scene, camera);
// render using requestAnimationFrame
requestAnimationFrame(renderScene);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.getElementById("Stats-output").appendChild(stats.domElement);
return stats;
}
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.onload = init;
// listen to the resize events
window.addEventListener('resize', onResize, false);
</script>
</body>
</html>