//device情報を取得する public bool GetPropertyFloat(uint idx, ETrackedDeviceProperty prop, out float result) { ETrackedPropertyError error = new ETrackedPropertyError(); result = openvr.GetFloatTrackedDeviceProperty(idx, prop, ref error); return(error == ETrackedPropertyError.TrackedProp_Success); }
//Copy & Past openvr_api.dll [32bit version - Can be found in Unity folder] into **FilePath/bin/Debug [together with the build *.exe] private void GetViveControllerBattery() { CVRSystem hmd = OpenVR.System; ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success; float leftBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error); float rightBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error); leftBat = leftBat * 100f; rightBat = rightBat * 100f; int leftBatInt = (int)leftBat; int rightBatInt = (int)rightBat; string leftBatString = ((int)leftBat).ToString() + " %"; string rightBatString = ((int)rightBat).ToString() + " %"; if (leftBatInt == 0) { leftBatString = "Off"; } if (rightBatInt == 0) { rightBatString = "Off"; } controllerBattery = "Controller Battery : Left - " + leftBatString + " / Right - " + rightBatString; ChangeBatteryImage(leftBatInt, rightBatInt); }
// Update is called once per frame void Update() { ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success; float leftBat = 0f; float rightBat = 0f; if (leftController.activeInHierarchy) { leftBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error); } if (rightController.activeInHierarchy) { rightBat = hmd.GetFloatTrackedDeviceProperty(hmd.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand), ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref error); } int leftBatInt = (int)(leftBat * 100f); int rightBatInt = (int)(rightBat * 100f); batText.text = "Left : " + leftBatInt + " % / Right : " + rightBatInt + " %"; }
public static float GetFloatTrackedDeviceProperty(this CVRSystem system, uint unDeviceIndex, ETrackedDeviceProperty prop) { ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success; float value = system.GetFloatTrackedDeviceProperty(unDeviceIndex, prop, ref error); if (error != ETrackedPropertyError.TrackedProp_Success) { throw OpenVRException.Make(error); } return(value); }
public float GetTrackedDevicePropertyFloat(uint index, ETrackedDeviceProperty property) { var error = new ETrackedPropertyError(); float value = _vr_system.GetFloatTrackedDeviceProperty(index, property, ref error); if (error == ETrackedPropertyError.TrackedProp_Success) { return(value); } return(-1); }
private static bool GetFloatProperty(uint device, ETrackedDeviceProperty property, ref float outputValue)//convenience wrap with logging { ETrackedPropertyError pError = 0; var val = m_vrSystem.GetFloatTrackedDeviceProperty(device, ETrackedDeviceProperty.Prop_UserIpdMeters_Float, ref pError); if (pError == ETrackedPropertyError.TrackedProp_Success) { outputValue = val; return(true); } Log.WriteLine("ERROR GetFloatTrackedDeviceProperty(" + device + "," + property + "," + outputValue + ") returned error " + pError); return(false); }
private void OnDeviceConnected(params object[] args) { int num = (int)args[0]; if (num != (int)this.index) { return; } base.GetComponent <MeshFilter>().mesh = null; bool flag = (bool)args[1]; if (flag) { CVRSystem system = OpenVR.System; if (system != null && system.GetTrackedDeviceClass((uint)num) == ETrackedDeviceClass.TrackingReference) { ETrackedPropertyError eTrackedPropertyError = ETrackedPropertyError.TrackedProp_Success; float floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewLeftDegrees_Float, ref eTrackedPropertyError); if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovLeft = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewRightDegrees_Float, ref eTrackedPropertyError); if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovRight = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewTopDegrees_Float, ref eTrackedPropertyError); if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovTop = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_FieldOfViewBottomDegrees_Float, ref eTrackedPropertyError); if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovBottom = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_TrackingRangeMinimumMeters_Float, ref eTrackedPropertyError); if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.nearZ = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)num, ETrackedDeviceProperty.Prop_TrackingRangeMaximumMeters_Float, ref eTrackedPropertyError); if (eTrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.farZ = floatTrackedDeviceProperty; } this.UpdateModel(); } } }
// Token: 0x06000D8A RID: 3466 RVA: 0x00055420 File Offset: 0x00053620 private void OnDeviceConnected(int i, bool connected) { if (i != (int)this.index) { return; } base.GetComponent <MeshFilter>().mesh = null; if (connected) { CVRSystem system = OpenVR.System; if (system != null && system.GetTrackedDeviceClass((uint)i) == ETrackedDeviceClass.TrackingReference) { ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success; float floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_FieldOfViewLeftDegrees_Float, ref etrackedPropertyError); if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovLeft = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_FieldOfViewRightDegrees_Float, ref etrackedPropertyError); if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovRight = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_FieldOfViewTopDegrees_Float, ref etrackedPropertyError); if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovTop = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_FieldOfViewBottomDegrees_Float, ref etrackedPropertyError); if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.fovBottom = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_TrackingRangeMinimumMeters_Float, ref etrackedPropertyError); if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.nearZ = floatTrackedDeviceProperty; } floatTrackedDeviceProperty = system.GetFloatTrackedDeviceProperty((uint)i, ETrackedDeviceProperty.Prop_TrackingRangeMaximumMeters_Float, ref etrackedPropertyError); if (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) { this.farZ = floatTrackedDeviceProperty; } this.UpdateModel(); } } }
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(); } } } }
public MyOpenVR() { if (true) { m_viewHMD = Matrix.Identity; m_headsetPosD = MatrixD.Identity; m_c1pos = Matrix.Identity; m_c2pos = Matrix.Identity; //IntPtr Handle = MyRender11.LoadLibrary(@"D:\KeenSWH.VR\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll"); //IntPtr Handle = LoadLibrary(@"C:\Program Files (x86)\Steam\SteamApps\common\SteamVR\bin\win64\openvr_api.dll"); IntPtr Handle = LoadLibrary(@"openvr_api.dll"); //IntPtr Handle = MyRender11.LoadLibrary(@"c:\Program Files (x86)\Steam\bin\openvr_api.dll");//err code 193 - not a valid Win32 application //Assembly.LoadFile(@"D:\KeenSWH\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll"); if (Handle == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); throw new Exception(string.Format("Failed to load library (ErrorCode: {0})", errorCode)); } EVRInitError error = EVRInitError.None; IntPtr ptr = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Scene); if (error == EVRInitError.None) { m_vrSystem = new CVRSystem(ptr); uint sizeX = 0, sizeY = 0; m_vrSystem.GetRecommendedRenderTargetSize(ref sizeX, ref sizeY); m_vrSystem.SetDisplayVisibility(true); ETrackedPropertyError pError = 0; m_ipd_2 = 0.5f * m_vrSystem.GetFloatTrackedDeviceProperty(0, ETrackedDeviceProperty.Prop_UserIpdMeters_Float, ref pError); SetIPD(m_ipd_2 * 2); IntPtr pointer = OpenVR.GetGenericInterface(OpenVR.IVRCompositor_Version, ref error); if (error == EVRInitError.None) { m_vrCompositor = new CVRCompositor(pointer); m_vrCompositor.CompositorBringToFront(); m_vrCompositor.ShowMirrorWindow(); m_openVR = this; } else { var errString = OpenVR.GetStringForHmdError(error); Log.WriteLineAndConsole(errString); Debug.Fail("No compositor interface"); throw new Exception(errString); } } else { var errString = OpenVR.GetStringForHmdError(error); Log.WriteLineAndConsole(errString); //Debug.Fail("OpenVR init failed"); throw new Exception(errString); } InitOverlays(); } }
private void InitOpenVrDevices() { if (_vrSystem == null) { return; } List <HOVR_TrackerInfo> trackerDescriptions = new List <HOVR_TrackerInfo>(); for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { var deviceClass = _vrSystem.GetTrackedDeviceClass(i); if (deviceClass != ETrackedDeviceClass.Invalid) { HOVR_TrackerInfo tmpTracker = new HOVR_TrackerInfo((int)i, deviceClass); if (deviceClass == ETrackedDeviceClass.TrackingReference) { var modeLabel = GetDevicePropertyString(i, ETrackedDeviceProperty.Prop_ModeLabel_String, 16); tmpTracker.AdditionalString = modeLabel; } var serialNumber = GetDevicePropertyString(i, ETrackedDeviceProperty.Prop_SerialNumber_String, 32); tmpTracker.SerialNumber = serialNumber; var batteryError = ETrackedPropertyError.TrackedProp_UnknownProperty; _vrSystem.GetFloatTrackedDeviceProperty(i, ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float, ref batteryError); if (batteryError == ETrackedPropertyError.TrackedProp_Success) { tmpTracker.HasBattery = true; } trackerDescriptions.Add(tmpTracker); } } for (int i = 0; i < trackerDescriptions.Count; i++) { var curInfo = trackerDescriptions[i]; var tracker = GetValidTracker(curInfo); if (tracker != null) { if (tracker.DeviceId == HOVR_Tracker.UNINIT_DEVICE_ID) { tracker.Init(curInfo); if (_debugLogs) { Debug.Log("Found OVR Device: " + curInfo.ToString(), gameObject); } } else if (_debugLogs) { Debug.LogWarning("Found the same ViveTracker for multiple different detected OVR devices for info: " + curInfo.ToString(), gameObject); } } else if (_debugLogs) { Debug.LogWarning("Could not find ViveTracker for: " + curInfo.ToString(), gameObject); } } }
private float GetFloatProperty(ETrackedDeviceProperty prop) { var error = ETrackedPropertyError.TrackedProp_Success; return(hmd.GetFloatTrackedDeviceProperty(OpenVR.k_unTrackedDeviceIndex_Hmd, prop, ref error)); }