One of the many advantages of distance fields, is that they naturally provide global information. This means that when shading a point, one can easily explore the surrounding geometry by simply querying the distance function. Unlike in a classic rasterizer (REYES or scaline based), where one has to bake the global data somehow as a preprocess for later consumption (in a shadowmap, depthmap, pointcloud...), or in a raytracer where finding global information must be done by sampling the geometry by raycasting, in a distance field the information is ready for use at shading time, pretty much for free ("free" with many quotes of course). This means that many of the more realistic shading and illumination techniques are easy to implement with distance fields. And this is even more true when sampling/rendering distance fields with a raymarcher. In this article we are going to exploit this nice properties to render soft shadows with penumbra, for free, when doing raymarching based rendering.
soft shadow and penumbra computed for free
classic shadow raycasting
So, let's assume you have a distance field encoded in function float map(vec3 p). You can see how to construct some basic distance functions here. Just let's assume, for simplicity, that this map() function contains all of the world you are rendering, and that all objects are allowed to cast shadows in all other objects. Then, the easy way to compute shadowing information at a shading point, is to raymarch along the light vector, as far as the distance from the light to the shading point is, until an intersection is found. You can probably do that with some code like this:
This code works beautifully, and produces nice and accurate sharp shadows, as seen in the top-rightmost image in the articles. Now, we can add only one line of code and make this look much better. The trick is to think about what happens when a shadow ray doesn't hit any object, but is just pretty close to do so. Then, perhaps, you want to put that point you are shading under penumbra. Probably, the closest your point was to hit an object, the darker you want to make it. Also, the closest this happened from the point you are shading, the darker too. Well, it happens that as we raymarched our shadow ray, both these distances where available to us! Of course the first one is h in the code above, and the second one is t. So, we can simply compute a penumbra factor for every step point in our marching process and take the darkest of all penumbras.
k = 2
k = 8
k = 32
k = 128
So, basically, if you can do classic raymarched shadows, you can do soft shadows with penumbras too. For free!
The image below shows an example of the technique in action, in a raymarched procedural distance field:
soft penumbra shadows in action
and you can see a lot more examples of this technique in action in the raymarching with signed distance fields article.
7 years after the publication of this technique, Sebastian Aaltonen published an improvement at his GDC presentation, that helps some of the banding artifacts you can get from this technique, especially for shadows from caster with sharp corners.
Beware that for this algorithm to be stable, we should be searching for penumbras exhaustively along the ray. However since we are marching, chances are we miss the point along the ray that produces the darkest penumbra. That can manifest as light leaking in patterns that match the marching steps. In particular, sharp corners in the shadow casters are a usual source of missed darkes penumbras. Sebastian's technique helps with the situation by computing the penumbra as h/t not just at the ray marching sampling positions, but at an estimation of the closest point from the surface to the marched ray at each iteration. Or in other words, by using the current sampling point and the previous one, his technique computes a closest distance estimation by triangulating the information. The picture below shows the geometry of the situation:
The white arrow is the ray we are marching. The green dot is the current position along the ray, and the red dot is the previous position. The green and red circles represent the current and previous SDF unbounding spheres. One can estimate that the closest surface will be at a point close to where these two spheres meet (yellow line and pair of dots). The closest point along the ray will be the intersection of that yellow area with the actual ray (yellow dot in the center).
Let's call y the distance from the current point (green) to that closest point along the ray (yellow), and d the distance from that point to the estimated closest distance (half the length of the yellow line in the diagram above). Then, the code to compute these two quantities is pretty easy:
where r1 and r2 are the radius of the red and green sphere, or in other words, the SDFs evaluation at the previous and current raymarch points. From these two quantities, we can improve our penumbra shadow estimation by doing:
This produces better shadows in difficult cases, as can be seen in the comparison below:
You can find reference implementation for the improved method here: https://www.shadertoy.com/view/lsKcDD