diff --git a/res/scripts/hud.lua b/res/scripts/hud.lua index f415cf39..79fe0e42 100644 --- a/res/scripts/hud.lua +++ b/res/scripts/hud.lua @@ -58,5 +58,5 @@ function on_hud_open() local slot = gfx.posteffects.index("core:default") gfx.posteffects.set_effect(slot, "ssao") - --gfx.posteffects.set_intensity(slot, 1.0) + gfx.posteffects.set_intensity(slot, 1.0) end diff --git a/res/shaders/background.glslf b/res/shaders/background.glslf index b6807a2f..9f588ba5 100644 --- a/res/shaders/background.glslf +++ b/res/shaders/background.glslf @@ -7,7 +7,7 @@ uniform samplerCube u_cubemap; void main(){ vec3 dir = normalize(v_coord); - f_position = vec4(0.0, 0.0, 0.0, 1e9); + f_position = vec4(0.0, 0.0, 0.0, 0.0); f_normal = vec4(0.0); f_color = texture(u_cubemap, dir); } diff --git a/res/shaders/effects/ssao.glsl b/res/shaders/effects/ssao.glsl index f0f65eb6..854b9673 100644 --- a/res/shaders/effects/ssao.glsl +++ b/res/shaders/effects/ssao.glsl @@ -1,6 +1,6 @@ uniform vec3 u_ssaoSamples[64]; -int kernelSize = 32; +int kernelSize = 16; float radius = 0.25; float bias = 0.025; @@ -32,8 +32,10 @@ vec4 effect() { float rangeCheck = smoothstep(0.0, 1.0, radius / abs(position.z - sampleDepth)); occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck; } - occlusion = 1.1 - (occlusion / kernelSize); + occlusion = min(1.0, 1.05 - (occlusion / kernelSize)); } - return vec4(color * occlusion, 1.0); + float z = -position.z * 0.02; + z = max(0.0, 1.0 - z); + return vec4(color * mix(1.0, occlusion, z), 1.0); } diff --git a/res/shaders/main.glslf b/res/shaders/main.glslf index db8b8982..55ecfe2f 100644 --- a/res/shaders/main.glslf +++ b/res/shaders/main.glslf @@ -14,6 +14,8 @@ in vec4 a_modelpos; uniform sampler2D u_texture0; uniform samplerCube u_cubemap; uniform sampler2DShadow u_shadows; +uniform vec3 u_sunDir; +uniform int u_shadowsRes; // flags uniform bool u_alphaClip; @@ -28,21 +30,27 @@ const int BLUR_SAMPLES = 6; void main() { float shadow = 1.0; if (u_enableShadows) { - vec4 mpos = u_shadowsMatrix * vec4(a_modelpos.xyz, 1.0); + vec4 mpos = u_shadowsMatrix * vec4(a_modelpos.xyz + a_realnormal * 0.08, 1.0); vec3 projCoords = mpos.xyz / mpos.w; projCoords = projCoords * 0.5 + 0.5; projCoords.z -= 0.0001; shadow = 0.0; - for (int i = 0; i < BLUR_SAMPLES; i++) { - shadow += texture(u_shadows, projCoords.xyz + vec3( - -0.002*(i%2==0?1:0)*i/BLUR_SAMPLES / 4.0, - -0.002*(i%2==0?0:1)*i/BLUR_SAMPLES / 4.0, - -0.0005 * i/BLUR_SAMPLES)); + if (dot(a_realnormal, u_sunDir) < 0.0) { + for (int x = -1; x <= 1; x++) { + for (int y = -1; y <= 1; y++) { + shadow += texture(u_shadows, projCoords.xyz + vec3( + x * (1.0 / u_shadowsRes), + y * (1.0 / u_shadowsRes), 0.0 + )); + } + } + shadow /= 9; + shadow = shadow * 0.5 + 0.5; + } else { + shadow = 0.5; } - shadow /= BLUR_SAMPLES; - shadow = shadow * 0.5 + 0.5; } vec3 fogColor = texture(u_cubemap, a_dir).rgb; @@ -63,7 +71,7 @@ void main() { } f_color = a_color * texColor; f_color.rgb *= shadow; - f_color = mix(f_color, vec4(fogColor,1.0), a_fog); + f_color = mix(f_color, vec4(fogColor, 1.0), a_fog); f_color.a = alpha; f_position = vec4(a_position, 1.0); f_normal = vec4(a_normal, 1.0); diff --git a/res/shaders/main.glslv b/res/shaders/main.glslv index ab77d17b..8df0b81e 100644 --- a/res/shaders/main.glslv +++ b/res/shaders/main.glslv @@ -37,11 +37,11 @@ void main() { vec3 pos3d = modelpos.xyz-u_cameraPos; modelpos.xyz = apply_planet_curvature(modelpos.xyz, pos3d); - a_realnormal = v_normal.xyz; + a_realnormal = v_normal.xyz * 2.0 - 1.0; mat3 normalMatrix = transpose(inverse(mat3(u_view * u_model))); - a_normal = v_normal.xyz * 2.0 - 1.0; + a_normal = a_realnormal; a_normal = normalMatrix * (false ? -a_normal : a_normal); - //a_normal = v_normal.xyz * 2.0 - 1.0; + //a_normal = a_realnormal; vec3 light = v_light.rgb; float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz) / diff --git a/src/graphics/core/GBuffer.cpp b/src/graphics/core/GBuffer.cpp index 901b6a57..574f7a9b 100644 --- a/src/graphics/core/GBuffer.cpp +++ b/src/graphics/core/GBuffer.cpp @@ -111,6 +111,7 @@ GBuffer::~GBuffer() { void GBuffer::bind() { glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glClear(GL_COLOR_BUFFER_BIT); } void GBuffer::unbind() { diff --git a/src/graphics/core/PostProcessing.cpp b/src/graphics/core/PostProcessing.cpp index e090783a..ceab2c1d 100644 --- a/src/graphics/core/PostProcessing.cpp +++ b/src/graphics/core/PostProcessing.cpp @@ -72,17 +72,21 @@ void PostProcessing::use(DrawContext& context, bool gbufferPipeline) { } context.setFramebuffer(gbuffer.get()); } else { - if (fbo) { - fbo->resize(vp.x, vp.y); - fboSecond->resize(vp.x, vp.y); - } else { - fbo = std::make_unique(vp.x, vp.y); - fboSecond = std::make_unique(vp.x, vp.y); - } + refreshFbos(vp.x, vp.y); context.setFramebuffer(fbo.get()); } } +void PostProcessing::refreshFbos(uint width, uint height) { + if (fbo) { + fbo->resize(width, height); + fboSecond->resize(width, height); + } else { + fbo = std::make_unique(width, height); + fboSecond = std::make_unique(width, height); + } +} + void PostProcessing::configureEffect( const DrawContext& context, Shader& shader, @@ -127,6 +131,9 @@ void PostProcessing::render( totalPasses += (effect != nullptr && effect->isActive()); } + const auto& vp = context.getViewport(); + refreshFbos(vp.x, vp.y); + if (gbuffer) { gbuffer->bindBuffers(); @@ -157,7 +164,9 @@ void PostProcessing::render( auto& shader = effect->use(); configureEffect(context, shader, timer, camera, shadowMap); - fbo->getTexture()->bind(); + if (currentPass > 1) { + fbo->getTexture()->bind(); + } if (currentPass < totalPasses) { fboSecond->bind(); diff --git a/src/graphics/core/PostProcessing.hpp b/src/graphics/core/PostProcessing.hpp index 0ab31a49..41597a4f 100644 --- a/src/graphics/core/PostProcessing.hpp +++ b/src/graphics/core/PostProcessing.hpp @@ -64,6 +64,8 @@ private: uint shadowMap ); + void refreshFbos(uint width, uint height); + /// @brief Main framebuffer (lasy field) std::unique_ptr fbo; std::unique_ptr fboSecond; diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index b3826cf4..31f94803 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -137,6 +137,8 @@ void WorldRenderer::setupWorldShader( if (gbufferPipeline) { shader.uniformMatrix("u_shadowsMatrix", shadowCamera->getProjView()); + shader.uniform3f("u_sunDir", shadowCamera->front); + shader.uniform1i("u_shadowsRes", shadowMap->getResolution()); glActiveTexture(GL_TEXTURE4); shader.uniform1i("u_shadows", 4); glBindTexture(GL_TEXTURE_2D, shadowMap->getDepthMap()); @@ -373,11 +375,11 @@ void WorldRenderer::draw( auto& shadowsShader = assets.require("shadows"); if (gbufferPipeline) { - float shadowMapScale = 0.05f * 2; + float shadowMapScale = 0.05f; float shadowMapSize = shadowMap->getResolution() * shadowMapScale; *shadowCamera = Camera(camera.position, shadowMapSize); - shadowCamera->near = 0.5f; - shadowCamera->far = 600.0f; + shadowCamera->near = 0.1f; + shadowCamera->far = 800.0f; shadowCamera->perspective = false; shadowCamera->setAspectRatio(1.0f); shadowCamera->rotate(glm::radians(-64.0f), glm::radians(-35.0f), glm::radians(-35.0f)); @@ -387,10 +389,10 @@ void WorldRenderer::draw( // ); shadowCamera->updateVectors(); //shadowCamera->position += camera.dir * shadowMapSize * 0.5f; - shadowCamera->position -= shadowCamera->front * 100.0f; + shadowCamera->position -= shadowCamera->front * 200.0f; shadowCamera->position -= shadowCamera->right * (shadowMap->getResolution() * shadowMapScale) * 0.5f; shadowCamera->position -= shadowCamera->up * (shadowMap->getResolution() * shadowMapScale) * 0.5f; - shadowCamera->position = glm::floor(shadowCamera->position); + shadowCamera->position = glm::floor(shadowCamera->position * 0.25f) * 4.0f; { frustumCulling->update(shadowCamera->getProjView()); auto sctx = pctx.sub(); @@ -419,22 +421,22 @@ void WorldRenderer::draw( ctx.setCullFace(true); renderLevel(ctx, camera, settings, uiDelta, pause, hudVisible); // Debug lines - // if (hudVisible) { - // if (debug) { - // guides->renderDebugLines( - // ctx, camera, *lineBatch, linesShader, showChunkBorders - // ); - // } - // if (player.currentCamera == player.fpCamera) { - // renderHands(camera, delta); - // } - // } + if (hudVisible) { + if (debug) { + guides->renderDebugLines( + ctx, camera, *lineBatch, linesShader, showChunkBorders + ); + } + if (player.currentCamera == player.fpCamera) { + renderHands(camera, delta); + } + } } - // { - // DrawContext ctx = wctx.sub(); - // texts->render(ctx, camera, settings, hudVisible, true); - // } - //renderBlockOverlay(wctx); + { + DrawContext ctx = wctx.sub(); + texts->render(ctx, camera, settings, hudVisible, true); + } + renderBlockOverlay(wctx); } postProcessing.render( @@ -444,6 +446,7 @@ void WorldRenderer::draw( camera, gbufferPipeline ? shadowMap->getDepthMap() : 0 ); + glBindTexture(GL_TEXTURE_2D, 0); } void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {