Search Unity

Bug URP Pixel perfect overwrites orthographic size set by Cinemachine when "crop frame" is set to "none"

Discussion in 'Universal Render Pipeline' started by LelouBil, Aug 3, 2022.

  1. LelouBil

    LelouBil

    Joined:
    Jun 24, 2017
    Posts:
    5
    Hello, I have recently tried using "crop frame" to "none" on the URP PixelPerfectCamera, while also setting the orthographic size of the camera via cinemachine.

    I am facing an issue where URP is overwriting the orthographic size set by cinemachine each frame.
    This shows flickering between the two orthographic size in edit mode, and shows a size discrepancy between the vcam and the unity camera in the scene.
    upload_2022-8-3_16-54-47.png
    The Unity camera outline (in white) is not the same as the VCam outline (in red), even though the VCam is controlling the Unity camera via a CinemachineBrain script

    Even though there is no flicker in play mode (only the cinemachine orthographic size is visible), I still have problems because the actual orthographic size of the Unity Camera is wrong, so my calls to Camera.ScreenToWorldPoint return the wrong values, for example.

    I searched in URP's and Cinemachine's source code and I think the problem is in URP's Renderer2D class.

    Right now it contains this :
    Code (CSharp):
    1.  
    2. // Pixel Perfect Camera doesn't support camera stacking.
    3.             if (cameraData.renderType == CameraRenderType.Base && lastCameraInStack)
    4.             {
    5.                 cameraData.camera.TryGetComponent(out ppc);
    6.                 if (ppc != null && ppc.enabled)
    7.                 {
    8.                     if (ppc.offscreenRTSize != Vector2Int.zero)
    9.                     {
    10.                         ppcUsesOffscreenRT = true;
    11.  
    12.                         // Pixel Perfect Camera may request a different RT size than camera VP size.
    13.                         // In that case we need to modify cameraTargetDescriptor here so that all the passes would use the same size.
    14.                         cameraTargetDescriptor.width = ppc.offscreenRTSize.x;
    15.                         cameraTargetDescriptor.height = ppc.offscreenRTSize.y;
    16.                     }
    17.  
    18.                     renderingData.cameraData.camera.orthographic = true;
    19.                     renderingData.cameraData.camera.orthographicSize = ppc.orthographicSize;
    20.  
    21.                     colorTextureFilterMode = ppc.finalBlitFilterMode;
    22.                     ppcUpscaleRT = ppc.gridSnapping == PixelPerfectCamera.GridSnapping.UpscaleRenderTexture;
    23.                 }
    24.             }
    25.  
    Which sets the actual camera orthographic size to the PixelPerfectCamera orthographic size, completely ignoring the fact that cinemachine could be in control of the orthographic size of the camera.

    And here is why this issue only appears with Crop frame = none :

    Still in Renderer2D.cs, a bit below

    Code (CSharp):
    1.  
    2. if (ppc != null && ppc.enabled && (ppc.cropFrame == PixelPerfectCamera.CropFrame.Pillarbox || ppc.cropFrame == PixelPerfectCamera.CropFrame.Letterbox || ppc.cropFrame == PixelPerfectCamera.CropFrame.Windowbox || ppc.cropFrame == PixelPerfectCamera.CropFrame.StretchFill))
    3.             {
    4.                 m_PixelPerfectBackgroundPass.Setup(savedIsOrthographic, savedOrthographicSize);
    5.                 EnqueuePass(m_PixelPerfectBackgroundPass);
    6.             }
    7.  
    If the PixelPerfectCamera has a crop frame value other than "none", the PixelPerfectBackgroundPass is enqueued. This pass restores orthographic values save before the overwrite.

    This leads me to believe this is a URP bug, and Renderer2D should not overwrite the orthographicSize if cinemachine controls it.
     
  2. OverEngineer

    OverEngineer

    Joined:
    Jan 4, 2023
    Posts:
    1
    I also encountered this issue.