Debian Codegraph @

Shaders/scenery-lights.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // Shader that takes a list of GL_POINTS and draws a light (point-sprite like
5 // texture, more accurately a light halo) at the given point. This shader
6 // provides support for light animations like blinking, time period handling
7 // for lights on only during night time or in low visiblity and directional
8 // lighting.
9 //
10 // The actual rendering code is heavily based on an existing implementation
11 // found at:
12 // FGData commit 9355d464c175bd5d51ba32527180ed4e94e86fbb
13 // Shaders/surface-lights-ALS.frag
14 // with minor modifications for readability and tuning.
15 //
16 // Licence: GPL v2+
17 // Written by Fahim Dalvi, January 2021
18
19 uniform sampler2D texture;
20
21 uniform float visibility;
22 uniform float avisibility;
23 uniform float hazeLayerAltitude;
24 uniform float eye_alt;
25 uniform float terminator;
26
27 uniform bool use_IR_vision;
28 uniform bool use_night_vision;
29
30 varying vec3 relativePosition;
31 varying vec2 rawPosition;
32 varying float apparentSize;
33 varying float haloSize;
34 varying float lightSize;
35 varying float lightIntensity;
36
37 float alt;
38
39 float Noise2D(in vec2 coord, in float wavelength);
40
41 float fog_func (in float targ)
42 {
43 float fade_mix;
44
45 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
46 // create the illusion of improved visibility range
47 targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
48 if (alt < 30000.0) {
49 return exp(-targ - targ * targ * targ * targ);
50 } else if (alt < 50000.0) {
51 fade_mix = (alt - 30000.0)/20000.0;
52 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
53 } else {
54 return exp(- targ * targ - pow(targ,4.0));
55 }
56 }
57
58
59 float light_sprite (in vec2 coord, in float transmission, in float noise)
60 {
61 // Center the texture coordinates at (0,0)
62 coord.s = coord.s - 0.5;
63 coord.t = coord.t - 0.5;
64
65 // Radius of the current pixel from the center of the light ranging from 0 to 1
66 float r = length(coord) * 2;
67
68 // If the light is too small, return constant intensity
69 if (apparentSize<1.3) {return 0.08;}
70
71 // Calculate the rays (star-shaped structure) around the light
72 // These are randomized for every light based on `noise`
73 float angle = noise * 6.2832;
74 float sinphi = dot(vec2 (sin(angle),cos(angle)), normalize(coord));
75 float sinterm = sin(mod((sinphi-3.0) * (sinphi-3.0),6.2832));
76 float ray = 0.0;
77 if (sinterm == 0.0) {
78 ray = 0.0;
79 } else {
80 ray = sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm;
81 }
82 ray *= 0.2 * exp(-4 * pow(r, 2.5));
83
84 float fogEffect = (1.0-smoothstep(0.4, 0.8, transmission));
85 float halo = 0.2 * exp(-4.0 * pow(r, 2.5));
86 float base = exp(-4 * pow(r * haloSize, 2.5));
87
88 // Combine:
89 // base: the central disc of the light
90 // halo: the faint discs around the light
91 // ray: star-like structures around the disk
92 float intensity = clamp(ray + base + halo, 0.0, 1.0) + 0.1 * fogEffect * (1.0-smoothstep(0.3, 0.6, r));
93
94 return intensity;
95 }
96
97
98 void main()
99 {
100 float dist = length(relativePosition);
101 float delta_z = hazeLayerAltitude - eye_alt;
102 float transmission;
103 float vAltitude;
104 float delta_zv;
105 float H;
106 float distance_in_layer;
107 float transmission_arg;
108
109 if (use_IR_vision) {discard;}
110
111 float noise = Noise2D(rawPosition.xy ,1.0);
112
113 // angle with horizon
114 float ct = dot(vec3(0.0, 0.0, 1.0), relativePosition)/dist;
115
116 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
117 if (delta_z > 0.0) // we're inside the layer
118 {
119 if (ct < 0.0) {
120 // we look down
121 distance_in_layer = dist;
122 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
123 delta_zv = delta_z - vAltitude;
124 } else {
125 // we may look through upper layer edge
126 H = dist * ct;
127 if (H > delta_z) {
128 distance_in_layer = dist/H * delta_z;
129 } else {
130 distance_in_layer = dist;
131 }
132 vAltitude = min(distance_in_layer,visibility) * ct;
133 delta_zv = delta_z - vAltitude;
134 }
135 } else {
136 // we see the layer from above, delta_z < 0.0
137 H = dist * -ct;
138 if (H < (-delta_z)) {
139 // we don't see into the layer at all, aloft visibility is the only fading
140 distance_in_layer = 0.0;
141 delta_zv = 0.0;
142 } else {
143 vAltitude = H + delta_z;
144 distance_in_layer = vAltitude/H * dist;
145 vAltitude = min(distance_in_layer,visibility) * (-ct);
146 delta_zv = vAltitude;
147 }
148 }
149
150 // ground haze cannot be thinner than aloft visibility in the model,
151 // so we need to use aloft visibility otherwise
152
153 transmission_arg = (dist-distance_in_layer)/avisibility;
154 if (visibility < avisibility) {
155 transmission_arg = transmission_arg + (distance_in_layer/visibility);
156 } else {
157 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
158 }
159
160 transmission = fog_func(transmission_arg);
161 float lightArg = terminator/100000.0;
162 float attenuationScale = 1.0 + 20.0 * (1.0 -smoothstep(-15.0, 0.0, lightArg));
163 float dist_att = exp(-dist/200.0/lightSize/attenuationScale);
164
165 float intensity = light_sprite(gl_TexCoord[0].st, transmission, noise);
166 vec3 light_color = gl_Color.rgb;
167
168 if (use_night_vision) {
169 light_color.rgb = vec3 (0.0, 1.0, 0.0);
170 }
171
172 light_color = mix(light_color, vec3 (1.0, 1.0, 1.0), 0.5 * intensity * intensity);
173 gl_FragColor = vec4 (clamp(light_color.rgb, 0.0, 1.0), intensity * transmission * dist_att);
174 }