Debian Codegraph @

Shaders/model-ALS-base.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 vec4 ecPosition;
10
11 uniform sampler2D texture;
12
13
14 varying float yprime_alt;
15 varying float mie_angle;
16
17
18 uniform float visibility;
19 uniform float avisibility;
20 uniform float scattering;
21 uniform float terminator;
22 uniform float terrain_alt;
23 uniform float hazeLayerAltitude;
24 uniform float overcast;
25 uniform float eye_alt;
26 uniform float cloud_self_shading;
27 uniform float air_pollution;
28 uniform float landing_light1_offset;
29 uniform float landing_light2_offset;
30 uniform float landing_light3_offset;
31
32 uniform int quality_level;
33 uniform int tquality_level;
34 uniform int use_searchlight;
35 uniform int use_landing_light;
36 uniform int use_alt_landing_light;
37
38
39 const float EarthRadius = 5800000.0;
40 const float terminator_width = 200000.0;
41
42 float alt;
43 float eShade;
44
45
46 float fog_func (in float targ, in float alt);
47 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
48 float alt_factor(in float eye_alt, in float vertex_alt);
49 float light_distance_fading(in float dist);
50 float fog_backscatter(in float avisibility);
51
52
53 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
54 vec3 get_hazeColor(in float light_arg);
55 vec3 searchlight();
56 vec3 landing_light(in float offset, in float offsetv);
57 vec3 filter_combined (in vec3 color) ;
58
59 float getShadowing();
60 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
61
62 float luminance(vec3 color)
63 {
64 return dot(vec3(0.212671, 0.715160, 0.072169), color);
65 }
66
67
68 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
69 {
70 x = x - 0.5;
71
72 // use the asymptotics to shorten computations
73 if (x > 30.0) {return e;}
74 if (x < -15.0) {return 0.0;}
75
76 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
77 }
78
79 // this determines how light is attenuated in the distance
80 // physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
81 // for distance > visibility
82
83
84
85
86 void main()
87 {
88
89 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
90 // this is taken from default.frag
91 vec3 n;
92 float NdotL, NdotHV, fogFactor;
93 vec4 color = gl_Color;
94 vec3 lightDir = gl_LightSource[0].position.xyz;
95 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
96 vec4 texel;
97 vec4 fragColor;
98 vec4 specular = vec4(0.0);
99 float intensity;
100
101 float effective_scattering = min(scattering, cloud_self_shading);
102
103 eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
104 vec4 light_specular = gl_LightSource[0].specular * (eShade - 0.1);
105
106 // If gl_Color.a == 0, this is a back-facing polygon and the
107 // normal should be reversed.
108 n = (2.0 * gl_Color.a - 1.0) * normal;
109 n = normalize(n);
110
111 NdotL = dot(n, lightDir);
112 if (NdotL > 0.0) {
113 float shadowmap = getShadowing();
114 color += diffuse_term * NdotL * shadowmap;
115 NdotHV = max(dot(n, halfVector), 0.0);
116 if (gl_FrontMaterial.shininess > 0.0)
117 specular.rgb = (gl_FrontMaterial.specular.rgb
118 * light_specular.rgb
119 * pow(NdotHV, gl_FrontMaterial.shininess)
120 * shadowmap);
121 }
122 color.a = diffuse_term.a;
123 // This shouldn't be necessary, but our lighting becomes very
124 // saturated. Clamping the color before modulating by the texture
125 // is closer to what the OpenGL fixed function pipeline does.
126 color = clamp(color, 0.0, 1.0);
127
128 float dist = length(relPos);
129 vec3 secondary_light = vec3 (0.0,0.0,0.0);
130
131 if ((quality_level > 5) && (tquality_level > 5))
132 {
133 if (use_searchlight == 1)
134 {
135 secondary_light += searchlight();
136 }
137 if (use_landing_light == 1)
138 {
139 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
140 }
141 if (use_alt_landing_light == 1)
142 {
143 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
144 }
145 if (dist > 2.0) // we don't want to light the cockpit...
146 {color.rgb +=secondary_light * light_distance_fading(dist);}
147 }
148
149 texel = texture2D(texture, gl_TexCoord[0].st);
150 fragColor = color * texel + specular;
151 fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
152
153
154 float lightArg = (terminator-yprime_alt)/100000.0;
155
156 vec3 hazeColor = get_hazeColor(lightArg);
157
158
159 // Rayleigh color shift due to in-scattering
160
161 if ((quality_level > 5) && (tquality_level > 5))
162 {
163 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
164 float outscatter = 1.0-exp(-dist/rayleigh_length);
165 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
166 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
167 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
168 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
169 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
170 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
171 }
172
173
174 // here comes the terrain haze model
175
176
177 float delta_z = hazeLayerAltitude - eye_alt;
178 float mvisibility = min(visibility, avisibility);
179
180 if (dist > 0.04 * mvisibility)
181 {
182
183 alt = eye_alt;
184
185
186 float transmission;
187 float vAltitude;
188 float delta_zv;
189 float H;
190 float distance_in_layer;
191 float transmission_arg;
192
193 // angle with horizon
194 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
195
196
197 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
198
199 if (delta_z > 0.0) // we're inside the layer
200 {
201 if (ct < 0.0) // we look down
202 {
203 distance_in_layer = dist;
204 vAltitude = min(distance_in_layer,mvisibility) * ct;
205 delta_zv = delta_z - vAltitude;
206 }
207 else // we may look through upper layer edge
208 {
209 H = dist * ct;
210 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
211 else {distance_in_layer = dist;}
212 vAltitude = min(distance_in_layer,visibility) * ct;
213 delta_zv = delta_z - vAltitude;
214 }
215 }
216 else // we see the layer from above, delta_z < 0.0
217 {
218 H = dist * -ct;
219 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
220 {
221 distance_in_layer = 0.0;
222 delta_zv = 0.0;
223 }
224 else
225 {
226 vAltitude = H + delta_z;
227 distance_in_layer = vAltitude/H * dist;
228 vAltitude = min(distance_in_layer,visibility) * (-ct);
229 delta_zv = vAltitude;
230 }
231 }
232
233
234 // ground haze cannot be thinner than aloft visibility in the model,
235 // so we need to use aloft visibility otherwise
236
237
238 transmission_arg = (dist-distance_in_layer)/avisibility;
239
240
241 float eqColorFactor;
242
243 //float scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, relPos.z + eye_alt);
244
245 if (visibility < avisibility)
246 {
247 transmission_arg = transmission_arg + (distance_in_layer/visibility);
248 // this combines the Weber-Fechner intensity
249 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
250
251 }
252 else
253 {
254 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
255 // this combines the Weber-Fechner intensity
256 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
257 }
258
259
260
261 transmission = fog_func(transmission_arg, alt);
262
263 // there's always residual intensity, we should never be driven to zero
264 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
265
266
267
268
269
270
271
272 // Mie-like factor
273
274 if (lightArg < 10.0)
275 {intensity = length(hazeColor);
276 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
277 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)) );
278 }
279
280 // high altitude desaturation of the haze color
281
282 intensity = length(hazeColor);
283 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
284
285 // blue hue of haze
286
287 hazeColor.x = hazeColor.x * 0.83;
288 hazeColor.y = hazeColor.y * 0.9;
289
290
291 // additional blue in indirect light
292 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
293 intensity = length(hazeColor);
294 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
295
296 // change haze color to blue hue for strong fogging
297 //intensity = length(hazeColor);
298 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
299
300
301 // reduce haze intensity when looking at shaded surfaces, only in terminator region
302
303 float shadow = mix( min(1.0 + dot(normal,lightDir),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission));
304 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
305
306 // don't let the light fade out too rapidly
307
308 lightArg = (terminator + 200000.0)/100000.0;
309 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
310 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
311 hazeColor *= eqColorFactor * eShade;
312 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
313
314 // determine the right mix of transmission and haze
315
316
317 fragColor.rgb = mix(hazeColor + secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
318
319
320
321 }
322
323 fragColor.rgb = filter_combined(fragColor.rgb);
324
325 gl_FragColor = fragColor;
326
327 }
328