diff --git a/res/shaders/entity.glslf b/res/shaders/entity.glslf index 4348c60f..cda2c92c 100644 --- a/res/shaders/entity.glslf +++ b/res/shaders/entity.glslf @@ -1,17 +1,17 @@ -in float a_distance; -in vec4 a_color; -in vec2 a_texCoord; -in vec3 a_position; -in vec3 a_dir; -in vec3 a_normal; -in vec3 a_realnormal; -in vec4 a_modelpos; -in float a_fog; - layout (location = 0) out vec4 f_color; layout (location = 1) out vec4 f_position; layout (location = 2) out vec4 f_normal; +in float a_distance; +in float a_fog; +in vec2 a_texCoord; +in vec3 a_dir; +in vec3 a_normal; +in vec3 a_position; +in vec3 a_realnormal; +in vec4 a_color; +in vec4 a_modelpos; + uniform sampler2D u_texture0; uniform samplerCube u_skybox; uniform vec3 u_fogColor; @@ -20,8 +20,6 @@ uniform float u_fogCurve; uniform bool u_alphaClip; uniform vec3 u_sunDir; -uniform bool u_enableShadows; - #include void main() { @@ -33,8 +31,7 @@ void main() { if (alpha < (u_alphaClip ? 0.5f : 0.15f)) { discard; } - f_color = a_color * tex_color; - f_color.rgb *= shadow; + f_color = a_color * tex_color * shadow; f_color = mix(f_color, vec4(fogColor, 1.0), a_fog); f_color.a = alpha; f_position = vec4(a_position, 1.0); diff --git a/res/shaders/entity.glslv b/res/shaders/entity.glslv index 60a1b715..df11f062 100644 --- a/res/shaders/entity.glslv +++ b/res/shaders/entity.glslv @@ -4,6 +4,7 @@ layout (location = 0) in vec3 v_position; layout (location = 1) in vec2 v_texCoord; layout (location = 2) in vec3 v_color; layout (location = 3) in vec4 v_light; +layout (location = 4) in vec4 v_normal; out float a_distance; out float a_fog; @@ -34,8 +35,8 @@ void main() { vec3 pos3d = a_modelpos.xyz - u_cameraPos; a_modelpos.xyz = apply_planet_curvature(a_modelpos.xyz, pos3d); - a_normal = vec3(0.0, 1.0, 0.0);//v_normal.xyz * 2.0 - 1.0; - a_realnormal = a_normal; + a_realnormal = v_normal.xyz * 2.0 - 1.0; + a_normal = calc_screen_normal(a_realnormal); vec3 light = v_light.rgb; float torchlight = calc_torch_light(a_realnormal, a_modelpos.xyz); diff --git a/res/shaders/lib/lighting.glsl b/res/shaders/lib/lighting.glsl index 2d164c30..a18a85a2 100644 --- a/res/shaders/lib/lighting.glsl +++ b/res/shaders/lib/lighting.glsl @@ -8,7 +8,8 @@ uniform float u_weatherFogDencity; uniform float u_weatherFogCurve; float calc_torch_light(vec3 normal, vec3 modelpos) { - return max(0.0, 1.0 - distance(u_cameraPos, modelpos) / u_torchlightDistance) * max(0.0, -dot(normal, normalize(modelpos - u_cameraPos))); + return max(0.0, 1.0 - distance(u_cameraPos, modelpos) / u_torchlightDistance) + * max(0.0, -dot(normal, normalize(modelpos - u_cameraPos))); } vec3 calc_screen_normal(vec3 normal) { diff --git a/res/shaders/main.glslf b/res/shaders/main.glslf index f1a4a549..51f0b9c6 100644 --- a/res/shaders/main.glslf +++ b/res/shaders/main.glslf @@ -3,15 +3,15 @@ layout (location = 1) out vec4 f_position; layout (location = 2) out vec4 f_normal; in float a_distance; -in vec4 a_torchLight; -in vec3 a_skyLight; -in vec2 a_texCoord; in float a_fog; -in vec3 a_position; +in vec2 a_texCoord; in vec3 a_dir; in vec3 a_normal; +in vec3 a_position; in vec3 a_realnormal; +in vec3 a_skyLight; in vec4 a_modelpos; +in vec4 a_torchLight; uniform sampler2D u_texture0; uniform samplerCube u_skybox; @@ -21,7 +21,6 @@ uniform vec3 u_sunDir; uniform bool u_alphaClip; uniform bool u_debugLights; uniform bool u_debugNormals; -uniform bool u_enableShadows; #include diff --git a/src/graphics/render/MainBatch.hpp b/src/graphics/render/MainBatch.hpp index 2ca1d3d0..bafecd80 100644 --- a/src/graphics/render/MainBatch.hpp +++ b/src/graphics/render/MainBatch.hpp @@ -18,13 +18,15 @@ struct MainBatchVertex { glm::vec3 position; glm::vec2 uv; glm::vec3 tint; - std::array color; + std::array color; + std::array normal; static constexpr VertexAttribute ATTRIBUTES[] = { {VertexAttribute::Type::FLOAT, false, 3}, {VertexAttribute::Type::FLOAT, false, 2}, {VertexAttribute::Type::FLOAT, false, 3}, {VertexAttribute::Type::UNSIGNED_BYTE, true, 4}, + {VertexAttribute::Type::UNSIGNED_BYTE, true, 4}, {{}, 0}}; }; @@ -61,7 +63,8 @@ public: const glm::vec3& pos, const glm::vec2& uv, const glm::vec4& light, - const glm::vec3& tint + const glm::vec3& tint, + const glm::vec3& normal ) { MainBatchVertex* buffer = this->buffer.get(); buffer[index].position = pos; @@ -72,6 +75,10 @@ public: buffer[index].color[1] = static_cast(light.g * 255); buffer[index].color[2] = static_cast(light.b * 255); buffer[index].color[3] = static_cast(light.a * 255); + + buffer[index].normal[0] = static_cast(normal.x * 128 + 127); + buffer[index].normal[1] = static_cast(normal.y * 128 + 127); + buffer[index].normal[2] = static_cast(normal.z * 128 + 127); index++; } @@ -89,38 +96,44 @@ public: pos - right * size.x * 0.5f - up * size.y * 0.5f, {subregion.u1, subregion.v1}, light, - tint + tint, + glm::cross(up, right) ); vertex( pos + right * size.x * 0.5f - up * size.y * 0.5f, {subregion.u2, subregion.v1}, light, - tint + tint, + glm::cross(up, right) ); vertex( pos + right * size.x * 0.5f + up * size.y * 0.5f, {subregion.u2, subregion.v2}, light, - tint + tint, + glm::cross(up, right) ); vertex( pos - right * size.x * 0.5f - up * size.y * 0.5f, {subregion.u1, subregion.v1}, light, - tint + tint, + glm::cross(up, right) ); vertex( pos + right * size.x * 0.5f + up * size.y * 0.5f, {subregion.u2, subregion.v2}, light, - tint + tint, + glm::cross(up, right) ); vertex( pos - right * size.x * 0.5f + up * size.y * 0.5f, {subregion.u1, subregion.v2}, light, - tint + tint, + glm::cross(up, right) ); } diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 08deb7bd..77e948e7 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -57,12 +57,14 @@ ModelBatch::ModelBatch( ModelBatch::~ModelBatch() = default; -void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, - const glm::mat3& rotation, glm::vec3 tint, - const texture_names_map* varTextures, - bool backlight) { - - +void ModelBatch::draw( + const model::Mesh& mesh, + const glm::mat4& matrix, + const glm::mat3& rotation, + glm::vec3 tint, + const texture_names_map* varTextures, + bool backlight +) { setTexture(mesh.texture, varTextures); size_t vcount = mesh.vertices.size(); const auto& vertexData = mesh.vertices.data(); @@ -78,12 +80,12 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, for (size_t j = 0; j < 3; j++) { const auto vert = vertexData[i * 3 + j]; float d = 1.0f; + auto norm = rotation * vert.normal; if (mesh.lighting) { - auto norm = rotation * vert.normal; d = glm::dot(norm, SUN_VECTOR); d = 0.8f + d * 0.2f; } - batch->vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, lights*d, tint); + batch->vertex(matrix * glm::vec4(vert.coord, 1.0f), vert.uv, lights*d, tint, norm); } } } diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 4d2a2c2a..3f74d8fb 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -367,7 +367,7 @@ void WorldRenderer::generateShadowsMap( float shadowMapScale = 0.32f / (1 << glm::max(0, quality)) * scale; float shadowMapSize = resolution * shadowMapScale; - glm::vec3 basePos = glm::floor(camera.position); + glm::vec3 basePos = glm::floor(camera.position / 4.0f) * 4.0f; shadowCamera = Camera(basePos, shadowMapSize); shadowCamera.near = 0.1f; shadowCamera.far = 1000.0f; @@ -400,8 +400,9 @@ void WorldRenderer::generateShadowsMap( auto view = shadowCamera.getView(); auto currentPos = shadowCamera.position; - auto min = view * glm::vec4(currentPos - (shadowCamera.right + shadowCamera.up) * (shadowMapSize * 0.5f), 1.0f); - auto max = view * glm::vec4(currentPos + (shadowCamera.right + shadowCamera.up) * (shadowMapSize * 0.5f), 1.0f); + auto topRight = shadowCamera.right + shadowCamera.up; + auto min = view * glm::vec4(currentPos - topRight * shadowMapSize * 0.5f, 1.0f); + auto max = view * glm::vec4(currentPos + topRight * shadowMapSize * 0.5f, 1.0f); shadowCamera.setProjection(glm::ortho(min.x, max.x, min.y, max.y, 0.1f, 1000.0f)); @@ -436,6 +437,7 @@ void WorldRenderer::draw( camera.setAspectRatio(vp.x / static_cast(vp.y)); auto& mainShader = assets.require("main"); + auto& entityShader = assets.require("entity"); const auto& settings = engine.getSettings(); gbufferPipeline = settings.graphics.advancedRender.get(); int shadowsQuality = settings.graphics.shadowsQuality.get(); @@ -446,12 +448,14 @@ void WorldRenderer::draw( shadows = true; Shader::preprocessor->define("ENABLE_SHADOWS", "true"); mainShader.recompile(); + entityShader.recompile(); } else if (shadowsQuality == 0 && shadows) { shadowMap.reset(); wideShadowMap.reset(); shadows = false; Shader::preprocessor->undefine("ENABLE_SHADOWS"); mainShader.recompile(); + entityShader.recompile(); } if (shadows && shadowMap->getResolution() != resolution) { shadowMap = std::make_unique(resolution);