A while back I had a quick search for soft shadowing techniques for ray-marching rendering, but somehow I didn’t manage to find anything very useful. So I just had a play around and came up with something. Here’s the video from last time again:

Then I did another search and found almost exactly the same technique described here by Iñigo Quilez, apart from his method was a bit simpler and more elegant so I’ve just gone with that. I’ll give a bit of background and explain the method anyway.

#### Why shadows are soft

If we want to model lighting in the real world we have to understand how it works and why, and then come up with an approximation in code. In the case of shadows, they are caused when the path from a point in the world to a light source is blocked by something else so the light can’t reach it. Which is obvious, but what the shadow looks like depends on the shape of the light source.

The simplest case is when the light is really tiny, so that it’s effectively just a point in space. In this case, each position in the world can either ‘see’ the light source, or it can’t. Each position is either fully lit or fully in shadow, so you get very sharp shadows. Real lights aren’t points though, and have a size. Therefore positions in the world can see either none of the light, all of it or just some of it. This gives ‘soft’ shadows, with a smooth transition from fully lit to fully shadowed. The larger the light, the softer the shadow.

This is a pretty bad diagram but hopefully it shows what I’m talking about. The region in full shadow is called the umbra, and the region in partial shadow is called the penumbra.

One final thing to note from the diagram is that the size of the penumbra is wider the further away you are from the occluding object. This means you get sharper shadows close to the shadowing object, and blurrier shadows further away.

#### Quick intro to ray marching

I’d better give a brief description of how ray-marching renderers work, or the rest of this won’t make a lot of sense.

When using a ray marching renderer, you define your scene by using an equation. What this equation does is it takes a 3D point and tells you how far that point is from a surface in the scene. To render the scene you start at the eye position and for every pixel on the screen, get a direction vector that goes from the start position through that pixel. Then you iteratively step through the scene along this vector.

At each step, you plug the current position into the equation and it tells you how far you are from a surface. You then know that you can safely move that distance along the vector and not hit anything. As you approach a surface the distance of each step will get smaller and smaller, and at some threshold you stop and say that you’ve hit it. There is a more detailed explanation in this great presentation.

#### Simulating soft shadows

We now know where soft shadows come from and how the ray-marching renderer works, so we can combine them.

The simplest shadowing method is hard shadows. For this we simply need to fire a ray from the position of the pixel we’re rendering, to the sun. If it hits anything, it’s in shadow. If it doesn’t (i.e. the distance function gets sufficiently large) then there is nothing in the way and it’s lit.

But, at each step we know how far the ray is from something in the scene. The closer the ray comes to an object, the more shadowed it’s going to be. Also, the further away from the rendered pixel the object is, the more shadowed it’s going to be (as objects further away case larger penumbra). Therefore, the amount of light at each step is proportional to:

distanceFromObject / distanceFromPixel

Furthermore, the penumbra size is proportional to the light source size, so we can extend it to:

distanceFromObject / (distanceFromPixel * sunSize)

Take the minimum of this at every step and clamp the amount of light to 1 (fully lit), and you’ll get nice soft shadows.

This shadow method doesn’t give fully correct shadows though. The reason is that it only gives the outer half of the penumbra, meaning that there is actually more shadow than there should be (i.e. light energy is lost). Hopefully this illustrates the reason:

Rays that intersect with an object never make it to the light source, they just converge on the object itself. Therefore it is impossible for a single ray to measure the inner half of the penumbra. The central red line is from a pixel that will be drawn as fully shadowed, instead of half shadowed as it should be. However, it still looks nice and it’s a really simple shadowing method, so we can live with it being a little wrong.

Finally, here is the full shadow code if you’re interested:

float shadow = 1.0f; float shadowRayLength = epsilon; while (shadowRad < 150.0) { float3 testPos = worldPos + SunDir*shadowRayLength; float distFromObject = DistanceFunc(testPos); shadowRayLength += distFromObject; shadow = min(shadow, distFromObject/(shadowRayLength*SunSize)); if (distFromObject < 0.0001) { shadow = 0.0; break; } }