# Light attenuation for deferred shading

## Recommended Posts

l0calh05t    1796
Ok, I am currently experimenting with deferred shading and one of the advantages of deferred shading is that many small lights (limited radius) can be rendered efficiently. But there is one problem: correct light attenuation never drops to zero. So I've been thinking about how the attenuation function could be modified to accommodate a limited radius, without this being too noticeable. I'll be assuming 1/(1+k*d^2) as the standard attenuation function, where k is the base [quadratic] attenuation coefficient, and d is the distance between the light, and the lit point. The simplest way to do this would be to just set the attenuation to 0 (as in multiply the lights intensity with 0) if the normal attenuation reaches a value of... let's say 0.01 (1% of original intensity). Obviously this would result in some quite nasty artifacts. And light radius would be constant as long as the attenuation coefficient isn't modified, but the attenuation coefficient should be constant, as it describes the attenuation in the medium (air, water or glass for example). A better way would probably be to multiply it with a function which passes through 0 at the desired radius and is 1 at a distance of 0. I think a function of the form 1-d^2/r^2 should work well enough. Using this and the same assumption that we want a cutoff at the radius where the attenuation should be 0.01 I got: (r^2-d^2)/(r^2+99*d^2) [result clamped to the range 0..1] I left out k here because this version simply sets a specified radius. Again, not particularly realistic, because we basically modify the attenuation in the medium, but ideal for artist-tunable light radius. But how should the radius of the light be determined? (This is an actual question, not a rhetorical one, despite my insanely long post/discourse ;-) ) One option I came up with is to base it on the lights intensity [Il] relative to the average intensity of the scene [Is], which should be computed for HDR scenes anyway, to be able to adjust exposure, so it's not really any extra work. (Obviously the last frame's intensity has to be used) So for a cutoff at 1% of the scene's intensity I got the following two formulae: r^2 = (100*Il-Is)/(k*Is) [if the result is negative, the light can simply be culled] att = (1-d^2/r^2)/(1+k*d^2) This can easily be adjusted to other cutoffs (the "100" in the first equation is simply 1/0.01) What do you think of this last type of attenuation? Would it work well enough? Or does anyone have any better suggestions? One potential problem might be that the feedback in the equation (via Is) might cause the average brightness of the scene to oscillate, but I think it is improbable that this would happen, as usually the intensity is averaged over time because an instant change in exposure wouldn't look particularly good. (Ok... that was one loooong post :-P)

#### Share this post

##### Share on other sites
yahastu    154
There is no correct way to modify the function for intensity as a function of distance to have a strict cutoff because that is not realistic as you pointed out. Given some arbitrary attenuation function f(x), just pick a cutoff X_max where f(x) is neglibible, then define g(x) = 1 - 1/X_max*x, and use f'(x)=f(x)*g(x) as your new attenuation func.

as a side not, where does that model for light attenuation you are using as a basis come from? 1/(1+kx^2)? because i doubt that is realistic at all. the energy of an electromagneti wave emitted from a point source decreases via the inverse square law, that is, k/x^2, but in addition the molecular density of the medium which is travels through will absorb and further reduce that, that equation is P(x)=exp(-mx) acccording to this guy (http://plus.maths.org/issue13/features/garbett/index.html), so your final equation would be k*exp(-mx)/x^2...if your basic attenuation function has no physical basis, dont waste too much time trying to modify it in a physically correc way

#### Share this post

##### Share on other sites
Cypher19    768
Quote:
 as a side not, where does that model for light attenuation you are using as a basis come from? 1/(1+kx^2)? because i doubt that is realistic at all.

Having a non-zero constant there actually is much more realistic than just plain-jane 1/(kx^2) because the latter shoots to infinity; something real-life (and non-point) lights just don't do.

Fwiw, one option you might want to entertain is having a 1D texture-based lookup for the attenuation, which gives you way more control over the attenuation as a whole: you can have it go from 1 to 0 however you want, and due to linear interpolation can even have it smoothly (and often unnoticably) drop to zero. Plus, you can throw complex equations such as that e^(-mx) stuff without a) making a new shader and b) not incurring any extra cost relative to a quadratic operation.

#### Share this post

