Debian Codegraph @

Shaders/tree-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // written by Thorsten Renk, Oct 2011, based on default.frag
5
6
7
8 varying vec3 relPos;
9
10
11 uniform sampler2D texture;
12
13
14 varying float yprime_alt;
15 varying float autumn_flag;
16
17 uniform float visibility;
18 uniform float avisibility;
19 uniform float scattering;
20 uniform float ground_scattering;
21 uniform float cloud_self_shading;
22 uniform float terminator;
23 uniform float terrain_alt;
24 uniform float hazeLayerAltitude;
25 uniform float overcast;
26 uniform float eye_alt;
27 uniform float dust_cover_factor;
28 uniform float air_pollution;
29 uniform float landing_light1_offset;
30 uniform float landing_light2_offset;
31 uniform float landing_light3_offset;
32 uniform float cseason;
33
34 uniform int use_searchlight;
35 uniform int use_landing_light;
36 uniform int use_alt_landing_light;
37 uniform int quality_level;
38 uniform int tquality_level;
39
40
41 const float EarthRadius = 5800000.0;
42 const float terminator_width = 200000.0;
43
44 float alt;
45 float mie_angle;
46
47
48 float light_distance_fading(in float dist);
49 float fog_backscatter(in float avisibility);
50 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
51
52 vec3 searchlight();
53 vec3 landing_light(in float offset, in float offsetv);
54 vec3 get_hazeColor(in float light_arg);
55 vec3 filter_combined (in vec3 color) ;
56 float Noise2D(in vec2 coord, in float wavelength);
57
58
59
60 float tree_fog_func (in float targ)
61 {
62
63
64 float fade_mix;
65
66 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
67 // create the illusion of improved visibility range
68
69 targ = 1.25 * targ * smoothstep(0.07,0.1,targ); // need to sync with the distance to which terrain is drawn
70
71
72 if (alt < 30000.0)
73 {return exp(-targ - targ * targ * targ * targ);}
74 else if (alt < 50000.0)
75 {
76 fade_mix = (alt - 30000.0)/20000.0;
77 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
78 }
79 else
80 {
81 return exp(- targ * targ - pow(targ,4.0));
82 }
83
84 }
85
86 void main()
87 {
88
89
90 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
91
92
93
94 vec3 lightDir = gl_LightSource[0].position.xyz;
95 float intensity;
96
97 mie_angle = gl_Color.a;
98 vec4 texel = texture2D(texture, gl_TexCoord[0].st);
99
100 float effective_scattering = min(scattering, cloud_self_shading);
101 float dist = length(relPos);
102
103 if (quality_level > 3)
104 {
105
106 // seasonal color changes
107
108 if ((cseason < 1.5)&& (autumn_flag > 0.0))
109 {
110 texel.r = min(1.0, (1.0 + 5.0 *cseason * autumn_flag ) * texel.r);
111 //texel.g = texel.g + 0.05 * (autumn_flag-0.5) * cseason;
112 texel.b = max(0.0, (1.0 - 8.0 * cseason) * texel.b);
113 }
114
115 // mix dust
116 vec4 dust_color = vec4 (0.76, 0.71, 0.56, texel.a);
117
118 texel = mix(texel, dust_color, clamp(0.6 * dust_cover_factor ,0.0, 1.0) );
119
120
121 }
122
123
124 // ALS secondary light sources
125
126 vec3 secondary_light = vec3 (0.0,0.0,0.0);
127
128 if ((quality_level>5) && (tquality_level>5))
129 {
130 if (use_searchlight == 1)
131 {
132 secondary_light += searchlight();
133 }
134 if (use_landing_light == 1)
135 {
136 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
137 }
138 if (use_alt_landing_light == 1)
139 {
140 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
141 }
142 }
143
144 vec4 fragColor = vec4 (gl_Color.rgb +secondary_light * light_distance_fading(dist),1.0) * texel;
145
146
147
148
149 float lightArg = (terminator-yprime_alt)/100000.0;
150
151 vec3 hazeColor = get_hazeColor(lightArg);
152
153
154
155 // Rayleigh color shift due to in-scattering
156
157 if ((quality_level > 5) && (tquality_level > 5))
158 {
159 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
160 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
161 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
162 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
163 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
164 }
165
166
167
168 // here comes the terrain haze model
169
170
171 float delta_z = hazeLayerAltitude - eye_alt;
172 float mvisibility = min(visibility,avisibility);
173
174 if (dist > max(40.0, 0.07 * mvisibility))
175 {
176
177 alt = eye_alt;
178
179
180 float transmission;
181 float vAltitude;
182 float delta_zv;
183 float H;
184 float distance_in_layer;
185 float transmission_arg;
186
187 // angle with horizon
188 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
189
190
191 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
192
193 if (delta_z > 0.0) // we're inside the layer
194 {
195 if (ct < 0.0) // we look down
196 {
197 distance_in_layer = dist;
198 vAltitude = min(distance_in_layer,mvisibility) * ct;
199 delta_zv = delta_z - vAltitude;
200 }
201 else // we may look through upper layer edge
202 {
203 H = dist * ct;
204 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
205 else {distance_in_layer = dist;}
206 vAltitude = min(distance_in_layer,visibility) * ct;
207 delta_zv = delta_z - vAltitude;
208 }
209 }
210 else // we see the layer from above, delta_z < 0.0
211 {
212 H = dist * -ct;
213 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
214 {
215 distance_in_layer = 0.0;
216 delta_zv = 0.0;
217 }
218 else
219 {
220 vAltitude = H + delta_z;
221 distance_in_layer = vAltitude/H * dist;
222 vAltitude = min(distance_in_layer,visibility) * (-ct);
223 delta_zv = vAltitude;
224 }
225 }
226
227
228 // blur of the haze layer edge
229
230 float blur_thickness = 50.0;
231 float cphi = dot(vec3(0.0, 1.0, 0.0), relPos)/dist;
232 float ctlayer;
233 float ctblur = 0.035 ;
234
235 float blur_dist;
236
237 if ((abs(delta_z) < 400.0)&&(quality_level>5)&&(tquality_level>5))
238 {
239 ctlayer = delta_z/dist-0.01 + 0.02 * Noise2D(vec2(cphi,1.0),0.1) -0.01;
240 blur_dist = dist * (1.0-smoothstep(0.0,300.0,-delta_z)) * smoothstep(-400.0,-200.0, -delta_z);
241 blur_dist = blur_dist * smoothstep(ctlayer-4.0*ctblur, ctlayer-ctblur, ct) * (1.0-smoothstep(ctlayer+0.5*ctblur, ctlayer+ctblur, ct));
242 distance_in_layer = max(distance_in_layer, blur_dist);
243 }
244
245 // ground haze cannot be thinner than aloft visibility in the model,
246 // so we need to use aloft visibility otherwise
247
248
249 transmission_arg = (dist-distance_in_layer)/avisibility;
250
251
252 float eqColorFactor;
253
254 //float scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, relPos.z + eye_alt);
255
256 if (visibility < avisibility)
257 {
258 transmission_arg = transmission_arg + (distance_in_layer/visibility);
259 // this combines the Weber-Fechner intensity
260 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
261
262 }
263 else
264 {
265 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
266 // this combines the Weber-Fechner intensity
267 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
268 }
269
270
271
272 transmission = tree_fog_func(transmission_arg);
273
274 // there's always residual intensity, we should never be driven to zero
275 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
276
277
278
279
280
281 // now dim the light for haze
282 float eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
283
284 // Mie-like factor
285
286 if (lightArg < 10.0)
287 {intensity = length(hazeColor);
288 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
289 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)) );
290 }
291
292 // high altitude desaturation of the haze color
293
294 intensity = length(hazeColor);
295 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
296
297 // blue hue of haze
298
299 hazeColor.x = hazeColor.x * 0.83;
300 hazeColor.y = hazeColor.y * 0.9;
301
302
303 // additional blue in indirect light
304 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
305 intensity = length(hazeColor);
306 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
307
308 // change haze color to blue hue for strong fogging
309
310 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
311
312
313 // don't let the light fade out too rapidly
314 lightArg = (terminator + 200000.0)/100000.0;
315 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
316 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
317
318 hazeColor.rgb *= eqColorFactor * eShade;
319 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
320
321 // determine the right mix of transmission and haze
322
323 //hazeColor = clamp(hazeColor,0.0,1.0);
324 fragColor.rgb = mix( hazeColor + secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
325
326 }
327
328
329 fragColor.rgb = filter_combined(fragColor.rgb);
330
331 gl_FragColor = fragColor;
332 }
333