Debian Codegraph @

Shaders/surface-light-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 uniform sampler2D texture;
5
6 uniform float visibility;
7 uniform float avisibility;
8 uniform float hazeLayerAltitude;
9 uniform float eye_alt;
10 uniform float terminator;
11 uniform float size;
12
13 uniform bool use_IR_vision;
14 uniform bool use_night_vision;
15
16 varying vec3 relPos;
17 varying vec2 rawPos;
18 varying float pixelSize;
19
20 float alt;
21
22 float Noise2D(in vec2 coord, in float wavelength);
23
24 float fog_func (in float targ)
25 {
26
27
28 float fade_mix;
29
30 // for large altitude > 30 km, we switch to some component of quadratic distance fading to
31 // create the illusion of improved visibility range
32
33 targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
34
35
36 if (alt < 30000.0)
37 {return exp(-targ - targ * targ * targ * targ);}
38 else if (alt < 50000.0)
39 {
40 fade_mix = (alt - 30000.0)/20000.0;
41 return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
42 }
43 else
44 {
45 return exp(- targ * targ - pow(targ,4.0));
46 }
47
48 }
49
50
51 float light_sprite (in vec2 coord, in float transmission, in float noise)
52 {
53
54 coord.s = coord.s - 0.5;
55 coord.t = coord.t - 0.5;
56
57 float r = length(coord);
58
59 if (pixelSize<1.3) {return 0.08;}
60
61 float angle = noise * 6.2832;
62
63 float sinphi = dot(vec2 (sin(angle),cos(angle)), normalize(coord));
64 float sinterm = sin(mod((sinphi-3.0) * (sinphi-3.0),6.2832));
65 float ray = 0.0;
66 if (sinterm == 0.0)
67 {ray = 0.0;}
68 else
69 //{ray = clamp(pow(sinterm,10.0),0.0,1.0);}
70 {ray = sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm;}
71
72
73 float fogEffect = (1.0-smoothstep(0.4,0.8,transmission));
74 float halo = 0.2 * exp(-10.0 * r * r);
75 float base = exp(-80.0*r*r);
76 ray *= exp(-40.0 * r * r);
77
78
79 float intensity = clamp(ray + base + halo,0.0,1.0) + 0.1 * fogEffect * (1.0-smoothstep(0.3, 0.6,r));
80
81 return intensity;
82
83 }
84
85
86 void main()
87 {
88
89 float dist = length(relPos);
90 float delta_z = hazeLayerAltitude - eye_alt;
91 float transmission;
92 float vAltitude;
93 float delta_zv;
94 float H;
95 float distance_in_layer;
96 float transmission_arg;
97
98 if (use_IR_vision) {discard;}
99
100 // Discard the second and third vertex, which are used for directional lighting
101 if (gl_Color.a == 0.0) {discard;}
102
103 float noise = Noise2D(rawPos.xy ,1.0);
104
105 // angle with horizon
106 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
107
108
109 // we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
110
111 if (delta_z > 0.0) // we're inside the layer
112 {
113 if (ct < 0.0) // we look down
114 {
115 distance_in_layer = dist;
116 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
117 delta_zv = delta_z - vAltitude;
118 }
119 else // we may look through upper layer edge
120 {
121 H = dist * ct;
122 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
123 else {distance_in_layer = dist;}
124 vAltitude = min(distance_in_layer,visibility) * ct;
125 delta_zv = delta_z - vAltitude;
126 }
127 }
128 else // we see the layer from above, delta_z < 0.0
129 {
130 H = dist * -ct;
131 if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
132 {
133 distance_in_layer = 0.0;
134 delta_zv = 0.0;
135 }
136 else
137 {
138 vAltitude = H + delta_z;
139 distance_in_layer = vAltitude/H * dist;
140 vAltitude = min(distance_in_layer,visibility) * (-ct);
141 delta_zv = vAltitude;
142 }
143 }
144
145
146 // ground haze cannot be thinner than aloft visibility in the model,
147 // so we need to use aloft visibility otherwise
148
149
150 transmission_arg = (dist-distance_in_layer)/avisibility;
151 if (visibility < avisibility)
152 {
153 transmission_arg = transmission_arg + (distance_in_layer/visibility);
154 }
155 else
156 {
157 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
158 }
159
160
161
162 transmission = fog_func(transmission_arg);
163 float lightArg = terminator/100000.0;
164 float attenuationScale = 1.0 + 20.0 * (1.0 -smoothstep(-15.0, 0.0, lightArg));
165 float dist_att = exp(-dist/200.0/size/attenuationScale);
166
167 float intensity = light_sprite(gl_TexCoord[0].st,transmission, noise);
168 vec3 light_color = gl_Color.rgb;
169
170 if (use_night_vision)
171 {light_color.rgb = vec3 (0.0, 1.0, 0.0);}
172
173 light_color = mix(light_color, vec3 (1.0, 1.0, 1.0), 0.5 * intensity * intensity);
174
175 gl_FragColor = vec4 (clamp(light_color.rgb,0.0,1.0), intensity * transmission * dist_att);
176
177
178 }