NativeEmulator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. #if UNITY_EDITOR
  12. using System;
  13. using UnityEngine;
  14. using System.Runtime.InteropServices;
  15. [Obsolete]
  16. internal partial class NativeEmulator
  17. {
  18. /// <summary> Default constructor. </summary>
  19. public NativeEmulator()
  20. {
  21. }
  22. /// <summary> Handle of the tracking. </summary>
  23. private UInt64 m_TrackingHandle;
  24. /// <summary> Handle of the controller. </summary>
  25. private UInt64 m_ControllerHandle;
  26. #region Tracking
  27. /// <summary> Creates simulation tracking. </summary>
  28. /// <returns> True if it succeeds, false if it fails. </returns>
  29. public bool CreateSIMTracking()
  30. {
  31. NativeResult result = NativeApi.NRSIMTrackingCreate(ref m_TrackingHandle);
  32. return result == NativeResult.Success;
  33. }
  34. /// <summary> Sets head tracking pose. </summary>
  35. /// <param name="position"> The position.</param>
  36. /// <param name="rotation"> The rotation.</param>
  37. /// <returns> True if it succeeds, false if it fails. </returns>
  38. public bool SetHeadTrackingPose(Vector3 position, Quaternion rotation)
  39. {
  40. NativeVector3f nativeVector3F = new NativeVector3f(new Vector3(position.x, position.y, -position.z));
  41. NativeVector4f nativeVector4F = new NativeVector4f(new Vector4(rotation.x, rotation.y, -rotation.z, -rotation.w));
  42. NativeResult result = NativeApi.NRSIMTrackingSetHeadTrackingPose(m_TrackingHandle, ref nativeVector3F, ref nativeVector4F);
  43. return result == NativeResult.Success;
  44. }
  45. /// <summary> Updates the trackable image data. </summary>
  46. /// <param name="centerPos"> The center position.</param>
  47. /// <param name="centerQua"> The center qua.</param>
  48. /// <param name="extentX"> The extent x coordinate.</param>
  49. /// <param name="extentZ"> The extent z coordinate.</param>
  50. /// <param name="identifier"> The identifier.</param>
  51. /// <param name="state"> The state.</param>
  52. /// <returns> True if it succeeds, false if it fails. </returns>
  53. public bool UpdateTrackableImageData(Vector3 centerPos, Quaternion centerQua, float extentX, float extentZ, UInt32 identifier, TrackingState state)
  54. {
  55. NativeVector3f pos = new NativeVector3f(new Vector3(centerPos.x, centerPos.y, -centerPos.z));
  56. NativeVector4f qua = new NativeVector4f(new Vector4(-centerQua.x, -centerQua.y, centerQua.z, centerQua.w));
  57. NativeResult result = NativeApi.NRSIMTrackingUpdateTrackableImageData(m_TrackingHandle, ref pos, ref qua, extentX, extentZ, identifier, (int)state);
  58. return result == NativeResult.Success;
  59. }
  60. /// <summary> Updates the trackable plane data. </summary>
  61. /// <param name="centerPos"> The center position.</param>
  62. /// <param name="centerQua"> The center qua.</param>
  63. /// <param name="extentX"> The extent x coordinate.</param>
  64. /// <param name="extentZ"> The extent z coordinate.</param>
  65. /// <param name="identifier"> The identifier.</param>
  66. /// <param name="state"> The state.</param>
  67. /// <returns> True if it succeeds, false if it fails. </returns>
  68. public bool UpdateTrackablePlaneData(Vector3 centerPos, Quaternion centerQua, float extentX, float extentZ, UInt32 identifier, TrackingState state)
  69. {
  70. NativeVector3f pos = new NativeVector3f(new Vector3(centerPos.x, centerPos.y, -centerPos.z));
  71. NativeVector4f qua = new NativeVector4f(new Vector4(-centerQua.x, -centerQua.y, centerQua.z, centerQua.w));
  72. NativeResult result = NativeApi.NRSIMTrackingUpdateTrackablePlaneData(m_TrackingHandle, ref pos, ref qua, extentX, extentZ, identifier, (int)state);
  73. return result == NativeResult.Success;
  74. }
  75. /// <summary> Set the trackables data for Unity Editor develop. </summary>
  76. /// <typeparam name="T"> Generic type parameter.</typeparam>
  77. /// <param name="centerPos"> The center position.</param>
  78. /// <param name="centerQua"> The center qua.</param>
  79. /// <param name="extentX"> The extent x coordinate.</param>
  80. /// <param name="extentZ"> The extent z coordinate.</param>
  81. /// <param name="identifier"> The identifier.</param>
  82. /// <param name="state"> The state.</param>
  83. /// <returns> True if it succeeds, false if it fails. </returns>
  84. public bool UpdateTrackableData<T>(Vector3 centerPos, Quaternion centerQua, float extentX, float extentZ, System.UInt32 identifier, TrackingState state) where T : NRTrackable
  85. {
  86. if (typeof(NRTrackablePlane).Equals(typeof(T)))
  87. {
  88. return NREmulatorTrackableProvider.UpdateTrackablePlaneData(centerPos, centerQua, extentX, extentZ, identifier, state);
  89. }
  90. else if (typeof(NRTrackableImage).Equals(typeof(T)))
  91. {
  92. return NREmulatorTrackableProvider.UpdateTrackableImageData(centerPos, centerQua, extentX, extentZ, identifier, state);
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. #endregion
  100. #region Controller
  101. /// <summary> Creates simulation controller. </summary>
  102. /// <returns> True if it succeeds, false if it fails. </returns>
  103. public bool CreateSIMController()
  104. {
  105. NativeResult result = NativeApi.NRSIMControllerCreate(ref m_ControllerHandle);
  106. return result == NativeResult.Success;
  107. }
  108. /// <summary> Determines if we can destory simulation controller. </summary>
  109. /// <returns> True if it succeeds, false if it fails. </returns>
  110. public bool DestorySIMController()
  111. {
  112. NativeResult result = NativeApi.NRSIMControllerDestroyAll();
  113. return result == NativeResult.Success;
  114. }
  115. /// <summary> Sets controller timestamp. </summary>
  116. /// <param name="timestamp"> The timestamp.</param>
  117. /// <returns> True if it succeeds, false if it fails. </returns>
  118. public bool SetControllerTimestamp(UInt64 timestamp)
  119. {
  120. NativeResult result = NativeApi.NRSIMControllerSetTimestamp(m_ControllerHandle, timestamp);
  121. return result == NativeResult.Success;
  122. }
  123. /// <summary> Sets controller position. </summary>
  124. /// <param name="postion"> The postion.</param>
  125. /// <returns> True if it succeeds, false if it fails. </returns>
  126. public bool SetControllerPosition(Vector3 postion)
  127. {
  128. NativeVector3f nv3 = new NativeVector3f(postion);
  129. NativeResult result = NativeApi.NRSIMControllerSetPosition(m_ControllerHandle, ref nv3);
  130. return result == NativeResult.Success;
  131. }
  132. /// <summary> Sets controller rotation. </summary>
  133. /// <param name="quaternion"> The quaternion.</param>
  134. /// <returns> True if it succeeds, false if it fails. </returns>
  135. public bool SetControllerRotation(Quaternion quaternion)
  136. {
  137. NativeVector4f nv4 = new NativeVector4f(new Vector4(quaternion.x, quaternion.y, -quaternion.z, -quaternion.w));
  138. NativeResult result = NativeApi.NRSIMControllerSetRotation(m_ControllerHandle, ref nv4);
  139. return result == NativeResult.Success;
  140. }
  141. /// <summary> Sets controller accelerometer. </summary>
  142. /// <param name="accel"> The accel.</param>
  143. /// <returns> True if it succeeds, false if it fails. </returns>
  144. public bool SetControllerAccelerometer(Vector3 accel)
  145. {
  146. NativeVector3f nv3 = new NativeVector3f(accel);
  147. NativeResult result = NativeApi.NRSIMControllerSetAccelerometer(m_ControllerHandle, ref nv3);
  148. return result == NativeResult.Success;
  149. }
  150. /// <summary> Sets controller button state. </summary>
  151. /// <param name="buttonState"> State of the button.</param>
  152. /// <returns> True if it succeeds, false if it fails. </returns>
  153. public bool SetControllerButtonState(Int32 buttonState)
  154. {
  155. NativeResult result = NativeApi.NRSIMControllerSetButtonState(m_ControllerHandle, buttonState);
  156. return result == NativeResult.Success;
  157. }
  158. /// <summary> Sets controller is touching. </summary>
  159. /// <param name="isTouching"> True if is touching, false if not.</param>
  160. /// <returns> True if it succeeds, false if it fails. </returns>
  161. public bool SetControllerIsTouching(bool isTouching)
  162. {
  163. NativeResult result = NativeApi.NRSIMControllerSetIsTouching(m_ControllerHandle, isTouching);
  164. return result == NativeResult.Success;
  165. }
  166. /// <summary> Sets controller touch point. </summary>
  167. /// <param name="x"> The x coordinate.</param>
  168. /// <param name="y"> The y coordinate.</param>
  169. /// <returns> True if it succeeds, false if it fails. </returns>
  170. public bool SetControllerTouchPoint(float x, float y)
  171. {
  172. NativeVector3f nv3 = new NativeVector3f(new Vector3(x, y, 0f));
  173. NativeResult result = NativeApi.NRSIMControllerSetTouchPoint(m_ControllerHandle, ref nv3);
  174. return result == NativeResult.Success;
  175. }
  176. /// <summary> Sets controller submit. </summary>
  177. /// <returns> True if it succeeds, false if it fails. </returns>
  178. public bool SetControllerSubmit()
  179. {
  180. NativeResult result = NativeApi.NRSIMControllerSubmit(m_ControllerHandle);
  181. return result == NativeResult.Success;
  182. }
  183. #endregion
  184. private partial struct NativeApi
  185. {
  186. /// <summary> Nrsim tracking create. </summary>
  187. /// <param name="out_tracking_handle"> [in,out] Handle of the out tracking.</param>
  188. /// <returns> A NativeResult. </returns>
  189. [DllImport(NativeConstants.NRNativeLibrary)]
  190. public static extern NativeResult NRSIMTrackingCreate(ref UInt64 out_tracking_handle);
  191. /// <summary> Nrsim tracking set head tracking pose. </summary>
  192. /// <param name="tracking_handle"> Handle of the tracking.</param>
  193. /// <param name="position"> [in,out] The position.</param>
  194. /// <param name="rotation"> [in,out] The rotation.</param>
  195. /// <returns> A NativeResult. </returns>
  196. [DllImport(NativeConstants.NRNativeLibrary)]
  197. public static extern NativeResult NRSIMTrackingSetHeadTrackingPose(UInt64 tracking_handle,
  198. ref NativeVector3f position, ref NativeVector4f rotation);
  199. /// <summary> Nrsim tracking update trackable image data. </summary>
  200. /// <param name="tracking_handle"> Handle of the tracking.</param>
  201. /// <param name="center_pos"> [in,out] The center position.</param>
  202. /// <param name="center_rotation"> [in,out] The center rotation.</param>
  203. /// <param name="extent_x"> The extent x coordinate.</param>
  204. /// <param name="extent_z"> The extent z coordinate.</param>
  205. /// <param name="identifier"> The identifier.</param>
  206. /// <param name="state"> The state.</param>
  207. /// <returns> A NativeResult. </returns>
  208. [DllImport(NativeConstants.NRNativeLibrary)]
  209. public static extern NativeResult NRSIMTrackingUpdateTrackableImageData
  210. (UInt64 tracking_handle, ref NativeVector3f center_pos, ref NativeVector4f center_rotation,
  211. float extent_x, float extent_z, UInt32 identifier, int state);
  212. /// <summary> Nrsim tracking update trackable plane data. </summary>
  213. /// <param name="tracking_handle"> Handle of the tracking.</param>
  214. /// <param name="center_pos"> [in,out] The center position.</param>
  215. /// <param name="center_rotation"> [in,out] The center rotation.</param>
  216. /// <param name="extent_x"> The extent x coordinate.</param>
  217. /// <param name="extent_z"> The extent z coordinate.</param>
  218. /// <param name="identifier"> The identifier.</param>
  219. /// <param name="state"> The state.</param>
  220. /// <returns> A NativeResult. </returns>
  221. [DllImport(NativeConstants.NRNativeLibrary)]
  222. public static extern NativeResult NRSIMTrackingUpdateTrackablePlaneData(UInt64 tracking_handle,
  223. ref NativeVector3f center_pos, ref NativeVector4f center_rotation,
  224. float extent_x, float extent_z, UInt32 identifier, int state);
  225. /// <summary> Controller. </summary>
  226. /// <param name="out_controller_handle"> [in,out] Handle of the out controller.</param>
  227. /// <returns> A NativeResult. </returns>
  228. [DllImport(NativeConstants.NRNativeLibrary)]
  229. public static extern NativeResult NRSIMControllerCreate(ref UInt64 out_controller_handle);
  230. /// <summary> Nrsim controller destroy all. </summary>
  231. /// <returns> A NativeResult. </returns>
  232. [DllImport(NativeConstants.NRNativeLibrary)]
  233. public static extern NativeResult NRSIMControllerDestroyAll();
  234. /// <summary> Nrsim controller set timestamp. </summary>
  235. /// <param name="controller_handle"> Handle of the controller.</param>
  236. /// <param name="timestamp"> The timestamp.</param>
  237. /// <returns> A NativeResult. </returns>
  238. [DllImport(NativeConstants.NRNativeLibrary)]
  239. public static extern NativeResult NRSIMControllerSetTimestamp(UInt64 controller_handle, UInt64 timestamp);
  240. /// <summary> Nrsim controller set position. </summary>
  241. /// <param name="controller_handle"> Handle of the controller.</param>
  242. /// <param name="position"> [in,out] The position.</param>
  243. /// <returns> A NativeResult. </returns>
  244. [DllImport(NativeConstants.NRNativeLibrary)]
  245. public static extern NativeResult NRSIMControllerSetPosition(UInt64 controller_handle, ref NativeVector3f position);
  246. /// <summary> Nrsim controller set rotation. </summary>
  247. /// <param name="controller_handle"> Handle of the controller.</param>
  248. /// <param name="rotation"> [in,out] The rotation.</param>
  249. /// <returns> A NativeResult. </returns>
  250. [DllImport(NativeConstants.NRNativeLibrary)]
  251. public static extern NativeResult NRSIMControllerSetRotation(UInt64 controller_handle, ref NativeVector4f rotation);
  252. /// <summary> Nrsim controller set accelerometer. </summary>
  253. /// <param name="controller_handle"> Handle of the controller.</param>
  254. /// <param name="accel"> [in,out] The accel.</param>
  255. /// <returns> A NativeResult. </returns>
  256. [DllImport(NativeConstants.NRNativeLibrary)]
  257. public static extern NativeResult NRSIMControllerSetAccelerometer(UInt64 controller_handle, ref NativeVector3f accel);
  258. /// <summary> Nrsim controller set button state. </summary>
  259. /// <param name="controller_handle"> Handle of the controller.</param>
  260. /// <param name="buttonState"> State of the button.</param>
  261. /// <returns> A NativeResult. </returns>
  262. [DllImport(NativeConstants.NRNativeLibrary)]
  263. public static extern NativeResult NRSIMControllerSetButtonState(UInt64 controller_handle, Int32 buttonState);
  264. /// <summary> Nrsim controller set is touching. </summary>
  265. /// <param name="controller_handle"> Handle of the controller.</param>
  266. /// <param name="isTouching"> True if is touching, false if not.</param>
  267. /// <returns> A NativeResult. </returns>
  268. [DllImport(NativeConstants.NRNativeLibrary)]
  269. public static extern NativeResult NRSIMControllerSetIsTouching(UInt64 controller_handle, bool isTouching);
  270. /// <summary> Nrsim controller set touch point. </summary>
  271. /// <param name="controller_handle"> Handle of the controller.</param>
  272. /// <param name="point"> [in,out] The point.</param>
  273. /// <returns> A NativeResult. </returns>
  274. [DllImport(NativeConstants.NRNativeLibrary)]
  275. public static extern NativeResult NRSIMControllerSetTouchPoint(UInt64 controller_handle, ref NativeVector3f point);
  276. /// <summary> Nrsim controller submit. </summary>
  277. /// <param name="controller_handle"> Handle of the controller.</param>
  278. /// <returns> A NativeResult. </returns>
  279. [DllImport(NativeConstants.NRNativeLibrary)]
  280. public static extern NativeResult NRSIMControllerSubmit(UInt64 controller_handle);
  281. /// <summary> Free library. </summary>
  282. /// <param name="hModule"> The module.</param>
  283. /// <returns> True if it succeeds, false if it fails. </returns>
  284. [DllImport("kernel32", SetLastError = true)]
  285. public static extern bool FreeLibrary(IntPtr hModule);
  286. }
  287. }
  288. #endif
  289. }