Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Render multiple cameras into same RenderTexture, but depth/overlap is ignored

Discussion in 'General Graphics' started by kbm, Aug 7, 2020.

  1. kbm

    kbm

    Joined:
    Aug 7, 2014
    Posts:
    83
    Hi there,

    I have the following setup: I have two cameras, the one is rendering the terrain layer, clearflags to skybox,like this:

    upload_2020-8-7_17-55-8.png

    The other camera is rendering a single layer with clearflags set to "Don't clear" and a higher camera depth of 1
    The result when both cameras are rendering to the game view is this:

    upload_2020-8-7_17-56-29.png

    The "ghostly" thing is from the layer that the second camera renders but it gets correctly occluded by the grass.

    The problem occurs when I manually render both cameras to a rendertexture like this:

    Code (CSharp):
    1.  
    2.  
    3.             RenderTexture.active = myRenderTexture;
    4.  
    5.             cameraOne.targetTexture = myRenderTexture;
    6.             cameraOne.Render();
    7.            
    8.             cameraTwo.targetTexture = myRenderTexture;
    9.             cameraTwo.Render();
    10.            
    11.             myTexture2D.ReadPixels(myRect, 0, 0, true);
    12.             myTexture2D.Apply();
    13.  
    I do this to read the pixels from myRenderTexture into myTexture2D and use that for other parts of the game.
    Unfortunately, the things that get rendered by cameraTwo are just "stamped" on top of the image rendered by cameraOne, the things don't get occluded by terrain anymore.

    What am I doing wrong? Can anyone help with this?
    Cheers!
     

    Attached Files:

unityunity