Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OpenVR sample Input code?

Discussion in 'AR/VR (XR) Discussion' started by pan-master, Apr 19, 2018.

  1. pan-master

    pan-master

    Joined:
    Nov 11, 2013
    Posts:
    127
  2. WilbertBlom

    WilbertBlom

    Joined:
    Feb 27, 2017
    Posts:
    14
    Yes please,

    Just a few lines of example code of how to get the buttons, position, rotation of the controllers and trackers would be HIGHLY appreciated.
     
    astracat111 likes this.
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Get VTRK, otherwise this is how you set it up...You basically go into your InputManager in your Unity project:

    https://i.ibb.co/bNxTL79/tutorial-1.png

    For me I didn't have to make a difference between right and left triggers, but basically the three buttons you can use are your index trigger button, your hand trigger button and your menu button.

    Index trigger and hand trigger are both instead of buttons floats 0 to 1, so if you want to have like an if (HandTriggerPressed()) or whatever you have to set that up yourself.





    //I named it VR_LeftController_Horizontal but

    //you'll be naming it whatever you like in your Input Manager...

    if (Input.GetAxis("VR_LeftController_Horizontal") == 1f) {

    LeftHandTriggerPressed = true;

    } else {

    LeftHandTriggerPressed = false;

    }



    Ultimately, it will end up looking something like that in your Update() method. Then in whatever script you want you'd just access your class and use it:



    if (LeftHandTriggerPressed)

    MoveBox();



    Or whatever.

    These are good settings for your horizontal or vertical axis:

    https://i.ibb.co/MMBcF9y/Horizontal.png

    For the menu button you have to put in 'Positive Button' this:

    joystick button 0

    For the left hand controller I believe and then:

    joystick button 2

    For the right hand controller...

    Remember that on Windows Mixed Reality and the Oculus the joystick is used while on the Vive the trackpad will be used in it's place.

    I'll be honest with you, OpenVR is pretty great. While the input is just a tad more limited, the idea that you can just have it work across the Vive, Rift and WMR for us it's just a godsend. Also, with my Lenovo Explorer my app will load almost instantly into my headset to playtest as a native windows app, instead of needing to change it to the windows universal platform.

    In other words, everything is really instant, and it keeps the project much lighter weight than if you end up importing bulky packages and apis.
     
  4. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    The low level code to get the raw data for OpenVR is something like this:

    Code (CSharp):
    1.  
    2. void OnNewPoses(TrackedDevicePose_t[] poses) {
    3.     //Loop through each current pose
    4.     for (uint i = 0; i < poses.Length; i++) {
    5.         //Query SteamVR for controller position & rotation (aka pose)
    6.         var pose = poses[i];
    7.         var worldPose = new SteamVR_Utils.RigidTransform(pose.mDeviceToAbsoluteTracking);
    8.         //Valid tracked device at this Index?
    9.         if (pose.bDeviceIsConnected && pose.bPoseIsValid) {
    10.             var deviceClass = OpenVR.System.GetTrackedDeviceClass((uint)i);
    11.             if (deviceClass == ETrackedDeviceClass.Controller) {
    12.                 //Get Rotation and Position Data
    13.                 var pos = worldPos.pos;
    14.                 var rot = worldPos.rot.eulerAngle;
    15.                 //Get input state
    16.                 VRControllerState_t state = new VRControllerState_t();
    17.                 OpenVR.System.GetControllerState(index, ref state, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t)));
    18.                 print(GetPress(ButtonMask.Trigger))
    19.             }
    20.         }
    21.     }
    22. }
    23.  
    24.  
    25. static bool GetPress(ulong buttonMask) { return (state.ulButtonPressed & buttonMask) != 0; }
    26. static bool GetPressDown(ulong buttonMask) { return (state.ulButtonPressed & buttonMask) != 0 && (prevState.ulButtonPressed & buttonMask) == 0; }
    27. static bool GetPressUp(ulong buttonMask) { return (state.ulButtonPressed & buttonMask) == 0 && (prevState.ulButtonPressed & buttonMask) != 0; }
    28.  
    29. public static class ButtonMask
    30. {
    31.     public const ulong System = (1ul << (int)EVRButtonId.k_EButton_System); // reserved
    32.     public const ulong ApplicationMenu = (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu);
    33.     public const ulong Grip = (1ul << (int)EVRButtonId.k_EButton_Grip);
    34.     public const ulong Axis0 = (1ul << (int)EVRButtonId.k_EButton_Axis0);
    35.     public const ulong Axis1 = (1ul << (int)EVRButtonId.k_EButton_Axis1);
    36.     public const ulong Axis2 = (1ul << (int)EVRButtonId.k_EButton_Axis2);
    37.     public const ulong Axis3 = (1ul << (int)EVRButtonId.k_EButton_Axis3);
    38.     public const ulong Axis4 = (1ul << (int)EVRButtonId.k_EButton_Axis4);
    39.     public const ulong Touchpad = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Touchpad);
    40.     public const ulong Trigger = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Trigger);
    41. }
    42.    
    OnNewPoses is called automatically via an event dispatched from the OpenVR SDK, just stick it in any monobehavior.

    There are other ways to get the info, using Valve's helper components, checking the Inputs, but at a low level, this is what's happening under the hood.
     
    Last edited: Jun 24, 2019
    mew_fi likes this.