2014-05-11 12:00:30 -04:00
|
|
|
// From paper http://graphics.cs.williams.edu/papers/AlchemyHPG11/
|
2014-05-11 12:09:48 -04:00
|
|
|
// and improvements here http://graphics.cs.williams.edu/papers/SAOHPG12/
|
2014-05-11 12:00:30 -04:00
|
|
|
|
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 vec4 samplePoints[16];
|
|
|
|
|
2014-05-02 12:11:34 -04:00
|
|
|
#ifdef UBO_DISABLED
|
|
|
|
uniform mat4 ViewMatrix;
|
|
|
|
uniform mat4 ProjectionMatrix;
|
|
|
|
uniform mat4 InverseViewMatrix;
|
|
|
|
uniform mat4 InverseProjectionMatrix;
|
2014-02-28 11:29:05 -05:00
|
|
|
#else
|
2014-05-02 12:11:34 -04:00
|
|
|
layout (std140) uniform MatrixesData
|
|
|
|
{
|
|
|
|
mat4 ViewMatrix;
|
|
|
|
mat4 ProjectionMatrix;
|
|
|
|
mat4 InverseViewMatrix;
|
|
|
|
mat4 InverseProjectionMatrix;
|
|
|
|
mat4 ShadowViewProjMatrixes[4];
|
|
|
|
};
|
2014-02-28 11:29:05 -05:00
|
|
|
#endif
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-05-02 12:11:34 -04:00
|
|
|
in vec2 uv;
|
|
|
|
out float AO;
|
|
|
|
|
2014-05-11 12:00:30 -04:00
|
|
|
const float sigma = 1.;
|
|
|
|
const float beta = 0.0001;
|
|
|
|
const float epsilon = .00001;
|
|
|
|
const float radius = .1;
|
|
|
|
const float k = 1.;
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-01-26 18:47:35 -05:00
|
|
|
#define SAMPLES 16
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-05-11 12:00:30 -04:00
|
|
|
const float invSamples = 1. / SAMPLES;
|
|
|
|
|
|
|
|
vec3 DecodeNormal(vec2 n);
|
|
|
|
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
|
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-05-11 12:00:30 -04:00
|
|
|
float noiseX = clamp(fract(sin(dot(co ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;
|
|
|
|
float noiseY = clamp(fract(sin(dot(co ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;
|
|
|
|
return vec3(noiseX, noiseY, length(texture(noise_texture, co * pow(3.14159265359, 2.)).xyz));
|
2014-01-17 17:59:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2014-05-11 12:00:30 -04:00
|
|
|
vec4 cur = texture(ntex, uv);
|
|
|
|
float curdepth = texture(dtex, uv).x;
|
2014-05-02 12:11:34 -04:00
|
|
|
vec4 FragPos = getPosFromUVDepth(vec3(uv, curdepth), InverseProjectionMatrix);
|
2014-05-11 12:00:30 -04:00
|
|
|
|
|
|
|
// get the normal of current fragment
|
2014-05-11 12:09:48 -04:00
|
|
|
vec3 ddx = dFdx(FragPos.xyz);
|
|
|
|
vec3 ddy = dFdy(FragPos.xyz);
|
|
|
|
vec3 norm = normalize(cross(ddy, ddx));
|
2014-05-11 12:00:30 -04:00
|
|
|
// Workaround for nvidia and skyboxes
|
|
|
|
float len = dot(vec3(1.0), abs(cur.xyz));
|
|
|
|
if (len < 0.2 || curdepth > 0.9955) discard;
|
|
|
|
|
|
|
|
float randAngle = rand(uv).x;
|
|
|
|
vec2 Xaxis = vec2(cos(randAngle), sin(randAngle));
|
|
|
|
Xaxis = normalize(Xaxis);
|
|
|
|
vec2 Yaxis = vec2(sin(randAngle), -cos(randAngle));
|
|
|
|
Yaxis = normalize(Yaxis);
|
|
|
|
|
|
|
|
float bl = 0.0;
|
|
|
|
|
|
|
|
for(int i = 0; i < SAMPLES; ++i) {
|
|
|
|
vec2 occluder_uv = samplePoints[i].x * Xaxis + samplePoints[i].y * Yaxis;
|
|
|
|
occluder_uv *= samplePoints[i].w * radius;
|
|
|
|
occluder_uv += uv;
|
|
|
|
|
|
|
|
if (occluder_uv.x < 0. || occluder_uv.x > 1. || occluder_uv.y < 0. || occluder_uv.y > 1.) continue;
|
|
|
|
|
|
|
|
float occluderFragmentDepth = texture(dtex, occluder_uv).x;
|
|
|
|
vec4 OccluderPos = getPosFromUVDepth(vec3(occluder_uv, occluderFragmentDepth), InverseProjectionMatrix);
|
|
|
|
|
|
|
|
vec3 vi = (OccluderPos - FragPos).xyz;
|
|
|
|
bl += max(0, dot(vi, norm) - FragPos.z * beta) / (dot(vi, vi) + epsilon);
|
|
|
|
}
|
2014-01-17 17:59:08 -05:00
|
|
|
|
2014-05-11 12:00:30 -04:00
|
|
|
AO = max(pow(1.0 - 2. * sigma * bl * invSamples, k), 0.);
|
2014-01-17 17:59:08 -05:00
|
|
|
}
|