Debian Codegraph @

Shaders/hud-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 varying vec2 rawPos;
5 varying vec2 nPos;
6 varying vec3 vertPos;
7 varying vec3 normal;
8 varying vec3 refl_vec;
9 varying vec3 light_diffuse;
10 varying float splash_angle;
11 varying float Mie;
12 varying float ambient_fraction;
13
14 uniform sampler2D texture;
15 uniform sampler2D frost_texture;
16 uniform sampler2D func_texture;
17 uniform samplerCube cube_texture;
18 uniform samplerCube cube_light_texture;
19
20 uniform vec4 tint;
21 uniform vec3 overlay_color;
22
23
24 uniform float rain_norm;
25 uniform float ground_splash_norm;
26 uniform float frost_level;
27 uniform float fog_level;
28 uniform float reflection_strength;
29 uniform float overlay_alpha;
30 uniform float overlay_glare;
31 uniform float splash_x;
32 uniform float splash_y;
33 uniform float splash_z;
34 uniform float lightmap_r_factor;
35 uniform float lightmap_g_factor;
36 uniform float lightmap_b_factor;
37 uniform float lightmap_a_factor;
38 uniform float osg_SimulationTime;
39
40 uniform float sample_res;
41 uniform float sample_far;
42 uniform float hud_brightness;
43
44 uniform int use_reflection;
45 uniform int use_reflection_lightmap;
46 uniform int use_mask;
47 uniform int use_wipers;
48 uniform int use_overlay;
49 uniform int adaptive_mapping;
50 uniform int lightmap_multi;
51
52 uniform vec3 lightmap_r_color;
53 uniform vec3 lightmap_g_color;
54 uniform vec3 lightmap_b_color;
55 uniform vec3 lightmap_a_color;
56
57 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
58 float DropletNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
59 float Noise2D(in vec2 coord, in float wavelength);
60 vec3 filter_combined (in vec3 color) ;
61
62 void main()
63 {
64
65 vec4 texel;
66
67
68
69 vec4 frost_texel;
70 vec4 func_texel;
71
72
73 texel = texture2D(texture, gl_TexCoord[0].st);
74
75 texel+= texture2D(texture, vec2 (gl_TexCoord[0].s + sample_res, gl_TexCoord[0].t));
76 texel+= texture2D(texture, vec2 (gl_TexCoord[0].s - sample_res, gl_TexCoord[0].t));
77 texel+= texture2D(texture, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + sample_res));
78 texel+= texture2D(texture, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t - sample_res));
79
80 texel+= 0.75* texture2D(texture, vec2 (gl_TexCoord[0].s + sample_far * sample_res, gl_TexCoord[0].t+ sample_far * sample_res));
81 texel+= 0.75* texture2D(texture, vec2 (gl_TexCoord[0].s - sample_far * sample_res, gl_TexCoord[0].t+ sample_far * sample_res));
82 texel+= 0.75* texture2D(texture, vec2 (gl_TexCoord[0].s + sample_far * sample_res, gl_TexCoord[0].t- sample_far * sample_res));
83 texel+= 0.75* texture2D(texture, vec2 (gl_TexCoord[0].s - sample_far * sample_res, gl_TexCoord[0].t- sample_far * sample_res));
84
85 texel+= 0.5 * texture2D(texture, vec2 (gl_TexCoord[0].s + 2.0 * sample_far * sample_res, gl_TexCoord[0].t - sample_res));
86 texel+= 0.5 * texture2D(texture, vec2 (gl_TexCoord[0].s - 2.0 * sample_far * sample_res, gl_TexCoord[0].t + sample_res));
87 texel+= 0.5 * texture2D(texture, vec2 (gl_TexCoord[0].s - sample_res, gl_TexCoord[0].t + 2.0 * sample_far * sample_res));
88 texel+= 0.5 * texture2D(texture, vec2 (gl_TexCoord[0].s + sample_res, gl_TexCoord[0].t - 2.0 * sample_far * sample_res));
89
90 texel/=10.0;
91
92 float threshold_high = max(hud_brightness, 0.05) * 0.7;
93 float threshold_low = max(hud_brightness, 0.05) * 0.4;
94 float threshold_mid = max(hud_brightness, 0.05) * 0.5;
95
96 texel.rgb = mix(texel.rgb, vec3 (1.0, 1.0, 1.0), smoothstep(threshold_mid, threshold_high, texel.a));
97 texel.rgb = mix(texel.rgb, vec3 (0.0, 0.0, 0.0), 1.0 - smoothstep(0.0, threshold_low, texel.a));
98
99 texel *=gl_Color;
100
101 vec2 frost_coords;
102
103 if (adaptive_mapping == 1) {frost_coords = gl_TexCoord[0].st * 7.0;}
104 else if (adaptive_mapping ==2) {frost_coords = nPos * 7.0;}
105 else {frost_coords = vertPos.xy * 7.0;}
106
107 frost_texel = texture2D(frost_texture, frost_coords);
108 func_texel = texture2D(func_texture, gl_TexCoord[0].st * 2.0);
109
110
111
112 float noise_003m = Noise2D(vertPos.xy, 0.03);
113 float noise_0003m = Noise2D(vertPos.xy, 0.003);
114
115
116 // environment reflection, including a lightmap for the reflections
117
118 vec4 reflection = textureCube(cube_texture, refl_vec);
119 vec4 reflection_lighting = textureCube(cube_light_texture, refl_vec);
120
121 vec3 lightmapcolor = vec3(0.0, 0.0, 0.0);
122
123
124 if (use_reflection_lightmap == 1)
125 {
126 vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor, lightmap_b_factor, lightmap_a_factor);
127
128 if (lightmap_multi > 0 )
129 {
130 lightmapFactor = lightmapFactor * reflection_lighting;
131 lightmapcolor = lightmap_r_color * lightmapFactor.r +
132 lightmap_g_color * lightmapFactor.g +
133 lightmap_b_color * lightmapFactor.b +
134 lightmap_a_color * lightmapFactor.a ;
135 }
136 else
137 {
138 lightmapcolor = reflection_lighting.rgb * lightmap_r_color * lightmapFactor.r;
139 }
140
141 }
142
143 float lightmap_intensity = length(lightmapcolor);
144 float light_fraction = clamp(lightmap_intensity / (length(light_diffuse.rgb) + 0.01), 0.0, 5.0);
145
146 if (light_fraction < 1.0) {light_fraction = smoothstep(0.7, 1.0, light_fraction);}
147
148
149 if (use_reflection ==1)
150 {
151 // to determine whether what we see reflected is currently in light, we make the somewhat drastic
152 // assumption that its normal will be opposite to the glass normal
153 // (which is mostly truish in a normal cockpit)
154 float reflection_shade = ambient_fraction + (1.0-ambient_fraction) * max(0.0, dot (normalize(normal), normalize(gl_LightSource[0].position.xyz)));
155
156 texel.rgb = mix(texel.rgb, reflection.rgb, (reflection_strength * reflection_shade * (1.0-Mie)));
157
158 }
159
160 //texel.rgb = mix(texel.rgb, lightmapcolor.rgb, lightmap_intensity);
161
162 // overlay pattern
163
164 if ((use_mask == 1) && (use_overlay==1))
165 {
166 vec4 overlay_texel = vec4(overlay_color, overlay_alpha);
167 overlay_texel.rgb *= light_diffuse.rgb* (1.0 + (1.0 + overlay_glare)*Mie);
168 overlay_texel.a *=(1.0 + overlay_glare* Mie);
169 texel = mix(texel, overlay_texel, func_texel.b * overlay_texel.a);
170 }
171
172
173 // frost
174
175 float fth = (1.0-frost_level) * 0.4 + 0.3;
176 float fbl = 0.2 * frost_level;
177
178
179 float frost_factor = (fbl + (1.0-fbl)* smoothstep(fth,fth+0.2,noise_003m)) * (4.0 + 4.0* Mie);
180
181
182 float background_frost = 0.5 * smoothstep(0.7,1.0,frost_level);
183 frost_texel.rgb = mix(frost_texel.rgb, vec3 (0.5,0.5,0.5), (1.0- smoothstep(0.0,0.02,frost_texel.a)));
184 frost_texel.a =max(frost_texel.a, background_frost * (1.0- smoothstep(0.0,0.02,frost_texel.a)));
185
186 frost_texel *= vec4(light_diffuse.rgb,0.5) * (1.0 + 3.0 * Mie);
187
188 frost_factor = max(frost_factor, 0.8*background_frost);
189
190
191 texel.rgb = mix(texel.rgb, frost_texel.rgb, frost_texel.a * frost_factor * smoothstep(0.0,0.1,frost_level));
192 texel.a = max(texel.a, frost_texel.a * frost_level);
193
194 // rain splashes
195
196 vec3 splash_vec = vec3 (splash_x, splash_y, splash_z);
197 float splash_speed = length(splash_vec);
198
199
200 float rain_factor = 0.0;
201
202 float rnorm = max(rain_norm, ground_splash_norm);
203
204 if (rnorm > 0.0)
205 {
206 float droplet_size = (0.5 + 0.8 * rnorm) * (1.0 - 0.1 * splash_speed);
207 vec2 rainPos = vec2 (rawPos.x * splash_speed, rawPos.y / splash_speed );
208 rainPos.y = rainPos.y - 0.1 * smoothstep(1.0,2.0, splash_speed) * osg_SimulationTime;
209 if (splash_angle> 0.0)
210 {
211 // the dynamically impacting raindrops
212
213 float time_shape = 1.0;
214 float base_rate = 6.0 + 3.0 * rnorm + 4.0 * (splash_speed - 1.0);
215 float base_density = 0.6 * rnorm + 0.4 * (splash_speed -1.0);
216 if ((use_mask ==1)&&(use_wipers==1)) {base_density *= (1.0 - 0.5 * func_texel.g);}
217
218 float time_fact1 = (sin(base_rate*osg_SimulationTime));
219 float time_fact2 = (sin(base_rate*osg_SimulationTime + 1.570));
220 float time_fact3 = (sin(base_rate*osg_SimulationTime + 3.1415));
221 float time_fact4 = (sin(base_rate*osg_SimulationTime + 4.712));
222
223 time_fact1 = smoothstep(0.0,1.0, time_fact1);
224 time_fact2 = smoothstep(0.0,1.0, time_fact2);
225 time_fact3 = smoothstep(0.0,1.0, time_fact3);
226 time_fact4 = smoothstep(0.0,1.0, time_fact4);
227
228 rain_factor += DotNoise2D(rawPos.xy, 0.02 * droplet_size ,0.5, base_density ) * time_fact1;
229 rain_factor += DotNoise2D(rainPos.xy, 0.03 * droplet_size,0.4, base_density) * time_fact2;
230 rain_factor += DotNoise2D(rawPos.xy, 0.04 * droplet_size ,0.3, base_density)* time_fact3;
231 rain_factor += DotNoise2D(rainPos.xy, 0.05 * droplet_size ,0.25, base_density)* time_fact4;
232 }
233
234
235 // the static pattern of small droplets created by the splashes
236
237 float sweep = min(1./splash_speed,1.0);
238 if ((use_mask ==1)&&(use_wipers==1)) {sweep *= (1.0 - func_texel.g);}
239 if (adaptive_mapping ==2) {rainPos = nPos;}
240 rain_factor += DropletNoise2D(rainPos.xy, 0.02 * droplet_size ,0.5, 0.6* rnorm * sweep);
241 rain_factor += DotNoise2D(rainPos.xy, 0.012 * droplet_size ,0.7, 0.6* rnorm * sweep);
242 }
243
244 rain_factor = smoothstep(0.1,0.2, rain_factor) * (1.0 - smoothstep(0.4,1.0, rain_factor) * (0.2+0.8*noise_0003m));
245
246
247 vec4 rainColor = vec4 (0.2,0.2, 0.2, 0.6 - 0.3 * smoothstep(1.0,2.0, splash_speed));
248 rainColor.rgb *= length(light_diffuse)/1.73;
249
250
251
252 // glass tint
253
254
255 vec4 outerColor = mix(texel, rainColor, rain_factor);
256 // now mix illuminated reflections in
257
258 vec3 reflLitColor = reflection.rgb * lightmapcolor.rgb;
259
260 outerColor.rgb = mix(outerColor.rgb, reflLitColor, clamp(reflection_strength * light_fraction,0.0,1.0));
261 outerColor.a = max(outerColor.a, 0.1 * light_fraction * reflection_strength);
262
263 outerColor *= tint;
264
265
266
267
268 // fogging - this is inside the glass
269
270 vec4 fog_texel = vec4 (0.6,0.6,0.6, fog_level);
271
272 if (use_mask == 1) {fog_texel.a = fog_texel.a * func_texel.r;}
273
274 fog_texel *= vec4(light_diffuse.rgb,1.0);
275 fog_texel.rgb *= (1.0 + 3.0 * Mie);
276 fog_texel.a *= min((1.0 + 0.5 * Mie), 0.85);
277
278
279 vec4 fragColor;
280
281 fragColor.rgb = mix(outerColor.rgb, fog_texel.rgb, fog_texel.a);
282 fragColor.a = max(outerColor.a, fog_texel.a);
283
284 fragColor.rgb = filter_combined(fragColor.rgb);
285
286
287 gl_FragColor = clamp(fragColor,0.0,1.0);
288
289
290 }