Debian Codegraph @

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