Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Bug Shadow Issues with custom Camera Matrix (CASE IN-9123)

Discussion in 'Universal Render Pipeline' started by DanielThomas, Jul 22, 2022.

  1. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    96
    (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 I'm misunderstanding camera projection and it should be done in another way.
    Any help would be appreciated, thanks!
     

    Attached Files:

unityunity