move compiler to its own folder, api changes. wip maps
[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 //TODO: Make exponent a material param (default: 5.0)
78 float ffresnel = pow(1.0-dot( vnorm, halfview ),0.8);
79
80 vec3 lightdir = vec3(0.95,0.0,-0.3);
81 vec3 specdir = reflect( -lightdir, vnorm );
82 float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
83
84 // Depth
85 float depthblend = pow( beneath.r, 0.8 );
86
87 // Foam
88 float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
89 fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
90
91 vec4 surf = mix( vec4(surface_tint,depthblend),
92 vec4(1.0,1.0,1.0,0.5), fband );
93 surf.rgb = water_compute_lighting( surf.rgb, aNorm.xyz, aWorldCo );
94 surf.rgb = mix(surf.rgb, above.rgb, ffresnel );
95
96 // Take a section of the sky function to give us a matching fog colour
97 vec3 fog_colour = clearskies_ambient( -halfview );
98 float sun_theta = dot( -halfview, g_sun_dir.xyz );
99 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );
100 float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;
101
102 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
103 sun_colour *= sun_shape;
104
105 fog_colour += sun_colour;
106 surf.rgb = scene_apply_fog( surf.rgb, fog_colour,
107 distance(uCamera, aWorldCo) );
108
109 return surf;
110 }
111
112 void main()
113 {
114 compute_motion_vectors();
115
116 // Create texture coords
117 vec2 ssuv = gl_FragCoord.xy*uInvRes;
118
119 // Surface colour composite
120 float depthvalue = clamp( -world_water_depth(aCo)*(1.0/25.0), 0.0,1.0 );
121
122 //TODO: Material param (default: 0.008)
123 vec2 world_coord = aCo.xz * 0.12;
124 //TODO: Material param ( 0.008, 0.006, 0.003, 0.03 );
125 vec4 time_offsets = vec4( uTime ) * vec4( 0.08, -0.08, -0.03, -0.01 );
126 vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;
127 vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;
128
129 vec3 surfnorm = dudva.rgb + dudvb.rgb;
130 surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);
131
132 // Lighting
133 vec3 halfview = -normalize( aCo-uCamera );
134
135 // Sample textures
136 vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );
137 vec4 beneath = texture( uTexBack, ssuv );
138
139 // Fog
140 float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
141
142 // Composite
143 vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above, dudva );
144 vsurface.a -= fdist;
145
146 oColour = vsurface;
147 }