From 6ecbdc6b9a5ec1e67701f00464d729403de11a80 Mon Sep 17 00:00:00 2001 From: Geenz Date: Thu, 19 May 2016 20:03:05 -0400 Subject: [PATCH] Modify evalLightAttenuation to "fade" edges of lights. This provides a more attractive light falloff around the bounds of a light - removing harsh edges. This also adds a new parameter to evalLightAttenuation - the unnormalized light vector. --- libraries/model/src/model/Light.slh | 9 ++++++--- libraries/render-utils/src/point_light.slf | 4 ++-- libraries/render-utils/src/spot_light.slf | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libraries/model/src/model/Light.slh b/libraries/model/src/model/Light.slh index 7cc4691d63..51c78b9a4d 100644 --- a/libraries/model/src/model/Light.slh +++ b/libraries/model/src/model/Light.slh @@ -98,10 +98,13 @@ float getLightShowContour(Light l) { return l._control.w; } -float evalLightAttenuation(Light l, float d) { - float radius = getLightRadius(l); +float evalLightAttenuation(Light l, float d, vec3 ulightvec) { + float radius = getLightSquareRadius(l); float denom = d / radius + 1.0; - float attenuation = min(1.0, 1.0 / (denom * denom)); + float attenuation = 1.0 / (denom * denom); + // "Fade" the edges of light sources to make things look a bit more attractive. + // Note: this tends to look a bit odd at lower exponents. + attenuation *= clamp(0, 1, mix(0, 1, getLightCutoffSquareRadius(l) - dot(ulightvec, ulightvec))); return attenuation; } diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index fc72f094e7..581aa8c250 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -66,8 +66,8 @@ void main(void) { vec3 fragEyeDir = normalize(fragEyeVector.xyz); vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.metallic, frag.specular, frag.roughness); - // Eval attenuation - float radialAttenuation = evalLightAttenuation(light, fragLightDistance); + // Eval attenuation + float radialAttenuation = evalLightAttenuation(light, fragLightDistance, fragLightVec); // Final Lighting color vec3 fragColor = (shading.w * frag.diffuse + shading.xyz); diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 4191ba3f63..1cb6ebb878 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -74,7 +74,7 @@ void main(void) { vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.metallic, frag.specular, frag.roughness); // Eval attenuation - float radialAttenuation = evalLightAttenuation(light, fragLightDistance); + float radialAttenuation = evalLightAttenuation(light, fragLightDistance, fragLightVec); float angularAttenuation = evalLightSpotAttenuation(light, cosSpotAngle); // Final Lighting color