1
0
Fork 0
fgdata/Shaders/water_lightfield.frag

757 lines
22 KiB
GLSL
Raw Normal View History

2012-04-26 10:03:51 +00:00
// This shader is mostly an adaptation of the shader found at
// http://www.bonzaisoftware.com/water_tut.html and its glsl conversion
// available at http://forum.bonzaisoftware.com/viewthread.php?tid=10
// <20> Michael Horsch - 2005
// Major update and revisions - 2011-10-07
// <20> Emilian Huminiuc and Vivian Meazza
2012-11-20 13:11:21 +00:00
// ported to lightfield shading Thorsten Renk 2012
2012-04-26 10:03:51 +00:00
#version 120
uniform sampler2D water_normalmap;
uniform sampler2D water_dudvmap;
uniform sampler2D sea_foam;
uniform sampler2D perlin_normalmap;
uniform sampler2D ice_texture;
2013-11-23 10:15:52 +00:00
uniform sampler2D topo_map;
2012-04-26 10:03:51 +00:00
uniform sampler3D Noise;
uniform float saturation, Overcast, WindE, WindN;
uniform float osg_SimulationTime;
varying vec4 waterTex1; //moving texcoords
varying vec4 waterTex2; //moving texcoords
varying vec4 waterTex4; //viewts
varying vec3 viewerdir;
varying vec3 lightdir;
varying vec3 relPos;
varying vec3 rawPos;
2013-11-23 10:15:52 +00:00
varying vec2 TopoUV;
2012-04-26 10:03:51 +00:00
varying float earthShade;
varying float yprime_alt;
varying float mie_angle;
varying float steepness;
2012-04-26 10:03:51 +00:00
uniform float WaveFreq ;
uniform float WaveAmp ;
uniform float WaveSharp ;
uniform float WaveAngle ;
uniform float WaveFactor ;
uniform float WaveDAngle ;
uniform float normalmap_dds;
2012-04-26 10:03:51 +00:00
uniform float hazeLayerAltitude;
uniform float terminator;
2012-11-20 13:11:21 +00:00
uniform float terrain_alt;
2012-04-26 10:03:51 +00:00
uniform float avisibility;
uniform float visibility;
uniform float overcast;
uniform float scattering;
uniform float ground_scattering;
2012-11-20 13:11:21 +00:00
uniform float cloud_self_shading;
2012-04-26 10:03:51 +00:00
uniform float eye_alt;
uniform float fogstructure;
uniform float ice_cover;
2012-04-26 10:03:51 +00:00
uniform float sea_r;
uniform float sea_g;
uniform float sea_b;
uniform float air_pollution;
2012-04-26 10:03:51 +00:00
uniform int quality_level;
uniform int ocean_flag;
2014-03-11 10:25:47 +00:00
uniform int cloud_shadow_flag;
2012-11-20 13:11:21 +00:00
vec3 specular_light;
//uniform int wquality_level;
2012-04-26 10:03:51 +00:00
const float terminator_width = 200000.0;
const float EarthRadius = 5800000.0;
////fog "include" /////
//uniform int fogType;
2014-03-11 10:25:47 +00:00
//vec3 fog_Func(vec3 color, int type);
float shadow_func (in float x, in float y, in float noise, in float dist);
float fog_func (in float targ, in float alt);
float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
float alt_factor(in float eye_alt, in float vertex_alt);
vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
2012-04-26 10:03:51 +00:00
//////////////////////
/////// functions /////////
2014-03-11 10:25:47 +00:00
float rand2D(in vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float rand3D(in vec3 co){
return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453);
}
float cosine_interpolate(in float a, in float b, in float x)
{
float ft = x * 3.1415927;
float f = (1.0 - cos(ft)) * .5;
return a*(1.0-f) + b*f;
}
float simple_interpolate(in float a, in float b, in float x)
{
return a + smoothstep(0.0,1.0,x) * (b-a);
}
float interpolatedNoise2D(in float x, in float y)
{
float integer_x = x - fract(x);
float fractional_x = x - integer_x;
float integer_y = y - fract(y);
float fractional_y = y - integer_y;
float v1 = rand2D(vec2(integer_x, integer_y));
float v2 = rand2D(vec2(integer_x+1.0, integer_y));
float v3 = rand2D(vec2(integer_x, integer_y+1.0));
float v4 = rand2D(vec2(integer_x+1.0, integer_y +1.0));
float i1 = simple_interpolate(v1 , v2 , fractional_x);
float i2 = simple_interpolate(v3 , v4 , fractional_x);
return simple_interpolate(i1 , i2 , fractional_y);
}
float interpolatedNoise3D(in float x, in float y, in float z)
{
float integer_x = x - fract(x);
float fractional_x = x - integer_x;
float integer_y = y - fract(y);
float fractional_y = y - integer_y;
float integer_z = z - fract(z);
float fractional_z = z - integer_z;
float v1 = rand3D(vec3(integer_x, integer_y, integer_z));
float v2 = rand3D(vec3(integer_x+1.0, integer_y, integer_z));
float v3 = rand3D(vec3(integer_x, integer_y+1.0, integer_z));
float v4 = rand3D(vec3(integer_x+1.0, integer_y +1.0, integer_z));
float v5 = rand3D(vec3(integer_x, integer_y, integer_z+1.0));
float v6 = rand3D(vec3(integer_x+1.0, integer_y, integer_z+1.0));
float v7 = rand3D(vec3(integer_x, integer_y+1.0, integer_z+1.0));
float v8 = rand3D(vec3(integer_x+1.0, integer_y +1.0, integer_z+1.0));
float i1 = simple_interpolate(v1,v5, fractional_z);
float i2 = simple_interpolate(v2,v6, fractional_z);
float i3 = simple_interpolate(v3,v7, fractional_z);
float i4 = simple_interpolate(v4,v8, fractional_z);
float ii1 = simple_interpolate(i1,i2,fractional_x);
float ii2 = simple_interpolate(i3,i4,fractional_x);
return simple_interpolate(ii1 , ii2 , fractional_y);
}
float Noise2D(in vec2 coord, in float wavelength)
{
return interpolatedNoise2D(coord.x/wavelength, coord.y/wavelength);
}
float Noise3D(in vec3 coord, in float wavelength)
{
return interpolatedNoise3D(coord.x/wavelength, coord.y/wavelength, coord.z/wavelength);
}
2012-04-26 10:03:51 +00:00
void rotationmatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
// wave functions ///////////////////////
struct Wave {
float freq; // 2*PI / wavelength
float amp; // amplitude
float phase; // speed * 2*PI / wavelength
vec2 dir;
};
Wave wave0 = Wave(1.0, 1.0, 0.5, vec2(0.97, 0.25));
Wave wave1 = Wave(2.0, 0.5, 1.3, vec2(0.97, -0.25));
Wave wave2 = Wave(1.0, 1.0, 0.6, vec2(0.95, -0.3));
Wave wave3 = Wave(2.0, 0.5, 1.4, vec2(0.99, 0.1));
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
float evaluateWave(in Wave w, vec2 pos, float t)
{
return w.amp * sin( dot(w.dir, pos) * w.freq + t * w.phase);
}
// derivative of wave function
float evaluateWaveDeriv(Wave w, vec2 pos, float t)
{
return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
// sharp wave functions
float evaluateWaveSharp(Wave w, vec2 pos, float t, float k)
{
return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);
}
float evaluateWaveDerivSharp(Wave w, vec2 pos, float t, float k)
{
return k*w.freq*w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k - 1) * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
void sumWaves(float angle, float dangle, float windScale, float factor, out float ddx, float ddy)
{
mat4 RotationMatrix;
float deriv;
2012-11-20 13:11:21 +00:00
vec4 P = waterTex1 * 1024;
2012-04-26 10:03:51 +00:00
rotationmatrix(radians(angle + dangle * windScale + 0.6 * sin(P.x * factor)), RotationMatrix);
P *= RotationMatrix;
P.y += evaluateWave(wave0, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave0, P.xz, osg_SimulationTime );
ddx = deriv * wave0.dir.x;
ddy = deriv * wave0.dir.y;
P.y += evaluateWave(wave1, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave1, P.xz, osg_SimulationTime);
ddx += deriv * wave1.dir.x;
ddy += deriv * wave1.dir.y;
P.y += evaluateWaveSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave2.dir.x;
ddy += deriv * wave2.dir.y;
P.y += evaluateWaveSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave3.dir.x;
ddy += deriv * wave3.dir.y;
}
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.0;}
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
}
// this determines how light is attenuated in the distance
// physically this should be exp(-arg) but for technical reasons we use a sharper cutoff
// for distance > visibility
void main(void)
{
2012-11-20 13:11:21 +00:00
2013-11-23 10:15:52 +00:00
vec3 shadedFogColor = vec3(0.65, 0.67, 0.78);
2012-11-20 13:11:21 +00:00
float effective_scattering = min(scattering, cloud_self_shading);
2012-04-26 10:03:51 +00:00
float dist = length(relPos);
const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
float noise_50m = Noise3D(rawPos.xyz, 50.0);
float noise_250m = Noise3D(rawPos.xyz,250.0);
2014-03-11 10:25:47 +00:00
float noise_500m = Noise3D(rawPos.xyz,500.0);
float noise_1500m = Noise3D(rawPos.xyz,1500.0);
float noise_2000m = Noise3D(rawPos.xyz,2000.0);
float noise_2500m = Noise3D(rawPos.xyz, 2500.0);
2013-11-23 10:15:52 +00:00
// get depth map
vec4 topoTexel = texture2D(topo_map, TopoUV);
float floorMixFactor = smoothstep(0.3, 0.985, topoTexel.a);
vec3 floorColour = topoTexel.rgb;
2012-04-26 10:03:51 +00:00
mat4 RotationMatrix;
// compute direction to viewer
vec3 E = normalize(viewerdir);
// compute direction to light source
vec3 L = lightdir; // normalize(lightdir);
// half vector
vec3 Hv = normalize(L + E);
//vec3 Normal = normalize(normal);
vec3 Normal = vec3 (0.0, 0.0, 1.0);
const float water_shininess = 240.0;
// approximate cloud cover
//float cover = 0.0;
//bool Status = true;
float windEffect = sqrt( WindE*WindE + WindN*WindN ) * 0.6; //wind speed in kt
float windScale = 15.0/(3.0 + windEffect); //wave scale
float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect); //low windspeed wave filter
float waveRoughness = 0.01 + smoothstep(0.0, 40.0, windEffect); //wave roughness filter
float mixFactor = 0.2 + 0.02 * smoothstep(0.0, 50.0, windEffect);
//mixFactor = 0.2;
mixFactor = clamp(mixFactor, 0.3, 0.8);
// there's no need to do wave patterns or foam for pixels which are so far away that we can't actually see them
// we only need detail in the near zone or where the sun reflection is
int detail_flag;
2012-11-20 13:11:21 +00:00
if ((dist > 15000.0) && (dot(normalize(vec3 (lightdir.x, lightdir.y, 0.0) ), normalize(relPos)) < 0.7 )) {detail_flag = 0;}
2012-04-26 10:03:51 +00:00
else {detail_flag = 1;}
2012-11-20 13:11:21 +00:00
//detail_flag = 1;
2012-04-26 10:03:51 +00:00
// sine waves
float ddx, ddx1, ddx2, ddx3, ddy, ddy1, ddy2, ddy3;
2012-11-20 13:11:21 +00:00
float angle;
ddx = 0.0, ddy = 0.0;
ddx1 = 0.0, ddy1 = 0.0;
ddx2 = 0.0, ddy2 = 0.0;
ddx3 = 0.0, ddy3 = 0.0;
2012-04-26 10:03:51 +00:00
if (detail_flag == 1)
{
2012-11-20 13:11:21 +00:00
angle = 0.0;
2012-04-26 10:03:51 +00:00
wave0.freq = WaveFreq ;
wave0.amp = WaveAmp;
2012-11-20 13:11:21 +00:00
wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
angle -= 45;
2012-04-26 10:03:51 +00:00
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = WaveAmp * 1.25;
2012-11-20 13:11:21 +00:00
wave1.dir = vec2(0.70710, -0.7071); //vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
angle += 30;
2012-04-26 10:03:51 +00:00
wave2.freq = WaveFreq * 3.5;
wave2.amp = WaveAmp * 0.75;
2012-11-20 13:11:21 +00:00
wave2.dir = vec2(0.96592, -0.2588);// vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
angle -= 50;
2012-04-26 10:03:51 +00:00
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = WaveAmp * 0.75;
2012-11-20 13:11:21 +00:00
wave3.dir = vec2(0.42261, -0.9063); //vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
// sum waves
2012-04-26 10:03:51 +00:00
sumWaves(WaveAngle, -1.5, windScale, WaveFactor, ddx, ddy);
sumWaves(WaveAngle, 1.5, windScale, WaveFactor, ddx1, ddy1);
//reset the waves
angle = 0.0;
float waveamp = WaveAmp * 0.75;
wave0.freq = WaveFreq ;
wave0.amp = waveamp;
2012-11-20 13:11:21 +00:00
wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
angle -= 20;
2012-04-26 10:03:51 +00:00
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = waveamp * 1.25;
2012-11-20 13:11:21 +00:00
wave1.dir = vec2(0.93969, -0.34202);// vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
angle += 35;
2012-04-26 10:03:51 +00:00
wave2.freq = WaveFreq * 3.5;
wave2.amp = waveamp * 0.75;
2012-11-20 13:11:21 +00:00
wave2.dir = vec2(0.965925, 0.25881); //vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
angle -= 45;
2012-04-26 10:03:51 +00:00
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = waveamp * 0.75;
2012-11-20 13:11:21 +00:00
wave3.dir = vec2(0.866025, -0.5); //vec2(cos(radians(angle)), sin(radians(angle)));
2012-04-26 10:03:51 +00:00
2012-04-26 10:03:51 +00:00
sumWaves(WaveAngle + WaveDAngle, -1.5, windScale, WaveFactor, ddx2, ddy2);
sumWaves(WaveAngle + WaveDAngle, 1.5, windScale, WaveFactor, ddx3, ddy3);
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
}
// end sine stuff
//cover = 5.0 * smoothstep(0.6, 1.0, scattering);
//cover = 5.0 * ground_scattering;
vec4 viewt = normalize(waterTex4);
vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)* windScale) * 2.0 - 1.0;
2012-11-20 13:11:21 +00:00
vec4 vNorm;
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
//normalmaps
vec4 nmap = texture2D(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0;
vec4 nmap1 = texture2D(perlin_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0;
rotationmatrix(radians(3.0 * sin(osg_SimulationTime * 0.0075)), RotationMatrix);
nmap += texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * tscale) * windScale) * 2.0 - 1.0;
nmap1 += texture2D(perlin_normalmap, vec2(waterTex2 * RotationMatrix * tscale) * windScale) * 2.0 - 1.0;
nmap *= windEffect_low;
nmap1 *= windEffect_low;
// mix water and noise, modulated by factor
vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness);
vNorm.r += ddx + ddx1 + ddx2 + ddx3;
2012-11-20 13:11:21 +00:00
if (normalmap_dds > 0)
{vNorm = -vNorm;} //dds fix
vNorm = vNorm * (0.5 + 0.5 * noise_250m);
2012-04-26 10:03:51 +00:00
//load reflection
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
vec4 refl ;
refl.r = sea_r;
refl.g = sea_g;
refl.b = sea_b;
2012-11-20 13:11:21 +00:00
refl.a = 1.0;
refl.g = refl.g * (0.9 + 0.2* noise_2500m);
2013-11-23 10:15:52 +00:00
if (ocean_flag ==1) // use depth information
{
refl.rgb = mix(refl.rgb, 0.65* floorColour, floorMixFactor);
refl.rgb = refl.rgb * (0.5 + 0.5 * smoothstep(0.0,0.3,topoTexel.a));
}
else
{
refl.rgb = 1.3 * refl.rgb;
}
2012-04-26 10:03:51 +00:00
float intensity;
// de-saturate for reduced light
2012-11-20 13:11:21 +00:00
refl.rgb = mix(refl.rgb, vec3 (0.248, 0.248, 0.248), 1.0 - smoothstep(0.1, 0.8, ground_scattering));
2012-04-26 10:03:51 +00:00
// de-saturate light for overcast haze
intensity = length(refl.rgb);
2012-11-20 13:11:21 +00:00
refl.rgb = mix(refl.rgb, intensity * vec3 (1.0, 1.0, 1.0), 0.5 * smoothstep(0.1, 0.9, overcast));
2012-04-26 10:03:51 +00:00
vec3 N;
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
vec3 N0 = vec3(texture2D(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0);
vec3 N1 = vec3(texture2D(perlin_normalmap, vec2(waterTex1 + disdis * sca) * windScale) * 2.0 - 1.0);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * tscale) * windScale) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2 * tscale) * windScale) * 2.0 - 1.0);
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
rotationmatrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca) * windScale) * 2.0 - 1.0);
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
N0 *= windEffect_low;
N1 *= windEffect_low;
N0.r += (ddx + ddx1 + ddx2 + ddx3);
N0.g += (ddy + ddy1 + ddy2 + ddy3);
N = normalize(mix(Normal + N0, Normal + N1, mixFactor) * waveRoughness);
2012-11-20 13:11:21 +00:00
if (normalmap_dds > 0)
{N = -N;} //dds fix
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
// primary reflection of the sun
specular_light = gl_Color.rgb * earthShade;
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
vec3 specular_color = vec3(specular_light)
* pow(max(0.0, dot(N, Hv)), water_shininess) * 6.0;
// secondary reflection of sky irradiance
vec3 ER = E - 2.0 * N * dot(E,N);
float ctrefl = dot(vec3(0.0,0.0,1.0), -normalize(ER));
float fresnel = -0.5 + 8.0 * (1.0-smoothstep(0.0,0.4, dot(E,N)));
specular_color += (ctrefl*ctrefl) * fresnel* specular_light.rgb;
2012-04-26 10:03:51 +00:00
vec4 specular = vec4(specular_color, 0.5);
specular = specular * saturation * 0.3 * earthShade ;
//calculate fresnel
vec4 invfres = vec4( dot(vNorm, viewt) );
vec4 fres = vec4(1.0) + invfres;
refl *= fres;
vec4 ambient_light;
2012-11-20 13:11:21 +00:00
//intensity = length(specular_light.rgb);
ambient_light.rgb = max(specular_light.rgb, vec3(0.05, 0.05, 0.05));
2012-11-20 13:11:21 +00:00
//ambient_light.rgb = max(intensity * normalize(vec3 (0.33, 0.4, 0.5)), vec3 (0.1,0.1,0.1));
2012-04-26 10:03:51 +00:00
ambient_light.a = 1.0;
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
vec4 finalColor;
2014-03-11 10:25:47 +00:00
float shadowValue;
if (cloud_shadow_flag == 1)
{
shadowValue = shadow_func(relPos.x, relPos.y, 0.3 * noise_250m + 0.5 * noise_500m+0.2 * noise_1500m, dist);
specular = specular * shadowValue;
refl = refl * (0.7 + 0.3 *shadowValue);
}
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
finalColor = refl + specular * smoothstep(0.3, 0.6, ground_scattering);
//add foam
vec4 foam_texel = texture2D(sea_foam, vec2(waterTex2 * tscale) * 25.0);
2012-04-26 10:03:51 +00:00
if (dist < 10000.0)
{
float foamSlope = 0.10 + 0.1 * windScale;
2012-04-26 10:03:51 +00:00
float waveSlope = N.g;
float surfFact = 0.0;
if ((windEffect >= 8.0) || (steepness < 0.999))
{
if ((waveSlope > 0.0) && (ocean_flag ==1))
{
surfFact = surfFact +(1.0 -smoothstep(0.97,1.0,steepness));
waveSlope = waveSlope + 2.0 * surfFact;
}
2012-04-26 10:03:51 +00:00
if (waveSlope >= foamSlope){
finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel), smoothstep(0.01, 0.50, N.g+0.2 * surfFact));
2012-04-26 10:03:51 +00:00
}
}
2012-04-26 10:03:51 +00:00
}
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
// add ice
vec4 ice_texel = texture2D(ice_texture, vec2(waterTex2) * 0.2 );
float nSum = 0.5 * (noise_250m + noise_50m);
float mix_factor = smoothstep(1.0 - ice_cover, 1.04-ice_cover, nSum);
finalColor = mix(finalColor, ice_texel, mix_factor * ice_texel.a);
finalColor.a = 1.0;
finalColor *= ambient_light;
2012-04-26 10:03:51 +00:00
// Rayleigh color shift due to out-scattering
float rayleigh_length = 0.4 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, eye_alt+relPos.z);
float outscatter = 1.0-exp(-dist/rayleigh_length);
finalColor.rgb = rayleigh_out_shift(finalColor.rgb,outscatter);
2012-04-26 10:03:51 +00:00
// here comes the terrain haze model
float delta_z = hazeLayerAltitude - eye_alt;
if (dist > 40.0)
{
float transmission;
float vAltitude;
float delta_zv;
float H;
float distance_in_layer;
float transmission_arg;
// angle with horizon
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
// we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
if (delta_z > 0.0) // we're inside the layer
{
2012-11-20 13:11:21 +00:00
if (ct < 0.0) // we look down
2012-04-26 10:03:51 +00:00
{
distance_in_layer = dist;
vAltitude = min(distance_in_layer,min(visibility,avisibility)) * ct;
delta_zv = delta_z - vAltitude;
}
else // we may look through upper layer edge
{
H = dist * ct;
if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
else {distance_in_layer = dist;}
vAltitude = min(distance_in_layer,visibility) * ct;
2012-11-20 13:11:21 +00:00
delta_zv = delta_z - vAltitude;
2012-04-26 10:03:51 +00:00
}
}
else // we see the layer from above, delta_z < 0.0
2012-11-20 13:11:21 +00:00
{
2012-04-26 10:03:51 +00:00
H = dist * -ct;
if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
{
distance_in_layer = 0.0;
delta_zv = 0.0;
2012-11-20 13:11:21 +00:00
}
2012-04-26 10:03:51 +00:00
else
{
vAltitude = H + delta_z;
2012-11-20 13:11:21 +00:00
distance_in_layer = vAltitude/H * dist;
2012-04-26 10:03:51 +00:00
vAltitude = min(distance_in_layer,visibility) * (-ct);
delta_zv = vAltitude;
2012-11-20 13:11:21 +00:00
}
2012-04-26 10:03:51 +00:00
}
2012-11-20 13:11:21 +00:00
2012-04-26 10:03:51 +00:00
// ground haze cannot be thinner than aloft visibility in the model,
// so we need to use aloft visibility otherwise
transmission_arg = (dist-distance_in_layer)/avisibility;
float eqColorFactor;
/*
2012-04-26 10:03:51 +00:00
if (visibility < avisibility)
{
transmission_arg = transmission_arg + (distance_in_layer/visibility);
// this combines the Weber-Fechner intensity
2012-11-20 13:11:21 +00:00
eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
2012-04-26 10:03:51 +00:00
}
2012-11-20 13:11:21 +00:00
else
2012-04-26 10:03:51 +00:00
{
transmission_arg = transmission_arg + (distance_in_layer/avisibility);
// this combines the Weber-Fechner intensity
eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
}
*/
if (visibility < avisibility)
{
if (quality_level > 3)
{
transmission_arg = transmission_arg + (distance_in_layer/(1.0 * visibility + 1.0 * visibility * fogstructure * 0.06 * (noise_1500m + noise_2000m -1.0) ));
}
else
{
transmission_arg = transmission_arg + (distance_in_layer/visibility);
}
// this combines the Weber-Fechner intensity
eqColorFactor = 1.0 - 0.1 * delta_zv/visibility - (1.0 -effective_scattering);
}
else
{
if (quality_level > 3)
{
transmission_arg = transmission_arg + (distance_in_layer/(1.0 * avisibility + 1.0 * avisibility * fogstructure * 0.06 * (noise_1500m + noise_2000m - 1.0) ));
}
else
{
transmission_arg = transmission_arg + (distance_in_layer/avisibility);
}
// this combines the Weber-Fechner intensity
2012-11-20 13:11:21 +00:00
eqColorFactor = 1.0 - 0.1 * delta_zv/avisibility - (1.0 -effective_scattering);
2012-04-26 10:03:51 +00:00
}
transmission = fog_func(transmission_arg, eye_alt);
2012-04-26 10:03:51 +00:00
// there's always residual intensity, we should never be driven to zero
if (eqColorFactor < 0.2) eqColorFactor = 0.2;
float lightArg = (terminator-yprime_alt)/100000.0;
vec3 hazeColor;
hazeColor.b = light_func(lightArg, 1.330e-05, 0.264, 2.527, 1.08e-05, 1.0);
hazeColor.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
hazeColor.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
// now dim the light for haze
float eShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
// Mie-like factor
2012-11-20 13:11:21 +00:00
if (lightArg < 10.0)
2012-04-26 10:03:51 +00:00
{intensity = length(hazeColor);
float mie_magnitude = 0.5 * smoothstep(350000.0, 150000.0, terminator-sqrt(2.0 * EarthRadius * terrain_alt));
2012-11-20 13:11:21 +00:00
hazeColor = intensity * ((1.0 - mie_magnitude) + mie_magnitude * mie_angle) * normalize(mix(hazeColor, vec3 (0.5, 0.58, 0.65), mie_magnitude * (0.5 - 0.5 * mie_angle)) );
2012-04-26 10:03:51 +00:00
}
// high altitude desaturation of the haze color
intensity = length(hazeColor);
2012-11-20 13:11:21 +00:00
if (intensity > 0.0) // this needs to be a condition, because otherwise hazeColor doesn't come out correctly
{
hazeColor = intensity * normalize (mix(hazeColor, intensity * vec3 (1.0,1.0,1.0), 0.7* smoothstep(5000.0, 50000.0, eye_alt)));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
// blue hue of haze
hazeColor.x = hazeColor.x * 0.83;
hazeColor.y = hazeColor.y * 0.9;
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
// additional blue in indirect light
float fade_out = max(0.65 - 0.3 *overcast, 0.45);
intensity = length(hazeColor);
hazeColor = intensity * normalize(mix(hazeColor, 1.5* shadedFogColor, 1.0 -smoothstep(0.25, fade_out,eShade) ));
2012-04-26 10:03:51 +00:00
2012-11-20 13:11:21 +00:00
// change haze color to blue hue for strong fogging
hazeColor = intensity * normalize(mix(hazeColor, shadedFogColor, (1.0-smoothstep(0.5,0.9,eqColorFactor))));
}
2012-04-26 10:03:51 +00:00
float rShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt-340000.0) + 0.1;
float lightIntensity = length(gl_Color.rgb)/1.73 * rShade;
vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, eye_alt + relPos.z);
finalColor.rgb = mix(finalColor.rgb, rayleighColor, rayleighStrength);
2012-04-26 10:03:51 +00:00
finalColor.rgb = mix(eqColorFactor * hazeColor * eShade, finalColor.rgb,transmission);
}
2012-11-20 13:11:21 +00:00
gl_FragColor = finalColor;
2012-04-26 10:03:51 +00:00
}