# How can you efficiently get the last element occluding light when rendering shadows?

A long time ago I made an application that used volumetric rendering and I developed some shadowing equations that I felt very happy with, since they have pretty good results.

For these equations to work one needs the object that occludes the light that is closest to the currently-being-shaded fragment (rather than the one closest to the light source)

However, with shadow maps, you invariably get the first point that occludes the light, even if there are points behind it that also occlude the light.

I had originally intended to use volumetric rendering over shadow maps and use clever hashing for optimization.

But 800 triangles consume 90% of a GTX 1070's cpus power. In other words, just 800 triangle checks per fragment are enough to lag the entire pipeline.

I wanted to know if there is a way to get the distance to the closest point occluding the light (from the fragment perspective) using shadow maps. Or if I have to give away with my pretty shadows

Edit:

When I refer to distance I am speaking about distance from the occluded point to the light. So if A is a surface and R is the ray from that surface to the light, then the "closest" point is a point right on A and the "farthest" point is a point right on the light.

Edit:

As per request, a poorly drawn diagram of what I mean. There are 2 walls occluding the light and one is closer to the shadow point than the other

• I don't quite understand how the last object information is helpful for standard shadow mapping. Isn't any element behind "first one" along direction from light source occluded? Can you explain what are you trying to achieve? Are you up to some kind of transparency? If so then "depth peeling" might be the keyword. Feb 13, 2018 at 23:33
• Can you clarify what's meant by "first" and "closest" here? Are you talking about closest distance to the light source, closest distance to the camera, or something else? A diagram might help. I'm having a hard time understanding the question. Feb 13, 2018 at 23:34
• @narthex go outside on a sunny day and look at something casting a shadow, a tree, a car, a sign. Put your hand on the shadow and lift it up a couple centimeters, you will notice that there are 2 shadows now, one is your hand one is the bigger shadow of the object. Feb 14, 2018 at 0:32
• The latter shadow is caused by your hand blocking light from the rest of the sky, not from it extra-blocking light that the first object already blocked. The usual way to approximate that is with ambient occlusion, but I’d be interested to hear your approach. Feb 14, 2018 at 0:44
• "Ambient light" is light from the rest of the sky, or the rest of the environment in general if you're indoors. Anyway, you should definitely look into depth peeling (from the perspective of the light source), as narthex suggested in the very first comment.
– user106
Feb 17, 2018 at 5:32