Search Unity

Resolved RenderPipelineManager.endCameraRendering and RenderTexture.active

Discussion in 'VR' started by NevinAF, Jul 22, 2021.

  1. NevinAF

    NevinAF

    Joined:
    May 12, 2021
    Posts:
    36
    [Unity 2020.3 in URP]

    I have been working on blending cameras with different culling masks. I got it working exactly how I wanted, but it stopped working without any changes to the code. I had working versions on the previous git commits, but once I reverted to the commits, it was no longer working.

    I was using RenderTexture.active and Graphics.Blit() to overlay one camera render texture over another (Note that this is set up for VR using depth layers and WAS working in Multipass and SinglePass):

    Code (CSharp):
    1. // All fields are created in script.
    2. public Camera blendLayerCamera;
    3. public Camera mainCamera;
    4. public RenderTexture leftRenderTexture;
    5. public RenderTexture rightRenderTexture;
    6. // Subscription: RenderPipelineManager.endCameraRendering += RenderPipelineManager_endCameraRendering
    7. private void RenderPipelineManager_endCameraRendering(ScriptableRenderContext context, Camera camera) {
    8.         if (camera == blendLayerCamera)
    9.         {
    10.             RenderTexture active = RenderTexture.active;
    11.             Graphics.Blit(
    12.                 source: active,
    13.                 dest: leftRenderTexture,
    14.                 sourceDepthSlice: 0,
    15.                 destDepthSlice: 0);
    16.             Graphics.Blit(
    17.                 source: active,
    18.                 dest: rightRenderTexture,
    19.                 sourceDepthSlice: 1,
    20.                 destDepthSlice: 0);
    21.         }
    22.         else if (camera == mainCamera)
    23.         {
    24.             Graphics.Blit(leftRenderTexture,
    25.                 RenderTexture.active,
    26.                 leftBlendMat, -1, 0);
    27.             Graphics.Blit(rightRenderTexture,
    28.                 RenderTexture.active,
    29.                 rightBlendMat, -1, 1);
    30.         }
    31. }
    The problem is RenderTexture.active was originally set to the camera's render, but it is now (and in the commits) RenderTexture.active is null.
    I'm not sure if something is not being tracked in the project settings, or if this setup is not reliable. Anyone with any insights would be helpful
     
  2. NevinAF

    NevinAF

    Joined:
    May 12, 2021
    Posts:
    36
    I just realized that Unity has built-in camera renderings that differ depending on if a VR headset is connected. I figured this might be a problem at first but I dismissed it because I ran builds of the previous commits without an HMD and everything ran as expected.

    For anyone who is curious, the above only works if a headset is connected. But, if the project is built when the headset is connected, then it will run properly without a headset connected. A little weird which is why it gave me some trouble.