SteamVR_TrackedObject.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4. public class SteamVR_TrackedObject : MonoBehaviour
  5. {
  6. private void OnNewPoses(params object[] args)
  7. {
  8. if (this.index == SteamVR_TrackedObject.EIndex.None)
  9. {
  10. return;
  11. }
  12. int num = (int)this.index;
  13. this.isValid = false;
  14. TrackedDevicePose_t[] array = (TrackedDevicePose_t[])args[0];
  15. if (array.Length <= num)
  16. {
  17. return;
  18. }
  19. if (!array[num].bDeviceIsConnected)
  20. {
  21. return;
  22. }
  23. if (!array[num].bPoseIsValid)
  24. {
  25. return;
  26. }
  27. this.isValid = true;
  28. SteamVR_Utils.RigidTransform b = new SteamVR_Utils.RigidTransform(array[num].mDeviceToAbsoluteTracking);
  29. if (this.origin != null)
  30. {
  31. b = new SteamVR_Utils.RigidTransform(this.origin) * b;
  32. b.pos.x = b.pos.x * this.origin.localScale.x;
  33. b.pos.y = b.pos.y * this.origin.localScale.y;
  34. b.pos.z = b.pos.z * this.origin.localScale.z;
  35. base.transform.position = b.pos;
  36. base.transform.rotation = b.rot;
  37. }
  38. else
  39. {
  40. base.transform.localPosition = b.pos;
  41. base.transform.localRotation = b.rot;
  42. }
  43. }
  44. private void OnEnable()
  45. {
  46. SteamVR_Render instance = SteamVR_Render.instance;
  47. if (instance == null)
  48. {
  49. base.enabled = false;
  50. return;
  51. }
  52. SteamVR_Utils.Event.Listen("new_poses", new SteamVR_Utils.Event.Handler(this.OnNewPoses));
  53. }
  54. private void OnDisable()
  55. {
  56. SteamVR_Utils.Event.Remove("new_poses", new SteamVR_Utils.Event.Handler(this.OnNewPoses));
  57. this.isValid = false;
  58. }
  59. public void SetDeviceIndex(int index)
  60. {
  61. if (Enum.IsDefined(typeof(SteamVR_TrackedObject.EIndex), index))
  62. {
  63. this.index = (SteamVR_TrackedObject.EIndex)index;
  64. }
  65. }
  66. public SteamVR_TrackedObject.EIndex index;
  67. public Transform origin;
  68. public bool isValid;
  69. public enum EIndex
  70. {
  71. None = -1,
  72. Hmd,
  73. Device1,
  74. Device2,
  75. Device3,
  76. Device4,
  77. Device5,
  78. Device6,
  79. Device7,
  80. Device8,
  81. Device9,
  82. Device10,
  83. Device11,
  84. Device12,
  85. Device13,
  86. Device14,
  87. Device15
  88. }
  89. }