change shader properties to be vg_msg based
[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 uniform float uFresnel;
15 uniform float uWaterScale;
16 uniform vec4 uWaveSpeed;
17
18 #include "light_clearskies_stddef.glsl"
19 #include "common_scene.glsl"
20 #include "motion_vectors_fs.glsl"
21
22 // Pasted from common_world.glsl
23 vec3 water_compute_lighting( vec3 diffuse, vec3 normal, vec3 co )
24 {
25 float light_mask = compute_board_shadow();
26
27 if( g_light_preview == 1 )
28 diffuse = vec3(0.75);
29
30 // Lighting
31 vec3 halfview = uCamera - co;
32 float fdist = length(halfview);
33 halfview /= fdist;
34
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)) );
37
38 vec3 total_light = clearskies_lighting(
39 normal, min( light_mask, world_shadow ), halfview );
40
41 vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;
42 cube_coord = floor( cube_coord );
43
44 if( g_debug_indices == 1 )
45 {
46 return rand33(cube_coord);
47 }
48
49 if( g_debug_complexity == 1 )
50 {
51 ivec3 coord = ivec3( cube_coord );
52 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
53
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 );
56 }
57
58 // FIXME: this coord should absolutely must be clamped!
59
60 ivec3 coord = ivec3( cube_coord );
61 uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
62
63 total_light +=
64 scene_calculate_packed_light_patch( index_sample.x,
65 halfview, co, normal )
66 * light_mask;
67 total_light +=
68 scene_calculate_packed_light_patch( index_sample.y,
69 halfview, co, normal )
70 * light_mask;
71
72 return diffuse * total_light;
73 }
74
75 vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue,
76 vec4 beneath, vec4 above, vec4 dudva )
77 {
78 vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);
79
80 float ffresnel = pow(1.0-dot( vnorm, halfview ),uFresnel);
81
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;
85
86 // Depth
87 float depthblend = pow( beneath.r, 0.8 );
88
89 // Foam
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);
92
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 );
97
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;
103
104 vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );
105 sun_colour *= sun_shape;
106
107 fog_colour += sun_colour;
108 surf.rgb = scene_apply_fog( surf.rgb, fog_colour,
109 distance(uCamera, aWorldCo) );
110
111 return surf;
112 }
113
114 void main()
115 {
116 compute_motion_vectors();
117
118 // Create texture coords
119 vec2 ssuv = gl_FragCoord.xy*uInvRes;
120
121 // Surface colour composite
122 float depthvalue = clamp( -world_water_depth(aCo)*(1.0/25.0), 0.0,1.0 );
123
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;
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 }