2014-02-12 16:16:10 -05:00
|
|
|
#version 330
|
2014-01-27 13:20:20 -05:00
|
|
|
uniform sampler2D ntex;
|
|
|
|
uniform sampler2D dtex;
|
2014-01-19 12:20:57 -05:00
|
|
|
uniform sampler2D noise_texture;
|
2014-01-17 17:59:08 -05:00
|
|
|
uniform mat4 invprojm;
|
|
|
|
uniform mat4 projm;
|
|
|
|
uniform vec4 samplePoints[16];
|
|
|
|
|
|
|
|
in vec2 uv;
|
2014-01-27 13:20:20 -05:00
|
|
|
out float ao;
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-01-26 18:52:20 -05:00
|
|
|
const float strengh = 4.;
|
2014-01-17 17:59:08 -05:00
|
|
|
const float radius = .4f;
|
|
|
|
|
2014-01-26 18:47:35 -05:00
|
|
|
#define SAMPLES 16
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-01-26 18:52:20 -05:00
|
|
|
const float invSamples = strengh / SAMPLES;
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-01-20 14:43:03 -05:00
|
|
|
vec3 rand(vec2 co)
|
2014-01-14 09:22:01 -05:00
|
|
|
{
|
2014-01-26 18:09:36 -05:00
|
|
|
return texture(noise_texture, co*20.16).xyz;
|
2014-01-17 17:59:08 -05:00
|
|
|
}
|
|
|
|
|
2014-01-27 17:29:46 -05:00
|
|
|
vec3 DecodeNormal(vec2 n)
|
|
|
|
{
|
|
|
|
float z = dot(n, n) * 2. - 1.;
|
|
|
|
vec2 xy = normalize(n) * sqrt(1. - z * z);
|
|
|
|
return vec3(xy,z);
|
|
|
|
}
|
|
|
|
|
2014-01-17 17:59:08 -05:00
|
|
|
void main(void)
|
|
|
|
{
|
2014-01-27 13:20:20 -05:00
|
|
|
vec4 cur = texture(ntex, uv);
|
|
|
|
float curdepth = texture(dtex, uv).x;
|
2014-01-17 17:59:08 -05:00
|
|
|
vec4 FragPos = invprojm * (2.0f * vec4(uv, curdepth, 1.0f) - 1.0f);
|
|
|
|
FragPos /= FragPos.w;
|
|
|
|
|
|
|
|
// get the normal of current fragment
|
2014-01-27 17:29:46 -05:00
|
|
|
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
2014-01-17 17:59:08 -05:00
|
|
|
// Workaround for nvidia and skyboxes
|
|
|
|
float len = dot(vec3(1.0), abs(cur.xyz));
|
|
|
|
if (len < 0.2 || curdepth > 0.999) discard;
|
|
|
|
// Make a tangent as random as possible
|
2014-01-20 14:43:03 -05:00
|
|
|
vec3 randvect = rand(uv);
|
2014-01-17 17:59:08 -05:00
|
|
|
vec3 tangent = normalize(cross(norm, randvect));
|
|
|
|
vec3 bitangent = cross(norm, tangent);
|
|
|
|
|
|
|
|
float bl = 0.0;
|
|
|
|
|
|
|
|
for(int i = 0; i < SAMPLES; ++i) {
|
|
|
|
vec3 sampleDir = samplePoints[i].x * tangent + samplePoints[i].y * bitangent + samplePoints[i].z * norm;
|
|
|
|
sampleDir *= samplePoints[i].w;
|
|
|
|
vec4 samplePos = FragPos + radius * vec4(sampleDir, 0.0);
|
|
|
|
vec4 sampleProj = projm * samplePos;
|
|
|
|
sampleProj /= sampleProj.w;
|
|
|
|
|
|
|
|
bool isInsideTexture = (sampleProj.x > -1.) && (sampleProj.x < 1.) && (sampleProj.y > -1.) && (sampleProj.y < 1.);
|
|
|
|
// get the depth of the occluder fragment
|
2014-01-27 13:20:20 -05:00
|
|
|
float occluderFragmentDepth = texture(dtex, (sampleProj.xy * 0.5) + 0.5).x;
|
2014-01-17 17:59:08 -05:00
|
|
|
// Position of the occluder fragment in worldSpace
|
|
|
|
vec4 occluderPos = invprojm * vec4(sampleProj.xy, 2.0 * occluderFragmentDepth - 1.0, 1.0f);
|
|
|
|
occluderPos /= occluderPos.w;
|
|
|
|
|
2014-01-26 18:56:58 -05:00
|
|
|
bool isOccluded = isInsideTexture && (sampleProj.z > (2. * occluderFragmentDepth - 1.0)) && (distance(FragPos, occluderPos) < radius);
|
2014-01-17 17:59:08 -05:00
|
|
|
bl += isOccluded ? smoothstep(radius, 0, distance(samplePos, FragPos)) : 0.;
|
|
|
|
}
|
|
|
|
|
2014-01-27 13:20:20 -05:00
|
|
|
ao = 1.0 - bl * invSamples;
|
2014-01-17 17:59:08 -05:00
|
|
|
}
|