##### Share on other sites
l0calh05t    1796
@yahatsu: As I already pointed out in the second line of my original post, correct light attenuation never drops to zero, so yes there is no right way to do this, but I was talking about finding a function which is a sufficiently close approximation and has a smooth cutoff, so that it looks realistic.

You are absolutely right about the quadratic attenuation of a point source, but a point source itself is always an approximation since there are no point sources in real life. In real life, no matter how close you go to a light source (or any other other source of magnetic waves) the maximum intensity when moving closer to it will always be a finite constant, exactly what the function 1/(1+kd^2) provides.

You are also right about the exponential function for decay due to absorption by a medium, but again: I am only looking for a good approximation, and the exponential factor can be approximated quite well by modifying k in the equation I have given. (See pic below comparing 1/(1+1.3*x^2) [red] and e^(-0.1*x)/(1+x^2) [black])

--

@Cypher19:

Isn't the cost of a simple quadratic function such as 1/(1+kd^2) quite close to an extra texture lookup on modern cards? And texture lookups suffer from the same basic problems as (r^2-d^2)/(r^2+99*d^2) i.E. the radius is independent from the intensity unless you scale the function as a whole, effectively modifying the rate of attenuation.

#### Share this post

##### Share on other sites
yahastu    154
Quote:
 Having a non-zero constant there actually is much more realistic than just plain-jane 1/(kx^2) because the latter shoots to infinity; something real-life (and non-point) lights just don't do.

Are you sure about that? It only goes to infinity when the distance asymptotically approaches zero. Have you ever asymptotically approached a light source? I've never looked at a light source from 2-quark radii away, so I don't know. In real life the maximum distance you get to a light is limited by the physical space that the light enclosure assumes etc.

Point vs non-point source makes a big difference for soft shadowing but I don't think it makes any difference for attenuation. You could think of a non-point source as being an infinite collection of point sources, then you see your attenuation would still be an inverse square one, so you can still use inverse square attenuation for non-point sources..no?

Quote:
 @yahatsu: As I already pointed out in the second line of my original post, correct light attenuation never drops to zero, so yes there is no right way to do this, but I was talking about finding a function which is a sufficiently close approximation and has a smooth cutoff, so that it looks realistic.

I hear you, which is why I suggest just multiplying your existing function by a linear attenuation cutoff. Doesn't that do exactly what you want?

Thanks for showing that graph. I didn't notice how closely those two functions match. However, since you equated it to the absorption equation, I guess that proves that it does not account for light attenuation due to the spreading out of photons (the inverse square thing)...wouldn't you want that also to be accounted for?

#### Share this post

##### Share on other sites
l0calh05t    1796
Quote:
 Original post by yahastuAre you sure about that? It only goes to infinity when the distance asymptotically approaches zero. Have you ever asymptotically approached a light source? I've never looked at a light source from 2-quark radii away, so I don't know. In real life the maximum distance you get to a light is limited by the physical space that the light enclosure assumes etc.Point vs non-point source makes a big difference for soft shadowing but I don't think it makes any difference for attenuation. You could think of a non-point source as being an infinite collection of point sources, then you see your attenuation would still be an inverse square one, so you can still use inverse square attenuation for non-point sources..no?

