Search Unity

what camera does the built-in shader variables reference during a custom ShadowCaster pass

Discussion in 'Shaders' started by Isomania, Jul 2, 2022.

  1. Isomania

    Isomania

    Joined:
    Jan 28, 2021
    Posts:
    16
    Hi!

    I'am currently doing a custom vert shader for my materials that snaps each object to a grid. That part is done and working, however when I try to do the same thing with a custom ShadowCaster pass, I get these artifacts:
    They do not appear when I use the "Legacy Shaders/VertexLit/SHADOWCASTER" pass. However, then, the shadow is not at the correct place. (duh!)

    IF the built-in shader variables reference the same things (ie camera etc) as my main pass does, I see no reason for these artifacts to occur, given that the vertices should be in the same position on both passes no matter the camera.

    So, what differs from the ShadowCaster pass compared to my main pass? Variable wise ofc. Or does that pass only run once per draw call to populate the declared shadowmap? Ignoring how many cameras are in the scene, since the shadowmap is in its own shadowspace (maybe in reference to the light object)? Idk im just spitballing tbh.

    I use:
    unity_ObjectToWorld
    unity_WorldToObject
    UNITY_MATRIX_VP
    UnityObjectToClipPos()

    And render using multiple orthographic cameras.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    unity_ObjectToWorld
    and
    unity_WorldToObject
    will be exactly the same. The
    UNITY_MATRIX_VP
    , and by extension the
    UnityObjectToClipPos()
    which uses it, will be completely different. This is because the VP matrix is for what the currently rendered view is. When rendering the shadow maps the VP matrix is from the “view” of the light. For directional lights this is a view projection matrix (or several if using cascades) that isn’t exposed anywhere to c#.

    If you’re snapping to a world space grid, there shouldn’t be a problem. If you’re snapping to a screen space grid you’ll likely want to use the
    unity_CameraProjection
    and
    unity_CameraInvProjection
    matrices, along with
    unity_WorldToCamera
    and
    unity_CameraToWorld
    to get consistent view or clip space positions between the camera rendering and shadow map rendering.
     
    Isomania likes this.