Exemplo n.º 1
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            VREvent_t evt = new VREvent_t();

            while (_hmd.PollNextEvent(ref evt, (uint)Marshal.SizeOf(evt)))
            {
                ProcessEvent(ref evt);
            }

            _validPoseCount          = 0;
            _trackedControllerCount  = 0;
            _leftControllerDeviceID  = -1;
            _rightControllerDeviceID = -1;

            OpenVR.Compositor.WaitGetPoses(_trackedDevices, _gamePose);

            for (var i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; ++i)
            {
                if (_trackedDevices[i].bPoseIsValid)
                {
                    _validPoseCount++;
                    _devicePoses[i] = _trackedDevices[i].mDeviceToAbsoluteTracking.ToXNA();

                    if (_hmd.GetTrackedDeviceClass((uint)i) == ETrackedDeviceClass.Controller)
                    {
                        _trackedControllerCount++;

                        if (_hmd.GetControllerRoleForTrackedDeviceIndex((uint)i) == ETrackedControllerRole.LeftHand)
                        {
                            _leftControllerDeviceID = i;
                            _leftControllerPose     = _devicePoses[i];
                        }
                        else if (_hmd.GetControllerRoleForTrackedDeviceIndex((uint)i) == ETrackedControllerRole.RightHand)
                        {
                            _rightControllerDeviceID = i;
                            _rightControllerPose     = _devicePoses[i];
                        }
                    }
                }
            }

            if (_trackedDevices[OpenVR.k_unTrackedDeviceIndex_Hmd].bPoseIsValid)
            {
                _hmdPose = _devicePoses[OpenVR.k_unTrackedDeviceIndex_Hmd];
            }
        }
Exemplo n.º 2
0
        private void Update()
        {
            if (this.controller != null)
            {
                CVRSystem system = OpenVR.System;
                this.ETrackedControllerRole = system.GetControllerRoleForTrackedDeviceIndex(this.controller.index);
                if (!this.TrackedHandMatchesStarting())
                {
                    this.RefreshAssignedRoles();
                }
            }
            this.UpdateHandPoses();
            this.UpdateNoSteamVRFallback();
            GameObject currentAttachedObject = this.currentAttachedObject;

            if (currentAttachedObject)
            {
                currentAttachedObject.SendMessage("HandAttachedUpdate", this, SendMessageOptions.DontRequireReceiver);
            }
            if (this.hoveringInteractable)
            {
                this.hoveringInteractable.SendMessage("HandHoverUpdate", this, SendMessageOptions.DontRequireReceiver);
            }
            if (Time.timeScale == 0f)
            {
                this.UpdateHandPoses();
            }
        }
Exemplo n.º 3
0
        public override void Evaluate(int SpreadMax, CVRSystem system)
        {
            VREvent_t evt = default(VREvent_t);

            FEventsOut.SliceCount      = 0;
            FDeviceIndexOut.SliceCount = 0;

            while (system.PollNextEvent(ref evt, FEvtSize))
            {
                var evtType = (EVREventType)evt.eventType;
                FEventsOut.Add(evtType.ToString());
                ProcessEvent(evtType, evt);
            }

            //controller states
            OpenVRController.Update(FFrame++);

            FDeviceIndexOut.SliceCount = 0;
            FDeviceRoleOut.SliceCount  = 0;

            FControllerLeftOut.SliceCount      = 0;
            FControllerRightOut.SliceCount     = 0;
            FControllerLeftRightOut.SliceCount = 0;
            FDevicesOut.SliceCount             = 0;

            var indexLeft  = (int)system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
            var indexRight = (int)system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);

            if (indexLeft > 0)
            {
                var c = OpenVRController.Input(indexLeft);
                FControllerLeftOut.Add(c);
                FControllerLeftRightOut.Add(c);
            }

            if (indexRight > 0)
            {
                var c = OpenVRController.Input(indexRight);
                FControllerRightOut.Add(c);
                FControllerLeftRightOut.Add(c);
            }

            //output all in one
            for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                //if(FOpenVRSystem.GetTrackedDeviceClass((uint)i) != ETrackedDeviceClass.Controller) continue;
                var c = OpenVRController.Input(i);
                if (!c.connected || !c.valid)
                {
                    continue;
                }
                FDevicesOut.Add(c);
                FDeviceRoleOut.Add(system.GetControllerRoleForTrackedDeviceIndex((uint)i));
            }
        }
Exemplo n.º 4
0
        public static Result <int, string> RightControllerIndex(this CVRSystem hmd)
        {
            if (hmd == null)
            {
                throw new ArgumentNullException(nameof(hmd));
            }
            int?rightContIndex =
                hmd.DevicesInClass(ETrackedDeviceClass.Controller)
                .SingleOrDefault(i => hmd.GetControllerRoleForTrackedDeviceIndex((uint)i) == ETrackedControllerRole.RightHand);

            return(rightContIndex == null ? Result <int, string> .Err("No right controller") : rightContIndex.Value);
        }
