stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / water.fs
1 out vec4 FragColor;
2
3 uniform sampler2D uTexMain;
4 uniform sampler2D uTexDudv;
5 uniform sampler2D uTexDepth;
6 uniform sampler2D uTexBack;
7
8 uniform vec2 uInvRes;
9 uniform float uTime;
10
11 uniform vec3 uCamera;
12 uniform float uSurfaceY;
13
14 in vec4 aUv;
15 in vec3 aCo;
16 in float aDepth;
17
18 void main()
19 {
20 // Reflected and warped texture
21 vec2 ssuv = gl_FragCoord.xy*uInvRes;
22
23 vec4 dudva = texture( uTexDudv, aUv.xy - vec2(uTime*0.004f,uTime*0.003f) );
24 vec4 dudvb = texture( uTexDudv, aUv.xy*0.7 - vec2(uTime*0.01,uTime*0.0054) );
25 vec2 distortamt = (dudva.rg-0.5) * (dudvb.ba-0.5) * 0.6;
26
27 vec4 reflected = texture( uTexMain, ssuv+distortamt );
28
29 // Surface colour composite
30 float depthvalue = texture( uTexDepth, aUv.zw ).r;
31
32 vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );
33 vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );
34 vec3 surface_tint = mix(colour_shore, colour_ocean, pow(depthvalue,1.8))*1.5;
35
36 // Foam
37 float fband = fract( aCo.z*0.1+uTime*0.1-depthvalue*10.0 );
38 fband = step( fband+dudvb.g*0.8, 0.5 ) * max((1.0-depthvalue*4.0),0.0);
39
40 // Lighting
41 vec3 surfnorm = vec3(distortamt.x,1.0,distortamt.y);
42
43 vec3 halfview = -normalize( aCo-uCamera );
44 float ffresnel = pow(1.0-dot( surfnorm, halfview ),5.0);
45
46 vec3 lightdir = vec3(0.95,0.0,-0.3);
47 vec3 specdir = reflect( -lightdir, surfnorm );
48 float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
49
50 // Depth
51 vec4 backsample = texture( uTexBack, ssuv+distortamt*0.1 );
52 float depthblend = pow(backsample.a,0.8);
53
54 // Composite
55 vec3 vsurface = mix(surface_tint*backsample.rgb, reflected.rgb, ffresnel );
56 vsurface += spec;
57
58 FragColor = mix( vec4(vsurface,depthblend), vec4(1.0,1.0,1.0,0.8), fband );
59 }