So, I’ve talked about how we render the balls in Hustle Kings, and how we achieve most of the lighting. The remaining thing that gels everything together are the shadows that the balls cast onto the table. We could have quite simply implemented these with drop shadows, small quads in the x-z plane rendered slightly above the table, with a radial alpha gradient. However, this would really not have looked anywhere near as good as we wanted, and this would not have allowed us to cast shadows onto the cushions. I also wanted each ball to cast shadows correctly onto other balls – although we ended up disabling this feature in Hustle Kings, the algorithm I’m about to describe does support this, but we were limited by z-cull requirements of the PlayStation hardware to get maximum performance.
As I mentioned in my last post, our light sources are luminous surfaces modelled by the artists, area lights effectively. I’m only really interested in the shadows caused by the light above the table, as this is far stronger than other lights in the scene, and a global solution would have introduced only subtle differences. I figured that if I treated the light above the table as a 2D rectangle positioned in 3D space, then given that the balls are perfect spheres, a simple bit of maths should give us incredibly accurate shadows.
The first step I take is to render an invisible box around the ball, using stencil masking to mask the pixels on the surface of the table that are potentially affected by the shadow. In the vertex shader, I set the extents of this box such that I clamp to the edge of the shadow as closely as possible, by projecting the light through the extents of the ball. Then, I render the front faces of this box again, only rendering to pixels that are in the stencil mask, and this time use the depth buffer to back transform into world space. Each pixel is shaded black, setting the alpha value to be equal to the proportion of the rectangular light that is occluded by the ball.
So how do I compute the proportion of the light occluded by the ball? Well, time for a bit of math. Lets say we know what point in world space we want to figure out the shadow value for, call this , then set each of the following values relative to that point:-
Now, let’s define a function , which is the visibility of a single point on the light source. This is effectively a binary function, giving a value of one if is visible, or zero otherwise. Then the total proportion of the light that is not occluded is:-
Note that if , i.e. all points visible, then , as expected.
Now we want to figure out how to express . Consider the line , we want to figure out if this intersects the ball:-
This is now a standard quadratic of the form with:-
Since the table and light are always disjoint, is never zero length, assuring us that always. So, remembering the standard formula for solving quadratics:-
We don’t actually care where exactly the points of intersection occur, only whether or not this particular line intersects the sphere. So if this equation has any solutions at all, then an intersection does exist. The only way a solution would not exist would be if the expression under the square root were negative. Recall that if a solution does not exist, then there is a clear, un-occluded path to the light, and therefore . So,
The easiest way to integrate a binary function such as this is to find the size of the interval over which the function holds true. Let’s fix a particular value for and let . Concentrating for now just on the inner integral we have:-
And now expand the components of :-
If we let:-
This is a standard quadratic equation again, this time with:-
So now we can find the roots and use this to help us to work out the size of the interval in the range where is negative, which gives us the result to the inner visibility integral . We must take care here when is close to zero, as here we need to instead solve for .
The outer integral is more difficult to solve analytically, but it turns out we don’t need to. Notice that I chose to be the shortest edge of the light? Well, my solution in Hustle Kings is to integrate along as described, but then sample along and average the results. In fact, as most of the above math is scalar, after the initial dot products, using the vector pipeline of the fragment shader allows us to evaluate four samples at the same time, and it turns out that for Hustle Kings, four samples is all we need.
This does turn out to give us really nice shadows, but even though they look quite accurate and natural, they are still far from correct. For starters, as explained previously, we ignore all other lights in the scene except for the one above the pool table. Rather than subtracting the light contributed by this light in shadow, we simply modulate whatever the overall light level is by a visibility factor. We also don’t take light colour into account, though this doesn’t matter too much as the table light is always white. Surface normals are not considered in the evaluation of the shadow factor either.
Another more subtle point is that we weight every point in the area light above the table equally. In actual fact, the points in the light directly above the point we are evaluating (or more precisely, in the direction of the surface normal), will contribute more to the lighting, and therefore should have a greater effect on the shadow. Resolving this would involve applying a cosine falloff weighting factor to our visibility function , and then rescaling the result of the integral to normalise the case where .
Despite these omissions from the algorithm, we do get much better results than we would have been able to achieve with old fashioned drop shadows, and the basis of the idea of real time analytic solutions to shadowing is there, and something I’m looking forward to exploring further.