Search Unity

When using SteamVR, How do I change which camera is displayed to the desktop application window?

Discussion in 'AR/VR (XR) Discussion' started by CloudyVR, Mar 20, 2018.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I am using SteamVR and a VRTK camera rig in my project. Currently when I build the application I see that the HMD's camera is rendered to the main window. I was wondering if there is a way to change which camera is being rendered to the main window?

    Thanks
     
  2. solidearthvr

    solidearthvr

    Joined:
    Jan 23, 2017
    Posts:
    50
    Set the "Target Eye" of the camera that you want to display on the main window to be "Main Display":
     

    Attached Files:

  3. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Just gave your suggestion a try but didn't see the main display change. Maybe this has more to do with the VRTK camera rig scripts?
     
  4. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
  5. solidearthvr

    solidearthvr

    Joined:
    Jan 23, 2017
    Posts:
    50
    I just tested this on 2017.1 with the [CameraRig] and having a separate camera set to "None (Main Display)" and the desktop monitor shows this main display camera (both when I run inside the unity editor and when I build and run a standalone executeable).

    I tested with VRTK and it also works for me.
     
  6. solidearthvr

    solidearthvr

    Joined:
    Jan 23, 2017
    Posts:
    50
    Check to make sure that the Depth setting of the main display camera is a larger value than the camera in the camerarig. I was able to repeat the behavior you're seeing when I set the main display camera depth to -1. A depth of 0 or higher shows this camera rather than the camera rig camera.

    Note that you'll probably want to disable the Audio Listener component on your standalone camera or you'll get a bunch of warnings about multiple audio listeners.
     
    CloudyVR likes this.
  7. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Hey Solidearthvr, great explanation, and it helped me find a few mistakes I was making.

    I did get things working too, and because I never simplified my scene to isolate the issue I kept thinking Unity was hitting a limitation, but after you confirmed it was possible I used a VRTK example scene and your above advice and all worked.

    I then realized that the camera in my main scene that I was trying to push to the main display was also feeding it's output to a "target texture" and that is why no matter what the Depth Buffer it never showed.

    I created a dual camera object (for now) where one camera is for the texture and the other is set as "None (Main Display)".

    Thank you for the assistance!
     
  8. solidearthvr

    solidearthvr

    Joined:
    Jan 23, 2017
    Posts:
    50
    Glad it helped!
     
  9. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Just hit a checkmate with Cinemachine using my above (dual camera method).

    As stated I require rendering both a RenderTexture and to the main screen at the same time. But as mentioned previously I had to use two cameras to accomplish that. Problem is, you may have only a single Cinemachine virtual camera.

    So I had to find a more robust solution, and it was simple.

    I now use a single camera and I have written a script that copies the camera's source RenderTexture to an external RenderTexture that can be used elsewhere in the game. This still allows the camera to be displayed in the main window and is probably hella less expensive than two cameras :)

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class VaptureCameraRenderTexture : MonoBehaviour
    4. {
    5.     public RenderTexture targetTexture; //your external rendertexture
    6.  
    7.     void OnRenderImage(RenderTexture src, RenderTexture dest)
    8.     {
    9.         Graphics.Blit(src, dest); //copy the texture normally
    10.         Graphics.Blit(src, targetTexture); //now copy the source to the external texture
    11.     }
    12. }
    --------------------------
    Also I should mention that when doing the above the external RenderTexture will take on the resolution and aspect-ratio of the main viewport.

    So depending on the user's monitor or resolution settings that rendertexture may look distorted.

    To help with any stretching that may occur there is a very useful script that can be used to enforce the aspect ratio of the camera: Force camera aspect ratio 16:9 in viewport
     
    Last edited: May 27, 2018
    solidearthvr likes this.