示例#1
0
        private bool TryGetDeviceIndexFromOrigin(CVRInput vrInput, ulong origin, out OriginData originData, out EVRInputError error)
        {
            if (!m_originDataCache.TryGetValue(origin, out originData))
            {
                var originInfo = default(InputOriginInfo_t);
                error = vrInput.GetOriginTrackedDeviceInfo(origin, ref originInfo, m_originInfoSize);
                if (error != EVRInputError.None)
                {
                    originData = new OriginData()
                    {
                        devicePath  = OpenVR.k_ulInvalidInputValueHandle,
                        deviceIndex = OpenVR.k_unTrackedDeviceIndexInvalid,
                    };
                    return(false);
                }
                else
                {
                    originData = new OriginData()
                    {
                        devicePath  = originInfo.devicePath,
                        deviceIndex = originInfo.trackedDeviceIndex,
                    };

                    s_devicePathHandles[originInfo.trackedDeviceIndex] = originInfo.devicePath;
                    //Debug.Log("Set device path " + originInfo.trackedDeviceIndex + " to " + originInfo.devicePath);
                    m_originDataCache.Add(origin, originData);
                    return(true);
                }
            }
            else
            {
                error = EVRInputError.None;
                return(true);
            }
        }
示例#2
0
 public void InitiateHandles(CVRInput vrInput)
 {
     for (int i = 0; i < Len; ++i)
     {
         m_handles[i] = SafeGetActionHandle(vrInput, m_paths[i]);
     }
 }
示例#3
0
        private ActionCollection <T> .Enumerable AllActionDevicePath <T>(CVRInput vrInput, ActionCollection <T> actionCollection)
#if CSHARP_7_OR_LATER
            where T : Enum
#endif
        {
            return(new ActionCollection <T> .Enumerable(AllActionDevicePathEnumerator(vrInput, actionCollection)));
        }
示例#4
0
 public void ResolveHandles(CVRInput vrInput)
 {
     foreach (var key in EnumArrayBase <T> .StaticEnums)
     {
         actionHandles[key] = SafeGetActionHandle(vrInput, actionPaths[key]);
     }
 }
示例#5
0
            public EVRInputError TryGetOriginData(CVRInput vrInput, ulong originHandle, out OriginData originData)
            {
                if (cache.TryGetValue(originHandle, out originData))
                {
                    return(EVRInputError.None);
                }

                var info  = default(InputOriginInfo_t);
                var error = vrInput.GetOriginTrackedDeviceInfo(originHandle, ref info, originInfoSize);

                if (error == EVRInputError.None)
                {
                    cache.Add(originHandle, originData = new OriginData()
                    {
                        devicePathHandle = info.devicePath,
                        deviceIndex      = info.trackedDeviceIndex,
                    });
                    index2PathHandle[originData.deviceIndex] = originData.devicePathHandle;
                }
                else
                {
                    originData = default(OriginData);
                }

                return(error);
            }
示例#6
0
        private EVRInputError TryGetDigitalValue(CVRInput vrInput, ulong actionHandle, ulong devicePathHandle, out bool value)
        {
            var data  = default(InputDigitalActionData_t);
            var error = vrInput.GetDigitalActionData(actionHandle, ref data, m_digitalDataSize, devicePathHandle);

            value = data.bState;
            return(error);
        }
示例#7
0
        public void Init()
        {
            Console.CancelKeyPress += CancelKeyPressed;

            var initError = EVRInitError.None;

            vrSystem = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Background);
            if (initError != EVRInitError.None)
            {
                var message = OpenVR.GetStringForHmdError(initError);
                throw new Exception($"Failed to initialize OpenVR: {message}");
            }

            vrInput    = OpenVR.Input;
            vrSettings = OpenVR.Settings;

            var appDir       = AppDomain.CurrentDomain.BaseDirectory;
            var manifestPath = Path.Combine(appDir, "action_manifest.json");

            var inputError = vrInput.SetActionManifestPath(manifestPath);

            if (inputError != EVRInputError.None)
            {
                var message = inputError.ToString();
                throw new Exception($"Failed to set action manifest path: {message}");
            }

            inputError = vrInput.GetActionSetHandle("/actions/main", ref inputSet);
            if (inputError != EVRInputError.None)
            {
                var message = inputError.ToString();
                throw new Exception($"Failed to action set handle: {message}");
            }

            inputError = vrInput.GetActionHandle("/actions/main/in/activate", ref inputActivate);
            if (inputError != EVRInputError.None)
            {
                var message = inputError.ToString();
                throw new Exception($"Failed to get action handle for Activate: {message}");
            }

            inputError = vrInput.GetActionHandle("/actions/main/in/reset-auto", ref inputResetAuto);
            if (inputError != EVRInputError.None)
            {
                var message = inputError.ToString();
                throw new Exception($"Failed to get action handle for Reset (Auto): {message}");
            }

            inputError = vrInput.GetActionHandle("/actions/main/in/reset", ref inputResetHold);
            if (inputError != EVRInputError.None)
            {
                var message = inputError.ToString();
                throw new Exception($"Failed to get action handle for Reset (Hold): {message}");
            }

            activateSound = new SoundPlayer(Path.Combine(appDir, "activate.wav"));
            resetSound    = new SoundPlayer(Path.Combine(appDir, "reset.wav"));
        }
