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