website articles
soft shadows in raymarched SDFs - 2010


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

The trick

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:

float shadow( in vec3 ro, in vec3 rd, float mint, float maxt ) { for( float t=mint; t<maxt; ) { float h = map(ro + rd*t); if( h<0.001 ) return 0.0; t += h; } return 1.0; }

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.

float softshadow( in vec3 ro, in vec3 rd, float mint, float maxt, float k ) { float res = 1.0; for( float t=mint; t<maxt; ) { float h = map(ro + rd*t); if( h<0.001 ) return 0.0; res = min( res, k*h/t ); t += h; } return res; }

k = 2

k = 8

k = 32

k = 128
This simple modification is enough to generate the much nicer left image in the beginning of this page. As you can see, the improvement is massive: not only you get soft shadows, but they even behave realistically as when shadows are sharp next to occluder and occluder contact (see where the bridge touches the floor) and much softer penumbras when the occluder is far from the occluded point. This, at the cost of one division per marching point, which represents a zero cost relative to the evaluation of map(). Of course, the k parameter in the function controls how hard/soft the shadows are. See images on the right of this text to compare the same shadow rendered with different values of k.

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.

An improvement

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:

float y = r2*r2/(2.0*r1); float d = sqrt(r2*r2-y*y);

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:

float softshadow( in vec3 ro, in vec3 rd, float mint, float maxt, float k ) { float res = 1.0; float ph = 1e20; for( float t=mint; t<maxt; ) { float h = map(ro + rd*t); if( h<0.001 ) return 0.0; float y = h*h/(2.0*ph); float d = sqrt(h*h-y*y); res = min( res, k*d/max(0.0,t-y) ); ph = h; t += h; } return res; }

This produces better shadows in difficult cases, as can be seen in the comparison below:

Original method

Improved method

You can find reference implementation for the improved method here: