From 6ecbdc6b9a5ec1e67701f00464d729403de11a80 Mon Sep 17 00:00:00 2001 From: Geenz Date: Thu, 19 May 2016 20:03:05 -0400 Subject: [PATCH 1/6] 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 From a54e8206faeb7c689bae0e3731ca90a58d3e298c Mon Sep 17 00:00:00 2001 From: Geenz Date: Thu, 19 May 2016 20:10:49 -0400 Subject: [PATCH 2/6] Switch back to using the light's radius + spacing --- libraries/model/src/model/Light.slh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/model/src/model/Light.slh b/libraries/model/src/model/Light.slh index 51c78b9a4d..6211505090 100644 --- a/libraries/model/src/model/Light.slh +++ b/libraries/model/src/model/Light.slh @@ -99,12 +99,14 @@ float getLightShowContour(Light l) { } float evalLightAttenuation(Light l, float d, vec3 ulightvec) { - float radius = getLightSquareRadius(l); + float radius = getLightRadius(l); float denom = d / radius + 1.0; 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; } From 75532cda084c34c09b759eb120d00bfcc7d61ea5 Mon Sep 17 00:00:00 2001 From: Geenz Date: Thu, 19 May 2016 20:52:28 -0400 Subject: [PATCH 3/6] Document evalLightAttenuation's parameters. --- libraries/model/src/model/Light.slh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/model/src/model/Light.slh b/libraries/model/src/model/Light.slh index 6211505090..46eb352d16 100644 --- a/libraries/model/src/model/Light.slh +++ b/libraries/model/src/model/Light.slh @@ -98,6 +98,9 @@ float getLightShowContour(Light l) { return l._control.w; } +// Light is the light source its self, d is the light's distance, and ulightvec is the unnormalized light vector. +// Note that ulightvec should be calculated as light position - fragment position. +// Additionally, length(light position) != dot(light position, light position). float evalLightAttenuation(Light l, float d, vec3 ulightvec) { float radius = getLightRadius(l); float denom = d / radius + 1.0; From 180f4ba4f509e2c70d6f86905d5e4c49ecd704c9 Mon Sep 17 00:00:00 2001 From: Geenz Date: Sun, 22 May 2016 21:02:28 -0400 Subject: [PATCH 4/6] Tweaked light attenuation formula some more. Keeping the ulightvec parameter for now - I want to revisit this later. --- libraries/model/src/model/Light.slh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/model/src/model/Light.slh b/libraries/model/src/model/Light.slh index 46eb352d16..de15e50908 100644 --- a/libraries/model/src/model/Light.slh +++ b/libraries/model/src/model/Light.slh @@ -106,9 +106,11 @@ float evalLightAttenuation(Light l, float d, vec3 ulightvec) { float denom = d / radius + 1.0; float attenuation = 1.0 / (denom * denom); + float cutoff = getLightCutoffRadius(l); + // "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))); + attenuation *= min(1, max(0, -(d - cutoff))); return attenuation; } From ff3fca3dc345c048009fc8ba03263850734aef9f Mon Sep 17 00:00:00 2001 From: Geenz Date: Sun, 22 May 2016 21:50:48 -0400 Subject: [PATCH 5/6] Remove no longer necessary light vector parameter. --- libraries/model/src/model/Light.slh | 6 ++---- libraries/render-utils/src/point_light.slf | 2 +- libraries/render-utils/src/spot_light.slf | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libraries/model/src/model/Light.slh b/libraries/model/src/model/Light.slh index de15e50908..7cb745ff53 100644 --- a/libraries/model/src/model/Light.slh +++ b/libraries/model/src/model/Light.slh @@ -98,10 +98,8 @@ float getLightShowContour(Light l) { return l._control.w; } -// Light is the light source its self, d is the light's distance, and ulightvec is the unnormalized light vector. -// Note that ulightvec should be calculated as light position - fragment position. -// Additionally, length(light position) != dot(light position, light position). -float evalLightAttenuation(Light l, float d, vec3 ulightvec) { +// Light is the light source its self, d is the light's distance calculated as length(unnormalized light vector). +float evalLightAttenuation(Light l, float d) { float radius = getLightRadius(l); float denom = d / radius + 1.0; float attenuation = 1.0 / (denom * denom); diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 581aa8c250..8c9ff2c8ad 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -67,7 +67,7 @@ void main(void) { vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.metallic, frag.specular, frag.roughness); // Eval attenuation - float radialAttenuation = evalLightAttenuation(light, fragLightDistance, fragLightVec); + float radialAttenuation = evalLightAttenuation(light, fragLightDistance); // 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 1cb6ebb878..63e87e95c0 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, fragLightVec); + float radialAttenuation = evalLightAttenuation(light, fragLightDistance4); float angularAttenuation = evalLightSpotAttenuation(light, cosSpotAngle); // Final Lighting color From bd6f6d2eef4916646334b16878bf8cc565c3ce7d Mon Sep 17 00:00:00 2001 From: Geenz Date: Mon, 23 May 2016 09:53:22 -0400 Subject: [PATCH 6/6] Typo. Not quite sure how this got here. --- libraries/render-utils/src/spot_light.slf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 63e87e95c0..4191ba3f63 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, fragLightDistance4); + float radialAttenuation = evalLightAttenuation(light, fragLightDistance); float angularAttenuation = evalLightSpotAttenuation(light, cosSpotAngle); // Final Lighting color