Better SSAO + added shader reloading in debug

This commit is contained in:
antopilo
2024-11-23 19:46:39 -05:00
parent 1782f18f26
commit dd46e32bee
8 changed files with 210 additions and 342 deletions

View File

@@ -27,8 +27,8 @@ void main()
#shader fragment
#version 440 core
uniform sampler2D u_Depth;
uniform sampler2D u_Normal;
uniform sampler2D u_Depth;
uniform sampler2D u_Noise;
uniform int u_KernelSize = 64;
@@ -51,7 +51,7 @@ out vec4 FragColor;
float linearize_depth(float d, float zNear, float zFar)
{
return zNear * zFar / (zFar + d * (zNear - zFar));
return (2.0 * zNear * zFar) / (zFar + zNear - d * (zFar - zNear)) / 400.0;
}
vec3 WorldPosFromDepth(float depth)
@@ -64,89 +64,53 @@ vec3 WorldPosFromDepth(float depth)
// Perspective division
viewSpacePosition /= viewSpacePosition.w;
vec4 worldSpacePosition = v_InvView * viewSpacePosition;
return worldSpacePosition.xyz;
}
vec3 ViewPosFromDepth(float depth)
{
float z = depth * 2.0 - 1.0;
vec4 clipSpacePosition = vec4(UV * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = v_InvProjection * clipSpacePosition;
viewSpacePosition.xyz /= viewSpacePosition.w;
return viewSpacePosition.xyz;
}
vec3 normal_from_depth(float depth, vec2 texcoords) {
const vec2 offset1 = vec2(0.0,0.0002);
const vec2 offset2 = vec2(0.0002,0.0);
float depth1 = texture(u_Depth, UV + offset1).r;
float depth2 = texture(u_Depth, UV + offset2).r;
vec3 p1 = vec3(offset1, depth1 - depth);
vec3 p2 = vec3(offset2, depth2 - depth);
vec3 normal = cross(p1, p2);
normal.z = -normal.z;
return normalize(normal);
}
void main()
{
float depth = texture(u_Depth, UV).r;
if (depth > 0.9999999f)
{
FragColor = vec4(0, 0, 0, 0);
FragColor = vec4(1, 1, 1, 0);
return;
}
const float SCALING_NEAR = 0.92;
float depthScaler = (depth - SCALING_NEAR) / (1.0 - SCALING_NEAR);
vec3 fragPos = WorldPosFromDepth(depth);
vec3 worldSpaceNormal = texture(u_Normal, UV).xyz * 2.0 - 1.0;
mat3 normalMatrix = transpose(inverse(mat3(v_View)));
vec3 normal = (normalMatrix * worldSpaceNormal).xyz;
const float minRadius = 0.05f;
const float maxRadius = 1.2f;
const float scalerPow = 1.8f;
depthScaler = min(max(pow(depthScaler, scalerPow), minRadius), maxRadius);
float scaledRadius = u_Radius;
vec3 fragPos = ViewPosFromDepth(depth);
vec3 normal = texture(u_Normal, UV).xyz * 2.0 - 1.0;//normal_from_depth(depth, UV);
normal.z *= -1.0f;
// Remove translation from view;
mat4 invView = v_InvView;
invView[3] = vec4(0, 0, 0, 1);
normal = (invView * vec4(normal, 1.0f)).xyz;
vec3 randomVec = texture(u_Noise, UV * u_NoiseScale).xyz;
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal);
float occlusion = 0.0;
for (int i = 0; i < 64; i++)
float occlusion = 0.0f;
const int kernelCount = 64;
for (int i = 0; i < kernelCount; i++)
{
vec3 samplePos = TBN * u_Samples[i]; // generate a random point
samplePos.z *= -1.0f;
samplePos = fragPos + samplePos * scaledRadius;
samplePos = fragPos + samplePos * u_Radius;
vec4 offset = vec4(samplePos, 1.0); // make it a 4-vector
offset = v_Projection * offset; // project on the near clipping plane
offset.xyz /= offset.w; // perform perspective divide
offset.xyz = offset.xyz * 0.5 + 0.5; // transform to (0,1) range
float sampleDepth = texture(u_Depth, offset.xy).r;
float rangeCheck = smoothstep(0.0, 1.0, scaledRadius / abs((samplePos.z) - sampleDepth));
occlusion += (sampleDepth <= depth - u_Bias / 100.0 ? 1.0 : 0.0) * rangeCheck;
offset.x = clamp(offset.x, 0.00001, 0.999);
offset.y = clamp(offset.y, 0.00001, 0.999);
float sampleDepth = texture(u_Depth, offset.xy).x;
float epsilon = 0.000001f; // Adjust based on your scene's scale
float realBias = (-0.001 / 1000.0f);
float rangeCheck = smoothstep(0.0, 1.0, (u_Radius / 500000.0f) / (abs(depth - sampleDepth))) ;
occlusion += (sampleDepth <= offset.z + realBias ? 1.0f : 0.0f) * rangeCheck ;
}
float ao = 1.0 - (occlusion / 64.0);
float finalAO = pow(ao, u_Strength * 100.0);
FragColor = vec4(finalAO, finalAO, finalAO, 1.0);
float ao = 1.0 - pow(occlusion, u_Strength) / 64.0f;
FragColor = vec4(ao, ao, ao, 1.0f);
}