GSXRPluginAndroid.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using SC.XR.Unity;
  6. class GSXRPluginAndroid : GSXRPlugin
  7. {
  8. private const string DLLName = "gsxrplugin";
  9. [DllImport(DLLName)]
  10. private static extern IntPtr GetRenderEventFunc();
  11. [DllImport(DLLName)]
  12. private static extern bool GSXRIsInitialized();
  13. [DllImport(DLLName)]
  14. private static extern bool GSXRIsRunning();
  15. [DllImport(DLLName)]
  16. private static extern bool GSXRCanBeginVR();
  17. [DllImport(DLLName)]
  18. private static extern void GSXRInitializeEventData(IntPtr activity);
  19. [DllImport(DLLName)]
  20. private static extern void GSXRSubmitFrameEventData(int frameIndex, float fieldOfView, int frameType);
  21. [DllImport(DLLName)]
  22. private static extern void GSXRSetupLayerCoords(int layerIndex, float[] lowerLeft, float[] lowerRight, float[] upperLeft, float[] upperRight);
  23. [DllImport(DLLName)]
  24. private static extern void GSXRSetupLayerData(int layerIndex, int sideMask, int textureId, int textureType, int layerFlags);
  25. [DllImport(DLLName)]
  26. private static extern void GSXRSetTrackingModeEventData(int mode);
  27. [DllImport(DLLName)]
  28. private static extern void GSXRSetPerformanceLevelsEventData(int newCpuPerfLevel, int newGpuPerfLevel);
  29. [DllImport(DLLName)]
  30. private static extern void GSXRSetEyeEventData(int sideMask, int layerMask);
  31. [DllImport(DLLName)]
  32. private static extern void GSXRSetColorSpace(int colorSpace);
  33. [DllImport(DLLName)]
  34. private static extern void GSXRSetFrameOption(uint frameOption);
  35. [DllImport(DLLName)]
  36. private static extern void GSXRUnsetFrameOption(uint frameOption);
  37. [DllImport(DLLName)]
  38. private static extern void GSXRSetVSyncCount(int vSyncCount);
  39. [DllImport(DLLName)]
  40. private static extern int GSXRGetPredictedPose(ref float rx,
  41. ref float ry,
  42. ref float rz,
  43. ref float rw,
  44. ref float px,
  45. ref float py,
  46. ref float pz,
  47. int frameIndex,
  48. bool isMultiThreadedRender);
  49. [DllImport(DLLName)]
  50. private static extern int GSXRGetEyePose(ref int leftStatus,
  51. ref int rightStatus,
  52. ref int combinedStatus,
  53. ref float leftOpenness,
  54. ref float rightOpenness,
  55. ref float leftDirectionX,
  56. ref float leftDirectionY,
  57. ref float leftDirectionZ,
  58. ref float leftPositionX,
  59. ref float leftPositionY,
  60. ref float leftPositionZ,
  61. ref float rightDirectionX,
  62. ref float rightDirectionY,
  63. ref float rightDirectionZ,
  64. ref float rightPositionX,
  65. ref float rightPositionY,
  66. ref float rightPositionZ,
  67. ref float combinedDirectionX,
  68. ref float combinedDirectionY,
  69. ref float combinedDirectionZ,
  70. ref float combinedPositionX,
  71. ref float combinedPositionY,
  72. ref float combinedPositionZ,
  73. int frameIndex);
  74. [DllImport(DLLName)]
  75. private static extern bool GSXRRecenterTrackingPose();
  76. [DllImport(DLLName)]
  77. private static extern int GSXRGetTrackingMode();
  78. [DllImport(DLLName)]
  79. private static extern void GSXRGetDeviceInfo(ref int displayWidthPixels,
  80. ref int displayHeightPixels,
  81. ref float displayRefreshRateHz,
  82. ref int targetEyeWidthPixels,
  83. ref int targetEyeHeightPixels,
  84. ref float targetFovXRad,
  85. ref float targetFovYRad,
  86. ref float leftFrustumLeft, ref float leftFrustumRight, ref float leftFrustumBottom, ref float leftFrustumTop, ref float leftFrustumNear, ref float leftEyeFrustumFar,
  87. ref float rightFrustumLeft, ref float rightFrustumRight, ref float rightFrustumBottom, ref float rightFrustumTop, ref float rightFrustumNear, ref float rightFrustumFar,
  88. ref float targetfrustumConvergence, ref float targetFrustumPitch);
  89. [DllImport(DLLName)]
  90. private static extern void GSXRSetFrameOffset(float[] delta);
  91. [DllImport(DLLName)]
  92. private static extern void GSXRSetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum);
  93. [DllImport(DLLName)]
  94. private static extern bool GSXRPollEvent(ref int eventType, ref uint deviceId, ref float eventTimeStamp, int eventDataCount, uint[] eventData);
  95. //---------------------------------------------------------------------------------------------
  96. // Conotroller Apis
  97. //---------------------------------------------------------------------------------------------
  98. [DllImport(DLLName)]
  99. private static extern int GSXRControllerStartTracking(string desc);
  100. [DllImport(DLLName)]
  101. private static extern void GSXRControllerStopTracking(int handle);
  102. [DllImport(DLLName)]
  103. private static extern void GSXRControllerGetState(int handle, int space, ref GSXRControllerState state);
  104. [DllImport(DLLName)]
  105. private static extern void GSXRControllerSendMessage(int handle, int what, int arg1, int arg2);
  106. [DllImport(DLLName)]
  107. private static extern int GSXRControllerQuery(int handle, int what, IntPtr mem, int size);
  108. private enum RenderEvent
  109. {
  110. Initialize,
  111. BeginVr,
  112. EndVr,
  113. BeginEye,
  114. EndEye,
  115. SubmitFrame,
  116. Foveation,
  117. Shutdown,
  118. RecenterTracking,
  119. SetTrackingMode,
  120. SetPerformanceLevels
  121. };
  122. public static GSXRPluginAndroid Create()
  123. {
  124. if(Application.isEditor)
  125. {
  126. Debug.LogError("GSXRPlugin not supported in unity editor!");
  127. throw new InvalidOperationException();
  128. }
  129. return new GSXRPluginAndroid();
  130. }
  131. private GSXRPluginAndroid() {}
  132. private void IssueEvent(RenderEvent e)
  133. {
  134. // Queue a specific callback to be called on the render thread
  135. GL.IssuePluginEvent(GetRenderEventFunc(), (int)e);
  136. }
  137. public override bool IsInitialized() { return GSXRIsInitialized(); }
  138. public override bool IsRunning() { return GSXRIsRunning(); }
  139. public override IEnumerator Initialize()
  140. {
  141. //yield return new WaitUntil(() => SvrIsInitialized() == false); // Wait for shutdown
  142. yield return base.Initialize();
  143. if(Application.platform == RuntimePlatform.Android) {
  144. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  145. AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  146. GSXRInitializeEventData(activity.GetRawObject());
  147. }
  148. IssueEvent(RenderEvent.Initialize);
  149. yield return new WaitUntil (() => GSXRIsInitialized () == true);
  150. yield return null; // delay one frame - fix for re-init w multi-threaded rendering
  151. deviceInfo = GetDeviceInfo();
  152. }
  153. public override IEnumerator BeginVr(int cpuPerfLevel, int gpuPerfLevel)
  154. {
  155. //yield return new WaitUntil(() => GSXRIsRunning() == false); // Wait for EndVr
  156. yield return base.BeginVr(cpuPerfLevel, gpuPerfLevel);
  157. // float[6]: x, y, z, w, u, v
  158. float[] lowerLeft = { -1f, -1f, 0f, 1f, 0f, 0f };
  159. float[] upperLeft = { -1f, 1f, 0f, 1f, 0f, 1f };
  160. float[] upperRight = { 1f, 1f, 0f, 1f, 1f, 1f };
  161. float[] lowerRight = { 1f, -1f, 0f, 1f, 1f, 0f };
  162. GSXRSetupLayerCoords(-1, lowerLeft, lowerRight, upperLeft, upperRight); // Layers/All
  163. GSXRSetPerformanceLevelsEventData(cpuPerfLevel, gpuPerfLevel);
  164. ColorSpace space = QualitySettings.activeColorSpace;
  165. if(space == ColorSpace.Gamma)
  166. {
  167. // 0 == kColorSpaceLinear from svrApi.h
  168. GSXRSetColorSpace(0); //Unity will be supplying gamma space eye buffers into warp so we want
  169. //to setup a linear color space display surface so no further gamma
  170. //correction is performed
  171. }
  172. else
  173. {
  174. // 1 == kColorSpaceSRGB from svrApi.h
  175. GSXRSetColorSpace(1); //Unity will be supplying linear space eye buffers into warp so we want
  176. //to setup an sRGB color space display surface to properly convert
  177. //incoming linear values into sRGB
  178. }
  179. yield return new WaitUntil(() => GSXRCanBeginVR() == true);
  180. IssueEvent (RenderEvent.BeginVr);
  181. //yield return new WaitUntil(() => GSXRIsRunning() == true);
  182. }
  183. public override void EndVr()
  184. {
  185. base.EndVr();
  186. Debug.Log("EndVR...."+Time.frameCount);
  187. IssueEvent (RenderEvent.EndVr);
  188. }
  189. public override void BeginEye(int sideMask, float[] frameDelta)
  190. {
  191. GSXRSetFrameOffset(frameDelta); // Enabled for foveation head orientation delta
  192. GSXRSetEyeEventData(sideMask, 0);
  193. IssueEvent (RenderEvent.BeginEye);
  194. }
  195. public override void EndEye(int sideMask, int layerMask)
  196. {
  197. GSXRSetEyeEventData(sideMask, layerMask);
  198. IssueEvent(RenderEvent.EndEye);
  199. }
  200. public override void SetTrackingMode(int modeMask)
  201. {
  202. GSXRSetTrackingModeEventData(modeMask);
  203. IssueEvent (RenderEvent.SetTrackingMode);
  204. }
  205. public override void SetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum)
  206. {
  207. GSXRSetFoveationParameters(textureId, previousId, focalPointX, focalPointY, foveationGainX, foveationGainY, foveationArea, foveationMinimum);
  208. }
  209. public override void ApplyFoveation()
  210. {
  211. IssueEvent(RenderEvent.Foveation);
  212. }
  213. public override int GetTrackingMode()
  214. {
  215. return GSXRGetTrackingMode();
  216. }
  217. public override void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel)
  218. {
  219. GSXRSetPerformanceLevelsEventData((int)newCpuPerfLevel, (int)newGpuPerfLevel);
  220. IssueEvent (RenderEvent.SetPerformanceLevels);
  221. }
  222. public override void SetFrameOption(FrameOption frameOption)
  223. {
  224. GSXRSetFrameOption((uint)frameOption);
  225. }
  226. public override void UnsetFrameOption(FrameOption frameOption)
  227. {
  228. GSXRUnsetFrameOption((uint)frameOption);
  229. }
  230. public override void SetVSyncCount(int vSyncCount)
  231. {
  232. GSXRSetVSyncCount(vSyncCount);
  233. }
  234. public override bool RecenterTracking()
  235. {
  236. //IssueEvent (RenderEvent.RecenterTracking);
  237. return GSXRRecenterTrackingPose();
  238. }
  239. public override int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex)
  240. {
  241. orientation.z = -orientation.z;
  242. position.x = -position.x;
  243. position.y = -position.y;
  244. int rv = GSXRGetPredictedPose(ref orientation.x, ref orientation.y, ref orientation.z, ref orientation.w,
  245. ref position.x, ref position.y, ref position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  246. orientation.z = -orientation.z;
  247. position.x = -position.x;
  248. position.y = -position.y;
  249. return rv;
  250. }
  251. private float[] leftViewData = new float[16];
  252. private float[] rightViewData = new float[16];
  253. private float[] TData = new float[3];
  254. private float[] RData = new float[4];
  255. public override int GetHeadPose(ref HeadPose headPose, int frameIndex)
  256. {
  257. int rv = 0;
  258. if (!GSXR_Is_SupportOpticsCalibration()) {
  259. headPose.orientation.z = -headPose.orientation.z;
  260. headPose.position.x = -headPose.position.x;
  261. headPose.position.y = -headPose.position.y;
  262. rv = GSXRGetPredictedPose(ref headPose.orientation.x, ref headPose.orientation.y, ref headPose.orientation.z, ref headPose.orientation.w,
  263. ref headPose.position.x, ref headPose.position.y, ref headPose.position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  264. } else {
  265. rv = GSXR_Get_LatestEyeMatrices(leftViewData, rightViewData, TData, RData, frameIndex, SystemInfo.graphicsMultiThreaded);
  266. leftViewMatrix.SetColumn(0, new Vector4(leftViewData[0], leftViewData[1], leftViewData[2], leftViewData[3]));
  267. leftViewMatrix.SetColumn(1, new Vector4(leftViewData[4], leftViewData[5], leftViewData[6], leftViewData[7]));
  268. leftViewMatrix.SetColumn(2, new Vector4(leftViewData[8], leftViewData[9], leftViewData[10], leftViewData[11]));
  269. leftViewMatrix.SetColumn(3, new Vector4(leftViewData[12], leftViewData[13], leftViewData[14], leftViewData[15]));
  270. rightViewMatrix.SetColumn(0, new Vector4(rightViewData[0], rightViewData[1], rightViewData[2], rightViewData[3]));
  271. rightViewMatrix.SetColumn(1, new Vector4(rightViewData[4], rightViewData[5], rightViewData[6], rightViewData[7]));
  272. rightViewMatrix.SetColumn(2, new Vector4(rightViewData[8], rightViewData[9], rightViewData[10], rightViewData[11]));
  273. rightViewMatrix.SetColumn(3, new Vector4(rightViewData[12], rightViewData[13], rightViewData[14], rightViewData[15]));
  274. // Debug.Log($"leftViewMatrix [{leftViewMatrix.m00}, {leftViewMatrix.m01}, {leftViewMatrix.m02}, {leftViewMatrix.m03};" +
  275. //$"{leftViewMatrix.m10}, {leftViewMatrix.m11}, {leftViewMatrix.m12}, {leftViewMatrix.m13};" +
  276. //$"{leftViewMatrix.m20}, {leftViewMatrix.m21}, {leftViewMatrix.m22}, {leftViewMatrix.m23};" +
  277. //$"{leftViewMatrix.m30}, {leftViewMatrix.m31}, {leftViewMatrix.m32}, {leftViewMatrix.m33}]");
  278. // Debug.Log($"rightViewMatrix [{rightViewMatrix.m00}, {rightViewMatrix.m01}, {rightViewMatrix.m02}, {rightViewMatrix.m03};" +
  279. //$"{rightViewMatrix.m10}, {rightViewMatrix.m11}, {rightViewMatrix.m12}, {rightViewMatrix.m13};" +
  280. //$"{rightViewMatrix.m20}, {rightViewMatrix.m21}, {rightViewMatrix.m22}, {rightViewMatrix.m23};" +
  281. //$"{rightViewMatrix.m30}, {rightViewMatrix.m31}, {rightViewMatrix.m32}, {rightViewMatrix.m33}]");
  282. headPose.orientation.x = RData[0];
  283. headPose.orientation.y = RData[1];
  284. headPose.orientation.z = RData[2];
  285. headPose.orientation.w = RData[3];
  286. headPose.position.x = TData[0];
  287. headPose.position.y = TData[1];
  288. headPose.position.z = TData[2];
  289. }
  290. headPose.orientation.z = -headPose.orientation.z;
  291. headPose.position.x = -headPose.position.x;
  292. headPose.position.y = -headPose.position.y;
  293. return rv;
  294. }
  295. public override int GetEyePose(ref EyePose eyePose, int frameIndex = -1)
  296. {
  297. int rv = GSXRGetEyePose(
  298. ref eyePose.leftStatus, ref eyePose.rightStatus, ref eyePose.combinedStatus,
  299. ref eyePose.leftOpenness, ref eyePose.rightOpenness,
  300. ref eyePose.leftDirection.x, ref eyePose.leftDirection.y, ref eyePose.leftDirection.z,
  301. ref eyePose.leftPosition.x, ref eyePose.leftPosition.y, ref eyePose.leftPosition.z,
  302. ref eyePose.rightDirection.x, ref eyePose.rightDirection.y, ref eyePose.rightDirection.z,
  303. ref eyePose.rightPosition.x, ref eyePose.rightPosition.y, ref eyePose.rightPosition.z,
  304. ref eyePose.combinedDirection.x, ref eyePose.combinedDirection.y, ref eyePose.combinedDirection.z,
  305. ref eyePose.combinedPosition.x, ref eyePose.combinedPosition.y, ref eyePose.combinedPosition.z,
  306. frameIndex);
  307. return rv;
  308. }
  309. public override DeviceInfo GetDeviceInfo()
  310. {
  311. DeviceInfo info = new DeviceInfo();
  312. GSXRGetDeviceInfo (ref info.displayWidthPixels,
  313. ref info.displayHeightPixels,
  314. ref info.displayRefreshRateHz,
  315. ref info.targetEyeWidthPixels,
  316. ref info.targetEyeHeightPixels,
  317. ref info.targetFovXRad,
  318. ref info.targetFovYRad,
  319. ref info.targetFrustumLeft.left, ref info.targetFrustumLeft.right, ref info.targetFrustumLeft.bottom, ref info.targetFrustumLeft.top, ref info.targetFrustumLeft.near, ref info.targetFrustumLeft.far,
  320. ref info.targetFrustumRight.left, ref info.targetFrustumRight.right, ref info.targetFrustumRight.bottom, ref info.targetFrustumRight.top, ref info.targetFrustumRight.near, ref info.targetFrustumRight.far,
  321. ref info.targetFrustumConvergence, ref info.targetFrustumPitch);
  322. return info;
  323. }
  324. public override void SubmitFrame(int frameIndex, float fieldOfView, int frameType)
  325. {
  326. int i;
  327. int layerCount = 0;
  328. if (eyes != null) for (i = 0; i < eyes.Length; i++)
  329. {
  330. var eye = eyes[i];
  331. if (eyes[i].isActiveAndEnabled == false || eye.TextureId == 0 || eye.Side == 0) continue;
  332. if (eye.imageTransform != null && eye.imageTransform.gameObject.activeSelf == false) continue;
  333. GSXRSetupLayerData(layerCount, (int)eye.Side, eye.TextureId, eye.ImageType == GSXREye.eType.EglTexture ? 2 : 0, eye.layerDepth > 0 ? 0x0 : 0x2);
  334. float[] lowerLeft = { eye.clipLowerLeft.x, eye.clipLowerLeft.y, eye.clipLowerLeft.z, eye.clipLowerLeft.w, eye.uvLowerLeft.x, eye.uvLowerLeft.y };
  335. float[] upperLeft = { eye.clipUpperLeft.x, eye.clipUpperLeft.y, eye.clipUpperLeft.z, eye.clipUpperLeft.w, eye.uvUpperLeft.x, eye.uvUpperLeft.y };
  336. float[] upperRight = { eye.clipUpperRight.x, eye.clipUpperRight.y, eye.clipUpperRight.z, eye.clipUpperRight.w, eye.uvUpperRight.x, eye.uvUpperRight.y };
  337. float[] lowerRight = { eye.clipLowerRight.x, eye.clipLowerRight.y, eye.clipLowerRight.z, eye.clipLowerRight.w, eye.uvLowerRight.x, eye.uvLowerRight.y };
  338. GSXRSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  339. layerCount++;
  340. }
  341. if (overlays != null) for (i = 0; i < overlays.Length; i++)
  342. {
  343. var overlay = overlays[i];
  344. if (overlay.isActiveAndEnabled == false || overlay.TextureId == 0 || overlay.Side == 0) continue;
  345. if (overlay.imageTransform != null && overlay.imageTransform.gameObject.activeSelf == false) continue;
  346. GSXRSetupLayerData(layerCount, (int)overlay.Side, overlay.TextureId, overlay.ImageType == GSXROverlay.eType.EglTexture ? 2 : 0, 0x1);
  347. float[] lowerLeft = { overlay.clipLowerLeft.x, overlay.clipLowerLeft.y, overlay.clipLowerLeft.z, overlay.clipLowerLeft.w, overlay.uvLowerLeft.x, overlay.uvLowerLeft.y };
  348. float[] upperLeft = { overlay.clipUpperLeft.x, overlay.clipUpperLeft.y, overlay.clipUpperLeft.z, overlay.clipUpperLeft.w, overlay.uvUpperLeft.x, overlay.uvUpperLeft.y };
  349. float[] upperRight = { overlay.clipUpperRight.x, overlay.clipUpperRight.y, overlay.clipUpperRight.z, overlay.clipUpperRight.w, overlay.uvUpperRight.x, overlay.uvUpperRight.y };
  350. float[] lowerRight = { overlay.clipLowerRight.x, overlay.clipLowerRight.y, overlay.clipLowerRight.z, overlay.clipLowerRight.w, overlay.uvLowerRight.x, overlay.uvLowerRight.y };
  351. GSXRSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  352. layerCount++;
  353. }
  354. for (i = layerCount; i < GSXRManager.RenderLayersMax; i++)
  355. {
  356. GSXRSetupLayerData(i, 0, 0, 0, 0);
  357. }
  358. GSXRSubmitFrameEventData(frameIndex, fieldOfView, frameType);
  359. IssueEvent (RenderEvent.SubmitFrame);
  360. }
  361. public override void Shutdown()
  362. {
  363. IssueEvent (RenderEvent.Shutdown);
  364. base.Shutdown();
  365. }
  366. public override bool PollEvent(ref GSXRManager.SvrEvent frameEvent)
  367. {
  368. uint[] dataBuffer = new uint[2];
  369. int dataCount = Marshal.SizeOf(frameEvent.eventData) / sizeof(uint);
  370. int eventType = 0;
  371. bool isEvent = GSXRPollEvent(ref eventType, ref frameEvent.deviceId, ref frameEvent.eventTimeStamp, dataCount, dataBuffer);
  372. frameEvent.eventType = (GSXRManager.svrEventType)(eventType);
  373. switch (frameEvent.eventType)
  374. {
  375. case GSXRManager.svrEventType.kEventThermal:
  376. //Debug.LogFormat("PollEvent: data {0} {1}", dataBuffer[0], dataBuffer[1]);
  377. frameEvent.eventData.thermal.zone = (GSXRManager.svrThermalZone)dataBuffer[0];
  378. frameEvent.eventData.thermal.level = (GSXRManager.svrThermalLevel)dataBuffer[1];
  379. break;
  380. }
  381. return isEvent;
  382. }
  383. //---------------------------------------------------------------------------------------------
  384. //Controller Apis
  385. //---------------------------------------------------------------------------------------------
  386. /// <summary>
  387. /// Controllers the start tracking.
  388. /// </summary>
  389. /// <returns>The start tracking.</returns>
  390. /// <param name="desc">Desc.</param>
  391. //---------------------------------------------------------------------------------------------
  392. public override int ControllerStartTracking(string desc)
  393. {
  394. return GSXRControllerStartTracking(desc);
  395. }
  396. /// <summary>
  397. /// Controllers the stop tracking.
  398. /// </summary>
  399. /// <param name="handle">Handle.</param>
  400. //---------------------------------------------------------------------------------------------
  401. public override void ControllerStopTracking(int handle)
  402. {
  403. GSXRControllerStopTracking(handle);
  404. }
  405. /// <summary>
  406. /// Dumps the state.
  407. /// </summary>
  408. /// <param name="state">State.</param>
  409. //---------------------------------------------------------------------------------------------
  410. private void dumpState(GSXRControllerState state)
  411. {
  412. String s = "{" + state.rotation + "}\n";
  413. s += "[" + state.position + "]\n";
  414. s += "<" + state.timestamp + ">\n";
  415. Debug.Log (s);
  416. }
  417. /// <summary>
  418. /// Controllers the state of the get.
  419. /// </summary>
  420. /// <returns>The get state.</returns>
  421. /// <param name="handle">Handle.</param>
  422. //---------------------------------------------------------------------------------------------
  423. public override GSXRControllerState ControllerGetState(int handle, int space)
  424. {
  425. GSXRControllerState state = new GSXRControllerState();
  426. GSXRControllerGetState (handle, space, ref state);
  427. //dumpState (state);
  428. return state;
  429. }
  430. /// <summary>
  431. /// Controllers the send event.
  432. /// </summary>
  433. /// <param name="handle">Handle.</param>
  434. /// <param name="what">What.</param>
  435. /// <param name="arg1">Arg1.</param>
  436. /// <param name="arg2">Arg2.</param>
  437. //---------------------------------------------------------------------------------------------
  438. public override void ControllerSendMessage(int handle, GSXRController.svrControllerMessageType what, int arg1, int arg2)
  439. {
  440. GSXRControllerSendMessage (handle, (int)what, arg1, arg2);
  441. }
  442. /// <summary>
  443. /// Controllers the query.
  444. /// </summary>
  445. /// <returns>The query.</returns>
  446. /// <param name="handle">Handle.</param>
  447. /// <param name="what">What.</param>
  448. /// <param name="mem">Mem.</param>
  449. /// <param name="size">Size.</param>
  450. //---------------------------------------------------------------------------------------------
  451. public override object ControllerQuery(int handle, GSXRController.svrControllerQueryType what)
  452. {
  453. int memorySize = 0;
  454. IntPtr memory = IntPtr.Zero;
  455. object result = null;
  456. System.Type typeOfObject = null;
  457. switch(what)
  458. {
  459. case GSXRController.svrControllerQueryType.kControllerBatteryRemaining:
  460. {
  461. typeOfObject = typeof(int);
  462. memorySize = System.Runtime.InteropServices.Marshal.SizeOf (typeOfObject);
  463. memory = System.Runtime.InteropServices.Marshal.AllocHGlobal (memorySize);
  464. }
  465. break;
  466. case GSXRController.svrControllerQueryType.kControllerControllerCaps:
  467. {
  468. typeOfObject = typeof(SvrControllerCaps);
  469. memorySize = System.Runtime.InteropServices.Marshal.SizeOf (typeOfObject);
  470. memory = System.Runtime.InteropServices.Marshal.AllocHGlobal (memorySize);
  471. }
  472. break;
  473. }
  474. int writtenBytes = GSXRControllerQuery (handle, (int)what, memory, memorySize);
  475. if (memorySize == writtenBytes) {
  476. result = System.Runtime.InteropServices.Marshal.PtrToStructure (memory, typeOfObject);
  477. }
  478. if (memory != IntPtr.Zero) {
  479. Marshal.FreeHGlobal (memory);
  480. }
  481. return result;
  482. }
  483. #region Controller
  484. [DllImport(DLLName)]
  485. public static extern bool GSXRIsSupportController();
  486. [DllImport(DLLName)]
  487. public static extern int GSXRControllerMode();
  488. [DllImport(DLLName)]
  489. public static extern int GSXRGetControllerNum();
  490. [DllImport(DLLName)]
  491. public static extern void GSXRSetControllerKeyEventCallback(OnKeyEvent _event);
  492. [DllImport(DLLName)]
  493. public static extern void GSXRSetControllerKeyTouchEventCallback(OnKeyTouchEvent _event);
  494. [DllImport(DLLName)]
  495. public static extern void GSXRSetControllerRockerCallback(OnTouchEvent _event);
  496. [DllImport(DLLName)]
  497. public static extern void GSXRSetControllerTriggerCallback(OnHallEvent _event);
  498. [DllImport(DLLName)]
  499. public static extern void GSXRSetControllerChargingEventCallback(OnChargingEvent _event);
  500. [DllImport(DLLName)]
  501. public static extern void GSXRSetControllerBatteryEventCallback(OnBatteryEvent _event);
  502. [DllImport(DLLName)]
  503. public static extern void GSXRSetControllerConnectEventCallback(OnConnectEvent _event);
  504. [DllImport(DLLName)]
  505. public static extern int GSXRGetControllerBattery(int lr);
  506. [DllImport(DLLName)]
  507. public static extern int GSXRGetControllerVersion(int lr);
  508. [DllImport(DLLName)]
  509. public static extern int GSXRGetControllerList(int lr);
  510. [DllImport(DLLName)]
  511. public static extern bool GSXRIsControllerConnect(int lr);
  512. [DllImport(DLLName)]
  513. public static extern void GSXRSetControllerLed(int enable);
  514. [DllImport(DLLName)]
  515. public static extern void GSXRSetControllerVibrate(int value);
  516. [DllImport(DLLName)]
  517. public static extern int GSXRGetControllerPosture(float[] outOrientationArray, int lr);
  518. //[DllImport(DLLName)]
  519. //public static extern int ScFetch3dofHandShank(float[] outOrientationArray, int lr);
  520. //[DllImport(DLLName)]
  521. //public static extern int ScFetch6dofHandShank(float[] outOrientationArray, int lr);
  522. public override bool GSXR_Is_SupportController() { return GSXRIsSupportController(); }
  523. public override int GSXR_Get_ControllerMode() { return GSXRControllerMode(); }
  524. public override int GSXR_Get_ControllerNum() { return GSXRGetControllerNum(); }
  525. public override void GSXR_Set_ControllerKeyEventCallback(OnKeyEvent _event) { GSXRSetControllerKeyEventCallback(_event); }
  526. public override void GSXR_Set_ControllerKeyTouchEventCallback(OnKeyTouchEvent _event) { GSXRSetControllerKeyTouchEventCallback(_event); }
  527. public override void GSXR_Set_ControllerRockerCallback(OnTouchEvent _event) { GSXRSetControllerRockerCallback(_event); }
  528. public override void GSXR_Set_ControllerHallCallback(OnHallEvent _event) { GSXRSetControllerTriggerCallback(_event); }
  529. public override void GSXR_Set_ControllerChargingEventCallback(OnChargingEvent _event) { GSXRSetControllerChargingEventCallback(_event); }
  530. public override void GSXR_Set_ControllerBatteryEventCallback(OnBatteryEvent _event) { GSXRSetControllerBatteryEventCallback(_event); }
  531. public override void GSXR_Set_ControllerConnectEventCallback(OnConnectEvent _event) { GSXRSetControllerConnectEventCallback(_event); }
  532. public override int GSXR_Get_ControllerBattery(int lr) { return GSXRGetControllerBattery(lr); }
  533. public override int GSXR_Get_ControllerVersion(int lr) { return GSXRGetControllerVersion(lr); }
  534. public override int GSXR_Get_ControllerList(int lr) { return GSXRGetControllerList(lr); }
  535. public override bool GSXR_Is_ControllerConnect(int lr) { return GSXRIsControllerConnect(lr); }
  536. public override void GSXR_Set_ControllerLed(int lr) { GSXRSetControllerLed(lr); }
  537. public override void GSXR_Set_ControllerVibrate(int lr) { GSXRSetControllerVibrate(lr); }
  538. public override int GSXR_Get_ControllerPosture(float[] outOrientationArray, int lr) {return GSXRGetControllerPosture(outOrientationArray, lr);}
  539. #endregion Controller
  540. #region HandTracking
  541. [DllImport(DLLName)]
  542. private static extern bool GSXRIsSupportHandTracking();
  543. [DllImport(DLLName)]
  544. protected static extern void GSXRStartGesture(Action<int> func);
  545. [DllImport(DLLName)]
  546. public static extern void GSXRStopGesture();
  547. [DllImport(DLLName)]
  548. private static extern void GSXRSetGestureCallback(Action<int> func);
  549. [DllImport(DLLName)]
  550. private static extern int GSXRGetHandTrackingData(float[] model, float[] pose);
  551. //for test
  552. [DllImport(DLLName)]
  553. private static extern int GSXRSetGestureData(float[] model, float[] pose);
  554. [DllImport(DLLName)]
  555. private static extern int ScHANDTRACK_GetGestureWithIdx(ref ulong index, float[] model, float[] pose);
  556. [DllImport(DLLName)]
  557. private static extern int GSXRSetGestureModelCallback(Action func);
  558. [DllImport(DLLName)]
  559. protected static extern void GSXRSetHandTrackingLowPowerWarningCallback(Action<int> func);
  560. public override bool GSXR_Is_SupportHandTracking() {
  561. return GSXRIsSupportHandTracking();
  562. }
  563. public override void GSXR_StartHandTracking(Action<int> func) {
  564. GSXRStartGesture(func);
  565. }
  566. public override void GSXR_StopHandTracking() {
  567. GSXRStopGesture();
  568. }
  569. public override void GSXR_Get_HandTrackingData(float[] mode, float[] pose) {
  570. GSXRGetHandTrackingData(mode, pose);
  571. }
  572. public override int GSXR_Set_HandTrackingData(float[] mode, float[] pose) {
  573. return GSXRSetGestureData(mode, pose);
  574. }
  575. public override int GSXR_Get_HandTrackingGestureIdx(ref ulong index, float[] model, float[] pose) {
  576. return ScHANDTRACK_GetGestureWithIdx(ref index, model, pose);
  577. }
  578. public override void GSXR_Set_HandTrackingCallBack(Action<int> callback) {
  579. GSXRSetGestureCallback(callback);
  580. }
  581. public override int GSXR_Set_HandTrackingModelDataCallBack(Action callback) {
  582. return GSXRSetGestureModelCallback(callback);
  583. }
  584. public override void GSXR_Set_HandTrackingLowPowerWarningCallback(Action<int> func) {
  585. GSXRSetHandTrackingLowPowerWarningCallback(func);
  586. }
  587. #endregion HandTracking
  588. #region Deflection
  589. [DllImport(DLLName)]
  590. private static extern float GSXRFetchDeflection();
  591. [DllImport(DLLName)]
  592. private static extern void GSXRUpdateRelativeDeflection(int deflection);
  593. public override float GSXR_Get_Deflection() {
  594. return GSXRFetchDeflection();
  595. }
  596. public override void GSXR_Set_RelativeDeflection(int deflection) {
  597. GSXRUpdateRelativeDeflection(deflection);
  598. }
  599. #endregion Deflection
  600. #region PointCloud & Map
  601. [DllImport(DLLName)]
  602. private static extern int GSXRGetPointCloudData(ref int dataNum,ref ulong dataTimestamp,float[] dataArray);
  603. [DllImport(DLLName)]
  604. private static extern int GSXRGetOfflineMapRelocState();
  605. /// <summary>
  606. /// 将地图信息保存在path里,是ply格式文件,存的是点云信息
  607. /// </summary>
  608. /// <param name="path"></param>
  609. /// <returns></returns>
  610. [DllImport(DLLName)]
  611. private static extern int GSXRResaveMap(string path);
  612. [DllImport(DLLName)]
  613. private static extern void GSXRSaveMap();
  614. [DllImport(DLLName)]
  615. private static extern int ScGetGnss(ref double dt, float[] gnss);
  616. /// <summary>
  617. /// Get the identified plane
  618. /// </summary>
  619. /// <returns></returns>
  620. [DllImport(DLLName)]
  621. private static extern int GSXRGetPanelNum();
  622. /// <summary>
  623. /// Get the plane Infomations
  624. /// 1:id
  625. /// 2:nPoints
  626. /// 3: 20个点的坐标 :如果没20个点,后面为空
  627. /// 4: createPoint 平面顶点
  628. /// 5: normal 法向量
  629. /// </summary>
  630. /// <param name="info"></param>
  631. /// <returns></returns>
  632. [DllImport(DLLName)]
  633. private static extern int GSXRGetPlaneData(float[] info);
  634. public override int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray) {
  635. return GSXRGetPointCloudData(ref dataNum, ref dataTimestamp, dataArray);
  636. }
  637. public override int GSXR_Get_OfflineMapRelocState() {
  638. return GSXRGetOfflineMapRelocState();
  639. }
  640. public override int GSXR_ResaveMap(string path) {
  641. return GSXRResaveMap(path);
  642. }
  643. public override void GSXR_SaveMap() {
  644. GSXRSaveMap();
  645. }
  646. public override int GSXR_Get_Gnss(ref double dt, float[] gnss) {
  647. return ScGetGnss(ref dt, gnss);
  648. }
  649. public override int GSXR_Get_PanelNum() {
  650. return GSXRGetPanelNum();
  651. }
  652. public override int GSXR_Get_PanelInfo(float[] info) {
  653. return GSXRGetPlaneData(info);
  654. }
  655. #endregion PointCloud & Map
  656. #region Grey Camera Data
  657. [DllImport(DLLName)]
  658. public static extern void GSXRGetLatestCameraFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  659. byte[] outFrameData, float[] outTRDataArray);
  660. [DllImport(DLLName)]
  661. public static extern void GSXRGetLatestCameraFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  662. byte[] outFrameData, float[] outTRDataArray);
  663. public override void GSXR_Get_LatestQVRCameraFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  664. byte[] outFrameData, float[] outTRDataArray) {
  665. GSXRGetLatestCameraFrameData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outFrameData, outTRDataArray);
  666. if (outBUdate == false) {
  667. Debug.LogError("Error: Please Check Svrconfig prop: gUseQVRCamera = true");
  668. }
  669. }
  670. public override void GSXR_Get_LatestQVRCameraFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  671. byte[] outFrameData, float[] outTRDataArray) {
  672. GSXRGetLatestCameraFrameDataNoTransform(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outFrameData, outTRDataArray);
  673. if (outBUdate == false) {
  674. Debug.LogError("Error: Please Check Svrconfig prop: gUseQVRCamera = true");
  675. }
  676. }
  677. [DllImport(DLLName)]
  678. private static extern int GSXRGetLatestCameraBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData);
  679. public override int GSXR_Get_LatestQVRCameraBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData) {
  680. return GSXRGetLatestCameraBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  681. }
  682. #endregion Grey Camera Data
  683. #region Optics Calibration
  684. [DllImport(DLLName)]
  685. private static extern bool GSXRIsSupportOpticsCalibration();
  686. public override bool GSXR_Is_SupportOpticsCalibration() {
  687. return GSXRIsSupportOpticsCalibration();
  688. }
  689. [DllImport(DLLName)]
  690. private static extern int GSXRGetTransformMatrix(ref bool outBLoaded, float[] outTransformArray);
  691. public override int GSXR_Get_TransformMatrix(ref bool outBLoaded, float[] outTransformArray) {
  692. return GSXRGetTransformMatrix(ref outBLoaded, outTransformArray);
  693. }
  694. [DllImport(DLLName)]
  695. private static extern int GSXRGetLatestEyeMatrices(float[] outLeftEyeMatrix,
  696. float[] outRightEyeMatrix,
  697. float[] outT,
  698. float[] outR,
  699. int frameIndex,
  700. bool isMultiThreadedRender);
  701. public override int GSXR_Get_LatestEyeMatrices(float[] outLeftEyeMatrix, float[] outRightEyeMatrix, float[] outT, float[] outR, int frameIndex, bool isMultiThreadedRender) {
  702. return GSXRGetLatestEyeMatrices(outLeftEyeMatrix, outRightEyeMatrix, outT, outR, frameIndex, isMultiThreadedRender);
  703. }
  704. #endregion Optics Calibration
  705. #region USBDisconnect
  706. [DllImport(DLLName)]
  707. public static extern void Sc_setAppExitCallback(Action callBack);
  708. public override void SetGlassDisconnectedCallBack(Action callBack) {
  709. if (API_Module_Device.CurrentAndroid.type == SC.XR.Unity.Module_Device.AndroidDeviceType.HONGHU) {
  710. Sc_setAppExitCallback(callBack);
  711. } else if (API_Module_Device.CurrentAndroid.type == SC.XR.Unity.Module_Device.AndroidDeviceType.MV201) {
  712. }
  713. }
  714. #endregion
  715. #region luncher
  716. [DllImport(DLLName)]
  717. public static extern int ScInitLayer();
  718. [DllImport(DLLName)]
  719. public static extern int ScStartLayerRendering();
  720. [DllImport(DLLName)]
  721. public static extern int ScGetAllLayersData(ref GSXRManager.SCAllLayers outAllLayers);
  722. [DllImport(DLLName)]
  723. public static extern int ScEndLayerRendering(ref GSXRManager.SCAllLayers allLayers);
  724. [DllImport(DLLName)]
  725. public static extern int ScDestroyLayer();
  726. [DllImport(DLLName)]
  727. public static extern int ScUpdateModelMatrix(UInt32 layerId, float[] modelMatrixArray);
  728. [DllImport(DLLName)]
  729. public static extern int ScSendActionBarCMD(UInt32 layerId, int cmd);
  730. [DllImport(DLLName)]
  731. public static extern int ScInjectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y);
  732. public override int initLayer() {
  733. return ScInitLayer();
  734. }
  735. public override int startLayerRendering() {
  736. return ScStartLayerRendering();
  737. }
  738. public override int getAllLayersData(ref GSXRManager.SCAllLayers outAllLayers) {
  739. return ScGetAllLayersData(ref outAllLayers);
  740. }
  741. public override int endLayerRendering(ref GSXRManager.SCAllLayers allLayers) {
  742. return ScEndLayerRendering(ref allLayers);
  743. }
  744. public override int destroyLayer() {
  745. return ScDestroyLayer();
  746. }
  747. public override int updateModelMatrix(UInt32 layerId, float[] modelMatrixArray) {
  748. return ScUpdateModelMatrix(layerId, modelMatrixArray);
  749. }
  750. public override int sendActionBarCMD(UInt32 layerId, int cmd) {
  751. return ScSendActionBarCMD(layerId, cmd);
  752. }
  753. public override int injectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y) {
  754. return ScInjectMotionEvent(layerId, displayID, action, x, y);
  755. }
  756. #endregion
  757. #region Device
  758. [DllImport(DLLName)]
  759. private static extern int GSXRGetXRType();
  760. [DllImport(DLLName)]
  761. private static extern void GSXRGetDeviceName(ref string deviceName);
  762. public override XRType GSXR_Get_XRType() { return (XRType)GSXRGetXRType(); }
  763. public override void GSXR_Get_DeviceName(ref string deviceName) { GSXRGetDeviceName(ref deviceName); }
  764. #endregion
  765. }