Exemplo n.º 5
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            while (m_System.PollNextEvent(ref m_VREvent, (uint)Marshal.SizeOf(m_VREvent)))
            {
                ProcessEvent(ref m_VREvent);
            }

            OpenVR.Compositor.WaitGetPoses(m_TrackedDevices, m_GamePose);

            for (var i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; ++i)
            {
                if (m_TrackedDevices[i].bPoseIsValid)
                {
                    m_DevicePoses[i] = m_TrackedDevices[i].mDeviceToAbsoluteTracking.ToXNA();

                    if (m_System.GetTrackedDeviceClass((uint)i) == ETrackedDeviceClass.Controller)
                    {
                        if (m_System.GetControllerRoleForTrackedDeviceIndex((uint)i) == ETrackedControllerRole.LeftHand)
                        {
                            m_Controllers[0].Update(m_System, i, m_DevicePoses[i]);
                        }
                        else if (m_System.GetControllerRoleForTrackedDeviceIndex((uint)i) == ETrackedControllerRole.RightHand)
                        {
                            m_Controllers[1].Update(m_System, i, m_DevicePoses[i]);
                        }
                    }
                }
            }

            if (m_TrackedDevices[OpenVR.k_unTrackedDeviceIndex_Hmd].bPoseIsValid)
            {
                m_HMDPose = m_DevicePoses[OpenVR.k_unTrackedDeviceIndex_Hmd];
            }
        }