示例#8
0
        private EVRInputError TryGetAnalogValue(CVRInput vrInput, ulong actionHandle, ulong devicePathHandle, out Vector3 value)
        {
            var data  = default(InputAnalogActionData_t);
            var error = vrInput.GetAnalogActionData(actionHandle, ref data, m_analogDataSize, devicePathHandle);

            value.x = data.x;
            value.y = data.y;
            value.z = data.z;
            return(error);
        }
示例#9
0
            public void ResetOrigins(CVRInput vrInput)
            {
                if (CurrentHandle == OpenVR.k_ulInvalidActionHandle)
                {
                    m_originIterator = -1;
                    return;
                }

                m_originIterator = 0;
                var error = vrInput.GetActionOrigins(s_actionSetHandle, CurrentHandle, s_actionOrigins);

                if (error != EVRInputError.None)
                {
                    Debug.LogError("GetActionOrigins failed! action=" + CurrentPath + " error=" + error);
                }
            }
示例#10
0
            private bool TryGetCurrentOriginDataAndDeviceState(CVRInput vrInput, out IVRModuleDeviceState prevState, out IVRModuleDeviceStateRW currState, out ulong originDevicePath)
            {
                OriginData    originData;
                EVRInputError error;

                if (!s_moduleInstance.TryGetDeviceIndexFromOrigin(vrInput, CurrentOrigin, out originData, out error))
                {
                    Debug.LogError("GetOriginTrackedDeviceInfo failed! error=" + error + " action=" + pressActions.CurrentPath);
                    prevState        = null;
                    currState        = null;
                    originDevicePath = 0ul;
                    return(false);
                }

                originDevicePath = originData.devicePath;
                return(s_moduleInstance.TryGetValidDeviceState(originData.deviceIndex, out prevState, out currState) && currState.isConnected);
            }
示例#11
0
            public bool TryGetCurrentAnalogData(CVRInput vrInput, out IVRModuleDeviceState prevState, out IVRModuleDeviceStateRW currState, ref InputAnalogActionData_t data)
            {
                ulong originDevicePath;

                if (!TryGetCurrentOriginDataAndDeviceState(vrInput, out prevState, out currState, out originDevicePath))
                {
                    return(false);
                }

                var error = vrInput.GetAnalogActionData(CurrentHandle, ref data, s_moduleInstance.m_analogDataSize, originDevicePath);

                if (error != EVRInputError.None)
                {
                    Debug.LogError("GetAnalogActionData failed! action=" + CurrentPath + " error=" + error);
                    return(false);
                }

                return(true);
            }
示例#12
0
        private static ulong SafeGetActionHandle(CVRInput vrInput, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0ul);
            }

            var handle = OpenVR.k_ulInvalidActionHandle;
            var error  = vrInput.GetActionHandle(path, ref handle);

            if (error != EVRInputError.None)
            {
                Debug.LogError("Load " + path + " action failed! error=" + error);
                return(OpenVR.k_ulInvalidActionHandle);
            }
            else
            {
                return(handle);
            }
        }
示例#13
0
        public void Deinit()
        {
            if (vrSettings != null)
            {
                TriggerReset();
            }

            if (vrSystem != null)
            {
                OpenVR.Shutdown();
                vrInput    = null;
                vrSettings = null;
                vrSystem   = null;

                inputActivate  = 0;
                inputResetAuto = 0;
                inputResetHold = 0;
                inputSet       = 0;
            }
        }
