2
0

IpdInfo.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using Valve.VR;
  3. namespace SicknessReduction.Visual
  4. {
  5. public class IpdInfo : MonoBehaviour
  6. {
  7. public delegate void OnIpdChangedEvent(float ipdMeters);
  8. private SteamVR_Events.Action ipdChangedAction;
  9. private void OnEnable()
  10. {
  11. SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected);
  12. ipdChangedAction = SteamVR_Events.SystemAction(EVREventType.VREvent_IpdChanged, OnIpdChanged);
  13. ipdChangedAction.enabled = true;
  14. }
  15. private void OnDisable()
  16. {
  17. ipdChangedAction.enabled = false;
  18. }
  19. public event OnIpdChangedEvent onIpdChanged; // data event
  20. private void OnIpdChanged(VREvent_t vrEvent)
  21. {
  22. var ipdMeters = vrEvent.data.ipd.ipdMeters;
  23. onIpdChanged?.Invoke(ipdMeters);
  24. Debug.Log($"IPD changed to {ipdMeters}");
  25. }
  26. private void OnDeviceConnected(int index, bool connected)
  27. {
  28. if (index < 0) return;
  29. if (!connected || OpenVR.System == null) return;
  30. var deviceClass = OpenVR.System.GetTrackedDeviceClass((uint) index);
  31. if (deviceClass != ETrackedDeviceClass.HMD) return;
  32. var error = ETrackedPropertyError.TrackedProp_Success;
  33. var ipdMeters = OpenVR.System.GetFloatTrackedDeviceProperty((uint) index,
  34. ETrackedDeviceProperty.Prop_UserIpdMeters_Float, ref error);
  35. if (error != ETrackedPropertyError.TrackedProp_Success)
  36. {
  37. Debug.LogError($"Could not get ipd for HMD at index {index}");
  38. }
  39. else
  40. {
  41. onIpdChanged?.Invoke(ipdMeters);
  42. Debug.Log($"Initial IPD = {ipdMeters}");
  43. }
  44. Debug.Log("Controller got connected at index:" + index);
  45. }
  46. }
  47. }