Search Unity

Setting camera ClearFlags from OnPreRender erases the previous render?

Discussion in 'General Graphics' started by CloudyVR, Feb 22, 2020.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I am trying to render twice, once to generate the skybox, and again to render the foreground objects.

    Currently I must use two cameras to properly display a falloff shader I created (with help from Bgolus) for my floor, I use one camera for rendering the skybox and another for the rest of the scene.

    But now I have another camera in my scene that instead uses a renderTexture, so i can not think of a way to use two cameras for this.

    I am having trouble with this code because even though I render twice and it would seem to be easy, instead once I change the clearFlags I lose the previous texture and my skybox goes black:

    Code (csharp):
    1. void OnPreRender()
    2. {
    3. //First render the skybox background texture:
    4.    m_camera.clearFlags = CameraClearFlags.Skybox; //only render skybox
    5.  
    6.    m_camera.cullingMask = cullingMask1; //Layers: Nothing - for skybox rendering only
    7.  
    8.    m_camera.Render(); //render the skybox background
    9.  
    10. //Now render the foreground and place on top of the last (skybox) texture:
    11.    m_camera.clearFlags = CameraClearFlags.Nothing; //This is where my skybox render gets wiped to solid black.
    12.  
    13.    m_camera.cullingMask = cullingMask2; //Layers: Default, Water, Others..
    14.  
    15.    //m_camera.Render(); //will render on it's own after OnPreRender
    16. }
    Is it possible to preserve the skybox render and render a second texture overtop of it?

    Sorry for my inexperience... Would commandbuffers be a better alternative?

    Thank you very much!!
     
    Last edited: Feb 22, 2020