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