improve shading & enable disabled elements

This commit is contained in:
MihailRis 2025-05-09 21:52:29 +03:00
parent c1608fe8e2
commit 657b4304b4
9 changed files with 70 additions and 45 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

@ -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);
}

View File

@ -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);
}

View File

@ -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++) {
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(
-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));
x * (1.0 / u_shadowsRes),
y * (1.0 / u_shadowsRes), 0.0
));
}
shadow /= BLUR_SAMPLES;
}
shadow /= 9;
shadow = shadow * 0.5 + 0.5;
} else {
shadow = 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);

View File

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

View File

@ -111,6 +111,7 @@ GBuffer::~GBuffer() {
void GBuffer::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClear(GL_COLOR_BUFFER_BIT);
}
void GBuffer::unbind() {

View File

@ -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<Framebuffer>(vp.x, vp.y);
fboSecond = std::make_unique<Framebuffer>(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<Framebuffer>(width, height);
fboSecond = std::make_unique<Framebuffer>(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);
if (currentPass > 1) {
fbo->getTexture()->bind();
}
if (currentPass < totalPasses) {
fboSecond->bind();

View File

@ -64,6 +64,8 @@ private:
uint shadowMap
);
void refreshFbos(uint width, uint height);
/// @brief Main framebuffer (lasy field)
std::unique_ptr<Framebuffer> fbo;
std::unique_ptr<Framebuffer> fboSecond;

View File

@ -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<Shader>("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
);
}
// {
// DrawContext ctx = wctx.sub();
// texts->render(ctx, camera, settings, hudVisible, true);
// }
//renderBlockOverlay(wctx);
if (player.currentCamera == player.fpCamera) {
renderHands(camera, delta);
}
}
}
{
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) {