Debian Codegraph @

Shaders/bowwave-ALS.frag

raw
1 // This shader is mostly an adaptation of the shader found at
2 // http://www.bonzaisoftware.com/water_tut.html and its glsl conversion
3 // available at http://forum.bonzaisoftware.com/viewthread.php?tid=10
4 // © Michael Horsch - 2005
5 // Major update and revisions - 2011-10-07
6 // © Emilian Huminiuc and Vivian Meazza
7 // Optimisation - 2012-5-05
8 // © Emilian Huminiuc and Vivian Meazza
9 // Ported to the Atmospheric Light Scattering Framework
10 // by Thorsten Renk, Aug. 2013
11
12 #version 120
13 #define fps2kts 0.5925
14
15 uniform sampler2D water_normalmap;
16 uniform sampler2D water_reflection;
17 uniform sampler2D water_dudvmap;
18 uniform sampler2D water_reflection_grey;
19 uniform sampler2D sea_foam;
20 uniform sampler2D alpha_tex;
21 uniform sampler2D bowwave_nmap;
22
23 uniform float saturation, Overcast, WindE, WindN, spd, hdg;
24 uniform float CloudCover0, CloudCover1, CloudCover2, CloudCover3, CloudCover4;
25 uniform int Status;
26
27 uniform float hazeLayerAltitude;
28 uniform float terminator;
29 uniform float terrain_alt;
30 uniform float avisibility;
31 uniform float visibility;
32 uniform float overcast;
33 uniform float scattering;
34 uniform float ground_scattering;
35 uniform float cloud_self_shading;
36 uniform float eye_alt;
37 uniform float fogstructure;
38 uniform float ice_cover;
39 uniform float sea_r;
40 uniform float sea_g;
41 uniform float sea_b;
42
43 uniform int quality_level;
44
45
46 varying vec4 waterTex1; //moving texcoords
47 varying vec4 waterTex2; //moving texcoords
48 varying vec3 viewerdir;
49 varying vec3 lightdir;
50 varying vec3 normal;
51 varying vec3 relPos;
52 varying float earthShade;
53 varying float yprime_alt;
54 varying float mie_angle;
55 varying float steepness;
56
57 vec3 specular_light;
58
59 float fog_func (in float targ, in float alt);
60
61 vec3 get_hazeColor(in float light_arg);
62 vec3 filter_combined (in vec3 color) ;
63
64 const float terminator_width = 200000.0;
65 const float EarthRadius = 5800000.0;
66
67
68 /////// functions /////////
69
70 float normalize_range(float _val)
71 {
72 if (_val > 180.0)
73 return _val - 360.0;
74 else
75 return _val;
76 }
77
78
79 void relWind(out float rel_wind_speed_kts, out float rel_wind_from_rad)
80 {
81 //calculate the carrier speed north and east in kts
82 float speed_north_kts = cos(radians(hdg)) * spd ;
83 float speed_east_kts = sin(radians(hdg)) * spd ;
84
85 //calculate the relative wind speed north and east in kts
86 float rel_wind_speed_from_east_kts = WindE*fps2kts + speed_east_kts;
87 float rel_wind_speed_from_north_kts = WindN*fps2kts + speed_north_kts;
88
89 //combine relative speeds north and east to get relative windspeed in kts
90 rel_wind_speed_kts = sqrt(rel_wind_speed_from_east_kts*rel_wind_speed_from_east_kts
91 + rel_wind_speed_from_north_kts*rel_wind_speed_from_north_kts);
92
93 //calculate the relative wind direction
94 float rel_wind_from_deg = degrees(atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts));
95 // rel_wind_from_rad = atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts);
96 float rel_wind = rel_wind_from_deg - hdg;
97 rel_wind = normalize_range(rel_wind);
98 rel_wind_from_rad = radians(rel_wind);
99 }
100
101 void rotationmatrix(in float angle, out mat4 rotmat)
102 {
103 rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
104 sin( angle ), cos( angle ), 0.0, 0.0,
105 0.0 , 0.0 , 1.0, 0.0,
106 0.0 , 0.0 , 0.0, 1.0 );
107 }
108
109
110 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
111 {
112 x = x - 0.5;
113
114 // use the asymptotics to shorten computations
115 if (x > 30.0) {return e;}
116 if (x < -15.0) {return 0.0;}
117
118 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
119 }
120
121 // this determines how light is attenuated in the distance
122 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
123 // for distance > visibility
124
125
126
127 //////////////////////
128
129 void main(void)
130 {
131 const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
132 const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
133 const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
134
135 mat4 RotationMatrix;
136
137 float relWindspd=0;
138 float relWinddir=0;
139
140 float dist = length(relPos);
141 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
142 float effective_scattering = min(scattering, cloud_self_shading);
143
144 // compute relative wind speed and direction
145 relWind (relWindspd, relWinddir);
146
147 rotationmatrix(relWinddir, RotationMatrix);
148
149 // compute direction to viewer
150 vec3 E = normalize(viewerdir);
151
152 // compute direction to light source
153 vec3 L = normalize(lightdir);
154
155 // half vector
156 vec3 H = normalize(L + E);
157
158 const float water_shininess = 240.0;
159 // approximate cloud cover
160 float cover = 0.0;
161 //bool Status = true;
162
163 float windEffect = relWindspd; //wind speed in kt
164 // float windEffect = sqrt(pow(abs(WindE),2)+pow(abs(WindN),2)) * 0.6; //wind speed in kt
165 float windScale = 15.0/(5.0 + windEffect); //wave scale
166 float waveRoughness = 0.05 + smoothstep(0.0, 50.0, windEffect); //wave roughness filter
167
168
169 if (Status == 1){
170 cover = min(min(min(min(CloudCover0, CloudCover1),CloudCover2),CloudCover3),CloudCover4);
171 } else {
172 // hack to allow for Overcast not to be set by Local Weather
173
174 if (Overcast == 0){
175 cover = 5;
176 } else {
177 cover = Overcast * 5;
178 }
179
180 }
181
182 //vec4 viewt = normalize(waterTex4);
183 vec4 viewt = vec4(-E, 0.0) * 0.6;
184
185 vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)* windScale * 2.0) * 2.0 - 1.0;
186 vec4 dist1 = texture2D(water_dudvmap, vec2(waterTex1 + disdis*sca2)* windScale * 2.0) * 2.0 - 1.0;
187 vec4 fdist = normalize(dist1);
188 fdist = -fdist;
189 fdist *= sca;
190
191 //normalmap
192 rotationmatrix(-relWinddir, RotationMatrix);
193
194 vec4 nmap0 = texture2D(water_normalmap, vec2((waterTex1 + disdis*sca2) * RotationMatrix ) * windScale * 2.0) * 2.0 - 1.0;
195 vec4 nmap2 = texture2D(water_normalmap, vec2(waterTex2 * tscale * RotationMatrix ) * windScale * 2.0) * 2.0 - 1.0;
196 vec4 nmap3 = texture2D(bowwave_nmap, gl_TexCoord[0].st) * 2.0 - 1.0;
197 vec4 vNorm = normalize(mix(nmap3, nmap0 + nmap2, 0.3 )* waveRoughness);
198 vNorm = -vNorm;
199
200 //load reflection
201 vec4 tmp = vec4(lightdir, 0.0);
202 vec4 refTex = texture2D(water_reflection, vec2(tmp + waterTex1) * 32.0) ;
203 vec4 refTexGrey = texture2D(water_reflection_grey, vec2(tmp + waterTex1) * 32.0) ;
204 vec4 refl ;
205 // cover = 0;
206
207 /*if(cover >= 1.5){
208 refl= normalize(refTex);
209 }
210 else
211 {
212 refl = normalize(refTexGrey);
213 refl.r *= (0.75 + 0.15 * cover);
214 refl.g *= (0.80 + 0.15 * cover);
215 refl.b *= (0.875 + 0.125 * cover);
216 refl.a *= 1.0;
217 }
218 */
219
220 refl.r = sea_r;
221 refl.g = sea_g;
222 refl.b = sea_b;
223 refl.a = 1.0;
224
225
226 float intensity;
227 // de-saturate for reduced light
228 refl.rgb = mix(refl.rgb, vec3 (0.248, 0.248, 0.248), 1.0 - smoothstep(0.1, 0.8, ground_scattering));
229
230 // de-saturate light for overcast haze
231 intensity = length(refl.rgb);
232 refl.rgb = mix(refl.rgb, intensity * vec3 (1.0, 1.0, 1.0), 0.5 * smoothstep(0.1, 0.9, overcast));
233
234 vec3 N0 = vec3(texture2D(water_normalmap, vec2((waterTex1 + disdis*sca2)* RotationMatrix) * windScale * 2.0) * 2.0 - 1.0);
235 vec3 N1 = vec3(texture2D(water_normalmap, vec2(waterTex2 * tscale * RotationMatrix ) * windScale * 2.0) * 2.0 - 1.0);
236 vec3 N2 = vec3(texture2D(bowwave_nmap, gl_TexCoord[0].st)*2.0-1.0);
237 //vec3 Nf = normalize((normal+N0+N1)*waveRoughness);
238 vec3 N = normalize(mix(normal+N2, normal+N0+N1, 0.3)* waveRoughness);
239 N = -N;
240
241 // specular
242
243 specular_light = gl_Color.rgb;
244
245 vec3 specular_color = vec3(specular_light)
246 * pow(max(0.0, dot(N, H)), water_shininess) * 6.0;
247 vec4 specular = vec4(specular_color, 0.5);
248
249 specular = specular * saturation * 0.3;
250
251 //calculate fresnel
252 vec4 invfres = vec4( dot(vNorm, viewt) );
253 vec4 fres = vec4(1.0) + invfres;
254 refl *= fres;
255
256 vec4 alpha0 = texture2D(alpha_tex, gl_TexCoord[0].st);
257
258 //calculate final colour
259 vec4 ambient_light;
260 ambient_light.rgb = max(specular_light.rgb, vec3(0.1, 0.1, 0.1));
261 ambient_light.a = 1.0;
262 vec4 finalColor;
263
264 finalColor = refl + specular * smoothstep(0.3, 0.6, ground_scattering);
265
266 //add foam
267
268 float foamSlope = 0.05 + 0.01 * windScale;
269 //float waveSlope = mix(N0.g, N1.g, 0.25);
270
271 vec4 foam_texel = texture2D(sea_foam, vec2(waterTex2 * tscale) * 50.0);
272 float waveSlope = N.g;
273
274 if (windEffect >= 12.0)
275 if (waveSlope >= foamSlope){
276 finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel), smoothstep(foamSlope, 0.5, N.g));
277 }
278
279 //generate final colour
280 finalColor *= ambient_light;//+ alpha0 * 0.35;
281
282
283
284 float delta_z = hazeLayerAltitude - eye_alt;
285
286
287 if (dist > 40.0)
288 {
289
290
291 float transmission;
292 float vAltitude;
293 float delta_zv;
294 float H;
295 float distance_in_layer;
296 float transmission_arg;
297
298
299 // angle with horizon
300 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
301
302 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
303
304
305 if (delta_z > 0.0) // we're inside the layer
306 {
307 if (ct < 0.0) // we look down
308 {
309 distance_in_layer = dist;
310 vAltitude = min(distance_in_layer,min(visibility,avisibility)) * ct;
311 delta_zv = delta_z - vAltitude;
312 }
313 else // we may look through upper layer edge
314 {
315 H = dist * ct;
316 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
317 else {distance_in_layer = dist;}
318 vAltitude = min(distance_in_layer,visibility) * ct;
319 delta_zv = delta_z - vAltitude;
320 }
321 }
322 else // we see the layer from above, delta_z < 0.0
323 {
324 H = dist * -ct;
325 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
326 {
327 distance_in_layer = 0.0;
328 delta_zv = 0.0;
329 }
330 else
331 {
332 vAltitude = H + delta_z;
333 distance_in_layer = vAltitude/H * dist;
334 vAltitude = min(distance_in_layer,visibility) * (-ct);
335 delta_zv = vAltitude;
336 }
337 }
338
339
340 // ground haze cannot be thinner than aloft visibility in the model,
341 // so we need to use aloft visibility otherwise
342
343 transmission_arg = (dist-distance_in_layer)/avisibility;
344
345
346 float eqColorFactor;
347
348
349 if (visibility < avisibility)
350 {
351 if (quality_level > 3)
352 {
353 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility ));
354 }
355 else
356 {
357 transmission_arg = transmission_arg + (distance_in_layer/visibility);
358 }
359 // this combines the Weber-Fechner intensity
360 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
361 }
362 else
363 {
364 if (quality_level > 3)
365 {
366 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility ));
367 }
368 else
369 {
370 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
371 }
372 // this combines the Weber-Fechner intensity
373 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
374 }
375
376
377 transmission = fog_func(transmission_arg, eye_alt);
378
379 // there's always residual intensity, we should never be driven to zero
380 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
381
382
383 float lightArg = (terminator-yprime_alt)/100000.0;
384
385 vec3 hazeColor = get_hazeColor(lightArg);
386
387
388 // now dim the light for haze
389 float eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
390
391
392 // Mie-like factor
393
394 if (lightArg < 10.0)
395 {intensity = length(hazeColor);
396 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
397 hazeColor = intensity * ((1.0 - mie_magnitude) + mie_magnitude * mie_angle) * normalize(mix(hazeColor, vec3 (0.5, 0.58, 0.65), mie_magnitude * (0.5 - 0.5 * mie_angle)) );
398 }
399
400 // high altitude desaturation of the haze color
401
402 intensity = length(hazeColor);
403
404
405 if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
406 {
407 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, eye_alt)));
408
409 // blue hue of haze
410
411 hazeColor.x = hazeColor.x * 0.83;
412 hazeColor.y = hazeColor.y * 0.9;
413
414
415 // additional blue in indirect light
416 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
417 intensity = length(hazeColor);
418 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
419
420 // change haze color to blue hue for strong fogging
421 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
422 }
423
424
425
426 // don't let the light fade out too rapidly
427 lightArg = (terminator + 200000.0)/100000.0;
428 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
429 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
430
431 hazeColor.rgb *= eqColorFactor * eShade;
432 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
433
434 finalColor.rgb = mix(hazeColor, finalColor.rgb,transmission);
435
436
437 }
438
439
440 finalColor.rgb = filter_combined(finalColor.rgb);
441 gl_FragColor = vec4(finalColor.rgb, alpha0.a * 1.35);
442
443
444 }