Search Unity

Bug Shadow Issues with custom Camera Matrix

Discussion in 'General Graphics' started by DanielThomas, Jul 3, 2022.

  1. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    110
    (CASE IN-9123) (Using URP 12)
    I'm having issues with realtime shadows when using my custom camera worldToCameraMatrix.

    My goal is to have a camera projection that will make the game look top down 2D but allow for shadows, depth etc. - by using planes standing straight up but rendering facing camera. I got this from this article: https://www.gamedeveloper.com/programming/adding-some-perspective-to-your-unity-2d-game-

    I have the camera projection setup as I want it, but there is a problem with shadows not rendering at all when camera goes past a certain distance from the world center. In certain spots one can clearly see the shadow sphere as well.





    This is how I change camera projection:
    Code (CSharp):
    1.   public class CustomProjection : MonoBehaviour
    2.     {
    3.         [SerializeField] private bool _resetCameraMatrixOnEnd;
    4.         [SerializeField] private Camera _camera;
    5.         [SerializeField] private Vector4 _matrixYOffset = new Vector4(0, -1, 1, -0.1f);
    6.         [SerializeField] private Vector4 _matrixZOffset = new Vector4(0, 0, 0, 0);
    7.  
    8.         private void Start()
    9.         {
    10.             _camera = GetComponent<Camera>();
    11.         }
    12.  
    13.         private void OnEnable()
    14.         {
    15.             RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
    16.             RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
    17.         }
    18.  
    19.         private void OnDisable()
    20.         {
    21.             RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
    22.             RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
    23.             _camera.ResetWorldToCameraMatrix();
    24.         }
    25.  
    26.         private void OnBeginCameraRendering(ScriptableRenderContext scriptableRenderContext, Camera camera)
    27.         {
    28.             SetMatrix(camera);
    29.         }
    30.  
    31.         private void OnEndCameraRendering(ScriptableRenderContext scriptableRenderContext, Camera camera)
    32.         {
    33.             ResetMatrix(camera);
    34.         }
    35.  
    36.         private void SetMatrix(Camera camera)
    37.         {
    38.             if (camera != _camera && camera.cameraType != CameraType.Game)
    39.                 return;
    40.        
    41.             var matrix = camera.transform.worldToLocalMatrix;
    42.             matrix.SetRow(2, -matrix.GetRow(2));
    43.             matrix.SetColumn(1, 1e-3f * matrix.GetColumn(1) - _matrixYOffset);
    44.             matrix.SetColumn(2, matrix.GetColumn(2) - _matrixZOffset);
    45.             camera.worldToCameraMatrix = matrix;
    46.         }
    47.  
    48.         private void ResetMatrix(Camera camera)
    49.         {
    50.             if (camera != _camera && camera.cameraType != CameraType.Game && _resetCameraMatrixOnEnd)
    51.                 camera.ResetWorldToCameraMatrix();
    52.         }
    53.     }
    Camera settings:


    I've also attached the small test project and moving the camera around should show the shadows popping in and out clearly.

    Not sure if this is a bug or if Im misunderstanding camera projection and it should be done in another way.
    Any help would be appreciated, thanks!
     

    Attached Files:

    Last edited: Jul 3, 2022
  2. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    110
    Bump
    Also, it seems that URP and the standard RP have different results/behavior. So if any moderator sees this, feel free to move it to the URP section.