framebuffer formalitites
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / water.fs
1 uniform sampler2D uTexMain;
2 uniform sampler2D uTexDudv;
3 uniform sampler2D uTexBack;
4
5 uniform vec2 uInvRes;
6 uniform float uTime;
7 uniform vec3 uCamera;
8 uniform float uSurfaceY;
9
10 uniform vec3 uShoreColour;
11 uniform vec3 uOceanColour;
12
13 in vec4 aColour;
14 in vec2 aUv;
15 in vec3 aNorm;
16 in vec3 aCo;
17 in vec3 aWorldCo;
18
19 #include "common_world.glsl"
20 #include "motion_vectors_fs.glsl"
21
22 vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue,
23 vec4 beneath, vec4 above )
24 {
25 vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
26
27 float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);
28
29 vec3 lightdir = vec3(0.95,0.0,-0.3);
30 vec3 specdir = reflect( -lightdir, vnorm );
31 float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
32
33 // Depth
34 float depthblend = pow( beneath.a,0.8 );
35
36 // Composite
37 vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );
38 //vsurface += spec;
39
40 return vec4( vsurface,depthblend );
41 }
42
43 void main()
44 {
45 compute_motion_vectors();
46
47 // Create texture coords
48 vec2 ssuv = gl_FragCoord.xy*uInvRes;
49
50 // Surface colour composite
51 float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );
52
53 vec2 world_coord = aCo.xz * 0.008;
54 vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );
55 vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;
56 vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;
57
58 vec3 surfnorm = dudva.rgb + dudvb.rgb;
59 surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);
60
61 // Foam
62 float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
63 fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
64
65 // Lighting
66 vec3 halfview = -normalize( aCo-uCamera );
67
68 // Sample textures
69 vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );
70 vec4 beneath = texture( uTexBack, ssuv );
71
72 // Fog
73 float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
74
75 // Composite
76 vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );
77 vsurface.a -= fdist;
78 oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );
79 }