| 1 |
// -*-C++-*- |
| 2 |
|
| 3 |
// This is a library of filter functions |
| 4 |
|
| 5 |
// Thorsten Renk 2016 |
| 6 |
|
| 7 |
#version 120 |
| 8 |
|
| 9 |
uniform float gamma; |
| 10 |
uniform float brightness; |
| 11 |
uniform float delta_T; |
| 12 |
uniform float osg_SimulationTime; |
| 13 |
uniform float fact_grey; |
| 14 |
uniform float fact_black; |
| 15 |
|
| 16 |
uniform bool use_filtering; |
| 17 |
uniform bool use_night_vision; |
| 18 |
uniform bool use_IR_vision; |
| 19 |
|
| 20 |
uniform int display_xsize; |
| 21 |
uniform int display_ysize; |
| 22 |
|
| 23 |
float rand2D(in vec2 co); |
| 24 |
float Noise2D(in vec2 coord, in float wavelength); |
| 25 |
|
| 26 |
vec3 gamma_correction (in vec3 color) { |
| 27 |
|
| 28 |
|
| 29 |
float value = length(color)/1.732; |
| 30 |
return pow(value, gamma) * color; |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
vec3 brightness_adjust (in vec3 color) { |
| 35 |
|
| 36 |
return clamp(brightness * color, 0.0, 1.0); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
vec3 night_vision (in vec3 color) { |
| 41 |
|
| 42 |
float value = length(color)/1.732; |
| 43 |
|
| 44 |
vec2 center = vec2 (float(display_xsize) * 0.5, float(display_ysize) * 0.5); |
| 45 |
float noise = Noise2D( vec2 (gl_FragCoord.x + 100.0 * osg_SimulationTime, gl_FragCoord.y + 300.0 * osg_SimulationTime), 4.0); |
| 46 |
|
| 47 |
float fade = 1.0 - smoothstep( 0.3 * display_ysize, 0.55 * display_ysize, length(gl_FragCoord.xy -center)); |
| 48 |
|
| 49 |
return vec3 (0.0, 1.0, 0.0) * value * (0.5 + 0.5 * noise) * fade; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
vec3 IR_vision (in vec3 color) { |
| 55 |
|
| 56 |
float value = length(color)/1.732; |
| 57 |
value = 1.0 - value; |
| 58 |
|
| 59 |
float T_mapped = smoothstep(-10.0, 10.0, delta_T); |
| 60 |
|
| 61 |
float gain = mix(T_mapped, value, 0.5); |
| 62 |
//float gain = 0.2 * T_mapped + 0.8 * value * T_mapped; |
| 63 |
if (delta_T < -10.0) {gain = 0.0;} |
| 64 |
|
| 65 |
|
| 66 |
return vec3 (0.7, 0.7, 0.7) * gain; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
vec3 moonlight_perception (in vec3 light) { |
| 72 |
|
| 73 |
return light; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
vec3 g_force (in vec3 color) { |
| 79 |
|
| 80 |
vec2 center = vec2 (float(display_xsize) * 0.5, float(display_ysize) * 0.5); |
| 81 |
|
| 82 |
float greyout_band_width = 0.2; |
| 83 |
float blackout_band_width = 0.3; |
| 84 |
|
| 85 |
float f_grey = 1.0 - fact_grey; |
| 86 |
|
| 87 |
float greyout = smoothstep( f_grey * display_ysize, (f_grey + greyout_band_width) * display_ysize, length(gl_FragCoord.xy -center)); |
| 88 |
|
| 89 |
float tgt_brightness = (1.0 - 0.5 * greyout); |
| 90 |
|
| 91 |
float noise = Noise2D( vec2 (gl_FragCoord.x + 100.0 * osg_SimulationTime, |
| 92 |
gl_FragCoord.y + 300.0 * osg_SimulationTime), 8.0); |
| 93 |
|
| 94 |
float f_black = 1.0 - fact_black; |
| 95 |
|
| 96 |
noise *= (1.0 - smoothstep(0.0, 0.5, f_black)); |
| 97 |
|
| 98 |
color = mix(color, vec3 (1.0, 1.0, 1.0) * mix(length(color), |
| 99 |
tgt_brightness, greyout) , 0.9* greyout + 0.6 * noise); |
| 100 |
color *= tgt_brightness; |
| 101 |
|
| 102 |
float blackout = 1.0 - smoothstep( f_black * display_ysize, (f_black + blackout_band_width) * display_ysize, length(gl_FragCoord.xy -center)); |
| 103 |
|
| 104 |
color *= blackout; |
| 105 |
|
| 106 |
return color; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
vec3 filter_combined (in vec3 color) { |
| 111 |
|
| 112 |
// Apply some dithering to eliminate banding caused by rendering to a |
| 113 |
// 32 bpp framebuffer |
| 114 |
float dither_noise = rand2D(gl_FragCoord.xy); |
| 115 |
color += mix(-0.5/255.0, 0.5/255.0, dither_noise); |
| 116 |
|
| 117 |
if (use_filtering == false) |
| 118 |
{ |
| 119 |
return color; |
| 120 |
} |
| 121 |
|
| 122 |
color = g_force(color); |
| 123 |
|
| 124 |
|
| 125 |
if (use_night_vision) |
| 126 |
{ |
| 127 |
color = brightness_adjust(color); |
| 128 |
color = night_vision(color); |
| 129 |
} |
| 130 |
|
| 131 |
else if (use_IR_vision) |
| 132 |
{ |
| 133 |
float IR_brightness = min(1.0/(brightness+0.01), 5.0); |
| 134 |
color = clamp(IR_brightness * color, 0.0, 1.0); |
| 135 |
color = IR_vision(color); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
color = brightness_adjust(color); |
| 140 |
} |
| 141 |
|
| 142 |
return gamma_correction (color); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
|