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