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