-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
750 lines (565 loc) · 22.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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
<html>
<head>
<!-- Scripts -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/libs/underscore.js"></script>
<script src="js/libs/three.js"></script>
<!-- Stylesheets -->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<canvas id="heightmap">
You're browser doesn't support WebGL....lets try a more modern browser?
</canvas>
<canvas id="viewport" tabindex='1'>
You're browser doesn't support WebGL....lets try a more modern browser?
</canvas>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec4 aVertexSlope;
uniform mat4 uMVMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
varying vec3 pos;
varying float depth;
varying float distance;
varying vec3 normal;
varying float slope;
varying float zband;
void main(void) {
vec4 position = uPMatrix * uVMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);// * 0.1;
gl_Position = position;
pos = aVertexPosition.xyz;
depth = aVertexPosition.y/50.0;
distance = sqrt(position.x*position.x + position.z*position.z + position.y*position.y);
normal = aVertexSlope.xyz;
slope = aVertexSlope.w;
zband = clamp(slope / 800.0, 0.0, 1.0);
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
#extension GL_OES_half_float_linear : enable
precision mediump float;
uniform vec3 uColor;
uniform sampler2D snow;
uniform sampler2D ice;
uniform sampler2D snowmud;
uniform sampler2D volcano;
uniform sampler2D fault;
uniform sampler2D canyon;
uniform sampler2D deepcave;
uniform sampler2D bison;
uniform sampler2D rocky;
uniform sampler2D grass;
uniform sampler2D gravel;
uniform vec3 viewDirection;
varying vec3 pos;
varying float depth;
varying float distance;
varying vec3 normal;
varying float slope;
varying float zband;
// Per-fragment fog
vec3 applyFog( in vec3 rgb, // original color of the pixel
in float distance ) // camera to point distance
{
float b = 0.00002;
float fogAmount = 1.0 - exp( -distance*b );
vec3 fogColor = vec3(0.5,0.6,0.7);
return mix( rgb, fogColor, fogAmount );
}
// Triplanar texture mapping
vec4 triPlanar( sampler2D tex, vec3 coords, vec3 normal ) {
vec4 color = texture2D( tex, mod(coords.xy, 1.0) ) * normal.z + texture2D( tex, mod(coords.xz, 1.0) ) * normal.y + texture2D( tex, mod(coords.yz, 1.0) ) * normal.x;
return color;
}
//////////////////////////////////////////////////////////////////
//
// Using GLSL simplex noise from Ashima Arts
//
// 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;
}
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)
{
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);
m = m * m;
return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
}
//
//
//////////////////////////////////////////////////////////////////
void main(void) {
// TODO: allow switching between triPlanar mapping and XZ mapping
vec3 blending = abs( normalize(normal) );
blending = normalize(max(blending, 0.00001)); // Force weights to sum to 1.0
float b = (blending.x + blending.y + blending.z);
blending /= vec3(b, b, b);
// vec4 tex1 = triPlanar( deepcave, pos*0.01, blending );
// vec4 tex2 = triPlanar( fault, pos*0.001, blending );
// vec4 tex3 = triPlanar( canyon, pos*0.01, blending );
vec4 colBase1;
vec4 colBase2;
vec4 colSlope1;
vec4 colSlope2;
vec4 colDetail;
float texBaseScale = 0.005;
float texBaseScale2 = -0.0005;
float texSlopeScale = 0.001;
float texSlopeScale2 = -0.0001;
float texDetailScale = texBaseScale*8.0;
// Texture Synthesis Bands
//
// Texture synthesis is applied in 2 fields: height and slope
// The height band determines which texture band we're working
// in, while the slope determines which texture we use within
// that band. To avoid cutoff lines, I apply a fading effect
// between texture/slope bands using linear interpolation
//
//
// Bands
// 4 [6000+] snow, ice
// 3 [6000,6000] snow
// 3 [3000,5000] snow-mud, volcano
// 2 [700,3000] canyon, deep-cave
// 1 [500,700] bison, rocky
// 0 [500-] dirt
float bx;
float by;
float bz = zband;
float bMult;
float band0 = 500.0;
float band1 = 700.0;
float band2 = 3000.0;
float band3 = 5000.0;
float band4 = 6000.0;
float band5 = 10000.0;
vec2 steepSlopePos = vec2(pos.y, max(pos.x , pos.z)); // Allow sideways UV mapping on steep slopes; this is an artifact which can be exploited to make a stretched out look on the mountain slopes
// vec2 steepSlopePos = pos.xz;
vec3 norm = normalize(normal);
float overrideColor = 0.0;
if (pos.y > band4) {
bx = 5.0;
by = clamp((pos.y - band4) / (band5 - band4), 0.0, 1.0);
} else if (pos.y > band3) {
bx = 4.0;
by = clamp((pos.y - band3) / (band4 - band3), 0.0, 1.0);
} else if (pos.y > band2) {
bx = 3.0;
by = clamp((pos.y - band2) / (band3 - band2), 0.0, 1.0);
} else if (pos.y > band1) {
bx = 2.0;
by = clamp((pos.y - band1) / (band2 - band1), 0.0, 1.0);
} else if (pos.y > band0) {
bx = 1.0;
by = clamp((pos.y - band0) / (band1 - band0), 0.0, 1.0);
} else {
bx = 0.0;
by = clamp((pos.y) / (band0), 0.0, 1.0);
}
overrideColor = by / 1.0;
float scaleRocks = 2.0;
float scaleRocks2 = 4.0;
float scaleCave = 0.5;
float scaleVolcano = 3.0;
float lightIntensity = 1.0;
float attenuation = 0.0;
float shininess = 0.0;
// TODO: tinting the cave texture on the mountain to closely match the rocks..
vec4 tintRock = vec4(1.0,1.0,1.0,1.0)*0.2;
vec4 tintCave = vec4(1.0,1.0,1.0,1.0)*1.0;
//vec4 tintCave = vec4( 56.0/250.0, 50.0/250.0, 40.0/250.0, 1.0 ) * 1.2;//2.0
if (bx < 1.0) {
colBase1 = texture2D( fault, pos.xz * texBaseScale );
colBase1 *= texture2D( fault, pos.xz * texBaseScale2 );
colBase2 = texture2D( bison, pos.xz * texBaseScale );
colBase2 *= texture2D( bison, pos.xz * texBaseScale2 );
colSlope1 = texture2D( rocky, pos.xz * texSlopeScale * scaleRocks ) * tintRock;
colSlope2 = texture2D( rocky, pos.xz * texSlopeScale * scaleRocks ) * tintRock;
colDetail = texture2D( gravel, pos.xz * texDetailScale );
bMult =3.8;
lightIntensity = 0.25;
} else if (bx < 2.0) {
colBase1 = texture2D( bison, pos.xz * texBaseScale );
colBase1 *= texture2D( bison, pos.xz * texBaseScale2 );
colBase2 = texture2D( rocky, pos.xz * texBaseScale );
colBase2 *= texture2D( rocky, pos.xz * texBaseScale2 * scaleRocks2 );
colSlope1 = texture2D( rocky, pos.xz * texSlopeScale * scaleRocks ) * tintRock;
colSlope2 = texture2D( deepcave, pos.xz * texSlopeScale * scaleCave ) * tintCave;
colDetail = texture2D( gravel, pos.xz * texDetailScale );
bMult = (3.8 - 1.8) * (1.0 - by) + 1.8;
lightIntensity = 0.25;
} else if (bx < 3.0) {
colBase1 = texture2D( rocky, pos.xz * texBaseScale );
colBase1 *= texture2D( rocky, pos.xz * texBaseScale2 * scaleRocks2 );
colBase2 = texture2D( rocky, pos.xz * texBaseScale * scaleRocks );
colBase2 *= texture2D( rocky, pos.xz * texBaseScale2 * scaleRocks2 );
//////////////////////////////////////////////////
//
// STEEP SLOPE :: TRI-PLANAR MAPPING IS AN OPTION
// texSlopeScale = 0.001;
// bz = 1.0;
//
// No tri-planar
colSlope1 = texture2D( deepcave, pos.xz * texSlopeScale * scaleCave ) * tintCave;
colSlope2 = texture2D( deepcave, steepSlopePos * texSlopeScale * scaleCave ) * tintCave;
// tri-planar mapping
// colSlope1 = triPlanar( deepcave, pos.xyz * texSlopeScale * scaleCave, blending ) * tintCave;
// colSlope2 = triPlanar( deepcave, pos.xyz * texSlopeScale * scaleCave, blending ) * tintCave;
//
//
//////////////////////////////////////////////////
colDetail = texture2D( gravel, pos.xz * texDetailScale );
bMult =1.8;
lightIntensity = (0.25 - 1.0) * (1.0 - by) + 1.0;
} else if (bx < 4.0) {
colBase1 = texture2D( rocky, pos.xz * texBaseScale * scaleRocks );
colBase1 *= texture2D( rocky, pos.xz * texBaseScale2 * scaleRocks2 );
colBase2 = texture2D( snowmud, pos.xz * texBaseScale * 0.2 );
colBase2 *= texture2D( snowmud, pos.xz * texBaseScale2 );
//////////////////////////////////////////////////
//
// STEEP SLOPE :: TRI-PLANAR MAPPING IS AN OPTION
// texSlopeScale = 0.001;
// bz = 1.0;
//
// No tri-planar
colSlope1 = texture2D( deepcave, steepSlopePos * texSlopeScale * scaleCave ) * tintCave;
colSlope2 = texture2D( volcano, pos.xz * texSlopeScale * scaleVolcano );
// tri-planar mapping
// colSlope1 = triPlanar( deepcave, pos.xyz * texSlopeScale * scaleCave, blending ) * tintCave;
// colSlope2 = triPlanar( volcano, pos.xyz * texSlopeScale * scaleVolcano, blending );
//
//
//////////////////////////////////////////////////
colDetail = texture2D( gravel, pos.xz * texDetailScale );
bMult =1.8;
lightIntensity = 1.0;
attenuation = 0.2;
shininess = 5.0;
} else if (bx < 5.0) {
colBase1 = texture2D( snowmud, pos.xz * texBaseScale * 0.2 );
colBase1 *= texture2D( snowmud, pos.xz * texBaseScale2 );
colBase2 = texture2D( snow, pos.xz * texBaseScale );
colBase2 *= texture2D( snow, pos.xz * texBaseScale2 );
colSlope1 = texture2D( volcano, pos.xz * texSlopeScale * scaleVolcano );
colSlope2 = texture2D( ice, pos.xz * texSlopeScale );
colDetail = texture2D( gravel, pos.xz * texDetailScale );
bMult = (1.8 - 1.0) * (1.0 - by) + 1.0;
lightIntensity = (0.6 - 1.0) * (by - 1.0) + 0.6;
attenuation = 0.2;
shininess = 5.0;
} else if (bx == 5.0) {
colBase1 = texture2D( snow, pos.xz * texBaseScale );
colBase1 *= texture2D( snow, pos.xz * texBaseScale2 );
colBase2 = texture2D( snow, pos.xz * texBaseScale );
colBase2 *= texture2D( snow, pos.xz * texBaseScale2 );
colSlope1 = texture2D( ice, pos.xz * texSlopeScale );
colSlope2 = texture2D( ice, pos.xz * texSlopeScale );
colDetail = texture2D( gravel, pos.xz * texDetailScale );
bMult = 1.0;
lightIntensity = 0.6;
attenuation = 0.2;
shininess = 5.0;
}
// TODO: noise-based bumpmapping & lighting turbulence
// float alpha = snoise(pos * 0.1);
// float alpha2 = snoise(pos * 0.0001) * 0.2;
// // Noise based bumpmapping
// vec3 bump = vec3(alpha*0.5 + 0.5);
// float E = 2.0;
// vec3 modNormal = vec3( (bump.x-alpha) / E, (bump.y-alpha) / E, (bump.z-alpha) / E);
// norm = normalize(norm + modNormal);
// lightIntensity *= 3.0;
// bMult *= 1.0 + alpha/10.0;
// lightIntensity *= 1.0 + alpha/10.0;
// bz = clamp(bz + alpha2, 0.0, 1.0);
// Synthesize the texture w/ linear interpolation in heightband & slope
// TODO: add detail texture (per height-band)
vec4 texBase = colBase1 * (1.0 - by) + colBase2 * by;
vec4 texSlope = colSlope1 * (1.0 - by) + colSlope2 * by;
vec4 tex = texBase * (1.0 - bz) + texSlope * bz;
tex *= bMult;
// tex *= colDetail * 2.0;
// Diffuse lighting
vec3 lightCol = vec3(1.0, 1.0, 1.0);
vec3 light_norm = normalize(vec3(-4.0, -1.0, 4.0));
float lDotN = max(dot(norm, -light_norm), 0.0) * 1.0;
// Specular lighting
vec3 specular;
// attenuation = 0.5; // allow specular lighting everywhere
// shininess = 3.0; // allow constant shininess
if (lDotN < 0.0 || attenuation == 0.0) { // light source on the wrong side?
specular = vec3(0.0, 0.0, 0.0); // no specular reflection
} else { // light source on the right side
specular = vec3(1.0, 1.0, 1.0) * pow(max(0.0, dot(reflect(light_norm, -norm), -viewDirection)), shininess);
}
vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
result.rgb = tex.rgb * (0.7) + tex.rgb * lightCol * lDotN * lightIntensity + tex.rgb * specular * attenuation;
result.rgb = applyFog(result.rgb, distance);
////////////////////////////////////////////////////
//
// TEXTURE SYNTHESIS INDIVIDUAL CONTRIBUTIONS
//
// result = colBase1 * bMult; // Heightband
// result = colSlope1 * bMult; // Slope
// result = tex; // No lighting
// result.rgb = tex.rgb * lDotN * 1.0; // Diffuse lighting
// result.rgb = tex.rgb * specular * 1.0; // Specular lighting
//
//
////////////////////////////////////////////////////
////////////////////////////////////////////////////
//
// TEXTURE MAPS FOR QUICK VISUALIZATION
//
//
//
// result.rgb = vec3( pos.y / 5000.0 ); // Altitude map
// result.rgb = norm; // Normal map
// result.rgb = vec3( slope / 650.0 ); // Slope map
//
//
// result.rgb *= uColor; // LOD map
//
//
//
////////////////////////////////////////////////////
gl_FragColor = result;
//gl_FragColor = vec4(overrideColor, overrideColor, overrideColor, 1.0);
}
</script>
<script id="skybox-vs" type="x-shader/x-vertex">
attribute vec3 aVertexCoord;
attribute vec3 aTexCoord;
uniform mat4 uMVMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
varying vec3 texPos;
varying float distance;
void main(void) {
vec4 position = uPMatrix * uVMatrix * vec4(aVertexCoord, 1.0);
gl_Position = position;
distance = sqrt(position.x*position.x + position.z*position.z);
texPos = aTexCoord.xyz;
}
</script>
<script id="skybox-fs" type="x-shader/x-fragment">
#extension GL_OES_half_float_linear : enable
precision mediump float;
uniform samplerCube TexSampler;
varying vec3 texPos;
varying float distance;
vec3 applyFog( in vec3 rgb, // original color of the pixel
in float distance ) // camera to point distance
{
float b = 0.10;
float fogAmount = 1.0 - 0.4;// exp( -distance*b );
vec3 fogColor = vec3(0.5,0.6,0.7);
return mix( rgb, fogColor, fogAmount );
}
void main(void) {
vec4 tex;
// TODO: apply fog to skybox?
tex = textureCube(TexSampler, texPos);
tex.rgb = applyFog(tex.rgb, distance);
gl_FragColor = tex;
}
</script>
<script id="water-vs" type="x-shader/x-vertex">
attribute vec3 aVertexCoord;
attribute vec2 aTexCoord;
uniform mat4 uMVMatrix;
uniform mat4 uMVYMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
uniform vec3 uOffset;
uniform vec3 viewDirection;
varying vec2 offset;
varying vec2 texPos;
varying vec3 view;
varying vec3 cameraPosition;
varying vec3 worldPosition;
//attribute vec3 skyboxTexCoord;
//varying vec3 skyboxTexPos;
//uniform float rtime;
varying float time;
void main(void) {
vec4 position = uPMatrix * uVMatrix * uMVYMatrix * vec4(aVertexCoord, 1.0);// * 0.1;
gl_Position = position;
offset = uOffset.yx * -(0.00001666 + 0.008333);// -0.012; // 1.0/farPlane == water texture always follows you; 1.0/120.0 (keeps in sync w/ landscape...why???)
texPos = aTexCoord.xy;
view = viewDirection * vec3(1.0, -1.0, -1.0); // xyz <--> (roll, yaw, pitch)
//cameraPosition = uOffset.xyz;//uOffset.yzx * -0.012;
//worldPosition = (uMVYMatrix * vec4(aVertexCoord, 1.0)).xyz;
worldPosition = aVertexCoord;
//worldPosition = (aVertexCoord + (uVMatrix * uMVYMatrix * vec4(0.0, 0.0, 0.01, 1.0)).xyz);
//time = rtime * 10000.0;
//skyboxTexPos = skyboxTexCoord.xyz;
}
</script>
<script id="water-fs" type="x-shader/x-fragment">
#extension GL_OES_standard_derivatives : enable
#extension GL_OES_half_float_linear : enable
precision mediump float;
//uniform sampler2D TexSampler;
//uniform sampler2D BumpSampler;
//uniform sampler2D DuDvSampler;
uniform sampler2D NormalSampler1;
uniform sampler2D NormalSampler2;
//uniform sampler2D rtt;
uniform samplerCube SkyboxSampler;
varying vec2 offset;
varying vec2 texPos;
varying vec3 view;
varying vec3 cameraPosition;
varying vec3 worldPosition;
//varying vec3 skyboxTexPos;
//varying float time;
uniform float time;
void main(void) {
vec4 tex;
float scale = 0.1;
float scale2 = 0.06;
//tex = texture2D(TexSampler, scale*(offset.yx + texPos.xy));
//tex.rgb *= vec3(0.05, 0.05, 0.2);
tex = vec4(0.02, 0.02, 0.06, 0.9);
//tex.a = 0.9;
//tex = texture2D(rtt, scale*(offset.yx + texPos.xy));
//vec4 bump = texture2D(BumpSampler, scale*(offset.yx + texPos.xy)) * 2.0 - 1.0;
//bump *= texture2D(BumpSampler, scale2*(offset.yx + texPos.xy)) * 2.0 - 1.0;
//vec4 dudv = texture2D(DuDvSampler, scale*(offset.yx + texPos.xy));
// Diffuse lighting
vec4 bump = texture2D(NormalSampler1, scale*(offset.yx + vec2(time,-time) + texPos.xy)) * 2.0 - 1.0;
vec4 bump2 = texture2D(NormalSampler2, scale2*(offset.yx + vec2(time,-time) + texPos.xy)) * 2.0 - 1.0;
vec3 norm = bump.xzy * 0.6 + 0.4 * bump2.xzy;
vec3 lightCol = vec3(0.2, 0.2, 0.0);
//vec3 lightPos = normalize(vec3(-200.0, 10.0, 200.0));
//vec3 lightPos = normalize(vec3(-12320.0, 2000.0, 8000.0));
//vec3 lightPos = normalize(vec3( -90.0, 20.0, 40.0 )); // xyz: (?, height, ?) // NOTE: GOOD POSITION FOR SPEC
vec3 lightPos = normalize(vec3( -41.0, 0.1, 19.0 )); // xyz: (?, height, ?)
//vec3 lightPos = normalize(vec3( -1.0, 0.1, 1.0 )); // xyz: (?, height, ?)
float lDotN = min(1.0, max(dot(norm * vec3(1.0, 1.0, 1.0), lightPos), 0.0));
//tex.rgb += lightCol * lDotN * 0.2;
float intensity = 0.6;
vec3 ambience = vec3(0.2, 0.1, 1.0);
float ambienceIntensity = 1.2;
vec3 waterCol = vec3(0.02, 0.02, 0.06);
tex.rgb = 0.3 * waterCol * (ambience * ambienceIntensity + lDotN * intensity);
/*
// Diffuse lighting
vec3 lightCol = vec3(0.0, 0.004, 0.02);
//vec3 light_norm = normalize(vec3(2.8, -0.5, -1.0));
vec3 light_norm = normalize(vec3(1.0, 0.1, -2.00));
float lDotN = min(1.0, max(dot(bump.xzy, light_norm), 0.0) * 1.0);
tex.rgb += lightCol * (lDotN) * 8.4;
*/
vec3 viewDir = normalize(worldPosition);// - cameraPosition);
vec3 reflection = normalize(reflect(viewDir, normalize(norm)));
vec3 reflectionRev = normalize(reflect(-viewDir, normalize(norm)));
// Sunlight
float sun_strength = 0.5;
vec3 sun_colour = vec3(0.664, 0.554, 0.222);
vec3 light_dir = lightPos;
float sun_shininess = 200.0;
vec3 refl_sun = sun_strength * sun_colour * pow(clamp(dot(reflectionRev, light_dir), 0.0, 1.0), sun_shininess);
// Global reflection
vec4 global_refl = 0.20 * textureCube(SkyboxSampler, reflection);
float thetai = dot(light_dir, norm);
float thetat = dot(viewDir, norm);
//float fresnel = pow( sin(thetai - thetat) / sin(thetai + thetat), 2.0 ) + pow( tan(thetai - thetat) / tan(thetai + thetat), 2.0 );
//float fresnel = pow(1.0 - cos(thetai), 5.0);// pow( sin(thetai - thetat) / sin(thetai + thetat), 2.0 ) + pow( tan(thetai - thetat) / tan(thetai + thetat), 2.0 );
//float fresnel = pow(sin(thetai), 2.0); // NOTE:
float fresnelBias = 0.6;
float fresnel = max(fresnelBias + (1.0 - fresnelBias) * pow(dot(light_dir, norm), 5.0), 0.0);
//tex.rgb = (1.0 - fresnel) * refl_sun;// + refl_sun;
tex.rgb += (1.0 - fresnel) * (refl_sun + global_refl.rgb);// + (refl_sun + global_refl.rgb);
//tex.rgb = (1.0 - fresnel) * global_refl.rgb;// + global_refl.rgb;
//vec3 rF0 = vec3(0.2);
//tex.rgb = rF0 + (vec3(1.0) - rF0) * pow(1.0 - cos(thetai), 1.0);
float direction = max(0.0, dot(viewDir, reflection));
vec3 specularColor = pow(direction, 100.0)*vec3(1.0, 1.0, 1.0)*0.85;
//vec3 diffuseColor = max(dot(lightPos, norm),0.0)*vec3(1.0, 1.0, 1.0)*4.5;
//tex.rgb += specularColor;// + diffuseColor;
tex.a = 1.0;//max(pow(direction, 1.0), 0.8);
gl_FragColor = tex;
}
</script>
<script src="js/main.js"></script>
<script src="js/viewport.js"></script>
<script src="js/generator.js"></script>
</body>
</html>