fix entities shading

This commit is contained in:
MihailRis
2025-07-07 19:43:18 +03:00
parent 07d39d2742
commit 7a98f0c411
7 changed files with 58 additions and 41 deletions
+21 -8
View File
@@ -18,13 +18,15 @@ struct MainBatchVertex {
glm::vec3 position;
glm::vec2 uv;
glm::vec3 tint;
std::array<uint8_t,4> color;
std::array<uint8_t, 4> color;
std::array<uint8_t, 4> 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<uint8_t>(light.g * 255);
buffer[index].color[2] = static_cast<uint8_t>(light.b * 255);
buffer[index].color[3] = static_cast<uint8_t>(light.a * 255);
buffer[index].normal[0] = static_cast<uint8_t>(normal.x * 128 + 127);
buffer[index].normal[1] = static_cast<uint8_t>(normal.y * 128 + 127);
buffer[index].normal[2] = static_cast<uint8_t>(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)
);
}
+10 -8
View File
@@ -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);
}
}
}
+7 -3
View File
@@ -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<float>(vp.y));
auto& mainShader = assets.require<Shader>("main");
auto& entityShader = assets.require<Shader>("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<ShadowMap>(resolution);