| 1 |
// -*- mode: C; -*- |
| 2 |
// Licence: GPL v2 |
| 3 |
// Authors: Frederic Bouvier and Gijs de Rooy |
| 4 |
// with major additions and revisions by |
| 5 |
// Emilian Huminiuc and Vivian Meazza 2011 |
| 6 |
// ported to Atmospheric Light Scattering |
| 7 |
// by Thorsten Renk, 2013 |
| 8 |
// changes for road and traffic rendering |
| 9 |
// by Thorsten Renk 2017 -2019 |
| 10 |
#version 120 |
| 11 |
|
| 12 |
varying vec3 VBinormal; |
| 13 |
varying vec3 VNormal; |
| 14 |
varying vec3 VTangent; |
| 15 |
varying vec3 rawpos; |
| 16 |
//varying vec3 reflVec; |
| 17 |
//varying vec3 vViewVec; |
| 18 |
varying vec3 vertVec; |
| 19 |
varying vec3 relPos; |
| 20 |
|
| 21 |
|
| 22 |
varying float alpha; |
| 23 |
|
| 24 |
uniform sampler2D BaseTex; |
| 25 |
//uniform sampler2D NormalTex; |
| 26 |
//uniform sampler2D ReflMapTex; |
| 27 |
uniform sampler2D CarTex; |
| 28 |
uniform sampler2D CarMetaTex; |
| 29 |
//uniform sampler2D ReflGradientsTex; |
| 30 |
//uniform samplerCube Environment; |
| 31 |
//uniform sampler2D GrainTex; |
| 32 |
|
| 33 |
//uniform int dirt_enabled; |
| 34 |
//uniform int dirt_multi; |
| 35 |
//uniform int nmap_dds; |
| 36 |
//uniform int nmap_enabled; |
| 37 |
//uniform int refl_enabled; |
| 38 |
//uniform int refl_type; |
| 39 |
//uniform int refl_map; |
| 40 |
//uniform int grain_texture_enabled; |
| 41 |
uniform int road_traffic_direction; |
| 42 |
//uniform int rain_enabled; |
| 43 |
uniform int road_traffic_enabled; |
| 44 |
uniform int cloud_shadow_flag; |
| 45 |
uniform int use_searchlight; |
| 46 |
uniform int use_landing_light; |
| 47 |
uniform int use_alt_landing_light; |
| 48 |
|
| 49 |
uniform float amb_correction; |
| 50 |
uniform float dirt_b_factor; |
| 51 |
uniform float dirt_g_factor; |
| 52 |
uniform float dirt_r_factor; |
| 53 |
//uniform float nmap_tile; |
| 54 |
//uniform float refl_correction; |
| 55 |
//uniform float refl_fresnel; |
| 56 |
//uniform float refl_fresnel_factor; |
| 57 |
//uniform float refl_noise; |
| 58 |
//uniform float refl_rainbow; |
| 59 |
//uniform float grain_magnification; |
| 60 |
//uniform float wetness; |
| 61 |
//uniform float rain_norm; |
| 62 |
uniform float road_traffic_density; |
| 63 |
uniform float streetlight_factor; |
| 64 |
uniform float road_traffic_variation; |
| 65 |
|
| 66 |
uniform float avisibility; |
| 67 |
uniform float cloud_self_shading; |
| 68 |
uniform float eye_alt; |
| 69 |
uniform float ground_scattering; |
| 70 |
uniform float hazeLayerAltitude; |
| 71 |
uniform float moonlight; |
| 72 |
uniform float overcast; |
| 73 |
uniform float scattering; |
| 74 |
uniform float terminator; |
| 75 |
uniform float terrain_alt; |
| 76 |
uniform float visibility; |
| 77 |
uniform float air_pollution; |
| 78 |
uniform float snowlevel; |
| 79 |
uniform float snow_thickness_factor; |
| 80 |
|
| 81 |
uniform float osg_SimulationTime; |
| 82 |
|
| 83 |
uniform float landing_light1_offset; |
| 84 |
uniform float landing_light2_offset; |
| 85 |
uniform float landing_light3_offset; |
| 86 |
|
| 87 |
uniform bool use_IR_vision; |
| 88 |
|
| 89 |
uniform mat4 fg_zUpTransform; |
| 90 |
uniform vec3 fg_modelOffset; |
| 91 |
|
| 92 |
// constants needed by the light and fog computations ################################################### |
| 93 |
|
| 94 |
const float EarthRadius = 5800000.0; |
| 95 |
const float terminator_width = 200000.0; |
| 96 |
|
| 97 |
|
| 98 |
//uniform vec3 dirt_r_color; |
| 99 |
//uniform vec3 dirt_g_color; |
| 100 |
//uniform vec3 dirt_b_color; |
| 101 |
|
| 102 |
uniform vec3 streetlight_color; |
| 103 |
|
| 104 |
float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density); |
| 105 |
float shadow_func (in float x, in float y, in float noise, in float dist); |
| 106 |
float fog_func (in float targ, in float altitude); |
| 107 |
float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt); |
| 108 |
float alt_factor(in float eye_alt, in float vertex_alt); |
| 109 |
float light_distance_fading(in float dist); |
| 110 |
float fog_backscatter(in float avisibility); |
| 111 |
float rand2D(in vec2 co); |
| 112 |
float Noise2D(in vec2 coord, in float wavelength); |
| 113 |
|
| 114 |
vec3 rayleigh_out_shift(in vec3 color, in float outscatter); |
| 115 |
vec3 get_hazeColor(in float lightArg); |
| 116 |
vec3 searchlight(); |
| 117 |
vec3 landing_light(in float offset, in float offsetv); |
| 118 |
vec3 filter_combined (in vec3 color) ; |
| 119 |
vec3 addLights(in vec3 color1, in vec3 color2); |
| 120 |
|
| 121 |
float getShadowing(); |
| 122 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 123 |
|
| 124 |
|
| 125 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 126 |
{ |
| 127 |
if (x > 30.0) {return e;} |
| 128 |
if (x < -15.0) {return 0.0;} |
| 129 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
void road_type_mapper (in vec2 coord, out float rtype_traffic_density, out float rtype_base_illumination, out float rtype_traffic_speed, out int rtype_dual_lane) |
| 134 |
{ |
| 135 |
if (coord.s < 0.125) // railway |
| 136 |
{ |
| 137 |
rtype_dual_lane = 0; |
| 138 |
rtype_traffic_density = 0; |
| 139 |
rtype_base_illumination = 0; |
| 140 |
rtype_traffic_speed = 0.0; |
| 141 |
} |
| 142 |
else if (coord.s < 0.250) // residential |
| 143 |
{ |
| 144 |
rtype_dual_lane = 0; |
| 145 |
rtype_traffic_density = 0.3; |
| 146 |
rtype_base_illumination = 0.65; |
| 147 |
rtype_traffic_speed = 0.5; |
| 148 |
} |
| 149 |
else if (coord.s < 0.375) // single-lane major |
| 150 |
{ |
| 151 |
rtype_dual_lane = 0; |
| 152 |
rtype_traffic_density = 1.0; |
| 153 |
rtype_base_illumination = 0.65; |
| 154 |
rtype_traffic_speed = 1.0; |
| 155 |
} |
| 156 |
else if (coord.s < 0.5) |
| 157 |
{ |
| 158 |
rtype_dual_lane = 0; |
| 159 |
rtype_traffic_density = 0.0; |
| 160 |
rtype_base_illumination = 0.0; |
| 161 |
rtype_traffic_speed = 0.0; |
| 162 |
} |
| 163 |
else if (coord.s < 0.625) // grass |
| 164 |
{ |
| 165 |
rtype_dual_lane = 0; |
| 166 |
rtype_traffic_density = 0.0; |
| 167 |
rtype_base_illumination = 0.0; |
| 168 |
rtype_traffic_speed = 0.0; |
| 169 |
} |
| 170 |
else if (coord.s < 0.750) // dual-lane highway |
| 171 |
{ |
| 172 |
rtype_dual_lane = 1; |
| 173 |
rtype_traffic_density = 1.0; |
| 174 |
rtype_base_illumination = 0.0; |
| 175 |
rtype_traffic_speed = 1.0; |
| 176 |
} |
| 177 |
else if (coord.s < 0.875) // dirt |
| 178 |
{ |
| 179 |
rtype_dual_lane = 0; |
| 180 |
rtype_traffic_density = 0.1; |
| 181 |
rtype_base_illumination = 0.0; |
| 182 |
rtype_traffic_speed = 0.3; |
| 183 |
} |
| 184 |
else // tramway |
| 185 |
{ |
| 186 |
rtype_dual_lane = 0; |
| 187 |
rtype_traffic_density = 0.0; |
| 188 |
rtype_base_illumination = 0.0; |
| 189 |
rtype_traffic_speed = 0.0; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
void main (void) |
| 198 |
{ |
| 199 |
vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st); |
| 200 |
vec4 noisevec = vec4 (1.0, 1.0, 1.0, 1.0); |
| 201 |
|
| 202 |
vec4 grainTexel; |
| 203 |
|
| 204 |
vec3 mixedcolor; |
| 205 |
vec3 N = vec3(0.0,0.0,1.0); |
| 206 |
|
| 207 |
// noise |
| 208 |
|
| 209 |
float noise_1m = Noise2D(rawpos.xy, 1.0); |
| 210 |
float noise_5m = Noise2D(rawpos.xy, 5.0); |
| 211 |
|
| 212 |
// road type characteristics |
| 213 |
|
| 214 |
float rtype_traffic_density = 0.0; |
| 215 |
float rtype_base_illumination = 0.0; |
| 216 |
float rtype_traffic_speed = 0.0; |
| 217 |
int rtype_dual_lane = 0; |
| 218 |
road_type_mapper (gl_TexCoord[0].st, rtype_traffic_density, rtype_base_illumination, rtype_traffic_speed, rtype_dual_lane); |
| 219 |
|
| 220 |
float pf = 0.0; |
| 221 |
float pf1 = 0.0; |
| 222 |
///some generic light scattering parameters |
| 223 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 224 |
vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight; |
| 225 |
float alt = eye_alt; |
| 226 |
float effective_scattering = min(scattering, cloud_self_shading); |
| 227 |
|
| 228 |
|
| 229 |
/// BEGIN geometry for light |
| 230 |
|
| 231 |
vec3 up = (gl_ModelViewMatrix * vec4(0.0,0.0,1.0,0.0)).xyz; |
| 232 |
|
| 233 |
vec3 nVertVec = normalize(vertVec); |
| 234 |
|
| 235 |
float dist = length(relPos); |
| 236 |
// angle of view vector with horizon |
| 237 |
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist; |
| 238 |
|
| 239 |
float vertex_alt = (relPos.z + eye_alt); |
| 240 |
float vertex_scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt); |
| 241 |
|
| 242 |
|
| 243 |
vec3 lightHorizon = gl_LightSource[0].position.xyz - up * dot(up,gl_LightSource[0].position.xyz); |
| 244 |
float yprime = -dot(vertVec, lightHorizon); |
| 245 |
float yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt); |
| 246 |
float lightArg = (terminator-yprime_alt)/100000.0; |
| 247 |
|
| 248 |
float earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4; |
| 249 |
|
| 250 |
float mie_angle = gl_Color.a; |
| 251 |
float fog_vertex_alt = max(vertex_alt,hazeLayerAltitude); |
| 252 |
float fog_yprime_alt = yprime_alt; |
| 253 |
if (fog_vertex_alt > hazeLayerAltitude) |
| 254 |
{ |
| 255 |
if (dist > 0.8 * avisibility) |
| 256 |
{ |
| 257 |
fog_vertex_alt = mix(fog_vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist)); |
| 258 |
fog_yprime_alt = yprime -sqrt(2.0 * EarthRadius * fog_vertex_alt); |
| 259 |
} |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
fog_vertex_alt = hazeLayerAltitude; |
| 264 |
fog_yprime_alt = yprime -sqrt(2.0 * EarthRadius * fog_vertex_alt); |
| 265 |
} |
| 266 |
|
| 267 |
float fog_lightArg = (terminator-fog_yprime_alt)/100000.0; |
| 268 |
float fog_earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, fog_yprime_alt) + 0.1; |
| 269 |
|
| 270 |
/// END geometry for light |
| 271 |
|
| 272 |
|
| 273 |
/// BEGIN light |
| 274 |
vec4 light_diffuse; |
| 275 |
vec4 light_ambient; |
| 276 |
float intensity; |
| 277 |
|
| 278 |
light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0); |
| 279 |
light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 280 |
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); |
| 281 |
light_diffuse.a = 1.0; |
| 282 |
light_diffuse = light_diffuse * vertex_scattering; |
| 283 |
|
| 284 |
light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33); |
| 285 |
light_ambient.g = light_ambient.r * 0.4/0.33; |
| 286 |
light_ambient.b = light_ambient.r * 0.5/0.33; |
| 287 |
light_ambient.a = 1.0; |
| 288 |
|
| 289 |
if (earthShade < 0.5) |
| 290 |
{ |
| 291 |
intensity = length(light_ambient.rgb); |
| 292 |
light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade) )); |
| 293 |
light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade)); |
| 294 |
|
| 295 |
intensity = length(light_diffuse.rgb); |
| 296 |
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.7,earthShade) )); |
| 297 |
} |
| 298 |
|
| 299 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 300 |
vec3 ecViewDir = (gl_ModelViewMatrix * (ep - vec4(rawpos, 1.0))).xyz; |
| 301 |
vec3 HV = normalize(normalize(gl_LightSource[0].position.xyz) + normalize(ecViewDir)); |
| 302 |
|
| 303 |
/// END light |
| 304 |
|
| 305 |
/// BEGIN procedural textures - cars and snow |
| 306 |
|
| 307 |
vec2 roadCoords = gl_TexCoord[0].st; |
| 308 |
roadCoords.s *=8.0; |
| 309 |
roadCoords.s = fract(roadCoords.s); |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
vec4 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0); |
| 314 |
float noise_term = 0.5 * (noise_5m - 0.5); |
| 315 |
noise_term += 0.5 * (noise_1m - 0.5); |
| 316 |
snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + 0.5*snow_thickness_factor +0.0001*(relPos.z +eye_alt -snowlevel) ); |
| 317 |
|
| 318 |
float noise_2000m = 0.0; |
| 319 |
float noise_10m = 0.0; |
| 320 |
|
| 321 |
float snowLaneShape = smoothstep(0.20, 0.28, roadCoords.s) * (1.0-smoothstep(0.42, 0.5, roadCoords.s)); |
| 322 |
snowLaneShape += smoothstep(0.6, 0.68, roadCoords.s) * (1.0-smoothstep(0.82, 0.9, roadCoords.s)); |
| 323 |
snow_texel.a *= (1.0 - 0.3* snowLaneShape * rtype_traffic_density); |
| 324 |
|
| 325 |
texel.rgb = mix(texel.rgb, snow_texel.rgb, snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, 1.0 * (relPos.z + eye_alt)+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0)); |
| 326 |
|
| 327 |
float cTag = 0.0; |
| 328 |
float cPresent = 0.0; |
| 329 |
|
| 330 |
float cSign = 1.0; |
| 331 |
float total_traffic_density = 0.0; |
| 332 |
|
| 333 |
vec4 carMetaTexel; |
| 334 |
|
| 335 |
if (road_traffic_enabled == 1) |
| 336 |
{ |
| 337 |
float cOffset = 0.0; |
| 338 |
if (roadCoords.s > 0.5) |
| 339 |
{ |
| 340 |
if (rtype_dual_lane == 0) {cSign = -1.0;} |
| 341 |
else {cOffset = 5.0;} |
| 342 |
} |
| 343 |
|
| 344 |
if (rtype_dual_lane == 1) {cSign = -1.0;} |
| 345 |
|
| 346 |
cSign *= road_traffic_direction; |
| 347 |
|
| 348 |
total_traffic_density = road_traffic_density * rtype_traffic_density * road_traffic_variation; |
| 349 |
|
| 350 |
float cCoord = roadCoords.t + cOffset; |
| 351 |
cCoord += 0.3 * osg_SimulationTime * cSign * rtype_traffic_speed * (1.0 - (0.9 * smoothstep(1.0, 2.5, total_traffic_density))); |
| 352 |
cCoord *= 5.0; |
| 353 |
|
| 354 |
cTag = fract(cCoord); |
| 355 |
float cDomain = cCoord - cTag; |
| 356 |
float cRnd = rand2D(vec2 (cDomain, cSign)); |
| 357 |
|
| 358 |
cPresent = 0.0; |
| 359 |
float cDisc = 0.2 * total_traffic_density; |
| 360 |
if (cRnd > 1.0 - cDisc) {cPresent = 1.0;} |
| 361 |
|
| 362 |
float cColorRnd = (cRnd - 1.0 + cDisc)/ max(cDisc, 0.05); |
| 363 |
float cColorRnd2 = rand2D(vec2 (cDomain, 0.5)); |
| 364 |
|
| 365 |
float cColumn = (cColorRnd2 * 16.0) - fract(cColorRnd2 * 16.0); |
| 366 |
float cRow = (rand2D(vec2 (cDomain, 1.5)) * 2.0); |
| 367 |
cRow = cRow- fract(cRow); |
| 368 |
//cRow = 0.0; |
| 369 |
|
| 370 |
|
| 371 |
vec3 cColor = vec3 (0.8 * (1.0 - cColorRnd), 0.8 * 2.0 * (0.5 - abs(cColorRnd - 0.5)) , 0.8 * cColorRnd); |
| 372 |
cColor *= cColorRnd2; |
| 373 |
|
| 374 |
float cPos = cTag; |
| 375 |
if (cSign > 0.0) {cPos = 1.0 - cPos;} |
| 376 |
float cShape = smoothstep(0.0, 0.05, cPos) * (1.0-smoothstep(0.35, 0.4, cPos)); |
| 377 |
|
| 378 |
float ctPos; |
| 379 |
if (roadCoords.s < 0.5) |
| 380 |
{ctPos = clamp(5.0 * (roadCoords.s - 0.25), 0.0,1.0); } |
| 381 |
else |
| 382 |
{ctPos = clamp(5.0 * (roadCoords.s - 0.65), 0.0,1.0); } |
| 383 |
|
| 384 |
float clPos = cPos;// + 0.45; |
| 385 |
|
| 386 |
vec4 carTexel = texture2D(CarTex, vec2((ctPos + cColumn) / 16.0 , ((clPos + cRow) / 2.0) )); |
| 387 |
carMetaTexel = texture2D(CarMetaTex, vec2((ctPos + cColumn) / 16.0 , ((clPos + cRow) / 2.0) )); |
| 388 |
|
| 389 |
|
| 390 |
float laneShape = smoothstep(0.25, 0.28, roadCoords.s) * (1.0-smoothstep(0.42, 0.45, roadCoords.s)); |
| 391 |
laneShape += smoothstep(0.65, 0.68, roadCoords.s) * (1.0-smoothstep(0.82, 0.85, roadCoords.s)); |
| 392 |
cShape *= laneShape; |
| 393 |
|
| 394 |
//texel.rgb = mix(texel.rgb, cColor, cPresent * cShape); |
| 395 |
texel.rgb = mix(texel.rgb, carTexel.rgb, cPresent * carTexel.a * laneShape); |
| 396 |
//texel.rgb = mix(texel.rgb, vec3 (1.0, 0.0, 0.0) * cColorRnd2, 0.3); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
/// END procedural cars |
| 402 |
|
| 403 |
//vec3 reflVecN; |
| 404 |
|
| 405 |
//vec3 viewVec = normalize(vViewVec); |
| 406 |
//float v = abs(dot(viewVec, normalize(VNormal)));// Map a rainbowish color |
| 407 |
|
| 408 |
//float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz))); |
| 409 |
|
| 410 |
|
| 411 |
float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz))); |
| 412 |
|
| 413 |
//float nDotHV = max(0.0, dot(N, normalize(gl_LightSource[0].halfVector.xyz))); |
| 414 |
float nDotHV = max(0.0, dot(N,HV)); |
| 415 |
//glare on the backside of tranparent objects |
| 416 |
//if ((gl_FrontMaterial.diffuse.a < 1.0 || texel.a < 1.0) |
| 417 |
// && dot(N, normalize(gl_LightSource[0].position.xyz)) < 0.0) { |
| 418 |
// nDotVP = max(0.0, dot(-N, normalize(gl_LightSource[0].position.xyz)) * (1.0 -texel.a) ); |
| 419 |
// nDotHV = max(0.0, dot(-N, HV) * (1.0 -texel.a) ); |
| 420 |
// } |
| 421 |
|
| 422 |
float nDotVP1 = 0.0; |
| 423 |
float nDotHV1 = 0.0; |
| 424 |
|
| 425 |
|
| 426 |
// try specular reflection of sky irradiance |
| 427 |
nDotVP1 = max(0.0, dot(N, up)); |
| 428 |
nDotHV1 = max(0.0, dot(N, normalize(normalize(up) + normalize(-vertVec)))); |
| 429 |
|
| 430 |
|
| 431 |
if (nDotVP == 0.0) |
| 432 |
{pf = 0.0;} |
| 433 |
else |
| 434 |
{pf = pow(nDotHV, gl_FrontMaterial.shininess);} |
| 435 |
|
| 436 |
if (nDotVP1 == 0.0) |
| 437 |
{pf1 = 0.0;} |
| 438 |
else |
| 439 |
{pf1 = pow(nDotHV1, 0.5*gl_FrontMaterial.shininess);} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if (cloud_shadow_flag == 1) |
| 444 |
{ |
| 445 |
light_diffuse = light_diffuse * shadow_func(relPos.x, relPos.y, 1.0, dist); |
| 446 |
} |
| 447 |
|
| 448 |
vec3 secondary_light = vec3 (0.0,0.0,0.0); |
| 449 |
|
| 450 |
if (use_searchlight == 1) |
| 451 |
{ |
| 452 |
secondary_light += searchlight(); |
| 453 |
} |
| 454 |
if (use_landing_light == 1) |
| 455 |
{ |
| 456 |
secondary_light += landing_light(landing_light1_offset, landing_light3_offset); |
| 457 |
} |
| 458 |
if (use_alt_landing_light == 1) |
| 459 |
{ |
| 460 |
secondary_light += landing_light(landing_light2_offset, landing_light3_offset); |
| 461 |
} |
| 462 |
|
| 463 |
float shadowmap = getShadowing(); |
| 464 |
vec4 Diffuse = light_diffuse * nDotVP * shadowmap; |
| 465 |
Diffuse.rgb += secondary_light * light_distance_fading(dist); |
| 466 |
if (use_IR_vision) |
| 467 |
{ |
| 468 |
Diffuse.rgb = max(Diffuse.rgb, vec3 (0.5, 0.5, 0.5)); |
| 469 |
} |
| 470 |
vec4 Specular = gl_FrontMaterial.specular * light_diffuse * pf + gl_FrontMaterial.specular * light_ambient * pf1 * shadowmap; |
| 471 |
Specular+= gl_FrontMaterial.specular * pow(max(0.0,-dot(N,nVertVec)),gl_FrontMaterial.shininess) * vec4(secondary_light,1.0); |
| 472 |
|
| 473 |
|
| 474 |
//vec4 color = gl_Color + Diffuse * gl_FrontMaterial.diffuse; |
| 475 |
vec4 color = Diffuse;// * gl_FrontMaterial.diffuse; |
| 476 |
|
| 477 |
|
| 478 |
color = clamp( color, 0.0, 1.0 ); |
| 479 |
|
| 480 |
// set ambient adjustment to remove bluiness with user input |
| 481 |
float ambient_offset = clamp(amb_correction, -1.0, 1.0); |
| 482 |
vec4 ambient = gl_LightModel.ambient + light_ambient; |
| 483 |
vec4 ambient_Correction = vec4(ambient.rg, ambient.b * 0.6, 1.0) |
| 484 |
* ambient_offset ; |
| 485 |
ambient_Correction = clamp(ambient_Correction, -1.0, 1.0); |
| 486 |
|
| 487 |
color += ambient; |
| 488 |
color.a = texel.a * alpha; |
| 489 |
vec4 fragColor = vec4(color.rgb * mixedcolor + ambient_Correction.rgb, color.a); |
| 490 |
|
| 491 |
fragColor += Specular; |
| 492 |
|
| 493 |
fragColor.rgb += getClusteredLightsContribution(vertVec, N, texel.rgb); |
| 494 |
|
| 495 |
////////////////////////////////////////////////////////////////////// |
| 496 |
// BEGIN procedural lightmap |
| 497 |
////////////////////////////////////////////////////////////////////// |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
vec3 pLMColor = streetlight_color; |
| 507 |
|
| 508 |
float pLMIntensity = smoothstep(0.0, 0.4, roadCoords.s) * (1.0 - smoothstep(0.6, 1.0, roadCoords.s)); |
| 509 |
pLMIntensity = 0.25 * rtype_base_illumination * (1.0+ streetlight_factor) + 0.1 * max(0.0,sin(4.0 * roadCoords.t)) * streetlight_factor; |
| 510 |
pLMIntensity = clamp(pLMIntensity, 0.0, 1.0); |
| 511 |
|
| 512 |
if (gl_FrontMaterial.diffuse.r == 0.0) {pLMIntensity =0.0;} |
| 513 |
|
| 514 |
pLMColor *= pLMIntensity; |
| 515 |
|
| 516 |
if (road_traffic_enabled == 1) |
| 517 |
{ |
| 518 |
|
| 519 |
float viewAngleFactor = smoothstep(-0.05, 0.0, cSign * dot(normalize(VBinormal), nVertVec)); |
| 520 |
|
| 521 |
vec3 pCLColor = vec3 (0.95, 1.0, 1.0); |
| 522 |
vec3 pTLColor = vec3 (0.95, 0.0, 0.0); |
| 523 |
|
| 524 |
|
| 525 |
// mean illumination by car headlights |
| 526 |
pLMColor = pLMColor + 0.2 * min(1.0,total_traffic_density) * pCLColor; |
| 527 |
|
| 528 |
//float pCLIntensity = smoothstep(0.4, 0.6, cTag) * (1.0-smoothstep(0.6, 0.8, cTag)); |
| 529 |
//float laneFact = smoothstep(0.25, 0.3, roadCoords.s) * (1.0-smoothstep(0.3, 0.35, roadCoords.s)); |
| 530 |
//laneFact += smoothstep(0.35, 0.4, roadCoords.s) * (1.0-smoothstep(0.4, 0.45, roadCoords.s)); |
| 531 |
//laneFact += smoothstep(0.65, 0.7, roadCoords.s) * (1.0-smoothstep(0.7, 0.75, roadCoords.s)); |
| 532 |
//laneFact += smoothstep(0.75, 0.8, roadCoords.s) * (1.0-smoothstep(0.8, 0.85, roadCoords.s)); |
| 533 |
//pCLIntensity = pCLIntensity * laneFact * cPresent; |
| 534 |
|
| 535 |
|
| 536 |
float pCLIntensity, pTLIntensity; |
| 537 |
if (cSign == -1.0) |
| 538 |
{ |
| 539 |
pCLIntensity = carMetaTexel.r * cPresent * smoothstep(0.45, 0.55, cTag) * viewAngleFactor ; |
| 540 |
pCLIntensity += 0.85 * carMetaTexel.b * cPresent * smoothstep(0.45, 0.55, cTag); |
| 541 |
pTLIntensity = carMetaTexel.r * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag)) * (1.0 - viewAngleFactor); |
| 542 |
pTLIntensity += 0.85 * carMetaTexel.b * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag)); |
| 543 |
|
| 544 |
} |
| 545 |
else |
| 546 |
{ |
| 547 |
pCLIntensity = carMetaTexel.r * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag)) *viewAngleFactor ; |
| 548 |
pCLIntensity += 0.85 * carMetaTexel.b * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag)); |
| 549 |
pTLIntensity = carMetaTexel.r * cPresent * smoothstep(0.45, 0.55, cTag) * (1.0 - viewAngleFactor); |
| 550 |
pTLIntensity += 0.85 * carMetaTexel.b * cPresent * smoothstep(0.45, 0.55, cTag); |
| 551 |
} |
| 552 |
|
| 553 |
//pCLColor *= pCLIntensity; |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
//if (cSign == 1.0) |
| 558 |
// { |
| 559 |
// pTLIntensity = smoothstep(0.9, 0.94, cTag) * (1.0-smoothstep(0.96, 1.0, cTag)); |
| 560 |
// } |
| 561 |
//else |
| 562 |
// { |
| 563 |
// pTLIntensity = smoothstep(0.0, 0.04, cTag) * (1.0-smoothstep(0.06, 0.1, cTag)); |
| 564 |
// } |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
//pTLIntensity = pTLIntensity * laneFact * cPresent * (1.0 - viewAngleFactor); |
| 569 |
|
| 570 |
|
| 571 |
pCLColor = pCLColor * pCLIntensity + pTLColor * pTLIntensity; |
| 572 |
|
| 573 |
|
| 574 |
pLMColor = clamp(pLMColor, 0.0, 1.0); |
| 575 |
|
| 576 |
pLMColor = max(pLMColor,pCLColor); |
| 577 |
} |
| 578 |
|
| 579 |
//fragColor.rgb = max(fragColor.rgb, pLMColor * gl_FrontMaterial.diffuse.rgb * smoothstep(0.0, 1.0, mixedcolor*.5 + pLMColor*.5)); |
| 580 |
|
| 581 |
fragColor.rgb = max(fragColor.rgb, pLMColor * smoothstep(0.0, 1.0, mixedcolor*.5 + pLMColor*.5)); |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
////////////////////////////////////////////////////////////////////// |
| 586 |
// END procedural lightmap |
| 587 |
////////////////////////////////////////////////////////////////////// |
| 588 |
|
| 589 |
|
| 590 |
/// BEGIN fog amount |
| 591 |
|
| 592 |
float transmission; |
| 593 |
float vAltitude; |
| 594 |
float delta_zv; |
| 595 |
float H; |
| 596 |
float distance_in_layer; |
| 597 |
float transmission_arg; |
| 598 |
float eqColorFactor; |
| 599 |
|
| 600 |
float delta_z = hazeLayerAltitude - eye_alt; |
| 601 |
float mvisibility = min(visibility, avisibility); |
| 602 |
|
| 603 |
if (dist > 0.04 * mvisibility) |
| 604 |
{ |
| 605 |
if (delta_z > 0.0) // we're inside the layer |
| 606 |
{ |
| 607 |
if (ct < 0.0) // we look down |
| 608 |
{ |
| 609 |
distance_in_layer = dist; |
| 610 |
vAltitude = min(distance_in_layer,mvisibility) * ct; |
| 611 |
delta_zv = delta_z - vAltitude; |
| 612 |
} |
| 613 |
else // we may look through upper layer edge |
| 614 |
{ |
| 615 |
H = dist * ct; |
| 616 |
if (H > delta_z) {distance_in_layer = dist/H * delta_z;} |
| 617 |
else {distance_in_layer = dist;} |
| 618 |
vAltitude = min(distance_in_layer,visibility) * ct; |
| 619 |
delta_zv = delta_z - vAltitude; |
| 620 |
} |
| 621 |
} |
| 622 |
else // we see the layer from above, delta_z < 0.0 |
| 623 |
{ |
| 624 |
H = dist * -ct; |
| 625 |
if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading |
| 626 |
{ |
| 627 |
distance_in_layer = 0.0; |
| 628 |
delta_zv = 0.0; |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
vAltitude = H + delta_z; |
| 633 |
distance_in_layer = vAltitude/H * dist; |
| 634 |
vAltitude = min(distance_in_layer,visibility) * (-ct); |
| 635 |
delta_zv = vAltitude; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
transmission_arg = (dist-distance_in_layer)/avisibility; |
| 640 |
|
| 641 |
|
| 642 |
if (visibility < avisibility) |
| 643 |
{ |
| 644 |
transmission_arg = transmission_arg + (distance_in_layer/visibility); |
| 645 |
eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering); |
| 646 |
} |
| 647 |
else |
| 648 |
{ |
| 649 |
transmission_arg = transmission_arg + (distance_in_layer/avisibility); |
| 650 |
eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering); |
| 651 |
} |
| 652 |
transmission = fog_func(transmission_arg, alt); |
| 653 |
if (eqColorFactor < 0.2) eqColorFactor = 0.2; |
| 654 |
} |
| 655 |
else |
| 656 |
{ |
| 657 |
eqColorFactor = 1.0; |
| 658 |
transmission = 1.0; |
| 659 |
} |
| 660 |
|
| 661 |
/// END fog amount |
| 662 |
|
| 663 |
/// BEGIN fog color |
| 664 |
|
| 665 |
vec3 hazeColor = get_hazeColor(fog_lightArg); |
| 666 |
|
| 667 |
float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0); |
| 668 |
float lightIntensity = length(hazeColor * effective_scattering) * rShade; |
| 669 |
|
| 670 |
if (transmission< 1.0) |
| 671 |
{ |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
if (fog_lightArg < 10.0) |
| 676 |
{ |
| 677 |
intensity = length(hazeColor); |
| 678 |
float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt)); |
| 679 |
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)) ); |
| 680 |
} |
| 681 |
|
| 682 |
intensity = length(hazeColor); |
| 683 |
hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt))); |
| 684 |
|
| 685 |
hazeColor.r = hazeColor.r * 0.83; |
| 686 |
hazeColor.g = hazeColor.g * 0.9; |
| 687 |
|
| 688 |
float fade_out = max(0.65 - 0.3 *overcast, 0.45); |
| 689 |
intensity = length(hazeColor); |
| 690 |
hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,fog_earthShade) )); |
| 691 |
hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor)))); |
| 692 |
|
| 693 |
float shadow = mix( min(1.0 + dot(VNormal,gl_LightSource[0].position.xyz),1.0), 1.0, 1.0-smoothstep(0.1, 0.4, transmission)); |
| 694 |
hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator)); |
| 695 |
} |
| 696 |
else |
| 697 |
{ |
| 698 |
hazeColor = vec3 (1.0, 1.0, 1.0); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
/// END fog color |
| 705 |
fragColor = clamp(fragColor, 0.0, 1.0); |
| 706 |
hazeColor = clamp(hazeColor, 0.0, 1.0); |
| 707 |
|
| 708 |
///BEGIN Rayleigh fog /// |
| 709 |
|
| 710 |
// Rayleigh color shift due to out-scattering |
| 711 |
float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z); |
| 712 |
float outscatter = 1.0-exp(-dist/rayleigh_length); |
| 713 |
fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter); |
| 714 |
|
| 715 |
vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity; |
| 716 |
float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z); |
| 717 |
fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength); |
| 718 |
|
| 719 |
/// END Rayleigh fog |
| 720 |
|
| 721 |
// don't let the light fade out too rapidly |
| 722 |
lightArg = (terminator + 200000.0)/100000.0; |
| 723 |
float minLightIntensity = min(0.2,0.16 * lightArg + 0.5); |
| 724 |
vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4); |
| 725 |
hazeColor *= eqColorFactor * fog_earthShade; |
| 726 |
hazeColor.rgb = max(hazeColor.rgb, minLight.rgb); |
| 727 |
|
| 728 |
|
| 729 |
fragColor.rgb = mix(hazeColor +secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission); |
| 730 |
|
| 731 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 732 |
|
| 733 |
gl_FragColor = fragColor; |
| 734 |
|
| 735 |
} |