Untitled
Loading...
Searching...
No Matches
ssao.frag
Go to the documentation of this file.
1
2#version 430 core
3
5
6layout(std140, binding = 0) uniform CameraData {
7 vec4 cam_pos;
8 vec4 cam_vel;
10 mat4 unjittered_projection;
11 mat4 view;
12 mat4 inv_projection;
13 mat4 unjittered_inv_projection;
14 mat4 inv_view;
15};
16layout(std140, binding = 1) uniform RenderingData {
17 ivec4 resolution;
18};
19layout(std140, binding = 2) uniform LightCameraData {
20 vec4 light_cam_pos;
21 vec4 light_cam_vel;
22 mat4 light_projection;
23 mat4 light_unjittered_projection;
24 mat4 light_view;
25 mat4 light_inv_projection;
26 mat4 light_unjittered_inv_projection;
27 mat4 light_inv_view;
28};
29
30layout(std140, binding = 4) uniform PrevCameraData {
31 vec4 prev_cam_pos;
32 vec4 prev_cam_vel;
33 mat4 prev_projection;
34 mat4 prev_unjittered_projection;
35 mat4 prev_view;
36 mat4 prev_inv_projection;
37 mat4 prev_unjittered_inv_projection;
38 mat4 prev_inv_view;
39};
40layout(std140, binding = 5) uniform SSAOKernel {
41 vec4 kernel[16];
42};
43
44uniform sampler2D uDepth;
45uniform sampler2D uNoise;
47
48out float fragColor;
49
51{
52 float depth = texture(uDepth, uv).r;
53 vec4 ndc = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
54 vec4 view_pos = unjittered_inv_projection * ndc;
55 return view_pos.xyz / view_pos.w;
56}
57
58
60{
61
63 vec3 view_top = get_view_pos(uv + vec2(0.0, uTexelSize.y));
64 vec3 view_right = get_view_pos(uv + vec2(uTexelSize.x, 0.0));
65
66 vec3 dx = view_right - view_pos;
67 vec3 dy = view_top - view_pos;
68
69 return normalize(cross(dx, dy));
70
71}
72
73
74void main()
75{
76
77 vec3 fragPos = get_view_pos(vUV);
78
79 vec3 normal = reconstruct_normal(vUV, fragPos);
80
81 vec2 noiseScale = uSSAOResolution / 4.0;
82 vec3 randomVec = texture(uNoise, vUV * noiseScale).xyz;
83
84 vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
85 vec3 bitangent = cross(normal, tangent);
86 mat3 TBN = mat3(tangent, bitangent, normal);
87
88 float radius = 0.5;
89 int samples = 16;
90
91
92 float occlusion = 0.0f;
93 for (int i = 0; i < 16; i++)
94 {
95
96 vec3 samplePos = TBN * kernel[i].xyz;
97 samplePos = fragPos + samplePos * radius;
98
99 vec4 offset = vec4(samplePos, 1.0);
100 offset = unjittered_projection * offset;
101 offset.xyz /= offset.w;
102 offset.xyz = offset.xyz * 0.5 + 0.5;
103
104 vec3 surface_pos = get_view_pos(offset.xy);
105
106 float range_check = smoothstep(0.0, 1.0, radius / abs(fragPos.z - surface_pos.z));
107 occlusion += (surface_pos.z >= samplePos.z + 0.01 ? 1.0 : 0.0) * range_check;
108
109 }
110 fragColor = float(1.0f - occlusion / samples);
111}
layout(local_size_x=16, local_size_y=16) in
void main()
out vec4 fragColor
Definition bloom.frag:6
uniform vec2 uTexelSize
Definition bokeh.frag:18
uniform sampler2D uDepth
in mat3 TBN
in vec2 uv
in vec2 vUV
Definition lightmap.frag:13
float smoothstep(float a, float b, float c)
Definition mathf.h:27
const int samples
uniform mat4 projection
Definition shadow.vert:15
uniform mat4 view
Definition shadow.vert:16
vec3 get_view_pos(vec2 uv)
Definition ssao.frag:50
uniform sampler2D uNoise
Definition ssao.frag:45
vec3 reconstruct_normal(vec2 uv, vec3 view_pos)
Definition ssao.frag:59
uniform ivec2 uSSAOResolution
Definition ssao.frag:46
float x
Definition types.h:11
float w
Definition types.h:13
float y
Definition types.h:11
float z
Definition types.h:12
Definition types.h:14
Definition types.h:16
Definition types.h:20
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13