Exemplo n.º 6
0
        private static void UpdateDevices(CVRSystem system, ref uint overlayIndex)
        {
            m_Lock.EnterWriteLock();
            try
            {
                m_Devices.Clear();
            }
            finally
            {
                m_Lock.ExitWriteLock();
            }
            var sb    = new StringBuilder(256);
            var state = new VRControllerState_t();

            for (var i = 0u; i < OpenVR.k_unMaxTrackedDeviceCount; ++i)
            {
                var devClass = system.GetTrackedDeviceClass(i);
                if (devClass == ETrackedDeviceClass.Controller ||
                    devClass == ETrackedDeviceClass.GenericTracker ||
                    devClass == ETrackedDeviceClass.TrackingReference)
                {
                    var err = ETrackedPropertyError.TrackedProp_Success;
                    var batteryPercentage = system.GetFloatTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref err);
                    if (err != ETrackedPropertyError.TrackedProp_Success)
                    {
                        batteryPercentage = 1f;
                    }
                    sb.Clear();
                    system.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_TrackingSystemName_String, sb, (uint)sb.Capacity, ref err);
                    var isOculus = sb.ToString().IndexOf("oculus", StringComparison.OrdinalIgnoreCase) >= 0;
                    // Oculus : B/Y, Bit 1, Mask 2
                    // Oculus : A/X, Bit 7, Mask 128
                    // Vive : Menu, Bit 1, Mask 2,
                    // Vive : Grip, Bit 2, Mask 4
                    var role = system.GetControllerRoleForTrackedDeviceIndex(i);
                    if (role == ETrackedControllerRole.LeftHand ||
                        role == ETrackedControllerRole.RightHand)
                    {
                        if (system.GetControllerState(i, ref state, (uint)Marshal.SizeOf(state)) &&
                            (state.ulButtonPressed & (isOculus ? 2u : 4u)) != 0)
                        {
                            if (role == ETrackedControllerRole.LeftHand)
                            {
                                Array.Copy(m_L_Translation, m_Translation, 3);
                                Array.Copy(m_L_Rotation, m_Rotation, 3);
                            }
                            else
                            {
                                Array.Copy(m_R_Translation, m_Translation, 3);
                                Array.Copy(m_R_Rotation, m_Rotation, 3);
                            }
                            overlayIndex = i;
                        }
                    }
                    var type = string.Empty;
                    if (devClass == ETrackedDeviceClass.Controller)
                    {
                        if (role == ETrackedControllerRole.LeftHand)
                        {
                            type = "leftController";
                        }
                        else if (role == ETrackedControllerRole.RightHand)
                        {
                            type = "rightController";
                        }
                        else
                        {
                            type = "controller";
                        }
                    }
                    else if (devClass == ETrackedDeviceClass.GenericTracker)
                    {
                        type = "tracker";
                    }
                    else if (devClass == ETrackedDeviceClass.TrackingReference)
                    {
                        type = "base";
                    }
                    var item = new[]
                    {
                        type,
                        system.IsTrackedDeviceConnected(i)
                            ? "connected"
                            : "disconnected",
                        (batteryPercentage * 100).ToString()
                    };
                    m_Lock.EnterWriteLock();
                    try
                    {
                        m_Devices.Add(item);
                    }
                    finally
                    {
                        m_Lock.ExitWriteLock();
                    }
                }
            }
        }
        private async void ParseTrackingFrame()
        {
            try
            {
                for (uint id = 0; id < OpenVR.k_unMaxTrackedDeviceCount; id++)
                {
                    if (vr_pointer != null)
                    {
                        TrackedDevicePose_t[] trackedDevicePose = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];

                        if (!vr_pointer.IsTrackedDeviceConnected(id))
                        {
                            continue;
                        }

                        VRControllerState_t controllState = new VRControllerState_t();

                        ETrackedDeviceClass trackedDeviceClass = vr_pointer.GetTrackedDeviceClass(id);

                        switch (trackedDeviceClass)
                        {
                        case ETrackedDeviceClass.Controller:

                            vr_pointer.GetControllerStateWithPose(ETrackingUniverseOrigin.TrackingUniverseStanding, id, ref controllState, OpenVR.k_unMaxTrackedDeviceCount, ref trackedDevicePose[id]);

                            HmdVector3_t position = GetPosition(trackedDevicePose[id].mDeviceToAbsoluteTracking);    // devicePose->mDeviceToAbsoluteTracking);
                            HmdVector3_t rotation = GetRotationEuler(trackedDevicePose[id].mDeviceToAbsoluteTracking);


                            int positionControllerX = (int)position.v0;
                            int positionControllerY = (int)position.v1;
                            int positionControllerZ = (int)position.v2 * (-1);

                            int rotationControllerX = (int)rotation.v0 + 180;
                            int rotationControllerY = (int)rotation.v1;
                            int rotationControllerZ = (int)rotation.v2;

                            if (ControlViewModel.teleporOffset[0] != -1)
                            {
                                ControlViewModel.transOffSetControllerX = ControlViewModel.teleporOffset[0];
                                ControlViewModel.transOffSetControllerY = ControlViewModel.teleporOffset[1];
                                ControlViewModel.transOffSetControllerZ = ControlViewModel.teleporOffset[2];
                                ControlViewModel.rotOffSetControllerZ   = ControlViewModel.teleporOffset[3];
                            }

                            ETrackedControllerRole result = vr_pointer.GetControllerRoleForTrackedDeviceIndex(id);

                            if (result != ETrackedControllerRole.LeftHand && lastController != ETrackedControllerRole.LeftHand)
                            {
                                RaiseControllerEvent(new TrackingChangedEventArgs(false), Tracker.LeftController);
                            }

                            if (result != ETrackedControllerRole.RightHand && lastController != ETrackedControllerRole.RightHand)
                            {
                                RaiseControllerEvent(new TrackingChangedEventArgs(false), Tracker.RightController);
                            }

                            switch (result)
                            {
                            case ETrackedControllerRole.Invalid:
                                break;

                            case ETrackedControllerRole.LeftHand:

                                if (communicationProtocolUdp == false)
                                {
                                    MessageBuffer mb2 = new MessageBuffer();
                                    someThingTracked = true;
                                    int type = 101;
                                    mb2.add(type);
                                    mb2.add(userId);
                                    int handid = 1;
                                    mb2.add(handid);
                                    mb2.add(positionControllerX + ControlViewModel.transOffSetControllerX);
                                    mb2.add(positionControllerY + ControlViewModel.transOffSetControllerY);
                                    mb2.add(positionControllerZ + ControlViewModel.transOffSetControllerZ);
                                    mb2.add(rotationControllerX);
                                    mb2.add(rotationControllerY);
                                    mb2.add(rotationControllerZ);
                                    mb2.add(ControlViewModel.rotOffSetControllerZ);
                                    RaiseControllerEvent(new TrackingChangedEventArgs(true), Tracker.LeftController);
                                    Message msg = new Message(mb2, Message.MessagesType.COVISE_MESSAGE_VRB_MESSAGE);
                                    lock (tcpSocketManager.send_msg(msg))
                                    {
                                        tcpSocketManager.send_msg(msg);
                                    }
                                    Thread.Sleep(20);
                                    break;
                                }
                                break;

                            case ETrackedControllerRole.RightHand:

                                if (communicationProtocolUdp == false)
                                {
                                    MessageBuffer mb2 = new MessageBuffer();
                                    someThingTracked = true;
                                    int type = 101;
                                    mb2.add(type);
                                    mb2.add(userId);
                                    int handid = 2;
                                    mb2.add(handid);
                                    mb2.add(positionControllerX + ControlViewModel.transOffSetControllerX);
                                    mb2.add(positionControllerY + ControlViewModel.transOffSetControllerY);
                                    mb2.add(positionControllerZ + ControlViewModel.transOffSetControllerZ);
                                    mb2.add(rotationControllerX);
                                    mb2.add(rotationControllerY);
                                    mb2.add(rotationControllerZ);
                                    mb2.add(ControlViewModel.rotOffSetControllerZ);
                                    Message msg = new Message(mb2, Message.MessagesType.COVISE_MESSAGE_VRB_MESSAGE);
                                    RaiseControllerEvent(new TrackingChangedEventArgs(true), Tracker.RightController);
                                    lock (tcpSocketManager.send_msg(msg))
                                    {
                                        tcpSocketManager.send_msg(msg);
                                    }
                                    Thread.Sleep(20);
                                    break;
                                }
                                break;
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                log.Warn("ParseTrackingFrame in Controller Tracking wurde beendet!" + e);
                RaiseControllerEvent(new TrackingChangedEventArgs(false), Tracker.LeftController);
                RaiseControllerEvent(new TrackingChangedEventArgs(false), Tracker.RightController);
            }
        }
Exemplo n.º 8
0
        private bool StartupOVR()
        {
            EVRInitError error = EVRInitError.Driver_Failed;

            vr = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay);

            if (error != EVRInitError.None)
            {
                Debug.WriteLine("OpenVR Init Error: " + error.ToString());
                return(false);
            }

            overlay = OpenVR.Overlay;

            EVROverlayError overlayError = overlay.CreateOverlay("Jstf_ovr_cards", "OpenVR Cards System Overlay", ref overlayHandle);

            if (overlayError != EVROverlayError.None)
            {
                OpenVR.Shutdown();
                Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
                return(false);
            }

            /*overlayError = overlay.SetOverlayFromFile(overlayHandle, AssetsPath + "overlay.png");
             * if (overlayError != EVROverlayError.None)
             * {
             *      CleanupOVR();
             *      Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
             *      return false;
             * }*/

            overlayError = overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.Mouse);
            if (overlayError != EVROverlayError.None)
            {
                CleanupOVR();
                Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
                return(false);
            }

            overlayTexture = new Texture_t
            {
                eType       = ETextureType.OpenGL,
                eColorSpace = EColorSpace.Auto,
                handle      = (IntPtr)textureId
            };

            overlayError = overlay.SetOverlayTexture(overlayHandle, ref overlayTexture);
            if (overlayError != EVROverlayError.None)
            {
                CleanupOVR();
                Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString());
                return(false);
            }

            HmdId = OpenVR.k_unTrackedDeviceIndex_Hmd;

            for (uint i = HmdId + 1; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
            {
                if (vr.IsTrackedDeviceConnected(i))
                {
                    ETrackedDeviceClass cls = vr.GetTrackedDeviceClass(i);
                    if (cls == ETrackedDeviceClass.Controller)
                    {
                        ETrackedControllerRole rl = vr.GetControllerRoleForTrackedDeviceIndex(i);
                        if (rl == ETrackedControllerRole.LeftHand)
                        {
                            LeftControllerId = i;
                            Debug.WriteLine("Found Left Controller");
                        }
                        else if (rl == ETrackedControllerRole.RightHand)
                        {
                            RightControllerId = i;
                            Debug.WriteLine("Found Right Controller");
                        }
                    }
                }
            }

            mat = new HmdMatrix34_t
            {
                m0  = 1,
                m1  = 0,
                m2  = 0,
                m3  = 0f,
                m4  = 0,
                m5  = 1,
                m6  = 0,
                m7  = 0.1f,
                m8  = 0,
                m9  = 0,
                m10 = 1,
                m11 = 0f
            };



            overlayError = overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, RightControllerId, ref mat);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot bind overlay to Tracked device.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }
            overlayError = overlay.SetOverlayWidthInMeters(overlayHandle, 0.2f);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot set overlay size.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }

            overlayError = overlay.SetOverlayAlpha(overlayHandle, 1);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot set overlay alpha.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }

            overlayError = overlay.SetOverlayColor(overlayHandle, 1, 1, 1);
            if (overlayError != EVROverlayError.None)
            {
                Debug.WriteLine("Cannot set overlay color.");
                Debug.WriteLine("Error: " + overlayError.ToString());
                CleanupOVR();
                return(false);
            }
