Debian Codegraph @

Shaders/model_ALS_base.glsl

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