| 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 visibility; |
| 12 |
uniform float air_pollution; |
| 13 |
uniform float sun_angle; |
| 14 |
uniform bool use_clouds; |
| 15 |
uniform bool use_cloud_shadows; |
| 16 |
uniform bool use_overlay; |
| 17 |
uniform sampler2D texture; |
| 18 |
uniform sampler2D shadowtex; |
| 19 |
uniform sampler2D grain_texture; |
| 20 |
|
| 21 |
float Noise2D(in vec2 coord, in float wavelength); |
| 22 |
vec3 filter_combined (in vec3 color) ; |
| 23 |
|
| 24 |
|
| 25 |
void main() |
| 26 |
{ |
| 27 |
vec3 n; |
| 28 |
float NdotL, NdotHV; |
| 29 |
vec4 color = gl_Color; |
| 30 |
vec3 lightDir = normalize(gl_LightSource[0].position.xyz); |
| 31 |
|
| 32 |
vec3 halfVector = normalize(lightDir + normalize(ecViewDir)); |
| 33 |
vec4 texel; |
| 34 |
vec4 shadowTexel; |
| 35 |
vec4 grainTexel; |
| 36 |
vec4 fragColor; |
| 37 |
vec4 specular = vec4(0.0); |
| 38 |
|
| 39 |
// If gl_Color.a == 0, this is a back-facing polygon and the |
| 40 |
// normal should be reversed. |
| 41 |
n = (2.0 * gl_Color.a - 1.0) * normal; |
| 42 |
n = normalize(n); |
| 43 |
|
| 44 |
vec3 VBinormal; |
| 45 |
VBinormal = cross(normal, VTangent); |
| 46 |
|
| 47 |
float xOffset = -0.005 * dot(lightDir, normalize(VTangent)); |
| 48 |
float yOffset = -0.005 * dot(lightDir, normalize(VBinormal)); |
| 49 |
|
| 50 |
if ((use_cloud_shadows)&&(use_clouds)) |
| 51 |
{shadowTexel = texture2D(shadowtex, vec2(gl_TexCoord[0].s-xOffset, gl_TexCoord[0].t-yOffset));} |
| 52 |
else |
| 53 |
{shadowTexel = vec4 (0.0,0.0,0.0,0.0);} |
| 54 |
|
| 55 |
texel = texture2D(texture, gl_TexCoord[0].st); |
| 56 |
float night_light = (1.0 -texel.a); |
| 57 |
texel.a = 1.0; |
| 58 |
grainTexel = texture2D(grain_texture, gl_TexCoord[0].st * 40.0); |
| 59 |
|
| 60 |
float noise = Noise2D( gl_TexCoord[0].st, 0.00005); |
| 61 |
noise += Noise2D( gl_TexCoord[0].st, 0.0002); |
| 62 |
noise += Noise2D( gl_TexCoord[0].st, 0.0001); |
| 63 |
|
| 64 |
noise= noise/3.0; |
| 65 |
|
| 66 |
|
| 67 |
vec3 light_specular = vec3 (1.0, 1.0, 1.0); |
| 68 |
NdotL = dot(n, lightDir); |
| 69 |
float NdotLraw = NdotL; |
| 70 |
// due to atmosphere scattering, we should make this harder |
| 71 |
NdotL = smoothstep(-0.2,0.2,NdotL); |
| 72 |
|
| 73 |
float intensity = length(diffuse_term); |
| 74 |
vec4 dawn = intensity * normalize (vec4 (1.0,0.5,0.3,1.0)); |
| 75 |
vec4 diff_term = mix(dawn, diffuse_term, smoothstep(0.0, 0.3, NdotL)); |
| 76 |
|
| 77 |
|
| 78 |
intensity = length(light_specular); |
| 79 |
light_specular = mix(dawn.rgb, light_specular, smoothstep(0.0, 0.4, NdotL)); |
| 80 |
|
| 81 |
float oceanness = smoothstep(0.0, 0.1,length(texel.rgb - vec3 (0.007,0.019, 0.078))); |
| 82 |
float specular_enhancement = 4.0 * (1.0 - oceanness); |
| 83 |
|
| 84 |
if (use_overlay) { |
| 85 |
//texel.rgb = mix(texel.rgb, grainTexel.rgb, 0.4* grainTexel.a * oceanness); |
| 86 |
texel.rgb = texel.rgb * (0.85 + 0.3 * noise); |
| 87 |
texel.r = smoothstep(0.0, 0.95, texel.r); |
| 88 |
texel.g = smoothstep(0.0, 0.95, texel.g); |
| 89 |
texel.b = smoothstep(0.0, 0.95, texel.b); |
| 90 |
float intensity = length(texel.rgb); |
| 91 |
texel.rgb = mix(texel.rgb, intensity * vec3 (1.0,1.0,1.0), 0.3); |
| 92 |
} |
| 93 |
|
| 94 |
//texel.rgb = vec3 (0.5,0.5,0.5); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
// strictly positive produces visible hard cut |
| 99 |
if (NdotL >= 0.0) { |
| 100 |
color += diff_term * NdotL * (1.0-shadowTexel.a); |
| 101 |
NdotHV = max(dot(n, halfVector), 0.0); |
| 102 |
if (gl_FrontMaterial.shininess > 0.0) |
| 103 |
specular.rgb = (gl_FrontMaterial.specular.rgb * specular_enhancement |
| 104 |
* light_specular * (1.0-shadowTexel.a) |
| 105 |
* pow(NdotHV, gl_FrontMaterial.shininess)); |
| 106 |
} |
| 107 |
color.a = diffuse_term.a; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
// This shouldn't be necessary, but our lighting becomes very |
| 112 |
// saturated. Clamping the color before modulating by the texture |
| 113 |
// is closer to what the OpenGL fixed function pipeline does. |
| 114 |
color = clamp(color, 0.0, 1.0); |
| 115 |
|
| 116 |
fragColor = color * texel + specular; |
| 117 |
|
| 118 |
|
| 119 |
float night_light_factor = night_light * (1.0 - smoothstep(-0.3, 0.0, NdotLraw)); |
| 120 |
|
| 121 |
float noise_factor = (0.4 + 0.6* smoothstep(0.7 - 0.4* night_light,0.9 - 0.4 * night_light,noise)); |
| 122 |
night_light_factor *= noise_factor; |
| 123 |
|
| 124 |
|
| 125 |
vec3 light_color = vec3(1.0, 0.7, 0.3); |
| 126 |
vec3 central_light_color = vec3 (1.0, 1.0, 1.0); |
| 127 |
light_color = mix(light_color, central_light_color, smoothstep(0.3, 0.6,noise*noise * night_light)); |
| 128 |
|
| 129 |
fragColor.rgb += light_color * night_light_factor * 1.4; |
| 130 |
|
| 131 |
float angle = dot(normalize(ecViewDir), normalize(normal)); |
| 132 |
float distance_through_atmosphere = min(10.0 / (abs(angle)+0.001),500.0); |
| 133 |
|
| 134 |
float fogLighting = clamp(NdotL,0.0,1.0) * length(diff_term.rgb/1.73); |
| 135 |
|
| 136 |
|
| 137 |
vec4 fogColor = vec4 (0.83,0.9,1.0,1.0) * fogLighting; |
| 138 |
vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * fogLighting; |
| 139 |
|
| 140 |
|
| 141 |
float fogFactor = exp(-distance_through_atmosphere/(visibility/1000.0)); |
| 142 |
float rayleighFactor = exp(-distance_through_atmosphere/(300.0 / (1.0 + 4.0 * air_pollution)) ); |
| 143 |
|
| 144 |
fragColor.rgb = mix(rayleighColor, fragColor.rgb, rayleighFactor); |
| 145 |
fragColor = mix(fogColor, fragColor, fogFactor); |
| 146 |
|
| 147 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 148 |
|
| 149 |
gl_FragColor = clamp(fragColor, 0.0, 1.0); |
| 150 |
|
| 151 |
} |