I agree with Cypher on this. Consider this: The inverse square law stems from the spherical expansion of waves from a point source (See: http://hyperphysics.phy-astr.gsu.edu/hbase/vision/isql.html). Now if you have a non-point light you agree that it's brightness is finite, right? Yet from a wave propagation point of view we have an infinite number of point sources! (the whole surface of our real light) Now why is this possible? Well, each of these point sources has an infinitesimally low intensity, and you can only ever be asymptotically close to one of these practically-zero intensity point sources.

Another point is: Light sources only emit a limited number (albeit extremely large) of photons in every unit of time, so infinite strength, even at a distance of 0 is impossible.

Quote:
 I hear you, which is why I suggest just multiplying your existing function by a linear attenuation cutoff. Doesn't that do exactly what you want?

Not really, because that causes a much sharper falloff close to zero. In my original post I suggested multiplying by (1-d^2/r^2) because that effectively "stretches" the attenuation downward slightly. although of course the actual cutoff is sharper...

Quote:
 Thanks for showing that graph. I didn't notice how closely those two functions match. However, since you equated it to the absorption equation, I guess that proves that it does not account for light attenuation due to the spreading out of photons (the inverse square thing)...wouldn't you want that also to be accounted for?

The graph compares 1/(1+1.3*x^2) and e^(-0.1*x)/(1+x^2), so it compares the attenuation function with a changed attenuation constant, to the attenuation function multiplied by the absorption equation, so it already does both.

#### Share this post

##### Share on other sites
yahastu    154
Quote:
 Original post by l0calh05tConsider this: The inverse square law stems from the spherical expansion of waves from a point source. Now if you have a non-point light you agree that it's brightness is finite, right? Yet from a wave propagation point of view we have an infinite number of point sources! (the whole surface of our real light) Now why is this possible? Well, each of these point sources has an infinitesimally low intensity, and you can only ever be asymptotically close to one of these practically-zero intensity point sources.Another point is: Light sources only emit a limited number (albeit extremely large) of photons in every unit of time, so infinite strength, even at a distance of 0 is impossible.

I agree with everything you said in this paragraph. From the first paragraph: Yes, you approach an infinitesimally low-intensity light source...and as you do, the energy of the wave function approaches infinity.

From the second paragraph: true, but the deviation from inverse square would not occur until you got to sub atomic distances...which doesn't really apply to computer games.

These limiting cases we have been discussing are not even relevant, because in the physical world the distance you can get is going to be limited by electromagnetic repulsions....so I would say that the only reason lights appear to have a maximum brightness is because there is a minimum on the distance you can observe them from...combined with the automatic gamma adjustment that your eyes do, which make a light APPEAR to not get brighter as you get closer, because your brain can only perceive a finite amount of brightness.

If your game isn't doing HDR dynamic range adjustments, then I would say that is a good attenuation model. But I think a lot of this "finite brightness" stuff is really just due to automatic adjustment in perception by the eye.

#### Share this post

##### Share on other sites
l0calh05t    1796
Quote:
 Original post by yahastuI agree with everything you said in this paragraph. From the first paragraph: Yes, you approach an infinitesimally low-intensity light source...and as you do, the energy of the wave function approaches infinity.

This, I believe, is where you are mistaken.
sin(x) becomes infinitesimally small when you approach x->0
1/x becomes infinitely large when you approach x->0
sin(x)/x becomes 1 when you approach x->0

Quote:
 From the second paragraph: true, but the deviation from inverse square would not occur until you got to sub atomic distances...which doesn't really apply to computer games.

From the wikipedia article on the topic of Point sources:

Quote:
 LightGenerally a source of light can be considered a point source if the resolution of the imaging instrument is too low to resolve its size, or if the object is at a very great distance.Examples: * Light from a distant star seen through a small telescope * Light passing through a pinhole or other small aperture, viewed from a distance much greater than the size of the hole * Light from a street light in a large-scale study of light pollution or street illumination

You would agree that none of these apply here, correct?

How about a little analogy of astronomical scale:
The inverse square law also applies to gravity.
But yet, the gravity on earth is quite finite. And we are actually touching it's surface. (And if we dig a hole to the center of earth gravity would become less, not more!)
From the wikipedia article: "In mathematics, a point source is a singularity from which flux or flow is emanating."
So, what's the closest thing to an actual point source? A black hole (aka singularity), because it's gravity is very high compared to it's size.
I believe our lights aren't even close. ;-)

Quote:
 These limiting cases we have been discussing are not even relevant, because in the physical world the distance you can get is going to be limited by electromagnetic repulsions....so I would say that the only reason lights appear to have a maximum brightness is because there is a minimum on the distance you can observe them from...combined with the automatic gamma adjustment that your eyes do, which make a light APPEAR to not get brighter as you get closer, because your brain can only perceive a finite amount of brightness.If your game isn't doing HDR dynamic range adjustments, then I would say that is a good attenuation model. But I think a lot of this "finite brightness" stuff is really just due to automatic adjustment in perception by the eye.

See the above points ;-)

