Untitled
Toggle main menu visibility
Loading...
Searching...
No Matches
ssao.frag
Go to the documentation of this file.
1
2
#version 430 core
3
4
in
vec2
vUV
;
5
6
layout
(std140, binding = 0) uniform CameraData {
7
vec4
cam_pos;
8
vec4
cam_vel;
9
mat4
projection
;
10
mat4
unjittered_projection;
11
mat4
view
;
12
mat4
inv_projection;
13
mat4
unjittered_inv_projection;
14
mat4
inv_view;
15
};
16
layout
(std140, binding = 1) uniform RenderingData {
17
ivec4
resolution;
18
};
19
layout
(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
30
layout
(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
};
40
layout
(std140, binding = 5) uniform SSAOKernel {
41
vec4
kernel[16];
42
};
43
44
uniform sampler2D
uDepth
;
45
uniform sampler2D
uNoise
;
46
uniform
ivec2
uSSAOResolution
;
47
48
out
float
fragColor
;
49
50
vec3
get_view_pos
(
vec2
uv
)
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
59
vec3
reconstruct_normal
(
vec2
uv
,
vec3
view_pos)
60
{
61
62
vec2
uTexelSize
= 1.0 /
uSSAOResolution
;
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
74
void
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
layout(local_size_x=16, local_size_y=16) in
main
void main()
Definition
audio_probe_bake.comp:465
fragColor
out vec4 fragColor
Definition
bloom.frag:6
uTexelSize
uniform vec2 uTexelSize
Definition
bokeh.frag:18
uDepth
uniform sampler2D uDepth
Definition
depth_downsample.frag:2
TBN
in mat3 TBN
Definition
depth_pre_pass.frag:68
uv
in vec2 uv
Definition
fullscreen.frag:4
vUV
in vec2 vUV
Definition
lightmap.frag:13
smoothstep
float smoothstep(float a, float b, float c)
Definition
mathf.h:27
samples
const int samples
Definition
postprocess.frag:177
projection
uniform mat4 projection
Definition
shadow.vert:15
view
uniform mat4 view
Definition
shadow.vert:16
get_view_pos
vec3 get_view_pos(vec2 uv)
Definition
ssao.frag:50
uNoise
uniform sampler2D uNoise
Definition
ssao.frag:45
reconstruct_normal
vec3 reconstruct_normal(vec2 uv, vec3 view_pos)
Definition
ssao.frag:59
uSSAOResolution
uniform ivec2 uSSAOResolution
Definition
ssao.frag:46
vec2::x
float x
Definition
types.h:11
vec4::w
float w
Definition
types.h:13
vec2::y
float y
Definition
types.h:11
vec3::z
float z
Definition
types.h:12
ivec2
Definition
types.h:14
ivec4
Definition
types.h:16
mat3
Definition
types.h:20
mat4
Definition
types.h:21
vec2
Definition
types.h:11
vec3
Definition
types.h:12
vec4
Definition
types.h:13
src
render
ssao.frag
Generated by
1.18.0