Debian Codegraph @

Shaders/model-interior-display-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 // written by Thorsten Renk, Oct 2017
5
6 varying vec4 diffuse_term;
7 varying vec3 normal;
8 varying vec3 relPos;
9 varying vec4 ecPosition;
10
11
12 uniform sampler2D texture;
13 uniform sampler2D dust_texture;
14 uniform sampler2D lightmap_texture;
15 uniform sampler2D grain_texture;
16 uniform samplerCube cube_texture;
17
18
19 varying float yprime_alt;
20 varying float mie_angle;
21
22
23 uniform float visibility;
24 uniform float avisibility;
25 uniform float scattering;
26 uniform float terminator;
27 uniform float terrain_alt;
28 uniform float hazeLayerAltitude;
29 uniform float overcast;
30 uniform float eye_alt;
31 uniform float cloud_self_shading;
32 uniform float angle;
33 uniform float threshold_low;
34 uniform float threshold_high;
35 uniform float emit_intensity;
36 uniform float light_radius;
37 uniform float dirt_factor;
38 uniform float lightmap_r_factor;
39 uniform float lightmap_g_factor;
40 uniform float lightmap_b_factor;
41 uniform float lightmap_a_factor;
42 uniform float grain_magnification;
43 uniform float sample_res;
44
45
46 uniform float contrast;
47
48 uniform vec3 offset_vec;
49 uniform vec3 scale_vec;
50 uniform vec3 tag_color;
51 uniform vec3 emit_color;
52 uniform vec3 light_filter_one;
53 uniform vec3 light_filter_two;
54 uniform vec3 lightmap_r_color;
55 uniform vec3 lightmap_g_color;
56 uniform vec3 lightmap_b_color;
57 uniform vec3 lightmap_a_color;
58
59
60 uniform int quality_level;
61 uniform int tquality_level;
62 uniform int use_searchlight;
63 uniform int implicit_lightmap_enabled;
64 uniform int use_flashlight;
65 uniform int lightmap_enabled;
66 uniform int lightmap_multi;
67 uniform int grain_texture_enabled;
68
69 uniform bool shadow_mapping_enabled;
70
71
72 const float EarthRadius = 5800000.0;
73 const float terminator_width = 200000.0;
74
75 float alt;
76 float eShade;
77
78
79 float fog_func (in float targ, in float alt);
80 float alt_factor(in float eye_alt, in float vertex_alt);
81 float light_distance_fading(in float dist);
82 float fog_backscatter(in float avisibility);
83
84 vec3 addLights(in vec3 color1, in vec3 color2);
85 vec3 flashlight(in vec3 color, in float radius);
86 vec3 filter_combined (in vec3 color) ;
87
88 float getShadowing();
89 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
90
91 float luminance(vec3 color)
92 {
93 return dot(vec3(0.212671, 0.715160, 0.072169), color);
94 }
95
96
97 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
98 {
99 x = x - 0.5;
100
101 // use the asymptotics to shorten computations
102 if (x > 30.0) {return e;}
103 if (x < -15.0) {return 0.0;}
104
105 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
106 }
107
108 // bloom effect on the display texture
109
110
111 vec4 find_max (in vec4 texel1, in vec4 texel2, in vec4 texel3, in vec4 texel4)
112 {
113
114 float r = max(texel1.r, texel2.r);
115 r = max(r, texel3.r);
116 r = max(r, texel4.r);
117
118 float g = max(texel1.g, texel2.g);
119 g = max(g, texel3.g);
120 g = max(g, texel4.g);
121
122 float b = max(texel1.b, texel2.b);
123 b = max(b, texel3.b);
124 b = max(b, texel4.b);
125
126 float a = max(texel1.a, texel2.a);
127 a = max(a, texel3.a);
128 a = max(a, texel4.a);
129
130 //float a = (texel1.a + texel2.a + texel3.a + texel4.a)/4.0;
131
132 return vec4 (r,g,b,a);
133 }
134
135 vec4 get_blurred_texel (in vec4 direct_light)
136 {
137
138
139
140 float fade0 = smoothstep(0.5, 1.0,contrast);
141 float fade1 = 0.25 * contrast * contrast;
142 float fade2 = 0.25 * contrast * contrast;
143
144 //fade0 = 1.0;
145 //fade1 = 1.0;
146 //fade2 = 1.0;
147
148 float desaturation_brightness = 1.0;
149
150 float sample_far = 2.0;
151 float curr_sample_res = sample_res * contrast;
152
153 vec4 texel = texture2D(texture, gl_TexCoord[0].st);
154
155 vec4 texel1 = texture2D(texture, vec2 (gl_TexCoord[0].s + curr_sample_res, gl_TexCoord[0].t));
156 vec4 texel2 = texture2D(texture, vec2 (gl_TexCoord[0].s - curr_sample_res, gl_TexCoord[0].t));
157 vec4 texel3 = texture2D(texture, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + curr_sample_res));
158 vec4 texel4 = texture2D(texture, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t - curr_sample_res));
159
160 vec4 sample_texel = find_max (texel1, texel2, texel3, texel4);
161
162 texel.rgb += fade0 * sample_texel.rgb;
163
164
165 texel1 = texture2D(texture, vec2 (gl_TexCoord[0].s + sample_far * curr_sample_res, gl_TexCoord[0].t+ sample_far * curr_sample_res));
166 texel2 = texture2D(texture, vec2 (gl_TexCoord[0].s - sample_far * curr_sample_res, gl_TexCoord[0].t+ sample_far * curr_sample_res));
167 texel3 = texture2D(texture, vec2 (gl_TexCoord[0].s + sample_far * curr_sample_res, gl_TexCoord[0].t- sample_far * curr_sample_res));
168 texel4 = texture2D(texture, vec2 (gl_TexCoord[0].s - sample_far * curr_sample_res, gl_TexCoord[0].t- sample_far * curr_sample_res));
169
170 sample_texel = find_max (texel1, texel2, texel3, texel4);
171
172 texel.rgb += fade1 * sample_texel.rgb;
173
174
175
176 texel1 = texture2D(texture, vec2 (gl_TexCoord[0].s + 2.0 * sample_far * curr_sample_res, gl_TexCoord[0].t - curr_sample_res));
177 texel2 = texture2D(texture, vec2 (gl_TexCoord[0].s - 2.0 * sample_far * curr_sample_res, gl_TexCoord[0].t + curr_sample_res));
178 texel3 = texture2D(texture, vec2 (gl_TexCoord[0].s - curr_sample_res, gl_TexCoord[0].t + 2.0 * sample_far * curr_sample_res));
179 texel4 = texture2D(texture, vec2 (gl_TexCoord[0].s + curr_sample_res, gl_TexCoord[0].t - 2.0 * sample_far * curr_sample_res));
180
181 sample_texel = find_max (texel1, texel2, texel3, texel4);
182
183 texel.rgb += fade2 * sample_texel.rgb;
184
185
186 texel1 = texture2D(texture, vec2 (gl_TexCoord[0].s + 2.5 * sample_far * curr_sample_res, gl_TexCoord[0].t + 2.5 * sample_far * curr_sample_res));
187 texel2 = texture2D(texture, vec2 (gl_TexCoord[0].s - 2.5 * sample_far * curr_sample_res, gl_TexCoord[0].t + 2.5 * sample_far * curr_sample_res));
188 texel3 = texture2D(texture, vec2 (gl_TexCoord[0].s + 2.5 * sample_far * curr_sample_res, gl_TexCoord[0].t - 2.5 * sample_far * curr_sample_res));
189 texel4 = texture2D(texture, vec2 (gl_TexCoord[0].s - 2.5 * sample_far * curr_sample_res, gl_TexCoord[0].t - 2.5 * sample_far * curr_sample_res));
190
191 sample_texel = find_max (texel1, texel2, texel3, texel4);
192
193 texel.rgb += fade2 * sample_texel.rgb;
194
195
196
197 texel.rgb/= (1.0 + 1.0 * fade0 + 1.0 * fade1 + 2.0 * fade2);
198 texel.a = smoothstep(0.0, 0.1, length(texel.rgb));
199
200 texel.a *= (1.0 - 0.9 * smoothstep(0.0, 1.0, length(direct_light.rgb)/1.73 * 4.0 *( 0.5 - contrast)) );
201
202 float threshold_high = max(desaturation_brightness, 0.05) * 1.0;
203 float threshold_mid = max(desaturation_brightness, 0.05) * 0.6;
204
205 float pixel_brightness = max(texel.r, texel.g);
206 pixel_brightness = max(pixel_brightness, texel.b);
207 pixel_brightness *= contrast;
208
209
210 texel.rgb = mix(texel.rgb, vec3 (1.0, 1.0, 1.0), 0.9 * smoothstep(threshold_mid, threshold_high, pixel_brightness));
211
212
213 return texel;
214 }
215
216
217 void main()
218 {
219
220 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
221 // this is taken from default.frag
222 vec3 n;
223 float NdotL, NdotHV, fogFactor;
224 vec4 color = gl_Color;
225 vec3 lightDir = gl_LightSource[0].position.xyz;
226 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
227 vec4 texel;
228 vec4 fragColor;
229 vec4 specular = vec4(0.0);
230 float intensity;
231
232 float effective_scattering = min(scattering, cloud_self_shading);
233
234 eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt);
235 vec4 light_specular = gl_LightSource[0].specular * (eShade - 0.1);
236
237 // If gl_Color.a == 0, this is a back-facing polygon and the
238 // normal should be reversed.
239 n = (2.0 * gl_Color.a - 1.0) * normal;
240 n = normalize(n);
241
242 // lookup on the opacity map
243 vec3 light_vec = normalize((gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz);
244 //vec3 light_vec = vec3 (-1.0,0.0,0.0);
245
246 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
247 vec3 scaled_pos = relPos + ep.xyz;
248
249 //vec3 lookup_vec = normalize(- normalize(light_vec) + relPos);
250 scaled_pos -= offset_vec;
251 float rangle = radians(angle);
252 mat2 rotMat = mat2 (cos(rangle), -sin(rangle), sin(rangle), cos(rangle));
253 scaled_pos.xy *=rotMat;
254
255 scaled_pos /= scale_vec;
256
257 //vec3 lookup_pos = dot(base1,scaled_pos) * base1 + dot(base2,scaled_pos) * base2;
258 vec3 lookup_pos = scaled_pos - light_vec * dot(light_vec, scaled_pos);
259
260 vec3 lookup_vec = normalize(normalize(light_vec) + lookup_pos);
261 vec4 opacity = textureCube(cube_texture, lookup_vec);
262
263
264 vec4 diffuse = diffuse_term;
265 NdotL = dot(n, lightDir);
266 //NdotL = dot(n, (gl_ModelViewMatrix * vec4 (light_vec,0.0)).xyz);
267 if (NdotL > 0.0) {
268 float shadowmap = 1.0;
269 if (shadow_mapping_enabled) {
270 shadowmap = getShadowing();
271 }
272 diffuse.rgb += 2.0 * diffuse.rgb * (1.0 - opacity.a);
273 color += diffuse * NdotL * opacity * shadowmap;
274 NdotHV = max(dot(n, halfVector), 0.0);
275 if (gl_FrontMaterial.shininess > 0.0)
276 specular.rgb = (gl_FrontMaterial.specular.rgb
277 * light_specular.rgb
278 * pow(NdotHV, gl_FrontMaterial.shininess)
279 * shadowmap);
280 }
281 color.a = diffuse.a;
282 // This shouldn't be necessary, but our lighting becomes very
283 // saturated. Clamping the color before modulating by the texture
284 // is closer to what the OpenGL fixed function pipeline does.
285 //color = clamp(color, 0.0, 1.0);
286
287 vec3 secondary_light = vec3 (0.0,0.0,0.0);
288
289 if (use_flashlight == 1)
290 {
291 secondary_light.rgb += flashlight(light_filter_one, light_radius);
292 }
293 if (use_flashlight == 2)
294 {
295 secondary_light.rgb += flashlight(light_filter_two, light_radius);
296 }
297 float dist = length(relPos);
298 color.rgb += secondary_light * light_distance_fading(dist);
299
300 //texel = texture2D(texture, gl_TexCoord[0].st);
301
302 texel = get_blurred_texel(diffuse * max(0.0, NdotL) * opacity);
303
304 if (grain_texture_enabled ==1)
305 {
306 vec4 grainTexel = texture2D(grain_texture, gl_TexCoord[0].st * grain_magnification);
307 texel.rgb = mix(texel.rgb, grainTexel.rgb, grainTexel.a );
308 }
309
310
311
312 fragColor = color * texel + specular;
313 fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
314
315 // explicit lightmap
316
317 vec3 lightmapcolor = vec3(0.0, 0.0, 0.0);
318
319
320 if (lightmap_enabled == 1)
321 {
322 vec4 lightmapTexel = texture2D(lightmap_texture, gl_TexCoord[0].st);
323 vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor, lightmap_b_factor, lightmap_a_factor);
324
325
326 if (lightmap_multi > 0 )
327 {
328 lightmapFactor = lightmapFactor * lightmapTexel;
329
330
331 lightmapcolor = lightmap_r_color * lightmapFactor.r;
332 lightmapcolor = addLights(lightmapcolor, lightmap_g_color * lightmapFactor.g);
333 lightmapcolor = addLights(lightmapcolor, lightmap_b_color * lightmapFactor.b);
334 lightmapcolor = addLights(lightmapcolor, lightmap_a_color * lightmapFactor.a);
335
336 }
337 else
338 {
339 lightmapcolor = lightmapTexel.rgb * lightmap_r_color * lightmapFactor.r;
340 }
341
342
343 // implicit lightmap - the user gets to select a color which is then made emissive
344
345 if (implicit_lightmap_enabled == 1)
346 {
347 float cdiff = (length(texel.rgb - tag_color));
348 float enhance = 1.0 - smoothstep(threshold_low, threshold_high, cdiff);
349 enhance *= (1.0 - length(lightmapcolor.rgb)/1.73);
350 fragColor.rgb = fragColor.rgb + enhance * emit_color * emit_intensity;
351 }
352
353
354 fragColor.rgb = max(fragColor.rgb, lightmapcolor.rgb * gl_FrontMaterial.diffuse.rgb * smoothstep(0.0, 1.0, texel.rgb*.5 + lightmapcolor.rgb*.5));
355 }
356
357 // add visible dust for glancing sun angle
358
359 vec4 dustTexel = texture2D(dust_texture, gl_TexCoord[0].st);
360 dustTexel.rgb *= diffuse.rgb;
361 dustTexel.a = clamp(dustTexel.a * dirt_factor * (1.0 - 0.4 * max(0.0,NdotL)) * (length(opacity.rgb)/1.76),0.0,1.0);
362 fragColor.rgb = mix(fragColor.rgb, dustTexel.rgb, dustTexel.a );
363 fragColor.a = max(fragColor.a, dustTexel.a);
364
365
366 fragColor.rgb = filter_combined(fragColor.rgb);
367
368 gl_FragColor = fragColor;
369
370
371
372 }
373