Debian Codegraph @

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