SteamVR_Action_Pose.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. OpenVR.Compositor.SetTrackingSpace(newOrigin);
  140. }
  141. }
  142. [Serializable]
  143. /// <summary>
  144. /// The base pose action (pose and skeleton inherit from this)
  145. /// </summary>
  146. public abstract class SteamVR_Action_Pose_Base<SourceMap, SourceElement> : SteamVR_Action_In<SourceMap, SourceElement>, ISteamVR_Action_Pose
  147. where SourceMap : SteamVR_Action_Pose_Source_Map<SourceElement>, new()
  148. where SourceElement : SteamVR_Action_Pose_Source, new()
  149. {
  150. /// <summary>
  151. /// Sets all pose (and skeleton) actions to use the specified universe origin.
  152. /// </summary>
  153. protected static void SetUniverseOrigin(ETrackingUniverseOrigin newOrigin)
  154. {
  155. for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsPose.Length; actionIndex++)
  156. {
  157. SteamVR_Input.actionsPose[actionIndex].sourceMap.SetTrackingUniverseOrigin(newOrigin);
  158. }
  159. for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsSkeleton.Length; actionIndex++)
  160. {
  161. SteamVR_Input.actionsSkeleton[actionIndex].sourceMap.SetTrackingUniverseOrigin(newOrigin);
  162. }
  163. }
  164. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local position of this action relative to the universe origin</summary>
  165. public Vector3 localPosition { get { return sourceMap[SteamVR_Input_Sources.Any].localPosition; } }
  166. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local rotation of this action relative to the universe origin</summary>
  167. public Quaternion localRotation { get { return sourceMap[SteamVR_Input_Sources.Any].localRotation; } }
  168. /// <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>
  169. public ETrackingResult trackingState { get { return sourceMap[SteamVR_Input_Sources.Any].trackingState; } }
  170. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local velocity of this pose relative to the universe origin</summary>
  171. public Vector3 velocity { get { return sourceMap[SteamVR_Input_Sources.Any].velocity; } }
  172. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local angular velocity of this pose relative to the universe origin</summary>
  173. public Vector3 angularVelocity { get { return sourceMap[SteamVR_Input_Sources.Any].angularVelocity; } }
  174. /// <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>
  175. public bool poseIsValid { get { return sourceMap[SteamVR_Input_Sources.Any].poseIsValid; } }
  176. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the device bound to this action and input source is connected</summary>
  177. public bool deviceIsConnected { get { return sourceMap[SteamVR_Input_Sources.Any].deviceIsConnected; } }
  178. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local position for this pose during the previous update</summary>
  179. public Vector3 lastLocalPosition { get { return sourceMap[SteamVR_Input_Sources.Any].lastLocalPosition; } }
  180. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The local rotation for this pose during the previous update</summary>
  181. public Quaternion lastLocalRotation { get { return sourceMap[SteamVR_Input_Sources.Any].lastLocalRotation; } }
  182. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The tracking state for this pose during the previous update</summary>
  183. public ETrackingResult lastTrackingState { get { return sourceMap[SteamVR_Input_Sources.Any].lastTrackingState; } }
  184. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The velocity for this pose during the previous update</summary>
  185. public Vector3 lastVelocity { get { return sourceMap[SteamVR_Input_Sources.Any].lastVelocity; } }
  186. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The angular velocity for this pose during the previous update</summary>
  187. public Vector3 lastAngularVelocity { get { return sourceMap[SteamVR_Input_Sources.Any].lastAngularVelocity; } }
  188. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the pose was valid during the previous update</summary>
  189. public bool lastPoseIsValid { get { return sourceMap[SteamVR_Input_Sources.Any].lastPoseIsValid; } }
  190. /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> True if the device bound to this action was connected during the previous update</summary>
  191. public bool lastDeviceIsConnected { get { return sourceMap[SteamVR_Input_Sources.Any].lastDeviceIsConnected; } }
  192. public SteamVR_Action_Pose_Base() { }
  193. /// <summary>
  194. /// <strong>[Should not be called by user code]</strong>
  195. /// Updates the data for all the input sources the system has detected need to be updated.
  196. /// </summary>
  197. public virtual void UpdateValues(bool skipStateAndEventUpdates)
  198. {
  199. sourceMap.UpdateValues(skipStateAndEventUpdates);
  200. }
  201. /// <summary>
  202. /// 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.
  203. /// </summary>
  204. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  205. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  206. /// <returns>true if the call succeeded</returns>
  207. public bool GetVelocitiesAtTimeOffset(SteamVR_Input_Sources inputSource, float secondsFromNow, out Vector3 velocity, out Vector3 angularVelocity)
  208. {
  209. return sourceMap[inputSource].GetVelocitiesAtTimeOffset(secondsFromNow, out velocity, out angularVelocity);
  210. }
  211. /// <summary>
  212. /// 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.
  213. /// </summary>
  214. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  215. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  216. /// <returns>true if the call succeeded</returns>
  217. public bool GetPoseAtTimeOffset(SteamVR_Input_Sources inputSource, float secondsFromNow, out Vector3 localPosition, out Quaternion localRotation, out Vector3 velocity, out Vector3 angularVelocity)
  218. {
  219. return sourceMap[inputSource].GetPoseAtTimeOffset(secondsFromNow, out localPosition, out localRotation, out velocity, out angularVelocity);
  220. }
  221. /// <summary>
  222. /// Update a transform's local position and local roation to match the pose from the most recent update
  223. /// </summary>
  224. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  225. /// <param name="transformToUpdate">The transform of the object to be updated</param>
  226. public virtual void UpdateTransform(SteamVR_Input_Sources inputSource, Transform transformToUpdate)
  227. {
  228. sourceMap[inputSource].UpdateTransform(transformToUpdate);
  229. }
  230. /// <summary>The local position of this action relative to the universe origin</summary>
  231. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  232. public Vector3 GetLocalPosition(SteamVR_Input_Sources inputSource)
  233. {
  234. return sourceMap[inputSource].localPosition;
  235. }
  236. /// <summary>The local rotation of this action relative to the universe origin</summary>
  237. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  238. public Quaternion GetLocalRotation(SteamVR_Input_Sources inputSource)
  239. {
  240. return sourceMap[inputSource].localRotation;
  241. }
  242. /// <summary>The local velocity of this pose relative to the universe origin</summary>
  243. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  244. public Vector3 GetVelocity(SteamVR_Input_Sources inputSource)
  245. {
  246. return sourceMap[inputSource].velocity;
  247. }
  248. /// <summary>The local angular velocity of this pose relative to the universe origin</summary>
  249. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  250. public Vector3 GetAngularVelocity(SteamVR_Input_Sources inputSource)
  251. {
  252. return sourceMap[inputSource].angularVelocity;
  253. }
  254. /// <summary>True if the device bound to this action and input source is connected</summary>
  255. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  256. public bool GetDeviceIsConnected(SteamVR_Input_Sources inputSource)
  257. {
  258. return sourceMap[inputSource].deviceIsConnected;
  259. }
  260. /// <summary>True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  261. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  262. public bool GetPoseIsValid(SteamVR_Input_Sources inputSource)
  263. {
  264. return sourceMap[inputSource].poseIsValid;
  265. }
  266. /// <summary>The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  267. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  268. public ETrackingResult GetTrackingResult(SteamVR_Input_Sources inputSource)
  269. {
  270. return sourceMap[inputSource].trackingState;
  271. }
  272. /// <summary>The local position for this pose during the previous update</summary>
  273. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  274. public Vector3 GetLastLocalPosition(SteamVR_Input_Sources inputSource)
  275. {
  276. return sourceMap[inputSource].lastLocalPosition;
  277. }
  278. /// <summary>The local rotation for this pose during the previous update</summary>
  279. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  280. public Quaternion GetLastLocalRotation(SteamVR_Input_Sources inputSource)
  281. {
  282. return sourceMap[inputSource].lastLocalRotation;
  283. }
  284. /// <summary>The velocity for this pose during the previous update</summary>
  285. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  286. public Vector3 GetLastVelocity(SteamVR_Input_Sources inputSource)
  287. {
  288. return sourceMap[inputSource].lastVelocity;
  289. }
  290. /// <summary>The angular velocity for this pose during the previous update</summary>
  291. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  292. public Vector3 GetLastAngularVelocity(SteamVR_Input_Sources inputSource)
  293. {
  294. return sourceMap[inputSource].lastAngularVelocity;
  295. }
  296. /// <summary>True if the device bound to this action was connected during the previous update</summary>
  297. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  298. public bool GetLastDeviceIsConnected(SteamVR_Input_Sources inputSource)
  299. {
  300. return sourceMap[inputSource].lastDeviceIsConnected;
  301. }
  302. /// <summary>True if the pose was valid during the previous update</summary>
  303. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  304. public bool GetLastPoseIsValid(SteamVR_Input_Sources inputSource)
  305. {
  306. return sourceMap[inputSource].lastPoseIsValid;
  307. }
  308. /// <summary>The tracking state for this pose during the previous update</summary>
  309. /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
  310. public ETrackingResult GetLastTrackingResult(SteamVR_Input_Sources inputSource)
  311. {
  312. return sourceMap[inputSource].lastTrackingState;
  313. }
  314. }
  315. /// <summary>
  316. /// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
  317. /// </summary>
  318. public class SteamVR_Action_Pose_Source_Map<Source> : SteamVR_Action_In_Source_Map<Source>
  319. where Source : SteamVR_Action_Pose_Source, new()
  320. {
  321. /// <summary>
  322. /// Sets all pose (and skeleton) actions to use the specified universe origin without going through the sourcemap indexer
  323. /// </summary>
  324. public void SetTrackingUniverseOrigin(ETrackingUniverseOrigin newOrigin)
  325. {
  326. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  327. {
  328. if (sources[sourceIndex] != null)
  329. sources[sourceIndex].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 distance the pose needs to move/rotate before a change is detected</summary>
  345. public float changeTolerance = Mathf.Epsilon;
  346. /// <summary>Event fires when the active state (ActionSet active and binding active) changes</summary>
  347. public event SteamVR_Action_Pose.ActiveChangeHandler onActiveChange;
  348. /// <summary>Event fires when the active state of the binding changes</summary>
  349. public event SteamVR_Action_Pose.ActiveChangeHandler onActiveBindingChange;
  350. /// <summary>Event fires when the orientation of the pose changes more than the changeTolerance</summary>
  351. public event SteamVR_Action_Pose.ChangeHandler onChange;
  352. /// <summary>Event fires when the action is updated</summary>
  353. public event SteamVR_Action_Pose.UpdateHandler onUpdate;
  354. /// <summary>Event fires when the state of the tracking system that is used to create pose data (position, rotation, etc) changes</summary>
  355. public event SteamVR_Action_Pose.TrackingChangeHandler onTrackingChanged;
  356. /// <summary>Event fires when the state of the pose data retrieved for this action changes validity (good/bad data from the tracking source)</summary>
  357. public event SteamVR_Action_Pose.ValidPoseChangeHandler onValidPoseChanged;
  358. /// <summary>Event fires when the device bound to this action is connected or disconnected</summary>
  359. public event SteamVR_Action_Pose.DeviceConnectedChangeHandler onDeviceConnectedChanged;
  360. /// <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>
  361. public override bool changed { get; protected set; }
  362. /// <summary>The value of the action's 'changed' during the previous update</summary>
  363. public override bool lastChanged { get; protected set; }
  364. /// <summary>The handle to the origin of the component that was used to update this pose</summary>
  365. public override ulong activeOrigin
  366. {
  367. get
  368. {
  369. if (active)
  370. return poseActionData.activeOrigin;
  371. return 0;
  372. }
  373. }
  374. /// <summary>The handle to the origin of the component that was used to update the value for this action (for the previous update)</summary>
  375. public override ulong lastActiveOrigin { get { return lastPoseActionData.activeOrigin; } }
  376. /// <summary>True if this action is bound and the ActionSet is active</summary>
  377. public override bool active { get { return activeBinding && action.actionSet.IsActive(inputSource); } }
  378. /// <summary>True if the action is bound</summary>
  379. public override bool activeBinding { get { return poseActionData.bActive; } }
  380. /// <summary>If the action was active (ActionSet active and binding active) during the last update</summary>
  381. public override bool lastActive { get; protected set; }
  382. /// <summary>If the action's binding was active during the previous update</summary>
  383. public override bool lastActiveBinding { get { return lastPoseActionData.bActive; } }
  384. /// <summary>The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  385. public ETrackingResult trackingState { get { return poseActionData.pose.eTrackingResult; } }
  386. /// <summary>The tracking state for this pose during the previous update</summary>
  387. public ETrackingResult lastTrackingState { get { return lastPoseActionData.pose.eTrackingResult; } }
  388. /// <summary>True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  389. public bool poseIsValid { get { return poseActionData.pose.bPoseIsValid; } }
  390. /// <summary>True if the pose was valid during the previous update</summary>
  391. public bool lastPoseIsValid { get { return lastPoseActionData.pose.bPoseIsValid; } }
  392. /// <summary>True if the device bound to this action and input source is connected</summary>
  393. public bool deviceIsConnected { get { return poseActionData.pose.bDeviceIsConnected; } }
  394. /// <summary>True if the device bound to this action was connected during the previous update</summary>
  395. public bool lastDeviceIsConnected { get { return lastPoseActionData.pose.bDeviceIsConnected; } }
  396. /// <summary>The local position of this action relative to the universe origin</summary>
  397. public Vector3 localPosition { get; protected set; }
  398. /// <summary>The local rotation of this action relative to the universe origin</summary>
  399. public Quaternion localRotation { get; protected set; }
  400. /// <summary>The local position for this pose during the previous update</summary>
  401. public Vector3 lastLocalPosition { get; protected set; }
  402. /// <summary>The local rotation for this pose during the previous update</summary>
  403. public Quaternion lastLocalRotation { get; protected set; }
  404. /// <summary>The local velocity of this pose relative to the universe origin</summary>
  405. public Vector3 velocity { get; protected set; }
  406. /// <summary>The velocity for this pose during the previous update</summary>
  407. public Vector3 lastVelocity { get; protected set; }
  408. /// <summary>The local angular velocity of this pose relative to the universe origin</summary>
  409. public Vector3 angularVelocity { get; protected set; }
  410. /// <summary>The angular velocity for this pose during the previous update</summary>
  411. public Vector3 lastAngularVelocity { get; protected set; }
  412. protected InputPoseActionData_t poseActionData = new InputPoseActionData_t();
  413. protected InputPoseActionData_t lastPoseActionData = new InputPoseActionData_t();
  414. protected InputPoseActionData_t tempPoseActionData = new InputPoseActionData_t();
  415. protected SteamVR_Action_Pose poseAction;
  416. /// <summary>
  417. /// <strong>[Should not be called by user code]</strong> Sets up the internals of the action source before SteamVR has been initialized.
  418. /// </summary>
  419. public override void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
  420. {
  421. base.Preinitialize(wrappingAction, forInputSource);
  422. poseAction = wrappingAction as SteamVR_Action_Pose;
  423. }
  424. /// <summary>
  425. /// <strong>[Should not be called by user code]</strong>
  426. /// Initializes the handle for the inputSource, the pose action data size, and any other related SteamVR data.
  427. /// </summary>
  428. public override void Initialize()
  429. {
  430. base.Initialize();
  431. if (poseActionData_size == 0)
  432. poseActionData_size = (uint)Marshal.SizeOf(typeof(InputPoseActionData_t));
  433. }
  434. /// <summary><strong>[Should not be called by user code]</strong>
  435. /// Updates the data for this action and this input source. Sends related events.
  436. /// </summary>
  437. public override void UpdateValue()
  438. {
  439. UpdateValue(false);
  440. }
  441. public static float framesAhead = 2;
  442. /// <summary><strong>[Should not be called by user code]</strong>
  443. /// Updates the data for this action and this input source. Sends related events.
  444. /// </summary>
  445. public virtual void UpdateValue(bool skipStateAndEventUpdates)
  446. {
  447. lastChanged = changed;
  448. lastPoseActionData = poseActionData;
  449. lastLocalPosition = localPosition;
  450. lastLocalRotation = localRotation;
  451. lastVelocity = velocity;
  452. lastAngularVelocity = angularVelocity;
  453. EVRInputError err;
  454. if (framesAhead == 0)
  455. err = OpenVR.Input.GetPoseActionDataForNextFrame(handle, universeOrigin, ref poseActionData, poseActionData_size, inputSourceHandle);
  456. else
  457. err = OpenVR.Input.GetPoseActionDataRelativeToNow(handle, universeOrigin, framesAhead * (Time.timeScale / SteamVR.instance.hmd_DisplayFrequency), ref poseActionData, poseActionData_size, inputSourceHandle);
  458. if (err != EVRInputError.None)
  459. {
  460. Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " Handle: " + handle.ToString() + ". Input source: " + inputSource.ToString());
  461. }
  462. if (active)
  463. {
  464. SetCacheVariables();
  465. changed = GetChanged();
  466. }
  467. if (changed)
  468. changedTime = updateTime;
  469. if (skipStateAndEventUpdates == false)
  470. CheckAndSendEvents();
  471. }
  472. protected void SetCacheVariables()
  473. {
  474. localPosition = poseActionData.pose.mDeviceToAbsoluteTracking.GetPosition();
  475. localRotation = poseActionData.pose.mDeviceToAbsoluteTracking.GetRotation();
  476. velocity = GetUnityCoordinateVelocity(poseActionData.pose.vVelocity);
  477. angularVelocity = GetUnityCoordinateAngularVelocity(poseActionData.pose.vAngularVelocity);
  478. updateTime = Time.realtimeSinceStartup;
  479. }
  480. protected bool GetChanged()
  481. {
  482. if (Vector3.Distance(localPosition, lastLocalPosition) > changeTolerance)
  483. return true;
  484. else if (Mathf.Abs(Quaternion.Angle(localRotation, lastLocalRotation)) > changeTolerance)
  485. return true;
  486. else if (Vector3.Distance(velocity, lastVelocity) > changeTolerance)
  487. return true;
  488. else if (Vector3.Distance(angularVelocity, lastAngularVelocity) > changeTolerance)
  489. return true;
  490. return false;
  491. }
  492. /// <summary>
  493. /// 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.
  494. /// </summary>
  495. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  496. /// <returns>true if we successfully returned a pose</returns>
  497. public bool GetVelocitiesAtTimeOffset(float secondsFromNow, out Vector3 velocityAtTime, out Vector3 angularVelocityAtTime)
  498. {
  499. EVRInputError err = OpenVR.Input.GetPoseActionDataRelativeToNow(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, inputSourceHandle);
  500. if (err != EVRInputError.None)
  501. {
  502. Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error
  503. velocityAtTime = Vector3.zero;
  504. angularVelocityAtTime = Vector3.zero;
  505. return false;
  506. }
  507. velocityAtTime = GetUnityCoordinateVelocity(tempPoseActionData.pose.vVelocity);
  508. angularVelocityAtTime = GetUnityCoordinateAngularVelocity(tempPoseActionData.pose.vAngularVelocity);
  509. return true;
  510. }
  511. /// <summary>
  512. /// 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.
  513. /// </summary>
  514. /// <param name="secondsFromNow">The time offset in the future (estimated) or in the past (previously recorded) you want to get data from</param>
  515. /// <returns>true if we successfully returned a pose</returns>
  516. public bool GetPoseAtTimeOffset(float secondsFromNow, out Vector3 positionAtTime, out Quaternion rotationAtTime, out Vector3 velocityAtTime, out Vector3 angularVelocityAtTime)
  517. {
  518. EVRInputError err = OpenVR.Input.GetPoseActionDataRelativeToNow(handle, universeOrigin, secondsFromNow, ref tempPoseActionData, poseActionData_size, inputSourceHandle);
  519. if (err != EVRInputError.None)
  520. {
  521. Debug.LogError("<b>[SteamVR]</b> GetPoseActionData error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString()); //todo: this should be an error
  522. velocityAtTime = Vector3.zero;
  523. angularVelocityAtTime = Vector3.zero;
  524. positionAtTime = Vector3.zero;
  525. rotationAtTime = Quaternion.identity;
  526. return false;
  527. }
  528. velocityAtTime = GetUnityCoordinateVelocity(tempPoseActionData.pose.vVelocity);
  529. angularVelocityAtTime = GetUnityCoordinateAngularVelocity(tempPoseActionData.pose.vAngularVelocity);
  530. positionAtTime = tempPoseActionData.pose.mDeviceToAbsoluteTracking.GetPosition();
  531. rotationAtTime = tempPoseActionData.pose.mDeviceToAbsoluteTracking.GetRotation();
  532. return true;
  533. }
  534. /// <summary>
  535. /// Update a transform's local position and local roation to match the pose.
  536. /// </summary>
  537. /// <param name="transformToUpdate">The transform of the object to be updated</param>
  538. public void UpdateTransform(Transform transformToUpdate)
  539. {
  540. transformToUpdate.localPosition = localPosition;
  541. transformToUpdate.localRotation = localRotation;
  542. }
  543. protected virtual void CheckAndSendEvents()
  544. {
  545. if (trackingState != lastTrackingState && onTrackingChanged != null)
  546. onTrackingChanged.Invoke(poseAction, inputSource, trackingState);
  547. if (poseIsValid != lastPoseIsValid && onValidPoseChanged != null)
  548. onValidPoseChanged.Invoke(poseAction, inputSource, poseIsValid);
  549. if (deviceIsConnected != lastDeviceIsConnected && onDeviceConnectedChanged != null)
  550. onDeviceConnectedChanged.Invoke(poseAction, inputSource, deviceIsConnected);
  551. if (changed && onChange != null)
  552. onChange.Invoke(poseAction, inputSource);
  553. if (active != lastActive && onActiveChange != null)
  554. onActiveChange.Invoke(poseAction, inputSource, active);
  555. if (activeBinding != lastActiveBinding && onActiveBindingChange != null)
  556. onActiveBindingChange.Invoke(poseAction, inputSource, activeBinding);
  557. if (onUpdate != null)
  558. onUpdate.Invoke(poseAction, inputSource);
  559. }
  560. protected Vector3 GetUnityCoordinateVelocity(HmdVector3_t vector)
  561. {
  562. return GetUnityCoordinateVelocity(vector.v0, vector.v1, vector.v2);
  563. }
  564. protected Vector3 GetUnityCoordinateAngularVelocity(HmdVector3_t vector)
  565. {
  566. return GetUnityCoordinateAngularVelocity(vector.v0, vector.v1, vector.v2);
  567. }
  568. protected Vector3 GetUnityCoordinateVelocity(float x, float y, float z)
  569. {
  570. Vector3 vector = new Vector3();
  571. vector.x = x;
  572. vector.y = y;
  573. vector.z = -z;
  574. return vector;
  575. }
  576. protected Vector3 GetUnityCoordinateAngularVelocity(float x, float y, float z)
  577. {
  578. Vector3 vector = new Vector3();
  579. vector.x = -x;
  580. vector.y = -y;
  581. vector.z = z;
  582. return vector;
  583. }
  584. }
  585. /// <summary>
  586. /// Boolean actions are either true or false. There is an onStateUp and onStateDown event for the rising and falling edge.
  587. /// </summary>
  588. public interface ISteamVR_Action_Pose : ISteamVR_Action_In_Source
  589. {
  590. /// <summary>The local position of this action relative to the universe origin</summary>
  591. Vector3 localPosition { get; }
  592. /// <summary>The local rotation of this action relative to the universe origin</summary>
  593. Quaternion localRotation { get; }
  594. /// <summary>The state of the tracking system that is used to create pose data (position, rotation, etc)</summary>
  595. ETrackingResult trackingState { get; }
  596. /// <summary>The local velocity of this pose relative to the universe origin</summary>
  597. Vector3 velocity { get; }
  598. /// <summary>The local angular velocity of this pose relative to the universe origin</summary>
  599. Vector3 angularVelocity { get; }
  600. /// <summary>True if the pose retrieved for this action and input source is valid (good data from the tracking source)</summary>
  601. bool poseIsValid { get; }
  602. /// <summary>True if the device bound to this action and input source is connected</summary>
  603. bool deviceIsConnected { get; }
  604. /// <summary>The local position for this pose during the previous update</summary>
  605. Vector3 lastLocalPosition { get; }
  606. /// <summary>The local rotation for this pose during the previous update</summary>
  607. Quaternion lastLocalRotation { get; }
  608. /// <summary>The tracking state for this pose during the previous update</summary>
  609. ETrackingResult lastTrackingState { get; }
  610. /// <summary>The velocity for this pose during the previous update</summary>
  611. Vector3 lastVelocity { get; }
  612. /// <summary>The angular velocity for this pose during the previous update</summary>
  613. Vector3 lastAngularVelocity { get; }
  614. /// <summary>True if the pose was valid during the previous update</summary>
  615. bool lastPoseIsValid { get; }
  616. /// <summary>True if the device bound to this action was connected during the previous update</summary>
  617. bool lastDeviceIsConnected { get; }
  618. }
  619. }