Debian Codegraph @

Shaders/road-ALS-ultra.frag

raw
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
20 varying float alpha;
21
22 uniform sampler2D BaseTex;
23 uniform sampler2D NormalTex;
24 uniform sampler2D ReflMapTex;
25 uniform sampler2D CarTex;
26 uniform sampler2D CarMetaTex;
27 uniform sampler2D ReflGradientsTex;
28 uniform samplerCube Environment;
29 uniform sampler2D GrainTex;
30
31 uniform int dirt_enabled;
32 uniform int dirt_multi;
33 uniform int nmap_dds;
34 uniform int nmap_enabled;
35 uniform int refl_enabled;
36 uniform int refl_type;
37 uniform int refl_map;
38 uniform int grain_texture_enabled;
39 uniform int road_traffic_direction;
40 uniform int rain_enabled;
41 uniform int road_traffic_enabled;
42 uniform int cloud_shadow_flag;
43 uniform int use_searchlight;
44 uniform int use_landing_light;
45 uniform int use_alt_landing_light;
46
47 uniform float amb_correction;
48 uniform float dirt_b_factor;
49 uniform float dirt_g_factor;
50 uniform float dirt_r_factor;
51 uniform float nmap_tile;
52 uniform float refl_correction;
53 uniform float refl_fresnel;
54 uniform float refl_fresnel_factor;
55 uniform float refl_noise;
56 uniform float refl_rainbow;
57 uniform float grain_magnification;
58 uniform float wetness;
59 uniform float rain_norm;
60 uniform float road_traffic_density;
61 uniform float streetlight_factor;
62 uniform float road_traffic_variation;
63
64 uniform float avisibility;
65 uniform float cloud_self_shading;
66 uniform float eye_alt;
67 uniform float ground_scattering;
68 uniform float hazeLayerAltitude;
69 uniform float moonlight;
70 uniform float overcast;
71 uniform float scattering;
72 uniform float terminator;
73 uniform float terrain_alt;
74 uniform float visibility;
75 uniform float air_pollution;
76 uniform float snowlevel;
77 uniform float snow_thickness_factor;
78
79 uniform int quality_level;
80 uniform int tquality_level;
81
82 uniform float osg_SimulationTime;
83
84 uniform float landing_light1_offset;
85 uniform float landing_light2_offset;
86 uniform float landing_light3_offset;
87
88 uniform bool use_IR_vision;
89
90 // constants needed by the light and fog computations ###################################################
91
92 const float EarthRadius = 5800000.0;
93 const float terminator_width = 200000.0;
94
95
96 uniform vec3 dirt_r_color;
97 uniform vec3 dirt_g_color;
98 uniform vec3 dirt_b_color;
99
100 uniform vec3 streetlight_color;
101
102 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
103 float shadow_func (in float x, in float y, in float noise, in float dist);
104 float fog_func (in float targ, in float altitude);
105 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
106 float alt_factor(in float eye_alt, in float vertex_alt);
107 float light_distance_fading(in float dist);
108 float fog_backscatter(in float avisibility);
109 float rand2D(in vec2 co);
110 float Noise2D(in vec2 coord, in float wavelength);
111
112 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
113 vec3 get_hazeColor(in float lightArg);
114 vec3 searchlight();
115 vec3 landing_light(in float offset, in float offsetv);
116 vec3 filter_combined (in vec3 color) ;
117 vec3 addLights(in vec3 color1, in vec3 color2);
118
119 float getShadowing();
120 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
121
122
123 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
124 {
125 if (x > 30.0) {return e;}
126 if (x < -15.0) {return 0.0;}
127 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
128 }
129
130
131 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)
132 {
133 if (coord.s < 0.125) // railway
134 {
135 rtype_dual_lane = 0;
136 rtype_traffic_density = 0;
137 rtype_base_illumination = 0;
138 rtype_traffic_speed = 0.0;
139 }
140 else if (coord.s < 0.250) // residential
141 {
142 rtype_dual_lane = 0;
143 rtype_traffic_density = 0.3;
144 rtype_base_illumination = 0.65;
145 rtype_traffic_speed = 0.5;
146 }
147 else if (coord.s < 0.375) // single-lane major
148 {
149 rtype_dual_lane = 0;
150 rtype_traffic_density = 1.0;
151 rtype_base_illumination = 0.65;
152 rtype_traffic_speed = 1.0;
153 }
154 else if (coord.s < 0.5)
155 {
156 rtype_dual_lane = 0;
157 rtype_traffic_density = 0.0;
158 rtype_base_illumination = 0.0;
159 rtype_traffic_speed = 0.0;
160 }
161 else if (coord.s < 0.625) // grass
162 {
163 rtype_dual_lane = 0;
164 rtype_traffic_density = 0.0;
165 rtype_base_illumination = 0.0;
166 rtype_traffic_speed = 0.0;
167 }
168 else if (coord.s < 0.750) // dual-lane highway
169 {
170 rtype_dual_lane = 1;
171 rtype_traffic_density = 1.0;
172 rtype_base_illumination = 0.0;
173 rtype_traffic_speed = 1.0;
174 }
175 else if (coord.s < 0.875) // dirt
176 {
177 rtype_dual_lane = 0;
178 rtype_traffic_density = 0.1;
179 rtype_base_illumination = 0.0;
180 rtype_traffic_speed = 0.3;
181 }
182 else // tramway
183 {
184 rtype_dual_lane = 0;
185 rtype_traffic_density = 0.0;
186 rtype_base_illumination = 0.0;
187 rtype_traffic_speed = 0.0;
188 }
189
190
191 }
192
193
194
195 void main (void)
196 {
197 vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st);
198 vec4 nmap = texture2D(NormalTex, gl_TexCoord[0].st * nmap_tile);
199 vec4 reflmap = texture2D(ReflMapTex, 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(vertVec);
236 float vertex_alt = max(100.0,dot(up, vertVec) + alt);
237 float vertex_scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
238
239
240 vec3 lightHorizon = gl_LightSource[0].position.xyz - up * dot(up,gl_LightSource[0].position.xyz);
241 float yprime = -dot(vertVec, lightHorizon);
242 float yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
243 float lightArg = (terminator-yprime_alt)/100000.0;
244
245 float earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
246
247 float mie_angle;
248 if (lightArg < 10.0)
249 {mie_angle = (0.5 * dot(nVertVec, normalize(gl_LightSource[0].position.xyz)) ) + 0.5;}
250 else
251 {mie_angle = 1.0;}
252
253 float fog_vertex_alt = max(vertex_alt,hazeLayerAltitude);
254 float fog_yprime_alt = yprime_alt;
255 if (fog_vertex_alt > hazeLayerAltitude)
256 {
257 if (dist > 0.8 * avisibility)
258 {
259 fog_vertex_alt = mix(fog_vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
260 fog_yprime_alt = yprime -sqrt(2.0 * EarthRadius * fog_vertex_alt);
261 }
262 }
263 else
264 {
265 fog_vertex_alt = hazeLayerAltitude;
266 fog_yprime_alt = yprime -sqrt(2.0 * EarthRadius * fog_vertex_alt);
267 }
268
269 float fog_lightArg = (terminator-fog_yprime_alt)/100000.0;
270 float fog_earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, fog_yprime_alt) + 0.1;
271
272 float ct = dot(normalize(up), nVertVec);
273
274 vec3 relPos = (gl_ModelViewMatrixInverse * vec4 (vertVec,0.0)).xyz;
275
276 /// END geometry for light
277
278
279 /// BEGIN light
280 vec4 light_diffuse;
281 vec4 light_ambient;
282 float intensity;
283
284 light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
285 light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
286 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
287 light_diffuse.a = 1.0;
288 light_diffuse = light_diffuse * vertex_scattering;
289
290 light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
291 light_ambient.g = light_ambient.r * 0.4/0.33;
292 light_ambient.b = light_ambient.r * 0.5/0.33;
293 light_ambient.a = 1.0;
294
295 if (earthShade < 0.5)
296 {
297 intensity = length(light_ambient.rgb);
298 light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade) ));
299 light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
300
301 intensity = length(light_diffuse.rgb);
302 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.7,earthShade) ));
303 }
304
305 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
306 vec3 ecViewDir = (gl_ModelViewMatrix * (ep - vec4(rawpos, 1.0))).xyz;
307 vec3 HV = normalize(normalize(gl_LightSource[0].position.xyz) + normalize(ecViewDir));
308
309 /// END light
310
311 /// BEGIN grain overlay
312 if (grain_texture_enabled ==1)
313 {
314 grainTexel = texture2D(GrainTex, gl_TexCoord[0].st * grain_magnification);
315 texel.rgb = mix(texel.rgb, grainTexel.rgb, grainTexel.a );
316 }
317 else if (grain_texture_enabled == 2)
318 {
319 grainTexel = texture2D(GrainTex, rawpos.xy * grain_magnification);
320 texel.rgb = mix(texel.rgb, grainTexel.rgb, grainTexel.a );
321 }
322
323 /// END grain overlay
324
325
326 /// BEGIN procedural textures - cars and snow
327
328 vec2 roadCoords = gl_TexCoord[0].st;
329 roadCoords.s *=8.0;
330 roadCoords.s = fract(roadCoords.s);
331
332
333
334 vec4 snow_texel = vec4 (0.95, 0.95, 0.95, 1.0);
335 float noise_term = 0.5 * (noise_5m - 0.5);
336 noise_term += 0.5 * (noise_1m - 0.5);
337 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) );
338
339 float noise_2000m = 0.0;
340 float noise_10m = 0.0;
341
342 float snowLaneShape = smoothstep(0.20, 0.28, roadCoords.s) * (1.0-smoothstep(0.42, 0.5, roadCoords.s));
343 snowLaneShape += smoothstep(0.6, 0.68, roadCoords.s) * (1.0-smoothstep(0.82, 0.9, roadCoords.s));
344 snow_texel.a *= (1.0 - 0.3* snowLaneShape * rtype_traffic_density);
345
346 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));
347
348 float cTag = 0.0;
349 float cPresent = 0.0;
350
351 float cSign = 1.0;
352 float total_traffic_density = 0.0;
353
354 vec4 carMetaTexel;
355
356 if (road_traffic_enabled == 1)
357 {
358 float cOffset = 0.0;
359 if (roadCoords.s > 0.5)
360 {
361 if (rtype_dual_lane == 0) {cSign = -1.0;}
362 else {cOffset = 5.0;}
363 }
364
365 if (rtype_dual_lane == 1) {cSign = -1.0;}
366
367 cSign *= road_traffic_direction;
368
369 total_traffic_density = road_traffic_density * rtype_traffic_density * road_traffic_variation;
370
371 float cCoord = roadCoords.t + cOffset;
372 cCoord += 0.3 * osg_SimulationTime * cSign * rtype_traffic_speed * (1.0 - (0.9 * smoothstep(1.0, 2.5, total_traffic_density)));
373 cCoord *= 5.0;
374
375 cTag = fract(cCoord);
376 float cDomain = cCoord - cTag;
377 float cRnd = rand2D(vec2 (cDomain, cSign));
378
379 cPresent = 0.0;
380 float cDisc = 0.2 * total_traffic_density;
381 if (cRnd > 1.0 - cDisc) {cPresent = 1.0;}
382
383 float cColorRnd = (cRnd - 1.0 + cDisc)/ max(cDisc, 0.05);
384 float cColorRnd2 = rand2D(vec2 (cDomain, 0.5));
385
386 float cColumn = (cColorRnd2 * 16.0) - fract(cColorRnd2 * 16.0);
387 float cRow = (rand2D(vec2 (cDomain, 1.5)) * 2.0);
388 cRow = cRow- fract(cRow);
389 //cRow = 0.0;
390
391
392 vec3 cColor = vec3 (0.8 * (1.0 - cColorRnd), 0.8 * 2.0 * (0.5 - abs(cColorRnd - 0.5)) , 0.8 * cColorRnd);
393 cColor *= cColorRnd2;
394
395 float cPos = cTag;
396 if (cSign > 0.0) {cPos = 1.0 - cPos;}
397 float cShape = smoothstep(0.0, 0.05, cPos) * (1.0-smoothstep(0.35, 0.4, cPos));
398
399 float ctPos;
400 if (roadCoords.s < 0.5)
401 {ctPos = clamp(5.0 * (roadCoords.s - 0.25), 0.0,1.0); }
402 else
403 {ctPos = clamp(5.0 * (roadCoords.s - 0.65), 0.0,1.0); }
404
405 float clPos = cPos;// + 0.45;
406
407 vec4 carTexel = texture2D(CarTex, vec2((ctPos + cColumn) / 16.0 , ((clPos + cRow) / 2.0) ));
408 carMetaTexel = texture2D(CarMetaTex, vec2((ctPos + cColumn) / 16.0 , ((clPos + cRow) / 2.0) ));
409
410
411 float laneShape = smoothstep(0.25, 0.28, roadCoords.s) * (1.0-smoothstep(0.42, 0.45, roadCoords.s));
412 laneShape += smoothstep(0.65, 0.68, roadCoords.s) * (1.0-smoothstep(0.82, 0.85, roadCoords.s));
413 cShape *= laneShape;
414
415 //texel.rgb = mix(texel.rgb, cColor, cPresent * cShape);
416 texel.rgb = mix(texel.rgb, carTexel.rgb, cPresent * carTexel.a * laneShape);
417 //texel.rgb = mix(texel.rgb, vec3 (1.0, 0.0, 0.0) * cColorRnd2, 0.3);
418 }
419
420
421
422 /// END procedural cars
423
424 vec3 reflVecN;
425
426 ///BEGIN bump
427 if (nmap_enabled > 0){
428 N = nmap.rgb * 2.0 - 1.0;
429 // this is exact only for viewing under 90 degrees but much faster than the real solution
430 reflVecN = normalize (N.x * VTangent + N.y * VBinormal + N.z * reflVec);
431 N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
432 if (nmap_dds > 0)
433 N = -N;
434 } else {
435 N = normalize(VNormal);
436 reflVecN = reflVec;
437 }
438 ///END bump
439
440
441
442 vec4 reflection = textureCube(Environment, reflVecN );
443 vec3 viewVec = normalize(vViewVec);
444 float v = abs(dot(viewVec, normalize(VNormal)));// Map a rainbowish color
445 vec4 fresnel = texture2D(ReflGradientsTex, vec2(v, 0.75));
446 vec4 rainbow = texture2D(ReflGradientsTex, vec2(v, 0.25));
447
448 //float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz)));
449
450
451 float nDotVP = max(0.0, dot(N, normalize(gl_LightSource[0].position.xyz)));
452
453 //float nDotHV = max(0.0, dot(N, normalize(gl_LightSource[0].halfVector.xyz)));
454 float nDotHV = max(0.0, dot(N,HV));
455 //glare on the backside of tranparent objects
456 //if ((gl_FrontMaterial.diffuse.a < 1.0 || texel.a < 1.0)
457 // && dot(N, normalize(gl_LightSource[0].position.xyz)) < 0.0) {
458 // nDotVP = max(0.0, dot(-N, normalize(gl_LightSource[0].position.xyz)) * (1.0 -texel.a) );
459 // nDotHV = max(0.0, dot(-N, HV) * (1.0 -texel.a) );
460 // }
461
462 float nDotVP1 = 0.0;
463 float nDotHV1 = 0.0;
464
465
466 // try specular reflection of sky irradiance
467 nDotVP1 = max(0.0, dot(N, up));
468 nDotHV1 = max(0.0, dot(N, normalize(normalize(up) + normalize(-vertVec))));
469
470
471 if (nDotVP == 0.0)
472 {pf = 0.0;}
473 else
474 {pf = pow(nDotHV, gl_FrontMaterial.shininess);}
475
476 if (nDotVP1 == 0.0)
477 {pf1 = 0.0;}
478 else
479 {pf1 = pow(nDotHV1, 0.5*gl_FrontMaterial.shininess);}
480
481
482
483 if (cloud_shadow_flag == 1)
484 {
485 light_diffuse = light_diffuse * shadow_func(relPos.x, relPos.y, 1.0, dist);
486 }
487
488 vec3 secondary_light = vec3 (0.0,0.0,0.0);
489
490 if (use_searchlight == 1)
491 {
492 secondary_light += searchlight();
493 }
494 if (use_landing_light == 1)
495 {
496 secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
497 }
498 if (use_alt_landing_light == 1)
499 {
500 secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
501 }
502
503 float shadowmap = getShadowing();
504 vec4 Diffuse = light_diffuse * nDotVP * shadowmap;
505 Diffuse.rgb += secondary_light * light_distance_fading(dist);
506 if (use_IR_vision)
507 {
508 Diffuse.rgb = max(Diffuse.rgb, vec3 (0.5, 0.5, 0.5));
509 }
510 vec4 Specular = gl_FrontMaterial.specular * light_diffuse * pf + gl_FrontMaterial.specular * light_ambient * pf1 * shadowmap;
511 Specular+= gl_FrontMaterial.specular * pow(max(0.0,-dot(N,nVertVec)),gl_FrontMaterial.shininess) * vec4(secondary_light,1.0);
512
513
514 //vec4 color = gl_Color + Diffuse * gl_FrontMaterial.diffuse;
515 vec4 color = Diffuse;// * gl_FrontMaterial.diffuse;
516
517
518 color = clamp( color, 0.0, 1.0 );
519
520
521
522 ////////////////////////////////////////////////////////////////////
523 //BEGIN reflect
524 ////////////////////////////////////////////////////////////////////
525 if (refl_enabled > 0){
526 float reflFactor = 0.0;
527 float transparency_offset = clamp(refl_correction, -1.0, 1.0);// set the user shininess offset
528
529 if(refl_map > 0){
530 // map the shininess of the object with user input
531 //float pam = (map.a * -2) + 1; //reverse map
532 reflFactor = reflmap.a + transparency_offset;
533 } else if (nmap_enabled > 0) {
534 // set the reflectivity proportional to shininess with user input
535 reflFactor = gl_FrontMaterial.shininess * 0.0078125 * nmap.a + transparency_offset;
536 } else {
537 reflFactor = gl_FrontMaterial.shininess* 0.0078125 + transparency_offset;
538 }
539
540 // enhance low angle reflection by a fresnel term
541 float fresnel_enhance = (1.0-smoothstep(0.0,0.4, dot(N,-nVertVec))) * refl_fresnel_factor;
542
543 reflFactor+=fresnel_enhance;
544
545 reflFactor = clamp(reflFactor, 0.0, 1.0);
546
547 // add fringing fresnel and rainbow effects and modulate by reflection
548 vec4 reflcolor = mix(reflection, rainbow, refl_rainbow * v);
549 //vec4 reflcolor = reflection;
550 vec4 reflfrescolor = mix(reflcolor, fresnel, refl_fresnel * v);
551 vec4 noisecolor = mix(reflfrescolor, noisevec, refl_noise);
552 //vec4 raincolor = vec4(noisecolor.rgb * reflFactor, 1.0);
553 vec4 raincolor = vec4(noisecolor.rgb, 1.0);
554 raincolor += Specular;
555 raincolor *= light_diffuse;
556
557 if (refl_type == 1)
558 {mixedcolor = mix(texel, raincolor, reflFactor).rgb;}
559 else if (refl_type == 2)
560 {mixedcolor = ((texel +(reflcolor * reflFactor))-(0.5*reflFactor)).rgb;}
561
562 } else {
563 mixedcolor = texel.rgb;
564 }
565 /////////////////////////////////////////////////////////////////////
566 //END reflect
567 /////////////////////////////////////////////////////////////////////
568
569 //////////////////////////////////////////////////////////////////////
570 //begin DIRT
571 //////////////////////////////////////////////////////////////////////
572 if (dirt_enabled >= 1){
573 vec3 dirtFactorIn = vec3 (dirt_r_factor, dirt_g_factor, dirt_b_factor);
574 vec3 dirtFactor = reflmap.rgb * dirtFactorIn.rgb;
575 //dirtFactor.r = smoothstep(0.0, 1.0, dirtFactor.r);
576 mixedcolor.rgb = mix(mixedcolor.rgb, dirt_r_color, smoothstep(0.0, 1.0, dirtFactor.r));
577 if (dirt_multi > 0) {
578 //dirtFactor.g = smoothstep(0.0, 1.0, dirtFactor.g);
579 //dirtFactor.b = smoothstep(0.0, 1.0, dirtFactor.b);
580 mixedcolor.rgb = mix(mixedcolor.rgb, dirt_g_color, smoothstep(0.0, 1.0, dirtFactor.g));
581 mixedcolor.rgb = mix(mixedcolor.rgb, dirt_b_color, smoothstep(0.0, 1.0, dirtFactor.b));
582 }
583 }
584 //////////////////////////////////////////////////////////////////////
585 //END Dirt
586 //////////////////////////////////////////////////////////////////////
587
588 //////////////////////////////////////////////////////////////////////
589 //begin WETNESS
590 //////////////////////////////////////////////////////////////////////
591
592 if (rain_enabled >0.0)
593 {
594 texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
595 float rain_factor = 0.0;
596
597 float rain_orientation = max(dot(VNormal, up),0.0);
598
599 if ((rain_norm > 0.0) && (rain_orientation > 0.0))
600 {
601 rain_factor += DotNoise2D(rawpos.xy, 0.2 ,0.5, rain_norm) * abs(sin(6.0*osg_SimulationTime));
602 rain_factor += DotNoise2D(rawpos.xy, 0.3 ,0.4, rain_norm) * abs(sin(6.0*osg_SimulationTime + 2.094));
603 rain_factor += DotNoise2D(rawpos.xy, 0.4 ,0.3, rain_norm)* abs(sin(6.0*osg_SimulationTime + 4.188));
604 }
605
606
607
608 // secondary reflection of sky irradiance in water film
609 float fresnelW = ((0.8 * wetness) ) * (1.0-smoothstep(0.0,0.4, dot(N,-nVertVec) * 1.0 - 0.2 * rain_factor * wetness));
610 float sky_factor = (1.0-ct*ct);
611 vec3 sky_light = vec3 (1.0,1.0,1.0) * length(light_diffuse.rgb) * (1.0-effective_scattering);
612 Specular.rgb += sky_factor * fresnelW * sky_light;
613 }
614 /////////////////////////////////////////////////////////////////////
615 //end WETNESS
616 //////////////////////////////////////////////////////////////////////
617
618
619 // set ambient adjustment to remove bluiness with user input
620 float ambient_offset = clamp(amb_correction, -1.0, 1.0);
621 vec4 ambient = gl_LightModel.ambient + light_ambient;
622 vec4 ambient_Correction = vec4(ambient.rg, ambient.b * 0.6, 1.0)
623 * ambient_offset ;
624 ambient_Correction = clamp(ambient_Correction, -1.0, 1.0);
625
626 color += ambient;
627 color.a = texel.a * alpha;
628 vec4 fragColor = vec4(color.rgb * mixedcolor + ambient_Correction.rgb, color.a);
629
630 fragColor += Specular * nmap.a;
631
632 fragColor.rgb += getClusteredLightsContribution(vertVec, N, texel.rgb);
633
634 //////////////////////////////////////////////////////////////////////
635 // BEGIN procedural lightmap
636 //////////////////////////////////////////////////////////////////////
637
638
639
640
641
642
643
644
645 vec3 pLMColor = streetlight_color;
646
647 float pLMIntensity = smoothstep(0.0, 0.4, roadCoords.s) * (1.0 - smoothstep(0.6, 1.0, roadCoords.s));
648 pLMIntensity = 0.25 * rtype_base_illumination * (1.0+ streetlight_factor) + 0.1 * max(0.0,sin(4.0 * roadCoords.t)) * streetlight_factor;
649 pLMIntensity = clamp(pLMIntensity, 0.0, 1.0);
650
651 if (gl_FrontMaterial.diffuse.r == 0.0) {pLMIntensity =0.0;}
652
653 pLMColor *= pLMIntensity;
654
655 if (road_traffic_enabled == 1)
656 {
657
658 float viewAngleFactor = smoothstep(-0.05, 0.0, cSign * dot(normalize(VBinormal), nVertVec));
659
660 vec3 pCLColor = vec3 (0.95, 1.0, 1.0);
661 vec3 pTLColor = vec3 (0.95, 0.0, 0.0);
662
663
664 // mean illumination by car headlights
665 pLMColor = pLMColor + 0.2 * min(1.0,total_traffic_density) * pCLColor;
666
667 //float pCLIntensity = smoothstep(0.4, 0.6, cTag) * (1.0-smoothstep(0.6, 0.8, cTag));
668 //float laneFact = smoothstep(0.25, 0.3, roadCoords.s) * (1.0-smoothstep(0.3, 0.35, roadCoords.s));
669 //laneFact += smoothstep(0.35, 0.4, roadCoords.s) * (1.0-smoothstep(0.4, 0.45, roadCoords.s));
670 //laneFact += smoothstep(0.65, 0.7, roadCoords.s) * (1.0-smoothstep(0.7, 0.75, roadCoords.s));
671 //laneFact += smoothstep(0.75, 0.8, roadCoords.s) * (1.0-smoothstep(0.8, 0.85, roadCoords.s));
672 //pCLIntensity = pCLIntensity * laneFact * cPresent;
673
674
675 float pCLIntensity, pTLIntensity;
676 if (cSign == -1.0)
677 {
678 pCLIntensity = carMetaTexel.r * cPresent * smoothstep(0.45, 0.55, cTag) * viewAngleFactor ;
679 pCLIntensity += 0.85 * carMetaTexel.b * cPresent * smoothstep(0.45, 0.55, cTag);
680 pTLIntensity = carMetaTexel.r * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag)) * (1.0 - viewAngleFactor);
681 pTLIntensity += 0.85 * carMetaTexel.b * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag));
682
683 }
684 else
685 {
686 pCLIntensity = carMetaTexel.r * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag)) *viewAngleFactor ;
687 pCLIntensity += 0.85 * carMetaTexel.b * cPresent * (1.0 - smoothstep(0.45, 0.55, cTag));
688 pTLIntensity = carMetaTexel.r * cPresent * smoothstep(0.45, 0.55, cTag) * (1.0 - viewAngleFactor);
689 pTLIntensity += 0.85 * carMetaTexel.b * cPresent * smoothstep(0.45, 0.55, cTag);
690 }
691
692 //pCLColor *= pCLIntensity;
693
694
695
696 //if (cSign == 1.0)
697 // {
698 // pTLIntensity = smoothstep(0.9, 0.94, cTag) * (1.0-smoothstep(0.96, 1.0, cTag));
699 // }
700 //else
701 // {
702 // pTLIntensity = smoothstep(0.0, 0.04, cTag) * (1.0-smoothstep(0.06, 0.1, cTag));
703 // }
704
705
706
707 //pTLIntensity = pTLIntensity * laneFact * cPresent * (1.0 - viewAngleFactor);
708
709
710 pCLColor = pCLColor * pCLIntensity + pTLColor * pTLIntensity;
711
712
713 pLMColor = clamp(pLMColor, 0.0, 1.0);
714
715 pLMColor = max(pLMColor,pCLColor);
716 }
717
718 //fragColor.rgb = max(fragColor.rgb, pLMColor * gl_FrontMaterial.diffuse.rgb * smoothstep(0.0, 1.0, mixedcolor*.5 + pLMColor*.5));
719
720 fragColor.rgb = max(fragColor.rgb, pLMColor * smoothstep(0.0, 1.0, mixedcolor*.5 + pLMColor*.5));
721
722
723
724 //////////////////////////////////////////////////////////////////////
725 // END procedural lightmap
726 //////////////////////////////////////////////////////////////////////
727
728
729 /// BEGIN fog amount
730
731 float transmission;
732 float vAltitude;
733 float delta_zv;
734 float H;
735 float distance_in_layer;
736 float transmission_arg;
737 float eqColorFactor;
738
739 float delta_z = hazeLayerAltitude - eye_alt;
740 float mvisibility = min(visibility, avisibility);
741
742 if (dist > 0.04 * mvisibility)
743 {
744 if (delta_z > 0.0) // we're inside the layer
745 {
746 if (ct < 0.0) // we look down
747 {
748 distance_in_layer = dist;
749 vAltitude = min(distance_in_layer,mvisibility) * ct;
750 delta_zv = delta_z - vAltitude;
751 }
752 else // we may look through upper layer edge
753 {
754 H = dist * ct;
755 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
756 else {distance_in_layer = dist;}
757 vAltitude = min(distance_in_layer,visibility) * ct;
758 delta_zv = delta_z - vAltitude;
759 }
760 }
761 else // we see the layer from above, delta_z < 0.0
762 {
763 H = dist * -ct;
764 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
765 {
766 distance_in_layer = 0.0;
767 delta_zv = 0.0;
768 }
769 else
770 {
771 vAltitude = H + delta_z;
772 distance_in_layer = vAltitude/H * dist;
773 vAltitude = min(distance_in_layer,visibility) * (-ct);
774 delta_zv = vAltitude;
775 }
776 }
777
778 transmission_arg = (dist-distance_in_layer)/avisibility;
779
780
781 if (visibility < avisibility)
782 {
783 transmission_arg = transmission_arg + (distance_in_layer/visibility);
784 eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
785 }
786 else
787 {
788 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
789 eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
790 }
791 transmission = fog_func(transmission_arg, alt);
792 if (eqColorFactor < 0.2) eqColorFactor = 0.2;
793 }
794 else
795 {
796 eqColorFactor = 1.0;
797 transmission = 1.0;
798 }
799
800 /// END fog amount
801
802 /// BEGIN fog color
803
804 vec3 hazeColor = get_hazeColor(fog_lightArg);
805
806 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
807 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
808
809 if (transmission< 1.0)
810 {
811
812
813
814 if (fog_lightArg < 10.0)
815 {
816 intensity = length(hazeColor);
817 float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
818 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)) );
819 }
820
821 intensity = length(hazeColor);
822 hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, alt)));
823
824 hazeColor.r = hazeColor.r * 0.83;
825 hazeColor.g = hazeColor.g * 0.9;
826
827 float fade_out = max(0.65 - 0.3 *overcast, 0.45);
828 intensity = length(hazeColor);
829 hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,fog_earthShade) ));
830 hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
831
832 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));
833 hazeColor = mix(shadow * hazeColor, hazeColor, 0.3 + 0.7* smoothstep(250000.0, 400000.0, terminator));
834 }
835 else
836 {
837 hazeColor = vec3 (1.0, 1.0, 1.0);
838 }
839
840
841
842
843 /// END fog color
844 fragColor = clamp(fragColor, 0.0, 1.0);
845 hazeColor = clamp(hazeColor, 0.0, 1.0);
846
847 ///BEGIN Rayleigh fog ///
848 // Only compute fog if terrain level is 'Ultra'
849 if ((quality_level > 5) && (tquality_level > 5))
850 {
851 // Rayleigh color shift due to out-scattering
852 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
853 float outscatter = 1.0-exp(-dist/rayleigh_length);
854 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
855
856 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
857 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
858 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
859 }
860 /// END Rayleigh fog
861
862 // don't let the light fade out too rapidly
863 lightArg = (terminator + 200000.0)/100000.0;
864 float minLightIntensity = min(0.2,0.16 * lightArg + 0.5);
865 vec3 minLight = minLightIntensity * vec3 (0.2, 0.3, 0.4);
866 hazeColor *= eqColorFactor * fog_earthShade;
867 hazeColor.rgb = max(hazeColor.rgb, minLight.rgb);
868
869
870 fragColor.rgb = mix(hazeColor +secondary_light * fog_backscatter(mvisibility), fragColor.rgb,transmission);
871
872 fragColor.rgb = filter_combined(fragColor.rgb);
873
874 gl_FragColor = fragColor;
875
876 }