diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index 721545920..d4275e5d2 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -56,7 +56,15 @@ namespace SceneUtil void setDiffuse(int index, const osg::Vec4& value) { - *(unsigned int*)(&(*mData)[3*index][0]) = asRGBA(value); + auto signedValue = value; + float signBit = 1.0; + if (value[0] < 0) + { + signedValue *= -1.0; + signBit = -1.0; + } + *(unsigned int*)(&(*mData)[3*index][0]) = asRGBA(signedValue); + *(int*)(&(*mData)[3*index][3]) = signBit; } void setAmbient(int index, const osg::Vec4& value) @@ -587,8 +595,16 @@ namespace SceneUtil : mStartLight(0) , mLightingMask(~0u) , mSun(nullptr) - , mPointLightRadiusMultiplier(std::max(0.0f, Settings::Manager::getFloat("light bounds multiplier", "Shaders"))) + , mPointLightRadiusMultiplier(std::max(0.f, Settings::Manager::getFloat("light bounds multiplier", "Shaders"))) + , mPointLightFadeStart(0.f) { + mPointLightFadeEnd = std::max(0.f, Settings::Manager::getFloat("maximum light distance", "Shaders")); + if (mPointLightFadeEnd > 0) + { + mPointLightFadeStart = std::clamp(Settings::Manager::getFloat("light fade start", "Shaders"), 0.f, 1.f); + mPointLightFadeStart = mPointLightFadeEnd * mPointLightFadeStart; + } + auto lightingModelString = Settings::Manager::getString("lighting method", "Shaders"); bool validLightingModel = isValidLightingModelString(lightingModelString); if (!validLightingModel) @@ -869,6 +885,19 @@ namespace SceneUtil osg::BoundingSphere viewBound = osg::BoundingSphere(osg::Vec3f(0,0,0), radius * mPointLightRadiusMultiplier); transformBoundingSphere(worldViewMat, viewBound); + static const float fadeDelta = mPointLightFadeEnd - mPointLightFadeStart; + + if (mPointLightFadeEnd != 0.f) + { + float fade = 1 - std::clamp((viewBound.center().length() - mPointLightFadeStart) / fadeDelta, 0.f, 1.f); + + if (fade == 0.f) + continue; + + auto* light = transform.mLightSource->getLight(frameNum); + light->setDiffuse(light->getDiffuse() * fade); + } + LightSourceViewBound l; l.mLightSource = transform.mLightSource; l.mViewBound = viewBound; @@ -896,7 +925,7 @@ namespace SceneUtil auto* light = lightSource->getLight(frameNum); auto& buf = getLightBuffer(frameNum); buf->setDiffuse(index, light->getDiffuse()); - buf->setAmbient(index, light->getSpecular()); + buf->setAmbient(index, light->getAmbient()); buf->setAttenuation(index, light->getConstantAttenuation(), light->getLinearAttenuation(), light->getQuadraticAttenuation()); buf->setRadius(index, lightSource->getRadius()); buf->setPosition(index, light->getPosition() * (*viewMatrix)); diff --git a/components/sceneutil/lightmanager.hpp b/components/sceneutil/lightmanager.hpp index b430153b2..214ffa138 100644 --- a/components/sceneutil/lightmanager.hpp +++ b/components/sceneutil/lightmanager.hpp @@ -215,6 +215,8 @@ namespace SceneUtil LightingMethod mLightingMethod; float mPointLightRadiusMultiplier; + float mPointLightFadeEnd; + float mPointLightFadeStart; int mMaxLights; diff --git a/files/settings-default.cfg b/files/settings-default.cfg index cb23f7df7..b74d3ce02 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -448,7 +448,13 @@ lighting method = experimental # Sets the bounding sphere multiplier of light sources, which are used to determine if an object should # receive lighting. Higher values will allow for smoother transitions of light sources, but may have a performance cost and # requires a higher number of 'max lights' set. It is recommended to keep this at 1.0 with 'legacy' lighting enabled. -light bounds multiplier = 1.9 +light bounds multiplier = 2.0 + +# The distance from the camera at which lights fade away completely. Set to 0 to disable fading. +maximum light distance = 8192 + +# Fraction of the maximum distance at which lights begin to gradually fade away. +light fade start = 0.85 # Set maximum number of lights per object. # Only used when 'lighting method' is not set to 'legacy' diff --git a/files/shaders/lighting.glsl b/files/shaders/lighting.glsl index 36be081ef..257c0513a 100644 --- a/files/shaders/lighting.glsl +++ b/files/shaders/lighting.glsl @@ -16,22 +16,21 @@ float quickstep(float x) #if @useUBO const uint mask = uint(0xff); +const uvec4 shift = uvec4(uint(0), uint(8), uint(16), uint(24)); -vec3 unpackRGB(float data) +vec3 unpackRGB(uint data) { - uint colors = uint(data); - return vec3( (((colors >> 0) & mask) / 255.0) - ,(((colors >> 8) & mask) / 255.0) - ,(((colors >> 16) & mask) / 255.0)); + return vec3( (float(((data >> shift.x) & mask)) / 255.0) + ,(float(((data >> shift.y) & mask)) / 255.0) + ,(float(((data >> shift.z) & mask)) / 255.0)); } -vec4 unpackRGBA(float data) +vec4 unpackRGBA(uint data) { - uint colors = uint(data); - return vec4( (((colors >> 0) & mask) / 255.0) - ,(((colors >> 8) & mask) / 255.0) - ,(((colors >> 16) & mask) / 255.0) - ,(((colors >> 24) & mask) / 255.0)); + return vec4( (float(((data >> shift.x) & mask)) / 255.0) + ,(float(((data >> shift.y) & mask)) / 255.0) + ,(float(((data >> shift.z) & mask)) / 255.0) + ,(float(((data >> shift.w) & mask)) / 255.0)); } struct LightData @@ -74,7 +73,7 @@ void perLightSun(out vec3 ambientOut, out vec3 diffuseOut, vec3 viewPos, vec3 vi vec3 lightDir = normalize(getLight[0].position.xyz); #if @lightingModel == LIGHTING_MODEL_SINGLE_UBO - vec4 data = getLight[0].packedColors; + uvec4 data = getLight[0].packedColors; ambientOut = unpackRGB(data.y); vec3 sunDiffuse = unpackRGB(data.x); #else @@ -108,11 +107,11 @@ void perLightPoint(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec float illumination = clamp(1.0 / (getLight[lightIndex].constantAttenuation + getLight[lightIndex].linearAttenuation * lightDistance + getLight[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0); #else float illumination = clamp(1.0 / (getLight[lightIndex].attenuation.x + getLight[lightIndex].attenuation.y * lightDistance + getLight[lightIndex].attenuation.z * lightDistance * lightDistance), 0.0, 1.0); - illumination *= 1.0 - quickstep((lightDistance * 0.887 / getLight[lightIndex].attenuation.w) - 0.887); + illumination *= 1.0 - quickstep((lightDistance / (getLight[lightIndex].attenuation.w)) - 1.0); #endif #if @useUBO - vec4 data = getLight[lightIndex].packedColors; + uvec4 data = getLight[lightIndex].packedColors; ambientOut = unpackRGB(data.y) * illumination; #else ambientOut = getLight[lightIndex].ambient.xyz * illumination; @@ -133,7 +132,7 @@ void perLightPoint(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec #endif #if @useUBO - diffuseOut = unpackRGB(data.x) * lambert; + diffuseOut = unpackRGB(data.x) * lambert * float(int(data.w)); #else diffuseOut = getLight[lightIndex].diffuse.xyz * lambert; #endif