summaryrefslogtreecommitdiffstats
path: root/cwd/assets/altcraft/shaders/frag/ssao.fs
diff options
context:
space:
mode:
authorLaG1924 <lag1924@gmail.com>2021-12-04 20:51:39 +0100
committerLaG1924 <lag1924@gmail.com>2021-12-04 20:56:29 +0100
commit3f122e57f118db1229a4bad2c54be624f2f8f19c (patch)
tree538ec737720cc8e344cbcfed1edcedff0d23a109 /cwd/assets/altcraft/shaders/frag/ssao.fs
parentAdded gamma correction (diff)
downloadAltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.tar
AltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.tar.gz
AltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.tar.bz2
AltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.tar.lz
AltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.tar.xz
AltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.tar.zst
AltCraft-3f122e57f118db1229a4bad2c54be624f2f8f19c.zip
Diffstat (limited to 'cwd/assets/altcraft/shaders/frag/ssao.fs')
-rw-r--r--cwd/assets/altcraft/shaders/frag/ssao.fs55
1 files changed, 55 insertions, 0 deletions
diff --git a/cwd/assets/altcraft/shaders/frag/ssao.fs b/cwd/assets/altcraft/shaders/frag/ssao.fs
new file mode 100644
index 0000000..f4fea34
--- /dev/null
+++ b/cwd/assets/altcraft/shaders/frag/ssao.fs
@@ -0,0 +1,55 @@
+#version 330 core
+
+out vec4 fragColor;
+
+in vec2 uv;
+
+uniform sampler2D normal;
+uniform sampler2D worldPos;
+uniform sampler2D ssaoNoise;
+
+layout (std140) uniform Globals {
+ mat4 projView;
+ mat4 proj;
+ mat4 view;
+ uvec2 viewportSize;
+ vec4 ssaoKernels[64];
+ float globalTime;
+ float dayTime;
+ float gamma;
+};
+
+const vec2 noiseScale = vec2(4.0f, 4.0f);
+const int kernelSize = 64;
+const float radius = 0.5f;
+const float bias = 0.025f;
+
+void main() {
+ vec3 normal = texture(normal, uv).xyz;
+ vec3 fragPos = texture(worldPos, uv).xyz;
+ vec2 noiseUv = uv * viewportSize / noiseScale;
+
+ vec3 randomVec = texture(ssaoNoise, noiseUv).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 < kernelSize; i++)
+ {
+ vec3 samplePos = TBN * ssaoKernels[i].xyz;
+ samplePos = fragPos + samplePos * radius;
+
+ vec4 offset = vec4(samplePos, 1.0);
+ offset = proj * offset;
+ offset.xyz /= offset.w;
+ offset.xyz = offset.xyz * 0.5 + 0.5;
+
+ float sampleDepth = texture(worldPos, offset.xy).z;
+ float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth));
+ occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
+ }
+
+ fragColor = vec4(vec3(occlusion / kernelSize), 1.0f);
+}