| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
uniform sampler2D texture; |
| 5 |
|
| 6 |
uniform float light_color_base_r; |
| 7 |
uniform float light_color_base_g; |
| 8 |
uniform float light_color_base_b; |
| 9 |
|
| 10 |
uniform float light_color_center_r; |
| 11 |
uniform float light_color_center_g; |
| 12 |
uniform float light_color_center_b; |
| 13 |
|
| 14 |
uniform float intensity_scale; |
| 15 |
|
| 16 |
uniform float pointing_x; |
| 17 |
uniform float pointing_y; |
| 18 |
uniform float pointing_z; |
| 19 |
|
| 20 |
uniform float outer_angle; |
| 21 |
uniform float inner_angle; |
| 22 |
uniform float zero_angle; |
| 23 |
uniform float outer_gain; |
| 24 |
|
| 25 |
uniform float visibility; |
| 26 |
uniform float avisibility; |
| 27 |
uniform float hazeLayerAltitude; |
| 28 |
uniform float eye_alt; |
| 29 |
uniform float terminator; |
| 30 |
|
| 31 |
uniform float osg_SimulationTime; |
| 32 |
|
| 33 |
uniform bool is_directional; |
| 34 |
uniform bool is_strobe; |
| 35 |
|
| 36 |
varying vec3 vertex; |
| 37 |
varying vec3 relPos; |
| 38 |
varying vec3 normal; |
| 39 |
|
| 40 |
float Noise2D(in vec2 coord, in float wavelength); |
| 41 |
float fog_func (in float targ, in float alt); |
| 42 |
|
| 43 |
float shape (in vec3 coord, in float noise, in float fade, in float transmission, in float glare, in float lightArg) |
| 44 |
{ |
| 45 |
float r = length (coord) / max(fade, 0.2); |
| 46 |
|
| 47 |
float angle = noise * 6.2832; |
| 48 |
|
| 49 |
float sinphi = dot(vec2 (sin(angle),cos(angle)), normalize(coord.yz)); |
| 50 |
float sinterm = sin(mod((sinphi-3.0) * (sinphi-3.0),6.2832)); |
| 51 |
float ray = 0.0; |
| 52 |
if (sinterm == 0.0) |
| 53 |
{ray = 0.0;} |
| 54 |
else |
| 55 |
//{ray = clamp(pow(sinterm,10.0),0.0,1.0); |
| 56 |
{ray = sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm * sinterm; |
| 57 |
ray *= exp(-40.0 * r * r) * smoothstep(0.8, 1.0,fade) * smoothstep(0.7, 1.0, glare); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
float base = exp(-80.0*r*r ); |
| 63 |
float halo = 0.2 * exp(-10.0 * r * r) * (1.0 - smoothstep(-5.0, 0.0, lightArg)); |
| 64 |
float fogEffect = (1.0-smoothstep(0.4,0.8,transmission)); |
| 65 |
//fogEffect = 1.0; |
| 66 |
|
| 67 |
//float offset = 0.0; |
| 68 |
//offset *=0.3; |
| 69 |
//vec2 offset_vec = vec2 (1.0, 0.0); |
| 70 |
//offset_vec *= offset; |
| 71 |
|
| 72 |
// vec2 coord_reduced1 = vec2(coord.y- 1.2* offset_vec.x, coord.z - 1.2 * offset_vec.y); |
| 73 |
//vec2 coord_reduced2 = vec2(coord.y- 2.0 * offset_vec.x, coord.z - 2.0 * offset_vec.y); |
| 74 |
//vec3 coord_reduced = coord; |
| 75 |
//r = min(length (coord_reduced1), 0.8* length(coord_reduced2)); |
| 76 |
//r /= 1.0 - 0.3 * smoothstep(0.0, 0.3, offset); |
| 77 |
|
| 78 |
float intensity = clamp(base + halo + ray,0.0,1.0) + 0.2 * fogEffect * (1.0-smoothstep(0.3, 0.6,r)); |
| 79 |
|
| 80 |
intensity *=fade; |
| 81 |
|
| 82 |
return intensity; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
float directional_fade (in float direction) |
| 87 |
{ |
| 88 |
|
| 89 |
float arg = clamp(direction, 0.0, 1.0); |
| 90 |
float ia = (1.0 - inner_angle); |
| 91 |
float oa = (1.0 - outer_angle); |
| 92 |
float za = (1.0 - zero_angle); |
| 93 |
|
| 94 |
if (direction > ia) {return 1.0;} |
| 95 |
else if (direction > oa) |
| 96 |
{return outer_gain + (1.0-outer_gain) * (direction - oa) / (ia - oa);} |
| 97 |
else if (direction > za) |
| 98 |
{return outer_gain * (direction - za) / (oa - za);} |
| 99 |
else {return 0.0;} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
float strobe_fade (in float fade) |
| 104 |
{ |
| 105 |
|
| 106 |
float time_arg1 = sin(4.0 * osg_SimulationTime); |
| 107 |
float time_arg2 = sin(4.0 * osg_SimulationTime - 0.4); |
| 108 |
|
| 109 |
return fade * 0.825 * (pow(time_arg1, 40.0) + pow(time_arg2, 8.0)); |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
void main() |
| 115 |
{ |
| 116 |
|
| 117 |
float noise = 0.0; |
| 118 |
|
| 119 |
vec3 light_color_base = vec3 (light_color_base_r, light_color_base_g, light_color_base_b); |
| 120 |
vec3 light_color_center = vec3 (light_color_center_r, light_color_center_g, light_color_center_b); |
| 121 |
|
| 122 |
vec3 pointing_vec = vec3 (pointing_x, pointing_y, pointing_z); |
| 123 |
vec3 viewDir = normalize(relPos); |
| 124 |
|
| 125 |
// fogging |
| 126 |
|
| 127 |
float dist = length(relPos); |
| 128 |
float delta_z = hazeLayerAltitude - eye_alt; |
| 129 |
float transmission; |
| 130 |
float vAltitude; |
| 131 |
float delta_zv; |
| 132 |
float H; |
| 133 |
float distance_in_layer; |
| 134 |
float transmission_arg; |
| 135 |
|
| 136 |
// angle with horizon |
| 137 |
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist; |
| 138 |
|
| 139 |
|
| 140 |
if (delta_z > 0.0) // we're inside the layer |
| 141 |
{ |
| 142 |
if (ct < 0.0) // we look down |
| 143 |
{ |
| 144 |
distance_in_layer = dist; |
| 145 |
vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct; |
| 146 |
delta_zv = delta_z - vAltitude; |
| 147 |
} |
| 148 |
else // we may look through upper layer edge |
| 149 |
{ |
| 150 |
H = dist * ct; |
| 151 |
if (H > delta_z) {distance_in_layer = dist/H * delta_z;} |
| 152 |
else {distance_in_layer = dist;} |
| 153 |
vAltitude = min(distance_in_layer,visibility) * ct; |
| 154 |
delta_zv = delta_z - vAltitude; |
| 155 |
} |
| 156 |
} |
| 157 |
else // we see the layer from above, delta_z < 0.0 |
| 158 |
{ |
| 159 |
H = dist * -ct; |
| 160 |
if (H < (-delta_z)) |
| 161 |
{ |
| 162 |
distance_in_layer = 0.0; |
| 163 |
delta_zv = 0.0; |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
vAltitude = H + delta_z; |
| 168 |
distance_in_layer = vAltitude/H * dist; |
| 169 |
vAltitude = min(distance_in_layer,visibility) * (-ct); |
| 170 |
delta_zv = vAltitude; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
transmission_arg = (dist-distance_in_layer)/avisibility; |
| 177 |
if (visibility < avisibility) |
| 178 |
{ |
| 179 |
transmission_arg = transmission_arg + (distance_in_layer/visibility); |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
transmission_arg = transmission_arg + (distance_in_layer/avisibility); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
transmission = fog_func(transmission_arg, 0.0); |
| 189 |
float lightArg = terminator/100000.0; |
| 190 |
|
| 191 |
|
| 192 |
float r = length(vertex); |
| 193 |
float mix_factor = 0.3 + 0.7 * smoothstep(0.0, 0.5, r); |
| 194 |
|
| 195 |
// directionality |
| 196 |
|
| 197 |
vec3 nViewDir = normalize(viewDir); |
| 198 |
vec3 nPointingVec = normalize(pointing_vec); |
| 199 |
float direction = dot (nViewDir, nPointingVec ); |
| 200 |
|
| 201 |
float fade; |
| 202 |
vec2 offset = vec2 (0.0, 0.0); |
| 203 |
|
| 204 |
if (is_directional) |
| 205 |
{ |
| 206 |
fade = directional_fade(direction); |
| 207 |
} |
| 208 |
else |
| 209 |
{fade = 1.0;} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
// time evolution |
| 214 |
|
| 215 |
if (is_strobe) {fade = strobe_fade (fade);} |
| 216 |
|
| 217 |
fade *= intensity_scale; |
| 218 |
|
| 219 |
// disc size correction for daylight |
| 220 |
|
| 221 |
// shape of the light disc |
| 222 |
float glare = length(light_color_center)/1.7321 * (1.0 - smoothstep(-5.0, 10.0, lightArg)); |
| 223 |
float intensity = shape(vertex, noise, fade, transmission, glare, lightArg); |
| 224 |
|
| 225 |
// coloring of the light disc |
| 226 |
vec3 light_color = mix(light_color_base, light_color_center, intensity*intensity); |
| 227 |
|
| 228 |
|
| 229 |
gl_FragColor = vec4 (light_color.rgb, intensity * transmission ); |
| 230 |
|
| 231 |
|
| 232 |
} |