# Shadows in Metal part 1

A quite important topic in `Computer Graphics`

is *lighting and shadows*. This will be a first episode from a multi-part series about **Shadows** in `Metal`

. We are going to work on the playground we used in Using metal part 15 and build up on that. Let’s set up a basic scene:

We first created the **differenceOp()** function which returns the difference between two signed distances. This comes in handy when we want to *carve* shapes out of other shapes. Next, we created the **distanceToRect()** function which determines if a given point is either inside or outside a rectangle. On the `1st`

line we offset the current coordinates by the given center. On the `2nd`

line we get the symmetrical coordinates of the given point. On the `3rd`

line we get the distance to any edge. Then, we created the **distanceToScene()** function which gives us the closest distance to any object in the scene. Note that the `fmod()`

function in `MSL`

uses `trunc()`

instead of `floor()`

so we need to create a custom **mod** operator here because we also want to use the negative values, so we use the `GLSL`

specification for `mod()`

which is `x - y * floor(x/y)`

. We need the `modulus`

operator to draw many small rectangles mirrored on a distance of **0.1** from each other. Finally, we use these functions to generate a shape that looks a bit like a tall building with windows:

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

For shadows to work we need to first - get the distance to the light, second - get the direction to the light, and third - step in that direction until we either reach the light or hit an object. So let’s create a light at position **lightPos** which we will animate for fun. We use that good old **timer** uniform that we have it handy passed from the host (`API`

) code. Then, we get the distance from any given point to `lightPos`

and then just color the pixel based on the distance from the light - if not inside an object. We want the color to be lighter closer to the light and darker when further away. We use the `max()`

function to avoid negative values for the brightness of the light. Replace the last line in the kernel with the lines below:

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

We did the first two steps (light position and direction) so let’s proceed to doing the third one - the actual shadow function:

Let’s go over the code, line by line. We first get the direction from the point to the light. Next, we find the distance to the light so we know how far we need to move along this light ray. Then, we use a loop to divide the ray into many smaller steps. If we don’t use enough steps, we might jump past our object and that would leave “holes” in the shadow. Next, we calculate how far along the ray we are currently and move along the ray by this distance to find the point in space we’re sampling at. Then, we see how far we are from the surface at that point and then test if we are inside an object. If we are, return **0** because we are in the shadow, otherwise return **1** as the ray did not hit any object. It is finally time to see some shadows! In the kernel, replace the last line with the lines below:

We use the value **0.5** to attenuate the effect of the shadow, however, feel free to play with various values and notice how it affects itc. If you run the playground now you should see a similar image:

Right now the loop goes in one-pixel steps which is not good performance-wise. We can improve that a little by accelerating the steps along the ray. We don’t need to move in really small steps. We can move in big steps so long as we don’t step past our object. We can safely step in *any* direction by the distance to the scene instead of a fixed step size, and this way we skip over empty areas really fast! When finding the distance to the nearest surface, we don’t know what direction the surface is in so in fact we have the *radius* of a circle that intersects with the nearest part of the scene. We can trace along the ray, always stepping to the edge of the circle, until the circle radius becomes **0** which means it intersected a surface. Oh, right, this is the **raymarching** technique we learned about last time! Simply replace the content of the **getShadow()** function with the lines below:

In `raymarching`

the size of the step depends on the distance from the surface. In empty areas it jumps big distances and it can travel a long way. But if it’s parallel to the object and close to it, the distance is always small so the jump size is also small. That means the ray travels very slowly. With a fixed number of steps, it doesn’t travel far. With **80** or more steps we should be safe from getting “holes” in the shadow. If you run the playground again the output looks similar except the shadow is faster now. 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:

This type of shadows is called `hard shadows`

. Next time we will be looking into `soft shadows`

which are more realistic and better looking. The source code is posted on `Github`

as usual.

Until next time!