| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
varying float fogFactor; |
| 5 |
varying vec3 hazeColor; |
| 6 |
|
| 7 |
uniform float range; // From /sim/rendering/clouds3d-vis-range |
| 8 |
uniform float detail_range; // From /sim/rendering/clouds3d_detail-range |
| 9 |
uniform float scattering; |
| 10 |
uniform float terminator; |
| 11 |
uniform float altitude; |
| 12 |
uniform float cloud_self_shading; |
| 13 |
uniform float visibility; |
| 14 |
uniform float moonlight; |
| 15 |
uniform float air_pollution; |
| 16 |
uniform float flash; |
| 17 |
uniform float lightning_pos_x; |
| 18 |
uniform float lightning_pos_y; |
| 19 |
uniform float lightning_range; |
| 20 |
|
| 21 |
attribute vec3 usrAttr1; |
| 22 |
attribute vec3 usrAttr2; |
| 23 |
|
| 24 |
float alpha_factor = usrAttr1.r; |
| 25 |
float shade_factor = usrAttr1.g; |
| 26 |
float cloud_height = usrAttr1.b; |
| 27 |
float bottom_factor = usrAttr2.r; |
| 28 |
float middle_factor = usrAttr2.g; |
| 29 |
float top_factor = usrAttr2.b; |
| 30 |
|
| 31 |
const float EarthRadius = 5800000.0; |
| 32 |
|
| 33 |
vec3 moonlight_perception (in vec3 light); |
| 34 |
|
| 35 |
// light_func is a generalized logistic function fit to the light intensity as a function |
| 36 |
// of scaled terminator position obtained from Flightgear core |
| 37 |
|
| 38 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 39 |
{ |
| 40 |
x = x-0.5; |
| 41 |
|
| 42 |
|
| 43 |
// use the asymptotics to shorten computations |
| 44 |
if (x > 30.0) {return e;} |
| 45 |
if (x < -15.0) {return 0.03;} |
| 46 |
|
| 47 |
|
| 48 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
float mie_func (in float x, in float Mie) |
| 53 |
{ |
| 54 |
return x + 2.0 * x * Mie * (1.0 -0.8*x) * (1.0 -0.8*x); |
| 55 |
} |
| 56 |
|
| 57 |
void main(void) |
| 58 |
{ |
| 59 |
|
| 60 |
|
| 61 |
//shade_factor = shade_factor * cloud_self_shading; |
| 62 |
//top_factor = top_factor * cloud_self_shading; |
| 63 |
//shade_factor = min(shade_factor, top_factor); |
| 64 |
//middle_factor = min(middle_factor, top_factor); |
| 65 |
//bottom_factor = min(bottom_factor, top_factor); |
| 66 |
|
| 67 |
float intensity; |
| 68 |
float mix_factor; |
| 69 |
|
| 70 |
|
| 71 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 72 |
vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight * scattering; |
| 73 |
moonLightColor = moonlight_perception (moonLightColor); |
| 74 |
|
| 75 |
gl_TexCoord[0] = gl_MultiTexCoord0; |
| 76 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 77 |
vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0); |
| 78 |
vec3 u = normalize(ep.xyz - l.xyz); |
| 79 |
|
| 80 |
// Find a rotation matrix that rotates 1,0,0 into u. u, r and w are |
| 81 |
// the columns of that matrix. |
| 82 |
vec3 absu = abs(u); |
| 83 |
vec3 r = normalize(vec3(-u.y, u.x, 0.0)); |
| 84 |
vec3 w = cross(u, r); |
| 85 |
|
| 86 |
// Do the matrix multiplication by [ u r w pos]. Assume no |
| 87 |
// scaling in the homogeneous component of pos. |
| 88 |
gl_Position = vec4(0.0, 0.0, 0.0, 1.0); |
| 89 |
gl_Position.xyz = gl_Vertex.x * u; |
| 90 |
gl_Position.xyz += gl_Vertex.y * r; |
| 91 |
gl_Position.xyz += gl_Vertex.z * w; |
| 92 |
// Apply Z scaling to allow sprites to be squashed in the z-axis |
| 93 |
gl_Position.z = gl_Position.z * gl_Color.w; |
| 94 |
|
| 95 |
// Now shift the sprite to the correct position in the cloud. |
| 96 |
gl_Position.xyz += gl_Color.xyz; |
| 97 |
|
| 98 |
// Determine a lighting normal based on the vertex position from the |
| 99 |
// center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker. |
| 100 |
float n = dot(normalize(-gl_LightSource[0].position.xyz), |
| 101 |
normalize(vec3(gl_ModelViewMatrix * vec4(- gl_Position.x, - gl_Position.y, - gl_Position.z, 0.0)))); |
| 102 |
|
| 103 |
// Determine the position - used for fog and shading calculations |
| 104 |
float fogCoord = length(vec3(gl_ModelViewMatrix * vec4(gl_Color.x, gl_Color.y, gl_Color.z, 1.0))); |
| 105 |
float center_dist = length(vec3(gl_ModelViewMatrix * vec4(0.0,0.0,0.0,1.0))); |
| 106 |
|
| 107 |
if ((fogCoord > detail_range) && (fogCoord > center_dist) && (shade_factor < 0.7)) { |
| 108 |
// More than detail_range away, so discard all sprites on opposite side of |
| 109 |
// cloud center by shifting them beyond the view fustrum |
| 110 |
gl_Position = vec4(0.0,0.0,10.0,1.0); |
| 111 |
gl_FrontColor.a = 0.0; |
| 112 |
} else { |
| 113 |
|
| 114 |
// Determine the shading of the vertex. We shade it based on it's position |
| 115 |
// in the cloud relative to the sun, and it's vertical position in the cloud. |
| 116 |
float shade = mix(shade_factor, top_factor, smoothstep(-0.3, 0.3, n)); |
| 117 |
//if (n < 0) { |
| 118 |
// shade = mix(top_factor, shade_factor, abs(n)); |
| 119 |
//} |
| 120 |
|
| 121 |
if (gl_Position.z < 0.5 * cloud_height) { |
| 122 |
shade = min(shade, mix(bottom_factor, middle_factor, gl_Position.z * 2.0 / cloud_height)); |
| 123 |
} else { |
| 124 |
shade = min(shade, mix(middle_factor, top_factor, gl_Position.z * 2.0 / cloud_height - 1.0)); |
| 125 |
} |
| 126 |
|
| 127 |
//float h = gl_Position.z / cloud_height; |
| 128 |
//if (h < 0.5) { |
| 129 |
// shade = min(shade, mix(bottom_factor, middle_factor, smoothstep(0.0, 0.5, h))); |
| 130 |
//} else { |
| 131 |
// shade = min(shade, mix(middle_factor, top_factor, smoothstep(2.0 * (h - 0.5))); |
| 132 |
// } |
| 133 |
|
| 134 |
// Final position of the sprite |
| 135 |
vec3 relVector = gl_Position.xyz - ep.xyz; |
| 136 |
gl_Position = gl_ModelViewProjectionMatrix * gl_Position; |
| 137 |
|
| 138 |
|
| 139 |
// Light at the final position |
| 140 |
|
| 141 |
// first obtain normal to sun position |
| 142 |
|
| 143 |
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz; |
| 144 |
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0)); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
// yprime is the distance of the vertex into sun direction, corrected for altitude |
| 149 |
// the altitude correction is clamped to reasonable values, sometimes altitude isn't parsed correctly, leading |
| 150 |
// to overbright or overdark clouds |
| 151 |
// float vertex_alt = clamp(altitude * 0.30480 + relVector.z,1000.0,10000.0); |
| 152 |
float vertex_alt = clamp(altitude + relVector.z, 300.0, 10000.0); |
| 153 |
float yprime = -dot(relVector, lightHorizon); |
| 154 |
float yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); |
| 155 |
|
| 156 |
// two times terminator width governs how quickly light fades into shadow |
| 157 |
float terminator_width = 200000.0; |
| 158 |
float earthShade = 1.0- 0.9* smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt); |
| 159 |
float earthShadeFactor = 1.0 - smoothstep(0.4, 0.5, earthShade); |
| 160 |
|
| 161 |
// compute the light at the position |
| 162 |
vec4 light_diffuse; |
| 163 |
|
| 164 |
float lightArg = (terminator-yprime_alt)/100000.0; |
| 165 |
|
| 166 |
light_diffuse.b = light_func(lightArg -1.2 * air_pollution, 1.330e-05, 0.264, 2.227, 1.08e-05, 1.0); |
| 167 |
light_diffuse.g = light_func(lightArg -0.6 * air_pollution, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); |
| 168 |
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); |
| 169 |
light_diffuse.a = 1.0; |
| 170 |
|
| 171 |
//light_diffuse *= cloud_self_shading; |
| 172 |
intensity = (1.0 - (0.8 * (1.0 - earthShade))) * length(light_diffuse.rgb); |
| 173 |
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, (1.0 - smoothstep(0.5,0.9, min(scattering, cloud_self_shading) )))); |
| 174 |
|
| 175 |
// correct ambient light intensity and hue before sunrise |
| 176 |
if (earthShade < 0.6) |
| 177 |
{ |
| 178 |
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,earthShade ) )); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
gl_FrontColor.rgb = intensity * shade * normalize(mix(light_diffuse.rgb, shadedFogColor, smoothstep(0.1,0.4, (1.0 - shade) ))) ; |
| 185 |
|
| 186 |
// lightning |
| 187 |
vec2 lightningRelVector = relVector.xy - vec2(lightning_pos_x, lightning_pos_y); |
| 188 |
float rCoord = length(lightningRelVector); |
| 189 |
|
| 190 |
vec3 flash_color = vec3 (0.43, 0.57, 1.0); |
| 191 |
float flash_factor = flash; |
| 192 |
|
| 193 |
if (flash == 2) |
| 194 |
{ |
| 195 |
flash_color = vec3 (0.8, 0.7, 0.4); |
| 196 |
flash_factor = 1; |
| 197 |
} |
| 198 |
|
| 199 |
float rn = 0.5 + 0.5 * fract(gl_Color.x); |
| 200 |
gl_FrontColor.rgb += flash_factor * flash_color * (1.0 - smoothstep(lightning_range, 5.0 * lightning_range, rCoord)) * rn; |
| 201 |
|
| 202 |
|
| 203 |
// fading of cloudlets |
| 204 |
|
| 205 |
if ((fogCoord > (0.9 * detail_range)) && (fogCoord > center_dist) && (shade_factor < 0.7)) { |
| 206 |
// cloudlet is almost at the detail range, so fade it out. |
| 207 |
gl_FrontColor.a = 1.0 - smoothstep(0.9 * detail_range, detail_range, fogCoord); |
| 208 |
} else { |
| 209 |
// As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out. |
| 210 |
gl_FrontColor.a = min(smoothstep(10.0, 100.0, fogCoord), 1.0 - smoothstep(0.9 * range, range, fogCoord)); |
| 211 |
} |
| 212 |
gl_FrontColor.a = gl_FrontColor.a * (1.0 - smoothstep(visibility, 3.0* visibility, fogCoord)); |
| 213 |
|
| 214 |
fogFactor = exp(-fogCoord/visibility); |
| 215 |
|
| 216 |
// haze of ground haze shader is slightly bluish |
| 217 |
hazeColor = light_diffuse.rgb; |
| 218 |
hazeColor.r = hazeColor.r * 0.83; |
| 219 |
hazeColor.g = hazeColor.g * 0.9; |
| 220 |
hazeColor = hazeColor * scattering; |
| 221 |
|
| 222 |
|
| 223 |
// Mie correction |
| 224 |
float Mie = 0.0; |
| 225 |
float MieFactor = 0.0; |
| 226 |
|
| 227 |
if (bottom_factor > 0.4) |
| 228 |
{ |
| 229 |
MieFactor = dot(normalize(lightFull), normalize(relVector)); |
| 230 |
Mie = 1.5 * smoothstep(0.9,1.0, MieFactor) * smoothstep(0.6, 0.8, bottom_factor) * (1.0-earthShadeFactor) ; |
| 231 |
//if (MieFactor < 0.0) {Mie = - Mie;} |
| 232 |
} |
| 233 |
//else {Mie = 0.0;} |
| 234 |
|
| 235 |
if (Mie > 0.0) |
| 236 |
{ |
| 237 |
hazeColor.r = mie_func(hazeColor.r, Mie); |
| 238 |
hazeColor.g = mie_func(hazeColor.g, 0.8* Mie); |
| 239 |
hazeColor.b = mie_func(hazeColor.b, 0.5* Mie); |
| 240 |
|
| 241 |
gl_FrontColor.r = mie_func(gl_FrontColor.r, Mie); |
| 242 |
gl_FrontColor.g = mie_func(gl_FrontColor.g, 0.8* Mie); |
| 243 |
gl_FrontColor.b = mie_func(gl_FrontColor.b, 0.5*Mie); |
| 244 |
} |
| 245 |
else if (MieFactor < 0.0) |
| 246 |
{ |
| 247 |
float thickness_reduction = smoothstep(0.4, 0.8, bottom_factor) ; |
| 248 |
float light_reduction = dot (lightFull, lightHorizon); |
| 249 |
light_reduction *= light_reduction; |
| 250 |
|
| 251 |
float factor_b = 0.8 + 0.2 * (1.0 - smoothstep(0.0, 0.7, -MieFactor) * thickness_reduction * light_reduction) ; |
| 252 |
float factor_r = 0.6 + 0.4 * (1.0 - smoothstep(0.0, 0.7, -MieFactor) * thickness_reduction * light_reduction) ; |
| 253 |
float factor_g = 0.65 + 0.35 * (1.0 - smoothstep(0.0, 0.7, -MieFactor) * thickness_reduction * light_reduction) ; |
| 254 |
hazeColor.r *= factor_r; |
| 255 |
hazeColor.g *= factor_g; |
| 256 |
hazeColor.b *= factor_b; |
| 257 |
|
| 258 |
gl_FrontColor.r *= factor_r; |
| 259 |
gl_FrontColor.g *= factor_g; |
| 260 |
gl_FrontColor.b *= factor_b; |
| 261 |
} |
| 262 |
|
| 263 |
gl_FrontColor.rgb = gl_FrontColor.rgb + moonLightColor * earthShadeFactor; |
| 264 |
hazeColor.rgb = hazeColor.rgb + moonLightColor * earthShadeFactor; |
| 265 |
gl_FrontColor.a = gl_FrontColor.a * alpha_factor; |
| 266 |
gl_BackColor = gl_FrontColor; |
| 267 |
} |
| 268 |
} |