e7cbc487565af8f85230e54ed9ca84d7afec2153
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_water_fast.fs
1 uniform sampler2D uTexDudv;
2
3 uniform float uTime;
4 uniform vec3 uCamera;
5 uniform float uSurfaceY;
6 uniform vec3 uBoard0;
7 uniform vec3 uBoard1;
8
9 uniform vec3 uShoreColour;
10 uniform vec3 uOceanColour;
11
12 in vec2 aUv;
13 in vec4 aNorm;
14 in vec3 aCo;
15 in vec3 aWorldCo;
16 flat in ivec4 aLights;
17
18 #include "common_scene.glsl"
19 #include "motion_vectors_fs.glsl"
20
21 vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )
22 {
23 vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
24
25 float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);
26
27 vec3 lightdir = vec3(0.95,0.0,-0.3);
28 vec3 specdir = reflect( -lightdir, vnorm );
29 float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
30
31 return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );
32 }
33
34 void main()
35 {
36 compute_motion_vectors();
37
38 // Surface colour composite
39 float depthvalue = clamp( -world_water_depth( aCo )*(1.0/25.0), 0.0, 1.0 );
40
41 vec2 world_coord = aCo.xz * 0.008;
42 vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );
43 vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;
44 vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;
45
46 vec3 surfnorm = dudva.rgb + dudvb.rgb;
47 surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);
48
49 // Foam
50 float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
51 fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
52
53 // Lighting
54 vec3 halfview = -normalize( aCo-uCamera );
55
56 // Fog
57 float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
58
59 // Composite
60 vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );
61 vsurface.a -= fdist;
62 oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );
63 }