Debian Codegraph @

Shaders/galaxy.frag

raw
1 // -*-C++-*-
2 //
3 // Chris Ringeval (November 2021)
4 //
5
6 #version 120
7
8
9 varying vec3 eye2VertInEyeSpace;
10 varying vec3 eye2ZenithInEyeSpace;
11 varying vec3 eye2MoonInEyeSpace;
12
13 uniform sampler2D milkyway;
14
15 uniform float moonlight;
16 uniform float mudarksky;
17 uniform float altitude;
18 uniform float atmosphere_top;
19
20 uniform float fg_ZenithSkyBrightness;
21 uniform float mugxybulge;
22
23
24 // conversion factor to recover moon logI in lux
25 const float max_loglux = -0.504030345621;
26 const float min_loglux = -4.399646345620;
27 // conversion factor to recover moon logI in footcandle
28 const float luxtofootcandle = -1.0319696543787917;
29
30 // the log10 of Mie + Rayleight scattering function at minimum,
31 // i.e., for a Moon-Sky distance of 90 degrees
32 const float logf90 = 5.399285;
33
34 //extinction coefficient at Maunea Kea (2800m asl), in mag/airmass
35 const float k2800 = 0.172;
36
37
38 // cos(3pi/5), at asl 0m, any light source < -18 degrees above the horizon does
39 // not light-up atmosphere -> zenital angle > 108 degrees.
40 const float cosUnvisible = -0.309016994374947;
41
42
43 // D65 white multiplied by rhodopic response function and converted
44 // to linear sRGB is [-0.321, 0.656, 0.455], i.e. out of gammut. We
45 // desaturate along red to mimic night vision color blindness
46 // (see https://github.com/eatdust/spectroll)
47 const vec4 nightColor = vec4(0.0,0.977,0.776,1.0);
48
49
50
51 vec3 filter_combined (in vec3 color) ;
52
53
54 float log10(in float x){
55 return 0.434294481903252*log(x);
56 }
57
58
59 //Rayleight + Mie scattering in unit of the minimal scattering at
60 //90 degrees (const f90)
61 float scattering_angular_dependency(in float cosMoonSep) {
62
63 float fR = 0.913514*(1.06 + cosMoonSep*cosMoonSep);
64
65 float moonSepRad = acos(cosMoonSep);
66
67 float fM = 5.63268*pow(10.0,-moonSepRad*1.432394);
68
69 return fR + fM;
70
71 }
72
73 float airmass_angular_dependency(in float sineZenithDistanceSquare) {
74
75 return 1.0/sqrt(1.0 - 0.96*sineZenithDistanceSquare);
76
77 }
78
79
80 //log10 of the moon illuminance in footcandles
81 float log10_moon_illuminance_fc(in float Inorm){
82
83 return (max_loglux-min_loglux)*(Inorm-1.0) + max_loglux + luxtofootcandle ;
84 }
85
86
87 //in mag/arcsec^2 from flux in nano Lambert
88 float magnitude_from_lognL(in float logBnL){
89 return 26.3313 - 2.5*logBnL;
90 }
91
92
93 void main()
94 {
95
96
97 //unit vectors
98 vec3 uViewDir = normalize(eye2VertInEyeSpace);
99 vec3 uZenithDir = normalize(eye2ZenithInEyeSpace);
100 vec3 uMoonDir = normalize(eye2MoonInEyeSpace);
101
102
103 // the intrinsic sky brightness without the Moon at
104 // zenith set in simgear and propagated as uniform
105
106 float muzenithsky = fg_ZenithSkyBrightness;
107
108
109 vec4 fragColor;
110
111
112
113 // the galaxy is visible only if
114 if (muzenithsky >= mugxybulge) {
115
116
117
118 // texture look-up
119 vec4 texel = texture2D(milkyway, gl_TexCoord[0].st);
120
121
122
123 float cosZenithView = max(dot(uZenithDir,uViewDir),0.0);
124
125 float sineZenithDist2 = 1.0 - pow(cosZenithView,2);
126
127 float Xview = airmass_angular_dependency(sineZenithDist2);
128
129
130 float k = k2800 * max(0.0,(atmosphere_top - altitude)/(atmosphere_top - 2800.0));
131
132
133
134 // add angular dependence from scattering within the atmosphere
135
136 float musky = muzenithsky + k*(Xview-1.0);
137
138 // main effect: airglow coming from the van Rhijn layer (height 130km)
139 //
140 // https://ui.adsabs.harvard.edu/abs/1986PASP...98..364G/abstract
141 //
142 // We smoothstep airglow to zero while approaching 130km of altitude
143
144 musky = musky - 2.5*log10(0.4+0.6*Xview) * (1.0 - smoothstep(0.0,130000.0,altitude));
145
146
147
148 // Moon illumination of the atmosphere, we use the same model as in
149 // simgear (see moonpos.cxx), based on Publ. Astron. Soc. Pacif.
150 // 103(667), 1033-1039 (DOI: http://dx.doi.org/10.1086/132921).
151 //
152 // https://ui.adsabs.harvard.edu/abs/1991PASP..103.1033K/abstract
153 //
154 // The altitude damping effect is encoded in k and the moon
155 // scattering smoothly disappears with altitude as k->0. Only smoothstep added to
156 // smooth the moon rising effects
157
158 float cosZenithMoon = dot(uZenithDir,uMoonDir);
159 float dmumoon = 0.0;
160
161 // Include values under the horizon to smooth the Moon rising jumps effect
162 if (cosZenithMoon >= cosUnvisible) {
163
164 //however, we use the math only with sane input: cosZenithMoon >= 0
165 float sineZenithMoon2 = 1.0 - pow(max(cosZenithMoon,0.0),2.0);
166
167 float Xmoon = airmass_angular_dependency(sineZenithMoon2);
168
169 float cosMoonView = dot(uMoonDir,uViewDir);
170
171 float moon_logI = log10_moon_illuminance_fc(moonlight);
172
173 // log10(Bmoon) with Bmoon in nanoLambert
174 float logBnL = logf90 + log10(scattering_angular_dependency(cosMoonView))
175 + moon_logI - 0.4*k*Xmoon + log10(1.0-pow(10.0,-0.4*k*Xview));
176
177 // sky brightness from the moon in mag/arcsec^2
178 float mumoon = magnitude_from_lognL(logBnL);
179
180 //relative flux w.r.t background
181 float Brel = pow(10.0,0.4*(musky-mumoon));
182
183 // artificial smoothing for the moon between -18 degrees and 0
184 Brel = Brel*smoothstep(cosUnvisible,0.0,cosZenithMoon);
185
186 dmumoon = - 2.5*log10(1.0 + Brel);
187
188
189 }
190
191 // final angle dependent sky brightness
192 musky = musky + dmumoon;
193
194 // we put the damping in the colors as to keep alpha channel to 1
195 // for the ALS filters to not being affected
196
197 fragColor.rgb = texel.rgb * nightColor.rgb * (musky-mugxybulge)/musky;
198 fragColor.a = 1.0;
199
200 //For debugging and testing, uncomment. The red shows sky low surface brightness
201 //fragColor.r = 8*(musky-mugxybulge)/musky;
202
203 }
204
205 else {
206 // galaxy is invisible, too much sky brightness, color the night sphere is black
207
208 fragColor = vec4(0.0,0.0,0.0,1.0);
209
210 }
211
212
213
214 fragColor.rgb = filter_combined(fragColor.rgb);
215
216 gl_FragColor = clamp(fragColor,0.0,1.0);
217
218 }