fix wwater fog
[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 #include "light_clearskies_stddef.glsl"
16 #include "common_scene.glsl"
17 #include "motion_vectors_fs.glsl"
18
19 // Pasted from common_world.glsl
20 vec3 water_compute_lighting( vec3 diffuse, vec3 normal, vec3 co )
21 {
22 float light_mask = compute_board_shadow();
23
24 if( g_light_preview == 1 )
25 diffuse = vec3(0.75);
26
27 // Lighting
28 vec3 halfview = uCamera - co;
29 float fdist = length(halfview);
30 halfview /= fdist;
31
32 float world_shadow = newlight_compute_sun_shadow(
33 co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );
34
35 vec3 total_light = clearskies_lighting(
36 normal, min( light_mask, world_shadow ), halfview );
37
38 vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
39 cube_coord = floor( cube_coord );
40
41 if( g_debug_indices == 1 )
42 {
43 return rand33(cube_coord);
44 }
45
46 if( g_debug_complexity == 1 )
47 {
48 ivec3 coord = ivec3( cube_coord );
49 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
50
51 uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
52 return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
53 }
54
55 // FIXME: this coord should absolutely must be clamped!
56
57 ivec3 coord = ivec3( cube_coord );
58 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
59
60 total_light +=
61 scene_calculate_packed_light_patch( index_sample.x,
62 halfview, co, normal )
63 * light_mask;
64 total_light +=
65 scene_calculate_packed_light_patch( index_sample.y,
66 halfview, co, normal )
67 * light_mask;
68
69 return diffuse * total_light;
70 }
71
72 vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue,
73 vec4 beneath, vec4 above, vec4 dudva )
74 {
75 vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
76
77 float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);
78
79 vec3 lightdir = vec3(0.95,0.0,-0.3);
80 vec3 specdir = reflect( -lightdir, vnorm );
81 float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
82
83 // Depth
84 float depthblend = pow( beneath.r, 0.8 );
85
86 // Foam
87 float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
88 fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
89
90 vec4 surf = mix( vec4(surface_tint,depthblend),
91 vec4(1.0,1.0,1.0,0.5), fband );
92 surf.rgb = water_compute_lighting( surf.rgb, aNorm.xyz, aWorldCo );
93 surf.rgb = mix(surf.rgb, above.rgb, ffresnel );
94
95 // Take a section of the sky function to give us a matching fog colour
96 vec3 fog_colour = clearskies_ambient( -halfview );
97 float sun_theta = dot( -halfview, g_sun_dir.xyz );
98 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );
99 float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;
100
101 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
102 sun_colour *= sun_shape;
103
104 fog_colour += sun_colour;
105 surf.rgb = scene_apply_fog( surf.rgb, fog_colour,
106 distance(uCamera, aWorldCo) );
107
108 return surf;
109 }
110
111 void main()
112 {
113 compute_motion_vectors();
114
115 // Create texture coords
116 vec2 ssuv = gl_FragCoord.xy*uInvRes;
117
118 // Surface colour composite
119 float depthvalue = clamp( -world_water_depth(aCo)*(1.0/25.0), 0.0,1.0 );
120
121 vec2 world_coord = aCo.xz * 0.008;
122 vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );
123 vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;
124 vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;
125
126 vec3 surfnorm = dudva.rgb + dudvb.rgb;
127 surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);
128
129 // Lighting
130 vec3 halfview = -normalize( aCo-uCamera );
131
132 // Sample textures
133 vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );
134 vec4 beneath = texture( uTexBack, ssuv );
135
136 // Fog
137 float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
138
139 // Composite
140 vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above, dudva );
141 vsurface.a -= fdist;
142
143 oColour = vsurface;
144 }