示例#14
0
        public override void Init()
        {
            base.Init();

            // make sure OpenVR is init right away
            EVRInitError e = EVRInitError.None;

            system = OpenVR.System;
            if (system == null)
            {
                system = OpenVR.Init(ref e);
            }
            Debug.Log("OpenVR version: " + system.GetRuntimeVersion());

            // init input system
            input = OpenVR.Input;
            string actionsPath;

            if (XRInput.singleton.steamSDK_InUse)
            {
                actionsPath = Path.Combine(Application.dataPath, "StreamingAssets", "SteamVR", "actions.json");
            }
            else
            {
                actionsPath = Path.Combine(Application.dataPath, "StreamingAssets", "OpenVR", "vrstudios_actions.json");
            }
            actionsPath = actionsPath.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
            Debug.Log($"Loading OpenVR Input actions: '{actionsPath}'");
            var error = input.SetActionManifestPath(actionsPath);

            if (error != EVRInputError.None)
            {
                Debug.LogError("Failed: 'SetActionManifestPath': " + error.ToString());
                return;
            }

            // get hands
            GetInputSourceHandle("/user/hand/right", ref viveSource_RightHand);
            GetInputSourceHandle("/user/hand/left", ref viveSource_LeftHand);

            // get action set
            actionSets = new VRActiveActionSet_t[1];
            if (!GetActionSetHandle("/actions/vrstudios", ref viveActionSetHandle))
            {
                return;
            }
            actionSets[0].ulActionSet = viveActionSetHandle;

            // get object actions (touch)
            GetActionHandle("/actions/vrstudios/in/bumpertouch", ref viveAction_BumperTouch);
            GetActionHandle("/actions/vrstudios/in/triggertouch", ref viveAction_TriggerTouch);
            GetActionHandle("/actions/vrstudios/in/griptouch", ref viveAction_GripTouch);
            GetActionHandle("/actions/vrstudios/in/menutouch", ref viveAction_MenuTouch);
            GetActionHandle("/actions/vrstudios/in/touch1", ref viveAction_Touch1);
            GetActionHandle("/actions/vrstudios/in/touch2", ref viveAction_Touch2);
            GetActionHandle("/actions/vrstudios/in/joystick1_touch", ref viveAction_Joystick1_Touch);

            // get object actions (buttons)
            GetActionHandle("/actions/vrstudios/in/bumperbutton", ref viveAction_BumperButton);
            GetActionHandle("/actions/vrstudios/in/triggerbutton", ref viveAction_TriggerButton);
            GetActionHandle("/actions/vrstudios/in/gripbutton", ref viveAction_GripButton);
            GetActionHandle("/actions/vrstudios/in/menubutton", ref viveAction_MenuButton);
            GetActionHandle("/actions/vrstudios/in/button1", ref viveAction_Button1);
            GetActionHandle("/actions/vrstudios/in/button2", ref viveAction_Button2);
            GetActionHandle("/actions/vrstudios/in/touchpad1_button", ref viveAction_Touchpad1_Button);
            GetActionHandle("/actions/vrstudios/in/joystick1_button", ref viveAction_Joystick1_Button);

            // get object actions (grips)
            GetActionHandle("/actions/vrstudios/in/grip", ref viveAction_Grip);

            // get object actions (triggers)
            GetActionHandle("/actions/vrstudios/in/trigger", ref viveAction_Trigger);

            // get object actions (touchpads)
            GetActionHandle("/actions/vrstudios/in/touchpad1", ref viveAction_Touchpad1);

            // get object actions (joysticks)
            GetActionHandle("/actions/vrstudios/in/joystick1", ref viveAction_Joystick1);

            // rumble
            GetActionHandle("/actions/vrstudios/out/haptic_right", ref viveAction_Rumble_RightHand);
            GetActionHandle("/actions/vrstudios/out/haptic_left", ref viveAction_Rumble_LeftHand);

            // finish
            isInit = true;
        }
示例#15
0
        private IEnumerator <ActionCollection <T> .EnumData> AllActionDevicePathEnumerator <T>(CVRInput vrInput, ActionCollection <T> actionCollection)
#if CSHARP_7_OR_LATER
            where T : Enum
#endif
        {
            IVRModuleDeviceState   prevState;
            IVRModuleDeviceStateRW currState;
            EVRInputError          error;
            var current = default(ActionCollection <T> .EnumData);

            foreach (var p in actionCollection.ActionHandles.EnumValues)
            {
                current.inputKey     = p.Key;
                current.actionHandle = p.Value;
                if (current.actionHandle == OpenVR.k_ulInvalidActionHandle)
                {
                    continue;
                }

                Array.Clear(outActionOrigins, 0, outActionOrigins.Length);
                error = vrInput.GetActionOrigins(s_actionSetHandle, current.actionHandle, outActionOrigins);

                while (error == EVRInputError.BufferTooSmall && outActionOrigins.Length <= 4096)
                {
                    Array.Resize(ref outActionOrigins, outActionOrigins.Length * 2);
                    Debug.LogWarning("Expanding outActionOrigins size to " + outActionOrigins.Length);
                    error = vrInput.GetActionOrigins(s_actionSetHandle, current.actionHandle, outActionOrigins);
                }

                if (error != EVRInputError.None)
                {
                    Debug.LogError("[SteamVRv2Module] CVRInput.GetActionOrigins fail. input=" + p.Key + " action=" + actionCollection.ActionPaths[p.Key] + " error=" + error);
                    continue;
                }

                foreach (var originHandle in outActionOrigins)
                {
                    if (originHandle == 0ul)
                    {
                        break;
                    }

                    OriginData data;
                    error = m_originDataCache.TryGetOriginData(vrInput, originHandle, out data);
                    if (error != EVRInputError.None)
                    {
                        Debug.LogError("[SteamVRv2Module] CVRInput.GetOriginTrackedDeviceInfo fail. input=" + p.Key + " action=" + actionCollection.ActionPaths[p.Key] + " originHandle=" + originHandle + " error=" + error);
                        continue;
                    }

                    current.devicePathHandle = data.devicePathHandle;
                    uint moduleIndex;
                    if (!m_indexMap.TryGetModuleIndex(data.deviceIndex, out moduleIndex))
                    {
                        continue;
                    }
                    if (!TryGetValidDeviceState(moduleIndex, out prevState, out currState))
                    {
                        continue;
                    }

                    current.currState = currState;
                    yield return(current);
                }
            }
        }
示例#16
0
 //初期化。失敗したらfalse
 public bool Init()
 {
     openvr  = OpenVR.System;
     vrinput = OpenVR.Input;
     return(IsReady());
 }