Debian Codegraph @

Shaders/skydome-ALS.frag

raw
1 // -*-C++-*-
2
3 #version 120
4
5 // Atmospheric scattering shader for flightgear
6 // Written by Lauri Peltonen (Zan)
7 // Implementation of O'Neil's algorithm
8 // Ground haze layer added by Thorsten Renk
9 // aurora and ice haze scattering Thorsten Renk 2016
10
11 varying vec3 rayleigh;
12 varying vec3 mie;
13 varying vec3 eye;
14 varying vec3 hazeColor;
15 varying vec3 viewVector;
16 varying float ct;
17 varying float cphi;
18 varying float delta_z;
19 varying float alt;
20 varying float earthShade;
21
22
23 uniform float overcast;
24 uniform float saturation;
25 uniform float visibility;
26 uniform float avisibility;
27 uniform float scattering;
28 uniform float terminator;
29 uniform float cloud_self_shading;
30 uniform float horizon_roughness;
31 uniform float ice_hex_col;
32 uniform float ice_hex_sheet;
33 uniform float parhelic;
34 uniform float ring;
35 uniform float aurora_strength;
36 uniform float aurora_hsize;
37 uniform float aurora_vsize;
38 uniform float aurora_ray_factor;
39 uniform float aurora_penetration_factor;
40 uniform float landing_light1_offset;
41 uniform float landing_light2_offset;
42 uniform float landing_light3_offset;
43 uniform float osg_SimulationTime;
44
45 uniform int use_searchlight;
46 uniform int use_landing_light;
47 uniform int use_alt_landing_light;
48
49 const float EarthRadius = 5800000.0;
50
51
52 float Noise2D(in vec2 coord, in float wavelength);
53 float fog_backscatter(in float avisibility);
54
55 vec3 searchlight();
56 vec3 landing_light(in float offset, in float offsetv);
57 vec3 filter_combined (in vec3 color) ;
58
59 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
60 {
61 x = x - 0.5;
62
63 // use the asymptotics to shorten computations
64 if (x > 30.0) {return e;}
65 if (x < -15.0) {return 0.0;}
66
67 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
68 }
69
70 float miePhase(in float cosTheta, in float g)
71 {
72 float g2 = g*g;
73 float a = 1.5 * (1.0 - g2);
74 float b = (2.0 + g2);
75 float c = 1.0 + cosTheta*cosTheta;
76 float d = pow(1.0 + g2 - 2.0 * g * cosTheta, 0.6667);
77
78 return (a*c) / (b*d);
79 }
80
81 float rayleighPhase(in float cosTheta)
82 {
83 //return 1.5 * (1.0 + cosTheta*cosTheta);
84 return 1.5 * (2.0 + 0.5*cosTheta*cosTheta);
85 }
86
87
88 void main()
89 {
90
91
92 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
93 float cosTheta = dot(normalize(eye), gl_LightSource[0].position.xyz);
94
95 // some geometry
96
97 vec3 nView = normalize(viewVector);
98 vec3 lightFull = normalize((gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz);
99 float calpha = dot(lightFull, nView);
100 float cbeta = dot ( normalize(lightFull.xy), normalize(nView.xy));
101 float costheta = ct;
102
103 // some noise
104
105 float hNoise_03 = Noise2D(vec2(0.0,cphi), 0.3);
106 float hNoiseAurora = Noise2D(vec2(0.001 * osg_SimulationTime,cphi + nView.x + nView.y), 0.07) * 0.2 * aurora_ray_factor;
107
108 // position of the horizon line
109
110 float lAltitude = alt + delta_z;
111 float radiusEye = (EarthRadius + alt);
112 float radiusLayer = (EarthRadius + lAltitude);
113 float cthorizon;
114 float ctterrain;
115 //float ctsd;
116
117 float SkydomeRadius = (EarthRadius + 100000.0);
118 float rEye = (EarthRadius + alt);
119
120 if (radiusEye > radiusLayer) cthorizon = -sqrt(radiusEye * radiusEye - radiusLayer * radiusLayer)/radiusEye;
121 else cthorizon = sqrt(radiusLayer * radiusLayer - radiusEye * radiusEye)/radiusLayer;
122
123 //if (rEye > SkydomeRadius) ctsd = -sqrt(rEye * rEye - SkydomeRadius * SkydomeRadius)/rEye;
124 //else ctsd = 0.0;//sqrt(SkydomeRadius * SkydomeRadius - rEye * rEye)/SkydomeRadius;
125
126 ctterrain = -sqrt(radiusEye * radiusEye - EarthRadius * EarthRadius)/radiusEye;
127
128 vec3 color = rayleigh * rayleighPhase(cosTheta);
129 color += mie * miePhase(cosTheta, -0.8);
130
131 vec3 black = vec3(0.0,0.0,0.0);
132
133
134 float ovc = overcast;
135
136
137 float sat = 1.0 - ((1.0 - saturation) * 2.0);
138 if (sat < 0.3) sat = 0.3;
139
140 if (color.r > 0.58) color.r = 1.0 - exp(-1.5 * color.r);
141 if (color.g > 0.58) color.g = 1.0 - exp(-1.5 * color.g);
142 if (color.b > 0.58) color.b = 1.0 - exp(-1.5 * color.b);
143
144
145
146 // Aurora Borealis / Australis
147
148 vec3 direction = vec3 (1.0, 0.0, 0.0);
149
150 float hArg = dot(nView, direction);
151
152 float aurora_vEdge = 0.2 - 0.6 * aurora_vsize * (1.0 - 0.8* aurora_ray_factor);
153 float aurora_vArg = costheta + hNoiseAurora;
154 float aurora_v = smoothstep(aurora_vEdge , 0.2 , costheta + hNoiseAurora) * (1.0- smoothstep(0.3, 0.3 + aurora_vsize, aurora_vArg));
155 aurora_v *= (1.0 + 5.0 * aurora_ray_factor * (1.0 -smoothstep(aurora_vEdge, 0.3, aurora_vArg)));
156
157 float aurora_h = smoothstep(1.0 - aurora_hsize, 1.0, hArg);
158 float aurora_time = 0.01 * osg_SimulationTime;
159
160 vec3 auroraBaseColor = vec3 (0.0, 0.2, 0.1);
161 vec3 auroraFringeColor = vec3 (0.4, 0.15, 0.2);
162
163 float fringe_factor = 1.0 - smoothstep(aurora_vEdge, aurora_vEdge + 0.08, aurora_vArg);
164 fringe_factor *= aurora_strength * aurora_penetration_factor;
165 auroraBaseColor = mix(auroraBaseColor, auroraFringeColor, fringe_factor );
166
167 float aurora_ray = mix(1.0, Noise2D(vec2(cbeta, 0.01 * aurora_time), 0.001), aurora_ray_factor);
168
169 float aurora_visible_strength = 0.3 + 0.7 * Noise2D(vec2(costheta + aurora_time, 0.5 * nView.x + 0.3 * nView.y + aurora_time), 0.1) ;
170 aurora_visible_strength *= aurora_ray;
171 float aurora_fade_in = 1.0 - smoothstep(0.1, 0.2, length(color.rgb));
172
173
174
175 color.rgb += auroraBaseColor * aurora_v * aurora_h * aurora_fade_in * aurora_visible_strength * aurora_strength;
176
177 // fog computations for a ground haze layer, extending from zero to lAltitude
178
179 float transmission;
180 float vAltitude;
181 float delta_zv;
182
183
184
185 float vis = min(visibility, avisibility);
186
187
188 if (delta_z > 0.0) // we're inside the layer
189 {
190 if (costheta>0.0 + ctterrain) // looking up, view ray intersecting upper layer edge
191 {
192 transmission = exp(-min((delta_z/max(costheta,0.1)),25000.0)/vis);
193 //transmission = 1.0;
194 vAltitude = min(vis * costheta, delta_z);
195 delta_zv = delta_z - vAltitude;
196 }
197
198 else // looking down, view range intersecting terrain (which may not be drawn)
199 {
200 transmission = exp(alt/vis/costheta);
201 vAltitude = min(-vis * costheta, alt);
202 delta_zv = delta_z + vAltitude;
203 }
204 }
205 else // we see the layer from above
206 {
207 if (costheta < 0.0 + cthorizon)
208 {
209 transmission = exp(-min(lAltitude/abs(costheta),25000.0)/vis);
210 transmission = transmission * exp(-alt/avisibility/abs(costheta));
211 transmission = 1.0 - (1.0 - transmission) * smoothstep(0+cthorizon, -0.02+cthorizon, costheta);
212 vAltitude = min(lAltitude, -vis * costheta);
213 delta_zv = vAltitude;
214 }
215 else
216 {
217 transmission = 1.0;
218 delta_zv = 0.0;
219 }
220 }
221
222 // combined intensity reduction by cloud shading and fog self-shading, corrected for Weber-Fechner perception law
223 float eqColorFactor = 1.0 - 0.1 * delta_zv/vis - (1.0 - min(scattering,cloud_self_shading));
224
225
226 // there's always residual intensity, we should never be driven to zero
227 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
228
229
230 // postprocessing of haze color
231 vec3 hColor = hazeColor;
232
233
234 // high altitude desaturation
235 float intensity = length(hColor);
236 hColor = intensity * normalize (mix(hColor, intensity * vec3 (1.0,1.0,1.0), 0.7 * smoothstep(5000.0, 50000.0, alt)));
237
238 hColor = clamp(hColor,0.0,1.0);
239
240 // blue hue
241 hColor.x = 0.83 * hColor.x;
242 hColor.y = 0.9 * hColor.y;
243
244
245
246 // further blueshift when in shadow, either cloud shadow, or self-shadow or Earth shadow, dependent on indirect
247 // light
248
249 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
250 intensity = length(hColor);
251 vec3 oColor = hColor;
252 oColor = intensity * normalize(mix(oColor, shadedFogColor, (smoothstep(0.1,1.0,ovc))));
253
254 // ice crystal halo
255
256 float sun_altitude = dot (lightFull, vec3 (0.0, 0.0, 1.0));
257 float view_altitude = dot(nView, vec3 (0.0, 0.0, 1.0));
258
259 //float halo_ring_enhancement = smoothstep (0.88, 0.927, calpha) * (1.0 - smoothstep(0.927, 0.98, calpha));
260 float halo_ring_enhancement = smoothstep (0.88, 0.927, calpha) * (1.0 - smoothstep(0.927, 0.94, calpha));
261 halo_ring_enhancement *= halo_ring_enhancement;
262 halo_ring_enhancement *= ring;
263
264 // parhelic circle
265
266 float parhelic_circle_enhancement = 0.3 * smoothstep (sun_altitude-0.01, sun_altitude, view_altitude) * (1.0 - smoothstep(sun_altitude, sun_altitude+ 0.01, view_altitude));
267
268 parhelic_circle_enhancement += 0.8 * smoothstep (sun_altitude-0.08, sun_altitude, view_altitude) * (1.0 - smoothstep(sun_altitude, sun_altitude+ 0.08, view_altitude));
269
270 parhelic_circle_enhancement *= parhelic * (0.2 + 0.8 * smoothstep(0.5, 1.0, cbeta));
271
272 // sundogs
273
274 float side_sun_enhancement = smoothstep (sun_altitude-0.03, sun_altitude, view_altitude) * (1.0 - smoothstep(sun_altitude, sun_altitude+ 0.03, view_altitude));
275
276 side_sun_enhancement *= halo_ring_enhancement * halo_ring_enhancement * ice_hex_sheet;
277
278 // pillar
279
280 float pillar_enhancement = smoothstep (sun_altitude-0.1, sun_altitude, view_altitude) * (1.0 - smoothstep(sun_altitude, sun_altitude+ 0.22, view_altitude));
281 float beta_thickness = 0.6 * smoothstep(0.999, 1.0, cbeta);
282 beta_thickness += 0.8 * smoothstep(0.99998, 1.0, cbeta);
283 pillar_enhancement *= beta_thickness * beta_thickness * smoothstep(0.99, 1.0, calpha) * ice_hex_col;
284
285
286
287 float scattering_enhancements = 0.25 * halo_ring_enhancement * ovc;
288 scattering_enhancements += side_sun_enhancement *0.4 * (1.0 - smoothstep(0.6, 0.95, transmission));
289 scattering_enhancements += pillar_enhancement *0.25 * (1.0 - smoothstep(0.7, 1.0, transmission));
290 scattering_enhancements += parhelic_circle_enhancement * 0.2 * (1.0 - smoothstep(0.7, 1.0, transmission));
291
292
293 color.rgb += vec3(1.0, 1.0, 1.0) * (5.0-4.0* earthShade) * scattering_enhancements * hazeColor;
294
295 oColor = clamp(oColor,0.0,1.0);
296 color = ovc * mix(color, oColor * earthShade ,smoothstep(-0.1+ctterrain, 0.0+ctterrain, ct)) + (1.0-ovc) * color;
297
298
299 hColor = intensity * normalize(mix(hColor, 1.5 * shadedFogColor, 1.0 -smoothstep(0.25, fade_out,earthShade) ));
300 hColor = intensity * normalize(mix(hColor, shadedFogColor, (1.0 - smoothstep(0.5,0.9,eqColorFactor))));
301 hColor = hColor * earthShade;
302
303 // accounting for overcast and saturation
304
305
306
307 color = sat * color + (1.0 - sat) * mix(color, black, smoothstep(0.4+cthorizon,0.2+cthorizon,ct));
308
309
310 // the terrain below the horizon gets drawn in one optical thickness
311 vec3 terrainHazeColor = eqColorFactor * hColor;
312
313 // determine a visibility-dependent angle for how smoothly the haze blends over the skydome
314
315 float hazeBlendAngle = max(0.01,1000.0/avisibility + 0.3 * (1.0 - smoothstep(5000.0, 30000.0, avisibility)));
316 float altFactor = smoothstep(-300.0, 0.0, delta_z);
317 float altFactor2 = 0.2 + 0.8 * smoothstep(-3000.0, 0.0, delta_z);
318 hazeBlendAngle = hazeBlendAngle + 0.1 * altFactor;
319 hazeBlendAngle = hazeBlendAngle + (1.0-horizon_roughness) * altFactor2 * 0.1 * hNoise_03;
320
321 terrainHazeColor = clamp(terrainHazeColor,0.0,1.0);
322
323
324 // don't let the light fade out too rapidly
325 float lightArg = (terminator + 200000.0)/100000.0;
326 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
327 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
328
329 // this is for the bare Rayleigh and Mie sky, highly altitude dependent
330 color.rgb = max(color.rgb, minLight.rgb * (1.0- min(alt/100000.0,1.0)) * (1.0 - costheta));
331
332 // this is for the terrain drawn
333 terrainHazeColor = max(terrainHazeColor.rgb, minLight.rgb);
334
335 color = mix(color, terrainHazeColor ,smoothstep(hazeBlendAngle + ctterrain, 0.0+ctterrain, ct));
336
337
338 // add the brightening of fog by lights
339
340 vec3 secondary_light = vec3 (0.0,0.0,0.0);
341
342 if (use_searchlight == 1)
343 {
344 secondary_light.rgb += searchlight();
345 }
346 if (use_landing_light == 1)
347 {
348 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
349 }
350 if (use_alt_landing_light == 1)
351 {
352 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
353 }
354
355
356
357
358 // mix fog the skydome with the right amount of haze
359
360 hColor *= eqColorFactor;
361 hColor = max(hColor.rgb, minLight.rgb);
362
363 hColor = clamp(hColor,0.0,1.0);
364
365 color = mix(hColor+secondary_light * fog_backscatter(avisibility),color, transmission);
366
367 // blur the upper skydome edge when we're outside the atmosphere
368
369 float asf = smoothstep (75000.0, 90000.0, alt);
370
371 float asf_corr = clamp((alt-115000.0)/45000.0, 0.0,1.0) * 0.08;
372
373
374 color *= (1.0 - smoothstep( -0.12 -asf_corr, -0.06 - asf_corr, costheta) * asf);
375 color = filter_combined(color);
376
377
378
379
380 gl_FragColor = vec4(color, 1.0);
381 gl_FragDepth = 0.1;
382
383
384 }
385