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. Dismiss Notice

Question Forward+ and custom camera projection

Discussion in 'Universal Render Pipeline' started by DanielThomas, Feb 16, 2023.

  1. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    106
    I'm trying Forward+ with URP 14.0.5.
    I have a custom camera projection where Y is pointing in in Z direction instead (matrix.y 0,0,1). (this is just to make fake 2D game, so everything is projected flat toward the camera)

    But the point lights aren't affecting anything with the projection. Changing render path OR resetting the projection, OR even setting matrix.y to 0,0,0 makes the point light work again.
    As I'm not fully aware of where the forward plus lights are getting its information from I'm not sure why this isn't working. I'm going to take a guess that it has to do with the depth.

    Is this just how it will work or is there a workaround, or maybe even it's a bug?

    Any suggestions would be much appreciated, thanks!
     
  2. peterbay

    peterbay

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    100
    I'm not sure if there's a way to make that work, since the culling math is inherently tied to the projection type. It uses the world-to-view matrix from the camera though. We're currently in the process of landing support for orthographic projection, which sounds similar to what you're doing (unless I'm misunderstanding). Would that solve what you're trying to do?
     
  3. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    106
    Thanks for the prompt reply!
    Not sure if I can call it similar to orthographic projection.

    I attached a screenshot of a top-down camera perspective camera:
    * Right side is the scene view with planes standing up in Y direction
    * Left side(game camera) is projecting everything in Y direction to to Z (giving the impression of 2D game, but having the benefits of shadows, depth/height etc.)

    The script does this:
    Code (CSharp):
    1.  
    2.         private void OnEnable()
    3.         {
    4.             RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
    5.             RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
    6.         }
    7.  
    8.         private void OnDisable()
    9.         {
    10.             RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
    11.             RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
    12.             _camera.ResetWorldToCameraMatrix();
    13.         }
    14.  
    15.         private void OnBeginCameraRendering(ScriptableRenderContext scriptableRenderContext, UnityEngine.Camera camera)
    16.         {
    17.             if (camera == _camera && camera.cameraType == CameraType.Game)
    18.                 SetMatrix(camera);
    19.         }
    20.  
    21.         private void OnEndCameraRendering(ScriptableRenderContext scriptableRenderContext, UnityEngine.Camera camera)
    22.         {
    23.             if (camera == _camera && camera.cameraType == CameraType.Game && _resetMatrixOnEnd)
    24.                 camera.ResetWorldToCameraMatrix();
    25.         }
    26.  
    27.         private void SetMatrix(UnityEngine.Camera camera)
    28.         {
    29.             var matrix = camera.transform.worldToLocalMatrix;
    30.  
    31.             // since Unity uses OpenGL's view matrix conventions we have to flip the output z-value.
    32.             matrix.SetRow(2, -matrix.GetRow(2));
    33.  
    34.             var x = _matrixXOffset;
    35.             var y = _matrixYOffset;
    36.             var z = _matrixZOffset;
    37.             var w = _matrixWOffset;
    38.  
    39.             matrix.SetColumn(0, matrix.GetColumn(0) - x);
    40.             matrix.SetColumn(1, matrix.GetColumn(1) - y);
    41.             matrix.SetColumn(2, matrix.GetColumn(2) - z);
    42.             matrix.SetColumn(3, matrix.GetColumn(3) - w);
    43.             camera.worldToCameraMatrix = matrix;
    44.         }

    I would be happy with Deferred, but it doesn't seem like you can do custom light with it yet.
     

    Attached Files:

    Last edited: Feb 16, 2023
  4. MattDavis

    MattDavis

    Joined:
    Aug 11, 2013
    Posts:
    16
    Just want to chime in and say that we are also hoping to see this implemented, if possible.
    Unfortunately I went through the entire process of converting our custom lighting to Forward+ before discovering it is incompatible with custom camera matrix.