I'd recommend you should read the wiki article yourself ( http://en.wikipedia.org/wiki/Point_source ) maybe also look at the article mentioned under "See also". (It deals with the potential finity of infinity :-P)

#### Share this post

##### Share on other sites
yahastu    154
Quote:
 How about a little analogy of astronomical scale:The inverse square law also applies to gravity.But yet, the gravity on earth is quite finite. And we are actually touching it's surface.

Barring the difference that the Earth represents a large number of point surfaces inside a spherical a volume, whereas before we were considering a large number of point surfaces distributed over the surface of a sphere, this is a pretty good example.

It is true that acceleration (and hence, also force) is approximated by the constant 9.8 m/s^2 at sea level. However, think about where that number comes from. It is the integral of the squared distance between each particle in object A with each particle in the Earth. As the distance between the radii of Earth and object A increases, the the ratio of relative difference between any two particle-pairs compared to the difference between radii becomes insignificant. Thus, the two objects can be represented as point sources at a distance for the calculation, and the force of gravity can be treated as constant for that distance.

However, your assumption that gravity on Earth does not follow the inverse square law just because it is finite is not true. If object A were to asymptotically approach the surface of Earth, then some particle pair differences would become extremely high; specifically, the distance between you and a particle on the surface next to you would be close to zero, and the distance between you and a particle on the opposite side of Earth would be the diameter of the Earth. Under this condition, approximating the Earth as a point source where all points are equal distance is no longer valid. In the limiting case, in fact, the equation for force would approach F=mP*m/(rp^2), where mP is the mass of the 1 particle you were closest to, and rp were the distance of you (assuming you are 1 particle just to make things simple, the result is the same if you are multi particulate). Thus, the force of gravity of the Earth does go to infinity according to these simplifications.

In reality, this cannot actually go to infinity, for several reasons. Electromagnetic repulsions that keeps molecules apart from each other give you a cushion of distance that you cannot get through, so you cannot actually approach a point on the surface. Secondly, if you did, fission and fusion would occur, changing the mass of the particles involved. Thirdly, the uncertainty principle forbids your position to be known infinitely for a constant mass, so it is impossible to approach something asymptotically. Fourthly, the inverse square law is not valid at quantum levels anyway because it approximates the EM field as waves, not quantized particles.

So, what is my point?
1) The inverse square law does not apply to subatomic or quantum scales, and thus just because the equation goes to infinity, does not mean it goes to infinity in reality, because as I said earlier, there is a limit to how small the distance can be...and the equation becomes invalid below atomic distances anyway.

2) The Earth DOES follow an inverse square law!

If you consider the Earth as a POINT source of mass and apply the equation for gravity at the radius of the Earth's surface, you get:

a_object = G m_earth / rEarth^2

(6.67300 × 10-11 m3 kg-1 s-2) * (5.9736×1024 KG) / (6 378.1 kilometers^2)^2

*** = 9.7988 m/s^2 ***

Thus, it is valid to represent the Earth as a point source that follows the inverse square law, even though it is not a point source of mass.

3) Therefore, it is also valid to represent a non-point light with the inverse square law in terms of the energy of the wave function...but like the Earth, it does not go to infinity, because you cannot allow sub atomic distances...so the distance is limited by the radius of the physical light size

[Edited by - yahastu on November 30, 2007 1:36:00 PM]

#### Share this post

##### Share on other sites
elFarto    206
There's a couple of equations on the Delphi3D site about attenuation:

http://www.delphi3d.net/articles/viewarticle.php?article=phong.htm

Regards
elFarto

#### Share this post

##### Share on other sites
l0calh05t    1796
Quote:
 2) The Earth DOES follow an inverse square law!If you consider the Earth as a POINT source of mass and apply the equation for gravity at the radius of the Earth's surface, you get:a_object = G m_earth / rEarth^2(6.67300 × 10-11 m3 kg-1 s-2) * (5.9736×1024 KG) / (6 378.1 kilometers^2)^2*** = 9.7988 m/s^2 ***Thus, it is valid to represent the Earth as a point source that follows the inverse square law, even though it is not a point source of mass.

Well, just for fun, lets try 1/(1+d^2) instead: (gravitational constant should actually be 6.67428*10^-11)

