move compiler to its own folder, api changes. wip maps
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_water.fs
index 645569900d14dd1ab6fb6d016584730f196c470f..17873083c340c64151ab07200f8a0c6fe45213d0 100644 (file)
@@ -12,34 +12,101 @@ uniform vec3 uBoard1;
 uniform vec3 uShoreColour;
 uniform vec3 uOceanColour;
 
-in vec2 aUv;
-in vec4 aNorm;
-in vec3 aCo;
-in vec3 aWorldCo;
-flat in ivec4 aLights;
-
+#include "light_clearskies_stddef.glsl"
 #include "common_scene.glsl"
 #include "motion_vectors_fs.glsl"
 
+// Pasted from common_world.glsl
+vec3 water_compute_lighting( vec3 diffuse, vec3 normal, vec3 co )
+{
+   float light_mask = compute_board_shadow();
+
+   if( g_light_preview == 1 )
+      diffuse = vec3(0.75);
+
+   // Lighting
+   vec3 halfview = uCamera - co;
+   float fdist = length(halfview);
+   halfview /= fdist;
+
+   float world_shadow = newlight_compute_sun_shadow( 
+               co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );
+
+   vec3 total_light = clearskies_lighting( 
+                           normal, min( light_mask, world_shadow ), halfview );
+
+   vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
+        cube_coord = floor( cube_coord );
+
+   if( g_debug_indices == 1 )
+   {
+      return rand33(cube_coord);
+   }
+
+   if( g_debug_complexity == 1 )
+   {
+      ivec3 coord = ivec3( cube_coord );
+      uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
+
+      uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
+      return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
+   }
+
+   // FIXME: this coord should absolutely must be clamped!
+   
+   ivec3 coord = ivec3( cube_coord );
+   uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
+
+   total_light += 
+      scene_calculate_packed_light_patch( index_sample.x,
+                                          halfview, co, normal ) 
+                                          * light_mask;
+   total_light += 
+      scene_calculate_packed_light_patch( index_sample.y,
+                                          halfview, co, normal )
+                                          * light_mask;
+
+   return diffuse * total_light;
+}
+
 vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, 
-                 vec4 beneath, vec4 above )
+                 vec4 beneath, vec4 above, vec4 dudva )
 {
    vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
 
-   float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);
+   //TODO: Make exponent a material param (default: 5.0)
+   float ffresnel = pow(1.0-dot( vnorm, halfview ),0.8);
 
    vec3 lightdir = vec3(0.95,0.0,-0.3);
    vec3 specdir = reflect( -lightdir, vnorm );
    float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
    
    // Depth 
-   float depthblend = pow( beneath.a,0.8 );
+   float depthblend = pow( beneath.r, 0.8 );
 
-   // Composite
-   vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );
-   //vsurface += spec;
+   // Foam
+   float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
+   fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
 
-   return vec4( vsurface,depthblend );
+   vec4 surf = mix( vec4(surface_tint,depthblend), 
+                    vec4(1.0,1.0,1.0,0.5), fband );
+   surf.rgb = water_compute_lighting( surf.rgb, aNorm.xyz, aWorldCo );
+   surf.rgb = mix(surf.rgb, above.rgb, ffresnel );
+
+   // Take a section of the sky function to give us a matching fog colour
+   vec3 fog_colour  = clearskies_ambient( -halfview );
+   float sun_theta  = dot( -halfview, g_sun_dir.xyz );
+   float sun_size   = max( 0.0, sun_theta * 0.5 + 0.5 );
+   float sun_shape  = sun_size * max(g_sun_dir.y,0.0) * 0.5;
+         
+   vec3 sun_colour  = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
+        sun_colour *= sun_shape;
+
+   fog_colour += sun_colour;
+   surf.rgb = scene_apply_fog( surf.rgb, fog_colour,  
+                               distance(uCamera, aWorldCo) );
+
+   return surf;
 }
 
 void main()
@@ -50,20 +117,18 @@ void main()
    vec2 ssuv = gl_FragCoord.xy*uInvRes;
    
    // Surface colour composite
-   float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );
+   float depthvalue = clamp( -world_water_depth(aCo)*(1.0/25.0), 0.0,1.0 );
 
-   vec2 world_coord = aCo.xz * 0.008;
-   vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );
+   //TODO: Material param (default: 0.008)
+   vec2 world_coord = aCo.xz * 0.12;
+   //TODO: Material param ( 0.008, 0.006, 0.003, 0.03 );
+   vec4 time_offsets = vec4( uTime ) * vec4( 0.08, -0.08, -0.03, -0.01 );
    vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;
    vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;
 
    vec3 surfnorm = dudva.rgb + dudvb.rgb;
    surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);
    
-   // Foam
-   float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
-   fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
-
    // Lighting
    vec3 halfview = -normalize( aCo-uCamera );
 
@@ -75,7 +140,8 @@ void main()
    float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
 
    // Composite
-   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );
+   vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above, dudva );
    vsurface.a -= fdist;
-   oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );
+
+   oColour = vsurface;
 }