| 1 |
// -*-C++-*- |
| 2 |
|
| 3 |
// Ambient term comes in gl_Color.rgb. |
| 4 |
#version 120 |
| 5 |
|
| 6 |
varying vec4 diffuse_term; |
| 7 |
varying vec3 normal; |
| 8 |
varying vec3 ecViewDir; |
| 9 |
varying vec3 VTangent; |
| 10 |
|
| 11 |
uniform float shade_effect; |
| 12 |
uniform float sun_angle; |
| 13 |
uniform float air_pollution; |
| 14 |
uniform float moonlight; |
| 15 |
|
| 16 |
uniform float roi_x1; |
| 17 |
uniform float roi_y1; |
| 18 |
uniform float lightning; |
| 19 |
uniform float cloudcover_bias; |
| 20 |
|
| 21 |
uniform bool use_overlay; |
| 22 |
uniform bool use_cloud_normals; |
| 23 |
|
| 24 |
uniform sampler2D texture; |
| 25 |
uniform sampler2D structure_texture; |
| 26 |
|
| 27 |
float Noise2D(in vec2 coord, in float wavelength); |
| 28 |
vec3 filter_combined (in vec3 color) ; |
| 29 |
vec3 moonlight_perception (in vec3 light); |
| 30 |
|
| 31 |
float add_cosines (in float cos1, in float cos2, in float sign) |
| 32 |
{ |
| 33 |
|
| 34 |
float sin1 = sqrt(1.0 - pow(cos1, 2.0)); |
| 35 |
float sin2 = sqrt(1.0 - pow(cos2, 2.0)); |
| 36 |
|
| 37 |
return cos1 * cos2 + sign * sin1 * sin2; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
vec3 lightning_color (in vec2 coord) |
| 42 |
{ |
| 43 |
|
| 44 |
vec2 roi1 = vec2 (roi_x1, roi_y1); |
| 45 |
|
| 46 |
float strength = 1.0 - smoothstep(0.0, 0.005, length(roi1 - coord)); |
| 47 |
|
| 48 |
return strength * vec3 (0.43, 0.57, 1.0); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void main() |
| 54 |
{ |
| 55 |
vec3 n; |
| 56 |
float NdotL, NdotHV, NdotLraw; |
| 57 |
vec4 color = gl_Color; |
| 58 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 59 |
|
| 60 |
vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewDir)); |
| 61 |
vec4 texel; |
| 62 |
vec4 ref_texel; |
| 63 |
vec4 structureTexel; |
| 64 |
|
| 65 |
vec4 fragColor; |
| 66 |
vec4 specular = vec4(0.0, 0.0, 0.0, 0.0); |
| 67 |
|
| 68 |
n = normalize(normal); |
| 69 |
|
| 70 |
|
| 71 |
vec3 light_specular = vec3 (1.0, 1.0, 1.0); |
| 72 |
NdotL = dot(n, normalize(lightDir)); |
| 73 |
NdotLraw = NdotL; |
| 74 |
NdotL = smoothstep(-0.2,0.2,NdotL); |
| 75 |
|
| 76 |
float intensity = length(diffuse_term.rgb); |
| 77 |
|
| 78 |
vec3 dawn_color = mix (vec3 (1.0,0.7,0.4), vec3 (1.0,0.4,0.2), air_pollution); |
| 79 |
|
| 80 |
vec3 dawn = intensity * 1.2 * normalize (dawn_color); |
| 81 |
|
| 82 |
vec4 diff_term = mix(vec4(dawn, 1.0), diffuse_term, smoothstep(0.0, 0.45, NdotL)); |
| 83 |
|
| 84 |
|
| 85 |
vec2 grad_dir = vec2 (1.0, 0.0); |
| 86 |
|
| 87 |
vec3 tangent = normalize(VTangent); |
| 88 |
vec3 binormal = cross(n, tangent); |
| 89 |
float NdotL2 = 1.0; |
| 90 |
|
| 91 |
texel = texture2D(texture, gl_TexCoord[0].st); |
| 92 |
ref_texel = texel; |
| 93 |
|
| 94 |
float sign = -1.0; |
| 95 |
float ml_fact = 1.0; |
| 96 |
|
| 97 |
|
| 98 |
if (use_cloud_normals) |
| 99 |
{ |
| 100 |
vec2 sun2d = vec2 (0.0, 1.0); |
| 101 |
|
| 102 |
float xOffset = -1.0 * dot(normalize(lightDir), tangent); |
| 103 |
float yOffset = -1.0 * dot(normalize(lightDir), binormal); |
| 104 |
|
| 105 |
grad_dir = normalize (vec2 (xOffset, yOffset)); |
| 106 |
|
| 107 |
vec4 comp_texel = texture2D(texture, gl_TexCoord[0].st - 0.0005 * grad_dir); |
| 108 |
|
| 109 |
// parallax mapping |
| 110 |
|
| 111 |
xOffset = -1.0 * dot(ecViewDir, tangent); |
| 112 |
yOffset = -1.0 * dot(ecViewDir, binormal); |
| 113 |
|
| 114 |
grad_dir = normalize (vec2 (xOffset, yOffset)); |
| 115 |
|
| 116 |
texel = texture2D(texture, gl_TexCoord[0].st - 0.0005 * grad_dir * ref_texel.a * 0.7); |
| 117 |
|
| 118 |
// relief shading based on gradient and parallax lookup |
| 119 |
|
| 120 |
float slope = shade_effect * (comp_texel.a - ref_texel.a) * texel.a; |
| 121 |
if (slope < 0.0) {sign = 1.0;} |
| 122 |
|
| 123 |
vec2 snormal = normalize(vec2 (slope, 1.0)); |
| 124 |
|
| 125 |
NdotL2 = dot (snormal, sun2d); |
| 126 |
NdotL = add_cosines(NdotL, NdotL2, sign ); |
| 127 |
|
| 128 |
ml_fact = 0.5 + 1.0 * add_cosines(0.0, NdotL2, sign); |
| 129 |
|
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
ref_texel = texel; |
| 134 |
texel.a = pow(texel.a,1.0/cloudcover_bias); |
| 135 |
texel.a = clamp(texel.a, 0.0, 1.0); |
| 136 |
|
| 137 |
|
| 138 |
color += diff_term * max(NdotL, 0.15) ; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
color.rgb *= smoothstep(-0.2, -0.1, NdotLraw); |
| 145 |
// |
| 146 |
|
| 147 |
float darkness_fact = 1.0 - smoothstep(0.0,0.2, length(color.rgb)); |
| 148 |
color.rgb += lightning_color(gl_TexCoord[0].st) * (1.0 - texel.a) * lightning * darkness_fact; |
| 149 |
|
| 150 |
vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight; |
| 151 |
moonLightColor = moonlight_perception (moonLightColor); |
| 152 |
color.rgb += moonLightColor * ml_fact; |
| 153 |
|
| 154 |
color.a = 1.0;//diffuse_term.a; |
| 155 |
color = clamp(color, 0.0, 1.0); |
| 156 |
|
| 157 |
structureTexel = texture2D(structure_texture, 20.0 * gl_TexCoord[0].st); |
| 158 |
|
| 159 |
float noise = Noise2D( gl_TexCoord[0].st, 0.01); |
| 160 |
noise += Noise2D( gl_TexCoord[0].st, 0.005); |
| 161 |
noise += Noise2D( gl_TexCoord[0].st, 0.002); |
| 162 |
|
| 163 |
|
| 164 |
vec4 noiseTexel = vec4 (1.0,1.0,1.0, 0.5* noise * texel.a); |
| 165 |
structureTexel = mix(structureTexel, noiseTexel,noiseTexel.a); |
| 166 |
|
| 167 |
structureTexel = mix(structureTexel, texel, clamp(1.5 * ref_texel.a * (cloudcover_bias - 1.0), 0.0, 1.0)); |
| 168 |
|
| 169 |
|
| 170 |
if (use_overlay) |
| 171 |
{ |
| 172 |
texel = vec4(structureTexel.rgb, smoothstep(0.0, 0.5,texel.a) * structureTexel.a); |
| 173 |
//texel.a = pow(texel.a,1.0/cloudcover_bias); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
texel.a = clamp((1.0 + darkness_fact) * texel.a, 0.0, 1.0); |
| 178 |
|
| 179 |
|
| 180 |
fragColor = color * texel; |
| 181 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 182 |
|
| 183 |
gl_FragColor = clamp(fragColor, 0.0, 1.0); |
| 184 |
|
| 185 |
|
| 186 |
} |