Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. //========= Copyright 2014, Valve Corporation, All rights reserved. ===========
  2. //
  3. // Purpose: For controlling in-game objects with tracked devices.
  4. //
  5. //=============================================================================
  6.  
  7. using UnityEngine;
  8. using Valve.VR;
  9.  
  10. public class SteamVR_TrackedObject : MonoBehaviour
  11. {
  12. public enum EIndex
  13. {
  14. None = -1,
  15. Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,
  16. Device1,
  17. Device2,
  18. Device3,
  19. Device4,
  20. Device5,
  21. Device6,
  22. Device7,
  23. Device8,
  24. Device9,
  25. Device10,
  26. Device11,
  27. Device12,
  28. Device13,
  29. Device14,
  30. Device15
  31. }
  32.  
  33. public EIndex index;
  34. public Transform origin; // if not set, relative to parent
  35. public bool isValid = false;
  36.  
  37. private void OnNewPoses(params object[] args)
  38. {
  39. if (index == EIndex.None)
  40. return;
  41.  
  42. var i = (int)index;
  43.  
  44. isValid = false;
  45. var poses = (Valve.VR.TrackedDevicePose_t[])args[0];
  46. if (poses.Length <= i)
  47. return;
  48.  
  49. if (!poses[i].bDeviceIsConnected)
  50. return;
  51.  
  52. if (!poses[i].bPoseIsValid)
  53. return;
  54.  
  55. isValid = true;
  56.  
  57. var pose = new SteamVR_Utils.RigidTransform(poses[i].mDeviceToAbsoluteTracking);
  58.  
  59. if (origin != null)
  60. {
  61. pose = new SteamVR_Utils.RigidTransform(origin) * pose;
  62. pose.pos.x *= origin.localScale.x;
  63. pose.pos.y *= origin.localScale.y;
  64. pose.pos.z *= origin.localScale.z;
  65. transform.position = pose.pos;
  66. transform.rotation = pose.rot;
  67. }
  68. else
  69. {
  70. transform.localPosition = pose.pos;
  71. transform.localRotation = pose.rot;
  72. }
  73. }
  74.  
  75. void OnEnable()
  76. {
  77. var render = SteamVR_Render.instance;
  78. if (render == null)
  79. {
  80. enabled = false;
  81. return;
  82. }
  83.  
  84. SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
  85. }
  86.  
  87. void OnDisable()
  88. {
  89. SteamVR_Utils.Event.Remove("new_poses", OnNewPoses);
  90. }
  91.  
  92. public void SetDeviceIndex(int index)
  93. {
  94. if (System.Enum.IsDefined(typeof(EIndex), index))
  95. this.index = (EIndex)index;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement