| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
#define MAX_LAYERS 8 |
| 5 |
#define MAX_DISTANCE 3000.0 |
| 6 |
|
| 7 |
uniform float visibility; |
| 8 |
uniform float avisibility; |
| 9 |
uniform float scattering; |
| 10 |
uniform float overlay_bias; |
| 11 |
uniform float season; |
| 12 |
uniform float dust_cover_factor; |
| 13 |
uniform float overlay_max_height; |
| 14 |
uniform float overlay_hardness; |
| 15 |
uniform float overlay_density; |
| 16 |
uniform float overlay_scale; |
| 17 |
uniform float overlay_steepness_factor; |
| 18 |
uniform float overlay_brightness_bottom; |
| 19 |
uniform float overlay_brightness_top; |
| 20 |
uniform float overlay_secondary_hardness; |
| 21 |
uniform float overlay_secondary_density; |
| 22 |
uniform float snowlevel; |
| 23 |
uniform float wetness; |
| 24 |
uniform float snow_thickness_factor; |
| 25 |
|
| 26 |
uniform int overlay_autumn_flag; |
| 27 |
uniform int overlay_secondary_flag; |
| 28 |
uniform int cloud_shadow_flag; |
| 29 |
|
| 30 |
uniform sampler2D overlayPrimaryTex; |
| 31 |
uniform sampler2D overlaySecondaryTex; |
| 32 |
|
| 33 |
|
| 34 |
uniform float osg_SimulationTime; |
| 35 |
|
| 36 |
varying vec2 g_rawpos; // Horizontal position in model space |
| 37 |
varying float g_distance_to_eye; // Distance to the camera. Layers were disregarded |
| 38 |
varying vec3 g_normal; |
| 39 |
varying float g_altitude; |
| 40 |
varying float g_layer; // The layer where the fragment lives (0-1 range) |
| 41 |
|
| 42 |
|
| 43 |
float rand2D(in vec2 co); |
| 44 |
float Noise2D(in vec2 co, in float wavelength); |
| 45 |
vec3 filter_combined (in vec3 color) ; |
| 46 |
|
| 47 |
float shadow_func_nearest (in float x, in float y, in float noise, in float dist); |
| 48 |
|
| 49 |
float getShadowing(); |
| 50 |
|
| 51 |
|
| 52 |
void main() |
| 53 |
{ |
| 54 |
|
| 55 |
if (g_distance_to_eye > MAX_DISTANCE) {discard;} |
| 56 |
|
| 57 |
vec2 texCoord = gl_TexCoord[0].st; |
| 58 |
|
| 59 |
vec2 pos_rotated = vec2 (0.707 * g_rawpos.x + 0.707 * g_rawpos.y, 0.707 * g_rawpos.x - 0.707 * g_rawpos.y); |
| 60 |
|
| 61 |
|
| 62 |
//float noise_1m = 0.5 * Noise2D(pos_rotated.xy, 1.0 * overlay_scale); |
| 63 |
//noise_1m += 0.5 * Noise2D(g_rawpos.xy, 1.1 * overlay_scale); |
| 64 |
|
| 65 |
float noise_1m = Noise2D(pos_rotated.xy, 1.0 * overlay_scale); |
| 66 |
|
| 67 |
float noise_2m = Noise2D(g_rawpos.xy, 2.0 * overlay_scale); ; |
| 68 |
float noise_10m = Noise2D(g_rawpos.xy, 10.0 * overlay_scale); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
float value = 0.0; |
| 73 |
|
| 74 |
float d_fade =smoothstep(100.0, MAX_DISTANCE, g_distance_to_eye); |
| 75 |
|
| 76 |
float steepness = dot (normalize(g_normal), vec3 (0.0, 0.0, 1.0)); |
| 77 |
float steepness_bias = smoothstep(overlay_steepness_factor, overlay_steepness_factor + 0.1, steepness); |
| 78 |
|
| 79 |
float overlayPattern = 0.2 * noise_10m + 0.3 * noise_2m + 0.5 * noise_1m - 0.2 * g_layer - 0.1 + 0.2 * overlay_density ; |
| 80 |
overlayPattern *= steepness_bias; |
| 81 |
|
| 82 |
float secondaryPattern = 0.2 * (1.0-noise_10m) + 0.3 * (1.0-noise_2m) + 0.5 * (1.0-noise_1m) - 0.4 * g_layer - 0.2 + 0.2 * overlay_secondary_density ; |
| 83 |
secondaryPattern *= overlay_secondary_flag; |
| 84 |
float secondaryMix = 0.0; |
| 85 |
|
| 86 |
if (overlayPattern > 0.5) |
| 87 |
{ |
| 88 |
value = smoothstep(0.5, (0.8 - 0.25 * overlay_hardness), overlayPattern); |
| 89 |
} |
| 90 |
else if (secondaryPattern > 0.5) |
| 91 |
{ |
| 92 |
value = smoothstep(0.5, (0.8 - 0.25 * overlay_secondary_hardness), secondaryPattern); |
| 93 |
secondaryMix = 1.0; |
| 94 |
} |
| 95 |
else {discard;} |
| 96 |
|
| 97 |
vec3 texel = texture2D(overlayPrimaryTex, texCoord * 20.0).rgb; |
| 98 |
vec3 secondary_texel = texture2D(overlaySecondaryTex, texCoord * 20.0).rgb; |
| 99 |
|
| 100 |
// autumn coloring |
| 101 |
|
| 102 |
if (overlay_autumn_flag == 1) |
| 103 |
{ |
| 104 |
texel.r = min(1.0, (1.0 + 2.5 * 0.1 * season) * texel.r); |
| 105 |
texel.g = texel.g; |
| 106 |
texel.b = max(0.0, (1.0 - 4.0 * 0.1 * season) * texel.b); |
| 107 |
float intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season)); |
| 108 |
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season))); |
| 109 |
} |
| 110 |
|
| 111 |
texel = mix (texel, secondary_texel, secondaryMix); |
| 112 |
|
| 113 |
|
| 114 |
float layer_arg = mix(g_layer, 1.0, smoothstep(250.0, 5000.0, g_distance_to_eye)); |
| 115 |
texel.rgb *= (overlay_brightness_bottom + (overlay_brightness_top - overlay_brightness_bottom) * g_layer); |
| 116 |
texel.rgb *= (1.0 - 0.5 * wetness); |
| 117 |
|
| 118 |
// dust overlay |
| 119 |
|
| 120 |
const vec3 dust_color = vec3 (0.76, 0.65, 0.45); |
| 121 |
texel = mix (texel, dust_color, 0.7 * dust_cover_factor); |
| 122 |
|
| 123 |
// snow overlay |
| 124 |
|
| 125 |
//vec3 snow_texel = vec3 (0.95, 0.95, 0.95); |
| 126 |
|
| 127 |
float snow_factor = 0.2+0.8* smoothstep(0.2,0.8, 0.3 + 0.5 * snow_thickness_factor +0.0001*(g_altitude -snowlevel) ); |
| 128 |
snow_factor *= smoothstep(0.5, 0.7, steepness); |
| 129 |
|
| 130 |
snow_factor *= smoothstep(g_layer - 0.1, g_layer , snow_factor + 0.2); |
| 131 |
|
| 132 |
//texel.rgb = mix(texel.rgb, snow_texel.rgb, snow_factor * smoothstep(snowlevel, snowlevel+200.0, g_altitude - 100.0)); |
| 133 |
value *= (1.0 - snow_factor * smoothstep(snowlevel, snowlevel+200.0, g_altitude - 100.0)); |
| 134 |
|
| 135 |
// do a conservative simple fog model, fading to alpha |
| 136 |
|
| 137 |
float min_visibility = min(visibility, avisibility); |
| 138 |
|
| 139 |
float base_alpha = clamp(0.4 * overlay_max_height/0.3, 0.4, 1.0); |
| 140 |
value*= base_alpha * (1.0 - d_fade); |
| 141 |
|
| 142 |
float targ = 8.0 * g_distance_to_eye/min_visibility; |
| 143 |
value *= exp(-targ - targ * targ * targ * targ); |
| 144 |
value= clamp(value, 0.0, 1.0); |
| 145 |
|
| 146 |
// cloud shadows |
| 147 |
|
| 148 |
float cloud_shade = 1.0; |
| 149 |
|
| 150 |
if (cloud_shadow_flag == 1) |
| 151 |
{ |
| 152 |
vec2 eyePos = (gl_ModelViewMatrixInverse * vec4 (0.0, 0.0, 0.0, 1.0)).xy; |
| 153 |
vec2 relPos = g_rawpos - eyePos; |
| 154 |
|
| 155 |
|
| 156 |
cloud_shade = shadow_func_nearest(relPos.x, relPos.y, 1.0, g_distance_to_eye); |
| 157 |
} |
| 158 |
|
| 159 |
// lighting is very simple, the ground underneath should do most of it |
| 160 |
|
| 161 |
vec3 N = normalize (gl_NormalMatrix * g_normal); |
| 162 |
float NdotL = 0.5 + 1.0 * clamp(dot (N, gl_LightSource[0].position.xyz), 0.0, 1.0) * cloud_shade * getShadowing(); |
| 163 |
|
| 164 |
texel *= length(gl_LightSource[0].diffuse.rgb)/1.73 * scattering * NdotL; |
| 165 |
texel = clamp(texel, 0.0, 1.0); |
| 166 |
|
| 167 |
|
| 168 |
vec4 fragColor = vec4 (texel, value); |
| 169 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 170 |
fragColor = clamp(fragColor, 0.0, 1.0); |
| 171 |
|
| 172 |
gl_FragColor = fragColor; |
| 173 |
} |