Use Alchemy SSAO
This commit is contained in:
parent
518a5fd8ef
commit
e5410e23a2
@ -1,3 +1,5 @@
|
||||
// From paper http://graphics.cs.williams.edu/papers/AlchemyHPG11/
|
||||
|
||||
uniform sampler2D ntex;
|
||||
uniform sampler2D dtex;
|
||||
uniform sampler2D noise_texture;
|
||||
@ -22,57 +24,60 @@ layout (std140) uniform MatrixesData
|
||||
in vec2 uv;
|
||||
out float AO;
|
||||
|
||||
const float strengh = 5.;
|
||||
const float radius = 1.f;
|
||||
const float sigma = 1.;
|
||||
const float beta = 0.0001;
|
||||
const float epsilon = .00001;
|
||||
const float radius = .1;
|
||||
const float k = 1.;
|
||||
|
||||
#define SAMPLES 16
|
||||
|
||||
const float invSamples = strengh / SAMPLES;
|
||||
|
||||
vec3 rand(vec2 co)
|
||||
{
|
||||
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));
|
||||
}
|
||||
const float invSamples = 1. / SAMPLES;
|
||||
|
||||
vec3 DecodeNormal(vec2 n);
|
||||
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
|
||||
|
||||
vec3 rand(vec2 co)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
vec4 cur = texture(ntex, uv);
|
||||
float curdepth = texture(dtex, uv).x;
|
||||
vec4 cur = texture(ntex, uv);
|
||||
float curdepth = texture(dtex, uv).x;
|
||||
vec4 FragPos = getPosFromUVDepth(vec3(uv, curdepth), InverseProjectionMatrix);
|
||||
if (FragPos.z < 0.) discard;
|
||||
|
||||
// get the normal of current fragment
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
// Workaround for nvidia and skyboxes
|
||||
float len = dot(vec3(1.0), abs(cur.xyz));
|
||||
if (len < 0.2 || curdepth > 0.9955) discard;
|
||||
// Make a tangent as random as possible
|
||||
vec3 randvect = rand(uv);
|
||||
vec3 tangent = normalize(cross(norm, randvect));
|
||||
vec3 bitangent = cross(norm, tangent);
|
||||
// get the normal of current fragment
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
// Workaround for nvidia and skyboxes
|
||||
float len = dot(vec3(1.0), abs(cur.xyz));
|
||||
if (len < 0.2 || curdepth > 0.9955) discard;
|
||||
|
||||
float bl = 0.0;
|
||||
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);
|
||||
|
||||
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 = ProjectionMatrix * samplePos;
|
||||
sampleProj /= sampleProj.w;
|
||||
float bl = 0.0;
|
||||
|
||||
bool isInsideTexture = (sampleProj.x > -1.) && (sampleProj.x < 1.) && (sampleProj.y > -1.) && (sampleProj.y < 1.);
|
||||
// get the depth of the occluder fragment
|
||||
float occluderFragmentDepth = texture(dtex, (sampleProj.xy * 0.5) + 0.5).x;
|
||||
// Position of the occluder fragment in worldSpace
|
||||
vec4 occluderPos = getPosFromUVDepth(vec3((sampleProj.xy * 0.5) + 0.5, occluderFragmentDepth), InverseProjectionMatrix);
|
||||
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;
|
||||
|
||||
bool isOccluded = isInsideTexture && (sampleProj.z > (2. * occluderFragmentDepth - 1.0)) && (distance(FragPos, occluderPos) < radius);
|
||||
bl += isOccluded ? samplePoints[i].z * smoothstep(5 * radius, 0, distance(samplePos, FragPos)) : 0.;
|
||||
}
|
||||
if (occluder_uv.x < 0. || occluder_uv.x > 1. || occluder_uv.y < 0. || occluder_uv.y > 1.) continue;
|
||||
|
||||
AO = max(1.0 - bl * invSamples, 0.);
|
||||
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);
|
||||
}
|
||||
|
||||
AO = max(pow(1.0 - 2. * sigma * bl * invSamples, k), 0.);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user