SvrPluginAndroid.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using SC.XR.Unity;
  6. class SvrPluginAndroid : GSXRPlugin
  7. {
  8. private const string DLLName = "svrplugin";
  9. [DllImport(DLLName)]
  10. private static extern IntPtr GetRenderEventFunc();
  11. [DllImport(DLLName)]
  12. private static extern bool SvrIsInitialized();
  13. [DllImport(DLLName)]
  14. private static extern bool SvrIsRunning();
  15. [DllImport(DLLName)]
  16. private static extern bool SvrCanBeginVR();
  17. [DllImport(DLLName)]
  18. private static extern void SvrInitializeEventData(IntPtr activity);
  19. [DllImport(DLLName)]
  20. private static extern void SvrSubmitFrameEventData(int frameIndex, float fieldOfView, int frameType);
  21. [DllImport(DLLName)]
  22. private static extern void SvrSetupLayerCoords(int layerIndex, float[] lowerLeft, float[] lowerRight, float[] upperLeft, float[] upperRight);
  23. [DllImport(DLLName)]
  24. private static extern void SvrSetupLayerData(int layerIndex, int sideMask, int textureId, int textureType, int layerFlags);
  25. [DllImport(DLLName)]
  26. private static extern void SvrSetTrackingModeEventData(int mode);
  27. [DllImport(DLLName)]
  28. private static extern void SvrSetPerformanceLevelsEventData(int newCpuPerfLevel, int newGpuPerfLevel);
  29. [DllImport(DLLName)]
  30. private static extern void SvrSetEyeEventData(int sideMask, int layerMask);
  31. [DllImport(DLLName)]
  32. private static extern void SvrSetColorSpace(int colorSpace);
  33. [DllImport(DLLName)]
  34. private static extern void SvrSetFrameOption(uint frameOption);
  35. [DllImport(DLLName)]
  36. private static extern void SvrUnsetFrameOption(uint frameOption);
  37. [DllImport(DLLName)]
  38. private static extern void SvrSetVSyncCount(int vSyncCount);
  39. [DllImport(DLLName)]
  40. private static extern int SvrGetPredictedPose(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 SvrGetEyePose(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 SvrRecenterTrackingPose();
  76. [DllImport(DLLName)]
  77. private static extern int SvrGetTrackingMode();
  78. [DllImport(DLLName)]
  79. private static extern void SvrGetDeviceInfo(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 SvrSetFrameOffset(float[] delta);
  91. [DllImport(DLLName)]
  92. private static extern void SvrSetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum);
  93. [DllImport(DLLName)]
  94. private static extern bool SvrPollEvent(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 SvrControllerStartTracking(string desc);
  100. [DllImport(DLLName)]
  101. private static extern void SvrControllerStopTracking(int handle);
  102. [DllImport(DLLName)]
  103. private static extern void SvrControllerGetState(int handle, int space, ref GSXRControllerState state);
  104. [DllImport(DLLName)]
  105. private static extern void SvrControllerSendMessage (int handle, int what, int arg1, int arg2);
  106. [DllImport(DLLName)]
  107. private static extern int SvrControllerQuery (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 SvrPluginAndroid Create()
  123. {
  124. if(Application.isEditor)
  125. {
  126. Debug.LogError("GSXRPlugin not supported in unity editor!");
  127. throw new InvalidOperationException();
  128. }
  129. return new SvrPluginAndroid ();
  130. }
  131. private SvrPluginAndroid() {}
  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 SvrIsInitialized(); }
  138. public override bool IsRunning() { return SvrIsRunning(); }
  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. SvrInitializeEventData(activity.GetRawObject());
  147. }
  148. IssueEvent(RenderEvent.Initialize);
  149. yield return new WaitUntil (() => SvrIsInitialized () == 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(() => SvrIsRunning() == 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. SvrSetupLayerCoords(-1, lowerLeft, lowerRight, upperLeft, upperRight); // Layers/All
  163. SvrSetPerformanceLevelsEventData(cpuPerfLevel, gpuPerfLevel);
  164. ColorSpace space = QualitySettings.activeColorSpace;
  165. if(space == ColorSpace.Gamma)
  166. {
  167. // 0 == kColorSpaceLinear from svrApi.h
  168. SvrSetColorSpace(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. SvrSetColorSpace(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(() => SvrCanBeginVR() == true);
  180. IssueEvent (RenderEvent.BeginVr);
  181. //yield return new WaitUntil(() => SvrIsRunning() == 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. SvrSetFrameOffset(frameDelta); // Enabled for foveation head orientation delta
  192. SvrSetEyeEventData(sideMask, 0);
  193. IssueEvent (RenderEvent.BeginEye);
  194. }
  195. public override void EndEye(int sideMask, int layerMask)
  196. {
  197. SvrSetEyeEventData(sideMask, layerMask);
  198. IssueEvent(RenderEvent.EndEye);
  199. }
  200. public override void SetTrackingMode(int modeMask)
  201. {
  202. SvrSetTrackingModeEventData(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. SvrSetFoveationParameters(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 SvrGetTrackingMode();
  216. }
  217. public override void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel)
  218. {
  219. SvrSetPerformanceLevelsEventData((int)newCpuPerfLevel, (int)newGpuPerfLevel);
  220. IssueEvent (RenderEvent.SetPerformanceLevels);
  221. }
  222. public override void SetFrameOption(FrameOption frameOption)
  223. {
  224. SvrSetFrameOption((uint)frameOption);
  225. }
  226. public override void UnsetFrameOption(FrameOption frameOption)
  227. {
  228. SvrUnsetFrameOption((uint)frameOption);
  229. }
  230. public override void SetVSyncCount(int vSyncCount)
  231. {
  232. SvrSetVSyncCount(vSyncCount);
  233. }
  234. public override bool RecenterTracking()
  235. {
  236. //IssueEvent (RenderEvent.RecenterTracking);
  237. return SvrRecenterTrackingPose();
  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 = SvrGetPredictedPose(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 = SvrGetPredictedPose(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 = SvrGetEyePose(
  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. SvrGetDeviceInfo (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. SvrSetupLayerData(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. SvrSetupLayerCoords(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. SvrSetupLayerData(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. SvrSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  352. layerCount++;
  353. }
  354. for (i = layerCount; i < GSXRManager.RenderLayersMax; i++)
  355. {
  356. SvrSetupLayerData(i, 0, 0, 0, 0);
  357. }
  358. SvrSubmitFrameEventData(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 = SvrPollEvent(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 SvrControllerStartTracking(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. SvrControllerStopTracking(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. SvrControllerGetState (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. SvrControllerSendMessage (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 = SvrControllerQuery (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 void ScHandShank_SetKeyEventCallback(OnKeyEvent _event);
  486. [DllImport(DLLName)]
  487. public static extern void ScHandShank_SetKeyTouchEventCallback(OnKeyTouchEvent _event);
  488. [DllImport(DLLName)]
  489. public static extern void ScHandShank_SetTouchEventCallback(OnTouchEvent _event);
  490. [DllImport(DLLName)]
  491. public static extern void ScHandShank_SetHallEventCallback(OnHallEvent _event);
  492. [DllImport(DLLName)]
  493. public static extern void ScHandShank_SetChargingEventCallback(OnChargingEvent _event);
  494. [DllImport(DLLName)]
  495. public static extern void ScHandShank_SetBatteryEventCallback(OnBatteryEvent _event);
  496. [DllImport(DLLName)]
  497. public static extern void ScHandShank_SetConnectEventCallback(OnConnectEvent _event);
  498. [DllImport(DLLName)]
  499. public static extern int ScHandShank_GetBattery(int lr);
  500. [DllImport(DLLName)]
  501. public static extern int ScHandShank_GetVersion(int lr);
  502. [DllImport(DLLName)]
  503. public static extern int ScHandShank_Getbond(int lr);
  504. [DllImport(DLLName)]
  505. public static extern bool ScHandShank_GetConnectState(int lr);
  506. [DllImport(DLLName)]
  507. public static extern int ScFetch3dofHandShank(float[] outOrientationArray, int lr);
  508. [DllImport(DLLName)]
  509. public static extern int ScFetch6dofHandShank(float[] outOrientationArray, int lr);
  510. public override bool GSXR_Is_SupportController() { return true;}
  511. public override int GSXR_Get_ControllerMode() {return 6;}
  512. public override int GSXR_Get_ControllerNum() {return 2;}
  513. public override void GSXR_Set_ControllerKeyEventCallback(OnKeyEvent _event) { ScHandShank_SetKeyEventCallback(_event); }
  514. public override void GSXR_Set_ControllerKeyTouchEventCallback(OnKeyTouchEvent _event) { ScHandShank_SetKeyTouchEventCallback(_event); }
  515. public override void GSXR_Set_ControllerRockerCallback(OnTouchEvent _event) { ScHandShank_SetTouchEventCallback(_event); }
  516. public override void GSXR_Set_ControllerHallCallback(OnHallEvent _event) { ScHandShank_SetHallEventCallback(_event); }
  517. public override void GSXR_Set_ControllerChargingEventCallback(OnChargingEvent _event) { ScHandShank_SetChargingEventCallback(_event); }
  518. public override void GSXR_Set_ControllerBatteryEventCallback(OnBatteryEvent _event) { ScHandShank_SetBatteryEventCallback(_event); }
  519. public override void GSXR_Set_ControllerConnectEventCallback(OnConnectEvent _event) { ScHandShank_SetConnectEventCallback(_event); }
  520. public override int GSXR_Get_ControllerBattery(int lr) { return ScHandShank_GetBattery(lr); }
  521. public override int GSXR_Get_ControllerVersion(int lr) { return ScHandShank_Getbond(lr); }
  522. public override int GSXR_Get_ControllerList(int lr) { return ScHandShank_Getbond(lr); }
  523. public override bool GSXR_Is_ControllerConnect(int lr) { return ScHandShank_GetConnectState(lr); }
  524. public override int Fetch3dofHandShank(float[] outOrientationArray,int lr) { return ScFetch3dofHandShank(outOrientationArray,lr); }
  525. public override int Fetch6dofHandShank(float[] outOrientationArray,int lr) { return ScFetch6dofHandShank(outOrientationArray,lr); }
  526. #endregion Controller
  527. #region HandTracking
  528. [DllImport(DLLName)]
  529. protected static extern void ScHANDTRACK_Start(Action<int> func);
  530. [DllImport(DLLName)]
  531. public static extern void ScHANDTRACK_Stop();
  532. [DllImport(DLLName)]
  533. private static extern void ScHANDTRACK_SetGestureCallback(Action<int> func);
  534. [DllImport(DLLName)]
  535. private static extern int ScHANDTRACK_GetGesture(float[] model, float[] pose);
  536. //for test
  537. [DllImport(DLLName)]
  538. private static extern int ScHANDTRACK_SetGestureData(float[] model, float[] pose);
  539. [DllImport(DLLName)]
  540. private static extern int ScHANDTRACK_GetGestureWithIdx(ref UInt64 index, float[] model, float[] pose);
  541. [DllImport(DLLName)]
  542. private static extern int ScHANDTRACK_SetGestureModelDataCallback(Action func);
  543. [DllImport(DLLName)]
  544. protected static extern void ScHANDTRACK_SetLowPowerWarningCallback(Action<int> func);
  545. public override bool GSXR_Is_SupportHandTracking() {
  546. return true;
  547. }
  548. public override void GSXR_StartHandTracking(Action<int> func) {
  549. ScHANDTRACK_Start(func);
  550. }
  551. public override void GSXR_StopHandTracking() {
  552. ScHANDTRACK_Stop();
  553. }
  554. public override void GSXR_Get_HandTrackingData(float[] mode, float[] pose) {
  555. ScHANDTRACK_GetGesture(mode, pose);
  556. }
  557. public override void GSXR_Set_HandTrackingCallBack(Action<int> callback) {
  558. ScHANDTRACK_SetGestureCallback(callback);
  559. }
  560. public override int GSXR_Get_HandTrackingGestureIdx(ref ulong index, float[] model, float[] pose) {
  561. return ScHANDTRACK_GetGestureWithIdx(ref index, model, pose);
  562. }
  563. public override int GSXR_Set_HandTrackingData(float[] mode, float[] pose) {
  564. return ScHANDTRACK_SetGestureData(mode, pose);
  565. }
  566. public override int GSXR_Set_HandTrackingModelDataCallBack(Action callback) {
  567. return ScHANDTRACK_SetGestureModelDataCallback(callback);
  568. }
  569. #endregion HandTracking
  570. #region Grey Camera Data
  571. [DllImport(DLLName)]
  572. public static extern void SvrGetLatestCameraFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  573. byte[] outFrameData, float[] outTRDataArray);
  574. [DllImport(DLLName)]
  575. public static extern void SvrGetLatestCameraFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  576. byte[] outFrameData, float[] outTRDataArray);
  577. public override void GSXR_Get_LatestQVRCameraFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  578. byte[] outFrameData, float[] outTRDataArray) {
  579. SvrGetLatestCameraFrameData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outFrameData, outTRDataArray);
  580. if(outBUdate == false) {
  581. Debug.LogError("Error: Please Check Svrconfig prop: gUseQVRCamera = true");
  582. }
  583. }
  584. public override void GSXR_Get_LatestQVRCameraFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  585. byte[] outFrameData, float[] outTRDataArray) {
  586. SvrGetLatestCameraFrameDataNoTransform(ref outBUdate,ref outCurrFrameIndex,ref outFrameExposureNano, outFrameData, outTRDataArray);
  587. if(outBUdate == false) {
  588. Debug.LogError("Error: Please Check Svrconfig prop: gUseQVRCamera = true");
  589. }
  590. }
  591. [DllImport(DLLName)]
  592. private static extern int SvrGetLatestCameraBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData);
  593. public override int GSXR_Get_LatestQVRCameraBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData) {
  594. return SvrGetLatestCameraBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  595. }
  596. #endregion Grey Camera Data
  597. #region Optics Calibration
  598. public override bool GSXR_Is_SupportOpticsCalibration() {
  599. return true;
  600. }
  601. [DllImport(DLLName)]
  602. private static extern int SvrGetTransformMatrix(ref bool outBLoaded, float[] outTransformArray);
  603. public override int GSXR_Get_TransformMatrix(ref bool outBLoaded, float[] outTransformArray) {
  604. return SvrGetTransformMatrix(ref outBLoaded, outTransformArray);
  605. }
  606. [DllImport(DLLName)]
  607. private static extern int SvrGetLatestEyeMatrices(float[] outLeftEyeMatrix,
  608. float[] outRightEyeMatrix,
  609. float[] outT,
  610. float[] outR,
  611. int frameIndex,
  612. bool isMultiThreadedRender);
  613. public override int GSXR_Get_LatestEyeMatrices(float[] outLeftEyeMatrix, float[] outRightEyeMatrix, float[] outT, float[] outR, int frameIndex, bool isMultiThreadedRender) {
  614. return SvrGetLatestEyeMatrices(outLeftEyeMatrix, outRightEyeMatrix, outT, outR, frameIndex, isMultiThreadedRender);
  615. }
  616. #endregion Optics Calibration
  617. #region USBDisconnect
  618. [DllImport(DLLName)]
  619. public static extern void Sc_setAppExitCallback(Action callBack);
  620. public override void SetGlassDisconnectedCallBack(Action callBack) {
  621. if (API_Module_Device.CurrentAndroid.type == SC.XR.Unity.Module_Device.AndroidDeviceType.HONGHU) {
  622. Sc_setAppExitCallback(callBack);
  623. } else if (API_Module_Device.CurrentAndroid.type == SC.XR.Unity.Module_Device.AndroidDeviceType.MV201) {
  624. }
  625. }
  626. #endregion
  627. #region luncher
  628. [DllImport(DLLName)]
  629. public static extern int ScInitLayer();
  630. [DllImport(DLLName)]
  631. public static extern int ScStartLayerRendering();
  632. [DllImport(DLLName)]
  633. public static extern int ScGetAllLayersData(ref GSXRManager.SCAllLayers outAllLayers);
  634. [DllImport(DLLName)]
  635. public static extern int ScEndLayerRendering(ref GSXRManager.SCAllLayers allLayers);
  636. [DllImport(DLLName)]
  637. public static extern int ScDestroyLayer();
  638. [DllImport(DLLName)]
  639. public static extern int ScUpdateModelMatrix(UInt32 layerId, float[] modelMatrixArray);
  640. [DllImport(DLLName)]
  641. public static extern int ScSendActionBarCMD(UInt32 layerId, int cmd);
  642. [DllImport(DLLName)]
  643. public static extern int ScInjectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y);
  644. public override int initLayer() {
  645. return ScInitLayer();
  646. }
  647. public override int startLayerRendering() {
  648. return ScStartLayerRendering();
  649. }
  650. public override int getAllLayersData(ref GSXRManager.SCAllLayers outAllLayers) {
  651. return ScGetAllLayersData(ref outAllLayers);
  652. }
  653. public override int endLayerRendering(ref GSXRManager.SCAllLayers allLayers) {
  654. return ScEndLayerRendering(ref allLayers);
  655. }
  656. public override int destroyLayer() {
  657. return ScDestroyLayer();
  658. }
  659. public override int updateModelMatrix(UInt32 layerId, float[] modelMatrixArray) {
  660. return ScUpdateModelMatrix(layerId, modelMatrixArray);
  661. }
  662. public override int sendActionBarCMD(UInt32 layerId, int cmd) {
  663. return ScSendActionBarCMD(layerId, cmd);
  664. }
  665. public override int injectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y) {
  666. return ScInjectMotionEvent(layerId, displayID, action, x, y);
  667. }
  668. #endregion
  669. #region Device
  670. #endregion
  671. }