SteamVR_Action_Pose.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. using Valve.VR;
  6. using System.Runtime.InteropServices;
  7. using System.Collections.Generic;
  8. namespace Valve.VR
  9. {
  10. [Serializable]
  11. /// <summary>
  12. /// Pose actions represent a position, rotation, and velocities inside the tracked space.
  13. /// SteamVR keeps a log of past poses so you can retrieve old poses with GetPoseAtTimeOffset or GetVelocitiesAtTimeOffset.
  14. /// You can also pass in times in the future to these methods for SteamVR's best prediction of where the pose will be at that time.
  15. /// </summary>
  16. public class SteamVR_Action_Pose : SteamVR_Action_Pose_Base<SteamVR_Action_Pose_Source_Map<SteamVR_Action_Pose_Source>, SteamVR_Action_Pose_Source>, ISerializationCallbackReceiver
  17. {
  18. public delegate void ActiveChangeHandler(SteamVR_Action_Pose fromAction, SteamVR_Input_Sources fromSource, bool active);
  19. public delegate void ChangeHandler(SteamVR_Action_Pose fromAction, SteamVR_Input_Sources fromSource);
  20. public delegate void UpdateHandler(SteamVR_Action_Pose fromAction, SteamVR_Input_Sources fromSource);
  21. public delegate void TrackingChangeHandler(SteamVR_Action_Pose fromAction, SteamVR_Input_Sources fromSource, ETrackingResult trackingState);
  22. public delegate void ValidPoseChangeHandler(SteamVR_Action_Pose fromAction, SteamVR_Input_Sources fromSource, bool validPose);
  23. public delegate void DeviceConnectedChangeHandler(SteamVR_Action_Pose fromAction, SteamVR_Input_Sources fromSource, bool deviceConnected);
  24. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the active state (ActionSet active and binding active) changes</summary>
  25. public event ActiveChangeHandler onActiveChange
  26. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveChange -= value; } }
  27. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the active state of the binding changes</summary>
  28. public event ActiveChangeHandler onActiveBindingChange
  29. { add { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onActiveBindingChange -= value; } }
  30. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the orientation of the pose changes more than the changeTolerance</summary>
  31. public event ChangeHandler onChange
  32. { add { sourceMap[SteamVR_Input_Sources.Any].onChange += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onChange -= value; } }
  33. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the action is updated</summary>
  34. public event UpdateHandler onUpdate
  35. { add { sourceMap[SteamVR_Input_Sources.Any].onUpdate += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onUpdate -= value; } }
  36. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the state of the tracking has changed</summary>
  37. public event TrackingChangeHandler onTrackingChanged
  38. { add { sourceMap[SteamVR_Input_Sources.Any].onTrackingChanged += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onTrackingChanged -= value; } }
  39. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the validity of the pose has changed</summary>
  40. public event ValidPoseChangeHandler onValidPoseChanged
  41. { add { sourceMap[SteamVR_Input_Sources.Any].onValidPoseChanged += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onValidPoseChanged -= value; } }
  42. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Event fires when the device bound to this pose is connected or disconnected</summary>
  43. public event DeviceConnectedChangeHandler onDeviceConnectedChanged
  44. { add { sourceMap[SteamVR_Input_Sources.Any].onDeviceConnectedChanged += value; } remove { sourceMap[SteamVR_Input_Sources.Any].onDeviceConnectedChanged -= value; } }
  45. /// <summary>Fires an event when a device is connected or disconnected.</summary>
  46. /// <param name="inputSource">The device you would like to add an event to. Any if the action is not device specific.</param>
  47. /// <param name="functionToCall">The method you would like to be called when a device is connected. Should take a SteamVR_Action_Pose as a param</param>
  48. public void AddOnDeviceConnectedChanged(SteamVR_Input_Sources inputSource, DeviceConnectedChangeHandler functionToCall)
  49. {
  50. sourceMap[inputSource].onDeviceConnectedChanged += functionToCall;
  51. }
  52. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  53. /// <param name="inputSource">The device you would like to remove an event from. Any if the action is not device specific.</param>
  54. /// <param name="functionToStopCalling">The method you would like to stop calling when a device is connected. Should take a SteamVR_Action_Pose as a param</param>
  55. public void RemoveOnDeviceConnectedChanged(SteamVR_Input_Sources inputSource, DeviceConnectedChangeHandler functionToStopCalling)
  56. {
  57. sourceMap[inputSource].onDeviceConnectedChanged -= functionToStopCalling;
  58. }
  59. /// <summary>Fires an event when the tracking of the device has changed</summary>
  60. /// <param name="inputSource">The device you would like to add an event to. Any if the action is not device specific.</param>
  61. /// <param name="functionToCall">The method you would like to be called when tracking has changed. Should take a SteamVR_Action_Pose as a param</param>
  62. public void AddOnTrackingChanged(SteamVR_Input_Sources inputSource, TrackingChangeHandler functionToCall)
  63. {
  64. sourceMap[inputSource].onTrackingChanged += functionToCall;
  65. }
  66. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  67. /// <param name="inputSource">The device you would like to remove an event from. Any if the action is not device specific.</param>
  68. /// <param name="functionToStopCalling">The method you would like to stop calling when tracking has changed. Should take a SteamVR_Action_Pose as a param</param>
  69. public void RemoveOnTrackingChanged(SteamVR_Input_Sources inputSource, TrackingChangeHandler functionToStopCalling)
  70. {
  71. sourceMap[inputSource].onTrackingChanged -= functionToStopCalling;
  72. }
  73. /// <summary>Fires an event when the device now has a valid pose or no longer has a valid pose</summary>
  74. /// <param name="inputSource">The device you would like to add an event to. Any if the action is not device specific.</param>
  75. /// <param name="functionToCall">The method you would like to be called when the pose has become valid or invalid. Should take a SteamVR_Action_Pose as a param</param>
  76. public void AddOnValidPoseChanged(SteamVR_Input_Sources inputSource, ValidPoseChangeHandler functionToCall)
  77. {
  78. sourceMap[inputSource].onValidPoseChanged += functionToCall;
  79. }
  80. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  81. /// <param name="inputSource">The device you would like to remove an event from. Any if the action is not device specific.</param>
  82. /// <param name="functionToStopCalling">The method you would like to stop calling when the pose has become valid or invalid. Should take a SteamVR_Action_Pose as a param</param>
  83. public void RemoveOnValidPoseChanged(SteamVR_Input_Sources inputSource, ValidPoseChangeHandler functionToStopCalling)
  84. {
  85. sourceMap[inputSource].onValidPoseChanged -= functionToStopCalling;
  86. }
  87. /// <summary>Executes a function when this action's bound state changes</summary>
  88. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  89. public void AddOnActiveChangeListener(SteamVR_Input_Sources inputSource, ActiveChangeHandler functionToCall)
  90. {
  91. sourceMap[inputSource].onActiveChange += functionToCall;
  92. }
  93. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  94. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  95. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  96. public void RemoveOnActiveChangeListener(SteamVR_Input_Sources inputSource, ActiveChangeHandler functionToStopCalling)
  97. {
  98. sourceMap[inputSource].onActiveChange -= functionToStopCalling;
  99. }
  100. /// <summary>Executes a function when the state of this action (with the specified inputSource) changes</summary>
  101. /// <param name="functionToCall">A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value</param>
  102. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  103. public void AddOnChangeListener(SteamVR_Input_Sources inputSource, ChangeHandler functionToCall)
  104. {
  105. sourceMap[inputSource].onChange += functionToCall;
  106. }
  107. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  108. /// <param name="functionToStopCalling">The local function that you've setup to receive on change events</param>
  109. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  110. public void RemoveOnChangeListener(SteamVR_Input_Sources inputSource, ChangeHandler functionToStopCalling)
  111. {
  112. sourceMap[inputSource].onChange -= functionToStopCalling;
  113. }
  114. /// <summary>Executes a function when the state of this action (with the specified inputSource) is updated.</summary>
  115. /// <param name="functionToCall">A local function that receives the boolean action who's state has changed, the corresponding input source, and the new value</param>
  116. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  117. public void AddOnUpdateListener(SteamVR_Input_Sources inputSource, UpdateHandler functionToCall)
  118. {
  119. sourceMap[inputSource].onUpdate += functionToCall;
  120. }
  121. /// <summary>Stops executing the function setup by the corresponding AddListener</summary>
  122. /// <param name="functionToStopCalling">The local function that you've setup to receive update events</param>
  123. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  124. public void RemoveOnUpdateListener(SteamVR_Input_Sources inputSource, UpdateHandler functionToStopCalling)
  125. {
  126. sourceMap[inputSource].onUpdate -= functionToStopCalling;
  127. }
  128. void ISerializationCallbackReceiver.OnBeforeSerialize() { }
  129. void ISerializationCallbackReceiver.OnAfterDeserialize()
  130. {
  131. InitAfterDeserialize();
  132. }
  133. /// <summary>
  134. /// Sets all pose and skeleton actions to use the specified universe origin.
  135. /// </summary>
  136. public static void SetTrackingUniverseOrigin(ETrackingUniverseOrigin newOrigin)
  137. {
  138. SetUniverseOrigin(newOrigin);
  139. }
  140. }
  141. [Serializable]
  142. /// <summary>
  143. /// The base pose action (pose and skeleton inherit from this)
  144. /// </summary>
  145. public abstract class SteamVR_Action_Pose_Base<SourceMap, SourceElement> : SteamVR_Action_In<SourceMap, SourceElement>, ISteamVR_Action_Pose
  146. where SourceMap : SteamVR_Action_Pose_Source_Map<SourceElement>, new()
  147. where SourceElement : SteamVR_Action_Pose_Source, new()
  148. {
  149. /// <summary>
  150. /// Sets all pose (and skeleton) actions to use the specified universe origin.
  151. /// </summary>
  152. protected static void SetUniverseOrigin(ETrackingUniverseOrigin newOrigin)
  153. {
  154. for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsPose.Length; actionIndex++)
  155. {
  156. SteamVR_Input.actionsPose[actionIndex].sourceMap.SetTrackingUniverseOrigin(newOrigin);
  157. }
  158. for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsSkeleton.Length; actionIndex++)
  159. {
  160. SteamVR_Input.actionsSkeleton[actionIndex].sourceMap.SetTrackingUniverseOrigin(newOrigin);
  161. }
  162. }
  163. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local position of this action relative to the universe origin</summary>
  164. public Vector3 localPosition { get { return sourceMap[SteamVR_Input_Sources.Any].localPosition; } }
  165. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local rotation of this action relative to the universe origin</summary>
  166. public Quaternion localRotation { get { return sourceMap[SteamVR_Input_Sources.Any].localRotation; } }
  167. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  168. public ETrackingResult trackingState { get { return sourceMap[SteamVR_Input_Sources.Any].trackingState; } }
  169. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local velocity of this pose relative to the universe origin</summary>
  170. public Vector3 velocity { get { return sourceMap[SteamVR_Input_Sources.Any].velocity; } }
  171. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local angular velocity of this pose relative to the universe origin</summary>
  172. public Vector3 angularVelocity { get { return sourceMap[SteamVR_Input_Sources.Any].angularVelocity; } }
  173. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  174. public bool poseIsValid { get { return sourceMap[SteamVR_Input_Sources.Any].poseIsValid; } }
  175. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the device bound to this action and input source is connected</summary>
  176. public bool deviceIsConnected { get { return sourceMap[SteamVR_Input_Sources.Any].deviceIsConnected; } }
  177. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local position for this pose during the previous update</summary>
  178. public Vector3 lastLocalPosition { get { return sourceMap[SteamVR_Input_Sources.Any].lastLocalPosition; } }
  179. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local rotation for this pose during the previous update</summary>
  180. public Quaternion lastLocalRotation { get { return sourceMap[SteamVR_Input_Sources.Any].lastLocalRotation; } }
  181. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The tracking state for this pose during the previous update</summary>
  182. public ETrackingResult lastTrackingState { get { return sourceMap[SteamVR_Input_Sources.Any].lastTrackingState; } }
  183. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The velocity for this pose during the previous update</summary>
  184. public Vector3 lastVelocity { get { return sourceMap[SteamVR_Input_Sources.Any].lastVelocity; } }
  185. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The angular velocity for this pose during the previous update</summary>
  186. public Vector3 lastAngularVelocity { get { return sourceMap[SteamVR_Input_Sources.Any].lastAngularVelocity; } }
  187. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the pose was valid during the previous update</summary>
  188. public bool lastPoseIsValid { get { return sourceMap[SteamVR_Input_Sources.Any].lastPoseIsValid; } }
  189. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the device bound to this action was connected during the previous update</summary>
  190. public bool lastDeviceIsConnected { get { return sourceMap[SteamVR_Input_Sources.Any].lastDeviceIsConnected; } }
  191. public SteamVR_Action_Pose_Base() { }
  192. /// <summary>
  193. /// <strong>[Should not be called by user code]</strong>
  194. /// Updates the data for all the input sources the system has detected need to be updated.
  195. /// </summary>
  196. public virtual void UpdateValues(bool skipStateAndEventUpdates)
  197. {
  198. sourceMap.UpdateValues(skipStateAndEventUpdates);
  199. }
  200. /// <summary>
  201. /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
  202. /// </summary>
  203. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  204. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  205. /// <returns>true if the call succeeded</returns>
  206. public bool GetVelocitiesAtTimeOffset(SteamVR_Input_Sources inputSource, float secondsFromNow, out Vector3 velocity, out Vector3 angularVelocity)
  207. {
  208. return sourceMap[inputSource].GetVelocitiesAtTimeOffset(secondsFromNow, out velocity, out angularVelocity);
  209. }
  210. /// <summary>
  211. /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
  212. /// </summary>
  213. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  214. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  215. /// <returns>true if the call succeeded</returns>
  216. public bool GetPoseAtTimeOffset(SteamVR_Input_Sources inputSource, float secondsFromNow, out Vector3 localPosition, out Quaternion localRotation, out Vector3 velocity, out Vector3 angularVelocity)
  217. {
  218. return sourceMap[inputSource].GetPoseAtTimeOffset(secondsFromNow, out localPosition, out localRotation, out velocity, out angularVelocity);
  219. }
  220. /// <summary>
  221. /// Update a transform's local position and local roation to match the pose from the most recent update
  222. /// </summary>
  223. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  224. /// <param name="transformToUpdate">The transform of the object to be updated</param>
  225. public virtual void UpdateTransform(SteamVR_Input_Sources inputSource, Transform transformToUpdate)
  226. {
  227. sourceMap[inputSource].UpdateTransform(transformToUpdate);
  228. }
  229. /// <summary>The local position of this action relative to the universe origin</summary>
  230. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  231. public Vector3 GetLocalPosition(SteamVR_Input_Sources inputSource)
  232. {
  233. return sourceMap[inputSource].localPosition;
  234. }
  235. /// <summary>The local rotation of this action relative to the universe origin</summary>
  236. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  237. public Quaternion GetLocalRotation(SteamVR_Input_Sources inputSource)
  238. {
  239. return sourceMap[inputSource].localRotation;
  240. }
  241. /// <summary>The local velocity of this pose relative to the universe origin</summary>
  242. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  243. public Vector3 GetVelocity(SteamVR_Input_Sources inputSource)
  244. {
  245. return sourceMap[inputSource].velocity;
  246. }
  247. /// <summary>The local angular velocity of this pose relative to the universe origin</summary>
  248. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  249. public Vector3 GetAngularVelocity(SteamVR_Input_Sources inputSource)
  250. {
  251. return sourceMap[inputSource].angularVelocity;
  252. }
  253. /// <summary>True if the device bound to this action and input source is connected</summary>
  254. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  255. public bool GetDeviceIsConnected(SteamVR_Input_Sources inputSource)
  256. {
  257. return sourceMap[inputSource].deviceIsConnected;
  258. }
  259. /// <summary>True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  260. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  261. public bool GetPoseIsValid(SteamVR_Input_Sources inputSource)
  262. {
  263. return sourceMap[inputSource].poseIsValid;
  264. }
  265. /// <summary>The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  266. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  267. public ETrackingResult GetTrackingResult(SteamVR_Input_Sources inputSource)
  268. {
  269. return sourceMap[inputSource].trackingState;
  270. }
  271. /// <summary>The local position for this pose during the previous update</summary>
  272. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  273. public Vector3 GetLastLocalPosition(SteamVR_Input_Sources inputSource)
  274. {
  275. return sourceMap[inputSource].lastLocalPosition;
  276. }
  277. /// <summary>The local rotation for this pose during the previous update</summary>
  278. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  279. public Quaternion GetLastLocalRotation(SteamVR_Input_Sources inputSource)
  280. {
  281. return sourceMap[inputSource].lastLocalRotation;
  282. }
  283. /// <summary>The velocity for this pose during the previous update</summary>
  284. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  285. public Vector3 GetLastVelocity(SteamVR_Input_Sources inputSource)
  286. {
  287. return sourceMap[inputSource].lastVelocity;
  288. }
  289. /// <summary>The angular velocity for this pose during the previous update</summary>
  290. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  291. public Vector3 GetLastAngularVelocity(SteamVR_Input_Sources inputSource)
  292. {
  293. return sourceMap[inputSource].lastAngularVelocity;
  294. }
  295. /// <summary>True if the device bound to this action was connected during the previous update</summary>
  296. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  297. public bool GetLastDeviceIsConnected(SteamVR_Input_Sources inputSource)
  298. {
  299. return sourceMap[inputSource].lastDeviceIsConnected;
  300. }
  301. /// <summary>True if the pose was valid during the previous update</summary>
  302. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  303. public bool GetLastPoseIsValid(SteamVR_Input_Sources inputSource)
  304. {
  305. return sourceMap[inputSource].lastPoseIsValid;
  306. }
  307. /// <summary>The tracking state for this pose during the previous update</summary>
  308. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  309. public ETrackingResult GetLastTrackingResult(SteamVR_Input_Sources inputSource)
  310. {
  311. return sourceMap[inputSource].lastTrackingState;
  312. }
  313. }
  314. /// <summary>
  315. /// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
  316. /// </summary>
  317. public class SteamVR_Action_Pose_Source_Map<Source> : SteamVR_Action_In_Source_Map<Source>
  318. where Source : SteamVR_Action_Pose_Source, new()
  319. {
  320. /// <summary>
  321. /// Sets all pose (and skeleton) actions to use the specified universe origin without going through the sourcemap indexer
  322. /// </summary>
  323. public void SetTrackingUniverseOrigin(ETrackingUniverseOrigin newOrigin)
  324. {
  325. var sourceEnumerator = sources.GetEnumerator();
  326. while (sourceEnumerator.MoveNext())
  327. {
  328. var sourceElement = sourceEnumerator.Current;
  329. sourceElement.Value.universeOrigin = newOrigin;
  330. }
  331. }
  332. public virtual void UpdateValues(bool skipStateAndEventUpdates)
  333. {
  334. for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
  335. {
  336. sources[updatingSources[sourceIndex]].UpdateValue(skipStateAndEventUpdates);
  337. }
  338. }
  339. }
  340. public class SteamVR_Action_Pose_Source : SteamVR_Action_In_Source, ISteamVR_Action_Pose
  341. {
  342. public ETrackingUniverseOrigin universeOrigin = ETrackingUniverseOrigin.TrackingUniverseRawAndUncalibrated;
  343. protected static uint poseActionData_size = 0;
  344. /// <summary>The amount of time in the future (or past!) the input system will predict poses for. Default is one frame forward (at 90hz) to account for render time.</summary>
  345. public float predictedSecondsFromNow = 0.011f;
  346. /// <summary>The distance the pose needs to move/rotate before a change is detected</summary>
  347. public float changeTolerance = Mathf.Epsilon;
  348. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  349. public event SteamVR_Action_Pose.ActiveChangeHandler onActiveChange;
  350. /// <summary>Event fires when the active state of the binding changes</summary>
  351. public event SteamVR_Action_Pose.ActiveChangeHandler onActiveBindingChange;
  352. /// <summary>Event fires when the orientation of the pose changes more than the changeTolerance</summary>
  353. public event SteamVR_Action_Pose.ChangeHandler onChange;
  354. /// <summary>Event fires when the action is updated</summary>
  355. public event SteamVR_Action_Pose.UpdateHandler onUpdate;
  356. /// <summary>Event fires when the state of the tracking system that is used to create pose data (position, rotation, etc) changes</summary>
  357. public event SteamVR_Action_Pose.TrackingChangeHandler onTrackingChanged;
  358. /// <summary>Event fires when the state of the pose data retrieved for this action changes validity (good/bad data from the tracking source)</summary>
  359. public event SteamVR_Action_Pose.ValidPoseChangeHandler onValidPoseChanged;
  360. /// <summary>Event fires when the device bound to this action is connected or disconnected</summary>
  361. public event SteamVR_Action_Pose.DeviceConnectedChangeHandler onDeviceConnectedChanged;
  362. /// <summary>True when the orientation of the pose has changhed more than changeTolerance in the last update. Note: Will only return true if the action is also active.</summary>
  363. public override bool changed { get; protected set; }
  364. /// <summary>The value of the action's 'changed' during the previous update</summary>
  365. public override bool lastChanged { get; protected set; }
  366. /// <summary>The handle to the origin of the component that was used to update this pose</summary>
  367. public override ulong activeOrigin
  368. {
  369. get
  370. {
  371. if (active)
  372. return poseActionData.activeOrigin;
  373. return 0;
  374. }
  375. }
  376. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  377. public override ulong lastActiveOrigin { get { return lastPoseActionData.activeOrigin; } }
  378. /// <summary>True if this action is bound and the ActionSet is active</summary>
  379. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  380. /// <summary>True if the action is bound</summary>
  381. public override bool activeBinding { get { return poseActionData.bActive; } }
  382. /// <summary>If the action was active (ActionSet active and binding active) during the last update</summary>
  383. public override bool lastActive { get; protected set; }
  384. /// <summary>If the action's binding was active during the previous update</summary>
  385. public override bool lastActiveBinding { get { return lastPoseActionData.bActive; } }
  386. /// <summary>The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  387. public ETrackingResult trackingState { get { return poseActionData.pose.eTrackingResult; } }
  388. /// <summary>The tracking state for this pose during the previous update</summary>
  389. public ETrackingResult lastTrackingState { get { return lastPoseActionData.pose.eTrackingResult; } }
  390. /// <summary>True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  391. public bool poseIsValid { get { return poseActionData.pose.bPoseIsValid; } }
  392. /// <summary>True if the pose was valid during the previous update</summary>
  393. public bool lastPoseIsValid { get { return lastPoseActionData.pose.bPoseIsValid; } }
  394. /// <summary>True if the device bound to this action and input source is connected</summary>
  395. public bool deviceIsConnected { get { return poseActionData.pose.bDeviceIsConnected; } }
  396. /// <summary>True if the device bound to this action was connected during the previous update</summary>
  397. public bool lastDeviceIsConnected { get { return lastPoseActionData.pose.bDeviceIsConnected; } }
  398. /// <summary>The local position of this action relative to the universe origin</summary>
  399. public Vector3 localPosition { get; protected set; }
  400. /// <summary>The local rotation of this action relative to the universe origin</summary>
  401. public Quaternion localRotation { get; protected set; }
  402. /// <summary>The local position for this pose during the previous update</summary>
  403. public Vector3 lastLocalPosition { get; protected set; }
  404. /// <summary>The local rotation for this pose during the previous update</summary>
  405. public Quaternion lastLocalRotation { get; protected set; }
  406. /// <summary>The local velocity of this pose relative to the universe origin</summary>
  407. public Vector3 velocity { get; protected set; }
  408. /// <summary>The velocity for this pose during the previous update</summary>
  409. public Vector3 lastVelocity { get; protected set; }
  410. /// <summary>The local angular velocity of this pose relative to the universe origin</summary>
  411. public Vector3 angularVelocity { get; protected set; }
  412. /// <summary>The angular velocity for this pose during the previous update</summary>
  413. public Vector3 lastAngularVelocity { get; protected set; }
  414. protected InputPoseActionData_t poseActionData = new InputPoseActionData_t();
  415. protected InputPoseActionData_t lastPoseActionData = new InputPoseActionData_t();
  416. protected InputPoseActionData_t tempPoseActionData = new InputPoseActionData_t();
  417. protected SteamVR_Action_Pose poseAction;
  418. /// <summary>
  419. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  420. /// </summary>
  421. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  422. {
  423. base.Preinitialize(wrappingAction, forInputSource);
  424. poseAction = wrappingAction as SteamVR_Action_Pose;
  425. }
  426. /// <summary>
  427. /// <strong>[Should not be called by user code]</strong>
  428. /// Initializes the handle for the inputSource, the pose action data size, and any other related SteamVR data.
  429. /// </summary>
  430. public override void Initialize()
  431. {
  432. base.Initialize();
  433. if (poseActionData_size == 0)
  434. poseActionData_size = (uint)Marshal.SizeOf(typeof(InputPoseActionData_t));
  435. }
  436. /// <summary><strong>[Should not be called by user code]</strong>
  437. /// Updates the data for this action and this input source. Sends related events.
  438. /// </summary>
  439. public override void UpdateValue()
  440. {
  441. UpdateValue(false);
  442. }
  443. /// <summary><strong>[Should not be called by user code]</strong>
  444. /// Updates the data for this action and this input source. Sends related events.
  445. /// </summary>
  446. public virtual void UpdateValue(bool skipStateAndEventUpdates)
  447. {
  448. lastChanged = changed;
  449. lastPoseActionData = poseActionData;
  450. lastLocalPosition = localPosition;
  451. lastLocalRotation = localRotation;
  452. lastVelocity = velocity;
  453. lastAngularVelocity = angularVelocity;
  454. EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, predictedSecondsFromNow, ref poseActionData, poseActionData_size, inputSourceHandle);
  455. if (err != EVRInputError.None)
  456. {
  457. Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " Handle: " + handle.ToString() + ". Input source: " + inputSource.ToString());
  458. }
  459. SetCacheVariables();
  460. changed = GetChanged();
  461. if (changed)
  462. changedTime = updateTime + predictedSecondsFromNow;
  463. if (skipStateAndEventUpdates == false)
  464. CheckAndSendEvents();
  465. }
  466. protected void SetCacheVariables()
  467. {
  468. localPosition = SteamVR_Utils.GetPosition(poseActionData.pose.mDeviceToAbsoluteTracking);
  469. localRotation = SteamVR_Utils.GetRotation(poseActionData.pose.mDeviceToAbsoluteTracking);
  470. velocity = GetUnityCoordinateVelocity(poseActionData.pose.vVelocity);
  471. angularVelocity = GetUnityCoordinateAngularVelocity(poseActionData.pose.vAngularVelocity);
  472. updateTime = Time.realtimeSinceStartup;
  473. }
  474. protected bool GetChanged()
  475. {
  476. if (Vector3.Distance(localPosition, lastLocalPosition) > changeTolerance)
  477. return true;
  478. else if (Mathf.Abs(Quaternion.Angle(localRotation, lastLocalRotation)) > changeTolerance)
  479. return true;
  480. else if (Vector3.Distance(velocity, lastVelocity) > changeTolerance)
  481. return true;
  482. else if (Vector3.Distance(angularVelocity, lastAngularVelocity) > changeTolerance)
  483. return true;
  484. return false;
  485. }
  486. /// <summary>
  487. /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
  488. /// </summary>
  489. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  490. /// <returns>true if we successfully returned a pose</returns>
  491. public bool GetVelocitiesAtTimeOffset(float secondsFromNow, out Vector3 velocityAtTime, out Vector3 angularVelocityAtTime)
  492. {
  493. EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, inputSourceHandle);
  494. if (err != EVRInputError.None)
  495. {
  496. Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error
  497. velocityAtTime = Vector3.zero;
  498. angularVelocityAtTime = Vector3.zero;
  499. return false;
  500. }
  501. velocityAtTime = GetUnityCoordinateVelocity(tempPoseActionData.pose.vVelocity);
  502. angularVelocityAtTime = GetUnityCoordinateAngularVelocity(tempPoseActionData.pose.vAngularVelocity);
  503. return true;
  504. }
  505. /// <summary>
  506. /// SteamVR keeps a log of past poses so you can retrieve old poses or estimated poses in the future by passing in a secondsFromNow value that is negative or positive.
  507. /// </summary>
  508. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  509. /// <returns>true if we successfully returned a pose</returns>
  510. public bool GetPoseAtTimeOffset(float secondsFromNow, out Vector3 positionAtTime, out Quaternion rotationAtTime, out Vector3 velocityAtTime, out Vector3 angularVelocityAtTime)
  511. {
  512. EVRInputError err = OpenVR.Input.GetPoseActionData(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, inputSourceHandle);
  513. if (err != EVRInputError.None)
  514. {
  515. Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error
  516. velocityAtTime = Vector3.zero;
  517. angularVelocityAtTime = Vector3.zero;
  518. positionAtTime = Vector3.zero;
  519. rotationAtTime = Quaternion.identity;
  520. return false;
  521. }
  522. velocityAtTime = GetUnityCoordinateVelocity(tempPoseActionData.pose.vVelocity);
  523. angularVelocityAtTime = GetUnityCoordinateAngularVelocity(tempPoseActionData.pose.vAngularVelocity);
  524. positionAtTime = SteamVR_Utils.GetPosition(tempPoseActionData.pose.mDeviceToAbsoluteTracking);
  525. rotationAtTime = SteamVR_Utils.GetRotation(tempPoseActionData.pose.mDeviceToAbsoluteTracking);
  526. return true;
  527. }
  528. /// <summary>
  529. /// Update a transform's local position and local roation to match the pose.
  530. /// </summary>
  531. /// <param name="transformToUpdate">The transform of the object to be updated</param>
  532. public void UpdateTransform(Transform transformToUpdate)
  533. {
  534. transformToUpdate.localPosition = localPosition;
  535. transformToUpdate.localRotation = localRotation;
  536. }
  537. protected virtual void CheckAndSendEvents()
  538. {
  539. if (trackingState != lastTrackingState && onTrackingChanged != null)
  540. onTrackingChanged.Invoke(poseAction, inputSource, trackingState);
  541. if (poseIsValid != lastPoseIsValid && onValidPoseChanged != null)
  542. onValidPoseChanged.Invoke(poseAction, inputSource, poseIsValid);
  543. if (deviceIsConnected != lastDeviceIsConnected && onDeviceConnectedChanged != null)
  544. onDeviceConnectedChanged.Invoke(poseAction, inputSource, deviceIsConnected);
  545. if (changed && onChange != null)
  546. onChange.Invoke(poseAction, inputSource);
  547. if (active != lastActive && onActiveChange != null)
  548. onActiveChange.Invoke(poseAction, inputSource, active);
  549. if (activeBinding != lastActiveBinding && onActiveBindingChange != null)
  550. onActiveBindingChange.Invoke(poseAction, inputSource, activeBinding);
  551. if (onUpdate != null)
  552. onUpdate.Invoke(poseAction, inputSource);
  553. }
  554. protected Vector3 GetUnityCoordinateVelocity(HmdVector3_t vector)
  555. {
  556. return GetUnityCoordinateVelocity(vector.v0, vector.v1, vector.v2);
  557. }
  558. protected Vector3 GetUnityCoordinateAngularVelocity(HmdVector3_t vector)
  559. {
  560. return GetUnityCoordinateAngularVelocity(vector.v0, vector.v1, vector.v2);
  561. }
  562. protected Vector3 GetUnityCoordinateVelocity(float x, float y, float z)
  563. {
  564. Vector3 vector = new Vector3();
  565. vector.x = x;
  566. vector.y = y;
  567. vector.z = -z;
  568. return vector;
  569. }
  570. protected Vector3 GetUnityCoordinateAngularVelocity(float x, float y, float z)
  571. {
  572. Vector3 vector = new Vector3();
  573. vector.x = -x;
  574. vector.y = -y;
  575. vector.z = z;
  576. return vector;
  577. }
  578. }
  579. /// <summary>
  580. /// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
  581. /// </summary>
  582. public interface ISteamVR_Action_Pose : ISteamVR_Action_In_Source
  583. {
  584. /// <summary>The local position of this action relative to the universe origin</summary>
  585. Vector3 localPosition { get; }
  586. /// <summary>The local rotation of this action relative to the universe origin</summary>
  587. Quaternion localRotation { get; }
  588. /// <summary>The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  589. ETrackingResult trackingState { get; }
  590. /// <summary>The local velocity of this pose relative to the universe origin</summary>
  591. Vector3 velocity { get; }
  592. /// <summary>The local angular velocity of this pose relative to the universe origin</summary>
  593. Vector3 angularVelocity { get; }
  594. /// <summary>True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  595. bool poseIsValid { get; }
  596. /// <summary>True if the device bound to this action and input source is connected</summary>
  597. bool deviceIsConnected { get; }
  598. /// <summary>The local position for this pose during the previous update</summary>
  599. Vector3 lastLocalPosition { get; }
  600. /// <summary>The local rotation for this pose during the previous update</summary>
  601. Quaternion lastLocalRotation { get; }
  602. /// <summary>The tracking state for this pose during the previous update</summary>
  603. ETrackingResult lastTrackingState { get; }
  604. /// <summary>The velocity for this pose during the previous update</summary>
  605. Vector3 lastVelocity { get; }
  606. /// <summary>The angular velocity for this pose during the previous update</summary>
  607. Vector3 lastAngularVelocity { get; }
  608. /// <summary>True if the pose was valid during the previous update</summary>
  609. bool lastPoseIsValid { get; }
  610. /// <summary>True if the device bound to this action was connected during the previous update</summary>
  611. bool lastDeviceIsConnected { get; }
  612. }
  613. }