#if DEBUG
            Debug.WriteLine("OpenVR Startup Complete");
#endif
            return(true);
        }
Exemplo n.º 9
0
        public unsafe override bool GatherInput(XRControllerState[] state_controllers, out int controllerCount, out bool leftSet, out int leftSetIndex, out bool rightSet, out int rightSetIndex, out SideToSet sideToSet)
        {
            // defaults
            GatherInputDefaults(out controllerCount, out leftSet, out leftSetIndex, out rightSet, out rightSetIndex, out sideToSet);

            // reset controllers
            ResetControllers(state_controllers);

            // validate OpenVR is avaliable
            if (!isInit)
            {
                return(false);
            }
            if (system == null || !system.IsInputAvailable() || input.IsUsingLegacyInput())
            {
                return(false);
            }

            // get controller connection status and side
            for (uint i = 0; i != OpenVR.k_unMaxTrackedDeviceCount; ++i)
            {
                if (!system.IsTrackedDeviceConnected(i))
                {
                    continue;
                }
                if (system.GetTrackedDeviceClass(i) != ETrackedDeviceClass.Controller)
                {
                    continue;
                }


                // get controller type
                ETrackedPropertyError e = ETrackedPropertyError.TrackedProp_Success;
                system.GetStringTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_ControllerType_String, OpenVR_Shared.propertyText, (uint)OpenVR_Shared.propertyText.Capacity, ref e);
                if (e != ETrackedPropertyError.TrackedProp_Success)
                {
                    continue;
                }

                // ignore gamepads
                if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_Gamepad))
                {
                    continue;
                }

                // get controller
                var controller = state_controllers[controllerCount];
                controller.connected = true;

                // get controller type
                if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_ViveController))
                {
                    controller.type = XRInputControllerType.HTCVive;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_ViveCosmosController))
                {
                    controller.type = XRInputControllerType.HTCViveCosmos;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_IndexController))
                {
                    controller.type = XRInputControllerType.ValveIndex;
                }
                else if (OpenVR_Shared.propertyText.ToString().StartsWith(OpenVR_Shared.propertyText_Oculus.ToString()))
                {
                    controller.type = XRInputControllerType.Oculus;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_WMR))
                {
                    controller.type = XRInputControllerType.WMR;
                }
                else if (OpenVR_Shared.propertyText.Equals(OpenVR_Shared.propertyText_WMR_G2))
                {
                    controller.type = XRInputControllerType.WMR_G2;
                }

                // update controller side
                var role = system.GetControllerRoleForTrackedDeviceIndex(i);
                switch (role)
                {
                case ETrackedControllerRole.LeftHand:
                    controller.side = XRControllerSide.Left;
                    leftSet         = true;
                    leftSetIndex    = controllerCount;
                    leftHand        = (int)i;
                    break;

                case ETrackedControllerRole.RightHand:
                    controller.side = XRControllerSide.Right;
                    rightSet        = true;
                    rightSetIndex   = controllerCount;
                    rightHand       = (int)i;
                    break;

                default: controller.side = XRControllerSide.Unknown; break;
                }

                state_controllers[controllerCount] = controller;
                ++controllerCount;
            }

            // pre-finish/pre-resolve unknown controller sides
            GatherInputFinish(state_controllers, controllerCount, ref leftSet, ref leftSetIndex, ref rightSet, ref rightSetIndex, ref sideToSet);

            // update inputs
            var error = input.UpdateActionState(actionSets, (uint)Marshal.SizeOf <VRActiveActionSet_t>());

            if (error != EVRInputError.None)
            {
                Debug.LogError("UpdateActionState: " + error.ToString());
            }

            // get hands
            var controllerRight = new XRControllerState();
            var controllerLeft  = new XRControllerState();

            if (rightSet)
            {
                controllerRight = state_controllers[rightSetIndex];
            }
            if (leftSet)
            {
                controllerLeft = state_controllers[leftSetIndex];
            }

            // update bumper buttons/touch
            controllerRight.buttonBumper.Update(GetButtonState(viveAction_BumperButton, viveSource_RightHand));
            controllerLeft.buttonBumper.Update(GetButtonState(viveAction_BumperButton, viveSource_LeftHand));
            controllerRight.touchBumper.Update(GetButtonState(viveAction_BumperTouch, viveSource_RightHand));
            controllerLeft.touchBumper.Update(GetButtonState(viveAction_BumperTouch, viveSource_LeftHand));

            // update trigger buttons/touch
            controllerRight.buttonTrigger.Update(GetButtonState(viveAction_TriggerButton, viveSource_RightHand));
            controllerLeft.buttonTrigger.Update(GetButtonState(viveAction_TriggerButton, viveSource_LeftHand));
            controllerRight.touchTrigger.Update(GetButtonState(viveAction_TriggerTouch, viveSource_RightHand));
            controllerLeft.touchTrigger.Update(GetButtonState(viveAction_TriggerTouch, viveSource_LeftHand));

            // update grip buttons/touch
            controllerRight.buttonGrip.Update(GetButtonState(viveAction_GripButton, viveSource_RightHand));
            controllerLeft.buttonGrip.Update(GetButtonState(viveAction_GripButton, viveSource_LeftHand));
            controllerRight.touchGrip.Update(GetButtonState(viveAction_GripTouch, viveSource_RightHand));
            controllerLeft.touchGrip.Update(GetButtonState(viveAction_GripTouch, viveSource_LeftHand));

            // update menu buttons/touch
            controllerRight.buttonMenu.Update(GetButtonState(viveAction_MenuButton, viveSource_RightHand));
            controllerLeft.buttonMenu.Update(GetButtonState(viveAction_MenuButton, viveSource_LeftHand));
            controllerRight.touchMenu.Update(GetButtonState(viveAction_MenuTouch, viveSource_RightHand));
            controllerLeft.touchMenu.Update(GetButtonState(viveAction_MenuTouch, viveSource_LeftHand));

            // update button/touch 1
            controllerRight.button1.Update(GetButtonState(viveAction_Button1, viveSource_RightHand));
            controllerLeft.button1.Update(GetButtonState(viveAction_Button1, viveSource_LeftHand));
            controllerRight.touch1.Update(GetButtonState(viveAction_Touch1, viveSource_RightHand));
            controllerLeft.touch1.Update(GetButtonState(viveAction_Touch1, viveSource_LeftHand));

            // update button/touch 2
            controllerRight.button2.Update(GetButtonState(viveAction_Button2, viveSource_RightHand));
            controllerLeft.button2.Update(GetButtonState(viveAction_Button2, viveSource_LeftHand));
            controllerRight.touch2.Update(GetButtonState(viveAction_Touch2, viveSource_RightHand));
            controllerLeft.touch2.Update(GetButtonState(viveAction_Touch2, viveSource_LeftHand));

            // update grip
            if (controllerRight.type == XRInputControllerType.HTCVive || controllerRight.type == XRInputControllerType.WMR)
            {
                controllerRight.grip.Update(controllerRight.buttonGrip.on ? 1 : 0);// simulate analog state
            }
            else
            {
                controllerRight.grip.Update(GetAnalogState(viveAction_Grip, viveSource_RightHand).x);
            }

            if (controllerLeft.type == XRInputControllerType.HTCVive || controllerLeft.type == XRInputControllerType.WMR)
            {
                controllerLeft.grip.Update(controllerLeft.buttonGrip.on ? 1 : 0);// simulate analog state
            }
            else
            {
                controllerLeft.grip.Update(GetAnalogState(viveAction_Grip, viveSource_LeftHand).x);
            }

            // update triggers
            controllerRight.trigger.Update(GetAnalogState(viveAction_Trigger, viveSource_RightHand).x);
            controllerLeft.trigger.Update(GetAnalogState(viveAction_Trigger, viveSource_LeftHand).x);

            // update trackpads / touch / button
            if (controllerRight.type == XRInputControllerType.HTCVive)
            {
                controllerRight.joystick.Update(GetAnalogState(viveAction_Touchpad1, viveSource_RightHand));
                controllerRight.buttonJoystick.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_RightHand));
            }
            else
            {
                controllerRight.joystick2.Update(GetAnalogState(viveAction_Touchpad1, viveSource_RightHand));
                controllerRight.buttonJoystick2.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_RightHand));
            }

            if (controllerLeft.type == XRInputControllerType.HTCVive)
            {
                controllerLeft.joystick.Update(GetAnalogState(viveAction_Touchpad1, viveSource_LeftHand));
                controllerLeft.buttonJoystick.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_LeftHand));
            }
            else
            {
                controllerLeft.joystick2.Update(GetAnalogState(viveAction_Touchpad1, viveSource_LeftHand));
                controllerLeft.buttonJoystick2.Update(GetButtonState(viveAction_Touchpad1_Button, viveSource_LeftHand));
            }


            // update joysticks / touch / button
            if (controllerRight.type != XRInputControllerType.HTCVive)
            {
                controllerRight.joystick.Update(GetAnalogState(viveAction_Joystick1, viveSource_RightHand));
                controllerRight.touchJoystick.Update(GetButtonState(viveAction_Joystick1_Touch, viveSource_RightHand));
                controllerRight.buttonJoystick.Update(GetButtonState(viveAction_Joystick1_Button, viveSource_RightHand));
            }

            if (controllerLeft.type != XRInputControllerType.HTCVive)
            {
                controllerLeft.joystick.Update(GetAnalogState(viveAction_Joystick1, viveSource_LeftHand));
                controllerLeft.touchJoystick.Update(GetButtonState(viveAction_Joystick1_Touch, viveSource_LeftHand));
                controllerLeft.buttonJoystick.Update(GetButtonState(viveAction_Joystick1_Button, viveSource_LeftHand));
            }

            // copy back hand updates
            if (rightSet)
            {
                state_controllers[rightSetIndex] = controllerRight;
            }
            if (leftSet)
            {
                state_controllers[leftSetIndex] = controllerLeft;
            }

            return(true);
        }
