From 8f20c78ed1bc0a2bdfebbe34e42676afce223372 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Thu, 26 Oct 2023 23:37:51 +0200 Subject: [PATCH] [ChunkRenderer] Now using 'u_mipLevel' uniform to specify max mip level. --- resources/shaders/game.fs.sc | 3 ++- source/client/graphics/ChunkRenderer.cpp | 18 +++++++++++++----- source/client/graphics/ChunkRenderer.hpp | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/resources/shaders/game.fs.sc b/resources/shaders/game.fs.sc index 648d0414..9dc53536 100644 --- a/resources/shaders/game.fs.sc +++ b/resources/shaders/game.fs.sc @@ -13,6 +13,7 @@ uniform vec4 u_renderDistance; uniform vec4 u_fogColor; uniform vec4 u_skyColor; uniform vec4 u_sunlightIntensity; +uniform vec4 u_mipLevel; // Get light color vec4 light(vec4 position, vec4 normal, vec4 color, vec3 lightColor, vec4 lightPosition, float ambientIntensity, float diffuseIntensity) { @@ -50,7 +51,7 @@ void main() { // Get current pixel color and apply multiplier on grayscale textures vec4 color = v_color0; if (v_texcoord0.x > -0.99 && v_texcoord0.y > -0.99) { - color = texture2D(u_tex, v_texcoord0); + color = texture2DLod(u_tex, v_texcoord0, clamp(mix(u_mipLevel.x, u_mipLevel.y, v_dist / u_renderDistance.x), u_mipLevel.x, u_mipLevel.y)); if (blockFace > -1 && color.r == color.g && color.g == color.b) { color *= v_color0; } diff --git a/source/client/graphics/ChunkRenderer.cpp b/source/client/graphics/ChunkRenderer.cpp index 66d6f532..61968052 100644 --- a/source/client/graphics/ChunkRenderer.cpp +++ b/source/client/graphics/ChunkRenderer.cpp @@ -36,9 +36,15 @@ ChunkRenderer::ChunkRenderer(const TextureAtlas &textureAtlas) : m_textureAtlas(textureAtlas) { m_renderDistance = bgfx::createUniform("u_renderDistance", bgfx::UniformType::Vec4); m_fogColor = bgfx::createUniform("u_fogColor", bgfx::UniformType::Vec4); + m_mipLevel = bgfx::createUniform("u_mipLevel", bgfx::UniformType::Vec4); } ChunkRenderer::~ChunkRenderer() { + if (bgfx::isValid(m_mipLevel)) { + bgfx::destroy(m_mipLevel); + m_mipLevel.idx = bgfx::kInvalidHandle; + } + if (bgfx::isValid(m_fogColor)) { bgfx::destroy(m_fogColor); m_fogColor.idx = bgfx::kInvalidHandle; @@ -306,11 +312,11 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const } for (u8 layer = 0 ; layer < ChunkMeshLayer::Count ; ++layer) { -#ifdef OM_NOT_IMPLEMENTED_GL_TEXTURE - // Disable mipmaps for specific layers - glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, - (layer == ChunkMeshLayer::NoMipMap || layer == ChunkMeshLayer::Flora) ? 0 : Config::mipmapLevels)); -#endif // OM_NOT_IMPLEMENTED_GL_TEXTURE + float mipLevel[4] = {0.f, + (layer == ChunkMeshLayer::NoMipMap || layer == ChunkMeshLayer::Flora) + ? 0.f : (float)Config::mipmapLevels, + 0.f, 0.f + }; if (layer == ChunkMeshLayer::Flora || layer == ChunkMeshLayer::Liquid) states.isCullFaceEnabled = false; @@ -321,6 +327,8 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const std::size_t verticesCount = std::get<0>(it)->getVerticesCount(layer); if (verticesCount == 0) continue; + bgfx::setUniform(m_mipLevel, mipLevel); + target.beginDrawing(states); bgfx::setTransform(std::get<1>(it).getRawMatrix()); diff --git a/source/client/graphics/ChunkRenderer.hpp b/source/client/graphics/ChunkRenderer.hpp index 2f45e8fe..9f619fa1 100644 --- a/source/client/graphics/ChunkRenderer.hpp +++ b/source/client/graphics/ChunkRenderer.hpp @@ -62,6 +62,7 @@ class ChunkRenderer { bgfx::UniformHandle m_renderDistance = BGFX_INVALID_HANDLE; bgfx::UniformHandle m_fogColor = BGFX_INVALID_HANDLE; + bgfx::UniformHandle m_mipLevel = BGFX_INVALID_HANDLE; }; #endif // CHUNKRENDERER_HPP_