From a4960e14fad85356ddd9d86ea2d92468275433d6 Mon Sep 17 00:00:00 2001 From: Thorsten Renk Date: Sat, 1 Sep 2018 09:42:34 +0300 Subject: [PATCH] ALS: Detailed scattering model for light on Cirrus clouds --- Effects/cloud-static.eff | 146 +++++++++++++++++++++++++ Effects/skydome.eff | 8 +- Environment/environment.xml | 2 + Shaders/cloud-static-ALS-detailed.frag | 59 ++++++++++ Shaders/cloud-static-ALS-detailed.vert | 142 ++++++++++++++++++++++++ Shaders/skydome-ALS.frag | 3 +- 6 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 Shaders/cloud-static-ALS-detailed.frag create mode 100644 Shaders/cloud-static-ALS-detailed.vert diff --git a/Effects/cloud-static.eff b/Effects/cloud-static.eff index 38d916c37..32fc81409 100644 --- a/Effects/cloud-static.eff +++ b/Effects/cloud-static.eff @@ -8,6 +8,8 @@ /sim/rendering/eye-altitude-m /environment/cloud-self-shading /environment/moonlight + /environment/scattering-phenomena/ring-factor + /environment/scattering-phenomena/rainbow-factor /sim/rendering/als-filters/use-filtering /sim/rendering/als-filters/gamma /sim/rendering/als-filters/brightness @@ -20,6 +22,150 @@ /sim/startup/ysize + + + + /sim/rendering/shaders/skydome + /sim/rendering/clouds3d-enable + + 1.0 + + + + 1.0 + /sim/rendering/shaders/clouds + + + + + + true + + 0.5 0.5 0.5 1.0 + 0.5 0.5 0.5 1.0 + off + + + greater + 0.01 + + smooth + + src-alpha + one-minus-src-alpha + + + false + + + 9 + DepthSortedBin + + + 0 + texture[0]/type + texture[0]/image + texture[0]/filter + texture[0]/wrap-s + texture[0]/wrap-t + + + + Shaders/cloud-static-ALS-detailed.vert + Shaders/cloud-static-ALS-detailed.frag + Shaders/noise.frag + Shaders/filters-ALS.frag + + + baseTexture + sampler-2d + 0 + + + terminator + float + terminator + + + altitude + float + altitude + + + cloud_self_shading + float + cloud_self_shading + + + moonlight + float + moonlight + + + ring_factor + float + ring-factor + + + rainbow_factor + float + rainbow-factor + + + gamma + float + gamma + + + brightness + float + brightness + + + use_filtering + bool + use_filtering + + + use_night_vision + bool + use_night_vision + + + use_IR_vision + bool + use_IR_vision + + + delta_T + float + delta_T + + + fact_grey + float + fact_grey + + + fact_black + float + fact_black + + + display_xsize + int + display_xsize + + + display_ysize + int + display_ysize + + true + + + diff --git a/Effects/skydome.eff b/Effects/skydome.eff index 997954d72..c30b439d6 100644 --- a/Effects/skydome.eff +++ b/Effects/skydome.eff @@ -13,11 +13,12 @@ /environment/scattering-phenomena/ice-hexagonal-column-factor /environment/scattering-phenomena/ice-hexagonal-sheet-factor /environment/scattering-phenomena/parhelic-factor + /environment/scattering-phenomena/ring-factor /environment/aurora/strength /environment/aurora/vsize /environment/aurora/hsize /environment/aurora/ray-factor - /environment/aurora/penetration-factor + /environment/aurora/penetration-factor /environment/ground-haze-thickness-m /environment/terminator-relative-position-m /environment/mean-terrain-elevation-m @@ -149,6 +150,11 @@ float parhelic + + ring + float + ring + ice_hex_sheet float diff --git a/Environment/environment.xml b/Environment/environment.xml index b90b363ec..db83b7311 100644 --- a/Environment/environment.xml +++ b/Environment/environment.xml @@ -436,6 +436,8 @@ 0.0 0.0 0.0 + 0.0 + 0.0 diff --git a/Shaders/cloud-static-ALS-detailed.frag b/Shaders/cloud-static-ALS-detailed.frag new file mode 100644 index 000000000..a63eddf83 --- /dev/null +++ b/Shaders/cloud-static-ALS-detailed.frag @@ -0,0 +1,59 @@ +// -*-C++-*- +#version 120 + +uniform sampler2D baseTexture; + +uniform float ring_factor; +uniform float rainbow_factor; + +varying float fogFactor; +varying float mie_frag; +varying float eShade; + +varying vec3 hazeColor; + +vec3 filter_combined (in vec3 color) ; + +void main(void) +{ + vec4 base = texture2D( baseTexture, gl_TexCoord[0].st); + + float fwd_enhancement = smoothstep(0.8, 1.0, mie_frag) * eShade; + float light_intensity = length(gl_Color.rgb)/1.76; + + + //22 deg ring + + float halo_ring_enhancement = smoothstep (0.88, 0.927, mie_frag) * (1.0 - smoothstep(0.927, 0.94, mie_frag)) * eShade * 1.5 * ring_factor; + + + float halo_ring_enhancement_r = smoothstep (0.88, 0.90, mie_frag) * (1.0 - smoothstep(0.90, 0.92, mie_frag)) * eShade * ring_factor; + float halo_ring_enhancement_b = smoothstep (0.91, 0.93, mie_frag) * (1.0 - smoothstep(0.93, 0.955, mie_frag)) * eShade * ring_factor; + + + vec4 finalColor = base * gl_Color; + + float reduction = 0.16 * light_intensity * rainbow_factor; + + finalColor.g *= (1.0 - reduction * halo_ring_enhancement_r); + finalColor.b *= (1.0 - reduction * halo_ring_enhancement_r); + + finalColor.r *= (1.0 - reduction * halo_ring_enhancement_b); + finalColor.g *= (1.0 - reduction * halo_ring_enhancement_b); + + fwd_enhancement *=(1.0-smoothstep(0.8, 1.0, light_intensity)); + finalColor.rgb *= (1.0 + fwd_enhancement) * (1.0 + 0.5 * halo_ring_enhancement * (1.0-smoothstep(0.8, 1.0, light_intensity))) ; + finalColor.a *= (1.0 + 0.5 * halo_ring_enhancement); + + + + finalColor.rgb = clamp(finalColor.rgb, 0.0, 1.0); + finalColor.a = clamp(finalColor.a, 0.0, 1.0); + + vec4 fragColor = vec4 (mix(hazeColor, finalColor.rgb, fogFactor ), mix(0.0, finalColor.a, 1.0 - 0.5 * (1.0 - fogFactor))); + + + fragColor.rgb = filter_combined(fragColor.rgb); + //fragColor.rgb = vec3 (1.0, 0.0, 0.0); + gl_FragColor = fragColor; +} diff --git a/Shaders/cloud-static-ALS-detailed.vert b/Shaders/cloud-static-ALS-detailed.vert new file mode 100644 index 000000000..2ea5d7c28 --- /dev/null +++ b/Shaders/cloud-static-ALS-detailed.vert @@ -0,0 +1,142 @@ +// -*-C++-*- +#version 120 + +varying float fogFactor; +varying float mie_frag; +varying float eShade; +varying vec3 hazeColor; + +uniform float terminator; +uniform float altitude; +uniform float cloud_self_shading; +uniform float moonlight; + +const float shade = 1.0; +const float cloud_height = 1000.0; +const float EarthRadius = 5800000.0; + +// light_func is a generalized logistic function fit to the light intensity as a function +// of scaled terminator position obtained from Flightgear core + +float light_func (in float x, in float a, in float b, in float c, in float d, in float e) +{ +x = x-0.5; + +// use the asymptotics to shorten computations +if (x > 30.0) {return e;} +if (x < -15.0) {return 0.03;} + + +return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); +} + +void main(void) +{ + + vec3 shadedFogColor = vec3 (0.55, 0.67, 0.88); + vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight; + + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; + //gl_TexCoord[0] = gl_MultiTexCoord0 + vec4(textureIndexX, textureIndexY, 0.0, 0.0); + vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); + vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0); + vec3 u = normalize(ep.xyz - l.xyz); + + gl_Position = vec4(0.0, 0.0, 0.0, 1.0); + gl_Position.x = gl_Vertex.x; + gl_Position.y += gl_Vertex.y; + gl_Position.z += gl_Vertex.z; + gl_Position.xyz += gl_Color.xyz; + + + // Determine a lighting normal based on the vertex position from the + // center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker. + float n = dot(normalize(-gl_LightSource[0].position.xyz), + normalize(mat3x3(gl_ModelViewMatrix) * (- gl_Position.xyz)));; + + // Determine the position - used for fog and shading calculations + vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Position); + float fogCoord = abs(ecPosition.z); + float fract = smoothstep(0.0, cloud_height, gl_Position.z + cloud_height); + + vec3 relVector = gl_Position.xyz - ep.xyz; + gl_Position = gl_ModelViewProjectionMatrix * gl_Position; + + // Light at the final position + + // first obtain normal to sun position + + vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz; + vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0)); + + + mie_frag = dot(normalize(lightFull), normalize(relVector)); + + + // yprime is the distance of the vertex into sun direction, corrected for altitude + //float vertex_alt = max(altitude * 0.30480 + relVector.z,100.0); + float vertex_alt = altitude + relVector.z; + float yprime = -dot(relVector, lightHorizon); + float yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt); + + // compute the light at the position + vec4 light_diffuse; + + float lightArg = (terminator-yprime_alt)/100000.0; + + light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 2.227, 1.08e-05, 1.0); + light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0); + light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0); + light_diffuse.a = 1.0; + + float intensity = length(light_diffuse.rgb); + light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, (1.0 - smoothstep(0.5,0.9, cloud_self_shading )))); + +// Determine the shading of the sprite based on its vertical position and position relative to the sun. + n = min(smoothstep(-0.5, 0.0, n), fract); +// Determine the shading based on a mixture from the backlight to the front + vec4 backlight = light_diffuse * shade; + + gl_FrontColor = mix(backlight, light_diffuse, n); + gl_FrontColor += gl_FrontLightModelProduct.sceneColor; + + // As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out. + gl_FrontColor.a = min(smoothstep(100.0, 250.0, fogCoord), 1.0 - smoothstep(70000.0, 75000.0, fogCoord)); + + // Fog doesn't affect rain as much as other objects. + //fogFactor = exp( -gl_Fog.density * fogCoord * 0.4); + //fogFactor = clamp(fogFactor, 0.0, 1.0); + +float fadeScale = 0.05 + 0.2 * log(fogCoord/1000.0); + if (fadeScale < 0.05) fadeScale = 0.05; + fogFactor = exp( -gl_Fog.density * fogCoord * fadeScale); + + hazeColor = light_diffuse.rgb; + hazeColor.r = hazeColor.r * 0.83; + hazeColor.g = hazeColor.g * 0.9; + + // in sunset or sunrise conditions, do extra shading of clouds + + + + // two times terminator width governs how quickly light fades into shadow + float terminator_width = 200000.0; + + // now dim the light + float earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1; + + eShade = earthShade; + + if (earthShade < 0.8) + { + intensity = length(light_diffuse.rgb); + gl_FrontColor.rgb = intensity * normalize(mix(gl_FrontColor.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade ) )); + } + + hazeColor = hazeColor * earthShade; + gl_FrontColor.rgb = gl_FrontColor.rgb * earthShade; + gl_FrontColor.rgb = gl_FrontColor.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade)); + hazeColor.rgb = hazeColor.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade)); + gl_BackColor = gl_FrontColor; + +} diff --git a/Shaders/skydome-ALS.frag b/Shaders/skydome-ALS.frag index de3489644..9a97b33e9 100644 --- a/Shaders/skydome-ALS.frag +++ b/Shaders/skydome-ALS.frag @@ -31,6 +31,7 @@ uniform float horizon_roughness; uniform float ice_hex_col; uniform float ice_hex_sheet; uniform float parhelic; +uniform float ring; uniform float aurora_strength; uniform float aurora_hsize; uniform float aurora_vsize; @@ -258,7 +259,7 @@ float view_altitude = dot(nView, vec3 (0.0, 0.0, 1.0)); //float halo_ring_enhancement = smoothstep (0.88, 0.927, calpha) * (1.0 - smoothstep(0.927, 0.98, calpha)); float halo_ring_enhancement = smoothstep (0.88, 0.927, calpha) * (1.0 - smoothstep(0.927, 0.94, calpha)); halo_ring_enhancement *= halo_ring_enhancement; - +halo_ring_enhancement *= ring; // parhelic circle