Some Light Improvements
One of the bottlenecks of Bunnies is the lighting effects. Then I remembered that light intensity is based on the square of the distance from the light source. That meant I was doing unnecessary squareroots.
The other thing I realized is that when you create a light on the Bunnies map editor you set the radius of the light. Well, what does that mean? Does that mean the light should have no effect at a distance greater than that radius? Well that’s impossible.
I / D^2 = i
I is the intensity of the light. D is the distance from the light to the point. i is the resulting intensity at the point.
As you can see as D becomes large i goes to zero but it will never be zero.
So I came up with a way to make the radius mean something. The radius is the point at which the intensity is 10% of the maximum intensity.
I / D^2 = 0.1
I = 0.1 * D^2
So the actual intensity of the light is now adjusted so that it is 10% of its peak intensity at the specified radius D.
It’s completely arbitrary. I just wanted to choose a percentage that was close to dark but not completely dark. If you lower the percentage too much the light starts shrinking considerably and you’re no longer illuminating the radius that was desired.
So now that’s all taken care of. The radius is used to calculate the lumenesence of the light source.
The next issue was there are a lot of lights in map 3. And mathematically every global light effects every pixel in the map to some amount greater than 0. But most of the time that amount is neglegable. So I devised another formula.
SQRT( I / i) = r
where I is the intensity of the light and i is the minimum intensity I care about and r is the distance at which that intensity occurs.
As i decreases, r increases significantly. So I couldn’t pick too low a number. So I went with 0.01. So now, 1% is the minimum intensity before I ignore the light source.
So now when lighting is calculated, the distance from the light source to the point is calculated and if it’s greater than the distance at which the light source is at 1% of it’s peak then it’s ignored.
This gives us a nice lighting effect without wasting time with light sources that are too far away to really do anything anyway.
With these optimizations in place the frame rate with lighting turned on more than doubled. At the location pictured the frame rate is now about 11fps instead of about 4-6fps.
Leave a comment
You must be logged in to post a comment.