(6.673 * 10^-11 m^3 kg^-1 s^-2) * (5.9742 × 10^24kg) /(1+(6378.1 km)^2)

Guess how big the difference is... at the precision of my calculator... zero.
It only begins making a difference using a constant factor > 4000.
So according to this, my equation would be equally valid in this situation. (Go to www.mathe-fa.de/en and plot 1/x^2 and 1/(1+x^2) and see how quickly they approach each other.)

And also note what is written here (the "Note that.." part)
http://en.wikipedia.org/wiki/Earth%27s_gravity#Estimating_g_from_the_law_of_universal_gravitation

@elFarto:
Thanks for the link, but all the radius-limited attenuation equations given on the page have a behavior that is extremely unrealistic

[Edited by - l0calh05t on November 30, 2007 3:17:46 PM]

#### Share this post

##### Share on other sites
yahastu    154
sure, the similarity to inverse square part converges...but at the same time, the representation of the light absorption by the constant diverges :P

#### Share this post

##### Share on other sites
l0calh05t    1796
Quote:
 Original post by yahastusure, the similarity to inverse square part converges...but at the same time, the representation of the light absorption by the constant diverges :P

You mean compared to actually multiplying with with exp(-mx)? Well, that one is obviously an approximation without any real physical basis, I never said that it was the same :-)

#### Share this post

##### Share on other sites
yahastu    154
Quote:
 You mean compared to actually multiplying with with exp(-mx)? Well, that one is obviously an approximation without any real physical basis, I never said that it was the same :-)

I have no idea what the constant should be, but did you read here? It does seem to have a physical basis..

http://plus.maths.org/issue13/features/garbett/index.html

I still don't understand your reasons for insisting on using 1/(1+x^2). It seems that you agree now that, for all realistic distances, we should be in the inverse square region...so why use an equation that deviates from that on no physical basis?

Quote:
 Not really, because that causes a much sharper falloff close to zero. In my original post I suggested multiplying by (1-d^2/r^2) because that effectively "stretches" the attenuation downward slightly. although of course the actual cutoff is sharper...

actually...it is the exact opposite. The cutoff is sharper when you square it than when it is linear.

#### Share this post

##### Share on other sites
l0calh05t    1796
Quote:
 Original post by yahastuI have no idea what the constant should be, but did you read here? It does seem to have a physical basis..http://plus.maths.org/issue13/features/garbett/index.html

Sorry about that, I meant approximating the exp(-mx) by modifying k in the attenuation function 1/kd^2 (or 1/(1+kd^2). exp(-mx) is of course absolutely physically correct :-)

Quote:
 I still don't understand your reasons for insisting on using 1/(1+x^2). It seems that you agree now that, for all realistic distances, we should be in the inverse square region...so why use an equation that deviates from that on no physical basis?

Two reasons:
1. Inverse square law is only exact when you have a real point source. In real life, these do not exist. In 3D graphics we have them, but we actually want to simulate real life. I find 1/(1+kd^2) to be closer to how a real light would behave.
2. 1/(1+kd^2) is far easier to control. No problems with excess brightening directly at the light, and for anything not directly in contact with the light it approaches the behavior of a point source, as in real life where point sources are a valid approximation at a sufficient distance.

To sum it up: 1/(1+kd^2) may not have any physical basis, but it works. 1/kd^2 is an approximation itself (for non-point sources) and is harder to control. So why use the approximation that is difficult to control over the one that is easy to control?

http://medialab.di.unipi.it/web/IUM/Waterloo/node105.html

Quote:
 actually...it is the exact opposite. The cutoff is sharper when you square it than when it is linear.

The cutoff before it goes to zero is sharper, yes, I said that myself. But the falloff at the beginning is less steep.
That is:
at x = 0:
d(1-x^2)/dx = 0 (less falloff at the beginning)
d(1-x)/dx = -1

at x = 1: (cutoff in this case)
d(1-x^2)/dx = -2 (sharper cutoff)
d(1-x)/dx = -1

Edit:

Hm... using (1-(x/r)^2)^3 as a cutoff function would result an both a soft behavior around 0 and a soft cutoff. But of course the divergence from the original curve is larger...

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now