1 uniform sampler2D uTexMain;
2 uniform sampler2D uTexDudv;
3 uniform sampler2D uTexBack;
8 uniform float uSurfaceY;
12 uniform vec3 uShoreColour;
13 uniform vec3 uOceanColour;
14 uniform float uFresnel;
15 uniform float uWaterScale;
16 uniform vec4 uWaveSpeed;
18 #include "light_clearskies_stddef.glsl"
19 #include "common_scene.glsl"
20 #include "motion_vectors_fs.glsl"
22 // Pasted from common_world.glsl
23 vec3 water_compute_lighting( vec3 diffuse, vec3 normal, vec3 co )
25 float light_mask = compute_board_shadow();
27 if( g_light_preview == 1 )
31 vec3 halfview = uCamera - co;
32 float fdist = length(halfview);
35 float world_shadow = newlight_compute_sun_shadow(
36 co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );
38 vec3 total_light = clearskies_lighting(
39 normal, min( light_mask, world_shadow ), halfview );
41 vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
42 cube_coord = floor( cube_coord );
44 if( g_debug_indices == 1 )
46 return rand33(cube_coord);
49 if( g_debug_complexity == 1 )
51 ivec3 coord = ivec3( cube_coord );
52 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
54 uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
55 return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
58 // FIXME: this coord should absolutely must be clamped!
60 ivec3 coord = ivec3( cube_coord );
61 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
64 scene_calculate_packed_light_patch( index_sample.x,
65 halfview, co, normal )
68 scene_calculate_packed_light_patch( index_sample.y,
69 halfview, co, normal )
72 return diffuse * total_light;
75 vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue,
76 vec4 beneath, vec4 above, vec4 dudva )
78 vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
80 float ffresnel = pow(1.0-dot( vnorm, halfview ),uFresnel);
82 vec3 lightdir = vec3(0.95,0.0,-0.3);
83 vec3 specdir = reflect( -lightdir, vnorm );
84 float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;
87 float depthblend = pow( beneath.r, 0.8 );
90 float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );
91 fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);
93 vec4 surf = mix( vec4(surface_tint,depthblend),
94 vec4(1.0,1.0,1.0,0.5), fband );
95 surf.rgb = water_compute_lighting( surf.rgb, aNorm.xyz, aWorldCo );
96 surf.rgb = mix(surf.rgb, above.rgb, ffresnel );
98 // Take a section of the sky function to give us a matching fog colour
99 vec3 fog_colour = clearskies_ambient( -halfview );
100 float sun_theta = dot( -halfview, g_sun_dir.xyz );
101 float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );
102 float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;
104 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
105 sun_colour *= sun_shape;
107 fog_colour += sun_colour;
108 surf.rgb = scene_apply_fog( surf.rgb, fog_colour,
109 distance(uCamera, aWorldCo) );
116 compute_motion_vectors();
118 // Create texture coords
119 vec2 ssuv = gl_FragCoord.xy*uInvRes;
121 // Surface colour composite
122 float depthvalue = clamp( -world_water_depth(aCo)*(1.0/25.0), 0.0,1.0 );
124 vec2 world_coord = aCo.xz * uWaterScale;
125 vec4 time_offsets = vec4( uTime ) * uWaveSpeed;
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;
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);
133 vec3 halfview = -normalize( aCo-uCamera );
136 vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );
137 vec4 beneath = texture( uTexBack, ssuv );
140 float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);
143 vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above, dudva );