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 $latex T $, then set each of the following values relative to that point:-

[latex]B = \mbox{Position of ball, relative to } T\newline

R = \mbox{Radius of ball}\newline

L_0 = \mbox{Position of one corner of light source, relative to } T\newline

L_x = \mbox{Vector along shortest edge of light source}\newline

L_z = \mbox{Vector along longest edge of light source}[/latex]

Now, let’s define a function $latex V(P) $, which is the visibility of a single point $latex P $ on the light source. This is effectively a binary function, giving a value of one if $latex P $ is visible, or zero otherwise. Then the total proportion of the light that is not occluded is:-

$latex S = \int_{x=0}^1 \int_{z=0}^1 V(L_0+xL_x+zL_z)\,dz \,dx$

Note that if $latex V = 1 $, i.e. all points visible, then $latex S = 1 $, as expected.

Now we want to figure out how to express $latex V(P) $. Consider the line $latex \lambda P $, we want to figure out if this intersects the ball:-

[latex]|\lambda P – B| = R\newline

\mbox{or, }(\lambda P – B).(\lambda P – B) = R^2\newline

\mbox{expanding, }\lambda^2P.P – 2\lambda P.B + B.B – R^2 = 0[/latex]

This is now a standard quadratic of the form $latex a\lambda^2 + b\lambda + c = 0 $ with:-

[latex]a = P.P\newline

b = -2P.B\newline

c = B.B – R^2[/latex]

Since the table and light are always disjoint, $latex P $ is never zero length, assuring us that $latex a>0 $ always. So, remembering the standard formula for solving quadratics:-

[latex]\ \frac{-b \pm \sqrt{b^2 – 4ac}}{2a} [/latex]

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 $latex V(P)=1 $. So,

[latex]V(P) = b^2 – 4ac < 0\newline

V(P) = 4(P.B)^2 – 4(P.P)(B.B – R^2) < 0\newline

V(P) = (P.B)^2 < (P.P)(B.B – R^2)[/latex]

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 $latex x $ and let $latex L = L_0 + xL_x $. Concentrating for now just on the inner integral we have:-

[latex]S_x = \int_{z=0}^1 V(L + zL_z) \,dz[/latex]

So set:-

[latex]P = L + zL_z[/latex]

Recall that:-

[latex]V(P) = (P.B)^2 < (P.P)(B.B – R^2)[/latex]

Let:-

[latex]H = B.B – R^2[/latex]

And now expand the components of $latex V(P) $:-

[latex](P.B)^2 = (L.B + zL_z.B)^2\newline

\indent= (L.B)^2 + 2z(L.B)(L_z.B) + z^2(L_z.B)^2\newline

P.P = (L + zL_z).(L + zL_z)\newline

\indent= L.L + 2z(L.L_z) + z^2(L_z.L_z)[/latex]

If we let:-

[latex]K(P) = (P.B)^2 – H(P.P)[/latex]

Then:-

[latex]V(P) = K(P) < 0[/latex]

So:-

[latex]K = z^2[(L_z.B)^2 – H(L_z.L_z)]\newline\indent+ z[2(L.B)(L_z.B) – 2H(L.L_z)]\newline\indent+ (L.B)^2 – H(L.L)[/latex]

This is a standard quadratic equation again, this time with:-

[latex]a = (L_z.B)^2 – H(L_z.L_z)\newline

b = 2(L.B)(L_z.B) – 2H(L.L_z)\newline

c = (L.B)^2 – H(L.L)[/latex]

So now we can find the roots and use this to help us to work out the size of the interval in the range $latex z = (0, 1) $ where $latex K $ is negative, which gives us the result to the inner visibility integral $latex S_x $. We must take care here when $latex a $ is close to zero, as here we need to instead solve for $latex bz + c < 0 $.

The outer integral is more difficult to solve analytically, but it turns out we don’t need to. Notice that I chose $latex L_x $ to be the shortest edge of the light? Well, my solution in Hustle Kings is to integrate along $latex z $ as described, but then sample along $latex x $ 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.

**Conclusion**

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 $latex V(P) $, and then rescaling the result of the integral to normalise the case where $latex V(P) = 1 $.

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.

Yz-Brd says

I would like 2 ask where I could find the date of the features will be released in Canada. I cant find it anywhere.This is the best pool ive played in all the years that ive played.It would be nice to see some more crazy tables ( good practice 4 bankshots.).Keep up the good work.I hope icomes soon……I can hardly wait.