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