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. Dismiss Notice

Setting 'Target Eye' on Camera, at runtime (cant seem to do it)

Discussion in 'Universal Render Pipeline' started by shinywhitebox, Dec 2, 2021.

  1. shinywhitebox

    shinywhitebox

    Joined:
    Feb 22, 2017
    Posts:
    9
    Im wanting to have both VR/Non-VR in game. And for my canvas raycasters to work right in each. (2020.3.23f1, URP)

    This seems to work well enough as it is (XR Managemnet, VR in unity in general), except for one thing: Target Eye Mode + those raycasters.

    In NON VR, if TargetEye on the main camera is set to 'Both' (the default), none of the raycasters work right. The UI shows on the left eye by default, and the raycasters hit ... things, but nothing lines up with what you see.

    If you manually switch the main camera targetEye (at runtime) to None, everything works. Suddenly the fov in player is large (90 deg, since that hasn't changed) and the raycasters work as expected.

    However I cannot find a way to change targetEye from a script.
    When I do, it's ignored. This is true if I'm trying to change it from Both -> None (the "Im not in VR" scenario) and also if I change it manually to None (in editor), pop on a headset, and have the code try to change it from None -> Both (the "Im in VR, honest!" mode).

    Digging through the docs I also see a:
    XRSettings.gameViewRenderMode

    I've tried setting that as well, still no effect.

    Is there a way to dynamically change the targetEye, at runtime?
    If you do it manually, in the editor, while the game is running - it *does* work. So there's gotta be a way... right?

    Code (CSharp):
    1.         private void SetupCamerasForVRAndNonVR(bool hmdEnabled)
    2.         {
    3.             var playerCamera = _worldUICoordinator.PlayerCamera();
    4.             if (!hmdEnabled)
    5.             {
    6.                 XRSettings.gameViewRenderMode = GameViewRenderMode.None;
    7.                 playerCamera.stereoTargetEye = StereoTargetEyeMask.None;
    8.             }
    9.             else
    10.             {
    11.                 // main camera needs to be setup for both eyes.
    12.                 XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes;
    13.                 playerCamera.stereoTargetEye = StereoTargetEyeMask.Both;
    14.             }
    15.         }
     
  2. mrstruijk

    mrstruijk

    Joined:
    Jan 24, 2018
    Posts:
    52
    Did you happen to find a solution?
     
  3. shinywhitebox

    shinywhitebox

    Joined:
    Feb 22, 2017
    Posts:
    9
    Nope. Unfortunately
     
  4. Mubanga

    Mubanga

    Joined:
    Mar 3, 2014
    Posts:
    4
    I also have this issue, for now I think the best solution is to have two camera's and switch between them.
     
  5. Max_Aigner

    Max_Aigner

    Joined:
    May 9, 2017
    Posts:
    42
    That issue is still there. Software side switching does not work in URP but it works on the legacy Renderer.
    switching between two cameras works perfectly fine however. So at least that worked :)
     
  6. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    122
    So Unity created a private field that duplicates the exact purpose of a pre-existing public field, disconnected the public field from any and all functionality, gave us no ability to alter or read the actual behavior of the camera, and left the old useless field intact with no documentation explaining what they had done.

    Genius.
     
    Last edited: Nov 4, 2022
  7. djhatvr

    djhatvr

    Joined:
    Sep 22, 2019
    Posts:
    41
    That tracks...
     
  8. djhatvr

    djhatvr

    Joined:
    Sep 22, 2019
    Posts:
    41
    Good idea with two cameras, Thanks @Mubanga! I'm on URP as well...

    They probably made it a [SerializeField] private...
     
  9. tinslychong_unity

    tinslychong_unity

    Joined:
    Feb 15, 2023
    Posts:
    1
    Camera.main.GetComponent<UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>().allowXRRendering = false;
    This works.