# Shadows in Metal part 2

In this second part of the series, we will be looking into **soft shadows**. We are going to work on the playground we used in Raymarching in Metal and build up on that because it was already set up for `3D`

objects. Let’s set up a basic scene that has a sphere, a plane, a light and a ray:

Next, we create a few `distance operation`

functions that help us determine distances between elements of the scene:

Next, we create a **distanceToScene()** function which gives us the closest distance to any object in the scene. We use these functions to generate a shape that looks like a hollow sphere with holes:

Everything we wrote so far is old code, just refactored from the *Raymarching* article. Let’s talk about **normals** and why they are needed. If we have a flat floor - like our plane - the normal is always `(0, 1, 0)`

, that is, pointing up. This case is trivial though. The normal in `3D`

space is a `float3`

and we need to know its position on the ray. Assume the ray just touches the left side of the sphere. The normal should be `(-1, 0, 0)`

, that is, pointing to the left and away from the sphere. If the ray moves slightly to the right of that point, it’s inside the sphere `(eg. -0.001)`

. If the ray moves slightly to the left, it’s outside the sphere `(eg. 0.001)`

. If we subtract left from right we get `-0.001 - 0.001 = -0.002`

which points to the left, so this is our `x`

coordinate of the normal. We then repeat this for `y`

and `z`

. We use a `2D`

vector named **eps** so we can easily do vector swizzling using the chosen value `0.001`

for various coordinates as needed in each case:

Finally, we are ready to see some visuals. We again use the old `Raymarching`

code and at the end of the kernel function we just add the normal so we can interpolate it with the color for every pixel:

If you run the playground now you should see a similar image:

Now that we have normals, we can calculate lighting for each pixel in the scene, using the **lighting()** function. First we need to know the direction to the light (`lightRay`

) which we get by normalizing the light position and the current ray. For **diffuse** lighting we need the angle between the normal and the `lightRay`

, that is, the dot product of the two. For **specular** lighting we need reflections on surfaces, and they depend on the angle we’re looking at. The difference is in this case we first cast a ray into the scene, reflect it from the surface and then we measure the angle between the reflected ray and the `lightRay`

. We then take a high power of that value to make it much sharper. Finally we return the combined light:

Replace the last line in the kernel function with these lines:

If you run the playground now you should see a similar image:

Next, shadows! We pretty much use the **shadow()** function from the first part of this series, with few modifications. We normalize the direction of the light (`lightDir`

) and then we just keep updating `distAlongRay`

as we march along the ray:

Replace the last line in the kernel function with these lines:

If you run the playground now you should see a similar image:

Let’s get some `soft shadows`

in the scene. In real life, a shadow spreads out the farther it gets from an object. For example, if there is a cube on the floor, at a cube’s vertex we get a sharp shadow but farther away from the cube it looks more like a blurred shadow. In other words, we start at some point on the floor, we march towards the light and either hit or miss. Hard shadows are straightforward: we hit something, it’s in the shadow. Soft shadows have in-between stages. Update the **shadow()** function with these lines:

You will notice that we are starting with a white (`1.0`

) light this time and we use an attenuator (**k**) to get various (intermediate) values of light. The **eps** variable tells us how much wider the beam is as we go out into the scene. A thin beam means sharp shadow while a wide beam means soft shadow. We start with a small `distAlongRay`

because otherwise the surface at this point would shadow itself. We then travel along the ray as we did for the hard shadows, then we get the distance to the scene, after that we subtract `dist`

from `eps`

(the beam width) and divide it by `eps`

. This gives us the percentage of beam covered. If we invert it (`1 - beam width`

) we get the percentage of beam that is in the light. We take the minimum of this new value and `light`

to preserve the darkest shadow as we march along the ray. We then again move along the ray and increase the beam width in proportion to the distance traveled and scaled by `k`

. If we’re past the light, we break out of the loop. Finally, we want to avoid negative values for the light so we return the maximum between **0.0** and the value of light. Now let’s adapt the kernel code to work with the new `shadow()`

function:

Notice we switched to having a rather white color by default. Then we added a boolean named **hit** that tells us if we hit the object or not. We determine we have a hit if the distance to scene is within **0.001**. If we didn’t hit anything, just color everything in grey, otherwise determine the shadow value. At the end we just add another (fixed) light source in front of the scene so see the shadows in greater detail. If you run the playground now you should see a similar image:

To see an animated version of this code, use the `Shadertoy`

embedded player below. Just hover over it and click the play button to watch it in action:

The source code is posted on `Github`

as usual.

Until next time!