From c7bd5ca2e6157ead239d0305eb1f685f227c4956 Mon Sep 17 00:00:00 2001 From: Issac332 <119879937+Issac8658@users.noreply.github.com> Date: Sat, 18 Jul 2026 20:16:15 +0300 Subject: [PATCH] light optimization --- src/shader.cpp | 32 ++++++++++++++++++++++---------- src/shader.h | 1 + 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/shader.cpp b/src/shader.cpp index 7c34b66..ba58591 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -301,6 +301,8 @@ void DynamicLightShader::setWallMapResolution(int x, int y){ } void DynamicLightShader::setCameraPosition(int x, int y){ + cameraPositionCache.x = x; + cameraPositionCache.y = y; gl.Uniform2f(u_cameraPosition, x, y); } @@ -310,21 +312,31 @@ void DynamicLightShader::setTileMapOffset(int x, int y){ void DynamicLightShader::setLightSources(std::vector sources){ int count = 0; - for(int i = 0; i < sources.size(); i++) { + int screenWidth = shState->graphics().width(); + int screenHeight = shState->graphics().height(); + for(int i = 0; i < sources.size(); ++i) { if (count < 64) { - if (!sources[i].hasEffect()) + LightSource source = sources[i]; + if (!source.hasEffect()) continue; + + if (source.x * 32 < cameraPositionCache.x - source.radius * 32 + || source.y * 32 < cameraPositionCache.y - source.radius * 32 + || source.x * 32 > cameraPositionCache.x + source.radius * 32 + screenWidth + || source.y * 32 > cameraPositionCache.y + source.radius * 32 + screenHeight + ) continue; + gl.Uniform4f(u_lightSources + count, - sources[i].x, - sources[i].y, - sources[i].power, - sources[i].radius + source.x, + source.y, + source.power, + source.radius ); gl.Uniform4f(u_lightSourcesColors + count, - sources[i].color.red / 255.0, - sources[i].color.green / 255.0, - sources[i].color.blue / 255.0, - sources[i].color.alpha / 255.0 + source.color.red / 255.0, + source.color.green / 255.0, + source.color.blue / 255.0, + source.color.alpha / 255.0 ); count++; } diff --git a/src/shader.h b/src/shader.h index 4b40b96..e9a5308 100644 --- a/src/shader.h +++ b/src/shader.h @@ -117,6 +117,7 @@ public: private: GLint u_wallMapTexture, u_wallMapResolution, u_cameraPosition, u_tileMapOffset, u_lightSources, u_lightSourcesCount, u_lightSourcesColors, u_ambientLight; + Vec2i cameraPositionCache; }; class SimpleColorShader : public ShaderBase{