Debian Codegraph @

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