Close

Shadows

A project log for Supercon Badge Surface Plotter

Firmware hack for real-time animated 3D surfaces

Ted YapoTed Yapo 11/08/2018 at 20:472 Comments

The last little trick added to the surface is self-shadowing.  At first, this seems like a very complex problem - how can you tell where the shadows are cast in the complex and arbitrary geometry of the animated functions?  As it turns out, pretty easily.

The trick again is in the choice of the light source.  Since we have placed the light source infinitely to the left of the image, the shadows are only cast along the x-direction, in other words, the lines can only shadow themselves.  As we draw each line, we don't have to worry about shadows from any others.  The mechanism for determining if a point is in shadow or not is illustrated in the diagram above.  We start drawing the line from the left side (closest to the light source), and maintain a variable (shadow_z) representing the height of any current shadow.  As we move along the surface, if a point on the line is below shadow_z, it lies in shadow and is colored darker.  Points above the shadow_z level are illuminated, and are colored brighter (using the output of the illumination model).  These illuminated points may cast shadows on subsequent points, so shadow_z is always updated to their level.  Finally, as we traverse the line, shadow_z is decremented a little at each point to simulate the angle from the light source.

But wait, you say: the light source is supposed to be infinitely to the left, so shadows should be cast horizontally, like on a mid-winter afternoon.  This is true, but it doesn't look as good as shadows with a partly vertical angle.  It turns out that your brain doesn't really object to the fact that the illumination and shadow models are using two different light sources.  Another cheat!

Next Up

Source code for everyone!

Discussions

Yann Guidon / YGDES wrote 11/09/2018 at 01:44 point

You're really too clever for us mere mortals !

  Are you sure? yes | no

Ted Yapo wrote 11/09/2018 at 02:48 point

Not at all - I've been re-writing this program in one form or another for 30 years.  Along the way, I stumbled into a few tricks.

  Are you sure? yes | no