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 ++++++++++++++++++++++++++++++ cwd/assets/altcraft/shaders/vert/entity.vs | 6 ++++ cwd/assets/altcraft/shaders/vert/face.vs | 6 ++++ cwd/assets/altcraft/shaders/vert/fbo.vs | 3 ++ cwd/assets/altcraft/shaders/vert/light.vs | 3 ++ cwd/assets/altcraft/shaders/vert/pp.vs | 3 ++ cwd/assets/altcraft/shaders/vert/rml.vs | 3 ++ cwd/assets/altcraft/shaders/vert/sky.vs | 3 ++ 12 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 cwd/assets/altcraft/shaders/frag/ssao.fs (limited to 'cwd/assets') 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); +} diff --git a/cwd/assets/altcraft/shaders/vert/entity.vs b/cwd/assets/altcraft/shaders/vert/entity.vs index 4acaa93..f90fbc1 100644 --- a/cwd/assets/altcraft/shaders/vert/entity.vs +++ b/cwd/assets/altcraft/shaders/vert/entity.vs @@ -2,11 +2,16 @@ in vec3 pos; +out vec4 entityWorldPos; + uniform mat4 model; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; @@ -14,4 +19,5 @@ layout (std140) uniform Globals { void main() { gl_Position = projView * model * vec4(pos, 1); + entityWorldPos = view * model * vec4(pos, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index 70c24c8..e50e503 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -8,6 +8,7 @@ in float animation; in vec3 color; in vec2 light; +out vec4 faceWorldPos; out vec3 faceTextureUv; out vec3 faceNormal; out vec3 faceAddColor; @@ -15,7 +16,10 @@ out vec2 faceLight; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; @@ -24,6 +28,8 @@ layout (std140) uniform Globals { void main() { gl_Position = projView * vec4(pos[gl_VertexID], 1.0f); + faceWorldPos = view * vec4(pos[gl_VertexID], 1.0f); + faceTextureUv = vec3(uv[gl_VertexID], uvLayer); faceTextureUv.y -= (uv[2].y - uv[0].y) * trunc(mod(globalTime * 4.0f, animation)); diff --git a/cwd/assets/altcraft/shaders/vert/fbo.vs b/cwd/assets/altcraft/shaders/vert/fbo.vs index cfde14e..0e05eaf 100644 --- a/cwd/assets/altcraft/shaders/vert/fbo.vs +++ b/cwd/assets/altcraft/shaders/vert/fbo.vs @@ -7,7 +7,10 @@ out vec2 uv; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; diff --git a/cwd/assets/altcraft/shaders/vert/light.vs b/cwd/assets/altcraft/shaders/vert/light.vs index 4e551f8..118ea5b 100644 --- a/cwd/assets/altcraft/shaders/vert/light.vs +++ b/cwd/assets/altcraft/shaders/vert/light.vs @@ -7,7 +7,10 @@ out vec2 uv; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; diff --git a/cwd/assets/altcraft/shaders/vert/pp.vs b/cwd/assets/altcraft/shaders/vert/pp.vs index 4e551f8..118ea5b 100644 --- a/cwd/assets/altcraft/shaders/vert/pp.vs +++ b/cwd/assets/altcraft/shaders/vert/pp.vs @@ -7,7 +7,10 @@ out vec2 uv; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; diff --git a/cwd/assets/altcraft/shaders/vert/rml.vs b/cwd/assets/altcraft/shaders/vert/rml.vs index ea78825..c61a4a0 100644 --- a/cwd/assets/altcraft/shaders/vert/rml.vs +++ b/cwd/assets/altcraft/shaders/vert/rml.vs @@ -11,7 +11,10 @@ uniform vec2 translation; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; diff --git a/cwd/assets/altcraft/shaders/vert/sky.vs b/cwd/assets/altcraft/shaders/vert/sky.vs index 617caed..7043584 100644 --- a/cwd/assets/altcraft/shaders/vert/sky.vs +++ b/cwd/assets/altcraft/shaders/vert/sky.vs @@ -8,7 +8,10 @@ uniform mat4 model; layout (std140) uniform Globals { mat4 projView; + mat4 proj; + mat4 view; uvec2 viewportSize; + vec4 ssaoKernels[64]; float globalTime; float dayTime; float gamma; -- cgit v1.2.3