From 3f122e57f118db1229a4bad2c54be624f2f8f19c Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 00:51:39 +0500 Subject: Added SSAO --- cwd/assets/altcraft/shaders/frag/entity.fs | 8 +++-- cwd/assets/altcraft/shaders/frag/face.fs | 7 ++-- cwd/assets/altcraft/shaders/frag/light.fs | 24 ++++++++++--- cwd/assets/altcraft/shaders/frag/sky.fs | 9 +++-- cwd/assets/altcraft/shaders/frag/ssao.fs | 55 ++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 cwd/assets/altcraft/shaders/frag/ssao.fs (limited to 'cwd/assets/altcraft/shaders/frag') diff --git a/cwd/assets/altcraft/shaders/frag/entity.fs b/cwd/assets/altcraft/shaders/frag/entity.fs index be4da09..6e77a29 100644 --- a/cwd/assets/altcraft/shaders/frag/entity.fs +++ b/cwd/assets/altcraft/shaders/frag/entity.fs @@ -1,15 +1,19 @@ #version 330 core +in vec4 entityWorldPos; + layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; -layout (location = 2) out vec4 addColor; -layout (location = 3) out vec4 light; +layout (location = 2) out vec4 worldPos; +layout (location = 3) out vec4 addColor; +layout (location = 4) out vec4 light; uniform vec3 entityColor; void main() { color = vec4(entityColor, 1.0f); normal = vec4(0.0f, 0.0f, 0.0f, 1.0f); + worldPos = entityWorldPos; addColor = vec4(0.0f, 0.0f, 0.0f, 1.0f); light = vec4(1.0f, 1.0f, 0.0f, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs index ebda304..65b7d15 100644 --- a/cwd/assets/altcraft/shaders/frag/face.fs +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -1,5 +1,6 @@ #version 330 core +in vec4 faceWorldPos; in vec3 faceTextureUv; in vec3 faceAddColor; in vec3 faceNormal; @@ -7,8 +8,9 @@ in vec2 faceLight; layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; -layout (location = 2) out vec4 addColor; -layout (location = 3) out vec4 light; +layout (location = 2) out vec4 worldPos; +layout (location = 3) out vec4 addColor; +layout (location = 4) out vec4 light; uniform sampler2DArray textureAtlas; @@ -19,6 +21,7 @@ void main() { color = vec4(col.rgb, 1.0f); normal = vec4(faceNormal, 1.0f); + worldPos = faceWorldPos; addColor = vec4(faceAddColor, 1.0f); light = vec4(faceLight / 15.0f, 0.0f, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/frag/light.fs b/cwd/assets/altcraft/shaders/frag/light.fs index 117e0ef..fd7aa4e 100644 --- a/cwd/assets/altcraft/shaders/frag/light.fs +++ b/cwd/assets/altcraft/shaders/frag/light.fs @@ -4,17 +4,22 @@ out vec4 fragColor; in vec2 uv; +uniform sampler2D depthStencil; uniform sampler2D color; uniform sampler2D normal; +uniform sampler2D worldPos; uniform sampler2D addColor; uniform sampler2D light; -uniform sampler2D depthStencil; +uniform sampler2D ssao; uniform int renderBuff; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; @@ -23,9 +28,14 @@ layout (std140) uniform Globals { void main() { vec4 c = texture(color, uv); vec4 n = texture(normal, uv); + n += 1.0f; + n /= 2.0f; + + vec4 wp = texture(worldPos, uv); vec4 ac = texture(addColor, uv); vec4 l = texture(light, uv); float d = (1.0f - texture(depthStencil, uv).r) * 16.0f; + vec4 s = texture(ssao, uv); float faceLight = l.r; float skyLight = l.g; @@ -34,7 +44,7 @@ void main() { lightLevel = clamp(lightLevel, 0.005f, 1.0f); vec3 faceColor = mix(ac.rgb * lightLevel, vec3(1,1,1) * lightLevel, float(ac.rgb == vec3(0,0,0))); - vec4 finalColor = vec4(c.rgb * faceColor, 1.0f); + vec4 finalColor = vec4(c.rgb * faceColor * (1.0f - s.r), 1.0f); finalColor.rgb = pow(finalColor.rgb, vec3(1.0f / gamma)); @@ -49,13 +59,19 @@ void main() { fragColor = n; break; case 3: - fragColor = ac; + fragColor = wp; break; case 4: - fragColor = l; + fragColor = ac; break; case 5: + fragColor = l; + break; + case 6: fragColor = vec4(vec3(d), 1.0f); break; + case 7: + fragColor = s; + break; } } diff --git a/cwd/assets/altcraft/shaders/frag/sky.fs b/cwd/assets/altcraft/shaders/frag/sky.fs index 1e36dd3..65a56ad 100644 --- a/cwd/assets/altcraft/shaders/frag/sky.fs +++ b/cwd/assets/altcraft/shaders/frag/sky.fs @@ -4,12 +4,16 @@ in vec3 facePos; layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; -layout (location = 2) out vec4 addColor; -layout (location = 3) out vec4 light; +layout (location = 2) out vec4 worldPos; +layout (location = 3) out vec4 addColor; +layout (location = 4) out vec4 light; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; @@ -61,6 +65,7 @@ void main() { color += vec4(Sun().rgb, 1.0f); color += vec4(Moon().rgb, 1.0f); normal = vec4(0.0f, 0.0f, 0.0f, 1.0f); + worldPos = vec4(0.0f, 0.0f, 0.0f, 1.0f); addColor = vec4(0.0f, 0.0f, 0.0f, 1.0f); light = vec4(1.0f, 1.0f, 0.0f, 1.0f); } 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); +} -- cgit v1.2.3