improve shadows

This commit is contained in:
MihailRis 2025-05-21 21:41:01 +03:00
parent 44f7051a94
commit ed5efd9c3a
5 changed files with 68 additions and 65 deletions

View File

@ -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

View File

@ -16,24 +16,21 @@ vec4 effect() {
vec3 bitangent = cross(normal, tangent);
mat3 tbn = mat3(tangent, bitangent, normal);
float occlusion = 1.0;
if (u_enableShadows) {
occlusion = 0.0;
for (int i = 0; i < kernelSize; i++) {
vec3 samplePos = tbn * u_ssaoSamples[i];
samplePos = position + samplePos * radius;
vec4 offset = vec4(samplePos, 1.0);
offset = u_projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
float sampleDepth = texture(u_position, offset.xy).z;
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(position.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
}
occlusion = min(1.0, 1.05 - (occlusion / kernelSize));
float occlusion = 0.0;
for (int i = 0; i < kernelSize; i++) {
vec3 samplePos = tbn * u_ssaoSamples[i];
samplePos = position + samplePos * radius;
vec4 offset = vec4(samplePos, 1.0);
offset = u_projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
float sampleDepth = texture(u_position, offset.xy).z;
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(position.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
}
occlusion = min(1.0, 1.05 - (occlusion / kernelSize));
float z = -position.z * 0.02;
z = max(0.0, 1.0 - z);

View File

@ -1,22 +1,21 @@
#ifndef SHADOWS_GLSL_
#define SHADOWS_GLSL_
uniform sampler2DShadow u_shadows;
uniform sampler2DShadow u_wideShadows;
uniform sampler2DShadow u_shadows[2];
uniform mat4 u_shadowsMatrix[2];
uniform int u_shadowsRes;
uniform bool u_blurShadows;
uniform mat4 u_shadowsMatrix;
uniform mat4 u_wideShadowsMatrix;
float calc_shadow() {
float shadow = 1.0;
if (u_enableShadows) {
vec4 mpos = u_shadowsMatrix * vec4(a_modelpos.xyz + a_realnormal * 0.04, 1.0);
vec4 mpos = u_shadowsMatrix[0] * vec4(a_modelpos.xyz + a_realnormal * 0.04, 1.0);
vec3 projCoords = mpos.xyz / mpos.w;
projCoords = projCoords * 0.5 + 0.5;
projCoords.z -= 0.0001;
vec4 wmpos = u_wideShadowsMatrix * vec4(a_modelpos.xyz + a_realnormal * 0.2, 1.0);
vec4 wmpos = u_shadowsMatrix[1] * vec4(a_modelpos.xyz + a_realnormal * 0.2, 1.0);
vec3 wprojCoords = wmpos.xyz / wmpos.w;
wprojCoords = wprojCoords * 0.5 + 0.5;
wprojCoords.z -= 0.0001;
@ -24,22 +23,37 @@ float calc_shadow() {
shadow = 0.0;
if (dot(a_realnormal, u_sunDir) < 0.0) {
if (u_blurShadows) {
const vec3 offsets[4] = vec3[4](
vec3(0.5, 0.5, 0.0),
vec3(-0.5, 0.5, 0.0),
vec3(0.5, -0.5, 0.0),
vec3(-0.5, -0.5, 0.0)
);
for (int i = 0; i < 4; i++) {
shadow += texture(u_shadows, projCoords.xyz + offsets[i] / u_shadowsRes);
if (u_blurShadows || true) {
if (a_distance > 64) {
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
shadow += texture(
u_shadows[1],
wprojCoords.xyz +
vec3(x, y, -(abs(x) + abs(y))) /
u_shadowsRes
);
}
}
shadow /= 9;
} else {
for (int y = -2; y <= 2; y++) {
for (int x = -2; x <= 2; x++) {
shadow += texture(
u_shadows[0],
projCoords.xyz +
vec3(x, y, -(abs(x) + abs(y))) /
u_shadowsRes * 1.0
);
}
}
shadow /= 25;
}
shadow /= 4;
} else {
if (a_distance > 32.0) {
shadow = texture(u_wideShadows, wprojCoords.xyz);
shadow = texture(u_shadows[1], wprojCoords.xyz);
} else {
shadow = texture(u_shadows, projCoords.xyz);
shadow = texture(u_shadows[0], projCoords.xyz);
}
}
shadow = shadow * 0.5 + 0.5;

View File

@ -203,21 +203,16 @@ void BlocksRenderer::blockXSprite(
const float w = size.x / 1.41f;
const glm::vec4 tint (0.8f);
glm::vec3 n;
float bias = 0.05f;
glm::vec3 n(0.0f, 1.0f, 0.0f);
n = glm::vec3(-0.7f, 0, -0.7f);
face(glm::vec3(x + xs, y, z + zs) + n * bias, w, size.y, 0, {-1, 0, 1}, {0, 1, 0}, n,
face({x + xs, y, z + zs}, w, size.y, 0, {-1, 0, 1}, {0, 1, 0}, n,
texface1, lights2, tint);
n = glm::vec3(-0.7f, 0, 0.7f);
face(glm::vec3(x + xs, y, z + zs) + n * bias, w, size.y, 0, {1, 0, 1}, {0, 1, 0}, n,
face({x + xs, y, z + zs}, w, size.y, 0, {1, 0, 1}, {0, 1, 0}, n,
texface1, lights1, tint);
n = glm::vec3(0.7f, 0, -0.7f);
face(glm::vec3(x + xs, y, z + zs) + n * bias, w, size.y, 0, {-1, 0, -1}, {0, 1, 0}, n,
face({x + xs, y, z + zs}, w, size.y, 0, {-1, 0, -1}, {0, 1, 0}, n,
texface2, lights2, tint);
n = glm::vec3(0.7f, 0, 0.7f);
face(glm::vec3(x + xs, y, z + zs) + n * bias, w, size.y, 0, {1, 0, -1}, {0, 1, 0}, n,
face({x + xs, y, z + zs}, w, size.y, 0, {1, 0, -1}, {0, 1, 0}, n,
texface2, lights1, tint);
}

View File

@ -131,17 +131,17 @@ void WorldRenderer::setupWorldShader(
shader.uniform1i("u_enableShadows", shadows);
if (shadows) {
shader.uniformMatrix("u_shadowsMatrix", shadowCamera.getProjView());
shader.uniformMatrix("u_wideShadowsMatrix", wideShadowCamera.getProjView());
shader.uniformMatrix("u_shadowsMatrix[0]", shadowCamera.getProjView());
shader.uniformMatrix("u_shadowsMatrix[1]", wideShadowCamera.getProjView());
shader.uniform3f("u_sunDir", shadowCamera.front);
shader.uniform1i("u_shadowsRes", shadowMap->getResolution());
glActiveTexture(GL_TEXTURE4);
shader.uniform1i("u_shadows", 4);
shader.uniform1i("u_shadows[0]", 4);
glBindTexture(GL_TEXTURE_2D, shadowMap->getDepthMap());
glActiveTexture(GL_TEXTURE5);
shader.uniform1i("u_wideShadows", 5);
shader.uniform1i("u_shadows[1]", 5);
glBindTexture(GL_TEXTURE_2D, wideShadowMap->getDepthMap());
glActiveTexture(GL_TEXTURE0);
@ -358,11 +358,13 @@ void WorldRenderer::generateShadowsMap(
const auto& settings = engine.getSettings();
int resolution = shadowMap.getResolution();
float shadowMapScale = 0.2f / (1 << glm::max(0L, settings.graphics.shadowsQuality.get())) * scale;
float shadowMapScale =
0.2f / (1 << glm::max(0L, settings.graphics.shadowsQuality.get())) *
scale;
float shadowMapSize = resolution * shadowMapScale;
glm::vec3 basePos = glm::floor(camera.position / 500.0f) * 500.0f;
shadowCamera = Camera(basePos + glm::mod(camera.position, 500.0f), shadowMapSize);
glm::vec3 basePos = glm::floor(camera.position);
shadowCamera = Camera(basePos, shadowMapSize);
shadowCamera.near = 0.1f;
shadowCamera.far = 800.0f;
shadowCamera.perspective = false;
@ -378,20 +380,15 @@ void WorldRenderer::generateShadowsMap(
shadowCamera.position -= shadowCamera.front * 200.0f;
shadowCamera.position += shadowCamera.up * 10.0f;
shadowCamera.position += camera.front * 100.0f;
auto view = shadowCamera.getView();
auto currentPos = shadowCamera.position;
auto min = view * glm::dvec4(currentPos - (shadowCamera.right + shadowCamera.up) * (shadowMapSize * 0.5f), 1.0f);
auto max = view * glm::dvec4(currentPos + (shadowCamera.right + shadowCamera.up) * (shadowMapSize * 0.5f), 1.0f);
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);
float texelSize = (max.x - min.x) / shadowMapSize;
float snappedLeft = glm::round(min.x / texelSize) * texelSize;
float snappedBottom = glm::round(min.y / texelSize) * texelSize;
float snappedRight = snappedLeft + shadowMapSize * texelSize;
float snappedTop = snappedBottom + shadowMapSize * texelSize;
shadowCamera.setProjection(glm::ortho(snappedLeft, snappedRight, snappedBottom, snappedTop, 0.1f, 800.0f));
shadowCamera.setProjection(glm::ortho(min.x, max.x, min.y, max.y, 0.1f, 800.0f));
{
frustumCulling->update(shadowCamera.getProjView());
@ -426,7 +423,7 @@ void WorldRenderer::draw(
const auto& settings = engine.getSettings();
gbufferPipeline = settings.graphics.advancedRender.get();
int shadowsQuality = settings.graphics.shadowsQuality.get();
int resolution = 512 << shadowsQuality;
int resolution = 1024 << shadowsQuality;
if (shadowsQuality > 0 && !shadows) {
shadowMap = std::make_unique<ShadowMap>(resolution);
wideShadowMap = std::make_unique<ShadowMap>(resolution);
@ -454,11 +451,11 @@ void WorldRenderer::draw(
chunks->update();
static int frameid = 0;
if (shadows) {
if (shadows && frameid % 3 == 0) {
if (frameid % 2 == 0) {
generateShadowsMap(camera, pctx, *shadowMap, shadowCamera, 1.0f);
} else {
generateShadowsMap(camera, pctx, *wideShadowMap, wideShadowCamera, 8.0f);
generateShadowsMap(camera, pctx, *wideShadowMap, wideShadowCamera, 4.0f);
}
}
frameid++;