Exemple #1
0
        public InputPoseActionData_t PoseFetchEventResult()
        {
            var size = (uint)Marshal.SizeOf(typeof(InputPoseActionData_t));

            OpenVR.Input.GetPoseActionData(ActionHandle, ETrackingUniverseOrigin.TrackingUniverseStanding, 1 / Program.DataFrameRate, ref Pose, size, controller.ControllerHandle);
            if (Pose.pose.bDeviceIsConnected == true && Pose.pose.bPoseIsValid == true)
            {
                HmdVector3_t    position   = new HmdVector3_t();
                HmdQuaternion_t quaternion = new HmdQuaternion_t();
                TrackableDeviceInfo.GetPosition(Pose.pose.mDeviceToAbsoluteTracking, ref position);
                TrackableDeviceInfo.GetRotation(Pose.pose.mDeviceToAbsoluteTracking, ref quaternion);
                VREventCallback.NewPoseEvent(controller.ControllerType, (PoseControllerEvent)this, position, quaternion);
            }
            return(Pose);
        }
Exemple #2
0
        private static void Worker()
        {
            Thread.CurrentThread.IsBackground = true;
            while (true)
            {
                TrackableDeviceInfo.UpdateTrackableDevicePosition();

                // Getting events
                var vrEvents = new List <VREvent_t>();
                var vrEvent  = new VREvent_t();
                try
                {
                    while (OpenVR.System.PollNextEvent(ref vrEvent, Utils.SizeOf(vrEvent)))
                    {
                        vrEvents.Add(vrEvent);
                    }
                }
                catch (Exception e)
                {
                    Utils.PrintWarning($"Could not get events: {e.Message}");
                }

                // Printing events
                foreach (var e in vrEvents)
                {
                    var pid = e.data.process.pid;
                    if (e.eventType == (uint)EVREventType.VREvent_Input_HapticVibration)
                    {
                        ETrackedControllerRole DeviceType = OpenVR.System.GetControllerRoleForTrackedDeviceIndex(e.data.process.pid);
                        if (DeviceType != ETrackedControllerRole.LeftHand && DeviceType != ETrackedControllerRole.RightHand)
                        {
                            continue;
                        }
                        NewVibrationEvent(DeviceType, e.data.hapticVibration);
                    }
#if DEBUG
                    if ((EVREventType)vrEvent.eventType != EVREventType.VREvent_None)
                    {
                        var name    = Enum.GetName(typeof(EVREventType), e.eventType);
                        var message = $"[{pid}] {name}";
                        if (pid == 0)
                        {
                            Utils.PrintVerbose(message);
                        }
                        else if (name == null)
                        {
                            Utils.PrintVerbose(message);
                        }
                        else if (name.ToLower().Contains("fail"))
                        {
                            Utils.PrintWarning(message);
                        }
                        else if (name.ToLower().Contains("error"))
                        {
                            Utils.PrintError(message);
                        }
                        else if (name.ToLower().Contains("success"))
                        {
                            Utils.PrintInfo(message);
                        }
                        else
                        {
                            Utils.Print(message);
                        }
                    }
#endif
                }

                // #6 Update action set
                if (mActionSetArray == null)
                {
                    var actionSet = new VRActiveActionSet_t
                    {
                        ulActionSet          = mActionSetHandle,
                        ulRestrictedToDevice = OpenVR.k_ulInvalidActionSetHandle,
                        nPriority            = 0
                    };
                    mActionSetArray = new VRActiveActionSet_t[] { actionSet };
                }

                var errorUAS = OpenVR.Input.UpdateActionState(mActionSetArray, (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t)));
                if (errorUAS != EVRInputError.None)
                {
                    Utils.PrintError($"UpdateActionState Error: {Enum.GetName(typeof(EVRInputError), errorUAS)}");
                }

                // #7 Load input action data
                leftController.UpdateAllState();
                rightController.UpdateAllState();

                // Restrict rate
                Thread.Sleep((int)(1000 / DataFrameRate));
            }
        }