| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
varying vec2 rawPos; |
| 5 |
varying vec2 nPos; |
| 6 |
varying vec3 vertPos; |
| 7 |
varying vec3 normal; |
| 8 |
varying vec3 refl_vec; |
| 9 |
varying vec3 light_diffuse; |
| 10 |
varying float splash_angle; |
| 11 |
varying float Mie; |
| 12 |
varying float ambient_fraction; |
| 13 |
|
| 14 |
uniform sampler2D texture; |
| 15 |
uniform sampler2D frost_texture; |
| 16 |
uniform sampler2D func_texture; |
| 17 |
uniform samplerCube cube_texture; |
| 18 |
uniform samplerCube cube_light_texture; |
| 19 |
|
| 20 |
uniform vec4 tint; |
| 21 |
uniform vec3 overlay_color; |
| 22 |
|
| 23 |
|
| 24 |
uniform float rain_norm; |
| 25 |
uniform float ground_splash_norm; |
| 26 |
uniform float frost_level; |
| 27 |
uniform float fog_level; |
| 28 |
uniform float reflection_strength; |
| 29 |
uniform float overlay_alpha; |
| 30 |
uniform float overlay_glare; |
| 31 |
uniform float splash_x; |
| 32 |
uniform float splash_y; |
| 33 |
uniform float splash_z; |
| 34 |
uniform float lightmap_r_factor; |
| 35 |
uniform float lightmap_g_factor; |
| 36 |
uniform float lightmap_b_factor; |
| 37 |
uniform float lightmap_a_factor; |
| 38 |
uniform float osg_SimulationTime; |
| 39 |
|
| 40 |
uniform int use_reflection; |
| 41 |
uniform int use_reflection_lightmap; |
| 42 |
uniform int use_mask; |
| 43 |
uniform int use_wipers; |
| 44 |
uniform int use_overlay; |
| 45 |
uniform int adaptive_mapping; |
| 46 |
uniform int lightmap_multi; |
| 47 |
|
| 48 |
uniform vec3 lightmap_r_color; |
| 49 |
uniform vec3 lightmap_g_color; |
| 50 |
uniform vec3 lightmap_b_color; |
| 51 |
uniform vec3 lightmap_a_color; |
| 52 |
|
| 53 |
float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density); |
| 54 |
float DropletNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density); |
| 55 |
float Noise2D(in vec2 coord, in float wavelength); |
| 56 |
vec3 filter_combined (in vec3 color) ; |
| 57 |
|
| 58 |
void main() |
| 59 |
{ |
| 60 |
|
| 61 |
vec4 texel; |
| 62 |
vec4 frost_texel; |
| 63 |
vec4 func_texel; |
| 64 |
|
| 65 |
texel = texture2D(texture, gl_TexCoord[0].st); |
| 66 |
texel *=gl_Color; |
| 67 |
|
| 68 |
vec2 frost_coords; |
| 69 |
|
| 70 |
if (adaptive_mapping == 1) {frost_coords = gl_TexCoord[0].st * 7.0;} |
| 71 |
else if (adaptive_mapping ==2) {frost_coords = nPos * 7.0;} |
| 72 |
else {frost_coords = vertPos.xy * 7.0;} |
| 73 |
|
| 74 |
frost_texel = texture2D(frost_texture, frost_coords); |
| 75 |
func_texel = texture2D(func_texture, gl_TexCoord[0].st); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
float noise_003m = Noise2D(vertPos.xy, 0.03); |
| 80 |
float noise_0003m = Noise2D(vertPos.xy, 0.003); |
| 81 |
|
| 82 |
|
| 83 |
// environment reflection, including a lightmap for the reflections |
| 84 |
|
| 85 |
vec4 reflection = textureCube(cube_texture, refl_vec); |
| 86 |
vec4 reflection_lighting = textureCube(cube_light_texture, refl_vec); |
| 87 |
|
| 88 |
vec3 lightmapcolor = vec3(0.0, 0.0, 0.0); |
| 89 |
|
| 90 |
|
| 91 |
if (use_reflection_lightmap == 1) |
| 92 |
{ |
| 93 |
vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor, lightmap_b_factor, lightmap_a_factor); |
| 94 |
|
| 95 |
if (lightmap_multi > 0 ) |
| 96 |
{ |
| 97 |
lightmapFactor = lightmapFactor * reflection_lighting; |
| 98 |
lightmapcolor = lightmap_r_color * lightmapFactor.r + |
| 99 |
lightmap_g_color * lightmapFactor.g + |
| 100 |
lightmap_b_color * lightmapFactor.b + |
| 101 |
lightmap_a_color * lightmapFactor.a ; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
lightmapcolor = reflection_lighting.rgb * lightmap_r_color * lightmapFactor.r; |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
float lightmap_intensity = length(lightmapcolor); |
| 111 |
float light_fraction = clamp(lightmap_intensity / (length(light_diffuse.rgb) + 0.01), 0.0, 5.0); |
| 112 |
|
| 113 |
if (light_fraction < 1.0) {light_fraction = smoothstep(0.7, 1.0, light_fraction);} |
| 114 |
|
| 115 |
|
| 116 |
if (use_reflection ==1) |
| 117 |
{ |
| 118 |
// to determine whether what we see reflected is currently in light, we make the somewhat drastic |
| 119 |
// assumption that its normal will be opposite to the glass normal |
| 120 |
// (which is mostly truish in a normal cockpit) |
| 121 |
float reflection_shade = ambient_fraction + (1.0-ambient_fraction) * max(0.0, dot (normalize(normal), normalize(gl_LightSource[0].position.xyz))); |
| 122 |
|
| 123 |
texel.rgb = mix(texel.rgb, reflection.rgb, (reflection_strength * reflection_shade * (1.0-Mie))); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
//texel.rgb = mix(texel.rgb, lightmapcolor.rgb, lightmap_intensity); |
| 128 |
|
| 129 |
// overlay pattern |
| 130 |
|
| 131 |
if ((use_mask == 1) && (use_overlay==1)) |
| 132 |
{ |
| 133 |
vec4 overlay_texel = vec4(overlay_color, overlay_alpha); |
| 134 |
overlay_texel.rgb *= light_diffuse.rgb* (1.0 + (1.0 + overlay_glare)*Mie); |
| 135 |
overlay_texel.a *=(1.0 + overlay_glare* Mie); |
| 136 |
texel = mix(texel, overlay_texel, func_texel.b * overlay_texel.a); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
// frost |
| 141 |
|
| 142 |
float fth = (1.0-frost_level) * 0.4 + 0.3; |
| 143 |
float fbl = 0.2 * frost_level; |
| 144 |
|
| 145 |
|
| 146 |
float frost_factor = (fbl + (1.0-fbl)* smoothstep(fth,fth+0.2,noise_003m)) * (4.0 + 4.0* Mie); |
| 147 |
|
| 148 |
|
| 149 |
float background_frost = 0.5 * smoothstep(0.7,1.0,frost_level); |
| 150 |
frost_texel.rgb = mix(frost_texel.rgb, vec3 (0.5,0.5,0.5), (1.0- smoothstep(0.0,0.02,frost_texel.a))); |
| 151 |
frost_texel.a =max(frost_texel.a, background_frost * (1.0- smoothstep(0.0,0.02,frost_texel.a))); |
| 152 |
|
| 153 |
frost_texel *= vec4(light_diffuse.rgb,0.5) * (1.0 + 3.0 * Mie); |
| 154 |
|
| 155 |
frost_factor = max(frost_factor, 0.8*background_frost); |
| 156 |
|
| 157 |
|
| 158 |
texel.rgb = mix(texel.rgb, frost_texel.rgb, frost_texel.a * frost_factor * smoothstep(0.0,0.1,frost_level)); |
| 159 |
texel.a = max(texel.a, frost_texel.a * frost_level); |
| 160 |
|
| 161 |
// rain splashes |
| 162 |
|
| 163 |
vec3 splash_vec = vec3 (splash_x, splash_y, splash_z); |
| 164 |
float splash_speed = length(splash_vec); |
| 165 |
|
| 166 |
|
| 167 |
float rain_factor = 0.0; |
| 168 |
|
| 169 |
float rnorm = max(rain_norm, ground_splash_norm); |
| 170 |
|
| 171 |
if (rnorm > 0.0) |
| 172 |
{ |
| 173 |
float droplet_size = (0.5 + 0.8 * rnorm) * (1.0 - 0.1 * splash_speed); |
| 174 |
vec2 rainPos = vec2 (rawPos.x * splash_speed, rawPos.y / splash_speed ); |
| 175 |
rainPos.y = rainPos.y - 0.1 * smoothstep(1.0,2.0, splash_speed) * osg_SimulationTime; |
| 176 |
if (splash_angle> 0.0) |
| 177 |
{ |
| 178 |
// the dynamically impacting raindrops |
| 179 |
|
| 180 |
float time_shape = 1.0; |
| 181 |
float base_rate = 6.0 + 3.0 * rnorm + 4.0 * (splash_speed - 1.0); |
| 182 |
float base_density = 0.6 * rnorm + 0.4 * (splash_speed -1.0); |
| 183 |
if ((use_mask ==1)&&(use_wipers==1)) {base_density *= (1.0 - 0.5 * func_texel.g);} |
| 184 |
|
| 185 |
float time_fact1 = (sin(base_rate*osg_SimulationTime)); |
| 186 |
float time_fact2 = (sin(base_rate*osg_SimulationTime + 1.570)); |
| 187 |
float time_fact3 = (sin(base_rate*osg_SimulationTime + 3.1415)); |
| 188 |
float time_fact4 = (sin(base_rate*osg_SimulationTime + 4.712)); |
| 189 |
|
| 190 |
time_fact1 = smoothstep(0.0,1.0, time_fact1); |
| 191 |
time_fact2 = smoothstep(0.0,1.0, time_fact2); |
| 192 |
time_fact3 = smoothstep(0.0,1.0, time_fact3); |
| 193 |
time_fact4 = smoothstep(0.0,1.0, time_fact4); |
| 194 |
|
| 195 |
rain_factor += DotNoise2D(rawPos.xy, 0.02 * droplet_size ,0.5, base_density ) * time_fact1; |
| 196 |
rain_factor += DotNoise2D(rainPos.xy, 0.03 * droplet_size,0.4, base_density) * time_fact2; |
| 197 |
rain_factor += DotNoise2D(rawPos.xy, 0.04 * droplet_size ,0.3, base_density)* time_fact3; |
| 198 |
rain_factor += DotNoise2D(rainPos.xy, 0.05 * droplet_size ,0.25, base_density)* time_fact4; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
// the static pattern of small droplets created by the splashes |
| 203 |
|
| 204 |
float sweep = min(1./splash_speed,1.0); |
| 205 |
if ((use_mask ==1)&&(use_wipers==1)) {sweep *= (1.0 - func_texel.g);} |
| 206 |
if (adaptive_mapping ==2) {rainPos = nPos;} |
| 207 |
rain_factor += DropletNoise2D(rainPos.xy, 0.02 * droplet_size ,0.5, 0.6* rnorm * sweep); |
| 208 |
rain_factor += DotNoise2D(rainPos.xy, 0.012 * droplet_size ,0.7, 0.6* rnorm * sweep); |
| 209 |
} |
| 210 |
|
| 211 |
rain_factor = smoothstep(0.1,0.2, rain_factor) * (1.0 - smoothstep(0.4,1.0, rain_factor) * (0.2+0.8*noise_0003m)); |
| 212 |
|
| 213 |
|
| 214 |
vec4 rainColor = vec4 (0.2,0.2, 0.2, 0.6 - 0.3 * smoothstep(1.0,2.0, splash_speed)); |
| 215 |
rainColor.rgb *= length(light_diffuse)/1.73; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
// glass tint |
| 220 |
|
| 221 |
|
| 222 |
vec4 outerColor = mix(texel, rainColor, rain_factor); |
| 223 |
// now mix illuminated reflections in |
| 224 |
|
| 225 |
vec3 reflLitColor = reflection.rgb * lightmapcolor.rgb; |
| 226 |
|
| 227 |
outerColor.rgb = mix(outerColor.rgb, reflLitColor, clamp(reflection_strength * light_fraction,0.0,1.0)); |
| 228 |
outerColor.a = max(outerColor.a, 0.1 * light_fraction * reflection_strength); |
| 229 |
|
| 230 |
outerColor *= tint; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
// fogging - this is inside the glass |
| 236 |
|
| 237 |
vec4 fog_texel = vec4 (0.6,0.6,0.6, fog_level); |
| 238 |
|
| 239 |
if (use_mask == 1) {fog_texel.a = fog_texel.a * func_texel.r;} |
| 240 |
|
| 241 |
fog_texel *= vec4(light_diffuse.rgb,1.0); |
| 242 |
fog_texel.rgb *= (1.0 + 3.0 * Mie); |
| 243 |
fog_texel.a *= min((1.0 + 0.5 * Mie), 0.85); |
| 244 |
|
| 245 |
|
| 246 |
vec4 fragColor; |
| 247 |
|
| 248 |
fragColor.rgb = mix(outerColor.rgb, fog_texel.rgb, fog_texel.a); |
| 249 |
fragColor.a = max(outerColor.a, fog_texel.a); |
| 250 |
|
| 251 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 252 |
|
| 253 |
gl_FragColor = clamp(fragColor,0.0,1.0); |
| 254 |
|
| 255 |
|
| 256 |
} |