-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSSAO.frag
73 lines (57 loc) · 2.21 KB
/
SSAO.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#version 450
layout(location = 0) in vec2 uv;
layout(location = 1) in vec2 viewRay;
layout(location = 0) out vec4 outColor;
#include "Shaders/Includes/Camera.h"
layout(set = 0, binding = 0) uniform GlobalBuffer
{
Camera camera;
};
layout(set = 1, binding = 0) uniform SSAOBuffer
{
vec4 samples[64];
int kernelSize;
int noiseSize;
float radius;
float bias;
float aoScale;
vec2 viewportScale;
};
layout(set = 1, binding = 1) uniform sampler2D depthTexture;
layout(set = 1, binding = 2) uniform sampler2D normalTexture;
layout(set = 1, binding = 3) uniform sampler2D noiseTexture;
vec2 noiseScale = vec2((camera.viewportSize.x * viewportScale.x) / noiseSize, (camera.viewportSize.y * viewportScale) / noiseSize);
void main()
{
vec3 position = CalculatePositionFromDepth(
texture(depthTexture, uv).x,
camera.projectionMat4,
viewRay);
vec3 normal = texture(normalTexture, uv).xyz;
vec3 randomVector = normalize(texture(noiseTexture, uv * noiseScale).xyz);
// Create TBN change-of-basis matrix: from tangent space to view space.
vec3 tangent = normalize(randomVector - normal * dot(randomVector, normal));
vec3 bitangent = normalize(cross(normal, tangent));
mat3 TBN = mat3(tangent, bitangent, normal);
float occlusion = 0.0f;
for (int i = 0; i < kernelSize; ++i)
{
vec3 currentSample = TBN * samples[i].xyz;
currentSample = position + currentSample * radius;
vec4 offset = vec4(currentSample, 1.0f);
offset = camera.projectionMat4 * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5f + 0.5f;
// Note: Only for Vulkan need to flip Y!
offset.y = 1.0f - offset.y;
float sampleDepth = CalculatePositionFromDepth(
texture(depthTexture, offset.xy).x,
camera.projectionMat4,
viewRay).z;
float rangeCheck = 1.0f - smoothstep(0.0f, 1.0f, abs(position.z - sampleDepth) / radius);
occlusion += (sampleDepth >= currentSample.z + bias ? 1.0f : 0.0f) * rangeCheck;
}
occlusion = 1.0f - (occlusion / float(kernelSize));
occlusion = pow(occlusion, aoScale);
outColor = vec4(occlusion, occlusion, occlusion, 1.0f);
}