Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mixing manual main camera move and Tracked pose driver ?

Discussion in 'Scripting' started by Akwakwak, Nov 29, 2020.

  1. Akwakwak

    Akwakwak

    Joined:
    Oct 29, 2020
    Posts:
    5
    Hello all,
    I use a framework that moves the main camera on input event (keyboard, mouse).
    I'm trying to add (6dof) VR capabilities to my application that use that framework.
    - I would like to keep the framework input events to move my VR cameras when it moves the main camera.
    - I need to move the main camera according to my VR cameras transform to keep the framework functionalities working.

    I spend one week on this problem without succes :(

    I made the following transform hierarchie :
    • Main Camera (_cameraMain)
      • Game Object VRHead (_head)
        • VR Camera Left (_cameraLeft)
        • VR Camera Right (_cameraRight)
    Then the following script (that does not work well) :
    Code (CSharp):
    1. void Update()
    2. {
    3.         // Get global position for each cameras
    4.         Vector3 positionCameraLeft = getPositionCamLef(); // Mycode
    5.         Vector3 positionCameraRight = getPositionCamRight(); // Mycode
    6.  
    7.         // Get global rotation for each cameras
    8.         Quaternion rotationCameraLeft = getRotationCamLeft(); // Mycode
    9.         Quaternion rotationCameraRight = getRotationCamRight(); // Mycode
    10.  
    11.  
    12.         // Set head global position and rotation
    13.         _head.transform.position = ((positionCameraLeft + positionCameraRight) / 2);
    14.         _head.transform.rotation = Quaternion.Slerp(rotationCameraLeft, rotationCameraRight, 0.5f);
    15.  
    16.         // Set VR cameras global position and rotation
    17.         _cameraLeft.transform.position = positionCameraLeft;
    18.         _cameraLeft.transform.rotation = rotationCameraLeft;
    19.         _cameraRight.transform.position = positionCameraRight;
    20.         _cameraRight.transform.rotation = rotationCameraRight;
    21.  
    22.         // Move head with delta main camera position
    23.         _head.transform.position -= _previousPositionMainCamera;
    24.         _head.transform.position += _cameraMain.transform.position;
    25.         _head.transform.rotation *= Quaternion.Inverse(_previousRotationMainCamera);
    26.         _head.transform.rotation *= _cameraMain.transform.rotation;
    27.  
    28.         // Update main camera position at the head position
    29.         _cameraMain.transform.position = _head.transform.position;
    30.         _cameraMain.transform.rotation = _head.transform.rotation;
    31.  
    32.         // Replace the head position into the main camera position
    33.         _head.transform.localPosition = Vector3.zero;
    34.         _head.transform.localRotation = Quaternion.identity;
    35.  
    36.         // Save transforms for next image
    37.         _previousPositionVRCamera = _head.transform.position;
    38.         _previousRotationVRCamera = _head.transform.rotation;
    39.         _previousPositionMainCamera = _cameraMain.transform.position;
    40.         _previousRotationMainCamera = _cameraMain.transform.rotation;
    41. }
    With that script, I cannot move the maincamera with my framework.
    And if I force a main camera rotation, When I move the head, the direction is bad (does not take the main camera rotation into account).

    I start thinking I miss something :(

    Any help could be appreciated.
     
  2. tigjohnnysilva

    tigjohnnysilva

    Joined:
    Oct 26, 2020
    Posts:
    3
    I am new to vr devlop but i do like to keep it simlple.

    Have you try to use te tracked pose Driver componnent in main camera


    xr.PNG ?
     
  3. Akwakwak

    Akwakwak

    Joined:
    Oct 29, 2020
    Posts:
    5
    Hum. Seems interesting.
    Could you explain me how to use it ?
     
  4. tigjohnnysilva

    tigjohnnysilva

    Joined:
    Oct 26, 2020
    Posts:
    3
    you have to go in package manager

    in the unity Registry scrool all down and install the 2 last xr plug in


    xr.PNG


    "XR legacy" ( stand for the component you need to add

    and

    "XR Plugin" (is for setting up the VR devices,


    Capturar.PNG
     
  5. Akwakwak

    Akwakwak

    Joined:
    Oct 29, 2020
    Posts:
    5
    Ok.
    I made a very simple test :
    I created a "TestTtrackedPoseDriver" GameObject and load the following script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.XR.Interaction;
    5.  
    6.  
    7. public class TestTransform : BasePoseProvider
    8. {
    9.     [Range(-180.0f, 180.0f)]
    10.     public float VrRotX = 0;
    11.  
    12.     [Range(-180.0f, 180.0f)]
    13.     public float VrRotY = 0;
    14.  
    15.     [Range(-180.0f, 180.0f)]
    16.     public float VrRotZ = 0;
    17.  
    18.     [Range(-10.0f, 10.0f)]
    19.     public float VrPosX = 0;
    20.  
    21.     [Range(-10.0f, 10.0f)]
    22.     public float VrPosY = 1.7f;
    23.  
    24.     [Range(-10.0f, 10.0f)]
    25.     public float VrPosZ = 0;
    26.  
    27.     public override bool TryGetPoseFromProvider(out Pose output)
    28.     {
    29.         output.position = new Vector3(VrPosX, VrPosY, VrPosZ);
    30.         output.rotation = Quaternion.Euler(VrRotX, VrRotY, VrRotZ);
    31.  
    32.         return true;
    33.     }
    34. }
    Then I added a tracked pose driver on my main camera and I use my Pose provider :

    Now I can move my main camera using my Range parameters in my TestTrackedPoseDriver object but I cannot move the maincamera directly :(
     
  6. tigjohnnysilva

    tigjohnnysilva

    Joined:
    Oct 26, 2020
    Posts:
    3
    forgot to mension a step

    1st remove the script and componnent you add

    2 Right click in main camera

    3 down to " XR "

    4 click "Convert main camera to XR RIG

    it will add XR Rig GameObject in hierarchy along with a camera offset script, the componnent Tracked Pose Rdiver right in the main camera

    Note: Tracked pose Driver support the camera and controller in the first parameter "Device"
     
  7. Akwakwak

    Akwakwak

    Joined:
    Oct 29, 2020
    Posts:
    5
    Thanks but that do not work in my case :(
     
  8. ShawnFeatherly

    ShawnFeatherly

    Joined:
    Feb 22, 2013
    Posts:
    57
    ref: https://forum.unity.com/threads/trackedposedriver-in-2019-1.634963/#post-5004626