Exemplo n.º 10
0
        private void VRInit()
        {
            // init
            var error = EVRInitError.None;

            system = OpenVR.Init(ref error);
            if (error != EVRInitError.None)
            {
                throw new Exception();
            }

            OpenVR.GetGenericInterface(OpenVR.IVRSystem_Version, ref error);
            if (error != EVRInitError.None)
            {
                throw new Exception();
            }

            TrackedDevicePose_t[] m_rTrackedDevicePose = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
            TrackedDevicePose_t   TrackedDevicePose    = new TrackedDevicePose_t();
            VRControllerState_t   controllerState      = new VRControllerState_t();

            while (true)
            {
                system.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0, m_rTrackedDevicePose);

                for (uint unDevice = 0; unDevice < OpenVR.k_unMaxTrackedDeviceCount; unDevice++)
                {
                    if (!system.IsTrackedDeviceConnected(unDevice))
                    {
                        continue;
                    }

                    HmdVector3_t    position;
                    HmdQuaternion_t quaternion;

                    // Get what type of device it is and work with its data
                    ETrackedDeviceClass    trackedDeviceClass    = system.GetTrackedDeviceClass(unDevice);
                    ETrackedControllerRole trackedControllerRole = system.GetControllerRoleForTrackedDeviceIndex(unDevice);
                    ETrackingResult        eTrackingResult;

                    switch (trackedDeviceClass)
                    {
                    case ETrackedDeviceClass.HMD:

                        // get the position and rotation
                        position   = GetPosition(m_rTrackedDevicePose[unDevice].mDeviceToAbsoluteTracking);
                        quaternion = GetRotation(m_rTrackedDevicePose[unDevice].mDeviceToAbsoluteTracking);

                        // for printing some more info to the user about the state of the device/pose
                        eTrackingResult = m_rTrackedDevicePose[unDevice].eTrackingResult;

                        // print the tracking data
                        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                            HmdIndex.Text       = String.Format("{0}", unDevice);
                            HmdVectorX.Text     = String.Format("{0:F4}", position.v0);
                            HmdVectorY.Text     = String.Format("{0:F4}", position.v1);
                            HmdVectorZ.Text     = String.Format("{0:F4}", position.v2);
                            HmdQuaternionX.Text = String.Format("{0:F4}", quaternion.x);
                            HmdQuaternionY.Text = String.Format("{0:F4}", quaternion.y);
                            HmdQuaternionZ.Text = String.Format("{0:F4}", quaternion.z);
                            HmdQuaternionW.Text = String.Format("{0:F4}", quaternion.w);
                            HmdState.Text       = GetDeviceState(eTrackingResult);
                        }));
                        break;

                    case ETrackedDeviceClass.Controller:

                        // get the position and rotation
                        position   = GetPosition(m_rTrackedDevicePose[unDevice].mDeviceToAbsoluteTracking);
                        quaternion = GetRotation(m_rTrackedDevicePose[unDevice].mDeviceToAbsoluteTracking);

                        // for printing some more info to the user about the state of the device/pose
                        eTrackingResult = m_rTrackedDevicePose[unDevice].eTrackingResult;

                        // get Controllers info
                        system.GetControllerStateWithPose(ETrackingUniverseOrigin.TrackingUniverseStanding, unDevice, ref controllerState, (uint)Marshal.SizeOf(controllerState), ref TrackedDevicePose);

                        switch (trackedControllerRole)
                        {
                        case ETrackedControllerRole.LeftHand:

                            // print the tracking data
                            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                                CLIndex.Text       = String.Format("{0}", unDevice);
                                CLVectorX.Text     = String.Format("{0:F4}", position.v0);
                                CLVectorY.Text     = String.Format("{0:F4}", position.v1);
                                CLVectorZ.Text     = String.Format("{0:F4}", position.v2);
                                CLQuaternionX.Text = String.Format("{0:F4}", quaternion.x);
                                CLQuaternionY.Text = String.Format("{0:F4}", quaternion.y);
                                CLQuaternionZ.Text = String.Format("{0:F4}", quaternion.z);
                                CLQuaternionW.Text = String.Format("{0:F4}", quaternion.w);
                                CLState.Text       = GetDeviceState(eTrackingResult);
                                CLulPressed.Text   = String.Format("{0}", controllerState.ulButtonPressed);
                                CLulTouched.Text   = String.Format("{0}", controllerState.ulButtonTouched);
                                CLAxis0X.Text      = String.Format("{0:F4}", controllerState.rAxis0.x);
                                CLAxis0Y.Text      = String.Format("{0:F4}", controllerState.rAxis0.y);
                                CLAxis1X.Text      = String.Format("{0:F4}", controllerState.rAxis1.x);
                                CLAxis1Y.Text      = String.Format("{0:F4}", controllerState.rAxis1.y);
                                CLAxis2X.Text      = String.Format("{0:F4}", controllerState.rAxis2.x);
                                CLAxis2Y.Text      = String.Format("{0:F4}", controllerState.rAxis2.y);
                                CLAxis3X.Text      = String.Format("{0:F4}", controllerState.rAxis3.x);
                                CLAxis3Y.Text      = String.Format("{0:F4}", controllerState.rAxis3.y);
                                CLAxis4X.Text      = String.Format("{0:F4}", controllerState.rAxis4.x);
                                CLAxis4Y.Text      = String.Format("{0:F4}", controllerState.rAxis4.y);
                            }));
                            break;

                        case ETrackedControllerRole.RightHand:

                            // print the tracking data
                            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                                CRIndex.Text       = String.Format("{0}", unDevice);
                                CRVectorX.Text     = String.Format("{0:F4}", position.v0);
                                CRVectorY.Text     = String.Format("{0:F4}", position.v1);
                                CRVectorZ.Text     = String.Format("{0:F4}", position.v2);
                                CRQuaternionX.Text = String.Format("{0:F4}", quaternion.x);
                                CRQuaternionY.Text = String.Format("{0:F4}", quaternion.y);
                                CRQuaternionZ.Text = String.Format("{0:F4}", quaternion.z);
                                CRQuaternionW.Text = String.Format("{0:F4}", quaternion.w);
                                CRState.Text       = GetDeviceState(eTrackingResult);
                                CRulPressed.Text   = String.Format("{0}", controllerState.ulButtonPressed);
                                CRulTouched.Text   = String.Format("{0}", controllerState.ulButtonTouched);
                                CRAxis0X.Text      = String.Format("{0:F4}", controllerState.rAxis0.x);
                                CRAxis0Y.Text      = String.Format("{0:F4}", controllerState.rAxis0.y);
                                CRAxis1X.Text      = String.Format("{0:F4}", controllerState.rAxis1.x);
                                CRAxis1Y.Text      = String.Format("{0:F4}", controllerState.rAxis1.y);
                                CRAxis2X.Text      = String.Format("{0:F4}", controllerState.rAxis2.x);
                                CRAxis2Y.Text      = String.Format("{0:F4}", controllerState.rAxis2.y);
                                CRAxis3X.Text      = String.Format("{0:F4}", controllerState.rAxis3.x);
                                CRAxis3Y.Text      = String.Format("{0:F4}", controllerState.rAxis3.y);
                                CRAxis4X.Text      = String.Format("{0:F4}", controllerState.rAxis4.x);
                                CRAxis4Y.Text      = String.Format("{0:F4}", controllerState.rAxis4.y);
                            }));
                            break;
                        }


                        break;

                    case ETrackedDeviceClass.GenericTracker:

                        system.GetControllerStateWithPose(ETrackingUniverseOrigin.TrackingUniverseStanding, unDevice, ref controllerState, (uint)Marshal.SizeOf(controllerState), ref TrackedDevicePose);
                        // get the position and rotation
                        position   = GetPosition(TrackedDevicePose.mDeviceToAbsoluteTracking);
                        quaternion = GetRotation(TrackedDevicePose.mDeviceToAbsoluteTracking);

                        // for printing some more info to the user about the state of the device/pose
                        eTrackingResult = m_rTrackedDevicePose[unDevice].eTrackingResult;

                        // print the tracking data
                        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                            TrackerIndex.Text       = String.Format("{0}", unDevice);
                            TrackerVectorX.Text     = String.Format("{0:F4}", position.v0);
                            TrackerVectorY.Text     = String.Format("{0:F4}", position.v1);
                            TrackerVectorZ.Text     = String.Format("{0:F4}", position.v2);
                            TrackerQuaternionX.Text = String.Format("{0:F4}", quaternion.x);
                            TrackerQuaternionY.Text = String.Format("{0:F4}", quaternion.y);
                            TrackerQuaternionZ.Text = String.Format("{0:F4}", quaternion.z);
                            TrackerQuaternionW.Text = String.Format("{0:F4}", quaternion.w);
                            TrackerState.Text       = GetDeviceState(eTrackingResult);
                        }));
                        break;
                    }
                    Thread.Sleep(10);
                }
            }
        }