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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Overlay camera and RenderSingleCamera

Discussion in 'Universal Render Pipeline' started by Phantom_X, Apr 12, 2022.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Hi,
    I'm having an issue using Overlay camera and the built-in function UniversalRenderPipeline.RenderSingleCamera( ).

    I have a main camera, an overlay camera and a camera created dynamically that I use to render in render texture.

    On the dynamic camera I call the method UniversalRenderPipeline.RenderSingleCamera( ) to render.

    The issue is that when I use this method with an overlay camera present, the objects rendered by the overlay camera disappear from the game view and I get some warnings.

    upload_2022-4-11_20-6-38.png

    Here a simplified version of the code that cause the problem.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using UnityEngine.Rendering.Universal;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Experimental.Rendering;
    6. using UnityEditor;
    7.  
    8. [ExecuteAlways]
    9. public class RenderCameraBug : MonoBehaviour
    10. {
    11.     public static event Action<ScriptableRenderContext, Camera> BeginDynamicEffects;
    12.  
    13.     private Camera m_Camera;
    14.     private RenderTexture m_TempRT;
    15.  
    16.     private void OnEnable()
    17.     {
    18.         RenderPipelineManager.beginCameraRendering += ComputeEffects;
    19.     }
    20.  
    21.     private void OnDisable()
    22.     {
    23.         RenderPipelineManager.beginCameraRendering -= ComputeEffects;
    24.         m_TempRT.Release();
    25.     }
    26.  
    27.     private void ComputeEffects(ScriptableRenderContext context, Camera camera)
    28.     {
    29.  
    30.         if (m_Camera == null)
    31.         {
    32.             m_Camera = CreateEffectsCamera();
    33.         }
    34.  
    35.         CreateRT();
    36.         m_Camera.targetTexture = m_TempRT;
    37.  
    38.         BeginDynamicEffects?.Invoke(context, m_Camera);
    39.  
    40.         // BUG HERE
    41.         // When calling this method, objects rendered by the overlay camera dissapear and you get some warnings.
    42.         // No issues when not using overlay camera.
    43.         UniversalRenderPipeline.RenderSingleCamera(context, m_Camera);
    44.     }
    45.  
    46.  
    47.     private Camera CreateEffectsCamera()
    48.     {
    49.         var go = new GameObject("Effects Camera", typeof(Camera));
    50.         go.hideFlags = HideFlags.DontSave;
    51.         go.transform.parent = this.transform;
    52.  
    53.         var cameraData = go.AddComponent(typeof(UniversalAdditionalCameraData)) as UniversalAdditionalCameraData;
    54.  
    55.         cameraData.renderShadows = false;
    56.         cameraData.requiresColorOption = CameraOverrideOption.Off;
    57.         cameraData.requiresDepthOption = CameraOverrideOption.Off;
    58.         cameraData.SetRenderer(0);
    59.  
    60.         var t = transform;
    61.         var position = Vector3.zero;
    62.         var rotation = Quaternion.Euler(new Vector3(90f, 0f, 0));
    63.  
    64.  
    65.         var effectsCamera = go.GetComponent<Camera>();
    66.         effectsCamera.transform.localPosition = position;
    67.         effectsCamera.transform.localRotation = rotation;
    68.         effectsCamera.cullingMask = 2;
    69.         effectsCamera.orthographic = true;
    70.         effectsCamera.orthographicSize = 500f;
    71.         effectsCamera.nearClipPlane = 0f;
    72.         effectsCamera.farClipPlane = 500f;
    73.         effectsCamera.clearFlags = CameraClearFlags.Color;
    74.         effectsCamera.backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
    75.         effectsCamera.aspect = 1;
    76.         effectsCamera.depth = -100;
    77.         effectsCamera.enabled = false;
    78.  
    79.         return effectsCamera;
    80.     }
    81.  
    82.     private void CreateRT()
    83.     {
    84.         if (m_TempRT == null)
    85.         {
    86.             m_TempRT = RenderTexture.GetTemporary(512, 512, 16, GraphicsFormatUtility.GetGraphicsFormat(RenderTextureFormat.ARGB32, false));
    87.         }
    88.     }
    89.  
    90. }
    91.  

    I have the issue in Unity 2021.2.13 and did not have the problem on 2020.3.1
    Did something change? Is there an other way to render the camera that would work with what I am trying to do?

    Thanks!
     
  2. burak_spektra

    burak_spektra

    Joined:
    Jan 30, 2023
    Posts:
    1
    Hello,

    Is there any update about this issue?

    We are having same problem.
     
  3. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    A fix in this case is to check that the camera given by beginCameraRendering is a Base Camera and with that avoid injecting anything inside the stack.:

    Code (CSharp):
    1.  
    2. var cameraData = camera.GetUniversalAdditionalCameraData();
    3. if (cameraData.renderType != CameraRenderType.Base) return;
    4.