Brighter point lights and light fade

pull/3065/head
glassmancody.info 4 years ago
parent 7370acdf54
commit 05a5cee132

@ -56,7 +56,15 @@ namespace SceneUtil
void setDiffuse(int index, const osg::Vec4& value) 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) void setAmbient(int index, const osg::Vec4& value)
@ -587,8 +595,16 @@ namespace SceneUtil
: mStartLight(0) : mStartLight(0)
, mLightingMask(~0u) , mLightingMask(~0u)
, mSun(nullptr) , 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"); auto lightingModelString = Settings::Manager::getString("lighting method", "Shaders");
bool validLightingModel = isValidLightingModelString(lightingModelString); bool validLightingModel = isValidLightingModelString(lightingModelString);
if (!validLightingModel) if (!validLightingModel)
@ -869,6 +885,19 @@ namespace SceneUtil
osg::BoundingSphere viewBound = osg::BoundingSphere(osg::Vec3f(0,0,0), radius * mPointLightRadiusMultiplier); osg::BoundingSphere viewBound = osg::BoundingSphere(osg::Vec3f(0,0,0), radius * mPointLightRadiusMultiplier);
transformBoundingSphere(worldViewMat, viewBound); 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; LightSourceViewBound l;
l.mLightSource = transform.mLightSource; l.mLightSource = transform.mLightSource;
l.mViewBound = viewBound; l.mViewBound = viewBound;
@ -896,7 +925,7 @@ namespace SceneUtil
auto* light = lightSource->getLight(frameNum); auto* light = lightSource->getLight(frameNum);
auto& buf = getLightBuffer(frameNum); auto& buf = getLightBuffer(frameNum);
buf->setDiffuse(index, light->getDiffuse()); 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->setAttenuation(index, light->getConstantAttenuation(), light->getLinearAttenuation(), light->getQuadraticAttenuation());
buf->setRadius(index, lightSource->getRadius()); buf->setRadius(index, lightSource->getRadius());
buf->setPosition(index, light->getPosition() * (*viewMatrix)); buf->setPosition(index, light->getPosition() * (*viewMatrix));

@ -215,6 +215,8 @@ namespace SceneUtil
LightingMethod mLightingMethod; LightingMethod mLightingMethod;
float mPointLightRadiusMultiplier; float mPointLightRadiusMultiplier;
float mPointLightFadeEnd;
float mPointLightFadeStart;
int mMaxLights; int mMaxLights;

@ -448,7 +448,13 @@ lighting method = experimental
# Sets the bounding sphere multiplier of light sources, which are used to determine if an object should # 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 # 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. # 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. # Set maximum number of lights per object.
# Only used when 'lighting method' is not set to 'legacy' # Only used when 'lighting method' is not set to 'legacy'

@ -16,22 +16,21 @@ float quickstep(float x)
#if @useUBO #if @useUBO
const uint mask = uint(0xff); 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( (float(((data >> shift.x) & mask)) / 255.0)
return vec3( (((colors >> 0) & mask) / 255.0) ,(float(((data >> shift.y) & mask)) / 255.0)
,(((colors >> 8) & mask) / 255.0) ,(float(((data >> shift.z) & mask)) / 255.0));
,(((colors >> 16) & mask) / 255.0));
} }
vec4 unpackRGBA(float data) vec4 unpackRGBA(uint data)
{ {
uint colors = uint(data); return vec4( (float(((data >> shift.x) & mask)) / 255.0)
return vec4( (((colors >> 0) & mask) / 255.0) ,(float(((data >> shift.y) & mask)) / 255.0)
,(((colors >> 8) & mask) / 255.0) ,(float(((data >> shift.z) & mask)) / 255.0)
,(((colors >> 16) & mask) / 255.0) ,(float(((data >> shift.w) & mask)) / 255.0));
,(((colors >> 24) & mask) / 255.0));
} }
struct LightData 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); vec3 lightDir = normalize(getLight[0].position.xyz);
#if @lightingModel == LIGHTING_MODEL_SINGLE_UBO #if @lightingModel == LIGHTING_MODEL_SINGLE_UBO
vec4 data = getLight[0].packedColors; uvec4 data = getLight[0].packedColors;
ambientOut = unpackRGB(data.y); ambientOut = unpackRGB(data.y);
vec3 sunDiffuse = unpackRGB(data.x); vec3 sunDiffuse = unpackRGB(data.x);
#else #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); float illumination = clamp(1.0 / (getLight[lightIndex].constantAttenuation + getLight[lightIndex].linearAttenuation * lightDistance + getLight[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0);
#else #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); 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 #endif
#if @useUBO #if @useUBO
vec4 data = getLight[lightIndex].packedColors; uvec4 data = getLight[lightIndex].packedColors;
ambientOut = unpackRGB(data.y) * illumination; ambientOut = unpackRGB(data.y) * illumination;
#else #else
ambientOut = getLight[lightIndex].ambient.xyz * illumination; ambientOut = getLight[lightIndex].ambient.xyz * illumination;
@ -133,7 +132,7 @@ void perLightPoint(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec
#endif #endif
#if @useUBO #if @useUBO
diffuseOut = unpackRGB(data.x) * lambert; diffuseOut = unpackRGB(data.x) * lambert * float(int(data.w));
#else #else
diffuseOut = getLight[lightIndex].diffuse.xyz * lambert; diffuseOut = getLight[lightIndex].diffuse.xyz * lambert;
#endif #endif

Loading…
Cancel
Save