Search Unity

How do point lights affect the number of shadow casters?

Discussion in 'General Graphics' started by PsyDev, Dec 18, 2017.

  1. PsyDev

    PsyDev

    Joined:
    Sep 30, 2015
    Posts:
    31
    As you move an object around relative to a point light, the number of shadow casters in the stats window goes up and down. Sometimes 1, sometimes 2, sometimes 3 shadow casters as the object is moved around within the range of the light. I read here in the documentation for Unity 5.6 that point lights use a cube map for shadows. I'm guessing it has something to do with that. But I don't understand how that works, and can't find any info on how the cubemap is used in the shadow rendering. So if anyone could explain why the number of shadow casters changes, and if related, how the cubemaps are used for rendering shadows for point lights, that would be appreciated.

    Note that I'm assuming this has nothing to do with shadow cascades as their are no directional lights in the scene, and setting shadow cascades to No Cascades in Quality settings has no affect on the number of shadow casters in my scene.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Yep, you have it exactly.

    Point lights do use a cube map for shadows. How this works can be thought of like having a 6 spot lights w/ 90 degree angles pointing in the 6 axial directions. FYI spotlights in Unity are actually square frustums, not cones, they just have a circular default cookie texture and gizmo lines that makes them look like a cone. For each spotlight if there's a shadow casting object in it's view it'll render it into the appropriate face of the cubemap. When it comes to sampling the shadow map you just need to know the 3D position relative to the light and sample the cubemap in that direction to get the distance in the cubemap.
     
  3. PsyDev

    PsyDev

    Joined:
    Sep 30, 2015
    Posts:
    31
    Thanks for confirming that. I guess I don't fully understand how the shadowmap sampling works in general, so I'll have to ramp up on that in order to understand how the cubemap sampling fits in.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The short version is it's a depth map.

    You render out a "view" of the scene from the point of view of a light and store the resulting depth in a texture. This is the shadow map. The shadow / depth map simply has a record of the closest shadow casting surface to the light for each pixel of that light's "view". The depth is a value between 0.0 and 1.0 which extends from the near plane to far plane, which is exactly like the near and far planes of your camera.

    When sampling the shadow map you need to know the projection matrix used for that light's "view". You use that to get the "light view" position of each pixel, then tests if it's closer to the light than the distance stored in the shadow map for that position.

    For point lights in Unity it's a little simpler. Instead of getting the projection matrix you just get the world space location relative to the light and scale it by the point light's range to bring it into that 0.0 to 1.0 range. For complicated reasons the resulting shadows aren't as nice or accurate as spot lights or directional lights due to that simplification. Other engines do more literally treat shadowing point lights as 6 spot light shadows which results in much nicer looking shadows, but more complex shaders.
     
    ifurkend likes this.
  5. PsyDev

    PsyDev

    Joined:
    Sep 30, 2015
    Posts:
    31
    Thanks again for the explanation.