Debian Codegraph @

Shaders/runway-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // written by Thorsten Renk, Oct 2011, based on default.frag
5 // Ambient term comes in gl_Color.rgb.
6 varying vec4 diffuse_term;
7 varying vec3 normal;
8 varying vec3 relPos;
9 varying vec2 rawPos;
10 varying vec3 worldPos;
11 varying vec3 ecViewdir;
12 varying vec4 ecPosition;
13
14
15 uniform sampler2D texture;
16 uniform sampler2D NormalTex;
17
18 varying float steepness;
19
20
21 uniform float visibility;
22 uniform float avisibility;
23 uniform float scattering;
24 uniform float terminator;
25 uniform float terrain_alt;
26 uniform float hazeLayerAltitude;
27 uniform float overcast;
28 uniform float eye_alt;
29 uniform float snowlevel;
30 uniform float dust_cover_factor;
31 uniform float lichen_cover_factor;
32 uniform float wetness;
33 uniform float rain_norm;
34 uniform float fogstructure;
35 uniform float snow_thickness_factor;
36 uniform float cloud_self_shading;
37 uniform float uvstretch;
38 uniform float landing_light1_offset;
39 uniform float landing_light2_offset;
40 uniform float landing_light3_offset;
41 uniform float air_pollution;
42 uniform float osg_SimulationTime;
43
44 uniform int quality_level;
45 uniform int tquality_level;
46 uniform int cloud_shadow_flag;
47 uniform int use_searchlight;
48 uniform int use_landing_light;
49 uniform int use_alt_landing_light;
50 uniform int overlay_flag;
51
52 const float EarthRadius = 5800000.0;
53 const float terminator_width = 200000.0;
54
55 float alt;
56 float eShade;
57 float yprime_alt;
58 float mie_angle;
59
60 float shadow_func (in float x, in float y, in float noise, in float dist);
61 float Noise2D(in vec2 coord, in float wavelength);
62 float Noise3D(in vec3 coord, in float wavelength);
63 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
64 float fog_func (in float targ, in float alt);
65 float alt_factor(in float eye_alt, in float vertex_alt);
66 float light_distance_fading(in float dist);
67 float fog_backscatter(in float avisibility);
68 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
69
70 vec3 searchlight();
71 vec3 landing_light(in float offset, in float offsetv);
72 vec3 lightspot(in vec3 relPos);
73 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
74 vec3 get_hazeColor(in float light_arg);
75 vec3 filter_combined (in vec3 color) ;
76
77 float getShadowing();
78 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
79
80
81 void main()
82 {
83
84 yprime_alt = diffuse_term.a;
85 mie_angle = gl_Color.a;
86 float effective_scattering = min(scattering, cloud_self_shading);
87
88 // distance to fragment
89 float dist = length(relPos);
90 // angle of view vector with horizon
91 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
92
93
94 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
95 // this is taken from default.frag
96 vec3 n;
97 float NdotL, NdotHV;
98 vec4 color = gl_Color;
99 color.a = 1.0;
100 vec3 lightDir = gl_LightSource[0].position.xyz;
101 vec3 E = normalize(ecViewdir);
102 vec3 halfVector;
103 if (quality_level<6)
104 {halfVector = gl_LightSource[0].halfVector.xyz;}
105 else
106 {halfVector = normalize(normalize(lightDir) + E);}
107
108 vec4 texel;
109 vec4 snow_texel;
110 vec4 detail_texel;
111 vec4 mix_texel;
112 vec4 fragColor;
113 vec4 specular = vec4(0.0);
114 float intensity;
115
116
117 // get noise at different wavelengths
118
119 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
120 // 50m: detail texel
121 // 250m: detail texel
122 // 500m: distortion and overlay
123 // 1500m: overlay, detail, dust, fog
124 // 2000m: overlay, detail, snow, fog
125
126 float noise_01m;
127 float noise_1m = Noise2D(rawPos.xy, 1.0);
128 float noise_10m = Noise2D(rawPos.xy, 10.0);
129 float noise_20m = Noise2D(rawPos.xy, 20.0);
130 float noise_5m = Noise2D(rawPos.xy ,5.0);
131
132
133
134 float noise_50m = Noise2D(rawPos.xy, 50.0);
135
136
137 float noise_1500m = Noise3D(worldPos.xyz, 1500.0);
138 float noise_2000m = Noise3D(worldPos.xyz, 2000.0);
139
140
141
142
143 //
144
145
146 // get the texels
147
148 texel = texture2D(texture, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t * uvstretch));
149 vec4 nmap = texture2D(NormalTex, gl_TexCoord[0].st * 8.0);
150 vec3 N = nmap.rgb * 2.0 - 1.0;
151
152
153 // skid marks by procedural darkening
154
155 float mix_factor = smoothstep(0.0, 0.3, 1.0 - gl_TexCoord[0].t)* smoothstep(0.0, 0.1, gl_TexCoord[0].t);
156 float threshold;
157
158 if (overlay_flag > 0)
159 {
160 float centerness = 2.0 * (0.5 - abs(gl_TexCoord[0].s - 0.5));
161
162 if (overlay_flag == 1)
163 {
164 threshold = 0.4 + 0.3 * (1.0 -noise_50m);
165 }
166 else if (overlay_flag == 2)
167 {
168 threshold = mix(0.4 + 0.3 * (1.0 -noise_50m), 0.4, mix_factor);
169 }
170 else if (overlay_flag == 3)
171 {
172 threshold = 0.4 + 0.3 * (1.0 -noise_50m);
173 }
174
175 centerness = smoothstep(threshold, 1.0, centerness);
176
177 float sine_factor1 = smoothstep(0.6, 1.0, abs(sin(1.0 * noise_50m + 2.0 * gl_TexCoord[0].s + gl_TexCoord[0].s * 40.0)));
178 float sine_factor2 = smoothstep(0.7, 1.0, abs(sin(2.0 * noise_10m + gl_TexCoord[0].s + gl_TexCoord[0].s * 60.0)));
179 float sine_factor3 = smoothstep(0.7, 1.0, abs(sin(1.0 * noise_20m + gl_TexCoord[0].s + gl_TexCoord[0].s * 80.0)));
180
181 float sine_factor = sine_factor1 * smoothstep(0.4, 0.6, noise_10m);
182 sine_factor += sine_factor2 * (1.0 - smoothstep(0.5, 0.7, noise_10m));
183 sine_factor += sine_factor3 * smoothstep(0.5, 0.6, noise_20m);
184 sine_factor = min(sine_factor, 1.0);
185
186
187 centerness *=(0.7 + 0.3 *sine_factor);
188
189 float t_factor;
190
191 if (overlay_flag == 1)
192 {t_factor = smoothstep(0.0, 0.3, gl_TexCoord[0].t) * (0.4 + 0.2 * noise_50m);}
193 else if (overlay_flag == 2)
194 {
195 t_factor = mix (0.4 + 0.2 * noise_50m, 0.7 + 0.2 * noise_50m, mix_factor);
196 }
197 else if (overlay_flag == 3)
198 {t_factor = 0.3 + 0.2 * noise_50m;}
199
200 centerness *= t_factor;
201
202
203 centerness = 1.0 - centerness;
204 texel.rgb = texel.rgb * (0.2 + 0.8 * centerness);
205 }
206
207 float distortion_factor = 1.0;
208 vec2 stprime;
209 int flag = 1;
210 int mix_flag = 1;
211 float noise_term;
212 float snow_alpha;
213
214
215 if (quality_level > 3)
216 {
217 float sfactor;
218 noise_01m = Noise2D(rawPos.xy,0.1);
219 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_50m + 0.1* (1.0 - noise_10m) );
220 snow_texel.a = 1.0;
221 noise_term = 0.1 * (noise_50m-0.5);
222 sfactor = 1.0;//sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
223 noise_term = noise_term + 0.2 * (noise_10m -0.5) * (1.0 - smoothstep(10000.0*sfactor, 16000.0*sfactor, dist) ) ;
224 noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1200.0 * sfactor, 2000.0 * sfactor, dist) ) ;
225 noise_term = noise_term + 0.3 * (noise_1m -0.5) * (1.0 - smoothstep(500.0 * sfactor, 1000.0 *sfactor, dist) );
226 noise_term = noise_term + 0.3 * (noise_01m -0.5) * (1.0 - smoothstep(20.0 * sfactor, 100.0 *sfactor, dist) );
227 snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + 0.2*snow_thickness_factor +0.0001*(relPos.z +eye_alt -snowlevel) );
228
229 }
230
231
232 const vec3 dust_color = vec3 (0.76, 0.71, 0.56);
233 const vec3 lichen_color = vec3 (0.17, 0.20, 0.06);
234 //float snow_alpha;
235
236 if (quality_level > 3)
237 {
238
239 // mix vegetation
240 texel.rgb = mix(texel.rgb, lichen_color, 0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)) );
241 // mix dust
242 texel.rgb = mix(texel.rgb, dust_color, clamp(0.5 * dust_cover_factor + 3.0 * dust_cover_factor * (((noise_1500m - 0.5) * 0.125)+0.125 ),0.0, 1.0) );
243
244 // mix snow
245 if (relPos.z + eye_alt +500.0 > snowlevel)
246 {
247 snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
248 texel.rgb = mix(texel.rgb, snow_texel.rgb, snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, snow_alpha * (relPos.z + eye_alt)+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0));
249 }
250 }
251 else if (relPos.z + eye_alt +500.0 > snowlevel)
252 {
253 float snow_alpha = 0.1+0.4* smoothstep(0.2,0.8, 0.3 + snow_thickness_factor +0.0001*(relPos.z +eye_alt -snowlevel) );
254 // texel = vec4(dot(vec3(0.2989, 0.5870, 0.1140), texel.rgb));
255 texel = mix(texel, vec4(1.0), snow_alpha* smoothstep(snowlevel, snowlevel+200.0, (relPos.z + eye_alt)));
256 }
257
258
259
260 // get distribution of water when terrain is wet
261
262 float water_threshold1;
263 float water_threshold2;
264 float water_factor =0.0;
265
266
267 if ((dist < 5000.0)&& (quality_level > 3) && (wetness>0.0))
268 {
269 water_threshold1 = 1.0-0.5* wetness;
270 water_threshold2 = 1.0 - 0.3 * wetness;
271 water_factor = smoothstep(water_threshold1, water_threshold2 , 0.5 * (noise_5m + (1.0 -noise_1m))) * (1.0 - smoothstep(1000.0, 3000.0, dist));
272 }
273
274 // darken wet terrain
275
276 texel.rgb = texel.rgb * (1.0 - 0.6 * wetness - 0.1 * water_factor);
277
278
279 // light computations
280
281 eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
282 vec4 light_specular = gl_LightSource[0].specular * eShade;
283
284 // If gl_Color.a == 0, this is a back-facing polygon and the
285 // normal should be reversed.
286 n = normal;
287 n = normalize(n);
288
289 // primary reflection of the Sun
290 float fresnel;
291 NdotL = dot(n, lightDir);
292
293 if (quality_level > 4)
294 {
295 NdotL = NdotL + (3.0 * N.r + 0.1 * (noise_01m-0.5))* (1.0 - water_factor) ;
296 }
297 if (NdotL > 0.0)
298 {
299 float shadowmap = getShadowing();
300 if (cloud_shadow_flag == 1)
301 {NdotL = NdotL * shadow_func(relPos.x, relPos.y, 1.0, dist);}
302 color += diffuse_term * NdotL * shadowmap;
303 NdotHV = max(dot(n, halfVector), 0.0);
304 fresnel = 1.0 + 5.0 * (1.0-smoothstep(0.0,0.2, dot(E,n)));
305 specular.rgb = ((vec3 (0.2,0.2,0.2) * fresnel + (water_factor * vec3 (1.0, 1.0, 1.0)))
306 * light_specular.rgb
307 * pow(NdotHV, max(4.0, (20.0 * water_factor)))
308 * shadowmap);
309 }
310
311 // raindrops
312 float rain_factor = 0.0;
313 if (rain_norm > 0.0)
314 {
315 rain_factor += DotNoise2D(rawPos.xy, 0.2 ,0.5, rain_norm) * abs(sin(6.0*osg_SimulationTime));
316 rain_factor += DotNoise2D(rawPos.xy, 0.3 ,0.4, rain_norm) * abs(sin(6.0*osg_SimulationTime + 2.094));
317 rain_factor += DotNoise2D(rawPos.xy, 0.4 ,0.3, rain_norm)* abs(sin(6.0*osg_SimulationTime + 4.188));
318 }
319
320 // secondary reflection of sky irradiance
321 float fresnelW = ((0.8 * wetness) + (0.2* water_factor)) * (1.0-smoothstep(0.0,0.4, dot(E,n) * 1.0 - 0.2 * rain_factor * wetness));
322 float sky_factor = (1.0-ct*ct);//mix((1.0 - ct * ct), 1.0-effective_scattering, effective_scattering);
323 vec3 sky_light = vec3 (1.0,1.0,1.0) * length(light_specular.rgb) * (1.0-effective_scattering);
324 specular.rgb += sky_factor * fresnelW * sky_light;
325
326
327
328 //specular.rgb *= 1.0 - 0.2 * dotnoise_02m * wetness;
329
330 color.a = 1.0;//diffuse_term.a;
331 // This shouldn't be necessary, but our lighting becomes very
332 // saturated. Clamping the color before modulating by the texture
333 // is closer to what the OpenGL fixed function pipeline does.
334 color = clamp(color, 0.0, 1.0);
335
336 vec3 secondary_light = vec3 (0.0,0.0,0.0);
337
338 if (use_searchlight == 1)
339 {
340 secondary_light.rgb += searchlight();
341 }
342 if (use_landing_light == 1)
343 {
344 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
345 }
346 if (use_alt_landing_light == 1)
347 {
348 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
349 }
350
351 color.rgb +=secondary_light * light_distance_fading(dist) + lightspot(relPos);
352
353 fragColor = color * texel + specular;
354 fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb + (2.0 * N.r + 0.1 * (noise_01m-0.5))* (1.0 - water_factor)) ;
355
356 float lightArg = (terminator-yprime_alt)/100000.0;
357
358 vec3 hazeColor = get_hazeColor(lightArg);
359
360
361 // Rayleigh color shifts
362
363 if ((quality_level > 5) && (tquality_level > 5))
364 {
365 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
366 float outscatter = 1.0-exp(-dist/rayleigh_length);
367 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
368 // Rayleigh color shift due to in-scattering
369 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
370 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
371 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
372 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
373 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
374 }
375
376
377 // here comes the terrain haze model
378
379 float delta_z = hazeLayerAltitude - eye_alt;
380 float mvisibility = min(visibility, avisibility);
381
382 if (dist > 0.04 * mvisibility)
383 {
384
385 alt = eye_alt;
386
387
388 float transmission;
389 float vAltitude;
390 float delta_zv;
391 float H;
392 float distance_in_layer;
393 float transmission_arg;
394
395
396
397
398 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
399
400 if (delta_z > 0.0) // we're inside the layer
401 {
402 if (ct < 0.0) // we look down
403 {
404 distance_in_layer = dist;
405 vAltitude = min(distance_in_layer,mvisibility) * ct;
406 delta_zv = delta_z - vAltitude;
407 }
408 else // we may look through upper layer edge
409 {
410 H = dist * ct;
411 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
412 else {distance_in_layer = dist;}
413 vAltitude = min(distance_in_layer,visibility) * ct;
414 delta_zv = delta_z - vAltitude;
415 }
416 }
417 else // we see the layer from above, delta_z < 0.0
418 {
419 H = dist * -ct;
420 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
421 {
422 distance_in_layer = 0.0;
423 delta_zv = 0.0;
424 }
425 else
426 {
427 vAltitude = H + delta_z;
428 distance_in_layer = vAltitude/H * dist;
429 vAltitude = min(distance_in_layer,visibility) * (-ct);
430 delta_zv = vAltitude;
431 }
432 }
433
434
435 // ground haze cannot be thinner than aloft visibility in the model,
436 // so we need to use aloft visibility otherwise
437
438
439 transmission_arg = (dist-distance_in_layer)/avisibility;
440
441
442 float eqColorFactor;
443
444
445
446 if (visibility < avisibility)
447 {
448 if (quality_level > 3)
449 {
450 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
451
452 }
453 else
454 {
455 transmission_arg = transmission_arg + (distance_in_layer/visibility);
456 }
457 // this combines the Weber-Fechner intensity
458 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0- effective_scattering);
459
460 }
461 else
462 {
463 if (quality_level > 3)
464 {
465 transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m - 1.0) ));
466 }
467 else
468 {
469 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
470 }
471 // this combines the Weber-Fechner intensity
472 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 - effective_scattering);
473 }
474
475
476
477 transmission = fog_func(transmission_arg, alt);
478
479 // there's always residual intensity, we should never be driven to zero
480 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
481
482
483 // Mie-like factor
484
485 if (lightArg < 10.0)
486 {
487 intensity = length(hazeColor);
488 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
489 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)) );
490 }
491
492 intensity = length(hazeColor);
493
494 if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
495 {
496
497
498 // high altitude desaturation of the haze color
499 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
500
501 // blue hue of haze
502 hazeColor.x = hazeColor.x * 0.83;
503 hazeColor.y = hazeColor.y * 0.9;
504
505
506 // additional blue in indirect light
507 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
508 intensity = length(hazeColor);
509 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
510
511 // change haze color to blue hue for strong fogging
512 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
513
514
515
516 // reduce haze intensity when looking at shaded surfaces, only in terminator region
517 float shadow = mix( min(1.0 + dot(n,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
518 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
519 }
520 //hazeColor = clamp(hazeColor, 0.0, 1.0);
521
522 // don't let the light fade out too rapidly
523 lightArg = (terminator + 200000.0)/100000.0;
524 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
525 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
526
527 hazeColor.rgb *= eqColorFactor * eShade;
528 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
529
530 fragColor.rgb = mix(hazeColor +secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
531
532 }
533
534 fragColor.rgb = filter_combined(fragColor.rgb);
535
536 gl_FragColor = fragColor;
537
538
539 }
540