Search Unity

Depth/Culling Problem in secondary camera created during runtime

Discussion in 'General Graphics' started by pmaloo, Aug 24, 2016.

  1. pmaloo

    pmaloo

    Joined:
    Jan 25, 2015
    Posts:
    35
    I am trying to capture two separate renders from the same position by using a secondary camera created during run time which calls Camera.Render(). However, I am getting weird depth issues:

    How it should be (as seen from the Primary Camera):
    Primary Camera.png

    As seen from Dynamic Camera (created using Camera.CopyFrom)
    Dynamic Camera.png

    Here is the code I am using to grab the screen shot from the dynamic camera:
    Code (CSharp):
    1. void Update(){
    2.         if (Input.GetKeyDown (KeyCode.Space)){
    3.             GrabScene();
    4.         }
    5.     }
    6.  
    7. public void GrabScene(){
    8.         Camera camera = GetTagCam ();
    9.         Texture2D image = new Texture2D (camera.pixelWidth, camera.pixelHeight, TextureFormat.ARGB32, false);
    10.         RenderTexture tmp = RenderTexture.GetTemporary (image.width, image.height);
    11.  
    12.  
    13.         camera.CopyFrom (mCamera);
    14.         camera.targetTexture = tmp;
    15.         camera.depth = -1;
    16.         camera.Render ();
    17.         RenderTexture.active = tmp;
    18.  
    19.         SaveFile ("DynamicCam", image);
    20.         RenderTexture.active = src;
    21.         tmp.Release ();
    22.  
    23.     }
    24.  
    25.     static public Camera GetTagCam(){
    26.        
    27.         if (!tagCam) {
    28.             GameObject cam = new GameObject ("TagCam");
    29.             var tagCamera = cam.AddComponent<Camera> ();
    30.             tagCamera.enabled = false;
    31.  
    32.             cam.hideFlags = HideFlags.DontSave;
    33.             tagCam = tagCamera;
    34.         }
    35.         return tagCam;
    36.  
    37.     }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    You're using a temporary render texture with no depth component, so objects don't do any per pixel depth sorting.

    You want to use GetTemporary(width, height, 24);
     
  3. pmaloo

    pmaloo

    Joined:
    Jan 25, 2015
    Posts:
    35
    Wow! Thanks for pointing it out. That was quite an oversight on my part.Works like a charm now.