diff --git a/Effects/fog.eff b/Effects/fog.eff new file mode 100644 index 000000000..9c9572bdc --- /dev/null +++ b/Effects/fog.eff @@ -0,0 +1,73 @@ + + + Effects/sunlight + + + false + + false + + + src-alpha + one-minus-src-alpha + + + 10000 + RenderBin + + + 0 + depth-buffer + + + 1 + normal-buffer + + + 2 + diffuse-buffer + + + 3 + spec-emis-buffer + + + Shaders/fog.vert + Shaders/fog.frag + + + depth_tex + sampler-2d + 0 + + + normal_tex + sampler-2d + 1 + + + color_tex + sampler-2d + 2 + + + spec_emis_tex + sampler-2d + 3 + + + + + + diff --git a/Shaders/fog.frag b/Shaders/fog.frag new file mode 100644 index 000000000..b61b612a5 --- /dev/null +++ b/Shaders/fog.frag @@ -0,0 +1,31 @@ +uniform sampler2D depth_tex; +uniform sampler2D normal_tex; +uniform sampler2D color_tex; +uniform sampler2D spec_emis_tex; +uniform vec4 fg_FogColor; +uniform float fg_FogDensity; +uniform vec3 fg_Planes; +varying vec3 ray; +void main() { + vec2 coords = gl_TexCoord[0].xy; + float initialized = texture2D( spec_emis_tex, coords ).a; + if ( initialized < 0.1 ) + discard; + vec3 normal; + normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0); + normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) ); + float len = length(normal); + normal /= len; + vec3 viewDir = normalize(ray); + float depth = texture2D( depth_tex, coords ).r; + vec3 pos; + pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z); + pos.xy = viewDir.xy / viewDir.z * pos.z; + + float fogFactor = 0.0; + const float LOG2 = 1.442695; + fogFactor = exp2(-fg_FogDensity * fg_FogDensity * pos.z * pos.z * LOG2); + fogFactor = clamp(fogFactor, 0.0, 1.0); + + gl_FragColor = vec4(fg_FogColor.rgb, 1.0 - fogFactor); +} diff --git a/Shaders/fog.vert b/Shaders/fog.vert new file mode 100644 index 000000000..f7b7475b8 --- /dev/null +++ b/Shaders/fog.vert @@ -0,0 +1,7 @@ +uniform mat4 fg_ProjectionMatrixInverse; +varying vec3 ray; +void main() { + gl_Position = gl_Vertex; + gl_TexCoord[0] = gl_MultiTexCoord0; + ray = (fg_ProjectionMatrixInverse * gl_Vertex).xyz; +}