Browse Source

strongly improved ssao shader (by taking linear depth interpolation and
varying radius depending on the z-distance)

Kajetan Johannes Hammerle 3 years ago
parent
commit
87b613a0c0

+ 4 - 2
client/Game.cpp

@@ -4,7 +4,9 @@
 Game::Game(const Controller& controller, const Clock& fps, const Clock& tps, RenderSettings& settings, const Size& size)
     : controller(controller), fps(fps), tps(tps), renderSettings(settings), size(size), world(blockRegistry),
       worldRenderer(world) {
-    pos = Vector3(16.0f, 24.0f, 0.0f);
+    pos = Vector3(16.0f, 30.0f, -10.0f);
+    rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
+    rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
 }
 
 void Game::tick() {
@@ -15,7 +17,7 @@ void Game::tick() {
     Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
     Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
 
-    const float speed = 6.0f;
+    const float speed = 8.0f;
     if(controller.down.isDown()) {
         pos += back * speed;
     }

+ 13 - 14
client/rendering/Engine.cpp

@@ -1,18 +1,17 @@
 #include "client/rendering/Engine.h"
-#include "gaming-core/wrapper/GL.h"
 #include "client/rendering/Renderer.h"
+#include "gaming-core/wrapper/GL.h"
 
-Engine::Engine(Shaders& shaders, Framebuffers& fb, const Size& size, RenderSettings& renderSettings) :
-shaders(shaders), framebuffers(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 1000.0f, size) {
+Engine::Engine(Shaders& shaders, Framebuffers& fb, const Size& size, RenderSettings& renderSettings)
+    : shaders(shaders), framebuffers(fb), size(size), renderSettings(renderSettings),
+      frustum(60.0f, 0.1f, 1000.0f, size) {
     TypedBuffer<Triangle> buffer(2);
-    buffer.add(Triangle(
-            Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
-            Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
-            Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
-    buffer.add(Triangle(
-            Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
-            Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
-            Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
+    buffer.add(Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
+                        Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
+                        Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
+    buffer.add(Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
+                        Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
+                        Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
     rectangle.build(buffer);
 }
 
@@ -125,7 +124,7 @@ void Engine::updateWorldProjection() {
     if(!renderSettings.shadows) {
         return;
     }
-    
+
     worldShadowProj.set(0, Vector4(2.0f / 40.0f, 0.0f, 0.0f, 0.0f));
     worldShadowProj.set(1, Vector4(0.0f, 2.0f / 30.0f, 0.0f, 0.0f));
     worldShadowProj.set(2, Vector4(0.0f, 0.0f, -2.0f / (1000.0f - 0.1f), 0.0f));
@@ -141,8 +140,8 @@ void Engine::updateWorldView() {
     Vector3 back(0.280166f, 0.573576f, 0.769751f);
     Vector3 up(-0.196175f, 0.819152f, -0.538986f);
     Vector3 center(16.0f, 24.0f, 24.0f);
-    
-    worldShadowView.set(0, Vector4(right[0],  right[1], right[2], right.dot(-center)));
+
+    worldShadowView.set(0, Vector4(right[0], right[1], right[2], right.dot(-center)));
     worldShadowView.set(1, Vector4(up[0], up[1], up[2], up.dot(-center)));
     worldShadowView.set(2, Vector4(back[0], back[1], back[2], back.dot(-center)));
     worldShadowView.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));

+ 4 - 6
client/rendering/Framebuffers.cpp

@@ -1,11 +1,9 @@
 #include "client/rendering/Framebuffers.h"
 
-Framebuffers::Framebuffers() :
-world(TextureFormat::float32(3), TextureFormat::float32(3), TextureFormat::color8(4), TextureFormat::float32(1),
-TextureFormat::depth32()),
-ssao(TextureFormat::float32(1)),
-ssaoBlur(TextureFormat::float32(1)),
-shadow(TextureFormat::depth32()) {
+Framebuffers::Framebuffers()
+    : world(TextureFormat::float32(3), TextureFormat::float32(3), TextureFormat::color8(4), TextureFormat::float32(1),
+            TextureFormat::depth32(true)),
+      ssao(TextureFormat::float32(1)), ssaoBlur(TextureFormat::float32(1)), shadow(TextureFormat::depth32()) {
 }
 
 void Framebuffers::resize(const Size& size) {

+ 1 - 1
client/rendering/RenderSettings.h

@@ -3,7 +3,7 @@
 
 struct RenderSettings final {
     RenderSettings();
-    
+
     bool ssao;
     bool shadows;
     float testRadius;

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit c59799aa8b1629818f235a7afc8602124a1fc030
+Subproject commit 26bdee18ce5b5de78c9ffa3707c6646c9b6742fc

+ 21 - 20
resources/shader/ssaoFragment.fs

@@ -6,34 +6,36 @@ layout (binding = 2) uniform sampler2D ssaoNoise;
 
 const int numberOfSamples = 64;
 const vec3 ssaoKernel[64] = {
-    vec3( 0.009,  0.006, -0.007), vec3( 0.008, -0.003,  0.014), vec3( 0.004, -0.005, -0.019), vec3( 0.016, -0.012, -0.014), 
-    vec3(-0.022, -0.018, -0.007), vec3(-0.018, -0.028, -0.006), vec3( 0.023, -0.028,  0.016), vec3( 0.010, -0.044,  0.000), 
-    vec3( 0.037, -0.035,  0.007), vec3( 0.028,  0.015, -0.048), vec3( 0.030, -0.023,  0.053), vec3(-0.003,  0.042,  0.059), 
-    vec3( 0.077, -0.020, -0.010), vec3( 0.029, -0.053,  0.064), vec3( 0.043,  0.058, -0.064), vec3( 0.102,  0.004,  0.028), 
-    vec3(-0.014, -0.110,  0.029), vec3(-0.019,  0.056, -0.110), vec3(-0.094,  0.019, -0.095), vec3(-0.092,  0.030, -0.108), 
-    vec3(-0.072, -0.120,  0.070), vec3(-0.071,  0.091, -0.121), vec3(-0.096,  0.148, -0.033), vec3(-0.080,  0.160, -0.068), 
-    vec3(-0.037,  0.189, -0.066), vec3( 0.024, -0.038, -0.212), vec3(-0.228, -0.018,  0.021), vec3(-0.169,  0.175,  0.007), 
-    vec3( 0.064, -0.150,  0.200), vec3(-0.017, -0.156, -0.223), vec3(-0.092,  0.240,  0.128), vec3(-0.264, -0.140,  0.044), 
-    vec3(-0.063, -0.311, -0.024), vec3(-0.232, -0.199,  0.136), vec3(-0.333, -0.005,  0.111), vec3( 0.245, -0.255,  0.099), 
-    vec3( 0.214,  0.198, -0.251), vec3( 0.192, -0.325,  0.139), vec3(-0.270,  0.294, -0.131), vec3( 0.303, -0.281, -0.147), 
-    vec3(-0.140, -0.275,  0.338), vec3( 0.387, -0.147,  0.237), vec3( 0.380, -0.260,  0.185), vec3(-0.172,  0.292, -0.390), 
-    vec3( 0.307, -0.081, -0.433), vec3( 0.065, -0.492,  0.256), vec3( 0.224,  0.274, -0.458), vec3( 0.131, -0.370, -0.455), 
-    vec3( 0.113, -0.508, -0.341), vec3(-0.012,  0.573,  0.295), vec3( 0.028, -0.417, -0.521), vec3(-0.555,  0.409, -0.037), 
-    vec3(-0.372,  0.605,  0.075), vec3( 0.352, -0.567, -0.316), vec3( 0.507,  0.558,  0.118), vec3( 0.460,  0.548,  0.329), 
-    vec3(-0.414, -0.391,  0.580), vec3( 0.792,  0.248, -0.122), vec3( 0.207, -0.654, -0.526), vec3( 0.223, -0.713, -0.485), 
-    vec3(-0.333, -0.446, -0.729), vec3( 0.757,  0.249,  0.506), vec3(-0.896, -0.145, -0.349), vec3(-0.125, -0.870, -0.476)
+    vec3(0.011682, -0.003627, 0.009722), vec3(0.015764, 0.021744, -0.015976), vec3(-0.020045, 0.032629, -0.027033), vec3(0.023833, -0.009981, 0.056909), 
+    vec3(-0.022371, 0.002217, 0.074821), vec3(0.079837, 0.026033, 0.041683), vec3(-0.064111, 0.019135, -0.086525), vec3(-0.059657, -0.084172, 0.070577), 
+    vec3(-0.093833, -0.027073, -0.101182), vec3(-0.088102, 0.112365, -0.063453), vec3(0.006216, 0.163006, 0.054144), vec3(-0.154702, 0.104328, 0.018421), 
+    vec3(-0.002524, 0.185987, -0.081623), vec3(0.154714, 0.015249, 0.153891), vec3(-0.051003, 0.200151, -0.110770), vec3(-0.068254, 0.142356, 0.193846), 
+    vec3(-0.183547, 0.191688, 0.011090), vec3(-0.215181, -0.159997, 0.084850), vec3(0.191741, -0.074246, -0.214144), vec3(-0.230607, -0.020322, -0.209913), 
+    vec3(-0.127767, 0.229756, 0.196351), vec3(0.284982, -0.189491, 0.032289), vec3(-0.155238, 0.323741, 0.015594), vec3(0.128016, 0.024124, -0.351646), 
+    vec3(-0.039729, 0.275108, 0.274455), vec3(0.230047, -0.224585, 0.248353), vec3(0.213946, -0.223074, 0.287130), vec3(-0.286808, -0.051429, 0.326347), 
+    vec3(0.325037, -0.167517, -0.267603), vec3(0.400496, -0.152350, 0.190050), vec3(0.450418, 0.087465, 0.155219), vec3(0.321040, -0.054098, 0.379482), 
+    vec3(-0.138326, 0.428701, 0.250899), vec3(0.436693, -0.018605, -0.301960), vec3(0.347054, 0.323834, -0.271583), vec3(0.520263, 0.192617, -0.092904), 
+    vec3(0.182182, -0.333513, 0.435669), vec3(-0.337270, -0.092780, -0.479771), vec3(-0.490179, -0.351030, 0.088546), vec3(-0.098339, -0.389115, 0.479107), 
+    vec3(-0.466230, -0.439320, -0.005353), vec3(0.243684, 0.453502, 0.406962), vec3(0.373559, -0.236580, 0.505865), vec3(-0.249184, -0.390539, -0.507979), 
+    vec3(0.135120, -0.408752, -0.555922), vec3(0.315168, -0.508461, 0.398420), vec3(-0.414646, 0.302526, -0.525217), vec3(0.531577, -0.528467, 0.025467), 
+    vec3(-0.485158, -0.389484, 0.446213), vec3(0.333460, 0.224401, 0.669925), vec3(0.221304, 0.412168, -0.645098), vec3(-0.531421, 0.029417, -0.613908), 
+    vec3(-0.680806, -0.467509, -0.061069), vec3(0.648694, 0.148779, 0.518628), vec3(-0.510980, -0.390245, 0.570205), vec3(-0.419819, 0.553692, -0.531791), 
+    vec3(0.580169, -0.519732, 0.431852), vec3(-0.497161, -0.576404, 0.491811), vec3(0.182698, -0.621941, -0.655488), vec3(-0.190361, 0.725918, 0.561883), 
+    vec3(-0.789754, 0.294172, 0.445196), vec3(-0.103338, 0.408322, -0.872394), vec3(0.093390, 0.779951, -0.593253), vec3(0.787384, 0.123125, 0.604042)
 };
 uniform mat4 proj;
 
 uniform int width;
 uniform int height;
-const float radius = 0.25;
+
+uniform vec3 eyePos;
 
 in vec2 varTex;
 out float color;
 
 void main() {     
     vec3 fragPos = texture(worldPositionSamp, varTex).xyz;
+    float radius = max(-fragPos.z * 0.02, 0.25);
     vec3 random = texture(ssaoNoise, varTex * vec2(width * 0.25, height * 0.25)).xyz * radius * 0.25; 
 
     float occlusion = 0.0;
@@ -44,8 +46,7 @@ void main() {
         float depth1 = texture(worldDepthSamp, projFragPos.xy).x;
         float depth2 = projFragPos.z;
 
-        float rangeCheck = float(abs(depth2 - depth1) < radius);
-        occlusion += float(depth2 >= depth1) * rangeCheck;
+        occlusion += float(depth2 >= depth1);
     }
     color = 1 - occlusion / numberOfSamples;
-} 
+}